pcm.c 44 KB

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