pvrusb2-std.c 8.9 KB

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