pd-video.c 40 KB

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