pcm.c 46 KB

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