radio-mr800.c 19 KB

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