pd-video.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  1. #include <linux/fs.h>
  2. #include <linux/vmalloc.h>
  3. #include <linux/videodev2.h>
  4. #include <linux/usb.h>
  5. #include <linux/mm.h>
  6. #include <linux/sched.h>
  7. #include <linux/slab.h>
  8. #include <media/v4l2-ioctl.h>
  9. #include <media/v4l2-dev.h>
  10. #include "pd-common.h"
  11. #include "vendorcmds.h"
  12. #ifdef CONFIG_PM
  13. static int pm_video_suspend(struct poseidon *pd);
  14. static int pm_video_resume(struct poseidon *pd);
  15. #endif
  16. static void iso_bubble_handler(struct work_struct *w);
  17. static int usb_transfer_mode;
  18. module_param(usb_transfer_mode, int, 0644);
  19. MODULE_PARM_DESC(usb_transfer_mode, "0 = Bulk, 1 = Isochronous");
  20. static const struct poseidon_format poseidon_formats[] = {
  21. { "YUV 422", V4L2_PIX_FMT_YUYV, 16, 0},
  22. { "RGB565", V4L2_PIX_FMT_RGB565, 16, 0},
  23. };
  24. static const struct poseidon_tvnorm poseidon_tvnorms[] = {
  25. { V4L2_STD_PAL_D, "PAL-D", TLG_TUNE_VSTD_PAL_D },
  26. { V4L2_STD_PAL_B, "PAL-B", TLG_TUNE_VSTD_PAL_B },
  27. { V4L2_STD_PAL_G, "PAL-G", TLG_TUNE_VSTD_PAL_G },
  28. { V4L2_STD_PAL_H, "PAL-H", TLG_TUNE_VSTD_PAL_H },
  29. { V4L2_STD_PAL_I, "PAL-I", TLG_TUNE_VSTD_PAL_I },
  30. { V4L2_STD_PAL_M, "PAL-M", TLG_TUNE_VSTD_PAL_M },
  31. { V4L2_STD_PAL_N, "PAL-N", TLG_TUNE_VSTD_PAL_N_COMBO },
  32. { V4L2_STD_PAL_Nc, "PAL-Nc", TLG_TUNE_VSTD_PAL_N_COMBO },
  33. { V4L2_STD_NTSC_M, "NTSC-M", TLG_TUNE_VSTD_NTSC_M },
  34. { V4L2_STD_NTSC_M_JP, "NTSC-JP", TLG_TUNE_VSTD_NTSC_M_J },
  35. { V4L2_STD_SECAM_B, "SECAM-B", TLG_TUNE_VSTD_SECAM_B },
  36. { V4L2_STD_SECAM_D, "SECAM-D", TLG_TUNE_VSTD_SECAM_D },
  37. { V4L2_STD_SECAM_G, "SECAM-G", TLG_TUNE_VSTD_SECAM_G },
  38. { V4L2_STD_SECAM_H, "SECAM-H", TLG_TUNE_VSTD_SECAM_H },
  39. { V4L2_STD_SECAM_K, "SECAM-K", TLG_TUNE_VSTD_SECAM_K },
  40. { V4L2_STD_SECAM_K1, "SECAM-K1", TLG_TUNE_VSTD_SECAM_K1 },
  41. { V4L2_STD_SECAM_L, "SECAM-L", TLG_TUNE_VSTD_SECAM_L },
  42. { V4L2_STD_SECAM_LC, "SECAM-LC", TLG_TUNE_VSTD_SECAM_L1 },
  43. };
  44. static const unsigned int POSEIDON_TVNORMS = ARRAY_SIZE(poseidon_tvnorms);
  45. struct pd_audio_mode {
  46. u32 tlg_audio_mode;
  47. u32 v4l2_audio_sub;
  48. u32 v4l2_audio_mode;
  49. };
  50. static const struct pd_audio_mode pd_audio_modes[] = {
  51. { TLG_TUNE_TVAUDIO_MODE_MONO, V4L2_TUNER_SUB_MONO,
  52. V4L2_TUNER_MODE_MONO },
  53. { TLG_TUNE_TVAUDIO_MODE_STEREO, V4L2_TUNER_SUB_STEREO,
  54. V4L2_TUNER_MODE_STEREO },
  55. { TLG_TUNE_TVAUDIO_MODE_LANG_A, V4L2_TUNER_SUB_LANG1,
  56. V4L2_TUNER_MODE_LANG1 },
  57. { TLG_TUNE_TVAUDIO_MODE_LANG_B, V4L2_TUNER_SUB_LANG2,
  58. V4L2_TUNER_MODE_LANG2 },
  59. { TLG_TUNE_TVAUDIO_MODE_LANG_C, V4L2_TUNER_SUB_LANG1,
  60. V4L2_TUNER_MODE_LANG1_LANG2 }
  61. };
  62. static const unsigned int POSEIDON_AUDIOMODS = ARRAY_SIZE(pd_audio_modes);
  63. struct pd_input {
  64. char *name;
  65. uint32_t tlg_src;
  66. };
  67. static const struct pd_input pd_inputs[] = {
  68. { "TV Antenna", TLG_SIG_SRC_ANTENNA },
  69. { "TV Cable", TLG_SIG_SRC_CABLE },
  70. { "TV SVideo", TLG_SIG_SRC_SVIDEO },
  71. { "TV Composite", TLG_SIG_SRC_COMPOSITE }
  72. };
  73. static const unsigned int POSEIDON_INPUTS = ARRAY_SIZE(pd_inputs);
  74. struct poseidon_control {
  75. struct v4l2_queryctrl v4l2_ctrl;
  76. enum cmd_custom_param_id vc_id;
  77. };
  78. static struct poseidon_control controls[] = {
  79. {
  80. { V4L2_CID_BRIGHTNESS, V4L2_CTRL_TYPE_INTEGER,
  81. "brightness", 0, 10000, 1, 100, 0, },
  82. CUST_PARM_ID_BRIGHTNESS_CTRL
  83. }, {
  84. { V4L2_CID_CONTRAST, V4L2_CTRL_TYPE_INTEGER,
  85. "contrast", 0, 10000, 1, 100, 0, },
  86. CUST_PARM_ID_CONTRAST_CTRL,
  87. }, {
  88. { V4L2_CID_HUE, V4L2_CTRL_TYPE_INTEGER,
  89. "hue", 0, 10000, 1, 100, 0, },
  90. CUST_PARM_ID_HUE_CTRL,
  91. }, {
  92. { V4L2_CID_SATURATION, V4L2_CTRL_TYPE_INTEGER,
  93. "saturation", 0, 10000, 1, 100, 0, },
  94. CUST_PARM_ID_SATURATION_CTRL,
  95. },
  96. };
  97. struct video_std_to_audio_std {
  98. v4l2_std_id video_std;
  99. int audio_std;
  100. };
  101. static const struct video_std_to_audio_std video_to_audio_map[] = {
  102. /* country : { 27, 32, 33, 34, 36, 44, 45, 46, 47, 48, 64,
  103. 65, 86, 351, 352, 353, 354, 358, 372, 852, 972 } */
  104. { (V4L2_STD_PAL_I | V4L2_STD_PAL_B | V4L2_STD_PAL_D |
  105. V4L2_STD_SECAM_L | V4L2_STD_SECAM_D), TLG_TUNE_ASTD_NICAM },
  106. /* country : { 1, 52, 54, 55, 886 } */
  107. {V4L2_STD_NTSC_M | V4L2_STD_PAL_N | V4L2_STD_PAL_M, TLG_TUNE_ASTD_BTSC},
  108. /* country : { 81 } */
  109. { V4L2_STD_NTSC_M_JP, TLG_TUNE_ASTD_EIAJ },
  110. /* other country : TLG_TUNE_ASTD_A2 */
  111. };
  112. static const unsigned int map_size = ARRAY_SIZE(video_to_audio_map);
  113. static int get_audio_std(v4l2_std_id v4l2_std)
  114. {
  115. int i = 0;
  116. for (; i < map_size; i++) {
  117. if (v4l2_std & video_to_audio_map[i].video_std)
  118. return video_to_audio_map[i].audio_std;
  119. }
  120. return TLG_TUNE_ASTD_A2;
  121. }
  122. static int vidioc_querycap(struct file *file, void *fh,
  123. struct v4l2_capability *cap)
  124. {
  125. struct front_face *front = fh;
  126. struct poseidon *p = front->pd;
  127. logs(front);
  128. strcpy(cap->driver, "tele-video");
  129. strcpy(cap->card, "Telegent Poseidon");
  130. usb_make_path(p->udev, cap->bus_info, sizeof(cap->bus_info));
  131. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
  132. V4L2_CAP_AUDIO | V4L2_CAP_STREAMING |
  133. V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
  134. return 0;
  135. }
  136. /*====================================================================*/
  137. static void init_copy(struct video_data *video, bool index)
  138. {
  139. struct front_face *front = video->front;
  140. video->field_count = index;
  141. video->lines_copied = 0;
  142. video->prev_left = 0 ;
  143. video->dst = (char *)videobuf_to_vmalloc(front->curr_frame)
  144. + index * video->lines_size;
  145. video->vbi->copied = 0; /* set it here */
  146. }
  147. static bool get_frame(struct front_face *front, int *need_init)
  148. {
  149. struct videobuf_buffer *vb = front->curr_frame;
  150. if (vb)
  151. return true;
  152. spin_lock(&front->queue_lock);
  153. if (!list_empty(&front->active)) {
  154. vb = list_entry(front->active.next,
  155. struct videobuf_buffer, queue);
  156. if (need_init)
  157. *need_init = 1;
  158. front->curr_frame = vb;
  159. list_del_init(&vb->queue);
  160. }
  161. spin_unlock(&front->queue_lock);
  162. return !!vb;
  163. }
  164. /* check if the video's buffer is ready */
  165. static bool get_video_frame(struct front_face *front, struct video_data *video)
  166. {
  167. int need_init = 0;
  168. bool ret = true;
  169. ret = get_frame(front, &need_init);
  170. if (ret && need_init)
  171. init_copy(video, 0);
  172. return ret;
  173. }
  174. static void submit_frame(struct front_face *front)
  175. {
  176. struct videobuf_buffer *vb = front->curr_frame;
  177. if (vb == NULL)
  178. return;
  179. front->curr_frame = NULL;
  180. vb->state = VIDEOBUF_DONE;
  181. vb->field_count++;
  182. v4l2_get_timestamp(&vb->ts);
  183. wake_up(&vb->done);
  184. }
  185. /*
  186. * A frame is composed of two fields. If we receive all the two fields,
  187. * call the submit_frame() to submit the whole frame to applications.
  188. */
  189. static void end_field(struct video_data *video)
  190. {
  191. /* logs(video->front); */
  192. if (1 == video->field_count)
  193. submit_frame(video->front);
  194. else
  195. init_copy(video, 1);
  196. }
  197. static void copy_video_data(struct video_data *video, char *src,
  198. unsigned int count)
  199. {
  200. #define copy_data(len) \
  201. do { \
  202. if (++video->lines_copied > video->lines_per_field) \
  203. goto overflow; \
  204. memcpy(video->dst, src, len);\
  205. video->dst += len + video->lines_size; \
  206. src += len; \
  207. count -= len; \
  208. } while (0)
  209. while (count && count >= video->lines_size) {
  210. if (video->prev_left) {
  211. copy_data(video->prev_left);
  212. video->prev_left = 0;
  213. continue;
  214. }
  215. copy_data(video->lines_size);
  216. }
  217. if (count && count < video->lines_size) {
  218. memcpy(video->dst, src, count);
  219. video->prev_left = video->lines_size - count;
  220. video->dst += count;
  221. }
  222. return;
  223. overflow:
  224. end_field(video);
  225. }
  226. static void check_trailer(struct video_data *video, char *src, int count)
  227. {
  228. struct vbi_data *vbi = video->vbi;
  229. int offset; /* trailer's offset */
  230. char *buf;
  231. offset = (video->context.pix.sizeimage / 2 + vbi->vbi_size / 2)
  232. - (vbi->copied + video->lines_size * video->lines_copied);
  233. if (video->prev_left)
  234. offset -= (video->lines_size - video->prev_left);
  235. if (offset > count || offset <= 0)
  236. goto short_package;
  237. buf = src + offset;
  238. /* trailer : (VFHS) + U32 + U32 + field_num */
  239. if (!strncmp(buf, "VFHS", 4)) {
  240. int field_num = *((u32 *)(buf + 12));
  241. if ((field_num & 1) ^ video->field_count) {
  242. init_copy(video, video->field_count);
  243. return;
  244. }
  245. copy_video_data(video, src, offset);
  246. }
  247. short_package:
  248. end_field(video);
  249. }
  250. /* ========== Check this more carefully! =========== */
  251. static inline void copy_vbi_data(struct vbi_data *vbi,
  252. char *src, unsigned int count)
  253. {
  254. struct front_face *front = vbi->front;
  255. if (front && get_frame(front, NULL)) {
  256. char *buf = videobuf_to_vmalloc(front->curr_frame);
  257. if (vbi->video->field_count)
  258. buf += (vbi->vbi_size / 2);
  259. memcpy(buf + vbi->copied, src, count);
  260. }
  261. vbi->copied += count;
  262. }
  263. /*
  264. * Copy the normal data (VBI or VIDEO) without the trailer.
  265. * VBI is not interlaced, while VIDEO is interlaced.
  266. */
  267. static inline void copy_vbi_video_data(struct video_data *video,
  268. char *src, unsigned int count)
  269. {
  270. struct vbi_data *vbi = video->vbi;
  271. unsigned int vbi_delta = (vbi->vbi_size / 2) - vbi->copied;
  272. if (vbi_delta >= count) {
  273. copy_vbi_data(vbi, src, count);
  274. } else {
  275. if (vbi_delta) {
  276. copy_vbi_data(vbi, src, vbi_delta);
  277. /* we receive the two fields of the VBI*/
  278. if (vbi->front && video->field_count)
  279. submit_frame(vbi->front);
  280. }
  281. copy_video_data(video, src + vbi_delta, count - vbi_delta);
  282. }
  283. }
  284. static void urb_complete_bulk(struct urb *urb)
  285. {
  286. struct front_face *front = urb->context;
  287. struct video_data *video = &front->pd->video_data;
  288. char *src = (char *)urb->transfer_buffer;
  289. int count = urb->actual_length;
  290. int ret = 0;
  291. if (!video->is_streaming || urb->status) {
  292. if (urb->status == -EPROTO)
  293. goto resend_it;
  294. return;
  295. }
  296. if (!get_video_frame(front, video))
  297. goto resend_it;
  298. if (count == urb->transfer_buffer_length)
  299. copy_vbi_video_data(video, src, count);
  300. else
  301. check_trailer(video, src, count);
  302. resend_it:
  303. ret = usb_submit_urb(urb, GFP_ATOMIC);
  304. if (ret)
  305. log(" submit failed: error %d", ret);
  306. }
  307. /************************* for ISO *********************/
  308. #define GET_SUCCESS (0)
  309. #define GET_TRAILER (1)
  310. #define GET_TOO_MUCH_BUBBLE (2)
  311. #define GET_NONE (3)
  312. static int get_chunk(int start, struct urb *urb,
  313. int *head, int *tail, int *bubble_err)
  314. {
  315. struct usb_iso_packet_descriptor *pkt = NULL;
  316. int ret = GET_SUCCESS;
  317. for (*head = *tail = -1; start < urb->number_of_packets; start++) {
  318. pkt = &urb->iso_frame_desc[start];
  319. /* handle the bubble of the Hub */
  320. if (-EOVERFLOW == pkt->status) {
  321. if (++*bubble_err > urb->number_of_packets / 3)
  322. return GET_TOO_MUCH_BUBBLE;
  323. continue;
  324. }
  325. /* This is the gap */
  326. if (pkt->status || pkt->actual_length <= 0
  327. || pkt->actual_length > ISO_PKT_SIZE) {
  328. if (*head != -1)
  329. break;
  330. continue;
  331. }
  332. /* a good isochronous packet */
  333. if (pkt->actual_length == ISO_PKT_SIZE) {
  334. if (*head == -1)
  335. *head = start;
  336. *tail = start;
  337. continue;
  338. }
  339. /* trailer is here */
  340. if (pkt->actual_length < ISO_PKT_SIZE) {
  341. if (*head == -1) {
  342. *head = start;
  343. *tail = start;
  344. return GET_TRAILER;
  345. }
  346. break;
  347. }
  348. }
  349. if (*head == -1 && *tail == -1)
  350. ret = GET_NONE;
  351. return ret;
  352. }
  353. /*
  354. * |__|------|___|-----|_______|
  355. * ^ ^
  356. * | |
  357. * gap gap
  358. */
  359. static void urb_complete_iso(struct urb *urb)
  360. {
  361. struct front_face *front = urb->context;
  362. struct video_data *video = &front->pd->video_data;
  363. int bubble_err = 0, head = 0, tail = 0;
  364. char *src = (char *)urb->transfer_buffer;
  365. int ret = 0;
  366. if (!video->is_streaming)
  367. return;
  368. do {
  369. if (!get_video_frame(front, video))
  370. goto out;
  371. switch (get_chunk(head, urb, &head, &tail, &bubble_err)) {
  372. case GET_SUCCESS:
  373. copy_vbi_video_data(video, src + (head * ISO_PKT_SIZE),
  374. (tail - head + 1) * ISO_PKT_SIZE);
  375. break;
  376. case GET_TRAILER:
  377. check_trailer(video, src + (head * ISO_PKT_SIZE),
  378. ISO_PKT_SIZE);
  379. break;
  380. case GET_NONE:
  381. goto out;
  382. case GET_TOO_MUCH_BUBBLE:
  383. log("\t We got too much bubble");
  384. schedule_work(&video->bubble_work);
  385. return;
  386. }
  387. } while (head = tail + 1, head < urb->number_of_packets);
  388. out:
  389. ret = usb_submit_urb(urb, GFP_ATOMIC);
  390. if (ret)
  391. log("usb_submit_urb err : %d", ret);
  392. }
  393. /*============================= [ end ] =====================*/
  394. static int prepare_iso_urb(struct video_data *video)
  395. {
  396. struct usb_device *udev = video->pd->udev;
  397. int i;
  398. if (video->urb_array[0])
  399. return 0;
  400. for (i = 0; i < SBUF_NUM; i++) {
  401. struct urb *urb;
  402. void *mem;
  403. int j;
  404. urb = usb_alloc_urb(PK_PER_URB, GFP_KERNEL);
  405. if (urb == NULL)
  406. goto out;
  407. video->urb_array[i] = urb;
  408. mem = usb_alloc_coherent(udev,
  409. ISO_PKT_SIZE * PK_PER_URB,
  410. GFP_KERNEL,
  411. &urb->transfer_dma);
  412. urb->complete = urb_complete_iso; /* handler */
  413. urb->dev = udev;
  414. urb->context = video->front;
  415. urb->pipe = usb_rcvisocpipe(udev,
  416. video->endpoint_addr);
  417. urb->interval = 1;
  418. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  419. urb->number_of_packets = PK_PER_URB;
  420. urb->transfer_buffer = mem;
  421. urb->transfer_buffer_length = PK_PER_URB * ISO_PKT_SIZE;
  422. for (j = 0; j < PK_PER_URB; j++) {
  423. urb->iso_frame_desc[j].offset = ISO_PKT_SIZE * j;
  424. urb->iso_frame_desc[j].length = ISO_PKT_SIZE;
  425. }
  426. }
  427. return 0;
  428. out:
  429. for (; i > 0; i--)
  430. ;
  431. return -ENOMEM;
  432. }
  433. /* return the succeeded number of the allocation */
  434. int alloc_bulk_urbs_generic(struct urb **urb_array, int num,
  435. struct usb_device *udev, u8 ep_addr,
  436. int buf_size, gfp_t gfp_flags,
  437. usb_complete_t complete_fn, void *context)
  438. {
  439. int i = 0;
  440. for (; i < num; i++) {
  441. void *mem;
  442. struct urb *urb = usb_alloc_urb(0, gfp_flags);
  443. if (urb == NULL)
  444. return i;
  445. mem = usb_alloc_coherent(udev, buf_size, gfp_flags,
  446. &urb->transfer_dma);
  447. if (mem == NULL) {
  448. usb_free_urb(urb);
  449. return i;
  450. }
  451. usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, ep_addr),
  452. mem, buf_size, complete_fn, context);
  453. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  454. urb_array[i] = urb;
  455. }
  456. return i;
  457. }
  458. void free_all_urb_generic(struct urb **urb_array, int num)
  459. {
  460. int i;
  461. struct urb *urb;
  462. for (i = 0; i < num; i++) {
  463. urb = urb_array[i];
  464. if (urb) {
  465. usb_free_coherent(urb->dev,
  466. urb->transfer_buffer_length,
  467. urb->transfer_buffer,
  468. urb->transfer_dma);
  469. usb_free_urb(urb);
  470. urb_array[i] = NULL;
  471. }
  472. }
  473. }
  474. static int prepare_bulk_urb(struct video_data *video)
  475. {
  476. if (video->urb_array[0])
  477. return 0;
  478. alloc_bulk_urbs_generic(video->urb_array, SBUF_NUM,
  479. video->pd->udev, video->endpoint_addr,
  480. 0x2000, GFP_KERNEL,
  481. urb_complete_bulk, video->front);
  482. return 0;
  483. }
  484. /* free the URBs */
  485. static void free_all_urb(struct video_data *video)
  486. {
  487. free_all_urb_generic(video->urb_array, SBUF_NUM);
  488. }
  489. static void pd_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  490. {
  491. videobuf_vmalloc_free(vb);
  492. vb->state = VIDEOBUF_NEEDS_INIT;
  493. }
  494. static void pd_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  495. {
  496. struct front_face *front = q->priv_data;
  497. vb->state = VIDEOBUF_QUEUED;
  498. list_add_tail(&vb->queue, &front->active);
  499. }
  500. static int pd_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  501. enum v4l2_field field)
  502. {
  503. struct front_face *front = q->priv_data;
  504. int rc;
  505. switch (front->type) {
  506. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  507. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  508. struct v4l2_pix_format *pix;
  509. pix = &front->pd->video_data.context.pix;
  510. vb->size = pix->sizeimage; /* real frame size */
  511. vb->width = pix->width;
  512. vb->height = pix->height;
  513. rc = videobuf_iolock(q, vb, NULL);
  514. if (rc < 0)
  515. return rc;
  516. }
  517. break;
  518. case V4L2_BUF_TYPE_VBI_CAPTURE:
  519. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  520. vb->size = front->pd->vbi_data.vbi_size;
  521. rc = videobuf_iolock(q, vb, NULL);
  522. if (rc < 0)
  523. return rc;
  524. }
  525. break;
  526. default:
  527. return -EINVAL;
  528. }
  529. vb->field = field;
  530. vb->state = VIDEOBUF_PREPARED;
  531. return 0;
  532. }
  533. static int fire_all_urb(struct video_data *video)
  534. {
  535. int i, ret;
  536. video->is_streaming = 1;
  537. for (i = 0; i < SBUF_NUM; i++) {
  538. ret = usb_submit_urb(video->urb_array[i], GFP_KERNEL);
  539. if (ret)
  540. log("(%d) failed: error %d", i, ret);
  541. }
  542. return ret;
  543. }
  544. static int start_video_stream(struct poseidon *pd)
  545. {
  546. struct video_data *video = &pd->video_data;
  547. s32 cmd_status;
  548. send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
  549. send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_START, &cmd_status);
  550. if (pd->cur_transfer_mode) {
  551. prepare_iso_urb(video);
  552. INIT_WORK(&video->bubble_work, iso_bubble_handler);
  553. } else {
  554. /* The bulk mode does not need a bubble handler */
  555. prepare_bulk_urb(video);
  556. }
  557. fire_all_urb(video);
  558. return 0;
  559. }
  560. static int pd_buf_setup(struct videobuf_queue *q, unsigned int *count,
  561. unsigned int *size)
  562. {
  563. struct front_face *front = q->priv_data;
  564. struct poseidon *pd = front->pd;
  565. switch (front->type) {
  566. default:
  567. return -EINVAL;
  568. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  569. struct video_data *video = &pd->video_data;
  570. struct v4l2_pix_format *pix = &video->context.pix;
  571. *size = PAGE_ALIGN(pix->sizeimage);/* page aligned frame size */
  572. if (*count < 4)
  573. *count = 4;
  574. if (1) {
  575. /* same in different altersetting */
  576. video->endpoint_addr = 0x82;
  577. video->vbi = &pd->vbi_data;
  578. video->vbi->video = video;
  579. video->pd = pd;
  580. video->lines_per_field = pix->height / 2;
  581. video->lines_size = pix->width * 2;
  582. video->front = front;
  583. }
  584. return start_video_stream(pd);
  585. }
  586. case V4L2_BUF_TYPE_VBI_CAPTURE: {
  587. struct vbi_data *vbi = &pd->vbi_data;
  588. *size = PAGE_ALIGN(vbi->vbi_size);
  589. log("size : %d", *size);
  590. if (*count == 0)
  591. *count = 4;
  592. }
  593. break;
  594. }
  595. return 0;
  596. }
  597. static struct videobuf_queue_ops pd_video_qops = {
  598. .buf_setup = pd_buf_setup,
  599. .buf_prepare = pd_buf_prepare,
  600. .buf_queue = pd_buf_queue,
  601. .buf_release = pd_buf_release,
  602. };
  603. static int vidioc_enum_fmt(struct file *file, void *fh,
  604. struct v4l2_fmtdesc *f)
  605. {
  606. if (ARRAY_SIZE(poseidon_formats) <= f->index)
  607. return -EINVAL;
  608. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  609. f->flags = 0;
  610. f->pixelformat = poseidon_formats[f->index].fourcc;
  611. strcpy(f->description, poseidon_formats[f->index].name);
  612. return 0;
  613. }
  614. static int vidioc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
  615. {
  616. struct front_face *front = fh;
  617. struct poseidon *pd = front->pd;
  618. logs(front);
  619. f->fmt.pix = pd->video_data.context.pix;
  620. return 0;
  621. }
  622. static int vidioc_try_fmt(struct file *file, void *fh,
  623. struct v4l2_format *f)
  624. {
  625. return 0;
  626. }
  627. /*
  628. * VLC calls VIDIOC_S_STD before VIDIOC_S_FMT, while
  629. * Mplayer calls them in the reverse order.
  630. */
  631. static int pd_vidioc_s_fmt(struct poseidon *pd, struct v4l2_pix_format *pix)
  632. {
  633. struct video_data *video = &pd->video_data;
  634. struct running_context *context = &video->context;
  635. struct v4l2_pix_format *pix_def = &context->pix;
  636. s32 ret = 0, cmd_status = 0, vid_resol;
  637. /* set the pixel format to firmware */
  638. if (pix->pixelformat == V4L2_PIX_FMT_RGB565) {
  639. vid_resol = TLG_TUNER_VID_FORMAT_RGB_565;
  640. } else {
  641. pix->pixelformat = V4L2_PIX_FMT_YUYV;
  642. vid_resol = TLG_TUNER_VID_FORMAT_YUV;
  643. }
  644. ret = send_set_req(pd, VIDEO_STREAM_FMT_SEL,
  645. vid_resol, &cmd_status);
  646. /* set the resolution to firmware */
  647. vid_resol = TLG_TUNE_VID_RES_720;
  648. switch (pix->width) {
  649. case 704:
  650. vid_resol = TLG_TUNE_VID_RES_704;
  651. break;
  652. default:
  653. pix->width = 720;
  654. case 720:
  655. break;
  656. }
  657. ret |= send_set_req(pd, VIDEO_ROSOLU_SEL,
  658. vid_resol, &cmd_status);
  659. if (ret || cmd_status)
  660. return -EBUSY;
  661. pix_def->pixelformat = pix->pixelformat; /* save it */
  662. pix->height = (context->tvnormid & V4L2_STD_525_60) ? 480 : 576;
  663. /* Compare with the default setting */
  664. if ((pix_def->width != pix->width)
  665. || (pix_def->height != pix->height)) {
  666. pix_def->width = pix->width;
  667. pix_def->height = pix->height;
  668. pix_def->bytesperline = pix->width * 2;
  669. pix_def->sizeimage = pix->width * pix->height * 2;
  670. }
  671. *pix = *pix_def;
  672. return 0;
  673. }
  674. static int vidioc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
  675. {
  676. struct front_face *front = fh;
  677. struct poseidon *pd = front->pd;
  678. logs(front);
  679. /* stop VBI here */
  680. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
  681. return -EINVAL;
  682. mutex_lock(&pd->lock);
  683. if (pd->file_for_stream == NULL)
  684. pd->file_for_stream = file;
  685. else if (file != pd->file_for_stream) {
  686. mutex_unlock(&pd->lock);
  687. return -EINVAL;
  688. }
  689. pd_vidioc_s_fmt(pd, &f->fmt.pix);
  690. mutex_unlock(&pd->lock);
  691. return 0;
  692. }
  693. static int vidioc_g_fmt_vbi(struct file *file, void *fh,
  694. struct v4l2_format *v4l2_f)
  695. {
  696. struct front_face *front = fh;
  697. struct poseidon *pd = front->pd;
  698. struct v4l2_vbi_format *vbi_fmt = &v4l2_f->fmt.vbi;
  699. vbi_fmt->samples_per_line = 720 * 2;
  700. vbi_fmt->sampling_rate = 6750000 * 4;
  701. vbi_fmt->sample_format = V4L2_PIX_FMT_GREY;
  702. vbi_fmt->offset = 64 * 4; /*FIXME: why offset */
  703. if (pd->video_data.context.tvnormid & V4L2_STD_525_60) {
  704. vbi_fmt->start[0] = 10;
  705. vbi_fmt->start[1] = 264;
  706. vbi_fmt->count[0] = V4L_NTSC_VBI_LINES;
  707. vbi_fmt->count[1] = V4L_NTSC_VBI_LINES;
  708. } else {
  709. vbi_fmt->start[0] = 6;
  710. vbi_fmt->start[1] = 314;
  711. vbi_fmt->count[0] = V4L_PAL_VBI_LINES;
  712. vbi_fmt->count[1] = V4L_PAL_VBI_LINES;
  713. }
  714. vbi_fmt->flags = V4L2_VBI_UNSYNC;
  715. logs(front);
  716. return 0;
  717. }
  718. static int set_std(struct poseidon *pd, v4l2_std_id *norm)
  719. {
  720. struct video_data *video = &pd->video_data;
  721. struct vbi_data *vbi = &pd->vbi_data;
  722. struct running_context *context;
  723. struct v4l2_pix_format *pix;
  724. s32 i, ret = 0, cmd_status, param;
  725. int height;
  726. for (i = 0; i < POSEIDON_TVNORMS; i++) {
  727. if (*norm & poseidon_tvnorms[i].v4l2_id) {
  728. param = poseidon_tvnorms[i].tlg_tvnorm;
  729. log("name : %s", poseidon_tvnorms[i].name);
  730. goto found;
  731. }
  732. }
  733. return -EINVAL;
  734. found:
  735. mutex_lock(&pd->lock);
  736. ret = send_set_req(pd, VIDEO_STD_SEL, param, &cmd_status);
  737. if (ret || cmd_status)
  738. goto out;
  739. /* Set vbi size and check the height of the frame */
  740. context = &video->context;
  741. context->tvnormid = poseidon_tvnorms[i].v4l2_id;
  742. if (context->tvnormid & V4L2_STD_525_60) {
  743. vbi->vbi_size = V4L_NTSC_VBI_FRAMESIZE;
  744. height = 480;
  745. } else {
  746. vbi->vbi_size = V4L_PAL_VBI_FRAMESIZE;
  747. height = 576;
  748. }
  749. pix = &context->pix;
  750. if (pix->height != height) {
  751. pix->height = height;
  752. pix->sizeimage = pix->width * pix->height * 2;
  753. }
  754. out:
  755. mutex_unlock(&pd->lock);
  756. return ret;
  757. }
  758. static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *norm)
  759. {
  760. struct front_face *front = fh;
  761. logs(front);
  762. return set_std(front->pd, norm);
  763. }
  764. static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *in)
  765. {
  766. struct front_face *front = fh;
  767. if (in->index >= POSEIDON_INPUTS)
  768. return -EINVAL;
  769. strcpy(in->name, pd_inputs[in->index].name);
  770. in->type = V4L2_INPUT_TYPE_TUNER;
  771. /*
  772. * the audio input index mixed with this video input,
  773. * Poseidon only have one audio/video, set to "0"
  774. */
  775. in->audioset = 0;
  776. in->tuner = 0;
  777. in->std = V4L2_STD_ALL;
  778. in->status = 0;
  779. logs(front);
  780. return 0;
  781. }
  782. static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
  783. {
  784. struct front_face *front = fh;
  785. struct poseidon *pd = front->pd;
  786. struct running_context *context = &pd->video_data.context;
  787. logs(front);
  788. *i = context->sig_index;
  789. return 0;
  790. }
  791. /* We can support several inputs */
  792. static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
  793. {
  794. struct front_face *front = fh;
  795. struct poseidon *pd = front->pd;
  796. s32 ret, cmd_status;
  797. if (i >= POSEIDON_INPUTS)
  798. return -EINVAL;
  799. ret = send_set_req(pd, SGNL_SRC_SEL,
  800. pd_inputs[i].tlg_src, &cmd_status);
  801. if (ret)
  802. return ret;
  803. pd->video_data.context.sig_index = i;
  804. return 0;
  805. }
  806. static struct poseidon_control *check_control_id(__u32 id)
  807. {
  808. struct poseidon_control *control = &controls[0];
  809. int array_size = ARRAY_SIZE(controls);
  810. for (; control < &controls[array_size]; control++)
  811. if (control->v4l2_ctrl.id == id)
  812. return control;
  813. return NULL;
  814. }
  815. static int vidioc_queryctrl(struct file *file, void *fh,
  816. struct v4l2_queryctrl *a)
  817. {
  818. struct poseidon_control *control = NULL;
  819. control = check_control_id(a->id);
  820. if (!control)
  821. return -EINVAL;
  822. *a = control->v4l2_ctrl;
  823. return 0;
  824. }
  825. static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
  826. {
  827. struct front_face *front = fh;
  828. struct poseidon *pd = front->pd;
  829. struct poseidon_control *control = NULL;
  830. struct tuner_custom_parameter_s tuner_param;
  831. s32 ret = 0, cmd_status;
  832. control = check_control_id(ctrl->id);
  833. if (!control)
  834. return -EINVAL;
  835. mutex_lock(&pd->lock);
  836. ret = send_get_req(pd, TUNER_CUSTOM_PARAMETER, control->vc_id,
  837. &tuner_param, &cmd_status, sizeof(tuner_param));
  838. mutex_unlock(&pd->lock);
  839. if (ret || cmd_status)
  840. return -1;
  841. ctrl->value = tuner_param.param_value;
  842. return 0;
  843. }
  844. static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
  845. {
  846. struct tuner_custom_parameter_s param = {0};
  847. struct poseidon_control *control = NULL;
  848. struct front_face *front = fh;
  849. struct poseidon *pd = front->pd;
  850. s32 ret = 0, cmd_status, params;
  851. control = check_control_id(a->id);
  852. if (!control)
  853. return -EINVAL;
  854. param.param_value = a->value;
  855. param.param_id = control->vc_id;
  856. params = *(s32 *)&param; /* temp code */
  857. mutex_lock(&pd->lock);
  858. ret = send_set_req(pd, TUNER_CUSTOM_PARAMETER, params, &cmd_status);
  859. ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
  860. mutex_unlock(&pd->lock);
  861. set_current_state(TASK_INTERRUPTIBLE);
  862. schedule_timeout(HZ/4);
  863. return ret;
  864. }
  865. /* Audio ioctls */
  866. static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
  867. {
  868. if (0 != a->index)
  869. return -EINVAL;
  870. a->capability = V4L2_AUDCAP_STEREO;
  871. strcpy(a->name, "USB audio in");
  872. /*Poseidon have no AVL function.*/
  873. a->mode = 0;
  874. return 0;
  875. }
  876. static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
  877. {
  878. a->index = 0;
  879. a->capability = V4L2_AUDCAP_STEREO;
  880. strcpy(a->name, "USB audio in");
  881. a->mode = 0;
  882. return 0;
  883. }
  884. static int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *a)
  885. {
  886. return (0 == a->index) ? 0 : -EINVAL;
  887. }
  888. /* Tuner ioctls */
  889. static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *tuner)
  890. {
  891. struct front_face *front = fh;
  892. struct poseidon *pd = front->pd;
  893. struct tuner_atv_sig_stat_s atv_stat;
  894. s32 count = 5, ret, cmd_status;
  895. int index;
  896. if (0 != tuner->index)
  897. return -EINVAL;
  898. mutex_lock(&pd->lock);
  899. ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_ANALOG_TV,
  900. &atv_stat, &cmd_status, sizeof(atv_stat));
  901. while (atv_stat.sig_lock_busy && count-- && !ret) {
  902. set_current_state(TASK_INTERRUPTIBLE);
  903. schedule_timeout(HZ);
  904. ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_ANALOG_TV,
  905. &atv_stat, &cmd_status, sizeof(atv_stat));
  906. }
  907. mutex_unlock(&pd->lock);
  908. if (debug_mode)
  909. log("P:%d,S:%d", atv_stat.sig_present, atv_stat.sig_strength);
  910. if (ret || cmd_status)
  911. tuner->signal = 0;
  912. else if (atv_stat.sig_present && !atv_stat.sig_strength)
  913. tuner->signal = 0xFFFF;
  914. else
  915. tuner->signal = (atv_stat.sig_strength * 255 / 10) << 8;
  916. strcpy(tuner->name, "Telegent Systems");
  917. tuner->type = V4L2_TUNER_ANALOG_TV;
  918. tuner->rangelow = TUNER_FREQ_MIN / 62500;
  919. tuner->rangehigh = TUNER_FREQ_MAX / 62500;
  920. tuner->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
  921. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  922. index = pd->video_data.context.audio_idx;
  923. tuner->rxsubchans = pd_audio_modes[index].v4l2_audio_sub;
  924. tuner->audmode = pd_audio_modes[index].v4l2_audio_mode;
  925. tuner->afc = 0;
  926. logs(front);
  927. return 0;
  928. }
  929. static int pd_vidioc_s_tuner(struct poseidon *pd, int index)
  930. {
  931. s32 ret = 0, cmd_status, param, audiomode;
  932. mutex_lock(&pd->lock);
  933. param = pd_audio_modes[index].tlg_audio_mode;
  934. ret = send_set_req(pd, TUNER_AUD_MODE, param, &cmd_status);
  935. audiomode = get_audio_std(pd->video_data.context.tvnormid);
  936. ret |= send_set_req(pd, TUNER_AUD_ANA_STD, audiomode,
  937. &cmd_status);
  938. if (!ret)
  939. pd->video_data.context.audio_idx = index;
  940. mutex_unlock(&pd->lock);
  941. return ret;
  942. }
  943. static int vidioc_s_tuner(struct file *file, void *fh, struct v4l2_tuner *a)
  944. {
  945. struct front_face *front = fh;
  946. struct poseidon *pd = front->pd;
  947. int index;
  948. if (0 != a->index)
  949. return -EINVAL;
  950. logs(front);
  951. for (index = 0; index < POSEIDON_AUDIOMODS; index++)
  952. if (a->audmode == pd_audio_modes[index].v4l2_audio_mode)
  953. return pd_vidioc_s_tuner(pd, index);
  954. return -EINVAL;
  955. }
  956. static int vidioc_g_frequency(struct file *file, void *fh,
  957. struct v4l2_frequency *freq)
  958. {
  959. struct front_face *front = fh;
  960. struct poseidon *pd = front->pd;
  961. struct running_context *context = &pd->video_data.context;
  962. if (0 != freq->tuner)
  963. return -EINVAL;
  964. freq->frequency = context->freq;
  965. freq->type = V4L2_TUNER_ANALOG_TV;
  966. return 0;
  967. }
  968. static int set_frequency(struct poseidon *pd, __u32 frequency)
  969. {
  970. s32 ret = 0, param, cmd_status;
  971. struct running_context *context = &pd->video_data.context;
  972. param = frequency * 62500 / 1000;
  973. if (param < TUNER_FREQ_MIN/1000 || param > TUNER_FREQ_MAX / 1000)
  974. return -EINVAL;
  975. mutex_lock(&pd->lock);
  976. ret = send_set_req(pd, TUNE_FREQ_SELECT, param, &cmd_status);
  977. ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
  978. msleep(250); /* wait for a while until the hardware is ready. */
  979. context->freq = frequency;
  980. mutex_unlock(&pd->lock);
  981. return ret;
  982. }
  983. static int vidioc_s_frequency(struct file *file, void *fh,
  984. struct v4l2_frequency *freq)
  985. {
  986. struct front_face *front = fh;
  987. struct poseidon *pd = front->pd;
  988. logs(front);
  989. #ifdef CONFIG_PM
  990. pd->pm_suspend = pm_video_suspend;
  991. pd->pm_resume = pm_video_resume;
  992. #endif
  993. return set_frequency(pd, freq->frequency);
  994. }
  995. static int vidioc_reqbufs(struct file *file, void *fh,
  996. struct v4l2_requestbuffers *b)
  997. {
  998. struct front_face *front = file->private_data;
  999. logs(front);
  1000. return videobuf_reqbufs(&front->q, b);
  1001. }
  1002. static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
  1003. {
  1004. struct front_face *front = file->private_data;
  1005. logs(front);
  1006. return videobuf_querybuf(&front->q, b);
  1007. }
  1008. static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  1009. {
  1010. struct front_face *front = file->private_data;
  1011. return videobuf_qbuf(&front->q, b);
  1012. }
  1013. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  1014. {
  1015. struct front_face *front = file->private_data;
  1016. return videobuf_dqbuf(&front->q, b, file->f_flags & O_NONBLOCK);
  1017. }
  1018. /* Just stop the URBs, do not free the URBs */
  1019. static int usb_transfer_stop(struct video_data *video)
  1020. {
  1021. if (video->is_streaming) {
  1022. int i;
  1023. s32 cmd_status;
  1024. struct poseidon *pd = video->pd;
  1025. video->is_streaming = 0;
  1026. for (i = 0; i < SBUF_NUM; ++i) {
  1027. if (video->urb_array[i])
  1028. usb_kill_urb(video->urb_array[i]);
  1029. }
  1030. send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
  1031. &cmd_status);
  1032. }
  1033. return 0;
  1034. }
  1035. int stop_all_video_stream(struct poseidon *pd)
  1036. {
  1037. struct video_data *video = &pd->video_data;
  1038. struct vbi_data *vbi = &pd->vbi_data;
  1039. mutex_lock(&pd->lock);
  1040. if (video->is_streaming) {
  1041. struct front_face *front = video->front;
  1042. /* stop the URBs */
  1043. usb_transfer_stop(video);
  1044. free_all_urb(video);
  1045. /* stop the host side of VIDEO */
  1046. videobuf_stop(&front->q);
  1047. videobuf_mmap_free(&front->q);
  1048. /* stop the host side of VBI */
  1049. front = vbi->front;
  1050. if (front) {
  1051. videobuf_stop(&front->q);
  1052. videobuf_mmap_free(&front->q);
  1053. }
  1054. }
  1055. mutex_unlock(&pd->lock);
  1056. return 0;
  1057. }
  1058. /*
  1059. * The bubbles can seriously damage the video's quality,
  1060. * though it occurs in very rare situation.
  1061. */
  1062. static void iso_bubble_handler(struct work_struct *w)
  1063. {
  1064. struct video_data *video;
  1065. struct poseidon *pd;
  1066. video = container_of(w, struct video_data, bubble_work);
  1067. pd = video->pd;
  1068. mutex_lock(&pd->lock);
  1069. usb_transfer_stop(video);
  1070. msleep(500);
  1071. start_video_stream(pd);
  1072. mutex_unlock(&pd->lock);
  1073. }
  1074. static int vidioc_streamon(struct file *file, void *fh,
  1075. enum v4l2_buf_type type)
  1076. {
  1077. struct front_face *front = fh;
  1078. logs(front);
  1079. if (unlikely(type != front->type))
  1080. return -EINVAL;
  1081. return videobuf_streamon(&front->q);
  1082. }
  1083. static int vidioc_streamoff(struct file *file, void *fh,
  1084. enum v4l2_buf_type type)
  1085. {
  1086. struct front_face *front = file->private_data;
  1087. logs(front);
  1088. if (unlikely(type != front->type))
  1089. return -EINVAL;
  1090. return videobuf_streamoff(&front->q);
  1091. }
  1092. /* Set the firmware's default values : need altersetting */
  1093. static int pd_video_checkmode(struct poseidon *pd)
  1094. {
  1095. s32 ret = 0, cmd_status, audiomode;
  1096. set_current_state(TASK_INTERRUPTIBLE);
  1097. schedule_timeout(HZ/2);
  1098. /* choose the altersetting */
  1099. ret = usb_set_interface(pd->udev, 0,
  1100. (pd->cur_transfer_mode ?
  1101. ISO_3K_BULK_ALTERNATE_IFACE :
  1102. BULK_ALTERNATE_IFACE));
  1103. if (ret < 0)
  1104. goto error;
  1105. /* set default parameters for PAL-D , with the VBI enabled*/
  1106. ret = set_tuner_mode(pd, TLG_MODE_ANALOG_TV);
  1107. ret |= send_set_req(pd, SGNL_SRC_SEL,
  1108. TLG_SIG_SRC_ANTENNA, &cmd_status);
  1109. ret |= send_set_req(pd, VIDEO_STD_SEL,
  1110. TLG_TUNE_VSTD_PAL_D, &cmd_status);
  1111. ret |= send_set_req(pd, VIDEO_STREAM_FMT_SEL,
  1112. TLG_TUNER_VID_FORMAT_YUV, &cmd_status);
  1113. ret |= send_set_req(pd, VIDEO_ROSOLU_SEL,
  1114. TLG_TUNE_VID_RES_720, &cmd_status);
  1115. ret |= send_set_req(pd, TUNE_FREQ_SELECT, TUNER_FREQ_MIN, &cmd_status);
  1116. ret |= send_set_req(pd, VBI_DATA_SEL, 1, &cmd_status);/* enable vbi */
  1117. /* set the audio */
  1118. audiomode = get_audio_std(pd->video_data.context.tvnormid);
  1119. ret |= send_set_req(pd, TUNER_AUD_ANA_STD, audiomode, &cmd_status);
  1120. ret |= send_set_req(pd, TUNER_AUD_MODE,
  1121. TLG_TUNE_TVAUDIO_MODE_STEREO, &cmd_status);
  1122. ret |= send_set_req(pd, AUDIO_SAMPLE_RATE_SEL,
  1123. ATV_AUDIO_RATE_48K, &cmd_status);
  1124. error:
  1125. return ret;
  1126. }
  1127. #ifdef CONFIG_PM
  1128. static int pm_video_suspend(struct poseidon *pd)
  1129. {
  1130. /* stop audio */
  1131. pm_alsa_suspend(pd);
  1132. /* stop and free all the URBs */
  1133. usb_transfer_stop(&pd->video_data);
  1134. free_all_urb(&pd->video_data);
  1135. /* reset the interface */
  1136. usb_set_interface(pd->udev, 0, 0);
  1137. msleep(300);
  1138. return 0;
  1139. }
  1140. static int restore_v4l2_context(struct poseidon *pd,
  1141. struct running_context *context)
  1142. {
  1143. struct front_face *front = pd->video_data.front;
  1144. pd_video_checkmode(pd);
  1145. set_std(pd, &context->tvnormid);
  1146. vidioc_s_input(NULL, front, context->sig_index);
  1147. pd_vidioc_s_tuner(pd, context->audio_idx);
  1148. pd_vidioc_s_fmt(pd, &context->pix);
  1149. set_frequency(pd, context->freq);
  1150. return 0;
  1151. }
  1152. static int pm_video_resume(struct poseidon *pd)
  1153. {
  1154. struct video_data *video = &pd->video_data;
  1155. /* resume the video */
  1156. /* [1] restore the origin V4L2 parameters */
  1157. restore_v4l2_context(pd, &video->context);
  1158. /* [2] initiate video copy variables */
  1159. if (video->front->curr_frame)
  1160. init_copy(video, 0);
  1161. /* [3] fire urbs */
  1162. start_video_stream(pd);
  1163. /* resume the audio */
  1164. pm_alsa_resume(pd);
  1165. return 0;
  1166. }
  1167. #endif
  1168. void set_debug_mode(struct video_device *vfd, int debug_mode)
  1169. {
  1170. vfd->debug = 0;
  1171. if (debug_mode & 0x1)
  1172. vfd->debug = V4L2_DEBUG_IOCTL;
  1173. if (debug_mode & 0x2)
  1174. vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
  1175. }
  1176. static void init_video_context(struct running_context *context)
  1177. {
  1178. context->sig_index = 0;
  1179. context->audio_idx = 1; /* stereo */
  1180. context->tvnormid = V4L2_STD_PAL_D;
  1181. context->pix = (struct v4l2_pix_format) {
  1182. .width = 720,
  1183. .height = 576,
  1184. .pixelformat = V4L2_PIX_FMT_YUYV,
  1185. .field = V4L2_FIELD_INTERLACED,
  1186. .bytesperline = 720 * 2,
  1187. .sizeimage = 720 * 576 * 2,
  1188. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  1189. .priv = 0
  1190. };
  1191. }
  1192. static int pd_video_open(struct file *file)
  1193. {
  1194. struct video_device *vfd = video_devdata(file);
  1195. struct poseidon *pd = video_get_drvdata(vfd);
  1196. struct front_face *front = NULL;
  1197. int ret = -ENOMEM;
  1198. mutex_lock(&pd->lock);
  1199. usb_autopm_get_interface(pd->interface);
  1200. if (vfd->vfl_type == VFL_TYPE_GRABBER
  1201. && !(pd->state & POSEIDON_STATE_ANALOG)) {
  1202. front = kzalloc(sizeof(struct front_face), GFP_KERNEL);
  1203. if (!front)
  1204. goto out;
  1205. pd->cur_transfer_mode = usb_transfer_mode;/* bulk or iso */
  1206. init_video_context(&pd->video_data.context);
  1207. ret = pd_video_checkmode(pd);
  1208. if (ret < 0) {
  1209. kfree(front);
  1210. ret = -1;
  1211. goto out;
  1212. }
  1213. pd->state |= POSEIDON_STATE_ANALOG;
  1214. front->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1215. pd->video_data.users++;
  1216. set_debug_mode(vfd, debug_mode);
  1217. videobuf_queue_vmalloc_init(&front->q, &pd_video_qops,
  1218. NULL, &front->queue_lock,
  1219. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1220. V4L2_FIELD_INTERLACED,/* video is interlacd */
  1221. sizeof(struct videobuf_buffer),/*it's enough*/
  1222. front, NULL);
  1223. } else if (vfd->vfl_type == VFL_TYPE_VBI
  1224. && !(pd->state & POSEIDON_STATE_VBI)) {
  1225. front = kzalloc(sizeof(struct front_face), GFP_KERNEL);
  1226. if (!front)
  1227. goto out;
  1228. pd->state |= POSEIDON_STATE_VBI;
  1229. front->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1230. pd->vbi_data.front = front;
  1231. pd->vbi_data.users++;
  1232. videobuf_queue_vmalloc_init(&front->q, &pd_video_qops,
  1233. NULL, &front->queue_lock,
  1234. V4L2_BUF_TYPE_VBI_CAPTURE,
  1235. V4L2_FIELD_NONE, /* vbi is NONE mode */
  1236. sizeof(struct videobuf_buffer),
  1237. front, NULL);
  1238. } else {
  1239. /* maybe add FM support here */
  1240. log("other ");
  1241. ret = -EINVAL;
  1242. goto out;
  1243. }
  1244. front->pd = pd;
  1245. front->curr_frame = NULL;
  1246. INIT_LIST_HEAD(&front->active);
  1247. spin_lock_init(&front->queue_lock);
  1248. file->private_data = front;
  1249. kref_get(&pd->kref);
  1250. mutex_unlock(&pd->lock);
  1251. return 0;
  1252. out:
  1253. usb_autopm_put_interface(pd->interface);
  1254. mutex_unlock(&pd->lock);
  1255. return ret;
  1256. }
  1257. static int pd_video_release(struct file *file)
  1258. {
  1259. struct front_face *front = file->private_data;
  1260. struct poseidon *pd = front->pd;
  1261. s32 cmd_status = 0;
  1262. logs(front);
  1263. mutex_lock(&pd->lock);
  1264. if (front->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1265. pd->state &= ~POSEIDON_STATE_ANALOG;
  1266. /* stop the device, and free the URBs */
  1267. usb_transfer_stop(&pd->video_data);
  1268. free_all_urb(&pd->video_data);
  1269. /* stop the firmware */
  1270. send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
  1271. &cmd_status);
  1272. pd->file_for_stream = NULL;
  1273. pd->video_data.users--;
  1274. } else if (front->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1275. pd->state &= ~POSEIDON_STATE_VBI;
  1276. pd->vbi_data.front = NULL;
  1277. pd->vbi_data.users--;
  1278. }
  1279. videobuf_stop(&front->q);
  1280. videobuf_mmap_free(&front->q);
  1281. usb_autopm_put_interface(pd->interface);
  1282. mutex_unlock(&pd->lock);
  1283. kfree(front);
  1284. file->private_data = NULL;
  1285. kref_put(&pd->kref, poseidon_delete);
  1286. return 0;
  1287. }
  1288. static int pd_video_mmap(struct file *file, struct vm_area_struct *vma)
  1289. {
  1290. struct front_face *front = file->private_data;
  1291. return videobuf_mmap_mapper(&front->q, vma);
  1292. }
  1293. static unsigned int pd_video_poll(struct file *file, poll_table *table)
  1294. {
  1295. struct front_face *front = file->private_data;
  1296. return videobuf_poll_stream(file, &front->q, table);
  1297. }
  1298. static ssize_t pd_video_read(struct file *file, char __user *buffer,
  1299. size_t count, loff_t *ppos)
  1300. {
  1301. struct front_face *front = file->private_data;
  1302. return videobuf_read_stream(&front->q, buffer, count, ppos,
  1303. 0, file->f_flags & O_NONBLOCK);
  1304. }
  1305. /* This struct works for both VIDEO and VBI */
  1306. static const struct v4l2_file_operations pd_video_fops = {
  1307. .owner = THIS_MODULE,
  1308. .open = pd_video_open,
  1309. .release = pd_video_release,
  1310. .read = pd_video_read,
  1311. .poll = pd_video_poll,
  1312. .mmap = pd_video_mmap,
  1313. .ioctl = video_ioctl2, /* maybe changed in future */
  1314. };
  1315. static const struct v4l2_ioctl_ops pd_video_ioctl_ops = {
  1316. .vidioc_querycap = vidioc_querycap,
  1317. /* Video format */
  1318. .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
  1319. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
  1320. .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
  1321. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi, /* VBI */
  1322. .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
  1323. /* Input */
  1324. .vidioc_g_input = vidioc_g_input,
  1325. .vidioc_s_input = vidioc_s_input,
  1326. .vidioc_enum_input = vidioc_enum_input,
  1327. /* Audio ioctls */
  1328. .vidioc_enumaudio = vidioc_enumaudio,
  1329. .vidioc_g_audio = vidioc_g_audio,
  1330. .vidioc_s_audio = vidioc_s_audio,
  1331. /* Tuner ioctls */
  1332. .vidioc_g_tuner = vidioc_g_tuner,
  1333. .vidioc_s_tuner = vidioc_s_tuner,
  1334. .vidioc_s_std = vidioc_s_std,
  1335. .vidioc_g_frequency = vidioc_g_frequency,
  1336. .vidioc_s_frequency = vidioc_s_frequency,
  1337. /* Buffer handlers */
  1338. .vidioc_reqbufs = vidioc_reqbufs,
  1339. .vidioc_querybuf = vidioc_querybuf,
  1340. .vidioc_qbuf = vidioc_qbuf,
  1341. .vidioc_dqbuf = vidioc_dqbuf,
  1342. /* Stream on/off */
  1343. .vidioc_streamon = vidioc_streamon,
  1344. .vidioc_streamoff = vidioc_streamoff,
  1345. /* Control handling */
  1346. .vidioc_queryctrl = vidioc_queryctrl,
  1347. .vidioc_g_ctrl = vidioc_g_ctrl,
  1348. .vidioc_s_ctrl = vidioc_s_ctrl,
  1349. };
  1350. static struct video_device pd_video_template = {
  1351. .name = "Telegent-Video",
  1352. .fops = &pd_video_fops,
  1353. .minor = -1,
  1354. .release = video_device_release,
  1355. .tvnorms = V4L2_STD_ALL,
  1356. .ioctl_ops = &pd_video_ioctl_ops,
  1357. };
  1358. struct video_device *vdev_init(struct poseidon *pd, struct video_device *tmp)
  1359. {
  1360. struct video_device *vfd;
  1361. vfd = video_device_alloc();
  1362. if (vfd == NULL)
  1363. return NULL;
  1364. *vfd = *tmp;
  1365. vfd->minor = -1;
  1366. vfd->v4l2_dev = &pd->v4l2_dev;
  1367. /*vfd->parent = &(pd->udev->dev); */
  1368. vfd->release = video_device_release;
  1369. video_set_drvdata(vfd, pd);
  1370. return vfd;
  1371. }
  1372. void destroy_video_device(struct video_device **v_dev)
  1373. {
  1374. struct video_device *dev = *v_dev;
  1375. if (dev == NULL)
  1376. return;
  1377. if (video_is_registered(dev))
  1378. video_unregister_device(dev);
  1379. else
  1380. video_device_release(dev);
  1381. *v_dev = NULL;
  1382. }
  1383. void pd_video_exit(struct poseidon *pd)
  1384. {
  1385. struct video_data *video = &pd->video_data;
  1386. struct vbi_data *vbi = &pd->vbi_data;
  1387. destroy_video_device(&video->v_dev);
  1388. destroy_video_device(&vbi->v_dev);
  1389. log();
  1390. }
  1391. int pd_video_init(struct poseidon *pd)
  1392. {
  1393. struct video_data *video = &pd->video_data;
  1394. struct vbi_data *vbi = &pd->vbi_data;
  1395. int ret = -ENOMEM;
  1396. video->v_dev = vdev_init(pd, &pd_video_template);
  1397. if (video->v_dev == NULL)
  1398. goto out;
  1399. ret = video_register_device(video->v_dev, VFL_TYPE_GRABBER, -1);
  1400. if (ret != 0)
  1401. goto out;
  1402. /* VBI uses the same template as video */
  1403. vbi->v_dev = vdev_init(pd, &pd_video_template);
  1404. if (vbi->v_dev == NULL) {
  1405. ret = -ENOMEM;
  1406. goto out;
  1407. }
  1408. ret = video_register_device(vbi->v_dev, VFL_TYPE_VBI, -1);
  1409. if (ret != 0)
  1410. goto out;
  1411. log("register VIDEO/VBI devices");
  1412. return 0;
  1413. out:
  1414. log("VIDEO/VBI devices register failed, : %d", ret);
  1415. pd_video_exit(pd);
  1416. return ret;
  1417. }