pcm.c 41 KB

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