pd-video.c 39 KB

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