pvrusb2-std.c 9.4 KB

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