em28xx-audio.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Empiatech em28x1 audio extension
  4. //
  5. // Copyright (C) 2006 Markus Rechberger <[email protected]>
  6. //
  7. // Copyright (C) 2007-2016 Mauro Carvalho Chehab
  8. // - Port to work with the in-kernel driver
  9. // - Cleanups, fixes, alsa-controls, etc.
  10. //
  11. // This driver is based on my previous au600 usb pstn audio driver
  12. // and inherits all the copyrights
  13. #include "em28xx.h"
  14. #include <linux/kernel.h>
  15. #include <linux/usb.h>
  16. #include <linux/init.h>
  17. #include <linux/sound.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/soundcard.h>
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/info.h>
  26. #include <sound/initval.h>
  27. #include <sound/control.h>
  28. #include <sound/tlv.h>
  29. #include <sound/ac97_codec.h>
  30. #include <media/v4l2-common.h>
  31. static int debug;
  32. module_param(debug, int, 0644);
  33. MODULE_PARM_DESC(debug, "activates debug info");
  34. #define EM28XX_MAX_AUDIO_BUFS 5
  35. #define EM28XX_MIN_AUDIO_PACKETS 64
  36. #define dprintk(fmt, arg...) do { \
  37. if (debug) \
  38. dev_printk(KERN_DEBUG, &dev->intf->dev, \
  39. "video: %s: " fmt, __func__, ## arg); \
  40. } while (0)
  41. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  42. static int em28xx_deinit_isoc_audio(struct em28xx *dev)
  43. {
  44. int i;
  45. dprintk("Stopping isoc\n");
  46. for (i = 0; i < dev->adev.num_urb; i++) {
  47. struct urb *urb = dev->adev.urb[i];
  48. if (!irqs_disabled())
  49. usb_kill_urb(urb);
  50. else
  51. usb_unlink_urb(urb);
  52. }
  53. return 0;
  54. }
  55. static void em28xx_audio_isocirq(struct urb *urb)
  56. {
  57. struct em28xx *dev = urb->context;
  58. int i;
  59. unsigned int oldptr;
  60. int period_elapsed = 0;
  61. int status;
  62. unsigned char *cp;
  63. unsigned int stride;
  64. struct snd_pcm_substream *substream;
  65. struct snd_pcm_runtime *runtime;
  66. if (dev->disconnected) {
  67. dprintk("device disconnected while streaming. URB status=%d.\n",
  68. urb->status);
  69. atomic_set(&dev->adev.stream_started, 0);
  70. return;
  71. }
  72. switch (urb->status) {
  73. case 0: /* success */
  74. case -ETIMEDOUT: /* NAK */
  75. break;
  76. case -ECONNRESET: /* kill */
  77. case -ENOENT:
  78. case -ESHUTDOWN:
  79. return;
  80. default: /* error */
  81. dprintk("urb completion error %d.\n", urb->status);
  82. break;
  83. }
  84. if (atomic_read(&dev->adev.stream_started) == 0)
  85. return;
  86. if (dev->adev.capture_pcm_substream) {
  87. substream = dev->adev.capture_pcm_substream;
  88. runtime = substream->runtime;
  89. stride = runtime->frame_bits >> 3;
  90. for (i = 0; i < urb->number_of_packets; i++) {
  91. unsigned long flags;
  92. int length =
  93. urb->iso_frame_desc[i].actual_length / stride;
  94. cp = (unsigned char *)urb->transfer_buffer +
  95. urb->iso_frame_desc[i].offset;
  96. if (!length)
  97. continue;
  98. oldptr = dev->adev.hwptr_done_capture;
  99. if (oldptr + length >= runtime->buffer_size) {
  100. unsigned int cnt =
  101. runtime->buffer_size - oldptr;
  102. memcpy(runtime->dma_area + oldptr * stride, cp,
  103. cnt * stride);
  104. memcpy(runtime->dma_area, cp + cnt * stride,
  105. length * stride - cnt * stride);
  106. } else {
  107. memcpy(runtime->dma_area + oldptr * stride, cp,
  108. length * stride);
  109. }
  110. snd_pcm_stream_lock_irqsave(substream, flags);
  111. dev->adev.hwptr_done_capture += length;
  112. if (dev->adev.hwptr_done_capture >=
  113. runtime->buffer_size)
  114. dev->adev.hwptr_done_capture -=
  115. runtime->buffer_size;
  116. dev->adev.capture_transfer_done += length;
  117. if (dev->adev.capture_transfer_done >=
  118. runtime->period_size) {
  119. dev->adev.capture_transfer_done -=
  120. runtime->period_size;
  121. period_elapsed = 1;
  122. }
  123. snd_pcm_stream_unlock_irqrestore(substream, flags);
  124. }
  125. if (period_elapsed)
  126. snd_pcm_period_elapsed(substream);
  127. }
  128. urb->status = 0;
  129. status = usb_submit_urb(urb, GFP_ATOMIC);
  130. if (status < 0)
  131. dev_err(&dev->intf->dev,
  132. "resubmit of audio urb failed (error=%i)\n",
  133. status);
  134. }
  135. static int em28xx_init_audio_isoc(struct em28xx *dev)
  136. {
  137. int i, err;
  138. dprintk("Starting isoc transfers\n");
  139. /* Start streaming */
  140. for (i = 0; i < dev->adev.num_urb; i++) {
  141. memset(dev->adev.transfer_buffer[i], 0x80,
  142. dev->adev.urb[i]->transfer_buffer_length);
  143. err = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
  144. if (err) {
  145. dev_err(&dev->intf->dev,
  146. "submit of audio urb failed (error=%i)\n",
  147. err);
  148. em28xx_deinit_isoc_audio(dev);
  149. atomic_set(&dev->adev.stream_started, 0);
  150. return err;
  151. }
  152. }
  153. return 0;
  154. }
  155. static const struct snd_pcm_hardware snd_em28xx_hw_capture = {
  156. .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
  157. SNDRV_PCM_INFO_MMAP |
  158. SNDRV_PCM_INFO_INTERLEAVED |
  159. SNDRV_PCM_INFO_BATCH |
  160. SNDRV_PCM_INFO_MMAP_VALID,
  161. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  162. .rates = SNDRV_PCM_RATE_48000,
  163. .rate_min = 48000,
  164. .rate_max = 48000,
  165. .channels_min = 2,
  166. .channels_max = 2,
  167. .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
  168. /*
  169. * The period is 12.288 bytes. Allow a 10% of variation along its
  170. * value, in order to avoid overruns/underruns due to some clock
  171. * drift.
  172. *
  173. * FIXME: This period assumes 64 packets, and a 48000 PCM rate.
  174. * Calculate it dynamically.
  175. */
  176. .period_bytes_min = 11059,
  177. .period_bytes_max = 13516,
  178. .periods_min = 2,
  179. .periods_max = 98, /* 12544, */
  180. };
  181. static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
  182. {
  183. struct em28xx *dev = snd_pcm_substream_chip(substream);
  184. struct snd_pcm_runtime *runtime = substream->runtime;
  185. int nonblock, ret = 0;
  186. if (!dev) {
  187. pr_err("em28xx-audio: BUG: em28xx can't find device struct. Can't proceed with open\n");
  188. return -ENODEV;
  189. }
  190. if (dev->disconnected)
  191. return -ENODEV;
  192. dprintk("opening device and trying to acquire exclusive lock\n");
  193. nonblock = !!(substream->f_flags & O_NONBLOCK);
  194. if (nonblock) {
  195. if (!mutex_trylock(&dev->lock))
  196. return -EAGAIN;
  197. } else {
  198. mutex_lock(&dev->lock);
  199. }
  200. runtime->hw = snd_em28xx_hw_capture;
  201. if (dev->adev.users == 0) {
  202. if (!dev->alt || dev->is_audio_only) {
  203. struct usb_device *udev;
  204. udev = interface_to_usbdev(dev->intf);
  205. if (dev->is_audio_only)
  206. /* audio is on a separate interface */
  207. dev->alt = 1;
  208. else
  209. /* audio is on the same interface as video */
  210. dev->alt = 7;
  211. /*
  212. * FIXME: The intention seems to be to select
  213. * the alt setting with the largest
  214. * wMaxPacketSize for the video endpoint.
  215. * At least dev->alt should be used instead, but
  216. * we should probably not touch it at all if it
  217. * is already >0, because wMaxPacketSize of the
  218. * audio endpoints seems to be the same for all.
  219. */
  220. dprintk("changing alternate number on interface %d to %d\n",
  221. dev->ifnum, dev->alt);
  222. usb_set_interface(udev, dev->ifnum, dev->alt);
  223. }
  224. /* Sets volume, mute, etc */
  225. dev->mute = 0;
  226. ret = em28xx_audio_analog_set(dev);
  227. if (ret < 0)
  228. goto err;
  229. }
  230. kref_get(&dev->ref);
  231. dev->adev.users++;
  232. mutex_unlock(&dev->lock);
  233. /* Dynamically adjust the period size */
  234. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  235. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  236. dev->adev.period * 95 / 100,
  237. dev->adev.period * 105 / 100);
  238. dev->adev.capture_pcm_substream = substream;
  239. return 0;
  240. err:
  241. mutex_unlock(&dev->lock);
  242. dev_err(&dev->intf->dev,
  243. "Error while configuring em28xx mixer\n");
  244. return ret;
  245. }
  246. static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
  247. {
  248. struct em28xx *dev = snd_pcm_substream_chip(substream);
  249. dprintk("closing device\n");
  250. dev->mute = 1;
  251. mutex_lock(&dev->lock);
  252. dev->adev.users--;
  253. if (atomic_read(&dev->adev.stream_started) > 0) {
  254. atomic_set(&dev->adev.stream_started, 0);
  255. schedule_work(&dev->adev.wq_trigger);
  256. }
  257. em28xx_audio_analog_set(dev);
  258. mutex_unlock(&dev->lock);
  259. kref_put(&dev->ref, em28xx_free_device);
  260. return 0;
  261. }
  262. static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
  263. {
  264. struct em28xx *dev = snd_pcm_substream_chip(substream);
  265. if (dev->disconnected)
  266. return -ENODEV;
  267. dev->adev.hwptr_done_capture = 0;
  268. dev->adev.capture_transfer_done = 0;
  269. return 0;
  270. }
  271. static void audio_trigger(struct work_struct *work)
  272. {
  273. struct em28xx_audio *adev =
  274. container_of(work, struct em28xx_audio, wq_trigger);
  275. struct em28xx *dev = container_of(adev, struct em28xx, adev);
  276. if (atomic_read(&adev->stream_started)) {
  277. dprintk("starting capture");
  278. em28xx_init_audio_isoc(dev);
  279. } else {
  280. dprintk("stopping capture");
  281. em28xx_deinit_isoc_audio(dev);
  282. }
  283. }
  284. static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
  285. int cmd)
  286. {
  287. struct em28xx *dev = snd_pcm_substream_chip(substream);
  288. int retval = 0;
  289. if (dev->disconnected)
  290. return -ENODEV;
  291. switch (cmd) {
  292. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  293. case SNDRV_PCM_TRIGGER_RESUME:
  294. case SNDRV_PCM_TRIGGER_START:
  295. atomic_set(&dev->adev.stream_started, 1);
  296. break;
  297. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  298. case SNDRV_PCM_TRIGGER_SUSPEND:
  299. case SNDRV_PCM_TRIGGER_STOP:
  300. atomic_set(&dev->adev.stream_started, 0);
  301. break;
  302. default:
  303. retval = -EINVAL;
  304. }
  305. schedule_work(&dev->adev.wq_trigger);
  306. return retval;
  307. }
  308. static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
  309. *substream)
  310. {
  311. unsigned long flags;
  312. struct em28xx *dev;
  313. snd_pcm_uframes_t hwptr_done;
  314. dev = snd_pcm_substream_chip(substream);
  315. if (dev->disconnected)
  316. return SNDRV_PCM_POS_XRUN;
  317. spin_lock_irqsave(&dev->adev.slock, flags);
  318. hwptr_done = dev->adev.hwptr_done_capture;
  319. spin_unlock_irqrestore(&dev->adev.slock, flags);
  320. return hwptr_done;
  321. }
  322. /*
  323. * AC97 volume control support
  324. */
  325. static int em28xx_vol_info(struct snd_kcontrol *kcontrol,
  326. struct snd_ctl_elem_info *info)
  327. {
  328. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  329. if (dev->disconnected)
  330. return -ENODEV;
  331. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  332. info->count = 2;
  333. info->value.integer.min = 0;
  334. info->value.integer.max = 0x1f;
  335. return 0;
  336. }
  337. static int em28xx_vol_put(struct snd_kcontrol *kcontrol,
  338. struct snd_ctl_elem_value *value)
  339. {
  340. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  341. struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
  342. u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) |
  343. (0x1f - (value->value.integer.value[1] & 0x1f)) << 8;
  344. int nonblock = 0;
  345. int rc;
  346. if (dev->disconnected)
  347. return -ENODEV;
  348. if (substream)
  349. nonblock = !!(substream->f_flags & O_NONBLOCK);
  350. if (nonblock) {
  351. if (!mutex_trylock(&dev->lock))
  352. return -EAGAIN;
  353. } else {
  354. mutex_lock(&dev->lock);
  355. }
  356. rc = em28xx_read_ac97(dev, kcontrol->private_value);
  357. if (rc < 0)
  358. goto err;
  359. val |= rc & 0x8000; /* Preserve the mute flag */
  360. rc = em28xx_write_ac97(dev, kcontrol->private_value, val);
  361. if (rc < 0)
  362. goto err;
  363. dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
  364. (val & 0x8000) ? "muted " : "",
  365. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  366. val, (int)kcontrol->private_value);
  367. err:
  368. mutex_unlock(&dev->lock);
  369. return rc;
  370. }
  371. static int em28xx_vol_get(struct snd_kcontrol *kcontrol,
  372. struct snd_ctl_elem_value *value)
  373. {
  374. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  375. struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
  376. int nonblock = 0;
  377. int val;
  378. if (dev->disconnected)
  379. return -ENODEV;
  380. if (substream)
  381. nonblock = !!(substream->f_flags & O_NONBLOCK);
  382. if (nonblock) {
  383. if (!mutex_trylock(&dev->lock))
  384. return -EAGAIN;
  385. } else {
  386. mutex_lock(&dev->lock);
  387. }
  388. val = em28xx_read_ac97(dev, kcontrol->private_value);
  389. mutex_unlock(&dev->lock);
  390. if (val < 0)
  391. return val;
  392. dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
  393. (val & 0x8000) ? "muted " : "",
  394. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  395. val, (int)kcontrol->private_value);
  396. value->value.integer.value[0] = 0x1f - (val & 0x1f);
  397. value->value.integer.value[1] = 0x1f - ((val >> 8) & 0x1f);
  398. return 0;
  399. }
  400. static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol,
  401. struct snd_ctl_elem_value *value)
  402. {
  403. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  404. u16 val = value->value.integer.value[0];
  405. struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
  406. int nonblock = 0;
  407. int rc;
  408. if (dev->disconnected)
  409. return -ENODEV;
  410. if (substream)
  411. nonblock = !!(substream->f_flags & O_NONBLOCK);
  412. if (nonblock) {
  413. if (!mutex_trylock(&dev->lock))
  414. return -EAGAIN;
  415. } else {
  416. mutex_lock(&dev->lock);
  417. }
  418. rc = em28xx_read_ac97(dev, kcontrol->private_value);
  419. if (rc < 0)
  420. goto err;
  421. if (val)
  422. rc &= 0x1f1f;
  423. else
  424. rc |= 0x8000;
  425. rc = em28xx_write_ac97(dev, kcontrol->private_value, rc);
  426. if (rc < 0)
  427. goto err;
  428. dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
  429. (val & 0x8000) ? "muted " : "",
  430. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  431. val, (int)kcontrol->private_value);
  432. err:
  433. mutex_unlock(&dev->lock);
  434. return rc;
  435. }
  436. static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol,
  437. struct snd_ctl_elem_value *value)
  438. {
  439. struct em28xx *dev = snd_kcontrol_chip(kcontrol);
  440. struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
  441. int nonblock = 0;
  442. int val;
  443. if (dev->disconnected)
  444. return -ENODEV;
  445. if (substream)
  446. nonblock = !!(substream->f_flags & O_NONBLOCK);
  447. if (nonblock) {
  448. if (!mutex_trylock(&dev->lock))
  449. return -EAGAIN;
  450. } else {
  451. mutex_lock(&dev->lock);
  452. }
  453. val = em28xx_read_ac97(dev, kcontrol->private_value);
  454. mutex_unlock(&dev->lock);
  455. if (val < 0)
  456. return val;
  457. if (val & 0x8000)
  458. value->value.integer.value[0] = 0;
  459. else
  460. value->value.integer.value[0] = 1;
  461. dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
  462. (val & 0x8000) ? "muted " : "",
  463. 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
  464. val, (int)kcontrol->private_value);
  465. return 0;
  466. }
  467. static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0);
  468. static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev,
  469. char *name, int id)
  470. {
  471. int err;
  472. char ctl_name[44];
  473. struct snd_kcontrol *kctl;
  474. struct snd_kcontrol_new tmp;
  475. memset(&tmp, 0, sizeof(tmp));
  476. tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  477. tmp.private_value = id;
  478. tmp.name = ctl_name;
  479. /* Add Mute Control */
  480. sprintf(ctl_name, "%s Switch", name);
  481. tmp.get = em28xx_vol_get_mute;
  482. tmp.put = em28xx_vol_put_mute;
  483. tmp.info = snd_ctl_boolean_mono_info;
  484. kctl = snd_ctl_new1(&tmp, dev);
  485. err = snd_ctl_add(card, kctl);
  486. if (err < 0)
  487. return err;
  488. dprintk("Added control %s for ac97 volume control 0x%04x\n",
  489. ctl_name, id);
  490. memset(&tmp, 0, sizeof(tmp));
  491. tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  492. tmp.private_value = id;
  493. tmp.name = ctl_name;
  494. /* Add Volume Control */
  495. sprintf(ctl_name, "%s Volume", name);
  496. tmp.get = em28xx_vol_get;
  497. tmp.put = em28xx_vol_put;
  498. tmp.info = em28xx_vol_info;
  499. tmp.tlv.p = em28xx_db_scale;
  500. kctl = snd_ctl_new1(&tmp, dev);
  501. err = snd_ctl_add(card, kctl);
  502. if (err < 0)
  503. return err;
  504. dprintk("Added control %s for ac97 volume control 0x%04x\n",
  505. ctl_name, id);
  506. return 0;
  507. }
  508. /*
  509. * register/unregister code and data
  510. */
  511. static const struct snd_pcm_ops snd_em28xx_pcm_capture = {
  512. .open = snd_em28xx_capture_open,
  513. .close = snd_em28xx_pcm_close,
  514. .prepare = snd_em28xx_prepare,
  515. .trigger = snd_em28xx_capture_trigger,
  516. .pointer = snd_em28xx_capture_pointer,
  517. };
  518. static void em28xx_audio_free_urb(struct em28xx *dev)
  519. {
  520. struct usb_device *udev = interface_to_usbdev(dev->intf);
  521. int i;
  522. for (i = 0; i < dev->adev.num_urb; i++) {
  523. struct urb *urb = dev->adev.urb[i];
  524. if (!urb)
  525. continue;
  526. usb_free_coherent(udev, urb->transfer_buffer_length,
  527. dev->adev.transfer_buffer[i],
  528. urb->transfer_dma);
  529. usb_free_urb(urb);
  530. }
  531. kfree(dev->adev.urb);
  532. kfree(dev->adev.transfer_buffer);
  533. dev->adev.num_urb = 0;
  534. }
  535. /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
  536. static int em28xx_audio_ep_packet_size(struct usb_device *udev,
  537. struct usb_endpoint_descriptor *e)
  538. {
  539. int size = le16_to_cpu(e->wMaxPacketSize);
  540. if (udev->speed == USB_SPEED_HIGH)
  541. return (size & 0x7ff) * (1 + (((size) >> 11) & 0x03));
  542. return size & 0x7ff;
  543. }
  544. static int em28xx_audio_urb_init(struct em28xx *dev)
  545. {
  546. struct usb_interface *intf;
  547. struct usb_endpoint_descriptor *e, *ep = NULL;
  548. struct usb_device *udev = interface_to_usbdev(dev->intf);
  549. int i, ep_size, interval, num_urb, npackets;
  550. int urb_size, bytes_per_transfer;
  551. u8 alt;
  552. if (dev->ifnum)
  553. alt = 1;
  554. else
  555. alt = 7;
  556. intf = usb_ifnum_to_if(udev, dev->ifnum);
  557. if (intf->num_altsetting <= alt) {
  558. dev_err(&dev->intf->dev, "alt %d doesn't exist on interface %d\n",
  559. dev->ifnum, alt);
  560. return -ENODEV;
  561. }
  562. for (i = 0; i < intf->altsetting[alt].desc.bNumEndpoints; i++) {
  563. e = &intf->altsetting[alt].endpoint[i].desc;
  564. if (!usb_endpoint_dir_in(e))
  565. continue;
  566. if (e->bEndpointAddress == EM28XX_EP_AUDIO) {
  567. ep = e;
  568. break;
  569. }
  570. }
  571. if (!ep) {
  572. dev_err(&dev->intf->dev, "Couldn't find an audio endpoint");
  573. return -ENODEV;
  574. }
  575. ep_size = em28xx_audio_ep_packet_size(udev, ep);
  576. interval = 1 << (ep->bInterval - 1);
  577. dev_info(&dev->intf->dev,
  578. "Endpoint 0x%02x %s on intf %d alt %d interval = %d, size %d\n",
  579. EM28XX_EP_AUDIO, usb_speed_string(udev->speed),
  580. dev->ifnum, alt, interval, ep_size);
  581. /* Calculate the number and size of URBs to better fit the audio samples */
  582. /*
  583. * Estimate the number of bytes per DMA transfer.
  584. *
  585. * This is given by the bit rate (for now, only 48000 Hz) multiplied
  586. * by 2 channels and 2 bytes/sample divided by the number of microframe
  587. * intervals and by the microframe rate (125 us)
  588. */
  589. bytes_per_transfer = DIV_ROUND_UP(48000 * 2 * 2, 125 * interval);
  590. /*
  591. * Estimate the number of transfer URBs. Don't let it go past the
  592. * maximum number of URBs that is known to be supported by the device.
  593. */
  594. num_urb = DIV_ROUND_UP(bytes_per_transfer, ep_size);
  595. if (num_urb > EM28XX_MAX_AUDIO_BUFS)
  596. num_urb = EM28XX_MAX_AUDIO_BUFS;
  597. /*
  598. * Now that we know the number of bytes per transfer and the number of
  599. * URBs, estimate the typical size of an URB, in order to adjust the
  600. * minimal number of packets.
  601. */
  602. urb_size = bytes_per_transfer / num_urb;
  603. /*
  604. * Now, calculate the amount of audio packets to be filled on each
  605. * URB. In order to preserve the old behaviour, use a minimal
  606. * threshold for this value.
  607. */
  608. npackets = EM28XX_MIN_AUDIO_PACKETS;
  609. if (urb_size > ep_size * npackets)
  610. npackets = DIV_ROUND_UP(urb_size, ep_size);
  611. dev_info(&dev->intf->dev,
  612. "Number of URBs: %d, with %d packets and %d size\n",
  613. num_urb, npackets, urb_size);
  614. /* Estimate the bytes per period */
  615. dev->adev.period = urb_size * npackets;
  616. /* Allocate space to store the number of URBs to be used */
  617. dev->adev.transfer_buffer = kcalloc(num_urb,
  618. sizeof(*dev->adev.transfer_buffer),
  619. GFP_KERNEL);
  620. if (!dev->adev.transfer_buffer)
  621. return -ENOMEM;
  622. dev->adev.urb = kcalloc(num_urb, sizeof(*dev->adev.urb), GFP_KERNEL);
  623. if (!dev->adev.urb) {
  624. kfree(dev->adev.transfer_buffer);
  625. return -ENOMEM;
  626. }
  627. /* Alloc memory for each URB and for each transfer buffer */
  628. dev->adev.num_urb = num_urb;
  629. for (i = 0; i < num_urb; i++) {
  630. struct urb *urb;
  631. int j, k;
  632. void *buf;
  633. urb = usb_alloc_urb(npackets, GFP_KERNEL);
  634. if (!urb) {
  635. em28xx_audio_free_urb(dev);
  636. return -ENOMEM;
  637. }
  638. dev->adev.urb[i] = urb;
  639. buf = usb_alloc_coherent(udev, npackets * ep_size, GFP_KERNEL,
  640. &urb->transfer_dma);
  641. if (!buf) {
  642. dev_err(&dev->intf->dev,
  643. "usb_alloc_coherent failed!\n");
  644. em28xx_audio_free_urb(dev);
  645. return -ENOMEM;
  646. }
  647. dev->adev.transfer_buffer[i] = buf;
  648. urb->dev = udev;
  649. urb->context = dev;
  650. urb->pipe = usb_rcvisocpipe(udev, EM28XX_EP_AUDIO);
  651. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  652. urb->transfer_buffer = buf;
  653. urb->interval = interval;
  654. urb->complete = em28xx_audio_isocirq;
  655. urb->number_of_packets = npackets;
  656. urb->transfer_buffer_length = ep_size * npackets;
  657. for (j = k = 0; j < npackets; j++, k += ep_size) {
  658. urb->iso_frame_desc[j].offset = k;
  659. urb->iso_frame_desc[j].length = ep_size;
  660. }
  661. }
  662. return 0;
  663. }
  664. static int em28xx_audio_init(struct em28xx *dev)
  665. {
  666. struct em28xx_audio *adev = &dev->adev;
  667. struct usb_device *udev = interface_to_usbdev(dev->intf);
  668. struct snd_pcm *pcm;
  669. struct snd_card *card;
  670. static int devnr;
  671. int err;
  672. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) {
  673. /*
  674. * This device does not support the extension (in this case
  675. * the device is expecting the snd-usb-audio module or
  676. * doesn't have analog audio support at all)
  677. */
  678. return 0;
  679. }
  680. dev_info(&dev->intf->dev, "Binding audio extension\n");
  681. kref_get(&dev->ref);
  682. dev_info(&dev->intf->dev,
  683. "em28xx-audio.c: Copyright (C) 2006 Markus Rechberger\n");
  684. dev_info(&dev->intf->dev,
  685. "em28xx-audio.c: Copyright (C) 2007-2016 Mauro Carvalho Chehab\n");
  686. err = snd_card_new(&dev->intf->dev, index[devnr], "Em28xx Audio",
  687. THIS_MODULE, 0, &card);
  688. if (err < 0)
  689. return err;
  690. spin_lock_init(&adev->slock);
  691. adev->sndcard = card;
  692. adev->udev = udev;
  693. err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
  694. if (err < 0)
  695. goto card_free;
  696. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
  697. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
  698. pcm->info_flags = 0;
  699. pcm->private_data = dev;
  700. strscpy(pcm->name, "Empia 28xx Capture", sizeof(pcm->name));
  701. strscpy(card->driver, "Em28xx-Audio", sizeof(card->driver));
  702. strscpy(card->shortname, "Em28xx Audio", sizeof(card->shortname));
  703. strscpy(card->longname, "Empia Em28xx Audio", sizeof(card->longname));
  704. INIT_WORK(&adev->wq_trigger, audio_trigger);
  705. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  706. em28xx_cvol_new(card, dev, "Video", AC97_VIDEO);
  707. em28xx_cvol_new(card, dev, "Line In", AC97_LINE);
  708. em28xx_cvol_new(card, dev, "Phone", AC97_PHONE);
  709. em28xx_cvol_new(card, dev, "Microphone", AC97_MIC);
  710. em28xx_cvol_new(card, dev, "CD", AC97_CD);
  711. em28xx_cvol_new(card, dev, "AUX", AC97_AUX);
  712. em28xx_cvol_new(card, dev, "PCM", AC97_PCM);
  713. em28xx_cvol_new(card, dev, "Master", AC97_MASTER);
  714. em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE);
  715. em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO);
  716. em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER);
  717. em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER);
  718. }
  719. err = em28xx_audio_urb_init(dev);
  720. if (err)
  721. goto card_free;
  722. err = snd_card_register(card);
  723. if (err < 0)
  724. goto urb_free;
  725. dev_info(&dev->intf->dev, "Audio extension successfully initialized\n");
  726. return 0;
  727. urb_free:
  728. em28xx_audio_free_urb(dev);
  729. card_free:
  730. snd_card_free(card);
  731. adev->sndcard = NULL;
  732. return err;
  733. }
  734. static int em28xx_audio_fini(struct em28xx *dev)
  735. {
  736. if (!dev)
  737. return 0;
  738. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) {
  739. /*
  740. * This device does not support the extension (in this case
  741. * the device is expecting the snd-usb-audio module or
  742. * doesn't have analog audio support at all)
  743. */
  744. return 0;
  745. }
  746. dev_info(&dev->intf->dev, "Closing audio extension\n");
  747. if (dev->adev.sndcard) {
  748. snd_card_disconnect(dev->adev.sndcard);
  749. flush_work(&dev->adev.wq_trigger);
  750. em28xx_audio_free_urb(dev);
  751. snd_card_free(dev->adev.sndcard);
  752. dev->adev.sndcard = NULL;
  753. }
  754. kref_put(&dev->ref, em28xx_free_device);
  755. return 0;
  756. }
  757. static int em28xx_audio_suspend(struct em28xx *dev)
  758. {
  759. if (!dev)
  760. return 0;
  761. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR)
  762. return 0;
  763. dev_info(&dev->intf->dev, "Suspending audio extension\n");
  764. em28xx_deinit_isoc_audio(dev);
  765. atomic_set(&dev->adev.stream_started, 0);
  766. return 0;
  767. }
  768. static int em28xx_audio_resume(struct em28xx *dev)
  769. {
  770. if (!dev)
  771. return 0;
  772. if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR)
  773. return 0;
  774. dev_info(&dev->intf->dev, "Resuming audio extension\n");
  775. /* Nothing to do other than schedule_work() ?? */
  776. schedule_work(&dev->adev.wq_trigger);
  777. return 0;
  778. }
  779. static struct em28xx_ops audio_ops = {
  780. .id = EM28XX_AUDIO,
  781. .name = "Em28xx Audio Extension",
  782. .init = em28xx_audio_init,
  783. .fini = em28xx_audio_fini,
  784. .suspend = em28xx_audio_suspend,
  785. .resume = em28xx_audio_resume,
  786. };
  787. static int __init em28xx_alsa_register(void)
  788. {
  789. return em28xx_register_extension(&audio_ops);
  790. }
  791. static void __exit em28xx_alsa_unregister(void)
  792. {
  793. em28xx_unregister_extension(&audio_ops);
  794. }
  795. MODULE_LICENSE("GPL v2");
  796. MODULE_AUTHOR("Markus Rechberger <[email protected]>");
  797. MODULE_AUTHOR("Mauro Carvalho Chehab");
  798. MODULE_DESCRIPTION(DRIVER_DESC " - audio interface");
  799. MODULE_VERSION(EM28XX_VERSION);
  800. module_init(em28xx_alsa_register);
  801. module_exit(em28xx_alsa_unregister);