Guest User

Untitled

a guest
Jan 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 105.29 KB | None | 0 0
  1. diff --git a/include/linux/usb/ch9.h b/include/linux/usb/ch9.h
  2. index 0fd3fbd..f7120e9 100644
  3. --- a/include/linux/usb/ch9.h
  4. +++ b/include/linux/usb/ch9.h
  5. @@ -377,11 +377,10 @@ struct usb_endpoint_descriptor {
  6. #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
  7. #define USB_ENDPOINT_DIR_MASK 0x80
  8.  
  9. -#define USB_ENDPOINT_SYNCTYPE 0x0c
  10. -#define USB_ENDPOINT_SYNC_NONE (0 << 2)
  11. -#define USB_ENDPOINT_SYNC_ASYNC (1 << 2)
  12. -#define USB_ENDPOINT_SYNC_ADAPTIVE (2 << 2)
  13. -#define USB_ENDPOINT_SYNC_SYNC (3 << 2)
  14. +#define USB_ENDPOINT_USAGE_MASK 0x30
  15. +#define USB_ENDPOINT_USAGE_DATA 0x00
  16. +#define USB_ENDPOINT_USAGE_FEEDBACK 0x10
  17. +#define USB_ENDPOINT_USAGE_IMPLICIT_FB 0x20 /* Implicit feedback Data endpoint */
  18.  
  19. #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
  20. #define USB_ENDPOINT_XFER_CONTROL 0
  21. @@ -390,6 +389,12 @@ struct usb_endpoint_descriptor {
  22. #define USB_ENDPOINT_XFER_INT 3
  23. #define USB_ENDPOINT_MAX_ADJUSTABLE 0x80
  24.  
  25. +#define USB_ENDPOINT_SYNCTYPE 0x0c
  26. +#define USB_ENDPOINT_SYNC_NONE (0 << 2)
  27. +#define USB_ENDPOINT_SYNC_ASYNC (1 << 2)
  28. +#define USB_ENDPOINT_SYNC_ADAPTIVE (2 << 2)
  29. +#define USB_ENDPOINT_SYNC_SYNC (3 << 2)
  30. +
  31. /*-------------------------------------------------------------------------*/
  32.  
  33. /**
  34. diff --git a/sound/usb/Makefile b/sound/usb/Makefile
  35. index cf9ed66..ac256dc 100644
  36. --- a/sound/usb/Makefile
  37. +++ b/sound/usb/Makefile
  38. @@ -3,16 +3,16 @@
  39. #
  40.  
  41. snd-usb-audio-objs := card.o \
  42. + clock.o \
  43. + endpoint.o \
  44. + format.o \
  45. + helper.o \
  46. mixer.o \
  47. mixer_quirks.o \
  48. + pcm.o \
  49. proc.o \
  50. quirks.o \
  51. - format.o \
  52. - endpoint.o \
  53. - urb.o \
  54. - pcm.o \
  55. - helper.o \
  56. - clock.o
  57. + stream.o
  58.  
  59. snd-usbmidi-lib-objs := midi.o
  60.  
  61. diff --git a/sound/usb/card.c b/sound/usb/card.c
  62. index 781d9e6..2029465 100644
  63. --- a/sound/usb/card.c
  64. +++ b/sound/usb/card.c
  65. @@ -65,9 +65,9 @@
  66. #include "helper.h"
  67. #include "debug.h"
  68. #include "pcm.h"
  69. -#include "urb.h"
  70. #include "format.h"
  71. #include "power.h"
  72. +#include "stream.h"
  73.  
  74. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  75. MODULE_DESCRIPTION("USB Audio");
  76. @@ -130,7 +130,7 @@ static void snd_usb_stream_disconnect(struct list_head *head)
  77. subs = &as->substream[idx];
  78. if (!subs->num_formats)
  79. continue;
  80. - snd_usb_release_substream_urbs(subs, 1);
  81. +
  82. subs->interface = -1;
  83. }
  84. }
  85. @@ -185,7 +185,7 @@ static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int int
  86. return -EINVAL;
  87. }
  88.  
  89. - if (! snd_usb_parse_audio_endpoints(chip, interface)) {
  90. + if (! snd_usb_parse_audio_interface(chip, interface)) {
  91. usb_set_interface(dev, interface, 0); /* reset the current interface */
  92. usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
  93. return -EINVAL;
  94. @@ -347,6 +347,7 @@ static int snd_usb_audio_create(struct usb_device *dev, int idx,
  95. chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  96. le16_to_cpu(dev->descriptor.idProduct));
  97. INIT_LIST_HEAD(&chip->pcm_list);
  98. + INIT_LIST_HEAD(&chip->ep_list);
  99. INIT_LIST_HEAD(&chip->midi_list);
  100. INIT_LIST_HEAD(&chip->mixer_list);
  101.  
  102. @@ -561,6 +562,10 @@ static void snd_usb_audio_disconnect(struct usb_device *dev,
  103. list_for_each(p, &chip->pcm_list) {
  104. snd_usb_stream_disconnect(p);
  105. }
  106. + /* release the endpoint resources */
  107. + list_for_each(p, &chip->ep_list) {
  108. + snd_usb_endpoint_free(p);
  109. + }
  110. /* release the midi resources */
  111. list_for_each(p, &chip->midi_list) {
  112. snd_usbmidi_disconnect(p);
  113. diff --git a/sound/usb/card.h b/sound/usb/card.h
  114. index ae4251d..63f9e09 100644
  115. --- a/sound/usb/card.h
  116. +++ b/sound/usb/card.h
  117. @@ -3,7 +3,7 @@
  118.  
  119. #define MAX_PACKS 20
  120. #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
  121. -#define MAX_URBS 8
  122. +#define MAX_URBS 16
  123. #define SYNC_URBS 4 /* always four urbs for sync */
  124. #define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */
  125.  
  126. @@ -28,21 +28,62 @@ struct audioformat {
  127. unsigned char clock; /* associated clock */
  128. };
  129.  
  130. -struct snd_usb_substream;
  131. +struct snd_usb_endpoint;
  132.  
  133. struct snd_urb_ctx {
  134. struct urb *urb;
  135. unsigned int buffer_size; /* size of data buffer, if data URB */
  136. - struct snd_usb_substream *subs;
  137. + struct snd_usb_endpoint *ep;
  138. int index; /* index for urb array */
  139. int packets; /* number of packets per urb */
  140. + int packet_size[MAX_PACKS_HS]; /* size of packets for next submission */
  141. };
  142.  
  143. -struct snd_urb_ops {
  144. - int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
  145. - int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
  146. - int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
  147. - int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
  148. +struct snd_usb_substream;
  149. +
  150. +struct snd_usb_endpoint {
  151. + struct snd_usb_audio *chip;
  152. +
  153. + int use_count;
  154. + int ep_num; /* the referenced endpoint number */
  155. + int type; /* SND_USB_ENDPOINT_TYPE_* */
  156. +
  157. + void (* prepare_data_urb) (struct snd_usb_substream *subs,
  158. + struct urb *urb);
  159. + void (* retire_data_urb) (struct snd_usb_substream *subs,
  160. + struct urb *urb);
  161. +
  162. + struct snd_usb_substream *data_subs;
  163. + struct snd_usb_endpoint *sync_master;
  164. + struct snd_usb_endpoint *sync_slave;
  165. +
  166. + struct snd_urb_ctx urb[MAX_URBS];
  167. + unsigned int nurbs; /* # urbs */
  168. + unsigned long active_mask; /* bitmask of active urbs */
  169. + unsigned long unlink_mask; /* bitmask of unlinked urbs */
  170. + char *syncbuf; /* sync buffer for all sync URBs */
  171. + dma_addr_t sync_dma; /* DMA address of syncbuf */
  172. +
  173. + unsigned int pipe; /* the data i/o pipe */
  174. + unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
  175. + unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
  176. + int freqshift; /* how much to shift the feedback value to get Q16.16 */
  177. + unsigned int freqmax; /* maximum sampling rate, used for buffer management */
  178. + unsigned int phase; /* phase accumulator */
  179. + unsigned int maxpacksize; /* max packet size in bytes */
  180. + unsigned int maxframesize; /* max packet size in frames */
  181. + unsigned int curpacksize; /* current packet size in bytes (for capture) */
  182. + unsigned int curframesize; /* current packet size in frames (for capture) */
  183. + unsigned int syncmaxsize; /* sync endpoint packet size */
  184. + unsigned int fill_max: 1; /* fill max packet size always */
  185. + unsigned int datainterval; /* log_2 of data packet interval */
  186. + unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
  187. + unsigned char silence_value;
  188. + unsigned int stride;
  189. + int iface, alt_idx;
  190. +
  191. + spinlock_t lock;
  192. + struct list_head list;
  193. };
  194.  
  195. struct snd_usb_substream {
  196. @@ -51,49 +92,27 @@ struct snd_usb_substream {
  197. struct snd_pcm_substream *pcm_substream;
  198. int direction; /* playback or capture */
  199. int interface; /* current interface */
  200. - int endpoint; /* assigned endpoint */
  201. struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
  202. unsigned int cur_rate; /* current rate (for hw_params callback) */
  203. unsigned int period_bytes; /* current period bytes (for hw_params callback) */
  204. unsigned int altset_idx; /* USB data format: index of alternate setting */
  205. - unsigned int datapipe; /* the data i/o pipe */
  206. - unsigned int syncpipe; /* 1 - async out or adaptive in */
  207. - unsigned int datainterval; /* log_2 of data packet interval */
  208. - unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
  209. - unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
  210. - unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
  211. - int freqshift; /* how much to shift the feedback value to get Q16.16 */
  212. - unsigned int freqmax; /* maximum sampling rate, used for buffer management */
  213. - unsigned int phase; /* phase accumulator */
  214. - unsigned int maxpacksize; /* max packet size in bytes */
  215. - unsigned int maxframesize; /* max packet size in frames */
  216. - unsigned int curpacksize; /* current packet size in bytes (for capture) */
  217. - unsigned int curframesize; /* current packet size in frames (for capture) */
  218. - unsigned int syncmaxsize; /* sync endpoint packet size */
  219. - unsigned int fill_max: 1; /* fill max packet size always */
  220. unsigned int txfr_quirk:1; /* allow sub-frame alignment */
  221. unsigned int fmt_type; /* USB audio format type (1-3) */
  222.  
  223. - unsigned int running: 1; /* running status */
  224. -
  225. unsigned int hwptr_done; /* processed byte position in the buffer */
  226. unsigned int transfer_done; /* processed frames since last period update */
  227. - unsigned long active_mask; /* bitmask of active urbs */
  228. - unsigned long unlink_mask; /* bitmask of unlinked urbs */
  229.  
  230. - unsigned int nurbs; /* # urbs */
  231. - struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */
  232. - struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */
  233. - char *syncbuf; /* sync buffer for all sync URBs */
  234. - dma_addr_t sync_dma; /* DMA address of syncbuf */
  235. + /* data and sync endpoints for this stream */
  236. + struct snd_usb_endpoint *data_endpoint;
  237. + struct snd_usb_endpoint *sync_endpoint;
  238. + unsigned int data_endpoint_started:1;
  239. + unsigned int sync_endpoint_started:1;
  240.  
  241. u64 formats; /* format bitmasks (all or'ed) */
  242. unsigned int num_formats; /* number of supported audio formats (list) */
  243. struct list_head fmt_list; /* format list */
  244. struct snd_pcm_hw_constraint_list rate_list; /* limited rates */
  245. spinlock_t lock;
  246. -
  247. - struct snd_urb_ops ops; /* callbacks (must be filled at init) */
  248. };
  249.  
  250. struct snd_usb_stream {
  251. diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c
  252. index 7d46e48..2dd7b50 100644
  253. --- a/sound/usb/endpoint.c
  254. +++ b/sound/usb/endpoint.c
  255. @@ -15,436 +15,847 @@
  256. *
  257. */
  258.  
  259. +#include <linux/gfp.h>
  260. #include <linux/init.h>
  261. -#include <linux/slab.h>
  262. #include <linux/usb.h>
  263. #include <linux/usb/audio.h>
  264. -#include <linux/usb/audio-v2.h>
  265. +#include <linux/slab.h>
  266.  
  267. #include <sound/core.h>
  268. #include <sound/pcm.h>
  269. +#include <sound/pcm_params.h>
  270.  
  271. #include "usbaudio.h"
  272. +#include "helper.h"
  273. #include "card.h"
  274. -#include "proc.h"
  275. -#include "quirks.h"
  276. #include "endpoint.h"
  277. -#include "urb.h"
  278. #include "pcm.h"
  279. -#include "helper.h"
  280. -#include "format.h"
  281. -#include "clock.h"
  282.  
  283. /*
  284. - * free a substream
  285. + * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
  286. + * this will overflow at approx 524 kHz
  287. */
  288. -static void free_substream(struct snd_usb_substream *subs)
  289. +static inline unsigned get_usb_full_speed_rate(unsigned int rate)
  290. +{
  291. + return ((rate << 13) + 62) / 125;
  292. +}
  293. +
  294. +/*
  295. + * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
  296. + * this will overflow at approx 4 MHz
  297. + */
  298. +static unsigned get_usb_high_speed_rate(unsigned int rate)
  299. +{
  300. + return ((rate << 10) + 62) / 125;
  301. +}
  302. +
  303. +int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep)
  304. +{
  305. + return ep->sync_master &&
  306. + ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA &&
  307. + ep->type == SND_USB_ENDPOINT_TYPE_DATA &&
  308. + usb_pipeout(ep->pipe);
  309. +}
  310. +
  311. +/* determine the number of frames in the next packet */
  312. +static int next_packet_size(struct snd_usb_endpoint *ep)
  313. {
  314. - struct list_head *p, *n;
  315. -
  316. - if (!subs->num_formats)
  317. - return; /* not initialized */
  318. - list_for_each_safe(p, n, &subs->fmt_list) {
  319. - struct audioformat *fp = list_entry(p, struct audioformat, list);
  320. - kfree(fp->rate_table);
  321. - kfree(fp);
  322. + if (ep->fill_max)
  323. + return ep->maxframesize;
  324. + else {
  325. + ep->phase = (ep->phase & 0xffff)
  326. + + (ep->freqm << ep->datainterval);
  327. + return min(ep->phase >> 16, ep->maxframesize);
  328. }
  329. - kfree(subs->rate_list.list);
  330. }
  331.  
  332. +static void retire_outbound_urb(struct snd_usb_endpoint *ep,
  333. + struct snd_urb_ctx *urb_ctx)
  334. +{
  335. + if (ep->retire_data_urb)
  336. + ep->retire_data_urb(ep->data_subs, urb_ctx->urb);
  337. +}
  338. +
  339. +static void retire_inbound_urb(struct snd_usb_endpoint *ep,
  340. + struct snd_urb_ctx *urb_ctx)
  341. +{
  342. + struct urb *urb = urb_ctx->urb;
  343. +
  344. + if (ep->sync_slave)
  345. + snd_usb_handle_sync_urb(ep->sync_slave, ep, urb);
  346. +
  347. + if (ep->retire_data_urb)
  348. + ep->retire_data_urb(ep->data_subs, urb);
  349. +}
  350. +
  351. +static void prepare_outbound_urb_sizes(struct snd_usb_endpoint *ep,
  352. + struct snd_urb_ctx *ctx)
  353. +{
  354. + int i;
  355. +
  356. + for (i = 0; i < ctx->packets; ++i)
  357. + ctx->packet_size[i] = next_packet_size(ep);
  358. +}
  359.  
  360. /*
  361. - * free a usb stream instance
  362. + * Prepare a PLAYBACK urb for submission to the bus.
  363. */
  364. -static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
  365. +static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
  366. + struct snd_urb_ctx *ctx)
  367. {
  368. - free_substream(&stream->substream[0]);
  369. - free_substream(&stream->substream[1]);
  370. - list_del(&stream->list);
  371. - kfree(stream);
  372. + int i;
  373. + struct urb *urb = ctx->urb;
  374. + unsigned char *cp = urb->transfer_buffer;
  375. +
  376. + urb->dev = ep->chip->dev; /* we need to set this at each time */
  377. +
  378. + switch (ep->type) {
  379. + case SND_USB_ENDPOINT_TYPE_DATA:
  380. + if (ep->prepare_data_urb) {
  381. + ep->prepare_data_urb(ep->data_subs, urb);
  382. + } else {
  383. + /* no data provider, so send silence */
  384. + unsigned int offs = 0;
  385. + for (i = 0; i < ctx->packets; ++i) {
  386. + int counts = ctx->packet_size[i];
  387. + urb->iso_frame_desc[i].offset = offs * ep->stride;
  388. + urb->iso_frame_desc[i].length = counts * ep->stride;
  389. + offs += counts;
  390. + }
  391. +
  392. + urb->number_of_packets = ctx->packets;
  393. + urb->transfer_buffer_length = offs * ep->stride;
  394. + memset(urb->transfer_buffer, ep->silence_value,
  395. + offs * ep->stride);
  396. + }
  397. + break;
  398. +
  399. + case SND_USB_ENDPOINT_TYPE_SYNC:
  400. + if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
  401. + /*
  402. + * fill the length and offset of each urb descriptor.
  403. + * the fixed 12.13 frequency is passed as 16.16 through the pipe.
  404. + */
  405. + urb->iso_frame_desc[0].length = 4;
  406. + urb->iso_frame_desc[0].offset = 0;
  407. + cp[0] = ep->freqn;
  408. + cp[1] = ep->freqn >> 8;
  409. + cp[2] = ep->freqn >> 16;
  410. + cp[3] = ep->freqn >> 24;
  411. + } else {
  412. + /*
  413. + * fill the length and offset of each urb descriptor.
  414. + * the fixed 10.14 frequency is passed through the pipe.
  415. + */
  416. + urb->iso_frame_desc[0].length = 3;
  417. + urb->iso_frame_desc[0].offset = 0;
  418. + cp[0] = ep->freqn >> 2;
  419. + cp[1] = ep->freqn >> 10;
  420. + cp[2] = ep->freqn >> 18;
  421. + }
  422. +
  423. + break;
  424. + }
  425. }
  426.  
  427. -static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
  428. +/*
  429. + * Prepare a CAPTURE or SYNC urb for submission to the bus.
  430. + */
  431. +static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
  432. + struct snd_urb_ctx *urb_ctx)
  433. {
  434. - struct snd_usb_stream *stream = pcm->private_data;
  435. - if (stream) {
  436. - stream->pcm = NULL;
  437. - snd_usb_audio_stream_free(stream);
  438. + int i, offs;
  439. + struct urb *urb = urb_ctx->urb;
  440. +
  441. + urb->dev = ep->chip->dev; /* we need to set this at each time */
  442. +
  443. + switch (ep->type) {
  444. + case SND_USB_ENDPOINT_TYPE_DATA:
  445. + offs = 0;
  446. + for (i = 0; i < urb_ctx->packets; i++) {
  447. + urb->iso_frame_desc[i].offset = offs;
  448. + urb->iso_frame_desc[i].length = ep->curpacksize;
  449. + offs += ep->curpacksize;
  450. + }
  451. +
  452. + urb->transfer_buffer_length = offs;
  453. + urb->number_of_packets = urb_ctx->packets;
  454. + break;
  455. +
  456. + case SND_USB_ENDPOINT_TYPE_SYNC:
  457. + urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);
  458. + urb->iso_frame_desc[0].offset = 0;
  459. + break;
  460. }
  461. }
  462.  
  463. -
  464. /*
  465. - * add this endpoint to the chip instance.
  466. - * if a stream with the same endpoint already exists, append to it.
  467. - * if not, create a new pcm stream.
  468. + * complete callback for urbs
  469. */
  470. -int snd_usb_add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)
  471. +static void snd_complete_urb(struct urb *urb)
  472. {
  473. - struct list_head *p;
  474. - struct snd_usb_stream *as;
  475. - struct snd_usb_substream *subs;
  476. - struct snd_pcm *pcm;
  477. + struct snd_urb_ctx *ctx = urb->context;
  478. + struct snd_usb_endpoint *ep = ctx->ep;
  479. int err;
  480.  
  481. - list_for_each(p, &chip->pcm_list) {
  482. - as = list_entry(p, struct snd_usb_stream, list);
  483. - if (as->fmt_type != fp->fmt_type)
  484. - continue;
  485. - subs = &as->substream[stream];
  486. - if (!subs->endpoint)
  487. - continue;
  488. - if (subs->endpoint == fp->endpoint) {
  489. - list_add_tail(&fp->list, &subs->fmt_list);
  490. - subs->num_formats++;
  491. - subs->formats |= fp->formats;
  492. - return 0;
  493. + if (ep->use_count == 0)
  494. + goto exit_clear;
  495. +
  496. + if (usb_pipeout(ep->pipe)) {
  497. + retire_outbound_urb(ep, ctx);
  498. + if (ep->use_count == 0) /* can be stopped during retire callback */
  499. + goto exit_clear;
  500. +
  501. + if (snd_usb_endpoint_implict_feedback_sink(ep))
  502. + goto exit_clear;
  503. +
  504. + prepare_outbound_urb_sizes(ep, ctx);
  505. + prepare_outbound_urb(ep, ctx);
  506. + } else {
  507. + retire_inbound_urb(ep, ctx);
  508. + if (ep->use_count == 0) /* can be stopped during retire callback */
  509. + goto exit_clear;
  510. +
  511. + prepare_inbound_urb(ep, ctx);
  512. + }
  513. +
  514. + err = usb_submit_urb(urb, GFP_ATOMIC);
  515. + if (err == 0)
  516. + return;
  517. +
  518. + snd_printk(KERN_ERR "cannot submit urb (err = %d)\n", err);
  519. + //snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  520. +
  521. +exit_clear:
  522. + clear_bit(ctx->index, &ep->active_mask);
  523. +}
  524. +
  525. +struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip,
  526. + struct usb_host_interface *alts,
  527. + int ep_num, int direction, int type)
  528. +{
  529. + struct list_head *p;
  530. + struct snd_usb_endpoint *ep;
  531. + int is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK;
  532. +
  533. + list_for_each(p, &chip->ep_list) {
  534. + ep = list_entry(p, struct snd_usb_endpoint, list);
  535. + if (ep->ep_num == ep_num) {
  536. + snd_printk("Re-using EP %x @%p\n", ep_num, ep);
  537. + return ep;
  538. }
  539. }
  540. - /* look for an empty stream */
  541. - list_for_each(p, &chip->pcm_list) {
  542. - as = list_entry(p, struct snd_usb_stream, list);
  543. - if (as->fmt_type != fp->fmt_type)
  544. - continue;
  545. - subs = &as->substream[stream];
  546. - if (subs->endpoint)
  547. - continue;
  548. - err = snd_pcm_new_stream(as->pcm, stream, 1);
  549. - if (err < 0)
  550. - return err;
  551. - snd_usb_init_substream(as, stream, fp);
  552. - return 0;
  553. +
  554. + snd_printk("Creating new %s %s endpoint #%x\n",
  555. + is_playback ? "playback" : "capture",
  556. + type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync",
  557. + ep_num);
  558. +
  559. + ep = kzalloc(sizeof(*ep), GFP_KERNEL);
  560. + if (!ep)
  561. + return NULL;
  562. +
  563. + spin_lock_init(&ep->lock);
  564. +
  565. + ep->chip = chip;
  566. + ep->type = type;
  567. + ep->ep_num = ep_num;
  568. + ep->iface = alts->desc.bInterfaceNumber;
  569. + ep->alt_idx = alts->desc.bAlternateSetting;
  570. +
  571. + /* select the alt setting once so the endpoints become valid */
  572. + usb_set_interface(ep->chip->dev, ep->iface, ep->alt_idx);
  573. +
  574. + ep_num &= USB_ENDPOINT_NUMBER_MASK;
  575. +
  576. + if (is_playback)
  577. + ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
  578. + else
  579. + ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
  580. +
  581. + switch (type) {
  582. + case SND_USB_ENDPOINT_TYPE_DATA:
  583. + break;
  584. + case SND_USB_ENDPOINT_TYPE_SYNC:
  585. + if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  586. + get_endpoint(alts, 1)->bRefresh >= 1 &&
  587. + get_endpoint(alts, 1)->bRefresh <= 9)
  588. + ep->syncinterval = get_endpoint(alts, 1)->bRefresh;
  589. + else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
  590. + ep->syncinterval = 1;
  591. + else if (get_endpoint(alts, 1)->bInterval >= 1 &&
  592. + get_endpoint(alts, 1)->bInterval <= 16)
  593. + ep->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
  594. + else
  595. + ep->syncinterval = 3;
  596. + ep->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
  597. +
  598. + break;
  599. }
  600.  
  601. - /* create a new pcm */
  602. - as = kzalloc(sizeof(*as), GFP_KERNEL);
  603. - if (!as)
  604. - return -ENOMEM;
  605. - as->pcm_index = chip->pcm_devs;
  606. - as->chip = chip;
  607. - as->fmt_type = fp->fmt_type;
  608. - err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
  609. - stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
  610. - stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
  611. - &pcm);
  612. - if (err < 0) {
  613. - kfree(as);
  614. - return err;
  615. + list_add_tail(&ep->list, &chip->ep_list);
  616. +
  617. + return ep;
  618. +}
  619. +
  620. +static const char *usb_error_string(int err)
  621. +{
  622. + switch (err) {
  623. + case -ENODEV:
  624. + return "no device";
  625. + case -ENOENT:
  626. + return "endpoint not enabled";
  627. + case -EPIPE:
  628. + return "endpoint stalled";
  629. + case -ENOSPC:
  630. + return "not enough bandwidth";
  631. + case -ESHUTDOWN:
  632. + return "device disabled";
  633. + case -EHOSTUNREACH:
  634. + return "device suspended";
  635. + case -EINVAL:
  636. + case -EAGAIN:
  637. + case -EFBIG:
  638. + case -EMSGSIZE:
  639. + return "internal error";
  640. + default:
  641. + return "unknown error";
  642. }
  643. - as->pcm = pcm;
  644. - pcm->private_data = as;
  645. - pcm->private_free = snd_usb_audio_pcm_free;
  646. - pcm->info_flags = 0;
  647. - if (chip->pcm_devs > 0)
  648. - sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
  649. - else
  650. - strcpy(pcm->name, "USB Audio");
  651. +}
  652.  
  653. - snd_usb_init_substream(as, stream, fp);
  654. +/*
  655. + * release a urb data
  656. + */
  657. +static void release_urb_ctx(struct snd_urb_ctx *u)
  658. +{
  659. + if (!u->urb)
  660. + return;
  661. +
  662. + if (u->buffer_size)
  663. + usb_free_coherent(u->ep->chip->dev, u->buffer_size,
  664. + u->urb->transfer_buffer,
  665. + u->urb->transfer_dma);
  666. + usb_free_urb(u->urb);
  667. + u->urb = NULL;
  668. +}
  669.  
  670. - list_add(&as->list, &chip->pcm_list);
  671. - chip->pcm_devs++;
  672. +/*
  673. + * wait until all urbs are processed.
  674. + */
  675. +static int wait_clear_urbs(struct snd_usb_endpoint *ep)
  676. +{
  677. + unsigned long end_time = jiffies + msecs_to_jiffies(1000);
  678. + unsigned int i;
  679. + int alive;
  680.  
  681. - snd_usb_proc_pcm_format_add(as);
  682. + do {
  683. + alive = 0;
  684. + for (i = 0; i < ep->nurbs; i++)
  685. + if (test_bit(i, &ep->active_mask))
  686. + alive++;
  687. +
  688. + if (! alive)
  689. + break;
  690. +
  691. + schedule_timeout_uninterruptible(1);
  692. + } while (time_before(jiffies, end_time));
  693. +
  694. + if (alive)
  695. + snd_printk(KERN_ERR "timeout: still %d active urbs on EP #%x\n",
  696. + alive, ep->ep_num);
  697.  
  698. return 0;
  699. }
  700.  
  701. -static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
  702. - struct usb_host_interface *alts,
  703. - int protocol, int iface_no)
  704. +/*
  705. + * unlink active urbs.
  706. + */
  707. +static int deactivate_urbs(struct snd_usb_endpoint *ep, int force, int can_sleep)
  708. {
  709. - /* parsed with a v1 header here. that's ok as we only look at the
  710. - * header first which is the same for both versions */
  711. - struct uac_iso_endpoint_descriptor *csep;
  712. - struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  713. - int attributes = 0;
  714. -
  715. - csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
  716. -
  717. - /* Creamware Noah has this descriptor after the 2nd endpoint */
  718. - if (!csep && altsd->bNumEndpoints >= 2)
  719. - csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
  720. -
  721. - if (!csep || csep->bLength < 7 ||
  722. - csep->bDescriptorSubtype != UAC_EP_GENERAL) {
  723. - snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
  724. - " class specific endpoint descriptor\n",
  725. - chip->dev->devnum, iface_no,
  726. - altsd->bAlternateSetting);
  727. + unsigned int i;
  728. + int async;
  729. +
  730. + if (!force && ep->chip->shutdown) /* to be sure... */
  731. + return -EBADFD;
  732. +
  733. + async = !can_sleep && ep->chip->async_unlink;
  734. +
  735. + if (!async && in_interrupt())
  736. return 0;
  737. +
  738. + for (i = 0; i < ep->nurbs; i++) {
  739. + if (test_bit(i, &ep->active_mask)) {
  740. + if (!test_and_set_bit(i, &ep->unlink_mask)) {
  741. + struct urb *u = ep->urb[i].urb;
  742. + if (async)
  743. + usb_unlink_urb(u);
  744. + else
  745. + usb_kill_urb(u);
  746. + }
  747. + }
  748. }
  749.  
  750. - if (protocol == UAC_VERSION_1) {
  751. - attributes = csep->bmAttributes;
  752. + return 0;
  753. +}
  754. +
  755. +/*
  756. + * release an endpoint's urbs
  757. + */
  758. +static void release_urbs(struct snd_usb_endpoint *ep, int force)
  759. +{
  760. + int i;
  761. +
  762. + /* route incoming urbs to nirvana */
  763. + ep->retire_data_urb = NULL;
  764. + ep->prepare_data_urb = NULL;
  765. +
  766. + /* stop urbs */
  767. + deactivate_urbs(ep, force, 1);
  768. + wait_clear_urbs(ep);
  769. +
  770. + for (i = 0; i < ep->nurbs; i++)
  771. + release_urb_ctx(&ep->urb[i]);
  772. +
  773. + if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
  774. + usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
  775. + ep->syncbuf, ep->sync_dma);
  776. + ep->syncbuf = NULL;
  777. + ep->nurbs = 0;
  778. +}
  779. +
  780. +static int data_ep_set_params(struct snd_usb_endpoint *ep,
  781. + struct snd_pcm_hw_params *hw_params,
  782. + struct audioformat *fmt,
  783. + struct snd_usb_endpoint *sync_ep)
  784. +{
  785. + unsigned int maxsize, i, urb_packs, total_packs, packs_per_ms;
  786. + int period_bytes = params_period_bytes(hw_params);
  787. + int format = params_format(hw_params);
  788. + int is_playback = usb_pipeout(ep->pipe);
  789. + int frame_bits = snd_pcm_format_physical_width(params_format(hw_params)) *
  790. + params_channels(hw_params);
  791. +
  792. + ep->datainterval = fmt->datainterval;
  793. + ep->stride = frame_bits >> 3;
  794. + ep->silence_value = format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0;
  795. +
  796. + /* calculate max. frequency */
  797. + if (ep->maxpacksize) {
  798. + /* whatever fits into a max. size packet */
  799. + maxsize = ep->maxpacksize;
  800. + ep->freqmax = (maxsize / (frame_bits >> 3))
  801. + << (16 - ep->datainterval);
  802. } else {
  803. - struct uac2_iso_endpoint_descriptor *csep2 =
  804. - (struct uac2_iso_endpoint_descriptor *) csep;
  805. + /* no max. packet size: just take 25% higher than nominal */
  806. + ep->freqmax = ep->freqn + (ep->freqn >> 2);
  807. + maxsize = ((ep->freqmax + 0xffff) * (frame_bits >> 3))
  808. + >> (16 - ep->datainterval);
  809. + }
  810. +
  811. + if (ep->fill_max)
  812. + ep->curpacksize = ep->maxpacksize;
  813. + else
  814. + ep->curpacksize = maxsize;
  815.  
  816. - attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
  817. + if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL)
  818. + packs_per_ms = 8 >> ep->datainterval;
  819. + else
  820. + packs_per_ms = 1;
  821. +
  822. + if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) {
  823. + urb_packs = max(ep->chip->nrpacks, 1);
  824. + urb_packs = min(urb_packs, (unsigned int) MAX_PACKS);
  825. + } else {
  826. + urb_packs = 1;
  827. + }
  828. +
  829. + urb_packs *= packs_per_ms;
  830. +
  831. + if (sync_ep && !snd_usb_endpoint_implict_feedback_sink(ep))
  832. + urb_packs = min(urb_packs, 1U << sync_ep->syncinterval);
  833. +
  834. + /* decide how many packets to be used */
  835. + if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) {
  836. + unsigned int minsize, maxpacks;
  837. + /* determine how small a packet can be */
  838. + minsize = (ep->freqn >> (16 - ep->datainterval))
  839. + * (frame_bits >> 3);
  840. + /* with sync from device, assume it can be 12% lower */
  841. + if (sync_ep)
  842. + minsize -= minsize >> 3;
  843. + minsize = max(minsize, 1u);
  844. + total_packs = (period_bytes + minsize - 1) / minsize;
  845. + /* we need at least two URBs for queueing */
  846. + if (total_packs < 2) {
  847. + total_packs = 2;
  848. + } else {
  849. + /* and we don't want too long a queue either */
  850. + maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
  851. + total_packs = min(total_packs, maxpacks);
  852. + }
  853. + } else {
  854. + while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
  855. + urb_packs >>= 1;
  856. + total_packs = MAX_URBS * urb_packs;
  857. + }
  858. +
  859. + snd_printk("is_playback? %d impf %d, total_packs %d urb_packs %d\n",
  860. + is_playback, snd_usb_endpoint_implict_feedback_sink(ep), total_packs, urb_packs);
  861. +
  862. + ep->nurbs = (total_packs + urb_packs - 1) / urb_packs;
  863. + if (ep->nurbs > MAX_URBS) {
  864. + /* too much... */
  865. + ep->nurbs = MAX_URBS;
  866. + total_packs = MAX_URBS * urb_packs;
  867. + } else if (ep->nurbs < 2) {
  868. + /* too little - we need at least two packets
  869. + * to ensure contiguous playback/capture
  870. + */
  871. + ep->nurbs = 2;
  872. + }
  873.  
  874. - /* emulate the endpoint attributes of a v1 device */
  875. - if (csep2->bmControls & UAC2_CONTROL_PITCH)
  876. - attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
  877. + /* allocate and initialize data urbs */
  878. + for (i = 0; i < ep->nurbs; i++) {
  879. + struct snd_urb_ctx *u = &ep->urb[i];
  880. + u->index = i;
  881. + u->ep = ep;
  882. + u->packets = (i + 1) * total_packs / ep->nurbs
  883. + - i * total_packs / ep->nurbs;
  884. + u->buffer_size = maxsize * u->packets;
  885. + snd_printk(" ep %p:: buffer_size %d maxsize %d packets %d\n", ep, u->buffer_size, maxsize, u->packets);
  886. + if (fmt->fmt_type == UAC_FORMAT_TYPE_II)
  887. + u->packets++; /* for transfer delimiter */
  888. + u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
  889. + if (!u->urb)
  890. + goto out_of_memory;
  891. +
  892. + u->urb->transfer_buffer =
  893. + usb_alloc_coherent(ep->chip->dev, u->buffer_size,
  894. + GFP_KERNEL, &u->urb->transfer_dma);
  895. + if (!u->urb->transfer_buffer)
  896. + goto out_of_memory;
  897. + u->urb->pipe = ep->pipe;
  898. + u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  899. + u->urb->interval = 1 << ep->datainterval;
  900. + u->urb->context = u;
  901. + u->urb->complete = snd_complete_urb;
  902. }
  903.  
  904. - return attributes;
  905. + return 0;
  906. +
  907. +out_of_memory:
  908. + release_urbs(ep, 0);
  909. + return -ENOMEM;
  910. }
  911.  
  912. -static struct uac2_input_terminal_descriptor *
  913. - snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  914. - int terminal_id)
  915. +static int sync_ep_set_params(struct snd_usb_endpoint *ep,
  916. + struct snd_pcm_hw_params *hw_params,
  917. + struct audioformat *fmt)
  918. {
  919. - struct uac2_input_terminal_descriptor *term = NULL;
  920. -
  921. - while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  922. - ctrl_iface->extralen,
  923. - term, UAC_INPUT_TERMINAL))) {
  924. - if (term->bTerminalID == terminal_id)
  925. - return term;
  926. + int i;
  927. +
  928. + ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4,
  929. + GFP_KERNEL, &ep->sync_dma);
  930. + if (!ep->syncbuf)
  931. + goto out_of_memory;
  932. +
  933. + for (i = 0; i < SYNC_URBS; i++) {
  934. + struct snd_urb_ctx *u = &ep->urb[i];
  935. + u->index = i;
  936. + u->ep = ep;
  937. + u->packets = 1;
  938. + u->urb = usb_alloc_urb(1, GFP_KERNEL);
  939. + if (!u->urb)
  940. + goto out_of_memory;
  941. + u->urb->transfer_buffer = ep->syncbuf + i * 4;
  942. + u->urb->transfer_dma = ep->sync_dma + i * 4;
  943. + u->urb->transfer_buffer_length = 4;
  944. + u->urb->pipe = ep->pipe;
  945. + u->urb->transfer_flags = URB_ISO_ASAP |
  946. + URB_NO_TRANSFER_DMA_MAP;
  947. + u->urb->number_of_packets = 1;
  948. + u->urb->interval = 1 << ep->syncinterval;
  949. + u->urb->context = u;
  950. + u->urb->complete = snd_complete_urb;
  951. }
  952.  
  953. - return NULL;
  954. + ep->nurbs = SYNC_URBS;
  955. +
  956. + return 0;
  957. +
  958. +out_of_memory:
  959. + release_urbs(ep, 0);
  960. + return -ENOMEM;
  961. }
  962.  
  963. -static struct uac2_output_terminal_descriptor *
  964. - snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  965. - int terminal_id)
  966. +int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,
  967. + struct snd_pcm_hw_params *hw_params,
  968. + struct audioformat *fmt,
  969. + struct snd_usb_endpoint *sync_ep)
  970. {
  971. - struct uac2_output_terminal_descriptor *term = NULL;
  972. + int err;
  973. +
  974. + if (ep->use_count > 0) {
  975. + snd_printk(KERN_WARNING "Unable to change format on ep #%x: already in use\n",
  976. + ep->ep_num);
  977. + return -EBUSY;
  978. + }
  979.  
  980. - while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  981. - ctrl_iface->extralen,
  982. - term, UAC_OUTPUT_TERMINAL))) {
  983. - if (term->bTerminalID == terminal_id)
  984. - return term;
  985. + ep->datainterval = fmt->datainterval;
  986. + ep->maxpacksize = fmt->maxpacksize;
  987. + ep->fill_max = fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX;
  988. +
  989. + if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL)
  990. + ep->freqn = get_usb_full_speed_rate(params_rate(hw_params));
  991. + else
  992. + ep->freqn = get_usb_high_speed_rate(params_rate(hw_params));
  993. +
  994. + /* calculate the frequency in 16.16 format */
  995. + ep->freqm = ep->freqn;
  996. + ep->freqshift = INT_MIN;
  997. +
  998. + ep->phase = 0;
  999. +
  1000. + switch (ep->type) {
  1001. + case SND_USB_ENDPOINT_TYPE_DATA:
  1002. + err = data_ep_set_params(ep, hw_params, fmt, sync_ep);
  1003. + break;
  1004. + case SND_USB_ENDPOINT_TYPE_SYNC:
  1005. + err = sync_ep_set_params(ep, hw_params, fmt);
  1006. + break;
  1007. + default:
  1008. + err = -EINVAL;
  1009. }
  1010.  
  1011. - return NULL;
  1012. + snd_printk("Setting params for ep #%x (type %d, %d urbs) -> %d\n",
  1013. + ep->ep_num, ep->type, ep->nurbs, err);
  1014. +
  1015. + return err;
  1016. }
  1017.  
  1018. -int snd_usb_parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
  1019. +int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
  1020. {
  1021. - struct usb_device *dev;
  1022. - struct usb_interface *iface;
  1023. - struct usb_host_interface *alts;
  1024. - struct usb_interface_descriptor *altsd;
  1025. - int i, altno, err, stream;
  1026. - int format = 0, num_channels = 0;
  1027. - struct audioformat *fp = NULL;
  1028. - int num, protocol, clock = 0;
  1029. - struct uac_format_type_i_continuous_descriptor *fmt;
  1030. -
  1031. - dev = chip->dev;
  1032. -
  1033. - /* parse the interface's altsettings */
  1034. - iface = usb_ifnum_to_if(dev, iface_no);
  1035. -
  1036. - num = iface->num_altsetting;
  1037. -
  1038. - /*
  1039. - * Dallas DS4201 workaround: It presents 5 altsettings, but the last
  1040. - * one misses syncpipe, and does not produce any sound.
  1041. + int err;
  1042. + unsigned int i;
  1043. +
  1044. + if (ep->chip->shutdown)
  1045. + return -EBADFD;
  1046. +
  1047. + snd_printk("%s(%p) type %s\n", __func__, ep,
  1048. + ep->type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync");
  1049. +
  1050. + /* already running? */
  1051. + if (++ep->use_count != 1)
  1052. + return 0;
  1053. +
  1054. + ep->phase = 0;
  1055. + ep->active_mask = 0;
  1056. + ep->unlink_mask = 0;
  1057. +
  1058. + /* in implicit feedback mode, don't start the urbs here.
  1059. + * instead, wait for the record urbs to arrive and queue the
  1060. + * urbs from that context.
  1061. */
  1062. - if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  1063. - num = 4;
  1064. -
  1065. - for (i = 0; i < num; i++) {
  1066. - alts = &iface->altsetting[i];
  1067. - altsd = get_iface_desc(alts);
  1068. - protocol = altsd->bInterfaceProtocol;
  1069. - /* skip invalid one */
  1070. - if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  1071. - altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  1072. - (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
  1073. - altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
  1074. - altsd->bNumEndpoints < 1 ||
  1075. - le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
  1076. - continue;
  1077. - /* must be isochronous */
  1078. - if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1079. - USB_ENDPOINT_XFER_ISOC)
  1080. - continue;
  1081. - /* check direction */
  1082. - stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
  1083. - SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  1084. - altno = altsd->bAlternateSetting;
  1085. -
  1086. - if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
  1087. - continue;
  1088. -
  1089. - /* get audio formats */
  1090. - switch (protocol) {
  1091. - default:
  1092. - snd_printdd(KERN_WARNING "%d:%u:%d: unknown interface protocol %#02x, assuming v1\n",
  1093. - dev->devnum, iface_no, altno, protocol);
  1094. - protocol = UAC_VERSION_1;
  1095. - /* fall through */
  1096. -
  1097. - case UAC_VERSION_1: {
  1098. - struct uac1_as_header_descriptor *as =
  1099. - snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  1100. -
  1101. - if (!as) {
  1102. - snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  1103. - dev->devnum, iface_no, altno);
  1104. - continue;
  1105. - }
  1106.  
  1107. - if (as->bLength < sizeof(*as)) {
  1108. - snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  1109. - dev->devnum, iface_no, altno);
  1110. - continue;
  1111. - }
  1112. + if (snd_usb_endpoint_implict_feedback_sink(ep))
  1113. + return 0;
  1114.  
  1115. - format = le16_to_cpu(as->wFormatTag); /* remember the format value */
  1116. - break;
  1117. + for (i = 0; i < ep->nurbs; i++) {
  1118. + struct urb *urb = ep->urb[i].urb;
  1119. +
  1120. + if (snd_BUG_ON(!urb))
  1121. + return -EINVAL;
  1122. +
  1123. + if (usb_pipeout(ep->pipe)) {
  1124. + prepare_outbound_urb_sizes(ep, urb->context);
  1125. + prepare_outbound_urb(ep, urb->context);
  1126. + } else {
  1127. + prepare_inbound_urb(ep, urb->context);
  1128. }
  1129. + }
  1130.  
  1131. - case UAC_VERSION_2: {
  1132. - struct uac2_input_terminal_descriptor *input_term;
  1133. - struct uac2_output_terminal_descriptor *output_term;
  1134. - struct uac2_as_header_descriptor *as =
  1135. - snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  1136. + for (i = 0; i < ep->nurbs; i++) {
  1137. + err = usb_submit_urb(ep->urb[i].urb, GFP_ATOMIC);
  1138. + snd_printk("Submmiting urb %d/%d (in? %d buf %p len %d) ... -> %d\n",
  1139. + i, ep->nurbs, usb_pipein(ep->pipe),
  1140. + ep->urb[i].urb->transfer_buffer,
  1141. + ep->urb[i].urb->transfer_buffer_length,
  1142. + err);
  1143. + if (err < 0) {
  1144. + snd_printk(KERN_ERR "cannot submit urb %d, error %d: %s\n",
  1145. + i, err, usb_error_string(err));
  1146. + goto __error;
  1147. + }
  1148. + set_bit(i, &ep->active_mask);
  1149. + }
  1150. + return 0;
  1151.  
  1152. - if (!as) {
  1153. - snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  1154. - dev->devnum, iface_no, altno);
  1155. - continue;
  1156. - }
  1157. + __error:
  1158. + deactivate_urbs(ep, 0, 0);
  1159. + return -EPIPE;
  1160. +}
  1161.  
  1162. - if (as->bLength < sizeof(*as)) {
  1163. - snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  1164. - dev->devnum, iface_no, altno);
  1165. - continue;
  1166. - }
  1167. +void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
  1168. +{
  1169. + if (!ep)
  1170. + return;
  1171.  
  1172. - num_channels = as->bNrChannels;
  1173. - format = le32_to_cpu(as->bmFormats);
  1174. + if (snd_BUG_ON(ep->use_count == 0))
  1175. + return;
  1176.  
  1177. - /* lookup the terminal associated to this interface
  1178. - * to extract the clock */
  1179. - input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
  1180. - as->bTerminalLink);
  1181. - if (input_term) {
  1182. - clock = input_term->bCSourceID;
  1183. - break;
  1184. - }
  1185. + if (--ep->use_count == 0)
  1186. + deactivate_urbs(ep, 0, 0);
  1187. +}
  1188. +
  1189. +void snd_usb_endpoint_free(struct list_head *head)
  1190. +{
  1191. + struct snd_usb_endpoint *ep;
  1192. + ep = list_entry(head, struct snd_usb_endpoint, list);
  1193. + release_urbs(ep, 1);
  1194. +}
  1195. +
  1196. +/*
  1197. + * process after playback sync complete
  1198. + *
  1199. + * Full speed devices report feedback values in 10.14 format as samples per
  1200. + * frame, high speed devices in 16.16 format as samples per microframe.
  1201. + * Because the Audio Class 1 spec was written before USB 2.0, many high speed
  1202. + * devices use a wrong interpretation, some others use an entirely different
  1203. + * format. Therefore, we cannot predict what format any particular device uses
  1204. + * and must detect it automatically.
  1205. + */
  1206. +void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
  1207. + struct snd_usb_endpoint *sender,
  1208. + const struct urb *urb)
  1209. +{
  1210. + int shift;
  1211. + unsigned int f;
  1212. + unsigned long flags;
  1213. +
  1214. + snd_BUG_ON(ep == sender);
  1215.  
  1216. - output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
  1217. - as->bTerminalLink);
  1218. - if (output_term) {
  1219. - clock = output_term->bCSourceID;
  1220. + if (snd_usb_endpoint_implict_feedback_sink(ep) && ep->use_count > 0) {
  1221. + /* implicit feedback case */
  1222. + int i, err, bytes = 0;
  1223. + struct urb *out_urb = NULL;
  1224. + struct snd_urb_ctx *in_ctx, *out_ctx;
  1225. +
  1226. + in_ctx = urb->context;
  1227. +
  1228. + /* Count overall packet size */
  1229. + for (i = 0; i < in_ctx->packets; i++)
  1230. + if (urb->iso_frame_desc[i].status == 0)
  1231. + bytes += urb->iso_frame_desc[i].actual_length;
  1232. +
  1233. + /* skip empty packets. At least M-Audio's Fast Track Ultra stops
  1234. + * streaming once it received a 0-byte OUT URB */
  1235. + if (bytes == 0)
  1236. + return;
  1237. +
  1238. + for (i = 0; i < ep->nurbs; i++)
  1239. + if (!test_and_set_bit(i, &ep->active_mask)) {
  1240. + out_urb = ep->urb[i].urb;
  1241. break;
  1242. }
  1243.  
  1244. - snd_printk(KERN_ERR "%d:%u:%d : bogus bTerminalLink %d\n",
  1245. - dev->devnum, iface_no, altno, as->bTerminalLink);
  1246. - continue;
  1247. - }
  1248. + if (!out_urb) {
  1249. + snd_printk("Unable to find an urb for playback (nurbs %d)\n", ep->nurbs);
  1250. + return;
  1251. }
  1252.  
  1253. - /* get format type */
  1254. - fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
  1255. - if (!fmt) {
  1256. - snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
  1257. - dev->devnum, iface_no, altno);
  1258. - continue;
  1259. - }
  1260. - if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8)) ||
  1261. - ((protocol == UAC_VERSION_2) && (fmt->bLength < 6))) {
  1262. - snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  1263. - dev->devnum, iface_no, altno);
  1264. - continue;
  1265. - }
  1266. + out_ctx = out_urb->context;
  1267. + out_ctx->packets = in_ctx->packets;
  1268.  
  1269. /*
  1270. - * Blue Microphones workaround: The last altsetting is identical
  1271. - * with the previous one, except for a larger packet size, but
  1272. - * is actually a mislabeled two-channel setting; ignore it.
  1273. + * Iterate through the inbound packet and prepare the lengths
  1274. + * for the output packet. The OUT packet we are about to send
  1275. + * will have the same amount of payload than the IN packet we
  1276. + * just received.
  1277. */
  1278. - if (fmt->bNrChannels == 1 &&
  1279. - fmt->bSubframeSize == 2 &&
  1280. - altno == 2 && num == 3 &&
  1281. - fp && fp->altsetting == 1 && fp->channels == 1 &&
  1282. - fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
  1283. - protocol == UAC_VERSION_1 &&
  1284. - le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
  1285. - fp->maxpacksize * 2)
  1286. - continue;
  1287. -
  1288. - fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  1289. - if (! fp) {
  1290. - snd_printk(KERN_ERR "cannot malloc\n");
  1291. - return -ENOMEM;
  1292. - }
  1293.  
  1294. - fp->iface = iface_no;
  1295. - fp->altsetting = altno;
  1296. - fp->altset_idx = i;
  1297. - fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  1298. - fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  1299. - fp->datainterval = snd_usb_parse_datainterval(chip, alts);
  1300. - fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  1301. - /* num_channels is only set for v2 interfaces */
  1302. - fp->channels = num_channels;
  1303. - if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
  1304. - fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
  1305. - * (fp->maxpacksize & 0x7ff);
  1306. - fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
  1307. - fp->clock = clock;
  1308. -
  1309. - /* some quirks for attributes here */
  1310. -
  1311. - switch (chip->usb_id) {
  1312. - case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
  1313. - /* Optoplay sets the sample rate attribute although
  1314. - * it seems not supporting it in fact.
  1315. - */
  1316. - fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
  1317. - break;
  1318. - case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
  1319. - case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  1320. - /* doesn't set the sample rate attribute, but supports it */
  1321. - fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
  1322. - break;
  1323. - case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */
  1324. - case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
  1325. - case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
  1326. - case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
  1327. - an older model 77d:223) */
  1328. - /*
  1329. - * plantronics headset and Griffin iMic have set adaptive-in
  1330. - * although it's really not...
  1331. - */
  1332. - fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
  1333. - if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1334. - fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
  1335. + for (i = 0; i < in_ctx->packets; i++) {
  1336. + if (urb->iso_frame_desc[i].status == 0)
  1337. + out_ctx->packet_size[i] =
  1338. + urb->iso_frame_desc[i].actual_length / ep->stride;
  1339. else
  1340. - fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
  1341. - break;
  1342. + out_ctx->packet_size[i] = 0;
  1343. }
  1344.  
  1345. - /* ok, let's parse further... */
  1346. - if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
  1347. - kfree(fp->rate_table);
  1348. - kfree(fp);
  1349. - fp = NULL;
  1350. - continue;
  1351. + prepare_outbound_urb(ep, out_ctx);
  1352. +
  1353. +
  1354. + for (i = 0; i < in_ctx->packets; i++) {
  1355. + if (urb->iso_frame_desc[i].status != 0)
  1356. + continue;
  1357. +
  1358. + if (urb->iso_frame_desc[i].actual_length !=
  1359. + out_urb->iso_frame_desc[i].length)
  1360. + snd_printk(" index %d: in %d, out %d (%d) stride %d\n", i,
  1361. + urb->iso_frame_desc[i].actual_length,
  1362. + out_urb->iso_frame_desc[i].length,
  1363. + out_ctx->packet_size[i], ep->stride);
  1364. }
  1365.  
  1366. - snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
  1367. - err = snd_usb_add_audio_endpoint(chip, stream, fp);
  1368. +
  1369. + err = usb_submit_urb(out_urb, GFP_ATOMIC);
  1370. if (err < 0) {
  1371. - kfree(fp->rate_table);
  1372. - kfree(fp);
  1373. - return err;
  1374. + snd_printk("Unable to submit urb #%d: %d (urb %p)\n", out_ctx->index, err, out_urb);
  1375. + clear_bit(out_ctx->index, &ep->active_mask);
  1376. }
  1377. - /* try to set the interface... */
  1378. - usb_set_interface(chip->dev, iface_no, altno);
  1379. - snd_usb_init_pitch(chip, iface_no, alts, fp);
  1380. - snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);
  1381. +
  1382. + return;
  1383. + }
  1384. +
  1385. + if (urb->iso_frame_desc[0].status != 0 ||
  1386. + urb->iso_frame_desc[0].actual_length < 3)
  1387. + return;
  1388. +
  1389. + f = le32_to_cpup(urb->transfer_buffer);
  1390. + if (urb->iso_frame_desc[0].actual_length == 3)
  1391. + f &= 0x00ffffff;
  1392. + else
  1393. + f &= 0x0fffffff;
  1394. +
  1395. + if (f == 0)
  1396. + return;
  1397. +
  1398. + if (unlikely(ep->freqshift == INT_MIN)) {
  1399. + /*
  1400. + * The first time we see a feedback value, determine its format
  1401. + * by shifting it left or right until it matches the nominal
  1402. + * frequency value. This assumes that the feedback does not
  1403. + * differ from the nominal value more than +50% or -25%.
  1404. + */
  1405. + shift = 0;
  1406. + while (f < ep->freqn - ep->freqn / 4) {
  1407. + f <<= 1;
  1408. + shift++;
  1409. + }
  1410. + while (f > ep->freqn + ep->freqn / 2) {
  1411. + f >>= 1;
  1412. + shift--;
  1413. + }
  1414. + ep->freqshift = shift;
  1415. + }
  1416. + else if (ep->freqshift >= 0)
  1417. + f <<= ep->freqshift;
  1418. + else
  1419. + f >>= -ep->freqshift;
  1420. +
  1421. + if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
  1422. + /*
  1423. + * If the frequency looks valid, set it.
  1424. + * This value is referred to in prepare_playback_urb().
  1425. + */
  1426. + spin_lock_irqsave(&ep->lock, flags);
  1427. + ep->freqm = f;
  1428. + spin_unlock_irqrestore(&ep->lock, flags);
  1429. + } else {
  1430. + /*
  1431. + * Out of range; maybe the shift value is wrong.
  1432. + * Reset it so that we autodetect again the next time.
  1433. + */
  1434. + ep->freqshift = INT_MIN;
  1435. }
  1436. - return 0;
  1437. }
  1438.  
  1439. diff --git a/sound/usb/endpoint.h b/sound/usb/endpoint.h
  1440. index 64dd0db..fcbcf9c 100644
  1441. --- a/sound/usb/endpoint.h
  1442. +++ b/sound/usb/endpoint.h
  1443. @@ -1,11 +1,26 @@
  1444. #ifndef __USBAUDIO_ENDPOINT_H
  1445. #define __USBAUDIO_ENDPOINT_H
  1446.  
  1447. -int snd_usb_parse_audio_endpoints(struct snd_usb_audio *chip,
  1448. - int iface_no);
  1449. +#define SND_USB_ENDPOINT_TYPE_DATA 0
  1450. +#define SND_USB_ENDPOINT_TYPE_SYNC 1
  1451.  
  1452. -int snd_usb_add_audio_endpoint(struct snd_usb_audio *chip,
  1453. - int stream,
  1454. - struct audioformat *fp);
  1455. +struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip,
  1456. + struct usb_host_interface *alts,
  1457. + int ep_num, int direction, int type);
  1458. +
  1459. +int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,
  1460. + struct snd_pcm_hw_params *hw_params,
  1461. + struct audioformat *fmt,
  1462. + struct snd_usb_endpoint *sync_ep);
  1463. +
  1464. +int snd_usb_endpoint_start(struct snd_usb_endpoint *ep);
  1465. +void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep);
  1466. +void snd_usb_endpoint_free(struct list_head *head);
  1467. +
  1468. +int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep);
  1469. +
  1470. +void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
  1471. + struct snd_usb_endpoint *sender,
  1472. + const struct urb *urb);
  1473.  
  1474. #endif /* __USBAUDIO_ENDPOINT_H */
  1475. diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c
  1476. index b8dcbf4..fd71ed6 100644
  1477. --- a/sound/usb/pcm.c
  1478. +++ b/sound/usb/pcm.c
  1479. @@ -28,7 +28,7 @@
  1480. #include "card.h"
  1481. #include "quirks.h"
  1482. #include "debug.h"
  1483. -#include "urb.h"
  1484. +#include "endpoint.h"
  1485. #include "helper.h"
  1486. #include "pcm.h"
  1487. #include "clock.h"
  1488. @@ -182,6 +182,64 @@ int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
  1489. }
  1490. }
  1491.  
  1492. +static int start_endpoints(struct snd_usb_substream *subs)
  1493. +{
  1494. + int err;
  1495. +
  1496. + if (!subs->data_endpoint)
  1497. + return -EINVAL;
  1498. +
  1499. + if (!subs->data_endpoint_started) {
  1500. + struct snd_usb_endpoint *ep = subs->data_endpoint;
  1501. +
  1502. + snd_printk("Starting data EP @%p\n", ep);
  1503. +
  1504. + //ep->prepare_data_urb = NULL;
  1505. + //ep->retire_data_urb = NULL;
  1506. + ep->data_subs = subs;
  1507. +
  1508. + err = snd_usb_endpoint_start(ep);
  1509. + if (err < 0)
  1510. + return err;
  1511. +
  1512. + subs->data_endpoint_started = 1;
  1513. + }
  1514. +
  1515. + if (!subs->sync_endpoint_started && subs->sync_endpoint) {
  1516. + struct snd_usb_endpoint *ep = subs->sync_endpoint;
  1517. +
  1518. + snd_printk("Starting sync EP @%p\n", ep);
  1519. +
  1520. + ep->sync_slave = subs->data_endpoint;
  1521. + err = snd_usb_endpoint_start(ep);
  1522. + if (err < 0)
  1523. + return err;
  1524. +
  1525. + subs->sync_endpoint_started = 1;
  1526. + }
  1527. +
  1528. + return 0;
  1529. +}
  1530. +
  1531. +static void stop_endpoints(struct snd_usb_substream *subs)
  1532. +{
  1533. + snd_printk("%s()\n", __func__);
  1534. +
  1535. + if (subs->sync_endpoint_started) {
  1536. + subs->sync_endpoint->sync_slave = NULL;
  1537. + snd_usb_endpoint_stop(subs->sync_endpoint);
  1538. + subs->sync_endpoint_started = 0;
  1539. + }
  1540. +
  1541. + if (subs->data_endpoint_started) {
  1542. + subs->data_endpoint->retire_data_urb = NULL;
  1543. + subs->data_endpoint->prepare_data_urb = NULL;
  1544. + subs->data_endpoint->sync_master = NULL;
  1545. + snd_usb_endpoint_stop(subs->data_endpoint);
  1546. + subs->data_endpoint_started = 0;
  1547. + }
  1548. +}
  1549. +
  1550. /*
  1551. * find a matching format and set up the interface
  1552. */
  1553. @@ -229,17 +287,11 @@ static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
  1554. subs->altset_idx = fmt->altset_idx;
  1555. }
  1556.  
  1557. - /* create a data pipe */
  1558. - ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
  1559. - if (is_playback)
  1560. - subs->datapipe = usb_sndisocpipe(dev, ep);
  1561. - else
  1562. - subs->datapipe = usb_rcvisocpipe(dev, ep);
  1563. - subs->datainterval = fmt->datainterval;
  1564. - subs->syncpipe = subs->syncinterval = 0;
  1565. - subs->maxpacksize = fmt->maxpacksize;
  1566. - subs->syncmaxsize = 0;
  1567. - subs->fill_max = 0;
  1568. + subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  1569. + alts, fmt->endpoint, subs->direction,
  1570. + SND_USB_ENDPOINT_TYPE_DATA);
  1571. + if (!subs->data_endpoint)
  1572. + return -EINVAL;
  1573.  
  1574. /* we need a sync pipe in async OUT or adaptive IN mode */
  1575. /* check the number of EP, since some devices have broken
  1576. @@ -247,50 +299,59 @@ static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
  1577. * assume it as adaptive-out or sync-in.
  1578. */
  1579. attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
  1580. + snd_printk("attr %x\n", attr);
  1581. if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
  1582. (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
  1583. altsd->bNumEndpoints >= 2) {
  1584. + int implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
  1585. + == USB_ENDPOINT_USAGE_IMPLICIT_FB;
  1586. +
  1587. + switch (subs->stream->chip->usb_id) {
  1588. + case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
  1589. + case USB_ID(0x0763, 0x2081):
  1590. + implicit_fb = 1;
  1591. + ep = 0x81;
  1592. + iface = usb_ifnum_to_if(dev, 2);
  1593. + alts = &iface->altsetting[1];
  1594. + goto add_sync_ep;
  1595. + }
  1596. +
  1597. /* check sync-pipe endpoint */
  1598. /* ... and check descriptor size before accessing bSynchAddress
  1599. because there is a version of the SB Audigy 2 NX firmware lacking
  1600. the audio fields in the endpoint descriptors */
  1601. if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
  1602. (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  1603. - get_endpoint(alts, 1)->bSynchAddress != 0)) {
  1604. + get_endpoint(alts, 1)->bSynchAddress != 0 &&
  1605. + !implicit_fb)) {
  1606. snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
  1607. dev->devnum, fmt->iface, fmt->altsetting);
  1608. return -EINVAL;
  1609. }
  1610. ep = get_endpoint(alts, 1)->bEndpointAddress;
  1611. +
  1612. if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  1613. (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
  1614. - (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
  1615. + (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)) ||
  1616. + ( is_playback && !implicit_fb))) {
  1617. snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
  1618. dev->devnum, fmt->iface, fmt->altsetting);
  1619. return -EINVAL;
  1620. }
  1621. - ep &= USB_ENDPOINT_NUMBER_MASK;
  1622. - if (is_playback)
  1623. - subs->syncpipe = usb_rcvisocpipe(dev, ep);
  1624. - else
  1625. - subs->syncpipe = usb_sndisocpipe(dev, ep);
  1626. - if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  1627. - get_endpoint(alts, 1)->bRefresh >= 1 &&
  1628. - get_endpoint(alts, 1)->bRefresh <= 9)
  1629. - subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
  1630. - else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  1631. - subs->syncinterval = 1;
  1632. - else if (get_endpoint(alts, 1)->bInterval >= 1 &&
  1633. - get_endpoint(alts, 1)->bInterval <= 16)
  1634. - subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
  1635. - else
  1636. - subs->syncinterval = 3;
  1637. - subs->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
  1638. - }
  1639. -
  1640. - /* always fill max packet size */
  1641. - if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
  1642. - subs->fill_max = 1;
  1643. +add_sync_ep:
  1644. + subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
  1645. + alts, ep, !subs->direction,
  1646. + implicit_fb ?
  1647. + SND_USB_ENDPOINT_TYPE_DATA :
  1648. + SND_USB_ENDPOINT_TYPE_SYNC);
  1649. +
  1650. + snd_printk(" -- imlicit_fb %d\n", implicit_fb);
  1651. +
  1652. + if (!subs->sync_endpoint)
  1653. + return -EINVAL;
  1654. +
  1655. + subs->data_endpoint->sync_master = subs->sync_endpoint;
  1656. + }
  1657.  
  1658. if ((err = snd_usb_init_pitch(subs->stream->chip, subs->interface, alts, fmt)) < 0)
  1659. return err;
  1660. @@ -364,12 +425,18 @@ static int snd_usb_hw_params(struct snd_pcm_substream *substream,
  1661. if (changed) {
  1662. mutex_lock(&subs->stream->chip->shutdown_mutex);
  1663. /* format changed */
  1664. - snd_usb_release_substream_urbs(subs, 0);
  1665. - /* influenced: period_bytes, channels, rate, format, */
  1666. - ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params),
  1667. - params_rate(hw_params),
  1668. - snd_pcm_format_physical_width(params_format(hw_params)) *
  1669. - params_channels(hw_params));
  1670. + stop_endpoints(subs);
  1671. +
  1672. + snd_printk(" -- data %p\n", subs->data_endpoint);
  1673. + snd_printk(" -- sync %p\n", subs->sync_endpoint);
  1674. +
  1675. + ret = snd_usb_endpoint_set_params(subs->data_endpoint, hw_params, fmt,
  1676. + subs->sync_endpoint);
  1677. + if (ret == 0) {
  1678. + if (subs->sync_endpoint)
  1679. + ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
  1680. + hw_params, fmt, NULL);
  1681. + }
  1682. mutex_unlock(&subs->stream->chip->shutdown_mutex);
  1683. }
  1684.  
  1685. @@ -389,7 +456,7 @@ static int snd_usb_hw_free(struct snd_pcm_substream *substream)
  1686. subs->cur_rate = 0;
  1687. subs->period_bytes = 0;
  1688. mutex_lock(&subs->stream->chip->shutdown_mutex);
  1689. - snd_usb_release_substream_urbs(subs, 0);
  1690. + stop_endpoints(subs);
  1691. mutex_unlock(&subs->stream->chip->shutdown_mutex);
  1692. return snd_pcm_lib_free_vmalloc_buffer(substream);
  1693. }
  1694. @@ -409,17 +476,26 @@ static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
  1695. return -ENXIO;
  1696. }
  1697.  
  1698. + if (snd_BUG_ON(!subs->data_endpoint))
  1699. + return -EIO;
  1700. +
  1701. /* some unit conversions in runtime */
  1702. - subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
  1703. - subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
  1704. + subs->data_endpoint->maxframesize =
  1705. + bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
  1706. + subs->data_endpoint->curframesize =
  1707. + bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
  1708.  
  1709. /* reset the pointer */
  1710. subs->hwptr_done = 0;
  1711. subs->transfer_done = 0;
  1712. - subs->phase = 0;
  1713. runtime->delay = 0;
  1714.  
  1715. - return snd_usb_substream_prepare(subs, runtime);
  1716. + /* for playback, submit the URBs now; otherwise, the first hwptr_done
  1717. + * updates for all URBs would happen at the same time when starting */
  1718. + if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
  1719. + return start_endpoints(subs);
  1720. +
  1721. + return -EINVAL;
  1722. }
  1723.  
  1724. static struct snd_pcm_hardware snd_usb_hardware =
  1725. @@ -824,6 +900,161 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
  1726. return 0;
  1727. }
  1728.  
  1729. +/* Since a URB can handle only a single linear buffer, we must use double
  1730. + * buffering when the data to be transferred overflows the buffer boundary.
  1731. + * To avoid inconsistencies when updating hwptr_done, we use double buffering
  1732. + * for all URBs.
  1733. + */
  1734. +static void retire_capture_urb(struct snd_usb_substream *subs,
  1735. + struct urb *urb)
  1736. +{
  1737. + struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1738. + unsigned int stride, frames, bytes, oldptr;
  1739. + int i, period_elapsed = 0;
  1740. + unsigned long flags;
  1741. + unsigned char *cp;
  1742. +
  1743. + stride = runtime->frame_bits >> 3;
  1744. +
  1745. + for (i = 0; i < urb->number_of_packets; i++) {
  1746. + cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  1747. + if (urb->iso_frame_desc[i].status) {
  1748. + snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
  1749. + // continue;
  1750. + }
  1751. + bytes = urb->iso_frame_desc[i].actual_length;
  1752. + frames = bytes / stride;
  1753. + if (!subs->txfr_quirk)
  1754. + bytes = frames * stride;
  1755. + if (bytes % (runtime->sample_bits >> 3) != 0) {
  1756. +#ifdef CONFIG_SND_DEBUG_VERBOSE
  1757. + int oldbytes = bytes;
  1758. +#endif
  1759. + bytes = frames * stride;
  1760. + snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
  1761. + oldbytes, bytes);
  1762. + }
  1763. + /* update the current pointer */
  1764. + spin_lock_irqsave(&subs->lock, flags);
  1765. + oldptr = subs->hwptr_done;
  1766. + subs->hwptr_done += bytes;
  1767. + if (subs->hwptr_done >= runtime->buffer_size * stride)
  1768. + subs->hwptr_done -= runtime->buffer_size * stride;
  1769. + frames = (bytes + (oldptr % stride)) / stride;
  1770. + subs->transfer_done += frames;
  1771. + if (subs->transfer_done >= runtime->period_size) {
  1772. + subs->transfer_done -= runtime->period_size;
  1773. + period_elapsed = 1;
  1774. + }
  1775. + spin_unlock_irqrestore(&subs->lock, flags);
  1776. + /* copy a data chunk */
  1777. + if (oldptr + bytes > runtime->buffer_size * stride) {
  1778. + unsigned int bytes1 =
  1779. + runtime->buffer_size * stride - oldptr;
  1780. + memcpy(runtime->dma_area + oldptr, cp, bytes1);
  1781. + memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  1782. + } else {
  1783. + memcpy(runtime->dma_area + oldptr, cp, bytes);
  1784. + }
  1785. + }
  1786. +
  1787. + if (period_elapsed)
  1788. + snd_pcm_period_elapsed(subs->pcm_substream);
  1789. +}
  1790. +
  1791. +static void prepare_playback_urb(struct snd_usb_substream *subs,
  1792. + struct urb *urb)
  1793. +{
  1794. + struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1795. + struct snd_urb_ctx *ctx = urb->context;
  1796. + unsigned int counts, frames, bytes;
  1797. + int i, stride, period_elapsed = 0;
  1798. + unsigned long flags;
  1799. +
  1800. + stride = runtime->frame_bits >> 3;
  1801. +
  1802. + frames = 0;
  1803. + urb->number_of_packets = 0;
  1804. + spin_lock_irqsave(&subs->lock, flags);
  1805. + for (i = 0; i < ctx->packets; i++) {
  1806. + counts = ctx->packet_size[i];
  1807. + /* set up descriptor */
  1808. + urb->iso_frame_desc[i].offset = frames * stride;
  1809. + urb->iso_frame_desc[i].length = counts * stride;
  1810. + frames += counts;
  1811. + urb->number_of_packets++;
  1812. + subs->transfer_done += counts;
  1813. + if (subs->transfer_done >= runtime->period_size) {
  1814. + subs->transfer_done -= runtime->period_size;
  1815. + period_elapsed = 1;
  1816. + if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  1817. + if (subs->transfer_done > 0) {
  1818. + /* FIXME: fill-max mode is not
  1819. + * supported yet */
  1820. + frames -= subs->transfer_done;
  1821. + counts -= subs->transfer_done;
  1822. + urb->iso_frame_desc[i].length =
  1823. + counts * stride;
  1824. + subs->transfer_done = 0;
  1825. + }
  1826. + i++;
  1827. + if (i < ctx->packets) {
  1828. + /* add a transfer delimiter */
  1829. + urb->iso_frame_desc[i].offset =
  1830. + frames * stride;
  1831. + urb->iso_frame_desc[i].length = 0;
  1832. + urb->number_of_packets++;
  1833. + }
  1834. + break;
  1835. + }
  1836. + }
  1837. + if (period_elapsed &&
  1838. + !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
  1839. + break;
  1840. + }
  1841. + bytes = frames * stride;
  1842. + if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  1843. + /* err, the transferred area goes over buffer boundary. */
  1844. + unsigned int bytes1 =
  1845. + runtime->buffer_size * stride - subs->hwptr_done;
  1846. + memcpy(urb->transfer_buffer,
  1847. + runtime->dma_area + subs->hwptr_done, bytes1);
  1848. + memcpy(urb->transfer_buffer + bytes1,
  1849. + runtime->dma_area, bytes - bytes1);
  1850. + } else {
  1851. + memcpy(urb->transfer_buffer,
  1852. + runtime->dma_area + subs->hwptr_done, bytes);
  1853. + }
  1854. + subs->hwptr_done += bytes;
  1855. + if (subs->hwptr_done >= runtime->buffer_size * stride)
  1856. + subs->hwptr_done -= runtime->buffer_size * stride;
  1857. + runtime->delay += frames;
  1858. + spin_unlock_irqrestore(&subs->lock, flags);
  1859. + urb->transfer_buffer_length = bytes;
  1860. + if (period_elapsed)
  1861. + snd_pcm_period_elapsed(subs->pcm_substream);
  1862. +}
  1863. +
  1864. +/*
  1865. + * process after playback data complete
  1866. + * - decrease the delay count again
  1867. + */
  1868. +static void retire_playback_urb(struct snd_usb_substream *subs,
  1869. + struct urb *urb)
  1870. +{
  1871. + unsigned long flags;
  1872. + struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
  1873. + int stride = runtime->frame_bits >> 3;
  1874. + int processed = urb->transfer_buffer_length / stride;
  1875. +
  1876. + spin_lock_irqsave(&subs->lock, flags);
  1877. + if (processed > runtime->delay)
  1878. + runtime->delay = 0;
  1879. + else
  1880. + runtime->delay -= processed;
  1881. + spin_unlock_irqrestore(&subs->lock, flags);
  1882. +}
  1883. +
  1884. static int snd_usb_playback_open(struct snd_pcm_substream *substream)
  1885. {
  1886. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1887. @@ -844,6 +1075,58 @@ static int snd_usb_capture_close(struct snd_pcm_substream *substream)
  1888. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  1889. }
  1890.  
  1891. +static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
  1892. + int cmd)
  1893. +{
  1894. + struct snd_usb_substream *subs = substream->runtime->private_data;
  1895. +
  1896. + switch (cmd) {
  1897. + case SNDRV_PCM_TRIGGER_START:
  1898. + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1899. + snd_printk("START!\n");
  1900. + subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
  1901. + subs->data_endpoint->retire_data_urb = retire_playback_urb;
  1902. + return 0;
  1903. + case SNDRV_PCM_TRIGGER_STOP:
  1904. + stop_endpoints(subs);
  1905. + return 0;
  1906. + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1907. + snd_printk("PAUSE!\n");
  1908. + subs->data_endpoint->prepare_data_urb = NULL;
  1909. + subs->data_endpoint->retire_data_urb = NULL;
  1910. + return 0;
  1911. + }
  1912. +
  1913. + return -EINVAL;
  1914. +}
  1915. +
  1916. +int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  1917. +{
  1918. + int err;
  1919. + struct snd_usb_substream *subs = substream->runtime->private_data;
  1920. +
  1921. + switch (cmd) {
  1922. + case SNDRV_PCM_TRIGGER_START:
  1923. + err = start_endpoints(subs);
  1924. + if (err < 0)
  1925. + return err;
  1926. +
  1927. + subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1928. + return 0;
  1929. + case SNDRV_PCM_TRIGGER_STOP:
  1930. + stop_endpoints(subs);
  1931. + return 0;
  1932. + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1933. + subs->data_endpoint->retire_data_urb = NULL;
  1934. + return 0;
  1935. + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1936. + subs->data_endpoint->retire_data_urb = retire_capture_urb;
  1937. + return 0;
  1938. + }
  1939. +
  1940. + return -EINVAL;
  1941. +}
  1942. +
  1943. static struct snd_pcm_ops snd_usb_playback_ops = {
  1944. .open = snd_usb_playback_open,
  1945. .close = snd_usb_playback_close,
  1946. diff --git a/sound/usb/proc.c b/sound/usb/proc.c
  1947. index 961c9a2..c4472f6 100644
  1948. --- a/sound/usb/proc.c
  1949. +++ b/sound/usb/proc.c
  1950. @@ -117,26 +117,28 @@ static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct s
  1951.  
  1952. static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
  1953. {
  1954. - if (subs->running) {
  1955. + struct snd_usb_endpoint *ep = subs->data_endpoint;
  1956. +
  1957. + if (ep) {
  1958. unsigned int i;
  1959. snd_iprintf(buffer, " Status: Running\n");
  1960. snd_iprintf(buffer, " Interface = %d\n", subs->interface);
  1961. snd_iprintf(buffer, " Altset = %d\n", subs->altset_idx);
  1962. - snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
  1963. - for (i = 0; i < subs->nurbs; i++)
  1964. - snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
  1965. + snd_iprintf(buffer, " URBs = %d [ ", ep->nurbs);
  1966. + for (i = 0; i < ep->nurbs; i++)
  1967. + snd_iprintf(buffer, "%d ", ep->urb[i].packets);
  1968. snd_iprintf(buffer, "]\n");
  1969. - snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
  1970. + snd_iprintf(buffer, " Packet Size = %d\n", ep->curpacksize);
  1971. snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
  1972. - snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
  1973. - ? get_full_speed_hz(subs->freqm)
  1974. - : get_high_speed_hz(subs->freqm),
  1975. - subs->freqm >> 16, subs->freqm & 0xffff);
  1976. - if (subs->freqshift != INT_MIN)
  1977. + snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL
  1978. + ? get_full_speed_hz(ep->freqm)
  1979. + : get_high_speed_hz(ep->freqm),
  1980. + ep->freqm >> 16, ep->freqm & 0xffff);
  1981. + if (ep->freqshift != INT_MIN)
  1982. snd_iprintf(buffer, " Feedback Format = %d.%d\n",
  1983. - (subs->syncmaxsize > 3 ? 32 : 24)
  1984. - - (16 - subs->freqshift),
  1985. - 16 - subs->freqshift);
  1986. + (ep->syncmaxsize > 3 ? 32 : 24)
  1987. + - (16 - ep->freqshift),
  1988. + 16 - ep->freqshift);
  1989. } else {
  1990. snd_iprintf(buffer, " Status: Stop\n");
  1991. }
  1992. diff --git a/sound/usb/quirks-table.h b/sound/usb/quirks-table.h
  1993. index a42e3ef..7035073 100644
  1994. --- a/sound/usb/quirks-table.h
  1995. +++ b/sound/usb/quirks-table.h
  1996. @@ -2065,7 +2065,7 @@ YAMAHA_DEVICE(0x7010, "UB99"),
  1997. .altset_idx = 1,
  1998. .attributes = UAC_EP_CS_ATTR_SAMPLE_RATE,
  1999. .endpoint = 0x01,
  2000. - .ep_attr = 0x09,
  2001. + .ep_attr = 0x05,
  2002. .rates = SNDRV_PCM_RATE_44100 |
  2003. SNDRV_PCM_RATE_48000 |
  2004. SNDRV_PCM_RATE_88200 |
  2005. diff --git a/sound/usb/quirks.c b/sound/usb/quirks.c
  2006. index 81e07d8..1fc5668 100644
  2007. --- a/sound/usb/quirks.c
  2008. +++ b/sound/usb/quirks.c
  2009. @@ -34,6 +34,7 @@
  2010. #include "endpoint.h"
  2011. #include "pcm.h"
  2012. #include "clock.h"
  2013. +#include "stream.h"
  2014.  
  2015. /*
  2016. * handle the quirks for the contained interfaces
  2017. @@ -106,7 +107,7 @@ static int create_standard_audio_quirk(struct snd_usb_audio *chip,
  2018.  
  2019. alts = &iface->altsetting[0];
  2020. altsd = get_iface_desc(alts);
  2021. - err = snd_usb_parse_audio_endpoints(chip, altsd->bInterfaceNumber);
  2022. + err = snd_usb_parse_audio_interface(chip, altsd->bInterfaceNumber);
  2023. if (err < 0) {
  2024. snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
  2025. altsd->bInterfaceNumber, err);
  2026. @@ -147,7 +148,7 @@ static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
  2027.  
  2028. stream = (fp->endpoint & USB_DIR_IN)
  2029. ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  2030. - err = snd_usb_add_audio_endpoint(chip, stream, fp);
  2031. + err = snd_usb_add_audio_stream(chip, stream, fp);
  2032. if (err < 0) {
  2033. kfree(fp);
  2034. kfree(rate_table);
  2035. @@ -254,7 +255,7 @@ static int create_uaxx_quirk(struct snd_usb_audio *chip,
  2036.  
  2037. stream = (fp->endpoint & USB_DIR_IN)
  2038. ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  2039. - err = snd_usb_add_audio_endpoint(chip, stream, fp);
  2040. + err = snd_usb_add_audio_stream(chip, stream, fp);
  2041. if (err < 0) {
  2042. kfree(fp);
  2043. return err;
  2044. diff --git a/sound/usb/stream.c b/sound/usb/stream.c
  2045. new file mode 100644
  2046. index 0000000..6b7d7a2
  2047. --- /dev/null
  2048. +++ b/sound/usb/stream.c
  2049. @@ -0,0 +1,477 @@
  2050. +/*
  2051. + * This program is free software; you can redistribute it and/or modify
  2052. + * it under the terms of the GNU General Public License as published by
  2053. + * the Free Software Foundation; either version 2 of the License, or
  2054. + * (at your option) any later version.
  2055. + *
  2056. + * This program is distributed in the hope that it will be useful,
  2057. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2058. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2059. + * GNU General Public License for more details.
  2060. + *
  2061. + * You should have received a copy of the GNU General Public License
  2062. + * along with this program; if not, write to the Free Software
  2063. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  2064. + */
  2065. +
  2066. +
  2067. +#include <linux/init.h>
  2068. +#include <linux/slab.h>
  2069. +#include <linux/usb.h>
  2070. +#include <linux/usb/audio.h>
  2071. +#include <linux/usb/audio-v2.h>
  2072. +
  2073. +#include <sound/core.h>
  2074. +#include <sound/pcm.h>
  2075. +
  2076. +#include "usbaudio.h"
  2077. +#include "card.h"
  2078. +#include "proc.h"
  2079. +#include "quirks.h"
  2080. +#include "endpoint.h"
  2081. +#include "pcm.h"
  2082. +#include "helper.h"
  2083. +#include "format.h"
  2084. +#include "clock.h"
  2085. +#include "stream.h"
  2086. +
  2087. +/*
  2088. + * free a substream
  2089. + */
  2090. +static void free_substream(struct snd_usb_substream *subs)
  2091. +{
  2092. + struct list_head *p, *n;
  2093. +
  2094. + if (!subs->num_formats)
  2095. + return; /* not initialized */
  2096. + list_for_each_safe(p, n, &subs->fmt_list) {
  2097. + struct audioformat *fp = list_entry(p, struct audioformat, list);
  2098. + kfree(fp->rate_table);
  2099. + kfree(fp);
  2100. + }
  2101. + kfree(subs->rate_list.list);
  2102. +}
  2103. +
  2104. +
  2105. +/*
  2106. + * free a usb stream instance
  2107. + */
  2108. +static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
  2109. +{
  2110. + free_substream(&stream->substream[0]);
  2111. + free_substream(&stream->substream[1]);
  2112. + list_del(&stream->list);
  2113. + kfree(stream);
  2114. +}
  2115. +
  2116. +static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
  2117. +{
  2118. + struct snd_usb_stream *stream = pcm->private_data;
  2119. + if (stream) {
  2120. + stream->pcm = NULL;
  2121. + snd_usb_audio_stream_free(stream);
  2122. + }
  2123. +}
  2124. +
  2125. +/*
  2126. + * initialize the substream instance.
  2127. + */
  2128. +
  2129. +static void snd_usb_init_substream(struct snd_usb_stream *as,
  2130. + int stream,
  2131. + struct audioformat *fp)
  2132. +{
  2133. + struct snd_usb_substream *subs = &as->substream[stream];
  2134. +
  2135. + INIT_LIST_HEAD(&subs->fmt_list);
  2136. + spin_lock_init(&subs->lock);
  2137. +
  2138. + subs->stream = as;
  2139. + subs->direction = stream;
  2140. + subs->dev = as->chip->dev;
  2141. + subs->txfr_quirk = as->chip->txfr_quirk;
  2142. +
  2143. + snd_usb_set_pcm_ops(as->pcm, stream);
  2144. +
  2145. + list_add_tail(&fp->list, &subs->fmt_list);
  2146. + subs->formats |= fp->formats;
  2147. + subs->num_formats++;
  2148. + subs->fmt_type = fp->fmt_type;
  2149. +}
  2150. +
  2151. +/*
  2152. + * add this endpoint to the chip instance.
  2153. + * if a stream with the same endpoint already exists, append to it.
  2154. + * if not, create a new pcm stream.
  2155. + */
  2156. +int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
  2157. + int stream,
  2158. + struct audioformat *fp)
  2159. +{
  2160. + struct list_head *p;
  2161. + struct snd_usb_stream *as;
  2162. + struct snd_usb_substream *subs;
  2163. + struct snd_pcm *pcm;
  2164. + int err;
  2165. +
  2166. + list_for_each(p, &chip->pcm_list) {
  2167. + as = list_entry(p, struct snd_usb_stream, list);
  2168. + if (as->fmt_type != fp->fmt_type)
  2169. + continue;
  2170. + subs = &as->substream[stream];
  2171. + if (!subs->data_endpoint)
  2172. + continue;
  2173. + if (subs->data_endpoint->ep_num == fp->endpoint) {
  2174. + list_add_tail(&fp->list, &subs->fmt_list);
  2175. + subs->num_formats++;
  2176. + subs->formats |= fp->formats;
  2177. + return 0;
  2178. + }
  2179. + }
  2180. + /* look for an empty stream */
  2181. + list_for_each(p, &chip->pcm_list) {
  2182. + as = list_entry(p, struct snd_usb_stream, list);
  2183. + if (as->fmt_type != fp->fmt_type)
  2184. + continue;
  2185. + subs = &as->substream[stream];
  2186. + if (subs->data_endpoint)
  2187. + continue;
  2188. + err = snd_pcm_new_stream(as->pcm, stream, 1);
  2189. + if (err < 0)
  2190. + return err;
  2191. + snd_usb_init_substream(as, stream, fp);
  2192. + return 0;
  2193. + }
  2194. +
  2195. + /* create a new pcm */
  2196. + as = kzalloc(sizeof(*as), GFP_KERNEL);
  2197. + if (!as)
  2198. + return -ENOMEM;
  2199. + as->pcm_index = chip->pcm_devs;
  2200. + as->chip = chip;
  2201. + as->fmt_type = fp->fmt_type;
  2202. + err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
  2203. + stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
  2204. + stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
  2205. + &pcm);
  2206. + if (err < 0) {
  2207. + kfree(as);
  2208. + return err;
  2209. + }
  2210. + as->pcm = pcm;
  2211. + pcm->private_data = as;
  2212. + pcm->private_free = snd_usb_audio_pcm_free;
  2213. + pcm->info_flags = 0;
  2214. + if (chip->pcm_devs > 0)
  2215. + sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
  2216. + else
  2217. + strcpy(pcm->name, "USB Audio");
  2218. +
  2219. + snd_usb_init_substream(as, stream, fp);
  2220. +
  2221. + list_add(&as->list, &chip->pcm_list);
  2222. + chip->pcm_devs++;
  2223. +
  2224. + snd_usb_proc_pcm_format_add(as);
  2225. +
  2226. + return 0;
  2227. +}
  2228. +
  2229. +static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
  2230. + struct usb_host_interface *alts,
  2231. + int protocol, int iface_no)
  2232. +{
  2233. + /* parsed with a v1 header here. that's ok as we only look at the
  2234. + * header first which is the same for both versions */
  2235. + struct uac_iso_endpoint_descriptor *csep;
  2236. + struct usb_interface_descriptor *altsd = get_iface_desc(alts);
  2237. + int attributes = 0;
  2238. +
  2239. + csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
  2240. +
  2241. + /* Creamware Noah has this descriptor after the 2nd endpoint */
  2242. + if (!csep && altsd->bNumEndpoints >= 2)
  2243. + csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
  2244. +
  2245. + if (!csep || csep->bLength < 7 ||
  2246. + csep->bDescriptorSubtype != UAC_EP_GENERAL) {
  2247. + snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
  2248. + " class specific endpoint descriptor\n",
  2249. + chip->dev->devnum, iface_no,
  2250. + altsd->bAlternateSetting);
  2251. + return 0;
  2252. + }
  2253. +
  2254. + if (protocol == UAC_VERSION_1) {
  2255. + attributes = csep->bmAttributes;
  2256. + } else {
  2257. + struct uac2_iso_endpoint_descriptor *csep2 =
  2258. + (struct uac2_iso_endpoint_descriptor *) csep;
  2259. +
  2260. + attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
  2261. +
  2262. + /* emulate the endpoint attributes of a v1 device */
  2263. + if (csep2->bmControls & UAC2_CONTROL_PITCH)
  2264. + attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
  2265. + }
  2266. +
  2267. + return attributes;
  2268. +}
  2269. +
  2270. +static struct uac2_input_terminal_descriptor *
  2271. + snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  2272. + int terminal_id)
  2273. +{
  2274. + struct uac2_input_terminal_descriptor *term = NULL;
  2275. +
  2276. + while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  2277. + ctrl_iface->extralen,
  2278. + term, UAC_INPUT_TERMINAL))) {
  2279. + if (term->bTerminalID == terminal_id)
  2280. + return term;
  2281. + }
  2282. +
  2283. + return NULL;
  2284. +}
  2285. +
  2286. +static struct uac2_output_terminal_descriptor *
  2287. + snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
  2288. + int terminal_id)
  2289. +{
  2290. + struct uac2_output_terminal_descriptor *term = NULL;
  2291. +
  2292. + while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
  2293. + ctrl_iface->extralen,
  2294. + term, UAC_OUTPUT_TERMINAL))) {
  2295. + if (term->bTerminalID == terminal_id)
  2296. + return term;
  2297. + }
  2298. +
  2299. + return NULL;
  2300. +}
  2301. +
  2302. +int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
  2303. +{
  2304. + struct usb_device *dev;
  2305. + struct usb_interface *iface;
  2306. + struct usb_host_interface *alts;
  2307. + struct usb_interface_descriptor *altsd;
  2308. + int i, altno, err, stream;
  2309. + int format = 0, num_channels = 0;
  2310. + struct audioformat *fp = NULL;
  2311. + int num, protocol, clock = 0;
  2312. + struct uac_format_type_i_continuous_descriptor *fmt;
  2313. +
  2314. + dev = chip->dev;
  2315. +
  2316. + /* parse the interface's altsettings */
  2317. + iface = usb_ifnum_to_if(dev, iface_no);
  2318. +
  2319. + num = iface->num_altsetting;
  2320. +
  2321. + /*
  2322. + * Dallas DS4201 workaround: It presents 5 altsettings, but the last
  2323. + * one misses syncpipe, and does not produce any sound.
  2324. + */
  2325. + if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  2326. + num = 4;
  2327. +
  2328. + for (i = 0; i < num; i++) {
  2329. + alts = &iface->altsetting[i];
  2330. + altsd = get_iface_desc(alts);
  2331. + protocol = altsd->bInterfaceProtocol;
  2332. + /* skip invalid one */
  2333. + if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  2334. + altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  2335. + (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
  2336. + altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
  2337. + altsd->bNumEndpoints < 1 ||
  2338. + le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
  2339. + continue;
  2340. + /* must be isochronous */
  2341. + if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  2342. + USB_ENDPOINT_XFER_ISOC)
  2343. + continue;
  2344. + /* check direction */
  2345. + stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
  2346. + SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  2347. + altno = altsd->bAlternateSetting;
  2348. +
  2349. + if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
  2350. + continue;
  2351. +
  2352. + /* get audio formats */
  2353. + switch (protocol) {
  2354. + default:
  2355. + snd_printdd(KERN_WARNING "%d:%u:%d: unknown interface protocol %#02x, assuming v1\n",
  2356. + dev->devnum, iface_no, altno, protocol);
  2357. + protocol = UAC_VERSION_1;
  2358. + /* fall through */
  2359. +
  2360. + case UAC_VERSION_1: {
  2361. + struct uac1_as_header_descriptor *as =
  2362. + snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  2363. +
  2364. + if (!as) {
  2365. + snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  2366. + dev->devnum, iface_no, altno);
  2367. + continue;
  2368. + }
  2369. +
  2370. + if (as->bLength < sizeof(*as)) {
  2371. + snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  2372. + dev->devnum, iface_no, altno);
  2373. + continue;
  2374. + }
  2375. +
  2376. + format = le16_to_cpu(as->wFormatTag); /* remember the format value */
  2377. + break;
  2378. + }
  2379. +
  2380. + case UAC_VERSION_2: {
  2381. + struct uac2_input_terminal_descriptor *input_term;
  2382. + struct uac2_output_terminal_descriptor *output_term;
  2383. + struct uac2_as_header_descriptor *as =
  2384. + snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  2385. +
  2386. + if (!as) {
  2387. + snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  2388. + dev->devnum, iface_no, altno);
  2389. + continue;
  2390. + }
  2391. +
  2392. + if (as->bLength < sizeof(*as)) {
  2393. + snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  2394. + dev->devnum, iface_no, altno);
  2395. + continue;
  2396. + }
  2397. +
  2398. + num_channels = as->bNrChannels;
  2399. + format = le32_to_cpu(as->bmFormats);
  2400. +
  2401. + /* lookup the terminal associated to this interface
  2402. + * to extract the clock */
  2403. + input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
  2404. + as->bTerminalLink);
  2405. + if (input_term) {
  2406. + clock = input_term->bCSourceID;
  2407. + break;
  2408. + }
  2409. +
  2410. + output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
  2411. + as->bTerminalLink);
  2412. + if (output_term) {
  2413. + clock = output_term->bCSourceID;
  2414. + break;
  2415. + }
  2416. +
  2417. + snd_printk(KERN_ERR "%d:%u:%d : bogus bTerminalLink %d\n",
  2418. + dev->devnum, iface_no, altno, as->bTerminalLink);
  2419. + continue;
  2420. + }
  2421. + }
  2422. +
  2423. + /* get format type */
  2424. + fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
  2425. + if (!fmt) {
  2426. + snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
  2427. + dev->devnum, iface_no, altno);
  2428. + continue;
  2429. + }
  2430. + if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8)) ||
  2431. + ((protocol == UAC_VERSION_2) && (fmt->bLength < 6))) {
  2432. + snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  2433. + dev->devnum, iface_no, altno);
  2434. + continue;
  2435. + }
  2436. +
  2437. + /*
  2438. + * Blue Microphones workaround: The last altsetting is identical
  2439. + * with the previous one, except for a larger packet size, but
  2440. + * is actually a mislabeled two-channel setting; ignore it.
  2441. + */
  2442. + if (fmt->bNrChannels == 1 &&
  2443. + fmt->bSubframeSize == 2 &&
  2444. + altno == 2 && num == 3 &&
  2445. + fp && fp->altsetting == 1 && fp->channels == 1 &&
  2446. + fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
  2447. + protocol == UAC_VERSION_1 &&
  2448. + le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
  2449. + fp->maxpacksize * 2)
  2450. + continue;
  2451. +
  2452. + fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  2453. + if (! fp) {
  2454. + snd_printk(KERN_ERR "cannot malloc\n");
  2455. + return -ENOMEM;
  2456. + }
  2457. +
  2458. + fp->iface = iface_no;
  2459. + fp->altsetting = altno;
  2460. + fp->altset_idx = i;
  2461. + fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  2462. + fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  2463. + fp->datainterval = snd_usb_parse_datainterval(chip, alts);
  2464. + fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  2465. + /* num_channels is only set for v2 interfaces */
  2466. + fp->channels = num_channels;
  2467. + if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
  2468. + fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
  2469. + * (fp->maxpacksize & 0x7ff);
  2470. + fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
  2471. + fp->clock = clock;
  2472. +
  2473. + /* some quirks for attributes here */
  2474. +
  2475. + switch (chip->usb_id) {
  2476. + case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
  2477. + /* Optoplay sets the sample rate attribute although
  2478. + * it seems not supporting it in fact.
  2479. + */
  2480. + fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
  2481. + break;
  2482. + case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
  2483. + case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  2484. + /* doesn't set the sample rate attribute, but supports it */
  2485. + fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
  2486. + break;
  2487. + case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */
  2488. + case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
  2489. + case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
  2490. + case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
  2491. + an older model 77d:223) */
  2492. + /*
  2493. + * plantronics headset and Griffin iMic have set adaptive-in
  2494. + * although it's really not...
  2495. + */
  2496. + fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
  2497. + if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  2498. + fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
  2499. + else
  2500. + fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
  2501. + break;
  2502. + }
  2503. +
  2504. + /* ok, let's parse further... */
  2505. + if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
  2506. + kfree(fp->rate_table);
  2507. + kfree(fp);
  2508. + fp = NULL;
  2509. + continue;
  2510. + }
  2511. +
  2512. + snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
  2513. + err = snd_usb_add_audio_stream(chip, stream, fp);
  2514. + if (err < 0) {
  2515. + kfree(fp->rate_table);
  2516. + kfree(fp);
  2517. + return err;
  2518. + }
  2519. + /* try to set the interface... */
  2520. + usb_set_interface(chip->dev, iface_no, altno);
  2521. + snd_usb_init_pitch(chip, iface_no, alts, fp);
  2522. + snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);
  2523. + }
  2524. + return 0;
  2525. +}
  2526. +
  2527. diff --git a/sound/usb/stream.h b/sound/usb/stream.h
  2528. new file mode 100644
  2529. index 0000000..c97f679
  2530. --- /dev/null
  2531. +++ b/sound/usb/stream.h
  2532. @@ -0,0 +1,12 @@
  2533. +#ifndef __USBAUDIO_STREAM_H
  2534. +#define __USBAUDIO_STREAM_H
  2535. +
  2536. +int snd_usb_parse_audio_interface(struct snd_usb_audio *chip,
  2537. + int iface_no);
  2538. +
  2539. +int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
  2540. + int stream,
  2541. + struct audioformat *fp);
  2542. +
  2543. +#endif /* __USBAUDIO_STREAM_H */
  2544. +
  2545. diff --git a/sound/usb/urb.c b/sound/usb/urb.c
  2546. deleted file mode 100644
  2547. index e184349..0000000
  2548. --- a/sound/usb/urb.c
  2549. +++ /dev/null
  2550. @@ -1,941 +0,0 @@
  2551. -/*
  2552. - * This program is free software; you can redistribute it and/or modify
  2553. - * it under the terms of the GNU General Public License as published by
  2554. - * the Free Software Foundation; either version 2 of the License, or
  2555. - * (at your option) any later version.
  2556. - *
  2557. - * This program is distributed in the hope that it will be useful,
  2558. - * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2559. - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2560. - * GNU General Public License for more details.
  2561. - *
  2562. - * You should have received a copy of the GNU General Public License
  2563. - * along with this program; if not, write to the Free Software
  2564. - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  2565. - *
  2566. - */
  2567. -
  2568. -#include <linux/gfp.h>
  2569. -#include <linux/init.h>
  2570. -#include <linux/usb.h>
  2571. -#include <linux/usb/audio.h>
  2572. -
  2573. -#include <sound/core.h>
  2574. -#include <sound/pcm.h>
  2575. -
  2576. -#include "usbaudio.h"
  2577. -#include "helper.h"
  2578. -#include "card.h"
  2579. -#include "urb.h"
  2580. -#include "pcm.h"
  2581. -
  2582. -/*
  2583. - * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
  2584. - * this will overflow at approx 524 kHz
  2585. - */
  2586. -static inline unsigned get_usb_full_speed_rate(unsigned int rate)
  2587. -{
  2588. - return ((rate << 13) + 62) / 125;
  2589. -}
  2590. -
  2591. -/*
  2592. - * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
  2593. - * this will overflow at approx 4 MHz
  2594. - */
  2595. -static inline unsigned get_usb_high_speed_rate(unsigned int rate)
  2596. -{
  2597. - return ((rate << 10) + 62) / 125;
  2598. -}
  2599. -
  2600. -/*
  2601. - * unlink active urbs.
  2602. - */
  2603. -static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
  2604. -{
  2605. - struct snd_usb_audio *chip = subs->stream->chip;
  2606. - unsigned int i;
  2607. - int async;
  2608. -
  2609. - subs->running = 0;
  2610. -
  2611. - if (!force && subs->stream->chip->shutdown) /* to be sure... */
  2612. - return -EBADFD;
  2613. -
  2614. - async = !can_sleep && chip->async_unlink;
  2615. -
  2616. - if (!async && in_interrupt())
  2617. - return 0;
  2618. -
  2619. - for (i = 0; i < subs->nurbs; i++) {
  2620. - if (test_bit(i, &subs->active_mask)) {
  2621. - if (!test_and_set_bit(i, &subs->unlink_mask)) {
  2622. - struct urb *u = subs->dataurb[i].urb;
  2623. - if (async)
  2624. - usb_unlink_urb(u);
  2625. - else
  2626. - usb_kill_urb(u);
  2627. - }
  2628. - }
  2629. - }
  2630. - if (subs->syncpipe) {
  2631. - for (i = 0; i < SYNC_URBS; i++) {
  2632. - if (test_bit(i+16, &subs->active_mask)) {
  2633. - if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
  2634. - struct urb *u = subs->syncurb[i].urb;
  2635. - if (async)
  2636. - usb_unlink_urb(u);
  2637. - else
  2638. - usb_kill_urb(u);
  2639. - }
  2640. - }
  2641. - }
  2642. - }
  2643. - return 0;
  2644. -}
  2645. -
  2646. -
  2647. -/*
  2648. - * release a urb data
  2649. - */
  2650. -static void release_urb_ctx(struct snd_urb_ctx *u)
  2651. -{
  2652. - if (u->urb) {
  2653. - if (u->buffer_size)
  2654. - usb_free_coherent(u->subs->dev, u->buffer_size,
  2655. - u->urb->transfer_buffer,
  2656. - u->urb->transfer_dma);
  2657. - usb_free_urb(u->urb);
  2658. - u->urb = NULL;
  2659. - }
  2660. -}
  2661. -
  2662. -/*
  2663. - * wait until all urbs are processed.
  2664. - */
  2665. -static int wait_clear_urbs(struct snd_usb_substream *subs)
  2666. -{
  2667. - unsigned long end_time = jiffies + msecs_to_jiffies(1000);
  2668. - unsigned int i;
  2669. - int alive;
  2670. -
  2671. - do {
  2672. - alive = 0;
  2673. - for (i = 0; i < subs->nurbs; i++) {
  2674. - if (test_bit(i, &subs->active_mask))
  2675. - alive++;
  2676. - }
  2677. - if (subs->syncpipe) {
  2678. - for (i = 0; i < SYNC_URBS; i++) {
  2679. - if (test_bit(i + 16, &subs->active_mask))
  2680. - alive++;
  2681. - }
  2682. - }
  2683. - if (! alive)
  2684. - break;
  2685. - schedule_timeout_uninterruptible(1);
  2686. - } while (time_before(jiffies, end_time));
  2687. - if (alive)
  2688. - snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
  2689. - return 0;
  2690. -}
  2691. -
  2692. -/*
  2693. - * release a substream
  2694. - */
  2695. -void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force)
  2696. -{
  2697. - int i;
  2698. -
  2699. - /* stop urbs (to be sure) */
  2700. - deactivate_urbs(subs, force, 1);
  2701. - wait_clear_urbs(subs);
  2702. -
  2703. - for (i = 0; i < MAX_URBS; i++)
  2704. - release_urb_ctx(&subs->dataurb[i]);
  2705. - for (i = 0; i < SYNC_URBS; i++)
  2706. - release_urb_ctx(&subs->syncurb[i]);
  2707. - usb_free_coherent(subs->dev, SYNC_URBS * 4,
  2708. - subs->syncbuf, subs->sync_dma);
  2709. - subs->syncbuf = NULL;
  2710. - subs->nurbs = 0;
  2711. -}
  2712. -
  2713. -/*
  2714. - * complete callback from data urb
  2715. - */
  2716. -static void snd_complete_urb(struct urb *urb)
  2717. -{
  2718. - struct snd_urb_ctx *ctx = urb->context;
  2719. - struct snd_usb_substream *subs = ctx->subs;
  2720. - struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
  2721. - int err = 0;
  2722. -
  2723. - if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
  2724. - !subs->running || /* can be stopped during retire callback */
  2725. - (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
  2726. - (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  2727. - clear_bit(ctx->index, &subs->active_mask);
  2728. - if (err < 0) {
  2729. - snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
  2730. - snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  2731. - }
  2732. - }
  2733. -}
  2734. -
  2735. -
  2736. -/*
  2737. - * complete callback from sync urb
  2738. - */
  2739. -static void snd_complete_sync_urb(struct urb *urb)
  2740. -{
  2741. - struct snd_urb_ctx *ctx = urb->context;
  2742. - struct snd_usb_substream *subs = ctx->subs;
  2743. - struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
  2744. - int err = 0;
  2745. -
  2746. - if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
  2747. - !subs->running || /* can be stopped during retire callback */
  2748. - (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
  2749. - (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  2750. - clear_bit(ctx->index + 16, &subs->active_mask);
  2751. - if (err < 0) {
  2752. - snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
  2753. - snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  2754. - }
  2755. - }
  2756. -}
  2757. -
  2758. -
  2759. -/*
  2760. - * initialize a substream for plaback/capture
  2761. - */
  2762. -int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
  2763. - unsigned int period_bytes,
  2764. - unsigned int rate,
  2765. - unsigned int frame_bits)
  2766. -{
  2767. - unsigned int maxsize, i;
  2768. - int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  2769. - unsigned int urb_packs, total_packs, packs_per_ms;
  2770. - struct snd_usb_audio *chip = subs->stream->chip;
  2771. -
  2772. - /* calculate the frequency in 16.16 format */
  2773. - if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  2774. - subs->freqn = get_usb_full_speed_rate(rate);
  2775. - else
  2776. - subs->freqn = get_usb_high_speed_rate(rate);
  2777. - subs->freqm = subs->freqn;
  2778. - subs->freqshift = INT_MIN;
  2779. - /* calculate max. frequency */
  2780. - if (subs->maxpacksize) {
  2781. - /* whatever fits into a max. size packet */
  2782. - maxsize = subs->maxpacksize;
  2783. - subs->freqmax = (maxsize / (frame_bits >> 3))
  2784. - << (16 - subs->datainterval);
  2785. - } else {
  2786. - /* no max. packet size: just take 25% higher than nominal */
  2787. - subs->freqmax = subs->freqn + (subs->freqn >> 2);
  2788. - maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
  2789. - >> (16 - subs->datainterval);
  2790. - }
  2791. - subs->phase = 0;
  2792. -
  2793. - if (subs->fill_max)
  2794. - subs->curpacksize = subs->maxpacksize;
  2795. - else
  2796. - subs->curpacksize = maxsize;
  2797. -
  2798. - if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL)
  2799. - packs_per_ms = 8 >> subs->datainterval;
  2800. - else
  2801. - packs_per_ms = 1;
  2802. -
  2803. - if (is_playback) {
  2804. - urb_packs = max(chip->nrpacks, 1);
  2805. - urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
  2806. - } else
  2807. - urb_packs = 1;
  2808. - urb_packs *= packs_per_ms;
  2809. - if (subs->syncpipe)
  2810. - urb_packs = min(urb_packs, 1U << subs->syncinterval);
  2811. -
  2812. - /* decide how many packets to be used */
  2813. - if (is_playback) {
  2814. - unsigned int minsize, maxpacks;
  2815. - /* determine how small a packet can be */
  2816. - minsize = (subs->freqn >> (16 - subs->datainterval))
  2817. - * (frame_bits >> 3);
  2818. - /* with sync from device, assume it can be 12% lower */
  2819. - if (subs->syncpipe)
  2820. - minsize -= minsize >> 3;
  2821. - minsize = max(minsize, 1u);
  2822. - total_packs = (period_bytes + minsize - 1) / minsize;
  2823. - /* we need at least two URBs for queueing */
  2824. - if (total_packs < 2) {
  2825. - total_packs = 2;
  2826. - } else {
  2827. - /* and we don't want too long a queue either */
  2828. - maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
  2829. - total_packs = min(total_packs, maxpacks);
  2830. - }
  2831. - } else {
  2832. - while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
  2833. - urb_packs >>= 1;
  2834. - total_packs = MAX_URBS * urb_packs;
  2835. - }
  2836. - subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
  2837. - if (subs->nurbs > MAX_URBS) {
  2838. - /* too much... */
  2839. - subs->nurbs = MAX_URBS;
  2840. - total_packs = MAX_URBS * urb_packs;
  2841. - } else if (subs->nurbs < 2) {
  2842. - /* too little - we need at least two packets
  2843. - * to ensure contiguous playback/capture
  2844. - */
  2845. - subs->nurbs = 2;
  2846. - }
  2847. -
  2848. - /* allocate and initialize data urbs */
  2849. - for (i = 0; i < subs->nurbs; i++) {
  2850. - struct snd_urb_ctx *u = &subs->dataurb[i];
  2851. - u->index = i;
  2852. - u->subs = subs;
  2853. - u->packets = (i + 1) * total_packs / subs->nurbs
  2854. - - i * total_packs / subs->nurbs;
  2855. - u->buffer_size = maxsize * u->packets;
  2856. - if (subs->fmt_type == UAC_FORMAT_TYPE_II)
  2857. - u->packets++; /* for transfer delimiter */
  2858. - u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
  2859. - if (!u->urb)
  2860. - goto out_of_memory;
  2861. - u->urb->transfer_buffer =
  2862. - usb_alloc_coherent(subs->dev, u->buffer_size,
  2863. - GFP_KERNEL, &u->urb->transfer_dma);
  2864. - if (!u->urb->transfer_buffer)
  2865. - goto out_of_memory;
  2866. - u->urb->pipe = subs->datapipe;
  2867. - u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  2868. - u->urb->interval = 1 << subs->datainterval;
  2869. - u->urb->context = u;
  2870. - u->urb->complete = snd_complete_urb;
  2871. - }
  2872. -
  2873. - if (subs->syncpipe) {
  2874. - /* allocate and initialize sync urbs */
  2875. - subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4,
  2876. - GFP_KERNEL, &subs->sync_dma);
  2877. - if (!subs->syncbuf)
  2878. - goto out_of_memory;
  2879. - for (i = 0; i < SYNC_URBS; i++) {
  2880. - struct snd_urb_ctx *u = &subs->syncurb[i];
  2881. - u->index = i;
  2882. - u->subs = subs;
  2883. - u->packets = 1;
  2884. - u->urb = usb_alloc_urb(1, GFP_KERNEL);
  2885. - if (!u->urb)
  2886. - goto out_of_memory;
  2887. - u->urb->transfer_buffer = subs->syncbuf + i * 4;
  2888. - u->urb->transfer_dma = subs->sync_dma + i * 4;
  2889. - u->urb->transfer_buffer_length = 4;
  2890. - u->urb->pipe = subs->syncpipe;
  2891. - u->urb->transfer_flags = URB_ISO_ASAP |
  2892. - URB_NO_TRANSFER_DMA_MAP;
  2893. - u->urb->number_of_packets = 1;
  2894. - u->urb->interval = 1 << subs->syncinterval;
  2895. - u->urb->context = u;
  2896. - u->urb->complete = snd_complete_sync_urb;
  2897. - }
  2898. - }
  2899. - return 0;
  2900. -
  2901. -out_of_memory:
  2902. - snd_usb_release_substream_urbs(subs, 0);
  2903. - return -ENOMEM;
  2904. -}
  2905. -
  2906. -/*
  2907. - * prepare urb for full speed capture sync pipe
  2908. - *
  2909. - * fill the length and offset of each urb descriptor.
  2910. - * the fixed 10.14 frequency is passed through the pipe.
  2911. - */
  2912. -static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
  2913. - struct snd_pcm_runtime *runtime,
  2914. - struct urb *urb)
  2915. -{
  2916. - unsigned char *cp = urb->transfer_buffer;
  2917. - struct snd_urb_ctx *ctx = urb->context;
  2918. -
  2919. - urb->dev = ctx->subs->dev; /* we need to set this at each time */
  2920. - urb->iso_frame_desc[0].length = 3;
  2921. - urb->iso_frame_desc[0].offset = 0;
  2922. - cp[0] = subs->freqn >> 2;
  2923. - cp[1] = subs->freqn >> 10;
  2924. - cp[2] = subs->freqn >> 18;
  2925. - return 0;
  2926. -}
  2927. -
  2928. -/*
  2929. - * prepare urb for high speed capture sync pipe
  2930. - *
  2931. - * fill the length and offset of each urb descriptor.
  2932. - * the fixed 12.13 frequency is passed as 16.16 through the pipe.
  2933. - */
  2934. -static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
  2935. - struct snd_pcm_runtime *runtime,
  2936. - struct urb *urb)
  2937. -{
  2938. - unsigned char *cp = urb->transfer_buffer;
  2939. - struct snd_urb_ctx *ctx = urb->context;
  2940. -
  2941. - urb->dev = ctx->subs->dev; /* we need to set this at each time */
  2942. - urb->iso_frame_desc[0].length = 4;
  2943. - urb->iso_frame_desc[0].offset = 0;
  2944. - cp[0] = subs->freqn;
  2945. - cp[1] = subs->freqn >> 8;
  2946. - cp[2] = subs->freqn >> 16;
  2947. - cp[3] = subs->freqn >> 24;
  2948. - return 0;
  2949. -}
  2950. -
  2951. -/*
  2952. - * process after capture sync complete
  2953. - * - nothing to do
  2954. - */
  2955. -static int retire_capture_sync_urb(struct snd_usb_substream *subs,
  2956. - struct snd_pcm_runtime *runtime,
  2957. - struct urb *urb)
  2958. -{
  2959. - return 0;
  2960. -}
  2961. -
  2962. -/*
  2963. - * prepare urb for capture data pipe
  2964. - *
  2965. - * fill the offset and length of each descriptor.
  2966. - *
  2967. - * we use a temporary buffer to write the captured data.
  2968. - * since the length of written data is determined by host, we cannot
  2969. - * write onto the pcm buffer directly... the data is thus copied
  2970. - * later at complete callback to the global buffer.
  2971. - */
  2972. -static int prepare_capture_urb(struct snd_usb_substream *subs,
  2973. - struct snd_pcm_runtime *runtime,
  2974. - struct urb *urb)
  2975. -{
  2976. - int i, offs;
  2977. - struct snd_urb_ctx *ctx = urb->context;
  2978. -
  2979. - offs = 0;
  2980. - urb->dev = ctx->subs->dev; /* we need to set this at each time */
  2981. - for (i = 0; i < ctx->packets; i++) {
  2982. - urb->iso_frame_desc[i].offset = offs;
  2983. - urb->iso_frame_desc[i].length = subs->curpacksize;
  2984. - offs += subs->curpacksize;
  2985. - }
  2986. - urb->transfer_buffer_length = offs;
  2987. - urb->number_of_packets = ctx->packets;
  2988. - return 0;
  2989. -}
  2990. -
  2991. -/*
  2992. - * process after capture complete
  2993. - *
  2994. - * copy the data from each desctiptor to the pcm buffer, and
  2995. - * update the current position.
  2996. - */
  2997. -static int retire_capture_urb(struct snd_usb_substream *subs,
  2998. - struct snd_pcm_runtime *runtime,
  2999. - struct urb *urb)
  3000. -{
  3001. - unsigned long flags;
  3002. - unsigned char *cp;
  3003. - int i;
  3004. - unsigned int stride, frames, bytes, oldptr;
  3005. - int period_elapsed = 0;
  3006. -
  3007. - stride = runtime->frame_bits >> 3;
  3008. -
  3009. - for (i = 0; i < urb->number_of_packets; i++) {
  3010. - cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  3011. - if (urb->iso_frame_desc[i].status) {
  3012. - snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
  3013. - // continue;
  3014. - }
  3015. - bytes = urb->iso_frame_desc[i].actual_length;
  3016. - frames = bytes / stride;
  3017. - if (!subs->txfr_quirk)
  3018. - bytes = frames * stride;
  3019. - if (bytes % (runtime->sample_bits >> 3) != 0) {
  3020. -#ifdef CONFIG_SND_DEBUG_VERBOSE
  3021. - int oldbytes = bytes;
  3022. -#endif
  3023. - bytes = frames * stride;
  3024. - snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
  3025. - oldbytes, bytes);
  3026. - }
  3027. - /* update the current pointer */
  3028. - spin_lock_irqsave(&subs->lock, flags);
  3029. - oldptr = subs->hwptr_done;
  3030. - subs->hwptr_done += bytes;
  3031. - if (subs->hwptr_done >= runtime->buffer_size * stride)
  3032. - subs->hwptr_done -= runtime->buffer_size * stride;
  3033. - frames = (bytes + (oldptr % stride)) / stride;
  3034. - subs->transfer_done += frames;
  3035. - if (subs->transfer_done >= runtime->period_size) {
  3036. - subs->transfer_done -= runtime->period_size;
  3037. - period_elapsed = 1;
  3038. - }
  3039. - spin_unlock_irqrestore(&subs->lock, flags);
  3040. - /* copy a data chunk */
  3041. - if (oldptr + bytes > runtime->buffer_size * stride) {
  3042. - unsigned int bytes1 =
  3043. - runtime->buffer_size * stride - oldptr;
  3044. - memcpy(runtime->dma_area + oldptr, cp, bytes1);
  3045. - memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  3046. - } else {
  3047. - memcpy(runtime->dma_area + oldptr, cp, bytes);
  3048. - }
  3049. - }
  3050. - if (period_elapsed)
  3051. - snd_pcm_period_elapsed(subs->pcm_substream);
  3052. - return 0;
  3053. -}
  3054. -
  3055. -/*
  3056. - * Process after capture complete when paused. Nothing to do.
  3057. - */
  3058. -static int retire_paused_capture_urb(struct snd_usb_substream *subs,
  3059. - struct snd_pcm_runtime *runtime,
  3060. - struct urb *urb)
  3061. -{
  3062. - return 0;
  3063. -}
  3064. -
  3065. -
  3066. -/*
  3067. - * prepare urb for playback sync pipe
  3068. - *
  3069. - * set up the offset and length to receive the current frequency.
  3070. - */
  3071. -static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
  3072. - struct snd_pcm_runtime *runtime,
  3073. - struct urb *urb)
  3074. -{
  3075. - struct snd_urb_ctx *ctx = urb->context;
  3076. -
  3077. - urb->dev = ctx->subs->dev; /* we need to set this at each time */
  3078. - urb->iso_frame_desc[0].length = min(4u, ctx->subs->syncmaxsize);
  3079. - urb->iso_frame_desc[0].offset = 0;
  3080. - return 0;
  3081. -}
  3082. -
  3083. -/*
  3084. - * process after playback sync complete
  3085. - *
  3086. - * Full speed devices report feedback values in 10.14 format as samples per
  3087. - * frame, high speed devices in 16.16 format as samples per microframe.
  3088. - * Because the Audio Class 1 spec was written before USB 2.0, many high speed
  3089. - * devices use a wrong interpretation, some others use an entirely different
  3090. - * format. Therefore, we cannot predict what format any particular device uses
  3091. - * and must detect it automatically.
  3092. - */
  3093. -static int retire_playback_sync_urb(struct snd_usb_substream *subs,
  3094. - struct snd_pcm_runtime *runtime,
  3095. - struct urb *urb)
  3096. -{
  3097. - unsigned int f;
  3098. - int shift;
  3099. - unsigned long flags;
  3100. -
  3101. - if (urb->iso_frame_desc[0].status != 0 ||
  3102. - urb->iso_frame_desc[0].actual_length < 3)
  3103. - return 0;
  3104. -
  3105. - f = le32_to_cpup(urb->transfer_buffer);
  3106. - if (urb->iso_frame_desc[0].actual_length == 3)
  3107. - f &= 0x00ffffff;
  3108. - else
  3109. - f &= 0x0fffffff;
  3110. - if (f == 0)
  3111. - return 0;
  3112. -
  3113. - if (unlikely(subs->freqshift == INT_MIN)) {
  3114. - /*
  3115. - * The first time we see a feedback value, determine its format
  3116. - * by shifting it left or right until it matches the nominal
  3117. - * frequency value. This assumes that the feedback does not
  3118. - * differ from the nominal value more than +50% or -25%.
  3119. - */
  3120. - shift = 0;
  3121. - while (f < subs->freqn - subs->freqn / 4) {
  3122. - f <<= 1;
  3123. - shift++;
  3124. - }
  3125. - while (f > subs->freqn + subs->freqn / 2) {
  3126. - f >>= 1;
  3127. - shift--;
  3128. - }
  3129. - subs->freqshift = shift;
  3130. - }
  3131. - else if (subs->freqshift >= 0)
  3132. - f <<= subs->freqshift;
  3133. - else
  3134. - f >>= -subs->freqshift;
  3135. -
  3136. - if (likely(f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax)) {
  3137. - /*
  3138. - * If the frequency looks valid, set it.
  3139. - * This value is referred to in prepare_playback_urb().
  3140. - */
  3141. - spin_lock_irqsave(&subs->lock, flags);
  3142. - subs->freqm = f;
  3143. - spin_unlock_irqrestore(&subs->lock, flags);
  3144. - } else {
  3145. - /*
  3146. - * Out of range; maybe the shift value is wrong.
  3147. - * Reset it so that we autodetect again the next time.
  3148. - */
  3149. - subs->freqshift = INT_MIN;
  3150. - }
  3151. -
  3152. - return 0;
  3153. -}
  3154. -
  3155. -/* determine the number of frames in the next packet */
  3156. -static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
  3157. -{
  3158. - if (subs->fill_max)
  3159. - return subs->maxframesize;
  3160. - else {
  3161. - subs->phase = (subs->phase & 0xffff)
  3162. - + (subs->freqm << subs->datainterval);
  3163. - return min(subs->phase >> 16, subs->maxframesize);
  3164. - }
  3165. -}
  3166. -
  3167. -/*
  3168. - * Prepare urb for streaming before playback starts or when paused.
  3169. - *
  3170. - * We don't have any data, so we send silence.
  3171. - */
  3172. -static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
  3173. - struct snd_pcm_runtime *runtime,
  3174. - struct urb *urb)
  3175. -{
  3176. - unsigned int i, offs, counts;
  3177. - struct snd_urb_ctx *ctx = urb->context;
  3178. - int stride = runtime->frame_bits >> 3;
  3179. -
  3180. - offs = 0;
  3181. - urb->dev = ctx->subs->dev;
  3182. - for (i = 0; i < ctx->packets; ++i) {
  3183. - counts = snd_usb_audio_next_packet_size(subs);
  3184. - urb->iso_frame_desc[i].offset = offs * stride;
  3185. - urb->iso_frame_desc[i].length = counts * stride;
  3186. - offs += counts;
  3187. - }
  3188. - urb->number_of_packets = ctx->packets;
  3189. - urb->transfer_buffer_length = offs * stride;
  3190. - memset(urb->transfer_buffer,
  3191. - runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
  3192. - offs * stride);
  3193. - return 0;
  3194. -}
  3195. -
  3196. -/*
  3197. - * prepare urb for playback data pipe
  3198. - *
  3199. - * Since a URB can handle only a single linear buffer, we must use double
  3200. - * buffering when the data to be transferred overflows the buffer boundary.
  3201. - * To avoid inconsistencies when updating hwptr_done, we use double buffering
  3202. - * for all URBs.
  3203. - */
  3204. -static int prepare_playback_urb(struct snd_usb_substream *subs,
  3205. - struct snd_pcm_runtime *runtime,
  3206. - struct urb *urb)
  3207. -{
  3208. - int i, stride;
  3209. - unsigned int counts, frames, bytes;
  3210. - unsigned long flags;
  3211. - int period_elapsed = 0;
  3212. - struct snd_urb_ctx *ctx = urb->context;
  3213. -
  3214. - stride = runtime->frame_bits >> 3;
  3215. -
  3216. - frames = 0;
  3217. - urb->dev = ctx->subs->dev; /* we need to set this at each time */
  3218. - urb->number_of_packets = 0;
  3219. - spin_lock_irqsave(&subs->lock, flags);
  3220. - for (i = 0; i < ctx->packets; i++) {
  3221. - counts = snd_usb_audio_next_packet_size(subs);
  3222. - /* set up descriptor */
  3223. - urb->iso_frame_desc[i].offset = frames * stride;
  3224. - urb->iso_frame_desc[i].length = counts * stride;
  3225. - frames += counts;
  3226. - urb->number_of_packets++;
  3227. - subs->transfer_done += counts;
  3228. - if (subs->transfer_done >= runtime->period_size) {
  3229. - subs->transfer_done -= runtime->period_size;
  3230. - period_elapsed = 1;
  3231. - if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  3232. - if (subs->transfer_done > 0) {
  3233. - /* FIXME: fill-max mode is not
  3234. - * supported yet */
  3235. - frames -= subs->transfer_done;
  3236. - counts -= subs->transfer_done;
  3237. - urb->iso_frame_desc[i].length =
  3238. - counts * stride;
  3239. - subs->transfer_done = 0;
  3240. - }
  3241. - i++;
  3242. - if (i < ctx->packets) {
  3243. - /* add a transfer delimiter */
  3244. - urb->iso_frame_desc[i].offset =
  3245. - frames * stride;
  3246. - urb->iso_frame_desc[i].length = 0;
  3247. - urb->number_of_packets++;
  3248. - }
  3249. - break;
  3250. - }
  3251. - }
  3252. - if (period_elapsed) /* finish at the period boundary */
  3253. - break;
  3254. - }
  3255. - bytes = frames * stride;
  3256. - if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  3257. - /* err, the transferred area goes over buffer boundary. */
  3258. - unsigned int bytes1 =
  3259. - runtime->buffer_size * stride - subs->hwptr_done;
  3260. - memcpy(urb->transfer_buffer,
  3261. - runtime->dma_area + subs->hwptr_done, bytes1);
  3262. - memcpy(urb->transfer_buffer + bytes1,
  3263. - runtime->dma_area, bytes - bytes1);
  3264. - } else {
  3265. - memcpy(urb->transfer_buffer,
  3266. - runtime->dma_area + subs->hwptr_done, bytes);
  3267. - }
  3268. - subs->hwptr_done += bytes;
  3269. - if (subs->hwptr_done >= runtime->buffer_size * stride)
  3270. - subs->hwptr_done -= runtime->buffer_size * stride;
  3271. - runtime->delay += frames;
  3272. - spin_unlock_irqrestore(&subs->lock, flags);
  3273. - urb->transfer_buffer_length = bytes;
  3274. - if (period_elapsed)
  3275. - snd_pcm_period_elapsed(subs->pcm_substream);
  3276. - return 0;
  3277. -}
  3278. -
  3279. -/*
  3280. - * process after playback data complete
  3281. - * - decrease the delay count again
  3282. - */
  3283. -static int retire_playback_urb(struct snd_usb_substream *subs,
  3284. - struct snd_pcm_runtime *runtime,
  3285. - struct urb *urb)
  3286. -{
  3287. - unsigned long flags;
  3288. - int stride = runtime->frame_bits >> 3;
  3289. - int processed = urb->transfer_buffer_length / stride;
  3290. -
  3291. - spin_lock_irqsave(&subs->lock, flags);
  3292. - if (processed > runtime->delay)
  3293. - runtime->delay = 0;
  3294. - else
  3295. - runtime->delay -= processed;
  3296. - spin_unlock_irqrestore(&subs->lock, flags);
  3297. - return 0;
  3298. -}
  3299. -
  3300. -static const char *usb_error_string(int err)
  3301. -{
  3302. - switch (err) {
  3303. - case -ENODEV:
  3304. - return "no device";
  3305. - case -ENOENT:
  3306. - return "endpoint not enabled";
  3307. - case -EPIPE:
  3308. - return "endpoint stalled";
  3309. - case -ENOSPC:
  3310. - return "not enough bandwidth";
  3311. - case -ESHUTDOWN:
  3312. - return "device disabled";
  3313. - case -EHOSTUNREACH:
  3314. - return "device suspended";
  3315. - case -EINVAL:
  3316. - case -EAGAIN:
  3317. - case -EFBIG:
  3318. - case -EMSGSIZE:
  3319. - return "internal error";
  3320. - default:
  3321. - return "unknown error";
  3322. - }
  3323. -}
  3324. -
  3325. -/*
  3326. - * set up and start data/sync urbs
  3327. - */
  3328. -static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
  3329. -{
  3330. - unsigned int i;
  3331. - int err;
  3332. -
  3333. - if (subs->stream->chip->shutdown)
  3334. - return -EBADFD;
  3335. -
  3336. - for (i = 0; i < subs->nurbs; i++) {
  3337. - if (snd_BUG_ON(!subs->dataurb[i].urb))
  3338. - return -EINVAL;
  3339. - if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
  3340. - snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
  3341. - goto __error;
  3342. - }
  3343. - }
  3344. - if (subs->syncpipe) {
  3345. - for (i = 0; i < SYNC_URBS; i++) {
  3346. - if (snd_BUG_ON(!subs->syncurb[i].urb))
  3347. - return -EINVAL;
  3348. - if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
  3349. - snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
  3350. - goto __error;
  3351. - }
  3352. - }
  3353. - }
  3354. -
  3355. - subs->active_mask = 0;
  3356. - subs->unlink_mask = 0;
  3357. - subs->running = 1;
  3358. - for (i = 0; i < subs->nurbs; i++) {
  3359. - err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
  3360. - if (err < 0) {
  3361. - snd_printk(KERN_ERR "cannot submit datapipe "
  3362. - "for urb %d, error %d: %s\n",
  3363. - i, err, usb_error_string(err));
  3364. - goto __error;
  3365. - }
  3366. - set_bit(i, &subs->active_mask);
  3367. - }
  3368. - if (subs->syncpipe) {
  3369. - for (i = 0; i < SYNC_URBS; i++) {
  3370. - err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
  3371. - if (err < 0) {
  3372. - snd_printk(KERN_ERR "cannot submit syncpipe "
  3373. - "for urb %d, error %d: %s\n",
  3374. - i, err, usb_error_string(err));
  3375. - goto __error;
  3376. - }
  3377. - set_bit(i + 16, &subs->active_mask);
  3378. - }
  3379. - }
  3380. - return 0;
  3381. -
  3382. - __error:
  3383. - // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
  3384. - deactivate_urbs(subs, 0, 0);
  3385. - return -EPIPE;
  3386. -}
  3387. -
  3388. -
  3389. -/*
  3390. - */
  3391. -static struct snd_urb_ops audio_urb_ops[2] = {
  3392. - {
  3393. - .prepare = prepare_nodata_playback_urb,
  3394. - .retire = retire_playback_urb,
  3395. - .prepare_sync = prepare_playback_sync_urb,
  3396. - .retire_sync = retire_playback_sync_urb,
  3397. - },
  3398. - {
  3399. - .prepare = prepare_capture_urb,
  3400. - .retire = retire_capture_urb,
  3401. - .prepare_sync = prepare_capture_sync_urb,
  3402. - .retire_sync = retire_capture_sync_urb,
  3403. - },
  3404. -};
  3405. -
  3406. -/*
  3407. - * initialize the substream instance.
  3408. - */
  3409. -
  3410. -void snd_usb_init_substream(struct snd_usb_stream *as,
  3411. - int stream, struct audioformat *fp)
  3412. -{
  3413. - struct snd_usb_substream *subs = &as->substream[stream];
  3414. -
  3415. - INIT_LIST_HEAD(&subs->fmt_list);
  3416. - spin_lock_init(&subs->lock);
  3417. -
  3418. - subs->stream = as;
  3419. - subs->direction = stream;
  3420. - subs->dev = as->chip->dev;
  3421. - subs->txfr_quirk = as->chip->txfr_quirk;
  3422. - subs->ops = audio_urb_ops[stream];
  3423. - if (snd_usb_get_speed(subs->dev) >= USB_SPEED_HIGH)
  3424. - subs->ops.prepare_sync = prepare_capture_sync_urb_hs;
  3425. -
  3426. - snd_usb_set_pcm_ops(as->pcm, stream);
  3427. -
  3428. - list_add_tail(&fp->list, &subs->fmt_list);
  3429. - subs->formats |= fp->formats;
  3430. - subs->endpoint = fp->endpoint;
  3431. - subs->num_formats++;
  3432. - subs->fmt_type = fp->fmt_type;
  3433. -}
  3434. -
  3435. -int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  3436. -{
  3437. - struct snd_usb_substream *subs = substream->runtime->private_data;
  3438. -
  3439. - switch (cmd) {
  3440. - case SNDRV_PCM_TRIGGER_START:
  3441. - case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  3442. - subs->ops.prepare = prepare_playback_urb;
  3443. - return 0;
  3444. - case SNDRV_PCM_TRIGGER_STOP:
  3445. - return deactivate_urbs(subs, 0, 0);
  3446. - case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  3447. - subs->ops.prepare = prepare_nodata_playback_urb;
  3448. - return 0;
  3449. - }
  3450. -
  3451. - return -EINVAL;
  3452. -}
  3453. -
  3454. -int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  3455. -{
  3456. - struct snd_usb_substream *subs = substream->runtime->private_data;
  3457. -
  3458. - switch (cmd) {
  3459. - case SNDRV_PCM_TRIGGER_START:
  3460. - subs->ops.retire = retire_capture_urb;
  3461. - return start_urbs(subs, substream->runtime);
  3462. - case SNDRV_PCM_TRIGGER_STOP:
  3463. - return deactivate_urbs(subs, 0, 0);
  3464. - case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  3465. - subs->ops.retire = retire_paused_capture_urb;
  3466. - return 0;
  3467. - case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  3468. - subs->ops.retire = retire_capture_urb;
  3469. - return 0;
  3470. - }
  3471. -
  3472. - return -EINVAL;
  3473. -}
  3474. -
  3475. -int snd_usb_substream_prepare(struct snd_usb_substream *subs,
  3476. - struct snd_pcm_runtime *runtime)
  3477. -{
  3478. - /* clear urbs (to be sure) */
  3479. - deactivate_urbs(subs, 0, 1);
  3480. - wait_clear_urbs(subs);
  3481. -
  3482. - /* for playback, submit the URBs now; otherwise, the first hwptr_done
  3483. - * updates for all URBs would happen at the same time when starting */
  3484. - if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
  3485. - subs->ops.prepare = prepare_nodata_playback_urb;
  3486. - return start_urbs(subs, runtime);
  3487. - }
  3488. -
  3489. - return 0;
  3490. -}
  3491. -
  3492. diff --git a/sound/usb/urb.h b/sound/usb/urb.h
  3493. deleted file mode 100644
  3494. index 888da38..0000000
  3495. --- a/sound/usb/urb.h
  3496. +++ /dev/null
  3497. @@ -1,21 +0,0 @@
  3498. -#ifndef __USBAUDIO_URB_H
  3499. -#define __USBAUDIO_URB_H
  3500. -
  3501. -void snd_usb_init_substream(struct snd_usb_stream *as,
  3502. - int stream,
  3503. - struct audioformat *fp);
  3504. -
  3505. -int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
  3506. - unsigned int period_bytes,
  3507. - unsigned int rate,
  3508. - unsigned int frame_bits);
  3509. -
  3510. -void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force);
  3511. -
  3512. -int snd_usb_substream_prepare(struct snd_usb_substream *subs,
  3513. - struct snd_pcm_runtime *runtime);
  3514. -
  3515. -int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd);
  3516. -int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd);
  3517. -
  3518. -#endif /* __USBAUDIO_URB_H */
  3519. diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h
  3520. index 1e79986..4aa9e11 100644
  3521. --- a/sound/usb/usbaudio.h
  3522. +++ b/sound/usb/usbaudio.h
  3523. @@ -39,13 +39,14 @@ struct snd_usb_audio {
  3524. struct mutex shutdown_mutex;
  3525. unsigned int shutdown:1;
  3526. unsigned int probing:1;
  3527. - unsigned int autosuspended:1;
  3528. + unsigned int autosuspended:1;
  3529. unsigned int txfr_quirk:1; /* Subframe boundaries on transfers */
  3530. -
  3531. +
  3532. int num_interfaces;
  3533. int num_suspended_intf;
  3534.  
  3535. struct list_head pcm_list; /* list of pcm streams */
  3536. + struct list_head ep_list; /* list of audio-related endpoints */
  3537. int pcm_devs;
  3538.  
  3539. struct list_head midi_list; /* list of midi interfaces */
Add Comment
Please, Sign In to add comment