pcm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/audio.h>
  20. #include <linux/usb/audio-v2.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include "usbaudio.h"
  25. #include "card.h"
  26. #include "quirks.h"
  27. #include "debug.h"
  28. #include "endpoint.h"
  29. #include "helper.h"
  30. #include "pcm.h"
  31. #include "clock.h"
  32. #include "power.h"
  33. /* return the estimated delay based on USB frame counters */
  34. snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
  35. unsigned int rate)
  36. {
  37. int current_frame_number;
  38. int frame_diff;
  39. int est_delay;
  40. current_frame_number = usb_get_current_frame_number(subs->dev);
  41. /*
  42. * HCD implementations use different widths, use lower 8 bits.
  43. * The delay will be managed up to 256ms, which is more than
  44. * enough
  45. */
  46. frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
  47. /* Approximation based on number of samples per USB frame (ms),
  48. some truncation for 44.1 but the estimate is good enough */
  49. est_delay = subs->last_delay - (frame_diff * rate / 1000);
  50. if (est_delay < 0)
  51. est_delay = 0;
  52. return est_delay;
  53. }
  54. /*
  55. * return the current pcm pointer. just based on the hwptr_done value.
  56. */
  57. static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
  58. {
  59. struct snd_usb_substream *subs;
  60. unsigned int hwptr_done;
  61. subs = (struct snd_usb_substream *)substream->runtime->private_data;
  62. if (subs->stream->chip->shutdown)
  63. return SNDRV_PCM_POS_XRUN;
  64. spin_lock(&subs->lock);
  65. hwptr_done = subs->hwptr_done;
  66. substream->runtime->delay = snd_usb_pcm_delay(subs,
  67. substream->runtime->rate);
  68. spin_unlock(&subs->lock);
  69. return hwptr_done / (substream->runtime->frame_bits >> 3);
  70. }
  71. /*
  72. * find a matching audio format
  73. */
  74. static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
  75. unsigned int rate, unsigned int channels)
  76. {
  77. struct list_head *p;
  78. struct audioformat *found = NULL;
  79. int cur_attr = 0, attr;
  80. list_for_each(p, &subs->fmt_list) {
  81. struct audioformat *fp;
  82. fp = list_entry(p, struct audioformat, list);
  83. if (!(fp->formats & (1uLL << format)))
  84. continue;
  85. if (fp->channels != channels)
  86. continue;
  87. if (rate < fp->rate_min || rate > fp->rate_max)
  88. continue;
  89. if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
  90. unsigned int i;
  91. for (i = 0; i < fp->nr_rates; i++)
  92. if (fp->rate_table[i] == rate)
  93. break;
  94. if (i >= fp->nr_rates)
  95. continue;
  96. }
  97. attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
  98. if (! found) {
  99. found = fp;
  100. cur_attr = attr;
  101. continue;
  102. }
  103. /* avoid async out and adaptive in if the other method
  104. * supports the same format.
  105. * this is a workaround for the case like
  106. * M-audio audiophile USB.
  107. */
  108. if (attr != cur_attr) {
  109. if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
  110. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  111. (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  112. subs->direction == SNDRV_PCM_STREAM_CAPTURE))
  113. continue;
  114. if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
  115. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  116. (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  117. subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
  118. found = fp;
  119. cur_attr = attr;
  120. continue;
  121. }
  122. }
  123. /* find the format with the largest max. packet size */
  124. if (fp->maxpacksize > found->maxpacksize) {
  125. found = fp;
  126. cur_attr = attr;
  127. }
  128. }
  129. return found;
  130. }
  131. static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
  132. struct usb_host_interface *alts,
  133. struct audioformat *fmt)
  134. {
  135. struct usb_device *dev = chip->dev;
  136. unsigned int ep;
  137. unsigned char data[1];
  138. int err;
  139. ep = get_endpoint(alts, 0)->bEndpointAddress;
  140. data[0] = 1;
  141. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  142. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  143. UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
  144. data, sizeof(data))) < 0) {
  145. snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
  146. dev->devnum, iface, ep);
  147. return err;
  148. }
  149. return 0;
  150. }
  151. static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
  152. struct usb_host_interface *alts,
  153. struct audioformat *fmt)
  154. {
  155. struct usb_device *dev = chip->dev;
  156. unsigned char data[1];
  157. unsigned int ep;
  158. int err;
  159. ep = get_endpoint(alts, 0)->bEndpointAddress;
  160. data[0] = 1;
  161. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
  162. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  163. UAC2_EP_CS_PITCH << 8, 0,
  164. data, sizeof(data))) < 0) {
  165. snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
  166. dev->devnum, iface, fmt->altsetting);
  167. return err;
  168. }
  169. return 0;
  170. }
  171. /*
  172. * initialize the pitch control and sample rate
  173. */
  174. int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
  175. struct usb_host_interface *alts,
  176. struct audioformat *fmt)
  177. {
  178. struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  179. /* if endpoint doesn't have pitch control, bail out */
  180. if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
  181. return 0;
  182. switch (altsd->bInterfaceProtocol) {
  183. case UAC_VERSION_1:
  184. default:
  185. return init_pitch_v1(chip, iface, alts, fmt);
  186. case UAC_VERSION_2:
  187. return init_pitch_v2(chip, iface, alts, fmt);
  188. }
  189. }
  190. /*
  191. * find a matching format and set up the interface
  192. */
  193. static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
  194. {
  195. struct usb_device *dev = subs->dev;
  196. struct usb_host_interface *alts;
  197. struct usb_interface_descriptor *altsd;
  198. struct usb_interface *iface;
  199. unsigned int ep, attr;
  200. int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  201. int err;
  202. iface = usb_ifnum_to_if(dev, fmt->iface);
  203. if (WARN_ON(!iface))
  204. return -EINVAL;
  205. alts = &iface->altsetting[fmt->altset_idx];
  206. altsd = get_iface_desc(alts);
  207. if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
  208. return -EINVAL;
  209. if (fmt == subs->cur_audiofmt)
  210. return 0;
  211. /* close the old interface */
  212. if (subs->interface >= 0 && subs->interface != fmt->iface) {
  213. if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
  214. snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
  215. dev->devnum, fmt->iface, fmt->altsetting);
  216. return -EIO;
  217. }
  218. subs->interface = -1;
  219. subs->altset_idx = 0;
  220. }
  221. /* set interface */
  222. if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) {
  223. if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
  224. snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
  225. dev->devnum, fmt->iface, fmt->altsetting);
  226. return -EIO;
  227. }
  228. snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
  229. subs->interface = fmt->iface;
  230. subs->altset_idx = fmt->altset_idx;
  231. }
  232. /* create a data pipe */
  233. ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
  234. if (is_playback)
  235. subs->datapipe = usb_sndisocpipe(dev, ep);
  236. else
  237. subs->datapipe = usb_rcvisocpipe(dev, ep);
  238. subs->datainterval = fmt->datainterval;
  239. subs->syncpipe = subs->syncinterval = 0;
  240. subs->maxpacksize = fmt->maxpacksize;
  241. subs->syncmaxsize = 0;
  242. subs->fill_max = 0;
  243. /* we need a sync pipe in async OUT or adaptive IN mode */
  244. /* check the number of EP, since some devices have broken
  245. * descriptors which fool us. if it has only one EP,
  246. * assume it as adaptive-out or sync-in.
  247. */
  248. attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
  249. if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
  250. (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
  251. altsd->bNumEndpoints >= 2) {
  252. /* check sync-pipe endpoint */
  253. /* ... and check descriptor size before accessing bSynchAddress
  254. because there is a version of the SB Audigy 2 NX firmware lacking
  255. the audio fields in the endpoint descriptors */
  256. if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
  257. (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  258. get_endpoint(alts, 1)->bSynchAddress != 0)) {
  259. snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
  260. dev->devnum, fmt->iface, fmt->altsetting);
  261. return -EINVAL;
  262. }
  263. ep = get_endpoint(alts, 1)->bEndpointAddress;
  264. if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  265. (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
  266. (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
  267. snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
  268. dev->devnum, fmt->iface, fmt->altsetting);
  269. return -EINVAL;
  270. }
  271. ep &= USB_ENDPOINT_NUMBER_MASK;
  272. if (is_playback)
  273. subs->syncpipe = usb_rcvisocpipe(dev, ep);
  274. else
  275. subs->syncpipe = usb_sndisocpipe(dev, ep);
  276. if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  277. get_endpoint(alts, 1)->bRefresh >= 1 &&
  278. get_endpoint(alts, 1)->bRefresh <= 9)
  279. subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
  280. else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  281. subs->syncinterval = 1;
  282. else if (get_endpoint(alts, 1)->bInterval >= 1 &&
  283. get_endpoint(alts, 1)->bInterval <= 16)
  284. subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
  285. else
  286. subs->syncinterval = 3;
  287. subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
  288. }
  289. /* always fill max packet size */
  290. if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
  291. subs->fill_max = 1;
  292. if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
  293. return err;
  294. subs->cur_audiofmt = fmt;
  295. snd_usb_set_format_quirk(subs, fmt);
  296. #if 0
  297. printk(KERN_DEBUG
  298. "setting done: format = %d, rate = %d..%d, channels = %d\n",
  299. fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
  300. printk(KERN_DEBUG
  301. " datapipe = 0x%0x, syncpipe = 0x%0x\n",
  302. subs->datapipe, subs->syncpipe);
  303. #endif
  304. return 0;
  305. }
  306. /*
  307. * hw_params callback
  308. *
  309. * allocate a buffer and set the given audio format.
  310. *
  311. * so far we use a physically linear buffer although packetize transfer
  312. * doesn't need a continuous area.
  313. * if sg buffer is supported on the later version of alsa, we'll follow
  314. * that.
  315. */
  316. static int snd_usb_hw_params(struct snd_pcm_substream *substream,
  317. struct snd_pcm_hw_params *hw_params)
  318. {
  319. struct snd_usb_substream *subs = substream->runtime->private_data;
  320. struct audioformat *fmt;
  321. unsigned int channels, rate, format;
  322. int ret, changed;
  323. ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  324. params_buffer_bytes(hw_params));
  325. if (ret < 0)
  326. return ret;
  327. format = params_format(hw_params);
  328. rate = params_rate(hw_params);
  329. channels = params_channels(hw_params);
  330. fmt = find_format(subs, format, rate, channels);
  331. if (!fmt) {
  332. snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
  333. format, rate, channels);
  334. return -EINVAL;
  335. }
  336. changed = subs->cur_audiofmt != fmt ||
  337. subs->period_bytes != params_period_bytes(hw_params) ||
  338. subs->cur_rate != rate;
  339. down_read(&subs->stream->chip->shutdown_rwsem);
  340. if (subs->stream->chip->shutdown) {
  341. ret = -ENODEV;
  342. goto unlock;
  343. }
  344. if ((ret = set_format(subs, fmt)) < 0)
  345. goto unlock;
  346. if (subs->cur_rate != rate) {
  347. struct usb_host_interface *alts;
  348. struct usb_interface *iface;
  349. iface = usb_ifnum_to_if(subs->dev, fmt->iface);
  350. alts = &iface->altsetting[fmt->altset_idx];
  351. ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
  352. if (ret < 0)
  353. goto unlock;
  354. subs->cur_rate = rate;
  355. }
  356. if (changed) {
  357. /* format changed */
  358. snd_usb_release_substream_urbs(subs, 0);
  359. /* influenced: period_bytes, channels, rate, format, */
  360. ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
  361. params_rate(hw_params),
  362. snd_pcm_format_physical_width(params_format(hw_params)) *
  363. params_channels(hw_params));
  364. }
  365. unlock:
  366. up_read(&subs->stream->chip->shutdown_rwsem);
  367. return ret;
  368. }
  369. /*
  370. * hw_free callback
  371. *
  372. * reset the audio format and release the buffer
  373. */
  374. static int snd_usb_hw_free(struct snd_pcm_substream *substream)
  375. {
  376. struct snd_usb_substream *subs = substream->runtime->private_data;
  377. subs->cur_audiofmt = NULL;
  378. subs->cur_rate = 0;
  379. subs->period_bytes = 0;
  380. down_read(&subs->stream->chip->shutdown_rwsem);
  381. snd_usb_release_substream_urbs(subs, 0);
  382. up_read(&subs->stream->chip->shutdown_rwsem);
  383. return snd_pcm_lib_free_vmalloc_buffer(substream);
  384. }
  385. /*
  386. * prepare callback
  387. *
  388. * only a few subtle things...
  389. */
  390. static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
  391. {
  392. struct snd_pcm_runtime *runtime = substream->runtime;
  393. struct snd_usb_substream *subs = runtime->private_data;
  394. int ret = 0;
  395. if (! subs->cur_audiofmt) {
  396. snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
  397. return -ENXIO;
  398. }
  399. down_read(&subs->stream->chip->shutdown_rwsem);
  400. if (subs->stream->chip->shutdown) {
  401. ret = -ENODEV;
  402. goto unlock;
  403. }
  404. /* some unit conversions in runtime */
  405. subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
  406. subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
  407. /* reset the pointer */
  408. subs->hwptr_done = 0;
  409. subs->transfer_done = 0;
  410. subs->phase = 0;
  411. subs->last_delay = 0;
  412. subs->last_frame_number = 0;
  413. runtime->delay = 0;
  414. ret = snd_usb_substream_prepare(subs, runtime);
  415. unlock:
  416. up_read(&subs->stream->chip->shutdown_rwsem);
  417. return ret;
  418. }
  419. static struct snd_pcm_hardware snd_usb_hardware =
  420. {
  421. .info = SNDRV_PCM_INFO_MMAP |
  422. SNDRV_PCM_INFO_MMAP_VALID |
  423. SNDRV_PCM_INFO_BATCH |
  424. SNDRV_PCM_INFO_INTERLEAVED |
  425. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  426. SNDRV_PCM_INFO_PAUSE,
  427. .buffer_bytes_max = 1024 * 1024,
  428. .period_bytes_min = 64,
  429. .period_bytes_max = 512 * 1024,
  430. .periods_min = 2,
  431. .periods_max = 1024,
  432. };
  433. static int hw_check_valid_format(struct snd_usb_substream *subs,
  434. struct snd_pcm_hw_params *params,
  435. struct audioformat *fp)
  436. {
  437. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  438. struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  439. struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  440. struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  441. struct snd_mask check_fmts;
  442. unsigned int ptime;
  443. /* check the format */
  444. snd_mask_none(&check_fmts);
  445. check_fmts.bits[0] = (u32)fp->formats;
  446. check_fmts.bits[1] = (u32)(fp->formats >> 32);
  447. snd_mask_intersect(&check_fmts, fmts);
  448. if (snd_mask_empty(&check_fmts)) {
  449. hwc_debug(" > check: no supported format %d\n", fp->format);
  450. return 0;
  451. }
  452. /* check the channels */
  453. if (fp->channels < ct->min || fp->channels > ct->max) {
  454. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  455. return 0;
  456. }
  457. /* check the rate is within the range */
  458. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  459. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  460. return 0;
  461. }
  462. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  463. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  464. return 0;
  465. }
  466. /* check whether the period time is >= the data packet interval */
  467. if (subs->speed != USB_SPEED_FULL) {
  468. ptime = 125 * (1 << fp->datainterval);
  469. if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  470. hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
  471. return 0;
  472. }
  473. }
  474. return 1;
  475. }
  476. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  477. struct snd_pcm_hw_rule *rule)
  478. {
  479. struct snd_usb_substream *subs = rule->private;
  480. struct list_head *p;
  481. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  482. unsigned int rmin, rmax;
  483. int changed;
  484. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  485. changed = 0;
  486. rmin = rmax = 0;
  487. list_for_each(p, &subs->fmt_list) {
  488. struct audioformat *fp;
  489. fp = list_entry(p, struct audioformat, list);
  490. if (!hw_check_valid_format(subs, params, fp))
  491. continue;
  492. if (changed++) {
  493. if (rmin > fp->rate_min)
  494. rmin = fp->rate_min;
  495. if (rmax < fp->rate_max)
  496. rmax = fp->rate_max;
  497. } else {
  498. rmin = fp->rate_min;
  499. rmax = fp->rate_max;
  500. }
  501. }
  502. if (!changed) {
  503. hwc_debug(" --> get empty\n");
  504. it->empty = 1;
  505. return -EINVAL;
  506. }
  507. changed = 0;
  508. if (it->min < rmin) {
  509. it->min = rmin;
  510. it->openmin = 0;
  511. changed = 1;
  512. }
  513. if (it->max > rmax) {
  514. it->max = rmax;
  515. it->openmax = 0;
  516. changed = 1;
  517. }
  518. if (snd_interval_checkempty(it)) {
  519. it->empty = 1;
  520. return -EINVAL;
  521. }
  522. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  523. return changed;
  524. }
  525. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  526. struct snd_pcm_hw_rule *rule)
  527. {
  528. struct snd_usb_substream *subs = rule->private;
  529. struct list_head *p;
  530. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  531. unsigned int rmin, rmax;
  532. int changed;
  533. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  534. changed = 0;
  535. rmin = rmax = 0;
  536. list_for_each(p, &subs->fmt_list) {
  537. struct audioformat *fp;
  538. fp = list_entry(p, struct audioformat, list);
  539. if (!hw_check_valid_format(subs, params, fp))
  540. continue;
  541. if (changed++) {
  542. if (rmin > fp->channels)
  543. rmin = fp->channels;
  544. if (rmax < fp->channels)
  545. rmax = fp->channels;
  546. } else {
  547. rmin = fp->channels;
  548. rmax = fp->channels;
  549. }
  550. }
  551. if (!changed) {
  552. hwc_debug(" --> get empty\n");
  553. it->empty = 1;
  554. return -EINVAL;
  555. }
  556. changed = 0;
  557. if (it->min < rmin) {
  558. it->min = rmin;
  559. it->openmin = 0;
  560. changed = 1;
  561. }
  562. if (it->max > rmax) {
  563. it->max = rmax;
  564. it->openmax = 0;
  565. changed = 1;
  566. }
  567. if (snd_interval_checkempty(it)) {
  568. it->empty = 1;
  569. return -EINVAL;
  570. }
  571. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  572. return changed;
  573. }
  574. static int hw_rule_format(struct snd_pcm_hw_params *params,
  575. struct snd_pcm_hw_rule *rule)
  576. {
  577. struct snd_usb_substream *subs = rule->private;
  578. struct list_head *p;
  579. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  580. u64 fbits;
  581. u32 oldbits[2];
  582. int changed;
  583. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  584. fbits = 0;
  585. list_for_each(p, &subs->fmt_list) {
  586. struct audioformat *fp;
  587. fp = list_entry(p, struct audioformat, list);
  588. if (!hw_check_valid_format(subs, params, fp))
  589. continue;
  590. fbits |= fp->formats;
  591. }
  592. oldbits[0] = fmt->bits[0];
  593. oldbits[1] = fmt->bits[1];
  594. fmt->bits[0] &= (u32)fbits;
  595. fmt->bits[1] &= (u32)(fbits >> 32);
  596. if (!fmt->bits[0] && !fmt->bits[1]) {
  597. hwc_debug(" --> get empty\n");
  598. return -EINVAL;
  599. }
  600. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  601. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  602. return changed;
  603. }
  604. static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  605. struct snd_pcm_hw_rule *rule)
  606. {
  607. struct snd_usb_substream *subs = rule->private;
  608. struct audioformat *fp;
  609. struct snd_interval *it;
  610. unsigned char min_datainterval;
  611. unsigned int pmin;
  612. int changed;
  613. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  614. hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
  615. min_datainterval = 0xff;
  616. list_for_each_entry(fp, &subs->fmt_list, list) {
  617. if (!hw_check_valid_format(subs, params, fp))
  618. continue;
  619. min_datainterval = min(min_datainterval, fp->datainterval);
  620. }
  621. if (min_datainterval == 0xff) {
  622. hwc_debug(" --> get empty\n");
  623. it->empty = 1;
  624. return -EINVAL;
  625. }
  626. pmin = 125 * (1 << min_datainterval);
  627. changed = 0;
  628. if (it->min < pmin) {
  629. it->min = pmin;
  630. it->openmin = 0;
  631. changed = 1;
  632. }
  633. if (snd_interval_checkempty(it)) {
  634. it->empty = 1;
  635. return -EINVAL;
  636. }
  637. hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
  638. return changed;
  639. }
  640. /*
  641. * If the device supports unusual bit rates, does the request meet these?
  642. */
  643. static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
  644. struct snd_usb_substream *subs)
  645. {
  646. struct audioformat *fp;
  647. int *rate_list;
  648. int count = 0, needs_knot = 0;
  649. int err;
  650. kfree(subs->rate_list.list);
  651. subs->rate_list.list = NULL;
  652. list_for_each_entry(fp, &subs->fmt_list, list) {
  653. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
  654. return 0;
  655. count += fp->nr_rates;
  656. if (fp->rates & SNDRV_PCM_RATE_KNOT)
  657. needs_knot = 1;
  658. }
  659. if (!needs_knot)
  660. return 0;
  661. subs->rate_list.list = rate_list =
  662. kmalloc(sizeof(int) * count, GFP_KERNEL);
  663. if (!subs->rate_list.list)
  664. return -ENOMEM;
  665. subs->rate_list.count = count;
  666. subs->rate_list.mask = 0;
  667. count = 0;
  668. list_for_each_entry(fp, &subs->fmt_list, list) {
  669. int i;
  670. for (i = 0; i < fp->nr_rates; i++)
  671. rate_list[count++] = fp->rate_table[i];
  672. }
  673. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  674. &subs->rate_list);
  675. if (err < 0)
  676. return err;
  677. return 0;
  678. }
  679. /*
  680. * set up the runtime hardware information.
  681. */
  682. static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  683. {
  684. struct list_head *p;
  685. unsigned int pt, ptmin;
  686. int param_period_time_if_needed;
  687. int err;
  688. runtime->hw.formats = subs->formats;
  689. runtime->hw.rate_min = 0x7fffffff;
  690. runtime->hw.rate_max = 0;
  691. runtime->hw.channels_min = 256;
  692. runtime->hw.channels_max = 0;
  693. runtime->hw.rates = 0;
  694. ptmin = UINT_MAX;
  695. /* check min/max rates and channels */
  696. list_for_each(p, &subs->fmt_list) {
  697. struct audioformat *fp;
  698. fp = list_entry(p, struct audioformat, list);
  699. runtime->hw.rates |= fp->rates;
  700. if (runtime->hw.rate_min > fp->rate_min)
  701. runtime->hw.rate_min = fp->rate_min;
  702. if (runtime->hw.rate_max < fp->rate_max)
  703. runtime->hw.rate_max = fp->rate_max;
  704. if (runtime->hw.channels_min > fp->channels)
  705. runtime->hw.channels_min = fp->channels;
  706. if (runtime->hw.channels_max < fp->channels)
  707. runtime->hw.channels_max = fp->channels;
  708. if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  709. /* FIXME: there might be more than one audio formats... */
  710. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  711. fp->frame_size;
  712. }
  713. pt = 125 * (1 << fp->datainterval);
  714. ptmin = min(ptmin, pt);
  715. }
  716. err = snd_usb_autoresume(subs->stream->chip);
  717. if (err < 0)
  718. return err;
  719. param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
  720. if (subs->speed == USB_SPEED_FULL)
  721. /* full speed devices have fixed data packet interval */
  722. ptmin = 1000;
  723. if (ptmin == 1000)
  724. /* if period time doesn't go below 1 ms, no rules needed */
  725. param_period_time_if_needed = -1;
  726. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  727. ptmin, UINT_MAX);
  728. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  729. hw_rule_rate, subs,
  730. SNDRV_PCM_HW_PARAM_FORMAT,
  731. SNDRV_PCM_HW_PARAM_CHANNELS,
  732. param_period_time_if_needed,
  733. -1)) < 0)
  734. goto rep_err;
  735. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  736. hw_rule_channels, subs,
  737. SNDRV_PCM_HW_PARAM_FORMAT,
  738. SNDRV_PCM_HW_PARAM_RATE,
  739. param_period_time_if_needed,
  740. -1)) < 0)
  741. goto rep_err;
  742. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  743. hw_rule_format, subs,
  744. SNDRV_PCM_HW_PARAM_RATE,
  745. SNDRV_PCM_HW_PARAM_CHANNELS,
  746. param_period_time_if_needed,
  747. -1)) < 0)
  748. goto rep_err;
  749. if (param_period_time_if_needed >= 0) {
  750. err = snd_pcm_hw_rule_add(runtime, 0,
  751. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  752. hw_rule_period_time, subs,
  753. SNDRV_PCM_HW_PARAM_FORMAT,
  754. SNDRV_PCM_HW_PARAM_CHANNELS,
  755. SNDRV_PCM_HW_PARAM_RATE,
  756. -1);
  757. if (err < 0)
  758. goto rep_err;
  759. }
  760. if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
  761. goto rep_err;
  762. return 0;
  763. rep_err:
  764. snd_usb_autosuspend(subs->stream->chip);
  765. return err;
  766. }
  767. static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
  768. {
  769. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  770. struct snd_pcm_runtime *runtime = substream->runtime;
  771. struct snd_usb_substream *subs = &as->substream[direction];
  772. subs->interface = -1;
  773. subs->altset_idx = 0;
  774. runtime->hw = snd_usb_hardware;
  775. runtime->private_data = subs;
  776. subs->pcm_substream = substream;
  777. /* runtime PM is also done there */
  778. return setup_hw_info(runtime, subs);
  779. }
  780. static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
  781. {
  782. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  783. struct snd_usb_substream *subs = &as->substream[direction];
  784. if (!as->chip->shutdown && subs->interface >= 0) {
  785. usb_set_interface(subs->dev, subs->interface, 0);
  786. subs->interface = -1;
  787. }
  788. subs->pcm_substream = NULL;
  789. snd_usb_autosuspend(subs->stream->chip);
  790. return 0;
  791. }
  792. static int snd_usb_playback_open(struct snd_pcm_substream *substream)
  793. {
  794. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
  795. }
  796. static int snd_usb_playback_close(struct snd_pcm_substream *substream)
  797. {
  798. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
  799. }
  800. static int snd_usb_capture_open(struct snd_pcm_substream *substream)
  801. {
  802. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
  803. }
  804. static int snd_usb_capture_close(struct snd_pcm_substream *substream)
  805. {
  806. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  807. }
  808. static struct snd_pcm_ops snd_usb_playback_ops = {
  809. .open = snd_usb_playback_open,
  810. .close = snd_usb_playback_close,
  811. .ioctl = snd_pcm_lib_ioctl,
  812. .hw_params = snd_usb_hw_params,
  813. .hw_free = snd_usb_hw_free,
  814. .prepare = snd_usb_pcm_prepare,
  815. .trigger = snd_usb_substream_playback_trigger,
  816. .pointer = snd_usb_pcm_pointer,
  817. .page = snd_pcm_lib_get_vmalloc_page,
  818. .mmap = snd_pcm_lib_mmap_vmalloc,
  819. };
  820. static struct snd_pcm_ops snd_usb_capture_ops = {
  821. .open = snd_usb_capture_open,
  822. .close = snd_usb_capture_close,
  823. .ioctl = snd_pcm_lib_ioctl,
  824. .hw_params = snd_usb_hw_params,
  825. .hw_free = snd_usb_hw_free,
  826. .prepare = snd_usb_pcm_prepare,
  827. .trigger = snd_usb_substream_capture_trigger,
  828. .pointer = snd_usb_pcm_pointer,
  829. .page = snd_pcm_lib_get_vmalloc_page,
  830. .mmap = snd_pcm_lib_mmap_vmalloc,
  831. };
  832. void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
  833. {
  834. snd_pcm_set_ops(pcm, stream,
  835. stream == SNDRV_PCM_STREAM_PLAYBACK ?
  836. &snd_usb_playback_ops : &snd_usb_capture_ops);
  837. }