pcm.c 28 KB

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