pcm.c 27 KB

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