pd-video.c 41 KB

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