pcm.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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, 0x2080): /* M-Audio FastTrack Ultra */
  317. case USB_ID(0x0763, 0x2081):
  318. if (is_playback) {
  319. implicit_fb = 1;
  320. ep = 0x81;
  321. iface = usb_ifnum_to_if(dev, 2);
  322. if (!iface || iface->num_altsetting == 0)
  323. return -EINVAL;
  324. alts = &iface->altsetting[1];
  325. goto add_sync_ep;
  326. }
  327. }
  328. if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
  329. (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
  330. altsd->bNumEndpoints >= 2) {
  331. /* check sync-pipe endpoint */
  332. /* ... and check descriptor size before accessing bSynchAddress
  333. because there is a version of the SB Audigy 2 NX firmware lacking
  334. the audio fields in the endpoint descriptors */
  335. if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
  336. (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  337. get_endpoint(alts, 1)->bSynchAddress != 0 &&
  338. !implicit_fb)) {
  339. snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
  340. dev->devnum, fmt->iface, fmt->altsetting,
  341. get_endpoint(alts, 1)->bmAttributes,
  342. get_endpoint(alts, 1)->bLength,
  343. get_endpoint(alts, 1)->bSynchAddress);
  344. return -EINVAL;
  345. }
  346. ep = get_endpoint(alts, 1)->bEndpointAddress;
  347. if (!implicit_fb &&
  348. get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  349. (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
  350. (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
  351. snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
  352. dev->devnum, fmt->iface, fmt->altsetting,
  353. is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
  354. return -EINVAL;
  355. }
  356. implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
  357. == USB_ENDPOINT_USAGE_IMPLICIT_FB;
  358. add_sync_ep:
  359. subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  360. alts, ep, !subs->direction,
  361. implicit_fb ?
  362. SND_USB_ENDPOINT_TYPE_DATA :
  363. SND_USB_ENDPOINT_TYPE_SYNC);
  364. if (!subs->sync_endpoint)
  365. return -EINVAL;
  366. subs->data_endpoint->sync_master = subs->sync_endpoint;
  367. }
  368. if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0)
  369. return err;
  370. subs->cur_audiofmt = fmt;
  371. snd_usb_set_format_quirk(subs, fmt);
  372. #if 0
  373. printk(KERN_DEBUG
  374. "setting done: format = %d, rate = %d..%d, channels = %d\n",
  375. fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
  376. printk(KERN_DEBUG
  377. " datapipe = 0x%0x, syncpipe = 0x%0x\n",
  378. subs->datapipe, subs->syncpipe);
  379. #endif
  380. return 0;
  381. }
  382. /*
  383. * configure endpoint params
  384. *
  385. * called during initial setup and upon resume
  386. */
  387. static int configure_endpoint(struct snd_usb_substream *subs)
  388. {
  389. int ret;
  390. /* format changed */
  391. stop_endpoints(subs, true);
  392. ret = snd_usb_endpoint_set_params(subs->data_endpoint,
  393. subs->pcm_format,
  394. subs->channels,
  395. subs->period_bytes,
  396. subs->cur_rate,
  397. subs->cur_audiofmt,
  398. subs->sync_endpoint);
  399. if (ret < 0)
  400. return ret;
  401. if (subs->sync_endpoint)
  402. ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
  403. subs->pcm_format,
  404. subs->channels,
  405. subs->period_bytes,
  406. subs->cur_rate,
  407. subs->cur_audiofmt,
  408. NULL);
  409. return ret;
  410. }
  411. /*
  412. * hw_params callback
  413. *
  414. * allocate a buffer and set the given audio format.
  415. *
  416. * so far we use a physically linear buffer although packetize transfer
  417. * doesn't need a continuous area.
  418. * if sg buffer is supported on the later version of alsa, we'll follow
  419. * that.
  420. */
  421. static int snd_usb_hw_params(struct snd_pcm_substream *substream,
  422. struct snd_pcm_hw_params *hw_params)
  423. {
  424. struct snd_usb_substream *subs = substream->runtime->private_data;
  425. struct audioformat *fmt;
  426. int ret;
  427. ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  428. params_buffer_bytes(hw_params));
  429. if (ret < 0)
  430. return ret;
  431. subs->pcm_format = params_format(hw_params);
  432. subs->period_bytes = params_period_bytes(hw_params);
  433. subs->channels = params_channels(hw_params);
  434. subs->cur_rate = params_rate(hw_params);
  435. fmt = find_format(subs);
  436. if (!fmt) {
  437. snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
  438. subs->pcm_format, subs->cur_rate, subs->channels);
  439. return -EINVAL;
  440. }
  441. down_read(&subs->stream->chip->shutdown_rwsem);
  442. if (subs->stream->chip->shutdown)
  443. ret = -ENODEV;
  444. else
  445. ret = set_format(subs, fmt);
  446. up_read(&subs->stream->chip->shutdown_rwsem);
  447. if (ret < 0)
  448. return ret;
  449. subs->interface = fmt->iface;
  450. subs->altset_idx = fmt->altset_idx;
  451. subs->need_setup_ep = true;
  452. return 0;
  453. }
  454. /*
  455. * hw_free callback
  456. *
  457. * reset the audio format and release the buffer
  458. */
  459. static int snd_usb_hw_free(struct snd_pcm_substream *substream)
  460. {
  461. struct snd_usb_substream *subs = substream->runtime->private_data;
  462. subs->cur_audiofmt = NULL;
  463. subs->cur_rate = 0;
  464. subs->period_bytes = 0;
  465. down_read(&subs->stream->chip->shutdown_rwsem);
  466. if (!subs->stream->chip->shutdown) {
  467. stop_endpoints(subs, true);
  468. deactivate_endpoints(subs);
  469. }
  470. up_read(&subs->stream->chip->shutdown_rwsem);
  471. return snd_pcm_lib_free_vmalloc_buffer(substream);
  472. }
  473. /*
  474. * prepare callback
  475. *
  476. * only a few subtle things...
  477. */
  478. static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
  479. {
  480. struct snd_pcm_runtime *runtime = substream->runtime;
  481. struct snd_usb_substream *subs = runtime->private_data;
  482. struct usb_host_interface *alts;
  483. struct usb_interface *iface;
  484. int ret;
  485. if (! subs->cur_audiofmt) {
  486. snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
  487. return -ENXIO;
  488. }
  489. down_read(&subs->stream->chip->shutdown_rwsem);
  490. if (subs->stream->chip->shutdown) {
  491. ret = -ENODEV;
  492. goto unlock;
  493. }
  494. if (snd_BUG_ON(!subs->data_endpoint)) {
  495. ret = -EIO;
  496. goto unlock;
  497. }
  498. snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
  499. snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
  500. ret = set_format(subs, subs->cur_audiofmt);
  501. if (ret < 0)
  502. goto unlock;
  503. iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
  504. alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
  505. ret = snd_usb_init_sample_rate(subs->stream->chip,
  506. subs->cur_audiofmt->iface,
  507. alts,
  508. subs->cur_audiofmt,
  509. subs->cur_rate);
  510. if (ret < 0)
  511. goto unlock;
  512. if (subs->need_setup_ep) {
  513. ret = configure_endpoint(subs);
  514. if (ret < 0)
  515. goto unlock;
  516. subs->need_setup_ep = false;
  517. }
  518. /* some unit conversions in runtime */
  519. subs->data_endpoint->maxframesize =
  520. bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
  521. subs->data_endpoint->curframesize =
  522. bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
  523. /* reset the pointer */
  524. subs->hwptr_done = 0;
  525. subs->transfer_done = 0;
  526. subs->last_delay = 0;
  527. subs->last_frame_number = 0;
  528. runtime->delay = 0;
  529. /* for playback, submit the URBs now; otherwise, the first hwptr_done
  530. * updates for all URBs would happen at the same time when starting */
  531. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
  532. ret = start_endpoints(subs, true);
  533. unlock:
  534. up_read(&subs->stream->chip->shutdown_rwsem);
  535. return ret;
  536. }
  537. static struct snd_pcm_hardware snd_usb_hardware =
  538. {
  539. .info = SNDRV_PCM_INFO_MMAP |
  540. SNDRV_PCM_INFO_MMAP_VALID |
  541. SNDRV_PCM_INFO_BATCH |
  542. SNDRV_PCM_INFO_INTERLEAVED |
  543. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  544. SNDRV_PCM_INFO_PAUSE,
  545. .buffer_bytes_max = 1024 * 1024,
  546. .period_bytes_min = 64,
  547. .period_bytes_max = 512 * 1024,
  548. .periods_min = 2,
  549. .periods_max = 1024,
  550. };
  551. static int hw_check_valid_format(struct snd_usb_substream *subs,
  552. struct snd_pcm_hw_params *params,
  553. struct audioformat *fp)
  554. {
  555. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  556. struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  557. struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  558. struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  559. struct snd_mask check_fmts;
  560. unsigned int ptime;
  561. /* check the format */
  562. snd_mask_none(&check_fmts);
  563. check_fmts.bits[0] = (u32)fp->formats;
  564. check_fmts.bits[1] = (u32)(fp->formats >> 32);
  565. snd_mask_intersect(&check_fmts, fmts);
  566. if (snd_mask_empty(&check_fmts)) {
  567. hwc_debug(" > check: no supported format %d\n", fp->format);
  568. return 0;
  569. }
  570. /* check the channels */
  571. if (fp->channels < ct->min || fp->channels > ct->max) {
  572. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  573. return 0;
  574. }
  575. /* check the rate is within the range */
  576. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  577. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  578. return 0;
  579. }
  580. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  581. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  582. return 0;
  583. }
  584. /* check whether the period time is >= the data packet interval */
  585. if (subs->speed != USB_SPEED_FULL) {
  586. ptime = 125 * (1 << fp->datainterval);
  587. if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  588. hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
  589. return 0;
  590. }
  591. }
  592. return 1;
  593. }
  594. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  595. struct snd_pcm_hw_rule *rule)
  596. {
  597. struct snd_usb_substream *subs = rule->private;
  598. struct list_head *p;
  599. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  600. unsigned int rmin, rmax;
  601. int changed;
  602. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  603. changed = 0;
  604. rmin = rmax = 0;
  605. list_for_each(p, &subs->fmt_list) {
  606. struct audioformat *fp;
  607. fp = list_entry(p, struct audioformat, list);
  608. if (!hw_check_valid_format(subs, params, fp))
  609. continue;
  610. if (changed++) {
  611. if (rmin > fp->rate_min)
  612. rmin = fp->rate_min;
  613. if (rmax < fp->rate_max)
  614. rmax = fp->rate_max;
  615. } else {
  616. rmin = fp->rate_min;
  617. rmax = fp->rate_max;
  618. }
  619. }
  620. if (!changed) {
  621. hwc_debug(" --> get empty\n");
  622. it->empty = 1;
  623. return -EINVAL;
  624. }
  625. changed = 0;
  626. if (it->min < rmin) {
  627. it->min = rmin;
  628. it->openmin = 0;
  629. changed = 1;
  630. }
  631. if (it->max > rmax) {
  632. it->max = rmax;
  633. it->openmax = 0;
  634. changed = 1;
  635. }
  636. if (snd_interval_checkempty(it)) {
  637. it->empty = 1;
  638. return -EINVAL;
  639. }
  640. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  641. return changed;
  642. }
  643. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  644. struct snd_pcm_hw_rule *rule)
  645. {
  646. struct snd_usb_substream *subs = rule->private;
  647. struct list_head *p;
  648. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  649. unsigned int rmin, rmax;
  650. int changed;
  651. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  652. changed = 0;
  653. rmin = rmax = 0;
  654. list_for_each(p, &subs->fmt_list) {
  655. struct audioformat *fp;
  656. fp = list_entry(p, struct audioformat, list);
  657. if (!hw_check_valid_format(subs, params, fp))
  658. continue;
  659. if (changed++) {
  660. if (rmin > fp->channels)
  661. rmin = fp->channels;
  662. if (rmax < fp->channels)
  663. rmax = fp->channels;
  664. } else {
  665. rmin = fp->channels;
  666. rmax = fp->channels;
  667. }
  668. }
  669. if (!changed) {
  670. hwc_debug(" --> get empty\n");
  671. it->empty = 1;
  672. return -EINVAL;
  673. }
  674. changed = 0;
  675. if (it->min < rmin) {
  676. it->min = rmin;
  677. it->openmin = 0;
  678. changed = 1;
  679. }
  680. if (it->max > rmax) {
  681. it->max = rmax;
  682. it->openmax = 0;
  683. changed = 1;
  684. }
  685. if (snd_interval_checkempty(it)) {
  686. it->empty = 1;
  687. return -EINVAL;
  688. }
  689. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  690. return changed;
  691. }
  692. static int hw_rule_format(struct snd_pcm_hw_params *params,
  693. struct snd_pcm_hw_rule *rule)
  694. {
  695. struct snd_usb_substream *subs = rule->private;
  696. struct list_head *p;
  697. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  698. u64 fbits;
  699. u32 oldbits[2];
  700. int changed;
  701. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  702. fbits = 0;
  703. list_for_each(p, &subs->fmt_list) {
  704. struct audioformat *fp;
  705. fp = list_entry(p, struct audioformat, list);
  706. if (!hw_check_valid_format(subs, params, fp))
  707. continue;
  708. fbits |= fp->formats;
  709. }
  710. oldbits[0] = fmt->bits[0];
  711. oldbits[1] = fmt->bits[1];
  712. fmt->bits[0] &= (u32)fbits;
  713. fmt->bits[1] &= (u32)(fbits >> 32);
  714. if (!fmt->bits[0] && !fmt->bits[1]) {
  715. hwc_debug(" --> get empty\n");
  716. return -EINVAL;
  717. }
  718. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  719. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  720. return changed;
  721. }
  722. static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  723. struct snd_pcm_hw_rule *rule)
  724. {
  725. struct snd_usb_substream *subs = rule->private;
  726. struct audioformat *fp;
  727. struct snd_interval *it;
  728. unsigned char min_datainterval;
  729. unsigned int pmin;
  730. int changed;
  731. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  732. hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
  733. min_datainterval = 0xff;
  734. list_for_each_entry(fp, &subs->fmt_list, list) {
  735. if (!hw_check_valid_format(subs, params, fp))
  736. continue;
  737. min_datainterval = min(min_datainterval, fp->datainterval);
  738. }
  739. if (min_datainterval == 0xff) {
  740. hwc_debug(" --> get empty\n");
  741. it->empty = 1;
  742. return -EINVAL;
  743. }
  744. pmin = 125 * (1 << min_datainterval);
  745. changed = 0;
  746. if (it->min < pmin) {
  747. it->min = pmin;
  748. it->openmin = 0;
  749. changed = 1;
  750. }
  751. if (snd_interval_checkempty(it)) {
  752. it->empty = 1;
  753. return -EINVAL;
  754. }
  755. hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
  756. return changed;
  757. }
  758. /*
  759. * If the device supports unusual bit rates, does the request meet these?
  760. */
  761. static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
  762. struct snd_usb_substream *subs)
  763. {
  764. struct audioformat *fp;
  765. int *rate_list;
  766. int count = 0, needs_knot = 0;
  767. int err;
  768. kfree(subs->rate_list.list);
  769. subs->rate_list.list = NULL;
  770. list_for_each_entry(fp, &subs->fmt_list, list) {
  771. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
  772. return 0;
  773. count += fp->nr_rates;
  774. if (fp->rates & SNDRV_PCM_RATE_KNOT)
  775. needs_knot = 1;
  776. }
  777. if (!needs_knot)
  778. return 0;
  779. subs->rate_list.list = rate_list =
  780. kmalloc(sizeof(int) * count, GFP_KERNEL);
  781. if (!subs->rate_list.list)
  782. return -ENOMEM;
  783. subs->rate_list.count = count;
  784. subs->rate_list.mask = 0;
  785. count = 0;
  786. list_for_each_entry(fp, &subs->fmt_list, list) {
  787. int i;
  788. for (i = 0; i < fp->nr_rates; i++)
  789. rate_list[count++] = fp->rate_table[i];
  790. }
  791. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  792. &subs->rate_list);
  793. if (err < 0)
  794. return err;
  795. return 0;
  796. }
  797. /*
  798. * set up the runtime hardware information.
  799. */
  800. static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  801. {
  802. struct list_head *p;
  803. unsigned int pt, ptmin;
  804. int param_period_time_if_needed;
  805. int err;
  806. runtime->hw.formats = subs->formats;
  807. runtime->hw.rate_min = 0x7fffffff;
  808. runtime->hw.rate_max = 0;
  809. runtime->hw.channels_min = 256;
  810. runtime->hw.channels_max = 0;
  811. runtime->hw.rates = 0;
  812. ptmin = UINT_MAX;
  813. /* check min/max rates and channels */
  814. list_for_each(p, &subs->fmt_list) {
  815. struct audioformat *fp;
  816. fp = list_entry(p, struct audioformat, list);
  817. runtime->hw.rates |= fp->rates;
  818. if (runtime->hw.rate_min > fp->rate_min)
  819. runtime->hw.rate_min = fp->rate_min;
  820. if (runtime->hw.rate_max < fp->rate_max)
  821. runtime->hw.rate_max = fp->rate_max;
  822. if (runtime->hw.channels_min > fp->channels)
  823. runtime->hw.channels_min = fp->channels;
  824. if (runtime->hw.channels_max < fp->channels)
  825. runtime->hw.channels_max = fp->channels;
  826. if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  827. /* FIXME: there might be more than one audio formats... */
  828. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  829. fp->frame_size;
  830. }
  831. pt = 125 * (1 << fp->datainterval);
  832. ptmin = min(ptmin, pt);
  833. }
  834. err = snd_usb_autoresume(subs->stream->chip);
  835. if (err < 0)
  836. return err;
  837. param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
  838. if (subs->speed == USB_SPEED_FULL)
  839. /* full speed devices have fixed data packet interval */
  840. ptmin = 1000;
  841. if (ptmin == 1000)
  842. /* if period time doesn't go below 1 ms, no rules needed */
  843. param_period_time_if_needed = -1;
  844. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  845. ptmin, UINT_MAX);
  846. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  847. hw_rule_rate, subs,
  848. SNDRV_PCM_HW_PARAM_FORMAT,
  849. SNDRV_PCM_HW_PARAM_CHANNELS,
  850. param_period_time_if_needed,
  851. -1)) < 0)
  852. goto rep_err;
  853. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  854. hw_rule_channels, subs,
  855. SNDRV_PCM_HW_PARAM_FORMAT,
  856. SNDRV_PCM_HW_PARAM_RATE,
  857. param_period_time_if_needed,
  858. -1)) < 0)
  859. goto rep_err;
  860. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  861. hw_rule_format, subs,
  862. SNDRV_PCM_HW_PARAM_RATE,
  863. SNDRV_PCM_HW_PARAM_CHANNELS,
  864. param_period_time_if_needed,
  865. -1)) < 0)
  866. goto rep_err;
  867. if (param_period_time_if_needed >= 0) {
  868. err = snd_pcm_hw_rule_add(runtime, 0,
  869. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  870. hw_rule_period_time, subs,
  871. SNDRV_PCM_HW_PARAM_FORMAT,
  872. SNDRV_PCM_HW_PARAM_CHANNELS,
  873. SNDRV_PCM_HW_PARAM_RATE,
  874. -1);
  875. if (err < 0)
  876. goto rep_err;
  877. }
  878. if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
  879. goto rep_err;
  880. return 0;
  881. rep_err:
  882. snd_usb_autosuspend(subs->stream->chip);
  883. return err;
  884. }
  885. static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
  886. {
  887. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  888. struct snd_pcm_runtime *runtime = substream->runtime;
  889. struct snd_usb_substream *subs = &as->substream[direction];
  890. subs->interface = -1;
  891. subs->altset_idx = 0;
  892. runtime->hw = snd_usb_hardware;
  893. runtime->private_data = subs;
  894. subs->pcm_substream = substream;
  895. /* runtime PM is also done there */
  896. return setup_hw_info(runtime, subs);
  897. }
  898. static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
  899. {
  900. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  901. struct snd_usb_substream *subs = &as->substream[direction];
  902. stop_endpoints(subs, true);
  903. if (!as->chip->shutdown && subs->interface >= 0) {
  904. usb_set_interface(subs->dev, subs->interface, 0);
  905. subs->interface = -1;
  906. }
  907. subs->pcm_substream = NULL;
  908. snd_usb_autosuspend(subs->stream->chip);
  909. return 0;
  910. }
  911. /* Since a URB can handle only a single linear buffer, we must use double
  912. * buffering when the data to be transferred overflows the buffer boundary.
  913. * To avoid inconsistencies when updating hwptr_done, we use double buffering
  914. * for all URBs.
  915. */
  916. static void retire_capture_urb(struct snd_usb_substream *subs,
  917. struct urb *urb)
  918. {
  919. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  920. unsigned int stride, frames, bytes, oldptr;
  921. int i, period_elapsed = 0;
  922. unsigned long flags;
  923. unsigned char *cp;
  924. stride = runtime->frame_bits >> 3;
  925. for (i = 0; i < urb->number_of_packets; i++) {
  926. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  927. if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
  928. snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
  929. // continue;
  930. }
  931. bytes = urb->iso_frame_desc[i].actual_length;
  932. frames = bytes / stride;
  933. if (!subs->txfr_quirk)
  934. bytes = frames * stride;
  935. if (bytes % (runtime->sample_bits >> 3) != 0) {
  936. #ifdef CONFIG_SND_DEBUG_VERBOSE
  937. int oldbytes = bytes;
  938. #endif
  939. bytes = frames * stride;
  940. snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
  941. oldbytes, bytes);
  942. }
  943. /* update the current pointer */
  944. spin_lock_irqsave(&subs->lock, flags);
  945. oldptr = subs->hwptr_done;
  946. subs->hwptr_done += bytes;
  947. if (subs->hwptr_done >= runtime->buffer_size * stride)
  948. subs->hwptr_done -= runtime->buffer_size * stride;
  949. frames = (bytes + (oldptr % stride)) / stride;
  950. subs->transfer_done += frames;
  951. if (subs->transfer_done >= runtime->period_size) {
  952. subs->transfer_done -= runtime->period_size;
  953. period_elapsed = 1;
  954. }
  955. spin_unlock_irqrestore(&subs->lock, flags);
  956. /* copy a data chunk */
  957. if (oldptr + bytes > runtime->buffer_size * stride) {
  958. unsigned int bytes1 =
  959. runtime->buffer_size * stride - oldptr;
  960. memcpy(runtime->dma_area + oldptr, cp, bytes1);
  961. memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  962. } else {
  963. memcpy(runtime->dma_area + oldptr, cp, bytes);
  964. }
  965. }
  966. if (period_elapsed)
  967. snd_pcm_period_elapsed(subs->pcm_substream);
  968. }
  969. static void prepare_playback_urb(struct snd_usb_substream *subs,
  970. struct urb *urb)
  971. {
  972. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  973. struct snd_usb_endpoint *ep = subs->data_endpoint;
  974. struct snd_urb_ctx *ctx = urb->context;
  975. unsigned int counts, frames, bytes;
  976. int i, stride, period_elapsed = 0;
  977. unsigned long flags;
  978. stride = runtime->frame_bits >> 3;
  979. frames = 0;
  980. urb->number_of_packets = 0;
  981. spin_lock_irqsave(&subs->lock, flags);
  982. for (i = 0; i < ctx->packets; i++) {
  983. if (ctx->packet_size[i])
  984. counts = ctx->packet_size[i];
  985. else
  986. counts = snd_usb_endpoint_next_packet_size(ep);
  987. /* set up descriptor */
  988. urb->iso_frame_desc[i].offset = frames * stride;
  989. urb->iso_frame_desc[i].length = counts * stride;
  990. frames += counts;
  991. urb->number_of_packets++;
  992. subs->transfer_done += counts;
  993. if (subs->transfer_done >= runtime->period_size) {
  994. subs->transfer_done -= runtime->period_size;
  995. period_elapsed = 1;
  996. if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  997. if (subs->transfer_done > 0) {
  998. /* FIXME: fill-max mode is not
  999. * supported yet */
  1000. frames -= subs->transfer_done;
  1001. counts -= subs->transfer_done;
  1002. urb->iso_frame_desc[i].length =
  1003. counts * stride;
  1004. subs->transfer_done = 0;
  1005. }
  1006. i++;
  1007. if (i < ctx->packets) {
  1008. /* add a transfer delimiter */
  1009. urb->iso_frame_desc[i].offset =
  1010. frames * stride;
  1011. urb->iso_frame_desc[i].length = 0;
  1012. urb->number_of_packets++;
  1013. }
  1014. break;
  1015. }
  1016. }
  1017. if (period_elapsed &&
  1018. !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
  1019. break;
  1020. }
  1021. bytes = frames * stride;
  1022. if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  1023. /* err, the transferred area goes over buffer boundary. */
  1024. unsigned int bytes1 =
  1025. runtime->buffer_size * stride - subs->hwptr_done;
  1026. memcpy(urb->transfer_buffer,
  1027. runtime->dma_area + subs->hwptr_done, bytes1);
  1028. memcpy(urb->transfer_buffer + bytes1,
  1029. runtime->dma_area, bytes - bytes1);
  1030. } else {
  1031. memcpy(urb->transfer_buffer,
  1032. runtime->dma_area + subs->hwptr_done, bytes);
  1033. }
  1034. subs->hwptr_done += bytes;
  1035. if (subs->hwptr_done >= runtime->buffer_size * stride)
  1036. subs->hwptr_done -= runtime->buffer_size * stride;
  1037. /* update delay with exact number of samples queued */
  1038. runtime->delay = subs->last_delay;
  1039. runtime->delay += frames;
  1040. subs->last_delay = runtime->delay;
  1041. /* realign last_frame_number */
  1042. subs->last_frame_number = usb_get_current_frame_number(subs->dev);
  1043. subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
  1044. spin_unlock_irqrestore(&subs->lock, flags);
  1045. urb->transfer_buffer_length = bytes;
  1046. if (period_elapsed)
  1047. snd_pcm_period_elapsed(subs->pcm_substream);
  1048. }
  1049. /*
  1050. * process after playback data complete
  1051. * - decrease the delay count again
  1052. */
  1053. static void retire_playback_urb(struct snd_usb_substream *subs,
  1054. struct urb *urb)
  1055. {
  1056. unsigned long flags;
  1057. struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1058. int stride = runtime->frame_bits >> 3;
  1059. int processed = urb->transfer_buffer_length / stride;
  1060. int est_delay;
  1061. /* ignore the delay accounting when procssed=0 is given, i.e.
  1062. * silent payloads are procssed before handling the actual data
  1063. */
  1064. if (!processed)
  1065. return;
  1066. spin_lock_irqsave(&subs->lock, flags);
  1067. if (!subs->last_delay)
  1068. goto out; /* short path */
  1069. est_delay = snd_usb_pcm_delay(subs, runtime->rate);
  1070. /* update delay with exact number of samples played */
  1071. if (processed > subs->last_delay)
  1072. subs->last_delay = 0;
  1073. else
  1074. subs->last_delay -= processed;
  1075. runtime->delay = subs->last_delay;
  1076. /*
  1077. * Report when delay estimate is off by more than 2ms.
  1078. * The error should be lower than 2ms since the estimate relies
  1079. * on two reads of a counter updated every ms.
  1080. */
  1081. if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
  1082. snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
  1083. est_delay, subs->last_delay);
  1084. if (!subs->running) {
  1085. /* update last_frame_number for delay counting here since
  1086. * prepare_playback_urb won't be called during pause
  1087. */
  1088. subs->last_frame_number =
  1089. usb_get_current_frame_number(subs->dev) & 0xff;
  1090. }
  1091. out:
  1092. spin_unlock_irqrestore(&subs->lock, flags);
  1093. }
  1094. static int snd_usb_playback_open(struct snd_pcm_substream *substream)
  1095. {
  1096. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1097. }
  1098. static int snd_usb_playback_close(struct snd_pcm_substream *substream)
  1099. {
  1100. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1101. }
  1102. static int snd_usb_capture_open(struct snd_pcm_substream *substream)
  1103. {
  1104. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
  1105. }
  1106. static int snd_usb_capture_close(struct snd_pcm_substream *substream)
  1107. {
  1108. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  1109. }
  1110. static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
  1111. int cmd)
  1112. {
  1113. struct snd_usb_substream *subs = substream->runtime->private_data;
  1114. switch (cmd) {
  1115. case SNDRV_PCM_TRIGGER_START:
  1116. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1117. subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
  1118. subs->data_endpoint->retire_data_urb = retire_playback_urb;
  1119. subs->running = 1;
  1120. return 0;
  1121. case SNDRV_PCM_TRIGGER_STOP:
  1122. stop_endpoints(subs, false);
  1123. subs->running = 0;
  1124. return 0;
  1125. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1126. subs->data_endpoint->prepare_data_urb = NULL;
  1127. /* keep retire_data_urb for delay calculation */
  1128. subs->data_endpoint->retire_data_urb = retire_playback_urb;
  1129. subs->running = 0;
  1130. return 0;
  1131. }
  1132. return -EINVAL;
  1133. }
  1134. static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
  1135. int cmd)
  1136. {
  1137. int err;
  1138. struct snd_usb_substream *subs = substream->runtime->private_data;
  1139. switch (cmd) {
  1140. case SNDRV_PCM_TRIGGER_START:
  1141. err = start_endpoints(subs, false);
  1142. if (err < 0)
  1143. return err;
  1144. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1145. subs->running = 1;
  1146. return 0;
  1147. case SNDRV_PCM_TRIGGER_STOP:
  1148. stop_endpoints(subs, false);
  1149. subs->running = 0;
  1150. return 0;
  1151. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1152. subs->data_endpoint->retire_data_urb = NULL;
  1153. subs->running = 0;
  1154. return 0;
  1155. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1156. subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1157. subs->running = 1;
  1158. return 0;
  1159. }
  1160. return -EINVAL;
  1161. }
  1162. static struct snd_pcm_ops snd_usb_playback_ops = {
  1163. .open = snd_usb_playback_open,
  1164. .close = snd_usb_playback_close,
  1165. .ioctl = snd_pcm_lib_ioctl,
  1166. .hw_params = snd_usb_hw_params,
  1167. .hw_free = snd_usb_hw_free,
  1168. .prepare = snd_usb_pcm_prepare,
  1169. .trigger = snd_usb_substream_playback_trigger,
  1170. .pointer = snd_usb_pcm_pointer,
  1171. .page = snd_pcm_lib_get_vmalloc_page,
  1172. .mmap = snd_pcm_lib_mmap_vmalloc,
  1173. };
  1174. static struct snd_pcm_ops snd_usb_capture_ops = {
  1175. .open = snd_usb_capture_open,
  1176. .close = snd_usb_capture_close,
  1177. .ioctl = snd_pcm_lib_ioctl,
  1178. .hw_params = snd_usb_hw_params,
  1179. .hw_free = snd_usb_hw_free,
  1180. .prepare = snd_usb_pcm_prepare,
  1181. .trigger = snd_usb_substream_capture_trigger,
  1182. .pointer = snd_usb_pcm_pointer,
  1183. .page = snd_pcm_lib_get_vmalloc_page,
  1184. .mmap = snd_pcm_lib_mmap_vmalloc,
  1185. };
  1186. void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
  1187. {
  1188. snd_pcm_set_ops(pcm, stream,
  1189. stream == SNDRV_PCM_STREAM_PLAYBACK ?
  1190. &snd_usb_playback_ops : &snd_usb_capture_ops);
  1191. }