pcm.c 25 KB

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