pcm.c 50 KB

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