pcm.c 25 KB

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