pcm.c 45 KB

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