aboutsummaryrefslogtreecommitdiffstats
path: root/sound/usb/usbaudio.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/usb/usbaudio.c')
-rw-r--r--sound/usb/usbaudio.c343
1 files changed, 159 insertions, 184 deletions
diff --git a/sound/usb/usbaudio.c b/sound/usb/usbaudio.c
index eec32e1a3020..823296d7d578 100644
--- a/sound/usb/usbaudio.c
+++ b/sound/usb/usbaudio.c
@@ -107,7 +107,7 @@ MODULE_PARM_DESC(ignore_ctl_error,
107#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */ 107#define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
108#define MAX_URBS 8 108#define MAX_URBS 8
109#define SYNC_URBS 4 /* always four urbs for sync */ 109#define SYNC_URBS 4 /* always four urbs for sync */
110#define MIN_PACKS_URB 1 /* minimum 1 packet per urb */ 110#define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */
111 111
112struct audioformat { 112struct audioformat {
113 struct list_head list; 113 struct list_head list;
@@ -121,6 +121,7 @@ struct audioformat {
121 unsigned char attributes; /* corresponding attributes of cs endpoint */ 121 unsigned char attributes; /* corresponding attributes of cs endpoint */
122 unsigned char endpoint; /* endpoint */ 122 unsigned char endpoint; /* endpoint */
123 unsigned char ep_attr; /* endpoint attributes */ 123 unsigned char ep_attr; /* endpoint attributes */
124 unsigned char datainterval; /* log_2 of data packet interval */
124 unsigned int maxpacksize; /* max. packet size */ 125 unsigned int maxpacksize; /* max. packet size */
125 unsigned int rates; /* rate bitmasks */ 126 unsigned int rates; /* rate bitmasks */
126 unsigned int rate_min, rate_max; /* min/max rates */ 127 unsigned int rate_min, rate_max; /* min/max rates */
@@ -170,7 +171,6 @@ struct snd_usb_substream {
170 unsigned int curframesize; /* current packet size in frames (for capture) */ 171 unsigned int curframesize; /* current packet size in frames (for capture) */
171 unsigned int fill_max: 1; /* fill max packet size always */ 172 unsigned int fill_max: 1; /* fill max packet size always */
172 unsigned int fmt_type; /* USB audio format type (1-3) */ 173 unsigned int fmt_type; /* USB audio format type (1-3) */
173 unsigned int packs_per_ms; /* packets per millisecond (for playback) */
174 174
175 unsigned int running: 1; /* running status */ 175 unsigned int running: 1; /* running status */
176 176
@@ -525,7 +525,7 @@ static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
525/* 525/*
526 * Prepare urb for streaming before playback starts or when paused. 526 * Prepare urb for streaming before playback starts or when paused.
527 * 527 *
528 * We don't have any data, so we send a frame of silence. 528 * We don't have any data, so we send silence.
529 */ 529 */
530static int prepare_nodata_playback_urb(struct snd_usb_substream *subs, 530static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
531 struct snd_pcm_runtime *runtime, 531 struct snd_pcm_runtime *runtime,
@@ -537,13 +537,13 @@ static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
537 537
538 offs = 0; 538 offs = 0;
539 urb->dev = ctx->subs->dev; 539 urb->dev = ctx->subs->dev;
540 urb->number_of_packets = subs->packs_per_ms; 540 for (i = 0; i < ctx->packets; ++i) {
541 for (i = 0; i < subs->packs_per_ms; ++i) {
542 counts = snd_usb_audio_next_packet_size(subs); 541 counts = snd_usb_audio_next_packet_size(subs);
543 urb->iso_frame_desc[i].offset = offs * stride; 542 urb->iso_frame_desc[i].offset = offs * stride;
544 urb->iso_frame_desc[i].length = counts * stride; 543 urb->iso_frame_desc[i].length = counts * stride;
545 offs += counts; 544 offs += counts;
546 } 545 }
546 urb->number_of_packets = ctx->packets;
547 urb->transfer_buffer_length = offs * stride; 547 urb->transfer_buffer_length = offs * stride;
548 memset(urb->transfer_buffer, 548 memset(urb->transfer_buffer,
549 subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0, 549 subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
@@ -607,9 +607,7 @@ static int prepare_playback_urb(struct snd_usb_substream *subs,
607 break; 607 break;
608 } 608 }
609 } 609 }
610 /* finish at the frame boundary at/after the period boundary */ 610 if (period_elapsed) /* finish at the period boundary */
611 if (period_elapsed &&
612 (i & (subs->packs_per_ms - 1)) == subs->packs_per_ms - 1)
613 break; 611 break;
614 } 612 }
615 if (subs->hwptr_done + offs > runtime->buffer_size) { 613 if (subs->hwptr_done + offs > runtime->buffer_size) {
@@ -1034,9 +1032,9 @@ static void release_substream_urbs(struct snd_usb_substream *subs, int force)
1034static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes, 1032static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes,
1035 unsigned int rate, unsigned int frame_bits) 1033 unsigned int rate, unsigned int frame_bits)
1036{ 1034{
1037 unsigned int maxsize, n, i; 1035 unsigned int maxsize, i;
1038 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; 1036 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1039 unsigned int npacks[MAX_URBS], urb_packs, total_packs, packs_per_ms; 1037 unsigned int urb_packs, total_packs, packs_per_ms;
1040 1038
1041 /* calculate the frequency in 16.16 format */ 1039 /* calculate the frequency in 16.16 format */
1042 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) 1040 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
@@ -1067,11 +1065,9 @@ static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int peri
1067 packs_per_ms = 8 >> subs->datainterval; 1065 packs_per_ms = 8 >> subs->datainterval;
1068 else 1066 else
1069 packs_per_ms = 1; 1067 packs_per_ms = 1;
1070 subs->packs_per_ms = packs_per_ms;
1071 1068
1072 if (is_playback) { 1069 if (is_playback) {
1073 urb_packs = nrpacks; 1070 urb_packs = max(nrpacks, 1);
1074 urb_packs = max(urb_packs, (unsigned int)MIN_PACKS_URB);
1075 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS); 1071 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
1076 } else 1072 } else
1077 urb_packs = 1; 1073 urb_packs = 1;
@@ -1079,7 +1075,7 @@ static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int peri
1079 1075
1080 /* decide how many packets to be used */ 1076 /* decide how many packets to be used */
1081 if (is_playback) { 1077 if (is_playback) {
1082 unsigned int minsize; 1078 unsigned int minsize, maxpacks;
1083 /* determine how small a packet can be */ 1079 /* determine how small a packet can be */
1084 minsize = (subs->freqn >> (16 - subs->datainterval)) 1080 minsize = (subs->freqn >> (16 - subs->datainterval))
1085 * (frame_bits >> 3); 1081 * (frame_bits >> 3);
@@ -1088,13 +1084,17 @@ static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int peri
1088 minsize -= minsize >> 3; 1084 minsize -= minsize >> 3;
1089 minsize = max(minsize, 1u); 1085 minsize = max(minsize, 1u);
1090 total_packs = (period_bytes + minsize - 1) / minsize; 1086 total_packs = (period_bytes + minsize - 1) / minsize;
1091 /* round up to multiple of packs_per_ms */
1092 total_packs = (total_packs + packs_per_ms - 1)
1093 & ~(packs_per_ms - 1);
1094 /* we need at least two URBs for queueing */ 1087 /* we need at least two URBs for queueing */
1095 if (total_packs < 2 * MIN_PACKS_URB * packs_per_ms) 1088 if (total_packs < 2) {
1096 total_packs = 2 * MIN_PACKS_URB * packs_per_ms; 1089 total_packs = 2;
1090 } else {
1091 /* and we don't want too long a queue either */
1092 maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
1093 total_packs = min(total_packs, maxpacks);
1094 }
1097 } else { 1095 } else {
1096 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
1097 urb_packs >>= 1;
1098 total_packs = MAX_URBS * urb_packs; 1098 total_packs = MAX_URBS * urb_packs;
1099 } 1099 }
1100 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs; 1100 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
@@ -1102,31 +1102,11 @@ static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int peri
1102 /* too much... */ 1102 /* too much... */
1103 subs->nurbs = MAX_URBS; 1103 subs->nurbs = MAX_URBS;
1104 total_packs = MAX_URBS * urb_packs; 1104 total_packs = MAX_URBS * urb_packs;
1105 } 1105 } else if (subs->nurbs < 2) {
1106 n = total_packs;
1107 for (i = 0; i < subs->nurbs; i++) {
1108 npacks[i] = n > urb_packs ? urb_packs : n;
1109 n -= urb_packs;
1110 }
1111 if (subs->nurbs <= 1) {
1112 /* too little - we need at least two packets 1106 /* too little - we need at least two packets
1113 * to ensure contiguous playback/capture 1107 * to ensure contiguous playback/capture
1114 */ 1108 */
1115 subs->nurbs = 2; 1109 subs->nurbs = 2;
1116 npacks[0] = (total_packs + 1) / 2;
1117 npacks[1] = total_packs - npacks[0];
1118 } else if (npacks[subs->nurbs-1] < MIN_PACKS_URB * packs_per_ms) {
1119 /* the last packet is too small.. */
1120 if (subs->nurbs > 2) {
1121 /* merge to the first one */
1122 npacks[0] += npacks[subs->nurbs - 1];
1123 subs->nurbs--;
1124 } else {
1125 /* divide to two */
1126 subs->nurbs = 2;
1127 npacks[0] = (total_packs + 1) / 2;
1128 npacks[1] = total_packs - npacks[0];
1129 }
1130 } 1110 }
1131 1111
1132 /* allocate and initialize data urbs */ 1112 /* allocate and initialize data urbs */
@@ -1134,7 +1114,8 @@ static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int peri
1134 struct snd_urb_ctx *u = &subs->dataurb[i]; 1114 struct snd_urb_ctx *u = &subs->dataurb[i];
1135 u->index = i; 1115 u->index = i;
1136 u->subs = subs; 1116 u->subs = subs;
1137 u->packets = npacks[i]; 1117 u->packets = (i + 1) * total_packs / subs->nurbs
1118 - i * total_packs / subs->nurbs;
1138 u->buffer_size = maxsize * u->packets; 1119 u->buffer_size = maxsize * u->packets;
1139 if (subs->fmt_type == USB_FORMAT_TYPE_II) 1120 if (subs->fmt_type == USB_FORMAT_TYPE_II)
1140 u->packets++; /* for transfer delimiter */ 1121 u->packets++; /* for transfer delimiter */
@@ -1292,14 +1273,14 @@ static int init_usb_sample_rate(struct usb_device *dev, int iface,
1292 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR, 1273 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
1293 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, 1274 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
1294 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) { 1275 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1295 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep 0x%x\n", 1276 snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
1296 dev->devnum, iface, fmt->altsetting, rate, ep); 1277 dev->devnum, iface, fmt->altsetting, rate, ep);
1297 return err; 1278 return err;
1298 } 1279 }
1299 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR, 1280 if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR,
1300 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN, 1281 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
1301 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) { 1282 SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
1302 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep 0x%x\n", 1283 snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
1303 dev->devnum, iface, fmt->altsetting, ep); 1284 dev->devnum, iface, fmt->altsetting, ep);
1304 return 0; /* some devices don't support reading */ 1285 return 0; /* some devices don't support reading */
1305 } 1286 }
@@ -1365,12 +1346,7 @@ static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
1365 subs->datapipe = usb_sndisocpipe(dev, ep); 1346 subs->datapipe = usb_sndisocpipe(dev, ep);
1366 else 1347 else
1367 subs->datapipe = usb_rcvisocpipe(dev, ep); 1348 subs->datapipe = usb_rcvisocpipe(dev, ep);
1368 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH && 1349 subs->datainterval = fmt->datainterval;
1369 get_endpoint(alts, 0)->bInterval >= 1 &&
1370 get_endpoint(alts, 0)->bInterval <= 4)
1371 subs->datainterval = get_endpoint(alts, 0)->bInterval - 1;
1372 else
1373 subs->datainterval = 0;
1374 subs->syncpipe = subs->syncinterval = 0; 1350 subs->syncpipe = subs->syncinterval = 0;
1375 subs->maxpacksize = fmt->maxpacksize; 1351 subs->maxpacksize = fmt->maxpacksize;
1376 subs->fill_max = 0; 1352 subs->fill_max = 0;
@@ -1431,9 +1407,11 @@ static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
1431 subs->cur_audiofmt = fmt; 1407 subs->cur_audiofmt = fmt;
1432 1408
1433#if 0 1409#if 0
1434 printk("setting done: format = %d, rate = %d..%d, channels = %d\n", 1410 printk(KERN_DEBUG
1411 "setting done: format = %d, rate = %d..%d, channels = %d\n",
1435 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels); 1412 fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
1436 printk(" datapipe = 0x%0x, syncpipe = 0x%0x\n", 1413 printk(KERN_DEBUG
1414 " datapipe = 0x%0x, syncpipe = 0x%0x\n",
1437 subs->datapipe, subs->syncpipe); 1415 subs->datapipe, subs->syncpipe);
1438#endif 1416#endif
1439 1417
@@ -1468,7 +1446,7 @@ static int snd_usb_hw_params(struct snd_pcm_substream *substream,
1468 channels = params_channels(hw_params); 1446 channels = params_channels(hw_params);
1469 fmt = find_format(subs, format, rate, channels); 1447 fmt = find_format(subs, format, rate, channels);
1470 if (!fmt) { 1448 if (!fmt) {
1471 snd_printd(KERN_DEBUG "cannot set format: format = 0x%x, rate = %d, channels = %d\n", 1449 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
1472 format, rate, channels); 1450 format, rate, channels);
1473 return -EINVAL; 1451 return -EINVAL;
1474 } 1452 }
@@ -1581,11 +1559,15 @@ static struct snd_pcm_hardware snd_usb_hardware =
1581#define hwc_debug(fmt, args...) /**/ 1559#define hwc_debug(fmt, args...) /**/
1582#endif 1560#endif
1583 1561
1584static int hw_check_valid_format(struct snd_pcm_hw_params *params, struct audioformat *fp) 1562static int hw_check_valid_format(struct snd_usb_substream *subs,
1563 struct snd_pcm_hw_params *params,
1564 struct audioformat *fp)
1585{ 1565{
1586 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 1566 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1587 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 1567 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1588 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 1568 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1569 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1570 unsigned int ptime;
1589 1571
1590 /* check the format */ 1572 /* check the format */
1591 if (!snd_mask_test(fmts, fp->format)) { 1573 if (!snd_mask_test(fmts, fp->format)) {
@@ -1606,6 +1588,14 @@ static int hw_check_valid_format(struct snd_pcm_hw_params *params, struct audiof
1606 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min); 1588 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1607 return 0; 1589 return 0;
1608 } 1590 }
1591 /* check whether the period time is >= the data packet interval */
1592 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
1593 ptime = 125 * (1 << fp->datainterval);
1594 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
1595 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
1596 return 0;
1597 }
1598 }
1609 return 1; 1599 return 1;
1610} 1600}
1611 1601
@@ -1624,7 +1614,7 @@ static int hw_rule_rate(struct snd_pcm_hw_params *params,
1624 list_for_each(p, &subs->fmt_list) { 1614 list_for_each(p, &subs->fmt_list) {
1625 struct audioformat *fp; 1615 struct audioformat *fp;
1626 fp = list_entry(p, struct audioformat, list); 1616 fp = list_entry(p, struct audioformat, list);
1627 if (!hw_check_valid_format(params, fp)) 1617 if (!hw_check_valid_format(subs, params, fp))
1628 continue; 1618 continue;
1629 if (changed++) { 1619 if (changed++) {
1630 if (rmin > fp->rate_min) 1620 if (rmin > fp->rate_min)
@@ -1678,7 +1668,7 @@ static int hw_rule_channels(struct snd_pcm_hw_params *params,
1678 list_for_each(p, &subs->fmt_list) { 1668 list_for_each(p, &subs->fmt_list) {
1679 struct audioformat *fp; 1669 struct audioformat *fp;
1680 fp = list_entry(p, struct audioformat, list); 1670 fp = list_entry(p, struct audioformat, list);
1681 if (!hw_check_valid_format(params, fp)) 1671 if (!hw_check_valid_format(subs, params, fp))
1682 continue; 1672 continue;
1683 if (changed++) { 1673 if (changed++) {
1684 if (rmin > fp->channels) 1674 if (rmin > fp->channels)
@@ -1731,7 +1721,7 @@ static int hw_rule_format(struct snd_pcm_hw_params *params,
1731 list_for_each(p, &subs->fmt_list) { 1721 list_for_each(p, &subs->fmt_list) {
1732 struct audioformat *fp; 1722 struct audioformat *fp;
1733 fp = list_entry(p, struct audioformat, list); 1723 fp = list_entry(p, struct audioformat, list);
1734 if (!hw_check_valid_format(params, fp)) 1724 if (!hw_check_valid_format(subs, params, fp))
1735 continue; 1725 continue;
1736 fbits |= (1ULL << fp->format); 1726 fbits |= (1ULL << fp->format);
1737 } 1727 }
@@ -1749,95 +1739,42 @@ static int hw_rule_format(struct snd_pcm_hw_params *params,
1749 return changed; 1739 return changed;
1750} 1740}
1751 1741
1752#define MAX_MASK 64 1742static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1753 1743 struct snd_pcm_hw_rule *rule)
1754/*
1755 * check whether the registered audio formats need special hw-constraints
1756 */
1757static int check_hw_params_convention(struct snd_usb_substream *subs)
1758{ 1744{
1759 int i; 1745 struct snd_usb_substream *subs = rule->private;
1760 u32 *channels; 1746 struct audioformat *fp;
1761 u32 *rates; 1747 struct snd_interval *it;
1762 u32 cmaster, rmaster; 1748 unsigned char min_datainterval;
1763 u32 rate_min = 0, rate_max = 0; 1749 unsigned int pmin;
1764 struct list_head *p; 1750 int changed;
1765 int err = 1;
1766
1767 channels = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1768 rates = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
1769 if (!channels || !rates) {
1770 err = -ENOMEM;
1771 goto __out;
1772 }
1773 1751
1774 list_for_each(p, &subs->fmt_list) { 1752 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1775 struct audioformat *f; 1753 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1776 f = list_entry(p, struct audioformat, list); 1754 min_datainterval = 0xff;
1777 /* unconventional channels? */ 1755 list_for_each_entry(fp, &subs->fmt_list, list) {
1778 if (f->channels > 32) 1756 if (!hw_check_valid_format(subs, params, fp))
1779 goto __out;
1780 /* continuous rate min/max matches? */
1781 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1782 if (rate_min && f->rate_min != rate_min)
1783 goto __out;
1784 if (rate_max && f->rate_max != rate_max)
1785 goto __out;
1786 rate_min = f->rate_min;
1787 rate_max = f->rate_max;
1788 }
1789 /* combination of continuous rates and fixed rates? */
1790 if (rates[f->format] & SNDRV_PCM_RATE_CONTINUOUS) {
1791 if (f->rates != rates[f->format])
1792 goto __out;
1793 }
1794 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
1795 if (rates[f->format] && rates[f->format] != f->rates)
1796 goto __out;
1797 }
1798 channels[f->format] |= (1 << f->channels);
1799 rates[f->format] |= f->rates;
1800 /* needs knot? */
1801 if (f->rates & SNDRV_PCM_RATE_KNOT)
1802 goto __out;
1803 }
1804 /* check whether channels and rates match for all formats */
1805 cmaster = rmaster = 0;
1806 for (i = 0; i < MAX_MASK; i++) {
1807 if (cmaster != channels[i] && cmaster && channels[i])
1808 goto __out;
1809 if (rmaster != rates[i] && rmaster && rates[i])
1810 goto __out;
1811 if (channels[i])
1812 cmaster = channels[i];
1813 if (rates[i])
1814 rmaster = rates[i];
1815 }
1816 /* check whether channels match for all distinct rates */
1817 memset(channels, 0, MAX_MASK * sizeof(u32));
1818 list_for_each(p, &subs->fmt_list) {
1819 struct audioformat *f;
1820 f = list_entry(p, struct audioformat, list);
1821 if (f->rates & SNDRV_PCM_RATE_CONTINUOUS)
1822 continue; 1757 continue;
1823 for (i = 0; i < 32; i++) { 1758 min_datainterval = min(min_datainterval, fp->datainterval);
1824 if (f->rates & (1 << i)) 1759 }
1825 channels[i] |= (1 << f->channels); 1760 if (min_datainterval == 0xff) {
1826 } 1761 hwc_debug(" --> get emtpy\n");
1762 it->empty = 1;
1763 return -EINVAL;
1827 } 1764 }
1828 cmaster = 0; 1765 pmin = 125 * (1 << min_datainterval);
1829 for (i = 0; i < 32; i++) { 1766 changed = 0;
1830 if (cmaster != channels[i] && cmaster && channels[i]) 1767 if (it->min < pmin) {
1831 goto __out; 1768 it->min = pmin;
1832 if (channels[i]) 1769 it->openmin = 0;
1833 cmaster = channels[i]; 1770 changed = 1;
1834 } 1771 }
1835 err = 0; 1772 if (snd_interval_checkempty(it)) {
1836 1773 it->empty = 1;
1837 __out: 1774 return -EINVAL;
1838 kfree(channels); 1775 }
1839 kfree(rates); 1776 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1840 return err; 1777 return changed;
1841} 1778}
1842 1779
1843/* 1780/*
@@ -1885,6 +1822,8 @@ static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1885static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs) 1822static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1886{ 1823{
1887 struct list_head *p; 1824 struct list_head *p;
1825 unsigned int pt, ptmin;
1826 int param_period_time_if_needed;
1888 int err; 1827 int err;
1889 1828
1890 runtime->hw.formats = subs->formats; 1829 runtime->hw.formats = subs->formats;
@@ -1894,6 +1833,7 @@ static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substre
1894 runtime->hw.channels_min = 256; 1833 runtime->hw.channels_min = 256;
1895 runtime->hw.channels_max = 0; 1834 runtime->hw.channels_max = 0;
1896 runtime->hw.rates = 0; 1835 runtime->hw.rates = 0;
1836 ptmin = UINT_MAX;
1897 /* check min/max rates and channels */ 1837 /* check min/max rates and channels */
1898 list_for_each(p, &subs->fmt_list) { 1838 list_for_each(p, &subs->fmt_list) {
1899 struct audioformat *fp; 1839 struct audioformat *fp;
@@ -1912,42 +1852,54 @@ static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substre
1912 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max = 1852 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1913 fp->frame_size; 1853 fp->frame_size;
1914 } 1854 }
1855 pt = 125 * (1 << fp->datainterval);
1856 ptmin = min(ptmin, pt);
1915 } 1857 }
1916 1858
1917 /* set the period time minimum 1ms */ 1859 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1918 /* FIXME: high-speed mode allows 125us minimum period, but many parts 1860 if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
1919 * in the current code assume the 1ms period. 1861 /* full speed devices have fixed data packet interval */
1920 */ 1862 ptmin = 1000;
1863 if (ptmin == 1000)
1864 /* if period time doesn't go below 1 ms, no rules needed */
1865 param_period_time_if_needed = -1;
1921 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1866 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1922 1000 * MIN_PACKS_URB, 1867 ptmin, UINT_MAX);
1923 /*(nrpacks * MAX_URBS) * 1000*/ UINT_MAX); 1868
1924 1869 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1925 err = check_hw_params_convention(subs); 1870 hw_rule_rate, subs,
1926 if (err < 0) 1871 SNDRV_PCM_HW_PARAM_FORMAT,
1872 SNDRV_PCM_HW_PARAM_CHANNELS,
1873 param_period_time_if_needed,
1874 -1)) < 0)
1927 return err; 1875 return err;
1928 else if (err) { 1876 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1929 hwc_debug("setting extra hw constraints...\n"); 1877 hw_rule_channels, subs,
1930 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, 1878 SNDRV_PCM_HW_PARAM_FORMAT,
1931 hw_rule_rate, subs, 1879 SNDRV_PCM_HW_PARAM_RATE,
1932 SNDRV_PCM_HW_PARAM_FORMAT, 1880 param_period_time_if_needed,
1933 SNDRV_PCM_HW_PARAM_CHANNELS, 1881 -1)) < 0)
1934 -1)) < 0) 1882 return err;
1935 return err; 1883 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1936 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 1884 hw_rule_format, subs,
1937 hw_rule_channels, subs, 1885 SNDRV_PCM_HW_PARAM_RATE,
1938 SNDRV_PCM_HW_PARAM_FORMAT, 1886 SNDRV_PCM_HW_PARAM_CHANNELS,
1939 SNDRV_PCM_HW_PARAM_RATE, 1887 param_period_time_if_needed,
1940 -1)) < 0) 1888 -1)) < 0)
1941 return err; 1889 return err;
1942 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, 1890 if (param_period_time_if_needed >= 0) {
1943 hw_rule_format, subs, 1891 err = snd_pcm_hw_rule_add(runtime, 0,
1944 SNDRV_PCM_HW_PARAM_RATE, 1892 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1945 SNDRV_PCM_HW_PARAM_CHANNELS, 1893 hw_rule_period_time, subs,
1946 -1)) < 0) 1894 SNDRV_PCM_HW_PARAM_FORMAT,
1947 return err; 1895 SNDRV_PCM_HW_PARAM_CHANNELS,
1948 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0) 1896 SNDRV_PCM_HW_PARAM_RATE,
1897 -1);
1898 if (err < 0)
1949 return err; 1899 return err;
1950 } 1900 }
1901 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
1902 return err;
1951 return 0; 1903 return 0;
1952} 1904}
1953 1905
@@ -2160,7 +2112,8 @@ static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct s
2160 fp = list_entry(p, struct audioformat, list); 2112 fp = list_entry(p, struct audioformat, list);
2161 snd_iprintf(buffer, " Interface %d\n", fp->iface); 2113 snd_iprintf(buffer, " Interface %d\n", fp->iface);
2162 snd_iprintf(buffer, " Altset %d\n", fp->altsetting); 2114 snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
2163 snd_iprintf(buffer, " Format: 0x%x\n", fp->format); 2115 snd_iprintf(buffer, " Format: %#x (%d bits)\n",
2116 fp->format, snd_pcm_format_width(fp->format));
2164 snd_iprintf(buffer, " Channels: %d\n", fp->channels); 2117 snd_iprintf(buffer, " Channels: %d\n", fp->channels);
2165 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n", 2118 snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
2166 fp->endpoint & USB_ENDPOINT_NUMBER_MASK, 2119 fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
@@ -2179,8 +2132,11 @@ static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct s
2179 } 2132 }
2180 snd_iprintf(buffer, "\n"); 2133 snd_iprintf(buffer, "\n");
2181 } 2134 }
2135 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
2136 snd_iprintf(buffer, " Data packet interval: %d us\n",
2137 125 * (1 << fp->datainterval));
2182 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize); 2138 // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
2183 // snd_iprintf(buffer, " EP Attribute = 0x%x\n", fp->attributes); 2139 // snd_iprintf(buffer, " EP Attribute = %#x\n", fp->attributes);
2184 } 2140 }
2185} 2141}
2186 2142
@@ -2524,7 +2480,6 @@ static int parse_audio_format_rates(struct snd_usb_audio *chip, struct audioform
2524 * build the rate table and bitmap flags 2480 * build the rate table and bitmap flags
2525 */ 2481 */
2526 int r, idx; 2482 int r, idx;
2527 unsigned int nonzero_rates = 0;
2528 2483
2529 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL); 2484 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
2530 if (fp->rate_table == NULL) { 2485 if (fp->rate_table == NULL) {
@@ -2532,24 +2487,27 @@ static int parse_audio_format_rates(struct snd_usb_audio *chip, struct audioform
2532 return -1; 2487 return -1;
2533 } 2488 }
2534 2489
2535 fp->nr_rates = nr_rates; 2490 fp->nr_rates = 0;
2536 fp->rate_min = fp->rate_max = combine_triple(&fmt[8]); 2491 fp->rate_min = fp->rate_max = 0;
2537 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) { 2492 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
2538 unsigned int rate = combine_triple(&fmt[idx]); 2493 unsigned int rate = combine_triple(&fmt[idx]);
2494 if (!rate)
2495 continue;
2539 /* C-Media CM6501 mislabels its 96 kHz altsetting */ 2496 /* C-Media CM6501 mislabels its 96 kHz altsetting */
2540 if (rate == 48000 && nr_rates == 1 && 2497 if (rate == 48000 && nr_rates == 1 &&
2541 chip->usb_id == USB_ID(0x0d8c, 0x0201) && 2498 (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
2499 chip->usb_id == USB_ID(0x0d8c, 0x0102)) &&
2542 fp->altsetting == 5 && fp->maxpacksize == 392) 2500 fp->altsetting == 5 && fp->maxpacksize == 392)
2543 rate = 96000; 2501 rate = 96000;
2544 fp->rate_table[r] = rate; 2502 fp->rate_table[fp->nr_rates] = rate;
2545 nonzero_rates |= rate; 2503 if (!fp->rate_min || rate < fp->rate_min)
2546 if (rate < fp->rate_min)
2547 fp->rate_min = rate; 2504 fp->rate_min = rate;
2548 else if (rate > fp->rate_max) 2505 if (!fp->rate_max || rate > fp->rate_max)
2549 fp->rate_max = rate; 2506 fp->rate_max = rate;
2550 fp->rates |= snd_pcm_rate_to_rate_bit(rate); 2507 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
2508 fp->nr_rates++;
2551 } 2509 }
2552 if (!nonzero_rates) { 2510 if (!fp->nr_rates) {
2553 hwc_debug("All rates were zero. Skipping format!\n"); 2511 hwc_debug("All rates were zero. Skipping format!\n");
2554 return -1; 2512 return -1;
2555 } 2513 }
@@ -2619,7 +2577,7 @@ static int parse_audio_format_ii(struct snd_usb_audio *chip, struct audioformat
2619 fp->format = SNDRV_PCM_FORMAT_MPEG; 2577 fp->format = SNDRV_PCM_FORMAT_MPEG;
2620 break; 2578 break;
2621 default: 2579 default:
2622 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag 0x%x is detected. processed as MPEG.\n", 2580 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
2623 chip->dev->devnum, fp->iface, fp->altsetting, format); 2581 chip->dev->devnum, fp->iface, fp->altsetting, format);
2624 fp->format = SNDRV_PCM_FORMAT_MPEG; 2582 fp->format = SNDRV_PCM_FORMAT_MPEG;
2625 break; 2583 break;
@@ -2670,6 +2628,17 @@ static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp
2670 return 0; 2628 return 0;
2671} 2629}
2672 2630
2631static unsigned char parse_datainterval(struct snd_usb_audio *chip,
2632 struct usb_host_interface *alts)
2633{
2634 if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH &&
2635 get_endpoint(alts, 0)->bInterval >= 1 &&
2636 get_endpoint(alts, 0)->bInterval <= 4)
2637 return get_endpoint(alts, 0)->bInterval - 1;
2638 else
2639 return 0;
2640}
2641
2673static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip, 2642static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
2674 int iface, int altno); 2643 int iface, int altno);
2675static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no) 2644static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
@@ -2775,6 +2744,7 @@ static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
2775 fp->altset_idx = i; 2744 fp->altset_idx = i;
2776 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; 2745 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
2777 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; 2746 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
2747 fp->datainterval = parse_datainterval(chip, alts);
2778 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); 2748 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2779 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH) 2749 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
2780 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1) 2750 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
@@ -2817,7 +2787,7 @@ static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
2817 continue; 2787 continue;
2818 } 2788 }
2819 2789
2820 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint 0x%x\n", dev->devnum, iface_no, altno, fp->endpoint); 2790 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
2821 err = add_audio_endpoint(chip, stream, fp); 2791 err = add_audio_endpoint(chip, stream, fp);
2822 if (err < 0) { 2792 if (err < 0) {
2823 kfree(fp->rate_table); 2793 kfree(fp->rate_table);
@@ -2966,6 +2936,8 @@ static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
2966 return -EINVAL; 2936 return -EINVAL;
2967 } 2937 }
2968 alts = &iface->altsetting[fp->altset_idx]; 2938 alts = &iface->altsetting[fp->altset_idx];
2939 fp->datainterval = parse_datainterval(chip, alts);
2940 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
2969 usb_set_interface(chip->dev, fp->iface, 0); 2941 usb_set_interface(chip->dev, fp->iface, 0);
2970 init_usb_pitch(chip->dev, fp->iface, alts, fp); 2942 init_usb_pitch(chip->dev, fp->iface, alts, fp);
2971 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max); 2943 init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
@@ -3059,6 +3031,7 @@ static int create_uaxx_quirk(struct snd_usb_audio *chip,
3059 fp->iface = altsd->bInterfaceNumber; 3031 fp->iface = altsd->bInterfaceNumber;
3060 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; 3032 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
3061 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; 3033 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
3034 fp->datainterval = 0;
3062 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); 3035 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3063 3036
3064 switch (fp->maxpacksize) { 3037 switch (fp->maxpacksize) {
@@ -3126,6 +3099,7 @@ static int create_ua1000_quirk(struct snd_usb_audio *chip,
3126 fp->iface = altsd->bInterfaceNumber; 3099 fp->iface = altsd->bInterfaceNumber;
3127 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; 3100 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
3128 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; 3101 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
3102 fp->datainterval = parse_datainterval(chip, alts);
3129 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); 3103 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3130 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]); 3104 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]);
3131 3105
@@ -3178,6 +3152,7 @@ static int create_ua101_quirk(struct snd_usb_audio *chip,
3178 fp->iface = altsd->bInterfaceNumber; 3152 fp->iface = altsd->bInterfaceNumber;
3179 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; 3153 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
3180 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; 3154 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
3155 fp->datainterval = parse_datainterval(chip, alts);
3181 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); 3156 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
3182 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[15]); 3157 fp->rate_max = fp->rate_min = combine_triple(&alts->extra[15]);
3183 3158
@@ -3763,7 +3738,7 @@ static int usb_audio_resume(struct usb_interface *intf)
3763 3738
3764static int __init snd_usb_audio_init(void) 3739static int __init snd_usb_audio_init(void)
3765{ 3740{
3766 if (nrpacks < MIN_PACKS_URB || nrpacks > MAX_PACKS) { 3741 if (nrpacks < 1 || nrpacks > MAX_PACKS) {
3767 printk(KERN_WARNING "invalid nrpacks value.\n"); 3742 printk(KERN_WARNING "invalid nrpacks value.\n");
3768 return -EINVAL; 3743 return -EINVAL;
3769 } 3744 }