em28xx-core.c 21 KB

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