pvrusb2-std.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. *
  3. *
  4. * Copyright (C) 2005 Mike Isely <isely@pobox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include "pvrusb2-std.h"
  21. #include "pvrusb2-debug.h"
  22. #include <asm/string.h>
  23. #include <linux/slab.h>
  24. struct std_name {
  25. const char *name;
  26. v4l2_std_id id;
  27. };
  28. #define CSTD_PAL \
  29. (V4L2_STD_PAL_B| \
  30. V4L2_STD_PAL_B1| \
  31. V4L2_STD_PAL_G| \
  32. V4L2_STD_PAL_H| \
  33. V4L2_STD_PAL_I| \
  34. V4L2_STD_PAL_D| \
  35. V4L2_STD_PAL_D1| \
  36. V4L2_STD_PAL_K| \
  37. V4L2_STD_PAL_M| \
  38. V4L2_STD_PAL_N| \
  39. V4L2_STD_PAL_Nc| \
  40. V4L2_STD_PAL_60)
  41. #define CSTD_NTSC \
  42. (V4L2_STD_NTSC_M| \
  43. V4L2_STD_NTSC_M_JP| \
  44. V4L2_STD_NTSC_M_KR| \
  45. V4L2_STD_NTSC_443)
  46. #define CSTD_ATSC \
  47. (V4L2_STD_ATSC_8_VSB| \
  48. V4L2_STD_ATSC_16_VSB)
  49. #define CSTD_SECAM \
  50. (V4L2_STD_SECAM_B| \
  51. V4L2_STD_SECAM_D| \
  52. V4L2_STD_SECAM_G| \
  53. V4L2_STD_SECAM_H| \
  54. V4L2_STD_SECAM_K| \
  55. V4L2_STD_SECAM_K1| \
  56. V4L2_STD_SECAM_L| \
  57. V4L2_STD_SECAM_LC)
  58. #define TSTD_B (V4L2_STD_PAL_B|V4L2_STD_SECAM_B)
  59. #define TSTD_B1 (V4L2_STD_PAL_B1)
  60. #define TSTD_D (V4L2_STD_PAL_D|V4L2_STD_SECAM_D)
  61. #define TSTD_D1 (V4L2_STD_PAL_D1)
  62. #define TSTD_G (V4L2_STD_PAL_G|V4L2_STD_SECAM_G)
  63. #define TSTD_H (V4L2_STD_PAL_H|V4L2_STD_SECAM_H)
  64. #define TSTD_I (V4L2_STD_PAL_I)
  65. #define TSTD_K (V4L2_STD_PAL_K|V4L2_STD_SECAM_K)
  66. #define TSTD_K1 (V4L2_STD_SECAM_K1)
  67. #define TSTD_L (V4L2_STD_SECAM_L)
  68. #define TSTD_M (V4L2_STD_PAL_M|V4L2_STD_NTSC_M)
  69. #define TSTD_N (V4L2_STD_PAL_N)
  70. #define TSTD_Nc (V4L2_STD_PAL_Nc)
  71. #define TSTD_60 (V4L2_STD_PAL_60)
  72. #define CSTD_ALL (CSTD_PAL|CSTD_NTSC|CSTD_ATSC|CSTD_SECAM)
  73. /* Mapping of standard bits to color system */
  74. static const struct std_name std_groups[] = {
  75. {"PAL",CSTD_PAL},
  76. {"NTSC",CSTD_NTSC},
  77. {"SECAM",CSTD_SECAM},
  78. {"ATSC",CSTD_ATSC},
  79. };
  80. /* Mapping of standard bits to modulation system */
  81. static const struct std_name std_items[] = {
  82. {"B",TSTD_B},
  83. {"B1",TSTD_B1},
  84. {"D",TSTD_D},
  85. {"D1",TSTD_D1},
  86. {"G",TSTD_G},
  87. {"H",TSTD_H},
  88. {"I",TSTD_I},
  89. {"K",TSTD_K},
  90. {"K1",TSTD_K1},
  91. {"L",TSTD_L},
  92. {"LC",V4L2_STD_SECAM_LC},
  93. {"M",TSTD_M},
  94. {"Mj",V4L2_STD_NTSC_M_JP},
  95. {"443",V4L2_STD_NTSC_443},
  96. {"Mk",V4L2_STD_NTSC_M_KR},
  97. {"N",TSTD_N},
  98. {"Nc",TSTD_Nc},
  99. {"60",TSTD_60},
  100. {"8VSB",V4L2_STD_ATSC_8_VSB},
  101. {"16VSB",V4L2_STD_ATSC_16_VSB},
  102. };
  103. // Search an array of std_name structures and return a pointer to the
  104. // element with the matching name.
  105. static const struct std_name *find_std_name(const struct std_name *arrPtr,
  106. unsigned int arrSize,
  107. const char *bufPtr,
  108. unsigned int bufSize)
  109. {
  110. unsigned int idx;
  111. const struct std_name *p;
  112. for (idx = 0; idx < arrSize; idx++) {
  113. p = arrPtr + idx;
  114. if (strlen(p->name) != bufSize) continue;
  115. if (!memcmp(bufPtr,p->name,bufSize)) return p;
  116. }
  117. return NULL;
  118. }
  119. int pvr2_std_str_to_id(v4l2_std_id *idPtr,const char *bufPtr,
  120. unsigned int bufSize)
  121. {
  122. v4l2_std_id id = 0;
  123. v4l2_std_id cmsk = 0;
  124. v4l2_std_id t;
  125. int mMode = 0;
  126. unsigned int cnt;
  127. char ch;
  128. const struct std_name *sp;
  129. while (bufSize) {
  130. if (!mMode) {
  131. cnt = 0;
  132. while ((cnt < bufSize) && (bufPtr[cnt] != '-')) cnt++;
  133. if (cnt >= bufSize) return 0; // No more characters
  134. sp = find_std_name(std_groups, ARRAY_SIZE(std_groups),
  135. bufPtr,cnt);
  136. if (!sp) return 0; // Illegal color system name
  137. cnt++;
  138. bufPtr += cnt;
  139. bufSize -= cnt;
  140. mMode = !0;
  141. cmsk = sp->id;
  142. continue;
  143. }
  144. cnt = 0;
  145. while (cnt < bufSize) {
  146. ch = bufPtr[cnt];
  147. if (ch == ';') {
  148. mMode = 0;
  149. break;
  150. }
  151. if (ch == '/') break;
  152. cnt++;
  153. }
  154. sp = find_std_name(std_items, ARRAY_SIZE(std_items),
  155. bufPtr,cnt);
  156. if (!sp) return 0; // Illegal modulation system ID
  157. t = sp->id & cmsk;
  158. if (!t) return 0; // Specific color + modulation system illegal
  159. id |= t;
  160. if (cnt < bufSize) cnt++;
  161. bufPtr += cnt;
  162. bufSize -= cnt;
  163. }
  164. if (idPtr) *idPtr = id;
  165. return !0;
  166. }
  167. unsigned int pvr2_std_id_to_str(char *bufPtr, unsigned int bufSize,
  168. v4l2_std_id id)
  169. {
  170. unsigned int idx1,idx2;
  171. const struct std_name *ip,*gp;
  172. int gfl,cfl;
  173. unsigned int c1,c2;
  174. cfl = 0;
  175. c1 = 0;
  176. for (idx1 = 0; idx1 < ARRAY_SIZE(std_groups); idx1++) {
  177. gp = std_groups + idx1;
  178. gfl = 0;
  179. for (idx2 = 0; idx2 < ARRAY_SIZE(std_items); idx2++) {
  180. ip = std_items + idx2;
  181. if (!(gp->id & ip->id & id)) continue;
  182. if (!gfl) {
  183. if (cfl) {
  184. c2 = scnprintf(bufPtr,bufSize,";");
  185. c1 += c2;
  186. bufSize -= c2;
  187. bufPtr += c2;
  188. }
  189. cfl = !0;
  190. c2 = scnprintf(bufPtr,bufSize,
  191. "%s-",gp->name);
  192. gfl = !0;
  193. } else {
  194. c2 = scnprintf(bufPtr,bufSize,"/");
  195. }
  196. c1 += c2;
  197. bufSize -= c2;
  198. bufPtr += c2;
  199. c2 = scnprintf(bufPtr,bufSize,
  200. ip->name);
  201. c1 += c2;
  202. bufSize -= c2;
  203. bufPtr += c2;
  204. }
  205. }
  206. return c1;
  207. }
  208. // Template data for possible enumerated video standards. Here we group
  209. // standards which share common frame rates and resolution.
  210. static struct v4l2_standard generic_standards[] = {
  211. {
  212. .id = (TSTD_B|TSTD_B1|
  213. TSTD_D|TSTD_D1|
  214. TSTD_G|
  215. TSTD_H|
  216. TSTD_I|
  217. TSTD_K|TSTD_K1|
  218. TSTD_L|
  219. V4L2_STD_SECAM_LC |
  220. TSTD_N|TSTD_Nc),
  221. .frameperiod =
  222. {
  223. .numerator = 1,
  224. .denominator= 25
  225. },
  226. .framelines = 625,
  227. .reserved = {0,0,0,0}
  228. }, {
  229. .id = (TSTD_M|
  230. V4L2_STD_NTSC_M_JP|
  231. V4L2_STD_NTSC_M_KR),
  232. .frameperiod =
  233. {
  234. .numerator = 1001,
  235. .denominator= 30000
  236. },
  237. .framelines = 525,
  238. .reserved = {0,0,0,0}
  239. }, { // This is a total wild guess
  240. .id = (TSTD_60),
  241. .frameperiod =
  242. {
  243. .numerator = 1001,
  244. .denominator= 30000
  245. },
  246. .framelines = 525,
  247. .reserved = {0,0,0,0}
  248. }, { // This is total wild guess
  249. .id = V4L2_STD_NTSC_443,
  250. .frameperiod =
  251. {
  252. .numerator = 1001,
  253. .denominator= 30000
  254. },
  255. .framelines = 525,
  256. .reserved = {0,0,0,0}
  257. }
  258. };
  259. static struct v4l2_standard *match_std(v4l2_std_id id)
  260. {
  261. unsigned int idx;
  262. for (idx = 0; idx < ARRAY_SIZE(generic_standards); idx++) {
  263. if (generic_standards[idx].id & id) {
  264. return generic_standards + idx;
  265. }
  266. }
  267. return NULL;
  268. }
  269. static int pvr2_std_fill(struct v4l2_standard *std,v4l2_std_id id)
  270. {
  271. struct v4l2_standard *template;
  272. int idx;
  273. unsigned int bcnt;
  274. template = match_std(id);
  275. if (!template) return 0;
  276. idx = std->index;
  277. memcpy(std,template,sizeof(*template));
  278. std->index = idx;
  279. std->id = id;
  280. bcnt = pvr2_std_id_to_str(std->name,sizeof(std->name)-1,id);
  281. std->name[bcnt] = 0;
  282. pvr2_trace(PVR2_TRACE_STD,"Set up standard idx=%u name=%s",
  283. std->index,std->name);
  284. return !0;
  285. }
  286. /* These are special cases of combined standards that we should enumerate
  287. separately if the component pieces are present. */
  288. static v4l2_std_id std_mixes[] = {
  289. V4L2_STD_PAL_B | V4L2_STD_PAL_G,
  290. V4L2_STD_PAL_D | V4L2_STD_PAL_K,
  291. V4L2_STD_SECAM_B | V4L2_STD_SECAM_G,
  292. V4L2_STD_SECAM_D | V4L2_STD_SECAM_K,
  293. };
  294. struct v4l2_standard *pvr2_std_create_enum(unsigned int *countptr,
  295. v4l2_std_id id)
  296. {
  297. unsigned int std_cnt = 0;
  298. unsigned int idx,bcnt,idx2;
  299. v4l2_std_id idmsk,cmsk,fmsk;
  300. struct v4l2_standard *stddefs;
  301. if (pvrusb2_debug & PVR2_TRACE_STD) {
  302. char buf[100];
  303. bcnt = pvr2_std_id_to_str(buf,sizeof(buf),id);
  304. pvr2_trace(
  305. PVR2_TRACE_STD,"Mapping standards mask=0x%x (%.*s)",
  306. (int)id,bcnt,buf);
  307. }
  308. *countptr = 0;
  309. std_cnt = 0;
  310. fmsk = 0;
  311. for (idmsk = 1, cmsk = id; cmsk; idmsk <<= 1) {
  312. if (!(idmsk & cmsk)) continue;
  313. cmsk &= ~idmsk;
  314. if (match_std(idmsk)) {
  315. std_cnt++;
  316. continue;
  317. }
  318. fmsk |= idmsk;
  319. }
  320. for (idx2 = 0; idx2 < ARRAY_SIZE(std_mixes); idx2++) {
  321. if ((id & std_mixes[idx2]) == std_mixes[idx2]) std_cnt++;
  322. }
  323. /* Don't complain about ATSC standard values */
  324. fmsk &= ~CSTD_ATSC;
  325. if (fmsk) {
  326. char buf[100];
  327. bcnt = pvr2_std_id_to_str(buf,sizeof(buf),fmsk);
  328. pvr2_trace(
  329. PVR2_TRACE_ERROR_LEGS,
  330. "WARNING:"
  331. " Failed to classify the following standard(s): %.*s",
  332. bcnt,buf);
  333. }
  334. pvr2_trace(PVR2_TRACE_STD,"Setting up %u unique standard(s)",
  335. std_cnt);
  336. if (!std_cnt) return NULL; // paranoia
  337. stddefs = kzalloc(sizeof(struct v4l2_standard) * std_cnt,
  338. GFP_KERNEL);
  339. if (!stddefs)
  340. return NULL;
  341. for (idx = 0; idx < std_cnt; idx++)
  342. stddefs[idx].index = idx;
  343. idx = 0;
  344. /* Enumerate potential special cases */
  345. for (idx2 = 0; (idx2 < ARRAY_SIZE(std_mixes)) && (idx < std_cnt);
  346. idx2++) {
  347. if (!(id & std_mixes[idx2])) continue;
  348. if (pvr2_std_fill(stddefs+idx,std_mixes[idx2])) idx++;
  349. }
  350. /* Now enumerate individual pieces */
  351. for (idmsk = 1, cmsk = id; cmsk && (idx < std_cnt); idmsk <<= 1) {
  352. if (!(idmsk & cmsk)) continue;
  353. cmsk &= ~idmsk;
  354. if (!pvr2_std_fill(stddefs+idx,idmsk)) continue;
  355. idx++;
  356. }
  357. *countptr = std_cnt;
  358. return stddefs;
  359. }
  360. v4l2_std_id pvr2_std_get_usable(void)
  361. {
  362. return CSTD_ALL;
  363. }