radio-mr800.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /*
  2. * A driver for the AverMedia MR 800 USB FM radio. This device plugs
  3. * into both the USB and an analog audio input, so this thing
  4. * only deals with initialization and frequency setting, the
  5. * audio data has to be handled by a sound driver.
  6. *
  7. * Copyright (c) 2008 Alexey Klimov <klimov.linux@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /*
  24. * Big thanks to authors and contributors of dsbr100.c and radio-si470x.c
  25. *
  26. * When work was looked pretty good, i discover this:
  27. * http://av-usbradio.sourceforge.net/index.php
  28. * http://sourceforge.net/projects/av-usbradio/
  29. * Latest release of theirs project was in 2005.
  30. * Probably, this driver could be improved through using their
  31. * achievements (specifications given).
  32. * Also, Faidon Liambotis <paravoid@debian.org> wrote nice driver for this radio
  33. * in 2007. He allowed to use his driver to improve current mr800 radio driver.
  34. * http://kerneltrap.org/mailarchive/linux-usb-devel/2007/10/11/342492
  35. *
  36. * Version 0.01: First working version.
  37. * It's required to blacklist AverMedia USB Radio
  38. * in usbhid/hid-quirks.c
  39. * Version 0.10: A lot of cleanups and fixes: unpluging the device,
  40. * few mutex locks were added, codinstyle issues, etc.
  41. * Added stereo support. Thanks to
  42. * Douglas Schilling Landgraf <dougsland@gmail.com> and
  43. * David Ellingsworth <david@identd.dyndns.org>
  44. * for discussion, help and support.
  45. * Version 0.11: Converted to v4l2_device.
  46. *
  47. * Many things to do:
  48. * - Correct power management of device (suspend & resume)
  49. * - Add code for scanning and smooth tuning
  50. * - Add code for sensitivity value
  51. * - Correct mistakes
  52. * - In Japan another FREQ_MIN and FREQ_MAX
  53. */
  54. /* kernel includes */
  55. #include <linux/kernel.h>
  56. #include <linux/module.h>
  57. #include <linux/init.h>
  58. #include <linux/slab.h>
  59. #include <linux/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. #define amradio_dev_err(dev, fmt, arg...) \
  82. dev_err(dev, MR800_DRIVER_NAME " - " fmt, ##arg)
  83. /* Probably USB_TIMEOUT should be modified in module parameter */
  84. #define BUFFER_LENGTH 8
  85. #define USB_TIMEOUT 500
  86. /* Frequency limits in MHz -- these are European values. For Japanese
  87. devices, that would be 76 and 91. */
  88. #define FREQ_MIN 87.5
  89. #define FREQ_MAX 108.0
  90. #define FREQ_MUL 16000
  91. /*
  92. * Commands that device should understand
  93. * List isnt full and will be updated with implementation of new functions
  94. */
  95. #define AMRADIO_SET_FREQ 0xa4
  96. #define AMRADIO_SET_MUTE 0xab
  97. #define AMRADIO_SET_MONO 0xae
  98. /* Comfortable defines for amradio_set_mute */
  99. #define AMRADIO_START 0x00
  100. #define AMRADIO_STOP 0x01
  101. /* Comfortable defines for amradio_set_stereo */
  102. #define WANT_STEREO 0x00
  103. #define WANT_MONO 0x01
  104. /* module parameter */
  105. static int radio_nr = -1;
  106. module_param(radio_nr, int, 0);
  107. MODULE_PARM_DESC(radio_nr, "Radio Nr");
  108. static int usb_amradio_probe(struct usb_interface *intf,
  109. const struct usb_device_id *id);
  110. static void usb_amradio_disconnect(struct usb_interface *intf);
  111. static int usb_amradio_open(struct file *file);
  112. static int usb_amradio_close(struct file *file);
  113. static int usb_amradio_suspend(struct usb_interface *intf,
  114. pm_message_t message);
  115. static int usb_amradio_resume(struct usb_interface *intf);
  116. /* Data for one (physical) device */
  117. struct amradio_device {
  118. /* reference to USB and video device */
  119. struct usb_device *usbdev;
  120. struct usb_interface *intf;
  121. struct video_device videodev;
  122. struct v4l2_device v4l2_dev;
  123. unsigned char *buffer;
  124. struct mutex lock; /* buffer locking */
  125. int curfreq;
  126. int stereo;
  127. int muted;
  128. int initialized;
  129. };
  130. static inline struct amradio_device *to_amradio_dev(struct v4l2_device *v4l2_dev)
  131. {
  132. return container_of(v4l2_dev, struct amradio_device, v4l2_dev);
  133. }
  134. /* USB Device ID List */
  135. static struct usb_device_id usb_amradio_device_table[] = {
  136. {USB_DEVICE_AND_INTERFACE_INFO(USB_AMRADIO_VENDOR, USB_AMRADIO_PRODUCT,
  137. USB_CLASS_HID, 0, 0) },
  138. { } /* Terminating entry */
  139. };
  140. MODULE_DEVICE_TABLE(usb, usb_amradio_device_table);
  141. /* USB subsystem interface */
  142. static struct usb_driver usb_amradio_driver = {
  143. .name = MR800_DRIVER_NAME,
  144. .probe = usb_amradio_probe,
  145. .disconnect = usb_amradio_disconnect,
  146. .suspend = usb_amradio_suspend,
  147. .resume = usb_amradio_resume,
  148. .reset_resume = usb_amradio_resume,
  149. .id_table = usb_amradio_device_table,
  150. .supports_autosuspend = 1,
  151. };
  152. /* switch on/off the radio. Send 8 bytes to device */
  153. static int amradio_set_mute(struct amradio_device *radio, char argument)
  154. {
  155. int retval;
  156. int size;
  157. BUG_ON(!mutex_is_locked(&radio->lock));
  158. radio->buffer[0] = 0x00;
  159. radio->buffer[1] = 0x55;
  160. radio->buffer[2] = 0xaa;
  161. radio->buffer[3] = 0x00;
  162. radio->buffer[4] = AMRADIO_SET_MUTE;
  163. radio->buffer[5] = argument;
  164. radio->buffer[6] = 0x00;
  165. radio->buffer[7] = 0x00;
  166. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  167. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  168. if (retval < 0 || size != BUFFER_LENGTH) {
  169. amradio_dev_warn(&radio->videodev.dev, "set mute failed\n");
  170. return retval;
  171. }
  172. radio->muted = argument;
  173. return retval;
  174. }
  175. /* set a frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
  176. static int amradio_setfreq(struct amradio_device *radio, int freq)
  177. {
  178. int retval;
  179. int size;
  180. unsigned short freq_send = 0x10 + (freq >> 3) / 25;
  181. BUG_ON(!mutex_is_locked(&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. goto out_err;
  194. /* frequency is calculated from freq_send and placed in first 2 bytes */
  195. radio->buffer[0] = (freq_send >> 8) & 0xff;
  196. radio->buffer[1] = freq_send & 0xff;
  197. radio->buffer[2] = 0x01;
  198. radio->buffer[3] = 0x00;
  199. radio->buffer[4] = 0x00;
  200. /* 5 and 6 bytes of buffer already = 0x00 */
  201. radio->buffer[7] = 0x00;
  202. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  203. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  204. if (retval < 0 || size != BUFFER_LENGTH)
  205. goto out_err;
  206. radio->curfreq = freq;
  207. goto out;
  208. out_err:
  209. amradio_dev_warn(&radio->videodev.dev, "set frequency failed\n");
  210. out:
  211. return retval;
  212. }
  213. static int amradio_set_stereo(struct amradio_device *radio, char argument)
  214. {
  215. int retval;
  216. int size;
  217. BUG_ON(!mutex_is_locked(&radio->lock));
  218. radio->buffer[0] = 0x00;
  219. radio->buffer[1] = 0x55;
  220. radio->buffer[2] = 0xaa;
  221. radio->buffer[3] = 0x00;
  222. radio->buffer[4] = AMRADIO_SET_MONO;
  223. radio->buffer[5] = argument;
  224. radio->buffer[6] = 0x00;
  225. radio->buffer[7] = 0x00;
  226. retval = usb_bulk_msg(radio->usbdev, usb_sndintpipe(radio->usbdev, 2),
  227. (void *) (radio->buffer), BUFFER_LENGTH, &size, USB_TIMEOUT);
  228. if (retval < 0 || size != BUFFER_LENGTH) {
  229. amradio_dev_warn(&radio->videodev.dev, "set stereo failed\n");
  230. return retval;
  231. }
  232. if (argument == WANT_STEREO)
  233. radio->stereo = 1;
  234. else
  235. radio->stereo = 0;
  236. return retval;
  237. }
  238. /* Handle unplugging the device.
  239. * We call video_unregister_device in any case.
  240. * The last function called in this procedure is
  241. * usb_amradio_device_release.
  242. */
  243. static void usb_amradio_disconnect(struct usb_interface *intf)
  244. {
  245. struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
  246. mutex_lock(&radio->lock);
  247. radio->usbdev = NULL;
  248. mutex_unlock(&radio->lock);
  249. v4l2_device_disconnect(&radio->v4l2_dev);
  250. video_unregister_device(&radio->videodev);
  251. }
  252. /* vidioc_querycap - query device capabilities */
  253. static int vidioc_querycap(struct file *file, void *priv,
  254. struct v4l2_capability *v)
  255. {
  256. struct amradio_device *radio = file->private_data;
  257. strlcpy(v->driver, "radio-mr800", sizeof(v->driver));
  258. strlcpy(v->card, "AverMedia MR 800 USB FM Radio", sizeof(v->card));
  259. usb_make_path(radio->usbdev, v->bus_info, sizeof(v->bus_info));
  260. v->version = RADIO_VERSION;
  261. v->capabilities = V4L2_CAP_TUNER;
  262. return 0;
  263. }
  264. /* vidioc_g_tuner - get tuner attributes */
  265. static int vidioc_g_tuner(struct file *file, void *priv,
  266. struct v4l2_tuner *v)
  267. {
  268. struct amradio_device *radio = file->private_data;
  269. int retval;
  270. if (v->index > 0)
  271. return -EINVAL;
  272. /* TODO: Add function which look is signal stereo or not
  273. * amradio_getstat(radio);
  274. */
  275. /* we call amradio_set_stereo to set radio->stereo
  276. * Honestly, amradio_getstat should cover this in future and
  277. * amradio_set_stereo shouldn't be here
  278. */
  279. retval = amradio_set_stereo(radio, WANT_STEREO);
  280. strcpy(v->name, "FM");
  281. v->type = V4L2_TUNER_RADIO;
  282. v->rangelow = FREQ_MIN * FREQ_MUL;
  283. v->rangehigh = FREQ_MAX * FREQ_MUL;
  284. v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  285. v->capability = V4L2_TUNER_CAP_LOW;
  286. if (radio->stereo)
  287. v->audmode = V4L2_TUNER_MODE_STEREO;
  288. else
  289. v->audmode = V4L2_TUNER_MODE_MONO;
  290. v->signal = 0xffff; /* Can't get the signal strength, sad.. */
  291. v->afc = 0; /* Don't know what is this */
  292. return retval;
  293. }
  294. /* vidioc_s_tuner - set tuner attributes */
  295. static int vidioc_s_tuner(struct file *file, void *priv,
  296. struct v4l2_tuner *v)
  297. {
  298. struct amradio_device *radio = file->private_data;
  299. int retval = -EINVAL;
  300. if (v->index > 0)
  301. return -EINVAL;
  302. /* mono/stereo selector */
  303. switch (v->audmode) {
  304. case V4L2_TUNER_MODE_MONO:
  305. retval = amradio_set_stereo(radio, WANT_MONO);
  306. break;
  307. case V4L2_TUNER_MODE_STEREO:
  308. retval = amradio_set_stereo(radio, WANT_STEREO);
  309. break;
  310. }
  311. return retval;
  312. }
  313. /* vidioc_s_frequency - set tuner radio frequency */
  314. static int vidioc_s_frequency(struct file *file, void *priv,
  315. struct v4l2_frequency *f)
  316. {
  317. struct amradio_device *radio = file->private_data;
  318. if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
  319. return -EINVAL;
  320. return amradio_setfreq(radio, f->frequency);
  321. }
  322. /* vidioc_g_frequency - get tuner radio frequency */
  323. static int vidioc_g_frequency(struct file *file, void *priv,
  324. struct v4l2_frequency *f)
  325. {
  326. struct amradio_device *radio = file->private_data;
  327. if (f->tuner != 0)
  328. return -EINVAL;
  329. f->type = V4L2_TUNER_RADIO;
  330. f->frequency = radio->curfreq;
  331. return 0;
  332. }
  333. /* vidioc_queryctrl - enumerate control items */
  334. static int vidioc_queryctrl(struct file *file, void *priv,
  335. struct v4l2_queryctrl *qc)
  336. {
  337. switch (qc->id) {
  338. case V4L2_CID_AUDIO_MUTE:
  339. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
  340. }
  341. return -EINVAL;
  342. }
  343. /* vidioc_g_ctrl - get the value of a control */
  344. static int vidioc_g_ctrl(struct file *file, void *priv,
  345. struct v4l2_control *ctrl)
  346. {
  347. struct amradio_device *radio = file->private_data;
  348. switch (ctrl->id) {
  349. case V4L2_CID_AUDIO_MUTE:
  350. ctrl->value = radio->muted;
  351. return 0;
  352. }
  353. return -EINVAL;
  354. }
  355. /* vidioc_s_ctrl - set the value of a control */
  356. static int vidioc_s_ctrl(struct file *file, void *priv,
  357. struct v4l2_control *ctrl)
  358. {
  359. struct amradio_device *radio = file->private_data;
  360. int retval = -EINVAL;
  361. switch (ctrl->id) {
  362. case V4L2_CID_AUDIO_MUTE:
  363. if (ctrl->value)
  364. retval = amradio_set_mute(radio, AMRADIO_STOP);
  365. else
  366. retval = amradio_set_mute(radio, AMRADIO_START);
  367. break;
  368. }
  369. return retval;
  370. }
  371. /* vidioc_g_audio - get audio attributes */
  372. static int vidioc_g_audio(struct file *file, void *priv,
  373. struct v4l2_audio *a)
  374. {
  375. if (a->index > 1)
  376. return -EINVAL;
  377. strcpy(a->name, "Radio");
  378. a->capability = V4L2_AUDCAP_STEREO;
  379. return 0;
  380. }
  381. /* vidioc_s_audio - set audio attributes */
  382. static int vidioc_s_audio(struct file *file, void *priv,
  383. struct v4l2_audio *a)
  384. {
  385. if (a->index != 0)
  386. return -EINVAL;
  387. return 0;
  388. }
  389. /* vidioc_g_input - get input */
  390. static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  391. {
  392. *i = 0;
  393. return 0;
  394. }
  395. /* vidioc_s_input - set input */
  396. static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  397. {
  398. if (i != 0)
  399. return -EINVAL;
  400. return 0;
  401. }
  402. static int usb_amradio_init(struct amradio_device *radio)
  403. {
  404. int retval;
  405. retval = amradio_set_mute(radio, AMRADIO_STOP);
  406. if (retval)
  407. goto out_err;
  408. retval = amradio_set_stereo(radio, WANT_STEREO);
  409. if (retval)
  410. goto out_err;
  411. radio->initialized = 1;
  412. goto out;
  413. out_err:
  414. amradio_dev_err(&radio->videodev.dev, "initialization failed\n");
  415. out:
  416. return retval;
  417. }
  418. /* open device - amradio_start() and amradio_setfreq() */
  419. static int usb_amradio_open(struct file *file)
  420. {
  421. struct amradio_device *radio = video_drvdata(file);
  422. int retval = 0;
  423. mutex_lock(&radio->lock);
  424. if (!radio->usbdev) {
  425. retval = -EIO;
  426. goto unlock;
  427. }
  428. file->private_data = radio;
  429. retval = usb_autopm_get_interface(radio->intf);
  430. if (retval)
  431. goto unlock;
  432. if (unlikely(!radio->initialized)) {
  433. retval = usb_amradio_init(radio);
  434. if (retval)
  435. usb_autopm_put_interface(radio->intf);
  436. }
  437. unlock:
  438. mutex_unlock(&radio->lock);
  439. return retval;
  440. }
  441. /*close device */
  442. static int usb_amradio_close(struct file *file)
  443. {
  444. struct amradio_device *radio = file->private_data;
  445. int retval = 0;
  446. mutex_lock(&radio->lock);
  447. if (!radio->usbdev)
  448. retval = -EIO;
  449. else
  450. usb_autopm_put_interface(radio->intf);
  451. mutex_unlock(&radio->lock);
  452. return retval;
  453. }
  454. static long usb_amradio_ioctl(struct file *file, unsigned int cmd,
  455. unsigned long arg)
  456. {
  457. struct amradio_device *radio = file->private_data;
  458. long retval = 0;
  459. mutex_lock(&radio->lock);
  460. if (!radio->usbdev) {
  461. retval = -EIO;
  462. goto unlock;
  463. }
  464. retval = video_ioctl2(file, cmd, arg);
  465. unlock:
  466. mutex_unlock(&radio->lock);
  467. return retval;
  468. }
  469. /* Suspend device - stop device. Need to be checked and fixed */
  470. static int usb_amradio_suspend(struct usb_interface *intf, pm_message_t message)
  471. {
  472. struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
  473. mutex_lock(&radio->lock);
  474. if (!radio->muted && radio->initialized) {
  475. amradio_set_mute(radio, AMRADIO_STOP);
  476. radio->muted = 0;
  477. }
  478. dev_info(&intf->dev, "going into suspend..\n");
  479. mutex_unlock(&radio->lock);
  480. return 0;
  481. }
  482. /* Resume device - start device. Need to be checked and fixed */
  483. static int usb_amradio_resume(struct usb_interface *intf)
  484. {
  485. struct amradio_device *radio = to_amradio_dev(usb_get_intfdata(intf));
  486. mutex_lock(&radio->lock);
  487. if (unlikely(!radio->initialized))
  488. goto unlock;
  489. if (radio->stereo)
  490. amradio_set_stereo(radio, WANT_STEREO);
  491. else
  492. amradio_set_stereo(radio, WANT_MONO);
  493. amradio_setfreq(radio, radio->curfreq);
  494. if (!radio->muted)
  495. amradio_set_mute(radio, AMRADIO_START);
  496. unlock:
  497. dev_info(&intf->dev, "coming out of suspend..\n");
  498. mutex_unlock(&radio->lock);
  499. return 0;
  500. }
  501. /* File system interface */
  502. static const struct v4l2_file_operations usb_amradio_fops = {
  503. .owner = THIS_MODULE,
  504. .open = usb_amradio_open,
  505. .release = usb_amradio_close,
  506. .ioctl = usb_amradio_ioctl,
  507. };
  508. static const struct v4l2_ioctl_ops usb_amradio_ioctl_ops = {
  509. .vidioc_querycap = vidioc_querycap,
  510. .vidioc_g_tuner = vidioc_g_tuner,
  511. .vidioc_s_tuner = vidioc_s_tuner,
  512. .vidioc_g_frequency = vidioc_g_frequency,
  513. .vidioc_s_frequency = vidioc_s_frequency,
  514. .vidioc_queryctrl = vidioc_queryctrl,
  515. .vidioc_g_ctrl = vidioc_g_ctrl,
  516. .vidioc_s_ctrl = vidioc_s_ctrl,
  517. .vidioc_g_audio = vidioc_g_audio,
  518. .vidioc_s_audio = vidioc_s_audio,
  519. .vidioc_g_input = vidioc_g_input,
  520. .vidioc_s_input = vidioc_s_input,
  521. };
  522. static void usb_amradio_video_device_release(struct video_device *videodev)
  523. {
  524. struct amradio_device *radio = video_get_drvdata(videodev);
  525. /* free rest memory */
  526. kfree(radio->buffer);
  527. kfree(radio);
  528. }
  529. /* check if the device is present and register with v4l and usb if it is */
  530. static int usb_amradio_probe(struct usb_interface *intf,
  531. const struct usb_device_id *id)
  532. {
  533. struct amradio_device *radio;
  534. int retval = 0;
  535. radio = kzalloc(sizeof(struct amradio_device), GFP_KERNEL);
  536. if (!radio) {
  537. dev_err(&intf->dev, "kmalloc for amradio_device failed\n");
  538. retval = -ENOMEM;
  539. goto err;
  540. }
  541. radio->buffer = kmalloc(BUFFER_LENGTH, GFP_KERNEL);
  542. if (!radio->buffer) {
  543. dev_err(&intf->dev, "kmalloc for radio->buffer failed\n");
  544. retval = -ENOMEM;
  545. goto err_nobuf;
  546. }
  547. retval = v4l2_device_register(&intf->dev, &radio->v4l2_dev);
  548. if (retval < 0) {
  549. dev_err(&intf->dev, "couldn't register v4l2_device\n");
  550. goto err_v4l2;
  551. }
  552. strlcpy(radio->videodev.name, radio->v4l2_dev.name,
  553. sizeof(radio->videodev.name));
  554. radio->videodev.v4l2_dev = &radio->v4l2_dev;
  555. radio->videodev.fops = &usb_amradio_fops;
  556. radio->videodev.ioctl_ops = &usb_amradio_ioctl_ops;
  557. radio->videodev.release = usb_amradio_video_device_release;
  558. radio->usbdev = interface_to_usbdev(intf);
  559. radio->intf = intf;
  560. radio->curfreq = 95.16 * FREQ_MUL;
  561. mutex_init(&radio->lock);
  562. video_set_drvdata(&radio->videodev, radio);
  563. retval = video_register_device(&radio->videodev, VFL_TYPE_RADIO,
  564. radio_nr);
  565. if (retval < 0) {
  566. dev_err(&intf->dev, "could not register video device\n");
  567. goto err_vdev;
  568. }
  569. return 0;
  570. err_vdev:
  571. v4l2_device_unregister(&radio->v4l2_dev);
  572. err_v4l2:
  573. kfree(radio->buffer);
  574. err_nobuf:
  575. kfree(radio);
  576. err:
  577. return retval;
  578. }
  579. static int __init amradio_init(void)
  580. {
  581. int retval = usb_register(&usb_amradio_driver);
  582. pr_info(KBUILD_MODNAME
  583. ": version " DRIVER_VERSION " " DRIVER_DESC "\n");
  584. if (retval)
  585. pr_err(KBUILD_MODNAME
  586. ": usb_register failed. Error number %d\n", retval);
  587. return retval;
  588. }
  589. static void __exit amradio_exit(void)
  590. {
  591. usb_deregister(&usb_amradio_driver);
  592. }
  593. module_init(amradio_init);
  594. module_exit(amradio_exit);