pcm.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  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_D0);
  647. if (ret < 0)
  648. return ret;
  649. ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D0);
  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 = set_format(subs, fmt);
  695. snd_usb_unlock_shutdown(subs->stream->chip);
  696. if (ret < 0)
  697. return ret;
  698. subs->interface = fmt->iface;
  699. subs->altset_idx = fmt->altset_idx;
  700. subs->need_setup_ep = true;
  701. return 0;
  702. }
  703. /*
  704. * hw_free callback
  705. *
  706. * reset the audio format and release the buffer
  707. */
  708. static int snd_usb_hw_free(struct snd_pcm_substream *substream)
  709. {
  710. struct snd_usb_substream *subs = substream->runtime->private_data;
  711. subs->cur_audiofmt = NULL;
  712. subs->cur_rate = 0;
  713. subs->period_bytes = 0;
  714. if (!snd_usb_lock_shutdown(subs->stream->chip)) {
  715. stop_endpoints(subs, true);
  716. snd_usb_endpoint_deactivate(subs->sync_endpoint);
  717. snd_usb_endpoint_deactivate(subs->data_endpoint);
  718. snd_usb_unlock_shutdown(subs->stream->chip);
  719. }
  720. if (snd_usb_use_vmalloc)
  721. return snd_pcm_lib_free_vmalloc_buffer(substream);
  722. else
  723. return snd_pcm_lib_free_pages(substream);
  724. }
  725. /*
  726. * prepare callback
  727. *
  728. * only a few subtle things...
  729. */
  730. static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
  731. {
  732. struct snd_pcm_runtime *runtime = substream->runtime;
  733. struct snd_usb_substream *subs = runtime->private_data;
  734. struct usb_host_interface *alts;
  735. struct usb_interface *iface;
  736. int ret;
  737. if (! subs->cur_audiofmt) {
  738. dev_err(&subs->dev->dev, "no format is specified!\n");
  739. return -ENXIO;
  740. }
  741. ret = snd_usb_lock_shutdown(subs->stream->chip);
  742. if (ret < 0)
  743. return ret;
  744. if (snd_BUG_ON(!subs->data_endpoint)) {
  745. ret = -EIO;
  746. goto unlock;
  747. }
  748. snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
  749. snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
  750. ret = set_format(subs, subs->cur_audiofmt);
  751. if (ret < 0)
  752. goto unlock;
  753. if (subs->need_setup_ep) {
  754. iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
  755. alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
  756. ret = snd_usb_init_sample_rate(subs->stream->chip,
  757. subs->cur_audiofmt->iface,
  758. alts,
  759. subs->cur_audiofmt,
  760. subs->cur_rate);
  761. if (ret < 0)
  762. goto unlock;
  763. ret = configure_endpoint(subs);
  764. if (ret < 0)
  765. goto unlock;
  766. subs->need_setup_ep = false;
  767. }
  768. /* some unit conversions in runtime */
  769. subs->data_endpoint->maxframesize =
  770. bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
  771. subs->data_endpoint->curframesize =
  772. bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
  773. /* reset the pointer */
  774. subs->hwptr_done = 0;
  775. subs->transfer_done = 0;
  776. subs->last_delay = 0;
  777. subs->last_frame_number = 0;
  778. runtime->delay = 0;
  779. /* for playback, submit the URBs now; otherwise, the first hwptr_done
  780. * updates for all URBs would happen at the same time when starting */
  781. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
  782. ret = start_endpoints(subs);
  783. unlock:
  784. snd_usb_unlock_shutdown(subs->stream->chip);
  785. return ret;
  786. }
  787. static const struct snd_pcm_hardware snd_usb_hardware =
  788. {
  789. .info = SNDRV_PCM_INFO_MMAP |
  790. SNDRV_PCM_INFO_MMAP_VALID |
  791. SNDRV_PCM_INFO_BATCH |
  792. SNDRV_PCM_INFO_INTERLEAVED |
  793. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  794. SNDRV_PCM_INFO_PAUSE,
  795. .buffer_bytes_max = 1024 * 1024,
  796. .period_bytes_min = 64,
  797. .period_bytes_max = 512 * 1024,
  798. .periods_min = 2,
  799. .periods_max = 1024,
  800. };
  801. static int hw_check_valid_format(struct snd_usb_substream *subs,
  802. struct snd_pcm_hw_params *params,
  803. struct audioformat *fp)
  804. {
  805. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  806. struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  807. struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  808. struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  809. struct snd_mask check_fmts;
  810. unsigned int ptime;
  811. /* check the format */
  812. snd_mask_none(&check_fmts);
  813. check_fmts.bits[0] = (u32)fp->formats;
  814. check_fmts.bits[1] = (u32)(fp->formats >> 32);
  815. snd_mask_intersect(&check_fmts, fmts);
  816. if (snd_mask_empty(&check_fmts)) {
  817. hwc_debug(" > check: no supported format %d\n", fp->format);
  818. return 0;
  819. }
  820. /* check the channels */
  821. if (fp->channels < ct->min || fp->channels > ct->max) {
  822. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  823. return 0;
  824. }
  825. /* check the rate is within the range */
  826. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  827. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  828. return 0;
  829. }
  830. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  831. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  832. return 0;
  833. }
  834. /* check whether the period time is >= the data packet interval */
  835. if (subs->speed != USB_SPEED_FULL) {
  836. ptime = 125 * (1 << fp->datainterval);
  837. if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  838. hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
  839. return 0;
  840. }
  841. }
  842. return 1;
  843. }
  844. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  845. struct snd_pcm_hw_rule *rule)
  846. {
  847. struct snd_usb_substream *subs = rule->private;
  848. struct audioformat *fp;
  849. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  850. unsigned int rmin, rmax;
  851. int changed;
  852. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  853. changed = 0;
  854. rmin = rmax = 0;
  855. list_for_each_entry(fp, &subs->fmt_list, list) {
  856. if (!hw_check_valid_format(subs, params, fp))
  857. continue;
  858. if (changed++) {
  859. if (rmin > fp->rate_min)
  860. rmin = fp->rate_min;
  861. if (rmax < fp->rate_max)
  862. rmax = fp->rate_max;
  863. } else {
  864. rmin = fp->rate_min;
  865. rmax = fp->rate_max;
  866. }
  867. }
  868. if (!changed) {
  869. hwc_debug(" --> get empty\n");
  870. it->empty = 1;
  871. return -EINVAL;
  872. }
  873. changed = 0;
  874. if (it->min < rmin) {
  875. it->min = rmin;
  876. it->openmin = 0;
  877. changed = 1;
  878. }
  879. if (it->max > rmax) {
  880. it->max = rmax;
  881. it->openmax = 0;
  882. changed = 1;
  883. }
  884. if (snd_interval_checkempty(it)) {
  885. it->empty = 1;
  886. return -EINVAL;
  887. }
  888. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  889. return changed;
  890. }
  891. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  892. struct snd_pcm_hw_rule *rule)
  893. {
  894. struct snd_usb_substream *subs = rule->private;
  895. struct audioformat *fp;
  896. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  897. unsigned int rmin, rmax;
  898. int changed;
  899. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  900. changed = 0;
  901. rmin = rmax = 0;
  902. list_for_each_entry(fp, &subs->fmt_list, list) {
  903. if (!hw_check_valid_format(subs, params, fp))
  904. continue;
  905. if (changed++) {
  906. if (rmin > fp->channels)
  907. rmin = fp->channels;
  908. if (rmax < fp->channels)
  909. rmax = fp->channels;
  910. } else {
  911. rmin = fp->channels;
  912. rmax = fp->channels;
  913. }
  914. }
  915. if (!changed) {
  916. hwc_debug(" --> get empty\n");
  917. it->empty = 1;
  918. return -EINVAL;
  919. }
  920. changed = 0;
  921. if (it->min < rmin) {
  922. it->min = rmin;
  923. it->openmin = 0;
  924. changed = 1;
  925. }
  926. if (it->max > rmax) {
  927. it->max = rmax;
  928. it->openmax = 0;
  929. changed = 1;
  930. }
  931. if (snd_interval_checkempty(it)) {
  932. it->empty = 1;
  933. return -EINVAL;
  934. }
  935. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  936. return changed;
  937. }
  938. static int hw_rule_format(struct snd_pcm_hw_params *params,
  939. struct snd_pcm_hw_rule *rule)
  940. {
  941. struct snd_usb_substream *subs = rule->private;
  942. struct audioformat *fp;
  943. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  944. u64 fbits;
  945. u32 oldbits[2];
  946. int changed;
  947. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  948. fbits = 0;
  949. list_for_each_entry(fp, &subs->fmt_list, list) {
  950. if (!hw_check_valid_format(subs, params, fp))
  951. continue;
  952. fbits |= fp->formats;
  953. }
  954. oldbits[0] = fmt->bits[0];
  955. oldbits[1] = fmt->bits[1];
  956. fmt->bits[0] &= (u32)fbits;
  957. fmt->bits[1] &= (u32)(fbits >> 32);
  958. if (!fmt->bits[0] && !fmt->bits[1]) {
  959. hwc_debug(" --> get empty\n");
  960. return -EINVAL;
  961. }
  962. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  963. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  964. return changed;
  965. }
  966. static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  967. struct snd_pcm_hw_rule *rule)
  968. {
  969. struct snd_usb_substream *subs = rule->private;
  970. struct audioformat *fp;
  971. struct snd_interval *it;
  972. unsigned char min_datainterval;
  973. unsigned int pmin;
  974. int changed;
  975. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  976. hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
  977. min_datainterval = 0xff;
  978. list_for_each_entry(fp, &subs->fmt_list, list) {
  979. if (!hw_check_valid_format(subs, params, fp))
  980. continue;
  981. min_datainterval = min(min_datainterval, fp->datainterval);
  982. }
  983. if (min_datainterval == 0xff) {
  984. hwc_debug(" --> get empty\n");
  985. it->empty = 1;
  986. return -EINVAL;
  987. }
  988. pmin = 125 * (1 << min_datainterval);
  989. changed = 0;
  990. if (it->min < pmin) {
  991. it->min = pmin;
  992. it->openmin = 0;
  993. changed = 1;
  994. }
  995. if (snd_interval_checkempty(it)) {
  996. it->empty = 1;
  997. return -EINVAL;
  998. }
  999. hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
  1000. return changed;
  1001. }
  1002. /*
  1003. * If the device supports unusual bit rates, does the request meet these?
  1004. */
  1005. static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
  1006. struct snd_usb_substream *subs)
  1007. {
  1008. struct audioformat *fp;
  1009. int *rate_list;
  1010. int count = 0, needs_knot = 0;
  1011. int err;
  1012. kfree(subs->rate_list.list);
  1013. subs->rate_list.list = NULL;
  1014. list_for_each_entry(fp, &subs->fmt_list, list) {
  1015. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
  1016. return 0;
  1017. count += fp->nr_rates;
  1018. if (fp->rates & SNDRV_PCM_RATE_KNOT)
  1019. needs_knot = 1;
  1020. }
  1021. if (!needs_knot)
  1022. return 0;
  1023. subs->rate_list.list = rate_list =
  1024. kmalloc_array(count, sizeof(int), GFP_KERNEL);
  1025. if (!subs->rate_list.list)
  1026. return -ENOMEM;
  1027. subs->rate_list.count = count;
  1028. subs->rate_list.mask = 0;
  1029. count = 0;
  1030. list_for_each_entry(fp, &subs->fmt_list, list) {
  1031. int i;
  1032. for (i = 0; i < fp->nr_rates; i++)
  1033. rate_list[count++] = fp->rate_table[i];
  1034. }
  1035. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1036. &subs->rate_list);
  1037. if (err < 0)
  1038. return err;
  1039. return 0;
  1040. }
  1041. /*
  1042. * set up the runtime hardware information.
  1043. */
  1044. static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  1045. {
  1046. struct audioformat *fp;
  1047. unsigned int pt, ptmin;
  1048. int param_period_time_if_needed;
  1049. int err;
  1050. runtime->hw.formats = subs->formats;
  1051. runtime->hw.rate_min = 0x7fffffff;
  1052. runtime->hw.rate_max = 0;
  1053. runtime->hw.channels_min = 256;
  1054. runtime->hw.channels_max = 0;
  1055. runtime->hw.rates = 0;
  1056. ptmin = UINT_MAX;
  1057. /* check min/max rates and channels */
  1058. list_for_each_entry(fp, &subs->fmt_list, list) {
  1059. runtime->hw.rates |= fp->rates;
  1060. if (runtime->hw.rate_min > fp->rate_min)
  1061. runtime->hw.rate_min = fp->rate_min;
  1062. if (runtime->hw.rate_max < fp->rate_max)
  1063. runtime->hw.rate_max = fp->rate_max;
  1064. if (runtime->hw.channels_min > fp->channels)
  1065. runtime->hw.channels_min = fp->channels;
  1066. if (runtime->hw.channels_max < fp->channels)
  1067. runtime->hw.channels_max = fp->channels;
  1068. if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  1069. /* FIXME: there might be more than one audio formats... */
  1070. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  1071. fp->frame_size;
  1072. }
  1073. pt = 125 * (1 << fp->datainterval);
  1074. ptmin = min(ptmin, pt);
  1075. }
  1076. param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
  1077. if (subs->speed == USB_SPEED_FULL)
  1078. /* full speed devices have fixed data packet interval */
  1079. ptmin = 1000;
  1080. if (ptmin == 1000)
  1081. /* if period time doesn't go below 1 ms, no rules needed */
  1082. param_period_time_if_needed = -1;
  1083. err = snd_pcm_hw_constraint_minmax(runtime,
  1084. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1085. ptmin, UINT_MAX);
  1086. if (err < 0)
  1087. return err;
  1088. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1089. hw_rule_rate, subs,
  1090. SNDRV_PCM_HW_PARAM_FORMAT,
  1091. SNDRV_PCM_HW_PARAM_CHANNELS,
  1092. param_period_time_if_needed,
  1093. -1);
  1094. if (err < 0)
  1095. return err;
  1096. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  1097. hw_rule_channels, subs,
  1098. SNDRV_PCM_HW_PARAM_FORMAT,
  1099. SNDRV_PCM_HW_PARAM_RATE,
  1100. param_period_time_if_needed,
  1101. -1);
  1102. if (err < 0)
  1103. return err;
  1104. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  1105. hw_rule_format, subs,
  1106. SNDRV_PCM_HW_PARAM_RATE,
  1107. SNDRV_PCM_HW_PARAM_CHANNELS,
  1108. param_period_time_if_needed,
  1109. -1);
  1110. if (err < 0)
  1111. return err;
  1112. if (param_period_time_if_needed >= 0) {
  1113. err = snd_pcm_hw_rule_add(runtime, 0,
  1114. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1115. hw_rule_period_time, subs,
  1116. SNDRV_PCM_HW_PARAM_FORMAT,
  1117. SNDRV_PCM_HW_PARAM_CHANNELS,
  1118. SNDRV_PCM_HW_PARAM_RATE,
  1119. -1);
  1120. if (err < 0)
  1121. return err;
  1122. }
  1123. err = snd_usb_pcm_check_knot(runtime, subs);
  1124. if (err < 0)
  1125. return err;
  1126. return snd_usb_autoresume(subs->stream->chip);
  1127. }
  1128. static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
  1129. {
  1130. int direction = substream->stream;
  1131. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  1132. struct snd_pcm_runtime *runtime = substream->runtime;
  1133. struct snd_usb_substream *subs = &as->substream[direction];
  1134. subs->interface = -1;
  1135. subs->altset_idx = 0;
  1136. runtime->hw = snd_usb_hardware;
  1137. runtime->private_data = subs;
  1138. subs->pcm_substream = substream;
  1139. /* runtime PM is also done there */
  1140. /* initialize DSD/DOP context */
  1141. subs->dsd_dop.byte_idx = 0;
  1142. subs->dsd_dop.channel = 0;
  1143. subs->dsd_dop.marker = 1;
  1144. return setup_hw_info(runtime, subs);
  1145. }
  1146. static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
  1147. {
  1148. int direction = substream->stream;
  1149. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  1150. struct snd_usb_substream *subs = &as->substream[direction];
  1151. stop_endpoints(subs, true);
  1152. if (!as->chip->keep_iface &&
  1153. subs->interface >= 0 &&
  1154. !snd_usb_lock_shutdown(subs->stream->chip)) {
  1155. usb_set_interface(subs->dev, subs->interface, 0);
  1156. subs->interface = -1;
  1157. snd_usb_unlock_shutdown(subs->stream->chip);
  1158. }
  1159. subs->pcm_substream = NULL;
  1160. snd_usb_autosuspend(subs->stream->chip);
  1161. return 0;
  1162. }
  1163. /* Since a URB can handle only a single linear buffer, we must use double
  1164. * buffering when the data to be transferred overflows the buffer boundary.
  1165. * To avoid inconsistencies when updating hwptr_done, we use double buffering
  1166. * for all URBs.
  1167. */
  1168. static void retire_capture_urb(struct snd_usb_substream *subs,
  1169. struct urb *urb)
  1170. {
  1171. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1172. unsigned int stride, frames, bytes, oldptr;
  1173. int i, period_elapsed = 0;
  1174. unsigned long flags;
  1175. unsigned char *cp;
  1176. int current_frame_number;
  1177. /* read frame number here, update pointer in critical section */
  1178. current_frame_number = usb_get_current_frame_number(subs->dev);
  1179. stride = runtime->frame_bits >> 3;
  1180. for (i = 0; i < urb->number_of_packets; i++) {
  1181. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
  1182. if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
  1183. dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
  1184. i, urb->iso_frame_desc[i].status);
  1185. // continue;
  1186. }
  1187. bytes = urb->iso_frame_desc[i].actual_length;
  1188. frames = bytes / stride;
  1189. if (!subs->txfr_quirk)
  1190. bytes = frames * stride;
  1191. if (bytes % (runtime->sample_bits >> 3) != 0) {
  1192. int oldbytes = bytes;
  1193. bytes = frames * stride;
  1194. dev_warn_ratelimited(&subs->dev->dev,
  1195. "Corrected urb data len. %d->%d\n",
  1196. oldbytes, bytes);
  1197. }
  1198. /* update the current pointer */
  1199. spin_lock_irqsave(&subs->lock, flags);
  1200. oldptr = subs->hwptr_done;
  1201. subs->hwptr_done += bytes;
  1202. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1203. subs->hwptr_done -= runtime->buffer_size * stride;
  1204. frames = (bytes + (oldptr % stride)) / stride;
  1205. subs->transfer_done += frames;
  1206. if (subs->transfer_done >= runtime->period_size) {
  1207. subs->transfer_done -= runtime->period_size;
  1208. period_elapsed = 1;
  1209. }
  1210. /* capture delay is by construction limited to one URB,
  1211. * reset delays here
  1212. */
  1213. runtime->delay = subs->last_delay = 0;
  1214. /* realign last_frame_number */
  1215. subs->last_frame_number = current_frame_number;
  1216. subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
  1217. spin_unlock_irqrestore(&subs->lock, flags);
  1218. /* copy a data chunk */
  1219. if (oldptr + bytes > runtime->buffer_size * stride) {
  1220. unsigned int bytes1 =
  1221. runtime->buffer_size * stride - oldptr;
  1222. memcpy(runtime->dma_area + oldptr, cp, bytes1);
  1223. memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  1224. } else {
  1225. memcpy(runtime->dma_area + oldptr, cp, bytes);
  1226. }
  1227. }
  1228. if (period_elapsed)
  1229. snd_pcm_period_elapsed(subs->pcm_substream);
  1230. }
  1231. static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
  1232. struct urb *urb, unsigned int bytes)
  1233. {
  1234. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1235. unsigned int stride = runtime->frame_bits >> 3;
  1236. unsigned int dst_idx = 0;
  1237. unsigned int src_idx = subs->hwptr_done;
  1238. unsigned int wrap = runtime->buffer_size * stride;
  1239. u8 *dst = urb->transfer_buffer;
  1240. u8 *src = runtime->dma_area;
  1241. u8 marker[] = { 0x05, 0xfa };
  1242. /*
  1243. * The DSP DOP format defines a way to transport DSD samples over
  1244. * normal PCM data endpoints. It requires stuffing of marker bytes
  1245. * (0x05 and 0xfa, alternating per sample frame), and then expects
  1246. * 2 additional bytes of actual payload. The whole frame is stored
  1247. * LSB.
  1248. *
  1249. * Hence, for a stereo transport, the buffer layout looks like this,
  1250. * where L refers to left channel samples and R to right.
  1251. *
  1252. * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
  1253. * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
  1254. * .....
  1255. *
  1256. */
  1257. while (bytes--) {
  1258. if (++subs->dsd_dop.byte_idx == 3) {
  1259. /* frame boundary? */
  1260. dst[dst_idx++] = marker[subs->dsd_dop.marker];
  1261. src_idx += 2;
  1262. subs->dsd_dop.byte_idx = 0;
  1263. if (++subs->dsd_dop.channel % runtime->channels == 0) {
  1264. /* alternate the marker */
  1265. subs->dsd_dop.marker++;
  1266. subs->dsd_dop.marker %= ARRAY_SIZE(marker);
  1267. subs->dsd_dop.channel = 0;
  1268. }
  1269. } else {
  1270. /* stuff the DSD payload */
  1271. int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
  1272. if (subs->cur_audiofmt->dsd_bitrev)
  1273. dst[dst_idx++] = bitrev8(src[idx]);
  1274. else
  1275. dst[dst_idx++] = src[idx];
  1276. subs->hwptr_done++;
  1277. }
  1278. }
  1279. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1280. subs->hwptr_done -= runtime->buffer_size * stride;
  1281. }
  1282. static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
  1283. int offset, int stride, unsigned int bytes)
  1284. {
  1285. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1286. if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  1287. /* err, the transferred area goes over buffer boundary. */
  1288. unsigned int bytes1 =
  1289. runtime->buffer_size * stride - subs->hwptr_done;
  1290. memcpy(urb->transfer_buffer + offset,
  1291. runtime->dma_area + subs->hwptr_done, bytes1);
  1292. memcpy(urb->transfer_buffer + offset + bytes1,
  1293. runtime->dma_area, bytes - bytes1);
  1294. } else {
  1295. memcpy(urb->transfer_buffer + offset,
  1296. runtime->dma_area + subs->hwptr_done, bytes);
  1297. }
  1298. subs->hwptr_done += bytes;
  1299. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1300. subs->hwptr_done -= runtime->buffer_size * stride;
  1301. }
  1302. static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
  1303. struct urb *urb, int stride,
  1304. unsigned int bytes)
  1305. {
  1306. __le32 packet_length;
  1307. int i;
  1308. /* Put __le32 length descriptor at start of each packet. */
  1309. for (i = 0; i < urb->number_of_packets; i++) {
  1310. unsigned int length = urb->iso_frame_desc[i].length;
  1311. unsigned int offset = urb->iso_frame_desc[i].offset;
  1312. packet_length = cpu_to_le32(length);
  1313. offset += i * sizeof(packet_length);
  1314. urb->iso_frame_desc[i].offset = offset;
  1315. urb->iso_frame_desc[i].length += sizeof(packet_length);
  1316. memcpy(urb->transfer_buffer + offset,
  1317. &packet_length, sizeof(packet_length));
  1318. copy_to_urb(subs, urb, offset + sizeof(packet_length),
  1319. stride, length);
  1320. }
  1321. /* Adjust transfer size accordingly. */
  1322. bytes += urb->number_of_packets * sizeof(packet_length);
  1323. return bytes;
  1324. }
  1325. static void prepare_playback_urb(struct snd_usb_substream *subs,
  1326. struct urb *urb)
  1327. {
  1328. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1329. struct snd_usb_endpoint *ep = subs->data_endpoint;
  1330. struct snd_urb_ctx *ctx = urb->context;
  1331. unsigned int counts, frames, bytes;
  1332. int i, stride, period_elapsed = 0;
  1333. unsigned long flags;
  1334. stride = runtime->frame_bits >> 3;
  1335. frames = 0;
  1336. urb->number_of_packets = 0;
  1337. spin_lock_irqsave(&subs->lock, flags);
  1338. subs->frame_limit += ep->max_urb_frames;
  1339. for (i = 0; i < ctx->packets; i++) {
  1340. if (ctx->packet_size[i])
  1341. counts = ctx->packet_size[i];
  1342. else
  1343. counts = snd_usb_endpoint_next_packet_size(ep);
  1344. /* set up descriptor */
  1345. urb->iso_frame_desc[i].offset = frames * ep->stride;
  1346. urb->iso_frame_desc[i].length = counts * ep->stride;
  1347. frames += counts;
  1348. urb->number_of_packets++;
  1349. subs->transfer_done += counts;
  1350. if (subs->transfer_done >= runtime->period_size) {
  1351. subs->transfer_done -= runtime->period_size;
  1352. subs->frame_limit = 0;
  1353. period_elapsed = 1;
  1354. if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  1355. if (subs->transfer_done > 0) {
  1356. /* FIXME: fill-max mode is not
  1357. * supported yet */
  1358. frames -= subs->transfer_done;
  1359. counts -= subs->transfer_done;
  1360. urb->iso_frame_desc[i].length =
  1361. counts * ep->stride;
  1362. subs->transfer_done = 0;
  1363. }
  1364. i++;
  1365. if (i < ctx->packets) {
  1366. /* add a transfer delimiter */
  1367. urb->iso_frame_desc[i].offset =
  1368. frames * ep->stride;
  1369. urb->iso_frame_desc[i].length = 0;
  1370. urb->number_of_packets++;
  1371. }
  1372. break;
  1373. }
  1374. }
  1375. /* finish at the period boundary or after enough frames */
  1376. if ((period_elapsed ||
  1377. subs->transfer_done >= subs->frame_limit) &&
  1378. !snd_usb_endpoint_implicit_feedback_sink(ep))
  1379. break;
  1380. }
  1381. bytes = frames * ep->stride;
  1382. if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
  1383. subs->cur_audiofmt->dsd_dop)) {
  1384. fill_playback_urb_dsd_dop(subs, urb, bytes);
  1385. } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
  1386. subs->cur_audiofmt->dsd_bitrev)) {
  1387. /* bit-reverse the bytes */
  1388. u8 *buf = urb->transfer_buffer;
  1389. for (i = 0; i < bytes; i++) {
  1390. int idx = (subs->hwptr_done + i)
  1391. % (runtime->buffer_size * stride);
  1392. buf[i] = bitrev8(runtime->dma_area[idx]);
  1393. }
  1394. subs->hwptr_done += bytes;
  1395. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1396. subs->hwptr_done -= runtime->buffer_size * stride;
  1397. } else {
  1398. /* usual PCM */
  1399. if (!subs->tx_length_quirk)
  1400. copy_to_urb(subs, urb, 0, stride, bytes);
  1401. else
  1402. bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
  1403. /* bytes is now amount of outgoing data */
  1404. }
  1405. /* update delay with exact number of samples queued */
  1406. runtime->delay = subs->last_delay;
  1407. runtime->delay += frames;
  1408. subs->last_delay = runtime->delay;
  1409. /* realign last_frame_number */
  1410. subs->last_frame_number = usb_get_current_frame_number(subs->dev);
  1411. subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
  1412. if (subs->trigger_tstamp_pending_update) {
  1413. /* this is the first actual URB submitted,
  1414. * update trigger timestamp to reflect actual start time
  1415. */
  1416. snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
  1417. subs->trigger_tstamp_pending_update = false;
  1418. }
  1419. spin_unlock_irqrestore(&subs->lock, flags);
  1420. urb->transfer_buffer_length = bytes;
  1421. if (period_elapsed)
  1422. snd_pcm_period_elapsed(subs->pcm_substream);
  1423. }
  1424. /*
  1425. * process after playback data complete
  1426. * - decrease the delay count again
  1427. */
  1428. static void retire_playback_urb(struct snd_usb_substream *subs,
  1429. struct urb *urb)
  1430. {
  1431. unsigned long flags;
  1432. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1433. struct snd_usb_endpoint *ep = subs->data_endpoint;
  1434. int processed = urb->transfer_buffer_length / ep->stride;
  1435. int est_delay;
  1436. /* ignore the delay accounting when procssed=0 is given, i.e.
  1437. * silent payloads are procssed before handling the actual data
  1438. */
  1439. if (!processed)
  1440. return;
  1441. spin_lock_irqsave(&subs->lock, flags);
  1442. if (!subs->last_delay)
  1443. goto out; /* short path */
  1444. est_delay = snd_usb_pcm_delay(subs, runtime->rate);
  1445. /* update delay with exact number of samples played */
  1446. if (processed > subs->last_delay)
  1447. subs->last_delay = 0;
  1448. else
  1449. subs->last_delay -= processed;
  1450. runtime->delay = subs->last_delay;
  1451. /*
  1452. * Report when delay estimate is off by more than 2ms.
  1453. * The error should be lower than 2ms since the estimate relies
  1454. * on two reads of a counter updated every ms.
  1455. */
  1456. if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
  1457. dev_dbg_ratelimited(&subs->dev->dev,
  1458. "delay: estimated %d, actual %d\n",
  1459. est_delay, subs->last_delay);
  1460. if (!subs->running) {
  1461. /* update last_frame_number for delay counting here since
  1462. * prepare_playback_urb won't be called during pause
  1463. */
  1464. subs->last_frame_number =
  1465. usb_get_current_frame_number(subs->dev) & 0xff;
  1466. }
  1467. out:
  1468. spin_unlock_irqrestore(&subs->lock, flags);
  1469. }
  1470. static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
  1471. int cmd)
  1472. {
  1473. struct snd_usb_substream *subs = substream->runtime->private_data;
  1474. switch (cmd) {
  1475. case SNDRV_PCM_TRIGGER_START:
  1476. subs->trigger_tstamp_pending_update = true;
  1477. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1478. subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
  1479. subs->data_endpoint->retire_data_urb = retire_playback_urb;
  1480. subs->running = 1;
  1481. return 0;
  1482. case SNDRV_PCM_TRIGGER_STOP:
  1483. stop_endpoints(subs, false);
  1484. subs->running = 0;
  1485. return 0;
  1486. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1487. subs->data_endpoint->prepare_data_urb = NULL;
  1488. /* keep retire_data_urb for delay calculation */
  1489. subs->data_endpoint->retire_data_urb = retire_playback_urb;
  1490. subs->running = 0;
  1491. return 0;
  1492. }
  1493. return -EINVAL;
  1494. }
  1495. static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
  1496. int cmd)
  1497. {
  1498. int err;
  1499. struct snd_usb_substream *subs = substream->runtime->private_data;
  1500. switch (cmd) {
  1501. case SNDRV_PCM_TRIGGER_START:
  1502. err = start_endpoints(subs);
  1503. if (err < 0)
  1504. return err;
  1505. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1506. subs->running = 1;
  1507. return 0;
  1508. case SNDRV_PCM_TRIGGER_STOP:
  1509. stop_endpoints(subs, false);
  1510. subs->running = 0;
  1511. return 0;
  1512. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1513. subs->data_endpoint->retire_data_urb = NULL;
  1514. subs->running = 0;
  1515. return 0;
  1516. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1517. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1518. subs->running = 1;
  1519. return 0;
  1520. }
  1521. return -EINVAL;
  1522. }
  1523. static const struct snd_pcm_ops snd_usb_playback_ops = {
  1524. .open = snd_usb_pcm_open,
  1525. .close = snd_usb_pcm_close,
  1526. .ioctl = snd_pcm_lib_ioctl,
  1527. .hw_params = snd_usb_hw_params,
  1528. .hw_free = snd_usb_hw_free,
  1529. .prepare = snd_usb_pcm_prepare,
  1530. .trigger = snd_usb_substream_playback_trigger,
  1531. .pointer = snd_usb_pcm_pointer,
  1532. .page = snd_pcm_lib_get_vmalloc_page,
  1533. };
  1534. static const struct snd_pcm_ops snd_usb_capture_ops = {
  1535. .open = snd_usb_pcm_open,
  1536. .close = snd_usb_pcm_close,
  1537. .ioctl = snd_pcm_lib_ioctl,
  1538. .hw_params = snd_usb_hw_params,
  1539. .hw_free = snd_usb_hw_free,
  1540. .prepare = snd_usb_pcm_prepare,
  1541. .trigger = snd_usb_substream_capture_trigger,
  1542. .pointer = snd_usb_pcm_pointer,
  1543. .page = snd_pcm_lib_get_vmalloc_page,
  1544. };
  1545. static const struct snd_pcm_ops snd_usb_playback_dev_ops = {
  1546. .open = snd_usb_pcm_open,
  1547. .close = snd_usb_pcm_close,
  1548. .ioctl = snd_pcm_lib_ioctl,
  1549. .hw_params = snd_usb_hw_params,
  1550. .hw_free = snd_usb_hw_free,
  1551. .prepare = snd_usb_pcm_prepare,
  1552. .trigger = snd_usb_substream_playback_trigger,
  1553. .pointer = snd_usb_pcm_pointer,
  1554. .page = snd_pcm_sgbuf_ops_page,
  1555. };
  1556. static const struct snd_pcm_ops snd_usb_capture_dev_ops = {
  1557. .open = snd_usb_pcm_open,
  1558. .close = snd_usb_pcm_close,
  1559. .ioctl = snd_pcm_lib_ioctl,
  1560. .hw_params = snd_usb_hw_params,
  1561. .hw_free = snd_usb_hw_free,
  1562. .prepare = snd_usb_pcm_prepare,
  1563. .trigger = snd_usb_substream_capture_trigger,
  1564. .pointer = snd_usb_pcm_pointer,
  1565. .page = snd_pcm_sgbuf_ops_page,
  1566. };
  1567. void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
  1568. {
  1569. const struct snd_pcm_ops *ops;
  1570. if (snd_usb_use_vmalloc)
  1571. ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
  1572. &snd_usb_playback_ops : &snd_usb_capture_ops;
  1573. else
  1574. ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
  1575. &snd_usb_playback_dev_ops : &snd_usb_capture_dev_ops;
  1576. snd_pcm_set_ops(pcm, stream, ops);
  1577. }
  1578. void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
  1579. {
  1580. struct snd_pcm *pcm = subs->stream->pcm;
  1581. struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
  1582. struct device *dev = subs->dev->bus->controller;
  1583. if (!snd_usb_use_vmalloc)
  1584. snd_pcm_lib_preallocate_pages(s, SNDRV_DMA_TYPE_DEV_SG,
  1585. dev, 64*1024, 512*1024);
  1586. }