radio-mr800.c 18 KB

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