/drivers/media/video/tlg2300/pd-video.c

https://bitbucket.org/abioy/linux · C · 1668 lines · 1332 code · 251 blank · 85 comment · 201 complexity · 23ee765d2f769539ea852be170be20f3 MD5 · raw file

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