pcm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. /* format changed */
  327. snd_usb_release_substream_urbs(subs, 0);
  328. /* influenced: period_bytes, channels, rate, format, */
  329. ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
  330. params_rate(hw_params),
  331. snd_pcm_format_physical_width(params_format(hw_params)) *
  332. params_channels(hw_params));
  333. }
  334. return ret;
  335. }
  336. /*
  337. * hw_free callback
  338. *
  339. * reset the audio format and release the buffer
  340. */
  341. static int snd_usb_hw_free(struct snd_pcm_substream *substream)
  342. {
  343. struct snd_usb_substream *subs = substream->runtime->private_data;
  344. subs->cur_audiofmt = NULL;
  345. subs->cur_rate = 0;
  346. subs->period_bytes = 0;
  347. if (!subs->stream->chip->shutdown)
  348. snd_usb_release_substream_urbs(subs, 0);
  349. return snd_pcm_lib_free_vmalloc_buffer(substream);
  350. }
  351. /*
  352. * prepare callback
  353. *
  354. * only a few subtle things...
  355. */
  356. static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
  357. {
  358. struct snd_pcm_runtime *runtime = substream->runtime;
  359. struct snd_usb_substream *subs = runtime->private_data;
  360. if (! subs->cur_audiofmt) {
  361. snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
  362. return -ENXIO;
  363. }
  364. /* some unit conversions in runtime */
  365. subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
  366. subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
  367. /* reset the pointer */
  368. subs->hwptr_done = 0;
  369. subs->transfer_done = 0;
  370. subs->phase = 0;
  371. runtime->delay = 0;
  372. return snd_usb_substream_prepare(subs, runtime);
  373. }
  374. static struct snd_pcm_hardware snd_usb_hardware =
  375. {
  376. .info = SNDRV_PCM_INFO_MMAP |
  377. SNDRV_PCM_INFO_MMAP_VALID |
  378. SNDRV_PCM_INFO_BATCH |
  379. SNDRV_PCM_INFO_INTERLEAVED |
  380. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  381. SNDRV_PCM_INFO_PAUSE,
  382. .buffer_bytes_max = 1024 * 1024,
  383. .period_bytes_min = 64,
  384. .period_bytes_max = 512 * 1024,
  385. .periods_min = 2,
  386. .periods_max = 1024,
  387. };
  388. static int hw_check_valid_format(struct snd_usb_substream *subs,
  389. struct snd_pcm_hw_params *params,
  390. struct audioformat *fp)
  391. {
  392. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  393. struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  394. struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  395. struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  396. struct snd_mask check_fmts;
  397. unsigned int ptime;
  398. /* check the format */
  399. snd_mask_none(&check_fmts);
  400. check_fmts.bits[0] = (u32)fp->formats;
  401. check_fmts.bits[1] = (u32)(fp->formats >> 32);
  402. snd_mask_intersect(&check_fmts, fmts);
  403. if (snd_mask_empty(&check_fmts)) {
  404. hwc_debug(" > check: no supported format %d\n", fp->format);
  405. return 0;
  406. }
  407. /* check the channels */
  408. if (fp->channels < ct->min || fp->channels > ct->max) {
  409. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  410. return 0;
  411. }
  412. /* check the rate is within the range */
  413. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  414. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  415. return 0;
  416. }
  417. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  418. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  419. return 0;
  420. }
  421. /* check whether the period time is >= the data packet interval */
  422. if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) {
  423. ptime = 125 * (1 << fp->datainterval);
  424. if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  425. hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
  426. return 0;
  427. }
  428. }
  429. return 1;
  430. }
  431. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  432. struct snd_pcm_hw_rule *rule)
  433. {
  434. struct snd_usb_substream *subs = rule->private;
  435. struct list_head *p;
  436. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  437. unsigned int rmin, rmax;
  438. int changed;
  439. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  440. changed = 0;
  441. rmin = rmax = 0;
  442. list_for_each(p, &subs->fmt_list) {
  443. struct audioformat *fp;
  444. fp = list_entry(p, struct audioformat, list);
  445. if (!hw_check_valid_format(subs, params, fp))
  446. continue;
  447. if (changed++) {
  448. if (rmin > fp->rate_min)
  449. rmin = fp->rate_min;
  450. if (rmax < fp->rate_max)
  451. rmax = fp->rate_max;
  452. } else {
  453. rmin = fp->rate_min;
  454. rmax = fp->rate_max;
  455. }
  456. }
  457. if (!changed) {
  458. hwc_debug(" --> get empty\n");
  459. it->empty = 1;
  460. return -EINVAL;
  461. }
  462. changed = 0;
  463. if (it->min < rmin) {
  464. it->min = rmin;
  465. it->openmin = 0;
  466. changed = 1;
  467. }
  468. if (it->max > rmax) {
  469. it->max = rmax;
  470. it->openmax = 0;
  471. changed = 1;
  472. }
  473. if (snd_interval_checkempty(it)) {
  474. it->empty = 1;
  475. return -EINVAL;
  476. }
  477. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  478. return changed;
  479. }
  480. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  481. struct snd_pcm_hw_rule *rule)
  482. {
  483. struct snd_usb_substream *subs = rule->private;
  484. struct list_head *p;
  485. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  486. unsigned int rmin, rmax;
  487. int changed;
  488. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  489. changed = 0;
  490. rmin = rmax = 0;
  491. list_for_each(p, &subs->fmt_list) {
  492. struct audioformat *fp;
  493. fp = list_entry(p, struct audioformat, list);
  494. if (!hw_check_valid_format(subs, params, fp))
  495. continue;
  496. if (changed++) {
  497. if (rmin > fp->channels)
  498. rmin = fp->channels;
  499. if (rmax < fp->channels)
  500. rmax = fp->channels;
  501. } else {
  502. rmin = fp->channels;
  503. rmax = fp->channels;
  504. }
  505. }
  506. if (!changed) {
  507. hwc_debug(" --> get empty\n");
  508. it->empty = 1;
  509. return -EINVAL;
  510. }
  511. changed = 0;
  512. if (it->min < rmin) {
  513. it->min = rmin;
  514. it->openmin = 0;
  515. changed = 1;
  516. }
  517. if (it->max > rmax) {
  518. it->max = rmax;
  519. it->openmax = 0;
  520. changed = 1;
  521. }
  522. if (snd_interval_checkempty(it)) {
  523. it->empty = 1;
  524. return -EINVAL;
  525. }
  526. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  527. return changed;
  528. }
  529. static int hw_rule_format(struct snd_pcm_hw_params *params,
  530. struct snd_pcm_hw_rule *rule)
  531. {
  532. struct snd_usb_substream *subs = rule->private;
  533. struct list_head *p;
  534. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  535. u64 fbits;
  536. u32 oldbits[2];
  537. int changed;
  538. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  539. fbits = 0;
  540. list_for_each(p, &subs->fmt_list) {
  541. struct audioformat *fp;
  542. fp = list_entry(p, struct audioformat, list);
  543. if (!hw_check_valid_format(subs, params, fp))
  544. continue;
  545. fbits |= fp->formats;
  546. }
  547. oldbits[0] = fmt->bits[0];
  548. oldbits[1] = fmt->bits[1];
  549. fmt->bits[0] &= (u32)fbits;
  550. fmt->bits[1] &= (u32)(fbits >> 32);
  551. if (!fmt->bits[0] && !fmt->bits[1]) {
  552. hwc_debug(" --> get empty\n");
  553. return -EINVAL;
  554. }
  555. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  556. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  557. return changed;
  558. }
  559. static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  560. struct snd_pcm_hw_rule *rule)
  561. {
  562. struct snd_usb_substream *subs = rule->private;
  563. struct audioformat *fp;
  564. struct snd_interval *it;
  565. unsigned char min_datainterval;
  566. unsigned int pmin;
  567. int changed;
  568. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  569. hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
  570. min_datainterval = 0xff;
  571. list_for_each_entry(fp, &subs->fmt_list, list) {
  572. if (!hw_check_valid_format(subs, params, fp))
  573. continue;
  574. min_datainterval = min(min_datainterval, fp->datainterval);
  575. }
  576. if (min_datainterval == 0xff) {
  577. hwc_debug(" --> get empty\n");
  578. it->empty = 1;
  579. return -EINVAL;
  580. }
  581. pmin = 125 * (1 << min_datainterval);
  582. changed = 0;
  583. if (it->min < pmin) {
  584. it->min = pmin;
  585. it->openmin = 0;
  586. changed = 1;
  587. }
  588. if (snd_interval_checkempty(it)) {
  589. it->empty = 1;
  590. return -EINVAL;
  591. }
  592. hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
  593. return changed;
  594. }
  595. /*
  596. * If the device supports unusual bit rates, does the request meet these?
  597. */
  598. static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
  599. struct snd_usb_substream *subs)
  600. {
  601. struct audioformat *fp;
  602. int count = 0, needs_knot = 0;
  603. int err;
  604. list_for_each_entry(fp, &subs->fmt_list, list) {
  605. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
  606. return 0;
  607. count += fp->nr_rates;
  608. if (fp->rates & SNDRV_PCM_RATE_KNOT)
  609. needs_knot = 1;
  610. }
  611. if (!needs_knot)
  612. return 0;
  613. subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
  614. if (!subs->rate_list.list)
  615. return -ENOMEM;
  616. subs->rate_list.count = count;
  617. subs->rate_list.mask = 0;
  618. count = 0;
  619. list_for_each_entry(fp, &subs->fmt_list, list) {
  620. int i;
  621. for (i = 0; i < fp->nr_rates; i++)
  622. subs->rate_list.list[count++] = fp->rate_table[i];
  623. }
  624. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  625. &subs->rate_list);
  626. if (err < 0)
  627. return err;
  628. return 0;
  629. }
  630. /*
  631. * set up the runtime hardware information.
  632. */
  633. static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  634. {
  635. struct list_head *p;
  636. unsigned int pt, ptmin;
  637. int param_period_time_if_needed;
  638. int err;
  639. runtime->hw.formats = subs->formats;
  640. runtime->hw.rate_min = 0x7fffffff;
  641. runtime->hw.rate_max = 0;
  642. runtime->hw.channels_min = 256;
  643. runtime->hw.channels_max = 0;
  644. runtime->hw.rates = 0;
  645. ptmin = UINT_MAX;
  646. /* check min/max rates and channels */
  647. list_for_each(p, &subs->fmt_list) {
  648. struct audioformat *fp;
  649. fp = list_entry(p, struct audioformat, list);
  650. runtime->hw.rates |= fp->rates;
  651. if (runtime->hw.rate_min > fp->rate_min)
  652. runtime->hw.rate_min = fp->rate_min;
  653. if (runtime->hw.rate_max < fp->rate_max)
  654. runtime->hw.rate_max = fp->rate_max;
  655. if (runtime->hw.channels_min > fp->channels)
  656. runtime->hw.channels_min = fp->channels;
  657. if (runtime->hw.channels_max < fp->channels)
  658. runtime->hw.channels_max = fp->channels;
  659. if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  660. /* FIXME: there might be more than one audio formats... */
  661. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  662. fp->frame_size;
  663. }
  664. pt = 125 * (1 << fp->datainterval);
  665. ptmin = min(ptmin, pt);
  666. }
  667. param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
  668. if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  669. /* full speed devices have fixed data packet interval */
  670. ptmin = 1000;
  671. if (ptmin == 1000)
  672. /* if period time doesn't go below 1 ms, no rules needed */
  673. param_period_time_if_needed = -1;
  674. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  675. ptmin, UINT_MAX);
  676. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  677. hw_rule_rate, subs,
  678. SNDRV_PCM_HW_PARAM_FORMAT,
  679. SNDRV_PCM_HW_PARAM_CHANNELS,
  680. param_period_time_if_needed,
  681. -1)) < 0)
  682. return err;
  683. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  684. hw_rule_channels, subs,
  685. SNDRV_PCM_HW_PARAM_FORMAT,
  686. SNDRV_PCM_HW_PARAM_RATE,
  687. param_period_time_if_needed,
  688. -1)) < 0)
  689. return err;
  690. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  691. hw_rule_format, subs,
  692. SNDRV_PCM_HW_PARAM_RATE,
  693. SNDRV_PCM_HW_PARAM_CHANNELS,
  694. param_period_time_if_needed,
  695. -1)) < 0)
  696. return err;
  697. if (param_period_time_if_needed >= 0) {
  698. err = snd_pcm_hw_rule_add(runtime, 0,
  699. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  700. hw_rule_period_time, subs,
  701. SNDRV_PCM_HW_PARAM_FORMAT,
  702. SNDRV_PCM_HW_PARAM_CHANNELS,
  703. SNDRV_PCM_HW_PARAM_RATE,
  704. -1);
  705. if (err < 0)
  706. return err;
  707. }
  708. if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
  709. return err;
  710. return 0;
  711. }
  712. static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
  713. {
  714. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  715. struct snd_pcm_runtime *runtime = substream->runtime;
  716. struct snd_usb_substream *subs = &as->substream[direction];
  717. subs->interface = -1;
  718. subs->altset_idx = 0;
  719. runtime->hw = snd_usb_hardware;
  720. runtime->private_data = subs;
  721. subs->pcm_substream = substream;
  722. return setup_hw_info(runtime, subs);
  723. }
  724. static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
  725. {
  726. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  727. struct snd_usb_substream *subs = &as->substream[direction];
  728. if (!as->chip->shutdown && subs->interface >= 0) {
  729. usb_set_interface(subs->dev, subs->interface, 0);
  730. subs->interface = -1;
  731. }
  732. subs->pcm_substream = NULL;
  733. return 0;
  734. }
  735. static int snd_usb_playback_open(struct snd_pcm_substream *substream)
  736. {
  737. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
  738. }
  739. static int snd_usb_playback_close(struct snd_pcm_substream *substream)
  740. {
  741. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
  742. }
  743. static int snd_usb_capture_open(struct snd_pcm_substream *substream)
  744. {
  745. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
  746. }
  747. static int snd_usb_capture_close(struct snd_pcm_substream *substream)
  748. {
  749. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  750. }
  751. static struct snd_pcm_ops snd_usb_playback_ops = {
  752. .open = snd_usb_playback_open,
  753. .close = snd_usb_playback_close,
  754. .ioctl = snd_pcm_lib_ioctl,
  755. .hw_params = snd_usb_hw_params,
  756. .hw_free = snd_usb_hw_free,
  757. .prepare = snd_usb_pcm_prepare,
  758. .trigger = snd_usb_substream_playback_trigger,
  759. .pointer = snd_usb_pcm_pointer,
  760. .page = snd_pcm_lib_get_vmalloc_page,
  761. .mmap = snd_pcm_lib_mmap_vmalloc,
  762. };
  763. static struct snd_pcm_ops snd_usb_capture_ops = {
  764. .open = snd_usb_capture_open,
  765. .close = snd_usb_capture_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_capture_trigger,
  771. .pointer = snd_usb_pcm_pointer,
  772. .page = snd_pcm_lib_get_vmalloc_page,
  773. .mmap = snd_pcm_lib_mmap_vmalloc,
  774. };
  775. void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
  776. {
  777. snd_pcm_set_ops(pcm, stream,
  778. stream == SNDRV_PCM_STREAM_PLAYBACK ?
  779. &snd_usb_playback_ops : &snd_usb_capture_ops);
  780. }