pcm.c 27 KB

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