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