em28xx-core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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/moduleparam.h>
  23. #include <linux/usb.h>
  24. #include <linux/vmalloc.h>
  25. #include "em28xx.h"
  26. /* #define ENABLE_DEBUG_ISOC_FRAMES */
  27. static unsigned int core_debug = 0;
  28. module_param(core_debug,int,0644);
  29. MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
  30. #define em28xx_coredbg(fmt, arg...) do {\
  31. if (core_debug) \
  32. printk(KERN_INFO "%s %s :"fmt, \
  33. dev->name, __FUNCTION__ , ##arg); } while (0)
  34. static unsigned int reg_debug = 0;
  35. module_param(reg_debug,int,0644);
  36. MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]");
  37. #define em28xx_regdbg(fmt, arg...) do {\
  38. if (reg_debug) \
  39. printk(KERN_INFO "%s %s :"fmt, \
  40. dev->name, __FUNCTION__ , ##arg); } while (0)
  41. static unsigned int isoc_debug = 0;
  42. module_param(isoc_debug,int,0644);
  43. MODULE_PARM_DESC(isoc_debug,"enable debug messages [isoc transfers]");
  44. #define em28xx_isocdbg(fmt, arg...) do {\
  45. if (isoc_debug) \
  46. printk(KERN_INFO "%s %s :"fmt, \
  47. dev->name, __FUNCTION__ , ##arg); } while (0)
  48. static int alt = EM28XX_PINOUT;
  49. module_param(alt, int, 0644);
  50. MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
  51. /*
  52. * em28xx_request_buffers()
  53. * allocate a number of buffers
  54. */
  55. u32 em28xx_request_buffers(struct em28xx *dev, u32 count)
  56. {
  57. const size_t imagesize = PAGE_ALIGN(dev->frame_size); /*needs to be page aligned cause the buffers can be mapped individually! */
  58. void *buff = NULL;
  59. u32 i;
  60. em28xx_coredbg("requested %i buffers with size %zi", 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", 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" : "%02x\n", val);
  150. if (ret < 0)
  151. return ret;
  152. return val;
  153. }
  154. int em28xx_read_reg(struct em28xx *dev, u16 reg)
  155. {
  156. return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
  157. }
  158. /*
  159. * em28xx_write_regs_req()
  160. * sends data to the usb device, specifying bRequest
  161. */
  162. int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
  163. int len)
  164. {
  165. int ret;
  166. /*usb_control_msg seems to expect a kmalloced buffer */
  167. unsigned char *bufs;
  168. if (dev->state & DEV_DISCONNECTED)
  169. return(-ENODEV);
  170. bufs = kmalloc(len, GFP_KERNEL);
  171. em28xx_regdbg("req=%02x reg=%02x:", req, reg);
  172. if (reg_debug) {
  173. int i;
  174. for (i = 0; i < len; ++i)
  175. printk (" %02x", (unsigned char)buf[i]);
  176. printk ("\n");
  177. }
  178. if (!bufs)
  179. return -ENOMEM;
  180. memcpy(bufs, buf, len);
  181. ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), req,
  182. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  183. 0x0000, reg, bufs, len, HZ);
  184. msleep(5); /* FIXME: magic number */
  185. kfree(bufs);
  186. return ret;
  187. }
  188. int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)
  189. {
  190. return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
  191. }
  192. /*
  193. * em28xx_write_reg_bits()
  194. * sets only some bits (specified by bitmask) of a register, by first reading
  195. * the actual value
  196. */
  197. int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
  198. u8 bitmask)
  199. {
  200. int oldval;
  201. u8 newval;
  202. if ((oldval = em28xx_read_reg(dev, reg)) < 0)
  203. return oldval;
  204. newval = (((u8) oldval) & ~bitmask) | (val & bitmask);
  205. return em28xx_write_regs(dev, reg, &newval, 1);
  206. }
  207. /*
  208. * em28xx_write_ac97()
  209. * write a 16 bit value to the specified AC97 address (LSB first!)
  210. */
  211. int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 * val)
  212. {
  213. int ret;
  214. u8 addr = reg & 0x7f;
  215. if ((ret = em28xx_write_regs(dev, AC97LSB_REG, val, 2)) < 0)
  216. return ret;
  217. if ((ret = em28xx_write_regs(dev, AC97ADDR_REG, &addr, 1)) < 0)
  218. return ret;
  219. if ((ret = em28xx_read_reg(dev, AC97BUSY_REG)) < 0)
  220. return ret;
  221. else if (((u8) ret) & 0x01) {
  222. em28xx_warn ("AC97 command still being executed: not handled properly!\n");
  223. }
  224. return 0;
  225. }
  226. int em28xx_audio_analog_set(struct em28xx *dev)
  227. {
  228. char s[2] = { 0x00, 0x00 };
  229. s[0] |= 0x1f - dev->volume;
  230. s[1] |= 0x1f - dev->volume;
  231. if (dev->mute)
  232. s[1] |= 0x80;
  233. return em28xx_write_ac97(dev, MASTER_AC97, s);
  234. }
  235. int em28xx_colorlevels_set_default(struct em28xx *dev)
  236. {
  237. em28xx_write_regs(dev, YGAIN_REG, "\x10", 1); /* contrast */
  238. em28xx_write_regs(dev, YOFFSET_REG, "\x00", 1); /* brightness */
  239. em28xx_write_regs(dev, UVGAIN_REG, "\x10", 1); /* saturation */
  240. em28xx_write_regs(dev, UOFFSET_REG, "\x00", 1);
  241. em28xx_write_regs(dev, VOFFSET_REG, "\x00", 1);
  242. em28xx_write_regs(dev, SHARPNESS_REG, "\x00", 1);
  243. em28xx_write_regs(dev, GAMMA_REG, "\x20", 1);
  244. em28xx_write_regs(dev, RGAIN_REG, "\x20", 1);
  245. em28xx_write_regs(dev, GGAIN_REG, "\x20", 1);
  246. em28xx_write_regs(dev, BGAIN_REG, "\x20", 1);
  247. em28xx_write_regs(dev, ROFFSET_REG, "\x00", 1);
  248. em28xx_write_regs(dev, GOFFSET_REG, "\x00", 1);
  249. return em28xx_write_regs(dev, BOFFSET_REG, "\x00", 1);
  250. }
  251. int em28xx_capture_start(struct em28xx *dev, int start)
  252. {
  253. int ret;
  254. /* FIXME: which is the best order? */
  255. /* video registers are sampled by VREF */
  256. if ((ret = em28xx_write_reg_bits(dev, USBSUSP_REG, start ? 0x10 : 0x00,
  257. 0x10)) < 0)
  258. return ret;
  259. /* enable video capture */
  260. return em28xx_write_regs(dev, VINENABLE_REG, start ? "\x67" : "\x27", 1);
  261. }
  262. int em28xx_outfmt_set_yuv422(struct em28xx *dev)
  263. {
  264. em28xx_write_regs(dev, OUTFMT_REG, "\x34", 1);
  265. em28xx_write_regs(dev, VINMODE_REG, "\x10", 1);
  266. return em28xx_write_regs(dev, VINCTRL_REG, "\x11", 1);
  267. }
  268. static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,
  269. u8 ymin, u8 ymax)
  270. {
  271. em28xx_coredbg("em28xx Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax);
  272. em28xx_write_regs(dev, XMIN_REG, &xmin, 1);
  273. em28xx_write_regs(dev, XMAX_REG, &xmax, 1);
  274. em28xx_write_regs(dev, YMIN_REG, &ymin, 1);
  275. return em28xx_write_regs(dev, YMAX_REG, &ymax, 1);
  276. }
  277. static int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
  278. u16 width, u16 height)
  279. {
  280. u8 cwidth = width;
  281. u8 cheight = height;
  282. u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);
  283. em28xx_coredbg("em28xx Area Set: (%d,%d)\n", (width | (overflow & 2) << 7),
  284. (height | (overflow & 1) << 8));
  285. em28xx_write_regs(dev, HSTART_REG, &hstart, 1);
  286. em28xx_write_regs(dev, VSTART_REG, &vstart, 1);
  287. em28xx_write_regs(dev, CWIDTH_REG, &cwidth, 1);
  288. em28xx_write_regs(dev, CHEIGHT_REG, &cheight, 1);
  289. return em28xx_write_regs(dev, OFLOW_REG, &overflow, 1);
  290. }
  291. static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
  292. {
  293. u8 mode;
  294. /* the em2800 scaler only supports scaling down to 50% */
  295. if(dev->is_em2800)
  296. mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
  297. else {
  298. u8 buf[2];
  299. buf[0] = h;
  300. buf[1] = h >> 8;
  301. em28xx_write_regs(dev, HSCALELOW_REG, (char *)buf, 2);
  302. buf[0] = v;
  303. buf[1] = v >> 8;
  304. em28xx_write_regs(dev, VSCALELOW_REG, (char *)buf, 2);
  305. /* it seems that both H and V scalers must be active to work correctly */
  306. mode = (h || v)? 0x30: 0x00;
  307. }
  308. return em28xx_write_reg_bits(dev, COMPR_REG, mode, 0x30);
  309. }
  310. /* FIXME: this only function read values from dev */
  311. int em28xx_resolution_set(struct em28xx *dev)
  312. {
  313. int width, height;
  314. width = norm_maxw(dev);
  315. height = norm_maxh(dev) >> 1;
  316. em28xx_outfmt_set_yuv422(dev);
  317. em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
  318. em28xx_capture_area_set(dev, 0, 0, width >> 2, height >> 2);
  319. return em28xx_scaler_set(dev, dev->hscale, dev->vscale);
  320. }
  321. /******************* isoc transfer handling ****************************/
  322. #ifdef ENABLE_DEBUG_ISOC_FRAMES
  323. static void em28xx_isoc_dump(struct urb *urb)
  324. {
  325. int len = 0;
  326. int ntrans = 0;
  327. int i;
  328. printk(KERN_DEBUG "isocIrq: sf=%d np=%d ec=%x\n",
  329. urb->start_frame, urb->number_of_packets,
  330. urb->error_count);
  331. for (i = 0; i < urb->number_of_packets; i++) {
  332. unsigned char *buf =
  333. urb->transfer_buffer +
  334. urb->iso_frame_desc[i].offset;
  335. int alen = urb->iso_frame_desc[i].actual_length;
  336. if (alen > 0) {
  337. if (buf[0] == 0x88) {
  338. ntrans++;
  339. len += alen;
  340. } else if (buf[0] == 0x22) {
  341. printk(KERN_DEBUG
  342. "= l=%d nt=%d bpp=%d\n",
  343. len - 4 * ntrans, ntrans,
  344. ntrans == 0 ? 0 : len / ntrans);
  345. ntrans = 1;
  346. len = alen;
  347. } else
  348. printk(KERN_DEBUG "!\n");
  349. }
  350. printk(KERN_DEBUG " n=%d s=%d al=%d %x\n", i,
  351. urb->iso_frame_desc[i].status,
  352. urb->iso_frame_desc[i].actual_length,
  353. (unsigned int)
  354. *((unsigned char *)(urb->transfer_buffer +
  355. urb->iso_frame_desc[i].
  356. offset)));
  357. }
  358. }
  359. #endif
  360. static inline int em28xx_isoc_video(struct em28xx *dev,struct em28xx_frame_t **f,
  361. unsigned long *lock_flags, unsigned char buf)
  362. {
  363. if (!(buf & 0x01)) {
  364. if ((*f)->state == F_GRABBING) {
  365. /*previous frame is incomplete */
  366. if ((*f)->fieldbytesused < dev->field_size) {
  367. (*f)->state = F_ERROR;
  368. em28xx_isocdbg ("dropping incomplete bottom field (%i missing bytes)",
  369. dev->field_size-(*f)->fieldbytesused);
  370. } else {
  371. (*f)->state = F_DONE;
  372. (*f)->buf.bytesused = dev->frame_size;
  373. }
  374. }
  375. if ((*f)->state == F_DONE || (*f)->state == F_ERROR) {
  376. /* move current frame to outqueue and get next free buffer from inqueue */
  377. spin_lock_irqsave(&dev-> queue_lock, *lock_flags);
  378. list_move_tail(&(*f)->frame, &dev->outqueue);
  379. if (!list_empty(&dev->inqueue))
  380. (*f) = list_entry(dev-> inqueue.next,
  381. struct em28xx_frame_t,frame);
  382. else
  383. (*f) = NULL;
  384. spin_unlock_irqrestore(&dev->queue_lock,*lock_flags);
  385. }
  386. if (!(*f)) {
  387. em28xx_isocdbg ("new frame but no buffer is free");
  388. return -1;
  389. }
  390. do_gettimeofday(&(*f)->buf.timestamp);
  391. (*f)->buf.sequence = ++dev->frame_count;
  392. (*f)->buf.field = V4L2_FIELD_INTERLACED;
  393. (*f)->state = F_GRABBING;
  394. (*f)->buf.bytesused = 0;
  395. (*f)->top_field = 1;
  396. (*f)->fieldbytesused = 0;
  397. } else {
  398. /* acquiring bottom field */
  399. if ((*f)->state == F_GRABBING) {
  400. if (!(*f)->top_field) {
  401. (*f)->state = F_ERROR;
  402. em28xx_isocdbg ("unexpected begin of bottom field; discarding it");
  403. } else if ((*f)-> fieldbytesused < dev->field_size - 172) {
  404. (*f)->state = F_ERROR;
  405. em28xx_isocdbg ("dropping incomplete top field (%i missing bytes)",
  406. dev->field_size-(*f)->fieldbytesused);
  407. } else {
  408. (*f)->top_field = 0;
  409. (*f)->fieldbytesused = 0;
  410. }
  411. }
  412. }
  413. return (0);
  414. }
  415. static inline void em28xx_isoc_video_copy(struct em28xx *dev,
  416. struct em28xx_frame_t **f, unsigned char *buf, int len)
  417. {
  418. void *fieldstart, *startwrite, *startread;
  419. int linesdone, currlinedone, offset, lencopy,remain;
  420. if(dev->frame_size != (*f)->buf.length){
  421. em28xx_err("frame_size %i and buf.length %i are different!!!\n",dev->frame_size,(*f)->buf.length);
  422. return;
  423. }
  424. if ((*f)->fieldbytesused + len > dev->field_size)
  425. len =dev->field_size - (*f)->fieldbytesused;
  426. if (buf[0] != 0x88 && buf[0] != 0x22) {
  427. em28xx_isocdbg("frame is not complete\n");
  428. startread = buf;
  429. len+=4;
  430. } else
  431. startread = buf + 4;
  432. remain = len;
  433. if ((*f)->top_field)
  434. fieldstart = (*f)->bufmem;
  435. else
  436. fieldstart = (*f)->bufmem + dev->bytesperline;
  437. linesdone = (*f)->fieldbytesused / dev->bytesperline;
  438. currlinedone = (*f)->fieldbytesused % dev->bytesperline;
  439. offset = linesdone * dev->bytesperline * 2 + currlinedone;
  440. startwrite = fieldstart + offset;
  441. lencopy = dev->bytesperline - currlinedone;
  442. lencopy = lencopy > remain ? remain : lencopy;
  443. memcpy(startwrite, startread, lencopy);
  444. remain -= lencopy;
  445. while (remain > 0) {
  446. startwrite += lencopy + dev->bytesperline;
  447. startread += lencopy;
  448. if (dev->bytesperline > remain)
  449. lencopy = remain;
  450. else
  451. lencopy = dev->bytesperline;
  452. memcpy(startwrite, startread, lencopy);
  453. remain -= lencopy;
  454. }
  455. (*f)->fieldbytesused += len;
  456. }
  457. /*
  458. * em28xx_isoIrq()
  459. * handles the incoming isoc urbs and fills the frames from our inqueue
  460. */
  461. static void em28xx_isocIrq(struct urb *urb)
  462. {
  463. struct em28xx *dev = urb->context;
  464. int i, status;
  465. struct em28xx_frame_t **f;
  466. unsigned long lock_flags;
  467. if (!dev)
  468. return;
  469. #ifdef ENABLE_DEBUG_ISOC_FRAMES
  470. if (isoc_debug>1)
  471. em28xx_isoc_dump(urb);
  472. #endif
  473. if (urb->status == -ENOENT)
  474. return;
  475. f = &dev->frame_current;
  476. if (dev->stream == STREAM_INTERRUPT) {
  477. dev->stream = STREAM_OFF;
  478. if ((*f))
  479. (*f)->state = F_QUEUED;
  480. em28xx_isocdbg("stream interrupted");
  481. wake_up_interruptible(&dev->wait_stream);
  482. }
  483. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  484. return;
  485. if (dev->stream == STREAM_ON && !list_empty(&dev->inqueue)) {
  486. if (!(*f))
  487. (*f) = list_entry(dev->inqueue.next,
  488. struct em28xx_frame_t, frame);
  489. for (i = 0; i < urb->number_of_packets; i++) {
  490. unsigned char *buf = urb->transfer_buffer +
  491. urb->iso_frame_desc[i].offset;
  492. int len = urb->iso_frame_desc[i].actual_length - 4;
  493. if (urb->iso_frame_desc[i].status) {
  494. em28xx_isocdbg("data error: [%d] len=%d, status=%d", i,
  495. urb->iso_frame_desc[i].actual_length,
  496. urb->iso_frame_desc[i].status);
  497. if (urb->iso_frame_desc[i].status != -EPROTO)
  498. continue;
  499. }
  500. if (urb->iso_frame_desc[i].actual_length <= 0) {
  501. em28xx_isocdbg("packet %d is empty",i);
  502. continue;
  503. }
  504. if (urb->iso_frame_desc[i].actual_length >
  505. dev->max_pkt_size) {
  506. em28xx_isocdbg("packet bigger than packet size");
  507. continue;
  508. }
  509. /*new frame */
  510. if (buf[0] == 0x22 && buf[1] == 0x5a) {
  511. em28xx_isocdbg("Video frame, length=%i!",len);
  512. if (em28xx_isoc_video(dev,f,&lock_flags,buf[2]))
  513. break;
  514. } else if (buf[0]==0x33 && buf[1]==0x95 && buf[2]==0x00) {
  515. em28xx_isocdbg("VBI HEADER!!!");
  516. }
  517. /* actual copying */
  518. if ((*f)->state == F_GRABBING) {
  519. em28xx_isoc_video_copy(dev,f,buf, len);
  520. }
  521. }
  522. }
  523. for (i = 0; i < urb->number_of_packets; i++) {
  524. urb->iso_frame_desc[i].status = 0;
  525. urb->iso_frame_desc[i].actual_length = 0;
  526. }
  527. urb->status = 0;
  528. if ((status = usb_submit_urb(urb, GFP_ATOMIC))) {
  529. em28xx_errdev("resubmit of urb failed (error=%i)\n", status);
  530. dev->state |= DEV_MISCONFIGURED;
  531. }
  532. wake_up_interruptible(&dev->wait_frame);
  533. return;
  534. }
  535. /*
  536. * em28xx_uninit_isoc()
  537. * deallocates the buffers and urbs allocated during em28xx_init_iosc()
  538. */
  539. void em28xx_uninit_isoc(struct em28xx *dev)
  540. {
  541. int i;
  542. for (i = 0; i < EM28XX_NUM_BUFS; i++) {
  543. if (dev->urb[i]) {
  544. usb_kill_urb(dev->urb[i]);
  545. if (dev->transfer_buffer[i]){
  546. usb_buffer_free(dev->udev,(EM28XX_NUM_PACKETS*dev->max_pkt_size),dev->transfer_buffer[i],dev->urb[i]->transfer_dma);
  547. }
  548. usb_free_urb(dev->urb[i]);
  549. }
  550. dev->urb[i] = NULL;
  551. dev->transfer_buffer[i] = NULL;
  552. }
  553. em28xx_capture_start(dev, 0);
  554. }
  555. /*
  556. * em28xx_init_isoc()
  557. * allocates transfer buffers and submits the urbs for isoc transfer
  558. */
  559. int em28xx_init_isoc(struct em28xx *dev)
  560. {
  561. /* change interface to 3 which allowes the biggest packet sizes */
  562. int i, errCode;
  563. const int sb_size = EM28XX_NUM_PACKETS * dev->max_pkt_size;
  564. /* reset streaming vars */
  565. dev->frame_current = NULL;
  566. dev->frame_count = 0;
  567. /* allocate urbs */
  568. for (i = 0; i < EM28XX_NUM_BUFS; i++) {
  569. struct urb *urb;
  570. int j, k;
  571. /* allocate transfer buffer */
  572. urb = usb_alloc_urb(EM28XX_NUM_PACKETS, GFP_KERNEL);
  573. if (!urb){
  574. em28xx_errdev("cannot alloc urb %i\n", i);
  575. em28xx_uninit_isoc(dev);
  576. return -ENOMEM;
  577. }
  578. dev->transfer_buffer[i] = usb_buffer_alloc(dev->udev, sb_size, GFP_KERNEL,&urb->transfer_dma);
  579. if (!dev->transfer_buffer[i]) {
  580. em28xx_errdev
  581. ("unable to allocate %i bytes for transfer buffer %i\n",
  582. sb_size, i);
  583. em28xx_uninit_isoc(dev);
  584. return -ENOMEM;
  585. }
  586. memset(dev->transfer_buffer[i], 0, sb_size);
  587. urb->dev = dev->udev;
  588. urb->context = dev;
  589. urb->pipe = usb_rcvisocpipe(dev->udev, 0x82);
  590. urb->transfer_flags = URB_ISO_ASAP;
  591. urb->interval = 1;
  592. urb->transfer_buffer = dev->transfer_buffer[i];
  593. urb->complete = em28xx_isocIrq;
  594. urb->number_of_packets = EM28XX_NUM_PACKETS;
  595. urb->transfer_buffer_length = sb_size;
  596. for (j = k = 0; j < EM28XX_NUM_PACKETS;
  597. j++, k += dev->max_pkt_size) {
  598. urb->iso_frame_desc[j].offset = k;
  599. urb->iso_frame_desc[j].length =
  600. dev->max_pkt_size;
  601. }
  602. dev->urb[i] = urb;
  603. }
  604. /* submit urbs */
  605. for (i = 0; i < EM28XX_NUM_BUFS; i++) {
  606. errCode = usb_submit_urb(dev->urb[i], GFP_KERNEL);
  607. if (errCode) {
  608. em28xx_errdev("submit of urb %i failed (error=%i)\n", i,
  609. errCode);
  610. em28xx_uninit_isoc(dev);
  611. return errCode;
  612. }
  613. }
  614. return 0;
  615. }
  616. int em28xx_set_alternate(struct em28xx *dev)
  617. {
  618. int errCode, prev_alt = dev->alt;
  619. dev->alt = alt;
  620. if (dev->alt == 0) {
  621. int i;
  622. for(i=0;i< dev->num_alt; i++)
  623. if(dev->alt_max_pkt_size[i]>dev->alt_max_pkt_size[dev->alt])
  624. dev->alt=i;
  625. }
  626. if (dev->alt != prev_alt) {
  627. dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
  628. em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n", dev->alt,
  629. dev->max_pkt_size);
  630. errCode = usb_set_interface(dev->udev, 0, dev->alt);
  631. if (errCode < 0) {
  632. em28xx_errdev ("cannot change alternate number to %d (error=%i)\n",
  633. dev->alt, errCode);
  634. return errCode;
  635. }
  636. }
  637. return 0;
  638. }