radio-mr800.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 removed;
  126. int muted;
  127. };
  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. /* safety check */
  152. if (radio->removed)
  153. return -EIO;
  154. mutex_lock(&radio->lock);
  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. mutex_unlock(&radio->lock);
  167. return retval;
  168. }
  169. radio->muted = argument;
  170. mutex_unlock(&radio->lock);
  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. /* safety check */
  180. if (radio->removed)
  181. return -EIO;
  182. mutex_lock(&radio->lock);
  183. radio->buffer[0] = 0x00;
  184. radio->buffer[1] = 0x55;
  185. radio->buffer[2] = 0xaa;
  186. radio->buffer[3] = 0x03;
  187. radio->buffer[4] = AMRADIO_SET_FREQ;
  188. radio->buffer[5] = 0x00;
  189. radio->buffer[6] = 0x00;
  190. radio->buffer[7] = 0x08;
  191. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  192. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  193. if (retval < 0 || size != BUFFER_LENGTH) {
  194. mutex_unlock(&radio->lock);
  195. return retval;
  196. }
  197. /* frequency is calculated from freq_send and placed in first 2 bytes */
  198. radio->buffer[0] = (freq_send >> 8) & 0xff;
  199. radio->buffer[1] = freq_send & 0xff;
  200. radio->buffer[2] = 0x01;
  201. radio->buffer[3] = 0x00;
  202. radio->buffer[4] = 0x00;
  203. /* 5 and 6 bytes of buffer already = 0x00 */
  204. radio->buffer[7] = 0x00;
  205. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  206. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  207. if (retval < 0 || size != BUFFER_LENGTH) {
  208. mutex_unlock(&radio->lock);
  209. return retval;
  210. }
  211. mutex_unlock(&radio->lock);
  212. return retval;
  213. }
  214. static int amradio_set_stereo(struct amradio_device *radio, char argument)
  215. {
  216. int retval;
  217. int size;
  218. /* safety check */
  219. if (radio->removed)
  220. return -EIO;
  221. mutex_lock(&radio->lock);
  222. radio->buffer[0] = 0x00;
  223. radio->buffer[1] = 0x55;
  224. radio->buffer[2] = 0xaa;
  225. radio->buffer[3] = 0x00;
  226. radio->buffer[4] = AMRADIO_SET_MONO;
  227. radio->buffer[5] = argument;
  228. radio->buffer[6] = 0x00;
  229. radio->buffer[7] = 0x00;
  230. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  231. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  232. if (retval < 0 || size != BUFFER_LENGTH) {
  233. radio->stereo = -1;
  234. mutex_unlock(&radio->lock);
  235. return retval;
  236. }
  237. radio->stereo = 1;
  238. mutex_unlock(&radio->lock);
  239. return retval;
  240. }
  241. /* Handle unplugging the device.
  242. * We call video_unregister_device in any case.
  243. * The last function called in this procedure is
  244. * usb_amradio_device_release.
  245. */
  246. static void usb_amradio_disconnect(struct usb_interface *intf)
  247. {
  248. struct amradio_device *radio = usb_get_intfdata(intf);
  249. mutex_lock(&radio->lock);
  250. radio->removed = 1;
  251. mutex_unlock(&radio->lock);
  252. usb_set_intfdata(intf, NULL);
  253. video_unregister_device(radio->videodev);
  254. v4l2_device_disconnect(&radio->v4l2_dev);
  255. }
  256. /* vidioc_querycap - query device capabilities */
  257. static int vidioc_querycap(struct file *file, void *priv,
  258. struct v4l2_capability *v)
  259. {
  260. struct amradio_device *radio = video_drvdata(file);
  261. strlcpy(v->driver, "radio-mr800", sizeof(v->driver));
  262. strlcpy(v->card, "AverMedia MR 800 USB FM Radio", sizeof(v->card));
  263. usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
  264. v->version = RADIO_VERSION;
  265. v->capabilities = V4L2_CAP_TUNER;
  266. return 0;
  267. }
  268. /* vidioc_g_tuner - get tuner attributes */
  269. static int vidioc_g_tuner(struct file *file, void *priv,
  270. struct v4l2_tuner *v)
  271. {
  272. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  273. int retval;
  274. /* safety check */
  275. if (radio->removed)
  276. return -EIO;
  277. if (v->index > 0)
  278. return -EINVAL;
  279. /* TODO: Add function which look is signal stereo or not
  280. * amradio_getstat(radio);
  281. */
  282. /* we call amradio_set_stereo to set radio->stereo
  283. * Honestly, amradio_getstat should cover this in future and
  284. * amradio_set_stereo shouldn't be here
  285. */
  286. retval = amradio_set_stereo(radio, WANT_STEREO);
  287. if (retval < 0)
  288. amradio_dev_warn(&radio->videodev->dev,
  289. "set stereo failed\n");
  290. strcpy(v->name, "FM");
  291. v->type = V4L2_TUNER_RADIO;
  292. v->rangelow = FREQ_MIN * FREQ_MUL;
  293. v->rangehigh = FREQ_MAX * FREQ_MUL;
  294. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  295. v->capability = V4L2_TUNER_CAP_LOW;
  296. if (radio->stereo)
  297. v->audmode = V4L2_TUNER_MODE_STEREO;
  298. else
  299. v->audmode = V4L2_TUNER_MODE_MONO;
  300. v->signal = 0xffff; /* Can't get the signal strength, sad.. */
  301. v->afc = 0; /* Don't know what is this */
  302. return 0;
  303. }
  304. /* vidioc_s_tuner - set tuner attributes */
  305. static int vidioc_s_tuner(struct file *file, void *priv,
  306. struct v4l2_tuner *v)
  307. {
  308. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  309. int retval;
  310. /* safety check */
  311. if (radio->removed)
  312. return -EIO;
  313. if (v->index > 0)
  314. return -EINVAL;
  315. /* mono/stereo selector */
  316. switch (v->audmode) {
  317. case V4L2_TUNER_MODE_MONO:
  318. retval = amradio_set_stereo(radio, WANT_MONO);
  319. if (retval < 0)
  320. amradio_dev_warn(&radio->videodev->dev,
  321. "set mono failed\n");
  322. break;
  323. case V4L2_TUNER_MODE_STEREO:
  324. retval = amradio_set_stereo(radio, WANT_STEREO);
  325. if (retval < 0)
  326. amradio_dev_warn(&radio->videodev->dev,
  327. "set stereo failed\n");
  328. break;
  329. default:
  330. return -EINVAL;
  331. }
  332. return 0;
  333. }
  334. /* vidioc_s_frequency - set tuner radio frequency */
  335. static int vidioc_s_frequency(struct file *file, void *priv,
  336. struct v4l2_frequency *f)
  337. {
  338. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  339. int retval;
  340. /* safety check */
  341. if (radio->removed)
  342. return -EIO;
  343. mutex_lock(&radio->lock);
  344. radio->curfreq = f->frequency;
  345. mutex_unlock(&radio->lock);
  346. retval = amradio_setfreq(radio, radio->curfreq);
  347. if (retval < 0)
  348. amradio_dev_warn(&radio->videodev->dev,
  349. "set frequency failed\n");
  350. return 0;
  351. }
  352. /* vidioc_g_frequency - get tuner radio frequency */
  353. static int vidioc_g_frequency(struct file *file, void *priv,
  354. struct v4l2_frequency *f)
  355. {
  356. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  357. /* safety check */
  358. if (radio->removed)
  359. return -EIO;
  360. f->type = V4L2_TUNER_RADIO;
  361. f->frequency = radio->curfreq;
  362. return 0;
  363. }
  364. /* vidioc_queryctrl - enumerate control items */
  365. static int vidioc_queryctrl(struct file *file, void *priv,
  366. struct v4l2_queryctrl *qc)
  367. {
  368. switch (qc->id) {
  369. case V4L2_CID_AUDIO_MUTE:
  370. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  371. }
  372. return -EINVAL;
  373. }
  374. /* vidioc_g_ctrl - get the value of a control */
  375. static int vidioc_g_ctrl(struct file *file, void *priv,
  376. struct v4l2_control *ctrl)
  377. {
  378. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  379. /* safety check */
  380. if (radio->removed)
  381. return -EIO;
  382. switch (ctrl->id) {
  383. case V4L2_CID_AUDIO_MUTE:
  384. ctrl->value = radio->muted;
  385. return 0;
  386. }
  387. return -EINVAL;
  388. }
  389. /* vidioc_s_ctrl - set the value of a control */
  390. static int vidioc_s_ctrl(struct file *file, void *priv,
  391. struct v4l2_control *ctrl)
  392. {
  393. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  394. int retval;
  395. /* safety check */
  396. if (radio->removed)
  397. return -EIO;
  398. switch (ctrl->id) {
  399. case V4L2_CID_AUDIO_MUTE:
  400. if (ctrl->value) {
  401. retval = amradio_set_mute(radio, AMRADIO_STOP);
  402. if (retval < 0) {
  403. amradio_dev_warn(&radio->videodev->dev,
  404. "amradio_stop failed\n");
  405. return -1;
  406. }
  407. } else {
  408. retval = amradio_set_mute(radio, AMRADIO_START);
  409. if (retval < 0) {
  410. amradio_dev_warn(&radio->videodev->dev,
  411. "amradio_start failed\n");
  412. return -1;
  413. }
  414. }
  415. return 0;
  416. }
  417. return -EINVAL;
  418. }
  419. /* vidioc_g_audio - get audio attributes */
  420. static int vidioc_g_audio(struct file *file, void *priv,
  421. struct v4l2_audio *a)
  422. {
  423. if (a->index > 1)
  424. return -EINVAL;
  425. strcpy(a->name, "Radio");
  426. a->capability = V4L2_AUDCAP_STEREO;
  427. return 0;
  428. }
  429. /* vidioc_s_audio - set audio attributes */
  430. static int vidioc_s_audio(struct file *file, void *priv,
  431. struct v4l2_audio *a)
  432. {
  433. if (a->index != 0)
  434. return -EINVAL;
  435. return 0;
  436. }
  437. /* vidioc_g_input - get input */
  438. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  439. {
  440. *i = 0;
  441. return 0;
  442. }
  443. /* vidioc_s_input - set input */
  444. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  445. {
  446. if (i != 0)
  447. return -EINVAL;
  448. return 0;
  449. }
  450. /* open device - amradio_start() and amradio_setfreq() */
  451. static int usb_amradio_open(struct file *file)
  452. {
  453. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  454. int retval;
  455. lock_kernel();
  456. radio->users = 1;
  457. radio->muted = 1;
  458. retval = amradio_set_mute(radio, AMRADIO_START);
  459. if (retval < 0) {
  460. amradio_dev_warn(&radio->videodev->dev,
  461. "radio did not start up properly\n");
  462. radio->users = 0;
  463. unlock_kernel();
  464. return -EIO;
  465. }
  466. retval = amradio_set_stereo(radio, WANT_STEREO);
  467. if (retval < 0)
  468. amradio_dev_warn(&radio->videodev->dev,
  469. "set stereo failed\n");
  470. retval = amradio_setfreq(radio, radio->curfreq);
  471. if (retval < 0)
  472. amradio_dev_warn(&radio->videodev->dev,
  473. "set frequency failed\n");
  474. unlock_kernel();
  475. return 0;
  476. }
  477. /*close device */
  478. static int usb_amradio_close(struct file *file)
  479. {
  480. struct amradio_device *radio = video_get_drvdata(video_devdata(file));
  481. int retval;
  482. if (!radio)
  483. return -ENODEV;
  484. mutex_lock(&radio->lock);
  485. radio->users = 0;
  486. mutex_unlock(&radio->lock);
  487. if (!radio->removed) {
  488. retval = amradio_set_mute(radio, AMRADIO_STOP);
  489. if (retval < 0)
  490. amradio_dev_warn(&radio->videodev->dev,
  491. "amradio_stop failed\n");
  492. }
  493. return 0;
  494. }
  495. /* Suspend device - stop device. Need to be checked and fixed */
  496. static int usb_amradio_suspend(struct usb_interface *intf, pm_message_t message)
  497. {
  498. struct amradio_device *radio = usb_get_intfdata(intf);
  499. int retval;
  500. retval = amradio_set_mute(radio, AMRADIO_STOP);
  501. if (retval < 0)
  502. dev_warn(&intf->dev, "amradio_stop failed\n");
  503. dev_info(&intf->dev, "going into suspend..\n");
  504. return 0;
  505. }
  506. /* Resume device - start device. Need to be checked and fixed */
  507. static int usb_amradio_resume(struct usb_interface *intf)
  508. {
  509. struct amradio_device *radio = usb_get_intfdata(intf);
  510. int retval;
  511. retval = amradio_set_mute(radio, AMRADIO_START);
  512. if (retval < 0)
  513. dev_warn(&intf->dev, "amradio_start failed\n");
  514. dev_info(&intf->dev, "coming out of suspend..\n");
  515. return 0;
  516. }
  517. /* File system interface */
  518. static const struct v4l2_file_operations usb_amradio_fops = {
  519. .owner = THIS_MODULE,
  520. .open = usb_amradio_open,
  521. .release = usb_amradio_close,
  522. .ioctl = video_ioctl2,
  523. };
  524. static const struct v4l2_ioctl_ops usb_amradio_ioctl_ops = {
  525. .vidioc_querycap = vidioc_querycap,
  526. .vidioc_g_tuner = vidioc_g_tuner,
  527. .vidioc_s_tuner = vidioc_s_tuner,
  528. .vidioc_g_frequency = vidioc_g_frequency,
  529. .vidioc_s_frequency = vidioc_s_frequency,
  530. .vidioc_queryctrl = vidioc_queryctrl,
  531. .vidioc_g_ctrl = vidioc_g_ctrl,
  532. .vidioc_s_ctrl = vidioc_s_ctrl,
  533. .vidioc_g_audio = vidioc_g_audio,
  534. .vidioc_s_audio = vidioc_s_audio,
  535. .vidioc_g_input = vidioc_g_input,
  536. .vidioc_s_input = vidioc_s_input,
  537. };
  538. static void usb_amradio_video_device_release(struct video_device *videodev)
  539. {
  540. struct amradio_device *radio = video_get_drvdata(videodev);
  541. /* we call v4l to free radio->videodev */
  542. video_device_release(videodev);
  543. v4l2_device_unregister(&radio->v4l2_dev);
  544. /* free rest memory */
  545. kfree(radio->buffer);
  546. kfree(radio);
  547. }
  548. /* check if the device is present and register with v4l and usb if it is */
  549. static int usb_amradio_probe(struct usb_interface *intf,
  550. const struct usb_device_id *id)
  551. {
  552. struct amradio_device *radio;
  553. struct v4l2_device *v4l2_dev;
  554. int retval;
  555. radio = kzalloc(sizeof(struct amradio_device), GFP_KERNEL);
  556. if (!radio) {
  557. dev_err(&intf->dev, "kmalloc for amradio_device failed\n");
  558. return -ENOMEM;
  559. }
  560. radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
  561. if (!radio->buffer) {
  562. dev_err(&intf->dev, "kmalloc for radio->buffer failed\n");
  563. kfree(radio);
  564. return -ENOMEM;
  565. }
  566. v4l2_dev = &radio->v4l2_dev;
  567. retval = v4l2_device_register(&intf->dev, v4l2_dev);
  568. if (retval < 0) {
  569. dev_err(&intf->dev, "couldn't register v4l2_device\n");
  570. kfree(radio->buffer);
  571. kfree(radio);
  572. return retval;
  573. }
  574. radio->videodev = video_device_alloc();
  575. if (!radio->videodev) {
  576. dev_err(&intf->dev, "video_device_alloc failed\n");
  577. kfree(radio->buffer);
  578. kfree(radio);
  579. return -ENOMEM;
  580. }
  581. strlcpy(radio->videodev->name, v4l2_dev->name, sizeof(radio->videodev->name));
  582. radio->videodev->v4l2_dev = v4l2_dev;
  583. radio->videodev->fops = &usb_amradio_fops;
  584. radio->videodev->ioctl_ops = &usb_amradio_ioctl_ops;
  585. radio->videodev->release = usb_amradio_video_device_release;
  586. radio->removed = 0;
  587. radio->users = 0;
  588. radio->usbdev = interface_to_usbdev(intf);
  589. radio->curfreq = 95.16 * FREQ_MUL;
  590. radio->stereo = -1;
  591. mutex_init(&radio->lock);
  592. video_set_drvdata(radio->videodev, radio);
  593. retval = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
  594. if (retval < 0) {
  595. dev_err(&intf->dev, "could not register video device\n");
  596. video_device_release(radio->videodev);
  597. v4l2_device_unregister(v4l2_dev);
  598. kfree(radio->buffer);
  599. kfree(radio);
  600. return -EIO;
  601. }
  602. usb_set_intfdata(intf, radio);
  603. return 0;
  604. }
  605. static int __init amradio_init(void)
  606. {
  607. int retval = usb_register(&usb_amradio_driver);
  608. pr_info(KBUILD_MODNAME
  609. ": version " DRIVER_VERSION " " DRIVER_DESC "\n");
  610. if (retval)
  611. pr_err(KBUILD_MODNAME
  612. ": usb_register failed. Error number %d\n", retval);
  613. return retval;
  614. }
  615. static void __exit amradio_exit(void)
  616. {
  617. usb_deregister(&usb_amradio_driver);
  618. }
  619. module_init(amradio_init);
  620. module_exit(amradio_exit);