pcm.c 45 KB

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