pcm.c 40 KB

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