pvrusb2-std.c 8.5 KB

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