pd-video.c 39 KB

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