pcm.c 50 KB

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