pcm.c 25 KB

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