radio-mr800.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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 through 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. * Version 0.11: Converted to v4l2_device.
  46. *
  47. * Many things to do:
  48. * - Correct power management of device (suspend & resume)
  49. * - Add code for scanning and smooth tuning
  50. * - Add code for sensitivity value
  51. * - Correct mistakes
  52. * - In Japan another FREQ_MIN and FREQ_MAX
  53. */
  54. /* kernel includes */
  55. #include <linux/kernel.h>
  56. #include <linux/module.h>
  57. #include <linux/init.h>
  58. #include <linux/slab.h>
  59. #include <linux/input.h>
  60. #include <linux/videodev2.h>
  61. #include <media/v4l2-device.h>
  62. #include <media/v4l2-ioctl.h>
  63. #include <linux/usb.h>
  64. #include <linux/mutex.h>
  65. /* driver and module definitions */
  66. #define DRIVER_AUTHOR "Alexey Klimov <klimov.linux@gmail.com>"
  67. #define DRIVER_DESC "AverMedia MR 800 USB FM radio driver"
  68. #define DRIVER_VERSION "0.1.2"
  69. MODULE_AUTHOR(DRIVER_AUTHOR);
  70. MODULE_DESCRIPTION(DRIVER_DESC);
  71. MODULE_LICENSE("GPL");
  72. MODULE_VERSION(DRIVER_VERSION);
  73. #define USB_AMRADIO_VENDOR 0x07ca
  74. #define USB_AMRADIO_PRODUCT 0xb800
  75. /* dev_warn macro with driver name */
  76. #define MR800_DRIVER_NAME "radio-mr800"
  77. #define amradio_dev_warn(dev, fmt, arg...) \
  78. dev_warn(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
  79. #define amradio_dev_err(dev, fmt, arg...) \
  80. dev_err(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
  81. /* Probably USB_TIMEOUT should be modified in module parameter */
  82. #define BUFFER_LENGTH 8
  83. #define USB_TIMEOUT 500
  84. /* Frequency limits in MHz -- these are European values. For Japanese
  85. devices, that would be 76 and 91. */
  86. #define FREQ_MIN 87.5
  87. #define FREQ_MAX 108.0
  88. #define FREQ_MUL 16000
  89. /*
  90. * Commands that device should understand
  91. * List isn't full and will be updated with implementation of new functions
  92. */
  93. #define AMRADIO_SET_FREQ 0xa4
  94. #define AMRADIO_SET_MUTE 0xab
  95. #define AMRADIO_SET_MONO 0xae
  96. /* Comfortable defines for amradio_set_mute */
  97. #define AMRADIO_START 0x00
  98. #define AMRADIO_STOP 0x01
  99. /* Comfortable defines for amradio_set_stereo */
  100. #define WANT_STEREO 0x00
  101. #define WANT_MONO 0x01
  102. /* module parameter */
  103. static int radio_nr = -1;
  104. module_param(radio_nr, int, 0);
  105. MODULE_PARM_DESC(radio_nr, "Radio Nr");
  106. static int usb_amradio_probe(struct usb_interface *intf,
  107. const struct usb_device_id *id);
  108. static void usb_amradio_disconnect(struct usb_interface *intf);
  109. static int usb_amradio_open(struct file *file);
  110. static int usb_amradio_close(struct file *file);
  111. static int usb_amradio_suspend(struct usb_interface *intf,
  112. pm_message_t message);
  113. static int usb_amradio_resume(struct usb_interface *intf);
  114. /* Data for one (physical) device */
  115. struct amradio_device {
  116. /* reference to USB and video device */
  117. struct usb_device *usbdev;
  118. struct usb_interface *intf;
  119. struct video_device videodev;
  120. struct v4l2_device v4l2_dev;
  121. unsigned char *buffer;
  122. struct mutex lock; /* buffer locking */
  123. int curfreq;
  124. int stereo;
  125. int muted;
  126. int initialized;
  127. };
  128. static inline struct amradio_device *to_amradio_dev(struct v4l2_device *v4l2_dev)
  129. {
  130. return container_of(v4l2_dev, struct amradio_device, v4l2_dev);
  131. }
  132. /* USB Device ID List */
  133. static struct usb_device_id usb_amradio_device_table[] = {
  134. {USB_DEVICE_AND_INTERFACE_INFO(USB_AMRADIO_VENDOR, USB_AMRADIO_PRODUCT,
  135. USB_CLASS_HID, 0, 0) },
  136. { } /* Terminating entry */
  137. };
  138. MODULE_DEVICE_TABLE(usb, usb_amradio_device_table);
  139. /* USB subsystem interface */
  140. static struct usb_driver usb_amradio_driver = {
  141. .name = MR800_DRIVER_NAME,
  142. .probe = usb_amradio_probe,
  143. .disconnect = usb_amradio_disconnect,
  144. .suspend = usb_amradio_suspend,
  145. .resume = usb_amradio_resume,
  146. .reset_resume = usb_amradio_resume,
  147. .id_table = usb_amradio_device_table,
  148. .supports_autosuspend = 1,
  149. };
  150. /* switch on/off the radio. Send 8 bytes to device */
  151. static int amradio_set_mute(struct amradio_device *radio, char argument)
  152. {
  153. int retval;
  154. int size;
  155. radio->buffer[0] = 0x00;
  156. radio->buffer[1] = 0x55;
  157. radio->buffer[2] = 0xaa;
  158. radio->buffer[3] = 0x00;
  159. radio->buffer[4] = AMRADIO_SET_MUTE;
  160. radio->buffer[5] = argument;
  161. radio->buffer[6] = 0x00;
  162. radio->buffer[7] = 0x00;
  163. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  164. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  165. if (retval < 0 || size != BUFFER_LENGTH) {
  166. amradio_dev_warn(&radio->videodev.dev, "set mute failed\n");
  167. return retval;
  168. }
  169. radio->muted = argument;
  170. return retval;
  171. }
  172. /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
  173. static int amradio_setfreq(struct amradio_device *radio, int freq)
  174. {
  175. int retval;
  176. int size;
  177. unsigned short freq_send = 0x10 + (freq >> 3) / 25;
  178. radio->buffer[0] = 0x00;
  179. radio->buffer[1] = 0x55;
  180. radio->buffer[2] = 0xaa;
  181. radio->buffer[3] = 0x03;
  182. radio->buffer[4] = AMRADIO_SET_FREQ;
  183. radio->buffer[5] = 0x00;
  184. radio->buffer[6] = 0x00;
  185. radio->buffer[7] = 0x08;
  186. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  187. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  188. if (retval < 0 || size != BUFFER_LENGTH)
  189. goto out_err;
  190. /* frequency is calculated from freq_send and placed in first 2 bytes */
  191. radio->buffer[0] = (freq_send >> 8) & 0xff;
  192. radio->buffer[1] = freq_send & 0xff;
  193. radio->buffer[2] = 0x01;
  194. radio->buffer[3] = 0x00;
  195. radio->buffer[4] = 0x00;
  196. /* 5 and 6 bytes of buffer already = 0x00 */
  197. radio->buffer[7] = 0x00;
  198. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  199. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  200. if (retval < 0 || size != BUFFER_LENGTH)
  201. goto out_err;
  202. radio->curfreq = freq;
  203. goto out;
  204. out_err:
  205. amradio_dev_warn(&radio->videodev.dev, "set frequency failed\n");
  206. out:
  207. return retval;
  208. }
  209. static int amradio_set_stereo(struct amradio_device *radio, char argument)
  210. {
  211. int retval;
  212. int size;
  213. radio->buffer[0] = 0x00;
  214. radio->buffer[1] = 0x55;
  215. radio->buffer[2] = 0xaa;
  216. radio->buffer[3] = 0x00;
  217. radio->buffer[4] = AMRADIO_SET_MONO;
  218. radio->buffer[5] = argument;
  219. radio->buffer[6] = 0x00;
  220. radio->buffer[7] = 0x00;
  221. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  222. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  223. if (retval < 0 || size != BUFFER_LENGTH) {
  224. amradio_dev_warn(&radio->videodev.dev, "set stereo failed\n");
  225. return retval;
  226. }
  227. if (argument == WANT_STEREO)
  228. radio->stereo = 1;
  229. else
  230. radio->stereo = 0;
  231. return retval;
  232. }
  233. /* Handle unplugging the device.
  234. * We call video_unregister_device in any case.
  235. * The last function called in this procedure is
  236. * usb_amradio_device_release.
  237. */
  238. static void usb_amradio_disconnect(struct usb_interface *intf)
  239. {
  240. struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
  241. mutex_lock(&radio->lock);
  242. /* increase the device node's refcount */
  243. get_device(&radio->videodev.dev);
  244. v4l2_device_disconnect(&radio->v4l2_dev);
  245. video_unregister_device(&radio->videodev);
  246. mutex_unlock(&radio->lock);
  247. /* decrease the device node's refcount, allowing it to be released */
  248. put_device(&radio->videodev.dev);
  249. }
  250. /* vidioc_querycap - query device capabilities */
  251. static int vidioc_querycap(struct file *file, void *priv,
  252. struct v4l2_capability *v)
  253. {
  254. struct amradio_device *radio = file->private_data;
  255. strlcpy(v->driver, "radio-mr800", sizeof(v->driver));
  256. strlcpy(v->card, "AverMedia MR 800 USB FM Radio", sizeof(v->card));
  257. usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
  258. v->capabilities = V4L2_CAP_TUNER;
  259. return 0;
  260. }
  261. /* vidioc_g_tuner - get tuner attributes */
  262. static int vidioc_g_tuner(struct file *file, void *priv,
  263. struct v4l2_tuner *v)
  264. {
  265. struct amradio_device *radio = file->private_data;
  266. int retval;
  267. if (v->index > 0)
  268. return -EINVAL;
  269. /* TODO: Add function which look is signal stereo or not
  270. * amradio_getstat(radio);
  271. */
  272. /* we call amradio_set_stereo to set radio->stereo
  273. * Honestly, amradio_getstat should cover this in future and
  274. * amradio_set_stereo shouldn't be here
  275. */
  276. retval = amradio_set_stereo(radio, WANT_STEREO);
  277. strcpy(v->name, "FM");
  278. v->type = V4L2_TUNER_RADIO;
  279. v->rangelow = FREQ_MIN * FREQ_MUL;
  280. v->rangehigh = FREQ_MAX * FREQ_MUL;
  281. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  282. v->capability = V4L2_TUNER_CAP_LOW;
  283. if (radio->stereo)
  284. v->audmode = V4L2_TUNER_MODE_STEREO;
  285. else
  286. v->audmode = V4L2_TUNER_MODE_MONO;
  287. v->signal = 0xffff; /* Can't get the signal strength, sad.. */
  288. v->afc = 0; /* Don't know what is this */
  289. return retval;
  290. }
  291. /* vidioc_s_tuner - set tuner attributes */
  292. static int vidioc_s_tuner(struct file *file, void *priv,
  293. struct v4l2_tuner *v)
  294. {
  295. struct amradio_device *radio = file->private_data;
  296. int retval = -EINVAL;
  297. if (v->index > 0)
  298. return -EINVAL;
  299. /* mono/stereo selector */
  300. switch (v->audmode) {
  301. case V4L2_TUNER_MODE_MONO:
  302. retval = amradio_set_stereo(radio, WANT_MONO);
  303. break;
  304. case V4L2_TUNER_MODE_STEREO:
  305. retval = amradio_set_stereo(radio, WANT_STEREO);
  306. break;
  307. }
  308. return retval;
  309. }
  310. /* vidioc_s_frequency - set tuner radio frequency */
  311. static int vidioc_s_frequency(struct file *file, void *priv,
  312. struct v4l2_frequency *f)
  313. {
  314. struct amradio_device *radio = file->private_data;
  315. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  316. return -EINVAL;
  317. return amradio_setfreq(radio, f->frequency);
  318. }
  319. /* vidioc_g_frequency - get tuner radio frequency */
  320. static int vidioc_g_frequency(struct file *file, void *priv,
  321. struct v4l2_frequency *f)
  322. {
  323. struct amradio_device *radio = file->private_data;
  324. if (f->tuner != 0)
  325. return -EINVAL;
  326. f->type = V4L2_TUNER_RADIO;
  327. f->frequency = radio->curfreq;
  328. return 0;
  329. }
  330. /* vidioc_queryctrl - enumerate control items */
  331. static int vidioc_queryctrl(struct file *file, void *priv,
  332. struct v4l2_queryctrl *qc)
  333. {
  334. switch (qc->id) {
  335. case V4L2_CID_AUDIO_MUTE:
  336. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  337. }
  338. return -EINVAL;
  339. }
  340. /* vidioc_g_ctrl - get the value of a control */
  341. static int vidioc_g_ctrl(struct file *file, void *priv,
  342. struct v4l2_control *ctrl)
  343. {
  344. struct amradio_device *radio = file->private_data;
  345. switch (ctrl->id) {
  346. case V4L2_CID_AUDIO_MUTE:
  347. ctrl->value = radio->muted;
  348. return 0;
  349. }
  350. return -EINVAL;
  351. }
  352. /* vidioc_s_ctrl - set the value of a control */
  353. static int vidioc_s_ctrl(struct file *file, void *priv,
  354. struct v4l2_control *ctrl)
  355. {
  356. struct amradio_device *radio = file->private_data;
  357. int retval = -EINVAL;
  358. switch (ctrl->id) {
  359. case V4L2_CID_AUDIO_MUTE:
  360. if (ctrl->value)
  361. retval = amradio_set_mute(radio, AMRADIO_STOP);
  362. else
  363. retval = amradio_set_mute(radio, AMRADIO_START);
  364. break;
  365. }
  366. return retval;
  367. }
  368. /* vidioc_g_audio - get audio attributes */
  369. static int vidioc_g_audio(struct file *file, void *priv,
  370. struct v4l2_audio *a)
  371. {
  372. if (a->index > 1)
  373. return -EINVAL;
  374. strcpy(a->name, "Radio");
  375. a->capability = V4L2_AUDCAP_STEREO;
  376. return 0;
  377. }
  378. /* vidioc_s_audio - set audio attributes */
  379. static int vidioc_s_audio(struct file *file, void *priv,
  380. struct v4l2_audio *a)
  381. {
  382. if (a->index != 0)
  383. return -EINVAL;
  384. return 0;
  385. }
  386. /* vidioc_g_input - get input */
  387. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  388. {
  389. *i = 0;
  390. return 0;
  391. }
  392. /* vidioc_s_input - set input */
  393. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  394. {
  395. if (i != 0)
  396. return -EINVAL;
  397. return 0;
  398. }
  399. static int usb_amradio_init(struct amradio_device *radio)
  400. {
  401. int retval;
  402. retval = amradio_set_mute(radio, AMRADIO_STOP);
  403. if (retval)
  404. goto out_err;
  405. retval = amradio_set_stereo(radio, WANT_STEREO);
  406. if (retval)
  407. goto out_err;
  408. radio->initialized = 1;
  409. goto out;
  410. out_err:
  411. amradio_dev_err(&radio->videodev.dev, "initialization failed\n");
  412. out:
  413. return retval;
  414. }
  415. /* open device - amradio_start() and amradio_setfreq() */
  416. static int usb_amradio_open(struct file *file)
  417. {
  418. struct amradio_device *radio = video_drvdata(file);
  419. int retval;
  420. file->private_data = radio;
  421. retval = usb_autopm_get_interface(radio->intf);
  422. if (retval)
  423. return retval;
  424. if (unlikely(!radio->initialized)) {
  425. retval = usb_amradio_init(radio);
  426. if (retval)
  427. usb_autopm_put_interface(radio->intf);
  428. }
  429. return retval;
  430. }
  431. /*close device */
  432. static int usb_amradio_close(struct file *file)
  433. {
  434. struct amradio_device *radio = file->private_data;
  435. if (video_is_registered(&radio->videodev))
  436. usb_autopm_put_interface(radio->intf);
  437. return 0;
  438. }
  439. /* Suspend device - stop device. Need to be checked and fixed */
  440. static int usb_amradio_suspend(struct usb_interface *intf, pm_message_t message)
  441. {
  442. struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
  443. mutex_lock(&radio->lock);
  444. if (!radio->muted && radio->initialized) {
  445. amradio_set_mute(radio, AMRADIO_STOP);
  446. radio->muted = 0;
  447. }
  448. mutex_unlock(&radio->lock);
  449. dev_info(&intf->dev, "going into suspend..\n");
  450. return 0;
  451. }
  452. /* Resume device - start device. Need to be checked and fixed */
  453. static int usb_amradio_resume(struct usb_interface *intf)
  454. {
  455. struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
  456. mutex_lock(&radio->lock);
  457. if (unlikely(!radio->initialized))
  458. goto unlock;
  459. if (radio->stereo)
  460. amradio_set_stereo(radio, WANT_STEREO);
  461. else
  462. amradio_set_stereo(radio, WANT_MONO);
  463. amradio_setfreq(radio, radio->curfreq);
  464. if (!radio->muted)
  465. amradio_set_mute(radio, AMRADIO_START);
  466. unlock:
  467. mutex_unlock(&radio->lock);
  468. dev_info(&intf->dev, "coming out of suspend..\n");
  469. return 0;
  470. }
  471. /* File system interface */
  472. static const struct v4l2_file_operations usb_amradio_fops = {
  473. .owner = THIS_MODULE,
  474. .open = usb_amradio_open,
  475. .release = usb_amradio_close,
  476. .unlocked_ioctl = video_ioctl2,
  477. };
  478. static const struct v4l2_ioctl_ops usb_amradio_ioctl_ops = {
  479. .vidioc_querycap = vidioc_querycap,
  480. .vidioc_g_tuner = vidioc_g_tuner,
  481. .vidioc_s_tuner = vidioc_s_tuner,
  482. .vidioc_g_frequency = vidioc_g_frequency,
  483. .vidioc_s_frequency = vidioc_s_frequency,
  484. .vidioc_queryctrl = vidioc_queryctrl,
  485. .vidioc_g_ctrl = vidioc_g_ctrl,
  486. .vidioc_s_ctrl = vidioc_s_ctrl,
  487. .vidioc_g_audio = vidioc_g_audio,
  488. .vidioc_s_audio = vidioc_s_audio,
  489. .vidioc_g_input = vidioc_g_input,
  490. .vidioc_s_input = vidioc_s_input,
  491. };
  492. static void usb_amradio_video_device_release(struct video_device *videodev)
  493. {
  494. struct amradio_device *radio = video_get_drvdata(videodev);
  495. /* free rest memory */
  496. kfree(radio->buffer);
  497. kfree(radio);
  498. }
  499. /* check if the device is present and register with v4l and usb if it is */
  500. static int usb_amradio_probe(struct usb_interface *intf,
  501. const struct usb_device_id *id)
  502. {
  503. struct amradio_device *radio;
  504. int retval = 0;
  505. radio = kzalloc(sizeof(struct amradio_device), GFP_KERNEL);
  506. if (!radio) {
  507. dev_err(&intf->dev, "kmalloc for amradio_device failed\n");
  508. retval = -ENOMEM;
  509. goto err;
  510. }
  511. radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
  512. if (!radio->buffer) {
  513. dev_err(&intf->dev, "kmalloc for radio->buffer failed\n");
  514. retval = -ENOMEM;
  515. goto err_nobuf;
  516. }
  517. retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
  518. if (retval < 0) {
  519. dev_err(&intf->dev, "couldn't register v4l2_device\n");
  520. goto err_v4l2;
  521. }
  522. mutex_init(&radio->lock);
  523. strlcpy(radio->videodev.name, radio->v4l2_dev.name,
  524. sizeof(radio->videodev.name));
  525. radio->videodev.v4l2_dev = &radio->v4l2_dev;
  526. radio->videodev.fops = &usb_amradio_fops;
  527. radio->videodev.ioctl_ops = &usb_amradio_ioctl_ops;
  528. radio->videodev.release = usb_amradio_video_device_release;
  529. radio->videodev.lock = &radio->lock;
  530. radio->usbdev = interface_to_usbdev(intf);
  531. radio->intf = intf;
  532. radio->curfreq = 95.16 * FREQ_MUL;
  533. video_set_drvdata(&radio->videodev, radio);
  534. retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
  535. radio_nr);
  536. if (retval < 0) {
  537. dev_err(&intf->dev, "could not register video device\n");
  538. goto err_vdev;
  539. }
  540. return 0;
  541. err_vdev:
  542. v4l2_device_unregister(&radio->v4l2_dev);
  543. err_v4l2:
  544. kfree(radio->buffer);
  545. err_nobuf:
  546. kfree(radio);
  547. err:
  548. return retval;
  549. }
  550. module_usb_driver(usb_amradio_driver);