pd-video.c 41 KB

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