radio-mr800.c 19 KB

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