pd-video.c 40 KB

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