2 em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com>
6 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7 Sascha Sommer <saschasommer@freenet.de>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/usb.h>
29 #include <linux/vmalloc.h>
33 /* #define ENABLE_DEBUG_ISOC_FRAMES */
35 static unsigned int core_debug = 0;
36 module_param(core_debug,int,0644);
37 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
39 #define em28xx_coredbg(fmt, arg...) do {\
41 printk(KERN_INFO "%s %s :"fmt, \
42 dev->name, __FUNCTION__ , ##arg); } while (0)
44 static unsigned int reg_debug = 0;
45 module_param(reg_debug,int,0644);
46 MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]");
48 #define em28xx_regdbg(fmt, arg...) do {\
50 printk(KERN_INFO "%s %s :"fmt, \
51 dev->name, __FUNCTION__ , ##arg); } while (0)
53 static unsigned int isoc_debug = 0;
54 module_param(isoc_debug,int,0644);
55 MODULE_PARM_DESC(isoc_debug,"enable debug messages [isoc transfers]");
57 #define em28xx_isocdbg(fmt, arg...) do {\
59 printk(KERN_INFO "%s %s :"fmt, \
60 dev->name, __FUNCTION__ , ##arg); } while (0)
62 static int alt = EM28XX_PINOUT;
63 module_param(alt, int, 0644);
64 MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
68 * em28xx_request_buffers()
69 * allocate a number of buffers
71 u32 em28xx_request_buffers(struct em28xx *dev, u32 count)
73 const size_t imagesize = PAGE_ALIGN(dev->frame_size); /*needs to be page aligned cause the buffers can be mapped individually! */
76 em28xx_coredbg("requested %i buffers with size %zi", count, imagesize);
77 if (count > EM28XX_NUM_FRAMES)
78 count = EM28XX_NUM_FRAMES;
80 dev->num_frames = count;
81 while (dev->num_frames > 0) {
82 if ((buff = vmalloc_32(dev->num_frames * imagesize))) {
83 memset(buff, 0, dev->num_frames * imagesize);
89 for (i = 0; i < dev->num_frames; i++) {
90 dev->frame[i].bufmem = buff + i * imagesize;
91 dev->frame[i].buf.index = i;
92 dev->frame[i].buf.m.offset = i * imagesize;
93 dev->frame[i].buf.length = dev->frame_size;
94 dev->frame[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
95 dev->frame[i].buf.sequence = 0;
96 dev->frame[i].buf.field = V4L2_FIELD_NONE;
97 dev->frame[i].buf.memory = V4L2_MEMORY_MMAP;
98 dev->frame[i].buf.flags = 0;
100 return dev->num_frames;
104 * em28xx_queue_unusedframes()
105 * add all frames that are not currently in use to the inbuffer queue
107 void em28xx_queue_unusedframes(struct em28xx *dev)
109 unsigned long lock_flags;
112 for (i = 0; i < dev->num_frames; i++)
113 if (dev->frame[i].state == F_UNUSED) {
114 dev->frame[i].state = F_QUEUED;
115 spin_lock_irqsave(&dev->queue_lock, lock_flags);
116 list_add_tail(&dev->frame[i].frame, &dev->inqueue);
117 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
122 * em28xx_release_buffers()
125 void em28xx_release_buffers(struct em28xx *dev)
127 if (dev->num_frames) {
128 vfree(dev->frame[0].bufmem);
134 * em28xx_read_reg_req()
135 * reads data from the usb device specifying bRequest
137 int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg,
142 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, buf, len, HZ);
149 printk(ret < 0 ? " failed!\n" : "%02x values: ", ret);
150 for (byte = 0; byte < len; byte++) {
151 printk(" %02x", buf[byte]);
160 * em28xx_read_reg_req()
161 * reads data from the usb device specifying bRequest
163 int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg)
168 em28xx_regdbg("req=%02x, reg=%02x:", req, reg);
170 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), req,
171 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
172 0x0000, reg, &val, 1, HZ);
175 printk(ret < 0 ? " failed!\n" : "%02x\n", val);
183 int em28xx_read_reg(struct em28xx *dev, u16 reg)
185 return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg);
189 * em28xx_write_regs_req()
190 * sends data to the usb device, specifying bRequest
192 int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf,
197 /*usb_control_msg seems to expect a kmalloced buffer */
198 unsigned char *bufs = kmalloc(len, GFP_KERNEL);
200 em28xx_regdbg("req=%02x reg=%02x:", req, reg);
204 for (i = 0; i < len; ++i)
205 printk (" %02x", (unsigned char)buf[i]);
211 memcpy(bufs, buf, len);
212 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), req,
213 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
214 0x0000, reg, bufs, len, HZ);
215 mdelay(5); /* FIXME: magic number */
220 int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len)
222 return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len);
226 * em28xx_write_reg_bits()
227 * sets only some bits (specified by bitmask) of a register, by first reading
230 int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val,
235 if ((oldval = em28xx_read_reg(dev, reg)) < 0)
237 newval = (((u8) oldval) & ~bitmask) | (val & bitmask);
238 return em28xx_write_regs(dev, reg, &newval, 1);
242 * em28xx_write_ac97()
243 * write a 16 bit value to the specified AC97 address (LSB first!)
245 int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 * val)
248 u8 addr = reg & 0x7f;
249 if ((ret = em28xx_write_regs(dev, AC97LSB_REG, val, 2)) < 0)
251 if ((ret = em28xx_write_regs(dev, AC97ADDR_REG, &addr, 1)) < 0)
253 if ((ret = em28xx_read_reg(dev, AC97BUSY_REG)) < 0)
255 else if (((u8) ret) & 0x01) {
256 em28xx_warn ("AC97 command still being exectuted: not handled properly!\n");
261 int em28xx_audio_analog_set(struct em28xx *dev)
263 char s[2] = { 0x00, 0x00 };
264 s[0] |= 0x1f - dev->volume;
265 s[1] |= 0x1f - dev->volume;
268 return em28xx_write_ac97(dev, MASTER_AC97, s);
272 int em28xx_colorlevels_set_default(struct em28xx *dev)
274 em28xx_write_regs(dev, YGAIN_REG, "\x10", 1); /* contrast */
275 em28xx_write_regs(dev, YOFFSET_REG, "\x00", 1); /* brightness */
276 em28xx_write_regs(dev, UVGAIN_REG, "\x10", 1); /* saturation */
277 em28xx_write_regs(dev, UOFFSET_REG, "\x00", 1);
278 em28xx_write_regs(dev, VOFFSET_REG, "\x00", 1);
279 em28xx_write_regs(dev, SHARPNESS_REG, "\x00", 1);
281 em28xx_write_regs(dev, GAMMA_REG, "\x20", 1);
282 em28xx_write_regs(dev, RGAIN_REG, "\x20", 1);
283 em28xx_write_regs(dev, GGAIN_REG, "\x20", 1);
284 em28xx_write_regs(dev, BGAIN_REG, "\x20", 1);
285 em28xx_write_regs(dev, ROFFSET_REG, "\x00", 1);
286 em28xx_write_regs(dev, GOFFSET_REG, "\x00", 1);
287 return em28xx_write_regs(dev, BOFFSET_REG, "\x00", 1);
290 int em28xx_capture_start(struct em28xx *dev, int start)
293 /* FIXME: which is the best order? */
294 /* video registers are sampled by VREF */
295 if ((ret = em28xx_write_reg_bits(dev, USBSUSP_REG, start ? 0x10 : 0x00,
298 /* enable video capture */
299 return em28xx_write_regs(dev, VINENABLE_REG, start ? "\x67" : "\x27", 1);
302 int em28xx_outfmt_set_yuv422(struct em28xx *dev)
304 em28xx_write_regs(dev, OUTFMT_REG, "\x34", 1);
305 em28xx_write_regs(dev, VINMODE_REG, "\x10", 1);
306 return em28xx_write_regs(dev, VINCTRL_REG, "\x11", 1);
309 int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax, u8 ymin,
312 em28xx_coredbg("em28xx Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax);
314 em28xx_write_regs(dev, XMIN_REG, &xmin, 1);
315 em28xx_write_regs(dev, XMAX_REG, &xmax, 1);
316 em28xx_write_regs(dev, YMIN_REG, &ymin, 1);
317 return em28xx_write_regs(dev, YMAX_REG, &ymax, 1);
320 int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
321 u16 width, u16 height)
325 u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01);
327 em28xx_coredbg("em28xx Area Set: (%d,%d)\n", (width | (overflow & 2) << 7),
328 (height | (overflow & 1) << 8));
330 em28xx_write_regs(dev, HSTART_REG, &hstart, 1);
331 em28xx_write_regs(dev, VSTART_REG, &vstart, 1);
332 em28xx_write_regs(dev, CWIDTH_REG, &cwidth, 1);
333 em28xx_write_regs(dev, CHEIGHT_REG, &cheight, 1);
334 return em28xx_write_regs(dev, OFLOW_REG, &overflow, 1);
337 int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
340 /* the em2800 scaler only supports scaling down to 50% */
342 mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
347 em28xx_write_regs(dev, HSCALELOW_REG, (char *)buf, 2);
350 em28xx_write_regs(dev, VSCALELOW_REG, (char *)buf, 2);
351 /* it seems that both H and V scalers must be active to work correctly */
352 mode = (h || v)? 0x30: 0x00;
354 return em28xx_write_reg_bits(dev, COMPR_REG, mode, 0x30);
357 /* FIXME: this only function read values from dev */
358 int em28xx_resolution_set(struct em28xx *dev)
361 width = norm_maxw(dev);
362 height = norm_maxh(dev) >> 1;
364 em28xx_outfmt_set_yuv422(dev);
365 em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
366 em28xx_capture_area_set(dev, 0, 0, width >> 2, height >> 2);
367 return em28xx_scaler_set(dev, dev->hscale, dev->vscale);
371 /******************* isoc transfer handling ****************************/
373 #ifdef ENABLE_DEBUG_ISOC_FRAMES
374 static void em28xx_isoc_dump(struct urb *urb, struct pt_regs *regs)
380 printk(KERN_DEBUG "isocIrq: sf=%d np=%d ec=%x\n",
381 urb->start_frame, urb->number_of_packets,
383 for (i = 0; i < urb->number_of_packets; i++) {
385 urb->transfer_buffer +
386 urb->iso_frame_desc[i].offset;
387 int alen = urb->iso_frame_desc[i].actual_length;
389 if (buf[0] == 0x88) {
392 } else if (buf[0] == 0x22) {
394 "= l=%d nt=%d bpp=%d\n",
395 len - 4 * ntrans, ntrans,
396 ntrans == 0 ? 0 : len / ntrans);
400 printk(KERN_DEBUG "!\n");
402 printk(KERN_DEBUG " n=%d s=%d al=%d %x\n", i,
403 urb->iso_frame_desc[i].status,
404 urb->iso_frame_desc[i].actual_length,
406 *((unsigned char *)(urb->transfer_buffer +
407 urb->iso_frame_desc[i].
413 static inline int em28xx_isoc_video(struct em28xx *dev,struct em28xx_frame_t **f,
414 unsigned long *lock_flags, unsigned char buf)
417 if ((*f)->state == F_GRABBING) {
418 /*previous frame is incomplete */
419 if ((*f)->fieldbytesused < dev->field_size) {
420 (*f)->state = F_ERROR;
421 em28xx_isocdbg ("dropping incomplete bottom field (%i missing bytes)",
422 dev->field_size-(*f)->fieldbytesused);
424 (*f)->state = F_DONE;
425 (*f)->buf.bytesused = dev->frame_size;
428 if ((*f)->state == F_DONE || (*f)->state == F_ERROR) {
429 /* move current frame to outqueue and get next free buffer from inqueue */
430 spin_lock_irqsave(&dev-> queue_lock, *lock_flags);
431 list_move_tail(&(*f)->frame, &dev->outqueue);
432 if (!list_empty(&dev->inqueue))
433 (*f) = list_entry(dev-> inqueue.next,
434 struct em28xx_frame_t,frame);
437 spin_unlock_irqrestore(&dev->queue_lock,*lock_flags);
440 em28xx_isocdbg ("new frame but no buffer is free");
443 do_gettimeofday(&(*f)->buf.timestamp);
444 (*f)->buf.sequence = ++dev->frame_count;
445 (*f)->buf.field = V4L2_FIELD_INTERLACED;
446 (*f)->state = F_GRABBING;
447 (*f)->buf.bytesused = 0;
449 (*f)->fieldbytesused = 0;
451 /* acquiring bottom field */
452 if ((*f)->state == F_GRABBING) {
453 if (!(*f)->top_field) {
454 (*f)->state = F_ERROR;
455 em28xx_isocdbg ("unexpected begin of bottom field; discarding it");
456 } else if ((*f)-> fieldbytesused < dev->field_size - 172) {
457 (*f)->state = F_ERROR;
458 em28xx_isocdbg ("dropping incomplete top field (%i missing bytes)",
459 dev->field_size-(*f)->fieldbytesused);
462 (*f)->fieldbytesused = 0;
469 static inline void em28xx_isoc_video_copy(struct em28xx *dev,
470 struct em28xx_frame_t **f, unsigned char *buf, int len)
472 void *fieldstart, *startwrite, *startread;
473 int linesdone, currlinedone, offset, lencopy,remain;
475 if(dev->frame_size != (*f)->buf.length){
476 em28xx_err("frame_size %i and buf.length %i are different!!!\n",dev->frame_size,(*f)->buf.length);
480 if ((*f)->fieldbytesused + len > dev->field_size)
481 len =dev->field_size - (*f)->fieldbytesused;
483 if (buf[0] != 0x88 && buf[0] != 0x22) {
484 em28xx_isocdbg("frame is not complete\n");
493 fieldstart = (*f)->bufmem;
495 fieldstart = (*f)->bufmem + dev->bytesperline;
497 linesdone = (*f)->fieldbytesused / dev->bytesperline;
498 currlinedone = (*f)->fieldbytesused % dev->bytesperline;
499 offset = linesdone * dev->bytesperline * 2 + currlinedone;
500 startwrite = fieldstart + offset;
501 lencopy = dev->bytesperline - currlinedone;
502 lencopy = lencopy > remain ? remain : lencopy;
504 memcpy(startwrite, startread, lencopy);
508 startwrite += lencopy + dev->bytesperline;
509 startread += lencopy;
510 if (dev->bytesperline > remain)
513 lencopy = dev->bytesperline;
515 memcpy(startwrite, startread, lencopy);
519 (*f)->fieldbytesused += len;
524 * handles the incoming isoc urbs and fills the frames from our inqueue
526 void em28xx_isocIrq(struct urb *urb, struct pt_regs *regs)
528 struct em28xx *dev = urb->context;
530 struct em28xx_frame_t **f;
531 unsigned long lock_flags;
535 #ifdef ENABLE_DEBUG_ISOC_FRAMES
537 em28xx_isoc_dump(urb, regs);
540 if (urb->status == -ENOENT)
543 f = &dev->frame_current;
545 if (dev->stream == STREAM_INTERRUPT) {
546 dev->stream = STREAM_OFF;
548 (*f)->state = F_QUEUED;
549 em28xx_isocdbg("stream interrupted");
550 wake_up_interruptible(&dev->wait_stream);
553 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
556 if (dev->stream == STREAM_ON && !list_empty(&dev->inqueue)) {
558 (*f) = list_entry(dev->inqueue.next,
559 struct em28xx_frame_t, frame);
561 for (i = 0; i < urb->number_of_packets; i++) {
562 unsigned char *buf = urb->transfer_buffer +
563 urb->iso_frame_desc[i].offset;
564 int len = urb->iso_frame_desc[i].actual_length - 4;
566 if (urb->iso_frame_desc[i].status) {
567 em28xx_isocdbg("data error: [%d] len=%d, status=%d", i,
568 urb->iso_frame_desc[i].actual_length,
569 urb->iso_frame_desc[i].status);
570 if (urb->iso_frame_desc[i].status != -EPROTO)
573 if (urb->iso_frame_desc[i].actual_length <= 0) {
574 em28xx_isocdbg("packet %d is empty",i);
577 if (urb->iso_frame_desc[i].actual_length >
579 em28xx_isocdbg("packet bigger than packet size");
583 if (buf[0] == 0x22 && buf[1] == 0x5a) {
584 em28xx_isocdbg("Video frame, length=%i!",len);
586 if (em28xx_isoc_video(dev,f,&lock_flags,buf[2]))
588 } else if (buf[0]==0x33 && buf[1]==0x95 && buf[2]==0x00) {
589 em28xx_isocdbg("VBI HEADER!!!");
593 if ((*f)->state == F_GRABBING) {
594 em28xx_isoc_video_copy(dev,f,buf, len);
599 for (i = 0; i < urb->number_of_packets; i++) {
600 urb->iso_frame_desc[i].status = 0;
601 urb->iso_frame_desc[i].actual_length = 0;
605 if ((status = usb_submit_urb(urb, GFP_ATOMIC))) {
606 em28xx_errdev("resubmit of urb failed (error=%i)\n", status);
607 dev->state |= DEV_MISCONFIGURED;
609 wake_up_interruptible(&dev->wait_frame);
614 * em28xx_uninit_isoc()
615 * deallocates the buffers and urbs allocated during em28xx_init_iosc()
617 void em28xx_uninit_isoc(struct em28xx *dev)
621 for (i = 0; i < EM28XX_NUM_BUFS; i++) {
623 usb_kill_urb(dev->urb[i]);
624 if (dev->transfer_buffer[i]){
625 usb_buffer_free(dev->udev,(EM28XX_NUM_PACKETS*dev->max_pkt_size),dev->transfer_buffer[i],dev->urb[i]->transfer_dma);
627 usb_free_urb(dev->urb[i]);
630 dev->transfer_buffer[i] = NULL;
632 em28xx_capture_start(dev, 0);
637 * allocates transfer buffers and submits the urbs for isoc transfer
639 int em28xx_init_isoc(struct em28xx *dev)
641 /* change interface to 3 which allowes the biggest packet sizes */
643 const int sb_size = EM28XX_NUM_PACKETS * dev->max_pkt_size;
645 /* reset streaming vars */
646 dev->frame_current = NULL;
647 dev->frame_count = 0;
650 for (i = 0; i < EM28XX_NUM_BUFS; i++) {
653 /* allocate transfer buffer */
654 urb = usb_alloc_urb(EM28XX_NUM_PACKETS, GFP_KERNEL);
656 em28xx_errdev("cannot alloc urb %i\n", i);
657 em28xx_uninit_isoc(dev);
660 dev->transfer_buffer[i] = usb_buffer_alloc(dev->udev, sb_size, GFP_KERNEL,&urb->transfer_dma);
661 if (!dev->transfer_buffer[i]) {
663 ("unable to allocate %i bytes for transfer buffer %i\n",
665 em28xx_uninit_isoc(dev);
668 memset(dev->transfer_buffer[i], 0, sb_size);
669 urb->dev = dev->udev;
671 urb->pipe = usb_rcvisocpipe(dev->udev, 0x82);
672 urb->transfer_flags = URB_ISO_ASAP;
674 urb->transfer_buffer = dev->transfer_buffer[i];
675 urb->complete = em28xx_isocIrq;
676 urb->number_of_packets = EM28XX_NUM_PACKETS;
677 urb->transfer_buffer_length = sb_size;
678 for (j = k = 0; j < EM28XX_NUM_PACKETS;
679 j++, k += dev->max_pkt_size) {
680 urb->iso_frame_desc[j].offset = k;
681 urb->iso_frame_desc[j].length =
688 for (i = 0; i < EM28XX_NUM_BUFS; i++) {
689 errCode = usb_submit_urb(dev->urb[i], GFP_KERNEL);
691 em28xx_errdev("submit of urb %i failed (error=%i)\n", i,
693 em28xx_uninit_isoc(dev);
701 int em28xx_set_alternate(struct em28xx *dev)
703 int errCode, prev_alt = dev->alt;
707 for(i=0;i< dev->num_alt; i++)
708 if(dev->alt_max_pkt_size[i]>dev->alt_max_pkt_size[dev->alt])
712 if (dev->alt != prev_alt) {
713 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
714 em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n", dev->alt,
716 errCode = usb_set_interface(dev->udev, 0, dev->alt);
718 em28xx_errdev ("cannot change alternate number to %d (error=%i)\n",