pcm.c 47 KB

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