pac_common.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Pixart PAC207BCA / PAC73xx common functions
  3. *
  4. * Copyright (C) 2008 Hans de Goede <j.w.r.degoede@hhs.nl>
  5. * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
  6. * Copyleft (C) 2005 Michel Xhaard mxhaard@magic.fr
  7. *
  8. * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. /* We calculate the autogain at the end of the transfer of a frame, at this
  26. moment a frame with the old settings is being transmitted, and a frame is
  27. being captured with the old settings. So if we adjust the autogain we must
  28. ignore atleast the 2 next frames for the new settings to come into effect
  29. before doing any other adjustments */
  30. #define PAC_AUTOGAIN_IGNORE_FRAMES 3
  31. static const unsigned char pac_sof_marker[5] =
  32. { 0xff, 0xff, 0x00, 0xff, 0x96 };
  33. /*
  34. The following state machine finds the SOF marker sequence
  35. 0xff, 0xff, 0x00, 0xff, 0x96 in a byte stream.
  36. +----------+
  37. | 0: START |<---------------\
  38. +----------+<-\ |
  39. | \---/otherwise |
  40. v 0xff |
  41. +----------+ otherwise |
  42. | 1 |--------------->*
  43. | | ^
  44. +----------+ |
  45. | |
  46. v 0xff |
  47. +----------+<-\0xff |
  48. /->| |--/ |
  49. | | 2 |--------------->*
  50. | | | otherwise ^
  51. | +----------+ |
  52. | | |
  53. | v 0x00 |
  54. | +----------+ |
  55. | | 3 | |
  56. | | |--------------->*
  57. | +----------+ otherwise ^
  58. | | |
  59. 0xff | v 0xff |
  60. | +----------+ |
  61. \--| 4 | |
  62. | |----------------/
  63. +----------+ otherwise
  64. |
  65. v 0x96
  66. +----------+
  67. | FOUND |
  68. +----------+
  69. */
  70. static unsigned char *pac_find_sof(struct gspca_dev *gspca_dev,
  71. unsigned char *m, int len)
  72. {
  73. struct sd *sd = (struct sd *) gspca_dev;
  74. int i;
  75. /* Search for the SOF marker (fixed part) in the header */
  76. for (i = 0; i < len; i++) {
  77. switch (sd->sof_read) {
  78. case 0:
  79. if (m[i] == 0xff)
  80. sd->sof_read = 1;
  81. break;
  82. case 1:
  83. if (m[i] == 0xff)
  84. sd->sof_read = 2;
  85. else
  86. sd->sof_read = 0;
  87. break;
  88. case 2:
  89. switch (m[i]) {
  90. case 0x00:
  91. sd->sof_read = 3;
  92. break;
  93. case 0xff:
  94. /* stay in this state */
  95. break;
  96. default:
  97. sd->sof_read = 0;
  98. }
  99. break;
  100. case 3:
  101. if (m[i] == 0xff)
  102. sd->sof_read = 4;
  103. else
  104. sd->sof_read = 0;
  105. break;
  106. case 4:
  107. switch (m[i]) {
  108. case 0x96:
  109. /* Pattern found */
  110. PDEBUG(D_FRAM,
  111. "SOF found, bytes to analyze: %u."
  112. " Frame starts at byte #%u",
  113. len, i + 1);
  114. sd->sof_read = 0;
  115. return m + i + 1;
  116. break;
  117. case 0xff:
  118. sd->sof_read = 2;
  119. break;
  120. default:
  121. sd->sof_read = 0;
  122. }
  123. break;
  124. default:
  125. sd->sof_read = 0;
  126. }
  127. }
  128. return NULL;
  129. }