aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/em28xx/em28xx-video.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/em28xx/em28xx-video.c')
-rw-r--r--drivers/media/video/em28xx/em28xx-video.c1078
1 files changed, 552 insertions, 526 deletions
diff --git a/drivers/media/video/em28xx/em28xx-video.c b/drivers/media/video/em28xx/em28xx-video.c
index 4abe6701a770..8996175cc950 100644
--- a/drivers/media/video/em28xx/em28xx-video.c
+++ b/drivers/media/video/em28xx/em28xx-video.c
@@ -1,5 +1,6 @@
1/* 1/*
2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB video capture devices 2 em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
3 video capture devices
3 4
4 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it> 5 Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
5 Markus Rechberger <mrechberger@gmail.com> 6 Markus Rechberger <mrechberger@gmail.com>
@@ -52,7 +53,19 @@
52#define em28xx_videodbg(fmt, arg...) do {\ 53#define em28xx_videodbg(fmt, arg...) do {\
53 if (video_debug) \ 54 if (video_debug) \
54 printk(KERN_INFO "%s %s :"fmt, \ 55 printk(KERN_INFO "%s %s :"fmt, \
55 dev->name, __FUNCTION__ , ##arg); } while (0) 56 dev->name, __func__ , ##arg); } while (0)
57
58static unsigned int isoc_debug;
59module_param(isoc_debug, int, 0644);
60MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
61
62#define em28xx_isocdbg(fmt, arg...) \
63do {\
64 if (isoc_debug) { \
65 printk(KERN_INFO "%s %s :"fmt, \
66 dev->name, __func__ , ##arg); \
67 } \
68 } while (0)
56 69
57MODULE_AUTHOR(DRIVER_AUTHOR); 70MODULE_AUTHOR(DRIVER_AUTHOR);
58MODULE_DESCRIPTION(DRIVER_DESC); 71MODULE_DESCRIPTION(DRIVER_DESC);
@@ -74,9 +87,9 @@ MODULE_PARM_DESC(video_nr, "video device numbers");
74MODULE_PARM_DESC(vbi_nr, "vbi device numbers"); 87MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
75MODULE_PARM_DESC(radio_nr, "radio device numbers"); 88MODULE_PARM_DESC(radio_nr, "radio device numbers");
76 89
77static unsigned int video_debug = 0; 90static unsigned int video_debug;
78module_param(video_debug,int,0644); 91module_param(video_debug, int, 0644);
79MODULE_PARM_DESC(video_debug,"enable debug messages [video]"); 92MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
80 93
81/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */ 94/* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
82static unsigned long em28xx_devused; 95static unsigned long em28xx_devused;
@@ -93,7 +106,7 @@ static struct v4l2_queryctrl em28xx_qctrl[] = {
93 .step = 0x1, 106 .step = 0x1,
94 .default_value = 0x1f, 107 .default_value = 0x1f,
95 .flags = 0, 108 .flags = 0,
96 },{ 109 }, {
97 .id = V4L2_CID_AUDIO_MUTE, 110 .id = V4L2_CID_AUDIO_MUTE,
98 .type = V4L2_CTRL_TYPE_BOOLEAN, 111 .type = V4L2_CTRL_TYPE_BOOLEAN,
99 .name = "Mute", 112 .name = "Mute",
@@ -107,8 +120,391 @@ static struct v4l2_queryctrl em28xx_qctrl[] = {
107 120
108static struct usb_driver em28xx_usb_driver; 121static struct usb_driver em28xx_usb_driver;
109 122
123/* ------------------------------------------------------------------
124 DMA and thread functions
125 ------------------------------------------------------------------*/
126
127/*
128 * Announces that a buffer were filled and request the next
129 */
130static inline void buffer_filled(struct em28xx *dev,
131 struct em28xx_dmaqueue *dma_q,
132 struct em28xx_buffer *buf)
133{
134 /* Advice that buffer was filled */
135 em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
136 buf->vb.state = VIDEOBUF_DONE;
137 buf->vb.field_count++;
138 do_gettimeofday(&buf->vb.ts);
110 139
111/********************* v4l2 interface ******************************************/ 140 dev->isoc_ctl.buf = NULL;
141
142 list_del(&buf->vb.queue);
143 wake_up(&buf->vb.done);
144}
145
146/*
147 * Identify the buffer header type and properly handles
148 */
149static void em28xx_copy_video(struct em28xx *dev,
150 struct em28xx_dmaqueue *dma_q,
151 struct em28xx_buffer *buf,
152 unsigned char *p,
153 unsigned char *outp, unsigned long len)
154{
155 void *fieldstart, *startwrite, *startread;
156 int linesdone, currlinedone, offset, lencopy, remain;
157 int bytesperline = dev->width << 1;
158
159 if (dma_q->pos + len > buf->vb.size)
160 len = buf->vb.size - dma_q->pos;
161
162 if (p[0] != 0x88 && p[0] != 0x22) {
163 em28xx_isocdbg("frame is not complete\n");
164 len += 4;
165 } else
166 p += 4;
167
168 startread = p;
169 remain = len;
170
171 /* Interlaces frame */
172 if (buf->top_field)
173 fieldstart = outp;
174 else
175 fieldstart = outp + bytesperline;
176
177 linesdone = dma_q->pos / bytesperline;
178 currlinedone = dma_q->pos % bytesperline;
179 offset = linesdone * bytesperline * 2 + currlinedone;
180 startwrite = fieldstart + offset;
181 lencopy = bytesperline - currlinedone;
182 lencopy = lencopy > remain ? remain : lencopy;
183
184 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
185 em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
186 ((char *)startwrite + lencopy) -
187 ((char *)outp + buf->vb.size));
188 lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
189 }
190 if (lencopy <= 0)
191 return;
192 memcpy(startwrite, startread, lencopy);
193
194 remain -= lencopy;
195
196 while (remain > 0) {
197 startwrite += lencopy + bytesperline;
198 startread += lencopy;
199 if (bytesperline > remain)
200 lencopy = remain;
201 else
202 lencopy = bytesperline;
203
204 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
205 em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
206 ((char *)startwrite + lencopy) -
207 ((char *)outp + buf->vb.size));
208 lencopy = remain = (char *)outp + buf->vb.size -
209 (char *)startwrite;
210 }
211 if (lencopy <= 0)
212 break;
213
214 memcpy(startwrite, startread, lencopy);
215
216 remain -= lencopy;
217 }
218
219 dma_q->pos += len;
220}
221
222static inline void print_err_status(struct em28xx *dev,
223 int packet, int status)
224{
225 char *errmsg = "Unknown";
226
227 switch (status) {
228 case -ENOENT:
229 errmsg = "unlinked synchronuously";
230 break;
231 case -ECONNRESET:
232 errmsg = "unlinked asynchronuously";
233 break;
234 case -ENOSR:
235 errmsg = "Buffer error (overrun)";
236 break;
237 case -EPIPE:
238 errmsg = "Stalled (device not responding)";
239 break;
240 case -EOVERFLOW:
241 errmsg = "Babble (bad cable?)";
242 break;
243 case -EPROTO:
244 errmsg = "Bit-stuff error (bad cable?)";
245 break;
246 case -EILSEQ:
247 errmsg = "CRC/Timeout (could be anything)";
248 break;
249 case -ETIME:
250 errmsg = "Device does not respond";
251 break;
252 }
253 if (packet < 0) {
254 em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
255 } else {
256 em28xx_isocdbg("URB packet %d, status %d [%s].\n",
257 packet, status, errmsg);
258 }
259}
260
261/*
262 * video-buf generic routine to get the next available buffer
263 */
264static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
265 struct em28xx_buffer **buf)
266{
267 struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
268 char *outp;
269
270 if (list_empty(&dma_q->active)) {
271 em28xx_isocdbg("No active queue to serve\n");
272 dev->isoc_ctl.buf = NULL;
273 *buf = NULL;
274 return;
275 }
276
277 /* Get the next buffer */
278 *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
279
280 /* Cleans up buffer - Usefull for testing for frame/URB loss */
281 outp = videobuf_to_vmalloc(&(*buf)->vb);
282 memset(outp, 0, (*buf)->vb.size);
283
284 dev->isoc_ctl.buf = *buf;
285
286 return;
287}
288
289/*
290 * Controls the isoc copy of each urb packet
291 */
292static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
293{
294 struct em28xx_buffer *buf;
295 struct em28xx_dmaqueue *dma_q = urb->context;
296 unsigned char *outp = NULL;
297 int i, len = 0, rc = 1;
298 unsigned char *p;
299
300 if (!dev)
301 return 0;
302
303 if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
304 return 0;
305
306 if (urb->status < 0) {
307 print_err_status(dev, -1, urb->status);
308 if (urb->status == -ENOENT)
309 return 0;
310 }
311
312 buf = dev->isoc_ctl.buf;
313 if (buf != NULL)
314 outp = videobuf_to_vmalloc(&buf->vb);
315
316 for (i = 0; i < urb->number_of_packets; i++) {
317 int status = urb->iso_frame_desc[i].status;
318
319 if (status < 0) {
320 print_err_status(dev, i, status);
321 if (urb->iso_frame_desc[i].status != -EPROTO)
322 continue;
323 }
324
325 len = urb->iso_frame_desc[i].actual_length - 4;
326
327 if (urb->iso_frame_desc[i].actual_length <= 0) {
328 /* em28xx_isocdbg("packet %d is empty",i); - spammy */
329 continue;
330 }
331 if (urb->iso_frame_desc[i].actual_length >
332 dev->max_pkt_size) {
333 em28xx_isocdbg("packet bigger than packet size");
334 continue;
335 }
336
337 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
338
339 /* FIXME: incomplete buffer checks where removed to make
340 logic simpler. Impacts of those changes should be evaluated
341 */
342 if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
343 em28xx_isocdbg("VBI HEADER!!!\n");
344 /* FIXME: Should add vbi copy */
345 continue;
346 }
347 if (p[0] == 0x22 && p[1] == 0x5a) {
348 em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
349 len, (p[2] & 1)? "odd" : "even");
350
351 if (!(p[2] & 1)) {
352 if (buf != NULL)
353 buffer_filled(dev, dma_q, buf);
354 get_next_buf(dma_q, &buf);
355 if (buf == NULL)
356 outp = NULL;
357 else
358 outp = videobuf_to_vmalloc(&buf->vb);
359 }
360
361 if (buf != NULL) {
362 if (p[2] & 1)
363 buf->top_field = 0;
364 else
365 buf->top_field = 1;
366 }
367
368 dma_q->pos = 0;
369 }
370 if (buf != NULL)
371 em28xx_copy_video(dev, dma_q, buf, p, outp, len);
372 }
373 return rc;
374}
375
376/* ------------------------------------------------------------------
377 Videobuf operations
378 ------------------------------------------------------------------*/
379
380static int
381buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
382{
383 struct em28xx_fh *fh = vq->priv_data;
384 struct em28xx *dev = fh->dev;
385 struct v4l2_frequency f;
386
387 *size = 16 * fh->dev->width * fh->dev->height >> 3;
388 if (0 == *count)
389 *count = EM28XX_DEF_BUF;
390
391 if (*count < EM28XX_MIN_BUF)
392 *count = EM28XX_MIN_BUF;
393
394 /* Ask tuner to go to analog mode */
395 memset(&f, 0, sizeof(f));
396 f.frequency = dev->ctl_freq;
397
398 em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
399
400 return 0;
401}
402
403/* This is called *without* dev->slock held; please keep it that way */
404static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
405{
406 struct em28xx_fh *fh = vq->priv_data;
407 struct em28xx *dev = fh->dev;
408 unsigned long flags = 0;
409 if (in_interrupt())
410 BUG();
411
412 /* We used to wait for the buffer to finish here, but this didn't work
413 because, as we were keeping the state as VIDEOBUF_QUEUED,
414 videobuf_queue_cancel marked it as finished for us.
415 (Also, it could wedge forever if the hardware was misconfigured.)
416
417 This should be safe; by the time we get here, the buffer isn't
418 queued anymore. If we ever start marking the buffers as
419 VIDEOBUF_ACTIVE, it won't be, though.
420 */
421 spin_lock_irqsave(&dev->slock, flags);
422 if (dev->isoc_ctl.buf == buf)
423 dev->isoc_ctl.buf = NULL;
424 spin_unlock_irqrestore(&dev->slock, flags);
425
426 videobuf_vmalloc_free(&buf->vb);
427 buf->vb.state = VIDEOBUF_NEEDS_INIT;
428}
429
430static int
431buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
432 enum v4l2_field field)
433{
434 struct em28xx_fh *fh = vq->priv_data;
435 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
436 struct em28xx *dev = fh->dev;
437 int rc = 0, urb_init = 0;
438
439 /* FIXME: It assumes depth = 16 */
440 /* The only currently supported format is 16 bits/pixel */
441 buf->vb.size = 16 * dev->width * dev->height >> 3;
442
443 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
444 return -EINVAL;
445
446 buf->vb.width = dev->width;
447 buf->vb.height = dev->height;
448 buf->vb.field = field;
449
450 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
451 rc = videobuf_iolock(vq, &buf->vb, NULL);
452 if (rc < 0)
453 goto fail;
454 }
455
456 if (!dev->isoc_ctl.num_bufs)
457 urb_init = 1;
458
459 if (urb_init) {
460 rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
461 EM28XX_NUM_BUFS, dev->max_pkt_size,
462 em28xx_isoc_copy);
463 if (rc < 0)
464 goto fail;
465 }
466
467 buf->vb.state = VIDEOBUF_PREPARED;
468 return 0;
469
470fail:
471 free_buffer(vq, buf);
472 return rc;
473}
474
475static void
476buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
477{
478 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
479 struct em28xx_fh *fh = vq->priv_data;
480 struct em28xx *dev = fh->dev;
481 struct em28xx_dmaqueue *vidq = &dev->vidq;
482
483 buf->vb.state = VIDEOBUF_QUEUED;
484 list_add_tail(&buf->vb.queue, &vidq->active);
485
486}
487
488static void buffer_release(struct videobuf_queue *vq,
489 struct videobuf_buffer *vb)
490{
491 struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
492 struct em28xx_fh *fh = vq->priv_data;
493 struct em28xx *dev = (struct em28xx *)fh->dev;
494
495 em28xx_isocdbg("em28xx: called buffer_release\n");
496
497 free_buffer(vq, buf);
498}
499
500static struct videobuf_queue_ops em28xx_video_qops = {
501 .buf_setup = buffer_setup,
502 .buf_prepare = buffer_prepare,
503 .buf_queue = buffer_queue,
504 .buf_release = buffer_release,
505};
506
507/********************* v4l2 interface **************************************/
112 508
113/* 509/*
114 * em28xx_config() 510 * em28xx_config()
@@ -123,9 +519,9 @@ static int em28xx_config(struct em28xx *dev)
123 519
124 /* enable vbi capturing */ 520 /* enable vbi capturing */
125 521
126/* em28xx_write_regs_req(dev,0x00,0x0e,"\xC0",1); audio register */ 522/* em28xx_write_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
127/* em28xx_write_regs_req(dev,0x00,0x0f,"\x80",1); clk register */ 523/* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
128 em28xx_write_regs_req(dev,0x00,0x11,"\x51",1); 524 em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
129 525
130 dev->mute = 1; /* maybe not the right place... */ 526 dev->mute = 1; /* maybe not the right place... */
131 dev->volume = 0x1f; 527 dev->volume = 0x1f;
@@ -152,23 +548,6 @@ static void em28xx_config_i2c(struct em28xx *dev)
152 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL); 548 em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
153} 549}
154 550
155/*
156 * em28xx_empty_framequeues()
157 * prepare queues for incoming and outgoing frames
158 */
159static void em28xx_empty_framequeues(struct em28xx *dev)
160{
161 u32 i;
162
163 INIT_LIST_HEAD(&dev->inqueue);
164 INIT_LIST_HEAD(&dev->outqueue);
165
166 for (i = 0; i < EM28XX_NUM_FRAMES; i++) {
167 dev->frame[i].state = F_UNUSED;
168 dev->frame[i].buf.bytesused = 0;
169 }
170}
171
172static void video_mux(struct em28xx *dev, int index) 551static void video_mux(struct em28xx *dev, int index)
173{ 552{
174 struct v4l2_routing route; 553 struct v4l2_routing route;
@@ -181,12 +560,15 @@ static void video_mux(struct em28xx *dev, int index)
181 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route); 560 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
182 561
183 if (dev->has_msp34xx) { 562 if (dev->has_msp34xx) {
184 if (dev->i2s_speed) 563 if (dev->i2s_speed) {
185 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ, &dev->i2s_speed); 564 em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
565 &dev->i2s_speed);
566 }
186 route.input = dev->ctl_ainput; 567 route.input = dev->ctl_ainput;
187 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1); 568 route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
188 /* Note: this is msp3400 specific */ 569 /* Note: this is msp3400 specific */
189 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING, &route); 570 em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
571 &route);
190 } 572 }
191 573
192 em28xx_audio_analog_set(dev); 574 em28xx_audio_analog_set(dev);
@@ -202,15 +584,12 @@ static int res_get(struct em28xx_fh *fh)
202 if (fh->stream_on) 584 if (fh->stream_on)
203 return rc; 585 return rc;
204 586
205 mutex_lock(&dev->lock);
206
207 if (dev->stream_on) 587 if (dev->stream_on)
208 rc = -EINVAL; 588 return -EINVAL;
209 else {
210 dev->stream_on = 1;
211 fh->stream_on = 1;
212 }
213 589
590 mutex_lock(&dev->lock);
591 dev->stream_on = 1;
592 fh->stream_on = 1;
214 mutex_unlock(&dev->lock); 593 mutex_unlock(&dev->lock);
215 return rc; 594 return rc;
216} 595}
@@ -231,33 +610,6 @@ static void res_free(struct em28xx_fh *fh)
231} 610}
232 611
233/* 612/*
234 * em28xx_vm_open()
235 */
236static void em28xx_vm_open(struct vm_area_struct *vma)
237{
238 struct em28xx_frame_t *f = vma->vm_private_data;
239 f->vma_use_count++;
240}
241
242/*
243 * em28xx_vm_close()
244 */
245static void em28xx_vm_close(struct vm_area_struct *vma)
246{
247 /* NOTE: buffers are not freed here */
248 struct em28xx_frame_t *f = vma->vm_private_data;
249
250 if (f->vma_use_count)
251 f->vma_use_count--;
252}
253
254static struct vm_operations_struct em28xx_vm_ops = {
255 .open = em28xx_vm_open,
256 .close = em28xx_vm_close,
257};
258
259
260/*
261 * em28xx_get_ctrl() 613 * em28xx_get_ctrl()
262 * return the current saturation, brightness or contrast, mute state 614 * return the current saturation, brightness or contrast, mute state
263 */ 615 */
@@ -296,34 +648,6 @@ static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
296 } 648 }
297} 649}
298 650
299/*
300 * em28xx_stream_interrupt()
301 * stops streaming
302 */
303static int em28xx_stream_interrupt(struct em28xx *dev)
304{
305 int rc = 0;
306
307 /* stop reading from the device */
308
309 dev->stream = STREAM_INTERRUPT;
310 rc = wait_event_timeout(dev->wait_stream,
311 (dev->stream == STREAM_OFF) ||
312 (dev->state & DEV_DISCONNECTED),
313 EM28XX_URB_TIMEOUT);
314
315 if (rc) {
316 dev->state |= DEV_MISCONFIGURED;
317 em28xx_videodbg("device is misconfigured; close and "
318 "open /dev/video%d again\n",
319 dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
320 return rc;
321 }
322
323 return 0;
324}
325
326
327static int check_dev(struct em28xx *dev) 651static int check_dev(struct em28xx *dev)
328{ 652{
329 if (dev->state & DEV_DISCONNECTED) { 653 if (dev->state & DEV_DISCONNECTED) {
@@ -370,8 +694,8 @@ static int vidioc_g_fmt_cap(struct file *file, void *priv,
370 f->fmt.pix.width = dev->width; 694 f->fmt.pix.width = dev->width;
371 f->fmt.pix.height = dev->height; 695 f->fmt.pix.height = dev->height;
372 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; 696 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
373 f->fmt.pix.bytesperline = dev->bytesperline; 697 f->fmt.pix.bytesperline = dev->width * 2;
374 f->fmt.pix.sizeimage = dev->frame_size; 698 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
375 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 699 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
376 700
377 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */ 701 /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
@@ -447,7 +771,7 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv,
447{ 771{
448 struct em28xx_fh *fh = priv; 772 struct em28xx_fh *fh = priv;
449 struct em28xx *dev = fh->dev; 773 struct em28xx *dev = fh->dev;
450 int rc, i; 774 int rc;
451 775
452 rc = check_dev(dev); 776 rc = check_dev(dev);
453 if (rc < 0) 777 if (rc < 0)
@@ -457,49 +781,34 @@ static int vidioc_s_fmt_cap(struct file *file, void *priv,
457 781
458 mutex_lock(&dev->lock); 782 mutex_lock(&dev->lock);
459 783
460 for (i = 0; i < dev->num_frames; i++) 784 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
461 if (dev->frame[i].vma_use_count) { 785 em28xx_errdev("%s queue busy\n", __func__);
462 em28xx_videodbg("VIDIOC_S_FMT failed. " 786 rc = -EBUSY;
463 "Unmap the buffers first.\n"); 787 goto out;
464 rc = -EINVAL;
465 goto err;
466 }
467
468 /* stop io in case it is already in progress */
469 if (dev->stream == STREAM_ON) {
470 em28xx_videodbg("VIDIOC_SET_FMT: interrupting stream\n");
471 rc = em28xx_stream_interrupt(dev);
472 if (rc < 0)
473 goto err;
474 } 788 }
475 789
476 em28xx_release_buffers(dev); 790 if (dev->stream_on && !fh->stream_on) {
477 dev->io = IO_NONE; 791 em28xx_errdev("%s device in use by another fh\n", __func__);
792 rc = -EBUSY;
793 goto out;
794 }
478 795
479 /* set new image size */ 796 /* set new image size */
480 dev->width = f->fmt.pix.width; 797 dev->width = f->fmt.pix.width;
481 dev->height = f->fmt.pix.height; 798 dev->height = f->fmt.pix.height;
482 dev->frame_size = dev->width * dev->height * 2;
483 dev->field_size = dev->frame_size >> 1;
484 dev->bytesperline = dev->width * 2;
485 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); 799 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
486 800
487 /* FIXME: This is really weird! Why capture is starting with
488 this ioctl ???
489 */
490 em28xx_uninit_isoc(dev);
491 em28xx_set_alternate(dev); 801 em28xx_set_alternate(dev);
492 em28xx_capture_start(dev, 1);
493 em28xx_resolution_set(dev); 802 em28xx_resolution_set(dev);
494 em28xx_init_isoc(dev); 803
495 rc = 0; 804 rc = 0;
496 805
497err: 806out:
498 mutex_unlock(&dev->lock); 807 mutex_unlock(&dev->lock);
499 return rc; 808 return rc;
500} 809}
501 810
502static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm) 811static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
503{ 812{
504 struct em28xx_fh *fh = priv; 813 struct em28xx_fh *fh = priv;
505 struct em28xx *dev = fh->dev; 814 struct em28xx *dev = fh->dev;
@@ -524,9 +833,6 @@ static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
524 /* set new image size */ 833 /* set new image size */
525 dev->width = f.fmt.pix.width; 834 dev->width = f.fmt.pix.width;
526 dev->height = f.fmt.pix.height; 835 dev->height = f.fmt.pix.height;
527 dev->frame_size = dev->width * dev->height * 2;
528 dev->field_size = dev->frame_size >> 1;
529 dev->bytesperline = dev->width * 2;
530 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale); 836 get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
531 837
532 em28xx_resolution_set(dev); 838 em28xx_resolution_set(dev);
@@ -619,11 +925,11 @@ static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
619 925
620 index = dev->ctl_ainput; 926 index = dev->ctl_ainput;
621 927
622 if (index == 0) { 928 if (index == 0)
623 strcpy(a->name, "Television"); 929 strcpy(a->name, "Television");
624 } else { 930 else
625 strcpy(a->name, "Line In"); 931 strcpy(a->name, "Line In");
626 } 932
627 a->capability = V4L2_AUDCAP_STEREO; 933 a->capability = V4L2_AUDCAP_STEREO;
628 a->index = index; 934 a->index = index;
629 935
@@ -834,9 +1140,9 @@ static int vidioc_s_frequency(struct file *file, void *priv,
834static int em28xx_reg_len(int reg) 1140static int em28xx_reg_len(int reg)
835{ 1141{
836 switch (reg) { 1142 switch (reg) {
837 case AC97LSB_REG: 1143 case EM28XX_R40_AC97LSB:
838 case HSCALELOW_REG: 1144 case EM28XX_R30_HSCALELOW:
839 case VSCALELOW_REG: 1145 case EM28XX_R32_VSCALELOW:
840 return 2; 1146 return 2;
841 default: 1147 default:
842 return 1; 1148 return 1;
@@ -918,23 +1224,11 @@ static int vidioc_streamon(struct file *file, void *priv,
918 if (rc < 0) 1224 if (rc < 0)
919 return rc; 1225 return rc;
920 1226
921 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP)
922 return -EINVAL;
923
924 if (list_empty(&dev->inqueue))
925 return -EINVAL;
926
927 mutex_lock(&dev->lock);
928 1227
929 if (unlikely(res_get(fh) < 0)) { 1228 if (unlikely(res_get(fh) < 0))
930 mutex_unlock(&dev->lock);
931 return -EBUSY; 1229 return -EBUSY;
932 }
933 1230
934 dev->stream = STREAM_ON; /* FIXME: Start video capture here? */ 1231 return (videobuf_streamon(&fh->vb_vidq));
935
936 mutex_unlock(&dev->lock);
937 return 0;
938} 1232}
939 1233
940static int vidioc_streamoff(struct file *file, void *priv, 1234static int vidioc_streamoff(struct file *file, void *priv,
@@ -948,23 +1242,14 @@ static int vidioc_streamoff(struct file *file, void *priv,
948 if (rc < 0) 1242 if (rc < 0)
949 return rc; 1243 return rc;
950 1244
951 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP) 1245 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1246 return -EINVAL;
1247 if (type != fh->type)
952 return -EINVAL; 1248 return -EINVAL;
953 1249
954 mutex_lock(&dev->lock); 1250 videobuf_streamoff(&fh->vb_vidq);
955 1251 res_free(fh);
956 if (dev->stream == STREAM_ON) {
957 em28xx_videodbg("VIDIOC_STREAMOFF: interrupting stream\n");
958 rc = em28xx_stream_interrupt(dev);
959 if (rc < 0) {
960 mutex_unlock(&dev->lock);
961 return rc;
962 }
963 }
964
965 em28xx_empty_framequeues(dev);
966 1252
967 mutex_unlock(&dev->lock);
968 return 0; 1253 return 0;
969} 1254}
970 1255
@@ -1058,53 +1343,13 @@ static int vidioc_reqbufs(struct file *file, void *priv,
1058{ 1343{
1059 struct em28xx_fh *fh = priv; 1344 struct em28xx_fh *fh = priv;
1060 struct em28xx *dev = fh->dev; 1345 struct em28xx *dev = fh->dev;
1061 u32 i;
1062 int rc; 1346 int rc;
1063 1347
1064 rc = check_dev(dev); 1348 rc = check_dev(dev);
1065 if (rc < 0) 1349 if (rc < 0)
1066 return rc; 1350 return rc;
1067 1351
1068 if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 1352 return (videobuf_reqbufs(&fh->vb_vidq, rb));
1069 rb->memory != V4L2_MEMORY_MMAP)
1070 return -EINVAL;
1071
1072 if (dev->io == IO_READ) {
1073 em28xx_videodbg("method is set to read;"
1074 " close and open the device again to"
1075 " choose the mmap I/O method\n");
1076 return -EINVAL;
1077 }
1078
1079 for (i = 0; i < dev->num_frames; i++)
1080 if (dev->frame[i].vma_use_count) {
1081 em28xx_videodbg("VIDIOC_REQBUFS failed; "
1082 "previous buffers are still mapped\n");
1083 return -EINVAL;
1084 }
1085
1086 mutex_lock(&dev->lock);
1087
1088 if (dev->stream == STREAM_ON) {
1089 em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
1090 rc = em28xx_stream_interrupt(dev);
1091 if (rc < 0) {
1092 mutex_unlock(&dev->lock);
1093 return rc;
1094 }
1095 }
1096
1097 em28xx_empty_framequeues(dev);
1098
1099 em28xx_release_buffers(dev);
1100 if (rb->count)
1101 rb->count = em28xx_request_buffers(dev, rb->count);
1102
1103 dev->frame_current = NULL;
1104 dev->io = rb->count ? IO_MMAP : IO_NONE;
1105
1106 mutex_unlock(&dev->lock);
1107 return 0;
1108} 1353}
1109 1354
1110static int vidioc_querybuf(struct file *file, void *priv, 1355static int vidioc_querybuf(struct file *file, void *priv,
@@ -1118,52 +1363,20 @@ static int vidioc_querybuf(struct file *file, void *priv,
1118 if (rc < 0) 1363 if (rc < 0)
1119 return rc; 1364 return rc;
1120 1365
1121 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 1366 return (videobuf_querybuf(&fh->vb_vidq, b));
1122 b->index >= dev->num_frames || dev->io != IO_MMAP)
1123 return -EINVAL;
1124
1125 mutex_lock(&dev->lock);
1126
1127 memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
1128
1129 if (dev->frame[b->index].vma_use_count)
1130 b->flags |= V4L2_BUF_FLAG_MAPPED;
1131
1132 if (dev->frame[b->index].state == F_DONE)
1133 b->flags |= V4L2_BUF_FLAG_DONE;
1134 else if (dev->frame[b->index].state != F_UNUSED)
1135 b->flags |= V4L2_BUF_FLAG_QUEUED;
1136
1137 mutex_unlock(&dev->lock);
1138 return 0;
1139} 1367}
1140 1368
1141static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) 1369static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1142{ 1370{
1143 struct em28xx_fh *fh = priv; 1371 struct em28xx_fh *fh = priv;
1144 struct em28xx *dev = fh->dev; 1372 struct em28xx *dev = fh->dev;
1145 unsigned long lock_flags;
1146 int rc; 1373 int rc;
1147 1374
1148 rc = check_dev(dev); 1375 rc = check_dev(dev);
1149 if (rc < 0) 1376 if (rc < 0)
1150 return rc; 1377 return rc;
1151 1378
1152 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP || 1379 return (videobuf_qbuf(&fh->vb_vidq, b));
1153 b->index >= dev->num_frames)
1154 return -EINVAL;
1155
1156 if (dev->frame[b->index].state != F_UNUSED)
1157 return -EAGAIN;
1158
1159 dev->frame[b->index].state = F_QUEUED;
1160
1161 /* add frame to fifo */
1162 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1163 list_add_tail(&dev->frame[b->index].frame, &dev->inqueue);
1164 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1165
1166 return 0;
1167} 1380}
1168 1381
1169static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) 1382static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
@@ -1171,46 +1384,24 @@ static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1171 struct em28xx_fh *fh = priv; 1384 struct em28xx_fh *fh = priv;
1172 struct em28xx *dev = fh->dev; 1385 struct em28xx *dev = fh->dev;
1173 int rc; 1386 int rc;
1174 struct em28xx_frame_t *f;
1175 unsigned long lock_flags;
1176 1387
1177 rc = check_dev(dev); 1388 rc = check_dev(dev);
1178 if (rc < 0) 1389 if (rc < 0)
1179 return rc; 1390 return rc;
1180 1391
1181 if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || dev->io != IO_MMAP) 1392 return (videobuf_dqbuf(&fh->vb_vidq, b,
1182 return -EINVAL; 1393 file->f_flags & O_NONBLOCK));
1183 1394}
1184 if (list_empty(&dev->outqueue)) {
1185 if (dev->stream == STREAM_OFF)
1186 return -EINVAL;
1187
1188 if (file->f_flags & O_NONBLOCK)
1189 return -EAGAIN;
1190
1191 rc = wait_event_interruptible(dev->wait_frame,
1192 (!list_empty(&dev->outqueue)) ||
1193 (dev->state & DEV_DISCONNECTED));
1194 if (rc)
1195 return rc;
1196
1197 if (dev->state & DEV_DISCONNECTED)
1198 return -ENODEV;
1199 }
1200
1201 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1202 f = list_entry(dev->outqueue.next, struct em28xx_frame_t, frame);
1203 list_del(dev->outqueue.next);
1204 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1205
1206 f->state = F_UNUSED;
1207 memcpy(b, &f->buf, sizeof(*b));
1208 1395
1209 if (f->vma_use_count) 1396#ifdef CONFIG_VIDEO_V4L1_COMPAT
1210 b->flags |= V4L2_BUF_FLAG_MAPPED; 1397static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1398{
1399 struct em28xx_fh *fh = priv;
1211 1400
1212 return 0; 1401 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1213} 1402}
1403#endif
1404
1214 1405
1215/* ----------------------------------------------------------- */ 1406/* ----------------------------------------------------------- */
1216/* RADIO ESPECIFIC IOCTLS */ 1407/* RADIO ESPECIFIC IOCTLS */
@@ -1316,17 +1507,18 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1316{ 1507{
1317 int minor = iminor(inode); 1508 int minor = iminor(inode);
1318 int errCode = 0, radio = 0; 1509 int errCode = 0, radio = 0;
1319 struct em28xx *h,*dev = NULL; 1510 struct em28xx *h, *dev = NULL;
1320 struct em28xx_fh *fh; 1511 struct em28xx_fh *fh;
1512 enum v4l2_buf_type fh_type = 0;
1321 1513
1322 list_for_each_entry(h, &em28xx_devlist, devlist) { 1514 list_for_each_entry(h, &em28xx_devlist, devlist) {
1323 if (h->vdev->minor == minor) { 1515 if (h->vdev->minor == minor) {
1324 dev = h; 1516 dev = h;
1325 dev->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1517 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1326 } 1518 }
1327 if (h->vbi_dev->minor == minor) { 1519 if (h->vbi_dev->minor == minor) {
1328 dev = h; 1520 dev = h;
1329 dev->type = V4L2_BUF_TYPE_VBI_CAPTURE; 1521 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1330 } 1522 }
1331 if (h->radio_dev && 1523 if (h->radio_dev &&
1332 h->radio_dev->minor == minor) { 1524 h->radio_dev->minor == minor) {
@@ -1338,10 +1530,10 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1338 return -ENODEV; 1530 return -ENODEV;
1339 1531
1340 em28xx_videodbg("open minor=%d type=%s users=%d\n", 1532 em28xx_videodbg("open minor=%d type=%s users=%d\n",
1341 minor,v4l2_type_names[dev->type],dev->users); 1533 minor, v4l2_type_names[fh_type], dev->users);
1342 1534
1343 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1344 1535
1536 fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
1345 if (!fh) { 1537 if (!fh) {
1346 em28xx_errdev("em28xx-video.c: Out of memory?!\n"); 1538 em28xx_errdev("em28xx-video.c: Out of memory?!\n");
1347 return -ENOMEM; 1539 return -ENOMEM;
@@ -1349,28 +1541,24 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1349 mutex_lock(&dev->lock); 1541 mutex_lock(&dev->lock);
1350 fh->dev = dev; 1542 fh->dev = dev;
1351 fh->radio = radio; 1543 fh->radio = radio;
1544 fh->type = fh_type;
1352 filp->private_data = fh; 1545 filp->private_data = fh;
1353 1546
1354 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) { 1547 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1355 dev->width = norm_maxw(dev); 1548 dev->width = norm_maxw(dev);
1356 dev->height = norm_maxh(dev); 1549 dev->height = norm_maxh(dev);
1357 dev->frame_size = dev->width * dev->height * 2;
1358 dev->field_size = dev->frame_size >> 1; /*both_fileds ? dev->frame_size>>1 : dev->frame_size; */
1359 dev->bytesperline = dev->width * 2;
1360 dev->hscale = 0; 1550 dev->hscale = 0;
1361 dev->vscale = 0; 1551 dev->vscale = 0;
1362 1552
1553 em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
1363 em28xx_set_alternate(dev); 1554 em28xx_set_alternate(dev);
1364 em28xx_capture_start(dev, 1);
1365 em28xx_resolution_set(dev); 1555 em28xx_resolution_set(dev);
1366 1556
1557 /* Needed, since GPIO might have disabled power of
1558 some i2c device
1559 */
1560 em28xx_config_i2c(dev);
1367 1561
1368 /* start the transfer */
1369 errCode = em28xx_init_isoc(dev);
1370 if (errCode)
1371 goto err;
1372
1373 em28xx_empty_framequeues(dev);
1374 } 1562 }
1375 if (fh->radio) { 1563 if (fh->radio) {
1376 em28xx_videodbg("video_open: setting radio device\n"); 1564 em28xx_videodbg("video_open: setting radio device\n");
@@ -1379,8 +1567,12 @@ static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
1379 1567
1380 dev->users++; 1568 dev->users++;
1381 1569
1382err: 1570 videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
1571 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1572 sizeof(struct em28xx_buffer), fh);
1573
1383 mutex_unlock(&dev->lock); 1574 mutex_unlock(&dev->lock);
1575
1384 return errCode; 1576 return errCode;
1385} 1577}
1386 1578
@@ -1423,12 +1615,13 @@ static void em28xx_release_resources(struct em28xx *dev)
1423 usb_put_dev(dev->udev); 1615 usb_put_dev(dev->udev);
1424 1616
1425 /* Mark device as unused */ 1617 /* Mark device as unused */
1426 em28xx_devused&=~(1<<dev->devno); 1618 em28xx_devused &= ~(1<<dev->devno);
1427} 1619}
1428 1620
1429/* 1621/*
1430 * em28xx_v4l2_close() 1622 * em28xx_v4l2_close()
1431 * stops streaming and deallocates all resources allocated by the v4l2 calls and ioctls 1623 * stops streaming and deallocates all resources allocated by the v4l2
1624 * calls and ioctls
1432 */ 1625 */
1433static int em28xx_v4l2_close(struct inode *inode, struct file *filp) 1626static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1434{ 1627{
@@ -1445,9 +1638,8 @@ static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1445 mutex_lock(&dev->lock); 1638 mutex_lock(&dev->lock);
1446 1639
1447 if (dev->users == 1) { 1640 if (dev->users == 1) {
1448 em28xx_uninit_isoc(dev); 1641 videobuf_stop(&fh->vb_vidq);
1449 em28xx_release_buffers(dev); 1642 videobuf_mmap_free(&fh->vb_vidq);
1450 dev->io = IO_NONE;
1451 1643
1452 /* the device is already disconnect, 1644 /* the device is already disconnect,
1453 free the remaining resources */ 1645 free the remaining resources */
@@ -1458,6 +1650,10 @@ static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1458 return 0; 1650 return 0;
1459 } 1651 }
1460 1652
1653 /* do this before setting alternate! */
1654 em28xx_uninit_isoc(dev);
1655 em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
1656
1461 /* set alternate 0 */ 1657 /* set alternate 0 */
1462 dev->alt = 0; 1658 dev->alt = 0;
1463 em28xx_videodbg("setting alternate 0\n"); 1659 em28xx_videodbg("setting alternate 0\n");
@@ -1479,135 +1675,29 @@ static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
1479 * will allocate buffers when called for the first time 1675 * will allocate buffers when called for the first time
1480 */ 1676 */
1481static ssize_t 1677static ssize_t
1482em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count, 1678em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1483 loff_t * f_pos) 1679 loff_t *pos)
1484{ 1680{
1485 struct em28xx_frame_t *f, *i;
1486 unsigned long lock_flags;
1487 int ret = 0;
1488 struct em28xx_fh *fh = filp->private_data; 1681 struct em28xx_fh *fh = filp->private_data;
1489 struct em28xx *dev = fh->dev; 1682 struct em28xx *dev = fh->dev;
1683 int rc;
1684
1685 rc = check_dev(dev);
1686 if (rc < 0)
1687 return rc;
1490 1688
1491 /* FIXME: read() is not prepared to allow changing the video 1689 /* FIXME: read() is not prepared to allow changing the video
1492 resolution while streaming. Seems a bug at em28xx_set_fmt 1690 resolution while streaming. Seems a bug at em28xx_set_fmt
1493 */ 1691 */
1494 1692
1495 if (unlikely(res_get(fh) < 0)) 1693 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1496 return -EBUSY; 1694 if (unlikely(res_get(fh)))
1497 1695 return -EBUSY;
1498 mutex_lock(&dev->lock);
1499
1500 if (dev->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1501 em28xx_videodbg("V4l2_Buf_type_videocapture is set\n");
1502 1696
1503 if (dev->type == V4L2_BUF_TYPE_VBI_CAPTURE) { 1697 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1504 em28xx_videodbg("V4L2_BUF_TYPE_VBI_CAPTURE is set\n"); 1698 filp->f_flags & O_NONBLOCK);
1505 em28xx_videodbg("not supported yet! ...\n");
1506 if (copy_to_user(buf, "", 1)) {
1507 mutex_unlock(&dev->lock);
1508 return -EFAULT;
1509 }
1510 mutex_unlock(&dev->lock);
1511 return (1);
1512 }
1513 if (dev->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1514 em28xx_videodbg("V4L2_BUF_TYPE_SLICED_VBI_CAPTURE is set\n");
1515 em28xx_videodbg("not supported yet! ...\n");
1516 if (copy_to_user(buf, "", 1)) {
1517 mutex_unlock(&dev->lock);
1518 return -EFAULT;
1519 }
1520 mutex_unlock(&dev->lock);
1521 return (1);
1522 } 1699 }
1523 1700 return 0;
1524 if (dev->state & DEV_DISCONNECTED) {
1525 em28xx_videodbg("device not present\n");
1526 mutex_unlock(&dev->lock);
1527 return -ENODEV;
1528 }
1529
1530 if (dev->state & DEV_MISCONFIGURED) {
1531 em28xx_videodbg("device misconfigured; close and open it again\n");
1532 mutex_unlock(&dev->lock);
1533 return -EIO;
1534 }
1535
1536 if (dev->io == IO_MMAP) {
1537 em28xx_videodbg ("IO method is set to mmap; close and open"
1538 " the device again to choose the read method\n");
1539 mutex_unlock(&dev->lock);
1540 return -EINVAL;
1541 }
1542
1543 if (dev->io == IO_NONE) {
1544 if (!em28xx_request_buffers(dev, EM28XX_NUM_READ_FRAMES)) {
1545 em28xx_errdev("read failed, not enough memory\n");
1546 mutex_unlock(&dev->lock);
1547 return -ENOMEM;
1548 }
1549 dev->io = IO_READ;
1550 dev->stream = STREAM_ON;
1551 em28xx_queue_unusedframes(dev);
1552 }
1553
1554 if (!count) {
1555 mutex_unlock(&dev->lock);
1556 return 0;
1557 }
1558
1559 if (list_empty(&dev->outqueue)) {
1560 if (filp->f_flags & O_NONBLOCK) {
1561 mutex_unlock(&dev->lock);
1562 return -EAGAIN;
1563 }
1564 ret = wait_event_interruptible
1565 (dev->wait_frame,
1566 (!list_empty(&dev->outqueue)) ||
1567 (dev->state & DEV_DISCONNECTED));
1568 if (ret) {
1569 mutex_unlock(&dev->lock);
1570 return ret;
1571 }
1572 if (dev->state & DEV_DISCONNECTED) {
1573 mutex_unlock(&dev->lock);
1574 return -ENODEV;
1575 }
1576 dev->video_bytesread = 0;
1577 }
1578
1579 f = list_entry(dev->outqueue.prev, struct em28xx_frame_t, frame);
1580
1581 em28xx_queue_unusedframes(dev);
1582
1583 if (count > f->buf.length)
1584 count = f->buf.length;
1585
1586 if ((dev->video_bytesread + count) > dev->frame_size)
1587 count = dev->frame_size - dev->video_bytesread;
1588
1589 if (copy_to_user(buf, f->bufmem+dev->video_bytesread, count)) {
1590 em28xx_err("Error while copying to user\n");
1591 return -EFAULT;
1592 }
1593 dev->video_bytesread += count;
1594
1595 if (dev->video_bytesread == dev->frame_size) {
1596 spin_lock_irqsave(&dev->queue_lock, lock_flags);
1597 list_for_each_entry(i, &dev->outqueue, frame)
1598 i->state = F_UNUSED;
1599 INIT_LIST_HEAD(&dev->outqueue);
1600 spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
1601
1602 em28xx_queue_unusedframes(dev);
1603 dev->video_bytesread = 0;
1604 }
1605
1606 *f_pos += count;
1607
1608 mutex_unlock(&dev->lock);
1609
1610 return count;
1611} 1701}
1612 1702
1613/* 1703/*
@@ -1616,46 +1706,21 @@ em28xx_v4l2_read(struct file *filp, char __user * buf, size_t count,
1616 */ 1706 */
1617static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait) 1707static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
1618{ 1708{
1619 unsigned int mask = 0;
1620 struct em28xx_fh *fh = filp->private_data; 1709 struct em28xx_fh *fh = filp->private_data;
1621 struct em28xx *dev = fh->dev; 1710 struct em28xx *dev = fh->dev;
1711 int rc;
1712
1713 rc = check_dev(dev);
1714 if (rc < 0)
1715 return rc;
1622 1716
1623 if (unlikely(res_get(fh) < 0)) 1717 if (unlikely(res_get(fh) < 0))
1624 return POLLERR; 1718 return POLLERR;
1625 1719
1626 mutex_lock(&dev->lock); 1720 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1627 1721 return POLLERR;
1628 if (dev->state & DEV_DISCONNECTED) {
1629 em28xx_videodbg("device not present\n");
1630 } else if (dev->state & DEV_MISCONFIGURED) {
1631 em28xx_videodbg("device is misconfigured; close and open it again\n");
1632 } else {
1633 if (dev->io == IO_NONE) {
1634 if (!em28xx_request_buffers
1635 (dev, EM28XX_NUM_READ_FRAMES)) {
1636 em28xx_warn
1637 ("poll() failed, not enough memory\n");
1638 } else {
1639 dev->io = IO_READ;
1640 dev->stream = STREAM_ON;
1641 }
1642 }
1643
1644 if (dev->io == IO_READ) {
1645 em28xx_queue_unusedframes(dev);
1646 poll_wait(filp, &dev->wait_frame, wait);
1647
1648 if (!list_empty(&dev->outqueue))
1649 mask |= POLLIN | POLLRDNORM;
1650
1651 mutex_unlock(&dev->lock);
1652
1653 return mask;
1654 }
1655 }
1656 1722
1657 mutex_unlock(&dev->lock); 1723 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1658 return POLLERR;
1659} 1724}
1660 1725
1661/* 1726/*
@@ -1665,69 +1730,23 @@ static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1665{ 1730{
1666 struct em28xx_fh *fh = filp->private_data; 1731 struct em28xx_fh *fh = filp->private_data;
1667 struct em28xx *dev = fh->dev; 1732 struct em28xx *dev = fh->dev;
1668 unsigned long size = vma->vm_end - vma->vm_start; 1733 int rc;
1669 unsigned long start = vma->vm_start;
1670 void *pos;
1671 u32 i;
1672 1734
1673 if (unlikely(res_get(fh) < 0)) 1735 if (unlikely(res_get(fh) < 0))
1674 return -EBUSY; 1736 return -EBUSY;
1675 1737
1676 mutex_lock(&dev->lock); 1738 rc = check_dev(dev);
1677 1739 if (rc < 0)
1678 if (dev->state & DEV_DISCONNECTED) { 1740 return rc;
1679 em28xx_videodbg("mmap: device not present\n");
1680 mutex_unlock(&dev->lock);
1681 return -ENODEV;
1682 }
1683
1684 if (dev->state & DEV_MISCONFIGURED) {
1685 em28xx_videodbg ("mmap: Device is misconfigured; close and "
1686 "open it again\n");
1687 mutex_unlock(&dev->lock);
1688 return -EIO;
1689 }
1690
1691 if (dev->io != IO_MMAP || !(vma->vm_flags & VM_WRITE)) {
1692 mutex_unlock(&dev->lock);
1693 return -EINVAL;
1694 }
1695
1696 if (size > PAGE_ALIGN(dev->frame[0].buf.length))
1697 size = PAGE_ALIGN(dev->frame[0].buf.length);
1698
1699 for (i = 0; i < dev->num_frames; i++) {
1700 if ((dev->frame[i].buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
1701 break;
1702 }
1703 if (i == dev->num_frames) {
1704 em28xx_videodbg("mmap: user supplied mapping address is out of range\n");
1705 mutex_unlock(&dev->lock);
1706 return -EINVAL;
1707 }
1708
1709 /* VM_IO is eventually going to replace PageReserved altogether */
1710 vma->vm_flags |= VM_IO;
1711 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1712 1741
1713 pos = dev->frame[i].bufmem; 1742 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1714 while (size > 0) { /* size is page-aligned */
1715 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1716 em28xx_videodbg("mmap: vm_insert_page failed\n");
1717 mutex_unlock(&dev->lock);
1718 return -EAGAIN;
1719 }
1720 start += PAGE_SIZE;
1721 pos += PAGE_SIZE;
1722 size -= PAGE_SIZE;
1723 }
1724 1743
1725 vma->vm_ops = &em28xx_vm_ops; 1744 em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
1726 vma->vm_private_data = &dev->frame[i]; 1745 (unsigned long)vma->vm_start,
1746 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1747 rc);
1727 1748
1728 em28xx_vm_open(vma); 1749 return rc;
1729 mutex_unlock(&dev->lock);
1730 return 0;
1731} 1750}
1732 1751
1733static const struct file_operations em28xx_v4l_fops = { 1752static const struct file_operations em28xx_v4l_fops = {
@@ -1790,6 +1809,9 @@ static const struct video_device em28xx_video_template = {
1790 .vidioc_g_register = vidioc_g_register, 1809 .vidioc_g_register = vidioc_g_register,
1791 .vidioc_s_register = vidioc_s_register, 1810 .vidioc_s_register = vidioc_s_register,
1792#endif 1811#endif
1812#ifdef CONFIG_VIDEO_V4L1_COMPAT
1813 .vidiocgmbuf = vidiocgmbuf,
1814#endif
1793 1815
1794 .tvnorms = V4L2_STD_ALL, 1816 .tvnorms = V4L2_STD_ALL,
1795 .current_norm = V4L2_STD_PAL, 1817 .current_norm = V4L2_STD_PAL,
@@ -1818,7 +1840,7 @@ static struct video_device em28xx_radio_template = {
1818#endif 1840#endif
1819}; 1841};
1820 1842
1821/******************************** usb interface *****************************************/ 1843/******************************** usb interface ******************************/
1822 1844
1823 1845
1824static LIST_HEAD(em28xx_extension_devlist); 1846static LIST_HEAD(em28xx_extension_devlist);
@@ -1875,6 +1897,7 @@ static struct video_device *em28xx_vdev_init(struct em28xx *dev,
1875 vfd->dev = &dev->udev->dev; 1897 vfd->dev = &dev->udev->dev;
1876 vfd->release = video_device_release; 1898 vfd->release = video_device_release;
1877 vfd->type = type; 1899 vfd->type = type;
1900 vfd->debug = video_debug;
1878 1901
1879 snprintf(vfd->name, sizeof(vfd->name), "%s %s", 1902 snprintf(vfd->name, sizeof(vfd->name), "%s %s",
1880 dev->name, type_name); 1903 dev->name, type_name);
@@ -1898,7 +1921,7 @@ static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1898 1921
1899 dev->udev = udev; 1922 dev->udev = udev;
1900 mutex_init(&dev->lock); 1923 mutex_init(&dev->lock);
1901 spin_lock_init(&dev->queue_lock); 1924 spin_lock_init(&dev->slock);
1902 init_waitqueue_head(&dev->open); 1925 init_waitqueue_head(&dev->open);
1903 init_waitqueue_head(&dev->wait_frame); 1926 init_waitqueue_head(&dev->wait_frame);
1904 init_waitqueue_head(&dev->wait_stream); 1927 init_waitqueue_head(&dev->wait_stream);
@@ -1910,10 +1933,6 @@ static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1910 dev->em28xx_read_reg_req = em28xx_read_reg_req; 1933 dev->em28xx_read_reg_req = em28xx_read_reg_req;
1911 dev->is_em2800 = em28xx_boards[dev->model].is_em2800; 1934 dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
1912 1935
1913 errCode = em28xx_read_reg(dev, CHIPID_REG);
1914 if (errCode >= 0)
1915 em28xx_info("em28xx chip ID = %d\n", errCode);
1916
1917 em28xx_pre_card_setup(dev); 1936 em28xx_pre_card_setup(dev);
1918 1937
1919 errCode = em28xx_config(dev); 1938 errCode = em28xx_config(dev);
@@ -1946,10 +1965,6 @@ static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
1946 dev->width = maxw; 1965 dev->width = maxw;
1947 dev->height = maxh; 1966 dev->height = maxh;
1948 dev->interlaced = EM28XX_INTERLACED_DEFAULT; 1967 dev->interlaced = EM28XX_INTERLACED_DEFAULT;
1949 dev->field_size = dev->width * dev->height;
1950 dev->frame_size =
1951 dev->interlaced ? dev->field_size << 1 : dev->field_size;
1952 dev->bytesperline = dev->width * 2;
1953 dev->hscale = 0; 1968 dev->hscale = 0;
1954 dev->vscale = 0; 1969 dev->vscale = 0;
1955 dev->ctl_input = 2; 1970 dev->ctl_input = 2;
@@ -2005,6 +2020,10 @@ static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
2005 dev->radio_dev->minor & 0x1f); 2020 dev->radio_dev->minor & 0x1f);
2006 } 2021 }
2007 2022
2023 /* init video dma queues */
2024 INIT_LIST_HEAD(&dev->vidq.active);
2025 INIT_LIST_HEAD(&dev->vidq.queued);
2026
2008 2027
2009 if (dev->has_msp34xx) { 2028 if (dev->has_msp34xx) {
2010 /* Send a reset to other chips via gpio */ 2029 /* Send a reset to other chips via gpio */
@@ -2048,6 +2067,9 @@ static void request_module_async(struct work_struct *work)
2048 request_module("snd-usb-audio"); 2067 request_module("snd-usb-audio");
2049 else 2068 else
2050 request_module("em28xx-alsa"); 2069 request_module("em28xx-alsa");
2070
2071 if (dev->has_dvb)
2072 request_module("em28xx-dvb");
2051} 2073}
2052 2074
2053static void request_modules(struct em28xx *dev) 2075static void request_modules(struct em28xx *dev)
@@ -2077,22 +2099,24 @@ static int em28xx_usb_probe(struct usb_interface *interface,
2077 ifnum = interface->altsetting[0].desc.bInterfaceNumber; 2099 ifnum = interface->altsetting[0].desc.bInterfaceNumber;
2078 2100
2079 /* Check to see next free device and mark as used */ 2101 /* Check to see next free device and mark as used */
2080 nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS); 2102 nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
2081 em28xx_devused|=1<<nr; 2103 em28xx_devused |= 1<<nr;
2082 2104
2083 /* Don't register audio interfaces */ 2105 /* Don't register audio interfaces */
2084 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) { 2106 if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
2085 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n", 2107 em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
2086 udev->descriptor.idVendor,udev->descriptor.idProduct, 2108 udev->descriptor.idVendor,
2109 udev->descriptor.idProduct,
2087 ifnum, 2110 ifnum,
2088 interface->altsetting[0].desc.bInterfaceClass); 2111 interface->altsetting[0].desc.bInterfaceClass);
2089 2112
2090 em28xx_devused&=~(1<<nr); 2113 em28xx_devused &= ~(1<<nr);
2091 return -ENODEV; 2114 return -ENODEV;
2092 } 2115 }
2093 2116
2094 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n", 2117 em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
2095 udev->descriptor.idVendor,udev->descriptor.idProduct, 2118 udev->descriptor.idVendor,
2119 udev->descriptor.idProduct,
2096 ifnum, 2120 ifnum,
2097 interface->altsetting[0].desc.bInterfaceClass); 2121 interface->altsetting[0].desc.bInterfaceClass);
2098 2122
@@ -2102,18 +2126,19 @@ static int em28xx_usb_probe(struct usb_interface *interface,
2102 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 2126 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
2103 USB_ENDPOINT_XFER_ISOC) { 2127 USB_ENDPOINT_XFER_ISOC) {
2104 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n"); 2128 em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
2105 em28xx_devused&=~(1<<nr); 2129 em28xx_devused &= ~(1<<nr);
2106 return -ENODEV; 2130 return -ENODEV;
2107 } 2131 }
2108 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) { 2132 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
2109 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n"); 2133 em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
2110 em28xx_devused&=~(1<<nr); 2134 em28xx_devused &= ~(1<<nr);
2111 return -ENODEV; 2135 return -ENODEV;
2112 } 2136 }
2113 2137
2114 if (nr >= EM28XX_MAXBOARDS) { 2138 if (nr >= EM28XX_MAXBOARDS) {
2115 printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS); 2139 printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
2116 em28xx_devused&=~(1<<nr); 2140 EM28XX_MAXBOARDS);
2141 em28xx_devused &= ~(1<<nr);
2117 return -ENOMEM; 2142 return -ENOMEM;
2118 } 2143 }
2119 2144
@@ -2121,7 +2146,7 @@ static int em28xx_usb_probe(struct usb_interface *interface,
2121 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 2146 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2122 if (dev == NULL) { 2147 if (dev == NULL) {
2123 em28xx_err(DRIVER_NAME ": out of memory!\n"); 2148 em28xx_err(DRIVER_NAME ": out of memory!\n");
2124 em28xx_devused&=~(1<<nr); 2149 em28xx_devused &= ~(1<<nr);
2125 return -ENOMEM; 2150 return -ENOMEM;
2126 } 2151 }
2127 2152
@@ -2145,14 +2170,14 @@ static int em28xx_usb_probe(struct usb_interface *interface,
2145 /* compute alternate max packet sizes */ 2170 /* compute alternate max packet sizes */
2146 uif = udev->actconfig->interface[0]; 2171 uif = udev->actconfig->interface[0];
2147 2172
2148 dev->num_alt=uif->num_altsetting; 2173 dev->num_alt = uif->num_altsetting;
2149 em28xx_info("Alternate settings: %i\n",dev->num_alt); 2174 em28xx_info("Alternate settings: %i\n", dev->num_alt);
2150// dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* 2175/* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
2151 dev->alt_max_pkt_size = kmalloc(32* 2176 dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
2152 dev->num_alt,GFP_KERNEL); 2177
2153 if (dev->alt_max_pkt_size == NULL) { 2178 if (dev->alt_max_pkt_size == NULL) {
2154 em28xx_errdev("out of memory!\n"); 2179 em28xx_errdev("out of memory!\n");
2155 em28xx_devused&=~(1<<nr); 2180 em28xx_devused &= ~(1<<nr);
2156 kfree(dev); 2181 kfree(dev);
2157 return -ENOMEM; 2182 return -ENOMEM;
2158 } 2183 }
@@ -2162,11 +2187,11 @@ static int em28xx_usb_probe(struct usb_interface *interface,
2162 wMaxPacketSize); 2187 wMaxPacketSize);
2163 dev->alt_max_pkt_size[i] = 2188 dev->alt_max_pkt_size[i] =
2164 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1); 2189 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
2165 em28xx_info("Alternate setting %i, max size= %i\n",i, 2190 em28xx_info("Alternate setting %i, max size= %i\n", i,
2166 dev->alt_max_pkt_size[i]); 2191 dev->alt_max_pkt_size[i]);
2167 } 2192 }
2168 2193
2169 if ((card[nr]>=0)&&(card[nr]<em28xx_bcount)) 2194 if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
2170 dev->model = card[nr]; 2195 dev->model = card[nr];
2171 2196
2172 /* allocate device struct */ 2197 /* allocate device struct */
@@ -2202,7 +2227,8 @@ static void em28xx_usb_disconnect(struct usb_interface *interface)
2202 2227
2203 em28xx_info("disconnecting %s\n", dev->vdev->name); 2228 em28xx_info("disconnecting %s\n", dev->vdev->name);
2204 2229
2205 /* wait until all current v4l2 io is finished then deallocate resources */ 2230 /* wait until all current v4l2 io is finished then deallocate
2231 resources */
2206 mutex_lock(&dev->lock); 2232 mutex_lock(&dev->lock);
2207 2233
2208 wake_up_interruptible_all(&dev->open); 2234 wake_up_interruptible_all(&dev->open);