radio-ma901.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Driver for the MasterKit MA901 USB FM radio. This device plugs
  3. * into the USB port and an analog audio input or headphones, so this thing
  4. * only deals with initialization, frequency setting, volume.
  5. *
  6. * Copyright (c) 2012 Alexey Klimov <klimov.linux@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/input.h>
  27. #include <linux/videodev2.h>
  28. #include <media/v4l2-device.h>
  29. #include <media/v4l2-ioctl.h>
  30. #include <media/v4l2-ctrls.h>
  31. #include <media/v4l2-event.h>
  32. #include <linux/usb.h>
  33. #include <linux/mutex.h>
  34. #define DRIVER_AUTHOR "Alexey Klimov <klimov.linux@gmail.com>"
  35. #define DRIVER_DESC "Masterkit MA901 USB FM radio driver"
  36. #define DRIVER_VERSION "0.0.1"
  37. MODULE_AUTHOR(DRIVER_AUTHOR);
  38. MODULE_DESCRIPTION(DRIVER_DESC);
  39. MODULE_LICENSE("GPL");
  40. MODULE_VERSION(DRIVER_VERSION);
  41. #define USB_MA901_VENDOR 0x16c0
  42. #define USB_MA901_PRODUCT 0x05df
  43. /* dev_warn macro with driver name */
  44. #define MA901_DRIVER_NAME "radio-ma901"
  45. #define ma901radio_dev_warn(dev, fmt, arg...) \
  46. dev_warn(dev, MA901_DRIVER_NAME " - " fmt, ##arg)
  47. #define ma901radio_dev_err(dev, fmt, arg...) \
  48. dev_err(dev, MA901_DRIVER_NAME " - " fmt, ##arg)
  49. /* Probably USB_TIMEOUT should be modified in module parameter */
  50. #define BUFFER_LENGTH 8
  51. #define USB_TIMEOUT 500
  52. #define FREQ_MIN 87.5
  53. #define FREQ_MAX 108.0
  54. #define FREQ_MUL 16000
  55. #define MA901_VOLUME_MAX 16
  56. #define MA901_VOLUME_MIN 0
  57. /* Commands that device should understand
  58. * List isn't full and will be updated with implementation of new functions
  59. */
  60. #define MA901_RADIO_SET_FREQ 0x03
  61. #define MA901_RADIO_SET_VOLUME 0x04
  62. #define MA901_RADIO_SET_MONO_STEREO 0x05
  63. /* Comfortable defines for ma901radio_set_stereo */
  64. #define MA901_WANT_STEREO 0x50
  65. #define MA901_WANT_MONO 0xd0
  66. /* module parameter */
  67. static int radio_nr = -1;
  68. module_param(radio_nr, int, 0);
  69. MODULE_PARM_DESC(radio_nr, "Radio file number");
  70. /* Data for one (physical) device */
  71. struct ma901radio_device {
  72. /* reference to USB and video device */
  73. struct usb_device *usbdev;
  74. struct usb_interface *intf;
  75. struct video_device vdev;
  76. struct v4l2_device v4l2_dev;
  77. struct v4l2_ctrl_handler hdl;
  78. u8 *buffer;
  79. struct mutex lock; /* buffer locking */
  80. int curfreq;
  81. u16 volume;
  82. int stereo;
  83. bool muted;
  84. };
  85. static inline struct ma901radio_device *to_ma901radio_dev(struct v4l2_device *v4l2_dev)
  86. {
  87. return container_of(v4l2_dev, struct ma901radio_device, v4l2_dev);
  88. }
  89. /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
  90. static int ma901radio_set_freq(struct ma901radio_device *radio, int freq)
  91. {
  92. unsigned int freq_send = 0x300 + (freq >> 5) / 25;
  93. int retval;
  94. radio->buffer[0] = 0x0a;
  95. radio->buffer[1] = MA901_RADIO_SET_FREQ;
  96. radio->buffer[2] = ((freq_send >> 8) & 0xff) + 0x80;
  97. radio->buffer[3] = freq_send & 0xff;
  98. radio->buffer[4] = 0x00;
  99. radio->buffer[5] = 0x00;
  100. radio->buffer[6] = 0x00;
  101. radio->buffer[7] = 0x00;
  102. retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
  103. 9, 0x21, 0x0300, 0,
  104. radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
  105. if (retval < 0)
  106. return retval;
  107. radio->curfreq = freq;
  108. return 0;
  109. }
  110. static int ma901radio_set_volume(struct ma901radio_device *radio, u16 vol_to_set)
  111. {
  112. int retval;
  113. radio->buffer[0] = 0x0a;
  114. radio->buffer[1] = MA901_RADIO_SET_VOLUME;
  115. radio->buffer[2] = 0xc2;
  116. radio->buffer[3] = vol_to_set + 0x20;
  117. radio->buffer[4] = 0x00;
  118. radio->buffer[5] = 0x00;
  119. radio->buffer[6] = 0x00;
  120. radio->buffer[7] = 0x00;
  121. retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
  122. 9, 0x21, 0x0300, 0,
  123. radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
  124. if (retval < 0)
  125. return retval;
  126. radio->volume = vol_to_set;
  127. return retval;
  128. }
  129. static int ma901_set_stereo(struct ma901radio_device *radio, u8 stereo)
  130. {
  131. int retval;
  132. radio->buffer[0] = 0x0a;
  133. radio->buffer[1] = MA901_RADIO_SET_MONO_STEREO;
  134. radio->buffer[2] = stereo;
  135. radio->buffer[3] = 0x00;
  136. radio->buffer[4] = 0x00;
  137. radio->buffer[5] = 0x00;
  138. radio->buffer[6] = 0x00;
  139. radio->buffer[7] = 0x00;
  140. retval = usb_control_msg(radio->usbdev, usb_sndctrlpipe(radio->usbdev, 0),
  141. 9, 0x21, 0x0300, 0,
  142. radio->buffer, BUFFER_LENGTH, USB_TIMEOUT);
  143. if (retval < 0)
  144. return retval;
  145. if (stereo == MA901_WANT_STEREO)
  146. radio->stereo = V4L2_TUNER_MODE_STEREO;
  147. else
  148. radio->stereo = V4L2_TUNER_MODE_MONO;
  149. return retval;
  150. }
  151. /* Handle unplugging the device.
  152. * We call video_unregister_device in any case.
  153. * The last function called in this procedure is
  154. * usb_ma901radio_device_release.
  155. */
  156. static void usb_ma901radio_disconnect(struct usb_interface *intf)
  157. {
  158. struct ma901radio_device *radio = to_ma901radio_dev(usb_get_intfdata(intf));
  159. mutex_lock(&radio->lock);
  160. video_unregister_device(&radio->vdev);
  161. usb_set_intfdata(intf, NULL);
  162. v4l2_device_disconnect(&radio->v4l2_dev);
  163. mutex_unlock(&radio->lock);
  164. v4l2_device_put(&radio->v4l2_dev);
  165. }
  166. /* vidioc_querycap - query device capabilities */
  167. static int vidioc_querycap(struct file *file, void *priv,
  168. struct v4l2_capability *v)
  169. {
  170. struct ma901radio_device *radio = video_drvdata(file);
  171. strlcpy(v->driver, "radio-ma901", sizeof(v->driver));
  172. strlcpy(v->card, "Masterkit MA901 USB FM Radio", sizeof(v->card));
  173. usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
  174. v->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
  175. v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
  176. return 0;
  177. }
  178. /* vidioc_g_tuner - get tuner attributes */
  179. static int vidioc_g_tuner(struct file *file, void *priv,
  180. struct v4l2_tuner *v)
  181. {
  182. struct ma901radio_device *radio = video_drvdata(file);
  183. if (v->index > 0)
  184. return -EINVAL;
  185. v->signal = 0;
  186. /* TODO: the same words like in _probe() goes here.
  187. * When receiving of stats will be implemented then we can call
  188. * ma901radio_get_stat().
  189. * retval = ma901radio_get_stat(radio, &is_stereo, &v->signal);
  190. */
  191. strcpy(v->name, "FM");
  192. v->type = V4L2_TUNER_RADIO;
  193. v->rangelow = FREQ_MIN * FREQ_MUL;
  194. v->rangehigh = FREQ_MAX * FREQ_MUL;
  195. v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  196. /* v->rxsubchans = is_stereo ? V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO; */
  197. v->audmode = radio->stereo ?
  198. V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
  199. return 0;
  200. }
  201. /* vidioc_s_tuner - set tuner attributes */
  202. static int vidioc_s_tuner(struct file *file, void *priv,
  203. const struct v4l2_tuner *v)
  204. {
  205. struct ma901radio_device *radio = video_drvdata(file);
  206. if (v->index > 0)
  207. return -EINVAL;
  208. /* mono/stereo selector */
  209. switch (v->audmode) {
  210. case V4L2_TUNER_MODE_MONO:
  211. return ma901_set_stereo(radio, MA901_WANT_MONO);
  212. default:
  213. return ma901_set_stereo(radio, MA901_WANT_STEREO);
  214. }
  215. }
  216. /* vidioc_s_frequency - set tuner radio frequency */
  217. static int vidioc_s_frequency(struct file *file, void *priv,
  218. const struct v4l2_frequency *f)
  219. {
  220. struct ma901radio_device *radio = video_drvdata(file);
  221. if (f->tuner != 0)
  222. return -EINVAL;
  223. return ma901radio_set_freq(radio, clamp_t(unsigned, f->frequency,
  224. FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL));
  225. }
  226. /* vidioc_g_frequency - get tuner radio frequency */
  227. static int vidioc_g_frequency(struct file *file, void *priv,
  228. struct v4l2_frequency *f)
  229. {
  230. struct ma901radio_device *radio = video_drvdata(file);
  231. if (f->tuner != 0)
  232. return -EINVAL;
  233. f->frequency = radio->curfreq;
  234. return 0;
  235. }
  236. static int usb_ma901radio_s_ctrl(struct v4l2_ctrl *ctrl)
  237. {
  238. struct ma901radio_device *radio =
  239. container_of(ctrl->handler, struct ma901radio_device, hdl);
  240. switch (ctrl->id) {
  241. case V4L2_CID_AUDIO_VOLUME: /* set volume */
  242. return ma901radio_set_volume(radio, (u16)ctrl->val);
  243. }
  244. return -EINVAL;
  245. }
  246. /* TODO: Should we really need to implement suspend and resume functions?
  247. * Radio has it's own memory and will continue playing if power is present
  248. * on usb port and on resume it will start to play again based on freq, volume
  249. * values in device memory.
  250. */
  251. static int usb_ma901radio_suspend(struct usb_interface *intf, pm_message_t message)
  252. {
  253. return 0;
  254. }
  255. static int usb_ma901radio_resume(struct usb_interface *intf)
  256. {
  257. return 0;
  258. }
  259. static const struct v4l2_ctrl_ops usb_ma901radio_ctrl_ops = {
  260. .s_ctrl = usb_ma901radio_s_ctrl,
  261. };
  262. /* File system interface */
  263. static const struct v4l2_file_operations usb_ma901radio_fops = {
  264. .owner = THIS_MODULE,
  265. .open = v4l2_fh_open,
  266. .release = v4l2_fh_release,
  267. .poll = v4l2_ctrl_poll,
  268. .unlocked_ioctl = video_ioctl2,
  269. };
  270. static const struct v4l2_ioctl_ops usb_ma901radio_ioctl_ops = {
  271. .vidioc_querycap = vidioc_querycap,
  272. .vidioc_g_tuner = vidioc_g_tuner,
  273. .vidioc_s_tuner = vidioc_s_tuner,
  274. .vidioc_g_frequency = vidioc_g_frequency,
  275. .vidioc_s_frequency = vidioc_s_frequency,
  276. .vidioc_log_status = v4l2_ctrl_log_status,
  277. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  278. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  279. };
  280. static void usb_ma901radio_release(struct v4l2_device *v4l2_dev)
  281. {
  282. struct ma901radio_device *radio = to_ma901radio_dev(v4l2_dev);
  283. v4l2_ctrl_handler_free(&radio->hdl);
  284. v4l2_device_unregister(&radio->v4l2_dev);
  285. kfree(radio->buffer);
  286. kfree(radio);
  287. }
  288. /* check if the device is present and register with v4l and usb if it is */
  289. static int usb_ma901radio_probe(struct usb_interface *intf,
  290. const struct usb_device_id *id)
  291. {
  292. struct usb_device *dev = interface_to_usbdev(intf);
  293. struct ma901radio_device *radio;
  294. int retval = 0;
  295. /* Masterkit MA901 usb radio has the same USB ID as many others
  296. * Atmel V-USB devices. Let's make additional checks to be sure
  297. * that this is our device.
  298. */
  299. if (dev->product && dev->manufacturer &&
  300. (strncmp(dev->product, "MA901", 5) != 0
  301. || strncmp(dev->manufacturer, "www.masterkit.ru", 16) != 0))
  302. return -ENODEV;
  303. radio = kzalloc(sizeof(struct ma901radio_device), GFP_KERNEL);
  304. if (!radio) {
  305. dev_err(&intf->dev, "kzalloc for ma901radio_device failed\n");
  306. retval = -ENOMEM;
  307. goto err;
  308. }
  309. radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
  310. if (!radio->buffer) {
  311. dev_err(&intf->dev, "kmalloc for radio->buffer failed\n");
  312. retval = -ENOMEM;
  313. goto err_nobuf;
  314. }
  315. retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
  316. if (retval < 0) {
  317. dev_err(&intf->dev, "couldn't register v4l2_device\n");
  318. goto err_v4l2;
  319. }
  320. v4l2_ctrl_handler_init(&radio->hdl, 1);
  321. /* TODO:It looks like this radio doesn't have mute/unmute control
  322. * and windows program just emulate it using volume control.
  323. * Let's plan to do the same in this driver.
  324. *
  325. * v4l2_ctrl_new_std(&radio->hdl, &usb_ma901radio_ctrl_ops,
  326. * V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  327. */
  328. v4l2_ctrl_new_std(&radio->hdl, &usb_ma901radio_ctrl_ops,
  329. V4L2_CID_AUDIO_VOLUME, MA901_VOLUME_MIN,
  330. MA901_VOLUME_MAX, 1, MA901_VOLUME_MAX);
  331. if (radio->hdl.error) {
  332. retval = radio->hdl.error;
  333. dev_err(&intf->dev, "couldn't register control\n");
  334. goto err_ctrl;
  335. }
  336. mutex_init(&radio->lock);
  337. radio->v4l2_dev.ctrl_handler = &radio->hdl;
  338. radio->v4l2_dev.release = usb_ma901radio_release;
  339. strlcpy(radio->vdev.name, radio->v4l2_dev.name,
  340. sizeof(radio->vdev.name));
  341. radio->vdev.v4l2_dev = &radio->v4l2_dev;
  342. radio->vdev.fops = &usb_ma901radio_fops;
  343. radio->vdev.ioctl_ops = &usb_ma901radio_ioctl_ops;
  344. radio->vdev.release = video_device_release_empty;
  345. radio->vdev.lock = &radio->lock;
  346. radio->usbdev = interface_to_usbdev(intf);
  347. radio->intf = intf;
  348. usb_set_intfdata(intf, &radio->v4l2_dev);
  349. radio->curfreq = 95.21 * FREQ_MUL;
  350. video_set_drvdata(&radio->vdev, radio);
  351. /* TODO: we can get some statistics (freq, volume) from device
  352. * but it's not implemented yet. After insertion in usb-port radio
  353. * setups frequency and starts playing without any initialization.
  354. * So we don't call usb_ma901radio_init/get_stat() here.
  355. * retval = usb_ma901radio_init(radio);
  356. */
  357. retval = video_register_device(&radio->vdev, VFL_TYPE_RADIO,
  358. radio_nr);
  359. if (retval < 0) {
  360. dev_err(&intf->dev, "could not register video device\n");
  361. goto err_vdev;
  362. }
  363. return 0;
  364. err_vdev:
  365. v4l2_ctrl_handler_free(&radio->hdl);
  366. err_ctrl:
  367. v4l2_device_unregister(&radio->v4l2_dev);
  368. err_v4l2:
  369. kfree(radio->buffer);
  370. err_nobuf:
  371. kfree(radio);
  372. err:
  373. return retval;
  374. }
  375. /* USB Device ID List */
  376. static struct usb_device_id usb_ma901radio_device_table[] = {
  377. { USB_DEVICE_AND_INTERFACE_INFO(USB_MA901_VENDOR, USB_MA901_PRODUCT,
  378. USB_CLASS_HID, 0, 0) },
  379. { } /* Terminating entry */
  380. };
  381. MODULE_DEVICE_TABLE(usb, usb_ma901radio_device_table);
  382. /* USB subsystem interface */
  383. static struct usb_driver usb_ma901radio_driver = {
  384. .name = MA901_DRIVER_NAME,
  385. .probe = usb_ma901radio_probe,
  386. .disconnect = usb_ma901radio_disconnect,
  387. .suspend = usb_ma901radio_suspend,
  388. .resume = usb_ma901radio_resume,
  389. .reset_resume = usb_ma901radio_resume,
  390. .id_table = usb_ma901radio_device_table,
  391. };
  392. module_usb_driver(usb_ma901radio_driver);