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 i, rc, 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 if (peasycap->ntsc) {
232 JOM(8, "... trying PAL ...\n"); ntsc = false;
234 JOM(8, "... trying NTSC ...\n"); ntsc = true;
236 rc = setup_stk(peasycap->pusb_device, ntsc);
238 JOM(4, "setup_stk() OK\n");
240 SAM("ERROR: setup_stk() rc = %i\n", rc);
243 rc = setup_saa(peasycap->pusb_device, ntsc);
245 JOM(4, "setup_saa() OK\n");
247 SAM("ERROR: setup_saa() rc = %i\n", rc);
250 rate = ready_saa(peasycap->pusb_device);
252 JOM(8, "not ready to capture after %i ms ...\n", PATIENCE);
253 JOM(8, "... saa register 0x1F has 0x%02X\n",
254 read_saa(peasycap->pusb_device, 0x1F));
255 ntsc = peasycap->ntsc;
257 JOM(8, "... success at second try: %i=rate\n", rate);
258 ntsc = (0 < (rate/2)) ? true : false ;
262 JOM(8, "... success at first try: %i=rate\n", rate);
263 ntsc = (0 < rate/2) ? true : false ;
265 JOM(8, "ntsc=%d\n", ntsc);
266 /*---------------------------------------------------------------------------*/
268 rc = setup_stk(peasycap->pusb_device, ntsc);
270 JOM(4, "setup_stk() OK\n");
272 SAM("ERROR: setup_stk() rc = %i\n", rc);
275 rc = setup_saa(peasycap->pusb_device, ntsc);
277 JOM(4, "setup_saa() OK\n");
279 SAM("ERROR: setup_saa() rc = %i\n", rc);
283 for (i = 0; i < 180; i++)
284 peasycap->merit[i] = 0;
286 peasycap->video_eof = 0;
287 peasycap->audio_eof = 0;
288 do_gettimeofday(&peasycap->timeval7);
289 /*---------------------------------------------------------------------------*/
291 * RESTORE INPUT AND FORCE REFRESH OF STANDARD, FORMAT, ETC.
293 * WHILE THIS PROCEDURE IS IN PROGRESS, SOME IOCTL COMMANDS WILL RETURN -EBUSY.
295 /*---------------------------------------------------------------------------*/
296 peasycap->input = -8192;
297 peasycap->standard_offset = -8192;
298 fmtidx = ntsc ? NTSC_M : PAL_BGHIN;
300 peasycap_standard = &easycap_standard[0];
301 while (0xFFFF != peasycap_standard->mask) {
302 if (fmtidx == peasycap_standard->v4l2_standard.index) {
303 peasycap->inputset[input].standard_offset =
304 peasycap_standard - easycap_standard;
309 if (0xFFFF == peasycap_standard->mask) {
310 SAM("ERROR: standard not found\n");
313 JOM(8, "%i=peasycap->inputset[%i].standard_offset\n",
314 peasycap->inputset[input].standard_offset, input);
316 peasycap->format_offset = -8192;
317 peasycap->brightness = -8192;
318 peasycap->contrast = -8192;
319 peasycap->saturation = -8192;
320 peasycap->hue = -8192;
322 rc = newinput(peasycap, input);
325 SAM("ERROR: newinput(.,%i) rc = %i\n", rc, input);
328 JOM(4, "restored input, standard and format\n");
330 JOM(8, "true=peasycap->ntsc %d\n", peasycap->ntsc);
332 if (0 > peasycap->input) {
333 SAM("MISTAKE: %i=peasycap->input\n", peasycap->input);
336 if (0 > peasycap->standard_offset) {
337 SAM("MISTAKE: %i=peasycap->standard_offset\n",
338 peasycap->standard_offset);
341 if (0 > peasycap->format_offset) {
342 SAM("MISTAKE: %i=peasycap->format_offset\n",
343 peasycap->format_offset);
346 if (0 > peasycap->brightness) {
347 SAM("MISTAKE: %i=peasycap->brightness\n",
348 peasycap->brightness);
351 if (0 > peasycap->contrast) {
352 SAM("MISTAKE: %i=peasycap->contrast\n", peasycap->contrast);
355 if (0 > peasycap->saturation) {
356 SAM("MISTAKE: %i=peasycap->saturation\n",
357 peasycap->saturation);
360 if (0 > peasycap->hue) {
361 SAM("MISTAKE: %i=peasycap->hue\n", peasycap->hue);
366 /*****************************************************************************/
367 /*---------------------------------------------------------------------------*/
369 * IF THE REQUESTED INPUT IS THE SAME AS THE EXISTING INPUT, DO NOTHING.
371 * KILL URBS, CLEAR FIELD AND FRAME BUFFERS AND RESET THEIR
372 * _read AND _fill POINTERS.
373 * SELECT THE NEW INPUT.
374 * ADJUST THE STANDARD, FORMAT, BRIGHTNESS, CONTRAST, SATURATION AND HUE
375 * ON THE BASIS OF INFORMATION IN STRUCTURE easycap.inputset[input].
376 * RESUBMIT THE URBS IF STREAMING WAS ALREADY IN PROGRESS.
379 * THIS ROUTINE MAY BE CALLED FREQUENTLY BY ZONEMINDER VIA IOCTL,
380 * SO IT SHOULD WRITE ONLY SPARINGLY TO THE LOGFILE.
382 /*---------------------------------------------------------------------------*/
384 newinput(struct easycap *peasycap, int input)
386 int rc, k, m, mood, off;
387 int inputnow, video_idlenow, audio_idlenow;
391 SAY("ERROR: peasycap is NULL\n");
394 JOM(8, "%i=input sought\n", input);
396 if (0 > input && INPUT_MANY <= input)
398 inputnow = peasycap->input;
399 if (input == inputnow)
401 /*---------------------------------------------------------------------------*/
403 * IF STREAMING IS IN PROGRESS THE URBS ARE KILLED AT THIS
404 * STAGE AND WILL BE RESUBMITTED PRIOR TO EXIT FROM THE ROUTINE.
405 * IF NO STREAMING IS IN PROGRESS NO URBS WILL BE SUBMITTED BY THE
408 /*---------------------------------------------------------------------------*/
409 video_idlenow = peasycap->video_idle;
410 audio_idlenow = peasycap->audio_idle;
412 peasycap->video_idle = 1;
413 peasycap->audio_idle = 1;
414 if (peasycap->video_isoc_streaming) {
416 kill_video_urbs(peasycap);
420 /*---------------------------------------------------------------------------*/
421 if (!peasycap->pusb_device) {
422 SAM("ERROR: peasycap->pusb_device is NULL\n");
425 rc = usb_set_interface(peasycap->pusb_device,
426 peasycap->video_interface,
427 peasycap->video_altsetting_off);
429 SAM("ERROR: usb_set_interface() rc = %i\n", rc);
432 rc = stop_100(peasycap->pusb_device);
434 SAM("ERROR: stop_100() rc = %i\n", rc);
437 for (k = 0; k < FIELD_BUFFER_MANY; k++) {
438 for (m = 0; m < FIELD_BUFFER_SIZE/PAGE_SIZE; m++)
439 memset(peasycap->field_buffer[k][m].pgo, 0, PAGE_SIZE);
441 for (k = 0; k < FRAME_BUFFER_MANY; k++) {
442 for (m = 0; m < FRAME_BUFFER_SIZE/PAGE_SIZE; m++)
443 memset(peasycap->frame_buffer[k][m].pgo, 0, PAGE_SIZE);
445 peasycap->field_page = 0;
446 peasycap->field_read = 0;
447 peasycap->field_fill = 0;
449 peasycap->frame_read = 0;
450 peasycap->frame_fill = 0;
451 for (k = 0; k < peasycap->input; k++) {
452 (peasycap->frame_fill)++;
453 if (peasycap->frame_buffer_many <= peasycap->frame_fill)
454 peasycap->frame_fill = 0;
456 peasycap->input = input;
457 select_input(peasycap->pusb_device, peasycap->input, 9);
458 /*---------------------------------------------------------------------------*/
459 if (input == peasycap->inputset[input].input) {
460 off = peasycap->inputset[input].standard_offset;
461 if (off != peasycap->standard_offset) {
462 rc = adjust_standard(peasycap,
463 easycap_standard[off].v4l2_standard.id);
465 SAM("ERROR: adjust_standard() rc = %i\n", rc);
468 JOM(8, "%i=peasycap->standard_offset\n",
469 peasycap->standard_offset);
471 JOM(8, "%i=peasycap->standard_offset unchanged\n",
472 peasycap->standard_offset);
474 off = peasycap->inputset[input].format_offset;
475 if (off != peasycap->format_offset) {
476 struct v4l2_pix_format *pix =
477 &easycap_format[off].v4l2_format.fmt.pix;
478 rc = adjust_format(peasycap,
479 pix->width, pix->height,
480 pix->pixelformat, pix->field, false);
482 SAM("ERROR: adjust_format() rc = %i\n", rc);
485 JOM(8, "%i=peasycap->format_offset\n",
486 peasycap->format_offset);
488 JOM(8, "%i=peasycap->format_offset unchanged\n",
489 peasycap->format_offset);
491 mood = peasycap->inputset[input].brightness;
492 if (mood != peasycap->brightness) {
493 rc = adjust_brightness(peasycap, mood);
495 SAM("ERROR: adjust_brightness rc = %i\n", rc);
498 JOM(8, "%i=peasycap->brightness\n",
499 peasycap->brightness);
501 mood = peasycap->inputset[input].contrast;
502 if (mood != peasycap->contrast) {
503 rc = adjust_contrast(peasycap, mood);
505 SAM("ERROR: adjust_contrast rc = %i\n", rc);
508 JOM(8, "%i=peasycap->contrast\n", peasycap->contrast);
510 mood = peasycap->inputset[input].saturation;
511 if (mood != peasycap->saturation) {
512 rc = adjust_saturation(peasycap, mood);
514 SAM("ERROR: adjust_saturation rc = %i\n", rc);
517 JOM(8, "%i=peasycap->saturation\n",
518 peasycap->saturation);
520 mood = peasycap->inputset[input].hue;
521 if (mood != peasycap->hue) {
522 rc = adjust_hue(peasycap, mood);
524 SAM("ERROR: adjust_hue rc = %i\n", rc);
527 JOM(8, "%i=peasycap->hue\n", peasycap->hue);
530 SAM("MISTAKE: easycap.inputset[%i] unpopulated\n", input);
533 /*---------------------------------------------------------------------------*/
534 if (!peasycap->pusb_device) {
535 SAM("ERROR: peasycap->pusb_device is NULL\n");
538 rc = usb_set_interface(peasycap->pusb_device,
539 peasycap->video_interface,
540 peasycap->video_altsetting_on);
542 SAM("ERROR: usb_set_interface() rc = %i\n", rc);
545 rc = start_100(peasycap->pusb_device);
547 SAM("ERROR: start_100() rc = %i\n", rc);
551 submit_video_urbs(peasycap);
553 peasycap->video_isoc_sequence = VIDEO_ISOC_BUFFER_MANY - 1;
554 peasycap->video_idle = video_idlenow;
555 peasycap->audio_idle = audio_idlenow;
556 peasycap->video_junk = 0;
560 /*****************************************************************************/
561 int submit_video_urbs(struct easycap *peasycap)
563 struct data_urb *pdata_urb;
565 struct list_head *plist_head;
566 int j, isbad, nospc, m, rc;
570 SAY("ERROR: peasycap is NULL\n");
574 if (!peasycap->purb_video_head) {
575 SAY("ERROR: peasycap->urb_video_head uninitialized\n");
578 if (!peasycap->pusb_device) {
579 SAY("ERROR: peasycap->pusb_device is NULL\n");
582 if (!peasycap->video_isoc_streaming) {
583 JOM(4, "submission of all video urbs\n");
584 isbad = 0; nospc = 0; m = 0;
585 list_for_each(plist_head, (peasycap->purb_video_head)) {
586 pdata_urb = list_entry(plist_head,
587 struct data_urb, list_head);
588 if (pdata_urb && pdata_urb->purb) {
589 purb = pdata_urb->purb;
590 isbuf = pdata_urb->isbuf;
592 purb->dev = peasycap->pusb_device;
594 usb_rcvisocpipe(peasycap->pusb_device,
595 peasycap->video_endpointnumber);
596 purb->transfer_flags = URB_ISO_ASAP;
597 purb->transfer_buffer =
598 peasycap->video_isoc_buffer[isbuf].pgo;
599 purb->transfer_buffer_length =
600 peasycap->video_isoc_buffer_size;
601 purb->complete = easycap_complete;
602 purb->context = peasycap;
603 purb->start_frame = 0;
604 purb->number_of_packets =
605 peasycap->video_isoc_framesperdesc;
607 for (j = 0; j < peasycap->video_isoc_framesperdesc; j++) {
608 purb->iso_frame_desc[j]. offset =
609 j * peasycap->video_isoc_maxframesize;
610 purb->iso_frame_desc[j]. length =
611 peasycap->video_isoc_maxframesize;
614 rc = usb_submit_urb(purb, GFP_KERNEL);
617 SAM("ERROR: usb_submit_urb() failed "
618 "for urb with rc:-%s\n",
630 SAM("-ENOSPC=usb_submit_urb() for %i urbs\n", nospc);
631 SAM("..... possibly inadequate USB bandwidth\n");
632 peasycap->video_eof = 1;
636 JOM(4, "attempting cleanup instead of submitting\n");
637 list_for_each(plist_head, (peasycap->purb_video_head)) {
638 pdata_urb = list_entry(plist_head,
639 struct data_urb, list_head);
641 purb = pdata_urb->purb;
646 peasycap->video_isoc_streaming = 0;
648 peasycap->video_isoc_streaming = 1;
649 JOM(4, "submitted %i video urbs\n", m);
652 JOM(4, "already streaming video urbs\n");
656 /*****************************************************************************/
657 int kill_video_urbs(struct easycap *peasycap)
660 struct list_head *plist_head;
661 struct data_urb *pdata_urb;
664 SAY("ERROR: peasycap is NULL\n");
667 if (!peasycap->video_isoc_streaming) {
668 JOM(8, "%i=video_isoc_streaming, no video urbs killed\n",
669 peasycap->video_isoc_streaming);
672 if (!peasycap->purb_video_head) {
673 SAM("ERROR: peasycap->purb_video_head is NULL\n");
677 peasycap->video_isoc_streaming = 0;
678 JOM(4, "killing video urbs\n");
680 list_for_each(plist_head, (peasycap->purb_video_head)) {
681 pdata_urb = list_entry(plist_head, struct data_urb, list_head);
682 if (pdata_urb && pdata_urb->purb) {
683 usb_kill_urb(pdata_urb->purb);
687 JOM(4, "%i video urbs killed\n", m);
691 /****************************************************************************/
692 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
693 /*--------------------------------------------------------------------------*/
694 static int easycap_open_noinode(struct file *file)
696 return easycap_open(NULL, file);
699 static int videodev_release(struct video_device *pvideo_device)
701 struct easycap *peasycap;
703 peasycap = video_get_drvdata(pvideo_device);
705 SAY("ERROR: peasycap is NULL\n");
706 SAY("ending unsuccessfully\n");
709 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
710 SAY("ERROR: bad peasycap: %p\n", peasycap);
713 if (0 != kill_video_urbs(peasycap)) {
714 SAM("ERROR: kill_video_urbs() failed\n");
717 JOM(4, "ending successfully\n");
720 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
721 /*****************************************************************************/
722 /*--------------------------------------------------------------------------*/
724 * THIS FUNCTION IS CALLED FROM WITHIN easycap_usb_disconnect() AND IS
725 * PROTECTED BY SEMAPHORES SET AND CLEARED BY easycap_usb_disconnect().
727 * BY THIS STAGE THE DEVICE HAS ALREADY BEEN PHYSICALLY UNPLUGGED, SO
728 * peasycap->pusb_device IS NO LONGER VALID.
730 /*---------------------------------------------------------------------------*/
731 static void easycap_delete(struct kref *pkref)
733 struct easycap *peasycap;
734 struct data_urb *pdata_urb;
735 struct list_head *plist_head, *plist_next;
737 int allocation_video_urb;
738 int allocation_video_page;
739 int allocation_video_struct;
740 int allocation_audio_urb;
741 int allocation_audio_page;
742 int allocation_audio_struct;
743 int registered_video, registered_audio;
745 peasycap = container_of(pkref, struct easycap, kref);
747 SAM("ERROR: peasycap is NULL: cannot perform deletions\n");
750 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
751 SAY("ERROR: bad peasycap: %p\n", peasycap);
754 kd = isdongle(peasycap);
755 /*---------------------------------------------------------------------------*/
759 /*---------------------------------------------------------------------------*/
760 if (peasycap->purb_video_head) {
761 JOM(4, "freeing video urbs\n");
763 list_for_each(plist_head, (peasycap->purb_video_head)) {
764 pdata_urb = list_entry(plist_head,
765 struct data_urb, list_head);
767 JOM(4, "ERROR: pdata_urb is NULL\n");
769 if (pdata_urb->purb) {
770 usb_free_urb(pdata_urb->purb);
771 pdata_urb->purb = NULL;
772 peasycap->allocation_video_urb -= 1;
778 JOM(4, "%i video urbs freed\n", m);
779 /*---------------------------------------------------------------------------*/
780 JOM(4, "freeing video data_urb structures.\n");
782 list_for_each_safe(plist_head, plist_next,
783 peasycap->purb_video_head) {
784 pdata_urb = list_entry(plist_head,
785 struct data_urb, list_head);
787 peasycap->allocation_video_struct -=
788 sizeof(struct data_urb);
794 JOM(4, "%i video data_urb structures freed\n", m);
795 JOM(4, "setting peasycap->purb_video_head=NULL\n");
796 peasycap->purb_video_head = NULL;
798 /*---------------------------------------------------------------------------*/
799 JOM(4, "freeing video isoc buffers.\n");
801 for (k = 0; k < VIDEO_ISOC_BUFFER_MANY; k++) {
802 if (peasycap->video_isoc_buffer[k].pgo) {
803 free_pages((unsigned long)
804 peasycap->video_isoc_buffer[k].pgo,
806 peasycap->video_isoc_buffer[k].pgo = NULL;
807 peasycap->allocation_video_page -=
808 BIT(VIDEO_ISOC_ORDER);
812 JOM(4, "isoc video buffers freed: %i pages\n",
813 m * (0x01 << VIDEO_ISOC_ORDER));
814 /*---------------------------------------------------------------------------*/
815 JOM(4, "freeing video field buffers.\n");
817 for (k = 0; k < FIELD_BUFFER_MANY; k++) {
818 for (m = 0; m < FIELD_BUFFER_SIZE/PAGE_SIZE; m++) {
819 if (peasycap->field_buffer[k][m].pgo) {
820 free_page((unsigned long)
821 peasycap->field_buffer[k][m].pgo);
822 peasycap->field_buffer[k][m].pgo = NULL;
823 peasycap->allocation_video_page -= 1;
828 JOM(4, "video field buffers freed: %i pages\n", gone);
829 /*---------------------------------------------------------------------------*/
830 JOM(4, "freeing video frame buffers.\n");
832 for (k = 0; k < FRAME_BUFFER_MANY; k++) {
833 for (m = 0; m < FRAME_BUFFER_SIZE/PAGE_SIZE; m++) {
834 if (peasycap->frame_buffer[k][m].pgo) {
835 free_page((unsigned long)
836 peasycap->frame_buffer[k][m].pgo);
837 peasycap->frame_buffer[k][m].pgo = NULL;
838 peasycap->allocation_video_page -= 1;
843 JOM(4, "video frame buffers freed: %i pages\n", gone);
844 /*---------------------------------------------------------------------------*/
848 /*---------------------------------------------------------------------------*/
849 if (peasycap->purb_audio_head) {
850 JOM(4, "freeing audio urbs\n");
852 list_for_each(plist_head, (peasycap->purb_audio_head)) {
853 pdata_urb = list_entry(plist_head,
854 struct data_urb, list_head);
856 JOM(4, "ERROR: pdata_urb is NULL\n");
858 if (pdata_urb->purb) {
859 usb_free_urb(pdata_urb->purb);
860 pdata_urb->purb = NULL;
861 peasycap->allocation_audio_urb -= 1;
866 JOM(4, "%i audio urbs freed\n", m);
867 /*---------------------------------------------------------------------------*/
868 JOM(4, "freeing audio data_urb structures.\n");
870 list_for_each_safe(plist_head, plist_next,
871 peasycap->purb_audio_head) {
872 pdata_urb = list_entry(plist_head,
873 struct data_urb, list_head);
875 peasycap->allocation_audio_struct -=
876 sizeof(struct data_urb);
882 JOM(4, "%i audio data_urb structures freed\n", m);
883 JOM(4, "setting peasycap->purb_audio_head=NULL\n");
884 peasycap->purb_audio_head = NULL;
886 /*---------------------------------------------------------------------------*/
887 JOM(4, "freeing audio isoc buffers.\n");
889 for (k = 0; k < AUDIO_ISOC_BUFFER_MANY; k++) {
890 if (peasycap->audio_isoc_buffer[k].pgo) {
891 free_pages((unsigned long)
892 (peasycap->audio_isoc_buffer[k].pgo),
894 peasycap->audio_isoc_buffer[k].pgo = NULL;
895 peasycap->allocation_audio_page -=
896 BIT(AUDIO_ISOC_ORDER);
900 JOM(4, "easyoss_delete(): isoc audio buffers freed: %i pages\n",
901 m * (0x01 << AUDIO_ISOC_ORDER));
902 /*---------------------------------------------------------------------------*/
903 #ifdef CONFIG_EASYCAP_OSS
904 JOM(4, "freeing audio buffers.\n");
906 for (k = 0; k < peasycap->audio_buffer_page_many; k++) {
907 if (peasycap->audio_buffer[k].pgo) {
908 free_page((unsigned long)peasycap->audio_buffer[k].pgo);
909 peasycap->audio_buffer[k].pgo = NULL;
910 peasycap->allocation_audio_page -= 1;
914 JOM(4, "easyoss_delete(): audio buffers freed: %i pages\n", gone);
915 #endif /* CONFIG_EASYCAP_OSS */
916 /*---------------------------------------------------------------------------*/
917 JOM(4, "freeing easycap structure.\n");
918 allocation_video_urb = peasycap->allocation_video_urb;
919 allocation_video_page = peasycap->allocation_video_page;
920 allocation_video_struct = peasycap->allocation_video_struct;
921 registered_video = peasycap->registered_video;
922 allocation_audio_urb = peasycap->allocation_audio_urb;
923 allocation_audio_page = peasycap->allocation_audio_page;
924 allocation_audio_struct = peasycap->allocation_audio_struct;
925 registered_audio = peasycap->registered_audio;
929 if (0 <= kd && DONGLE_MANY > kd) {
930 if (mutex_lock_interruptible(&mutex_dongle)) {
931 SAY("ERROR: cannot down mutex_dongle\n");
933 JOM(4, "locked mutex_dongle\n");
934 easycapdc60_dongle[kd].peasycap = NULL;
935 mutex_unlock(&mutex_dongle);
936 JOM(4, "unlocked mutex_dongle\n");
937 JOT(4, " null-->dongle[%i].peasycap\n", kd);
938 allocation_video_struct -= sizeof(struct easycap);
941 SAY("ERROR: cannot purge dongle[].peasycap");
943 /*---------------------------------------------------------------------------*/
944 SAY("%8i=video urbs after all deletions\n", allocation_video_urb);
945 SAY("%8i=video pages after all deletions\n", allocation_video_page);
946 SAY("%8i=video structs after all deletions\n", allocation_video_struct);
947 SAY("%8i=video devices after all deletions\n", registered_video);
948 SAY("%8i=audio urbs after all deletions\n", allocation_audio_urb);
949 SAY("%8i=audio pages after all deletions\n", allocation_audio_page);
950 SAY("%8i=audio structs after all deletions\n", allocation_audio_struct);
951 SAY("%8i=audio devices after all deletions\n", registered_audio);
956 /*****************************************************************************/
957 static unsigned int easycap_poll(struct file *file, poll_table *wait)
959 struct easycap *peasycap;
964 if (NULL == ((poll_table *)wait))
965 JOT(8, "WARNING: poll table pointer is NULL ... continuing\n");
967 SAY("ERROR: file pointer is NULL\n");
970 peasycap = file->private_data;
972 SAY("ERROR: peasycap is NULL\n");
975 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
976 SAY("ERROR: bad peasycap: %p\n", peasycap);
979 if (!peasycap->pusb_device) {
980 SAY("ERROR: peasycap->pusb_device is NULL\n");
983 /*---------------------------------------------------------------------------*/
984 kd = isdongle(peasycap);
985 if (0 <= kd && DONGLE_MANY > kd) {
986 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].mutex_video)) {
987 SAY("ERROR: cannot down dongle[%i].mutex_video\n", kd);
990 JOM(4, "locked dongle[%i].mutex_video\n", kd);
992 * MEANWHILE, easycap_usb_disconnect() MAY HAVE FREED POINTER
993 * peasycap, IN WHICH CASE A REPEAT CALL TO isdongle() WILL FAIL.
994 * IF NECESSARY, BAIL OUT.
996 if (kd != isdongle(peasycap))
999 SAY("ERROR: file is NULL\n");
1000 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
1001 return -ERESTARTSYS;
1003 peasycap = file->private_data;
1005 SAY("ERROR: peasycap is NULL\n");
1006 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
1007 return -ERESTARTSYS;
1009 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
1010 SAY("ERROR: bad peasycap: %p\n", peasycap);
1011 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
1012 return -ERESTARTSYS;
1014 if (!peasycap->pusb_device) {
1015 SAM("ERROR: peasycap->pusb_device is NULL\n");
1016 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
1017 return -ERESTARTSYS;
1021 * IF easycap_usb_disconnect() HAS ALREADY FREED POINTER peasycap
1022 * BEFORE THE ATTEMPT TO ACQUIRE THE SEMAPHORE, isdongle() WILL
1023 * HAVE FAILED. BAIL OUT.
1025 return -ERESTARTSYS;
1026 /*---------------------------------------------------------------------------*/
1027 rc = easycap_dqbuf(peasycap, 0);
1028 peasycap->polled = 1;
1029 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
1031 return POLLIN | POLLRDNORM;
1035 /*****************************************************************************/
1036 /*---------------------------------------------------------------------------*/
1038 * IF mode IS NONZERO THIS ROUTINE RETURNS -EAGAIN RATHER THAN BLOCKING.
1040 /*---------------------------------------------------------------------------*/
1041 int easycap_dqbuf(struct easycap *peasycap, int mode)
1043 int input, ifield, miss, rc;
1047 SAY("ERROR: peasycap is NULL\n");
1050 if (!peasycap->pusb_device) {
1051 SAY("ERROR: peasycap->pusb_device is NULL\n");
1055 JOM(8, "%i=ifield\n", ifield);
1056 /*---------------------------------------------------------------------------*/
1058 * CHECK FOR LOST INPUT SIGNAL.
1060 * FOR THE FOUR-CVBS EasyCAP, THIS DOES NOT WORK AS EXPECTED.
1061 * IF INPUT 0 IS PRESENT AND SYNC ACQUIRED, UNPLUGGING INPUT 4 DOES NOT
1062 * RESULT IN SETTING BIT 0x40 ON REGISTER 0x1F, PRESUMABLY BECAUSE THERE
1063 * IS FLYWHEELING ON INPUT 0. THE UPSHOT IS:
1065 * INPUT 0 PLUGGED, INPUT 4 PLUGGED => SCREEN 0 OK, SCREEN 4 OK
1066 * INPUT 0 PLUGGED, INPUT 4 UNPLUGGED => SCREEN 0 OK, SCREEN 4 BLACK
1067 * INPUT 0 UNPLUGGED, INPUT 4 PLUGGED => SCREEN 0 BARS, SCREEN 4 OK
1068 * INPUT 0 UNPLUGGED, INPUT 4 UNPLUGGED => SCREEN 0 BARS, SCREEN 4 BARS
1070 /*---------------------------------------------------------------------------*/
1071 input = peasycap->input;
1072 if (0 <= input && INPUT_MANY > input) {
1073 rc = read_saa(peasycap->pusb_device, 0x1F);
1076 peasycap->lost[input] += 1;
1078 peasycap->lost[input] -= 2;
1080 if (0 > peasycap->lost[input])
1081 peasycap->lost[input] = 0;
1082 else if ((2 * VIDEO_LOST_TOLERATE) < peasycap->lost[input])
1083 peasycap->lost[input] = (2 * VIDEO_LOST_TOLERATE);
1086 /*---------------------------------------------------------------------------*/
1088 * WAIT FOR FIELD ifield (0 => TOP, 1 => BOTTOM)
1090 /*---------------------------------------------------------------------------*/
1092 while ((peasycap->field_read == peasycap->field_fill) ||
1093 (0 != (0xFF00 & peasycap->field_buffer
1094 [peasycap->field_read][0].kount)) ||
1095 (ifield != (0x00FF & peasycap->field_buffer
1096 [peasycap->field_read][0].kount))) {
1100 JOM(8, "first wait on wq_video, %i=field_read %i=field_fill\n",
1101 peasycap->field_read, peasycap->field_fill);
1103 if (0 != (wait_event_interruptible(peasycap->wq_video,
1104 (peasycap->video_idle || peasycap->video_eof ||
1105 ((peasycap->field_read != peasycap->field_fill) &&
1106 (0 == (0xFF00 & peasycap->field_buffer[peasycap->field_read][0].kount)) &&
1107 (ifield == (0x00FF & peasycap->field_buffer[peasycap->field_read][0].kount))))))) {
1108 SAM("aborted by signal\n");
1111 if (peasycap->video_idle) {
1112 JOM(8, "%i=peasycap->video_idle returning -EAGAIN\n",
1113 peasycap->video_idle);
1116 if (peasycap->video_eof) {
1117 JOM(8, "%i=peasycap->video_eof\n", peasycap->video_eof);
1118 #if defined(PERSEVERE)
1119 if (1 == peasycap->status) {
1120 JOM(8, "persevering ...\n");
1121 peasycap->video_eof = 0;
1122 peasycap->audio_eof = 0;
1123 if (0 != reset(peasycap)) {
1124 JOM(8, " ... failed returning -EIO\n");
1125 peasycap->video_eof = 1;
1126 peasycap->audio_eof = 1;
1127 kill_video_urbs(peasycap);
1130 peasycap->status = 0;
1131 JOM(8, " ... OK returning -EAGAIN\n");
1134 #endif /*PERSEVERE*/
1135 peasycap->video_eof = 1;
1136 peasycap->audio_eof = 1;
1137 kill_video_urbs(peasycap);
1138 JOM(8, "returning -EIO\n");
1143 JOM(8, "first awakening on wq_video after %i waits\n", miss);
1145 rc = field2frame(peasycap);
1147 SAM("ERROR: field2frame() rc = %i\n", rc);
1148 /*---------------------------------------------------------------------------*/
1150 * WAIT FOR THE OTHER FIELD
1152 /*---------------------------------------------------------------------------*/
1158 while ((peasycap->field_read == peasycap->field_fill) ||
1159 (0 != (0xFF00 & peasycap->field_buffer[peasycap->field_read][0].kount)) ||
1160 (ifield != (0x00FF & peasycap->field_buffer[peasycap->field_read][0].kount))) {
1164 JOM(8, "second wait on wq_video %i=field_read %i=field_fill\n",
1165 peasycap->field_read, peasycap->field_fill);
1166 if (0 != (wait_event_interruptible(peasycap->wq_video,
1167 (peasycap->video_idle || peasycap->video_eof ||
1168 ((peasycap->field_read != peasycap->field_fill) &&
1169 (0 == (0xFF00 & peasycap->field_buffer[peasycap->field_read][0].kount)) &&
1170 (ifield == (0x00FF & peasycap->field_buffer[peasycap->field_read][0].kount))))))) {
1171 SAM("aborted by signal\n");
1174 if (peasycap->video_idle) {
1175 JOM(8, "%i=peasycap->video_idle returning -EAGAIN\n",
1176 peasycap->video_idle);
1179 if (peasycap->video_eof) {
1180 JOM(8, "%i=peasycap->video_eof\n", peasycap->video_eof);
1181 #if defined(PERSEVERE)
1182 if (1 == peasycap->status) {
1183 JOM(8, "persevering ...\n");
1184 peasycap->video_eof = 0;
1185 peasycap->audio_eof = 0;
1186 if (0 != reset(peasycap)) {
1187 JOM(8, " ... failed returning -EIO\n");
1188 peasycap->video_eof = 1;
1189 peasycap->audio_eof = 1;
1190 kill_video_urbs(peasycap);
1193 peasycap->status = 0;
1194 JOM(8, " ... OK ... returning -EAGAIN\n");
1197 #endif /*PERSEVERE*/
1198 peasycap->video_eof = 1;
1199 peasycap->audio_eof = 1;
1200 kill_video_urbs(peasycap);
1201 JOM(8, "returning -EIO\n");
1206 JOM(8, "second awakening on wq_video after %i waits\n", miss);
1208 rc = field2frame(peasycap);
1210 SAM("ERROR: field2frame() rc = %i\n", rc);
1211 /*---------------------------------------------------------------------------*/
1215 /*---------------------------------------------------------------------------*/
1216 if (peasycap->skip) {
1217 peasycap->skipped++;
1218 if (peasycap->skip != peasycap->skipped)
1219 return peasycap->skip - peasycap->skipped;
1221 peasycap->skipped = 0;
1223 /*---------------------------------------------------------------------------*/
1224 peasycap->frame_read = peasycap->frame_fill;
1225 peasycap->queued[peasycap->frame_read] = 0;
1226 peasycap->done[peasycap->frame_read] = V4L2_BUF_FLAG_DONE;
1228 peasycap->frame_fill++;
1229 if (peasycap->frame_buffer_many <= peasycap->frame_fill)
1230 peasycap->frame_fill = 0;
1232 if (0x01 & easycap_standard[peasycap->standard_offset].mask)
1233 peasycap->frame_buffer[peasycap->frame_read][0].kount =
1236 peasycap->frame_buffer[peasycap->frame_read][0].kount =
1240 JOM(8, "setting: %i=peasycap->frame_read\n", peasycap->frame_read);
1241 JOM(8, "bumped to: %i=peasycap->frame_fill\n", peasycap->frame_fill);
1245 /*****************************************************************************/
1246 /*---------------------------------------------------------------------------*/
1248 * BY DEFINITION, odd IS true FOR THE FIELD OCCUPYING LINES 1,3,5,...,479
1249 * odd IS false FOR THE FIELD OCCUPYING LINES 0,2,4,...,478
1251 * WHEN BOOLEAN PARAMETER decimatepixel IS true, ONLY THE FIELD FOR WHICH
1252 * odd==false IS TRANSFERRED TO THE FRAME BUFFER.
1254 * THE BOOLEAN PARAMETER offerfields IS true ONLY WHEN THE USER PROGRAM
1255 * CHOOSES THE OPTION V4L2_FIELD_INTERLACED.
1257 /*---------------------------------------------------------------------------*/
1259 field2frame(struct easycap *peasycap)
1261 struct timeval timeval;
1262 long long int above, below;
1264 struct signed_div_result sdr;
1267 int kex, kad, mex, mad, rex, rad, rad2;
1268 int c2, c3, w2, w3, cz, wz;
1269 int rc, bytesperpixel, multiplier;
1270 int much, more, over, rump, caches, input;
1272 bool odd, isuy, decimatepixel, offerfields, badinput;
1275 SAY("ERROR: peasycap is NULL\n");
1280 input = 0x07 & peasycap->field_buffer[peasycap->field_read][0].input;
1282 JOM(8, "===== parity %i, input 0x%02X, field buffer %i --> "
1283 "frame buffer %i\n",
1284 peasycap->field_buffer[peasycap->field_read][0].kount,
1285 peasycap->field_buffer[peasycap->field_read][0].input,
1286 peasycap->field_read, peasycap->frame_fill);
1287 JOM(8, "===== %i=bytesperpixel\n", peasycap->bytesperpixel);
1288 if (peasycap->offerfields)
1289 JOM(8, "===== offerfields\n");
1291 /*---------------------------------------------------------------------------*/
1293 * REJECT OR CLEAN BAD FIELDS
1295 /*---------------------------------------------------------------------------*/
1296 if (peasycap->field_read == peasycap->field_fill) {
1297 SAM("ERROR: on entry, still filling field buffer %i\n",
1298 peasycap->field_read);
1301 #ifdef EASYCAP_TESTCARD
1302 easycap_testcard(peasycap, peasycap->field_read);
1304 if (0 <= input && INPUT_MANY > input) {
1305 if (easycap_bars && VIDEO_LOST_TOLERATE <= peasycap->lost[input])
1306 easycap_testcard(peasycap, peasycap->field_read);
1308 #endif /*EASYCAP_TESTCARD*/
1309 /*---------------------------------------------------------------------------*/
1311 offerfields = peasycap->offerfields;
1312 bytesperpixel = peasycap->bytesperpixel;
1313 decimatepixel = peasycap->decimatepixel;
1315 if ((2 != bytesperpixel) &&
1316 (3 != bytesperpixel) &&
1317 (4 != bytesperpixel)) {
1318 SAM("MISTAKE: %i=bytesperpixel\n", bytesperpixel);
1326 w2 = 2 * multiplier * (peasycap->width);
1327 w3 = bytesperpixel * multiplier * (peasycap->width);
1328 wz = multiplier * (peasycap->height) *
1329 multiplier * (peasycap->width);
1331 kex = peasycap->field_read; mex = 0;
1332 kad = peasycap->frame_fill; mad = 0;
1334 pex = peasycap->field_buffer[kex][0].pgo; rex = PAGE_SIZE;
1335 pad = peasycap->frame_buffer[kad][0].pgo; rad = PAGE_SIZE;
1336 odd = !!(peasycap->field_buffer[kex][0].kount);
1338 if (odd && (!decimatepixel)) {
1339 JOM(8, "initial skipping %4i bytes p.%4i\n",
1340 w3/multiplier, mad);
1341 pad += (w3 / multiplier); rad -= (w3 / multiplier);
1344 mask = 0; rump = 0; caches = 0;
1349 * PROCESS ONE LINE OF FRAME AT FULL RESOLUTION:
1350 * READ w2 BYTES FROM FIELD BUFFER,
1351 * WRITE w3 BYTES TO FRAME BUFFER
1353 if (!decimatepixel) {
1356 much = over; more = 0;
1357 margin = 0; mask = 0x00;
1363 SAM("MISTAKE: much is odd\n");
1367 more = (bytesperpixel *
1369 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1370 if (1 < bytesperpixel) {
1371 if (rad * 2 < much * bytesperpixel) {
1373 * INJUDICIOUS ALTERATION OF
1374 * THIS STATEMENT BLOCK WILL
1375 * CAUSE BREAKAGE. BEWARE.
1377 rad2 = rad + bytesperpixel - 1;
1378 much = ((((2 * rad2)/bytesperpixel)/2) * 2);
1379 rump = ((bytesperpixel * much) / 2) - rad;
1386 if ((mex + 1) < FIELD_BUFFER_SIZE / PAGE_SIZE)
1387 margin = *((u8 *)(peasycap->field_buffer[kex][mex + 1].pgo));
1392 SAM("MISTAKE: %i=bytesperpixel\n",
1399 JOM(8, "ERROR: 0x%02X=->field_buffer"
1401 "0x%02X=(0x08|->input)\n",
1402 peasycap->field_buffer
1403 [kex][mex].input, kex, mex,
1404 (0x08|peasycap->input));
1406 rc = redaub(peasycap, pad, pex, much, more,
1407 mask, margin, isuy);
1409 SAM("ERROR: redaub() failed\n");
1415 over -= much; cz += much;
1416 pex += much; rex -= much;
1419 pex = peasycap->field_buffer[kex][mex].pgo;
1421 if (peasycap->field_buffer[kex][mex].input != (0x08|peasycap->input))
1428 pad = peasycap->frame_buffer[kad][mad].pgo;
1436 /*---------------------------------------------------------------------------*/
1438 * SKIP w3 BYTES IN TARGET FRAME BUFFER,
1439 * UNLESS IT IS THE LAST LINE OF AN ODD FRAME
1441 /*---------------------------------------------------------------------------*/
1442 if (!odd || (cz != wz)) {
1447 pad = peasycap->frame_buffer
1459 /*---------------------------------------------------------------------------*/
1461 * PROCESS ONE LINE OF FRAME AT REDUCED RESOLUTION:
1462 * ONLY IF false==odd,
1463 * READ w2 BYTES FROM FIELD BUFFER,
1464 * WRITE w3 / 2 BYTES TO FRAME BUFFER
1466 /*---------------------------------------------------------------------------*/
1470 much = over; more = 0; margin = 0; mask = 0x00;
1476 SAM("MISTAKE: much is odd\n");
1480 more = (bytesperpixel * much) / 4;
1481 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1482 if (1 < bytesperpixel) {
1483 if (rad * 4 < much * bytesperpixel) {
1485 * INJUDICIOUS ALTERATION OF
1486 * THIS STATEMENT BLOCK
1487 * WILL CAUSE BREAKAGE.
1490 rad2 = rad + bytesperpixel - 1;
1491 much = ((((2 * rad2) / bytesperpixel) / 2) * 4);
1492 rump = ((bytesperpixel * much) / 4) - rad;
1499 if ((mex + 1) < FIELD_BUFFER_SIZE / PAGE_SIZE)
1500 margin = *((u8 *)(peasycap->field_buffer[kex][mex + 1].pgo));
1504 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1506 SAM("MISTAKE: %i=bytesperpixel\n",
1510 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1515 JOM(8, "ERROR: 0x%02X=->field_buffer"
1517 "0x%02X=(0x08|->input)\n",
1518 peasycap->field_buffer
1519 [kex][mex].input, kex, mex,
1520 (0x08|peasycap->input));
1522 rc = redaub(peasycap, pad, pex, much, more,
1523 mask, margin, isuy);
1525 SAM("ERROR: redaub() failed\n");
1528 over -= much; cz += much;
1529 pex += much; rex -= much;
1532 pex = peasycap->field_buffer[kex][mex].pgo;
1534 if (peasycap->field_buffer[kex][mex].input !=
1535 (0x08|peasycap->input))
1542 pad = peasycap->frame_buffer[kad][mad].pgo;
1550 /*---------------------------------------------------------------------------*/
1553 * READ w2 BYTES FROM FIELD BUFFER AND DISCARD THEM
1555 /*---------------------------------------------------------------------------*/
1561 pex = peasycap->field_buffer[kex][mex].pgo;
1563 if (peasycap->field_buffer[kex][mex].input !=
1564 (0x08|peasycap->input)) {
1565 JOM(8, "ERROR: 0x%02X=->field_buffer"
1567 "0x%02X=(0x08|->input)\n",
1568 peasycap->field_buffer
1569 [kex][mex].input, kex, mex,
1570 (0x08|peasycap->input));
1584 /*---------------------------------------------------------------------------*/
1588 /*---------------------------------------------------------------------------*/
1589 c2 = (mex + 1)*PAGE_SIZE - rex;
1591 SAM("ERROR: discrepancy %i in bytes read\n", c2 - cz);
1592 c3 = (mad + 1)*PAGE_SIZE - rad;
1594 if (!decimatepixel) {
1595 if (bytesperpixel * cz != c3)
1596 SAM("ERROR: discrepancy %i in bytes written\n",
1597 c3 - (bytesperpixel * cz));
1602 SAM("ERROR: discrepancy %i in bytes written\n",
1603 (2*c3)-(bytesperpixel * cz));
1606 SAM("ERROR: discrepancy %i "
1607 "in bytes written\n", c3);
1611 SAM("WORRY: undischarged cache at end of line in frame buffer\n");
1613 JOM(8, "===== field2frame(): %i bytes --> %i bytes (incl skip)\n", c2, c3);
1614 JOM(8, "===== field2frame(): %i=mad %i=rad\n", mad, rad);
1617 JOM(8, "+++++ field2frame(): frame buffer %i is full\n", kad);
1619 if (peasycap->field_read == peasycap->field_fill)
1620 SAM("WARNING: on exit, filling field buffer %i\n",
1621 peasycap->field_read);
1622 /*---------------------------------------------------------------------------*/
1624 * CALCULATE VIDEO STREAMING RATE
1626 /*---------------------------------------------------------------------------*/
1627 do_gettimeofday(&timeval);
1628 if (peasycap->timeval6.tv_sec) {
1629 below = ((long long int)(1000000)) *
1630 ((long long int)(timeval.tv_sec -
1631 peasycap->timeval6.tv_sec)) +
1632 (long long int)(timeval.tv_usec - peasycap->timeval6.tv_usec);
1633 above = (long long int)1000000;
1635 sdr = signed_div(above, below);
1636 above = sdr.quotient;
1637 remainder = (u32)sdr.remainder;
1639 JOM(8, "video streaming at %3lli.%03i fields per second\n",
1640 above, (remainder/1000));
1642 peasycap->timeval6 = timeval;
1645 JOM(8, "%i=caches\n", caches);
1648 /*****************************************************************************/
1649 struct signed_div_result
1650 signed_div(long long int above, long long int below)
1652 struct signed_div_result sdr;
1654 if (((0 <= above) && (0 <= below)) || ((0 > above) && (0 > below))) {
1655 sdr.remainder = (unsigned long long int) do_div(above, below);
1656 sdr.quotient = (long long int) above;
1662 sdr.remainder = (unsigned long long int) do_div(above, below);
1663 sdr.quotient = -((long long int) above);
1667 /*****************************************************************************/
1668 /*---------------------------------------------------------------------------*/
1670 * DECIMATION AND COLOURSPACE CONVERSION.
1672 * THIS ROUTINE REQUIRES THAT ALL THE DATA TO BE READ RESIDES ON ONE PAGE
1673 * AND THAT ALL THE DATA TO BE WRITTEN RESIDES ON ONE (DIFFERENT) PAGE.
1674 * THE CALLING ROUTINE MUST ENSURE THAT THIS REQUIREMENT IS MET, AND MUST
1675 * ALSO ENSURE THAT much IS EVEN.
1677 * much BYTES ARE READ, AT LEAST (bytesperpixel * much)/2 BYTES ARE WRITTEN
1678 * IF THERE IS NO DECIMATION, HALF THIS AMOUNT IF THERE IS DECIMATION.
1680 * mask IS ZERO WHEN NO SPECIAL BEHAVIOUR REQUIRED. OTHERWISE IT IS SET THUS:
1681 * 0x03 & mask = number of bytes to be written to cache instead of to
1683 * 0x04 & mask => use argument margin to set the chrominance for last pixel
1684 * 0x08 & mask => do not set the chrominance for last pixel
1686 * YUV to RGB CONVERSION IS (OR SHOULD BE) ITU-R BT 601.
1688 * THERE IS A LOT OF CODE REPETITION IN THIS ROUTINE IN ORDER TO AVOID
1689 * INEFFICIENT SWITCHING INSIDE INNER LOOPS. REARRANGING THE LOGIC TO
1690 * REDUCE CODE LENGTH WILL GENERALLY IMPAIR RUNTIME PERFORMANCE. BEWARE.
1692 /*---------------------------------------------------------------------------*/
1694 redaub(struct easycap *peasycap, void *pad, void *pex, int much, int more,
1695 u8 mask, u8 margin, bool isuy)
1697 static s32 ay[256], bu[256], rv[256], gu[256], gv[256];
1699 u8 r, g, b, y, u, v, c, *p2, *p3, *pz, *pr;
1701 bool byteswaporder, decimatepixel, last;
1706 SAM("MISTAKE: much is odd\n");
1709 bytesperpixel = peasycap->bytesperpixel;
1710 byteswaporder = peasycap->byteswaporder;
1711 decimatepixel = peasycap->decimatepixel;
1713 /*---------------------------------------------------------------------------*/
1715 for (j = 0; j < 112; j++) {
1716 tmp = (0xFF00 & (453 * j)) >> 8;
1717 bu[j + 128] = tmp; bu[127 - j] = -tmp;
1718 tmp = (0xFF00 & (359 * j)) >> 8;
1719 rv[j + 128] = tmp; rv[127 - j] = -tmp;
1720 tmp = (0xFF00 & (88 * j)) >> 8;
1721 gu[j + 128] = tmp; gu[127 - j] = -tmp;
1722 tmp = (0xFF00 & (183 * j)) >> 8;
1723 gv[j + 128] = tmp; gv[127 - j] = -tmp;
1725 for (j = 0; j < 16; j++) {
1726 bu[j] = bu[16]; rv[j] = rv[16];
1727 gu[j] = gu[16]; gv[j] = gv[16];
1729 for (j = 240; j < 256; j++) {
1730 bu[j] = bu[239]; rv[j] = rv[239];
1731 gu[j] = gu[239]; gv[j] = gv[239];
1733 for (j = 16; j < 236; j++)
1735 for (j = 0; j < 16; j++)
1737 for (j = 236; j < 256; j++)
1739 JOM(8, "lookup tables are prepared\n");
1741 pcache = peasycap->pcache;
1743 pcache = &peasycap->cache[0];
1744 /*---------------------------------------------------------------------------*/
1746 * TRANSFER CONTENTS OF CACHE TO THE FRAME BUFFER
1748 /*---------------------------------------------------------------------------*/
1750 SAM("MISTAKE: pcache is NULL\n");
1754 if (pcache != &peasycap->cache[0])
1755 JOM(16, "cache has %i bytes\n", (int)(pcache - &peasycap->cache[0]));
1756 p2 = &peasycap->cache[0];
1757 p3 = (u8 *)pad - (int)(pcache - &peasycap->cache[0]);
1758 while (p2 < pcache) {
1761 pcache = &peasycap->cache[0];
1763 SAM("MISTAKE: pointer misalignment\n");
1766 /*---------------------------------------------------------------------------*/
1767 rump = (int)(0x03 & mask);
1769 p2 = (u8 *)pex; pz = p2 + much; pr = p3 + more; last = false;
1778 JOM(16, "%4i=much %4i=more %i=rump\n", much, more, rump);
1780 /*---------------------------------------------------------------------------*/
1781 switch (bytesperpixel) {
1783 if (!decimatepixel) {
1784 memcpy(pad, pex, (size_t)much);
1785 if (!byteswaporder) {
1790 p3 = (u8 *)pad; pz = p3 + much;
1800 if (!byteswaporder) {
1801 /* UYVY DECIMATED */
1802 p2 = (u8 *)pex; p3 = (u8 *)pad; pz = p2 + much;
1805 *(p3 + 1) = *(p2 + 1);
1806 *(p3 + 2) = *(p2 + 2);
1807 *(p3 + 3) = *(p2 + 3);
1812 /* YUYV DECIMATED */
1813 p2 = (u8 *)pex; p3 = (u8 *)pad; pz = p2 + much;
1817 *(p3 + 2) = *(p2 + 3);
1818 *(p3 + 3) = *(p2 + 2);
1828 if (!decimatepixel) {
1829 if (!byteswaporder) {
1832 if (pr <= (p3 + bytesperpixel))
1837 if (last && (0x0C & mask)) {
1853 tmp = ay[(int)y] + rv[(int)v];
1854 r = (255 < tmp) ? 255 : ((0 > tmp) ?
1856 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
1857 g = (255 < tmp) ? 255 : ((0 > tmp) ?
1859 tmp = ay[(int)y] + bu[(int)u];
1860 b = (255 < tmp) ? 255 : ((0 > tmp) ?
1864 pcache = &peasycap->cache[0];
1865 switch (bytesperpixel - rump) {
1879 SAM("MISTAKE: %i=rump\n",
1880 bytesperpixel - rump);
1894 p3 += bytesperpixel;
1900 if (pr <= (p3 + bytesperpixel))
1905 if (last && (0x0C & mask)) {
1922 tmp = ay[(int)y] + rv[(int)v];
1923 r = (255 < tmp) ? 255 : ((0 > tmp) ?
1925 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
1926 g = (255 < tmp) ? 255 : ((0 > tmp) ?
1928 tmp = ay[(int)y] + bu[(int)u];
1929 b = (255 < tmp) ? 255 : ((0 > tmp) ?
1933 pcache = &peasycap->cache[0];
1934 switch (bytesperpixel - rump) {
1948 SAM("MISTAKE: %i=rump\n",
1949 bytesperpixel - rump);
1963 p3 += bytesperpixel;
1968 if (!byteswaporder) {
1971 if (pr <= (p3 + bytesperpixel))
1976 if (last && (0x0C & mask)) {
1993 tmp = ay[(int)y] + rv[(int)v];
1994 r = (255 < tmp) ? 255 : ((0 > tmp) ?
1996 tmp = ay[(int)y] - gu[(int)u] -
1998 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2000 tmp = ay[(int)y] + bu[(int)u];
2001 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2005 pcache = &peasycap->cache[0];
2006 switch (bytesperpixel - rump) {
2022 bytesperpixel - rump);
2032 p3 += bytesperpixel;
2042 if (pr <= (p3 + bytesperpixel))
2047 if (last && (0x0C & mask)) {
2065 tmp = ay[(int)y] + rv[(int)v];
2066 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2068 tmp = ay[(int)y] - gu[(int)u] -
2070 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2072 tmp = ay[(int)y] + bu[(int)u];
2073 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2077 pcache = &peasycap->cache[0];
2078 switch (bytesperpixel - rump) {
2094 bytesperpixel - rump);
2104 p3 += bytesperpixel;
2117 if (!decimatepixel) {
2118 if (!byteswaporder) {
2121 if (pr <= (p3 + bytesperpixel))
2126 if (last && (0x0C & mask)) {
2142 tmp = ay[(int)y] + rv[(int)v];
2143 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2145 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
2146 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2148 tmp = ay[(int)y] + bu[(int)u];
2149 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2153 pcache = &peasycap->cache[0];
2154 switch (bytesperpixel - rump) {
2177 SAM("MISTAKE: %i=rump\n",
2178 bytesperpixel - rump);
2193 p3 += bytesperpixel;
2201 if (pr <= (p3 + bytesperpixel))
2206 if (last && (0x0C & mask)) {
2222 tmp = ay[(int)y] + rv[(int)v];
2223 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2225 tmp = ay[(int)y] - gu[(int)u] - gv[(int)v];
2226 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2228 tmp = ay[(int)y] + bu[(int)u];
2229 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2233 pcache = &peasycap->cache[0];
2234 switch (bytesperpixel - rump) {
2257 SAM("MISTAKE: %i=rump\n",
2258 bytesperpixel - rump);
2272 p3 += bytesperpixel;
2277 if (!byteswaporder) {
2282 if (pr <= (p3 + bytesperpixel))
2287 if (last && (0x0C & mask)) {
2305 tmp = ay[(int)y] + rv[(int)v];
2306 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2308 tmp = ay[(int)y] - gu[(int)u] -
2310 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2312 tmp = ay[(int)y] + bu[(int)u];
2313 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2317 pcache = &peasycap->cache[0];
2318 switch (bytesperpixel - rump) {
2355 p3 += bytesperpixel;
2366 if (pr <= (p3 + bytesperpixel))
2371 if (last && (0x0C & mask)) {
2388 tmp = ay[(int)y] + rv[(int)v];
2389 r = (255 < tmp) ? 255 : ((0 > tmp) ?
2391 tmp = ay[(int)y] - gu[(int)u] -
2393 g = (255 < tmp) ? 255 : ((0 > tmp) ?
2395 tmp = ay[(int)y] + bu[(int)u];
2396 b = (255 < tmp) ? 255 : ((0 > tmp) ?
2400 pcache = &peasycap->cache[0];
2401 switch (bytesperpixel - rump) {
2426 bytesperpixel - rump);
2437 p3 += bytesperpixel;
2448 SAM("MISTAKE: %i=bytesperpixel\n", bytesperpixel);
2454 /*****************************************************************************/
2456 * SEE CORBET ET AL. "LINUX DEVICE DRIVERS", 3rd EDITION, PAGES 430-434
2458 /*****************************************************************************/
2459 static void easycap_vma_open(struct vm_area_struct *pvma)
2461 struct easycap *peasycap;
2463 peasycap = pvma->vm_private_data;
2465 SAY("ERROR: peasycap is NULL\n");
2468 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
2469 SAY("ERROR: bad peasycap: %p\n", peasycap);
2472 peasycap->vma_many++;
2473 JOT(8, "%i=peasycap->vma_many\n", peasycap->vma_many);
2476 /*****************************************************************************/
2477 static void easycap_vma_close(struct vm_area_struct *pvma)
2479 struct easycap *peasycap;
2481 peasycap = pvma->vm_private_data;
2483 SAY("ERROR: peasycap is NULL\n");
2486 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
2487 SAY("ERROR: bad peasycap: %p\n", peasycap);
2490 peasycap->vma_many--;
2491 JOT(8, "%i=peasycap->vma_many\n", peasycap->vma_many);
2494 /*****************************************************************************/
2495 static int easycap_vma_fault(struct vm_area_struct *pvma, struct vm_fault *pvmf)
2500 struct easycap *peasycap;
2502 retcode = VM_FAULT_NOPAGE;
2505 SAY("pvma is NULL\n");
2509 SAY("pvmf is NULL\n");
2513 k = (pvmf->pgoff) / (FRAME_BUFFER_SIZE/PAGE_SIZE);
2514 m = (pvmf->pgoff) % (FRAME_BUFFER_SIZE/PAGE_SIZE);
2517 JOT(4, "%4i=k, %4i=m\n", k, m);
2519 JOT(16, "%4i=k, %4i=m\n", k, m);
2521 if ((0 > k) || (FRAME_BUFFER_MANY <= k)) {
2522 SAY("ERROR: buffer index %i out of range\n", k);
2525 if ((0 > m) || (FRAME_BUFFER_SIZE/PAGE_SIZE <= m)) {
2526 SAY("ERROR: page number %i out of range\n", m);
2529 peasycap = pvma->vm_private_data;
2531 SAY("ERROR: peasycap is NULL\n");
2534 /*---------------------------------------------------------------------------*/
2535 pbuf = peasycap->frame_buffer[k][m].pgo;
2537 SAM("ERROR: pbuf is NULL\n");
2540 page = virt_to_page(pbuf);
2542 SAM("ERROR: page is NULL\n");
2546 /*---------------------------------------------------------------------------*/
2548 SAM("ERROR: page is NULL after get_page(page)\n");
2551 retcode = VM_FAULT_MINOR;
2556 static const struct vm_operations_struct easycap_vm_ops = {
2557 .open = easycap_vma_open,
2558 .close = easycap_vma_close,
2559 .fault = easycap_vma_fault,
2562 static int easycap_mmap(struct file *file, struct vm_area_struct *pvma)
2566 pvma->vm_ops = &easycap_vm_ops;
2567 pvma->vm_flags |= VM_RESERVED;
2569 pvma->vm_private_data = file->private_data;
2570 easycap_vma_open(pvma);
2573 /*****************************************************************************/
2574 /*---------------------------------------------------------------------------*/
2576 * ON COMPLETION OF A VIDEO URB ITS DATA IS COPIED TO THE FIELD BUFFERS
2577 * PROVIDED peasycap->video_idle IS ZERO. REGARDLESS OF THIS BEING TRUE,
2578 * IT IS RESUBMITTED PROVIDED peasycap->video_isoc_streaming IS NOT ZERO.
2580 * THIS FUNCTION IS AN INTERRUPT SERVICE ROUTINE AND MUST NOT SLEEP.
2582 * INFORMATION ABOUT THE VALIDITY OF THE CONTENTS OF THE FIELD BUFFER ARE
2583 * STORED IN THE TWO-BYTE STATUS PARAMETER
2584 * peasycap->field_buffer[peasycap->field_fill][0].kount
2585 * NOTICE THAT THE INFORMATION IS STORED ONLY WITH PAGE 0 OF THE FIELD BUFFER.
2587 * THE LOWER BYTE CONTAINS THE FIELD PARITY BYTE FURNISHED BY THE SAA7113H
2590 * THE UPPER BYTE IS ZERO IF NO PROBLEMS, OTHERWISE:
2591 * 0 != (kount & 0x8000) => AT LEAST ONE URB COMPLETED WITH ERRORS
2592 * 0 != (kount & 0x4000) => BUFFER HAS TOO MUCH DATA
2593 * 0 != (kount & 0x2000) => BUFFER HAS NOT ENOUGH DATA
2594 * 0 != (kount & 0x1000) => BUFFER HAS DATA FROM DISPARATE INPUTS
2595 * 0 != (kount & 0x0400) => RESERVED
2596 * 0 != (kount & 0x0200) => FIELD BUFFER NOT YET CHECKED
2597 * 0 != (kount & 0x0100) => BUFFER HAS TWO EXTRA BYTES - WHY?
2599 /*---------------------------------------------------------------------------*/
2600 static void easycap_complete(struct urb *purb)
2602 struct easycap *peasycap;
2603 struct data_buffer *pfield_buffer;
2605 int i, more, much, leap, rc, last;
2606 int videofieldamount;
2607 unsigned int override, bad;
2608 int framestatus, framelength, frameactual, frameoffset;
2612 SAY("ERROR: easycap_complete(): purb is NULL\n");
2615 peasycap = purb->context;
2617 SAY("ERROR: easycap_complete(): peasycap is NULL\n");
2620 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
2621 SAY("ERROR: bad peasycap: %p\n", peasycap);
2624 if (peasycap->video_eof)
2626 for (i = 0; i < VIDEO_ISOC_BUFFER_MANY; i++)
2627 if (purb->transfer_buffer == peasycap->video_isoc_buffer[i].pgo)
2629 JOM(16, "%2i=urb\n", i);
2630 last = peasycap->video_isoc_sequence;
2631 if ((((VIDEO_ISOC_BUFFER_MANY - 1) == last) && (0 != i)) ||
2632 (((VIDEO_ISOC_BUFFER_MANY - 1) != last) && ((last + 1) != i))) {
2633 JOM(16, "ERROR: out-of-order urbs %i,%i ... continuing\n",
2636 peasycap->video_isoc_sequence = i;
2638 if (peasycap->video_idle) {
2639 JOM(16, "%i=video_idle %i=video_isoc_streaming\n",
2640 peasycap->video_idle, peasycap->video_isoc_streaming);
2641 if (peasycap->video_isoc_streaming) {
2642 rc = usb_submit_urb(purb, GFP_ATOMIC);
2644 SAM("%s:%d ENOMEM\n", strerror(rc), rc);
2646 SAM("ERROR: while %i=video_idle, "
2648 "failed with rc:\n",
2649 peasycap->video_idle);
2655 /*---------------------------------------------------------------------------*/
2656 if (FIELD_BUFFER_MANY <= peasycap->field_fill) {
2657 SAM("ERROR: bad peasycap->field_fill\n");
2661 if ((-ESHUTDOWN == purb->status) || (-ENOENT == purb->status)) {
2662 JOM(8, "urb status -ESHUTDOWN or -ENOENT\n");
2666 (peasycap->field_buffer[peasycap->field_fill][0].kount) |= 0x8000 ;
2667 SAM("ERROR: bad urb status -%s: %d\n",
2668 strerror(purb->status), purb->status);
2669 /*---------------------------------------------------------------------------*/
2671 for (i = 0; i < purb->number_of_packets; i++) {
2672 if (0 != purb->iso_frame_desc[i].status) {
2673 (peasycap->field_buffer
2674 [peasycap->field_fill][0].kount) |= 0x8000 ;
2675 /* FIXME: 1. missing '-' check boundaries */
2677 strerror(purb->iso_frame_desc[i].status));
2679 framestatus = purb->iso_frame_desc[i].status;
2680 framelength = purb->iso_frame_desc[i].length;
2681 frameactual = purb->iso_frame_desc[i].actual_length;
2682 frameoffset = purb->iso_frame_desc[i].offset;
2684 JOM(16, "frame[%2i]:"
2689 i, framestatus, frameactual, framelength, frameoffset);
2690 if (!purb->iso_frame_desc[i].status) {
2691 more = purb->iso_frame_desc[i].actual_length;
2692 pfield_buffer = &peasycap->field_buffer
2693 [peasycap->field_fill][peasycap->field_page];
2694 videofieldamount = (peasycap->field_page *
2696 (int)(pfield_buffer->pto - pfield_buffer->pgo);
2698 peasycap->video_mt++;
2700 if (peasycap->video_mt) {
2701 JOM(8, "%4i empty video urb frames\n",
2702 peasycap->video_mt);
2703 peasycap->video_mt = 0;
2705 if (FIELD_BUFFER_MANY <= peasycap->field_fill) {
2706 SAM("ERROR: bad peasycap->field_fill\n");
2709 if (FIELD_BUFFER_SIZE/PAGE_SIZE <=
2710 peasycap->field_page) {
2711 SAM("ERROR: bad peasycap->field_page\n");
2714 pfield_buffer = &peasycap->field_buffer
2715 [peasycap->field_fill][peasycap->field_page];
2716 pu = (u8 *)(purb->transfer_buffer +
2717 purb->iso_frame_desc[i].offset);
2722 /*--------------------------------------------------------------------------*/
2724 * EIGHT-BYTE END-OF-VIDEOFIELD MARKER.
2725 * NOTE: A SUCCESSION OF URB FRAMES FOLLOWING THIS ARE EMPTY,
2726 * CORRESPONDING TO THE FIELD FLYBACK (VERTICAL BLANKING) PERIOD.
2728 * PROVIDED THE FIELD BUFFER CONTAINS GOOD DATA AS INDICATED BY A ZERO UPPER
2730 * peasycap->field_buffer[peasycap->field_fill][0].kount
2731 * THE CONTENTS OF THE FIELD BUFFER ARE OFFERED TO dqbuf(), field_read IS
2732 * UPDATED AND field_fill IS BUMPED. IF THE FIELD BUFFER CONTAINS BAD DATA
2733 * NOTHING IS OFFERED TO dqbuf().
2735 * THE DECISION ON WHETHER THE PARITY OF THE OFFERED FIELD BUFFER IS RIGHT
2736 * RESTS WITH dqbuf().
2738 /*---------------------------------------------------------------------------*/
2739 if ((8 == more) || override) {
2740 if (videofieldamount >
2741 peasycap->videofieldamount) {
2742 if (2 == videofieldamount -
2745 (peasycap->field_buffer
2746 [peasycap->field_fill]
2747 [0].kount) |= 0x0100;
2748 peasycap->video_junk += (1 +
2749 VIDEO_JUNK_TOLERATE);
2751 (peasycap->field_buffer
2752 [peasycap->field_fill]
2753 [0].kount) |= 0x4000;
2754 } else if (videofieldamount <
2757 (peasycap->field_buffer
2758 [peasycap->field_fill]
2759 [0].kount) |= 0x2000;
2761 bad = 0xFF00 & peasycap->field_buffer
2762 [peasycap->field_fill]
2765 (peasycap->video_junk)--;
2766 if (-VIDEO_JUNK_TOLERATE >
2767 peasycap->video_junk)
2768 peasycap->video_junk =
2769 -VIDEO_JUNK_TOLERATE;
2770 peasycap->field_read =
2773 if (FIELD_BUFFER_MANY <=
2778 peasycap->field_page = 0;
2779 pfield_buffer = &peasycap->
2785 pfield_buffer->pto =
2787 JOM(8, "bumped to: %i="
2791 peasycap->field_fill,
2793 pfield_buffer->kount);
2794 JOM(8, "field buffer %i has "
2795 "%i bytes fit to be "
2797 peasycap->field_read,
2799 JOM(8, "wakeup call to "
2804 peasycap->field_read,
2805 peasycap->field_fill,
2809 field_read][0].kount);
2810 wake_up_interruptible
2814 (&peasycap->timeval7);
2816 peasycap->video_junk++;
2818 peasycap->video_junk +=
2819 (1 + VIDEO_JUNK_TOLERATE/2);
2820 JOM(8, "field buffer %i had %i "
2821 "bytes, now discarded: "
2823 peasycap->field_fill,
2826 peasycap->field_buffer
2827 [peasycap->field_fill][0].
2829 (peasycap->field_fill)++;
2831 if (FIELD_BUFFER_MANY <=
2832 peasycap->field_fill)
2833 peasycap->field_fill = 0;
2834 peasycap->field_page = 0;
2836 &peasycap->field_buffer
2837 [peasycap->field_fill]
2838 [peasycap->field_page];
2839 pfield_buffer->pto =
2842 JOM(8, "bumped to: %i=peasycap->"
2843 "field_fill %i=parity\n",
2844 peasycap->field_fill,
2845 0x00FF & pfield_buffer->kount);
2848 JOM(8, "end-of-field: received "
2849 "parity byte 0x%02X\n",
2852 pfield_buffer->kount = 0x0000;
2854 pfield_buffer->kount = 0x0001;
2855 pfield_buffer->input = 0x08 |
2856 (0x07 & peasycap->input);
2857 JOM(8, "end-of-field: 0x%02X=kount\n",
2858 0xFF & pfield_buffer->kount);
2861 /*---------------------------------------------------------------------------*/
2863 * COPY more BYTES FROM ISOC BUFFER TO FIELD BUFFER
2865 /*---------------------------------------------------------------------------*/
2869 if (FIELD_BUFFER_MANY <= peasycap->field_fill) {
2870 SAM("ERROR: bad peasycap->field_fill\n");
2873 if (FIELD_BUFFER_SIZE/PAGE_SIZE <= peasycap->field_page) {
2874 SAM("ERROR: bad peasycap->field_page\n");
2877 pfield_buffer = &peasycap->field_buffer
2878 [peasycap->field_fill][peasycap->field_page];
2880 pfield_buffer = &peasycap->field_buffer
2881 [peasycap->field_fill]
2882 [peasycap->field_page];
2883 if (PAGE_SIZE < (pfield_buffer->pto -
2884 pfield_buffer->pgo)) {
2885 SAM("ERROR: bad pfield_buffer->pto\n");
2888 if (PAGE_SIZE == (pfield_buffer->pto -
2889 pfield_buffer->pgo)) {
2890 (peasycap->field_page)++;
2891 if (FIELD_BUFFER_SIZE/PAGE_SIZE <=
2892 peasycap->field_page) {
2893 JOM(16, "wrapping peasycap->"
2895 peasycap->field_page = 0;
2897 pfield_buffer = &peasycap->
2899 [peasycap->field_fill]
2900 [peasycap->field_page];
2901 pfield_buffer->pto = pfield_buffer->pgo;
2902 pfield_buffer->input = 0x08 |
2903 (0x07 & peasycap->input);
2904 if ((peasycap->field_buffer[peasycap->
2907 pfield_buffer->input)
2908 (peasycap->field_buffer
2909 [peasycap->field_fill]
2910 [0]).kount |= 0x1000;
2914 (int)(pfield_buffer->pto -
2915 pfield_buffer->pgo);
2919 memcpy(pfield_buffer->pto, pu, much);
2921 (pfield_buffer->pto) += much;
2928 /*---------------------------------------------------------------------------*/
2930 * RESUBMIT THIS URB, UNLESS A SEVERE PERSISTENT ERROR CONDITION EXISTS.
2932 * IF THE WAIT QUEUES ARE NOT CLEARED IN RESPONSE TO AN ERROR CONDITION
2933 * THE USERSPACE PROGRAM, E.G. mplayer, MAY HANG ON EXIT. BEWARE.
2935 /*---------------------------------------------------------------------------*/
2936 if (VIDEO_ISOC_BUFFER_MANY <= peasycap->video_junk) {
2937 SAM("easycap driver shutting down on condition green\n");
2938 peasycap->status = 1;
2939 peasycap->video_eof = 1;
2940 peasycap->video_junk = 0;
2941 wake_up_interruptible(&peasycap->wq_video);
2942 #if !defined(PERSEVERE)
2943 peasycap->audio_eof = 1;
2944 wake_up_interruptible(&peasycap->wq_audio);
2945 #endif /*PERSEVERE*/
2948 if (peasycap->video_isoc_streaming) {
2949 rc = usb_submit_urb(purb, GFP_ATOMIC);
2951 SAM("%s: %d\n", strerror(rc), rc);
2953 SAM("ERROR: while %i=video_idle, "
2955 "failed with rc:\n",
2956 peasycap->video_idle);
2961 static const struct file_operations easycap_fops = {
2962 .owner = THIS_MODULE,
2963 .open = easycap_open,
2964 .unlocked_ioctl = easycap_unlocked_ioctl,
2965 .poll = easycap_poll,
2966 .mmap = easycap_mmap,
2967 .llseek = no_llseek,
2969 static const struct usb_class_driver easycap_class = {
2970 .name = "usb/easycap%d",
2971 .fops = &easycap_fops,
2972 .minor_base = USB_SKEL_MINOR_BASE,
2974 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
2975 static const struct v4l2_file_operations v4l2_fops = {
2976 .owner = THIS_MODULE,
2977 .open = easycap_open_noinode,
2978 .unlocked_ioctl = easycap_unlocked_ioctl,
2979 .poll = easycap_poll,
2980 .mmap = easycap_mmap,
2982 /*****************************************************************************/
2983 /*---------------------------------------------------------------------------*/
2985 * WHEN THE EasyCAP IS PHYSICALLY PLUGGED IN, THIS FUNCTION IS CALLED THREE
2986 * TIMES, ONCE FOR EACH OF THE THREE INTERFACES. BEWARE.
2988 /*---------------------------------------------------------------------------*/
2989 static int easycap_usb_probe(struct usb_interface *pusb_interface,
2990 const struct usb_device_id *pusb_device_id)
2992 struct usb_device *pusb_device;
2993 struct usb_host_interface *pusb_host_interface;
2994 struct usb_endpoint_descriptor *pepd;
2995 struct usb_interface_descriptor *pusb_interface_descriptor;
2997 struct easycap *peasycap;
2999 struct data_urb *pdata_urb;
3000 size_t wMaxPacketSize;
3001 int ISOCwMaxPacketSize;
3002 int BULKwMaxPacketSize;
3003 int INTwMaxPacketSize;
3004 int CTRLwMaxPacketSize;
3005 u8 bEndpointAddress;
3006 u8 ISOCbEndpointAddress;
3007 u8 INTbEndpointAddress;
3008 int isin, i, j, k, m, rc;
3009 u8 bInterfaceNumber;
3011 u8 bInterfaceSubClass;
3013 int okalt[8], isokalt;
3019 struct easycap_format *peasycap_format;
3021 struct inputset *inputset;
3022 struct v4l2_device *pv4l2_device;
3024 /*---------------------------------------------------------------------------*/
3026 * GET POINTER TO STRUCTURE usb_device
3028 /*---------------------------------------------------------------------------*/
3029 pusb_device = interface_to_usbdev(pusb_interface);
3031 JOT(4, "bNumConfigurations=%i\n", pusb_device->descriptor.bNumConfigurations);
3032 /*---------------------------------------------------------------------------*/
3033 pusb_host_interface = pusb_interface->cur_altsetting;
3034 if (!pusb_host_interface) {
3035 SAY("ERROR: pusb_host_interface is NULL\n");
3038 pusb_interface_descriptor = &(pusb_host_interface->desc);
3039 if (!pusb_interface_descriptor) {
3040 SAY("ERROR: pusb_interface_descriptor is NULL\n");
3043 /*---------------------------------------------------------------------------*/
3045 * GET PROPERTIES OF PROBED INTERFACE
3047 /*---------------------------------------------------------------------------*/
3048 bInterfaceNumber = pusb_interface_descriptor->bInterfaceNumber;
3049 bInterfaceClass = pusb_interface_descriptor->bInterfaceClass;
3050 bInterfaceSubClass = pusb_interface_descriptor->bInterfaceSubClass;
3052 JOT(4, "intf[%i]: num_altsetting=%i\n",
3053 bInterfaceNumber, pusb_interface->num_altsetting);
3054 JOT(4, "intf[%i]: cur_altsetting - altsetting=%li\n",
3056 (long int)(pusb_interface->cur_altsetting -
3057 pusb_interface->altsetting));
3058 JOT(4, "intf[%i]: bInterfaceClass=0x%02X bInterfaceSubClass=0x%02X\n",
3059 bInterfaceNumber, bInterfaceClass, bInterfaceSubClass);
3060 /*---------------------------------------------------------------------------*/
3062 * A NEW struct easycap IS ALWAYS ALLOCATED WHEN INTERFACE 0 IS PROBED.
3063 * IT IS NOT POSSIBLE HERE TO FREE ANY EXISTING struct easycap. THIS
3064 * SHOULD HAVE BEEN DONE BY easycap_delete() WHEN THE EasyCAP WAS
3065 * PHYSICALLY UNPLUGGED.
3067 * THE POINTER peasycap TO THE struct easycap IS REMEMBERED WHEN
3068 * INTERFACES 1 AND 2 ARE PROBED.
3070 /*---------------------------------------------------------------------------*/
3071 if (0 == bInterfaceNumber) {
3072 peasycap = kzalloc(sizeof(struct easycap), GFP_KERNEL);
3074 SAY("ERROR: Could not allocate peasycap\n");
3077 /*---------------------------------------------------------------------------*/
3079 * PERFORM URGENT INTIALIZATIONS ...
3081 /*---------------------------------------------------------------------------*/
3082 peasycap->minor = -1;
3083 strcpy(&peasycap->telltale[0], TELLTALE);
3084 kref_init(&peasycap->kref);
3085 JOM(8, "intf[%i]: after kref_init(..._video) "
3086 "%i=peasycap->kref.refcount.counter\n",
3087 bInterfaceNumber, peasycap->kref.refcount.counter);
3090 peasycap->gain = (s8)clamp(easycap_gain, 0, 31);
3092 init_waitqueue_head(&peasycap->wq_video);
3093 init_waitqueue_head(&peasycap->wq_audio);
3094 init_waitqueue_head(&peasycap->wq_trigger);
3096 if (mutex_lock_interruptible(&mutex_dongle)) {
3097 SAY("ERROR: cannot down mutex_dongle\n");
3098 return -ERESTARTSYS;
3100 /*---------------------------------------------------------------------------*/
3102 * FOR INTERFACES 1 AND 2 THE POINTER peasycap WILL NEED TO
3103 * TO BE THE SAME AS THAT ALLOCATED NOW FOR INTERFACE 0.
3105 * NORMALLY ndong WILL NOT HAVE CHANGED SINCE INTERFACE 0 WAS
3106 * PROBED, BUT THIS MAY NOT BE THE CASE IF, FOR EXAMPLE, TWO
3107 * EASYCAPs ARE PLUGGED IN SIMULTANEOUSLY.
3109 /*---------------------------------------------------------------------------*/
3110 for (ndong = 0; ndong < DONGLE_MANY; ndong++) {
3111 if ((!easycapdc60_dongle[ndong].peasycap) &&
3112 (!mutex_is_locked(&easycapdc60_dongle
3113 [ndong].mutex_video)) &&
3114 (!mutex_is_locked(&easycapdc60_dongle
3115 [ndong].mutex_audio))) {
3116 easycapdc60_dongle[ndong].peasycap = peasycap;
3117 peasycap->isdongle = ndong;
3118 JOM(8, "intf[%i]: peasycap-->easycap"
3119 "_dongle[%i].peasycap\n",
3120 bInterfaceNumber, ndong);
3124 if (DONGLE_MANY <= ndong) {
3125 SAM("ERROR: too many dongles\n");
3126 mutex_unlock(&mutex_dongle);
3129 mutex_unlock(&mutex_dongle);
3131 peasycap->allocation_video_struct = sizeof(struct easycap);
3132 peasycap->allocation_video_page = 0;
3133 peasycap->allocation_video_urb = 0;
3134 peasycap->allocation_audio_struct = 0;
3135 peasycap->allocation_audio_page = 0;
3136 peasycap->allocation_audio_urb = 0;
3138 /*---------------------------------------------------------------------------*/
3140 * ... AND FURTHER INITIALIZE THE STRUCTURE
3142 /*---------------------------------------------------------------------------*/
3143 peasycap->pusb_device = pusb_device;
3144 peasycap->pusb_interface = pusb_interface;
3147 peasycap->microphone = false;
3149 peasycap->video_interface = -1;
3150 peasycap->video_altsetting_on = -1;
3151 peasycap->video_altsetting_off = -1;
3152 peasycap->video_endpointnumber = -1;
3153 peasycap->video_isoc_maxframesize = -1;
3154 peasycap->video_isoc_buffer_size = -1;
3156 peasycap->audio_interface = -1;
3157 peasycap->audio_altsetting_on = -1;
3158 peasycap->audio_altsetting_off = -1;
3159 peasycap->audio_endpointnumber = -1;
3160 peasycap->audio_isoc_maxframesize = -1;
3161 peasycap->audio_isoc_buffer_size = -1;
3163 peasycap->frame_buffer_many = FRAME_BUFFER_MANY;
3165 for (k = 0; k < INPUT_MANY; k++)
3166 peasycap->lost[k] = 0;
3168 peasycap->skipped = 0;
3169 peasycap->offerfields = 0;
3170 /*---------------------------------------------------------------------------*/
3172 * DYNAMICALLY FILL IN THE AVAILABLE FORMATS ...
3174 /*---------------------------------------------------------------------------*/
3175 rc = fillin_formats();
3177 SAM("ERROR: fillin_formats() rc = %i\n", rc);
3180 JOM(4, "%i formats available\n", rc);
3181 /*---------------------------------------------------------------------------*/
3183 * ... AND POPULATE easycap.inputset[]
3185 /*---------------------------------------------------------------------------*/
3186 /* FIXME: maybe we just use memset 0 */
3187 inputset = peasycap->inputset;
3188 for (k = 0; k < INPUT_MANY; k++) {
3189 inputset[k].input_ok = 0;
3190 inputset[k].standard_offset_ok = 0;
3191 inputset[k].format_offset_ok = 0;
3192 inputset[k].brightness_ok = 0;
3193 inputset[k].contrast_ok = 0;
3194 inputset[k].saturation_ok = 0;
3195 inputset[k].hue_ok = 0;
3198 fmtidx = peasycap->ntsc ? NTSC_M : PAL_BGHIN;
3201 for (i = 0; 0xFFFF != easycap_standard[i].mask; i++) {
3202 if (fmtidx == easycap_standard[i].v4l2_standard.index) {
3204 for (k = 0; k < INPUT_MANY; k++)
3205 inputset[k].standard_offset = i;
3207 mask = easycap_standard[i].mask;
3213 "inputset->standard_offset unpopulated, %i=m\n", m);
3217 peasycap_format = &easycap_format[0];
3219 for (i = 0; peasycap_format->v4l2_format.fmt.pix.width; i++) {
3220 struct v4l2_pix_format *pix =
3221 &peasycap_format->v4l2_format.fmt.pix;
3222 if (((peasycap_format->mask & 0x0F) == (mask & 0x0F)) &&
3223 pix->field == V4L2_FIELD_NONE &&
3224 pix->pixelformat == V4L2_PIX_FMT_UYVY &&
3225 pix->width == 640 && pix->height == 480) {
3227 for (k = 0; k < INPUT_MANY; k++)
3228 inputset[k].format_offset = i;
3234 SAM("ERROR: inputset[]->format_offset unpopulated\n");
3239 for (i = 0; 0xFFFFFFFF != easycap_control[i].id; i++) {
3240 value = easycap_control[i].default_value;
3241 if (V4L2_CID_BRIGHTNESS == easycap_control[i].id) {
3243 for (k = 0; k < INPUT_MANY; k++)
3244 inputset[k].brightness = value;
3245 } else if (V4L2_CID_CONTRAST == easycap_control[i].id) {
3247 for (k = 0; k < INPUT_MANY; k++)
3248 inputset[k].contrast = value;
3249 } else if (V4L2_CID_SATURATION == easycap_control[i].id) {
3251 for (k = 0; k < INPUT_MANY; k++)
3252 inputset[k].saturation = value;
3253 } else if (V4L2_CID_HUE == easycap_control[i].id) {
3255 for (k = 0; k < INPUT_MANY; k++)
3256 inputset[k].hue = value;
3261 SAM("ERROR: inputset[]->brightness underpopulated\n");
3264 for (k = 0; k < INPUT_MANY; k++)
3265 inputset[k].input = k;
3266 JOM(4, "populated inputset[]\n");
3267 JOM(4, "finished initialization\n");
3269 /*---------------------------------------------------------------------------*/
3273 * IDENTIFY THE APPROPRIATE POINTER peasycap FOR INTERFACES 1 AND 2.
3274 * THE ADDRESS OF peasycap->pusb_device IS RELUCTANTLY USED FOR THIS PURPOSE.
3276 /*---------------------------------------------------------------------------*/
3277 for (ndong = 0; ndong < DONGLE_MANY; ndong++) {
3278 if (pusb_device == easycapdc60_dongle[ndong].peasycap->
3280 peasycap = easycapdc60_dongle[ndong].peasycap;
3281 JOT(8, "intf[%i]: dongle[%i].peasycap\n",
3282 bInterfaceNumber, ndong);
3286 if (DONGLE_MANY <= ndong) {
3287 SAY("ERROR: peasycap is unknown when probing interface %i\n",
3292 SAY("ERROR: peasycap is NULL when probing interface %i\n",
3296 /*---------------------------------------------------------------------------*/
3298 * SOME VERSIONS OF THE videodev MODULE OVERWRITE THE DATA WHICH HAS
3299 * BEEN WRITTEN BY THE CALL TO usb_set_intfdata() IN easycap_usb_probe(),
3300 * REPLACING IT WITH A POINTER TO THE EMBEDDED v4l2_device STRUCTURE.
3301 * TO DETECT THIS, THE STRING IN THE easycap.telltale[] BUFFER IS CHECKED.
3303 /*---------------------------------------------------------------------------*/
3304 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
3305 pv4l2_device = usb_get_intfdata(pusb_interface);
3306 if (!pv4l2_device) {
3307 SAY("ERROR: pv4l2_device is NULL\n");
3310 peasycap = (struct easycap *)
3311 container_of(pv4l2_device, struct easycap, v4l2_device);
3314 /*---------------------------------------------------------------------------*/
3315 if ((USB_CLASS_VIDEO == bInterfaceClass) ||
3316 (USB_CLASS_VENDOR_SPEC == bInterfaceClass)) {
3317 if (-1 == peasycap->video_interface) {
3318 peasycap->video_interface = bInterfaceNumber;
3319 JOM(4, "setting peasycap->video_interface=%i\n",
3320 peasycap->video_interface);
3322 if (peasycap->video_interface != bInterfaceNumber) {
3323 SAM("ERROR: attempting to reset "
3324 "peasycap->video_interface\n");
3325 SAM("...... continuing with "
3326 "%i=peasycap->video_interface\n",
3327 peasycap->video_interface);
3330 } else if ((USB_CLASS_AUDIO == bInterfaceClass) &&
3331 (USB_SUBCLASS_AUDIOSTREAMING == bInterfaceSubClass)) {
3332 if (-1 == peasycap->audio_interface) {
3333 peasycap->audio_interface = bInterfaceNumber;
3334 JOM(4, "setting peasycap->audio_interface=%i\n",
3335 peasycap->audio_interface);
3337 if (peasycap->audio_interface != bInterfaceNumber) {
3338 SAM("ERROR: attempting to reset "
3339 "peasycap->audio_interface\n");
3340 SAM("...... continuing with "
3341 "%i=peasycap->audio_interface\n",
3342 peasycap->audio_interface);
3346 /*---------------------------------------------------------------------------*/
3348 * INVESTIGATE ALL ALTSETTINGS.
3349 * DONE IN DETAIL BECAUSE USB DEVICE 05e1:0408 HAS DISPARATE INCARNATIONS.
3351 /*---------------------------------------------------------------------------*/
3354 for (i = 0; i < pusb_interface->num_altsetting; i++) {
3355 pusb_host_interface = &(pusb_interface->altsetting[i]);
3356 if (!pusb_host_interface) {
3357 SAM("ERROR: pusb_host_interface is NULL\n");
3360 pusb_interface_descriptor = &(pusb_host_interface->desc);
3361 if (!pusb_interface_descriptor) {
3362 SAM("ERROR: pusb_interface_descriptor is NULL\n");
3366 JOM(4, "intf[%i]alt[%i]: desc.bDescriptorType=0x%02X\n",
3367 bInterfaceNumber, i, pusb_interface_descriptor->bDescriptorType);
3368 JOM(4, "intf[%i]alt[%i]: desc.bInterfaceNumber=0x%02X\n",
3369 bInterfaceNumber, i, pusb_interface_descriptor->bInterfaceNumber);
3370 JOM(4, "intf[%i]alt[%i]: desc.bAlternateSetting=0x%02X\n",
3371 bInterfaceNumber, i, pusb_interface_descriptor->bAlternateSetting);
3372 JOM(4, "intf[%i]alt[%i]: desc.bNumEndpoints=0x%02X\n",
3373 bInterfaceNumber, i, pusb_interface_descriptor->bNumEndpoints);
3374 JOM(4, "intf[%i]alt[%i]: desc.bInterfaceClass=0x%02X\n",
3375 bInterfaceNumber, i, pusb_interface_descriptor->bInterfaceClass);
3376 JOM(4, "intf[%i]alt[%i]: desc.bInterfaceSubClass=0x%02X\n",
3377 bInterfaceNumber, i, pusb_interface_descriptor->bInterfaceSubClass);
3378 JOM(4, "intf[%i]alt[%i]: desc.bInterfaceProtocol=0x%02X\n",
3379 bInterfaceNumber, i, pusb_interface_descriptor->bInterfaceProtocol);
3380 JOM(4, "intf[%i]alt[%i]: desc.iInterface=0x%02X\n",
3381 bInterfaceNumber, i, pusb_interface_descriptor->iInterface);
3383 ISOCwMaxPacketSize = -1;
3384 BULKwMaxPacketSize = -1;
3385 INTwMaxPacketSize = -1;
3386 CTRLwMaxPacketSize = -1;
3387 ISOCbEndpointAddress = 0;
3388 INTbEndpointAddress = 0;
3390 if (0 == pusb_interface_descriptor->bNumEndpoints)
3391 JOM(4, "intf[%i]alt[%i] has no endpoints\n",
3392 bInterfaceNumber, i);
3393 /*---------------------------------------------------------------------------*/
3394 for (j = 0; j < pusb_interface_descriptor->bNumEndpoints; j++) {
3395 pepd = &(pusb_host_interface->endpoint[j].desc);
3397 SAM("ERROR: pepd is NULL.\n");
3398 SAM("...... skipping\n");
3401 wMaxPacketSize = le16_to_cpu(pepd->wMaxPacketSize);
3402 bEndpointAddress = pepd->bEndpointAddress;
3404 JOM(4, "intf[%i]alt[%i]end[%i]: bEndpointAddress=0x%X\n",
3405 bInterfaceNumber, i, j,
3406 pepd->bEndpointAddress);
3407 JOM(4, "intf[%i]alt[%i]end[%i]: bmAttributes=0x%X\n",
3408 bInterfaceNumber, i, j,
3409 pepd->bmAttributes);
3410 JOM(4, "intf[%i]alt[%i]end[%i]: wMaxPacketSize=%i\n",
3411 bInterfaceNumber, i, j,
3412 pepd->wMaxPacketSize);
3413 JOM(4, "intf[%i]alt[%i]end[%i]: bInterval=%i\n",
3414 bInterfaceNumber, i, j,
3417 if (pepd->bEndpointAddress & USB_DIR_IN) {
3418 JOM(4, "intf[%i]alt[%i]end[%i] is an IN endpoint\n",
3419 bInterfaceNumber, i, j);
3422 JOM(4, "intf[%i]alt[%i]end[%i] is an OUT endpoint\n",
3423 bInterfaceNumber, i, j);
3424 SAM("ERROR: OUT endpoint unexpected\n");
3425 SAM("...... continuing\n");
3428 if ((pepd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
3429 USB_ENDPOINT_XFER_ISOC) {
3430 JOM(4, "intf[%i]alt[%i]end[%i] is an ISOC endpoint\n",
3431 bInterfaceNumber, i, j);
3433 switch (bInterfaceClass) {
3434 case USB_CLASS_VIDEO:
3435 case USB_CLASS_VENDOR_SPEC: {
3438 "peasycap is NULL\n");
3441 if (pepd->wMaxPacketSize) {
3466 if (-1 == peasycap->
3467 video_altsetting_off) {
3469 video_altsetting_off =
3475 video_altsetting_off);
3477 SAM("ERROR: peasycap"
3478 "->video_altsetting_"
3479 "off already set\n");
3482 "%i=peasycap->video_"
3485 video_altsetting_off);
3490 case USB_CLASS_AUDIO: {
3491 if (bInterfaceSubClass !=
3492 USB_SUBCLASS_AUDIOSTREAMING)
3496 "peasycap is NULL\n");
3499 if (pepd->wMaxPacketSize) {
3501 okalt[isokalt] = i ;
3524 if (-1 == peasycap->
3525 audio_altsetting_off) {
3527 audio_altsetting_off =
3533 audio_altsetting_off);
3535 SAM("ERROR: peasycap"
3536 "->audio_altsetting_"
3537 "off already set\n");
3544 audio_altsetting_off);
3553 } else if ((pepd->bmAttributes &
3554 USB_ENDPOINT_XFERTYPE_MASK) ==
3555 USB_ENDPOINT_XFER_BULK) {
3556 JOM(4, "intf[%i]alt[%i]end[%i] is a BULK endpoint\n",
3557 bInterfaceNumber, i, j);
3558 } else if ((pepd->bmAttributes &
3559 USB_ENDPOINT_XFERTYPE_MASK) ==
3560 USB_ENDPOINT_XFER_INT) {
3561 JOM(4, "intf[%i]alt[%i]end[%i] is an INT endpoint\n",
3562 bInterfaceNumber, i, j);
3564 JOM(4, "intf[%i]alt[%i]end[%i] is a CTRL endpoint\n",
3565 bInterfaceNumber, i, j);
3567 if (0 == pepd->wMaxPacketSize) {
3568 JOM(4, "intf[%i]alt[%i]end[%i] "
3569 "has zero packet size\n",
3570 bInterfaceNumber, i, j);
3574 /*---------------------------------------------------------------------------*/
3576 * PERFORM INITIALIZATION OF THE PROBED INTERFACE
3578 /*---------------------------------------------------------------------------*/
3579 JOM(4, "initialization begins for interface %i\n",
3580 pusb_interface_descriptor->bInterfaceNumber);
3581 switch (bInterfaceNumber) {
3582 /*---------------------------------------------------------------------------*/
3584 * INTERFACE 0 IS THE VIDEO INTERFACE
3586 /*---------------------------------------------------------------------------*/
3589 SAM("MISTAKE: peasycap is NULL\n");
3593 SAM("ERROR: no viable video_altsetting_on\n");
3596 peasycap->video_altsetting_on = okalt[isokalt - 1];
3597 JOM(4, "%i=video_altsetting_on <====\n",
3598 peasycap->video_altsetting_on);
3600 /*---------------------------------------------------------------------------*/
3602 * DECIDE THE VIDEO STREAMING PARAMETERS
3604 /*---------------------------------------------------------------------------*/
3605 peasycap->video_endpointnumber = okepn[isokalt - 1];
3606 JOM(4, "%i=video_endpointnumber\n", peasycap->video_endpointnumber);
3607 maxpacketsize = okmps[isokalt - 1];
3609 peasycap->video_isoc_maxframesize =
3610 min(maxpacketsize, USB_2_0_MAXPACKETSIZE);
3611 if (0 >= peasycap->video_isoc_maxframesize) {
3612 SAM("ERROR: bad video_isoc_maxframesize\n");
3613 SAM(" possibly because port is USB 1.1\n");
3616 JOM(4, "%i=video_isoc_maxframesize\n",
3617 peasycap->video_isoc_maxframesize);
3619 peasycap->video_isoc_framesperdesc = VIDEO_ISOC_FRAMESPERDESC;
3620 JOM(4, "%i=video_isoc_framesperdesc\n",
3621 peasycap->video_isoc_framesperdesc);
3622 if (0 >= peasycap->video_isoc_framesperdesc) {
3623 SAM("ERROR: bad video_isoc_framesperdesc\n");
3626 peasycap->video_isoc_buffer_size =
3627 peasycap->video_isoc_maxframesize *
3628 peasycap->video_isoc_framesperdesc;
3629 JOM(4, "%i=video_isoc_buffer_size\n",
3630 peasycap->video_isoc_buffer_size);
3631 if ((PAGE_SIZE << VIDEO_ISOC_ORDER) <
3632 peasycap->video_isoc_buffer_size) {
3633 SAM("MISTAKE: peasycap->video_isoc_buffer_size too big\n");
3636 /*---------------------------------------------------------------------------*/
3637 if (-1 == peasycap->video_interface) {
3638 SAM("MISTAKE: video_interface is unset\n");
3641 if (-1 == peasycap->video_altsetting_on) {
3642 SAM("MISTAKE: video_altsetting_on is unset\n");
3645 if (-1 == peasycap->video_altsetting_off) {
3646 SAM("MISTAKE: video_interface_off is unset\n");
3649 if (-1 == peasycap->video_endpointnumber) {
3650 SAM("MISTAKE: video_endpointnumber is unset\n");
3653 if (-1 == peasycap->video_isoc_maxframesize) {
3654 SAM("MISTAKE: video_isoc_maxframesize is unset\n");
3657 if (-1 == peasycap->video_isoc_buffer_size) {
3658 SAM("MISTAKE: video_isoc_buffer_size is unset\n");
3661 /*---------------------------------------------------------------------------*/
3663 * ALLOCATE MEMORY FOR VIDEO BUFFERS. LISTS MUST BE INITIALIZED FIRST.
3665 /*---------------------------------------------------------------------------*/
3666 INIT_LIST_HEAD(&(peasycap->urb_video_head));
3667 peasycap->purb_video_head = &(peasycap->urb_video_head);
3668 /*---------------------------------------------------------------------------*/
3669 JOM(4, "allocating %i frame buffers of size %li\n",
3670 FRAME_BUFFER_MANY, (long int)FRAME_BUFFER_SIZE);
3671 JOM(4, ".... each scattered over %li pages\n",
3672 FRAME_BUFFER_SIZE/PAGE_SIZE);
3674 for (k = 0; k < FRAME_BUFFER_MANY; k++) {
3675 for (m = 0; m < FRAME_BUFFER_SIZE/PAGE_SIZE; m++) {
3676 if (peasycap->frame_buffer[k][m].pgo)
3677 SAM("attempting to reallocate frame "
3680 pbuf = (void *)__get_free_page(GFP_KERNEL);
3682 SAM("ERROR: Could not allocate frame "
3683 "buffer %i page %i\n", k, m);
3686 peasycap->allocation_video_page += 1;
3687 peasycap->frame_buffer[k][m].pgo = pbuf;
3689 peasycap->frame_buffer[k][m].pto =
3690 peasycap->frame_buffer[k][m].pgo;
3694 peasycap->frame_fill = 0;
3695 peasycap->frame_read = 0;
3696 JOM(4, "allocation of frame buffers done: %i pages\n", k *
3698 /*---------------------------------------------------------------------------*/
3699 JOM(4, "allocating %i field buffers of size %li\n",
3700 FIELD_BUFFER_MANY, (long int)FIELD_BUFFER_SIZE);
3701 JOM(4, ".... each scattered over %li pages\n",
3702 FIELD_BUFFER_SIZE/PAGE_SIZE);
3704 for (k = 0; k < FIELD_BUFFER_MANY; k++) {
3705 for (m = 0; m < FIELD_BUFFER_SIZE/PAGE_SIZE; m++) {
3706 if (peasycap->field_buffer[k][m].pgo) {
3707 SAM("ERROR: attempting to reallocate "
3710 pbuf = (void *) __get_free_page(GFP_KERNEL);
3712 SAM("ERROR: Could not allocate field"
3713 " buffer %i page %i\n", k, m);
3717 peasycap->allocation_video_page += 1;
3718 peasycap->field_buffer[k][m].pgo = pbuf;
3720 peasycap->field_buffer[k][m].pto =
3721 peasycap->field_buffer[k][m].pgo;
3723 peasycap->field_buffer[k][0].kount = 0x0200;
3725 peasycap->field_fill = 0;
3726 peasycap->field_page = 0;
3727 peasycap->field_read = 0;
3728 JOM(4, "allocation of field buffers done: %i pages\n", k *
3730 /*---------------------------------------------------------------------------*/
3731 JOM(4, "allocating %i isoc video buffers of size %i\n",
3732 VIDEO_ISOC_BUFFER_MANY,
3733 peasycap->video_isoc_buffer_size);
3734 JOM(4, ".... each occupying contiguous memory pages\n");
3736 for (k = 0; k < VIDEO_ISOC_BUFFER_MANY; k++) {
3737 pbuf = (void *)__get_free_pages(GFP_KERNEL,
3740 SAM("ERROR: Could not allocate isoc video buffer "
3744 peasycap->allocation_video_page +=
3745 BIT(VIDEO_ISOC_ORDER);
3747 peasycap->video_isoc_buffer[k].pgo = pbuf;
3748 peasycap->video_isoc_buffer[k].pto =
3749 pbuf + peasycap->video_isoc_buffer_size;
3750 peasycap->video_isoc_buffer[k].kount = k;
3752 JOM(4, "allocation of isoc video buffers done: %i pages\n",
3753 k * (0x01 << VIDEO_ISOC_ORDER));
3754 /*---------------------------------------------------------------------------*/
3756 * ALLOCATE AND INITIALIZE MULTIPLE struct urb ...
3758 /*---------------------------------------------------------------------------*/
3759 JOM(4, "allocating %i struct urb.\n", VIDEO_ISOC_BUFFER_MANY);
3760 JOM(4, "using %i=peasycap->video_isoc_framesperdesc\n",
3761 peasycap->video_isoc_framesperdesc);
3762 JOM(4, "using %i=peasycap->video_isoc_maxframesize\n",
3763 peasycap->video_isoc_maxframesize);
3764 JOM(4, "using %i=peasycap->video_isoc_buffer_sizen",
3765 peasycap->video_isoc_buffer_size);
3767 for (k = 0; k < VIDEO_ISOC_BUFFER_MANY; k++) {
3768 purb = usb_alloc_urb(peasycap->video_isoc_framesperdesc,
3771 SAM("ERROR: usb_alloc_urb returned NULL for buffer "
3775 peasycap->allocation_video_urb += 1;
3776 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
3777 pdata_urb = kzalloc(sizeof(struct data_urb), GFP_KERNEL);
3779 SAM("ERROR: Could not allocate struct data_urb.\n");
3782 peasycap->allocation_video_struct +=
3783 sizeof(struct data_urb);
3785 pdata_urb->purb = purb;
3786 pdata_urb->isbuf = k;
3787 pdata_urb->length = 0;
3788 list_add_tail(&(pdata_urb->list_head),
3789 peasycap->purb_video_head);
3790 /*---------------------------------------------------------------------------*/
3792 * ... AND INITIALIZE THEM
3794 /*---------------------------------------------------------------------------*/
3796 JOM(4, "initializing video urbs thus:\n");
3797 JOM(4, " purb->interval = 1;\n");
3798 JOM(4, " purb->dev = peasycap->pusb_device;\n");
3799 JOM(4, " purb->pipe = usb_rcvisocpipe"
3800 "(peasycap->pusb_device,%i);\n",
3801 peasycap->video_endpointnumber);
3802 JOM(4, " purb->transfer_flags = URB_ISO_ASAP;\n");
3803 JOM(4, " purb->transfer_buffer = peasycap->"
3804 "video_isoc_buffer[.].pgo;\n");
3805 JOM(4, " purb->transfer_buffer_length = %i;\n",
3806 peasycap->video_isoc_buffer_size);
3807 JOM(4, " purb->complete = easycap_complete;\n");
3808 JOM(4, " purb->context = peasycap;\n");
3809 JOM(4, " purb->start_frame = 0;\n");
3810 JOM(4, " purb->number_of_packets = %i;\n",
3811 peasycap->video_isoc_framesperdesc);
3812 JOM(4, " for (j = 0; j < %i; j++)\n",
3813 peasycap->video_isoc_framesperdesc);
3815 JOM(4, " purb->iso_frame_desc[j].offset = j*%i;\n",
3816 peasycap->video_isoc_maxframesize);
3817 JOM(4, " purb->iso_frame_desc[j].length = %i;\n",
3818 peasycap->video_isoc_maxframesize);
3823 purb->dev = peasycap->pusb_device;
3824 purb->pipe = usb_rcvisocpipe(peasycap->pusb_device,
3825 peasycap->video_endpointnumber);
3826 purb->transfer_flags = URB_ISO_ASAP;
3827 purb->transfer_buffer = peasycap->video_isoc_buffer[k].pgo;
3828 purb->transfer_buffer_length =
3829 peasycap->video_isoc_buffer_size;
3830 purb->complete = easycap_complete;
3831 purb->context = peasycap;
3832 purb->start_frame = 0;
3833 purb->number_of_packets = peasycap->video_isoc_framesperdesc;
3834 for (j = 0; j < peasycap->video_isoc_framesperdesc; j++) {
3835 purb->iso_frame_desc[j].offset = j *
3836 peasycap->video_isoc_maxframesize;
3837 purb->iso_frame_desc[j].length =
3838 peasycap->video_isoc_maxframesize;
3841 JOM(4, "allocation of %i struct urb done.\n", k);
3842 /*--------------------------------------------------------------------------*/
3844 * SAVE POINTER peasycap IN THIS INTERFACE.
3846 /*--------------------------------------------------------------------------*/
3847 usb_set_intfdata(pusb_interface, peasycap);
3848 /*---------------------------------------------------------------------------*/
3850 * IT IS ESSENTIAL TO INITIALIZE THE HARDWARE BEFORE, RATHER THAN AFTER,
3851 * THE DEVICE IS REGISTERED, BECAUSE SOME VERSIONS OF THE videodev MODULE
3852 * CALL easycap_open() IMMEDIATELY AFTER REGISTRATION, CAUSING A CLASH.
3855 /*---------------------------------------------------------------------------*/
3856 peasycap->ntsc = easycap_ntsc;
3857 JOM(8, "defaulting initially to %s\n",
3858 easycap_ntsc ? "NTSC" : "PAL");
3859 rc = reset(peasycap);
3861 SAM("ERROR: reset() rc = %i\n", rc);
3864 /*--------------------------------------------------------------------------*/
3866 * THE VIDEO DEVICE CAN BE REGISTERED NOW, AS IT IS READY.
3868 /*--------------------------------------------------------------------------*/
3869 if (0 != (v4l2_device_register(&(pusb_interface->dev),
3870 &(peasycap->v4l2_device)))) {
3871 SAM("v4l2_device_register() failed\n");
3874 JOM(4, "registered device instance: %s\n",
3875 &(peasycap->v4l2_device.name[0]));
3877 /*---------------------------------------------------------------------------*/
3882 * THIS IS BELIEVED TO BE HARMLESS, BUT MAY WELL BE UNNECESSARY OR WRONG:
3884 /*---------------------------------------------------------------------------*/
3885 peasycap->video_device.v4l2_dev = NULL;
3886 /*---------------------------------------------------------------------------*/
3889 strcpy(&peasycap->video_device.name[0], "easycapdc60");
3890 peasycap->video_device.fops = &v4l2_fops;
3891 peasycap->video_device.minor = -1;
3892 peasycap->video_device.release = (void *)(&videodev_release);
3894 video_set_drvdata(&(peasycap->video_device), (void *)peasycap);
3896 if (0 != (video_register_device(&(peasycap->video_device),
3897 VFL_TYPE_GRABBER, -1))) {
3898 err("Not able to register with videodev");
3899 videodev_release(&(peasycap->video_device));
3902 (peasycap->registered_video)++;
3903 SAM("registered with videodev: %i=minor\n",
3904 peasycap->video_device.minor);
3905 peasycap->minor = peasycap->video_device.minor;
3907 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
3911 /*--------------------------------------------------------------------------*/
3913 * INTERFACE 1 IS THE AUDIO CONTROL INTERFACE
3914 * INTERFACE 2 IS THE AUDIO STREAMING INTERFACE
3916 /*--------------------------------------------------------------------------*/
3919 SAM("MISTAKE: peasycap is NULL\n");
3922 /*--------------------------------------------------------------------------*/
3924 * SAVE POINTER peasycap IN INTERFACE 1
3926 /*--------------------------------------------------------------------------*/
3927 usb_set_intfdata(pusb_interface, peasycap);
3928 JOM(4, "no initialization required for interface %i\n",
3929 pusb_interface_descriptor->bInterfaceNumber);
3932 /*--------------------------------------------------------------------------*/
3935 SAM("MISTAKE: peasycap is NULL\n");
3939 SAM("ERROR: no viable audio_altsetting_on\n");
3942 peasycap->audio_altsetting_on = okalt[isokalt - 1];
3943 JOM(4, "%i=audio_altsetting_on <====\n",
3944 peasycap->audio_altsetting_on);
3947 peasycap->audio_endpointnumber = okepn[isokalt - 1];
3948 JOM(4, "%i=audio_endpointnumber\n", peasycap->audio_endpointnumber);
3950 peasycap->audio_isoc_maxframesize = okmps[isokalt - 1];
3951 JOM(4, "%i=audio_isoc_maxframesize\n",
3952 peasycap->audio_isoc_maxframesize);
3953 if (0 >= peasycap->audio_isoc_maxframesize) {
3954 SAM("ERROR: bad audio_isoc_maxframesize\n");
3957 if (9 == peasycap->audio_isoc_maxframesize) {
3958 peasycap->ilk |= 0x02;
3959 SAM("audio hardware is microphone\n");
3960 peasycap->microphone = true;
3961 peasycap->audio_pages_per_fragment =
3962 PAGES_PER_AUDIO_FRAGMENT;
3963 } else if (256 == peasycap->audio_isoc_maxframesize) {
3964 peasycap->ilk &= ~0x02;
3965 SAM("audio hardware is AC'97\n");
3966 peasycap->microphone = false;
3967 peasycap->audio_pages_per_fragment =
3968 PAGES_PER_AUDIO_FRAGMENT;
3970 SAM("hardware is unidentified:\n");
3971 SAM("%i=audio_isoc_maxframesize\n",
3972 peasycap->audio_isoc_maxframesize);
3976 peasycap->audio_bytes_per_fragment =
3977 peasycap->audio_pages_per_fragment * PAGE_SIZE;
3978 peasycap->audio_buffer_page_many = (AUDIO_FRAGMENT_MANY *
3979 peasycap->audio_pages_per_fragment);
3981 JOM(4, "%6i=AUDIO_FRAGMENT_MANY\n", AUDIO_FRAGMENT_MANY);
3982 JOM(4, "%6i=audio_pages_per_fragment\n",
3983 peasycap->audio_pages_per_fragment);
3984 JOM(4, "%6i=audio_bytes_per_fragment\n",
3985 peasycap->audio_bytes_per_fragment);
3986 JOM(4, "%6i=audio_buffer_page_many\n",
3987 peasycap->audio_buffer_page_many);
3989 peasycap->audio_isoc_framesperdesc = AUDIO_ISOC_FRAMESPERDESC;
3991 JOM(4, "%i=audio_isoc_framesperdesc\n",
3992 peasycap->audio_isoc_framesperdesc);
3993 if (0 >= peasycap->audio_isoc_framesperdesc) {
3994 SAM("ERROR: bad audio_isoc_framesperdesc\n");
3998 peasycap->audio_isoc_buffer_size =
3999 peasycap->audio_isoc_maxframesize *
4000 peasycap->audio_isoc_framesperdesc;
4001 JOM(4, "%i=audio_isoc_buffer_size\n",
4002 peasycap->audio_isoc_buffer_size);
4003 if (AUDIO_ISOC_BUFFER_SIZE < peasycap->audio_isoc_buffer_size) {
4004 SAM("MISTAKE: audio_isoc_buffer_size bigger "
4005 "than %li=AUDIO_ISOC_BUFFER_SIZE\n",
4006 AUDIO_ISOC_BUFFER_SIZE);
4009 if (-1 == peasycap->audio_interface) {
4010 SAM("MISTAKE: audio_interface is unset\n");
4013 if (-1 == peasycap->audio_altsetting_on) {
4014 SAM("MISTAKE: audio_altsetting_on is unset\n");
4017 if (-1 == peasycap->audio_altsetting_off) {
4018 SAM("MISTAKE: audio_interface_off is unset\n");
4021 if (-1 == peasycap->audio_endpointnumber) {
4022 SAM("MISTAKE: audio_endpointnumber is unset\n");
4025 if (-1 == peasycap->audio_isoc_maxframesize) {
4026 SAM("MISTAKE: audio_isoc_maxframesize is unset\n");
4029 if (-1 == peasycap->audio_isoc_buffer_size) {
4030 SAM("MISTAKE: audio_isoc_buffer_size is unset\n");
4033 /*---------------------------------------------------------------------------*/
4035 * ALLOCATE MEMORY FOR AUDIO BUFFERS. LISTS MUST BE INITIALIZED FIRST.
4037 /*---------------------------------------------------------------------------*/
4038 INIT_LIST_HEAD(&(peasycap->urb_audio_head));
4039 peasycap->purb_audio_head = &(peasycap->urb_audio_head);
4041 #ifdef CONFIG_EASYCAP_OSS
4042 JOM(4, "allocating an audio buffer\n");
4043 JOM(4, ".... scattered over %i pages\n",
4044 peasycap->audio_buffer_page_many);
4046 for (k = 0; k < peasycap->audio_buffer_page_many; k++) {
4047 if (peasycap->audio_buffer[k].pgo) {
4048 SAM("ERROR: attempting to reallocate audio buffers\n");
4050 pbuf = (void *) __get_free_page(GFP_KERNEL);
4052 SAM("ERROR: Could not allocate audio "
4053 "buffer page %i\n", k);
4056 peasycap->allocation_audio_page += 1;
4058 peasycap->audio_buffer[k].pgo = pbuf;
4060 peasycap->audio_buffer[k].pto = peasycap->audio_buffer[k].pgo;
4063 peasycap->audio_fill = 0;
4064 peasycap->audio_read = 0;
4065 JOM(4, "allocation of audio buffer done: %i pages\n", k);
4066 #endif /* CONFIG_EASYCAP_OSS */
4067 /*---------------------------------------------------------------------------*/
4068 JOM(4, "allocating %i isoc audio buffers of size %i\n",
4069 AUDIO_ISOC_BUFFER_MANY,
4070 peasycap->audio_isoc_buffer_size);
4071 JOM(4, ".... each occupying contiguous memory pages\n");
4073 for (k = 0; k < AUDIO_ISOC_BUFFER_MANY; k++) {
4074 pbuf = (void *)__get_free_pages(GFP_KERNEL,
4077 SAM("ERROR: Could not allocate isoc audio buffer "
4081 peasycap->allocation_audio_page +=
4082 BIT(AUDIO_ISOC_ORDER);
4084 peasycap->audio_isoc_buffer[k].pgo = pbuf;
4085 peasycap->audio_isoc_buffer[k].pto = pbuf +
4086 peasycap->audio_isoc_buffer_size;
4087 peasycap->audio_isoc_buffer[k].kount = k;
4089 JOM(4, "allocation of isoc audio buffers done.\n");
4090 /*---------------------------------------------------------------------------*/
4092 * ALLOCATE AND INITIALIZE MULTIPLE struct urb ...
4094 /*---------------------------------------------------------------------------*/
4095 JOM(4, "allocating %i struct urb.\n", AUDIO_ISOC_BUFFER_MANY);
4096 JOM(4, "using %i=peasycap->audio_isoc_framesperdesc\n",
4097 peasycap->audio_isoc_framesperdesc);
4098 JOM(4, "using %i=peasycap->audio_isoc_maxframesize\n",
4099 peasycap->audio_isoc_maxframesize);
4100 JOM(4, "using %i=peasycap->audio_isoc_buffer_size\n",
4101 peasycap->audio_isoc_buffer_size);
4103 for (k = 0; k < AUDIO_ISOC_BUFFER_MANY; k++) {
4104 purb = usb_alloc_urb(peasycap->audio_isoc_framesperdesc,
4107 SAM("ERROR: usb_alloc_urb returned NULL for buffer "
4111 peasycap->allocation_audio_urb += 1 ;
4112 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
4113 pdata_urb = kzalloc(sizeof(struct data_urb), GFP_KERNEL);
4115 SAM("ERROR: Could not allocate struct data_urb.\n");
4118 peasycap->allocation_audio_struct +=
4119 sizeof(struct data_urb);
4121 pdata_urb->purb = purb;
4122 pdata_urb->isbuf = k;
4123 pdata_urb->length = 0;
4124 list_add_tail(&(pdata_urb->list_head),
4125 peasycap->purb_audio_head);
4126 /*---------------------------------------------------------------------------*/
4128 * ... AND INITIALIZE THEM
4130 /*---------------------------------------------------------------------------*/
4132 JOM(4, "initializing audio urbs thus:\n");
4133 JOM(4, " purb->interval = 1;\n");
4134 JOM(4, " purb->dev = peasycap->pusb_device;\n");
4135 JOM(4, " purb->pipe = usb_rcvisocpipe(peasycap->"
4136 "pusb_device,%i);\n",
4137 peasycap->audio_endpointnumber);
4138 JOM(4, " purb->transfer_flags = URB_ISO_ASAP;\n");
4139 JOM(4, " purb->transfer_buffer = "
4140 "peasycap->audio_isoc_buffer[.].pgo;\n");
4141 JOM(4, " purb->transfer_buffer_length = %i;\n",
4142 peasycap->audio_isoc_buffer_size);
4143 #ifdef CONFIG_EASYCAP_OSS
4144 JOM(4, " purb->complete = easyoss_complete;\n");
4145 #else /* CONFIG_EASYCAP_OSS */
4146 JOM(4, " purb->complete = easycap_alsa_complete;\n");
4147 #endif /* CONFIG_EASYCAP_OSS */
4148 JOM(4, " purb->context = peasycap;\n");
4149 JOM(4, " purb->start_frame = 0;\n");
4150 JOM(4, " purb->number_of_packets = %i;\n",
4151 peasycap->audio_isoc_framesperdesc);
4152 JOM(4, " for (j = 0; j < %i; j++)\n",
4153 peasycap->audio_isoc_framesperdesc);
4155 JOM(4, " purb->iso_frame_desc[j].offset = j*%i;\n",
4156 peasycap->audio_isoc_maxframesize);
4157 JOM(4, " purb->iso_frame_desc[j].length = %i;\n",
4158 peasycap->audio_isoc_maxframesize);
4163 purb->dev = peasycap->pusb_device;
4164 purb->pipe = usb_rcvisocpipe(peasycap->pusb_device,
4165 peasycap->audio_endpointnumber);
4166 purb->transfer_flags = URB_ISO_ASAP;
4167 purb->transfer_buffer = peasycap->audio_isoc_buffer[k].pgo;
4168 purb->transfer_buffer_length =
4169 peasycap->audio_isoc_buffer_size;
4170 #ifdef CONFIG_EASYCAP_OSS
4171 purb->complete = easyoss_complete;
4172 #else /* CONFIG_EASYCAP_OSS */
4173 purb->complete = easycap_alsa_complete;
4174 #endif /* CONFIG_EASYCAP_OSS */
4175 purb->context = peasycap;
4176 purb->start_frame = 0;
4177 purb->number_of_packets = peasycap->audio_isoc_framesperdesc;
4178 for (j = 0; j < peasycap->audio_isoc_framesperdesc; j++) {
4179 purb->iso_frame_desc[j].offset = j *
4180 peasycap->audio_isoc_maxframesize;
4181 purb->iso_frame_desc[j].length =
4182 peasycap->audio_isoc_maxframesize;
4185 JOM(4, "allocation of %i struct urb done.\n", k);
4186 /*---------------------------------------------------------------------------*/
4188 * SAVE POINTER peasycap IN THIS INTERFACE.
4190 /*---------------------------------------------------------------------------*/
4191 usb_set_intfdata(pusb_interface, peasycap);
4192 /*---------------------------------------------------------------------------*/
4194 * THE AUDIO DEVICE CAN BE REGISTERED NOW, AS IT IS READY.
4196 /*---------------------------------------------------------------------------*/
4197 #ifndef CONFIG_EASYCAP_OSS
4198 JOM(4, "initializing ALSA card\n");
4200 rc = easycap_alsa_probe(peasycap);
4202 err("easycap_alsa_probe() rc = %i\n", rc);
4205 JOM(8, "kref_get() with %i=kref.refcount.counter\n",
4206 peasycap->kref.refcount.counter);
4207 kref_get(&peasycap->kref);
4208 peasycap->registered_audio++;
4211 #else /* CONFIG_EASYCAP_OSS */
4212 rc = usb_register_dev(pusb_interface, &easyoss_class);
4214 SAY("ERROR: usb_register_dev() failed\n");
4215 usb_set_intfdata(pusb_interface, NULL);
4218 JOM(8, "kref_get() with %i=kref.refcount.counter\n",
4219 peasycap->kref.refcount.counter);
4220 kref_get(&peasycap->kref);
4221 peasycap->registered_audio++;
4223 SAM("easyoss attached to minor #%d\n", pusb_interface->minor);
4224 #endif /* CONFIG_EASYCAP_OSS */
4228 /*---------------------------------------------------------------------------*/
4230 * INTERFACES OTHER THAN 0, 1 AND 2 ARE UNEXPECTED
4232 /*---------------------------------------------------------------------------*/
4234 JOM(4, "ERROR: unexpected interface %i\n", bInterfaceNumber);
4237 SAM("ends successfully for interface %i\n", bInterfaceNumber);
4240 /*****************************************************************************/
4241 /*---------------------------------------------------------------------------*/
4243 * WHEN THIS FUNCTION IS CALLED THE EasyCAP HAS ALREADY BEEN PHYSICALLY
4244 * UNPLUGGED. HENCE peasycap->pusb_device IS NO LONGER VALID.
4246 * THIS FUNCTION AFFECTS BOTH OSS AND ALSA. BEWARE.
4248 /*---------------------------------------------------------------------------*/
4249 static void easycap_usb_disconnect(struct usb_interface *pusb_interface)
4251 struct usb_host_interface *pusb_host_interface;
4252 struct usb_interface_descriptor *pusb_interface_descriptor;
4253 u8 bInterfaceNumber;
4254 struct easycap *peasycap;
4256 struct list_head *plist_head;
4257 struct data_urb *pdata_urb;
4259 struct v4l2_device *pv4l2_device;
4263 pusb_host_interface = pusb_interface->cur_altsetting;
4264 if (!pusb_host_interface) {
4265 JOT(4, "ERROR: pusb_host_interface is NULL\n");
4268 pusb_interface_descriptor = &(pusb_host_interface->desc);
4269 if (!pusb_interface_descriptor) {
4270 JOT(4, "ERROR: pusb_interface_descriptor is NULL\n");
4273 bInterfaceNumber = pusb_interface_descriptor->bInterfaceNumber;
4274 minor = pusb_interface->minor;
4275 JOT(4, "intf[%i]: minor=%i\n", bInterfaceNumber, minor);
4277 if (1 == bInterfaceNumber)
4280 peasycap = usb_get_intfdata(pusb_interface);
4282 SAY("ERROR: peasycap is NULL\n");
4285 /*---------------------------------------------------------------------------*/
4287 * SOME VERSIONS OF THE videodev MODULE OVERWRITE THE DATA WHICH HAS
4288 * BEEN WRITTEN BY THE CALL TO usb_set_intfdata() IN easycap_usb_probe(),
4289 * REPLACING IT WITH A POINTER TO THE EMBEDDED v4l2_device STRUCTURE.
4290 * TO DETECT THIS, THE STRING IN THE easycap.telltale[] BUFFER IS CHECKED.
4292 /*---------------------------------------------------------------------------*/
4293 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
4294 pv4l2_device = usb_get_intfdata(pusb_interface);
4295 if (!pv4l2_device) {
4296 SAY("ERROR: pv4l2_device is NULL\n");
4299 peasycap = (struct easycap *)
4300 container_of(pv4l2_device, struct easycap, v4l2_device);
4302 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
4303 /*---------------------------------------------------------------------------*/
4304 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
4305 SAY("ERROR: bad peasycap: %p\n", peasycap);
4308 /*---------------------------------------------------------------------------*/
4310 * IF THE WAIT QUEUES ARE NOT CLEARED A DEADLOCK IS POSSIBLE. BEWARE.
4312 /*---------------------------------------------------------------------------*/
4313 peasycap->video_eof = 1;
4314 peasycap->audio_eof = 1;
4315 wake_up_interruptible(&(peasycap->wq_video));
4316 wake_up_interruptible(&(peasycap->wq_audio));
4317 /*---------------------------------------------------------------------------*/
4318 switch (bInterfaceNumber) {
4320 if (peasycap->purb_video_head) {
4321 JOM(4, "killing video urbs\n");
4323 list_for_each(plist_head, peasycap->purb_video_head) {
4324 pdata_urb = list_entry(plist_head,
4325 struct data_urb, list_head);
4327 if (pdata_urb->purb) {
4328 usb_kill_urb(pdata_urb->purb);
4333 JOM(4, "%i video urbs killed\n", m);
4337 /*---------------------------------------------------------------------------*/
4339 if (peasycap->purb_audio_head) {
4340 JOM(4, "killing audio urbs\n");
4342 list_for_each(plist_head, peasycap->purb_audio_head) {
4343 pdata_urb = list_entry(plist_head,
4344 struct data_urb, list_head);
4346 if (pdata_urb->purb) {
4347 usb_kill_urb(pdata_urb->purb);
4352 JOM(4, "%i audio urbs killed\n", m);
4359 /*--------------------------------------------------------------------------*/
4363 * THIS PROCEDURE WILL BLOCK UNTIL easycap_poll(), VIDEO IOCTL AND AUDIO
4364 * IOCTL ARE ALL UNLOCKED. IF THIS IS NOT DONE AN Oops CAN OCCUR WHEN
4365 * AN EasyCAP IS UNPLUGGED WHILE THE URBS ARE RUNNING. BEWARE.
4367 /*--------------------------------------------------------------------------*/
4368 kd = isdongle(peasycap);
4369 switch (bInterfaceNumber) {
4371 if (0 <= kd && DONGLE_MANY > kd) {
4372 wake_up_interruptible(&peasycap->wq_video);
4373 JOM(4, "about to lock dongle[%i].mutex_video\n", kd);
4374 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].
4377 "cannot lock dongle[%i].mutex_video\n", kd);
4380 JOM(4, "locked dongle[%i].mutex_video\n", kd);
4382 SAY("ERROR: %i=kd is bad: cannot lock dongle\n", kd);
4384 /*---------------------------------------------------------------------------*/
4385 if (!peasycap->v4l2_device.name[0]) {
4386 SAM("ERROR: peasycap->v4l2_device.name is empty\n");
4387 if (0 <= kd && DONGLE_MANY > kd)
4388 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
4391 v4l2_device_disconnect(&peasycap->v4l2_device);
4392 JOM(4, "v4l2_device_disconnect() OK\n");
4393 v4l2_device_unregister(&peasycap->v4l2_device);
4394 JOM(4, "v4l2_device_unregister() OK\n");
4396 video_unregister_device(&peasycap->video_device);
4397 JOM(4, "intf[%i]: video_unregister_device() minor=%i\n",
4398 bInterfaceNumber, minor);
4399 peasycap->registered_video--;
4400 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
4402 if (0 <= kd && DONGLE_MANY > kd) {
4403 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
4404 JOM(4, "unlocked dongle[%i].mutex_video\n", kd);
4409 if (0 <= kd && DONGLE_MANY > kd) {
4410 wake_up_interruptible(&peasycap->wq_audio);
4411 JOM(4, "about to lock dongle[%i].mutex_audio\n", kd);
4412 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].
4415 "cannot lock dongle[%i].mutex_audio\n", kd);
4418 JOM(4, "locked dongle[%i].mutex_audio\n", kd);
4420 SAY("ERROR: %i=kd is bad: cannot lock dongle\n", kd);
4421 #ifndef CONFIG_EASYCAP_OSS
4422 if (0 != snd_card_free(peasycap->psnd_card)) {
4423 SAY("ERROR: snd_card_free() failed\n");
4425 peasycap->psnd_card = NULL;
4426 (peasycap->registered_audio)--;
4428 #else /* CONFIG_EASYCAP_OSS */
4429 usb_deregister_dev(pusb_interface, &easyoss_class);
4430 peasycap->registered_audio--;
4431 JOM(4, "intf[%i]: usb_deregister_dev()\n", bInterfaceNumber);
4432 SAM("easyoss detached from minor #%d\n", minor);
4433 #endif /* CONFIG_EASYCAP_OSS */
4434 if (0 <= kd && DONGLE_MANY > kd) {
4435 mutex_unlock(&easycapdc60_dongle[kd].mutex_audio);
4436 JOM(4, "unlocked dongle[%i].mutex_audio\n", kd);
4443 /*---------------------------------------------------------------------------*/
4445 * CALL easycap_delete() IF NO REMAINING REFERENCES TO peasycap
4446 * (ALSO WHEN ALSA HAS BEEN IN USE)
4448 /*---------------------------------------------------------------------------*/
4449 if (!peasycap->kref.refcount.counter) {
4450 SAM("ERROR: peasycap->kref.refcount.counter is zero "
4451 "so cannot call kref_put()\n");
4452 SAM("ending unsuccessfully: may cause memory leak\n");
4455 if (0 <= kd && DONGLE_MANY > kd) {
4456 JOM(4, "about to lock dongle[%i].mutex_video\n", kd);
4457 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].mutex_video)) {
4458 SAY("ERROR: cannot lock dongle[%i].mutex_video\n", kd);
4459 SAM("ending unsuccessfully: may cause memory leak\n");
4462 JOM(4, "locked dongle[%i].mutex_video\n", kd);
4463 JOM(4, "about to lock dongle[%i].mutex_audio\n", kd);
4464 if (mutex_lock_interruptible(&easycapdc60_dongle[kd].mutex_audio)) {
4465 SAY("ERROR: cannot lock dongle[%i].mutex_audio\n", kd);
4466 mutex_unlock(&(easycapdc60_dongle[kd].mutex_video));
4467 JOM(4, "unlocked dongle[%i].mutex_video\n", kd);
4468 SAM("ending unsuccessfully: may cause memory leak\n");
4471 JOM(4, "locked dongle[%i].mutex_audio\n", kd);
4473 JOM(4, "intf[%i]: %i=peasycap->kref.refcount.counter\n",
4474 bInterfaceNumber, (int)peasycap->kref.refcount.counter);
4475 kref_put(&peasycap->kref, easycap_delete);
4476 JOT(4, "intf[%i]: kref_put() done.\n", bInterfaceNumber);
4477 if (0 <= kd && DONGLE_MANY > kd) {
4478 mutex_unlock(&(easycapdc60_dongle[kd].mutex_audio));
4479 JOT(4, "unlocked dongle[%i].mutex_audio\n", kd);
4480 mutex_unlock(&easycapdc60_dongle[kd].mutex_video);
4481 JOT(4, "unlocked dongle[%i].mutex_video\n", kd);
4483 /*---------------------------------------------------------------------------*/
4487 /*****************************************************************************/
4489 /*---------------------------------------------------------------------------*/
4491 * PARAMETERS APPLICABLE TO ENTIRE DRIVER, I.E. BOTH VIDEO AND AUDIO
4493 /*---------------------------------------------------------------------------*/
4494 static struct usb_device_id easycap_usb_device_id_table[] = {
4495 {USB_DEVICE(USB_EASYCAP_VENDOR_ID, USB_EASYCAP_PRODUCT_ID)},
4499 MODULE_DEVICE_TABLE(usb, easycap_usb_device_id_table);
4500 struct usb_driver easycap_usb_driver = {
4502 .id_table = easycap_usb_device_id_table,
4503 .probe = easycap_usb_probe,
4504 .disconnect = easycap_usb_disconnect,
4507 static int __init easycap_module_init(void)
4511 printk(KERN_INFO "Easycap version: "EASYCAP_DRIVER_VERSION "\n");
4513 JOT(4, "begins. %i=debug %i=bars %i=gain\n",
4514 easycap_debug, easycap_bars, easycap_gain);
4516 mutex_init(&mutex_dongle);
4517 for (k = 0; k < DONGLE_MANY; k++) {
4518 easycapdc60_dongle[k].peasycap = NULL;
4519 mutex_init(&easycapdc60_dongle[k].mutex_video);
4520 mutex_init(&easycapdc60_dongle[k].mutex_audio);
4522 rc = usb_register(&easycap_usb_driver);
4524 printk(KERN_ERR "Easycap: usb_register failed rc=%d\n", rc);
4528 /*****************************************************************************/
4529 static void __exit easycap_module_exit(void)
4531 usb_deregister(&easycap_usb_driver);
4533 /*****************************************************************************/
4535 module_init(easycap_module_init);
4536 module_exit(easycap_module_exit);
4538 /*****************************************************************************/