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, "NTCS 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);
70 const char *strerror(int err)
72 #define ERRNOSTR(_e) case _e: return # _e
84 ERRNOSTR(EINPROGRESS);
91 ERRNOSTR(EPFNOSUPPORT);
92 ERRNOSTR(EAFNOSUPPORT);
94 ERRNOSTR(EADDRNOTAVAIL);
100 ERRNOSTR(ECONNRESET);
106 default: return "unknown";
112 /*---------------------------------------------------------------------------*/
114 * PARAMETERS USED WHEN REGISTERING THE VIDEO INTERFACE
116 * NOTE: SOME KERNELS IGNORE usb_class_driver.minor_base, AS MENTIONED BY
117 * CORBET ET AL. "LINUX DEVICE DRIVERS", 3rd EDITION, PAGE 253.
118 * THIS IS THE CASE FOR OpenSUSE.
120 /*---------------------------------------------------------------------------*/
121 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
122 /****************************************************************************/
123 /*---------------------------------------------------------------------------*/
125 * THIS ROUTINE DOES NOT DETECT DUPLICATE OCCURRENCES OF POINTER peasycap
127 /*---------------------------------------------------------------------------*/
128 int isdongle(struct easycap *peasycap)
133 for (k = 0; k < DONGLE_MANY; k++) {
134 if (easycapdc60_dongle[k].peasycap == peasycap) {
135 peasycap->isdongle = k;
141 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
142 static int easycap_open(struct inode *inode, struct file *file)
144 struct video_device *pvideo_device;
145 struct easycap *peasycap;
149 SAY("==========OPEN=========\n");
151 pvideo_device = video_devdata(file);
152 if (!pvideo_device) {
153 SAY("ERROR: pvideo_device is NULL.\n");
156 peasycap = (struct easycap *)video_get_drvdata(pvideo_device);
158 SAY("ERROR: peasycap is NULL\n");
161 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
162 SAY("ERROR: bad peasycap: %p\n", peasycap);
165 if (!peasycap->pusb_device) {
166 SAM("ERROR: peasycap->pusb_device is NULL\n");
169 JOM(16, "peasycap->pusb_device=%p\n", peasycap->pusb_device);
171 file->private_data = peasycap;
172 rc = wakeup_device(peasycap->pusb_device);
174 JOM(8, "wakeup_device() OK\n");
176 SAM("ERROR: wakeup_device() rc = %i\n", rc);
178 SAM("ERROR: wakeup_device() returned -ENODEV\n");
180 SAM("ERROR: wakeup_device() rc = %i\n", rc);
184 rc = reset(peasycap);
186 SAM("ERROR: reset() rc = %i\n", rc);
192 /*****************************************************************************/
193 /*---------------------------------------------------------------------------*/
195 * RESET THE HARDWARE TO ITS REFERENCE STATE.
197 * THIS ROUTINE MAY BE CALLED REPEATEDLY IF easycap_complete() DETECTS
198 * A BAD VIDEO FRAME SIZE.
200 /*---------------------------------------------------------------------------*/
201 static int reset(struct easycap *peasycap)
203 struct easycap_standard const *peasycap_standard;
204 int fmtidx, input, rate;
209 SAY("ERROR: peasycap is NULL\n");
212 input = peasycap->input;
214 /*---------------------------------------------------------------------------*/
216 * IF THE SAA7113H HAS ALREADY ACQUIRED SYNC, USE ITS HARDWARE-DETECTED
217 * FIELD FREQUENCY TO DISTINGUISH NTSC FROM PAL. THIS IS ESSENTIAL FOR
218 * gstreamer AND OTHER USERSPACE PROGRAMS WHICH MAY NOT ATTEMPT TO INITIATE
219 * A SWITCH BETWEEN PAL AND NTSC.
221 * FUNCTION ready_saa() MAY REQUIRE A SUBSTANTIAL FRACTION OF A SECOND TO
222 * COMPLETE, SO SHOULD NOT BE INVOKED WITHOUT GOOD REASON.
224 /*---------------------------------------------------------------------------*/
226 JOM(8, "peasycap->ntsc=%d\n", peasycap->ntsc);
228 rate = ready_saa(peasycap->pusb_device);
230 JOM(8, "not ready to capture after %i ms ...\n", PATIENCE);
231 ntsc = !peasycap->ntsc;
232 JOM(8, "... trying %s ..\n", ntsc ? "NTSC" : "PAL");
233 rc = setup_stk(peasycap->pusb_device, ntsc);
235 SAM("ERROR: setup_stk() rc = %i\n", rc);
238 rc = setup_saa(peasycap->pusb_device, ntsc);
240 SAM("ERROR: setup_saa() rc = %i\n", rc);
244 rate = ready_saa(peasycap->pusb_device);
246 JOM(8, "not ready to capture after %i ms\n", PATIENCE);
247 JOM(8, "... saa register 0x1F has 0x%02X\n",
248 read_saa(peasycap->pusb_device, 0x1F));
249 ntsc = peasycap->ntsc;
251 JOM(8, "... success at second try: %i=rate\n", rate);
252 ntsc = (0 < (rate/2)) ? true : false ;
256 JOM(8, "... success at first try: %i=rate\n", rate);
257 ntsc = (0 < rate/2) ? true : false ;
259 JOM(8, "ntsc=%d\n", ntsc);
260 /*---------------------------------------------------------------------------*/
262 rc = setup_stk(peasycap->pusb_device, ntsc);
264 SAM("ERROR: setup_stk() rc = %i\n", rc);
267 rc = setup_saa(peasycap->pusb_device, ntsc);
269 SAM("ERROR: setup_saa() rc = %i\n", rc);
273 memset(peasycap->merit, 0, sizeof(peasycap->merit));
275 peasycap->video_eof = 0;
276 peasycap->audio_eof = 0;
277 do_gettimeofday(&peasycap->timeval7);
278 /*---------------------------------------------------------------------------*/
280 * RESTORE INPUT AND FORCE REFRESH OF STANDARD, FORMAT, ETC.
282 * WHILE THIS PROCEDURE IS IN PROGRESS, SOME IOCTL COMMANDS WILL RETURN -EBUSY.
284 /*---------------------------------------------------------------------------*/
285 peasycap->input = -8192;
286 peasycap->standard_offset = -8192;
287 fmtidx = ntsc ? NTSC_M : PAL_BGHIN;
289 peasycap_standard = &easycap_standard[0];
290 while (0xFFFF != peasycap_standard->mask) {
291 if (fmtidx == peasycap_standard->v4l2_standard.index) {
292 peasycap->inputset[input].standard_offset =
293 peasycap_standard - easycap_standard;
298 if (0xFFFF == peasycap_standard->mask) {
299 SAM("ERROR: standard not found\n");
302 JOM(8, "%i=peasycap->inputset[%i].standard_offset\n",
303 peasycap->inputset[input].standard_offset, input);
305 peasycap->format_offset = -8192;
306 peasycap->brightness = -8192;
307 peasycap->contrast = -8192;
308 peasycap->saturation = -8192;
309 peasycap->hue = -8192;
311 rc = newinput(peasycap, input);
314 SAM("ERROR: newinput(.,%i) rc = %i\n", rc, input);
317 JOM(4, "restored input, standard and format\n");
319 JOM(8, "true=peasycap->ntsc %d\n", peasycap->ntsc);
321 if (0 > peasycap->input) {
322 SAM("MISTAKE: %i=peasycap->input\n", peasycap->input);
325 if (0 > peasycap->standard_offset) {
326 SAM("MISTAKE: %i=peasycap->standard_offset\n",
327 peasycap->standard_offset);
330 if (0 > peasycap->format_offset) {
331 SAM("MISTAKE: %i=peasycap->format_offset\n",
332 peasycap->format_offset);
335 if (0 > peasycap->brightness) {
336 SAM("MISTAKE: %i=peasycap->brightness\n",
337 peasycap->brightness);
340 if (0 > peasycap->contrast) {
341 SAM("MISTAKE: %i=peasycap->contrast\n", peasycap->contrast);
344 if (0 > peasycap->saturation) {
345 SAM("MISTAKE: %i=peasycap->saturation\n",
346 peasycap->saturation);
349 if (0 > peasycap->hue) {
350 SAM("MISTAKE: %i=peasycap->hue\n", peasycap->hue);
355 /*****************************************************************************/
356 /*---------------------------------------------------------------------------*/
358 * IF THE REQUESTED INPUT IS THE SAME AS THE EXISTING INPUT, DO NOTHING.
360 * KILL URBS, CLEAR FIELD AND FRAME BUFFERS AND RESET THEIR
361 * _read AND _fill POINTERS.
362 * SELECT THE NEW INPUT.
363 * ADJUST THE STANDARD, FORMAT, BRIGHTNESS, CONTRAST, SATURATION AND HUE
364 * ON THE BASIS OF INFORMATION IN STRUCTURE easycap.inputset[input].
365 * RESUBMIT THE URBS IF STREAMING WAS ALREADY IN PROGRESS.
368 * THIS ROUTINE MAY BE CALLED FREQUENTLY BY ZONEMINDER VIA IOCTL,
369 * SO IT SHOULD WRITE ONLY SPARINGLY TO THE LOGFILE.
371 /*---------------------------------------------------------------------------*/
373 newinput(struct easycap *peasycap, int input)
375 int rc, k, m, mood, off;
376 int inputnow, video_idlenow, audio_idlenow;
380 SAY("ERROR: peasycap is NULL\n");
383 JOM(8, "%i=input sought\n", input);
385 if (0 > input && INPUT_MANY <= input)
387 inputnow = peasycap->input;
388 if (input == inputnow)
390 /*---------------------------------------------------------------------------*/
392 * IF STREAMING IS IN PROGRESS THE URBS ARE KILLED AT THIS
393 * STAGE AND WILL BE RESUBMITTED PRIOR TO EXIT FROM THE ROUTINE.
394 * IF NO STREAMING IS IN PROGRESS NO URBS WILL BE SUBMITTED BY THE
397 /*---------------------------------------------------------------------------*/
398 video_idlenow = peasycap->video_idle;
399 audio_idlenow = peasycap->audio_idle;
401 peasycap->video_idle = 1;
402 peasycap->audio_idle = 1;
403 if (peasycap->video_isoc_streaming) {
405 kill_video_urbs(peasycap);
409 /*---------------------------------------------------------------------------*/
410 if (!peasycap->pusb_device) {
411 SAM("ERROR: peasycap->pusb_device is NULL\n");
414 rc = usb_set_interface(peasycap->pusb_device,
415 peasycap->video_interface,
416 peasycap->video_altsetting_off);
418 SAM("ERROR: usb_set_interface() rc = %i\n", rc);
421 rc = stop_100(peasycap->pusb_device);
423 SAM("ERROR: stop_100() rc = %i\n", rc);
426 for (k = 0; k < FIELD_BUFFER_MANY; k++) {
427 for (m = 0; m < FIELD_BUFFER_SIZE/PAGE_SIZE; m++)
428 memset(peasycap->field_buffer[k][m].pgo, 0, PAGE_SIZE);
430 for (k = 0; k < FRAME_BUFFER_MANY; k++) {
431 for (m = 0; m < FRAME_BUFFER_SIZE/PAGE_SIZE; m++)
432 memset(peasycap->frame_buffer[k][m].pgo, 0, PAGE_SIZE);
434 peasycap->field_page = 0;
435 peasycap->field_read = 0;
436 peasycap->field_fill = 0;
438 peasycap->frame_read = 0;
439 peasycap->frame_fill = 0;
440 for (k = 0; k < peasycap->input; k++) {
441 (peasycap->frame_fill)++;
442 if (peasycap->frame_buffer_many <= peasycap->frame_fill)
443 peasycap->frame_fill = 0;
445 peasycap->input = input;
446 select_input(peasycap->pusb_device, peasycap->input, 9);
447 /*---------------------------------------------------------------------------*/
448 if (input == peasycap->inputset[input].input) {
449 off = peasycap->inputset[input].standard_offset;
450 if (off != peasycap->standard_offset) {
451 rc = adjust_standard(peasycap,
452 easycap_standard[off].v4l2_standard.id);
454 SAM("ERROR: adjust_standard() rc = %i\n", rc);
457 JOM(8, "%i=peasycap->standard_offset\n",
458 peasycap->standard_offset);
460 JOM(8, "%i=peasycap->standard_offset unchanged\n",
461 peasycap->standard_offset);
463 off = peasycap->inputset[input].format_offset;
464 if (off != peasycap->format_offset) {
465 struct v4l2_pix_format *pix =
466 &easycap_format[off].v4l2_format.fmt.pix;
467 rc = adjust_format(peasycap,
468 pix->width, pix->height,
469 pix->pixelformat, pix->field, false);
471 SAM("ERROR: adjust_format() rc = %i\n", rc);
474 JOM(8, "%i=peasycap->format_offset\n",
475 peasycap->format_offset);
477 JOM(8, "%i=peasycap->format_offset unchanged\n",
478 peasycap->format_offset);
480 mood = peasycap->inputset[input].brightness;
481 if (mood != peasycap->brightness) {
482 rc = adjust_brightness(peasycap, mood);
484 SAM("ERROR: adjust_brightness rc = %i\n", rc);
487 JOM(8, "%i=peasycap->brightness\n",
488 peasycap->brightness);
490 mood = peasycap->inputset[input].contrast;
491 if (mood != peasycap->contrast) {
492 rc = adjust_contrast(peasycap, mood);
494 SAM("ERROR: adjust_contrast rc = %i\n", rc);
497 JOM(8, "%i=peasycap->contrast\n", peasycap->contrast);
499 mood = peasycap->inputset[input].saturation;
500 if (mood != peasycap->saturation) {
501 rc = adjust_saturation(peasycap, mood);
503 SAM("ERROR: adjust_saturation rc = %i\n", rc);
506 JOM(8, "%i=peasycap->saturation\n",
507 peasycap->saturation);
509 mood = peasycap->inputset[input].hue;
510 if (mood != peasycap->hue) {
511 rc = adjust_hue(peasycap, mood);
513 SAM("ERROR: adjust_hue rc = %i\n", rc);
516 JOM(8, "%i=peasycap->hue\n", peasycap->hue);
519 SAM("MISTAKE: easycap.inputset[%i] unpopulated\n", input);
522 /*---------------------------------------------------------------------------*/
523 if (!peasycap->pusb_device) {
524 SAM("ERROR: peasycap->pusb_device is NULL\n");
527 rc = usb_set_interface(peasycap->pusb_device,
528 peasycap->video_interface,
529 peasycap->video_altsetting_on);
531 SAM("ERROR: usb_set_interface() rc = %i\n", rc);
534 rc = start_100(peasycap->pusb_device);
536 SAM("ERROR: start_100() rc = %i\n", rc);
540 submit_video_urbs(peasycap);
542 peasycap->video_isoc_sequence = VIDEO_ISOC_BUFFER_MANY - 1;
543 peasycap->video_idle = video_idlenow;
544 peasycap->audio_idle = audio_idlenow;
545 peasycap->video_junk = 0;
549 /*****************************************************************************/
550 int submit_video_urbs(struct easycap *peasycap)
552 struct data_urb *pdata_urb;
554 struct list_head *plist_head;
555 int j, isbad, nospc, m, rc;
559 SAY("ERROR: peasycap is NULL\n");
563 if (!peasycap->purb_video_head) {
564 SAY("ERROR: peasycap->urb_video_head uninitialized\n");
567 if (!peasycap->pusb_device) {
568 SAY("ERROR: peasycap->pusb_device is NULL\n");
571 if (!peasycap->video_isoc_streaming) {
572 JOM(4, "submission of all video urbs\n");
573 isbad = 0; nospc = 0; m = 0;
574 list_for_each(plist_head, (peasycap->purb_video_head)) {
575 pdata_urb = list_entry(plist_head,
576 struct data_urb, list_head);
577 if (pdata_urb && pdata_urb->purb) {
578 purb = pdata_urb->purb;
579 isbuf = pdata_urb->isbuf;
581 purb->dev = peasycap->pusb_device;
583 usb_rcvisocpipe(peasycap->pusb_device,
584 peasycap->video_endpointnumber);
585 purb->transfer_flags = URB_ISO_ASAP;
586 purb->transfer_buffer =
587 peasycap->video_isoc_buffer[isbuf].pgo;
588 purb->transfer_buffer_length =
589 peasycap->video_isoc_buffer_size;
590 purb->complete = easycap_complete;
591 purb->context = peasycap;
592 purb->start_frame = 0;
593 purb->number_of_packets =
594 peasycap->video_isoc_framesperdesc;
596 for (j = 0; j < peasycap->video_isoc_framesperdesc; j++) {
597 purb->iso_frame_desc[j]. offset =
598 j * peasycap->video_isoc_maxframesize;
599 purb->iso_frame_desc[j]. length =
600 peasycap->video_isoc_maxframesize;
603 rc = usb_submit_urb(purb, GFP_KERNEL);
606 SAM("ERROR: usb_submit_urb() failed "
607 "for urb with rc:-%s\n",
619 SAM("-ENOSPC=usb_submit_urb() for %i urbs\n", nospc);
620 SAM("..... possibly inadequate USB bandwidth\n");
621 peasycap->video_eof = 1;
625 JOM(4, "attempting cleanup instead of submitting\n");
626 list_for_each(plist_head, (peasycap->purb_video_head)) {
627 pdata_urb = list_entry(plist_head,
628 struct data_urb, list_head);
630 purb = pdata_urb->purb;
635 peasycap->video_isoc_streaming = 0;
637 peasycap->video_isoc_streaming = 1;
638 JOM(4, "submitted %i video urbs\n", m);
641 JOM(4, "already streaming video urbs\n");
645 /*****************************************************************************/
646 int kill_video_urbs(struct easycap *peasycap)
649 struct list_head *plist_head;
650 struct data_urb *pdata_urb;
653 SAY("ERROR: peasycap is NULL\n");
656 if (!peasycap->video_isoc_streaming) {
657 JOM(8, "%i=video_isoc_streaming, no video urbs killed\n",
658 peasycap->video_isoc_streaming);
661 if (!peasycap->purb_video_head) {
662 SAM("ERROR: peasycap->purb_video_head is NULL\n");
666 peasycap->video_isoc_streaming = 0;
667 JOM(4, "killing video urbs\n");
669 list_for_each(plist_head, (peasycap->purb_video_head)) {
670 pdata_urb = list_entry(plist_head, struct data_urb, list_head);
671 if (pdata_urb && pdata_urb->purb) {
672 usb_kill_urb(pdata_urb->purb);
676 JOM(4, "%i video urbs killed\n", m);
680 /****************************************************************************/
681 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
682 /*--------------------------------------------------------------------------*/
683 static int easycap_open_noinode(struct file *file)
685 return easycap_open(NULL, file);
688 static int videodev_release(struct video_device *pvideo_device)
690 struct easycap *peasycap;
692 peasycap = video_get_drvdata(pvideo_device);
694 SAY("ERROR: peasycap is NULL\n");
695 SAY("ending unsuccessfully\n");
698 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
699 SAY("ERROR: bad peasycap: %p\n", peasycap);
702 if (0 != kill_video_urbs(peasycap)) {
703 SAM("ERROR: kill_video_urbs() failed\n");
706 JOM(4, "ending successfully\n");
709 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
710 /*****************************************************************************/
711 /*--------------------------------------------------------------------------*/
713 * THIS FUNCTION IS CALLED FROM WITHIN easycap_usb_disconnect() AND IS
714 * PROTECTED BY SEMAPHORES SET AND CLEARED BY easycap_usb_disconnect().
716 * BY THIS STAGE THE DEVICE HAS ALREADY BEEN PHYSICALLY UNPLUGGED, SO
717 * peasycap->pusb_device IS NO LONGER VALID.
719 /*---------------------------------------------------------------------------*/
720 static void easycap_delete(struct kref *pkref)
722 struct easycap *peasycap;
723 struct data_urb *pdata_urb;
724 struct list_head *plist_head, *plist_next;
726 int allocation_video_urb;
727 int allocation_video_page;
728 int allocation_video_struct;
729 int allocation_audio_urb;
730 int allocation_audio_page;
731 int allocation_audio_struct;
732 int registered_video, registered_audio;
734 peasycap = container_of(pkref, struct easycap, kref);
736 SAM("ERROR: peasycap is NULL: cannot perform deletions\n");
739 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
740 SAY("ERROR: bad peasycap: %p\n", peasycap);
743 kd = isdongle(peasycap);
744 /*---------------------------------------------------------------------------*/
748 /*---------------------------------------------------------------------------*/
749 if (peasycap->purb_video_head) {
750 JOM(4, "freeing video urbs\n");
752 list_for_each(plist_head, (peasycap->purb_video_head)) {
753 pdata_urb = list_entry(plist_head,
754 struct data_urb, list_head);
756 JOM(4, "ERROR: pdata_urb is NULL\n");
758 if (pdata_urb->purb) {
759 usb_free_urb(pdata_urb->purb);
760 pdata_urb->purb = NULL;
761 peasycap->allocation_video_urb -= 1;
767 JOM(4, "%i video urbs freed\n", m);
768 /*---------------------------------------------------------------------------*/
769 JOM(4, "freeing video data_urb structures.\n");
771 list_for_each_safe(plist_head, plist_next,
772 peasycap->purb_video_head) {
773 pdata_urb = list_entry(plist_head,
774 struct data_urb, list_head);
776 peasycap->allocation_video_struct -=
777 sizeof(struct data_urb);
783 JOM(4, "%i video data_urb structures freed\n", m);
784 JOM(4, "setting peasycap->purb_video_head=NULL\n");
785 peasycap->purb_video_head = NULL;
787 /*---------------------------------------------------------------------------*/
788 JOM(4, "freeing video isoc buffers.\n");
790 for (k = 0; k < VIDEO_ISOC_BUFFER_MANY; k++) {
791 if (peasycap->video_isoc_buffer[k].pgo) {
792 free_pages((unsigned long)
793 peasycap->video_isoc_buffer[k].pgo,
795 peasycap->video_isoc_buffer[k].pgo = NULL;
796 peasycap->allocation_video_page -=
797 BIT(VIDEO_ISOC_ORDER);
801 JOM(4, "isoc video buffers freed: %i pages\n",
802 m * (0x01 << VIDEO_ISOC_ORDER));
803 /*---------------------------------------------------------------------------*/
804 JOM(4, "freeing video field buffers.\n");
806 for (k = 0; k < FIELD_BUFFER_MANY; k++) {
807 for (m = 0; m < FIELD_BUFFER_SIZE/PAGE_SIZE; m++) {
808 if (peasycap->field_buffer[k][m].pgo) {
809 free_page((unsigned long)
810 peasycap->field_buffer[k][m].pgo);
811 peasycap->field_buffer[k][m].pgo = NULL;
812 peasycap->allocation_video_page -= 1;
817 JOM(4, "video field buffers freed: %i pages\n", gone);
818 /*---------------------------------------------------------------------------*/
819 JOM(4, "freeing video frame buffers.\n");
821 for (k = 0; k < FRAME_BUFFER_MANY; k++) {
822 for (m = 0; m < FRAME_BUFFER_SIZE/PAGE_SIZE; m++) {
823 if (peasycap->frame_buffer[k][m].pgo) {
824 free_page((unsigned long)
825 peasycap->frame_buffer[k][m].pgo);
826 peasycap->frame_buffer[k][m].pgo = NULL;
827 peasycap->allocation_video_page -= 1;
832 JOM(4, "video frame buffers freed: %i pages\n", gone);
833 /*---------------------------------------------------------------------------*/
837 /*---------------------------------------------------------------------------*/
838 if (peasycap->purb_audio_head) {
839 JOM(4, "freeing audio urbs\n");
841 list_for_each(plist_head, (peasycap->purb_audio_head)) {
842 pdata_urb = list_entry(plist_head,
843 struct data_urb, list_head);
845 JOM(4, "ERROR: pdata_urb is NULL\n");
847 if (pdata_urb->purb) {
848 usb_free_urb(pdata_urb->purb);
849 pdata_urb->purb = NULL;
850 peasycap->allocation_audio_urb -= 1;
855 JOM(4, "%i audio urbs freed\n", m);
856 /*---------------------------------------------------------------------------*/
857 JOM(4, "freeing audio data_urb structures.\n");
859 list_for_each_safe(plist_head, plist_next,
860 peasycap->purb_audio_head) {
861 pdata_urb = list_entry(plist_head,
862 struct data_urb, list_head);
864 peasycap->allocation_audio_struct -=
865 sizeof(struct data_urb);
871 JOM(4, "%i audio data_urb structures freed\n", m);
872 JOM(4, "setting peasycap->purb_audio_head=NULL\n");
873 peasycap->purb_audio_head = NULL;
875 /*---------------------------------------------------------------------------*/
876 JOM(4, "freeing audio isoc buffers.\n");
878 for (k = 0; k < AUDIO_ISOC_BUFFER_MANY; k++) {
879 if (peasycap->audio_isoc_buffer[k].pgo) {
880 free_pages((unsigned long)
881 (peasycap->audio_isoc_buffer[k].pgo),
883 peasycap->audio_isoc_buffer[k].pgo = NULL;
884 peasycap->allocation_audio_page -=
885 BIT(AUDIO_ISOC_ORDER);
889 JOM(4, "easyoss_delete(): isoc audio buffers freed: %i pages\n",
890 m * (0x01 << AUDIO_ISOC_ORDER));
891 /*---------------------------------------------------------------------------*/
892 #ifdef CONFIG_EASYCAP_OSS
893 JOM(4, "freeing audio buffers.\n");
895 for (k = 0; k < peasycap->audio_buffer_page_many; k++) {
896 if (peasycap->audio_buffer[k].pgo) {
897 free_page((unsigned long)peasycap->audio_buffer[k].pgo);
898 peasycap->audio_buffer[k].pgo = NULL;
899 peasycap->allocation_audio_page -= 1;
903 JOM(4, "easyoss_delete(): audio buffers freed: %i pages\n", gone);
904 #endif /* CONFIG_EASYCAP_OSS */
905 /*---------------------------------------------------------------------------*/
906 JOM(4, "freeing easycap structure.\n");
907 allocation_video_urb = peasycap->allocation_video_urb;
908 allocation_video_page = peasycap->allocation_video_page;
909 allocation_video_struct = peasycap->allocation_video_struct;
910 registered_video = peasycap->registered_video;
911 allocation_audio_urb = peasycap->allocation_audio_urb;
912 allocation_audio_page = peasycap->allocation_audio_page;
913 allocation_audio_struct = peasycap->allocation_audio_struct;
914 registered_audio = peasycap->registered_audio;
918 if (0 <= kd && DONGLE_MANY > kd) {
919 if (mutex_lock_interruptible(&mutex_dongle)) {
920 SAY("ERROR: cannot down mutex_dongle\n");
922 JOM(4, "locked mutex_dongle\n");
923 easycapdc60_dongle[kd].peasycap = NULL;
924 mutex_unlock(&mutex_dongle);
925 JOM(4, "unlocked mutex_dongle\n");
926 JOT(4, " null-->dongle[%i].peasycap\n", kd);
927 allocation_video_struct -= sizeof(struct easycap);
930 SAY("ERROR: cannot purge dongle[].peasycap");
932 /*---------------------------------------------------------------------------*/
933 SAY("%8i=video urbs after all deletions\n", allocation_video_urb);
934 SAY("%8i=video pages after all deletions\n", allocation_video_page);
935 SAY("%8i=video structs after all deletions\n", allocation_video_struct);
936 SAY("%8i=video devices after all deletions\n", registered_video);
937 SAY("%8i=audio urbs after all deletions\n", allocation_audio_urb);
938 SAY("%8i=audio pages after all deletions\n", allocation_audio_page);
939 SAY("%8i=audio structs after all deletions\n", allocation_audio_struct);
940 SAY("%8i=audio devices after all deletions\n", registered_audio);
945 /*****************************************************************************/
946 static unsigned int easycap_poll(struct file *file, poll_table *wait)
948 struct easycap *peasycap;
953 if (NULL == ((poll_table *)wait))
954 JOT(8, "WARNING: poll table pointer is NULL ... continuing\n");
956 SAY("ERROR: file pointer is NULL\n");
959 peasycap = file->private_data;
961 SAY("ERROR: peasycap is NULL\n");
964 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
965 SAY("ERROR: bad peasycap: %p\n", peasycap);
968 if (!peasycap->pusb_device) {
969 SAY("ERROR: peasycap->pusb_device is NULL\n");
972 /*---------------------------------------------------------------------------*/
973 kd = isdongle(peasycap);
974 if (0 <= kd && DONGLE_MANY > kd) {
975 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].mutex_video)) {
976 SAY("ERROR: cannot down dongle[%i].mutex_video\n", kd);
979 JOM(4, "locked dongle[%i].mutex_video\n", kd);
981 * MEANWHILE, easycap_usb_disconnect() MAY HAVE FREED POINTER
982 * peasycap, IN WHICH CASE A REPEAT CALL TO isdongle() WILL FAIL.
983 * IF NECESSARY, BAIL OUT.
985 if (kd != isdongle(peasycap))
988 SAY("ERROR: file is NULL\n");
989 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
992 peasycap = file->private_data;
994 SAY("ERROR: peasycap is NULL\n");
995 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
998 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
999 SAY("ERROR: bad peasycap: %p\n", peasycap);
1000 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
1001 return -ERESTARTSYS;
1003 if (!peasycap->pusb_device) {
1004 SAM("ERROR: peasycap->pusb_device is NULL\n");
1005 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
1006 return -ERESTARTSYS;
1010 * IF easycap_usb_disconnect() HAS ALREADY FREED POINTER peasycap
1011 * BEFORE THE ATTEMPT TO ACQUIRE THE SEMAPHORE, isdongle() WILL
1012 * HAVE FAILED. BAIL OUT.
1014 return -ERESTARTSYS;
1015 /*---------------------------------------------------------------------------*/
1016 rc = easycap_dqbuf(peasycap, 0);
1017 peasycap->polled = 1;
1018 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
1020 return POLLIN | POLLRDNORM;
1024 /*****************************************************************************/
1025 /*---------------------------------------------------------------------------*/
1027 * IF mode IS NONZERO THIS ROUTINE RETURNS -EAGAIN RATHER THAN BLOCKING.
1029 /*---------------------------------------------------------------------------*/
1030 int easycap_dqbuf(struct easycap *peasycap, int mode)
1032 int input, ifield, miss, rc;
1036 SAY("ERROR: peasycap is NULL\n");
1039 if (!peasycap->pusb_device) {
1040 SAY("ERROR: peasycap->pusb_device is NULL\n");
1044 JOM(8, "%i=ifield\n", ifield);
1045 /*---------------------------------------------------------------------------*/
1047 * CHECK FOR LOST INPUT SIGNAL.
1049 * FOR THE FOUR-CVBS EasyCAP, THIS DOES NOT WORK AS EXPECTED.
1050 * IF INPUT 0 IS PRESENT AND SYNC ACQUIRED, UNPLUGGING INPUT 4 DOES NOT
1051 * RESULT IN SETTING BIT 0x40 ON REGISTER 0x1F, PRESUMABLY BECAUSE THERE
1052 * IS FLYWHEELING ON INPUT 0. THE UPSHOT IS:
1054 * INPUT 0 PLUGGED, INPUT 4 PLUGGED => SCREEN 0 OK, SCREEN 4 OK
1055 * INPUT 0 PLUGGED, INPUT 4 UNPLUGGED => SCREEN 0 OK, SCREEN 4 BLACK
1056 * INPUT 0 UNPLUGGED, INPUT 4 PLUGGED => SCREEN 0 BARS, SCREEN 4 OK
1057 * INPUT 0 UNPLUGGED, INPUT 4 UNPLUGGED => SCREEN 0 BARS, SCREEN 4 BARS
1059 /*---------------------------------------------------------------------------*/
1060 input = peasycap->input;
1061 if (0 <= input && INPUT_MANY > input) {
1062 rc = read_saa(peasycap->pusb_device, 0x1F);
1065 peasycap->lost[input] += 1;
1067 peasycap->lost[input] -= 2;
1069 if (0 > peasycap->lost[input])
1070 peasycap->lost[input] = 0;
1071 else if ((2 * VIDEO_LOST_TOLERATE) < peasycap->lost[input])
1072 peasycap->lost[input] = (2 * VIDEO_LOST_TOLERATE);
1075 /*---------------------------------------------------------------------------*/
1077 * WAIT FOR FIELD ifield (0 => TOP, 1 => BOTTOM)
1079 /*---------------------------------------------------------------------------*/
1081 while ((peasycap->field_read == peasycap->field_fill) ||
1082 (0 != (0xFF00 & peasycap->field_buffer
1083 [peasycap->field_read][0].kount)) ||
1084 (ifield != (0x00FF & peasycap->field_buffer
1085 [peasycap->field_read][0].kount))) {
1089 JOM(8, "first wait on wq_video, %i=field_read %i=field_fill\n",
1090 peasycap->field_read, peasycap->field_fill);
1092 if (0 != (wait_event_interruptible(peasycap->wq_video,
1093 (peasycap->video_idle || peasycap->video_eof ||
1094 ((peasycap->field_read != peasycap->field_fill) &&
1095 (0 == (0xFF00 & peasycap->field_buffer[peasycap->field_read][0].kount)) &&
1096 (ifield == (0x00FF & peasycap->field_buffer[peasycap->field_read][0].kount))))))) {
1097 SAM("aborted by signal\n");
1100 if (peasycap->video_idle) {
1101 JOM(8, "%i=peasycap->video_idle returning -EAGAIN\n",
1102 peasycap->video_idle);
1105 if (peasycap->video_eof) {
1106 JOM(8, "%i=peasycap->video_eof\n", peasycap->video_eof);
1107 #if defined(PERSEVERE)
1108 if (1 == peasycap->status) {
1109 JOM(8, "persevering ...\n");
1110 peasycap->video_eof = 0;
1111 peasycap->audio_eof = 0;
1112 if (0 != reset(peasycap)) {
1113 JOM(8, " ... failed returning -EIO\n");
1114 peasycap->video_eof = 1;
1115 peasycap->audio_eof = 1;
1116 kill_video_urbs(peasycap);
1119 peasycap->status = 0;
1120 JOM(8, " ... OK returning -EAGAIN\n");
1123 #endif /*PERSEVERE*/
1124 peasycap->video_eof = 1;
1125 peasycap->audio_eof = 1;
1126 kill_video_urbs(peasycap);
1127 JOM(8, "returning -EIO\n");
1132 JOM(8, "first awakening on wq_video after %i waits\n", miss);
1134 rc = field2frame(peasycap);
1136 SAM("ERROR: field2frame() rc = %i\n", rc);
1137 /*---------------------------------------------------------------------------*/
1139 * WAIT FOR THE OTHER FIELD
1141 /*---------------------------------------------------------------------------*/
1147 while ((peasycap->field_read == peasycap->field_fill) ||
1148 (0 != (0xFF00 & peasycap->field_buffer[peasycap->field_read][0].kount)) ||
1149 (ifield != (0x00FF & peasycap->field_buffer[peasycap->field_read][0].kount))) {
1153 JOM(8, "second wait on wq_video %i=field_read %i=field_fill\n",
1154 peasycap->field_read, peasycap->field_fill);
1155 if (0 != (wait_event_interruptible(peasycap->wq_video,
1156 (peasycap->video_idle || peasycap->video_eof ||
1157 ((peasycap->field_read != peasycap->field_fill) &&
1158 (0 == (0xFF00 & peasycap->field_buffer[peasycap->field_read][0].kount)) &&
1159 (ifield == (0x00FF & peasycap->field_buffer[peasycap->field_read][0].kount))))))) {
1160 SAM("aborted by signal\n");
1163 if (peasycap->video_idle) {
1164 JOM(8, "%i=peasycap->video_idle returning -EAGAIN\n",
1165 peasycap->video_idle);
1168 if (peasycap->video_eof) {
1169 JOM(8, "%i=peasycap->video_eof\n", peasycap->video_eof);
1170 #if defined(PERSEVERE)
1171 if (1 == peasycap->status) {
1172 JOM(8, "persevering ...\n");
1173 peasycap->video_eof = 0;
1174 peasycap->audio_eof = 0;
1175 if (0 != reset(peasycap)) {
1176 JOM(8, " ... failed returning -EIO\n");
1177 peasycap->video_eof = 1;
1178 peasycap->audio_eof = 1;
1179 kill_video_urbs(peasycap);
1182 peasycap->status = 0;
1183 JOM(8, " ... OK ... returning -EAGAIN\n");
1186 #endif /*PERSEVERE*/
1187 peasycap->video_eof = 1;
1188 peasycap->audio_eof = 1;
1189 kill_video_urbs(peasycap);
1190 JOM(8, "returning -EIO\n");
1195 JOM(8, "second awakening on wq_video after %i waits\n", miss);
1197 rc = field2frame(peasycap);
1199 SAM("ERROR: field2frame() rc = %i\n", rc);
1200 /*---------------------------------------------------------------------------*/
1204 /*---------------------------------------------------------------------------*/
1205 if (peasycap->skip) {
1206 peasycap->skipped++;
1207 if (peasycap->skip != peasycap->skipped)
1208 return peasycap->skip - peasycap->skipped;
1210 peasycap->skipped = 0;
1212 /*---------------------------------------------------------------------------*/
1213 peasycap->frame_read = peasycap->frame_fill;
1214 peasycap->queued[peasycap->frame_read] = 0;
1215 peasycap->done[peasycap->frame_read] = V4L2_BUF_FLAG_DONE;
1217 peasycap->frame_fill++;
1218 if (peasycap->frame_buffer_many <= peasycap->frame_fill)
1219 peasycap->frame_fill = 0;
1221 if (0x01 & easycap_standard[peasycap->standard_offset].mask)
1222 peasycap->frame_buffer[peasycap->frame_read][0].kount =
1225 peasycap->frame_buffer[peasycap->frame_read][0].kount =
1229 JOM(8, "setting: %i=peasycap->frame_read\n", peasycap->frame_read);
1230 JOM(8, "bumped to: %i=peasycap->frame_fill\n", peasycap->frame_fill);
1234 /*****************************************************************************/
1235 /*---------------------------------------------------------------------------*/
1237 * BY DEFINITION, odd IS true FOR THE FIELD OCCUPYING LINES 1,3,5,...,479
1238 * odd IS false FOR THE FIELD OCCUPYING LINES 0,2,4,...,478
1240 * WHEN BOOLEAN PARAMETER decimatepixel IS true, ONLY THE FIELD FOR WHICH
1241 * odd==false IS TRANSFERRED TO THE FRAME BUFFER.
1243 * THE BOOLEAN PARAMETER offerfields IS true ONLY WHEN THE USER PROGRAM
1244 * CHOOSES THE OPTION V4L2_FIELD_INTERLACED.
1246 /*---------------------------------------------------------------------------*/
1248 field2frame(struct easycap *peasycap)
1250 struct timeval timeval;
1251 long long int above, below;
1253 struct signed_div_result sdr;
1256 int kex, kad, mex, mad, rex, rad, rad2;
1257 int c2, c3, w2, w3, cz, wz;
1258 int rc, bytesperpixel, multiplier;
1259 int much, more, over, rump, caches, input;
1261 bool odd, isuy, decimatepixel, offerfields, badinput;
1264 SAY("ERROR: peasycap is NULL\n");
1269 input = 0x07 & peasycap->field_buffer[peasycap->field_read][0].input;
1271 JOM(8, "===== parity %i, input 0x%02X, field buffer %i --> "
1272 "frame buffer %i\n",
1273 peasycap->field_buffer[peasycap->field_read][0].kount,
1274 peasycap->field_buffer[peasycap->field_read][0].input,
1275 peasycap->field_read, peasycap->frame_fill);
1276 JOM(8, "===== %i=bytesperpixel\n", peasycap->bytesperpixel);
1277 if (peasycap->offerfields)
1278 JOM(8, "===== offerfields\n");
1280 /*---------------------------------------------------------------------------*/
1282 * REJECT OR CLEAN BAD FIELDS
1284 /*---------------------------------------------------------------------------*/
1285 if (peasycap->field_read == peasycap->field_fill) {
1286 SAM("ERROR: on entry, still filling field buffer %i\n",
1287 peasycap->field_read);
1290 #ifdef EASYCAP_TESTCARD
1291 easycap_testcard(peasycap, peasycap->field_read);
1293 if (0 <= input && INPUT_MANY > input) {
1294 if (easycap_bars && VIDEO_LOST_TOLERATE <= peasycap->lost[input])
1295 easycap_testcard(peasycap, peasycap->field_read);
1297 #endif /*EASYCAP_TESTCARD*/
1298 /*---------------------------------------------------------------------------*/
1300 offerfields = peasycap->offerfields;
1301 bytesperpixel = peasycap->bytesperpixel;
1302 decimatepixel = peasycap->decimatepixel;
1304 if ((2 != bytesperpixel) &&
1305 (3 != bytesperpixel) &&
1306 (4 != bytesperpixel)) {
1307 SAM("MISTAKE: %i=bytesperpixel\n", bytesperpixel);
1315 w2 = 2 * multiplier * (peasycap->width);
1316 w3 = bytesperpixel * multiplier * (peasycap->width);
1317 wz = multiplier * (peasycap->height) *
1318 multiplier * (peasycap->width);
1320 kex = peasycap->field_read; mex = 0;
1321 kad = peasycap->frame_fill; mad = 0;
1323 pex = peasycap->field_buffer[kex][0].pgo; rex = PAGE_SIZE;
1324 pad = peasycap->frame_buffer[kad][0].pgo; rad = PAGE_SIZE;
1325 odd = !!(peasycap->field_buffer[kex][0].kount);
1327 if (odd && (!decimatepixel)) {
1328 JOM(8, "initial skipping %4i bytes p.%4i\n",
1329 w3/multiplier, mad);
1330 pad += (w3 / multiplier); rad -= (w3 / multiplier);
1333 mask = 0; rump = 0; caches = 0;
1338 * PROCESS ONE LINE OF FRAME AT FULL RESOLUTION:
1339 * READ w2 BYTES FROM FIELD BUFFER,
1340 * WRITE w3 BYTES TO FRAME BUFFER
1342 if (!decimatepixel) {
1345 much = over; more = 0;
1346 margin = 0; mask = 0x00;
1352 SAM("MISTAKE: much is odd\n");
1356 more = (bytesperpixel *
1358 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1359 if (1 < bytesperpixel) {
1360 if (rad * 2 < much * bytesperpixel) {
1362 * INJUDICIOUS ALTERATION OF
1363 * THIS STATEMENT BLOCK WILL
1364 * CAUSE BREAKAGE. BEWARE.
1366 rad2 = rad + bytesperpixel - 1;
1367 much = ((((2 * rad2)/bytesperpixel)/2) * 2);
1368 rump = ((bytesperpixel * much) / 2) - rad;
1375 if ((mex + 1) < FIELD_BUFFER_SIZE / PAGE_SIZE)
1376 margin = *((u8 *)(peasycap->field_buffer[kex][mex + 1].pgo));
1381 SAM("MISTAKE: %i=bytesperpixel\n",
1388 JOM(8, "ERROR: 0x%02X=->field_buffer"
1390 "0x%02X=(0x08|->input)\n",
1391 peasycap->field_buffer
1392 [kex][mex].input, kex, mex,
1393 (0x08|peasycap->input));
1395 rc = redaub(peasycap, pad, pex, much, more,
1396 mask, margin, isuy);
1398 SAM("ERROR: redaub() failed\n");
1404 over -= much; cz += much;
1405 pex += much; rex -= much;
1408 pex = peasycap->field_buffer[kex][mex].pgo;
1410 if (peasycap->field_buffer[kex][mex].input != (0x08|peasycap->input))
1417 pad = peasycap->frame_buffer[kad][mad].pgo;
1425 /*---------------------------------------------------------------------------*/
1427 * SKIP w3 BYTES IN TARGET FRAME BUFFER,
1428 * UNLESS IT IS THE LAST LINE OF AN ODD FRAME
1430 /*---------------------------------------------------------------------------*/
1431 if (!odd || (cz != wz)) {
1436 pad = peasycap->frame_buffer
1448 /*---------------------------------------------------------------------------*/
1450 * PROCESS ONE LINE OF FRAME AT REDUCED RESOLUTION:
1451 * ONLY IF false==odd,
1452 * READ w2 BYTES FROM FIELD BUFFER,
1453 * WRITE w3 / 2 BYTES TO FRAME BUFFER
1455 /*---------------------------------------------------------------------------*/
1459 much = over; more = 0; margin = 0; mask = 0x00;
1465 SAM("MISTAKE: much is odd\n");
1469 more = (bytesperpixel * much) / 4;
1470 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1471 if (1 < bytesperpixel) {
1472 if (rad * 4 < much * bytesperpixel) {
1474 * INJUDICIOUS ALTERATION OF
1475 * THIS STATEMENT BLOCK
1476 * WILL CAUSE BREAKAGE.
1479 rad2 = rad + bytesperpixel - 1;
1480 much = ((((2 * rad2) / bytesperpixel) / 2) * 4);
1481 rump = ((bytesperpixel * much) / 4) - rad;
1488 if ((mex + 1) < FIELD_BUFFER_SIZE / PAGE_SIZE)
1489 margin = *((u8 *)(peasycap->field_buffer[kex][mex + 1].pgo));
1493 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1495 SAM("MISTAKE: %i=bytesperpixel\n",
1499 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1504 JOM(8, "ERROR: 0x%02X=->field_buffer"
1506 "0x%02X=(0x08|->input)\n",
1507 peasycap->field_buffer
1508 [kex][mex].input, kex, mex,
1509 (0x08|peasycap->input));
1511 rc = redaub(peasycap, pad, pex, much, more,
1512 mask, margin, isuy);
1514 SAM("ERROR: redaub() failed\n");
1517 over -= much; cz += much;
1518 pex += much; rex -= much;
1521 pex = peasycap->field_buffer[kex][mex].pgo;
1523 if (peasycap->field_buffer[kex][mex].input !=
1524 (0x08|peasycap->input))
1531 pad = peasycap->frame_buffer[kad][mad].pgo;
1539 /*---------------------------------------------------------------------------*/
1542 * READ w2 BYTES FROM FIELD BUFFER AND DISCARD THEM
1544 /*---------------------------------------------------------------------------*/
1550 pex = peasycap->field_buffer[kex][mex].pgo;
1552 if (peasycap->field_buffer[kex][mex].input !=
1553 (0x08|peasycap->input)) {
1554 JOM(8, "ERROR: 0x%02X=->field_buffer"
1556 "0x%02X=(0x08|->input)\n",
1557 peasycap->field_buffer
1558 [kex][mex].input, kex, mex,
1559 (0x08|peasycap->input));
1573 /*---------------------------------------------------------------------------*/
1577 /*---------------------------------------------------------------------------*/
1578 c2 = (mex + 1)*PAGE_SIZE - rex;
1580 SAM("ERROR: discrepancy %i in bytes read\n", c2 - cz);
1581 c3 = (mad + 1)*PAGE_SIZE - rad;
1583 if (!decimatepixel) {
1584 if (bytesperpixel * cz != c3)
1585 SAM("ERROR: discrepancy %i in bytes written\n",
1586 c3 - (bytesperpixel * cz));
1591 SAM("ERROR: discrepancy %i in bytes written\n",
1592 (2*c3)-(bytesperpixel * cz));
1595 SAM("ERROR: discrepancy %i "
1596 "in bytes written\n", c3);
1600 SAM("WORRY: undischarged cache at end of line in frame buffer\n");
1602 JOM(8, "===== field2frame(): %i bytes --> %i bytes (incl skip)\n", c2, c3);
1603 JOM(8, "===== field2frame(): %i=mad %i=rad\n", mad, rad);
1606 JOM(8, "+++++ field2frame(): frame buffer %i is full\n", kad);
1608 if (peasycap->field_read == peasycap->field_fill)
1609 SAM("WARNING: on exit, filling field buffer %i\n",
1610 peasycap->field_read);
1611 /*---------------------------------------------------------------------------*/
1613 * CALCULATE VIDEO STREAMING RATE
1615 /*---------------------------------------------------------------------------*/
1616 do_gettimeofday(&timeval);
1617 if (peasycap->timeval6.tv_sec) {
1618 below = ((long long int)(1000000)) *
1619 ((long long int)(timeval.tv_sec -
1620 peasycap->timeval6.tv_sec)) +
1621 (long long int)(timeval.tv_usec - peasycap->timeval6.tv_usec);
1622 above = (long long int)1000000;
1624 sdr = signed_div(above, below);
1625 above = sdr.quotient;
1626 remainder = (u32)sdr.remainder;
1628 JOM(8, "video streaming at %3lli.%03i fields per second\n",
1629 above, (remainder/1000));
1631 peasycap->timeval6 = timeval;
1634 JOM(8, "%i=caches\n", caches);
1637 /*****************************************************************************/
1638 struct signed_div_result
1639 signed_div(long long int above, long long int below)
1641 struct signed_div_result sdr;
1643 if (((0 <= above) && (0 <= below)) || ((0 > above) && (0 > below))) {
1644 sdr.remainder = (unsigned long long int) do_div(above, below);
1645 sdr.quotient = (long long int) above;
1651 sdr.remainder = (unsigned long long int) do_div(above, below);
1652 sdr.quotient = -((long long int) above);
1656 /*****************************************************************************/
1657 /*---------------------------------------------------------------------------*/
1659 * DECIMATION AND COLOURSPACE CONVERSION.
1661 * THIS ROUTINE REQUIRES THAT ALL THE DATA TO BE READ RESIDES ON ONE PAGE
1662 * AND THAT ALL THE DATA TO BE WRITTEN RESIDES ON ONE (DIFFERENT) PAGE.
1663 * THE CALLING ROUTINE MUST ENSURE THAT THIS REQUIREMENT IS MET, AND MUST
1664 * ALSO ENSURE THAT much IS EVEN.
1666 * much BYTES ARE READ, AT LEAST (bytesperpixel * much)/2 BYTES ARE WRITTEN
1667 * IF THERE IS NO DECIMATION, HALF THIS AMOUNT IF THERE IS DECIMATION.
1669 * mask IS ZERO WHEN NO SPECIAL BEHAVIOUR REQUIRED. OTHERWISE IT IS SET THUS:
1670 * 0x03 & mask = number of bytes to be written to cache instead of to
1672 * 0x04 & mask => use argument margin to set the chrominance for last pixel
1673 * 0x08 & mask => do not set the chrominance for last pixel
1675 * YUV to RGB CONVERSION IS (OR SHOULD BE) ITU-R BT 601.
1677 * THERE IS A LOT OF CODE REPETITION IN THIS ROUTINE IN ORDER TO AVOID
1678 * INEFFICIENT SWITCHING INSIDE INNER LOOPS. REARRANGING THE LOGIC TO
1679 * REDUCE CODE LENGTH WILL GENERALLY IMPAIR RUNTIME PERFORMANCE. BEWARE.
1681 /*---------------------------------------------------------------------------*/
1683 redaub(struct easycap *peasycap, void *pad, void *pex, int much, int more,
1684 u8 mask, u8 margin, bool isuy)
1686 static s32 ay[256], bu[256], rv[256], gu[256], gv[256];
1688 u8 r, g, b, y, u, v, c, *p2, *p3, *pz, *pr;
1690 bool byteswaporder, decimatepixel, last;
1695 SAM("MISTAKE: much is odd\n");
1698 bytesperpixel = peasycap->bytesperpixel;
1699 byteswaporder = peasycap->byteswaporder;
1700 decimatepixel = peasycap->decimatepixel;
1702 /*---------------------------------------------------------------------------*/
1704 for (j = 0; j < 112; j++) {
1705 tmp = (0xFF00 & (453 * j)) >> 8;
1706 bu[j + 128] = tmp; bu[127 - j] = -tmp;
1707 tmp = (0xFF00 & (359 * j)) >> 8;
1708 rv[j + 128] = tmp; rv[127 - j] = -tmp;
1709 tmp = (0xFF00 & (88 * j)) >> 8;
1710 gu[j + 128] = tmp; gu[127 - j] = -tmp;
1711 tmp = (0xFF00 & (183 * j)) >> 8;
1712 gv[j + 128] = tmp; gv[127 - j] = -tmp;
1714 for (j = 0; j < 16; j++) {
1715 bu[j] = bu[16]; rv[j] = rv[16];
1716 gu[j] = gu[16]; gv[j] = gv[16];
1718 for (j = 240; j < 256; j++) {
1719 bu[j] = bu[239]; rv[j] = rv[239];
1720 gu[j] = gu[239]; gv[j] = gv[239];
1722 for (j = 16; j < 236; j++)
1724 for (j = 0; j < 16; j++)
1726 for (j = 236; j < 256; j++)
1728 JOM(8, "lookup tables are prepared\n");
1730 pcache = peasycap->pcache;
1732 pcache = &peasycap->cache[0];
1733 /*---------------------------------------------------------------------------*/
1735 * TRANSFER CONTENTS OF CACHE TO THE FRAME BUFFER
1737 /*---------------------------------------------------------------------------*/
1739 SAM("MISTAKE: pcache is NULL\n");
1743 if (pcache != &peasycap->cache[0])
1744 JOM(16, "cache has %i bytes\n", (int)(pcache - &peasycap->cache[0]));
1745 p2 = &peasycap->cache[0];
1746 p3 = (u8 *)pad - (int)(pcache - &peasycap->cache[0]);
1747 while (p2 < pcache) {
1750 pcache = &peasycap->cache[0];
1752 SAM("MISTAKE: pointer misalignment\n");
1755 /*---------------------------------------------------------------------------*/
1756 rump = (int)(0x03 & mask);
1758 p2 = (u8 *)pex; pz = p2 + much; pr = p3 + more; last = false;
1767 JOM(16, "%4i=much %4i=more %i=rump\n", much, more, rump);
1769 /*---------------------------------------------------------------------------*/
1770 switch (bytesperpixel) {
1772 if (!decimatepixel) {
1773 memcpy(pad, pex, (size_t)much);
1774 if (!byteswaporder) {
1779 p3 = (u8 *)pad; pz = p3 + much;
1789 if (!byteswaporder) {
1790 /* UYVY DECIMATED */
1791 p2 = (u8 *)pex; p3 = (u8 *)pad; pz = p2 + much;
1794 *(p3 + 1) = *(p2 + 1);
1795 *(p3 + 2) = *(p2 + 2);
1796 *(p3 + 3) = *(p2 + 3);
1801 /* YUYV DECIMATED */
1802 p2 = (u8 *)pex; p3 = (u8 *)pad; pz = p2 + much;
1806 *(p3 + 2) = *(p2 + 3);
1807 *(p3 + 3) = *(p2 + 2);
1817 if (!decimatepixel) {
1818 if (!byteswaporder) {
1821 if (pr <= (p3 + bytesperpixel))
1826 if (last && (0x0C & mask)) {
1842 tmp = ay[(int)y] + rv[(int)v];
1843 r = (255 < tmp) ? 255 : ((0 > tmp) ?
1845 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
1846 g = (255 < tmp) ? 255 : ((0 > tmp) ?
1848 tmp = ay[(int)y] + bu[(int)u];
1849 b = (255 < tmp) ? 255 : ((0 > tmp) ?
1853 pcache = &peasycap->cache[0];
1854 switch (bytesperpixel - rump) {
1868 SAM("MISTAKE: %i=rump\n",
1869 bytesperpixel - rump);
1883 p3 += bytesperpixel;
1889 if (pr <= (p3 + bytesperpixel))
1894 if (last && (0x0C & mask)) {
1911 tmp = ay[(int)y] + rv[(int)v];
1912 r = (255 < tmp) ? 255 : ((0 > tmp) ?
1914 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
1915 g = (255 < tmp) ? 255 : ((0 > tmp) ?
1917 tmp = ay[(int)y] + bu[(int)u];
1918 b = (255 < tmp) ? 255 : ((0 > tmp) ?
1922 pcache = &peasycap->cache[0];
1923 switch (bytesperpixel - rump) {
1937 SAM("MISTAKE: %i=rump\n",
1938 bytesperpixel - rump);
1952 p3 += bytesperpixel;
1957 if (!byteswaporder) {
1960 if (pr <= (p3 + bytesperpixel))
1965 if (last && (0x0C & mask)) {
1982 tmp = ay[(int)y] + rv[(int)v];
1983 r = (255 < tmp) ? 255 : ((0 > tmp) ?
1985 tmp = ay[(int)y] - gu[(int)u] -
1987 g = (255 < tmp) ? 255 : ((0 > tmp) ?
1989 tmp = ay[(int)y] + bu[(int)u];
1990 b = (255 < tmp) ? 255 : ((0 > tmp) ?
1994 pcache = &peasycap->cache[0];
1995 switch (bytesperpixel - rump) {
2011 bytesperpixel - rump);
2021 p3 += bytesperpixel;
2031 if (pr <= (p3 + bytesperpixel))
2036 if (last && (0x0C & mask)) {
2054 tmp = ay[(int)y] + rv[(int)v];
2055 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2057 tmp = ay[(int)y] - gu[(int)u] -
2059 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2061 tmp = ay[(int)y] + bu[(int)u];
2062 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2066 pcache = &peasycap->cache[0];
2067 switch (bytesperpixel - rump) {
2083 bytesperpixel - rump);
2093 p3 += bytesperpixel;
2106 if (!decimatepixel) {
2107 if (!byteswaporder) {
2110 if (pr <= (p3 + bytesperpixel))
2115 if (last && (0x0C & mask)) {
2131 tmp = ay[(int)y] + rv[(int)v];
2132 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2134 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
2135 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2137 tmp = ay[(int)y] + bu[(int)u];
2138 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2142 pcache = &peasycap->cache[0];
2143 switch (bytesperpixel - rump) {
2166 SAM("MISTAKE: %i=rump\n",
2167 bytesperpixel - rump);
2182 p3 += bytesperpixel;
2190 if (pr <= (p3 + bytesperpixel))
2195 if (last && (0x0C & mask)) {
2211 tmp = ay[(int)y] + rv[(int)v];
2212 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2214 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
2215 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2217 tmp = ay[(int)y] + bu[(int)u];
2218 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2222 pcache = &peasycap->cache[0];
2223 switch (bytesperpixel - rump) {
2246 SAM("MISTAKE: %i=rump\n",
2247 bytesperpixel - rump);
2261 p3 += bytesperpixel;
2266 if (!byteswaporder) {
2271 if (pr <= (p3 + bytesperpixel))
2276 if (last && (0x0C & mask)) {
2294 tmp = ay[(int)y] + rv[(int)v];
2295 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2297 tmp = ay[(int)y] - gu[(int)u] -
2299 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2301 tmp = ay[(int)y] + bu[(int)u];
2302 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2306 pcache = &peasycap->cache[0];
2307 switch (bytesperpixel - rump) {
2344 p3 += bytesperpixel;
2355 if (pr <= (p3 + bytesperpixel))
2360 if (last && (0x0C & mask)) {
2377 tmp = ay[(int)y] + rv[(int)v];
2378 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2380 tmp = ay[(int)y] - gu[(int)u] -
2382 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2384 tmp = ay[(int)y] + bu[(int)u];
2385 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2389 pcache = &peasycap->cache[0];
2390 switch (bytesperpixel - rump) {
2415 bytesperpixel - rump);
2426 p3 += bytesperpixel;
2437 SAM("MISTAKE: %i=bytesperpixel\n", bytesperpixel);
2443 /*****************************************************************************/
2445 * SEE CORBET ET AL. "LINUX DEVICE DRIVERS", 3rd EDITION, PAGES 430-434
2447 /*****************************************************************************/
2448 static void easycap_vma_open(struct vm_area_struct *pvma)
2450 struct easycap *peasycap;
2452 peasycap = pvma->vm_private_data;
2454 SAY("ERROR: peasycap is NULL\n");
2457 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
2458 SAY("ERROR: bad peasycap: %p\n", peasycap);
2461 peasycap->vma_many++;
2462 JOT(8, "%i=peasycap->vma_many\n", peasycap->vma_many);
2465 /*****************************************************************************/
2466 static void easycap_vma_close(struct vm_area_struct *pvma)
2468 struct easycap *peasycap;
2470 peasycap = pvma->vm_private_data;
2472 SAY("ERROR: peasycap is NULL\n");
2475 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
2476 SAY("ERROR: bad peasycap: %p\n", peasycap);
2479 peasycap->vma_many--;
2480 JOT(8, "%i=peasycap->vma_many\n", peasycap->vma_many);
2483 /*****************************************************************************/
2484 static int easycap_vma_fault(struct vm_area_struct *pvma, struct vm_fault *pvmf)
2489 struct easycap *peasycap;
2491 retcode = VM_FAULT_NOPAGE;
2494 SAY("pvma is NULL\n");
2498 SAY("pvmf is NULL\n");
2502 k = (pvmf->pgoff) / (FRAME_BUFFER_SIZE/PAGE_SIZE);
2503 m = (pvmf->pgoff) % (FRAME_BUFFER_SIZE/PAGE_SIZE);
2506 JOT(4, "%4i=k, %4i=m\n", k, m);
2508 JOT(16, "%4i=k, %4i=m\n", k, m);
2510 if ((0 > k) || (FRAME_BUFFER_MANY <= k)) {
2511 SAY("ERROR: buffer index %i out of range\n", k);
2514 if ((0 > m) || (FRAME_BUFFER_SIZE/PAGE_SIZE <= m)) {
2515 SAY("ERROR: page number %i out of range\n", m);
2518 peasycap = pvma->vm_private_data;
2520 SAY("ERROR: peasycap is NULL\n");
2523 /*---------------------------------------------------------------------------*/
2524 pbuf = peasycap->frame_buffer[k][m].pgo;
2526 SAM("ERROR: pbuf is NULL\n");
2529 page = virt_to_page(pbuf);
2531 SAM("ERROR: page is NULL\n");
2535 /*---------------------------------------------------------------------------*/
2537 SAM("ERROR: page is NULL after get_page(page)\n");
2540 retcode = VM_FAULT_MINOR;
2545 static const struct vm_operations_struct easycap_vm_ops = {
2546 .open = easycap_vma_open,
2547 .close = easycap_vma_close,
2548 .fault = easycap_vma_fault,
2551 static int easycap_mmap(struct file *file, struct vm_area_struct *pvma)
2555 pvma->vm_ops = &easycap_vm_ops;
2556 pvma->vm_flags |= VM_RESERVED;
2558 pvma->vm_private_data = file->private_data;
2559 easycap_vma_open(pvma);
2562 /*****************************************************************************/
2563 /*---------------------------------------------------------------------------*/
2565 * ON COMPLETION OF A VIDEO URB ITS DATA IS COPIED TO THE FIELD BUFFERS
2566 * PROVIDED peasycap->video_idle IS ZERO. REGARDLESS OF THIS BEING TRUE,
2567 * IT IS RESUBMITTED PROVIDED peasycap->video_isoc_streaming IS NOT ZERO.
2569 * THIS FUNCTION IS AN INTERRUPT SERVICE ROUTINE AND MUST NOT SLEEP.
2571 * INFORMATION ABOUT THE VALIDITY OF THE CONTENTS OF THE FIELD BUFFER ARE
2572 * STORED IN THE TWO-BYTE STATUS PARAMETER
2573 * peasycap->field_buffer[peasycap->field_fill][0].kount
2574 * NOTICE THAT THE INFORMATION IS STORED ONLY WITH PAGE 0 OF THE FIELD BUFFER.
2576 * THE LOWER BYTE CONTAINS THE FIELD PARITY BYTE FURNISHED BY THE SAA7113H
2579 * THE UPPER BYTE IS ZERO IF NO PROBLEMS, OTHERWISE:
2580 * 0 != (kount & 0x8000) => AT LEAST ONE URB COMPLETED WITH ERRORS
2581 * 0 != (kount & 0x4000) => BUFFER HAS TOO MUCH DATA
2582 * 0 != (kount & 0x2000) => BUFFER HAS NOT ENOUGH DATA
2583 * 0 != (kount & 0x1000) => BUFFER HAS DATA FROM DISPARATE INPUTS
2584 * 0 != (kount & 0x0400) => RESERVED
2585 * 0 != (kount & 0x0200) => FIELD BUFFER NOT YET CHECKED
2586 * 0 != (kount & 0x0100) => BUFFER HAS TWO EXTRA BYTES - WHY?
2588 /*---------------------------------------------------------------------------*/
2589 static void easycap_complete(struct urb *purb)
2591 struct easycap *peasycap;
2592 struct data_buffer *pfield_buffer;
2594 int i, more, much, leap, rc, last;
2595 int videofieldamount;
2596 unsigned int override, bad;
2597 int framestatus, framelength, frameactual, frameoffset;
2601 SAY("ERROR: easycap_complete(): purb is NULL\n");
2604 peasycap = purb->context;
2606 SAY("ERROR: easycap_complete(): peasycap is NULL\n");
2609 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
2610 SAY("ERROR: bad peasycap: %p\n", peasycap);
2613 if (peasycap->video_eof)
2615 for (i = 0; i < VIDEO_ISOC_BUFFER_MANY; i++)
2616 if (purb->transfer_buffer == peasycap->video_isoc_buffer[i].pgo)
2618 JOM(16, "%2i=urb\n", i);
2619 last = peasycap->video_isoc_sequence;
2620 if ((((VIDEO_ISOC_BUFFER_MANY - 1) == last) && (0 != i)) ||
2621 (((VIDEO_ISOC_BUFFER_MANY - 1) != last) && ((last + 1) != i))) {
2622 JOM(16, "ERROR: out-of-order urbs %i,%i ... continuing\n",
2625 peasycap->video_isoc_sequence = i;
2627 if (peasycap->video_idle) {
2628 JOM(16, "%i=video_idle %i=video_isoc_streaming\n",
2629 peasycap->video_idle, peasycap->video_isoc_streaming);
2630 if (peasycap->video_isoc_streaming) {
2631 rc = usb_submit_urb(purb, GFP_ATOMIC);
2633 SAM("%s:%d ENOMEM\n", strerror(rc), rc);
2635 SAM("ERROR: while %i=video_idle, "
2637 "failed with rc:\n",
2638 peasycap->video_idle);
2644 /*---------------------------------------------------------------------------*/
2645 if (FIELD_BUFFER_MANY <= peasycap->field_fill) {
2646 SAM("ERROR: bad peasycap->field_fill\n");
2650 if ((-ESHUTDOWN == purb->status) || (-ENOENT == purb->status)) {
2651 JOM(8, "urb status -ESHUTDOWN or -ENOENT\n");
2655 (peasycap->field_buffer[peasycap->field_fill][0].kount) |= 0x8000 ;
2656 SAM("ERROR: bad urb status -%s: %d\n",
2657 strerror(purb->status), purb->status);
2658 /*---------------------------------------------------------------------------*/
2660 for (i = 0; i < purb->number_of_packets; i++) {
2661 if (0 != purb->iso_frame_desc[i].status) {
2662 (peasycap->field_buffer
2663 [peasycap->field_fill][0].kount) |= 0x8000 ;
2664 /* FIXME: 1. missing '-' check boundaries */
2666 strerror(purb->iso_frame_desc[i].status));
2668 framestatus = purb->iso_frame_desc[i].status;
2669 framelength = purb->iso_frame_desc[i].length;
2670 frameactual = purb->iso_frame_desc[i].actual_length;
2671 frameoffset = purb->iso_frame_desc[i].offset;
2673 JOM(16, "frame[%2i]:"
2678 i, framestatus, frameactual, framelength, frameoffset);
2679 if (!purb->iso_frame_desc[i].status) {
2680 more = purb->iso_frame_desc[i].actual_length;
2681 pfield_buffer = &peasycap->field_buffer
2682 [peasycap->field_fill][peasycap->field_page];
2683 videofieldamount = (peasycap->field_page *
2685 (int)(pfield_buffer->pto - pfield_buffer->pgo);
2687 peasycap->video_mt++;
2689 if (peasycap->video_mt) {
2690 JOM(8, "%4i empty video urb frames\n",
2691 peasycap->video_mt);
2692 peasycap->video_mt = 0;
2694 if (FIELD_BUFFER_MANY <= peasycap->field_fill) {
2695 SAM("ERROR: bad peasycap->field_fill\n");
2698 if (FIELD_BUFFER_SIZE/PAGE_SIZE <=
2699 peasycap->field_page) {
2700 SAM("ERROR: bad peasycap->field_page\n");
2703 pfield_buffer = &peasycap->field_buffer
2704 [peasycap->field_fill][peasycap->field_page];
2705 pu = (u8 *)(purb->transfer_buffer +
2706 purb->iso_frame_desc[i].offset);
2711 /*--------------------------------------------------------------------------*/
2713 * EIGHT-BYTE END-OF-VIDEOFIELD MARKER.
2714 * NOTE: A SUCCESSION OF URB FRAMES FOLLOWING THIS ARE EMPTY,
2715 * CORRESPONDING TO THE FIELD FLYBACK (VERTICAL BLANKING) PERIOD.
2717 * PROVIDED THE FIELD BUFFER CONTAINS GOOD DATA AS INDICATED BY A ZERO UPPER
2719 * peasycap->field_buffer[peasycap->field_fill][0].kount
2720 * THE CONTENTS OF THE FIELD BUFFER ARE OFFERED TO dqbuf(), field_read IS
2721 * UPDATED AND field_fill IS BUMPED. IF THE FIELD BUFFER CONTAINS BAD DATA
2722 * NOTHING IS OFFERED TO dqbuf().
2724 * THE DECISION ON WHETHER THE PARITY OF THE OFFERED FIELD BUFFER IS RIGHT
2725 * RESTS WITH dqbuf().
2727 /*---------------------------------------------------------------------------*/
2728 if ((8 == more) || override) {
2729 if (videofieldamount >
2730 peasycap->videofieldamount) {
2731 if (2 == videofieldamount -
2734 (peasycap->field_buffer
2735 [peasycap->field_fill]
2736 [0].kount) |= 0x0100;
2737 peasycap->video_junk += (1 +
2738 VIDEO_JUNK_TOLERATE);
2740 (peasycap->field_buffer
2741 [peasycap->field_fill]
2742 [0].kount) |= 0x4000;
2743 } else if (videofieldamount <
2746 (peasycap->field_buffer
2747 [peasycap->field_fill]
2748 [0].kount) |= 0x2000;
2750 bad = 0xFF00 & peasycap->field_buffer
2751 [peasycap->field_fill]
2754 (peasycap->video_junk)--;
2755 if (-VIDEO_JUNK_TOLERATE >
2756 peasycap->video_junk)
2757 peasycap->video_junk =
2758 -VIDEO_JUNK_TOLERATE;
2759 peasycap->field_read =
2762 if (FIELD_BUFFER_MANY <=
2767 peasycap->field_page = 0;
2768 pfield_buffer = &peasycap->
2774 pfield_buffer->pto =
2776 JOM(8, "bumped to: %i="
2780 peasycap->field_fill,
2782 pfield_buffer->kount);
2783 JOM(8, "field buffer %i has "
2784 "%i bytes fit to be "
2786 peasycap->field_read,
2788 JOM(8, "wakeup call to "
2793 peasycap->field_read,
2794 peasycap->field_fill,
2798 field_read][0].kount);
2799 wake_up_interruptible
2803 (&peasycap->timeval7);
2805 peasycap->video_junk++;
2807 peasycap->video_junk +=
2808 (1 + VIDEO_JUNK_TOLERATE/2);
2809 JOM(8, "field buffer %i had %i "
2810 "bytes, now discarded: "
2812 peasycap->field_fill,
2815 peasycap->field_buffer
2816 [peasycap->field_fill][0].
2818 (peasycap->field_fill)++;
2820 if (FIELD_BUFFER_MANY <=
2821 peasycap->field_fill)
2822 peasycap->field_fill = 0;
2823 peasycap->field_page = 0;
2825 &peasycap->field_buffer
2826 [peasycap->field_fill]
2827 [peasycap->field_page];
2828 pfield_buffer->pto =
2831 JOM(8, "bumped to: %i=peasycap->"
2832 "field_fill %i=parity\n",
2833 peasycap->field_fill,
2834 0x00FF & pfield_buffer->kount);
2837 JOM(8, "end-of-field: received "
2838 "parity byte 0x%02X\n",
2841 pfield_buffer->kount = 0x0000;
2843 pfield_buffer->kount = 0x0001;
2844 pfield_buffer->input = 0x08 |
2845 (0x07 & peasycap->input);
2846 JOM(8, "end-of-field: 0x%02X=kount\n",
2847 0xFF & pfield_buffer->kount);
2850 /*---------------------------------------------------------------------------*/
2852 * COPY more BYTES FROM ISOC BUFFER TO FIELD BUFFER
2854 /*---------------------------------------------------------------------------*/
2858 if (FIELD_BUFFER_MANY <= peasycap->field_fill) {
2859 SAM("ERROR: bad peasycap->field_fill\n");
2862 if (FIELD_BUFFER_SIZE/PAGE_SIZE <= peasycap->field_page) {
2863 SAM("ERROR: bad peasycap->field_page\n");
2866 pfield_buffer = &peasycap->field_buffer
2867 [peasycap->field_fill][peasycap->field_page];
2869 pfield_buffer = &peasycap->field_buffer
2870 [peasycap->field_fill]
2871 [peasycap->field_page];
2872 if (PAGE_SIZE < (pfield_buffer->pto -
2873 pfield_buffer->pgo)) {
2874 SAM("ERROR: bad pfield_buffer->pto\n");
2877 if (PAGE_SIZE == (pfield_buffer->pto -
2878 pfield_buffer->pgo)) {
2879 (peasycap->field_page)++;
2880 if (FIELD_BUFFER_SIZE/PAGE_SIZE <=
2881 peasycap->field_page) {
2882 JOM(16, "wrapping peasycap->"
2884 peasycap->field_page = 0;
2886 pfield_buffer = &peasycap->
2888 [peasycap->field_fill]
2889 [peasycap->field_page];
2890 pfield_buffer->pto = pfield_buffer->pgo;
2891 pfield_buffer->input = 0x08 |
2892 (0x07 & peasycap->input);
2893 if ((peasycap->field_buffer[peasycap->
2896 pfield_buffer->input)
2897 (peasycap->field_buffer
2898 [peasycap->field_fill]
2899 [0]).kount |= 0x1000;
2903 (int)(pfield_buffer->pto -
2904 pfield_buffer->pgo);
2908 memcpy(pfield_buffer->pto, pu, much);
2910 (pfield_buffer->pto) += much;
2917 /*---------------------------------------------------------------------------*/
2919 * RESUBMIT THIS URB, UNLESS A SEVERE PERSISTENT ERROR CONDITION EXISTS.
2921 * IF THE WAIT QUEUES ARE NOT CLEARED IN RESPONSE TO AN ERROR CONDITION
2922 * THE USERSPACE PROGRAM, E.G. mplayer, MAY HANG ON EXIT. BEWARE.
2924 /*---------------------------------------------------------------------------*/
2925 if (VIDEO_ISOC_BUFFER_MANY <= peasycap->video_junk) {
2926 SAM("easycap driver shutting down on condition green\n");
2927 peasycap->status = 1;
2928 peasycap->video_eof = 1;
2929 peasycap->video_junk = 0;
2930 wake_up_interruptible(&peasycap->wq_video);
2931 #if !defined(PERSEVERE)
2932 peasycap->audio_eof = 1;
2933 wake_up_interruptible(&peasycap->wq_audio);
2934 #endif /*PERSEVERE*/
2937 if (peasycap->video_isoc_streaming) {
2938 rc = usb_submit_urb(purb, GFP_ATOMIC);
2940 SAM("%s: %d\n", strerror(rc), rc);
2942 SAM("ERROR: while %i=video_idle, "
2944 "failed with rc:\n",
2945 peasycap->video_idle);
2950 static const struct file_operations easycap_fops = {
2951 .owner = THIS_MODULE,
2952 .open = easycap_open,
2953 .unlocked_ioctl = easycap_unlocked_ioctl,
2954 .poll = easycap_poll,
2955 .mmap = easycap_mmap,
2956 .llseek = no_llseek,
2958 static const struct usb_class_driver easycap_class = {
2959 .name = "usb/easycap%d",
2960 .fops = &easycap_fops,
2961 .minor_base = USB_SKEL_MINOR_BASE,
2963 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
2964 static const struct v4l2_file_operations v4l2_fops = {
2965 .owner = THIS_MODULE,
2966 .open = easycap_open_noinode,
2967 .unlocked_ioctl = easycap_unlocked_ioctl,
2968 .poll = easycap_poll,
2969 .mmap = easycap_mmap,
2971 /*****************************************************************************/
2972 /*---------------------------------------------------------------------------*/
2974 * WHEN THE EasyCAP IS PHYSICALLY PLUGGED IN, THIS FUNCTION IS CALLED THREE
2975 * TIMES, ONCE FOR EACH OF THE THREE INTERFACES. BEWARE.
2977 /*---------------------------------------------------------------------------*/
2978 static int easycap_usb_probe(struct usb_interface *intf,
2979 const struct usb_device_id *id)
2981 struct usb_device *usbdev;
2982 struct usb_host_interface *alt;
2983 struct usb_endpoint_descriptor *ep;
2984 struct usb_interface_descriptor *interface;
2986 struct easycap *peasycap;
2988 struct data_urb *pdata_urb;
2989 size_t wMaxPacketSize;
2990 int ISOCwMaxPacketSize;
2991 int BULKwMaxPacketSize;
2992 int INTwMaxPacketSize;
2993 int CTRLwMaxPacketSize;
2994 u8 bEndpointAddress;
2995 u8 ISOCbEndpointAddress;
2996 u8 INTbEndpointAddress;
2997 int isin, i, j, k, m, rc;
2998 u8 bInterfaceNumber;
3000 u8 bInterfaceSubClass;
3002 int okalt[8], isokalt;
3008 struct easycap_format *peasycap_format;
3010 struct inputset *inputset;
3011 struct v4l2_device *pv4l2_device;
3013 usbdev = interface_to_usbdev(intf);
3015 /*---------------------------------------------------------------------------*/
3016 alt = usb_altnum_to_altsetting(intf, 0);
3018 SAY("ERROR: usb_host_interface not found\n");
3021 interface = &alt->desc;
3023 SAY("ERROR: intf_descriptor is NULL\n");
3026 /*---------------------------------------------------------------------------*/
3028 * GET PROPERTIES OF PROBED INTERFACE
3030 /*---------------------------------------------------------------------------*/
3031 bInterfaceNumber = interface->bInterfaceNumber;
3032 bInterfaceClass = interface->bInterfaceClass;
3033 bInterfaceSubClass = interface->bInterfaceSubClass;
3035 JOT(4, "intf[%i]: num_altsetting=%i\n",
3036 bInterfaceNumber, intf->num_altsetting);
3037 JOT(4, "intf[%i]: cur_altsetting - altsetting=%li\n",
3039 (long int)(intf->cur_altsetting - intf->altsetting));
3040 JOT(4, "intf[%i]: bInterfaceClass=0x%02X bInterfaceSubClass=0x%02X\n",
3041 bInterfaceNumber, bInterfaceClass, bInterfaceSubClass);
3042 /*---------------------------------------------------------------------------*/
3044 * A NEW struct easycap IS ALWAYS ALLOCATED WHEN INTERFACE 0 IS PROBED.
3045 * IT IS NOT POSSIBLE HERE TO FREE ANY EXISTING struct easycap. THIS
3046 * SHOULD HAVE BEEN DONE BY easycap_delete() WHEN THE EasyCAP WAS
3047 * PHYSICALLY UNPLUGGED.
3049 * THE POINTER peasycap TO THE struct easycap IS REMEMBERED WHEN
3050 * INTERFACES 1 AND 2 ARE PROBED.
3052 /*---------------------------------------------------------------------------*/
3053 if (0 == bInterfaceNumber) {
3054 peasycap = kzalloc(sizeof(struct easycap), GFP_KERNEL);
3056 SAY("ERROR: Could not allocate peasycap\n");
3059 /*---------------------------------------------------------------------------*/
3061 * PERFORM URGENT INTIALIZATIONS ...
3063 /*---------------------------------------------------------------------------*/
3064 peasycap->minor = -1;
3065 strcpy(&peasycap->telltale[0], TELLTALE);
3066 kref_init(&peasycap->kref);
3067 JOM(8, "intf[%i]: after kref_init(..._video) "
3068 "%i=peasycap->kref.refcount.counter\n",
3069 bInterfaceNumber, peasycap->kref.refcount.counter);
3072 peasycap->gain = (s8)clamp(easycap_gain, 0, 31);
3074 init_waitqueue_head(&peasycap->wq_video);
3075 init_waitqueue_head(&peasycap->wq_audio);
3076 init_waitqueue_head(&peasycap->wq_trigger);
3078 if (mutex_lock_interruptible(&mutex_dongle)) {
3079 SAY("ERROR: cannot down mutex_dongle\n");
3080 return -ERESTARTSYS;
3082 /*---------------------------------------------------------------------------*/
3084 * FOR INTERFACES 1 AND 2 THE POINTER peasycap WILL NEED TO
3085 * TO BE THE SAME AS THAT ALLOCATED NOW FOR INTERFACE 0.
3087 * NORMALLY ndong WILL NOT HAVE CHANGED SINCE INTERFACE 0 WAS
3088 * PROBED, BUT THIS MAY NOT BE THE CASE IF, FOR EXAMPLE, TWO
3089 * EASYCAPs ARE PLUGGED IN SIMULTANEOUSLY.
3091 /*---------------------------------------------------------------------------*/
3092 for (ndong = 0; ndong < DONGLE_MANY; ndong++) {
3093 if ((!easycapdc60_dongle[ndong].peasycap) &&
3094 (!mutex_is_locked(&easycapdc60_dongle
3095 [ndong].mutex_video)) &&
3096 (!mutex_is_locked(&easycapdc60_dongle
3097 [ndong].mutex_audio))) {
3098 easycapdc60_dongle[ndong].peasycap = peasycap;
3099 peasycap->isdongle = ndong;
3100 JOM(8, "intf[%i]: peasycap-->easycap"
3101 "_dongle[%i].peasycap\n",
3102 bInterfaceNumber, ndong);
3106 if (DONGLE_MANY <= ndong) {
3107 SAM("ERROR: too many dongles\n");
3108 mutex_unlock(&mutex_dongle);
3111 mutex_unlock(&mutex_dongle);
3113 peasycap->allocation_video_struct = sizeof(struct easycap);
3114 peasycap->allocation_video_page = 0;
3115 peasycap->allocation_video_urb = 0;
3116 peasycap->allocation_audio_struct = 0;
3117 peasycap->allocation_audio_page = 0;
3118 peasycap->allocation_audio_urb = 0;
3120 /*---------------------------------------------------------------------------*/
3122 * ... AND FURTHER INITIALIZE THE STRUCTURE
3124 /*---------------------------------------------------------------------------*/
3125 peasycap->pusb_device = usbdev;
3126 peasycap->pusb_interface = intf;
3129 peasycap->microphone = false;
3131 peasycap->video_interface = -1;
3132 peasycap->video_altsetting_on = -1;
3133 peasycap->video_altsetting_off = -1;
3134 peasycap->video_endpointnumber = -1;
3135 peasycap->video_isoc_maxframesize = -1;
3136 peasycap->video_isoc_buffer_size = -1;
3138 peasycap->audio_interface = -1;
3139 peasycap->audio_altsetting_on = -1;
3140 peasycap->audio_altsetting_off = -1;
3141 peasycap->audio_endpointnumber = -1;
3142 peasycap->audio_isoc_maxframesize = -1;
3143 peasycap->audio_isoc_buffer_size = -1;
3145 peasycap->frame_buffer_many = FRAME_BUFFER_MANY;
3147 for (k = 0; k < INPUT_MANY; k++)
3148 peasycap->lost[k] = 0;
3150 peasycap->skipped = 0;
3151 peasycap->offerfields = 0;
3152 /*---------------------------------------------------------------------------*/
3154 * DYNAMICALLY FILL IN THE AVAILABLE FORMATS ...
3156 /*---------------------------------------------------------------------------*/
3157 rc = fillin_formats();
3159 SAM("ERROR: fillin_formats() rc = %i\n", rc);
3162 JOM(4, "%i formats available\n", rc);
3163 /*---------------------------------------------------------------------------*/
3165 * ... AND POPULATE easycap.inputset[]
3167 /*---------------------------------------------------------------------------*/
3168 /* FIXME: maybe we just use memset 0 */
3169 inputset = peasycap->inputset;
3170 for (k = 0; k < INPUT_MANY; k++) {
3171 inputset[k].input_ok = 0;
3172 inputset[k].standard_offset_ok = 0;
3173 inputset[k].format_offset_ok = 0;
3174 inputset[k].brightness_ok = 0;
3175 inputset[k].contrast_ok = 0;
3176 inputset[k].saturation_ok = 0;
3177 inputset[k].hue_ok = 0;
3180 fmtidx = peasycap->ntsc ? NTSC_M : PAL_BGHIN;
3183 for (i = 0; 0xFFFF != easycap_standard[i].mask; i++) {
3184 if (fmtidx == easycap_standard[i].v4l2_standard.index) {
3186 for (k = 0; k < INPUT_MANY; k++)
3187 inputset[k].standard_offset = i;
3189 mask = easycap_standard[i].mask;
3195 "inputset->standard_offset unpopulated, %i=m\n", m);
3199 peasycap_format = &easycap_format[0];
3201 for (i = 0; peasycap_format->v4l2_format.fmt.pix.width; i++) {
3202 struct v4l2_pix_format *pix =
3203 &peasycap_format->v4l2_format.fmt.pix;
3204 if (((peasycap_format->mask & 0x0F) == (mask & 0x0F)) &&
3205 pix->field == V4L2_FIELD_NONE &&
3206 pix->pixelformat == V4L2_PIX_FMT_UYVY &&
3207 pix->width == 640 && pix->height == 480) {
3209 for (k = 0; k < INPUT_MANY; k++)
3210 inputset[k].format_offset = i;
3216 SAM("ERROR: inputset[]->format_offset unpopulated\n");
3221 for (i = 0; 0xFFFFFFFF != easycap_control[i].id; i++) {
3222 value = easycap_control[i].default_value;
3223 if (V4L2_CID_BRIGHTNESS == easycap_control[i].id) {
3225 for (k = 0; k < INPUT_MANY; k++)
3226 inputset[k].brightness = value;
3227 } else if (V4L2_CID_CONTRAST == easycap_control[i].id) {
3229 for (k = 0; k < INPUT_MANY; k++)
3230 inputset[k].contrast = value;
3231 } else if (V4L2_CID_SATURATION == easycap_control[i].id) {
3233 for (k = 0; k < INPUT_MANY; k++)
3234 inputset[k].saturation = value;
3235 } else if (V4L2_CID_HUE == easycap_control[i].id) {
3237 for (k = 0; k < INPUT_MANY; k++)
3238 inputset[k].hue = value;
3243 SAM("ERROR: inputset[]->brightness underpopulated\n");
3246 for (k = 0; k < INPUT_MANY; k++)
3247 inputset[k].input = k;
3248 JOM(4, "populated inputset[]\n");
3249 JOM(4, "finished initialization\n");
3251 /*---------------------------------------------------------------------------*/
3255 * IDENTIFY THE APPROPRIATE POINTER peasycap FOR INTERFACES 1 AND 2.
3256 * THE ADDRESS OF peasycap->pusb_device IS RELUCTANTLY USED FOR THIS PURPOSE.
3258 /*---------------------------------------------------------------------------*/
3259 for (ndong = 0; ndong < DONGLE_MANY; ndong++) {
3260 if (usbdev == easycapdc60_dongle[ndong].peasycap->
3262 peasycap = easycapdc60_dongle[ndong].peasycap;
3263 JOT(8, "intf[%i]: dongle[%i].peasycap\n",
3264 bInterfaceNumber, ndong);
3268 if (DONGLE_MANY <= ndong) {
3269 SAY("ERROR: peasycap is unknown when probing interface %i\n",
3274 SAY("ERROR: peasycap is NULL when probing interface %i\n",
3278 /*---------------------------------------------------------------------------*/
3280 * SOME VERSIONS OF THE videodev MODULE OVERWRITE THE DATA WHICH HAS
3281 * BEEN WRITTEN BY THE CALL TO usb_set_intfdata() IN easycap_usb_probe(),
3282 * REPLACING IT WITH A POINTER TO THE EMBEDDED v4l2_device STRUCTURE.
3283 * TO DETECT THIS, THE STRING IN THE easycap.telltale[] BUFFER IS CHECKED.
3285 /*---------------------------------------------------------------------------*/
3286 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
3287 pv4l2_device = usb_get_intfdata(intf);
3288 if (!pv4l2_device) {
3289 SAY("ERROR: pv4l2_device is NULL\n");
3292 peasycap = (struct easycap *)
3293 container_of(pv4l2_device, struct easycap, v4l2_device);
3296 /*---------------------------------------------------------------------------*/
3297 if ((USB_CLASS_VIDEO == bInterfaceClass) ||
3298 (USB_CLASS_VENDOR_SPEC == bInterfaceClass)) {
3299 if (-1 == peasycap->video_interface) {
3300 peasycap->video_interface = bInterfaceNumber;
3301 JOM(4, "setting peasycap->video_interface=%i\n",
3302 peasycap->video_interface);
3304 if (peasycap->video_interface != bInterfaceNumber) {
3305 SAM("ERROR: attempting to reset "
3306 "peasycap->video_interface\n");
3307 SAM("...... continuing with "
3308 "%i=peasycap->video_interface\n",
3309 peasycap->video_interface);
3312 } else if ((USB_CLASS_AUDIO == bInterfaceClass) &&
3313 (USB_SUBCLASS_AUDIOSTREAMING == bInterfaceSubClass)) {
3314 if (-1 == peasycap->audio_interface) {
3315 peasycap->audio_interface = bInterfaceNumber;
3316 JOM(4, "setting peasycap->audio_interface=%i\n",
3317 peasycap->audio_interface);
3319 if (peasycap->audio_interface != bInterfaceNumber) {
3320 SAM("ERROR: attempting to reset "
3321 "peasycap->audio_interface\n");
3322 SAM("...... continuing with "
3323 "%i=peasycap->audio_interface\n",
3324 peasycap->audio_interface);
3328 /*---------------------------------------------------------------------------*/
3330 * INVESTIGATE ALL ALTSETTINGS.
3331 * DONE IN DETAIL BECAUSE USB DEVICE 05e1:0408 HAS DISPARATE INCARNATIONS.
3333 /*---------------------------------------------------------------------------*/
3336 for (i = 0; i < intf->num_altsetting; i++) {
3337 alt = usb_altnum_to_altsetting(intf, i);
3339 SAM("ERROR: alt is NULL\n");
3342 interface = &alt->desc;
3344 SAM("ERROR: intf_descriptor is NULL\n");
3348 JOM(4, "intf[%i]alt[%i]: desc.bDescriptorType=0x%02X\n",
3349 bInterfaceNumber, i, interface->bDescriptorType);
3350 JOM(4, "intf[%i]alt[%i]: desc.bInterfaceNumber=0x%02X\n",
3351 bInterfaceNumber, i, interface->bInterfaceNumber);
3352 JOM(4, "intf[%i]alt[%i]: desc.bAlternateSetting=0x%02X\n",
3353 bInterfaceNumber, i, interface->bAlternateSetting);
3354 JOM(4, "intf[%i]alt[%i]: desc.bNumEndpoints=0x%02X\n",
3355 bInterfaceNumber, i, interface->bNumEndpoints);
3356 JOM(4, "intf[%i]alt[%i]: desc.bInterfaceClass=0x%02X\n",
3357 bInterfaceNumber, i, interface->bInterfaceClass);
3358 JOM(4, "intf[%i]alt[%i]: desc.bInterfaceSubClass=0x%02X\n",
3359 bInterfaceNumber, i, interface->bInterfaceSubClass);
3360 JOM(4, "intf[%i]alt[%i]: desc.bInterfaceProtocol=0x%02X\n",
3361 bInterfaceNumber, i, interface->bInterfaceProtocol);
3362 JOM(4, "intf[%i]alt[%i]: desc.iInterface=0x%02X\n",
3363 bInterfaceNumber, i, interface->iInterface);
3365 ISOCwMaxPacketSize = -1;
3366 BULKwMaxPacketSize = -1;
3367 INTwMaxPacketSize = -1;
3368 CTRLwMaxPacketSize = -1;
3369 ISOCbEndpointAddress = 0;
3370 INTbEndpointAddress = 0;
3372 if (0 == interface->bNumEndpoints)
3373 JOM(4, "intf[%i]alt[%i] has no endpoints\n",
3374 bInterfaceNumber, i);
3375 /*---------------------------------------------------------------------------*/
3376 for (j = 0; j < interface->bNumEndpoints; j++) {
3377 ep = &alt->endpoint[j].desc;
3379 SAM("ERROR: ep is NULL.\n");
3380 SAM("...... skipping\n");
3383 wMaxPacketSize = le16_to_cpu(ep->wMaxPacketSize);
3384 bEndpointAddress = ep->bEndpointAddress;
3386 JOM(4, "intf[%i]alt[%i]end[%i]: bEndpointAddress=0x%X\n",
3387 bInterfaceNumber, i, j,
3388 ep->bEndpointAddress);
3389 JOM(4, "intf[%i]alt[%i]end[%i]: bmAttributes=0x%X\n",
3390 bInterfaceNumber, i, j,
3392 JOM(4, "intf[%i]alt[%i]end[%i]: wMaxPacketSize=%i\n",
3393 bInterfaceNumber, i, j,
3394 ep->wMaxPacketSize);
3395 JOM(4, "intf[%i]alt[%i]end[%i]: bInterval=%i\n",
3396 bInterfaceNumber, i, j,
3399 if (ep->bEndpointAddress & USB_DIR_IN) {
3400 JOM(4, "intf[%i]alt[%i]end[%i] is an IN endpoint\n",
3401 bInterfaceNumber, i, j);
3404 JOM(4, "intf[%i]alt[%i]end[%i] is an OUT endpoint\n",
3405 bInterfaceNumber, i, j);
3406 SAM("ERROR: OUT endpoint unexpected\n");
3407 SAM("...... continuing\n");
3410 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
3411 USB_ENDPOINT_XFER_ISOC) {
3412 JOM(4, "intf[%i]alt[%i]end[%i] is an ISOC endpoint\n",
3413 bInterfaceNumber, i, j);
3415 switch (bInterfaceClass) {
3416 case USB_CLASS_VIDEO:
3417 case USB_CLASS_VENDOR_SPEC: {
3420 "peasycap is NULL\n");
3423 if (ep->wMaxPacketSize) {
3448 if (-1 == peasycap->
3449 video_altsetting_off) {
3451 video_altsetting_off =
3457 video_altsetting_off);
3459 SAM("ERROR: peasycap"
3460 "->video_altsetting_"
3461 "off already set\n");
3464 "%i=peasycap->video_"
3467 video_altsetting_off);
3472 case USB_CLASS_AUDIO: {
3473 if (bInterfaceSubClass !=
3474 USB_SUBCLASS_AUDIOSTREAMING)
3478 "peasycap is NULL\n");
3481 if (ep->wMaxPacketSize) {
3483 okalt[isokalt] = i ;
3506 if (-1 == peasycap->
3507 audio_altsetting_off) {
3509 audio_altsetting_off =
3515 audio_altsetting_off);
3517 SAM("ERROR: peasycap"
3518 "->audio_altsetting_"
3519 "off already set\n");
3526 audio_altsetting_off);
3535 } else if ((ep->bmAttributes &
3536 USB_ENDPOINT_XFERTYPE_MASK) ==
3537 USB_ENDPOINT_XFER_BULK) {
3538 JOM(4, "intf[%i]alt[%i]end[%i] is a BULK endpoint\n",
3539 bInterfaceNumber, i, j);
3540 } else if ((ep->bmAttributes &
3541 USB_ENDPOINT_XFERTYPE_MASK) ==
3542 USB_ENDPOINT_XFER_INT) {
3543 JOM(4, "intf[%i]alt[%i]end[%i] is an INT endpoint\n",
3544 bInterfaceNumber, i, j);
3546 JOM(4, "intf[%i]alt[%i]end[%i] is a CTRL endpoint\n",
3547 bInterfaceNumber, i, j);
3549 if (0 == ep->wMaxPacketSize) {
3550 JOM(4, "intf[%i]alt[%i]end[%i] "
3551 "has zero packet size\n",
3552 bInterfaceNumber, i, j);
3556 /*---------------------------------------------------------------------------*/
3558 * PERFORM INITIALIZATION OF THE PROBED INTERFACE
3560 /*---------------------------------------------------------------------------*/
3561 JOM(4, "initialization begins for interface %i\n",
3562 interface->bInterfaceNumber);
3563 switch (bInterfaceNumber) {
3564 /*---------------------------------------------------------------------------*/
3566 * INTERFACE 0 IS THE VIDEO INTERFACE
3568 /*---------------------------------------------------------------------------*/
3571 SAM("MISTAKE: peasycap is NULL\n");
3575 SAM("ERROR: no viable video_altsetting_on\n");
3578 peasycap->video_altsetting_on = okalt[isokalt - 1];
3579 JOM(4, "%i=video_altsetting_on <====\n",
3580 peasycap->video_altsetting_on);
3582 /*---------------------------------------------------------------------------*/
3584 * DECIDE THE VIDEO STREAMING PARAMETERS
3586 /*---------------------------------------------------------------------------*/
3587 peasycap->video_endpointnumber = okepn[isokalt - 1];
3588 JOM(4, "%i=video_endpointnumber\n", peasycap->video_endpointnumber);
3589 maxpacketsize = okmps[isokalt - 1];
3591 peasycap->video_isoc_maxframesize =
3592 min(maxpacketsize, USB_2_0_MAXPACKETSIZE);
3593 if (0 >= peasycap->video_isoc_maxframesize) {
3594 SAM("ERROR: bad video_isoc_maxframesize\n");
3595 SAM(" possibly because port is USB 1.1\n");
3598 JOM(4, "%i=video_isoc_maxframesize\n",
3599 peasycap->video_isoc_maxframesize);
3601 peasycap->video_isoc_framesperdesc = VIDEO_ISOC_FRAMESPERDESC;
3602 JOM(4, "%i=video_isoc_framesperdesc\n",
3603 peasycap->video_isoc_framesperdesc);
3604 if (0 >= peasycap->video_isoc_framesperdesc) {
3605 SAM("ERROR: bad video_isoc_framesperdesc\n");
3608 peasycap->video_isoc_buffer_size =
3609 peasycap->video_isoc_maxframesize *
3610 peasycap->video_isoc_framesperdesc;
3611 JOM(4, "%i=video_isoc_buffer_size\n",
3612 peasycap->video_isoc_buffer_size);
3613 if ((PAGE_SIZE << VIDEO_ISOC_ORDER) <
3614 peasycap->video_isoc_buffer_size) {
3615 SAM("MISTAKE: peasycap->video_isoc_buffer_size too big\n");
3618 /*---------------------------------------------------------------------------*/
3619 if (-1 == peasycap->video_interface) {
3620 SAM("MISTAKE: video_interface is unset\n");
3623 if (-1 == peasycap->video_altsetting_on) {
3624 SAM("MISTAKE: video_altsetting_on is unset\n");
3627 if (-1 == peasycap->video_altsetting_off) {
3628 SAM("MISTAKE: video_interface_off is unset\n");
3631 if (-1 == peasycap->video_endpointnumber) {
3632 SAM("MISTAKE: video_endpointnumber is unset\n");
3635 if (-1 == peasycap->video_isoc_maxframesize) {
3636 SAM("MISTAKE: video_isoc_maxframesize is unset\n");
3639 if (-1 == peasycap->video_isoc_buffer_size) {
3640 SAM("MISTAKE: video_isoc_buffer_size is unset\n");
3643 /*---------------------------------------------------------------------------*/
3645 * ALLOCATE MEMORY FOR VIDEO BUFFERS. LISTS MUST BE INITIALIZED FIRST.
3647 /*---------------------------------------------------------------------------*/
3648 INIT_LIST_HEAD(&(peasycap->urb_video_head));
3649 peasycap->purb_video_head = &(peasycap->urb_video_head);
3650 /*---------------------------------------------------------------------------*/
3651 JOM(4, "allocating %i frame buffers of size %li\n",
3652 FRAME_BUFFER_MANY, (long int)FRAME_BUFFER_SIZE);
3653 JOM(4, ".... each scattered over %li pages\n",
3654 FRAME_BUFFER_SIZE/PAGE_SIZE);
3656 for (k = 0; k < FRAME_BUFFER_MANY; k++) {
3657 for (m = 0; m < FRAME_BUFFER_SIZE/PAGE_SIZE; m++) {
3658 if (peasycap->frame_buffer[k][m].pgo)
3659 SAM("attempting to reallocate frame "
3662 pbuf = (void *)__get_free_page(GFP_KERNEL);
3664 SAM("ERROR: Could not allocate frame "
3665 "buffer %i page %i\n", k, m);
3668 peasycap->allocation_video_page += 1;
3669 peasycap->frame_buffer[k][m].pgo = pbuf;
3671 peasycap->frame_buffer[k][m].pto =
3672 peasycap->frame_buffer[k][m].pgo;
3676 peasycap->frame_fill = 0;
3677 peasycap->frame_read = 0;
3678 JOM(4, "allocation of frame buffers done: %i pages\n", k *
3680 /*---------------------------------------------------------------------------*/
3681 JOM(4, "allocating %i field buffers of size %li\n",
3682 FIELD_BUFFER_MANY, (long int)FIELD_BUFFER_SIZE);
3683 JOM(4, ".... each scattered over %li pages\n",
3684 FIELD_BUFFER_SIZE/PAGE_SIZE);
3686 for (k = 0; k < FIELD_BUFFER_MANY; k++) {
3687 for (m = 0; m < FIELD_BUFFER_SIZE/PAGE_SIZE; m++) {
3688 if (peasycap->field_buffer[k][m].pgo) {
3689 SAM("ERROR: attempting to reallocate "
3692 pbuf = (void *) __get_free_page(GFP_KERNEL);
3694 SAM("ERROR: Could not allocate field"
3695 " buffer %i page %i\n", k, m);
3699 peasycap->allocation_video_page += 1;
3700 peasycap->field_buffer[k][m].pgo = pbuf;
3702 peasycap->field_buffer[k][m].pto =
3703 peasycap->field_buffer[k][m].pgo;
3705 peasycap->field_buffer[k][0].kount = 0x0200;
3707 peasycap->field_fill = 0;
3708 peasycap->field_page = 0;
3709 peasycap->field_read = 0;
3710 JOM(4, "allocation of field buffers done: %i pages\n", k *
3712 /*---------------------------------------------------------------------------*/
3713 JOM(4, "allocating %i isoc video buffers of size %i\n",
3714 VIDEO_ISOC_BUFFER_MANY,
3715 peasycap->video_isoc_buffer_size);
3716 JOM(4, ".... each occupying contiguous memory pages\n");
3718 for (k = 0; k < VIDEO_ISOC_BUFFER_MANY; k++) {
3719 pbuf = (void *)__get_free_pages(GFP_KERNEL,
3722 SAM("ERROR: Could not allocate isoc video buffer "
3726 peasycap->allocation_video_page +=
3727 BIT(VIDEO_ISOC_ORDER);
3729 peasycap->video_isoc_buffer[k].pgo = pbuf;
3730 peasycap->video_isoc_buffer[k].pto =
3731 pbuf + peasycap->video_isoc_buffer_size;
3732 peasycap->video_isoc_buffer[k].kount = k;
3734 JOM(4, "allocation of isoc video buffers done: %i pages\n",
3735 k * (0x01 << VIDEO_ISOC_ORDER));
3736 /*---------------------------------------------------------------------------*/
3738 * ALLOCATE AND INITIALIZE MULTIPLE struct urb ...
3740 /*---------------------------------------------------------------------------*/
3741 JOM(4, "allocating %i struct urb.\n", VIDEO_ISOC_BUFFER_MANY);
3742 JOM(4, "using %i=peasycap->video_isoc_framesperdesc\n",
3743 peasycap->video_isoc_framesperdesc);
3744 JOM(4, "using %i=peasycap->video_isoc_maxframesize\n",
3745 peasycap->video_isoc_maxframesize);
3746 JOM(4, "using %i=peasycap->video_isoc_buffer_sizen",
3747 peasycap->video_isoc_buffer_size);
3749 for (k = 0; k < VIDEO_ISOC_BUFFER_MANY; k++) {
3750 purb = usb_alloc_urb(peasycap->video_isoc_framesperdesc,
3753 SAM("ERROR: usb_alloc_urb returned NULL for buffer "
3757 peasycap->allocation_video_urb += 1;
3758 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
3759 pdata_urb = kzalloc(sizeof(struct data_urb), GFP_KERNEL);
3761 SAM("ERROR: Could not allocate struct data_urb.\n");
3764 peasycap->allocation_video_struct +=
3765 sizeof(struct data_urb);
3767 pdata_urb->purb = purb;
3768 pdata_urb->isbuf = k;
3769 pdata_urb->length = 0;
3770 list_add_tail(&(pdata_urb->list_head),
3771 peasycap->purb_video_head);
3772 /*---------------------------------------------------------------------------*/
3774 * ... AND INITIALIZE THEM
3776 /*---------------------------------------------------------------------------*/
3778 JOM(4, "initializing video urbs thus:\n");
3779 JOM(4, " purb->interval = 1;\n");
3780 JOM(4, " purb->dev = peasycap->pusb_device;\n");
3781 JOM(4, " purb->pipe = usb_rcvisocpipe"
3782 "(peasycap->pusb_device,%i);\n",
3783 peasycap->video_endpointnumber);
3784 JOM(4, " purb->transfer_flags = URB_ISO_ASAP;\n");
3785 JOM(4, " purb->transfer_buffer = peasycap->"
3786 "video_isoc_buffer[.].pgo;\n");
3787 JOM(4, " purb->transfer_buffer_length = %i;\n",
3788 peasycap->video_isoc_buffer_size);
3789 JOM(4, " purb->complete = easycap_complete;\n");
3790 JOM(4, " purb->context = peasycap;\n");
3791 JOM(4, " purb->start_frame = 0;\n");
3792 JOM(4, " purb->number_of_packets = %i;\n",
3793 peasycap->video_isoc_framesperdesc);
3794 JOM(4, " for (j = 0; j < %i; j++)\n",
3795 peasycap->video_isoc_framesperdesc);
3797 JOM(4, " purb->iso_frame_desc[j].offset = j*%i;\n",
3798 peasycap->video_isoc_maxframesize);
3799 JOM(4, " purb->iso_frame_desc[j].length = %i;\n",
3800 peasycap->video_isoc_maxframesize);
3805 purb->dev = peasycap->pusb_device;
3806 purb->pipe = usb_rcvisocpipe(peasycap->pusb_device,
3807 peasycap->video_endpointnumber);
3808 purb->transfer_flags = URB_ISO_ASAP;
3809 purb->transfer_buffer = peasycap->video_isoc_buffer[k].pgo;
3810 purb->transfer_buffer_length =
3811 peasycap->video_isoc_buffer_size;
3812 purb->complete = easycap_complete;
3813 purb->context = peasycap;
3814 purb->start_frame = 0;
3815 purb->number_of_packets = peasycap->video_isoc_framesperdesc;
3816 for (j = 0; j < peasycap->video_isoc_framesperdesc; j++) {
3817 purb->iso_frame_desc[j].offset = j *
3818 peasycap->video_isoc_maxframesize;
3819 purb->iso_frame_desc[j].length =
3820 peasycap->video_isoc_maxframesize;
3823 JOM(4, "allocation of %i struct urb done.\n", k);
3824 /*--------------------------------------------------------------------------*/
3826 * SAVE POINTER peasycap IN THIS INTERFACE.
3828 /*--------------------------------------------------------------------------*/
3829 usb_set_intfdata(intf, peasycap);
3830 /*---------------------------------------------------------------------------*/
3832 * IT IS ESSENTIAL TO INITIALIZE THE HARDWARE BEFORE, RATHER THAN AFTER,
3833 * THE DEVICE IS REGISTERED, BECAUSE SOME VERSIONS OF THE videodev MODULE
3834 * CALL easycap_open() IMMEDIATELY AFTER REGISTRATION, CAUSING A CLASH.
3837 /*---------------------------------------------------------------------------*/
3838 peasycap->ntsc = easycap_ntsc;
3839 JOM(8, "defaulting initially to %s\n",
3840 easycap_ntsc ? "NTSC" : "PAL");
3841 rc = reset(peasycap);
3843 SAM("ERROR: reset() rc = %i\n", rc);
3846 /*--------------------------------------------------------------------------*/
3848 * THE VIDEO DEVICE CAN BE REGISTERED NOW, AS IT IS READY.
3850 /*--------------------------------------------------------------------------*/
3851 if (0 != (v4l2_device_register(&(intf->dev),
3852 &(peasycap->v4l2_device)))) {
3853 SAM("v4l2_device_register() failed\n");
3856 JOM(4, "registered device instance: %s\n",
3857 &(peasycap->v4l2_device.name[0]));
3859 /*---------------------------------------------------------------------------*/
3864 * THIS IS BELIEVED TO BE HARMLESS, BUT MAY WELL BE UNNECESSARY OR WRONG:
3866 /*---------------------------------------------------------------------------*/
3867 peasycap->video_device.v4l2_dev = NULL;
3868 /*---------------------------------------------------------------------------*/
3871 strcpy(&peasycap->video_device.name[0], "easycapdc60");
3872 peasycap->video_device.fops = &v4l2_fops;
3873 peasycap->video_device.minor = -1;
3874 peasycap->video_device.release = (void *)(&videodev_release);
3876 video_set_drvdata(&(peasycap->video_device), (void *)peasycap);
3878 if (0 != (video_register_device(&(peasycap->video_device),
3879 VFL_TYPE_GRABBER, -1))) {
3880 err("Not able to register with videodev");
3881 videodev_release(&(peasycap->video_device));
3884 (peasycap->registered_video)++;
3885 SAM("registered with videodev: %i=minor\n",
3886 peasycap->video_device.minor);
3887 peasycap->minor = peasycap->video_device.minor;
3889 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
3893 /*--------------------------------------------------------------------------*/
3895 * INTERFACE 1 IS THE AUDIO CONTROL INTERFACE
3896 * INTERFACE 2 IS THE AUDIO STREAMING INTERFACE
3898 /*--------------------------------------------------------------------------*/
3901 SAM("MISTAKE: peasycap is NULL\n");
3904 /*--------------------------------------------------------------------------*/
3906 * SAVE POINTER peasycap IN INTERFACE 1
3908 /*--------------------------------------------------------------------------*/
3909 usb_set_intfdata(intf, peasycap);
3910 JOM(4, "no initialization required for interface %i\n",
3911 interface->bInterfaceNumber);
3914 /*--------------------------------------------------------------------------*/
3917 SAM("MISTAKE: peasycap is NULL\n");
3921 SAM("ERROR: no viable audio_altsetting_on\n");
3924 peasycap->audio_altsetting_on = okalt[isokalt - 1];
3925 JOM(4, "%i=audio_altsetting_on <====\n",
3926 peasycap->audio_altsetting_on);
3929 peasycap->audio_endpointnumber = okepn[isokalt - 1];
3930 JOM(4, "%i=audio_endpointnumber\n", peasycap->audio_endpointnumber);
3932 peasycap->audio_isoc_maxframesize = okmps[isokalt - 1];
3933 JOM(4, "%i=audio_isoc_maxframesize\n",
3934 peasycap->audio_isoc_maxframesize);
3935 if (0 >= peasycap->audio_isoc_maxframesize) {
3936 SAM("ERROR: bad audio_isoc_maxframesize\n");
3939 if (9 == peasycap->audio_isoc_maxframesize) {
3940 peasycap->ilk |= 0x02;
3941 SAM("audio hardware is microphone\n");
3942 peasycap->microphone = true;
3943 peasycap->audio_pages_per_fragment =
3944 PAGES_PER_AUDIO_FRAGMENT;
3945 } else if (256 == peasycap->audio_isoc_maxframesize) {
3946 peasycap->ilk &= ~0x02;
3947 SAM("audio hardware is AC'97\n");
3948 peasycap->microphone = false;
3949 peasycap->audio_pages_per_fragment =
3950 PAGES_PER_AUDIO_FRAGMENT;
3952 SAM("hardware is unidentified:\n");
3953 SAM("%i=audio_isoc_maxframesize\n",
3954 peasycap->audio_isoc_maxframesize);
3958 peasycap->audio_bytes_per_fragment =
3959 peasycap->audio_pages_per_fragment * PAGE_SIZE;
3960 peasycap->audio_buffer_page_many = (AUDIO_FRAGMENT_MANY *
3961 peasycap->audio_pages_per_fragment);
3963 JOM(4, "%6i=AUDIO_FRAGMENT_MANY\n", AUDIO_FRAGMENT_MANY);
3964 JOM(4, "%6i=audio_pages_per_fragment\n",
3965 peasycap->audio_pages_per_fragment);
3966 JOM(4, "%6i=audio_bytes_per_fragment\n",
3967 peasycap->audio_bytes_per_fragment);
3968 JOM(4, "%6i=audio_buffer_page_many\n",
3969 peasycap->audio_buffer_page_many);
3971 peasycap->audio_isoc_framesperdesc = AUDIO_ISOC_FRAMESPERDESC;
3973 JOM(4, "%i=audio_isoc_framesperdesc\n",
3974 peasycap->audio_isoc_framesperdesc);
3975 if (0 >= peasycap->audio_isoc_framesperdesc) {
3976 SAM("ERROR: bad audio_isoc_framesperdesc\n");
3980 peasycap->audio_isoc_buffer_size =
3981 peasycap->audio_isoc_maxframesize *
3982 peasycap->audio_isoc_framesperdesc;
3983 JOM(4, "%i=audio_isoc_buffer_size\n",
3984 peasycap->audio_isoc_buffer_size);
3985 if (AUDIO_ISOC_BUFFER_SIZE < peasycap->audio_isoc_buffer_size) {
3986 SAM("MISTAKE: audio_isoc_buffer_size bigger "
3987 "than %li=AUDIO_ISOC_BUFFER_SIZE\n",
3988 AUDIO_ISOC_BUFFER_SIZE);
3991 if (-1 == peasycap->audio_interface) {
3992 SAM("MISTAKE: audio_interface is unset\n");
3995 if (-1 == peasycap->audio_altsetting_on) {
3996 SAM("MISTAKE: audio_altsetting_on is unset\n");
3999 if (-1 == peasycap->audio_altsetting_off) {
4000 SAM("MISTAKE: audio_interface_off is unset\n");
4003 if (-1 == peasycap->audio_endpointnumber) {
4004 SAM("MISTAKE: audio_endpointnumber is unset\n");
4007 if (-1 == peasycap->audio_isoc_maxframesize) {
4008 SAM("MISTAKE: audio_isoc_maxframesize is unset\n");
4011 if (-1 == peasycap->audio_isoc_buffer_size) {
4012 SAM("MISTAKE: audio_isoc_buffer_size is unset\n");
4015 /*---------------------------------------------------------------------------*/
4017 * ALLOCATE MEMORY FOR AUDIO BUFFERS. LISTS MUST BE INITIALIZED FIRST.
4019 /*---------------------------------------------------------------------------*/
4020 INIT_LIST_HEAD(&(peasycap->urb_audio_head));
4021 peasycap->purb_audio_head = &(peasycap->urb_audio_head);
4023 #ifdef CONFIG_EASYCAP_OSS
4024 JOM(4, "allocating an audio buffer\n");
4025 JOM(4, ".... scattered over %i pages\n",
4026 peasycap->audio_buffer_page_many);
4028 for (k = 0; k < peasycap->audio_buffer_page_many; k++) {
4029 if (peasycap->audio_buffer[k].pgo) {
4030 SAM("ERROR: attempting to reallocate audio buffers\n");
4032 pbuf = (void *) __get_free_page(GFP_KERNEL);
4034 SAM("ERROR: Could not allocate audio "
4035 "buffer page %i\n", k);
4038 peasycap->allocation_audio_page += 1;
4040 peasycap->audio_buffer[k].pgo = pbuf;
4042 peasycap->audio_buffer[k].pto = peasycap->audio_buffer[k].pgo;
4045 peasycap->audio_fill = 0;
4046 peasycap->audio_read = 0;
4047 JOM(4, "allocation of audio buffer done: %i pages\n", k);
4048 #endif /* CONFIG_EASYCAP_OSS */
4049 /*---------------------------------------------------------------------------*/
4050 JOM(4, "allocating %i isoc audio buffers of size %i\n",
4051 AUDIO_ISOC_BUFFER_MANY,
4052 peasycap->audio_isoc_buffer_size);
4053 JOM(4, ".... each occupying contiguous memory pages\n");
4055 for (k = 0; k < AUDIO_ISOC_BUFFER_MANY; k++) {
4056 pbuf = (void *)__get_free_pages(GFP_KERNEL,
4059 SAM("ERROR: Could not allocate isoc audio buffer "
4063 peasycap->allocation_audio_page +=
4064 BIT(AUDIO_ISOC_ORDER);
4066 peasycap->audio_isoc_buffer[k].pgo = pbuf;
4067 peasycap->audio_isoc_buffer[k].pto = pbuf +
4068 peasycap->audio_isoc_buffer_size;
4069 peasycap->audio_isoc_buffer[k].kount = k;
4071 JOM(4, "allocation of isoc audio buffers done.\n");
4072 /*---------------------------------------------------------------------------*/
4074 * ALLOCATE AND INITIALIZE MULTIPLE struct urb ...
4076 /*---------------------------------------------------------------------------*/
4077 JOM(4, "allocating %i struct urb.\n", AUDIO_ISOC_BUFFER_MANY);
4078 JOM(4, "using %i=peasycap->audio_isoc_framesperdesc\n",
4079 peasycap->audio_isoc_framesperdesc);
4080 JOM(4, "using %i=peasycap->audio_isoc_maxframesize\n",
4081 peasycap->audio_isoc_maxframesize);
4082 JOM(4, "using %i=peasycap->audio_isoc_buffer_size\n",
4083 peasycap->audio_isoc_buffer_size);
4085 for (k = 0; k < AUDIO_ISOC_BUFFER_MANY; k++) {
4086 purb = usb_alloc_urb(peasycap->audio_isoc_framesperdesc,
4089 SAM("ERROR: usb_alloc_urb returned NULL for buffer "
4093 peasycap->allocation_audio_urb += 1 ;
4094 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
4095 pdata_urb = kzalloc(sizeof(struct data_urb), GFP_KERNEL);
4097 SAM("ERROR: Could not allocate struct data_urb.\n");
4100 peasycap->allocation_audio_struct +=
4101 sizeof(struct data_urb);
4103 pdata_urb->purb = purb;
4104 pdata_urb->isbuf = k;
4105 pdata_urb->length = 0;
4106 list_add_tail(&(pdata_urb->list_head),
4107 peasycap->purb_audio_head);
4108 /*---------------------------------------------------------------------------*/
4110 * ... AND INITIALIZE THEM
4112 /*---------------------------------------------------------------------------*/
4114 JOM(4, "initializing audio urbs thus:\n");
4115 JOM(4, " purb->interval = 1;\n");
4116 JOM(4, " purb->dev = peasycap->pusb_device;\n");
4117 JOM(4, " purb->pipe = usb_rcvisocpipe(peasycap->"
4118 "pusb_device,%i);\n",
4119 peasycap->audio_endpointnumber);
4120 JOM(4, " purb->transfer_flags = URB_ISO_ASAP;\n");
4121 JOM(4, " purb->transfer_buffer = "
4122 "peasycap->audio_isoc_buffer[.].pgo;\n");
4123 JOM(4, " purb->transfer_buffer_length = %i;\n",
4124 peasycap->audio_isoc_buffer_size);
4125 #ifdef CONFIG_EASYCAP_OSS
4126 JOM(4, " purb->complete = easyoss_complete;\n");
4127 #else /* CONFIG_EASYCAP_OSS */
4128 JOM(4, " purb->complete = easycap_alsa_complete;\n");
4129 #endif /* CONFIG_EASYCAP_OSS */
4130 JOM(4, " purb->context = peasycap;\n");
4131 JOM(4, " purb->start_frame = 0;\n");
4132 JOM(4, " purb->number_of_packets = %i;\n",
4133 peasycap->audio_isoc_framesperdesc);
4134 JOM(4, " for (j = 0; j < %i; j++)\n",
4135 peasycap->audio_isoc_framesperdesc);
4137 JOM(4, " purb->iso_frame_desc[j].offset = j*%i;\n",
4138 peasycap->audio_isoc_maxframesize);
4139 JOM(4, " purb->iso_frame_desc[j].length = %i;\n",
4140 peasycap->audio_isoc_maxframesize);
4145 purb->dev = peasycap->pusb_device;
4146 purb->pipe = usb_rcvisocpipe(peasycap->pusb_device,
4147 peasycap->audio_endpointnumber);
4148 purb->transfer_flags = URB_ISO_ASAP;
4149 purb->transfer_buffer = peasycap->audio_isoc_buffer[k].pgo;
4150 purb->transfer_buffer_length =
4151 peasycap->audio_isoc_buffer_size;
4152 #ifdef CONFIG_EASYCAP_OSS
4153 purb->complete = easyoss_complete;
4154 #else /* CONFIG_EASYCAP_OSS */
4155 purb->complete = easycap_alsa_complete;
4156 #endif /* CONFIG_EASYCAP_OSS */
4157 purb->context = peasycap;
4158 purb->start_frame = 0;
4159 purb->number_of_packets = peasycap->audio_isoc_framesperdesc;
4160 for (j = 0; j < peasycap->audio_isoc_framesperdesc; j++) {
4161 purb->iso_frame_desc[j].offset = j *
4162 peasycap->audio_isoc_maxframesize;
4163 purb->iso_frame_desc[j].length =
4164 peasycap->audio_isoc_maxframesize;
4167 JOM(4, "allocation of %i struct urb done.\n", k);
4168 /*---------------------------------------------------------------------------*/
4170 * SAVE POINTER peasycap IN THIS INTERFACE.
4172 /*---------------------------------------------------------------------------*/
4173 usb_set_intfdata(intf, peasycap);
4174 /*---------------------------------------------------------------------------*/
4176 * THE AUDIO DEVICE CAN BE REGISTERED NOW, AS IT IS READY.
4178 /*---------------------------------------------------------------------------*/
4179 #ifndef CONFIG_EASYCAP_OSS
4180 JOM(4, "initializing ALSA card\n");
4182 rc = easycap_alsa_probe(peasycap);
4184 err("easycap_alsa_probe() rc = %i\n", rc);
4188 #else /* CONFIG_EASYCAP_OSS */
4189 rc = usb_register_dev(intf, &easyoss_class);
4191 SAY("ERROR: usb_register_dev() failed\n");
4192 usb_set_intfdata(intf, NULL);
4195 SAM("easyoss attached to minor #%d\n", intf->minor);
4196 #endif /* CONFIG_EASYCAP_OSS */
4198 JOM(8, "kref_get() with %i=kref.refcount.counter\n",
4199 peasycap->kref.refcount.counter);
4200 kref_get(&peasycap->kref);
4201 peasycap->registered_audio++;
4204 /*---------------------------------------------------------------------------*/
4206 * INTERFACES OTHER THAN 0, 1 AND 2 ARE UNEXPECTED
4208 /*---------------------------------------------------------------------------*/
4210 JOM(4, "ERROR: unexpected interface %i\n", bInterfaceNumber);
4213 SAM("ends successfully for interface %i\n", bInterfaceNumber);
4216 /*****************************************************************************/
4217 /*---------------------------------------------------------------------------*/
4219 * WHEN THIS FUNCTION IS CALLED THE EasyCAP HAS ALREADY BEEN PHYSICALLY
4220 * UNPLUGGED. HENCE peasycap->pusb_device IS NO LONGER VALID.
4222 * THIS FUNCTION AFFECTS BOTH OSS AND ALSA. BEWARE.
4224 /*---------------------------------------------------------------------------*/
4225 static void easycap_usb_disconnect(struct usb_interface *pusb_interface)
4227 struct usb_host_interface *pusb_host_interface;
4228 struct usb_interface_descriptor *pusb_interface_descriptor;
4229 u8 bInterfaceNumber;
4230 struct easycap *peasycap;
4232 struct list_head *plist_head;
4233 struct data_urb *pdata_urb;
4235 struct v4l2_device *pv4l2_device;
4239 pusb_host_interface = pusb_interface->cur_altsetting;
4240 if (!pusb_host_interface) {
4241 JOT(4, "ERROR: pusb_host_interface is NULL\n");
4244 pusb_interface_descriptor = &(pusb_host_interface->desc);
4245 if (!pusb_interface_descriptor) {
4246 JOT(4, "ERROR: pusb_interface_descriptor is NULL\n");
4249 bInterfaceNumber = pusb_interface_descriptor->bInterfaceNumber;
4250 minor = pusb_interface->minor;
4251 JOT(4, "intf[%i]: minor=%i\n", bInterfaceNumber, minor);
4253 if (1 == bInterfaceNumber)
4256 peasycap = usb_get_intfdata(pusb_interface);
4258 SAY("ERROR: peasycap is NULL\n");
4261 /*---------------------------------------------------------------------------*/
4263 * SOME VERSIONS OF THE videodev MODULE OVERWRITE THE DATA WHICH HAS
4264 * BEEN WRITTEN BY THE CALL TO usb_set_intfdata() IN easycap_usb_probe(),
4265 * REPLACING IT WITH A POINTER TO THE EMBEDDED v4l2_device STRUCTURE.
4266 * TO DETECT THIS, THE STRING IN THE easycap.telltale[] BUFFER IS CHECKED.
4268 /*---------------------------------------------------------------------------*/
4269 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
4270 pv4l2_device = usb_get_intfdata(pusb_interface);
4271 if (!pv4l2_device) {
4272 SAY("ERROR: pv4l2_device is NULL\n");
4275 peasycap = (struct easycap *)
4276 container_of(pv4l2_device, struct easycap, v4l2_device);
4278 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
4279 /*---------------------------------------------------------------------------*/
4280 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
4281 SAY("ERROR: bad peasycap: %p\n", peasycap);
4284 /*---------------------------------------------------------------------------*/
4286 * IF THE WAIT QUEUES ARE NOT CLEARED A DEADLOCK IS POSSIBLE. BEWARE.
4288 /*---------------------------------------------------------------------------*/
4289 peasycap->video_eof = 1;
4290 peasycap->audio_eof = 1;
4291 wake_up_interruptible(&(peasycap->wq_video));
4292 wake_up_interruptible(&(peasycap->wq_audio));
4293 /*---------------------------------------------------------------------------*/
4294 switch (bInterfaceNumber) {
4296 if (peasycap->purb_video_head) {
4297 JOM(4, "killing video urbs\n");
4299 list_for_each(plist_head, peasycap->purb_video_head) {
4300 pdata_urb = list_entry(plist_head,
4301 struct data_urb, list_head);
4303 if (pdata_urb->purb) {
4304 usb_kill_urb(pdata_urb->purb);
4309 JOM(4, "%i video urbs killed\n", m);
4313 /*---------------------------------------------------------------------------*/
4315 if (peasycap->purb_audio_head) {
4316 JOM(4, "killing audio urbs\n");
4318 list_for_each(plist_head, peasycap->purb_audio_head) {
4319 pdata_urb = list_entry(plist_head,
4320 struct data_urb, list_head);
4322 if (pdata_urb->purb) {
4323 usb_kill_urb(pdata_urb->purb);
4328 JOM(4, "%i audio urbs killed\n", m);
4335 /*--------------------------------------------------------------------------*/
4339 * THIS PROCEDURE WILL BLOCK UNTIL easycap_poll(), VIDEO IOCTL AND AUDIO
4340 * IOCTL ARE ALL UNLOCKED. IF THIS IS NOT DONE AN Oops CAN OCCUR WHEN
4341 * AN EasyCAP IS UNPLUGGED WHILE THE URBS ARE RUNNING. BEWARE.
4343 /*--------------------------------------------------------------------------*/
4344 kd = isdongle(peasycap);
4345 switch (bInterfaceNumber) {
4347 if (0 <= kd && DONGLE_MANY > kd) {
4348 wake_up_interruptible(&peasycap->wq_video);
4349 JOM(4, "about to lock dongle[%i].mutex_video\n", kd);
4350 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].
4353 "cannot lock dongle[%i].mutex_video\n", kd);
4356 JOM(4, "locked dongle[%i].mutex_video\n", kd);
4358 SAY("ERROR: %i=kd is bad: cannot lock dongle\n", kd);
4360 /*---------------------------------------------------------------------------*/
4361 if (!peasycap->v4l2_device.name[0]) {
4362 SAM("ERROR: peasycap->v4l2_device.name is empty\n");
4363 if (0 <= kd && DONGLE_MANY > kd)
4364 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
4367 v4l2_device_disconnect(&peasycap->v4l2_device);
4368 JOM(4, "v4l2_device_disconnect() OK\n");
4369 v4l2_device_unregister(&peasycap->v4l2_device);
4370 JOM(4, "v4l2_device_unregister() OK\n");
4372 video_unregister_device(&peasycap->video_device);
4373 JOM(4, "intf[%i]: video_unregister_device() minor=%i\n",
4374 bInterfaceNumber, minor);
4375 peasycap->registered_video--;
4376 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
4378 if (0 <= kd && DONGLE_MANY > kd) {
4379 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
4380 JOM(4, "unlocked dongle[%i].mutex_video\n", kd);
4385 if (0 <= kd && DONGLE_MANY > kd) {
4386 wake_up_interruptible(&peasycap->wq_audio);
4387 JOM(4, "about to lock dongle[%i].mutex_audio\n", kd);
4388 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].
4391 "cannot lock dongle[%i].mutex_audio\n", kd);
4394 JOM(4, "locked dongle[%i].mutex_audio\n", kd);
4396 SAY("ERROR: %i=kd is bad: cannot lock dongle\n", kd);
4397 #ifndef CONFIG_EASYCAP_OSS
4398 if (0 != snd_card_free(peasycap->psnd_card)) {
4399 SAY("ERROR: snd_card_free() failed\n");
4401 peasycap->psnd_card = NULL;
4402 (peasycap->registered_audio)--;
4404 #else /* CONFIG_EASYCAP_OSS */
4405 usb_deregister_dev(pusb_interface, &easyoss_class);
4406 peasycap->registered_audio--;
4407 JOM(4, "intf[%i]: usb_deregister_dev()\n", bInterfaceNumber);
4408 SAM("easyoss detached from minor #%d\n", minor);
4409 #endif /* CONFIG_EASYCAP_OSS */
4410 if (0 <= kd && DONGLE_MANY > kd) {
4411 mutex_unlock(&easycapdc60_dongle[kd].mutex_audio);
4412 JOM(4, "unlocked dongle[%i].mutex_audio\n", kd);
4419 /*---------------------------------------------------------------------------*/
4421 * CALL easycap_delete() IF NO REMAINING REFERENCES TO peasycap
4422 * (ALSO WHEN ALSA HAS BEEN IN USE)
4424 /*---------------------------------------------------------------------------*/
4425 if (!peasycap->kref.refcount.counter) {
4426 SAM("ERROR: peasycap->kref.refcount.counter is zero "
4427 "so cannot call kref_put()\n");
4428 SAM("ending unsuccessfully: may cause memory leak\n");
4431 if (0 <= kd && DONGLE_MANY > kd) {
4432 JOM(4, "about to lock dongle[%i].mutex_video\n", kd);
4433 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].mutex_video)) {
4434 SAY("ERROR: cannot lock dongle[%i].mutex_video\n", kd);
4435 SAM("ending unsuccessfully: may cause memory leak\n");
4438 JOM(4, "locked dongle[%i].mutex_video\n", kd);
4439 JOM(4, "about to lock dongle[%i].mutex_audio\n", kd);
4440 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].mutex_audio)) {
4441 SAY("ERROR: cannot lock dongle[%i].mutex_audio\n", kd);
4442 mutex_unlock(&(easycapdc60_dongle[kd].mutex_video));
4443 JOM(4, "unlocked dongle[%i].mutex_video\n", kd);
4444 SAM("ending unsuccessfully: may cause memory leak\n");
4447 JOM(4, "locked dongle[%i].mutex_audio\n", kd);
4449 JOM(4, "intf[%i]: %i=peasycap->kref.refcount.counter\n",
4450 bInterfaceNumber, (int)peasycap->kref.refcount.counter);
4451 kref_put(&peasycap->kref, easycap_delete);
4452 JOT(4, "intf[%i]: kref_put() done.\n", bInterfaceNumber);
4453 if (0 <= kd && DONGLE_MANY > kd) {
4454 mutex_unlock(&(easycapdc60_dongle[kd].mutex_audio));
4455 JOT(4, "unlocked dongle[%i].mutex_audio\n", kd);
4456 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
4457 JOT(4, "unlocked dongle[%i].mutex_video\n", kd);
4459 /*---------------------------------------------------------------------------*/
4463 /*****************************************************************************/
4465 /*---------------------------------------------------------------------------*/
4467 * PARAMETERS APPLICABLE TO ENTIRE DRIVER, I.E. BOTH VIDEO AND AUDIO
4469 /*---------------------------------------------------------------------------*/
4470 static struct usb_device_id easycap_usb_device_id_table[] = {
4471 {USB_DEVICE(USB_EASYCAP_VENDOR_ID, USB_EASYCAP_PRODUCT_ID)},
4475 MODULE_DEVICE_TABLE(usb, easycap_usb_device_id_table);
4476 struct usb_driver easycap_usb_driver = {
4478 .id_table = easycap_usb_device_id_table,
4479 .probe = easycap_usb_probe,
4480 .disconnect = easycap_usb_disconnect,
4483 static int __init easycap_module_init(void)
4487 printk(KERN_INFO "Easycap version: "EASYCAP_DRIVER_VERSION "\n");
4489 JOT(4, "begins. %i=debug %i=bars %i=gain\n",
4490 easycap_debug, easycap_bars, easycap_gain);
4492 mutex_init(&mutex_dongle);
4493 for (k = 0; k < DONGLE_MANY; k++) {
4494 easycapdc60_dongle[k].peasycap = NULL;
4495 mutex_init(&easycapdc60_dongle[k].mutex_video);
4496 mutex_init(&easycapdc60_dongle[k].mutex_audio);
4498 rc = usb_register(&easycap_usb_driver);
4500 printk(KERN_ERR "Easycap: usb_register failed rc=%d\n", rc);
4504 /*****************************************************************************/
4505 static void __exit easycap_module_exit(void)
4507 usb_deregister(&easycap_usb_driver);
4509 /*****************************************************************************/
4511 module_init(easycap_module_init);
4512 module_exit(easycap_module_exit);
4514 /*****************************************************************************/