radio-mr800.c 18 KB

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