em28xx-core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 = 0;
  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, __FUNCTION__ , ##arg); } while (0)
  33. static unsigned int reg_debug = 0;
  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, __FUNCTION__ , ##arg); } while (0)
  40. static unsigned int isoc_debug = 0;
  41. module_param(isoc_debug,int,0644);
  42. MODULE_PARM_DESC(isoc_debug,"enable debug messages [isoc transfers]");
  43. #define em28xx_isocdbg(fmt, arg...) do {\
  44. if (isoc_debug) \
  45. printk(KERN_INFO "%s %s :"fmt, \
  46. dev->name, __FUNCTION__ , ##arg); } while (0)
  47. static int alt = EM28XX_PINOUT;
  48. module_param(alt, int, 0644);
  49. MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
  50. /*
  51. * em28xx_request_buffers()
  52. * allocate a number of buffers
  53. */
  54. u32 em28xx_request_buffers(struct em28xx *dev, u32 count)
  55. {
  56. const size_t imagesize = PAGE_ALIGN(dev->frame_size); /*needs to be page aligned cause the buffers can be mapped individually! */
  57. void *buff = NULL;
  58. u32 i;
  59. em28xx_coredbg("requested %i buffers with size %zi\n",
  60. count, imagesize);
  61. if (count > EM28XX_NUM_FRAMES)
  62. count = EM28XX_NUM_FRAMES;
  63. dev->num_frames = count;
  64. while (dev->num_frames > 0) {
  65. if ((buff = vmalloc_32(dev->num_frames * imagesize))) {
  66. memset(buff, 0, dev->num_frames * imagesize);
  67. break;
  68. }
  69. dev->num_frames--;
  70. }
  71. for (i = 0; i < dev->num_frames; i++) {
  72. dev->frame[i].bufmem = buff + i * imagesize;
  73. dev->frame[i].buf.index = i;
  74. dev->frame[i].buf.m.offset = i * imagesize;
  75. dev->frame[i].buf.length = dev->frame_size;
  76. dev->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  77. dev->frame[i].buf.sequence = 0;
  78. dev->frame[i].buf.field = V4L2_FIELD_NONE;
  79. dev->frame[i].buf.memory = V4L2_MEMORY_MMAP;
  80. dev->frame[i].buf.flags = 0;
  81. }
  82. return dev->num_frames;
  83. }
  84. /*
  85. * em28xx_queue_unusedframes()
  86. * add all frames that are not currently in use to the inbuffer queue
  87. */
  88. void em28xx_queue_unusedframes(struct em28xx *dev)
  89. {
  90. unsigned long lock_flags;
  91. u32 i;
  92. for (i = 0; i < dev->num_frames; i++)
  93. if (dev->frame[i].state == F_UNUSED) {
  94. dev->frame[i].state = F_QUEUED;
  95. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  96. list_add_tail(&dev->frame[i].frame, &dev->inqueue);
  97. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  98. }
  99. }
  100. /*
  101. * em28xx_release_buffers()
  102. * free frame buffers
  103. */
  104. void em28xx_release_buffers(struct em28xx *dev)
  105. {
  106. if (dev->num_frames) {
  107. vfree(dev->frame[0].bufmem);
  108. dev->num_frames = 0;
  109. }
  110. }
  111. /*
  112. * em28xx_read_reg_req()
  113. * reads data from the usb device specifying bRequest
  114. */
  115. int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
  116. char *buf, int len)
  117. {
  118. int ret, byte;
  119. if (dev->state & DEV_DISCONNECTED)
  120. return(-ENODEV);
  121. em28xx_regdbg("req=%02x, reg=%02x ", req, reg);
  122. ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
  123. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  124. 0x0000, reg, buf, len, HZ);
  125. if (reg_debug){
  126. printk(ret < 0 ? " failed!\n" : "%02x values: ", ret);
  127. for (byte = 0; byte < len; byte++) {
  128. printk(" %02x", (unsigned char)buf[byte]);
  129. }
  130. printk("\n");
  131. }
  132. return ret;
  133. }
  134. /*
  135. * em28xx_read_reg_req()
  136. * reads data from the usb device specifying bRequest
  137. */
  138. int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg)
  139. {
  140. u8 val;
  141. int ret;
  142. if (dev->state & DEV_DISCONNECTED)
  143. return(-ENODEV);
  144. em28xx_regdbg("req=%02x, reg=%02x:", req, reg);
  145. ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
  146. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  147. 0x0000, reg, &val, 1, HZ);
  148. if (reg_debug)
  149. printk(ret < 0 ? " failed!\n" :
  150. "%02x\n", (unsigned char) val);
  151. if (ret < 0)
  152. return ret;
  153. return val;
  154. }
  155. int em28xx_read_reg(struct em28xx *dev, u16 reg)
  156. {
  157. return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
  158. }
  159. /*
  160. * em28xx_write_regs_req()
  161. * sends data to the usb device, specifying bRequest
  162. */
  163. int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
  164. int len)
  165. {
  166. int ret;
  167. /*usb_control_msg seems to expect a kmalloced buffer */
  168. unsigned char *bufs;
  169. if (dev->state & DEV_DISCONNECTED)
  170. return(-ENODEV);
  171. bufs = kmalloc(len, GFP_KERNEL);
  172. em28xx_regdbg("req=%02x reg=%02x:", req, reg);
  173. if (reg_debug) {
  174. int i;
  175. for (i = 0; i < len; ++i)
  176. printk (" %02x", (unsigned char)buf[i]);
  177. printk ("\n");
  178. }
  179. if (!bufs)
  180. return -ENOMEM;
  181. memcpy(bufs, buf, len);
  182. ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), req,
  183. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  184. 0x0000, reg, bufs, len, HZ);
  185. msleep(5); /* FIXME: magic number */
  186. kfree(bufs);
  187. return ret;
  188. }
  189. int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)
  190. {
  191. return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
  192. }
  193. /*
  194. * em28xx_write_reg_bits()
  195. * sets only some bits (specified by bitmask) of a register, by first reading
  196. * the actual value
  197. */
  198. static int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
  199. u8 bitmask)
  200. {
  201. int oldval;
  202. u8 newval;
  203. if ((oldval = em28xx_read_reg(dev, reg)) < 0)
  204. return oldval;
  205. newval = (((u8) oldval) & ~bitmask) | (val & bitmask);
  206. return em28xx_write_regs(dev, reg, &newval, 1);
  207. }
  208. /*
  209. * em28xx_write_ac97()
  210. * write a 16 bit value to the specified AC97 address (LSB first!)
  211. */
  212. static int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 *val)
  213. {
  214. int ret, i;
  215. u8 addr = reg & 0x7f;
  216. if ((ret = em28xx_write_regs(dev, AC97LSB_REG, val, 2)) < 0)
  217. return ret;
  218. if ((ret = em28xx_write_regs(dev, AC97ADDR_REG, &addr, 1)) < 0)
  219. return ret;
  220. /* Wait up to 50 ms for AC97 command to complete */
  221. for (i = 0; i < 10; i++) {
  222. if ((ret = em28xx_read_reg(dev, AC97BUSY_REG)) < 0)
  223. return ret;
  224. if (!((u8) ret) & 0x01)
  225. return 0;
  226. msleep(5);
  227. }
  228. em28xx_warn ("AC97 command still being executed: not handled properly!\n");
  229. return 0;
  230. }
  231. static int em28xx_set_audio_source(struct em28xx *dev)
  232. {
  233. static char *enable = "\x08\x08";
  234. static char *disable = "\x08\x88";
  235. char *video = enable, *line = disable;
  236. int ret;
  237. u8 input;
  238. if (dev->is_em2800) {
  239. if (dev->ctl_ainput)
  240. input = EM2800_AUDIO_SRC_LINE;
  241. else
  242. input = EM2800_AUDIO_SRC_TUNER;
  243. ret = em28xx_write_regs(dev, EM2800_AUDIOSRC_REG, &input, 1);
  244. if (ret < 0)
  245. return ret;
  246. }
  247. if (dev->has_msp34xx)
  248. input = EM28XX_AUDIO_SRC_TUNER;
  249. else {
  250. switch (dev->ctl_ainput) {
  251. case EM28XX_AMUX_VIDEO:
  252. input = EM28XX_AUDIO_SRC_TUNER;
  253. break;
  254. case EM28XX_AMUX_LINE_IN:
  255. input = EM28XX_AUDIO_SRC_LINE;
  256. break;
  257. case EM28XX_AMUX_AC97_VIDEO:
  258. input = EM28XX_AUDIO_SRC_LINE;
  259. break;
  260. case EM28XX_AMUX_AC97_LINE_IN:
  261. input = EM28XX_AUDIO_SRC_LINE;
  262. video = disable;
  263. line = enable;
  264. break;
  265. }
  266. }
  267. ret = em28xx_write_reg_bits(dev, AUDIOSRC_REG, input, 0xc0);
  268. if (ret < 0)
  269. return ret;
  270. msleep(5);
  271. /* Sets AC97 mixer registers
  272. This is seems to be needed, even for non-ac97 configs
  273. */
  274. ret = em28xx_write_ac97(dev, VIDEO_AC97, video);
  275. if (ret < 0)
  276. return ret;
  277. ret = em28xx_write_ac97(dev, LINE_IN_AC97, line);
  278. return ret;
  279. }
  280. int em28xx_audio_analog_set(struct em28xx *dev)
  281. {
  282. int ret;
  283. char s[2] = { 0x00, 0x00 };
  284. u8 xclk = 0x07;
  285. s[0] |= 0x1f - dev->volume;
  286. s[1] |= 0x1f - dev->volume;
  287. /* Mute */
  288. s[1] |= 0x80;
  289. ret = em28xx_write_ac97(dev, MASTER_AC97, s);
  290. if (ret < 0)
  291. return ret;
  292. if (dev->has_12mhz_i2s)
  293. xclk |= 0x20;
  294. if (!dev->mute)
  295. xclk |= 0x80;
  296. ret = em28xx_write_reg_bits(dev, XCLK_REG, xclk, 0xa7);
  297. if (ret < 0)
  298. return ret;
  299. msleep(10);
  300. /* Selects the proper audio input */
  301. ret = em28xx_set_audio_source(dev);
  302. /* Unmute device */
  303. if (!dev->mute)
  304. s[1] &= ~0x80;
  305. ret = em28xx_write_ac97(dev, MASTER_AC97, s);
  306. return ret;
  307. }
  308. EXPORT_SYMBOL_GPL(em28xx_audio_analog_set);
  309. int em28xx_colorlevels_set_default(struct em28xx *dev)
  310. {
  311. em28xx_write_regs(dev, YGAIN_REG, "\x10", 1); /* contrast */
  312. em28xx_write_regs(dev, YOFFSET_REG, "\x00", 1); /* brightness */
  313. em28xx_write_regs(dev, UVGAIN_REG, "\x10", 1); /* saturation */
  314. em28xx_write_regs(dev, UOFFSET_REG, "\x00", 1);
  315. em28xx_write_regs(dev, VOFFSET_REG, "\x00", 1);
  316. em28xx_write_regs(dev, SHARPNESS_REG, "\x00", 1);
  317. em28xx_write_regs(dev, GAMMA_REG, "\x20", 1);
  318. em28xx_write_regs(dev, RGAIN_REG, "\x20", 1);
  319. em28xx_write_regs(dev, GGAIN_REG, "\x20", 1);
  320. em28xx_write_regs(dev, BGAIN_REG, "\x20", 1);
  321. em28xx_write_regs(dev, ROFFSET_REG, "\x00", 1);
  322. em28xx_write_regs(dev, GOFFSET_REG, "\x00", 1);
  323. return em28xx_write_regs(dev, BOFFSET_REG, "\x00", 1);
  324. }
  325. int em28xx_capture_start(struct em28xx *dev, int start)
  326. {
  327. int ret;
  328. /* FIXME: which is the best order? */
  329. /* video registers are sampled by VREF */
  330. if ((ret = em28xx_write_reg_bits(dev, USBSUSP_REG, start ? 0x10 : 0x00,
  331. 0x10)) < 0)
  332. return ret;
  333. /* enable video capture */
  334. return em28xx_write_regs(dev, VINENABLE_REG, start ? "\x67" : "\x27", 1);
  335. }
  336. int em28xx_outfmt_set_yuv422(struct em28xx *dev)
  337. {
  338. em28xx_write_regs(dev, OUTFMT_REG, "\x34", 1);
  339. em28xx_write_regs(dev, VINMODE_REG, "\x10", 1);
  340. return em28xx_write_regs(dev, VINCTRL_REG, "\x11", 1);
  341. }
  342. static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,
  343. u8 ymin, u8 ymax)
  344. {
  345. em28xx_coredbg("em28xx Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax);
  346. em28xx_write_regs(dev, XMIN_REG, &xmin, 1);
  347. em28xx_write_regs(dev, XMAX_REG, &xmax, 1);
  348. em28xx_write_regs(dev, YMIN_REG, &ymin, 1);
  349. return em28xx_write_regs(dev, YMAX_REG, &ymax, 1);
  350. }
  351. static int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
  352. u16 width, u16 height)
  353. {
  354. u8 cwidth = width;
  355. u8 cheight = height;
  356. u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);
  357. em28xx_coredbg("em28xx Area Set: (%d,%d)\n", (width | (overflow & 2) << 7),
  358. (height | (overflow & 1) << 8));
  359. em28xx_write_regs(dev, HSTART_REG, &hstart, 1);
  360. em28xx_write_regs(dev, VSTART_REG, &vstart, 1);
  361. em28xx_write_regs(dev, CWIDTH_REG, &cwidth, 1);
  362. em28xx_write_regs(dev, CHEIGHT_REG, &cheight, 1);
  363. return em28xx_write_regs(dev, OFLOW_REG, &overflow, 1);
  364. }
  365. static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
  366. {
  367. u8 mode;
  368. /* the em2800 scaler only supports scaling down to 50% */
  369. if(dev->is_em2800)
  370. mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
  371. else {
  372. u8 buf[2];
  373. buf[0] = h;
  374. buf[1] = h >> 8;
  375. em28xx_write_regs(dev, HSCALELOW_REG, (char *)buf, 2);
  376. buf[0] = v;
  377. buf[1] = v >> 8;
  378. em28xx_write_regs(dev, VSCALELOW_REG, (char *)buf, 2);
  379. /* it seems that both H and V scalers must be active to work correctly */
  380. mode = (h || v)? 0x30: 0x00;
  381. }
  382. return em28xx_write_reg_bits(dev, COMPR_REG, mode, 0x30);
  383. }
  384. /* FIXME: this only function read values from dev */
  385. int em28xx_resolution_set(struct em28xx *dev)
  386. {
  387. int width, height;
  388. width = norm_maxw(dev);
  389. height = norm_maxh(dev) >> 1;
  390. em28xx_outfmt_set_yuv422(dev);
  391. em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
  392. em28xx_capture_area_set(dev, 0, 0, width >> 2, height >> 2);
  393. return em28xx_scaler_set(dev, dev->hscale, dev->vscale);
  394. }
  395. /******************* isoc transfer handling ****************************/
  396. #ifdef ENABLE_DEBUG_ISOC_FRAMES
  397. static void em28xx_isoc_dump(struct urb *urb)
  398. {
  399. int len = 0;
  400. int ntrans = 0;
  401. int i;
  402. printk(KERN_DEBUG "isocIrq: sf=%d np=%d ec=%x\n",
  403. urb->start_frame, urb->number_of_packets,
  404. urb->error_count);
  405. for (i = 0; i < urb->number_of_packets; i++) {
  406. unsigned char *buf =
  407. urb->transfer_buffer +
  408. urb->iso_frame_desc[i].offset;
  409. int alen = urb->iso_frame_desc[i].actual_length;
  410. if (alen > 0) {
  411. if (buf[0] == 0x88) {
  412. ntrans++;
  413. len += alen;
  414. } else if (buf[0] == 0x22) {
  415. printk(KERN_DEBUG
  416. "= l=%d nt=%d bpp=%d\n",
  417. len - 4 * ntrans, ntrans,
  418. ntrans == 0 ? 0 : len / ntrans);
  419. ntrans = 1;
  420. len = alen;
  421. } else
  422. printk(KERN_DEBUG "!\n");
  423. }
  424. printk(KERN_DEBUG " n=%d s=%d al=%d %x\n", i,
  425. urb->iso_frame_desc[i].status,
  426. urb->iso_frame_desc[i].actual_length,
  427. (unsigned int)
  428. *((unsigned char *)(urb->transfer_buffer +
  429. urb->iso_frame_desc[i].
  430. offset)));
  431. }
  432. }
  433. #endif
  434. static inline int em28xx_isoc_video(struct em28xx *dev,struct em28xx_frame_t **f,
  435. unsigned long *lock_flags, unsigned char buf)
  436. {
  437. if (!(buf & 0x01)) {
  438. if ((*f)->state == F_GRABBING) {
  439. /*previous frame is incomplete */
  440. if ((*f)->fieldbytesused < dev->field_size) {
  441. (*f)->state = F_ERROR;
  442. em28xx_isocdbg ("dropping incomplete bottom field (%i missing bytes)",
  443. dev->field_size-(*f)->fieldbytesused);
  444. } else {
  445. (*f)->state = F_DONE;
  446. (*f)->buf.bytesused = dev->frame_size;
  447. }
  448. }
  449. if ((*f)->state == F_DONE || (*f)->state == F_ERROR) {
  450. /* move current frame to outqueue and get next free buffer from inqueue */
  451. spin_lock_irqsave(&dev-> queue_lock, *lock_flags);
  452. list_move_tail(&(*f)->frame, &dev->outqueue);
  453. if (!list_empty(&dev->inqueue))
  454. (*f) = list_entry(dev-> inqueue.next,
  455. struct em28xx_frame_t,frame);
  456. else
  457. (*f) = NULL;
  458. spin_unlock_irqrestore(&dev->queue_lock,*lock_flags);
  459. }
  460. if (!(*f)) {
  461. em28xx_isocdbg ("new frame but no buffer is free");
  462. return -1;
  463. }
  464. do_gettimeofday(&(*f)->buf.timestamp);
  465. (*f)->buf.sequence = ++dev->frame_count;
  466. (*f)->buf.field = V4L2_FIELD_INTERLACED;
  467. (*f)->state = F_GRABBING;
  468. (*f)->buf.bytesused = 0;
  469. (*f)->top_field = 1;
  470. (*f)->fieldbytesused = 0;
  471. } else {
  472. /* acquiring bottom field */
  473. if ((*f)->state == F_GRABBING) {
  474. if (!(*f)->top_field) {
  475. (*f)->state = F_ERROR;
  476. em28xx_isocdbg ("unexpected begin of bottom field; discarding it");
  477. } else if ((*f)-> fieldbytesused < dev->field_size - 172) {
  478. (*f)->state = F_ERROR;
  479. em28xx_isocdbg ("dropping incomplete top field (%i missing bytes)",
  480. dev->field_size-(*f)->fieldbytesused);
  481. } else {
  482. (*f)->top_field = 0;
  483. (*f)->fieldbytesused = 0;
  484. }
  485. }
  486. }
  487. return (0);
  488. }
  489. static inline void em28xx_isoc_video_copy(struct em28xx *dev,
  490. struct em28xx_frame_t **f, unsigned char *buf, int len)
  491. {
  492. void *fieldstart, *startwrite, *startread;
  493. int linesdone, currlinedone, offset, lencopy,remain;
  494. if(dev->frame_size != (*f)->buf.length){
  495. em28xx_err("frame_size %i and buf.length %i are different!!!\n",dev->frame_size,(*f)->buf.length);
  496. return;
  497. }
  498. if ((*f)->fieldbytesused + len > dev->field_size)
  499. len =dev->field_size - (*f)->fieldbytesused;
  500. if (buf[0] != 0x88 && buf[0] != 0x22) {
  501. em28xx_isocdbg("frame is not complete\n");
  502. startread = buf;
  503. len+=4;
  504. } else
  505. startread = buf + 4;
  506. remain = len;
  507. if ((*f)->top_field)
  508. fieldstart = (*f)->bufmem;
  509. else
  510. fieldstart = (*f)->bufmem + dev->bytesperline;
  511. linesdone = (*f)->fieldbytesused / dev->bytesperline;
  512. currlinedone = (*f)->fieldbytesused % dev->bytesperline;
  513. offset = linesdone * dev->bytesperline * 2 + currlinedone;
  514. startwrite = fieldstart + offset;
  515. lencopy = dev->bytesperline - currlinedone;
  516. lencopy = lencopy > remain ? remain : lencopy;
  517. memcpy(startwrite, startread, lencopy);
  518. remain -= lencopy;
  519. while (remain > 0) {
  520. startwrite += lencopy + dev->bytesperline;
  521. startread += lencopy;
  522. if (dev->bytesperline > remain)
  523. lencopy = remain;
  524. else
  525. lencopy = dev->bytesperline;
  526. memcpy(startwrite, startread, lencopy);
  527. remain -= lencopy;
  528. }
  529. (*f)->fieldbytesused += len;
  530. }
  531. /*
  532. * em28xx_isoIrq()
  533. * handles the incoming isoc urbs and fills the frames from our inqueue
  534. */
  535. static void em28xx_isocIrq(struct urb *urb)
  536. {
  537. struct em28xx *dev = urb->context;
  538. int i, status;
  539. struct em28xx_frame_t **f;
  540. unsigned long lock_flags;
  541. if (!dev)
  542. return;
  543. #ifdef ENABLE_DEBUG_ISOC_FRAMES
  544. if (isoc_debug>1)
  545. em28xx_isoc_dump(urb);
  546. #endif
  547. if (urb->status == -ENOENT)
  548. return;
  549. f = &dev->frame_current;
  550. if (dev->stream == STREAM_INTERRUPT) {
  551. dev->stream = STREAM_OFF;
  552. if ((*f))
  553. (*f)->state = F_QUEUED;
  554. em28xx_isocdbg("stream interrupted");
  555. wake_up_interruptible(&dev->wait_stream);
  556. }
  557. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  558. return;
  559. if (dev->stream == STREAM_ON && !list_empty(&dev->inqueue)) {
  560. if (!(*f))
  561. (*f) = list_entry(dev->inqueue.next,
  562. struct em28xx_frame_t, frame);
  563. for (i = 0; i < urb->number_of_packets; i++) {
  564. unsigned char *buf = urb->transfer_buffer +
  565. urb->iso_frame_desc[i].offset;
  566. int len = urb->iso_frame_desc[i].actual_length - 4;
  567. if (urb->iso_frame_desc[i].status) {
  568. em28xx_isocdbg("data error: [%d] len=%d, status=%d", i,
  569. urb->iso_frame_desc[i].actual_length,
  570. urb->iso_frame_desc[i].status);
  571. if (urb->iso_frame_desc[i].status != -EPROTO)
  572. continue;
  573. }
  574. if (urb->iso_frame_desc[i].actual_length <= 0) {
  575. em28xx_isocdbg("packet %d is empty",i);
  576. continue;
  577. }
  578. if (urb->iso_frame_desc[i].actual_length >
  579. urb->iso_frame_desc[i].length) {
  580. em28xx_isocdbg("packet bigger than packet size");
  581. continue;
  582. }
  583. /*new frame */
  584. if (buf[0] == 0x22 && buf[1] == 0x5a) {
  585. em28xx_isocdbg("Video frame, length=%i!",len);
  586. if (em28xx_isoc_video(dev,f,&lock_flags,buf[2]))
  587. break;
  588. } else if (buf[0]==0x33 && buf[1]==0x95 && buf[2]==0x00) {
  589. em28xx_isocdbg("VBI HEADER!!!");
  590. }
  591. /* actual copying */
  592. if ((*f)->state == F_GRABBING) {
  593. em28xx_isoc_video_copy(dev,f,buf, len);
  594. }
  595. }
  596. }
  597. for (i = 0; i < urb->number_of_packets; i++) {
  598. urb->iso_frame_desc[i].status = 0;
  599. urb->iso_frame_desc[i].actual_length = 0;
  600. }
  601. urb->status = 0;
  602. if ((status = usb_submit_urb(urb, GFP_ATOMIC))) {
  603. em28xx_errdev("resubmit of urb failed (error=%i)\n", status);
  604. dev->state |= DEV_MISCONFIGURED;
  605. }
  606. wake_up_interruptible(&dev->wait_frame);
  607. return;
  608. }
  609. /*
  610. * em28xx_uninit_isoc()
  611. * deallocates the buffers and urbs allocated during em28xx_init_iosc()
  612. */
  613. void em28xx_uninit_isoc(struct em28xx *dev)
  614. {
  615. int i;
  616. for (i = 0; i < EM28XX_NUM_BUFS; i++) {
  617. if (dev->urb[i]) {
  618. usb_kill_urb(dev->urb[i]);
  619. if (dev->transfer_buffer[i]) {
  620. usb_buffer_free(dev->udev,
  621. dev->urb[i]->transfer_buffer_length,
  622. dev->transfer_buffer[i],
  623. dev->urb[i]->transfer_dma);
  624. }
  625. usb_free_urb(dev->urb[i]);
  626. }
  627. dev->urb[i] = NULL;
  628. dev->transfer_buffer[i] = NULL;
  629. }
  630. em28xx_capture_start(dev, 0);
  631. }
  632. /*
  633. * em28xx_init_isoc()
  634. * allocates transfer buffers and submits the urbs for isoc transfer
  635. */
  636. int em28xx_init_isoc(struct em28xx *dev)
  637. {
  638. /* change interface to 3 which allows the biggest packet sizes */
  639. int i, errCode;
  640. int sb_size;
  641. em28xx_set_alternate(dev);
  642. sb_size = EM28XX_NUM_PACKETS * dev->max_pkt_size;
  643. /* reset streaming vars */
  644. dev->frame_current = NULL;
  645. dev->frame_count = 0;
  646. /* allocate urbs */
  647. for (i = 0; i < EM28XX_NUM_BUFS; i++) {
  648. struct urb *urb;
  649. int j;
  650. /* allocate transfer buffer */
  651. urb = usb_alloc_urb(EM28XX_NUM_PACKETS, GFP_KERNEL);
  652. if (!urb){
  653. em28xx_errdev("cannot alloc urb %i\n", i);
  654. em28xx_uninit_isoc(dev);
  655. return -ENOMEM;
  656. }
  657. dev->transfer_buffer[i] = usb_buffer_alloc(dev->udev, sb_size,
  658. GFP_KERNEL,
  659. &urb->transfer_dma);
  660. if (!dev->transfer_buffer[i]) {
  661. em28xx_errdev
  662. ("unable to allocate %i bytes for transfer buffer %i\n",
  663. sb_size, i);
  664. em28xx_uninit_isoc(dev);
  665. usb_free_urb(urb);
  666. return -ENOMEM;
  667. }
  668. memset(dev->transfer_buffer[i], 0, sb_size);
  669. urb->dev = dev->udev;
  670. urb->context = dev;
  671. urb->pipe = usb_rcvisocpipe(dev->udev, 0x82);
  672. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  673. urb->interval = 1;
  674. urb->transfer_buffer = dev->transfer_buffer[i];
  675. urb->complete = em28xx_isocIrq;
  676. urb->number_of_packets = EM28XX_NUM_PACKETS;
  677. urb->transfer_buffer_length = sb_size;
  678. for (j = 0; j < EM28XX_NUM_PACKETS; j++) {
  679. urb->iso_frame_desc[j].offset = j * dev->max_pkt_size;
  680. urb->iso_frame_desc[j].length = dev->max_pkt_size;
  681. }
  682. dev->urb[i] = urb;
  683. }
  684. /* submit urbs */
  685. em28xx_coredbg("Submitting %d urbs of %d packets (%d each)\n",
  686. EM28XX_NUM_BUFS, EM28XX_NUM_PACKETS, dev->max_pkt_size);
  687. for (i = 0; i < EM28XX_NUM_BUFS; i++) {
  688. errCode = usb_submit_urb(dev->urb[i], GFP_KERNEL);
  689. if (errCode) {
  690. em28xx_errdev("submit of urb %i failed (error=%i)\n", i,
  691. errCode);
  692. em28xx_uninit_isoc(dev);
  693. return errCode;
  694. }
  695. }
  696. return 0;
  697. }
  698. int em28xx_set_alternate(struct em28xx *dev)
  699. {
  700. int errCode, prev_alt = dev->alt;
  701. int i;
  702. unsigned int min_pkt_size = dev->bytesperline+4;
  703. /* When image size is bigger than a ceirtain value,
  704. the frame size should be increased, otherwise, only
  705. green screen will be received.
  706. */
  707. if (dev->frame_size > 720*240*2)
  708. min_pkt_size *= 2;
  709. for (i = 0; i < dev->num_alt; i++)
  710. if (dev->alt_max_pkt_size[i] >= min_pkt_size)
  711. break;
  712. dev->alt = i;
  713. if (dev->alt != prev_alt) {
  714. em28xx_coredbg("minimum isoc packet size: %u (alt=%d)\n",
  715. min_pkt_size, dev->alt);
  716. dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
  717. em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n",
  718. dev->alt, dev->max_pkt_size);
  719. errCode = usb_set_interface(dev->udev, 0, dev->alt);
  720. if (errCode < 0) {
  721. em28xx_errdev ("cannot change alternate number to %d (error=%i)\n",
  722. dev->alt, errCode);
  723. return errCode;
  724. }
  725. }
  726. return 0;
  727. }