em28xx-core.c 22 KB

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