pcm.c 52 KB

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