pcm.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  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/bitrev.h>
  19. #include <linux/ratelimit.h>
  20. #include <linux/usb.h>
  21. #include <linux/usb/audio.h>
  22. #include <linux/usb/audio-v2.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include "usbaudio.h"
  27. #include "card.h"
  28. #include "quirks.h"
  29. #include "debug.h"
  30. #include "endpoint.h"
  31. #include "helper.h"
  32. #include "pcm.h"
  33. #include "clock.h"
  34. #include "power.h"
  35. #define SUBSTREAM_FLAG_DATA_EP_STARTED 0
  36. #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
  37. /* return the estimated delay based on USB frame counters */
  38. snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
  39. unsigned int rate)
  40. {
  41. int current_frame_number;
  42. int frame_diff;
  43. int est_delay;
  44. if (!subs->last_delay)
  45. return 0; /* short path */
  46. current_frame_number = usb_get_current_frame_number(subs->dev);
  47. /*
  48. * HCD implementations use different widths, use lower 8 bits.
  49. * The delay will be managed up to 256ms, which is more than
  50. * enough
  51. */
  52. frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
  53. /* Approximation based on number of samples per USB frame (ms),
  54. some truncation for 44.1 but the estimate is good enough */
  55. est_delay = frame_diff * rate / 1000;
  56. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
  57. est_delay = subs->last_delay - est_delay;
  58. else
  59. est_delay = subs->last_delay + est_delay;
  60. if (est_delay < 0)
  61. est_delay = 0;
  62. return est_delay;
  63. }
  64. /*
  65. * return the current pcm pointer. just based on the hwptr_done value.
  66. */
  67. static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
  68. {
  69. struct snd_usb_substream *subs;
  70. unsigned int hwptr_done;
  71. subs = (struct snd_usb_substream *)substream->runtime->private_data;
  72. if (subs->stream->chip->shutdown)
  73. return SNDRV_PCM_POS_XRUN;
  74. spin_lock(&subs->lock);
  75. hwptr_done = subs->hwptr_done;
  76. substream->runtime->delay = snd_usb_pcm_delay(subs,
  77. substream->runtime->rate);
  78. spin_unlock(&subs->lock);
  79. return hwptr_done / (substream->runtime->frame_bits >> 3);
  80. }
  81. /*
  82. * find a matching audio format
  83. */
  84. static struct audioformat *find_format(struct snd_usb_substream *subs)
  85. {
  86. struct audioformat *fp;
  87. struct audioformat *found = NULL;
  88. int cur_attr = 0, attr;
  89. list_for_each_entry(fp, &subs->fmt_list, list) {
  90. if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
  91. continue;
  92. if (fp->channels != subs->channels)
  93. continue;
  94. if (subs->cur_rate < fp->rate_min ||
  95. subs->cur_rate > fp->rate_max)
  96. continue;
  97. if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
  98. unsigned int i;
  99. for (i = 0; i < fp->nr_rates; i++)
  100. if (fp->rate_table[i] == subs->cur_rate)
  101. break;
  102. if (i >= fp->nr_rates)
  103. continue;
  104. }
  105. attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
  106. if (! found) {
  107. found = fp;
  108. cur_attr = attr;
  109. continue;
  110. }
  111. /* avoid async out and adaptive in if the other method
  112. * supports the same format.
  113. * this is a workaround for the case like
  114. * M-audio audiophile USB.
  115. */
  116. if (attr != cur_attr) {
  117. if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
  118. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  119. (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  120. subs->direction == SNDRV_PCM_STREAM_CAPTURE))
  121. continue;
  122. if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
  123. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  124. (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  125. subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
  126. found = fp;
  127. cur_attr = attr;
  128. continue;
  129. }
  130. }
  131. /* find the format with the largest max. packet size */
  132. if (fp->maxpacksize > found->maxpacksize) {
  133. found = fp;
  134. cur_attr = attr;
  135. }
  136. }
  137. return found;
  138. }
  139. static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
  140. struct usb_host_interface *alts,
  141. struct audioformat *fmt)
  142. {
  143. struct usb_device *dev = chip->dev;
  144. unsigned int ep;
  145. unsigned char data[1];
  146. int err;
  147. ep = get_endpoint(alts, 0)->bEndpointAddress;
  148. data[0] = 1;
  149. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  150. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  151. UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
  152. data, sizeof(data))) < 0) {
  153. snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
  154. dev->devnum, iface, ep);
  155. return err;
  156. }
  157. return 0;
  158. }
  159. static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
  160. struct usb_host_interface *alts,
  161. struct audioformat *fmt)
  162. {
  163. struct usb_device *dev = chip->dev;
  164. unsigned char data[1];
  165. int err;
  166. data[0] = 1;
  167. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
  168. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  169. UAC2_EP_CS_PITCH << 8, 0,
  170. data, sizeof(data))) < 0) {
  171. snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
  172. dev->devnum, iface, fmt->altsetting);
  173. return err;
  174. }
  175. return 0;
  176. }
  177. /*
  178. * initialize the pitch control and sample rate
  179. */
  180. int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
  181. struct usb_host_interface *alts,
  182. struct audioformat *fmt)
  183. {
  184. /* if endpoint doesn't have pitch control, bail out */
  185. if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
  186. return 0;
  187. switch (fmt->protocol) {
  188. case UAC_VERSION_1:
  189. default:
  190. return init_pitch_v1(chip, iface, alts, fmt);
  191. case UAC_VERSION_2:
  192. return init_pitch_v2(chip, iface, alts, fmt);
  193. }
  194. }
  195. static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
  196. {
  197. int err;
  198. if (!subs->data_endpoint)
  199. return -EINVAL;
  200. if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
  201. struct snd_usb_endpoint *ep = subs->data_endpoint;
  202. snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
  203. ep->data_subs = subs;
  204. err = snd_usb_endpoint_start(ep, can_sleep);
  205. if (err < 0) {
  206. clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
  207. return err;
  208. }
  209. }
  210. if (subs->sync_endpoint &&
  211. !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
  212. struct snd_usb_endpoint *ep = subs->sync_endpoint;
  213. if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
  214. subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
  215. err = usb_set_interface(subs->dev,
  216. subs->sync_endpoint->iface,
  217. subs->sync_endpoint->alt_idx);
  218. if (err < 0) {
  219. snd_printk(KERN_ERR
  220. "%d:%d:%d: cannot set interface (%d)\n",
  221. subs->dev->devnum,
  222. subs->sync_endpoint->iface,
  223. subs->sync_endpoint->alt_idx, err);
  224. return -EIO;
  225. }
  226. }
  227. snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
  228. ep->sync_slave = subs->data_endpoint;
  229. err = snd_usb_endpoint_start(ep, can_sleep);
  230. if (err < 0) {
  231. clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
  232. return err;
  233. }
  234. }
  235. return 0;
  236. }
  237. static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
  238. {
  239. if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
  240. snd_usb_endpoint_stop(subs->sync_endpoint);
  241. if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
  242. snd_usb_endpoint_stop(subs->data_endpoint);
  243. if (wait) {
  244. snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
  245. snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
  246. }
  247. }
  248. static int deactivate_endpoints(struct snd_usb_substream *subs)
  249. {
  250. int reta, retb;
  251. reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
  252. retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
  253. if (reta < 0)
  254. return reta;
  255. if (retb < 0)
  256. return retb;
  257. return 0;
  258. }
  259. static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
  260. unsigned int altsetting,
  261. struct usb_host_interface **alts,
  262. unsigned int *ep)
  263. {
  264. struct usb_interface *iface;
  265. struct usb_interface_descriptor *altsd;
  266. struct usb_endpoint_descriptor *epd;
  267. iface = usb_ifnum_to_if(dev, ifnum);
  268. if (!iface || iface->num_altsetting < altsetting + 1)
  269. return -ENOENT;
  270. *alts = &iface->altsetting[altsetting];
  271. altsd = get_iface_desc(*alts);
  272. if (altsd->bAlternateSetting != altsetting ||
  273. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
  274. (altsd->bInterfaceSubClass != 2 &&
  275. altsd->bInterfaceProtocol != 2 ) ||
  276. altsd->bNumEndpoints < 1)
  277. return -ENOENT;
  278. epd = get_endpoint(*alts, 0);
  279. if (!usb_endpoint_is_isoc_in(epd) ||
  280. (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
  281. USB_ENDPOINT_USAGE_IMPLICIT_FB)
  282. return -ENOENT;
  283. *ep = epd->bEndpointAddress;
  284. return 0;
  285. }
  286. static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
  287. struct usb_device *dev,
  288. struct usb_interface_descriptor *altsd,
  289. unsigned int attr)
  290. {
  291. struct usb_host_interface *alts;
  292. struct usb_interface *iface;
  293. int implicit_fb = 0;
  294. unsigned int ep;
  295. /* Implicit feedback sync EPs consumers are always playback EPs */
  296. if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
  297. return 0;
  298. switch (subs->stream->chip->usb_id) {
  299. case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
  300. case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
  301. implicit_fb = 1;
  302. ep = 0x81;
  303. iface = usb_ifnum_to_if(dev, 3);
  304. if (!iface || iface->num_altsetting == 0)
  305. return -EINVAL;
  306. alts = &iface->altsetting[1];
  307. goto add_sync_ep;
  308. break;
  309. case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
  310. case USB_ID(0x0763, 0x2081):
  311. implicit_fb = 1;
  312. ep = 0x81;
  313. iface = usb_ifnum_to_if(dev, 2);
  314. if (!iface || iface->num_altsetting == 0)
  315. return -EINVAL;
  316. alts = &iface->altsetting[1];
  317. goto add_sync_ep;
  318. }
  319. if (attr == USB_ENDPOINT_SYNC_ASYNC &&
  320. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
  321. altsd->bInterfaceProtocol == 2 &&
  322. altsd->bNumEndpoints == 1 &&
  323. USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
  324. search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
  325. altsd->bAlternateSetting,
  326. &alts, &ep) >= 0) {
  327. implicit_fb = 1;
  328. goto add_sync_ep;
  329. }
  330. /* No quirk */
  331. return 0;
  332. add_sync_ep:
  333. subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  334. alts, ep, !subs->direction,
  335. implicit_fb ?
  336. SND_USB_ENDPOINT_TYPE_DATA :
  337. SND_USB_ENDPOINT_TYPE_SYNC);
  338. if (!subs->sync_endpoint)
  339. return -EINVAL;
  340. subs->data_endpoint->sync_master = subs->sync_endpoint;
  341. return 0;
  342. }
  343. static int set_sync_endpoint(struct snd_usb_substream *subs,
  344. struct audioformat *fmt,
  345. struct usb_device *dev,
  346. struct usb_host_interface *alts,
  347. struct usb_interface_descriptor *altsd)
  348. {
  349. int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  350. unsigned int ep, attr;
  351. bool implicit_fb;
  352. int err;
  353. /* we need a sync pipe in async OUT or adaptive IN mode */
  354. /* check the number of EP, since some devices have broken
  355. * descriptors which fool us. if it has only one EP,
  356. * assume it as adaptive-out or sync-in.
  357. */
  358. attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
  359. err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
  360. if (err < 0)
  361. return err;
  362. if (altsd->bNumEndpoints < 2)
  363. return 0;
  364. if ((is_playback && attr != USB_ENDPOINT_SYNC_ASYNC) ||
  365. (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
  366. return 0;
  367. /* check sync-pipe endpoint */
  368. /* ... and check descriptor size before accessing bSynchAddress
  369. because there is a version of the SB Audigy 2 NX firmware lacking
  370. the audio fields in the endpoint descriptors */
  371. if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
  372. (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  373. get_endpoint(alts, 1)->bSynchAddress != 0)) {
  374. snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
  375. dev->devnum, fmt->iface, fmt->altsetting,
  376. get_endpoint(alts, 1)->bmAttributes,
  377. get_endpoint(alts, 1)->bLength,
  378. get_endpoint(alts, 1)->bSynchAddress);
  379. return -EINVAL;
  380. }
  381. ep = get_endpoint(alts, 1)->bEndpointAddress;
  382. if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  383. ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
  384. (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
  385. snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
  386. dev->devnum, fmt->iface, fmt->altsetting,
  387. is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
  388. return -EINVAL;
  389. }
  390. implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
  391. == USB_ENDPOINT_USAGE_IMPLICIT_FB;
  392. subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  393. alts, ep, !subs->direction,
  394. implicit_fb ?
  395. SND_USB_ENDPOINT_TYPE_DATA :
  396. SND_USB_ENDPOINT_TYPE_SYNC);
  397. if (!subs->sync_endpoint)
  398. return -EINVAL;
  399. subs->data_endpoint->sync_master = subs->sync_endpoint;
  400. return 0;
  401. }
  402. /*
  403. * find a matching format and set up the interface
  404. */
  405. static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
  406. {
  407. struct usb_device *dev = subs->dev;
  408. struct usb_host_interface *alts;
  409. struct usb_interface_descriptor *altsd;
  410. struct usb_interface *iface;
  411. int err;
  412. iface = usb_ifnum_to_if(dev, fmt->iface);
  413. if (WARN_ON(!iface))
  414. return -EINVAL;
  415. alts = &iface->altsetting[fmt->altset_idx];
  416. altsd = get_iface_desc(alts);
  417. if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
  418. return -EINVAL;
  419. if (fmt == subs->cur_audiofmt)
  420. return 0;
  421. /* close the old interface */
  422. if (subs->interface >= 0 && subs->interface != fmt->iface) {
  423. err = usb_set_interface(subs->dev, subs->interface, 0);
  424. if (err < 0) {
  425. snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
  426. dev->devnum, fmt->iface, fmt->altsetting, err);
  427. return -EIO;
  428. }
  429. subs->interface = -1;
  430. subs->altset_idx = 0;
  431. }
  432. /* set interface */
  433. if (subs->interface != fmt->iface ||
  434. subs->altset_idx != fmt->altset_idx) {
  435. err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
  436. if (err < 0) {
  437. snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
  438. dev->devnum, fmt->iface, fmt->altsetting, err);
  439. return -EIO;
  440. }
  441. snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
  442. fmt->iface, fmt->altsetting);
  443. subs->interface = fmt->iface;
  444. subs->altset_idx = fmt->altset_idx;
  445. snd_usb_set_interface_quirk(dev);
  446. }
  447. subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  448. alts, fmt->endpoint, subs->direction,
  449. SND_USB_ENDPOINT_TYPE_DATA);
  450. if (!subs->data_endpoint)
  451. return -EINVAL;
  452. err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
  453. if (err < 0)
  454. return err;
  455. err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
  456. if (err < 0)
  457. return err;
  458. subs->cur_audiofmt = fmt;
  459. snd_usb_set_format_quirk(subs, fmt);
  460. return 0;
  461. }
  462. /*
  463. * Return the score of matching two audioformats.
  464. * Veto the audioformat if:
  465. * - It has no channels for some reason.
  466. * - Requested PCM format is not supported.
  467. * - Requested sample rate is not supported.
  468. */
  469. static int match_endpoint_audioformats(struct audioformat *fp,
  470. struct audioformat *match, int rate,
  471. snd_pcm_format_t pcm_format)
  472. {
  473. int i;
  474. int score = 0;
  475. if (fp->channels < 1) {
  476. snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
  477. return 0;
  478. }
  479. if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
  480. snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
  481. fp, pcm_format);
  482. return 0;
  483. }
  484. for (i = 0; i < fp->nr_rates; i++) {
  485. if (fp->rate_table[i] == rate) {
  486. score++;
  487. break;
  488. }
  489. }
  490. if (!score) {
  491. snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
  492. fp, rate);
  493. return 0;
  494. }
  495. if (fp->channels == match->channels)
  496. score++;
  497. snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
  498. return score;
  499. }
  500. /*
  501. * Configure the sync ep using the rate and pcm format of the data ep.
  502. */
  503. static int configure_sync_endpoint(struct snd_usb_substream *subs)
  504. {
  505. int ret;
  506. struct audioformat *fp;
  507. struct audioformat *sync_fp = NULL;
  508. int cur_score = 0;
  509. int sync_period_bytes = subs->period_bytes;
  510. struct snd_usb_substream *sync_subs =
  511. &subs->stream->substream[subs->direction ^ 1];
  512. if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
  513. !subs->stream)
  514. return snd_usb_endpoint_set_params(subs->sync_endpoint,
  515. subs->pcm_format,
  516. subs->channels,
  517. subs->period_bytes,
  518. subs->cur_rate,
  519. subs->cur_audiofmt,
  520. NULL);
  521. /* Try to find the best matching audioformat. */
  522. list_for_each_entry(fp, &sync_subs->fmt_list, list) {
  523. int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
  524. subs->cur_rate, subs->pcm_format);
  525. if (score > cur_score) {
  526. sync_fp = fp;
  527. cur_score = score;
  528. }
  529. }
  530. if (unlikely(sync_fp == NULL)) {
  531. snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
  532. __func__, sync_subs->ep_num);
  533. return -EINVAL;
  534. }
  535. /*
  536. * Recalculate the period bytes if channel number differ between
  537. * data and sync ep audioformat.
  538. */
  539. if (sync_fp->channels != subs->channels) {
  540. sync_period_bytes = (subs->period_bytes / subs->channels) *
  541. sync_fp->channels;
  542. snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
  543. __func__, subs->period_bytes, sync_period_bytes);
  544. }
  545. ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
  546. subs->pcm_format,
  547. sync_fp->channels,
  548. sync_period_bytes,
  549. subs->cur_rate,
  550. sync_fp,
  551. NULL);
  552. return ret;
  553. }
  554. /*
  555. * configure endpoint params
  556. *
  557. * called during initial setup and upon resume
  558. */
  559. static int configure_endpoint(struct snd_usb_substream *subs)
  560. {
  561. int ret;
  562. /* format changed */
  563. stop_endpoints(subs, true);
  564. ret = snd_usb_endpoint_set_params(subs->data_endpoint,
  565. subs->pcm_format,
  566. subs->channels,
  567. subs->period_bytes,
  568. subs->cur_rate,
  569. subs->cur_audiofmt,
  570. subs->sync_endpoint);
  571. if (ret < 0)
  572. return ret;
  573. if (subs->sync_endpoint)
  574. ret = configure_sync_endpoint(subs);
  575. return ret;
  576. }
  577. /*
  578. * hw_params callback
  579. *
  580. * allocate a buffer and set the given audio format.
  581. *
  582. * so far we use a physically linear buffer although packetize transfer
  583. * doesn't need a continuous area.
  584. * if sg buffer is supported on the later version of alsa, we'll follow
  585. * that.
  586. */
  587. static int snd_usb_hw_params(struct snd_pcm_substream *substream,
  588. struct snd_pcm_hw_params *hw_params)
  589. {
  590. struct snd_usb_substream *subs = substream->runtime->private_data;
  591. struct audioformat *fmt;
  592. int ret;
  593. ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  594. params_buffer_bytes(hw_params));
  595. if (ret < 0)
  596. return ret;
  597. subs->pcm_format = params_format(hw_params);
  598. subs->period_bytes = params_period_bytes(hw_params);
  599. subs->channels = params_channels(hw_params);
  600. subs->cur_rate = params_rate(hw_params);
  601. fmt = find_format(subs);
  602. if (!fmt) {
  603. snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
  604. subs->pcm_format, subs->cur_rate, subs->channels);
  605. return -EINVAL;
  606. }
  607. down_read(&subs->stream->chip->shutdown_rwsem);
  608. if (subs->stream->chip->shutdown)
  609. ret = -ENODEV;
  610. else
  611. ret = set_format(subs, fmt);
  612. up_read(&subs->stream->chip->shutdown_rwsem);
  613. if (ret < 0)
  614. return ret;
  615. subs->interface = fmt->iface;
  616. subs->altset_idx = fmt->altset_idx;
  617. subs->need_setup_ep = true;
  618. return 0;
  619. }
  620. /*
  621. * hw_free callback
  622. *
  623. * reset the audio format and release the buffer
  624. */
  625. static int snd_usb_hw_free(struct snd_pcm_substream *substream)
  626. {
  627. struct snd_usb_substream *subs = substream->runtime->private_data;
  628. subs->cur_audiofmt = NULL;
  629. subs->cur_rate = 0;
  630. subs->period_bytes = 0;
  631. down_read(&subs->stream->chip->shutdown_rwsem);
  632. if (!subs->stream->chip->shutdown) {
  633. stop_endpoints(subs, true);
  634. deactivate_endpoints(subs);
  635. }
  636. up_read(&subs->stream->chip->shutdown_rwsem);
  637. return snd_pcm_lib_free_vmalloc_buffer(substream);
  638. }
  639. /*
  640. * prepare callback
  641. *
  642. * only a few subtle things...
  643. */
  644. static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
  645. {
  646. struct snd_pcm_runtime *runtime = substream->runtime;
  647. struct snd_usb_substream *subs = runtime->private_data;
  648. struct usb_host_interface *alts;
  649. struct usb_interface *iface;
  650. int ret;
  651. if (! subs->cur_audiofmt) {
  652. snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
  653. return -ENXIO;
  654. }
  655. down_read(&subs->stream->chip->shutdown_rwsem);
  656. if (subs->stream->chip->shutdown) {
  657. ret = -ENODEV;
  658. goto unlock;
  659. }
  660. if (snd_BUG_ON(!subs->data_endpoint)) {
  661. ret = -EIO;
  662. goto unlock;
  663. }
  664. snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
  665. snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
  666. ret = set_format(subs, subs->cur_audiofmt);
  667. if (ret < 0)
  668. goto unlock;
  669. iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
  670. alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
  671. ret = snd_usb_init_sample_rate(subs->stream->chip,
  672. subs->cur_audiofmt->iface,
  673. alts,
  674. subs->cur_audiofmt,
  675. subs->cur_rate);
  676. if (ret < 0)
  677. goto unlock;
  678. if (subs->need_setup_ep) {
  679. ret = configure_endpoint(subs);
  680. if (ret < 0)
  681. goto unlock;
  682. subs->need_setup_ep = false;
  683. }
  684. /* some unit conversions in runtime */
  685. subs->data_endpoint->maxframesize =
  686. bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
  687. subs->data_endpoint->curframesize =
  688. bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
  689. /* reset the pointer */
  690. subs->hwptr_done = 0;
  691. subs->transfer_done = 0;
  692. subs->last_delay = 0;
  693. subs->last_frame_number = 0;
  694. runtime->delay = 0;
  695. /* for playback, submit the URBs now; otherwise, the first hwptr_done
  696. * updates for all URBs would happen at the same time when starting */
  697. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
  698. ret = start_endpoints(subs, true);
  699. unlock:
  700. up_read(&subs->stream->chip->shutdown_rwsem);
  701. return ret;
  702. }
  703. static struct snd_pcm_hardware snd_usb_hardware =
  704. {
  705. .info = SNDRV_PCM_INFO_MMAP |
  706. SNDRV_PCM_INFO_MMAP_VALID |
  707. SNDRV_PCM_INFO_BATCH |
  708. SNDRV_PCM_INFO_INTERLEAVED |
  709. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  710. SNDRV_PCM_INFO_PAUSE,
  711. .buffer_bytes_max = 1024 * 1024,
  712. .period_bytes_min = 64,
  713. .period_bytes_max = 512 * 1024,
  714. .periods_min = 2,
  715. .periods_max = 1024,
  716. };
  717. static int hw_check_valid_format(struct snd_usb_substream *subs,
  718. struct snd_pcm_hw_params *params,
  719. struct audioformat *fp)
  720. {
  721. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  722. struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  723. struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  724. struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  725. struct snd_mask check_fmts;
  726. unsigned int ptime;
  727. /* check the format */
  728. snd_mask_none(&check_fmts);
  729. check_fmts.bits[0] = (u32)fp->formats;
  730. check_fmts.bits[1] = (u32)(fp->formats >> 32);
  731. snd_mask_intersect(&check_fmts, fmts);
  732. if (snd_mask_empty(&check_fmts)) {
  733. hwc_debug(" > check: no supported format %d\n", fp->format);
  734. return 0;
  735. }
  736. /* check the channels */
  737. if (fp->channels < ct->min || fp->channels > ct->max) {
  738. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  739. return 0;
  740. }
  741. /* check the rate is within the range */
  742. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  743. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  744. return 0;
  745. }
  746. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  747. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  748. return 0;
  749. }
  750. /* check whether the period time is >= the data packet interval */
  751. if (subs->speed != USB_SPEED_FULL) {
  752. ptime = 125 * (1 << fp->datainterval);
  753. if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  754. hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
  755. return 0;
  756. }
  757. }
  758. return 1;
  759. }
  760. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  761. struct snd_pcm_hw_rule *rule)
  762. {
  763. struct snd_usb_substream *subs = rule->private;
  764. struct audioformat *fp;
  765. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  766. unsigned int rmin, rmax;
  767. int changed;
  768. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  769. changed = 0;
  770. rmin = rmax = 0;
  771. list_for_each_entry(fp, &subs->fmt_list, list) {
  772. if (!hw_check_valid_format(subs, params, fp))
  773. continue;
  774. if (changed++) {
  775. if (rmin > fp->rate_min)
  776. rmin = fp->rate_min;
  777. if (rmax < fp->rate_max)
  778. rmax = fp->rate_max;
  779. } else {
  780. rmin = fp->rate_min;
  781. rmax = fp->rate_max;
  782. }
  783. }
  784. if (!changed) {
  785. hwc_debug(" --> get empty\n");
  786. it->empty = 1;
  787. return -EINVAL;
  788. }
  789. changed = 0;
  790. if (it->min < rmin) {
  791. it->min = rmin;
  792. it->openmin = 0;
  793. changed = 1;
  794. }
  795. if (it->max > rmax) {
  796. it->max = rmax;
  797. it->openmax = 0;
  798. changed = 1;
  799. }
  800. if (snd_interval_checkempty(it)) {
  801. it->empty = 1;
  802. return -EINVAL;
  803. }
  804. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  805. return changed;
  806. }
  807. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  808. struct snd_pcm_hw_rule *rule)
  809. {
  810. struct snd_usb_substream *subs = rule->private;
  811. struct audioformat *fp;
  812. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  813. unsigned int rmin, rmax;
  814. int changed;
  815. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  816. changed = 0;
  817. rmin = rmax = 0;
  818. list_for_each_entry(fp, &subs->fmt_list, list) {
  819. if (!hw_check_valid_format(subs, params, fp))
  820. continue;
  821. if (changed++) {
  822. if (rmin > fp->channels)
  823. rmin = fp->channels;
  824. if (rmax < fp->channels)
  825. rmax = fp->channels;
  826. } else {
  827. rmin = fp->channels;
  828. rmax = fp->channels;
  829. }
  830. }
  831. if (!changed) {
  832. hwc_debug(" --> get empty\n");
  833. it->empty = 1;
  834. return -EINVAL;
  835. }
  836. changed = 0;
  837. if (it->min < rmin) {
  838. it->min = rmin;
  839. it->openmin = 0;
  840. changed = 1;
  841. }
  842. if (it->max > rmax) {
  843. it->max = rmax;
  844. it->openmax = 0;
  845. changed = 1;
  846. }
  847. if (snd_interval_checkempty(it)) {
  848. it->empty = 1;
  849. return -EINVAL;
  850. }
  851. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  852. return changed;
  853. }
  854. static int hw_rule_format(struct snd_pcm_hw_params *params,
  855. struct snd_pcm_hw_rule *rule)
  856. {
  857. struct snd_usb_substream *subs = rule->private;
  858. struct audioformat *fp;
  859. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  860. u64 fbits;
  861. u32 oldbits[2];
  862. int changed;
  863. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  864. fbits = 0;
  865. list_for_each_entry(fp, &subs->fmt_list, list) {
  866. if (!hw_check_valid_format(subs, params, fp))
  867. continue;
  868. fbits |= fp->formats;
  869. }
  870. oldbits[0] = fmt->bits[0];
  871. oldbits[1] = fmt->bits[1];
  872. fmt->bits[0] &= (u32)fbits;
  873. fmt->bits[1] &= (u32)(fbits >> 32);
  874. if (!fmt->bits[0] && !fmt->bits[1]) {
  875. hwc_debug(" --> get empty\n");
  876. return -EINVAL;
  877. }
  878. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  879. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  880. return changed;
  881. }
  882. static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  883. struct snd_pcm_hw_rule *rule)
  884. {
  885. struct snd_usb_substream *subs = rule->private;
  886. struct audioformat *fp;
  887. struct snd_interval *it;
  888. unsigned char min_datainterval;
  889. unsigned int pmin;
  890. int changed;
  891. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  892. hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
  893. min_datainterval = 0xff;
  894. list_for_each_entry(fp, &subs->fmt_list, list) {
  895. if (!hw_check_valid_format(subs, params, fp))
  896. continue;
  897. min_datainterval = min(min_datainterval, fp->datainterval);
  898. }
  899. if (min_datainterval == 0xff) {
  900. hwc_debug(" --> get empty\n");
  901. it->empty = 1;
  902. return -EINVAL;
  903. }
  904. pmin = 125 * (1 << min_datainterval);
  905. changed = 0;
  906. if (it->min < pmin) {
  907. it->min = pmin;
  908. it->openmin = 0;
  909. changed = 1;
  910. }
  911. if (snd_interval_checkempty(it)) {
  912. it->empty = 1;
  913. return -EINVAL;
  914. }
  915. hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
  916. return changed;
  917. }
  918. /*
  919. * If the device supports unusual bit rates, does the request meet these?
  920. */
  921. static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
  922. struct snd_usb_substream *subs)
  923. {
  924. struct audioformat *fp;
  925. int *rate_list;
  926. int count = 0, needs_knot = 0;
  927. int err;
  928. kfree(subs->rate_list.list);
  929. subs->rate_list.list = NULL;
  930. list_for_each_entry(fp, &subs->fmt_list, list) {
  931. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
  932. return 0;
  933. count += fp->nr_rates;
  934. if (fp->rates & SNDRV_PCM_RATE_KNOT)
  935. needs_knot = 1;
  936. }
  937. if (!needs_knot)
  938. return 0;
  939. subs->rate_list.list = rate_list =
  940. kmalloc(sizeof(int) * count, GFP_KERNEL);
  941. if (!subs->rate_list.list)
  942. return -ENOMEM;
  943. subs->rate_list.count = count;
  944. subs->rate_list.mask = 0;
  945. count = 0;
  946. list_for_each_entry(fp, &subs->fmt_list, list) {
  947. int i;
  948. for (i = 0; i < fp->nr_rates; i++)
  949. rate_list[count++] = fp->rate_table[i];
  950. }
  951. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  952. &subs->rate_list);
  953. if (err < 0)
  954. return err;
  955. return 0;
  956. }
  957. /*
  958. * set up the runtime hardware information.
  959. */
  960. static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  961. {
  962. struct audioformat *fp;
  963. unsigned int pt, ptmin;
  964. int param_period_time_if_needed;
  965. int err;
  966. runtime->hw.formats = subs->formats;
  967. runtime->hw.rate_min = 0x7fffffff;
  968. runtime->hw.rate_max = 0;
  969. runtime->hw.channels_min = 256;
  970. runtime->hw.channels_max = 0;
  971. runtime->hw.rates = 0;
  972. ptmin = UINT_MAX;
  973. /* check min/max rates and channels */
  974. list_for_each_entry(fp, &subs->fmt_list, list) {
  975. runtime->hw.rates |= fp->rates;
  976. if (runtime->hw.rate_min > fp->rate_min)
  977. runtime->hw.rate_min = fp->rate_min;
  978. if (runtime->hw.rate_max < fp->rate_max)
  979. runtime->hw.rate_max = fp->rate_max;
  980. if (runtime->hw.channels_min > fp->channels)
  981. runtime->hw.channels_min = fp->channels;
  982. if (runtime->hw.channels_max < fp->channels)
  983. runtime->hw.channels_max = fp->channels;
  984. if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  985. /* FIXME: there might be more than one audio formats... */
  986. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  987. fp->frame_size;
  988. }
  989. pt = 125 * (1 << fp->datainterval);
  990. ptmin = min(ptmin, pt);
  991. }
  992. err = snd_usb_autoresume(subs->stream->chip);
  993. if (err < 0)
  994. return err;
  995. param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
  996. if (subs->speed == USB_SPEED_FULL)
  997. /* full speed devices have fixed data packet interval */
  998. ptmin = 1000;
  999. if (ptmin == 1000)
  1000. /* if period time doesn't go below 1 ms, no rules needed */
  1001. param_period_time_if_needed = -1;
  1002. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1003. ptmin, UINT_MAX);
  1004. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1005. hw_rule_rate, subs,
  1006. SNDRV_PCM_HW_PARAM_FORMAT,
  1007. SNDRV_PCM_HW_PARAM_CHANNELS,
  1008. param_period_time_if_needed,
  1009. -1)) < 0)
  1010. goto rep_err;
  1011. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  1012. hw_rule_channels, subs,
  1013. SNDRV_PCM_HW_PARAM_FORMAT,
  1014. SNDRV_PCM_HW_PARAM_RATE,
  1015. param_period_time_if_needed,
  1016. -1)) < 0)
  1017. goto rep_err;
  1018. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  1019. hw_rule_format, subs,
  1020. SNDRV_PCM_HW_PARAM_RATE,
  1021. SNDRV_PCM_HW_PARAM_CHANNELS,
  1022. param_period_time_if_needed,
  1023. -1)) < 0)
  1024. goto rep_err;
  1025. if (param_period_time_if_needed >= 0) {
  1026. err = snd_pcm_hw_rule_add(runtime, 0,
  1027. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1028. hw_rule_period_time, subs,
  1029. SNDRV_PCM_HW_PARAM_FORMAT,
  1030. SNDRV_PCM_HW_PARAM_CHANNELS,
  1031. SNDRV_PCM_HW_PARAM_RATE,
  1032. -1);
  1033. if (err < 0)
  1034. goto rep_err;
  1035. }
  1036. if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
  1037. goto rep_err;
  1038. return 0;
  1039. rep_err:
  1040. snd_usb_autosuspend(subs->stream->chip);
  1041. return err;
  1042. }
  1043. static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
  1044. {
  1045. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  1046. struct snd_pcm_runtime *runtime = substream->runtime;
  1047. struct snd_usb_substream *subs = &as->substream[direction];
  1048. subs->interface = -1;
  1049. subs->altset_idx = 0;
  1050. runtime->hw = snd_usb_hardware;
  1051. runtime->private_data = subs;
  1052. subs->pcm_substream = substream;
  1053. /* runtime PM is also done there */
  1054. /* initialize DSD/DOP context */
  1055. subs->dsd_dop.byte_idx = 0;
  1056. subs->dsd_dop.channel = 0;
  1057. subs->dsd_dop.marker = 1;
  1058. return setup_hw_info(runtime, subs);
  1059. }
  1060. static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
  1061. {
  1062. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  1063. struct snd_usb_substream *subs = &as->substream[direction];
  1064. stop_endpoints(subs, true);
  1065. if (!as->chip->shutdown && subs->interface >= 0) {
  1066. usb_set_interface(subs->dev, subs->interface, 0);
  1067. subs->interface = -1;
  1068. }
  1069. subs->pcm_substream = NULL;
  1070. snd_usb_autosuspend(subs->stream->chip);
  1071. return 0;
  1072. }
  1073. /* Since a URB can handle only a single linear buffer, we must use double
  1074. * buffering when the data to be transferred overflows the buffer boundary.
  1075. * To avoid inconsistencies when updating hwptr_done, we use double buffering
  1076. * for all URBs.
  1077. */
  1078. static void retire_capture_urb(struct snd_usb_substream *subs,
  1079. struct urb *urb)
  1080. {
  1081. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1082. unsigned int stride, frames, bytes, oldptr;
  1083. int i, period_elapsed = 0;
  1084. unsigned long flags;
  1085. unsigned char *cp;
  1086. int current_frame_number;
  1087. /* read frame number here, update pointer in critical section */
  1088. current_frame_number = usb_get_current_frame_number(subs->dev);
  1089. stride = runtime->frame_bits >> 3;
  1090. for (i = 0; i < urb->number_of_packets; i++) {
  1091. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
  1092. if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
  1093. snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
  1094. // continue;
  1095. }
  1096. bytes = urb->iso_frame_desc[i].actual_length;
  1097. frames = bytes / stride;
  1098. if (!subs->txfr_quirk)
  1099. bytes = frames * stride;
  1100. if (bytes % (runtime->sample_bits >> 3) != 0) {
  1101. int oldbytes = bytes;
  1102. bytes = frames * stride;
  1103. snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
  1104. oldbytes, bytes);
  1105. }
  1106. /* update the current pointer */
  1107. spin_lock_irqsave(&subs->lock, flags);
  1108. oldptr = subs->hwptr_done;
  1109. subs->hwptr_done += bytes;
  1110. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1111. subs->hwptr_done -= runtime->buffer_size * stride;
  1112. frames = (bytes + (oldptr % stride)) / stride;
  1113. subs->transfer_done += frames;
  1114. if (subs->transfer_done >= runtime->period_size) {
  1115. subs->transfer_done -= runtime->period_size;
  1116. period_elapsed = 1;
  1117. }
  1118. /* capture delay is by construction limited to one URB,
  1119. * reset delays here
  1120. */
  1121. runtime->delay = subs->last_delay = 0;
  1122. /* realign last_frame_number */
  1123. subs->last_frame_number = current_frame_number;
  1124. subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
  1125. spin_unlock_irqrestore(&subs->lock, flags);
  1126. /* copy a data chunk */
  1127. if (oldptr + bytes > runtime->buffer_size * stride) {
  1128. unsigned int bytes1 =
  1129. runtime->buffer_size * stride - oldptr;
  1130. memcpy(runtime->dma_area + oldptr, cp, bytes1);
  1131. memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  1132. } else {
  1133. memcpy(runtime->dma_area + oldptr, cp, bytes);
  1134. }
  1135. }
  1136. if (period_elapsed)
  1137. snd_pcm_period_elapsed(subs->pcm_substream);
  1138. }
  1139. static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
  1140. struct urb *urb, unsigned int bytes)
  1141. {
  1142. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1143. unsigned int stride = runtime->frame_bits >> 3;
  1144. unsigned int dst_idx = 0;
  1145. unsigned int src_idx = subs->hwptr_done;
  1146. unsigned int wrap = runtime->buffer_size * stride;
  1147. u8 *dst = urb->transfer_buffer;
  1148. u8 *src = runtime->dma_area;
  1149. u8 marker[] = { 0x05, 0xfa };
  1150. /*
  1151. * The DSP DOP format defines a way to transport DSD samples over
  1152. * normal PCM data endpoints. It requires stuffing of marker bytes
  1153. * (0x05 and 0xfa, alternating per sample frame), and then expects
  1154. * 2 additional bytes of actual payload. The whole frame is stored
  1155. * LSB.
  1156. *
  1157. * Hence, for a stereo transport, the buffer layout looks like this,
  1158. * where L refers to left channel samples and R to right.
  1159. *
  1160. * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
  1161. * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
  1162. * .....
  1163. *
  1164. */
  1165. while (bytes--) {
  1166. if (++subs->dsd_dop.byte_idx == 3) {
  1167. /* frame boundary? */
  1168. dst[dst_idx++] = marker[subs->dsd_dop.marker];
  1169. src_idx += 2;
  1170. subs->dsd_dop.byte_idx = 0;
  1171. if (++subs->dsd_dop.channel % runtime->channels == 0) {
  1172. /* alternate the marker */
  1173. subs->dsd_dop.marker++;
  1174. subs->dsd_dop.marker %= ARRAY_SIZE(marker);
  1175. subs->dsd_dop.channel = 0;
  1176. }
  1177. } else {
  1178. /* stuff the DSD payload */
  1179. int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
  1180. if (subs->cur_audiofmt->dsd_bitrev)
  1181. dst[dst_idx++] = bitrev8(src[idx]);
  1182. else
  1183. dst[dst_idx++] = src[idx];
  1184. subs->hwptr_done++;
  1185. }
  1186. }
  1187. }
  1188. static void prepare_playback_urb(struct snd_usb_substream *subs,
  1189. struct urb *urb)
  1190. {
  1191. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1192. struct snd_usb_endpoint *ep = subs->data_endpoint;
  1193. struct snd_urb_ctx *ctx = urb->context;
  1194. unsigned int counts, frames, bytes;
  1195. int i, stride, period_elapsed = 0;
  1196. unsigned long flags;
  1197. stride = runtime->frame_bits >> 3;
  1198. frames = 0;
  1199. urb->number_of_packets = 0;
  1200. spin_lock_irqsave(&subs->lock, flags);
  1201. for (i = 0; i < ctx->packets; i++) {
  1202. if (ctx->packet_size[i])
  1203. counts = ctx->packet_size[i];
  1204. else
  1205. counts = snd_usb_endpoint_next_packet_size(ep);
  1206. /* set up descriptor */
  1207. urb->iso_frame_desc[i].offset = frames * ep->stride;
  1208. urb->iso_frame_desc[i].length = counts * ep->stride;
  1209. frames += counts;
  1210. urb->number_of_packets++;
  1211. subs->transfer_done += counts;
  1212. if (subs->transfer_done >= runtime->period_size) {
  1213. subs->transfer_done -= runtime->period_size;
  1214. period_elapsed = 1;
  1215. if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  1216. if (subs->transfer_done > 0) {
  1217. /* FIXME: fill-max mode is not
  1218. * supported yet */
  1219. frames -= subs->transfer_done;
  1220. counts -= subs->transfer_done;
  1221. urb->iso_frame_desc[i].length =
  1222. counts * ep->stride;
  1223. subs->transfer_done = 0;
  1224. }
  1225. i++;
  1226. if (i < ctx->packets) {
  1227. /* add a transfer delimiter */
  1228. urb->iso_frame_desc[i].offset =
  1229. frames * ep->stride;
  1230. urb->iso_frame_desc[i].length = 0;
  1231. urb->number_of_packets++;
  1232. }
  1233. break;
  1234. }
  1235. }
  1236. if (period_elapsed &&
  1237. !snd_usb_endpoint_implicit_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
  1238. break;
  1239. }
  1240. bytes = frames * ep->stride;
  1241. if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
  1242. subs->cur_audiofmt->dsd_dop)) {
  1243. fill_playback_urb_dsd_dop(subs, urb, bytes);
  1244. } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
  1245. subs->cur_audiofmt->dsd_bitrev)) {
  1246. /* bit-reverse the bytes */
  1247. u8 *buf = urb->transfer_buffer;
  1248. for (i = 0; i < bytes; i++) {
  1249. int idx = (subs->hwptr_done + i)
  1250. % (runtime->buffer_size * stride);
  1251. buf[i] = bitrev8(runtime->dma_area[idx]);
  1252. }
  1253. subs->hwptr_done += bytes;
  1254. } else {
  1255. /* usual PCM */
  1256. if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  1257. /* err, the transferred area goes over buffer boundary. */
  1258. unsigned int bytes1 =
  1259. runtime->buffer_size * stride - subs->hwptr_done;
  1260. memcpy(urb->transfer_buffer,
  1261. runtime->dma_area + subs->hwptr_done, bytes1);
  1262. memcpy(urb->transfer_buffer + bytes1,
  1263. runtime->dma_area, bytes - bytes1);
  1264. } else {
  1265. memcpy(urb->transfer_buffer,
  1266. runtime->dma_area + subs->hwptr_done, bytes);
  1267. }
  1268. subs->hwptr_done += bytes;
  1269. }
  1270. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1271. subs->hwptr_done -= runtime->buffer_size * stride;
  1272. /* update delay with exact number of samples queued */
  1273. runtime->delay = subs->last_delay;
  1274. runtime->delay += frames;
  1275. subs->last_delay = runtime->delay;
  1276. /* realign last_frame_number */
  1277. subs->last_frame_number = usb_get_current_frame_number(subs->dev);
  1278. subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
  1279. spin_unlock_irqrestore(&subs->lock, flags);
  1280. urb->transfer_buffer_length = bytes;
  1281. if (period_elapsed)
  1282. snd_pcm_period_elapsed(subs->pcm_substream);
  1283. }
  1284. /*
  1285. * process after playback data complete
  1286. * - decrease the delay count again
  1287. */
  1288. static void retire_playback_urb(struct snd_usb_substream *subs,
  1289. struct urb *urb)
  1290. {
  1291. unsigned long flags;
  1292. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1293. struct snd_usb_endpoint *ep = subs->data_endpoint;
  1294. int processed = urb->transfer_buffer_length / ep->stride;
  1295. int est_delay;
  1296. /* ignore the delay accounting when procssed=0 is given, i.e.
  1297. * silent payloads are procssed before handling the actual data
  1298. */
  1299. if (!processed)
  1300. return;
  1301. spin_lock_irqsave(&subs->lock, flags);
  1302. if (!subs->last_delay)
  1303. goto out; /* short path */
  1304. est_delay = snd_usb_pcm_delay(subs, runtime->rate);
  1305. /* update delay with exact number of samples played */
  1306. if (processed > subs->last_delay)
  1307. subs->last_delay = 0;
  1308. else
  1309. subs->last_delay -= processed;
  1310. runtime->delay = subs->last_delay;
  1311. /*
  1312. * Report when delay estimate is off by more than 2ms.
  1313. * The error should be lower than 2ms since the estimate relies
  1314. * on two reads of a counter updated every ms.
  1315. */
  1316. if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
  1317. snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
  1318. est_delay, subs->last_delay);
  1319. if (!subs->running) {
  1320. /* update last_frame_number for delay counting here since
  1321. * prepare_playback_urb won't be called during pause
  1322. */
  1323. subs->last_frame_number =
  1324. usb_get_current_frame_number(subs->dev) & 0xff;
  1325. }
  1326. out:
  1327. spin_unlock_irqrestore(&subs->lock, flags);
  1328. }
  1329. static int snd_usb_playback_open(struct snd_pcm_substream *substream)
  1330. {
  1331. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1332. }
  1333. static int snd_usb_playback_close(struct snd_pcm_substream *substream)
  1334. {
  1335. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1336. }
  1337. static int snd_usb_capture_open(struct snd_pcm_substream *substream)
  1338. {
  1339. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
  1340. }
  1341. static int snd_usb_capture_close(struct snd_pcm_substream *substream)
  1342. {
  1343. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  1344. }
  1345. static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
  1346. int cmd)
  1347. {
  1348. struct snd_usb_substream *subs = substream->runtime->private_data;
  1349. switch (cmd) {
  1350. case SNDRV_PCM_TRIGGER_START:
  1351. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1352. subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
  1353. subs->data_endpoint->retire_data_urb = retire_playback_urb;
  1354. subs->running = 1;
  1355. return 0;
  1356. case SNDRV_PCM_TRIGGER_STOP:
  1357. stop_endpoints(subs, false);
  1358. subs->running = 0;
  1359. return 0;
  1360. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1361. subs->data_endpoint->prepare_data_urb = NULL;
  1362. /* keep retire_data_urb for delay calculation */
  1363. subs->data_endpoint->retire_data_urb = retire_playback_urb;
  1364. subs->running = 0;
  1365. return 0;
  1366. }
  1367. return -EINVAL;
  1368. }
  1369. static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
  1370. int cmd)
  1371. {
  1372. int err;
  1373. struct snd_usb_substream *subs = substream->runtime->private_data;
  1374. switch (cmd) {
  1375. case SNDRV_PCM_TRIGGER_START:
  1376. err = start_endpoints(subs, false);
  1377. if (err < 0)
  1378. return err;
  1379. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1380. subs->running = 1;
  1381. return 0;
  1382. case SNDRV_PCM_TRIGGER_STOP:
  1383. stop_endpoints(subs, false);
  1384. subs->running = 0;
  1385. return 0;
  1386. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1387. subs->data_endpoint->retire_data_urb = NULL;
  1388. subs->running = 0;
  1389. return 0;
  1390. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1391. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1392. subs->running = 1;
  1393. return 0;
  1394. }
  1395. return -EINVAL;
  1396. }
  1397. static struct snd_pcm_ops snd_usb_playback_ops = {
  1398. .open = snd_usb_playback_open,
  1399. .close = snd_usb_playback_close,
  1400. .ioctl = snd_pcm_lib_ioctl,
  1401. .hw_params = snd_usb_hw_params,
  1402. .hw_free = snd_usb_hw_free,
  1403. .prepare = snd_usb_pcm_prepare,
  1404. .trigger = snd_usb_substream_playback_trigger,
  1405. .pointer = snd_usb_pcm_pointer,
  1406. .page = snd_pcm_lib_get_vmalloc_page,
  1407. .mmap = snd_pcm_lib_mmap_vmalloc,
  1408. };
  1409. static struct snd_pcm_ops snd_usb_capture_ops = {
  1410. .open = snd_usb_capture_open,
  1411. .close = snd_usb_capture_close,
  1412. .ioctl = snd_pcm_lib_ioctl,
  1413. .hw_params = snd_usb_hw_params,
  1414. .hw_free = snd_usb_hw_free,
  1415. .prepare = snd_usb_pcm_prepare,
  1416. .trigger = snd_usb_substream_capture_trigger,
  1417. .pointer = snd_usb_pcm_pointer,
  1418. .page = snd_pcm_lib_get_vmalloc_page,
  1419. .mmap = snd_pcm_lib_mmap_vmalloc,
  1420. };
  1421. void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
  1422. {
  1423. snd_pcm_set_ops(pcm, stream,
  1424. stream == SNDRV_PCM_STREAM_PLAYBACK ?
  1425. &snd_usb_playback_ops : &snd_usb_capture_ops);
  1426. }