pd-video.c 40 KB

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