em28xx-core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. 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_audio_analog_set(struct em28xx *dev)
  226. {
  227. char s[2] = { 0x00, 0x00 };
  228. s[0] |= 0x1f - dev->volume;
  229. s[1] |= 0x1f - dev->volume;
  230. if (dev->mute)
  231. s[1] |= 0x80;
  232. return em28xx_write_ac97(dev, MASTER_AC97, s);
  233. }
  234. int em28xx_colorlevels_set_default(struct em28xx *dev)
  235. {
  236. em28xx_write_regs(dev, YGAIN_REG, "\x10", 1); /* contrast */
  237. em28xx_write_regs(dev, YOFFSET_REG, "\x00", 1); /* brightness */
  238. em28xx_write_regs(dev, UVGAIN_REG, "\x10", 1); /* saturation */
  239. em28xx_write_regs(dev, UOFFSET_REG, "\x00", 1);
  240. em28xx_write_regs(dev, VOFFSET_REG, "\x00", 1);
  241. em28xx_write_regs(dev, SHARPNESS_REG, "\x00", 1);
  242. em28xx_write_regs(dev, GAMMA_REG, "\x20", 1);
  243. em28xx_write_regs(dev, RGAIN_REG, "\x20", 1);
  244. em28xx_write_regs(dev, GGAIN_REG, "\x20", 1);
  245. em28xx_write_regs(dev, BGAIN_REG, "\x20", 1);
  246. em28xx_write_regs(dev, ROFFSET_REG, "\x00", 1);
  247. em28xx_write_regs(dev, GOFFSET_REG, "\x00", 1);
  248. return em28xx_write_regs(dev, BOFFSET_REG, "\x00", 1);
  249. }
  250. int em28xx_capture_start(struct em28xx *dev, int start)
  251. {
  252. int ret;
  253. /* FIXME: which is the best order? */
  254. /* video registers are sampled by VREF */
  255. if ((ret = em28xx_write_reg_bits(dev, USBSUSP_REG, start ? 0x10 : 0x00,
  256. 0x10)) < 0)
  257. return ret;
  258. /* enable video capture */
  259. return em28xx_write_regs(dev, VINENABLE_REG, start ? "\x67" : "\x27", 1);
  260. }
  261. int em28xx_outfmt_set_yuv422(struct em28xx *dev)
  262. {
  263. em28xx_write_regs(dev, OUTFMT_REG, "\x34", 1);
  264. em28xx_write_regs(dev, VINMODE_REG, "\x10", 1);
  265. return em28xx_write_regs(dev, VINCTRL_REG, "\x11", 1);
  266. }
  267. static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,
  268. u8 ymin, u8 ymax)
  269. {
  270. em28xx_coredbg("em28xx Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax);
  271. em28xx_write_regs(dev, XMIN_REG, &xmin, 1);
  272. em28xx_write_regs(dev, XMAX_REG, &xmax, 1);
  273. em28xx_write_regs(dev, YMIN_REG, &ymin, 1);
  274. return em28xx_write_regs(dev, YMAX_REG, &ymax, 1);
  275. }
  276. static int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
  277. u16 width, u16 height)
  278. {
  279. u8 cwidth = width;
  280. u8 cheight = height;
  281. u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);
  282. em28xx_coredbg("em28xx Area Set: (%d,%d)\n", (width | (overflow & 2) << 7),
  283. (height | (overflow & 1) << 8));
  284. em28xx_write_regs(dev, HSTART_REG, &hstart, 1);
  285. em28xx_write_regs(dev, VSTART_REG, &vstart, 1);
  286. em28xx_write_regs(dev, CWIDTH_REG, &cwidth, 1);
  287. em28xx_write_regs(dev, CHEIGHT_REG, &cheight, 1);
  288. return em28xx_write_regs(dev, OFLOW_REG, &overflow, 1);
  289. }
  290. static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
  291. {
  292. u8 mode;
  293. /* the em2800 scaler only supports scaling down to 50% */
  294. if(dev->is_em2800)
  295. mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
  296. else {
  297. u8 buf[2];
  298. buf[0] = h;
  299. buf[1] = h >> 8;
  300. em28xx_write_regs(dev, HSCALELOW_REG, (char *)buf, 2);
  301. buf[0] = v;
  302. buf[1] = v >> 8;
  303. em28xx_write_regs(dev, VSCALELOW_REG, (char *)buf, 2);
  304. /* it seems that both H and V scalers must be active to work correctly */
  305. mode = (h || v)? 0x30: 0x00;
  306. }
  307. return em28xx_write_reg_bits(dev, COMPR_REG, mode, 0x30);
  308. }
  309. /* FIXME: this only function read values from dev */
  310. int em28xx_resolution_set(struct em28xx *dev)
  311. {
  312. int width, height;
  313. width = norm_maxw(dev);
  314. height = norm_maxh(dev) >> 1;
  315. em28xx_outfmt_set_yuv422(dev);
  316. em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
  317. em28xx_capture_area_set(dev, 0, 0, width >> 2, height >> 2);
  318. return em28xx_scaler_set(dev, dev->hscale, dev->vscale);
  319. }
  320. /******************* isoc transfer handling ****************************/
  321. #ifdef ENABLE_DEBUG_ISOC_FRAMES
  322. static void em28xx_isoc_dump(struct urb *urb)
  323. {
  324. int len = 0;
  325. int ntrans = 0;
  326. int i;
  327. printk(KERN_DEBUG "isocIrq: sf=%d np=%d ec=%x\n",
  328. urb->start_frame, urb->number_of_packets,
  329. urb->error_count);
  330. for (i = 0; i < urb->number_of_packets; i++) {
  331. unsigned char *buf =
  332. urb->transfer_buffer +
  333. urb->iso_frame_desc[i].offset;
  334. int alen = urb->iso_frame_desc[i].actual_length;
  335. if (alen > 0) {
  336. if (buf[0] == 0x88) {
  337. ntrans++;
  338. len += alen;
  339. } else if (buf[0] == 0x22) {
  340. printk(KERN_DEBUG
  341. "= l=%d nt=%d bpp=%d\n",
  342. len - 4 * ntrans, ntrans,
  343. ntrans == 0 ? 0 : len / ntrans);
  344. ntrans = 1;
  345. len = alen;
  346. } else
  347. printk(KERN_DEBUG "!\n");
  348. }
  349. printk(KERN_DEBUG " n=%d s=%d al=%d %x\n", i,
  350. urb->iso_frame_desc[i].status,
  351. urb->iso_frame_desc[i].actual_length,
  352. (unsigned int)
  353. *((unsigned char *)(urb->transfer_buffer +
  354. urb->iso_frame_desc[i].
  355. offset)));
  356. }
  357. }
  358. #endif
  359. static inline int em28xx_isoc_video(struct em28xx *dev,struct em28xx_frame_t **f,
  360. unsigned long *lock_flags, unsigned char buf)
  361. {
  362. if (!(buf & 0x01)) {
  363. if ((*f)->state == F_GRABBING) {
  364. /*previous frame is incomplete */
  365. if ((*f)->fieldbytesused < dev->field_size) {
  366. (*f)->state = F_ERROR;
  367. em28xx_isocdbg ("dropping incomplete bottom field (%i missing bytes)",
  368. dev->field_size-(*f)->fieldbytesused);
  369. } else {
  370. (*f)->state = F_DONE;
  371. (*f)->buf.bytesused = dev->frame_size;
  372. }
  373. }
  374. if ((*f)->state == F_DONE || (*f)->state == F_ERROR) {
  375. /* move current frame to outqueue and get next free buffer from inqueue */
  376. spin_lock_irqsave(&dev-> queue_lock, *lock_flags);
  377. list_move_tail(&(*f)->frame, &dev->outqueue);
  378. if (!list_empty(&dev->inqueue))
  379. (*f) = list_entry(dev-> inqueue.next,
  380. struct em28xx_frame_t,frame);
  381. else
  382. (*f) = NULL;
  383. spin_unlock_irqrestore(&dev->queue_lock,*lock_flags);
  384. }
  385. if (!(*f)) {
  386. em28xx_isocdbg ("new frame but no buffer is free");
  387. return -1;
  388. }
  389. do_gettimeofday(&(*f)->buf.timestamp);
  390. (*f)->buf.sequence = ++dev->frame_count;
  391. (*f)->buf.field = V4L2_FIELD_INTERLACED;
  392. (*f)->state = F_GRABBING;
  393. (*f)->buf.bytesused = 0;
  394. (*f)->top_field = 1;
  395. (*f)->fieldbytesused = 0;
  396. } else {
  397. /* acquiring bottom field */
  398. if ((*f)->state == F_GRABBING) {
  399. if (!(*f)->top_field) {
  400. (*f)->state = F_ERROR;
  401. em28xx_isocdbg ("unexpected begin of bottom field; discarding it");
  402. } else if ((*f)-> fieldbytesused < dev->field_size - 172) {
  403. (*f)->state = F_ERROR;
  404. em28xx_isocdbg ("dropping incomplete top field (%i missing bytes)",
  405. dev->field_size-(*f)->fieldbytesused);
  406. } else {
  407. (*f)->top_field = 0;
  408. (*f)->fieldbytesused = 0;
  409. }
  410. }
  411. }
  412. return (0);
  413. }
  414. static inline void em28xx_isoc_video_copy(struct em28xx *dev,
  415. struct em28xx_frame_t **f, unsigned char *buf, int len)
  416. {
  417. void *fieldstart, *startwrite, *startread;
  418. int linesdone, currlinedone, offset, lencopy,remain;
  419. if(dev->frame_size != (*f)->buf.length){
  420. em28xx_err("frame_size %i and buf.length %i are different!!!\n",dev->frame_size,(*f)->buf.length);
  421. return;
  422. }
  423. if ((*f)->fieldbytesused + len > dev->field_size)
  424. len =dev->field_size - (*f)->fieldbytesused;
  425. if (buf[0] != 0x88 && buf[0] != 0x22) {
  426. em28xx_isocdbg("frame is not complete\n");
  427. startread = buf;
  428. len+=4;
  429. } else
  430. startread = buf + 4;
  431. remain = len;
  432. if ((*f)->top_field)
  433. fieldstart = (*f)->bufmem;
  434. else
  435. fieldstart = (*f)->bufmem + dev->bytesperline;
  436. linesdone = (*f)->fieldbytesused / dev->bytesperline;
  437. currlinedone = (*f)->fieldbytesused % dev->bytesperline;
  438. offset = linesdone * dev->bytesperline * 2 + currlinedone;
  439. startwrite = fieldstart + offset;
  440. lencopy = dev->bytesperline - currlinedone;
  441. lencopy = lencopy > remain ? remain : lencopy;
  442. memcpy(startwrite, startread, lencopy);
  443. remain -= lencopy;
  444. while (remain > 0) {
  445. startwrite += lencopy + dev->bytesperline;
  446. startread += lencopy;
  447. if (dev->bytesperline > remain)
  448. lencopy = remain;
  449. else
  450. lencopy = dev->bytesperline;
  451. memcpy(startwrite, startread, lencopy);
  452. remain -= lencopy;
  453. }
  454. (*f)->fieldbytesused += len;
  455. }
  456. /*
  457. * em28xx_isoIrq()
  458. * handles the incoming isoc urbs and fills the frames from our inqueue
  459. */
  460. static void em28xx_isocIrq(struct urb *urb)
  461. {
  462. struct em28xx *dev = urb->context;
  463. int i, status;
  464. struct em28xx_frame_t **f;
  465. unsigned long lock_flags;
  466. if (!dev)
  467. return;
  468. #ifdef ENABLE_DEBUG_ISOC_FRAMES
  469. if (isoc_debug>1)
  470. em28xx_isoc_dump(urb);
  471. #endif
  472. if (urb->status == -ENOENT)
  473. return;
  474. f = &dev->frame_current;
  475. if (dev->stream == STREAM_INTERRUPT) {
  476. dev->stream = STREAM_OFF;
  477. if ((*f))
  478. (*f)->state = F_QUEUED;
  479. em28xx_isocdbg("stream interrupted");
  480. wake_up_interruptible(&dev->wait_stream);
  481. }
  482. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  483. return;
  484. if (dev->stream == STREAM_ON && !list_empty(&dev->inqueue)) {
  485. if (!(*f))
  486. (*f) = list_entry(dev->inqueue.next,
  487. struct em28xx_frame_t, frame);
  488. for (i = 0; i < urb->number_of_packets; i++) {
  489. unsigned char *buf = urb->transfer_buffer +
  490. urb->iso_frame_desc[i].offset;
  491. int len = urb->iso_frame_desc[i].actual_length - 4;
  492. if (urb->iso_frame_desc[i].status) {
  493. em28xx_isocdbg("data error: [%d] len=%d, status=%d", i,
  494. urb->iso_frame_desc[i].actual_length,
  495. urb->iso_frame_desc[i].status);
  496. if (urb->iso_frame_desc[i].status != -EPROTO)
  497. continue;
  498. }
  499. if (urb->iso_frame_desc[i].actual_length <= 0) {
  500. em28xx_isocdbg("packet %d is empty",i);
  501. continue;
  502. }
  503. if (urb->iso_frame_desc[i].actual_length >
  504. dev->max_pkt_size) {
  505. em28xx_isocdbg("packet bigger than packet size");
  506. continue;
  507. }
  508. /*new frame */
  509. if (buf[0] == 0x22 && buf[1] == 0x5a) {
  510. em28xx_isocdbg("Video frame, length=%i!",len);
  511. if (em28xx_isoc_video(dev,f,&lock_flags,buf[2]))
  512. break;
  513. } else if (buf[0]==0x33 && buf[1]==0x95 && buf[2]==0x00) {
  514. em28xx_isocdbg("VBI HEADER!!!");
  515. }
  516. /* actual copying */
  517. if ((*f)->state == F_GRABBING) {
  518. em28xx_isoc_video_copy(dev,f,buf, len);
  519. }
  520. }
  521. }
  522. for (i = 0; i < urb->number_of_packets; i++) {
  523. urb->iso_frame_desc[i].status = 0;
  524. urb->iso_frame_desc[i].actual_length = 0;
  525. }
  526. urb->status = 0;
  527. if ((status = usb_submit_urb(urb, GFP_ATOMIC))) {
  528. em28xx_errdev("resubmit of urb failed (error=%i)\n", status);
  529. dev->state |= DEV_MISCONFIGURED;
  530. }
  531. wake_up_interruptible(&dev->wait_frame);
  532. return;
  533. }
  534. /*
  535. * em28xx_uninit_isoc()
  536. * deallocates the buffers and urbs allocated during em28xx_init_iosc()
  537. */
  538. void em28xx_uninit_isoc(struct em28xx *dev)
  539. {
  540. int i;
  541. for (i = 0; i < EM28XX_NUM_BUFS; i++) {
  542. if (dev->urb[i]) {
  543. usb_kill_urb(dev->urb[i]);
  544. if (dev->transfer_buffer[i]){
  545. usb_buffer_free(dev->udev,(EM28XX_NUM_PACKETS*dev->max_pkt_size),dev->transfer_buffer[i],dev->urb[i]->transfer_dma);
  546. }
  547. usb_free_urb(dev->urb[i]);
  548. }
  549. dev->urb[i] = NULL;
  550. dev->transfer_buffer[i] = NULL;
  551. }
  552. em28xx_capture_start(dev, 0);
  553. }
  554. /*
  555. * em28xx_init_isoc()
  556. * allocates transfer buffers and submits the urbs for isoc transfer
  557. */
  558. int em28xx_init_isoc(struct em28xx *dev)
  559. {
  560. /* change interface to 3 which allowes the biggest packet sizes */
  561. int i, errCode;
  562. const int sb_size = EM28XX_NUM_PACKETS * dev->max_pkt_size;
  563. /* reset streaming vars */
  564. dev->frame_current = NULL;
  565. dev->frame_count = 0;
  566. /* allocate urbs */
  567. for (i = 0; i < EM28XX_NUM_BUFS; i++) {
  568. struct urb *urb;
  569. int j, k;
  570. /* allocate transfer buffer */
  571. urb = usb_alloc_urb(EM28XX_NUM_PACKETS, GFP_KERNEL);
  572. if (!urb){
  573. em28xx_errdev("cannot alloc urb %i\n", i);
  574. em28xx_uninit_isoc(dev);
  575. return -ENOMEM;
  576. }
  577. dev->transfer_buffer[i] = usb_buffer_alloc(dev->udev, sb_size, GFP_KERNEL,&urb->transfer_dma);
  578. if (!dev->transfer_buffer[i]) {
  579. em28xx_errdev
  580. ("unable to allocate %i bytes for transfer buffer %i\n",
  581. sb_size, i);
  582. em28xx_uninit_isoc(dev);
  583. return -ENOMEM;
  584. }
  585. memset(dev->transfer_buffer[i], 0, sb_size);
  586. urb->dev = dev->udev;
  587. urb->context = dev;
  588. urb->pipe = usb_rcvisocpipe(dev->udev, 0x82);
  589. urb->transfer_flags = URB_ISO_ASAP;
  590. urb->interval = 1;
  591. urb->transfer_buffer = dev->transfer_buffer[i];
  592. urb->complete = em28xx_isocIrq;
  593. urb->number_of_packets = EM28XX_NUM_PACKETS;
  594. urb->transfer_buffer_length = sb_size;
  595. for (j = k = 0; j < EM28XX_NUM_PACKETS;
  596. j++, k += dev->max_pkt_size) {
  597. urb->iso_frame_desc[j].offset = k;
  598. urb->iso_frame_desc[j].length =
  599. dev->max_pkt_size;
  600. }
  601. dev->urb[i] = urb;
  602. }
  603. /* submit urbs */
  604. for (i = 0; i < EM28XX_NUM_BUFS; i++) {
  605. errCode = usb_submit_urb(dev->urb[i], GFP_KERNEL);
  606. if (errCode) {
  607. em28xx_errdev("submit of urb %i failed (error=%i)\n", i,
  608. errCode);
  609. em28xx_uninit_isoc(dev);
  610. return errCode;
  611. }
  612. }
  613. return 0;
  614. }
  615. int em28xx_set_alternate(struct em28xx *dev)
  616. {
  617. int errCode, prev_alt = dev->alt;
  618. dev->alt = alt;
  619. if (dev->alt == 0) {
  620. int i;
  621. for(i=0;i< dev->num_alt; i++)
  622. if(dev->alt_max_pkt_size[i]>dev->alt_max_pkt_size[dev->alt])
  623. dev->alt=i;
  624. }
  625. if (dev->alt != prev_alt) {
  626. dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
  627. em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n", dev->alt,
  628. dev->max_pkt_size);
  629. errCode = usb_set_interface(dev->udev, 0, dev->alt);
  630. if (errCode < 0) {
  631. em28xx_errdev ("cannot change alternate number to %d (error=%i)\n",
  632. dev->alt, errCode);
  633. return errCode;
  634. }
  635. }
  636. return 0;
  637. }