1 /******************************************************************************
5 * Video driver for EasyCAP USB2.0 Video Capture Device DC60 *
8 ******************************************************************************/
11 * Copyright (C) 2010 R.M. Thomas <rmthomas@sciolus.org>
14 * This is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * The software is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this software; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 /*****************************************************************************/
32 #include <linux/usb/audio.h>
35 MODULE_LICENSE("GPL");
36 MODULE_AUTHOR("R.M. Thomas <rmthomas@sciolus.org>");
37 MODULE_DESCRIPTION(EASYCAP_DRIVER_DESCRIPTION);
38 MODULE_VERSION(EASYCAP_DRIVER_VERSION);
40 #ifdef CONFIG_EASYCAP_DEBUG
42 module_param_named(debug, easycap_debug, int, S_IRUGO | S_IWUSR);
43 MODULE_PARM_DESC(debug, "Debug level: 0(default),1,2,...,9");
44 #endif /* CONFIG_EASYCAP_DEBUG */
46 bool easycap_readback;
47 module_param_named(readback, easycap_readback, bool, S_IRUGO | S_IWUSR);
48 MODULE_PARM_DESC(readback, "read back written registers: (default false)");
50 static int easycap_bars = 1;
51 module_param_named(bars, easycap_bars, int, S_IRUGO | S_IWUSR);
52 MODULE_PARM_DESC(bars,
53 "Testcard bars on input signal failure: 0=>no, 1=>yes(default)");
55 static int easycap_gain = 16;
56 module_param_named(gain, easycap_gain, int, S_IRUGO | S_IWUSR);
57 MODULE_PARM_DESC(gain, "Audio gain: 0,...,16(default),...31");
59 static bool easycap_ntsc;
60 module_param_named(ntsc, easycap_ntsc, bool, S_IRUGO | S_IWUSR);
61 MODULE_PARM_DESC(ntsc, "NTSC default encoding (default PAL)");
65 struct easycap_dongle easycapdc60_dongle[DONGLE_MANY];
66 static struct mutex mutex_dongle;
67 static void easycap_complete(struct urb *purb);
68 static int reset(struct easycap *peasycap);
69 static int field2frame(struct easycap *peasycap);
70 static int redaub(struct easycap *peasycap,
71 void *pad, void *pex, int much, int more,
72 u8 mask, u8 margin, bool isuy);
74 const char *strerror(int err)
76 #define ERRNOSTR(_e) case _e: return # _e
88 ERRNOSTR(EINPROGRESS);
95 ERRNOSTR(EPFNOSUPPORT);
96 ERRNOSTR(EAFNOSUPPORT);
98 ERRNOSTR(EADDRNOTAVAIL);
104 ERRNOSTR(ECONNRESET);
110 default: return "unknown";
116 /****************************************************************************/
117 /*---------------------------------------------------------------------------*/
119 * THIS ROUTINE DOES NOT DETECT DUPLICATE OCCURRENCES OF POINTER peasycap
121 /*---------------------------------------------------------------------------*/
122 int easycap_isdongle(struct easycap *peasycap)
127 for (k = 0; k < DONGLE_MANY; k++) {
128 if (easycapdc60_dongle[k].peasycap == peasycap) {
129 peasycap->isdongle = k;
135 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
136 static int easycap_open(struct inode *inode, struct file *file)
138 struct video_device *pvideo_device;
139 struct easycap *peasycap;
143 SAY("==========OPEN=========\n");
145 pvideo_device = video_devdata(file);
146 if (!pvideo_device) {
147 SAY("ERROR: pvideo_device is NULL.\n");
150 peasycap = (struct easycap *)video_get_drvdata(pvideo_device);
152 SAY("ERROR: peasycap is NULL\n");
155 if (!peasycap->pusb_device) {
156 SAM("ERROR: peasycap->pusb_device is NULL\n");
160 JOM(16, "peasycap->pusb_device=%p\n", peasycap->pusb_device);
162 file->private_data = peasycap;
163 rc = easycap_wakeup_device(peasycap->pusb_device);
165 SAM("ERROR: wakeup_device() rc = %i\n", rc);
167 SAM("ERROR: wakeup_device() returned -ENODEV\n");
169 SAM("ERROR: wakeup_device() rc = %i\n", rc);
172 JOM(8, "wakeup_device() OK\n");
174 rc = reset(peasycap);
176 SAM("ERROR: reset() rc = %i\n", rc);
182 /*****************************************************************************/
183 /*---------------------------------------------------------------------------*/
185 * RESET THE HARDWARE TO ITS REFERENCE STATE.
187 * THIS ROUTINE MAY BE CALLED REPEATEDLY IF easycap_complete() DETECTS
188 * A BAD VIDEO FRAME SIZE.
190 /*---------------------------------------------------------------------------*/
191 static int reset(struct easycap *peasycap)
193 struct easycap_standard const *peasycap_standard;
194 int fmtidx, input, rate;
199 SAY("ERROR: peasycap is NULL\n");
202 input = peasycap->input;
204 /*---------------------------------------------------------------------------*/
206 * IF THE SAA7113H HAS ALREADY ACQUIRED SYNC, USE ITS HARDWARE-DETECTED
207 * FIELD FREQUENCY TO DISTINGUISH NTSC FROM PAL. THIS IS ESSENTIAL FOR
208 * gstreamer AND OTHER USERSPACE PROGRAMS WHICH MAY NOT ATTEMPT TO INITIATE
209 * A SWITCH BETWEEN PAL AND NTSC.
211 * FUNCTION ready_saa() MAY REQUIRE A SUBSTANTIAL FRACTION OF A SECOND TO
212 * COMPLETE, SO SHOULD NOT BE INVOKED WITHOUT GOOD REASON.
214 /*---------------------------------------------------------------------------*/
216 JOM(8, "peasycap->ntsc=%d\n", peasycap->ntsc);
218 rate = ready_saa(peasycap->pusb_device);
220 JOM(8, "not ready to capture after %i ms ...\n", PATIENCE);
221 ntsc = !peasycap->ntsc;
222 JOM(8, "... trying %s ..\n", ntsc ? "NTSC" : "PAL");
223 rc = setup_stk(peasycap->pusb_device, ntsc);
225 SAM("ERROR: setup_stk() rc = %i\n", rc);
228 rc = setup_saa(peasycap->pusb_device, ntsc);
230 SAM("ERROR: setup_saa() rc = %i\n", rc);
234 rate = ready_saa(peasycap->pusb_device);
236 JOM(8, "not ready to capture after %i ms\n", PATIENCE);
237 JOM(8, "... saa register 0x1F has 0x%02X\n",
238 read_saa(peasycap->pusb_device, 0x1F));
239 ntsc = peasycap->ntsc;
241 JOM(8, "... success at second try: %i=rate\n", rate);
242 ntsc = (0 < (rate/2)) ? true : false ;
246 JOM(8, "... success at first try: %i=rate\n", rate);
247 ntsc = (0 < rate/2) ? true : false ;
249 JOM(8, "ntsc=%d\n", ntsc);
250 /*---------------------------------------------------------------------------*/
252 rc = setup_stk(peasycap->pusb_device, ntsc);
254 SAM("ERROR: setup_stk() rc = %i\n", rc);
257 rc = setup_saa(peasycap->pusb_device, ntsc);
259 SAM("ERROR: setup_saa() rc = %i\n", rc);
263 memset(peasycap->merit, 0, sizeof(peasycap->merit));
265 peasycap->video_eof = 0;
266 peasycap->audio_eof = 0;
267 /*---------------------------------------------------------------------------*/
269 * RESTORE INPUT AND FORCE REFRESH OF STANDARD, FORMAT, ETC.
271 * WHILE THIS PROCEDURE IS IN PROGRESS, SOME IOCTL COMMANDS WILL RETURN -EBUSY.
273 /*---------------------------------------------------------------------------*/
274 peasycap->input = -8192;
275 peasycap->standard_offset = -8192;
276 fmtidx = ntsc ? NTSC_M : PAL_BGHIN;
278 peasycap_standard = &easycap_standard[0];
279 while (0xFFFF != peasycap_standard->mask) {
280 if (fmtidx == peasycap_standard->v4l2_standard.index) {
281 peasycap->inputset[input].standard_offset =
282 peasycap_standard - easycap_standard;
287 if (0xFFFF == peasycap_standard->mask) {
288 SAM("ERROR: standard not found\n");
291 JOM(8, "%i=peasycap->inputset[%i].standard_offset\n",
292 peasycap->inputset[input].standard_offset, input);
294 peasycap->format_offset = -8192;
295 peasycap->brightness = -8192;
296 peasycap->contrast = -8192;
297 peasycap->saturation = -8192;
298 peasycap->hue = -8192;
300 rc = easycap_newinput(peasycap, input);
303 SAM("ERROR: newinput(.,%i) rc = %i\n", rc, input);
306 JOM(4, "restored input, standard and format\n");
308 JOM(8, "true=peasycap->ntsc %d\n", peasycap->ntsc);
310 if (0 > peasycap->input) {
311 SAM("MISTAKE: %i=peasycap->input\n", peasycap->input);
314 if (0 > peasycap->standard_offset) {
315 SAM("MISTAKE: %i=peasycap->standard_offset\n",
316 peasycap->standard_offset);
319 if (0 > peasycap->format_offset) {
320 SAM("MISTAKE: %i=peasycap->format_offset\n",
321 peasycap->format_offset);
324 if (0 > peasycap->brightness) {
325 SAM("MISTAKE: %i=peasycap->brightness\n",
326 peasycap->brightness);
329 if (0 > peasycap->contrast) {
330 SAM("MISTAKE: %i=peasycap->contrast\n", peasycap->contrast);
333 if (0 > peasycap->saturation) {
334 SAM("MISTAKE: %i=peasycap->saturation\n",
335 peasycap->saturation);
338 if (0 > peasycap->hue) {
339 SAM("MISTAKE: %i=peasycap->hue\n", peasycap->hue);
344 /*****************************************************************************/
345 /*---------------------------------------------------------------------------*/
347 * IF THE REQUESTED INPUT IS THE SAME AS THE EXISTING INPUT, DO NOTHING.
349 * KILL URBS, CLEAR FIELD AND FRAME BUFFERS AND RESET THEIR
350 * _read AND _fill POINTERS.
351 * SELECT THE NEW INPUT.
352 * ADJUST THE STANDARD, FORMAT, BRIGHTNESS, CONTRAST, SATURATION AND HUE
353 * ON THE BASIS OF INFORMATION IN STRUCTURE easycap.inputset[input].
354 * RESUBMIT THE URBS IF STREAMING WAS ALREADY IN PROGRESS.
357 * THIS ROUTINE MAY BE CALLED FREQUENTLY BY ZONEMINDER VIA IOCTL,
358 * SO IT SHOULD WRITE ONLY SPARINGLY TO THE LOGFILE.
360 /*---------------------------------------------------------------------------*/
361 int easycap_newinput(struct easycap *peasycap, int input)
363 int rc, k, m, mood, off;
364 int inputnow, video_idlenow, audio_idlenow;
368 SAY("ERROR: peasycap is NULL\n");
371 JOM(8, "%i=input sought\n", input);
373 if (0 > input && INPUT_MANY <= input)
375 inputnow = peasycap->input;
376 if (input == inputnow)
378 /*---------------------------------------------------------------------------*/
380 * IF STREAMING IS IN PROGRESS THE URBS ARE KILLED AT THIS
381 * STAGE AND WILL BE RESUBMITTED PRIOR TO EXIT FROM THE ROUTINE.
382 * IF NO STREAMING IS IN PROGRESS NO URBS WILL BE SUBMITTED BY THE
385 /*---------------------------------------------------------------------------*/
386 video_idlenow = peasycap->video_idle;
387 audio_idlenow = peasycap->audio_idle;
389 peasycap->video_idle = 1;
390 peasycap->audio_idle = 1;
391 if (peasycap->video_isoc_streaming) {
393 easycap_video_kill_urbs(peasycap);
397 /*---------------------------------------------------------------------------*/
398 if (!peasycap->pusb_device) {
399 SAM("ERROR: peasycap->pusb_device is NULL\n");
402 rc = usb_set_interface(peasycap->pusb_device,
403 peasycap->video_interface,
404 peasycap->video_altsetting_off);
406 SAM("ERROR: usb_set_interface() rc = %i\n", rc);
409 rc = stop_100(peasycap->pusb_device);
411 SAM("ERROR: stop_100() rc = %i\n", rc);
414 for (k = 0; k < FIELD_BUFFER_MANY; k++) {
415 for (m = 0; m < FIELD_BUFFER_SIZE/PAGE_SIZE; m++)
416 memset(peasycap->field_buffer[k][m].pgo, 0, PAGE_SIZE);
418 for (k = 0; k < FRAME_BUFFER_MANY; k++) {
419 for (m = 0; m < FRAME_BUFFER_SIZE/PAGE_SIZE; m++)
420 memset(peasycap->frame_buffer[k][m].pgo, 0, PAGE_SIZE);
422 peasycap->field_page = 0;
423 peasycap->field_read = 0;
424 peasycap->field_fill = 0;
426 peasycap->frame_read = 0;
427 peasycap->frame_fill = 0;
428 for (k = 0; k < peasycap->input; k++) {
429 (peasycap->frame_fill)++;
430 if (peasycap->frame_buffer_many <= peasycap->frame_fill)
431 peasycap->frame_fill = 0;
433 peasycap->input = input;
434 select_input(peasycap->pusb_device, peasycap->input, 9);
435 /*---------------------------------------------------------------------------*/
436 if (input == peasycap->inputset[input].input) {
437 off = peasycap->inputset[input].standard_offset;
438 if (off != peasycap->standard_offset) {
439 rc = adjust_standard(peasycap,
440 easycap_standard[off].v4l2_standard.id);
442 SAM("ERROR: adjust_standard() rc = %i\n", rc);
445 JOM(8, "%i=peasycap->standard_offset\n",
446 peasycap->standard_offset);
448 JOM(8, "%i=peasycap->standard_offset unchanged\n",
449 peasycap->standard_offset);
451 off = peasycap->inputset[input].format_offset;
452 if (off != peasycap->format_offset) {
453 struct v4l2_pix_format *pix =
454 &easycap_format[off].v4l2_format.fmt.pix;
455 rc = adjust_format(peasycap,
456 pix->width, pix->height,
457 pix->pixelformat, pix->field, false);
459 SAM("ERROR: adjust_format() rc = %i\n", rc);
462 JOM(8, "%i=peasycap->format_offset\n",
463 peasycap->format_offset);
465 JOM(8, "%i=peasycap->format_offset unchanged\n",
466 peasycap->format_offset);
468 mood = peasycap->inputset[input].brightness;
469 if (mood != peasycap->brightness) {
470 rc = adjust_brightness(peasycap, mood);
472 SAM("ERROR: adjust_brightness rc = %i\n", rc);
475 JOM(8, "%i=peasycap->brightness\n",
476 peasycap->brightness);
478 mood = peasycap->inputset[input].contrast;
479 if (mood != peasycap->contrast) {
480 rc = adjust_contrast(peasycap, mood);
482 SAM("ERROR: adjust_contrast rc = %i\n", rc);
485 JOM(8, "%i=peasycap->contrast\n", peasycap->contrast);
487 mood = peasycap->inputset[input].saturation;
488 if (mood != peasycap->saturation) {
489 rc = adjust_saturation(peasycap, mood);
491 SAM("ERROR: adjust_saturation rc = %i\n", rc);
494 JOM(8, "%i=peasycap->saturation\n",
495 peasycap->saturation);
497 mood = peasycap->inputset[input].hue;
498 if (mood != peasycap->hue) {
499 rc = adjust_hue(peasycap, mood);
501 SAM("ERROR: adjust_hue rc = %i\n", rc);
504 JOM(8, "%i=peasycap->hue\n", peasycap->hue);
507 SAM("MISTAKE: easycap.inputset[%i] unpopulated\n", input);
510 /*---------------------------------------------------------------------------*/
511 if (!peasycap->pusb_device) {
512 SAM("ERROR: peasycap->pusb_device is NULL\n");
515 rc = usb_set_interface(peasycap->pusb_device,
516 peasycap->video_interface,
517 peasycap->video_altsetting_on);
519 SAM("ERROR: usb_set_interface() rc = %i\n", rc);
522 rc = start_100(peasycap->pusb_device);
524 SAM("ERROR: start_100() rc = %i\n", rc);
528 easycap_video_submit_urbs(peasycap);
530 peasycap->video_isoc_sequence = VIDEO_ISOC_BUFFER_MANY - 1;
531 peasycap->video_idle = video_idlenow;
532 peasycap->audio_idle = audio_idlenow;
533 peasycap->video_junk = 0;
537 /*****************************************************************************/
538 int easycap_video_submit_urbs(struct easycap *peasycap)
540 struct data_urb *pdata_urb;
542 struct list_head *plist_head;
543 int j, isbad, nospc, m, rc;
547 SAY("ERROR: peasycap is NULL\n");
551 if (!peasycap->purb_video_head) {
552 SAY("ERROR: peasycap->urb_video_head uninitialized\n");
555 if (!peasycap->pusb_device) {
556 SAY("ERROR: peasycap->pusb_device is NULL\n");
559 if (!peasycap->video_isoc_streaming) {
560 JOM(4, "submission of all video urbs\n");
561 isbad = 0; nospc = 0; m = 0;
562 list_for_each(plist_head, (peasycap->purb_video_head)) {
563 pdata_urb = list_entry(plist_head,
564 struct data_urb, list_head);
565 if (pdata_urb && pdata_urb->purb) {
566 purb = pdata_urb->purb;
567 isbuf = pdata_urb->isbuf;
569 purb->dev = peasycap->pusb_device;
571 usb_rcvisocpipe(peasycap->pusb_device,
572 peasycap->video_endpointnumber);
573 purb->transfer_flags = URB_ISO_ASAP;
574 purb->transfer_buffer =
575 peasycap->video_isoc_buffer[isbuf].pgo;
576 purb->transfer_buffer_length =
577 peasycap->video_isoc_buffer_size;
578 purb->complete = easycap_complete;
579 purb->context = peasycap;
580 purb->start_frame = 0;
581 purb->number_of_packets =
582 peasycap->video_isoc_framesperdesc;
584 for (j = 0; j < peasycap->video_isoc_framesperdesc; j++) {
585 purb->iso_frame_desc[j]. offset =
586 j * peasycap->video_isoc_maxframesize;
587 purb->iso_frame_desc[j]. length =
588 peasycap->video_isoc_maxframesize;
591 rc = usb_submit_urb(purb, GFP_KERNEL);
594 SAM("ERROR: usb_submit_urb() failed "
595 "for urb with rc:-%s\n",
607 SAM("-ENOSPC=usb_submit_urb() for %i urbs\n", nospc);
608 SAM("..... possibly inadequate USB bandwidth\n");
609 peasycap->video_eof = 1;
613 easycap_video_kill_urbs(peasycap);
615 peasycap->video_isoc_streaming = 1;
617 JOM(4, "already streaming video urbs\n");
621 /*****************************************************************************/
622 int easycap_audio_kill_urbs(struct easycap *peasycap)
625 struct list_head *plist_head;
626 struct data_urb *pdata_urb;
628 if (!peasycap->audio_isoc_streaming)
631 if (!peasycap->purb_audio_head) {
632 SAM("ERROR: peasycap->purb_audio_head is NULL\n");
636 peasycap->audio_isoc_streaming = 0;
638 list_for_each(plist_head, peasycap->purb_audio_head) {
639 pdata_urb = list_entry(plist_head, struct data_urb, list_head);
640 if (pdata_urb && pdata_urb->purb) {
641 usb_kill_urb(pdata_urb->purb);
646 JOM(4, "%i audio urbs killed\n", m);
650 int easycap_video_kill_urbs(struct easycap *peasycap)
653 struct list_head *plist_head;
654 struct data_urb *pdata_urb;
656 if (!peasycap->video_isoc_streaming)
659 if (!peasycap->purb_video_head) {
660 SAM("ERROR: peasycap->purb_video_head is NULL\n");
664 peasycap->video_isoc_streaming = 0;
665 JOM(4, "killing video urbs\n");
667 list_for_each(plist_head, (peasycap->purb_video_head)) {
668 pdata_urb = list_entry(plist_head, struct data_urb, list_head);
669 if (pdata_urb && pdata_urb->purb) {
670 usb_kill_urb(pdata_urb->purb);
674 JOM(4, "%i video urbs killed\n", m);
678 /****************************************************************************/
679 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
680 /*--------------------------------------------------------------------------*/
681 static int easycap_open_noinode(struct file *file)
683 return easycap_open(NULL, file);
686 static int videodev_release(struct video_device *pvideo_device)
688 struct easycap *peasycap;
690 peasycap = video_get_drvdata(pvideo_device);
692 SAY("ERROR: peasycap is NULL\n");
693 SAY("ending unsuccessfully\n");
696 if (easycap_video_kill_urbs(peasycap)) {
697 SAM("ERROR: easycap_video_kill_urbs() failed\n");
700 JOM(4, "ending successfully\n");
703 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
704 /*****************************************************************************/
705 /*--------------------------------------------------------------------------*/
707 * THIS FUNCTION IS CALLED FROM WITHIN easycap_usb_disconnect() AND IS
708 * PROTECTED BY SEMAPHORES SET AND CLEARED BY easycap_usb_disconnect().
710 * BY THIS STAGE THE DEVICE HAS ALREADY BEEN PHYSICALLY UNPLUGGED, SO
711 * peasycap->pusb_device IS NO LONGER VALID.
713 /*---------------------------------------------------------------------------*/
714 static void easycap_delete(struct kref *pkref)
716 struct easycap *peasycap;
717 struct data_urb *pdata_urb;
718 struct list_head *plist_head, *plist_next;
720 int allocation_video_urb;
721 int allocation_video_page;
722 int allocation_video_struct;
723 int allocation_audio_urb;
724 int allocation_audio_page;
725 int allocation_audio_struct;
726 int registered_video, registered_audio;
728 peasycap = container_of(pkref, struct easycap, kref);
730 SAM("ERROR: peasycap is NULL: cannot perform deletions\n");
733 kd = easycap_isdongle(peasycap);
734 /*---------------------------------------------------------------------------*/
738 /*---------------------------------------------------------------------------*/
739 if (peasycap->purb_video_head) {
741 list_for_each(plist_head, peasycap->purb_video_head) {
742 pdata_urb = list_entry(plist_head,
743 struct data_urb, list_head);
744 if (pdata_urb && pdata_urb->purb) {
745 usb_free_urb(pdata_urb->purb);
746 pdata_urb->purb = NULL;
747 peasycap->allocation_video_urb--;
752 JOM(4, "%i video urbs freed\n", m);
753 /*---------------------------------------------------------------------------*/
754 JOM(4, "freeing video data_urb structures.\n");
756 list_for_each_safe(plist_head, plist_next,
757 peasycap->purb_video_head) {
758 pdata_urb = list_entry(plist_head,
759 struct data_urb, list_head);
761 peasycap->allocation_video_struct -=
762 sizeof(struct data_urb);
767 JOM(4, "%i video data_urb structures freed\n", m);
768 JOM(4, "setting peasycap->purb_video_head=NULL\n");
769 peasycap->purb_video_head = NULL;
771 /*---------------------------------------------------------------------------*/
772 JOM(4, "freeing video isoc buffers.\n");
774 for (k = 0; k < VIDEO_ISOC_BUFFER_MANY; k++) {
775 if (peasycap->video_isoc_buffer[k].pgo) {
776 free_pages((unsigned long)
777 peasycap->video_isoc_buffer[k].pgo,
779 peasycap->video_isoc_buffer[k].pgo = NULL;
780 peasycap->allocation_video_page -=
781 BIT(VIDEO_ISOC_ORDER);
785 JOM(4, "isoc video buffers freed: %i pages\n",
786 m * (0x01 << VIDEO_ISOC_ORDER));
787 /*---------------------------------------------------------------------------*/
788 JOM(4, "freeing video field buffers.\n");
790 for (k = 0; k < FIELD_BUFFER_MANY; k++) {
791 for (m = 0; m < FIELD_BUFFER_SIZE/PAGE_SIZE; m++) {
792 if (peasycap->field_buffer[k][m].pgo) {
793 free_page((unsigned long)
794 peasycap->field_buffer[k][m].pgo);
795 peasycap->field_buffer[k][m].pgo = NULL;
796 peasycap->allocation_video_page -= 1;
801 JOM(4, "video field buffers freed: %i pages\n", gone);
802 /*---------------------------------------------------------------------------*/
803 JOM(4, "freeing video frame buffers.\n");
805 for (k = 0; k < FRAME_BUFFER_MANY; k++) {
806 for (m = 0; m < FRAME_BUFFER_SIZE/PAGE_SIZE; m++) {
807 if (peasycap->frame_buffer[k][m].pgo) {
808 free_page((unsigned long)
809 peasycap->frame_buffer[k][m].pgo);
810 peasycap->frame_buffer[k][m].pgo = NULL;
811 peasycap->allocation_video_page -= 1;
816 JOM(4, "video frame buffers freed: %i pages\n", gone);
817 /*---------------------------------------------------------------------------*/
821 /*---------------------------------------------------------------------------*/
822 if (peasycap->purb_audio_head) {
823 JOM(4, "freeing audio urbs\n");
825 list_for_each(plist_head, (peasycap->purb_audio_head)) {
826 pdata_urb = list_entry(plist_head,
827 struct data_urb, list_head);
828 if (pdata_urb && pdata_urb->purb) {
829 usb_free_urb(pdata_urb->purb);
830 pdata_urb->purb = NULL;
831 peasycap->allocation_audio_urb--;
835 JOM(4, "%i audio urbs freed\n", m);
836 /*---------------------------------------------------------------------------*/
837 JOM(4, "freeing audio data_urb structures.\n");
839 list_for_each_safe(plist_head, plist_next,
840 peasycap->purb_audio_head) {
841 pdata_urb = list_entry(plist_head,
842 struct data_urb, list_head);
844 peasycap->allocation_audio_struct -=
845 sizeof(struct data_urb);
850 JOM(4, "%i audio data_urb structures freed\n", m);
851 JOM(4, "setting peasycap->purb_audio_head=NULL\n");
852 peasycap->purb_audio_head = NULL;
854 /*---------------------------------------------------------------------------*/
855 JOM(4, "freeing audio isoc buffers.\n");
857 for (k = 0; k < AUDIO_ISOC_BUFFER_MANY; k++) {
858 if (peasycap->audio_isoc_buffer[k].pgo) {
859 free_pages((unsigned long)
860 (peasycap->audio_isoc_buffer[k].pgo),
862 peasycap->audio_isoc_buffer[k].pgo = NULL;
863 peasycap->allocation_audio_page -=
864 BIT(AUDIO_ISOC_ORDER);
868 JOM(4, "easyoss_delete(): isoc audio buffers freed: %i pages\n",
869 m * (0x01 << AUDIO_ISOC_ORDER));
870 /*---------------------------------------------------------------------------*/
871 JOM(4, "freeing easycap structure.\n");
872 allocation_video_urb = peasycap->allocation_video_urb;
873 allocation_video_page = peasycap->allocation_video_page;
874 allocation_video_struct = peasycap->allocation_video_struct;
875 registered_video = peasycap->registered_video;
876 allocation_audio_urb = peasycap->allocation_audio_urb;
877 allocation_audio_page = peasycap->allocation_audio_page;
878 allocation_audio_struct = peasycap->allocation_audio_struct;
879 registered_audio = peasycap->registered_audio;
881 if (0 <= kd && DONGLE_MANY > kd) {
882 if (mutex_lock_interruptible(&mutex_dongle)) {
883 SAY("ERROR: cannot down mutex_dongle\n");
885 JOM(4, "locked mutex_dongle\n");
886 easycapdc60_dongle[kd].peasycap = NULL;
887 mutex_unlock(&mutex_dongle);
888 JOM(4, "unlocked mutex_dongle\n");
889 JOT(4, " null-->dongle[%i].peasycap\n", kd);
890 allocation_video_struct -= sizeof(struct easycap);
893 SAY("ERROR: cannot purge dongle[].peasycap");
898 /*---------------------------------------------------------------------------*/
899 SAY("%8i=video urbs after all deletions\n", allocation_video_urb);
900 SAY("%8i=video pages after all deletions\n", allocation_video_page);
901 SAY("%8i=video structs after all deletions\n", allocation_video_struct);
902 SAY("%8i=video devices after all deletions\n", registered_video);
903 SAY("%8i=audio urbs after all deletions\n", allocation_audio_urb);
904 SAY("%8i=audio pages after all deletions\n", allocation_audio_page);
905 SAY("%8i=audio structs after all deletions\n", allocation_audio_struct);
906 SAY("%8i=audio devices after all deletions\n", registered_audio);
911 /*****************************************************************************/
912 static unsigned int easycap_poll(struct file *file, poll_table *wait)
914 struct easycap *peasycap;
919 if (NULL == ((poll_table *)wait))
920 JOT(8, "WARNING: poll table pointer is NULL ... continuing\n");
922 SAY("ERROR: file pointer is NULL\n");
925 peasycap = file->private_data;
927 SAY("ERROR: peasycap is NULL\n");
930 if (!peasycap->pusb_device) {
931 SAY("ERROR: peasycap->pusb_device is NULL\n");
934 /*---------------------------------------------------------------------------*/
935 kd = easycap_isdongle(peasycap);
936 if (0 <= kd && DONGLE_MANY > kd) {
937 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].mutex_video)) {
938 SAY("ERROR: cannot down dongle[%i].mutex_video\n", kd);
941 JOM(4, "locked dongle[%i].mutex_video\n", kd);
943 * MEANWHILE, easycap_usb_disconnect() MAY HAVE FREED POINTER
944 * peasycap, IN WHICH CASE A REPEAT CALL TO isdongle() WILL FAIL.
945 * IF NECESSARY, BAIL OUT.
947 if (kd != easycap_isdongle(peasycap)) {
948 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
952 SAY("ERROR: file is NULL\n");
953 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
956 peasycap = file->private_data;
958 SAY("ERROR: peasycap is NULL\n");
959 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
962 if (!peasycap->pusb_device) {
963 SAM("ERROR: peasycap->pusb_device is NULL\n");
964 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
969 * IF easycap_usb_disconnect() HAS ALREADY FREED POINTER peasycap
970 * BEFORE THE ATTEMPT TO ACQUIRE THE SEMAPHORE, isdongle() WILL
971 * HAVE FAILED. BAIL OUT.
974 /*---------------------------------------------------------------------------*/
975 rc = easycap_video_dqbuf(peasycap, 0);
976 peasycap->polled = 1;
977 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
981 return POLLIN | POLLRDNORM;
983 /*****************************************************************************/
984 /*---------------------------------------------------------------------------*/
986 * IF mode IS NONZERO THIS ROUTINE RETURNS -EAGAIN RATHER THAN BLOCKING.
988 /*---------------------------------------------------------------------------*/
989 int easycap_video_dqbuf(struct easycap *peasycap, int mode)
991 int input, ifield, miss, rc;
995 SAY("ERROR: peasycap is NULL\n");
998 if (!peasycap->pusb_device) {
999 SAY("ERROR: peasycap->pusb_device is NULL\n");
1003 JOM(8, "%i=ifield\n", ifield);
1004 /*---------------------------------------------------------------------------*/
1006 * CHECK FOR LOST INPUT SIGNAL.
1008 * FOR THE FOUR-CVBS EasyCAP, THIS DOES NOT WORK AS EXPECTED.
1009 * IF INPUT 0 IS PRESENT AND SYNC ACQUIRED, UNPLUGGING INPUT 4 DOES NOT
1010 * RESULT IN SETTING BIT 0x40 ON REGISTER 0x1F, PRESUMABLY BECAUSE THERE
1011 * IS FLYWHEELING ON INPUT 0. THE UPSHOT IS:
1013 * INPUT 0 PLUGGED, INPUT 4 PLUGGED => SCREEN 0 OK, SCREEN 4 OK
1014 * INPUT 0 PLUGGED, INPUT 4 UNPLUGGED => SCREEN 0 OK, SCREEN 4 BLACK
1015 * INPUT 0 UNPLUGGED, INPUT 4 PLUGGED => SCREEN 0 BARS, SCREEN 4 OK
1016 * INPUT 0 UNPLUGGED, INPUT 4 UNPLUGGED => SCREEN 0 BARS, SCREEN 4 BARS
1018 /*---------------------------------------------------------------------------*/
1019 input = peasycap->input;
1020 if (0 <= input && INPUT_MANY > input) {
1021 rc = read_saa(peasycap->pusb_device, 0x1F);
1024 peasycap->lost[input] += 1;
1026 peasycap->lost[input] -= 2;
1028 if (0 > peasycap->lost[input])
1029 peasycap->lost[input] = 0;
1030 else if ((2 * VIDEO_LOST_TOLERATE) < peasycap->lost[input])
1031 peasycap->lost[input] = (2 * VIDEO_LOST_TOLERATE);
1034 /*---------------------------------------------------------------------------*/
1036 * WAIT FOR FIELD ifield (0 => TOP, 1 => BOTTOM)
1038 /*---------------------------------------------------------------------------*/
1040 while ((peasycap->field_read == peasycap->field_fill) ||
1041 (0 != (0xFF00 & peasycap->field_buffer
1042 [peasycap->field_read][0].kount)) ||
1043 (ifield != (0x00FF & peasycap->field_buffer
1044 [peasycap->field_read][0].kount))) {
1048 JOM(8, "first wait on wq_video, %i=field_read %i=field_fill\n",
1049 peasycap->field_read, peasycap->field_fill);
1051 if (0 != (wait_event_interruptible(peasycap->wq_video,
1052 (peasycap->video_idle || peasycap->video_eof ||
1053 ((peasycap->field_read != peasycap->field_fill) &&
1054 (0 == (0xFF00 & peasycap->field_buffer[peasycap->field_read][0].kount)) &&
1055 (ifield == (0x00FF & peasycap->field_buffer[peasycap->field_read][0].kount))))))) {
1056 SAM("aborted by signal\n");
1059 if (peasycap->video_idle) {
1060 JOM(8, "%i=peasycap->video_idle returning -EAGAIN\n",
1061 peasycap->video_idle);
1064 if (peasycap->video_eof) {
1065 JOM(8, "%i=peasycap->video_eof\n", peasycap->video_eof);
1066 #if defined(PERSEVERE)
1067 if (1 == peasycap->status) {
1068 JOM(8, "persevering ...\n");
1069 peasycap->video_eof = 0;
1070 peasycap->audio_eof = 0;
1071 if (0 != reset(peasycap)) {
1072 JOM(8, " ... failed returning -EIO\n");
1073 peasycap->video_eof = 1;
1074 peasycap->audio_eof = 1;
1075 easycap_video_kill_urbs(peasycap);
1078 peasycap->status = 0;
1079 JOM(8, " ... OK returning -EAGAIN\n");
1082 #endif /*PERSEVERE*/
1083 peasycap->video_eof = 1;
1084 peasycap->audio_eof = 1;
1085 easycap_video_kill_urbs(peasycap);
1086 JOM(8, "returning -EIO\n");
1091 JOM(8, "first awakening on wq_video after %i waits\n", miss);
1093 rc = field2frame(peasycap);
1095 SAM("ERROR: field2frame() rc = %i\n", rc);
1096 /*---------------------------------------------------------------------------*/
1098 * WAIT FOR THE OTHER FIELD
1100 /*---------------------------------------------------------------------------*/
1106 while ((peasycap->field_read == peasycap->field_fill) ||
1107 (0 != (0xFF00 & peasycap->field_buffer[peasycap->field_read][0].kount)) ||
1108 (ifield != (0x00FF & peasycap->field_buffer[peasycap->field_read][0].kount))) {
1112 JOM(8, "second wait on wq_video %i=field_read %i=field_fill\n",
1113 peasycap->field_read, peasycap->field_fill);
1114 if (0 != (wait_event_interruptible(peasycap->wq_video,
1115 (peasycap->video_idle || peasycap->video_eof ||
1116 ((peasycap->field_read != peasycap->field_fill) &&
1117 (0 == (0xFF00 & peasycap->field_buffer[peasycap->field_read][0].kount)) &&
1118 (ifield == (0x00FF & peasycap->field_buffer[peasycap->field_read][0].kount))))))) {
1119 SAM("aborted by signal\n");
1122 if (peasycap->video_idle) {
1123 JOM(8, "%i=peasycap->video_idle returning -EAGAIN\n",
1124 peasycap->video_idle);
1127 if (peasycap->video_eof) {
1128 JOM(8, "%i=peasycap->video_eof\n", peasycap->video_eof);
1129 #if defined(PERSEVERE)
1130 if (1 == peasycap->status) {
1131 JOM(8, "persevering ...\n");
1132 peasycap->video_eof = 0;
1133 peasycap->audio_eof = 0;
1134 if (0 != reset(peasycap)) {
1135 JOM(8, " ... failed returning -EIO\n");
1136 peasycap->video_eof = 1;
1137 peasycap->audio_eof = 1;
1138 easycap_video_kill_urbs(peasycap);
1141 peasycap->status = 0;
1142 JOM(8, " ... OK ... returning -EAGAIN\n");
1145 #endif /*PERSEVERE*/
1146 peasycap->video_eof = 1;
1147 peasycap->audio_eof = 1;
1148 easycap_video_kill_urbs(peasycap);
1149 JOM(8, "returning -EIO\n");
1154 JOM(8, "second awakening on wq_video after %i waits\n", miss);
1156 rc = field2frame(peasycap);
1158 SAM("ERROR: field2frame() rc = %i\n", rc);
1159 /*---------------------------------------------------------------------------*/
1163 /*---------------------------------------------------------------------------*/
1164 if (peasycap->skip) {
1165 peasycap->skipped++;
1166 if (peasycap->skip != peasycap->skipped)
1167 return peasycap->skip - peasycap->skipped;
1169 peasycap->skipped = 0;
1171 /*---------------------------------------------------------------------------*/
1172 peasycap->frame_read = peasycap->frame_fill;
1173 peasycap->queued[peasycap->frame_read] = 0;
1174 peasycap->done[peasycap->frame_read] = V4L2_BUF_FLAG_DONE;
1176 peasycap->frame_fill++;
1177 if (peasycap->frame_buffer_many <= peasycap->frame_fill)
1178 peasycap->frame_fill = 0;
1180 if (0x01 & easycap_standard[peasycap->standard_offset].mask)
1181 peasycap->frame_buffer[peasycap->frame_read][0].kount =
1184 peasycap->frame_buffer[peasycap->frame_read][0].kount =
1188 JOM(8, "setting: %i=peasycap->frame_read\n", peasycap->frame_read);
1189 JOM(8, "bumped to: %i=peasycap->frame_fill\n", peasycap->frame_fill);
1193 /*****************************************************************************/
1194 /*---------------------------------------------------------------------------*/
1196 * BY DEFINITION, odd IS true FOR THE FIELD OCCUPYING LINES 1,3,5,...,479
1197 * odd IS false FOR THE FIELD OCCUPYING LINES 0,2,4,...,478
1199 * WHEN BOOLEAN PARAMETER decimatepixel IS true, ONLY THE FIELD FOR WHICH
1200 * odd==false IS TRANSFERRED TO THE FRAME BUFFER.
1203 /*---------------------------------------------------------------------------*/
1204 static int field2frame(struct easycap *peasycap)
1208 int kex, kad, mex, mad, rex, rad, rad2;
1209 int c2, c3, w2, w3, cz, wz;
1210 int rc, bytesperpixel, multiplier;
1211 int much, more, over, rump, caches, input;
1213 bool odd, isuy, decimatepixel, badinput;
1216 SAY("ERROR: peasycap is NULL\n");
1221 input = 0x07 & peasycap->field_buffer[peasycap->field_read][0].input;
1223 JOM(8, "===== parity %i, input 0x%02X, field buffer %i --> "
1224 "frame buffer %i\n",
1225 peasycap->field_buffer[peasycap->field_read][0].kount,
1226 peasycap->field_buffer[peasycap->field_read][0].input,
1227 peasycap->field_read, peasycap->frame_fill);
1228 JOM(8, "===== %i=bytesperpixel\n", peasycap->bytesperpixel);
1230 /*---------------------------------------------------------------------------*/
1232 * REJECT OR CLEAN BAD FIELDS
1234 /*---------------------------------------------------------------------------*/
1235 if (peasycap->field_read == peasycap->field_fill) {
1236 SAM("ERROR: on entry, still filling field buffer %i\n",
1237 peasycap->field_read);
1240 #ifdef EASYCAP_TESTCARD
1241 easycap_testcard(peasycap, peasycap->field_read);
1243 if (0 <= input && INPUT_MANY > input) {
1244 if (easycap_bars && VIDEO_LOST_TOLERATE <= peasycap->lost[input])
1245 easycap_testcard(peasycap, peasycap->field_read);
1247 #endif /*EASYCAP_TESTCARD*/
1248 /*---------------------------------------------------------------------------*/
1250 bytesperpixel = peasycap->bytesperpixel;
1251 decimatepixel = peasycap->decimatepixel;
1253 if ((2 != bytesperpixel) &&
1254 (3 != bytesperpixel) &&
1255 (4 != bytesperpixel)) {
1256 SAM("MISTAKE: %i=bytesperpixel\n", bytesperpixel);
1264 w2 = 2 * multiplier * (peasycap->width);
1265 w3 = bytesperpixel * multiplier * (peasycap->width);
1266 wz = multiplier * (peasycap->height) *
1267 multiplier * (peasycap->width);
1269 kex = peasycap->field_read; mex = 0;
1270 kad = peasycap->frame_fill; mad = 0;
1272 pex = peasycap->field_buffer[kex][0].pgo; rex = PAGE_SIZE;
1273 pad = peasycap->frame_buffer[kad][0].pgo; rad = PAGE_SIZE;
1274 odd = !!(peasycap->field_buffer[kex][0].kount);
1276 if (odd && (!decimatepixel)) {
1277 JOM(8, "initial skipping %4i bytes p.%4i\n",
1278 w3/multiplier, mad);
1279 pad += (w3 / multiplier); rad -= (w3 / multiplier);
1282 mask = 0; rump = 0; caches = 0;
1287 * PROCESS ONE LINE OF FRAME AT FULL RESOLUTION:
1288 * READ w2 BYTES FROM FIELD BUFFER,
1289 * WRITE w3 BYTES TO FRAME BUFFER
1291 if (!decimatepixel) {
1294 much = over; more = 0;
1295 margin = 0; mask = 0x00;
1301 SAM("MISTAKE: much is odd\n");
1305 more = (bytesperpixel *
1307 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1308 if (1 < bytesperpixel) {
1309 if (rad * 2 < much * bytesperpixel) {
1311 * INJUDICIOUS ALTERATION OF
1312 * THIS STATEMENT BLOCK WILL
1313 * CAUSE BREAKAGE. BEWARE.
1315 rad2 = rad + bytesperpixel - 1;
1316 much = ((((2 * rad2)/bytesperpixel)/2) * 2);
1317 rump = ((bytesperpixel * much) / 2) - rad;
1324 if ((mex + 1) < FIELD_BUFFER_SIZE / PAGE_SIZE)
1325 margin = *((u8 *)(peasycap->field_buffer[kex][mex + 1].pgo));
1330 SAM("MISTAKE: %i=bytesperpixel\n",
1337 JOM(8, "ERROR: 0x%02X=->field_buffer"
1339 "0x%02X=(0x08|->input)\n",
1340 peasycap->field_buffer
1341 [kex][mex].input, kex, mex,
1342 (0x08|peasycap->input));
1344 rc = redaub(peasycap, pad, pex, much, more,
1345 mask, margin, isuy);
1347 SAM("ERROR: redaub() failed\n");
1353 over -= much; cz += much;
1354 pex += much; rex -= much;
1357 pex = peasycap->field_buffer[kex][mex].pgo;
1359 if (peasycap->field_buffer[kex][mex].input != (0x08|peasycap->input))
1366 pad = peasycap->frame_buffer[kad][mad].pgo;
1374 /*---------------------------------------------------------------------------*/
1376 * SKIP w3 BYTES IN TARGET FRAME BUFFER,
1377 * UNLESS IT IS THE LAST LINE OF AN ODD FRAME
1379 /*---------------------------------------------------------------------------*/
1380 if (!odd || (cz != wz)) {
1385 pad = peasycap->frame_buffer
1397 /*---------------------------------------------------------------------------*/
1399 * PROCESS ONE LINE OF FRAME AT REDUCED RESOLUTION:
1400 * ONLY IF false==odd,
1401 * READ w2 BYTES FROM FIELD BUFFER,
1402 * WRITE w3 / 2 BYTES TO FRAME BUFFER
1404 /*---------------------------------------------------------------------------*/
1408 much = over; more = 0; margin = 0; mask = 0x00;
1414 SAM("MISTAKE: much is odd\n");
1418 more = (bytesperpixel * much) / 4;
1419 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1420 if (1 < bytesperpixel) {
1421 if (rad * 4 < much * bytesperpixel) {
1423 * INJUDICIOUS ALTERATION OF
1424 * THIS STATEMENT BLOCK
1425 * WILL CAUSE BREAKAGE.
1428 rad2 = rad + bytesperpixel - 1;
1429 much = ((((2 * rad2) / bytesperpixel) / 2) * 4);
1430 rump = ((bytesperpixel * much) / 4) - rad;
1437 if ((mex + 1) < FIELD_BUFFER_SIZE / PAGE_SIZE)
1438 margin = *((u8 *)(peasycap->field_buffer[kex][mex + 1].pgo));
1442 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1444 SAM("MISTAKE: %i=bytesperpixel\n",
1448 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1453 JOM(8, "ERROR: 0x%02X=->field_buffer"
1455 "0x%02X=(0x08|->input)\n",
1456 peasycap->field_buffer
1457 [kex][mex].input, kex, mex,
1458 (0x08|peasycap->input));
1460 rc = redaub(peasycap, pad, pex, much, more,
1461 mask, margin, isuy);
1463 SAM("ERROR: redaub() failed\n");
1466 over -= much; cz += much;
1467 pex += much; rex -= much;
1470 pex = peasycap->field_buffer[kex][mex].pgo;
1472 if (peasycap->field_buffer[kex][mex].input !=
1473 (0x08|peasycap->input))
1480 pad = peasycap->frame_buffer[kad][mad].pgo;
1488 /*---------------------------------------------------------------------------*/
1491 * READ w2 BYTES FROM FIELD BUFFER AND DISCARD THEM
1493 /*---------------------------------------------------------------------------*/
1499 pex = peasycap->field_buffer[kex][mex].pgo;
1501 if (peasycap->field_buffer[kex][mex].input !=
1502 (0x08|peasycap->input)) {
1503 JOM(8, "ERROR: 0x%02X=->field_buffer"
1505 "0x%02X=(0x08|->input)\n",
1506 peasycap->field_buffer
1507 [kex][mex].input, kex, mex,
1508 (0x08|peasycap->input));
1522 /*---------------------------------------------------------------------------*/
1526 /*---------------------------------------------------------------------------*/
1527 c2 = (mex + 1)*PAGE_SIZE - rex;
1529 SAM("ERROR: discrepancy %i in bytes read\n", c2 - cz);
1530 c3 = (mad + 1)*PAGE_SIZE - rad;
1532 if (!decimatepixel) {
1533 if (bytesperpixel * cz != c3)
1534 SAM("ERROR: discrepancy %i in bytes written\n",
1535 c3 - (bytesperpixel * cz));
1540 SAM("ERROR: discrepancy %i in bytes written\n",
1541 (2*c3)-(bytesperpixel * cz));
1544 SAM("ERROR: discrepancy %i "
1545 "in bytes written\n", c3);
1549 SAM("WORRY: undischarged cache at end of line in frame buffer\n");
1551 JOM(8, "===== field2frame(): %i bytes --> %i bytes (incl skip)\n", c2, c3);
1552 JOM(8, "===== field2frame(): %i=mad %i=rad\n", mad, rad);
1555 JOM(8, "+++++ field2frame(): frame buffer %i is full\n", kad);
1557 if (peasycap->field_read == peasycap->field_fill)
1558 SAM("WARNING: on exit, filling field buffer %i\n",
1559 peasycap->field_read);
1562 JOM(8, "%i=caches\n", caches);
1565 /*---------------------------------------------------------------------------*/
1567 * DECIMATION AND COLOURSPACE CONVERSION.
1569 * THIS ROUTINE REQUIRES THAT ALL THE DATA TO BE READ RESIDES ON ONE PAGE
1570 * AND THAT ALL THE DATA TO BE WRITTEN RESIDES ON ONE (DIFFERENT) PAGE.
1571 * THE CALLING ROUTINE MUST ENSURE THAT THIS REQUIREMENT IS MET, AND MUST
1572 * ALSO ENSURE THAT much IS EVEN.
1574 * much BYTES ARE READ, AT LEAST (bytesperpixel * much)/2 BYTES ARE WRITTEN
1575 * IF THERE IS NO DECIMATION, HALF THIS AMOUNT IF THERE IS DECIMATION.
1577 * mask IS ZERO WHEN NO SPECIAL BEHAVIOUR REQUIRED. OTHERWISE IT IS SET THUS:
1578 * 0x03 & mask = number of bytes to be written to cache instead of to
1580 * 0x04 & mask => use argument margin to set the chrominance for last pixel
1581 * 0x08 & mask => do not set the chrominance for last pixel
1583 * YUV to RGB CONVERSION IS (OR SHOULD BE) ITU-R BT 601.
1585 * THERE IS A LOT OF CODE REPETITION IN THIS ROUTINE IN ORDER TO AVOID
1586 * INEFFICIENT SWITCHING INSIDE INNER LOOPS. REARRANGING THE LOGIC TO
1587 * REDUCE CODE LENGTH WILL GENERALLY IMPAIR RUNTIME PERFORMANCE. BEWARE.
1589 /*---------------------------------------------------------------------------*/
1590 static int redaub(struct easycap *peasycap,
1591 void *pad, void *pex, int much, int more,
1592 u8 mask, u8 margin, bool isuy)
1594 static s32 ay[256], bu[256], rv[256], gu[256], gv[256];
1596 u8 r, g, b, y, u, v, c, *p2, *p3, *pz, *pr;
1598 bool byteswaporder, decimatepixel, last;
1603 SAM("MISTAKE: much is odd\n");
1606 bytesperpixel = peasycap->bytesperpixel;
1607 byteswaporder = peasycap->byteswaporder;
1608 decimatepixel = peasycap->decimatepixel;
1610 /*---------------------------------------------------------------------------*/
1612 for (j = 0; j < 112; j++) {
1613 tmp = (0xFF00 & (453 * j)) >> 8;
1614 bu[j + 128] = tmp; bu[127 - j] = -tmp;
1615 tmp = (0xFF00 & (359 * j)) >> 8;
1616 rv[j + 128] = tmp; rv[127 - j] = -tmp;
1617 tmp = (0xFF00 & (88 * j)) >> 8;
1618 gu[j + 128] = tmp; gu[127 - j] = -tmp;
1619 tmp = (0xFF00 & (183 * j)) >> 8;
1620 gv[j + 128] = tmp; gv[127 - j] = -tmp;
1622 for (j = 0; j < 16; j++) {
1623 bu[j] = bu[16]; rv[j] = rv[16];
1624 gu[j] = gu[16]; gv[j] = gv[16];
1626 for (j = 240; j < 256; j++) {
1627 bu[j] = bu[239]; rv[j] = rv[239];
1628 gu[j] = gu[239]; gv[j] = gv[239];
1630 for (j = 16; j < 236; j++)
1632 for (j = 0; j < 16; j++)
1634 for (j = 236; j < 256; j++)
1636 JOM(8, "lookup tables are prepared\n");
1638 pcache = peasycap->pcache;
1640 pcache = &peasycap->cache[0];
1641 /*---------------------------------------------------------------------------*/
1643 * TRANSFER CONTENTS OF CACHE TO THE FRAME BUFFER
1645 /*---------------------------------------------------------------------------*/
1647 SAM("MISTAKE: pcache is NULL\n");
1651 if (pcache != &peasycap->cache[0])
1652 JOM(16, "cache has %i bytes\n", (int)(pcache - &peasycap->cache[0]));
1653 p2 = &peasycap->cache[0];
1654 p3 = (u8 *)pad - (int)(pcache - &peasycap->cache[0]);
1655 while (p2 < pcache) {
1658 pcache = &peasycap->cache[0];
1660 SAM("MISTAKE: pointer misalignment\n");
1663 /*---------------------------------------------------------------------------*/
1664 rump = (int)(0x03 & mask);
1666 p2 = (u8 *)pex; pz = p2 + much; pr = p3 + more; last = false;
1675 JOM(16, "%4i=much %4i=more %i=rump\n", much, more, rump);
1677 /*---------------------------------------------------------------------------*/
1678 switch (bytesperpixel) {
1680 if (!decimatepixel) {
1681 memcpy(pad, pex, (size_t)much);
1682 if (!byteswaporder) {
1687 p3 = (u8 *)pad; pz = p3 + much;
1697 if (!byteswaporder) {
1698 /* UYVY DECIMATED */
1699 p2 = (u8 *)pex; p3 = (u8 *)pad; pz = p2 + much;
1702 *(p3 + 1) = *(p2 + 1);
1703 *(p3 + 2) = *(p2 + 2);
1704 *(p3 + 3) = *(p2 + 3);
1709 /* YUYV DECIMATED */
1710 p2 = (u8 *)pex; p3 = (u8 *)pad; pz = p2 + much;
1714 *(p3 + 2) = *(p2 + 3);
1715 *(p3 + 3) = *(p2 + 2);
1725 if (!decimatepixel) {
1726 if (!byteswaporder) {
1729 if (pr <= (p3 + bytesperpixel))
1734 if (last && (0x0C & mask)) {
1750 tmp = ay[(int)y] + rv[(int)v];
1751 r = (255 < tmp) ? 255 : ((0 > tmp) ?
1753 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
1754 g = (255 < tmp) ? 255 : ((0 > tmp) ?
1756 tmp = ay[(int)y] + bu[(int)u];
1757 b = (255 < tmp) ? 255 : ((0 > tmp) ?
1761 pcache = &peasycap->cache[0];
1762 switch (bytesperpixel - rump) {
1776 SAM("MISTAKE: %i=rump\n",
1777 bytesperpixel - rump);
1791 p3 += bytesperpixel;
1797 if (pr <= (p3 + bytesperpixel))
1802 if (last && (0x0C & mask)) {
1819 tmp = ay[(int)y] + rv[(int)v];
1820 r = (255 < tmp) ? 255 : ((0 > tmp) ?
1822 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
1823 g = (255 < tmp) ? 255 : ((0 > tmp) ?
1825 tmp = ay[(int)y] + bu[(int)u];
1826 b = (255 < tmp) ? 255 : ((0 > tmp) ?
1830 pcache = &peasycap->cache[0];
1831 switch (bytesperpixel - rump) {
1845 SAM("MISTAKE: %i=rump\n",
1846 bytesperpixel - rump);
1860 p3 += bytesperpixel;
1865 if (!byteswaporder) {
1868 if (pr <= (p3 + bytesperpixel))
1873 if (last && (0x0C & mask)) {
1890 tmp = ay[(int)y] + rv[(int)v];
1891 r = (255 < tmp) ? 255 : ((0 > tmp) ?
1893 tmp = ay[(int)y] - gu[(int)u] -
1895 g = (255 < tmp) ? 255 : ((0 > tmp) ?
1897 tmp = ay[(int)y] + bu[(int)u];
1898 b = (255 < tmp) ? 255 : ((0 > tmp) ?
1902 pcache = &peasycap->cache[0];
1903 switch (bytesperpixel - rump) {
1919 bytesperpixel - rump);
1929 p3 += bytesperpixel;
1939 if (pr <= (p3 + bytesperpixel))
1944 if (last && (0x0C & mask)) {
1962 tmp = ay[(int)y] + rv[(int)v];
1963 r = (255 < tmp) ? 255 : ((0 > tmp) ?
1965 tmp = ay[(int)y] - gu[(int)u] -
1967 g = (255 < tmp) ? 255 : ((0 > tmp) ?
1969 tmp = ay[(int)y] + bu[(int)u];
1970 b = (255 < tmp) ? 255 : ((0 > tmp) ?
1974 pcache = &peasycap->cache[0];
1975 switch (bytesperpixel - rump) {
1991 bytesperpixel - rump);
2001 p3 += bytesperpixel;
2014 if (!decimatepixel) {
2015 if (!byteswaporder) {
2018 if (pr <= (p3 + bytesperpixel))
2023 if (last && (0x0C & mask)) {
2039 tmp = ay[(int)y] + rv[(int)v];
2040 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2042 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
2043 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2045 tmp = ay[(int)y] + bu[(int)u];
2046 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2050 pcache = &peasycap->cache[0];
2051 switch (bytesperpixel - rump) {
2074 SAM("MISTAKE: %i=rump\n",
2075 bytesperpixel - rump);
2090 p3 += bytesperpixel;
2098 if (pr <= (p3 + bytesperpixel))
2103 if (last && (0x0C & mask)) {
2119 tmp = ay[(int)y] + rv[(int)v];
2120 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2122 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
2123 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2125 tmp = ay[(int)y] + bu[(int)u];
2126 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2130 pcache = &peasycap->cache[0];
2131 switch (bytesperpixel - rump) {
2154 SAM("MISTAKE: %i=rump\n",
2155 bytesperpixel - rump);
2169 p3 += bytesperpixel;
2174 if (!byteswaporder) {
2179 if (pr <= (p3 + bytesperpixel))
2184 if (last && (0x0C & mask)) {
2202 tmp = ay[(int)y] + rv[(int)v];
2203 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2205 tmp = ay[(int)y] - gu[(int)u] -
2207 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2209 tmp = ay[(int)y] + bu[(int)u];
2210 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2214 pcache = &peasycap->cache[0];
2215 switch (bytesperpixel - rump) {
2252 p3 += bytesperpixel;
2263 if (pr <= (p3 + bytesperpixel))
2268 if (last && (0x0C & mask)) {
2285 tmp = ay[(int)y] + rv[(int)v];
2286 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2288 tmp = ay[(int)y] - gu[(int)u] -
2290 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2292 tmp = ay[(int)y] + bu[(int)u];
2293 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2297 pcache = &peasycap->cache[0];
2298 switch (bytesperpixel - rump) {
2323 bytesperpixel - rump);
2334 p3 += bytesperpixel;
2345 SAM("MISTAKE: %i=bytesperpixel\n", bytesperpixel);
2351 /*****************************************************************************/
2353 * SEE CORBET ET AL. "LINUX DEVICE DRIVERS", 3rd EDITION, PAGES 430-434
2355 /*****************************************************************************/
2356 static void easycap_vma_open(struct vm_area_struct *pvma)
2358 struct easycap *peasycap;
2360 peasycap = pvma->vm_private_data;
2362 SAY("ERROR: peasycap is NULL\n");
2365 peasycap->vma_many++;
2366 JOT(8, "%i=peasycap->vma_many\n", peasycap->vma_many);
2369 /*****************************************************************************/
2370 static void easycap_vma_close(struct vm_area_struct *pvma)
2372 struct easycap *peasycap;
2374 peasycap = pvma->vm_private_data;
2376 SAY("ERROR: peasycap is NULL\n");
2379 peasycap->vma_many--;
2380 JOT(8, "%i=peasycap->vma_many\n", peasycap->vma_many);
2383 /*****************************************************************************/
2384 static int easycap_vma_fault(struct vm_area_struct *pvma, struct vm_fault *pvmf)
2389 struct easycap *peasycap;
2391 retcode = VM_FAULT_NOPAGE;
2394 SAY("pvma is NULL\n");
2398 SAY("pvmf is NULL\n");
2402 k = (pvmf->pgoff) / (FRAME_BUFFER_SIZE/PAGE_SIZE);
2403 m = (pvmf->pgoff) % (FRAME_BUFFER_SIZE/PAGE_SIZE);
2406 JOT(4, "%4i=k, %4i=m\n", k, m);
2408 JOT(16, "%4i=k, %4i=m\n", k, m);
2410 if ((0 > k) || (FRAME_BUFFER_MANY <= k)) {
2411 SAY("ERROR: buffer index %i out of range\n", k);
2414 if ((0 > m) || (FRAME_BUFFER_SIZE/PAGE_SIZE <= m)) {
2415 SAY("ERROR: page number %i out of range\n", m);
2418 peasycap = pvma->vm_private_data;
2420 SAY("ERROR: peasycap is NULL\n");
2423 /*---------------------------------------------------------------------------*/
2424 pbuf = peasycap->frame_buffer[k][m].pgo;
2426 SAM("ERROR: pbuf is NULL\n");
2429 page = virt_to_page(pbuf);
2431 SAM("ERROR: page is NULL\n");
2435 /*---------------------------------------------------------------------------*/
2437 SAM("ERROR: page is NULL after get_page(page)\n");
2440 retcode = VM_FAULT_MINOR;
2445 static const struct vm_operations_struct easycap_vm_ops = {
2446 .open = easycap_vma_open,
2447 .close = easycap_vma_close,
2448 .fault = easycap_vma_fault,
2451 static int easycap_mmap(struct file *file, struct vm_area_struct *pvma)
2455 pvma->vm_ops = &easycap_vm_ops;
2456 pvma->vm_flags |= VM_RESERVED;
2458 pvma->vm_private_data = file->private_data;
2459 easycap_vma_open(pvma);
2462 /*****************************************************************************/
2463 /*---------------------------------------------------------------------------*/
2465 * ON COMPLETION OF A VIDEO URB ITS DATA IS COPIED TO THE FIELD BUFFERS
2466 * PROVIDED peasycap->video_idle IS ZERO. REGARDLESS OF THIS BEING TRUE,
2467 * IT IS RESUBMITTED PROVIDED peasycap->video_isoc_streaming IS NOT ZERO.
2469 * THIS FUNCTION IS AN INTERRUPT SERVICE ROUTINE AND MUST NOT SLEEP.
2471 * INFORMATION ABOUT THE VALIDITY OF THE CONTENTS OF THE FIELD BUFFER ARE
2472 * STORED IN THE TWO-BYTE STATUS PARAMETER
2473 * peasycap->field_buffer[peasycap->field_fill][0].kount
2474 * NOTICE THAT THE INFORMATION IS STORED ONLY WITH PAGE 0 OF THE FIELD BUFFER.
2476 * THE LOWER BYTE CONTAINS THE FIELD PARITY BYTE FURNISHED BY THE SAA7113H
2479 * THE UPPER BYTE IS ZERO IF NO PROBLEMS, OTHERWISE:
2480 * 0 != (kount & 0x8000) => AT LEAST ONE URB COMPLETED WITH ERRORS
2481 * 0 != (kount & 0x4000) => BUFFER HAS TOO MUCH DATA
2482 * 0 != (kount & 0x2000) => BUFFER HAS NOT ENOUGH DATA
2483 * 0 != (kount & 0x1000) => BUFFER HAS DATA FROM DISPARATE INPUTS
2484 * 0 != (kount & 0x0400) => RESERVED
2485 * 0 != (kount & 0x0200) => FIELD BUFFER NOT YET CHECKED
2486 * 0 != (kount & 0x0100) => BUFFER HAS TWO EXTRA BYTES - WHY?
2488 /*---------------------------------------------------------------------------*/
2489 static void easycap_complete(struct urb *purb)
2491 struct easycap *peasycap;
2492 struct data_buffer *pfield_buffer;
2494 int i, more, much, leap, rc, last;
2495 int videofieldamount;
2496 unsigned int override, bad;
2497 int framestatus, framelength, frameactual, frameoffset;
2501 SAY("ERROR: easycap_complete(): purb is NULL\n");
2504 peasycap = purb->context;
2506 SAY("ERROR: easycap_complete(): peasycap is NULL\n");
2509 if (peasycap->video_eof)
2511 for (i = 0; i < VIDEO_ISOC_BUFFER_MANY; i++)
2512 if (purb->transfer_buffer == peasycap->video_isoc_buffer[i].pgo)
2514 JOM(16, "%2i=urb\n", i);
2515 last = peasycap->video_isoc_sequence;
2516 if ((((VIDEO_ISOC_BUFFER_MANY - 1) == last) && (0 != i)) ||
2517 (((VIDEO_ISOC_BUFFER_MANY - 1) != last) && ((last + 1) != i))) {
2518 JOM(16, "ERROR: out-of-order urbs %i,%i ... continuing\n",
2521 peasycap->video_isoc_sequence = i;
2523 if (peasycap->video_idle) {
2524 JOM(16, "%i=video_idle %i=video_isoc_streaming\n",
2525 peasycap->video_idle, peasycap->video_isoc_streaming);
2526 if (peasycap->video_isoc_streaming) {
2527 rc = usb_submit_urb(purb, GFP_ATOMIC);
2529 SAM("%s:%d ENOMEM\n", strerror(rc), rc);
2531 SAM("ERROR: while %i=video_idle, "
2533 "failed with rc:\n",
2534 peasycap->video_idle);
2540 /*---------------------------------------------------------------------------*/
2541 if (FIELD_BUFFER_MANY <= peasycap->field_fill) {
2542 SAM("ERROR: bad peasycap->field_fill\n");
2546 if ((-ESHUTDOWN == purb->status) || (-ENOENT == purb->status)) {
2547 JOM(8, "urb status -ESHUTDOWN or -ENOENT\n");
2551 (peasycap->field_buffer[peasycap->field_fill][0].kount) |= 0x8000 ;
2552 SAM("ERROR: bad urb status -%s: %d\n",
2553 strerror(purb->status), purb->status);
2554 /*---------------------------------------------------------------------------*/
2556 for (i = 0; i < purb->number_of_packets; i++) {
2557 if (0 != purb->iso_frame_desc[i].status) {
2558 (peasycap->field_buffer
2559 [peasycap->field_fill][0].kount) |= 0x8000 ;
2560 /* FIXME: 1. missing '-' check boundaries */
2562 strerror(purb->iso_frame_desc[i].status));
2564 framestatus = purb->iso_frame_desc[i].status;
2565 framelength = purb->iso_frame_desc[i].length;
2566 frameactual = purb->iso_frame_desc[i].actual_length;
2567 frameoffset = purb->iso_frame_desc[i].offset;
2569 JOM(16, "frame[%2i]:"
2574 i, framestatus, frameactual, framelength, frameoffset);
2575 if (!purb->iso_frame_desc[i].status) {
2576 more = purb->iso_frame_desc[i].actual_length;
2577 pfield_buffer = &peasycap->field_buffer
2578 [peasycap->field_fill][peasycap->field_page];
2579 videofieldamount = (peasycap->field_page *
2581 (int)(pfield_buffer->pto - pfield_buffer->pgo);
2583 peasycap->video_mt++;
2585 if (peasycap->video_mt) {
2586 JOM(8, "%4i empty video urb frames\n",
2587 peasycap->video_mt);
2588 peasycap->video_mt = 0;
2590 if (FIELD_BUFFER_MANY <= peasycap->field_fill) {
2591 SAM("ERROR: bad peasycap->field_fill\n");
2594 if (FIELD_BUFFER_SIZE/PAGE_SIZE <=
2595 peasycap->field_page) {
2596 SAM("ERROR: bad peasycap->field_page\n");
2599 pfield_buffer = &peasycap->field_buffer
2600 [peasycap->field_fill][peasycap->field_page];
2601 pu = (u8 *)(purb->transfer_buffer +
2602 purb->iso_frame_desc[i].offset);
2607 /*--------------------------------------------------------------------------*/
2609 * EIGHT-BYTE END-OF-VIDEOFIELD MARKER.
2610 * NOTE: A SUCCESSION OF URB FRAMES FOLLOWING THIS ARE EMPTY,
2611 * CORRESPONDING TO THE FIELD FLYBACK (VERTICAL BLANKING) PERIOD.
2613 * PROVIDED THE FIELD BUFFER CONTAINS GOOD DATA AS INDICATED BY A ZERO UPPER
2615 * peasycap->field_buffer[peasycap->field_fill][0].kount
2616 * THE CONTENTS OF THE FIELD BUFFER ARE OFFERED TO dqbuf(), field_read IS
2617 * UPDATED AND field_fill IS BUMPED. IF THE FIELD BUFFER CONTAINS BAD DATA
2618 * NOTHING IS OFFERED TO dqbuf().
2620 * THE DECISION ON WHETHER THE PARITY OF THE OFFERED FIELD BUFFER IS RIGHT
2621 * RESTS WITH dqbuf().
2623 /*---------------------------------------------------------------------------*/
2624 if ((8 == more) || override) {
2625 if (videofieldamount >
2626 peasycap->videofieldamount) {
2627 if (2 == videofieldamount -
2630 (peasycap->field_buffer
2631 [peasycap->field_fill]
2632 [0].kount) |= 0x0100;
2633 peasycap->video_junk += (1 +
2634 VIDEO_JUNK_TOLERATE);
2636 (peasycap->field_buffer
2637 [peasycap->field_fill]
2638 [0].kount) |= 0x4000;
2639 } else if (videofieldamount <
2642 (peasycap->field_buffer
2643 [peasycap->field_fill]
2644 [0].kount) |= 0x2000;
2646 bad = 0xFF00 & peasycap->field_buffer
2647 [peasycap->field_fill]
2650 (peasycap->video_junk)--;
2651 if (-VIDEO_JUNK_TOLERATE >
2652 peasycap->video_junk)
2653 peasycap->video_junk =
2654 -VIDEO_JUNK_TOLERATE;
2655 peasycap->field_read =
2658 if (FIELD_BUFFER_MANY <=
2663 peasycap->field_page = 0;
2664 pfield_buffer = &peasycap->
2670 pfield_buffer->pto =
2672 JOM(8, "bumped to: %i="
2676 peasycap->field_fill,
2678 pfield_buffer->kount);
2679 JOM(8, "field buffer %i has "
2680 "%i bytes fit to be "
2682 peasycap->field_read,
2684 JOM(8, "wakeup call to "
2689 peasycap->field_read,
2690 peasycap->field_fill,
2694 field_read][0].kount);
2695 wake_up_interruptible
2699 peasycap->video_junk++;
2701 peasycap->video_junk +=
2702 (1 + VIDEO_JUNK_TOLERATE/2);
2703 JOM(8, "field buffer %i had %i "
2704 "bytes, now discarded: "
2706 peasycap->field_fill,
2709 peasycap->field_buffer
2710 [peasycap->field_fill][0].
2712 (peasycap->field_fill)++;
2714 if (FIELD_BUFFER_MANY <=
2715 peasycap->field_fill)
2716 peasycap->field_fill = 0;
2717 peasycap->field_page = 0;
2719 &peasycap->field_buffer
2720 [peasycap->field_fill]
2721 [peasycap->field_page];
2722 pfield_buffer->pto =
2725 JOM(8, "bumped to: %i=peasycap->"
2726 "field_fill %i=parity\n",
2727 peasycap->field_fill,
2728 0x00FF & pfield_buffer->kount);
2731 JOM(8, "end-of-field: received "
2732 "parity byte 0x%02X\n",
2735 pfield_buffer->kount = 0x0000;
2737 pfield_buffer->kount = 0x0001;
2738 pfield_buffer->input = 0x08 |
2739 (0x07 & peasycap->input);
2740 JOM(8, "end-of-field: 0x%02X=kount\n",
2741 0xFF & pfield_buffer->kount);
2744 /*---------------------------------------------------------------------------*/
2746 * COPY more BYTES FROM ISOC BUFFER TO FIELD BUFFER
2748 /*---------------------------------------------------------------------------*/
2752 if (FIELD_BUFFER_MANY <= peasycap->field_fill) {
2753 SAM("ERROR: bad peasycap->field_fill\n");
2756 if (FIELD_BUFFER_SIZE/PAGE_SIZE <= peasycap->field_page) {
2757 SAM("ERROR: bad peasycap->field_page\n");
2760 pfield_buffer = &peasycap->field_buffer
2761 [peasycap->field_fill][peasycap->field_page];
2763 pfield_buffer = &peasycap->field_buffer
2764 [peasycap->field_fill]
2765 [peasycap->field_page];
2766 if (PAGE_SIZE < (pfield_buffer->pto -
2767 pfield_buffer->pgo)) {
2768 SAM("ERROR: bad pfield_buffer->pto\n");
2771 if (PAGE_SIZE == (pfield_buffer->pto -
2772 pfield_buffer->pgo)) {
2773 (peasycap->field_page)++;
2774 if (FIELD_BUFFER_SIZE/PAGE_SIZE <=
2775 peasycap->field_page) {
2776 JOM(16, "wrapping peasycap->"
2778 peasycap->field_page = 0;
2780 pfield_buffer = &peasycap->
2782 [peasycap->field_fill]
2783 [peasycap->field_page];
2784 pfield_buffer->pto = pfield_buffer->pgo;
2785 pfield_buffer->input = 0x08 |
2786 (0x07 & peasycap->input);
2787 if ((peasycap->field_buffer[peasycap->
2790 pfield_buffer->input)
2791 (peasycap->field_buffer
2792 [peasycap->field_fill]
2793 [0]).kount |= 0x1000;
2797 (int)(pfield_buffer->pto -
2798 pfield_buffer->pgo);
2802 memcpy(pfield_buffer->pto, pu, much);
2804 (pfield_buffer->pto) += much;
2811 /*---------------------------------------------------------------------------*/
2813 * RESUBMIT THIS URB, UNLESS A SEVERE PERSISTENT ERROR CONDITION EXISTS.
2815 * IF THE WAIT QUEUES ARE NOT CLEARED IN RESPONSE TO AN ERROR CONDITION
2816 * THE USERSPACE PROGRAM, E.G. mplayer, MAY HANG ON EXIT. BEWARE.
2818 /*---------------------------------------------------------------------------*/
2819 if (VIDEO_ISOC_BUFFER_MANY <= peasycap->video_junk) {
2820 SAM("easycap driver shutting down on condition green\n");
2821 peasycap->status = 1;
2822 peasycap->video_eof = 1;
2823 peasycap->video_junk = 0;
2824 wake_up_interruptible(&peasycap->wq_video);
2825 #if !defined(PERSEVERE)
2826 peasycap->audio_eof = 1;
2827 wake_up_interruptible(&peasycap->wq_audio);
2828 #endif /*PERSEVERE*/
2831 if (peasycap->video_isoc_streaming) {
2832 rc = usb_submit_urb(purb, GFP_ATOMIC);
2834 SAM("%s: %d\n", strerror(rc), rc);
2836 SAM("ERROR: while %i=video_idle, "
2838 "failed with rc:\n",
2839 peasycap->video_idle);
2845 static const struct v4l2_file_operations v4l2_fops = {
2846 .owner = THIS_MODULE,
2847 .open = easycap_open_noinode,
2848 .unlocked_ioctl = easycap_unlocked_ioctl,
2849 .poll = easycap_poll,
2850 .mmap = easycap_mmap,
2854 * When the device is plugged, this function is called three times,
2855 * one for each interface.
2857 static int easycap_usb_probe(struct usb_interface *intf,
2858 const struct usb_device_id *id)
2860 struct usb_device *usbdev;
2861 struct usb_host_interface *alt;
2862 struct usb_endpoint_descriptor *ep;
2863 struct usb_interface_descriptor *interface;
2865 struct easycap *peasycap;
2867 struct data_urb *pdata_urb;
2869 u8 bInterfaceNumber;
2871 u8 bInterfaceSubClass;
2873 int okalt[8], isokalt;
2879 struct easycap_format *peasycap_format;
2881 struct inputset *inputset;
2883 usbdev = interface_to_usbdev(intf);
2885 alt = usb_altnum_to_altsetting(intf, 0);
2887 SAY("ERROR: usb_host_interface not found\n");
2891 interface = &alt->desc;
2893 SAY("ERROR: intf_descriptor is NULL\n");
2897 /* Get properties of probed interface */
2898 bInterfaceNumber = interface->bInterfaceNumber;
2899 bInterfaceClass = interface->bInterfaceClass;
2900 bInterfaceSubClass = interface->bInterfaceSubClass;
2902 JOT(4, "intf[%i]: num_altsetting=%i\n",
2903 bInterfaceNumber, intf->num_altsetting);
2904 JOT(4, "intf[%i]: cur_altsetting - altsetting=%li\n",
2906 (long int)(intf->cur_altsetting - intf->altsetting));
2907 JOT(4, "intf[%i]: bInterfaceClass=0x%02X bInterfaceSubClass=0x%02X\n",
2908 bInterfaceNumber, bInterfaceClass, bInterfaceSubClass);
2911 * A new struct easycap is always allocated when interface 0 is probed.
2912 * It is not possible here to free any existing struct easycap.
2913 * This should have been done by easycap_delete() when the device was
2914 * physically unplugged.
2915 * The allocated struct easycap is saved for later usage when
2916 * interfaces 1 and 2 are probed.
2918 if (0 == bInterfaceNumber) {
2919 peasycap = kzalloc(sizeof(struct easycap), GFP_KERNEL);
2921 SAY("ERROR: Could not allocate peasycap\n");
2925 /* Perform urgent initializations */
2926 peasycap->minor = -1;
2927 kref_init(&peasycap->kref);
2928 JOM(8, "intf[%i]: after kref_init(..._video) "
2929 "%i=peasycap->kref.refcount.counter\n",
2930 bInterfaceNumber, peasycap->kref.refcount.counter);
2933 peasycap->gain = (s8)clamp(easycap_gain, 0, 31);
2935 init_waitqueue_head(&peasycap->wq_video);
2936 init_waitqueue_head(&peasycap->wq_audio);
2937 init_waitqueue_head(&peasycap->wq_trigger);
2939 if (mutex_lock_interruptible(&mutex_dongle)) {
2940 SAY("ERROR: cannot down mutex_dongle\n");
2941 return -ERESTARTSYS;
2944 for (ndong = 0; ndong < DONGLE_MANY; ndong++) {
2945 if ((!easycapdc60_dongle[ndong].peasycap) &&
2946 (!mutex_is_locked(&easycapdc60_dongle
2947 [ndong].mutex_video)) &&
2948 (!mutex_is_locked(&easycapdc60_dongle
2949 [ndong].mutex_audio))) {
2950 easycapdc60_dongle[ndong].peasycap = peasycap;
2951 peasycap->isdongle = ndong;
2952 JOM(8, "intf[%i]: peasycap-->easycap"
2953 "_dongle[%i].peasycap\n",
2954 bInterfaceNumber, ndong);
2959 if (DONGLE_MANY <= ndong) {
2960 SAM("ERROR: too many dongles\n");
2961 mutex_unlock(&mutex_dongle);
2964 mutex_unlock(&mutex_dongle);
2966 peasycap->allocation_video_struct = sizeof(struct easycap);
2968 /* and further initialize the structure */
2969 peasycap->pusb_device = usbdev;
2970 peasycap->pusb_interface = intf;
2972 peasycap->microphone = false;
2974 peasycap->video_interface = -1;
2975 peasycap->video_altsetting_on = -1;
2976 peasycap->video_altsetting_off = -1;
2977 peasycap->video_endpointnumber = -1;
2978 peasycap->video_isoc_maxframesize = -1;
2979 peasycap->video_isoc_buffer_size = -1;
2981 peasycap->audio_interface = -1;
2982 peasycap->audio_altsetting_on = -1;
2983 peasycap->audio_altsetting_off = -1;
2984 peasycap->audio_endpointnumber = -1;
2985 peasycap->audio_isoc_maxframesize = -1;
2986 peasycap->audio_isoc_buffer_size = -1;
2988 peasycap->frame_buffer_many = FRAME_BUFFER_MANY;
2990 /* Dynamically fill in the available formats */
2991 rc = easycap_video_fillin_formats();
2993 SAM("ERROR: fillin_formats() rc = %i\n", rc);
2996 JOM(4, "%i formats available\n", rc);
2998 /* Populate easycap.inputset[] */
2999 inputset = peasycap->inputset;
3000 fmtidx = peasycap->ntsc ? NTSC_M : PAL_BGHIN;
3003 for (i = 0; 0xFFFF != easycap_standard[i].mask; i++) {
3004 if (fmtidx == easycap_standard[i].v4l2_standard.index) {
3006 for (k = 0; k < INPUT_MANY; k++)
3007 inputset[k].standard_offset = i;
3009 mask = easycap_standard[i].mask;
3014 "inputset->standard_offset unpopulated, %i=m\n", m);
3018 peasycap_format = &easycap_format[0];
3020 for (i = 0; peasycap_format->v4l2_format.fmt.pix.width; i++) {
3021 struct v4l2_pix_format *pix =
3022 &peasycap_format->v4l2_format.fmt.pix;
3023 if (((peasycap_format->mask & 0x0F) == (mask & 0x0F)) &&
3024 pix->field == V4L2_FIELD_NONE &&
3025 pix->pixelformat == V4L2_PIX_FMT_UYVY &&
3026 pix->width == 640 && pix->height == 480) {
3028 for (k = 0; k < INPUT_MANY; k++)
3029 inputset[k].format_offset = i;
3035 SAM("ERROR: inputset[]->format_offset unpopulated\n");
3040 for (i = 0; 0xFFFFFFFF != easycap_control[i].id; i++) {
3041 value = easycap_control[i].default_value;
3042 if (V4L2_CID_BRIGHTNESS == easycap_control[i].id) {
3044 for (k = 0; k < INPUT_MANY; k++)
3045 inputset[k].brightness = value;
3046 } else if (V4L2_CID_CONTRAST == easycap_control[i].id) {
3048 for (k = 0; k < INPUT_MANY; k++)
3049 inputset[k].contrast = value;
3050 } else if (V4L2_CID_SATURATION == easycap_control[i].id) {
3052 for (k = 0; k < INPUT_MANY; k++)
3053 inputset[k].saturation = value;
3054 } else if (V4L2_CID_HUE == easycap_control[i].id) {
3056 for (k = 0; k < INPUT_MANY; k++)
3057 inputset[k].hue = value;
3062 SAM("ERROR: inputset[]->brightness underpopulated\n");
3065 for (k = 0; k < INPUT_MANY; k++)
3066 inputset[k].input = k;
3067 JOM(4, "populated inputset[]\n");
3068 JOM(4, "finished initialization\n");
3072 * FIXME: Identify the appropriate pointer
3073 * peasycap for interfaces 1 and 2.
3074 * The address of peasycap->pusb_device
3075 * is reluctantly used for this purpose.
3077 for (ndong = 0; ndong < DONGLE_MANY; ndong++) {
3078 if (usbdev == easycapdc60_dongle[ndong].peasycap->
3080 peasycap = easycapdc60_dongle[ndong].peasycap;
3081 JOT(8, "intf[%i]: dongle[%i].peasycap\n",
3082 bInterfaceNumber, ndong);
3086 if (DONGLE_MANY <= ndong) {
3087 SAY("ERROR: peasycap is unknown when probing interface %i\n",
3092 SAY("ERROR: peasycap is NULL when probing interface %i\n",
3098 if ((USB_CLASS_VIDEO == bInterfaceClass) ||
3099 (USB_CLASS_VENDOR_SPEC == bInterfaceClass)) {
3100 if (-1 == peasycap->video_interface) {
3101 peasycap->video_interface = bInterfaceNumber;
3102 JOM(4, "setting peasycap->video_interface=%i\n",
3103 peasycap->video_interface);
3105 if (peasycap->video_interface != bInterfaceNumber) {
3106 SAM("ERROR: attempting to reset "
3107 "peasycap->video_interface\n");
3108 SAM("...... continuing with "
3109 "%i=peasycap->video_interface\n",
3110 peasycap->video_interface);
3113 } else if ((USB_CLASS_AUDIO == bInterfaceClass) &&
3114 (USB_SUBCLASS_AUDIOSTREAMING == bInterfaceSubClass)) {
3115 if (-1 == peasycap->audio_interface) {
3116 peasycap->audio_interface = bInterfaceNumber;
3117 JOM(4, "setting peasycap->audio_interface=%i\n",
3118 peasycap->audio_interface);
3120 if (peasycap->audio_interface != bInterfaceNumber) {
3121 SAM("ERROR: attempting to reset "
3122 "peasycap->audio_interface\n");
3123 SAM("...... continuing with "
3124 "%i=peasycap->audio_interface\n",
3125 peasycap->audio_interface);
3131 * Investigate all altsettings. This is done in detail
3132 * because USB device 05e1:0408 has disparate incarnations.
3135 for (i = 0; i < intf->num_altsetting; i++) {
3136 alt = usb_altnum_to_altsetting(intf, i);
3138 SAM("ERROR: alt is NULL\n");
3141 interface = &alt->desc;
3143 SAM("ERROR: intf_descriptor is NULL\n");
3147 if (0 == interface->bNumEndpoints)
3148 JOM(4, "intf[%i]alt[%i] has no endpoints\n",
3149 bInterfaceNumber, i);
3150 for (j = 0; j < interface->bNumEndpoints; j++) {
3151 ep = &alt->endpoint[j].desc;
3153 SAM("ERROR: ep is NULL.\n");
3154 SAM("...... skipping\n");
3158 if (!usb_endpoint_is_isoc_in(ep)) {
3159 JOM(4, "intf[%i]alt[%i]end[%i] is a %d endpoint\n",
3161 i, j, ep->bmAttributes);
3162 if (usb_endpoint_dir_out(ep)) {
3163 SAM("ERROR: OUT endpoint unexpected\n");
3164 SAM("...... continuing\n");
3168 switch (bInterfaceClass) {
3169 case USB_CLASS_VIDEO:
3170 case USB_CLASS_VENDOR_SPEC: {
3171 if (ep->wMaxPacketSize) {
3196 if (-1 == peasycap->
3197 video_altsetting_off) {
3199 video_altsetting_off =
3205 video_altsetting_off);
3207 SAM("ERROR: peasycap"
3208 "->video_altsetting_"
3209 "off already set\n");
3212 "%i=peasycap->video_"
3215 video_altsetting_off);
3220 case USB_CLASS_AUDIO: {
3221 if (bInterfaceSubClass !=
3222 USB_SUBCLASS_AUDIOSTREAMING)
3226 "peasycap is NULL\n");
3229 if (ep->wMaxPacketSize) {
3231 okalt[isokalt] = i ;
3254 if (-1 == peasycap->
3255 audio_altsetting_off) {
3257 audio_altsetting_off =
3263 audio_altsetting_off);
3265 SAM("ERROR: peasycap"
3266 "->audio_altsetting_"
3267 "off already set\n");
3274 audio_altsetting_off);
3282 if (0 == ep->wMaxPacketSize) {
3283 JOM(4, "intf[%i]alt[%i]end[%i] "
3284 "has zero packet size\n",
3285 bInterfaceNumber, i, j);
3290 /* Perform initialization of the probed interface */
3291 JOM(4, "initialization begins for interface %i\n",
3292 interface->bInterfaceNumber);
3293 switch (bInterfaceNumber) {
3294 /* 0: Video interface */
3297 SAM("MISTAKE: peasycap is NULL\n");
3301 SAM("ERROR: no viable video_altsetting_on\n");
3304 peasycap->video_altsetting_on = okalt[isokalt - 1];
3305 JOM(4, "%i=video_altsetting_on <====\n",
3306 peasycap->video_altsetting_on);
3308 /* Decide video streaming parameters */
3309 peasycap->video_endpointnumber = okepn[isokalt - 1];
3310 JOM(4, "%i=video_endpointnumber\n", peasycap->video_endpointnumber);
3311 maxpacketsize = okmps[isokalt - 1];
3313 peasycap->video_isoc_maxframesize =
3314 min(maxpacketsize, USB_2_0_MAXPACKETSIZE);
3315 if (0 >= peasycap->video_isoc_maxframesize) {
3316 SAM("ERROR: bad video_isoc_maxframesize\n");
3317 SAM(" possibly because port is USB 1.1\n");
3320 JOM(4, "%i=video_isoc_maxframesize\n",
3321 peasycap->video_isoc_maxframesize);
3323 peasycap->video_isoc_framesperdesc = VIDEO_ISOC_FRAMESPERDESC;
3324 JOM(4, "%i=video_isoc_framesperdesc\n",
3325 peasycap->video_isoc_framesperdesc);
3326 if (0 >= peasycap->video_isoc_framesperdesc) {
3327 SAM("ERROR: bad video_isoc_framesperdesc\n");
3330 peasycap->video_isoc_buffer_size =
3331 peasycap->video_isoc_maxframesize *
3332 peasycap->video_isoc_framesperdesc;
3333 JOM(4, "%i=video_isoc_buffer_size\n",
3334 peasycap->video_isoc_buffer_size);
3335 if ((PAGE_SIZE << VIDEO_ISOC_ORDER) <
3336 peasycap->video_isoc_buffer_size) {
3337 SAM("MISTAKE: peasycap->video_isoc_buffer_size too big\n");
3340 if (-1 == peasycap->video_interface) {
3341 SAM("MISTAKE: video_interface is unset\n");
3344 if (-1 == peasycap->video_altsetting_on) {
3345 SAM("MISTAKE: video_altsetting_on is unset\n");
3348 if (-1 == peasycap->video_altsetting_off) {
3349 SAM("MISTAKE: video_interface_off is unset\n");
3352 if (-1 == peasycap->video_endpointnumber) {
3353 SAM("MISTAKE: video_endpointnumber is unset\n");
3356 if (-1 == peasycap->video_isoc_maxframesize) {
3357 SAM("MISTAKE: video_isoc_maxframesize is unset\n");
3360 if (-1 == peasycap->video_isoc_buffer_size) {
3361 SAM("MISTAKE: video_isoc_buffer_size is unset\n");
3366 * Allocate memory for video buffers.
3367 * Lists must be initialized first.
3369 INIT_LIST_HEAD(&(peasycap->urb_video_head));
3370 peasycap->purb_video_head = &(peasycap->urb_video_head);
3371 JOM(4, "allocating %i frame buffers of size %li\n",
3372 FRAME_BUFFER_MANY, (long int)FRAME_BUFFER_SIZE);
3373 JOM(4, ".... each scattered over %li pages\n",
3374 FRAME_BUFFER_SIZE/PAGE_SIZE);
3376 for (k = 0; k < FRAME_BUFFER_MANY; k++) {
3377 for (m = 0; m < FRAME_BUFFER_SIZE/PAGE_SIZE; m++) {
3378 if (peasycap->frame_buffer[k][m].pgo)
3379 SAM("attempting to reallocate frame "
3382 pbuf = (void *)__get_free_page(GFP_KERNEL);
3384 SAM("ERROR: Could not allocate frame "
3385 "buffer %i page %i\n", k, m);
3389 peasycap->allocation_video_page += 1;
3390 peasycap->frame_buffer[k][m].pgo = pbuf;
3392 peasycap->frame_buffer[k][m].pto =
3393 peasycap->frame_buffer[k][m].pgo;
3397 peasycap->frame_fill = 0;
3398 peasycap->frame_read = 0;
3399 JOM(4, "allocation of frame buffers done: %i pages\n", k *
3401 JOM(4, "allocating %i field buffers of size %li\n",
3402 FIELD_BUFFER_MANY, (long int)FIELD_BUFFER_SIZE);
3403 JOM(4, ".... each scattered over %li pages\n",
3404 FIELD_BUFFER_SIZE/PAGE_SIZE);
3406 for (k = 0; k < FIELD_BUFFER_MANY; k++) {
3407 for (m = 0; m < FIELD_BUFFER_SIZE/PAGE_SIZE; m++) {
3408 if (peasycap->field_buffer[k][m].pgo) {
3409 SAM("ERROR: attempting to reallocate "
3412 pbuf = (void *) __get_free_page(GFP_KERNEL);
3414 SAM("ERROR: Could not allocate field"
3415 " buffer %i page %i\n", k, m);
3419 peasycap->allocation_video_page += 1;
3420 peasycap->field_buffer[k][m].pgo = pbuf;
3422 peasycap->field_buffer[k][m].pto =
3423 peasycap->field_buffer[k][m].pgo;
3425 peasycap->field_buffer[k][0].kount = 0x0200;
3427 peasycap->field_fill = 0;
3428 peasycap->field_page = 0;
3429 peasycap->field_read = 0;
3430 JOM(4, "allocation of field buffers done: %i pages\n", k *
3432 JOM(4, "allocating %i isoc video buffers of size %i\n",
3433 VIDEO_ISOC_BUFFER_MANY,
3434 peasycap->video_isoc_buffer_size);
3435 JOM(4, ".... each occupying contiguous memory pages\n");
3437 for (k = 0; k < VIDEO_ISOC_BUFFER_MANY; k++) {
3438 pbuf = (void *)__get_free_pages(GFP_KERNEL,
3441 SAM("ERROR: Could not allocate isoc video buffer "
3445 peasycap->allocation_video_page +=
3446 BIT(VIDEO_ISOC_ORDER);
3448 peasycap->video_isoc_buffer[k].pgo = pbuf;
3449 peasycap->video_isoc_buffer[k].pto =
3450 pbuf + peasycap->video_isoc_buffer_size;
3451 peasycap->video_isoc_buffer[k].kount = k;
3453 JOM(4, "allocation of isoc video buffers done: %i pages\n",
3454 k * (0x01 << VIDEO_ISOC_ORDER));
3456 /* Allocate and initialize multiple struct usb */
3457 JOM(4, "allocating %i struct urb.\n", VIDEO_ISOC_BUFFER_MANY);
3458 JOM(4, "using %i=peasycap->video_isoc_framesperdesc\n",
3459 peasycap->video_isoc_framesperdesc);
3460 JOM(4, "using %i=peasycap->video_isoc_maxframesize\n",
3461 peasycap->video_isoc_maxframesize);
3462 JOM(4, "using %i=peasycap->video_isoc_buffer_sizen",
3463 peasycap->video_isoc_buffer_size);
3465 for (k = 0; k < VIDEO_ISOC_BUFFER_MANY; k++) {
3466 purb = usb_alloc_urb(peasycap->video_isoc_framesperdesc,
3469 SAM("ERROR: usb_alloc_urb returned NULL for buffer "
3474 peasycap->allocation_video_urb += 1;
3475 pdata_urb = kzalloc(sizeof(struct data_urb), GFP_KERNEL);
3477 SAM("ERROR: Could not allocate struct data_urb.\n");
3481 peasycap->allocation_video_struct +=
3482 sizeof(struct data_urb);
3484 pdata_urb->purb = purb;
3485 pdata_urb->isbuf = k;
3486 pdata_urb->length = 0;
3487 list_add_tail(&(pdata_urb->list_head),
3488 peasycap->purb_video_head);
3490 /* Initialize allocated urbs */
3492 JOM(4, "initializing video urbs thus:\n");
3493 JOM(4, " purb->interval = 1;\n");
3494 JOM(4, " purb->dev = peasycap->pusb_device;\n");
3495 JOM(4, " purb->pipe = usb_rcvisocpipe"
3496 "(peasycap->pusb_device,%i);\n",
3497 peasycap->video_endpointnumber);
3498 JOM(4, " purb->transfer_flags = URB_ISO_ASAP;\n");
3499 JOM(4, " purb->transfer_buffer = peasycap->"
3500 "video_isoc_buffer[.].pgo;\n");
3501 JOM(4, " purb->transfer_buffer_length = %i;\n",
3502 peasycap->video_isoc_buffer_size);
3503 JOM(4, " purb->complete = easycap_complete;\n");
3504 JOM(4, " purb->context = peasycap;\n");
3505 JOM(4, " purb->start_frame = 0;\n");
3506 JOM(4, " purb->number_of_packets = %i;\n",
3507 peasycap->video_isoc_framesperdesc);
3508 JOM(4, " for (j = 0; j < %i; j++)\n",
3509 peasycap->video_isoc_framesperdesc);
3511 JOM(4, " purb->iso_frame_desc[j].offset = j*%i;\n",
3512 peasycap->video_isoc_maxframesize);
3513 JOM(4, " purb->iso_frame_desc[j].length = %i;\n",
3514 peasycap->video_isoc_maxframesize);
3519 purb->dev = peasycap->pusb_device;
3520 purb->pipe = usb_rcvisocpipe(peasycap->pusb_device,
3521 peasycap->video_endpointnumber);
3522 purb->transfer_flags = URB_ISO_ASAP;
3523 purb->transfer_buffer = peasycap->video_isoc_buffer[k].pgo;
3524 purb->transfer_buffer_length =
3525 peasycap->video_isoc_buffer_size;
3526 purb->complete = easycap_complete;
3527 purb->context = peasycap;
3528 purb->start_frame = 0;
3529 purb->number_of_packets = peasycap->video_isoc_framesperdesc;
3530 for (j = 0; j < peasycap->video_isoc_framesperdesc; j++) {
3531 purb->iso_frame_desc[j].offset = j *
3532 peasycap->video_isoc_maxframesize;
3533 purb->iso_frame_desc[j].length =
3534 peasycap->video_isoc_maxframesize;
3537 JOM(4, "allocation of %i struct urb done.\n", k);
3539 /* Save pointer peasycap in this interface */
3540 usb_set_intfdata(intf, peasycap);
3543 * It is essential to initialize the hardware before,
3544 * rather than after, the device is registered,
3545 * because some udev rules triggers easycap_open()
3546 * immediately after registration, causing a clash.
3548 peasycap->ntsc = easycap_ntsc;
3549 JOM(8, "defaulting initially to %s\n",
3550 easycap_ntsc ? "NTSC" : "PAL");
3551 rc = reset(peasycap);
3553 SAM("ERROR: reset() rc = %i\n", rc);
3557 /* The video device can now be registered */
3558 if (v4l2_device_register(&intf->dev, &peasycap->v4l2_device)) {
3559 SAM("v4l2_device_register() failed\n");
3562 JOM(4, "registered device instance: %s\n",
3563 peasycap->v4l2_device.name);
3566 * FIXME: This is believed to be harmless,
3567 * but may well be unnecessary or wrong.
3569 peasycap->video_device.v4l2_dev = NULL;
3572 strcpy(&peasycap->video_device.name[0], "easycapdc60");
3573 peasycap->video_device.fops = &v4l2_fops;
3574 peasycap->video_device.minor = -1;
3575 peasycap->video_device.release = (void *)(&videodev_release);
3577 video_set_drvdata(&(peasycap->video_device), (void *)peasycap);
3579 if (0 != (video_register_device(&(peasycap->video_device),
3580 VFL_TYPE_GRABBER, -1))) {
3582 "Not able to register with videodev\n");
3583 videodev_release(&(peasycap->video_device));
3587 peasycap->registered_video++;
3588 SAM("registered with videodev: %i=minor\n",
3589 peasycap->video_device.minor);
3590 peasycap->minor = peasycap->video_device.minor;
3594 /* 1: Audio control */
3597 SAM("MISTAKE: peasycap is NULL\n");
3600 /* Save pointer peasycap in this interface */
3601 usb_set_intfdata(intf, peasycap);
3602 JOM(4, "no initialization required for interface %i\n",
3603 interface->bInterfaceNumber);
3606 /* 2: Audio streaming */
3609 SAM("MISTAKE: peasycap is NULL\n");
3613 SAM("ERROR: no viable audio_altsetting_on\n");
3616 peasycap->audio_altsetting_on = okalt[isokalt - 1];
3617 JOM(4, "%i=audio_altsetting_on <====\n",
3618 peasycap->audio_altsetting_on);
3620 peasycap->audio_endpointnumber = okepn[isokalt - 1];
3621 JOM(4, "%i=audio_endpointnumber\n", peasycap->audio_endpointnumber);
3623 peasycap->audio_isoc_maxframesize = okmps[isokalt - 1];
3624 JOM(4, "%i=audio_isoc_maxframesize\n",
3625 peasycap->audio_isoc_maxframesize);
3626 if (0 >= peasycap->audio_isoc_maxframesize) {
3627 SAM("ERROR: bad audio_isoc_maxframesize\n");
3630 if (9 == peasycap->audio_isoc_maxframesize) {
3631 peasycap->ilk |= 0x02;
3632 SAM("audio hardware is microphone\n");
3633 peasycap->microphone = true;
3634 peasycap->audio_pages_per_fragment =
3635 PAGES_PER_AUDIO_FRAGMENT;
3636 } else if (256 == peasycap->audio_isoc_maxframesize) {
3637 peasycap->ilk &= ~0x02;
3638 SAM("audio hardware is AC'97\n");
3639 peasycap->microphone = false;
3640 peasycap->audio_pages_per_fragment =
3641 PAGES_PER_AUDIO_FRAGMENT;
3643 SAM("hardware is unidentified:\n");
3644 SAM("%i=audio_isoc_maxframesize\n",
3645 peasycap->audio_isoc_maxframesize);
3649 peasycap->audio_bytes_per_fragment =
3650 peasycap->audio_pages_per_fragment * PAGE_SIZE;
3651 peasycap->audio_buffer_page_many = (AUDIO_FRAGMENT_MANY *
3652 peasycap->audio_pages_per_fragment);
3654 JOM(4, "%6i=AUDIO_FRAGMENT_MANY\n", AUDIO_FRAGMENT_MANY);
3655 JOM(4, "%6i=audio_pages_per_fragment\n",
3656 peasycap->audio_pages_per_fragment);
3657 JOM(4, "%6i=audio_bytes_per_fragment\n",
3658 peasycap->audio_bytes_per_fragment);
3659 JOM(4, "%6i=audio_buffer_page_many\n",
3660 peasycap->audio_buffer_page_many);
3662 peasycap->audio_isoc_framesperdesc = AUDIO_ISOC_FRAMESPERDESC;
3664 JOM(4, "%i=audio_isoc_framesperdesc\n",
3665 peasycap->audio_isoc_framesperdesc);
3666 if (0 >= peasycap->audio_isoc_framesperdesc) {
3667 SAM("ERROR: bad audio_isoc_framesperdesc\n");
3671 peasycap->audio_isoc_buffer_size =
3672 peasycap->audio_isoc_maxframesize *
3673 peasycap->audio_isoc_framesperdesc;
3674 JOM(4, "%i=audio_isoc_buffer_size\n",
3675 peasycap->audio_isoc_buffer_size);
3676 if (AUDIO_ISOC_BUFFER_SIZE < peasycap->audio_isoc_buffer_size) {
3677 SAM("MISTAKE: audio_isoc_buffer_size bigger "
3678 "than %li=AUDIO_ISOC_BUFFER_SIZE\n",
3679 AUDIO_ISOC_BUFFER_SIZE);
3682 if (-1 == peasycap->audio_interface) {
3683 SAM("MISTAKE: audio_interface is unset\n");
3686 if (-1 == peasycap->audio_altsetting_on) {
3687 SAM("MISTAKE: audio_altsetting_on is unset\n");
3690 if (-1 == peasycap->audio_altsetting_off) {
3691 SAM("MISTAKE: audio_interface_off is unset\n");
3694 if (-1 == peasycap->audio_endpointnumber) {
3695 SAM("MISTAKE: audio_endpointnumber is unset\n");
3698 if (-1 == peasycap->audio_isoc_maxframesize) {
3699 SAM("MISTAKE: audio_isoc_maxframesize is unset\n");
3702 if (-1 == peasycap->audio_isoc_buffer_size) {
3703 SAM("MISTAKE: audio_isoc_buffer_size is unset\n");
3708 * Allocate memory for audio buffers.
3709 * Lists must be initialized first.
3711 INIT_LIST_HEAD(&(peasycap->urb_audio_head));
3712 peasycap->purb_audio_head = &(peasycap->urb_audio_head);
3714 JOM(4, "allocating %i isoc audio buffers of size %i\n",
3715 AUDIO_ISOC_BUFFER_MANY,
3716 peasycap->audio_isoc_buffer_size);
3717 JOM(4, ".... each occupying contiguous memory pages\n");
3719 for (k = 0; k < AUDIO_ISOC_BUFFER_MANY; k++) {
3720 pbuf = (void *)__get_free_pages(GFP_KERNEL,
3723 SAM("ERROR: Could not allocate isoc audio buffer "
3727 peasycap->allocation_audio_page +=
3728 BIT(AUDIO_ISOC_ORDER);
3730 peasycap->audio_isoc_buffer[k].pgo = pbuf;
3731 peasycap->audio_isoc_buffer[k].pto = pbuf +
3732 peasycap->audio_isoc_buffer_size;
3733 peasycap->audio_isoc_buffer[k].kount = k;
3735 JOM(4, "allocation of isoc audio buffers done.\n");
3737 /* Allocate and initialize urbs */
3738 JOM(4, "allocating %i struct urb.\n", AUDIO_ISOC_BUFFER_MANY);
3739 JOM(4, "using %i=peasycap->audio_isoc_framesperdesc\n",
3740 peasycap->audio_isoc_framesperdesc);
3741 JOM(4, "using %i=peasycap->audio_isoc_maxframesize\n",
3742 peasycap->audio_isoc_maxframesize);
3743 JOM(4, "using %i=peasycap->audio_isoc_buffer_size\n",
3744 peasycap->audio_isoc_buffer_size);
3746 for (k = 0; k < AUDIO_ISOC_BUFFER_MANY; k++) {
3747 purb = usb_alloc_urb(peasycap->audio_isoc_framesperdesc,
3750 SAM("ERROR: usb_alloc_urb returned NULL for buffer "
3754 peasycap->allocation_audio_urb += 1 ;
3755 pdata_urb = kzalloc(sizeof(struct data_urb), GFP_KERNEL);
3758 SAM("ERROR: Could not allocate struct data_urb.\n");
3761 peasycap->allocation_audio_struct +=
3762 sizeof(struct data_urb);
3764 pdata_urb->purb = purb;
3765 pdata_urb->isbuf = k;
3766 pdata_urb->length = 0;
3767 list_add_tail(&(pdata_urb->list_head),
3768 peasycap->purb_audio_head);
3771 JOM(4, "initializing audio urbs thus:\n");
3772 JOM(4, " purb->interval = 1;\n");
3773 JOM(4, " purb->dev = peasycap->pusb_device;\n");
3774 JOM(4, " purb->pipe = usb_rcvisocpipe(peasycap->"
3775 "pusb_device,%i);\n",
3776 peasycap->audio_endpointnumber);
3777 JOM(4, " purb->transfer_flags = URB_ISO_ASAP;\n");
3778 JOM(4, " purb->transfer_buffer = "
3779 "peasycap->audio_isoc_buffer[.].pgo;\n");
3780 JOM(4, " purb->transfer_buffer_length = %i;\n",
3781 peasycap->audio_isoc_buffer_size);
3782 JOM(4, " purb->complete = easycap_alsa_complete;\n");
3783 JOM(4, " purb->context = peasycap;\n");
3784 JOM(4, " purb->start_frame = 0;\n");
3785 JOM(4, " purb->number_of_packets = %i;\n",
3786 peasycap->audio_isoc_framesperdesc);
3787 JOM(4, " for (j = 0; j < %i; j++)\n",
3788 peasycap->audio_isoc_framesperdesc);
3790 JOM(4, " purb->iso_frame_desc[j].offset = j*%i;\n",
3791 peasycap->audio_isoc_maxframesize);
3792 JOM(4, " purb->iso_frame_desc[j].length = %i;\n",
3793 peasycap->audio_isoc_maxframesize);
3798 purb->dev = peasycap->pusb_device;
3799 purb->pipe = usb_rcvisocpipe(peasycap->pusb_device,
3800 peasycap->audio_endpointnumber);
3801 purb->transfer_flags = URB_ISO_ASAP;
3802 purb->transfer_buffer = peasycap->audio_isoc_buffer[k].pgo;
3803 purb->transfer_buffer_length =
3804 peasycap->audio_isoc_buffer_size;
3805 purb->complete = easycap_alsa_complete;
3806 purb->context = peasycap;
3807 purb->start_frame = 0;
3808 purb->number_of_packets = peasycap->audio_isoc_framesperdesc;
3809 for (j = 0; j < peasycap->audio_isoc_framesperdesc; j++) {
3810 purb->iso_frame_desc[j].offset = j *
3811 peasycap->audio_isoc_maxframesize;
3812 purb->iso_frame_desc[j].length =
3813 peasycap->audio_isoc_maxframesize;
3816 JOM(4, "allocation of %i struct urb done.\n", k);
3818 /* Save pointer peasycap in this interface */
3819 usb_set_intfdata(intf, peasycap);
3821 /* The audio device can now be registered */
3822 JOM(4, "initializing ALSA card\n");
3824 rc = easycap_alsa_probe(peasycap);
3826 dev_err(&intf->dev, "easycap_alsa_probe() rc = %i\n",
3832 JOM(8, "kref_get() with %i=kref.refcount.counter\n",
3833 peasycap->kref.refcount.counter);
3834 kref_get(&peasycap->kref);
3835 peasycap->registered_audio++;
3838 /* Interfaces other than 0,1,2 are unexpected */
3840 JOM(4, "ERROR: unexpected interface %i\n", bInterfaceNumber);
3843 SAM("ends successfully for interface %i\n", bInterfaceNumber);
3846 /*****************************************************************************/
3847 /*---------------------------------------------------------------------------*/
3849 * WHEN THIS FUNCTION IS CALLED THE EasyCAP HAS ALREADY BEEN PHYSICALLY
3850 * UNPLUGGED. HENCE peasycap->pusb_device IS NO LONGER VALID.
3852 * THIS FUNCTION AFFECTS ALSA. BEWARE.
3854 /*---------------------------------------------------------------------------*/
3855 static void easycap_usb_disconnect(struct usb_interface *pusb_interface)
3857 struct usb_host_interface *pusb_host_interface;
3858 struct usb_interface_descriptor *pusb_interface_descriptor;
3859 struct easycap *peasycap;
3861 u8 bInterfaceNumber;
3865 pusb_host_interface = pusb_interface->cur_altsetting;
3866 if (!pusb_host_interface) {
3867 JOT(4, "ERROR: pusb_host_interface is NULL\n");
3870 pusb_interface_descriptor = &(pusb_host_interface->desc);
3871 if (!pusb_interface_descriptor) {
3872 JOT(4, "ERROR: pusb_interface_descriptor is NULL\n");
3875 bInterfaceNumber = pusb_interface_descriptor->bInterfaceNumber;
3876 minor = pusb_interface->minor;
3877 JOT(4, "intf[%i]: minor=%i\n", bInterfaceNumber, minor);
3879 if (1 == bInterfaceNumber)
3882 peasycap = usb_get_intfdata(pusb_interface);
3884 SAY("ERROR: peasycap is NULL\n");
3887 /*---------------------------------------------------------------------------*/
3889 * IF THE WAIT QUEUES ARE NOT CLEARED A DEADLOCK IS POSSIBLE. BEWARE.
3891 /*---------------------------------------------------------------------------*/
3892 peasycap->video_eof = 1;
3893 peasycap->audio_eof = 1;
3894 wake_up_interruptible(&(peasycap->wq_video));
3895 wake_up_interruptible(&(peasycap->wq_audio));
3897 switch (bInterfaceNumber) {
3899 easycap_video_kill_urbs(peasycap);
3902 easycap_audio_kill_urbs(peasycap);
3907 /*--------------------------------------------------------------------------*/
3911 * THIS PROCEDURE WILL BLOCK UNTIL easycap_poll(), VIDEO IOCTL AND AUDIO
3912 * IOCTL ARE ALL UNLOCKED. IF THIS IS NOT DONE AN Oops CAN OCCUR WHEN
3913 * AN EasyCAP IS UNPLUGGED WHILE THE URBS ARE RUNNING. BEWARE.
3915 /*--------------------------------------------------------------------------*/
3916 kd = easycap_isdongle(peasycap);
3917 switch (bInterfaceNumber) {
3919 if (0 <= kd && DONGLE_MANY > kd) {
3920 wake_up_interruptible(&peasycap->wq_video);
3921 JOM(4, "about to lock dongle[%i].mutex_video\n", kd);
3922 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].
3925 "cannot lock dongle[%i].mutex_video\n", kd);
3928 JOM(4, "locked dongle[%i].mutex_video\n", kd);
3930 SAY("ERROR: %i=kd is bad: cannot lock dongle\n", kd);
3932 /*---------------------------------------------------------------------------*/
3933 if (!peasycap->v4l2_device.name[0]) {
3934 SAM("ERROR: peasycap->v4l2_device.name is empty\n");
3935 if (0 <= kd && DONGLE_MANY > kd)
3936 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
3939 v4l2_device_disconnect(&peasycap->v4l2_device);
3940 JOM(4, "v4l2_device_disconnect() OK\n");
3941 v4l2_device_unregister(&peasycap->v4l2_device);
3942 JOM(4, "v4l2_device_unregister() OK\n");
3944 video_unregister_device(&peasycap->video_device);
3945 JOM(4, "intf[%i]: video_unregister_device() minor=%i\n",
3946 bInterfaceNumber, minor);
3947 peasycap->registered_video--;
3948 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
3950 if (0 <= kd && DONGLE_MANY > kd) {
3951 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
3952 JOM(4, "unlocked dongle[%i].mutex_video\n", kd);
3957 if (0 <= kd && DONGLE_MANY > kd) {
3958 wake_up_interruptible(&peasycap->wq_audio);
3959 JOM(4, "about to lock dongle[%i].mutex_audio\n", kd);
3960 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].
3963 "cannot lock dongle[%i].mutex_audio\n", kd);
3966 JOM(4, "locked dongle[%i].mutex_audio\n", kd);
3968 SAY("ERROR: %i=kd is bad: cannot lock dongle\n", kd);
3969 if (0 != snd_card_free(peasycap->psnd_card)) {
3970 SAY("ERROR: snd_card_free() failed\n");
3972 peasycap->psnd_card = NULL;
3973 (peasycap->registered_audio)--;
3975 if (0 <= kd && DONGLE_MANY > kd) {
3976 mutex_unlock(&easycapdc60_dongle[kd].mutex_audio);
3977 JOM(4, "unlocked dongle[%i].mutex_audio\n", kd);
3984 /*---------------------------------------------------------------------------*/
3986 * CALL easycap_delete() IF NO REMAINING REFERENCES TO peasycap
3987 * (ALSO WHEN ALSA HAS BEEN IN USE)
3989 /*---------------------------------------------------------------------------*/
3990 if (!peasycap->kref.refcount.counter) {
3991 SAM("ERROR: peasycap->kref.refcount.counter is zero "
3992 "so cannot call kref_put()\n");
3993 SAM("ending unsuccessfully: may cause memory leak\n");
3996 if (0 <= kd && DONGLE_MANY > kd) {
3997 JOM(4, "about to lock dongle[%i].mutex_video\n", kd);
3998 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].mutex_video)) {
3999 SAY("ERROR: cannot lock dongle[%i].mutex_video\n", kd);
4000 SAM("ending unsuccessfully: may cause memory leak\n");
4003 JOM(4, "locked dongle[%i].mutex_video\n", kd);
4004 JOM(4, "about to lock dongle[%i].mutex_audio\n", kd);
4005 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].mutex_audio)) {
4006 SAY("ERROR: cannot lock dongle[%i].mutex_audio\n", kd);
4007 mutex_unlock(&(easycapdc60_dongle[kd].mutex_video));
4008 JOM(4, "unlocked dongle[%i].mutex_video\n", kd);
4009 SAM("ending unsuccessfully: may cause memory leak\n");
4012 JOM(4, "locked dongle[%i].mutex_audio\n", kd);
4014 JOM(4, "intf[%i]: %i=peasycap->kref.refcount.counter\n",
4015 bInterfaceNumber, (int)peasycap->kref.refcount.counter);
4016 kref_put(&peasycap->kref, easycap_delete);
4017 JOT(4, "intf[%i]: kref_put() done.\n", bInterfaceNumber);
4018 if (0 <= kd && DONGLE_MANY > kd) {
4019 mutex_unlock(&(easycapdc60_dongle[kd].mutex_audio));
4020 JOT(4, "unlocked dongle[%i].mutex_audio\n", kd);
4021 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
4022 JOT(4, "unlocked dongle[%i].mutex_video\n", kd);
4024 /*---------------------------------------------------------------------------*/
4028 /*****************************************************************************/
4030 /*---------------------------------------------------------------------------*/
4032 * PARAMETERS APPLICABLE TO ENTIRE DRIVER, I.E. BOTH VIDEO AND AUDIO
4034 /*---------------------------------------------------------------------------*/
4035 static struct usb_device_id easycap_usb_device_id_table[] = {
4036 {USB_DEVICE(USB_EASYCAP_VENDOR_ID, USB_EASYCAP_PRODUCT_ID)},
4040 MODULE_DEVICE_TABLE(usb, easycap_usb_device_id_table);
4041 static struct usb_driver easycap_usb_driver = {
4043 .id_table = easycap_usb_device_id_table,
4044 .probe = easycap_usb_probe,
4045 .disconnect = easycap_usb_disconnect,
4048 static int __init easycap_module_init(void)
4052 printk(KERN_INFO "Easycap version: "EASYCAP_DRIVER_VERSION "\n");
4054 JOT(4, "begins. %i=debug %i=bars %i=gain\n",
4055 easycap_debug, easycap_bars, easycap_gain);
4057 mutex_init(&mutex_dongle);
4058 for (k = 0; k < DONGLE_MANY; k++) {
4059 easycapdc60_dongle[k].peasycap = NULL;
4060 mutex_init(&easycapdc60_dongle[k].mutex_video);
4061 mutex_init(&easycapdc60_dongle[k].mutex_audio);
4063 rc = usb_register(&easycap_usb_driver);
4065 printk(KERN_ERR "Easycap: usb_register failed rc=%d\n", rc);
4069 /*****************************************************************************/
4070 static void __exit easycap_module_exit(void)
4072 usb_deregister(&easycap_usb_driver);
4074 /*****************************************************************************/
4076 module_init(easycap_module_init);
4077 module_exit(easycap_module_exit);
4079 /*****************************************************************************/