pd-video.c 41 KB

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