pcm.c 40 KB

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