pvrusb2-std.c 9.3 KB

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