radio-mr800.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * A driver for the AverMedia MR 800 USB FM radio. This device plugs
  3. * into both the USB and an analog audio input, so this thing
  4. * only deals with initialization and frequency setting, the
  5. * audio data has to be handled by a sound driver.
  6. *
  7. * Copyright (c) 2008 Alexey Klimov <klimov.linux@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /*
  24. * Big thanks to authors and contributors of dsbr100.c and radio-si470x.c
  25. *
  26. * When work was looked pretty good, i discover this:
  27. * http://av-usbradio.sourceforge.net/index.php
  28. * http://sourceforge.net/projects/av-usbradio/
  29. * Latest release of theirs project was in 2005.
  30. * Probably, this driver could be improved trough using their
  31. * achievements (specifications given).
  32. * Also, Faidon Liambotis <paravoid@debian.org> wrote nice driver for this radio
  33. * in 2007. He allowed to use his driver to improve current mr800 radio driver.
  34. * http://kerneltrap.org/mailarchive/linux-usb-devel/2007/10/11/342492
  35. *
  36. * Version 0.01: First working version.
  37. * It's required to blacklist AverMedia USB Radio
  38. * in usbhid/hid-quirks.c
  39. * Version 0.10: A lot of cleanups and fixes: unpluging the device,
  40. * few mutex locks were added, codinstyle issues, etc.
  41. * Added stereo support. Thanks to
  42. * Douglas Schilling Landgraf <dougsland@gmail.com> and
  43. * David Ellingsworth <david@identd.dyndns.org>
  44. * for discussion, help and support.
  45. *
  46. * Many things to do:
  47. * - Correct power managment of device (suspend & resume)
  48. * - Add code for scanning and smooth tuning
  49. * - Add code for sensitivity value
  50. * - Correct mistakes
  51. * - In Japan another FREQ_MIN and FREQ_MAX
  52. */
  53. /* kernel includes */
  54. #include <linux/kernel.h>
  55. #include <linux/module.h>
  56. #include <linux/init.h>
  57. #include <linux/slab.h>
  58. #include <linux/input.h>
  59. #include <linux/videodev2.h>
  60. #include <media/v4l2-common.h>
  61. #include <media/v4l2-ioctl.h>
  62. #include <linux/usb.h>
  63. #include <linux/version.h> /* for KERNEL_VERSION MACRO */
  64. /* driver and module definitions */
  65. #define DRIVER_AUTHOR "Alexey Klimov <klimov.linux@gmail.com>"
  66. #define DRIVER_DESC "AverMedia MR 800 USB FM radio driver"
  67. #define DRIVER_VERSION "0.10"
  68. #define RADIO_VERSION KERNEL_VERSION(0, 1, 0)
  69. MODULE_AUTHOR(DRIVER_AUTHOR);
  70. MODULE_DESCRIPTION(DRIVER_DESC);
  71. MODULE_LICENSE("GPL");
  72. #define USB_AMRADIO_VENDOR 0x07ca
  73. #define USB_AMRADIO_PRODUCT 0xb800
  74. /* dev_warn macro with driver name */
  75. #define MR800_DRIVER_NAME "radio-mr800"
  76. #define amradio_dev_warn(dev, fmt, arg...) \
  77. dev_warn(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
  78. /* Probably USB_TIMEOUT should be modified in module parameter */
  79. #define BUFFER_LENGTH 8
  80. #define USB_TIMEOUT 500
  81. /* Frequency limits in MHz -- these are European values. For Japanese
  82. devices, that would be 76 and 91. */
  83. #define FREQ_MIN 87.5
  84. #define FREQ_MAX 108.0
  85. #define FREQ_MUL 16000
  86. /*
  87. * Commands that device should understand
  88. * List isnt full and will be updated with implementation of new functions
  89. */
  90. #define AMRADIO_SET_FREQ 0xa4
  91. #define AMRADIO_SET_MUTE 0xab
  92. #define AMRADIO_SET_MONO 0xae
  93. /* Comfortable defines for amradio_set_mute */
  94. #define AMRADIO_START 0x00
  95. #define AMRADIO_STOP 0x01
  96. /* Comfortable defines for amradio_set_stereo */
  97. #define WANT_STEREO 0x00
  98. #define WANT_MONO 0x01
  99. /* module parameter */
  100. static int radio_nr = -1;
  101. module_param(radio_nr, int, 0);
  102. MODULE_PARM_DESC(radio_nr, "Radio Nr");
  103. static struct v4l2_queryctrl radio_qctrl[] = {
  104. {
  105. .id = V4L2_CID_AUDIO_MUTE,
  106. .name = "Mute",
  107. .minimum = 0,
  108. .maximum = 1,
  109. .step = 1,
  110. .default_value = 1,
  111. .type = V4L2_CTRL_TYPE_BOOLEAN,
  112. },
  113. /* HINT: the disabled controls are only here to satify kradio and such apps */
  114. { .id = V4L2_CID_AUDIO_VOLUME,
  115. .flags = V4L2_CTRL_FLAG_DISABLED,
  116. },
  117. {
  118. .id = V4L2_CID_AUDIO_BALANCE,
  119. .flags = V4L2_CTRL_FLAG_DISABLED,
  120. },
  121. {
  122. .id = V4L2_CID_AUDIO_BASS,
  123. .flags = V4L2_CTRL_FLAG_DISABLED,
  124. },
  125. {
  126. .id = V4L2_CID_AUDIO_TREBLE,
  127. .flags = V4L2_CTRL_FLAG_DISABLED,
  128. },
  129. {
  130. .id = V4L2_CID_AUDIO_LOUDNESS,
  131. .flags = V4L2_CTRL_FLAG_DISABLED,
  132. },
  133. };
  134. static int usb_amradio_probe(struct usb_interface *intf,
  135. const struct usb_device_id *id);
  136. static void usb_amradio_disconnect(struct usb_interface *intf);
  137. static int usb_amradio_open(struct file *file);
  138. static int usb_amradio_close(struct file *file);
  139. static int usb_amradio_suspend(struct usb_interface *intf,
  140. pm_message_t message);
  141. static int usb_amradio_resume(struct usb_interface *intf);
  142. /* Data for one (physical) device */
  143. struct amradio_device {
  144. /* reference to USB and video device */
  145. struct usb_device *usbdev;
  146. struct video_device *videodev;
  147. unsigned char *buffer;
  148. struct mutex lock; /* buffer locking */
  149. int curfreq;
  150. int stereo;
  151. int users;
  152. int removed;
  153. int muted;
  154. };
  155. /* USB Device ID List */
  156. static struct usb_device_id usb_amradio_device_table[] = {
  157. {USB_DEVICE_AND_INTERFACE_INFO(USB_AMRADIO_VENDOR, USB_AMRADIO_PRODUCT,
  158. USB_CLASS_HID, 0, 0) },
  159. { } /* Terminating entry */
  160. };
  161. MODULE_DEVICE_TABLE(usb, usb_amradio_device_table);
  162. /* USB subsystem interface */
  163. static struct usb_driver usb_amradio_driver = {
  164. .name = MR800_DRIVER_NAME,
  165. .probe = usb_amradio_probe,
  166. .disconnect = usb_amradio_disconnect,
  167. .suspend = usb_amradio_suspend,
  168. .resume = usb_amradio_resume,
  169. .reset_resume = usb_amradio_resume,
  170. .id_table = usb_amradio_device_table,
  171. .supports_autosuspend = 0,
  172. };
  173. /* switch on/off the radio. Send 8 bytes to device */
  174. static int amradio_set_mute(struct amradio_device *radio, char argument)
  175. {
  176. int retval;
  177. int size;
  178. /* safety check */
  179. if (radio->removed)
  180. return -EIO;
  181. mutex_lock(&radio->lock);
  182. radio->buffer[0] = 0x00;
  183. radio->buffer[1] = 0x55;
  184. radio->buffer[2] = 0xaa;
  185. radio->buffer[3] = 0x00;
  186. radio->buffer[4] = AMRADIO_SET_MUTE;
  187. radio->buffer[5] = argument;
  188. radio->buffer[6] = 0x00;
  189. radio->buffer[7] = 0x00;
  190. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  191. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  192. if (retval) {
  193. mutex_unlock(&radio->lock);
  194. return retval;
  195. }
  196. radio->muted = argument;
  197. mutex_unlock(&radio->lock);
  198. return retval;
  199. }
  200. /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
  201. static int amradio_setfreq(struct amradio_device *radio, int freq)
  202. {
  203. int retval;
  204. int size;
  205. unsigned short freq_send = 0x10 + (freq >> 3) / 25;
  206. /* safety check */
  207. if (radio->removed)
  208. return -EIO;
  209. mutex_lock(&radio->lock);
  210. radio->buffer[0] = 0x00;
  211. radio->buffer[1] = 0x55;
  212. radio->buffer[2] = 0xaa;
  213. radio->buffer[3] = 0x03;
  214. radio->buffer[4] = AMRADIO_SET_FREQ;
  215. radio->buffer[5] = 0x00;
  216. radio->buffer[6] = 0x00;
  217. radio->buffer[7] = 0x08;
  218. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  219. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  220. if (retval) {
  221. mutex_unlock(&radio->lock);
  222. return retval;
  223. }
  224. /* frequency is calculated from freq_send and placed in first 2 bytes */
  225. radio->buffer[0] = (freq_send >> 8) & 0xff;
  226. radio->buffer[1] = freq_send & 0xff;
  227. radio->buffer[2] = 0x01;
  228. radio->buffer[3] = 0x00;
  229. radio->buffer[4] = 0x00;
  230. /* 5 and 6 bytes of buffer already = 0x00 */
  231. radio->buffer[7] = 0x00;
  232. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  233. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  234. if (retval) {
  235. mutex_unlock(&radio->lock);
  236. return retval;
  237. }
  238. mutex_unlock(&radio->lock);
  239. return retval;
  240. }
  241. static int amradio_set_stereo(struct amradio_device *radio, char argument)
  242. {
  243. int retval;
  244. int size;
  245. /* safety check */
  246. if (radio->removed)
  247. return -EIO;
  248. mutex_lock(&radio->lock);
  249. radio->buffer[0] = 0x00;
  250. radio->buffer[1] = 0x55;
  251. radio->buffer[2] = 0xaa;
  252. radio->buffer[3] = 0x00;
  253. radio->buffer[4] = AMRADIO_SET_MONO;
  254. radio->buffer[5] = argument;
  255. radio->buffer[6] = 0x00;
  256. radio->buffer[7] = 0x00;
  257. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  258. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  259. if (retval < 0 || size != BUFFER_LENGTH) {
  260. radio->stereo = -1;
  261. mutex_unlock(&radio->lock);
  262. return retval;
  263. }
  264. radio->stereo = 1;
  265. mutex_unlock(&radio->lock);
  266. return retval;
  267. }
  268. /* Handle unplugging the device.
  269. * We call video_unregister_device in any case.
  270. * The last function called in this procedure is
  271. * usb_amradio_device_release.
  272. */
  273. static void usb_amradio_disconnect(struct usb_interface *intf)
  274. {
  275. struct amradio_device *radio = usb_get_intfdata(intf);
  276. mutex_lock(&radio->lock);
  277. radio->removed = 1;
  278. mutex_unlock(&radio->lock);
  279. usb_set_intfdata(intf, NULL);
  280. video_unregister_device(radio->videodev);
  281. }
  282. /* vidioc_querycap - query device capabilities */
  283. static int vidioc_querycap(struct file *file, void *priv,
  284. struct v4l2_capability *v)
  285. {
  286. struct amradio_device *radio = video_drvdata(file);
  287. strlcpy(v->driver, "radio-mr800", sizeof(v->driver));
  288. strlcpy(v->card, "AverMedia MR 800 USB FM Radio", sizeof(v->card));
  289. usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
  290. v->version = RADIO_VERSION;
  291. v->capabilities = V4L2_CAP_TUNER;
  292. return 0;
  293. }
  294. /* vidioc_g_tuner - get tuner attributes */
  295. static int vidioc_g_tuner(struct file *file, void *priv,
  296. struct v4l2_tuner *v)
  297. {
  298. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  299. int retval;
  300. /* safety check */
  301. if (radio->removed)
  302. return -EIO;
  303. if (v->index > 0)
  304. return -EINVAL;
  305. /* TODO: Add function which look is signal stereo or not
  306. * amradio_getstat(radio);
  307. */
  308. /* we call amradio_set_stereo to set radio->stereo
  309. * Honestly, amradio_getstat should cover this in future and
  310. * amradio_set_stereo shouldn't be here
  311. */
  312. retval = amradio_set_stereo(radio, WANT_STEREO);
  313. if (retval < 0)
  314. amradio_dev_warn(&radio->videodev->dev,
  315. "set stereo failed\n");
  316. strcpy(v->name, "FM");
  317. v->type = V4L2_TUNER_RADIO;
  318. v->rangelow = FREQ_MIN * FREQ_MUL;
  319. v->rangehigh = FREQ_MAX * FREQ_MUL;
  320. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  321. v->capability = V4L2_TUNER_CAP_LOW;
  322. if (radio->stereo)
  323. v->audmode = V4L2_TUNER_MODE_STEREO;
  324. else
  325. v->audmode = V4L2_TUNER_MODE_MONO;
  326. v->signal = 0xffff; /* Can't get the signal strength, sad.. */
  327. v->afc = 0; /* Don't know what is this */
  328. return 0;
  329. }
  330. /* vidioc_s_tuner - set tuner attributes */
  331. static int vidioc_s_tuner(struct file *file, void *priv,
  332. struct v4l2_tuner *v)
  333. {
  334. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  335. int retval;
  336. /* safety check */
  337. if (radio->removed)
  338. return -EIO;
  339. if (v->index > 0)
  340. return -EINVAL;
  341. /* mono/stereo selector */
  342. switch (v->audmode) {
  343. case V4L2_TUNER_MODE_MONO:
  344. retval = amradio_set_stereo(radio, WANT_MONO);
  345. if (retval < 0)
  346. amradio_dev_warn(&radio->videodev->dev,
  347. "set mono failed\n");
  348. break;
  349. case V4L2_TUNER_MODE_STEREO:
  350. retval = amradio_set_stereo(radio, WANT_STEREO);
  351. if (retval < 0)
  352. amradio_dev_warn(&radio->videodev->dev,
  353. "set stereo failed\n");
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. return 0;
  359. }
  360. /* vidioc_s_frequency - set tuner radio frequency */
  361. static int vidioc_s_frequency(struct file *file, void *priv,
  362. struct v4l2_frequency *f)
  363. {
  364. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  365. int retval;
  366. /* safety check */
  367. if (radio->removed)
  368. return -EIO;
  369. mutex_lock(&radio->lock);
  370. radio->curfreq = f->frequency;
  371. mutex_unlock(&radio->lock);
  372. retval = amradio_setfreq(radio, radio->curfreq);
  373. if (retval < 0)
  374. amradio_dev_warn(&radio->videodev->dev,
  375. "set frequency failed\n");
  376. return 0;
  377. }
  378. /* vidioc_g_frequency - get tuner radio frequency */
  379. static int vidioc_g_frequency(struct file *file, void *priv,
  380. struct v4l2_frequency *f)
  381. {
  382. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  383. /* safety check */
  384. if (radio->removed)
  385. return -EIO;
  386. f->type = V4L2_TUNER_RADIO;
  387. f->frequency = radio->curfreq;
  388. return 0;
  389. }
  390. /* vidioc_queryctrl - enumerate control items */
  391. static int vidioc_queryctrl(struct file *file, void *priv,
  392. struct v4l2_queryctrl *qc)
  393. {
  394. int i;
  395. for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
  396. if (qc->id && qc->id == radio_qctrl[i].id) {
  397. memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
  398. return 0;
  399. }
  400. }
  401. return -EINVAL;
  402. }
  403. /* vidioc_g_ctrl - get the value of a control */
  404. static int vidioc_g_ctrl(struct file *file, void *priv,
  405. struct v4l2_control *ctrl)
  406. {
  407. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  408. /* safety check */
  409. if (radio->removed)
  410. return -EIO;
  411. switch (ctrl->id) {
  412. case V4L2_CID_AUDIO_MUTE:
  413. ctrl->value = radio->muted;
  414. return 0;
  415. }
  416. return -EINVAL;
  417. }
  418. /* vidioc_s_ctrl - set the value of a control */
  419. static int vidioc_s_ctrl(struct file *file, void *priv,
  420. struct v4l2_control *ctrl)
  421. {
  422. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  423. int retval;
  424. /* safety check */
  425. if (radio->removed)
  426. return -EIO;
  427. switch (ctrl->id) {
  428. case V4L2_CID_AUDIO_MUTE:
  429. if (ctrl->value) {
  430. retval = amradio_set_mute(radio, AMRADIO_STOP);
  431. if (retval < 0) {
  432. amradio_dev_warn(&radio->videodev->dev,
  433. "amradio_stop failed\n");
  434. return -1;
  435. }
  436. } else {
  437. retval = amradio_set_mute(radio, AMRADIO_START);
  438. if (retval < 0) {
  439. amradio_dev_warn(&radio->videodev->dev,
  440. "amradio_start failed\n");
  441. return -1;
  442. }
  443. }
  444. return 0;
  445. }
  446. return -EINVAL;
  447. }
  448. /* vidioc_g_audio - get audio attributes */
  449. static int vidioc_g_audio(struct file *file, void *priv,
  450. struct v4l2_audio *a)
  451. {
  452. if (a->index > 1)
  453. return -EINVAL;
  454. strcpy(a->name, "Radio");
  455. a->capability = V4L2_AUDCAP_STEREO;
  456. return 0;
  457. }
  458. /* vidioc_s_audio - set audio attributes */
  459. static int vidioc_s_audio(struct file *file, void *priv,
  460. struct v4l2_audio *a)
  461. {
  462. if (a->index != 0)
  463. return -EINVAL;
  464. return 0;
  465. }
  466. /* vidioc_g_input - get input */
  467. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  468. {
  469. *i = 0;
  470. return 0;
  471. }
  472. /* vidioc_s_input - set input */
  473. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  474. {
  475. if (i != 0)
  476. return -EINVAL;
  477. return 0;
  478. }
  479. /* open device - amradio_start() and amradio_setfreq() */
  480. static int usb_amradio_open(struct file *file)
  481. {
  482. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  483. int retval;
  484. lock_kernel();
  485. radio->users = 1;
  486. radio->muted = 1;
  487. retval = amradio_set_mute(radio, AMRADIO_START);
  488. if (retval < 0) {
  489. amradio_dev_warn(&radio->videodev->dev,
  490. "radio did not start up properly\n");
  491. radio->users = 0;
  492. unlock_kernel();
  493. return -EIO;
  494. }
  495. retval = amradio_set_stereo(radio, WANT_STEREO);
  496. if (retval < 0)
  497. amradio_dev_warn(&radio->videodev->dev,
  498. "set stereo failed\n");
  499. retval = amradio_setfreq(radio, radio->curfreq);
  500. if (retval < 0)
  501. amradio_dev_warn(&radio->videodev->dev,
  502. "set frequency failed\n");
  503. unlock_kernel();
  504. return 0;
  505. }
  506. /*close device */
  507. static int usb_amradio_close(struct file *file)
  508. {
  509. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  510. int retval;
  511. if (!radio)
  512. return -ENODEV;
  513. mutex_lock(&radio->lock);
  514. radio->users = 0;
  515. mutex_unlock(&radio->lock);
  516. if (!radio->removed) {
  517. retval = amradio_set_mute(radio, AMRADIO_STOP);
  518. if (retval < 0)
  519. amradio_dev_warn(&radio->videodev->dev,
  520. "amradio_stop failed\n");
  521. }
  522. return 0;
  523. }
  524. /* Suspend device - stop device. Need to be checked and fixed */
  525. static int usb_amradio_suspend(struct usb_interface *intf, pm_message_t message)
  526. {
  527. struct amradio_device *radio = usb_get_intfdata(intf);
  528. int retval;
  529. retval = amradio_set_mute(radio, AMRADIO_STOP);
  530. if (retval < 0)
  531. dev_warn(&intf->dev, "amradio_stop failed\n");
  532. dev_info(&intf->dev, "going into suspend..\n");
  533. return 0;
  534. }
  535. /* Resume device - start device. Need to be checked and fixed */
  536. static int usb_amradio_resume(struct usb_interface *intf)
  537. {
  538. struct amradio_device *radio = usb_get_intfdata(intf);
  539. int retval;
  540. retval = amradio_set_mute(radio, AMRADIO_START);
  541. if (retval < 0)
  542. dev_warn(&intf->dev, "amradio_start failed\n");
  543. dev_info(&intf->dev, "coming out of suspend..\n");
  544. return 0;
  545. }
  546. /* File system interface */
  547. static const struct v4l2_file_operations usb_amradio_fops = {
  548. .owner = THIS_MODULE,
  549. .open = usb_amradio_open,
  550. .release = usb_amradio_close,
  551. .ioctl = video_ioctl2,
  552. };
  553. static const struct v4l2_ioctl_ops usb_amradio_ioctl_ops = {
  554. .vidioc_querycap = vidioc_querycap,
  555. .vidioc_g_tuner = vidioc_g_tuner,
  556. .vidioc_s_tuner = vidioc_s_tuner,
  557. .vidioc_g_frequency = vidioc_g_frequency,
  558. .vidioc_s_frequency = vidioc_s_frequency,
  559. .vidioc_queryctrl = vidioc_queryctrl,
  560. .vidioc_g_ctrl = vidioc_g_ctrl,
  561. .vidioc_s_ctrl = vidioc_s_ctrl,
  562. .vidioc_g_audio = vidioc_g_audio,
  563. .vidioc_s_audio = vidioc_s_audio,
  564. .vidioc_g_input = vidioc_g_input,
  565. .vidioc_s_input = vidioc_s_input,
  566. };
  567. static void usb_amradio_device_release(struct video_device *videodev)
  568. {
  569. struct amradio_device *radio = video_get_drvdata(videodev);
  570. /* we call v4l to free radio->videodev */
  571. video_device_release(videodev);
  572. /* free rest memory */
  573. kfree(radio->buffer);
  574. kfree(radio);
  575. }
  576. /* V4L2 interface */
  577. static struct video_device amradio_videodev_template = {
  578. .name = "AverMedia MR 800 USB FM Radio",
  579. .fops = &usb_amradio_fops,
  580. .ioctl_ops = &usb_amradio_ioctl_ops,
  581. .release = usb_amradio_device_release,
  582. };
  583. /* check if the device is present and register with v4l and usb if it is */
  584. static int usb_amradio_probe(struct usb_interface *intf,
  585. const struct usb_device_id *id)
  586. {
  587. struct amradio_device *radio;
  588. int retval;
  589. radio = kmalloc(sizeof(struct amradio_device), GFP_KERNEL);
  590. if (!radio) {
  591. dev_err(&intf->dev, "kmalloc for amradio_device failed\n");
  592. return -ENOMEM;
  593. }
  594. radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
  595. if (!radio->buffer) {
  596. dev_err(&intf->dev, "kmalloc for radio->buffer failed\n");
  597. kfree(radio);
  598. return -ENOMEM;
  599. }
  600. radio->videodev = video_device_alloc();
  601. if (!radio->videodev) {
  602. dev_err(&intf->dev, "video_device_alloc failed\n");
  603. kfree(radio->buffer);
  604. kfree(radio);
  605. return -ENOMEM;
  606. }
  607. memcpy(radio->videodev, &amradio_videodev_template,
  608. sizeof(amradio_videodev_template));
  609. radio->removed = 0;
  610. radio->users = 0;
  611. radio->usbdev = interface_to_usbdev(intf);
  612. radio->curfreq = 95.16 * FREQ_MUL;
  613. radio->stereo = -1;
  614. mutex_init(&radio->lock);
  615. video_set_drvdata(radio->videodev, radio);
  616. retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
  617. if (retval < 0) {
  618. dev_err(&intf->dev, "could not register video device\n");
  619. video_device_release(radio->videodev);
  620. kfree(radio->buffer);
  621. kfree(radio);
  622. return -EIO;
  623. }
  624. usb_set_intfdata(intf, radio);
  625. return 0;
  626. }
  627. static int __init amradio_init(void)
  628. {
  629. int retval = usb_register(&usb_amradio_driver);
  630. pr_info(KBUILD_MODNAME
  631. ": version " DRIVER_VERSION " " DRIVER_DESC "\n");
  632. if (retval)
  633. pr_err(KBUILD_MODNAME
  634. ": usb_register failed. Error number %d\n", retval);
  635. return retval;
  636. }
  637. static void __exit amradio_exit(void)
  638. {
  639. usb_deregister(&usb_amradio_driver);
  640. }
  641. module_init(amradio_init);
  642. module_exit(amradio_exit);