pd-radio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #include <linux/init.h>
  2. #include <linux/list.h>
  3. #include <linux/module.h>
  4. #include <linux/kernel.h>
  5. #include <linux/bitmap.h>
  6. #include <linux/usb.h>
  7. #include <linux/i2c.h>
  8. #include <media/v4l2-dev.h>
  9. #include <linux/mm.h>
  10. #include <linux/mutex.h>
  11. #include <media/v4l2-ioctl.h>
  12. #include <linux/sched.h>
  13. #include "pd-common.h"
  14. #include "vendorcmds.h"
  15. static int set_frequency(struct poseidon *p, __u32 frequency);
  16. static int poseidon_fm_close(struct file *filp);
  17. static int poseidon_fm_open(struct file *filp);
  18. #define TUNER_FREQ_MIN_FM 76000000
  19. #define TUNER_FREQ_MAX_FM 108000000
  20. #define MAX_PREEMPHASIS (V4L2_PREEMPHASIS_75_uS + 1)
  21. static int preemphasis[MAX_PREEMPHASIS] = {
  22. TLG_TUNE_ASTD_NONE, /* V4L2_PREEMPHASIS_DISABLED */
  23. TLG_TUNE_ASTD_FM_EUR, /* V4L2_PREEMPHASIS_50_uS */
  24. TLG_TUNE_ASTD_FM_US, /* V4L2_PREEMPHASIS_75_uS */
  25. };
  26. static int poseidon_check_mode_radio(struct poseidon *p)
  27. {
  28. int ret;
  29. u32 status;
  30. set_current_state(TASK_INTERRUPTIBLE);
  31. schedule_timeout(HZ/2);
  32. ret = usb_set_interface(p->udev, 0, BULK_ALTERNATE_IFACE);
  33. if (ret < 0)
  34. goto out;
  35. ret = set_tuner_mode(p, TLG_MODE_FM_RADIO);
  36. if (ret != 0)
  37. goto out;
  38. ret = send_set_req(p, SGNL_SRC_SEL, TLG_SIG_SRC_ANTENNA, &status);
  39. ret = send_set_req(p, TUNER_AUD_ANA_STD,
  40. p->radio_data.pre_emphasis, &status);
  41. ret |= send_set_req(p, TUNER_AUD_MODE,
  42. TLG_TUNE_TVAUDIO_MODE_STEREO, &status);
  43. ret |= send_set_req(p, AUDIO_SAMPLE_RATE_SEL,
  44. ATV_AUDIO_RATE_48K, &status);
  45. ret |= send_set_req(p, TUNE_FREQ_SELECT, TUNER_FREQ_MIN_FM, &status);
  46. out:
  47. return ret;
  48. }
  49. #ifdef CONFIG_PM
  50. static int pm_fm_suspend(struct poseidon *p)
  51. {
  52. logpm(p);
  53. pm_alsa_suspend(p);
  54. usb_set_interface(p->udev, 0, 0);
  55. msleep(300);
  56. return 0;
  57. }
  58. static int pm_fm_resume(struct poseidon *p)
  59. {
  60. logpm(p);
  61. poseidon_check_mode_radio(p);
  62. set_frequency(p, p->radio_data.fm_freq);
  63. pm_alsa_resume(p);
  64. return 0;
  65. }
  66. #endif
  67. static int poseidon_fm_open(struct file *filp)
  68. {
  69. struct video_device *vfd = video_devdata(filp);
  70. struct poseidon *p = video_get_drvdata(vfd);
  71. int ret = 0;
  72. if (!p)
  73. return -1;
  74. mutex_lock(&p->lock);
  75. if (p->state & POSEIDON_STATE_DISCONNECT) {
  76. ret = -ENODEV;
  77. goto out;
  78. }
  79. if (p->state && !(p->state & POSEIDON_STATE_FM)) {
  80. ret = -EBUSY;
  81. goto out;
  82. }
  83. usb_autopm_get_interface(p->interface);
  84. if (0 == p->state) {
  85. /* default pre-emphasis */
  86. if (p->radio_data.pre_emphasis == 0)
  87. p->radio_data.pre_emphasis = TLG_TUNE_ASTD_FM_EUR;
  88. set_debug_mode(vfd, debug_mode);
  89. ret = poseidon_check_mode_radio(p);
  90. if (ret < 0) {
  91. usb_autopm_put_interface(p->interface);
  92. goto out;
  93. }
  94. p->state |= POSEIDON_STATE_FM;
  95. }
  96. p->radio_data.users++;
  97. kref_get(&p->kref);
  98. filp->private_data = p;
  99. out:
  100. mutex_unlock(&p->lock);
  101. return ret;
  102. }
  103. static int poseidon_fm_close(struct file *filp)
  104. {
  105. struct poseidon *p = filp->private_data;
  106. struct radio_data *fm = &p->radio_data;
  107. uint32_t status;
  108. mutex_lock(&p->lock);
  109. fm->users--;
  110. if (0 == fm->users)
  111. p->state &= ~POSEIDON_STATE_FM;
  112. if (fm->is_radio_streaming && filp == p->file_for_stream) {
  113. fm->is_radio_streaming = 0;
  114. send_set_req(p, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP, &status);
  115. }
  116. usb_autopm_put_interface(p->interface);
  117. mutex_unlock(&p->lock);
  118. kref_put(&p->kref, poseidon_delete);
  119. filp->private_data = NULL;
  120. return 0;
  121. }
  122. static int vidioc_querycap(struct file *file, void *priv,
  123. struct v4l2_capability *v)
  124. {
  125. struct poseidon *p = file->private_data;
  126. strlcpy(v->driver, "tele-radio", sizeof(v->driver));
  127. strlcpy(v->card, "Telegent Poseidon", sizeof(v->card));
  128. usb_make_path(p->udev, v->bus_info, sizeof(v->bus_info));
  129. v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
  130. return 0;
  131. }
  132. static const struct v4l2_file_operations poseidon_fm_fops = {
  133. .owner = THIS_MODULE,
  134. .open = poseidon_fm_open,
  135. .release = poseidon_fm_close,
  136. .ioctl = video_ioctl2,
  137. };
  138. static int tlg_fm_vidioc_g_tuner(struct file *file, void *priv,
  139. struct v4l2_tuner *vt)
  140. {
  141. struct tuner_fm_sig_stat_s fm_stat = {};
  142. int ret, status, count = 5;
  143. struct poseidon *p = file->private_data;
  144. if (vt->index != 0)
  145. return -EINVAL;
  146. vt->type = V4L2_TUNER_RADIO;
  147. vt->capability = V4L2_TUNER_CAP_STEREO;
  148. vt->rangelow = TUNER_FREQ_MIN_FM / 62500;
  149. vt->rangehigh = TUNER_FREQ_MAX_FM / 62500;
  150. vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
  151. vt->audmode = V4L2_TUNER_MODE_STEREO;
  152. vt->signal = 0;
  153. vt->afc = 0;
  154. mutex_lock(&p->lock);
  155. ret = send_get_req(p, TUNER_STATUS, TLG_MODE_FM_RADIO,
  156. &fm_stat, &status, sizeof(fm_stat));
  157. while (fm_stat.sig_lock_busy && count-- && !ret) {
  158. set_current_state(TASK_INTERRUPTIBLE);
  159. schedule_timeout(HZ);
  160. ret = send_get_req(p, TUNER_STATUS, TLG_MODE_FM_RADIO,
  161. &fm_stat, &status, sizeof(fm_stat));
  162. }
  163. mutex_unlock(&p->lock);
  164. if (ret || status) {
  165. vt->signal = 0;
  166. } else if ((fm_stat.sig_present || fm_stat.sig_locked)
  167. && fm_stat.sig_strength == 0) {
  168. vt->signal = 0xffff;
  169. } else
  170. vt->signal = (fm_stat.sig_strength * 255 / 10) << 8;
  171. return 0;
  172. }
  173. static int fm_get_freq(struct file *file, void *priv,
  174. struct v4l2_frequency *argp)
  175. {
  176. struct poseidon *p = file->private_data;
  177. argp->frequency = p->radio_data.fm_freq;
  178. return 0;
  179. }
  180. static int set_frequency(struct poseidon *p, __u32 frequency)
  181. {
  182. __u32 freq ;
  183. int ret, status;
  184. mutex_lock(&p->lock);
  185. ret = send_set_req(p, TUNER_AUD_ANA_STD,
  186. p->radio_data.pre_emphasis, &status);
  187. freq = (frequency * 125) * 500 / 1000;/* kHZ */
  188. if (freq < TUNER_FREQ_MIN_FM/1000 || freq > TUNER_FREQ_MAX_FM/1000) {
  189. ret = -EINVAL;
  190. goto error;
  191. }
  192. ret = send_set_req(p, TUNE_FREQ_SELECT, freq, &status);
  193. if (ret < 0)
  194. goto error ;
  195. ret = send_set_req(p, TAKE_REQUEST, 0, &status);
  196. set_current_state(TASK_INTERRUPTIBLE);
  197. schedule_timeout(HZ/4);
  198. if (!p->radio_data.is_radio_streaming) {
  199. ret = send_set_req(p, TAKE_REQUEST, 0, &status);
  200. ret = send_set_req(p, PLAY_SERVICE,
  201. TLG_TUNE_PLAY_SVC_START, &status);
  202. p->radio_data.is_radio_streaming = 1;
  203. }
  204. p->radio_data.fm_freq = frequency;
  205. error:
  206. mutex_unlock(&p->lock);
  207. return ret;
  208. }
  209. static int fm_set_freq(struct file *file, void *priv,
  210. struct v4l2_frequency *argp)
  211. {
  212. struct poseidon *p = file->private_data;
  213. p->file_for_stream = file;
  214. #ifdef CONFIG_PM
  215. p->pm_suspend = pm_fm_suspend;
  216. p->pm_resume = pm_fm_resume;
  217. #endif
  218. return set_frequency(p, argp->frequency);
  219. }
  220. static int tlg_fm_vidioc_g_ctrl(struct file *file, void *priv,
  221. struct v4l2_control *arg)
  222. {
  223. return 0;
  224. }
  225. static int tlg_fm_vidioc_g_exts_ctrl(struct file *file, void *fh,
  226. struct v4l2_ext_controls *ctrls)
  227. {
  228. struct poseidon *p = file->private_data;
  229. int i;
  230. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  231. return -EINVAL;
  232. for (i = 0; i < ctrls->count; i++) {
  233. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  234. if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS)
  235. continue;
  236. if (i < MAX_PREEMPHASIS)
  237. ctrl->value = p->radio_data.pre_emphasis;
  238. }
  239. return 0;
  240. }
  241. static int tlg_fm_vidioc_s_exts_ctrl(struct file *file, void *fh,
  242. struct v4l2_ext_controls *ctrls)
  243. {
  244. int i;
  245. if (ctrls->ctrl_class != V4L2_CTRL_CLASS_FM_TX)
  246. return -EINVAL;
  247. for (i = 0; i < ctrls->count; i++) {
  248. struct v4l2_ext_control *ctrl = ctrls->controls + i;
  249. if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS)
  250. continue;
  251. if (ctrl->value >= 0 && ctrl->value < MAX_PREEMPHASIS) {
  252. struct poseidon *p = file->private_data;
  253. int pre_emphasis = preemphasis[ctrl->value];
  254. u32 status;
  255. send_set_req(p, TUNER_AUD_ANA_STD,
  256. pre_emphasis, &status);
  257. p->radio_data.pre_emphasis = pre_emphasis;
  258. }
  259. }
  260. return 0;
  261. }
  262. static int tlg_fm_vidioc_s_ctrl(struct file *file, void *priv,
  263. struct v4l2_control *ctrl)
  264. {
  265. return 0;
  266. }
  267. static int tlg_fm_vidioc_queryctrl(struct file *file, void *priv,
  268. struct v4l2_queryctrl *ctrl)
  269. {
  270. if (!(ctrl->id & V4L2_CTRL_FLAG_NEXT_CTRL))
  271. return -EINVAL;
  272. ctrl->id &= ~V4L2_CTRL_FLAG_NEXT_CTRL;
  273. if (ctrl->id != V4L2_CID_TUNE_PREEMPHASIS) {
  274. /* return the next supported control */
  275. ctrl->id = V4L2_CID_TUNE_PREEMPHASIS;
  276. v4l2_ctrl_query_fill(ctrl, V4L2_PREEMPHASIS_DISABLED,
  277. V4L2_PREEMPHASIS_75_uS, 1,
  278. V4L2_PREEMPHASIS_50_uS);
  279. ctrl->flags = V4L2_CTRL_FLAG_UPDATE;
  280. return 0;
  281. }
  282. return -EINVAL;
  283. }
  284. static int tlg_fm_vidioc_querymenu(struct file *file, void *fh,
  285. struct v4l2_querymenu *qmenu)
  286. {
  287. return v4l2_ctrl_query_menu(qmenu, NULL, NULL);
  288. }
  289. static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
  290. {
  291. return vt->index > 0 ? -EINVAL : 0;
  292. }
  293. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *va)
  294. {
  295. return (va->index != 0) ? -EINVAL : 0;
  296. }
  297. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  298. {
  299. a->index = 0;
  300. a->mode = 0;
  301. a->capability = V4L2_AUDCAP_STEREO;
  302. strcpy(a->name, "Radio");
  303. return 0;
  304. }
  305. static int vidioc_s_input(struct file *filp, void *priv, u32 i)
  306. {
  307. return (i != 0) ? -EINVAL : 0;
  308. }
  309. static int vidioc_g_input(struct file *filp, void *priv, u32 *i)
  310. {
  311. return (*i != 0) ? -EINVAL : 0;
  312. }
  313. static const struct v4l2_ioctl_ops poseidon_fm_ioctl_ops = {
  314. .vidioc_querycap = vidioc_querycap,
  315. .vidioc_g_audio = vidioc_g_audio,
  316. .vidioc_s_audio = vidioc_s_audio,
  317. .vidioc_g_input = vidioc_g_input,
  318. .vidioc_s_input = vidioc_s_input,
  319. .vidioc_queryctrl = tlg_fm_vidioc_queryctrl,
  320. .vidioc_querymenu = tlg_fm_vidioc_querymenu,
  321. .vidioc_g_ctrl = tlg_fm_vidioc_g_ctrl,
  322. .vidioc_s_ctrl = tlg_fm_vidioc_s_ctrl,
  323. .vidioc_s_ext_ctrls = tlg_fm_vidioc_s_exts_ctrl,
  324. .vidioc_g_ext_ctrls = tlg_fm_vidioc_g_exts_ctrl,
  325. .vidioc_s_tuner = vidioc_s_tuner,
  326. .vidioc_g_tuner = tlg_fm_vidioc_g_tuner,
  327. .vidioc_g_frequency = fm_get_freq,
  328. .vidioc_s_frequency = fm_set_freq,
  329. };
  330. static struct video_device poseidon_fm_template = {
  331. .name = "Telegent-Radio",
  332. .fops = &poseidon_fm_fops,
  333. .minor = -1,
  334. .release = video_device_release,
  335. .ioctl_ops = &poseidon_fm_ioctl_ops,
  336. };
  337. int poseidon_fm_init(struct poseidon *p)
  338. {
  339. struct video_device *fm_dev;
  340. fm_dev = vdev_init(p, &poseidon_fm_template);
  341. if (fm_dev == NULL)
  342. return -1;
  343. if (video_register_device(fm_dev, VFL_TYPE_RADIO, -1) < 0) {
  344. video_device_release(fm_dev);
  345. return -1;
  346. }
  347. p->radio_data.fm_dev = fm_dev;
  348. return 0;
  349. }
  350. int poseidon_fm_exit(struct poseidon *p)
  351. {
  352. destroy_video_device(&p->radio_data.fm_dev);
  353. return 0;
  354. }