pcm.c 26 KB

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