pcm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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->fill_max = 0;
  216. /* we need a sync pipe in async OUT or adaptive IN mode */
  217. /* check the number of EP, since some devices have broken
  218. * descriptors which fool us. if it has only one EP,
  219. * assume it as adaptive-out or sync-in.
  220. */
  221. attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
  222. if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
  223. (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
  224. altsd->bNumEndpoints >= 2) {
  225. /* check sync-pipe endpoint */
  226. /* ... and check descriptor size before accessing bSynchAddress
  227. because there is a version of the SB Audigy 2 NX firmware lacking
  228. the audio fields in the endpoint descriptors */
  229. if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
  230. (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  231. get_endpoint(alts, 1)->bSynchAddress != 0)) {
  232. snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
  233. dev->devnum, fmt->iface, fmt->altsetting);
  234. return -EINVAL;
  235. }
  236. ep = get_endpoint(alts, 1)->bEndpointAddress;
  237. if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  238. (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
  239. (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
  240. snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
  241. dev->devnum, fmt->iface, fmt->altsetting);
  242. return -EINVAL;
  243. }
  244. ep &= USB_ENDPOINT_NUMBER_MASK;
  245. if (is_playback)
  246. subs->syncpipe = usb_rcvisocpipe(dev, ep);
  247. else
  248. subs->syncpipe = usb_sndisocpipe(dev, ep);
  249. if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  250. get_endpoint(alts, 1)->bRefresh >= 1 &&
  251. get_endpoint(alts, 1)->bRefresh <= 9)
  252. subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
  253. else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  254. subs->syncinterval = 1;
  255. else if (get_endpoint(alts, 1)->bInterval >= 1 &&
  256. get_endpoint(alts, 1)->bInterval <= 16)
  257. subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
  258. else
  259. subs->syncinterval = 3;
  260. }
  261. /* always fill max packet size */
  262. if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
  263. subs->fill_max = 1;
  264. if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
  265. return err;
  266. subs->cur_audiofmt = fmt;
  267. snd_usb_set_format_quirk(subs, fmt);
  268. #if 0
  269. printk(KERN_DEBUG
  270. "setting done: format = %d, rate = %d..%d, channels = %d\n",
  271. fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
  272. printk(KERN_DEBUG
  273. " datapipe = 0x%0x, syncpipe = 0x%0x\n",
  274. subs->datapipe, subs->syncpipe);
  275. #endif
  276. return 0;
  277. }
  278. /*
  279. * hw_params callback
  280. *
  281. * allocate a buffer and set the given audio format.
  282. *
  283. * so far we use a physically linear buffer although packetize transfer
  284. * doesn't need a continuous area.
  285. * if sg buffer is supported on the later version of alsa, we'll follow
  286. * that.
  287. */
  288. static int snd_usb_hw_params(struct snd_pcm_substream *substream,
  289. struct snd_pcm_hw_params *hw_params)
  290. {
  291. struct snd_usb_substream *subs = substream->runtime->private_data;
  292. struct audioformat *fmt;
  293. unsigned int channels, rate, format;
  294. int ret, changed;
  295. ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  296. params_buffer_bytes(hw_params));
  297. if (ret < 0)
  298. return ret;
  299. format = params_format(hw_params);
  300. rate = params_rate(hw_params);
  301. channels = params_channels(hw_params);
  302. fmt = find_format(subs, format, rate, channels);
  303. if (!fmt) {
  304. snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
  305. format, rate, channels);
  306. return -EINVAL;
  307. }
  308. changed = subs->cur_audiofmt != fmt ||
  309. subs->period_bytes != params_period_bytes(hw_params) ||
  310. subs->cur_rate != rate;
  311. if ((ret = set_format(subs, fmt)) < 0)
  312. return ret;
  313. if (subs->cur_rate != rate) {
  314. struct usb_host_interface *alts;
  315. struct usb_interface *iface;
  316. iface = usb_ifnum_to_if(subs->dev, fmt->iface);
  317. alts = &iface->altsetting[fmt->altset_idx];
  318. ret = snd_usb_init_sample_rate(subs->stream->chip, subs->interface, alts, fmt, rate);
  319. if (ret < 0)
  320. return ret;
  321. subs->cur_rate = rate;
  322. }
  323. if (changed) {
  324. /* format changed */
  325. snd_usb_release_substream_urbs(subs, 0);
  326. /* influenced: period_bytes, channels, rate, format, */
  327. ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
  328. params_rate(hw_params),
  329. snd_pcm_format_physical_width(params_format(hw_params)) *
  330. params_channels(hw_params));
  331. }
  332. return ret;
  333. }
  334. /*
  335. * hw_free callback
  336. *
  337. * reset the audio format and release the buffer
  338. */
  339. static int snd_usb_hw_free(struct snd_pcm_substream *substream)
  340. {
  341. struct snd_usb_substream *subs = substream->runtime->private_data;
  342. subs->cur_audiofmt = NULL;
  343. subs->cur_rate = 0;
  344. subs->period_bytes = 0;
  345. if (!subs->stream->chip->shutdown)
  346. snd_usb_release_substream_urbs(subs, 0);
  347. return snd_pcm_lib_free_vmalloc_buffer(substream);
  348. }
  349. /*
  350. * prepare callback
  351. *
  352. * only a few subtle things...
  353. */
  354. static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
  355. {
  356. struct snd_pcm_runtime *runtime = substream->runtime;
  357. struct snd_usb_substream *subs = runtime->private_data;
  358. if (! subs->cur_audiofmt) {
  359. snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
  360. return -ENXIO;
  361. }
  362. /* some unit conversions in runtime */
  363. subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
  364. subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
  365. /* reset the pointer */
  366. subs->hwptr_done = 0;
  367. subs->transfer_done = 0;
  368. subs->phase = 0;
  369. runtime->delay = 0;
  370. return snd_usb_substream_prepare(subs, runtime);
  371. }
  372. static struct snd_pcm_hardware snd_usb_hardware =
  373. {
  374. .info = SNDRV_PCM_INFO_MMAP |
  375. SNDRV_PCM_INFO_MMAP_VALID |
  376. SNDRV_PCM_INFO_BATCH |
  377. SNDRV_PCM_INFO_INTERLEAVED |
  378. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  379. SNDRV_PCM_INFO_PAUSE,
  380. .buffer_bytes_max = 1024 * 1024,
  381. .period_bytes_min = 64,
  382. .period_bytes_max = 512 * 1024,
  383. .periods_min = 2,
  384. .periods_max = 1024,
  385. };
  386. static int hw_check_valid_format(struct snd_usb_substream *subs,
  387. struct snd_pcm_hw_params *params,
  388. struct audioformat *fp)
  389. {
  390. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  391. struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  392. struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  393. struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  394. struct snd_mask check_fmts;
  395. unsigned int ptime;
  396. /* check the format */
  397. snd_mask_none(&check_fmts);
  398. check_fmts.bits[0] = (u32)fp->formats;
  399. check_fmts.bits[1] = (u32)(fp->formats >> 32);
  400. snd_mask_intersect(&check_fmts, fmts);
  401. if (snd_mask_empty(&check_fmts)) {
  402. hwc_debug(" > check: no supported format %d\n", fp->format);
  403. return 0;
  404. }
  405. /* check the channels */
  406. if (fp->channels < ct->min || fp->channels > ct->max) {
  407. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  408. return 0;
  409. }
  410. /* check the rate is within the range */
  411. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  412. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  413. return 0;
  414. }
  415. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  416. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  417. return 0;
  418. }
  419. /* check whether the period time is >= the data packet interval */
  420. if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
  421. ptime = 125 * (1 << fp->datainterval);
  422. if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  423. hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
  424. return 0;
  425. }
  426. }
  427. return 1;
  428. }
  429. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  430. struct snd_pcm_hw_rule *rule)
  431. {
  432. struct snd_usb_substream *subs = rule->private;
  433. struct list_head *p;
  434. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  435. unsigned int rmin, rmax;
  436. int changed;
  437. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  438. changed = 0;
  439. rmin = rmax = 0;
  440. list_for_each(p, &subs->fmt_list) {
  441. struct audioformat *fp;
  442. fp = list_entry(p, struct audioformat, list);
  443. if (!hw_check_valid_format(subs, params, fp))
  444. continue;
  445. if (changed++) {
  446. if (rmin > fp->rate_min)
  447. rmin = fp->rate_min;
  448. if (rmax < fp->rate_max)
  449. rmax = fp->rate_max;
  450. } else {
  451. rmin = fp->rate_min;
  452. rmax = fp->rate_max;
  453. }
  454. }
  455. if (!changed) {
  456. hwc_debug(" --> get empty\n");
  457. it->empty = 1;
  458. return -EINVAL;
  459. }
  460. changed = 0;
  461. if (it->min < rmin) {
  462. it->min = rmin;
  463. it->openmin = 0;
  464. changed = 1;
  465. }
  466. if (it->max > rmax) {
  467. it->max = rmax;
  468. it->openmax = 0;
  469. changed = 1;
  470. }
  471. if (snd_interval_checkempty(it)) {
  472. it->empty = 1;
  473. return -EINVAL;
  474. }
  475. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  476. return changed;
  477. }
  478. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  479. struct snd_pcm_hw_rule *rule)
  480. {
  481. struct snd_usb_substream *subs = rule->private;
  482. struct list_head *p;
  483. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  484. unsigned int rmin, rmax;
  485. int changed;
  486. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  487. changed = 0;
  488. rmin = rmax = 0;
  489. list_for_each(p, &subs->fmt_list) {
  490. struct audioformat *fp;
  491. fp = list_entry(p, struct audioformat, list);
  492. if (!hw_check_valid_format(subs, params, fp))
  493. continue;
  494. if (changed++) {
  495. if (rmin > fp->channels)
  496. rmin = fp->channels;
  497. if (rmax < fp->channels)
  498. rmax = fp->channels;
  499. } else {
  500. rmin = fp->channels;
  501. rmax = fp->channels;
  502. }
  503. }
  504. if (!changed) {
  505. hwc_debug(" --> get empty\n");
  506. it->empty = 1;
  507. return -EINVAL;
  508. }
  509. changed = 0;
  510. if (it->min < rmin) {
  511. it->min = rmin;
  512. it->openmin = 0;
  513. changed = 1;
  514. }
  515. if (it->max > rmax) {
  516. it->max = rmax;
  517. it->openmax = 0;
  518. changed = 1;
  519. }
  520. if (snd_interval_checkempty(it)) {
  521. it->empty = 1;
  522. return -EINVAL;
  523. }
  524. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  525. return changed;
  526. }
  527. static int hw_rule_format(struct snd_pcm_hw_params *params,
  528. struct snd_pcm_hw_rule *rule)
  529. {
  530. struct snd_usb_substream *subs = rule->private;
  531. struct list_head *p;
  532. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  533. u64 fbits;
  534. u32 oldbits[2];
  535. int changed;
  536. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  537. fbits = 0;
  538. list_for_each(p, &subs->fmt_list) {
  539. struct audioformat *fp;
  540. fp = list_entry(p, struct audioformat, list);
  541. if (!hw_check_valid_format(subs, params, fp))
  542. continue;
  543. fbits |= fp->formats;
  544. }
  545. oldbits[0] = fmt->bits[0];
  546. oldbits[1] = fmt->bits[1];
  547. fmt->bits[0] &= (u32)fbits;
  548. fmt->bits[1] &= (u32)(fbits >> 32);
  549. if (!fmt->bits[0] && !fmt->bits[1]) {
  550. hwc_debug(" --> get empty\n");
  551. return -EINVAL;
  552. }
  553. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  554. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  555. return changed;
  556. }
  557. static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  558. struct snd_pcm_hw_rule *rule)
  559. {
  560. struct snd_usb_substream *subs = rule->private;
  561. struct audioformat *fp;
  562. struct snd_interval *it;
  563. unsigned char min_datainterval;
  564. unsigned int pmin;
  565. int changed;
  566. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  567. hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
  568. min_datainterval = 0xff;
  569. list_for_each_entry(fp, &subs->fmt_list, list) {
  570. if (!hw_check_valid_format(subs, params, fp))
  571. continue;
  572. min_datainterval = min(min_datainterval, fp->datainterval);
  573. }
  574. if (min_datainterval == 0xff) {
  575. hwc_debug(" --> get empty\n");
  576. it->empty = 1;
  577. return -EINVAL;
  578. }
  579. pmin = 125 * (1 << min_datainterval);
  580. changed = 0;
  581. if (it->min < pmin) {
  582. it->min = pmin;
  583. it->openmin = 0;
  584. changed = 1;
  585. }
  586. if (snd_interval_checkempty(it)) {
  587. it->empty = 1;
  588. return -EINVAL;
  589. }
  590. hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
  591. return changed;
  592. }
  593. /*
  594. * If the device supports unusual bit rates, does the request meet these?
  595. */
  596. static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
  597. struct snd_usb_substream *subs)
  598. {
  599. struct audioformat *fp;
  600. int count = 0, needs_knot = 0;
  601. int err;
  602. list_for_each_entry(fp, &subs->fmt_list, list) {
  603. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
  604. return 0;
  605. count += fp->nr_rates;
  606. if (fp->rates & SNDRV_PCM_RATE_KNOT)
  607. needs_knot = 1;
  608. }
  609. if (!needs_knot)
  610. return 0;
  611. subs->rate_list.count = count;
  612. subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
  613. subs->rate_list.mask = 0;
  614. count = 0;
  615. list_for_each_entry(fp, &subs->fmt_list, list) {
  616. int i;
  617. for (i = 0; i < fp->nr_rates; i++)
  618. subs->rate_list.list[count++] = fp->rate_table[i];
  619. }
  620. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  621. &subs->rate_list);
  622. if (err < 0)
  623. return err;
  624. return 0;
  625. }
  626. /*
  627. * set up the runtime hardware information.
  628. */
  629. static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  630. {
  631. struct list_head *p;
  632. unsigned int pt, ptmin;
  633. int param_period_time_if_needed;
  634. int err;
  635. runtime->hw.formats = subs->formats;
  636. runtime->hw.rate_min = 0x7fffffff;
  637. runtime->hw.rate_max = 0;
  638. runtime->hw.channels_min = 256;
  639. runtime->hw.channels_max = 0;
  640. runtime->hw.rates = 0;
  641. ptmin = UINT_MAX;
  642. /* check min/max rates and channels */
  643. list_for_each(p, &subs->fmt_list) {
  644. struct audioformat *fp;
  645. fp = list_entry(p, struct audioformat, list);
  646. runtime->hw.rates |= fp->rates;
  647. if (runtime->hw.rate_min > fp->rate_min)
  648. runtime->hw.rate_min = fp->rate_min;
  649. if (runtime->hw.rate_max < fp->rate_max)
  650. runtime->hw.rate_max = fp->rate_max;
  651. if (runtime->hw.channels_min > fp->channels)
  652. runtime->hw.channels_min = fp->channels;
  653. if (runtime->hw.channels_max < fp->channels)
  654. runtime->hw.channels_max = fp->channels;
  655. if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  656. /* FIXME: there might be more than one audio formats... */
  657. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  658. fp->frame_size;
  659. }
  660. pt = 125 * (1 << fp->datainterval);
  661. ptmin = min(ptmin, pt);
  662. }
  663. param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
  664. if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
  665. /* full speed devices have fixed data packet interval */
  666. ptmin = 1000;
  667. if (ptmin == 1000)
  668. /* if period time doesn't go below 1 ms, no rules needed */
  669. param_period_time_if_needed = -1;
  670. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  671. ptmin, UINT_MAX);
  672. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  673. hw_rule_rate, subs,
  674. SNDRV_PCM_HW_PARAM_FORMAT,
  675. SNDRV_PCM_HW_PARAM_CHANNELS,
  676. param_period_time_if_needed,
  677. -1)) < 0)
  678. return err;
  679. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  680. hw_rule_channels, subs,
  681. SNDRV_PCM_HW_PARAM_FORMAT,
  682. SNDRV_PCM_HW_PARAM_RATE,
  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_FORMAT,
  687. hw_rule_format, subs,
  688. SNDRV_PCM_HW_PARAM_RATE,
  689. SNDRV_PCM_HW_PARAM_CHANNELS,
  690. param_period_time_if_needed,
  691. -1)) < 0)
  692. return err;
  693. if (param_period_time_if_needed >= 0) {
  694. err = snd_pcm_hw_rule_add(runtime, 0,
  695. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  696. hw_rule_period_time, subs,
  697. SNDRV_PCM_HW_PARAM_FORMAT,
  698. SNDRV_PCM_HW_PARAM_CHANNELS,
  699. SNDRV_PCM_HW_PARAM_RATE,
  700. -1);
  701. if (err < 0)
  702. return err;
  703. }
  704. if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
  705. return err;
  706. return 0;
  707. }
  708. static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
  709. {
  710. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  711. struct snd_pcm_runtime *runtime = substream->runtime;
  712. struct snd_usb_substream *subs = &as->substream[direction];
  713. subs->interface = -1;
  714. subs->altset_idx = 0;
  715. runtime->hw = snd_usb_hardware;
  716. runtime->private_data = subs;
  717. subs->pcm_substream = substream;
  718. return setup_hw_info(runtime, subs);
  719. }
  720. static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
  721. {
  722. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  723. struct snd_usb_substream *subs = &as->substream[direction];
  724. if (!as->chip->shutdown && subs->interface >= 0) {
  725. usb_set_interface(subs->dev, subs->interface, 0);
  726. subs->interface = -1;
  727. }
  728. subs->pcm_substream = NULL;
  729. return 0;
  730. }
  731. static int snd_usb_playback_open(struct snd_pcm_substream *substream)
  732. {
  733. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
  734. }
  735. static int snd_usb_playback_close(struct snd_pcm_substream *substream)
  736. {
  737. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
  738. }
  739. static int snd_usb_capture_open(struct snd_pcm_substream *substream)
  740. {
  741. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
  742. }
  743. static int snd_usb_capture_close(struct snd_pcm_substream *substream)
  744. {
  745. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  746. }
  747. static struct snd_pcm_ops snd_usb_playback_ops = {
  748. .open = snd_usb_playback_open,
  749. .close = snd_usb_playback_close,
  750. .ioctl = snd_pcm_lib_ioctl,
  751. .hw_params = snd_usb_hw_params,
  752. .hw_free = snd_usb_hw_free,
  753. .prepare = snd_usb_pcm_prepare,
  754. .trigger = snd_usb_substream_playback_trigger,
  755. .pointer = snd_usb_pcm_pointer,
  756. .page = snd_pcm_lib_get_vmalloc_page,
  757. .mmap = snd_pcm_lib_mmap_vmalloc,
  758. };
  759. static struct snd_pcm_ops snd_usb_capture_ops = {
  760. .open = snd_usb_capture_open,
  761. .close = snd_usb_capture_close,
  762. .ioctl = snd_pcm_lib_ioctl,
  763. .hw_params = snd_usb_hw_params,
  764. .hw_free = snd_usb_hw_free,
  765. .prepare = snd_usb_pcm_prepare,
  766. .trigger = snd_usb_substream_capture_trigger,
  767. .pointer = snd_usb_pcm_pointer,
  768. .page = snd_pcm_lib_get_vmalloc_page,
  769. .mmap = snd_pcm_lib_mmap_vmalloc,
  770. };
  771. void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
  772. {
  773. snd_pcm_set_ops(pcm, stream,
  774. stream == SNDRV_PCM_STREAM_PLAYBACK ?
  775. &snd_usb_playback_ops : &snd_usb_capture_ops);
  776. }