radio-miropcm20.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Miro PCM20 radio driver for Linux radio support
  3. * (c) 1998 Ruurd Reitsma <R.A.Reitsma@wbmt.tudelft.nl>
  4. * Thanks to Norberto Pellici for the ACI device interface specification
  5. * The API part is based on the radiotrack driver by M. Kirkwood
  6. * This driver relies on the aci mixer provided by the snd-miro
  7. * ALSA driver.
  8. * Look there for further info...
  9. *
  10. * From the original miro RDS sources:
  11. *
  12. * (c) 2001 Robert Siemer <Robert.Siemer@gmx.de>
  13. *
  14. * Many thanks to Fred Seidel <seidel@metabox.de>, the
  15. * designer of the RDS decoder hardware. With his help
  16. * I was able to code this driver.
  17. * Thanks also to Norberto Pellicci, Dominic Mounteney
  18. * <DMounteney@pinnaclesys.com> and www.teleauskunft.de
  19. * for good hints on finding Fred. It was somewhat hard
  20. * to locate him here in Germany... [:
  21. *
  22. * This code has been reintroduced and converted to use
  23. * the new V4L2 RDS API by:
  24. *
  25. * Hans Verkuil <hans.verkuil@cisco.com>
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/io.h>
  30. #include <linux/delay.h>
  31. #include <linux/videodev2.h>
  32. #include <linux/kthread.h>
  33. #include <media/v4l2-device.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include <media/v4l2-ctrls.h>
  36. #include <media/v4l2-fh.h>
  37. #include <media/v4l2-event.h>
  38. #include <sound/aci.h>
  39. #define RDS_DATASHIFT 2 /* Bit 2 */
  40. #define RDS_DATAMASK (1 << RDS_DATASHIFT)
  41. #define RDS_BUSYMASK 0x10 /* Bit 4 */
  42. #define RDS_CLOCKMASK 0x08 /* Bit 3 */
  43. #define RDS_DATA(x) (((x) >> RDS_DATASHIFT) & 1)
  44. #define RDS_STATUS 0x01
  45. #define RDS_STATIONNAME 0x02
  46. #define RDS_TEXT 0x03
  47. #define RDS_ALTFREQ 0x04
  48. #define RDS_TIMEDATE 0x05
  49. #define RDS_PI_CODE 0x06
  50. #define RDS_PTYTATP 0x07
  51. #define RDS_RESET 0x08
  52. #define RDS_RXVALUE 0x09
  53. static int radio_nr = -1;
  54. module_param(radio_nr, int, 0);
  55. MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
  56. struct pcm20 {
  57. struct v4l2_device v4l2_dev;
  58. struct video_device vdev;
  59. struct v4l2_ctrl_handler ctrl_handler;
  60. struct v4l2_ctrl *rds_pty;
  61. struct v4l2_ctrl *rds_ps_name;
  62. struct v4l2_ctrl *rds_radio_test;
  63. struct v4l2_ctrl *rds_ta;
  64. struct v4l2_ctrl *rds_tp;
  65. struct v4l2_ctrl *rds_ms;
  66. /* thread for periodic RDS status checking */
  67. struct task_struct *kthread;
  68. unsigned long freq;
  69. u32 audmode;
  70. struct snd_miro_aci *aci;
  71. struct mutex lock;
  72. };
  73. static struct pcm20 pcm20_card = {
  74. .freq = 87 * 16000,
  75. .audmode = V4L2_TUNER_MODE_STEREO,
  76. };
  77. static int rds_waitread(struct snd_miro_aci *aci)
  78. {
  79. u8 byte;
  80. int i = 2000;
  81. do {
  82. byte = inb(aci->aci_port + ACI_REG_RDS);
  83. i--;
  84. } while ((byte & RDS_BUSYMASK) && i);
  85. /*
  86. * It's magic, but without this the data that you read later on
  87. * is unreliable and full of bit errors. With this 1 usec delay
  88. * everything is fine.
  89. */
  90. udelay(1);
  91. return i ? byte : -1;
  92. }
  93. static int rds_rawwrite(struct snd_miro_aci *aci, u8 byte)
  94. {
  95. if (rds_waitread(aci) >= 0) {
  96. outb(byte, aci->aci_port + ACI_REG_RDS);
  97. return 0;
  98. }
  99. return -1;
  100. }
  101. static int rds_write(struct snd_miro_aci *aci, u8 byte)
  102. {
  103. u8 sendbuffer[8];
  104. int i;
  105. for (i = 7; i >= 0; i--)
  106. sendbuffer[7 - i] = (byte & (1 << i)) ? RDS_DATAMASK : 0;
  107. sendbuffer[0] |= RDS_CLOCKMASK;
  108. for (i = 0; i < 8; i++)
  109. rds_rawwrite(aci, sendbuffer[i]);
  110. return 0;
  111. }
  112. static int rds_readcycle_nowait(struct snd_miro_aci *aci)
  113. {
  114. outb(0, aci->aci_port + ACI_REG_RDS);
  115. return rds_waitread(aci);
  116. }
  117. static int rds_readcycle(struct snd_miro_aci *aci)
  118. {
  119. if (rds_rawwrite(aci, 0) < 0)
  120. return -1;
  121. return rds_waitread(aci);
  122. }
  123. static int rds_ack(struct snd_miro_aci *aci)
  124. {
  125. int i = rds_readcycle(aci);
  126. if (i < 0)
  127. return -1;
  128. if (i & RDS_DATAMASK)
  129. return 0; /* ACK */
  130. return 1; /* NACK */
  131. }
  132. static int rds_cmd(struct snd_miro_aci *aci, u8 cmd, u8 databuffer[], u8 datasize)
  133. {
  134. int i, j;
  135. rds_write(aci, cmd);
  136. /* RDS_RESET doesn't need further processing */
  137. if (cmd == RDS_RESET)
  138. return 0;
  139. if (rds_ack(aci))
  140. return -EIO;
  141. if (datasize == 0)
  142. return 0;
  143. /* to be able to use rds_readcycle_nowait()
  144. I have to waitread() here */
  145. if (rds_waitread(aci) < 0)
  146. return -1;
  147. memset(databuffer, 0, datasize);
  148. for (i = 0; i < 8 * datasize; i++) {
  149. j = rds_readcycle_nowait(aci);
  150. if (j < 0)
  151. return -EIO;
  152. databuffer[i / 8] |= RDS_DATA(j) << (7 - (i % 8));
  153. }
  154. return 0;
  155. }
  156. static int pcm20_setfreq(struct pcm20 *dev, unsigned long freq)
  157. {
  158. unsigned char freql;
  159. unsigned char freqh;
  160. struct snd_miro_aci *aci = dev->aci;
  161. freq /= 160;
  162. if (!(aci->aci_version == 0x07 || aci->aci_version >= 0xb0))
  163. freq /= 10; /* I don't know exactly which version
  164. * needs this hack */
  165. freql = freq & 0xff;
  166. freqh = freq >> 8;
  167. rds_cmd(aci, RDS_RESET, NULL, 0);
  168. return snd_aci_cmd(aci, ACI_WRITE_TUNE, freql, freqh);
  169. }
  170. static int vidioc_querycap(struct file *file, void *priv,
  171. struct v4l2_capability *v)
  172. {
  173. struct pcm20 *dev = video_drvdata(file);
  174. strlcpy(v->driver, "Miro PCM20", sizeof(v->driver));
  175. strlcpy(v->card, "Miro PCM20", sizeof(v->card));
  176. snprintf(v->bus_info, sizeof(v->bus_info), "ISA:%s", dev->v4l2_dev.name);
  177. v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE;
  178. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  179. return 0;
  180. }
  181. static bool sanitize(char *p, int size)
  182. {
  183. int i;
  184. bool ret = true;
  185. for (i = 0; i < size; i++) {
  186. if (p[i] < 32) {
  187. p[i] = ' ';
  188. ret = false;
  189. }
  190. }
  191. return ret;
  192. }
  193. static int vidioc_g_tuner(struct file *file, void *priv,
  194. struct v4l2_tuner *v)
  195. {
  196. struct pcm20 *dev = video_drvdata(file);
  197. int res;
  198. u8 buf;
  199. if (v->index)
  200. return -EINVAL;
  201. strlcpy(v->name, "FM", sizeof(v->name));
  202. v->type = V4L2_TUNER_RADIO;
  203. v->rangelow = 87*16000;
  204. v->rangehigh = 108*16000;
  205. res = snd_aci_cmd(dev->aci, ACI_READ_TUNERSTATION, -1, -1);
  206. v->signal = (res & 0x80) ? 0 : 0xffff;
  207. /* Note: stereo detection does not work if the audio is muted,
  208. it will default to mono in that case. */
  209. res = snd_aci_cmd(dev->aci, ACI_READ_TUNERSTEREO, -1, -1);
  210. v->rxsubchans = (res & 0x40) ? V4L2_TUNER_SUB_MONO :
  211. V4L2_TUNER_SUB_STEREO;
  212. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  213. V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS;
  214. v->audmode = dev->audmode;
  215. res = rds_cmd(dev->aci, RDS_RXVALUE, &buf, 1);
  216. if (res >= 0 && buf)
  217. v->rxsubchans |= V4L2_TUNER_SUB_RDS;
  218. return 0;
  219. }
  220. static int vidioc_s_tuner(struct file *file, void *priv,
  221. const struct v4l2_tuner *v)
  222. {
  223. struct pcm20 *dev = video_drvdata(file);
  224. if (v->index)
  225. return -EINVAL;
  226. if (v->audmode > V4L2_TUNER_MODE_STEREO)
  227. dev->audmode = V4L2_TUNER_MODE_STEREO;
  228. else
  229. dev->audmode = v->audmode;
  230. snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO,
  231. dev->audmode == V4L2_TUNER_MODE_MONO, -1);
  232. return 0;
  233. }
  234. static int vidioc_g_frequency(struct file *file, void *priv,
  235. struct v4l2_frequency *f)
  236. {
  237. struct pcm20 *dev = video_drvdata(file);
  238. if (f->tuner != 0)
  239. return -EINVAL;
  240. f->type = V4L2_TUNER_RADIO;
  241. f->frequency = dev->freq;
  242. return 0;
  243. }
  244. static int vidioc_s_frequency(struct file *file, void *priv,
  245. const struct v4l2_frequency *f)
  246. {
  247. struct pcm20 *dev = video_drvdata(file);
  248. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  249. return -EINVAL;
  250. dev->freq = clamp_t(u32, f->frequency, 87 * 16000U, 108 * 16000U);
  251. pcm20_setfreq(dev, dev->freq);
  252. return 0;
  253. }
  254. static int pcm20_s_ctrl(struct v4l2_ctrl *ctrl)
  255. {
  256. struct pcm20 *dev = container_of(ctrl->handler, struct pcm20, ctrl_handler);
  257. switch (ctrl->id) {
  258. case V4L2_CID_AUDIO_MUTE:
  259. snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, ctrl->val, -1);
  260. return 0;
  261. }
  262. return -EINVAL;
  263. }
  264. static int pcm20_thread(void *data)
  265. {
  266. struct pcm20 *dev = data;
  267. const unsigned no_rds_start_counter = 5;
  268. const unsigned sleep_msecs = 2000;
  269. unsigned no_rds_counter = no_rds_start_counter;
  270. for (;;) {
  271. char text_buffer[66];
  272. u8 buf;
  273. int res;
  274. msleep_interruptible(sleep_msecs);
  275. if (kthread_should_stop())
  276. break;
  277. res = rds_cmd(dev->aci, RDS_RXVALUE, &buf, 1);
  278. if (res)
  279. continue;
  280. if (buf == 0) {
  281. if (no_rds_counter == 0)
  282. continue;
  283. no_rds_counter--;
  284. if (no_rds_counter)
  285. continue;
  286. /*
  287. * No RDS seen for no_rds_start_counter * sleep_msecs
  288. * milliseconds, clear all RDS controls to their
  289. * default values.
  290. */
  291. v4l2_ctrl_s_ctrl_string(dev->rds_ps_name, "");
  292. v4l2_ctrl_s_ctrl(dev->rds_ms, 1);
  293. v4l2_ctrl_s_ctrl(dev->rds_ta, 0);
  294. v4l2_ctrl_s_ctrl(dev->rds_tp, 0);
  295. v4l2_ctrl_s_ctrl(dev->rds_pty, 0);
  296. v4l2_ctrl_s_ctrl_string(dev->rds_radio_test, "");
  297. continue;
  298. }
  299. no_rds_counter = no_rds_start_counter;
  300. res = rds_cmd(dev->aci, RDS_STATUS, &buf, 1);
  301. if (res)
  302. continue;
  303. if ((buf >> 3) & 1) {
  304. res = rds_cmd(dev->aci, RDS_STATIONNAME, text_buffer, 8);
  305. text_buffer[8] = 0;
  306. if (!res && sanitize(text_buffer, 8))
  307. v4l2_ctrl_s_ctrl_string(dev->rds_ps_name, text_buffer);
  308. }
  309. if ((buf >> 6) & 1) {
  310. u8 pty;
  311. res = rds_cmd(dev->aci, RDS_PTYTATP, &pty, 1);
  312. if (!res) {
  313. v4l2_ctrl_s_ctrl(dev->rds_ms, !!(pty & 0x01));
  314. v4l2_ctrl_s_ctrl(dev->rds_ta, !!(pty & 0x02));
  315. v4l2_ctrl_s_ctrl(dev->rds_tp, !!(pty & 0x80));
  316. v4l2_ctrl_s_ctrl(dev->rds_pty, (pty >> 2) & 0x1f);
  317. }
  318. }
  319. if ((buf >> 4) & 1) {
  320. res = rds_cmd(dev->aci, RDS_TEXT, text_buffer, 65);
  321. text_buffer[65] = 0;
  322. if (!res && sanitize(text_buffer + 1, 64))
  323. v4l2_ctrl_s_ctrl_string(dev->rds_radio_test, text_buffer + 1);
  324. }
  325. }
  326. return 0;
  327. }
  328. static int pcm20_open(struct file *file)
  329. {
  330. struct pcm20 *dev = video_drvdata(file);
  331. int res = v4l2_fh_open(file);
  332. if (!res && v4l2_fh_is_singular_file(file) &&
  333. IS_ERR_OR_NULL(dev->kthread)) {
  334. dev->kthread = kthread_run(pcm20_thread, dev, "%s",
  335. dev->v4l2_dev.name);
  336. if (IS_ERR(dev->kthread)) {
  337. v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
  338. v4l2_fh_release(file);
  339. return PTR_ERR(dev->kthread);
  340. }
  341. }
  342. return res;
  343. }
  344. static int pcm20_release(struct file *file)
  345. {
  346. struct pcm20 *dev = video_drvdata(file);
  347. if (v4l2_fh_is_singular_file(file) && !IS_ERR_OR_NULL(dev->kthread)) {
  348. kthread_stop(dev->kthread);
  349. dev->kthread = NULL;
  350. }
  351. return v4l2_fh_release(file);
  352. }
  353. static const struct v4l2_file_operations pcm20_fops = {
  354. .owner = THIS_MODULE,
  355. .open = pcm20_open,
  356. .poll = v4l2_ctrl_poll,
  357. .release = pcm20_release,
  358. .unlocked_ioctl = video_ioctl2,
  359. };
  360. static const struct v4l2_ioctl_ops pcm20_ioctl_ops = {
  361. .vidioc_querycap = vidioc_querycap,
  362. .vidioc_g_tuner = vidioc_g_tuner,
  363. .vidioc_s_tuner = vidioc_s_tuner,
  364. .vidioc_g_frequency = vidioc_g_frequency,
  365. .vidioc_s_frequency = vidioc_s_frequency,
  366. .vidioc_log_status = v4l2_ctrl_log_status,
  367. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  368. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  369. };
  370. static const struct v4l2_ctrl_ops pcm20_ctrl_ops = {
  371. .s_ctrl = pcm20_s_ctrl,
  372. };
  373. static int __init pcm20_init(void)
  374. {
  375. struct pcm20 *dev = &pcm20_card;
  376. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  377. struct v4l2_ctrl_handler *hdl;
  378. int res;
  379. dev->aci = snd_aci_get_aci();
  380. if (dev->aci == NULL) {
  381. v4l2_err(v4l2_dev,
  382. "you must load the snd-miro driver first!\n");
  383. return -ENODEV;
  384. }
  385. strlcpy(v4l2_dev->name, "radio-miropcm20", sizeof(v4l2_dev->name));
  386. mutex_init(&dev->lock);
  387. res = v4l2_device_register(NULL, v4l2_dev);
  388. if (res < 0) {
  389. v4l2_err(v4l2_dev, "could not register v4l2_device\n");
  390. return -EINVAL;
  391. }
  392. hdl = &dev->ctrl_handler;
  393. v4l2_ctrl_handler_init(hdl, 7);
  394. v4l2_ctrl_new_std(hdl, &pcm20_ctrl_ops,
  395. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  396. dev->rds_pty = v4l2_ctrl_new_std(hdl, NULL,
  397. V4L2_CID_RDS_RX_PTY, 0, 0x1f, 1, 0);
  398. dev->rds_ps_name = v4l2_ctrl_new_std(hdl, NULL,
  399. V4L2_CID_RDS_RX_PS_NAME, 0, 8, 8, 0);
  400. dev->rds_radio_test = v4l2_ctrl_new_std(hdl, NULL,
  401. V4L2_CID_RDS_RX_RADIO_TEXT, 0, 64, 64, 0);
  402. dev->rds_ta = v4l2_ctrl_new_std(hdl, NULL,
  403. V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT, 0, 1, 1, 0);
  404. dev->rds_tp = v4l2_ctrl_new_std(hdl, NULL,
  405. V4L2_CID_RDS_RX_TRAFFIC_PROGRAM, 0, 1, 1, 0);
  406. dev->rds_ms = v4l2_ctrl_new_std(hdl, NULL,
  407. V4L2_CID_RDS_RX_MUSIC_SPEECH, 0, 1, 1, 1);
  408. v4l2_dev->ctrl_handler = hdl;
  409. if (hdl->error) {
  410. res = hdl->error;
  411. v4l2_err(v4l2_dev, "Could not register control\n");
  412. goto err_hdl;
  413. }
  414. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  415. dev->vdev.v4l2_dev = v4l2_dev;
  416. dev->vdev.fops = &pcm20_fops;
  417. dev->vdev.ioctl_ops = &pcm20_ioctl_ops;
  418. dev->vdev.release = video_device_release_empty;
  419. dev->vdev.lock = &dev->lock;
  420. video_set_drvdata(&dev->vdev, dev);
  421. snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO,
  422. dev->audmode == V4L2_TUNER_MODE_MONO, -1);
  423. pcm20_setfreq(dev, dev->freq);
  424. if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0)
  425. goto err_hdl;
  426. v4l2_info(v4l2_dev, "Mirosound PCM20 Radio tuner\n");
  427. return 0;
  428. err_hdl:
  429. v4l2_ctrl_handler_free(hdl);
  430. v4l2_device_unregister(v4l2_dev);
  431. return -EINVAL;
  432. }
  433. MODULE_AUTHOR("Ruurd Reitsma, Krzysztof Helt");
  434. MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card.");
  435. MODULE_LICENSE("GPL");
  436. static void __exit pcm20_cleanup(void)
  437. {
  438. struct pcm20 *dev = &pcm20_card;
  439. video_unregister_device(&dev->vdev);
  440. snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, 1, -1);
  441. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  442. v4l2_device_unregister(&dev->v4l2_dev);
  443. }
  444. module_init(pcm20_init);
  445. module_exit(pcm20_cleanup);