Commit | Line | Data |
---|---|---|
a6c2ba28 | 1 | /* |
3acf2809 | 2 | em28xx-core.c - driver for Empia EM2800/EM2820/2840 USB video capture devices |
a6c2ba28 | 3 | |
f7abcd38 MCC |
4 | Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> |
5 | Markus Rechberger <mrechberger@gmail.com> | |
4ac97914 | 6 | Mauro Carvalho Chehab <mchehab@brturbo.com.br> |
f7abcd38 | 7 | Sascha Sommer <saschasommer@freenet.de> |
a6c2ba28 | 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> | |
a6c2ba28 | 28 | #include <linux/usb.h> |
29 | #include <linux/vmalloc.h> | |
30 | ||
f7abcd38 | 31 | #include "em28xx.h" |
a6c2ba28 | 32 | |
33 | /* #define ENABLE_DEBUG_ISOC_FRAMES */ | |
34 | ||
943a4902 | 35 | static unsigned int core_debug; |
a6c2ba28 | 36 | module_param(core_debug,int,0644); |
37 | MODULE_PARM_DESC(core_debug,"enable debug messages [core]"); | |
38 | ||
3acf2809 | 39 | #define em28xx_coredbg(fmt, arg...) do {\ |
4ac97914 MCC |
40 | if (core_debug) \ |
41 | printk(KERN_INFO "%s %s :"fmt, \ | |
674434c6 | 42 | dev->name, __FUNCTION__, ##arg); } while (0) |
a6c2ba28 | 43 | |
943a4902 | 44 | static unsigned int reg_debug; |
a6c2ba28 | 45 | module_param(reg_debug,int,0644); |
46 | MODULE_PARM_DESC(reg_debug,"enable debug messages [URB reg]"); | |
47 | ||
3acf2809 | 48 | #define em28xx_regdbg(fmt, arg...) do {\ |
4ac97914 MCC |
49 | if (reg_debug) \ |
50 | printk(KERN_INFO "%s %s :"fmt, \ | |
674434c6 | 51 | dev->name, __FUNCTION__, ##arg); } while (0) |
a6c2ba28 | 52 | |
943a4902 | 53 | static unsigned int isoc_debug; |
a6c2ba28 | 54 | module_param(isoc_debug,int,0644); |
55b8b2d1 | 55 | MODULE_PARM_DESC(isoc_debug,"enable debug messages [isoc transfers]"); |
a6c2ba28 | 56 | |
3acf2809 | 57 | #define em28xx_isocdbg(fmt, arg...) do {\ |
4ac97914 MCC |
58 | if (isoc_debug) \ |
59 | printk(KERN_INFO "%s %s :"fmt, \ | |
674434c6 | 60 | dev->name, __FUNCTION__, ##arg); } while (0) |
a6c2ba28 | 61 | |
3acf2809 | 62 | static int alt = EM28XX_PINOUT; |
a6c2ba28 | 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 | ||
3acf2809 | 91 | void em28xx_print_ioctl(char *name, unsigned int cmd) |
a6c2ba28 | 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 | ||
674434c6 | 119 | |
a6c2ba28 | 120 | /* |
3acf2809 | 121 | * em28xx_request_buffers() |
a6c2ba28 | 122 | * allocate a number of buffers |
123 | */ | |
3acf2809 | 124 | u32 em28xx_request_buffers(struct em28xx *dev, u32 count) |
a6c2ba28 | 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; | |
8bcc2476 | 129 | em28xx_coredbg("requested %i buffers with size %zd", count, imagesize); |
3acf2809 MCC |
130 | if (count > EM28XX_NUM_FRAMES) |
131 | count = EM28XX_NUM_FRAMES; | |
a6c2ba28 | 132 | |
133 | dev->num_frames = count; | |
134 | while (dev->num_frames > 0) { | |
3639c861 SS |
135 | if ((buff = vmalloc_32(dev->num_frames * imagesize))) { |
136 | memset(buff, 0, dev->num_frames * imagesize); | |
a6c2ba28 | 137 | break; |
3639c861 | 138 | } |
a6c2ba28 | 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 | /* | |
3acf2809 | 157 | * em28xx_queue_unusedframes() |
a6c2ba28 | 158 | * add all frames that are not currently in use to the inbuffer queue |
159 | */ | |
3acf2809 | 160 | void em28xx_queue_unusedframes(struct em28xx *dev) |
a6c2ba28 | 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 | /* | |
3acf2809 | 175 | * em28xx_release_buffers() |
a6c2ba28 | 176 | * free frame buffers |
177 | */ | |
3acf2809 | 178 | void em28xx_release_buffers(struct em28xx *dev) |
a6c2ba28 | 179 | { |
180 | if (dev->num_frames) { | |
3639c861 | 181 | vfree(dev->frame[0].bufmem); |
a6c2ba28 | 182 | dev->num_frames = 0; |
183 | } | |
184 | } | |
185 | ||
186 | /* | |
3acf2809 | 187 | * em28xx_read_reg_req() |
a6c2ba28 | 188 | * reads data from the usb device specifying bRequest |
189 | */ | |
3acf2809 | 190 | int em28xx_read_reg_req_len(struct em28xx *dev, u8 req, u16 reg, |
a6c2ba28 | 191 | char *buf, int len) |
192 | { | |
193 | int ret, byte; | |
194 | ||
3acf2809 | 195 | em28xx_regdbg("req=%02x, reg=%02x ", req, reg); |
a6c2ba28 | 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 | /* | |
3acf2809 | 213 | * em28xx_read_reg_req() |
a6c2ba28 | 214 | * reads data from the usb device specifying bRequest |
215 | */ | |
3acf2809 | 216 | int em28xx_read_reg_req(struct em28xx *dev, u8 req, u16 reg) |
a6c2ba28 | 217 | { |
218 | u8 val; | |
219 | int ret; | |
220 | ||
3acf2809 | 221 | em28xx_regdbg("req=%02x, reg=%02x:", req, reg); |
a6c2ba28 | 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 | ||
3acf2809 | 236 | int em28xx_read_reg(struct em28xx *dev, u16 reg) |
a6c2ba28 | 237 | { |
3acf2809 | 238 | return em28xx_read_reg_req(dev, USB_REQ_GET_STATUS, reg); |
a6c2ba28 | 239 | } |
240 | ||
241 | /* | |
3acf2809 | 242 | * em28xx_write_regs_req() |
a6c2ba28 | 243 | * sends data to the usb device, specifying bRequest |
244 | */ | |
3acf2809 | 245 | int em28xx_write_regs_req(struct em28xx *dev, u8 req, u16 reg, char *buf, |
a6c2ba28 | 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 | ||
3acf2809 | 253 | em28xx_regdbg("req=%02x reg=%02x:", req, reg); |
a6c2ba28 | 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 | ||
3acf2809 | 273 | int em28xx_write_regs(struct em28xx *dev, u16 reg, char *buf, int len) |
a6c2ba28 | 274 | { |
3acf2809 | 275 | return em28xx_write_regs_req(dev, USB_REQ_GET_STATUS, reg, buf, len); |
a6c2ba28 | 276 | } |
277 | ||
278 | /* | |
3acf2809 | 279 | * em28xx_write_reg_bits() |
a6c2ba28 | 280 | * sets only some bits (specified by bitmask) of a register, by first reading |
281 | * the actual value | |
282 | */ | |
3acf2809 | 283 | int em28xx_write_reg_bits(struct em28xx *dev, u16 reg, u8 val, |
a6c2ba28 | 284 | u8 bitmask) |
285 | { | |
286 | int oldval; | |
287 | u8 newval; | |
3acf2809 | 288 | if ((oldval = em28xx_read_reg(dev, reg)) < 0) |
a6c2ba28 | 289 | return oldval; |
290 | newval = (((u8) oldval) & ~bitmask) | (val & bitmask); | |
3acf2809 | 291 | return em28xx_write_regs(dev, reg, &newval, 1); |
a6c2ba28 | 292 | } |
293 | ||
294 | /* | |
3acf2809 | 295 | * em28xx_write_ac97() |
a6c2ba28 | 296 | * write a 16 bit value to the specified AC97 address (LSB first!) |
297 | */ | |
3acf2809 | 298 | int em28xx_write_ac97(struct em28xx *dev, u8 reg, u8 * val) |
a6c2ba28 | 299 | { |
300 | int ret; | |
301 | u8 addr = reg & 0x7f; | |
3acf2809 | 302 | if ((ret = em28xx_write_regs(dev, AC97LSB_REG, val, 2)) < 0) |
a6c2ba28 | 303 | return ret; |
3acf2809 | 304 | if ((ret = em28xx_write_regs(dev, AC97ADDR_REG, &addr, 1)) < 0) |
a6c2ba28 | 305 | return ret; |
3acf2809 | 306 | if ((ret = em28xx_read_reg(dev, AC97BUSY_REG)) < 0) |
a6c2ba28 | 307 | return ret; |
308 | else if (((u8) ret) & 0x01) { | |
3acf2809 | 309 | em28xx_warn ("AC97 command still being exectuted: not handled properly!\n"); |
a6c2ba28 | 310 | } |
311 | return 0; | |
312 | } | |
313 | ||
3acf2809 | 314 | int em28xx_audio_analog_set(struct em28xx *dev) |
a6c2ba28 | 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; | |
3acf2809 | 321 | return em28xx_write_ac97(dev, MASTER_AC97, s); |
a6c2ba28 | 322 | } |
323 | ||
324 | ||
3acf2809 | 325 | int em28xx_colorlevels_set_default(struct em28xx *dev) |
a6c2ba28 | 326 | { |
3acf2809 MCC |
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); | |
333 | ||
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); | |
a6c2ba28 | 341 | } |
342 | ||
3acf2809 | 343 | int em28xx_capture_start(struct em28xx *dev, int start) |
a6c2ba28 | 344 | { |
345 | int ret; | |
346 | /* FIXME: which is the best order? */ | |
347 | /* video registers are sampled by VREF */ | |
3acf2809 | 348 | if ((ret = em28xx_write_reg_bits(dev, USBSUSP_REG, start ? 0x10 : 0x00, |
a6c2ba28 | 349 | 0x10)) < 0) |
350 | return ret; | |
351 | /* enable video capture */ | |
3acf2809 | 352 | return em28xx_write_regs(dev, VINENABLE_REG, start ? "\x67" : "\x27", 1); |
a6c2ba28 | 353 | } |
354 | ||
3acf2809 | 355 | int em28xx_outfmt_set_yuv422(struct em28xx *dev) |
a6c2ba28 | 356 | { |
3acf2809 MCC |
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); | |
a6c2ba28 | 360 | } |
361 | ||
3acf2809 | 362 | int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax, u8 ymin, |
a6c2ba28 | 363 | u8 ymax) |
364 | { | |
3acf2809 | 365 | em28xx_coredbg("em28xx Scale: (%d,%d)-(%d,%d)\n", xmin, ymin, xmax, ymax); |
a6c2ba28 | 366 | |
3acf2809 MCC |
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); | |
a6c2ba28 | 371 | } |
372 | ||
3acf2809 | 373 | int em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart, |
a6c2ba28 | 374 | u16 width, u16 height) |
375 | { | |
376 | u8 cwidth = width; | |
377 | u8 cheight = height; | |
378 | u8 overflow = (height >> 7 & 0x02) | (width >> 8 & 0x01); | |
379 | ||
3acf2809 | 380 | em28xx_coredbg("em28xx Area Set: (%d,%d)\n", (width | (overflow & 2) << 7), |
a6c2ba28 | 381 | (height | (overflow & 1) << 8)); |
382 | ||
3acf2809 MCC |
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); | |
a6c2ba28 | 388 | } |
389 | ||
3acf2809 | 390 | int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v) |
a6c2ba28 | 391 | { |
52c02fcd SS |
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; | |
3acf2809 | 400 | em28xx_write_regs(dev, HSCALELOW_REG, (char *)buf, 2); |
52c02fcd SS |
401 | buf[0] = v; |
402 | buf[1] = v >> 8; | |
3acf2809 | 403 | em28xx_write_regs(dev, VSCALELOW_REG, (char *)buf, 2); |
52c02fcd SS |
404 | /* it seems that both H and V scalers must be active to work correctly */ |
405 | mode = (h || v)? 0x30: 0x00; | |
74458e6c | 406 | } |
3acf2809 | 407 | return em28xx_write_reg_bits(dev, COMPR_REG, mode, 0x30); |
a6c2ba28 | 408 | } |
409 | ||
410 | /* FIXME: this only function read values from dev */ | |
3acf2809 | 411 | int em28xx_resolution_set(struct em28xx *dev) |
a6c2ba28 | 412 | { |
413 | int width, height; | |
414 | width = norm_maxw(dev); | |
415 | height = norm_maxh(dev) >> 1; | |
416 | ||
3acf2809 MCC |
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); | |
a6c2ba28 | 421 | } |
422 | ||
423 | ||
424 | /******************* isoc transfer handling ****************************/ | |
425 | ||
426 | #ifdef ENABLE_DEBUG_ISOC_FRAMES | |
3acf2809 | 427 | static void em28xx_isoc_dump(struct urb *urb, struct pt_regs *regs) |
a6c2ba28 | 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 | ||
3acf2809 | 466 | static inline int em28xx_isoc_video(struct em28xx *dev,struct em28xx_frame_t **f, |
a6c2ba28 | 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; | |
3acf2809 | 474 | em28xx_isocdbg ("dropping incomplete bottom field (%i missing bytes)", |
a6c2ba28 | 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, | |
3acf2809 | 487 | struct em28xx_frame_t,frame); |
a6c2ba28 | 488 | else |
489 | (*f) = NULL; | |
490 | spin_unlock_irqrestore(&dev->queue_lock,*lock_flags); | |
491 | } | |
492 | if (!(*f)) { | |
3acf2809 | 493 | em28xx_isocdbg ("new frame but no buffer is free"); |
a6c2ba28 | 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; | |
3acf2809 | 508 | em28xx_isocdbg ("unexpected begin of bottom field; discarding it"); |
a6c2ba28 | 509 | } else if ((*f)-> fieldbytesused < dev->field_size - 172) { |
510 | (*f)->state = F_ERROR; | |
3acf2809 | 511 | em28xx_isocdbg ("dropping incomplete top field (%i missing bytes)", |
a6c2ba28 | 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 | ||
3acf2809 MCC |
522 | static inline void em28xx_isoc_video_copy(struct em28xx *dev, |
523 | struct em28xx_frame_t **f, unsigned char *buf, int len) | |
a6c2ba28 | 524 | { |
525 | void *fieldstart, *startwrite, *startread; | |
526 | int linesdone, currlinedone, offset, lencopy,remain; | |
527 | ||
596d92d5 | 528 | if(dev->frame_size != (*f)->buf.length){ |
3acf2809 | 529 | em28xx_err("frame_size %i and buf.length %i are different!!!\n",dev->frame_size,(*f)->buf.length); |
596d92d5 MCC |
530 | return; |
531 | } | |
532 | ||
a6c2ba28 | 533 | if ((*f)->fieldbytesused + len > dev->field_size) |
534 | len =dev->field_size - (*f)->fieldbytesused; | |
feff0485 MCC |
535 | |
536 | if (buf[0] != 0x88 && buf[0] != 0x22) { | |
3acf2809 | 537 | em28xx_isocdbg("frame is not complete\n"); |
feff0485 MCC |
538 | startread = buf; |
539 | len+=4; | |
540 | } else | |
541 | startread = buf + 4; | |
542 | ||
a6c2ba28 | 543 | remain = len; |
feff0485 | 544 | |
a6c2ba28 | 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 | /* | |
3acf2809 | 576 | * em28xx_isoIrq() |
a6c2ba28 | 577 | * handles the incoming isoc urbs and fills the frames from our inqueue |
578 | */ | |
3acf2809 | 579 | void em28xx_isocIrq(struct urb *urb, struct pt_regs *regs) |
a6c2ba28 | 580 | { |
3acf2809 | 581 | struct em28xx *dev = urb->context; |
a6c2ba28 | 582 | int i, status; |
3acf2809 | 583 | struct em28xx_frame_t **f; |
a6c2ba28 | 584 | unsigned long lock_flags; |
585 | ||
586 | if (!dev) | |
587 | return; | |
588 | #ifdef ENABLE_DEBUG_ISOC_FRAMES | |
589 | if (isoc_debug>1) | |
3acf2809 | 590 | em28xx_isoc_dump(urb, regs); |
a6c2ba28 | 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; | |
3acf2809 | 602 | em28xx_isocdbg("stream interrupted"); |
a6c2ba28 | 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, | |
3acf2809 | 612 | struct em28xx_frame_t, frame); |
a6c2ba28 | 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) { | |
3acf2809 | 620 | em28xx_isocdbg("data error: [%d] len=%d, status=%d", i, |
a6c2ba28 | 621 | urb->iso_frame_desc[i].actual_length, |
622 | urb->iso_frame_desc[i].status); | |
feff0485 MCC |
623 | if (urb->iso_frame_desc[i].status != -EPROTO) |
624 | continue; | |
a6c2ba28 | 625 | } |
626 | if (urb->iso_frame_desc[i].actual_length <= 0) { | |
3acf2809 | 627 | em28xx_isocdbg("packet %d is empty",i); |
a6c2ba28 | 628 | continue; |
629 | } | |
630 | if (urb->iso_frame_desc[i].actual_length > | |
631 | dev->max_pkt_size) { | |
3acf2809 | 632 | em28xx_isocdbg("packet bigger than packet size"); |
a6c2ba28 | 633 | continue; |
634 | } | |
635 | /*new frame */ | |
636 | if (buf[0] == 0x22 && buf[1] == 0x5a) { | |
3acf2809 | 637 | em28xx_isocdbg("Video frame, length=%i!",len); |
a6c2ba28 | 638 | |
3acf2809 | 639 | if (em28xx_isoc_video(dev,f,&lock_flags,buf[2])) |
a6c2ba28 | 640 | break; |
641 | } else if (buf[0]==0x33 && buf[1]==0x95 && buf[2]==0x00) { | |
3acf2809 | 642 | em28xx_isocdbg("VBI HEADER!!!"); |
a6c2ba28 | 643 | } |
644 | ||
645 | /* actual copying */ | |
646 | if ((*f)->state == F_GRABBING) { | |
3acf2809 | 647 | em28xx_isoc_video_copy(dev,f,buf, len); |
a6c2ba28 | 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))) { | |
3acf2809 | 659 | em28xx_errdev("resubmit of urb failed (error=%i)\n", status); |
a6c2ba28 | 660 | dev->state |= DEV_MISCONFIGURED; |
661 | } | |
662 | wake_up_interruptible(&dev->wait_frame); | |
663 | return; | |
664 | } | |
665 | ||
666 | /* | |
3acf2809 MCC |
667 | * em28xx_uninit_isoc() |
668 | * deallocates the buffers and urbs allocated during em28xx_init_iosc() | |
a6c2ba28 | 669 | */ |
3acf2809 | 670 | void em28xx_uninit_isoc(struct em28xx *dev) |
a6c2ba28 | 671 | { |
672 | int i; | |
673 | ||
3acf2809 | 674 | for (i = 0; i < EM28XX_NUM_BUFS; i++) { |
a6c2ba28 | 675 | if (dev->urb[i]) { |
676 | usb_kill_urb(dev->urb[i]); | |
02f74273 | 677 | if (dev->transfer_buffer[i]){ |
3acf2809 | 678 | usb_buffer_free(dev->udev,(EM28XX_NUM_PACKETS*dev->max_pkt_size),dev->transfer_buffer[i],dev->urb[i]->transfer_dma); |
02f74273 | 679 | } |
a6c2ba28 | 680 | usb_free_urb(dev->urb[i]); |
681 | } | |
682 | dev->urb[i] = NULL; | |
a6c2ba28 | 683 | dev->transfer_buffer[i] = NULL; |
684 | } | |
3acf2809 | 685 | em28xx_capture_start(dev, 0); |
a6c2ba28 | 686 | } |
687 | ||
688 | /* | |
3acf2809 | 689 | * em28xx_init_isoc() |
a6c2ba28 | 690 | * allocates transfer buffers and submits the urbs for isoc transfer |
691 | */ | |
3acf2809 | 692 | int em28xx_init_isoc(struct em28xx *dev) |
a6c2ba28 | 693 | { |
694 | /* change interface to 3 which allowes the biggest packet sizes */ | |
695 | int i, errCode; | |
3acf2809 | 696 | const int sb_size = EM28XX_NUM_PACKETS * dev->max_pkt_size; |
a6c2ba28 | 697 | |
698 | /* reset streaming vars */ | |
699 | dev->frame_current = NULL; | |
700 | dev->frame_count = 0; | |
701 | ||
702 | /* allocate urbs */ | |
3acf2809 | 703 | for (i = 0; i < EM28XX_NUM_BUFS; i++) { |
a6c2ba28 | 704 | struct urb *urb; |
705 | int j, k; | |
706 | /* allocate transfer buffer */ | |
3acf2809 | 707 | urb = usb_alloc_urb(EM28XX_NUM_PACKETS, GFP_KERNEL); |
02f74273 | 708 | if (!urb){ |
3acf2809 MCC |
709 | em28xx_errdev("cannot alloc urb %i\n", i); |
710 | em28xx_uninit_isoc(dev); | |
02f74273 MR |
711 | return -ENOMEM; |
712 | } | |
713 | dev->transfer_buffer[i] = usb_buffer_alloc(dev->udev, sb_size, GFP_KERNEL,&urb->transfer_dma); | |
a6c2ba28 | 714 | if (!dev->transfer_buffer[i]) { |
3acf2809 | 715 | em28xx_errdev |
a6c2ba28 | 716 | ("unable to allocate %i bytes for transfer buffer %i\n", |
717 | sb_size, i); | |
3acf2809 | 718 | em28xx_uninit_isoc(dev); |
a6c2ba28 | 719 | return -ENOMEM; |
720 | } | |
721 | memset(dev->transfer_buffer[i], 0, sb_size); | |
02f74273 MR |
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]; | |
3acf2809 MCC |
728 | urb->complete = em28xx_isocIrq; |
729 | urb->number_of_packets = EM28XX_NUM_PACKETS; | |
02f74273 | 730 | urb->transfer_buffer_length = sb_size; |
3acf2809 | 731 | for (j = k = 0; j < EM28XX_NUM_PACKETS; |
02f74273 MR |
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; | |
a6c2ba28 | 736 | } |
02f74273 | 737 | dev->urb[i] = urb; |
a6c2ba28 | 738 | } |
739 | ||
740 | /* submit urbs */ | |
3acf2809 | 741 | for (i = 0; i < EM28XX_NUM_BUFS; i++) { |
a6c2ba28 | 742 | errCode = usb_submit_urb(dev->urb[i], GFP_KERNEL); |
743 | if (errCode) { | |
3acf2809 | 744 | em28xx_errdev("submit of urb %i failed (error=%i)\n", i, |
a6c2ba28 | 745 | errCode); |
3acf2809 | 746 | em28xx_uninit_isoc(dev); |
a6c2ba28 | 747 | return errCode; |
748 | } | |
749 | } | |
750 | ||
751 | return 0; | |
752 | } | |
753 | ||
3acf2809 | 754 | int em28xx_set_alternate(struct em28xx *dev) |
a6c2ba28 | 755 | { |
756 | int errCode, prev_alt = dev->alt; | |
757 | dev->alt = alt; | |
758 | if (dev->alt == 0) { | |
759 | int i; | |
9d4d9c05 | 760 | for(i=0;i< dev->num_alt; i++) |
eac94356 MCC |
761 | if(dev->alt_max_pkt_size[i]>dev->alt_max_pkt_size[dev->alt]) |
762 | dev->alt=i; | |
a6c2ba28 | 763 | } |
764 | ||
765 | if (dev->alt != prev_alt) { | |
766 | dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt]; | |
9d4d9c05 | 767 | em28xx_coredbg("setting alternate %d with wMaxPacketSize=%u\n", dev->alt, |
a6c2ba28 | 768 | dev->max_pkt_size); |
769 | errCode = usb_set_interface(dev->udev, 0, dev->alt); | |
770 | if (errCode < 0) { | |
9d4d9c05 MCC |
771 | em28xx_errdev ("cannot change alternate number to %d (error=%i)\n", |
772 | dev->alt, errCode); | |
a6c2ba28 | 773 | return errCode; |
774 | } | |
775 | } | |
776 | return 0; | |
777 | } |