/[openvortex]/alsa/alsa-kernel/usb/usbaudio.c
ViewVC logotype

Diff of /alsa/alsa-kernel/usb/usbaudio.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by vizard, Sun Jun 15 21:01:32 2003 UTC revision 1.4 by vizard, Mon Aug 11 00:06:40 2003 UTC
# Line 55  static char *id[SNDRV_CARDS] = SNDRV_DEF Line 55  static char *id[SNDRV_CARDS] = SNDRV_DEF
55  static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */  static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
56  static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Vendor ID for this card */  static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Vendor ID for this card */
57  static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Product ID for this card */  static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Product ID for this card */
58    static int nrpacks = 4;         /* max. number of packets per urb */
59    
60  MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");  MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
61  MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");  MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
# Line 71  MODULE_PARM_SYNTAX(vid, SNDRV_ENABLED ", Line 72  MODULE_PARM_SYNTAX(vid, SNDRV_ENABLED ",
72  MODULE_PARM(pid, "1-" __MODULE_STRING(SNDRV_CARDS) "i");  MODULE_PARM(pid, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
73  MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");  MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
74  MODULE_PARM_SYNTAX(pid, SNDRV_ENABLED ",allows:{{-1,0xffff}},base:16");  MODULE_PARM_SYNTAX(pid, SNDRV_ENABLED ",allows:{{-1,0xffff}},base:16");
75    MODULE_PARM(nrpacks, "i");
76    MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
77    MODULE_PARM_SYNTAX(nrpacks, SNDRV_ENABLED ",allows:{{2,10}}");
78    
79    
80  /*  /*
# Line 98  MODULE_PARM_SYNTAX(pid, SNDRV_ENABLED ", Line 102  MODULE_PARM_SYNTAX(pid, SNDRV_ENABLED ",
102   *   *
103   */   */
104    
105  #define NRPACKS         4       /* 4ms per urb */  #define MAX_PACKS       10      
106  #define MAX_URBS        5       /* max. 20ms long packets */  #define MAX_URBS        5       /* max. 20ms long packets */
107  #define SYNC_URBS       2       /* always two urbs for sync */  #define SYNC_URBS       2       /* always two urbs for sync */
108  #define MIN_PACKS_URB   1       /* minimum 1 packet per urb */  #define MIN_PACKS_URB   1       /* minimum 1 packet per urb */
# Line 149  struct snd_usb_substream { Line 153  struct snd_usb_substream {
153          int direction;  /* playback or capture */          int direction;  /* playback or capture */
154          int interface;  /* current interface */          int interface;  /* current interface */
155          int endpoint;   /* assigned endpoint */          int endpoint;   /* assigned endpoint */
156            struct audioformat *cur_audiofmt;       /* current audioformat pointer (for hw_params callback) */
157            unsigned int cur_rate;          /* current rate (for hw_params callback) */
158            unsigned int period_bytes;      /* current period bytes (for hw_params callback) */
159          unsigned int format;     /* USB data format */          unsigned int format;     /* USB data format */
160          unsigned int datapipe;   /* the data i/o pipe */          unsigned int datapipe;   /* the data i/o pipe */
161          unsigned int syncpipe;   /* 1 - async out or adaptive in */          unsigned int syncpipe;   /* 1 - async out or adaptive in */
# Line 176  struct snd_usb_substream { Line 183  struct snd_usb_substream {
183          unsigned int nurbs;                     /* # urbs */          unsigned int nurbs;                     /* # urbs */
184          snd_urb_ctx_t dataurb[MAX_URBS];        /* data urb table */          snd_urb_ctx_t dataurb[MAX_URBS];        /* data urb table */
185          snd_urb_ctx_t syncurb[SYNC_URBS];       /* sync urb table */          snd_urb_ctx_t syncurb[SYNC_URBS];       /* sync urb table */
186          char syncbuf[SYNC_URBS * NRPACKS * 3]; /* sync buffer; it's so small - let's get static */          char syncbuf[SYNC_URBS * MAX_PACKS * 3]; /* sync buffer; it's so small - let's get static */
187          char *tmpbuf;                   /* temporary buffer for playback */          char *tmpbuf;                   /* temporary buffer for playback */
188    
189          u64 formats;                    /* format bitmasks (all or'ed) */          u64 formats;                    /* format bitmasks (all or'ed) */
# Line 381  static int prepare_playback_sync_urb(snd Line 388  static int prepare_playback_sync_urb(snd
388                  urb->iso_frame_desc[i].length = 3;                  urb->iso_frame_desc[i].length = 3;
389                  urb->iso_frame_desc[i].offset = offs;                  urb->iso_frame_desc[i].offset = offs;
390          }          }
391            urb->interval = 1;
392          return 0;          return 0;
393  }  }
394    
# Line 505  static int prepare_playback_urb(snd_usb_ Line 513  static int prepare_playback_urb(snd_usb_
513          spin_unlock_irqrestore(&subs->lock, flags);          spin_unlock_irqrestore(&subs->lock, flags);
514          urb->transfer_buffer_length = offs * stride;          urb->transfer_buffer_length = offs * stride;
515          ctx->transfer = offs;          ctx->transfer = offs;
516            urb->interval = 1;
517    
518          return 0;          return 0;
519  }  }
# Line 804  static void release_substream_urbs(snd_u Line 813  static void release_substream_urbs(snd_u
813  /*  /*
814   * initialize a substream for plaback/capture   * initialize a substream for plaback/capture
815   */   */
816  static int init_substream_urbs(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime)  static int init_substream_urbs(snd_usb_substream_t *subs, unsigned int period_bytes,
817                                   unsigned int rate, unsigned int frame_bits)
818  {  {
819          unsigned int maxsize, n, i;          unsigned int maxsize, n, i;
820          int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;          int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
821          unsigned int npacks[MAX_URBS], total_packs;          unsigned int npacks[MAX_URBS], total_packs;
822    
823          /* calculate the frequency in 10.14 format */          /* calculate the frequency in 10.14 format */
824          subs->freqn = subs->freqm = get_usb_rate(runtime->rate);          subs->freqn = subs->freqm = get_usb_rate(rate);
825          subs->freqmax = subs->freqn + (subs->freqn >> 2); /* max. allowed frequency */          subs->freqmax = subs->freqn + (subs->freqn >> 2); /* max. allowed frequency */
826          subs->phase = 0;          subs->phase = 0;
827    
# Line 824  static int init_substream_urbs(snd_usb_s Line 834  static int init_substream_urbs(snd_usb_s
834          subs->unlink_mask = 0;          subs->unlink_mask = 0;
835    
836          /* calculate the max. size of packet */          /* calculate the max. size of packet */
837          maxsize = ((subs->freqmax + 0x3fff) * (runtime->frame_bits >> 3)) >> 14;          maxsize = ((subs->freqmax + 0x3fff) * (frame_bits >> 3)) >> 14;
838          if (subs->maxpacksize && maxsize > subs->maxpacksize) {          if (subs->maxpacksize && maxsize > subs->maxpacksize) {
839                  //snd_printd(KERN_DEBUG "maxsize %d is greater than defined size %d\n",                  //snd_printd(KERN_DEBUG "maxsize %d is greater than defined size %d\n",
840                  //         maxsize, subs->maxpacksize);                  //         maxsize, subs->maxpacksize);
# Line 835  static int init_substream_urbs(snd_usb_s Line 845  static int init_substream_urbs(snd_usb_s
845                  subs->curpacksize = subs->maxpacksize;                  subs->curpacksize = subs->maxpacksize;
846          else          else
847                  subs->curpacksize = maxsize;                  subs->curpacksize = maxsize;
         subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);  
848    
849          /* allocate a temporary buffer for playback */          /* allocate a temporary buffer for playback */
850          if (is_playback) {          if (is_playback) {
851                  subs->tmpbuf = kmalloc(maxsize * NRPACKS, GFP_KERNEL);                  subs->tmpbuf = kmalloc(maxsize * nrpacks, GFP_KERNEL);
852                  if (! subs->tmpbuf) {                  if (! subs->tmpbuf) {
853                          snd_printk(KERN_ERR "cannot malloc tmpbuf\n");                          snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
854                          return -ENOMEM;                          return -ENOMEM;
# Line 847  static int init_substream_urbs(snd_usb_s Line 856  static int init_substream_urbs(snd_usb_s
856          }          }
857    
858          /* decide how many packets to be used */          /* decide how many packets to be used */
859          total_packs = (frames_to_bytes(runtime, runtime->period_size) + maxsize - 1) / maxsize;          total_packs = (period_bytes + maxsize - 1) / maxsize;
860          if (total_packs < 2 * MIN_PACKS_URB)          if (total_packs < 2 * MIN_PACKS_URB)
861                  total_packs = 2 * MIN_PACKS_URB;                  total_packs = 2 * MIN_PACKS_URB;
862          subs->nurbs = (total_packs + NRPACKS - 1) / NRPACKS;          subs->nurbs = (total_packs + nrpacks - 1) / nrpacks;
863          if (subs->nurbs > MAX_URBS) {          if (subs->nurbs > MAX_URBS) {
864                  /* too much... */                  /* too much... */
865                  subs->nurbs = MAX_URBS;                  subs->nurbs = MAX_URBS;
866                  total_packs = MAX_URBS * NRPACKS;                  total_packs = MAX_URBS * nrpacks;
867          }          }
868          n = total_packs;          n = total_packs;
869          for (i = 0; i < subs->nurbs; i++) {          for (i = 0; i < subs->nurbs; i++) {
870                  npacks[i] = n > NRPACKS ? NRPACKS : n;                  npacks[i] = n > nrpacks ? nrpacks : n;
871                  n -= NRPACKS;                  n -= nrpacks;
872          }          }
873          if (subs->nurbs <= 1) {          if (subs->nurbs <= 1) {
874                  /* too little - we need at least two packets                  /* too little - we need at least two packets
# Line 918  static int init_substream_urbs(snd_usb_s Line 927  static int init_substream_urbs(snd_usb_s
927                          snd_urb_ctx_t *u = &subs->syncurb[i];                          snd_urb_ctx_t *u = &subs->syncurb[i];
928                          u->index = i;                          u->index = i;
929                          u->subs = subs;                          u->subs = subs;
930                          u->packets = NRPACKS;                          u->packets = nrpacks;
931                          u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);                          u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
932                          if (! u->urb) {                          if (! u->urb) {
933                                  release_substream_urbs(subs, 0);                                  release_substream_urbs(subs, 0);
934                                  return -ENOMEM;                                  return -ENOMEM;
935                          }                          }
936                          u->urb->transfer_buffer = subs->syncbuf + i * NRPACKS * 3;                          u->urb->transfer_buffer = subs->syncbuf + i * nrpacks * 3;
937                          u->urb->transfer_buffer_length = NRPACKS * 3;                          u->urb->transfer_buffer_length = nrpacks * 3;
938                          u->urb->dev = subs->dev;                          u->urb->dev = subs->dev;
939                          u->urb->pipe = subs->syncpipe;                          u->urb->pipe = subs->syncpipe;
940                          u->urb->transfer_flags = URB_ISO_ASAP | UNLINK_FLAGS;                          u->urb->transfer_flags = URB_ISO_ASAP | UNLINK_FLAGS;
# Line 941  static int init_substream_urbs(snd_usb_s Line 950  static int init_substream_urbs(snd_usb_s
950  /*  /*
951   * find a matching audio format   * find a matching audio format
952   */   */
953  static struct audioformat *find_format(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime)  static struct audioformat *find_format(snd_usb_substream_t *subs, unsigned int format,
954                                           unsigned int rate, unsigned int channels)
955  {  {
956          struct list_head *p;          struct list_head *p;
957          struct audioformat *found = NULL;          struct audioformat *found = NULL;
# Line 949  static struct audioformat *find_format(s Line 959  static struct audioformat *find_format(s
959          list_for_each(p, &subs->fmt_list) {          list_for_each(p, &subs->fmt_list) {
960                  struct audioformat *fp;                  struct audioformat *fp;
961                  fp = list_entry(p, struct audioformat, list);                  fp = list_entry(p, struct audioformat, list);
962                  if (fp->format != runtime->format ||                  if (fp->format != format || fp->channels != channels)
                     fp->channels != runtime->channels)  
963                          continue;                          continue;
964                  if (runtime->rate < fp->rate_min || runtime->rate > fp->rate_max)                  if (rate < fp->rate_min || rate > fp->rate_max)
965                          continue;                          continue;
966                  if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {                  if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
                         if (! found || fp->maxpacksize > found->maxpacksize)  
                                 found = fp;  
                 } else {  
967                          unsigned int i;                          unsigned int i;
968                          for (i = 0; i < fp->nr_rates; i++)                          for (i = 0; i < fp->nr_rates; i++)
969                                  if (fp->rate_table[i] == runtime->rate) {                                  if (fp->rate_table[i] == rate)
                                         if (! found || fp->maxpacksize > found->maxpacksize)  
                                                 found = fp;  
970                                          break;                                          break;
971                                  }                          if (i >= fp->nr_rates)
972                                    continue;
973                  }                  }
974                    /* find the format with the largest max. packet size */
975                    if (! found || fp->maxpacksize > found->maxpacksize)
976                            found = fp;
977          }          }
978          return found;          return found;
979  }  }
# Line 1038  static int init_usb_sample_rate(struct u Line 1046  static int init_usb_sample_rate(struct u
1046  /*  /*
1047   * find a matching format and set up the interface   * find a matching format and set up the interface
1048   */   */
1049  static int set_format(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime)  static int set_format(snd_usb_substream_t *subs, struct audioformat *fmt)
1050  {  {
1051          struct usb_device *dev = subs->dev;          struct usb_device *dev = subs->dev;
1052          struct usb_host_config *config = dev->actconfig;          struct usb_host_config *config = dev->actconfig;
1053          struct usb_host_interface *alts;          struct usb_host_interface *alts;
1054          struct usb_interface_descriptor *altsd;          struct usb_interface_descriptor *altsd;
1055          struct usb_interface *iface;          struct usb_interface *iface;
         struct audioformat *fmt;  
1056          unsigned int ep, attr;          unsigned int ep, attr;
1057          int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;          int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
1058          int err;          int err;
1059    
         fmt = find_format(subs, runtime);  
         if (! fmt) {  
                 snd_printd(KERN_DEBUG "cannot set format: format = %s, rate = %d, channels = %d\n",  
                            snd_pcm_format_name(runtime->format), runtime->rate, runtime->channels);  
                 return -EINVAL;  
         }  
   
1060          iface = &config->interface[fmt->iface];          iface = &config->interface[fmt->iface];
1061          alts = &iface->altsetting[fmt->altset_idx];          alts = &iface->altsetting[fmt->altset_idx];
1062          altsd = get_iface_desc(alts);          altsd = get_iface_desc(alts);
1063          snd_assert(altsd->bAlternateSetting == fmt->altsetting, return -EINVAL);          snd_assert(altsd->bAlternateSetting == fmt->altsetting, return -EINVAL);
1064    
1065            if (fmt == subs->cur_audiofmt)
1066                    return 0;
1067    
1068          /* close the old interface */          /* close the old interface */
1069          if (subs->interface >= 0 && subs->interface != fmt->iface) {          if (subs->interface >= 0 && subs->interface != fmt->iface) {
1070                  usb_set_interface(subs->dev, subs->interface, 0);                  usb_set_interface(subs->dev, subs->interface, 0);
# Line 1089  static int set_format(snd_usb_substream_ Line 1092  static int set_format(snd_usb_substream_
1092                  subs->datapipe = usb_rcvisocpipe(dev, ep);                  subs->datapipe = usb_rcvisocpipe(dev, ep);
1093          subs->syncpipe = subs->syncinterval = 0;          subs->syncpipe = subs->syncinterval = 0;
1094          subs->maxpacksize = get_endpoint(alts, 0)->wMaxPacketSize;          subs->maxpacksize = get_endpoint(alts, 0)->wMaxPacketSize;
         subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);  
1095          subs->fill_max = 0;          subs->fill_max = 0;
1096    
1097          /* we need a sync pipe in async OUT or adaptive IN mode */          /* we need a sync pipe in async OUT or adaptive IN mode */
1098          attr = fmt->ep_attr & EP_ATTR_MASK;          attr = fmt->ep_attr & EP_ATTR_MASK;
1099          if ((is_playback && attr == EP_ATTR_ASYNC) ||          if ((is_playback && attr == EP_ATTR_ASYNC) ||
1100              (! is_playback && attr == EP_ATTR_ADAPTIVE)) {              (! is_playback && attr == EP_ATTR_ADAPTIVE)) {
                 /*  
                  * QUIRK: plantronics headset has adaptive-in  
                  * although it's really not...  
                  */  
                 if ((dev->descriptor.idVendor == 0x047f &&  
                      dev->descriptor.idProduct == 0x0ca1) ||  
                     /* Griffin iMic (note that there is an older model 77d:223) */  
                     (dev->descriptor.idVendor == 0x077d &&  
                      dev->descriptor.idProduct == 0x07af))  
                         goto _ok;  
1101                  /* check endpoint */                  /* check endpoint */
1102                  if (altsd->bNumEndpoints < 2 ||                  if (altsd->bNumEndpoints < 2 ||
1103                      get_endpoint(alts, 1)->bmAttributes != 0x01 ||                      get_endpoint(alts, 1)->bmAttributes != 0x01 ||
# Line 1129  static int set_format(snd_usb_substream_ Line 1121  static int set_format(snd_usb_substream_
1121                  subs->syncinterval = get_endpoint(alts, 1)->bRefresh;                  subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
1122          }          }
1123    
  _ok:  
         if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0 ||  
             (err = init_usb_sample_rate(dev, subs->interface, alts, fmt,  
                                         runtime->rate)) < 0)  
                 return err;  
   
1124          /* always fill max packet size */          /* always fill max packet size */
1125          if (fmt->attributes & EP_CS_ATTR_FILL_MAX)          if (fmt->attributes & EP_CS_ATTR_FILL_MAX)
1126                  subs->fill_max = 1;                  subs->fill_max = 1;
1127    
1128            if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
1129                    return err;
1130    
1131            subs->cur_audiofmt = fmt;
1132    
1133  #if 0  #if 0
1134          printk("setting done: format = %d, rate = %d, channels = %d\n",          printk("setting done: format = %d, rate = %d, channels = %d\n",
1135                 runtime->format, runtime->rate, runtime->channels);                 fmt->format, fmt->rate, fmt->channels);
1136          printk("  datapipe = 0x%0x, syncpipe = 0x%0x\n",          printk("  datapipe = 0x%0x, syncpipe = 0x%0x\n",
1137                 subs->datapipe, subs->syncpipe);                 subs->datapipe, subs->syncpipe);
1138  #endif  #endif
# Line 1150  static int set_format(snd_usb_substream_ Line 1141  static int set_format(snd_usb_substream_
1141  }  }
1142    
1143  /*  /*
1144   * allocate a buffer.   * hw_params callback
1145     *
1146     * allocate a buffer and set the given audio format.
1147   *   *
1148   * so far we use a physically linear buffer although packetize transfer   * so far we use a physically linear buffer although packetize transfer
1149   * doesn't need a continuous area.   * doesn't need a continuous area.
# Line 1160  static int set_format(snd_usb_substream_ Line 1153  static int set_format(snd_usb_substream_
1153  static int snd_usb_hw_params(snd_pcm_substream_t *substream,  static int snd_usb_hw_params(snd_pcm_substream_t *substream,
1154                               snd_pcm_hw_params_t *hw_params)                               snd_pcm_hw_params_t *hw_params)
1155  {  {
1156          return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));          snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
1157            struct audioformat *fmt;
1158            unsigned int channels, rate, format;
1159            int ret, changed;
1160    
1161            ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1162            if (ret < 0)
1163                    return ret;
1164            
1165            format = params_format(hw_params);
1166            rate = params_rate(hw_params);
1167            channels = params_channels(hw_params);
1168            fmt = find_format(subs, format, rate, channels);
1169            if (! fmt) {
1170                    snd_printd(KERN_DEBUG "cannot set format: format = %s, rate = %d, channels = %d\n",
1171                               snd_pcm_format_name(format), rate, channels);
1172                    return -EINVAL;
1173            }
1174    
1175            changed = subs->cur_audiofmt != fmt ||
1176                    subs->period_bytes != params_period_bytes(hw_params) ||
1177                    subs->cur_rate != rate;
1178            if ((ret = set_format(subs, fmt)) < 0)
1179                    return ret;
1180    
1181            if (subs->cur_rate != rate) {
1182                    struct usb_host_config *config = subs->dev->actconfig;
1183                    struct usb_host_interface *alts;
1184                    struct usb_interface *iface;
1185                    iface = &config->interface[fmt->iface];
1186                    alts = &iface->altsetting[fmt->altset_idx];
1187                    ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
1188                    if (ret < 0)
1189                            return ret;
1190                    subs->cur_rate = rate;
1191            }
1192    
1193            if (changed) {
1194                    /* format changed */
1195                    release_substream_urbs(subs, 0);
1196                    /* influenced: period_bytes, channels, rate, format, */
1197                    ret = init_substream_urbs(subs, params_period_bytes(hw_params),
1198                                              params_rate(hw_params),
1199                                              snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
1200            }
1201    
1202            return ret;
1203  }  }
1204    
1205  /*  /*
1206   * free the buffer   * hw_free callback
1207     *
1208     * reset the audio format and release the buffer
1209   */   */
1210  static int snd_usb_hw_free(snd_pcm_substream_t *substream)  static int snd_usb_hw_free(snd_pcm_substream_t *substream)
1211  {  {
1212            snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
1213    
1214            subs->cur_audiofmt = NULL;
1215            subs->cur_rate = 0;
1216            subs->period_bytes = 0;
1217            release_substream_urbs(subs, 0);
1218          return snd_pcm_lib_free_pages(substream);          return snd_pcm_lib_free_pages(substream);
1219  }  }
1220    
1221  /*  /*
1222   * prepare callback   * prepare callback
1223   *   *
1224   * set format and initialize urbs   * only a few subtle things...
1225   */   */
1226  static int snd_usb_pcm_prepare(snd_pcm_substream_t *substream)  static int snd_usb_pcm_prepare(snd_pcm_substream_t *substream)
1227  {  {
1228          snd_pcm_runtime_t *runtime = substream->runtime;          snd_pcm_runtime_t *runtime = substream->runtime;
1229          snd_usb_substream_t *subs = (snd_usb_substream_t *)runtime->private_data;          snd_usb_substream_t *subs = (snd_usb_substream_t *)runtime->private_data;
         int err;  
1230    
1231          release_substream_urbs(subs, 0);          if (! subs->cur_audiofmt) {
1232          if ((err = set_format(subs, runtime)) < 0)                  snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
1233                  return err;                  return -ENXIO;
1234            }
1235    
1236            /* some unit conversions in runtime */
1237            subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
1238            subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
1239    
1240          return init_substream_urbs(subs, runtime);          return 0;
1241  }  }
1242    
1243  static snd_pcm_hardware_t snd_usb_playback =  static snd_pcm_hardware_t snd_usb_playback =
# Line 1497  static int setup_hw_info(snd_pcm_runtime Line 1548  static int setup_hw_info(snd_pcm_runtime
1548          /* set the period time minimum 1ms */          /* set the period time minimum 1ms */
1549          snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,          snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1550                                       1000 * MIN_PACKS_URB,                                       1000 * MIN_PACKS_URB,
1551                                       /*(NRPACKS * MAX_URBS) * 1000*/ UINT_MAX);                                       /*(nrpacks * MAX_URBS) * 1000*/ UINT_MAX);
1552    
1553          if (check_hw_params_convention(subs)) {          if (check_hw_params_convention(subs)) {
1554                  hwc_debug("setting extra hw constraints...\n");                  hwc_debug("setting extra hw constraints...\n");
# Line 1543  static int snd_usb_pcm_close(snd_pcm_sub Line 1594  static int snd_usb_pcm_close(snd_pcm_sub
1594          snd_usb_stream_t *as = snd_pcm_substream_chip(substream);          snd_usb_stream_t *as = snd_pcm_substream_chip(substream);
1595          snd_usb_substream_t *subs = &as->substream[direction];          snd_usb_substream_t *subs = &as->substream[direction];
1596    
1597          release_substream_urbs(subs, 0);          if (subs->interface >= 0) {
         if (subs->interface >= 0)  
1598                  usb_set_interface(subs->dev, subs->interface, 0);                  usb_set_interface(subs->dev, subs->interface, 0);
1599                    subs->interface = -1;
1600            }
1601          subs->pcm_substream = NULL;          subs->pcm_substream = NULL;
1602          return 0;          return 0;
1603  }  }
# Line 1939  static int parse_audio_format_i_type(str Line 1991  static int parse_audio_format_i_type(str
1991          switch (format) {          switch (format) {
1992          case 0: /* some devices don't define this correctly... */          case 0: /* some devices don't define this correctly... */
1993                  snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",                  snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
1994                              dev->devnum, iface_no, altno);                              dev->devnum, fp->iface, fp->altsetting);
1995                  /* fall-through */                  /* fall-through */
1996          case USB_AUDIO_FORMAT_PCM:          case USB_AUDIO_FORMAT_PCM:
1997                  if (sample_width > sample_bytes * 8) {                  if (sample_width > sample_bytes * 8) {
# Line 2170  static int parse_audio_endpoints(snd_usb Line 2222  static int parse_audio_endpoints(snd_usb
2222                  /* skip invalid one */                  /* skip invalid one */
2223                  if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&                  if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
2224                       altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||                       altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
2225                      altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING ||                      (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING &&
2226                      altsd->bNumEndpoints < 1)                       altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
2227                        altsd->bNumEndpoints < 1 ||
2228                        get_endpoint(alts, 0)->wMaxPacketSize == 0)
2229                          continue;                          continue;
2230                  /* must be isochronous */                  /* must be isochronous */
2231                  if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=                  if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
# Line 2233  static int parse_audio_endpoints(snd_usb Line 2287  static int parse_audio_endpoints(snd_usb
2287                  fp->maxpacksize = get_endpoint(alts, 0)->wMaxPacketSize;                  fp->maxpacksize = get_endpoint(alts, 0)->wMaxPacketSize;
2288                  fp->attributes = csep[3];                  fp->attributes = csep[3];
2289    
2290                    /* some quirks for attributes here */
2291    
2292                    /* workaround for AudioTrak Optoplay */
2293                    if (dev->descriptor.idVendor == 0x0a92 &&
2294                        dev->descriptor.idProduct == 0x0053) {
2295                            /* Optoplay sets the sample rate attribute although
2296                             * it seems not supporting it in fact.
2297                             */
2298                            fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE;
2299                    }
2300    
2301                    /* workaround for M-Audio Audiophile USB */
2302                    if (dev->descriptor.idVendor == 0x0763 &&
2303                        dev->descriptor.idProduct == 0x2003) {
2304                            /* doesn't set the sample rate attribute, but supports it */
2305                            fp->attributes |= EP_CS_ATTR_SAMPLE_RATE;
2306                    }
2307    
2308                    /*
2309                     * plantronics headset and Griffin iMic have set adaptive-in
2310                     * although it's really not...
2311                     */
2312                    if ((dev->descriptor.idVendor == 0x047f &&
2313                         dev->descriptor.idProduct == 0x0ca1) ||
2314                        /* Griffin iMic (note that there is an older model 77d:223) */
2315                        (dev->descriptor.idVendor == 0x077d &&
2316                         dev->descriptor.idProduct == 0x07af)) {
2317                            fp->ep_attr &= ~EP_ATTR_MASK;
2318                            if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2319                                    fp->ep_attr |= EP_ATTR_ADAPTIVE;
2320                            else
2321                                    fp->ep_attr |= EP_ATTR_SYNC;
2322                    }
2323    
2324                    /* ok, let's parse further... */
2325                  if (parse_audio_format(dev, fp, format, fmt, stream) < 0) {                  if (parse_audio_format(dev, fp, format, fmt, stream) < 0) {
2326                          if (fp->rate_table)                          if (fp->rate_table)
2327                                  kfree(fp->rate_table);                                  kfree(fp->rate_table);
# Line 2258  static int parse_audio_endpoints(snd_usb Line 2347  static int parse_audio_endpoints(snd_usb
2347    
2348    
2349  /*  /*
2350     * disconnect streams
2351     * called from snd_usb_audio_disconnect()
2352     */
2353    static void snd_usb_stream_disconnect(struct list_head *head, struct usb_driver *driver)
2354    {
2355            int idx;
2356            snd_usb_stream_t *as;
2357            snd_usb_substream_t *subs;
2358            struct list_head *p;
2359    
2360            as = list_entry(head, snd_usb_stream_t, list);
2361            for (idx = 0; idx < 2; idx++) {
2362                    subs = &as->substream[idx];
2363                    if (!subs->num_formats)
2364                            return;
2365                    release_substream_urbs(subs, 1);
2366                    subs->interface = -1;
2367                    /* release interfaces */
2368                    list_for_each(p, &subs->fmt_list) {
2369                            struct audioformat *fp = list_entry(p, struct audioformat, list);
2370                            usb_driver_release_interface(driver, usb_ifnum_to_if(subs->dev, fp->iface));
2371                    }
2372            }
2373    }
2374    
2375    /*
2376   * parse audio control descriptor and create pcm/midi streams   * parse audio control descriptor and create pcm/midi streams
2377   */   */
2378  static int snd_usb_create_streams(snd_usb_audio_t *chip, int ctrlif)  static int snd_usb_create_streams(snd_usb_audio_t *chip, int ctrlif)
# Line 2361  static int create_fixed_stream_quirk(snd Line 2476  static int create_fixed_stream_quirk(snd
2476   * create a stream for an interface with proper descriptors   * create a stream for an interface with proper descriptors
2477   */   */
2478  static int create_standard_interface_quirk(snd_usb_audio_t *chip,  static int create_standard_interface_quirk(snd_usb_audio_t *chip,
2479                                             struct usb_interface *iface)                                             struct usb_interface *iface,
2480                                               const snd_usb_audio_quirk_t *quirk)
2481  {  {
2482          struct usb_host_interface *alts;          struct usb_host_interface *alts;
2483          struct usb_interface_descriptor *altsd;          struct usb_interface_descriptor *altsd;
# Line 2369  static int create_standard_interface_qui Line 2485  static int create_standard_interface_qui
2485    
2486          alts = &iface->altsetting[0];          alts = &iface->altsetting[0];
2487          altsd = get_iface_desc(alts);          altsd = get_iface_desc(alts);
2488          switch (altsd->bInterfaceSubClass) {          switch (quirk->type) {
2489          case USB_SUBCLASS_AUDIO_STREAMING:          case QUIRK_AUDIO_STANDARD_INTERFACE:
2490                  err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);                  err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
2491                  if (!err)                  if (!err)
2492                          usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0); /* reset the current interface */                          usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0); /* reset the current interface */
2493                  break;                  break;
2494          case USB_SUBCLASS_MIDI_STREAMING:          case QUIRK_MIDI_STANDARD_INTERFACE:
2495                  err = snd_usb_create_midi_interface(chip, iface, NULL);                  err = snd_usb_create_midi_interface(chip, iface, NULL);
2496                  break;                  break;
2497          default:          default:
2498                  snd_printk(KERN_ERR "if %d: non-supported subclass %d\n",                  snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2499                             altsd->bInterfaceNumber, altsd->bInterfaceSubClass);                  return -ENXIO;
                 return -ENODEV;  
2500          }          }
2501          if (err < 0) {          if (err < 0) {
2502                  snd_printk(KERN_ERR "cannot setup if %d: error %d\n",                  snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
# Line 2456  static int snd_usb_extigy_boot_quirk(str Line 2571  static int snd_usb_extigy_boot_quirk(str
2571    
2572  /*  /*
2573   * audio-interface quirks   * audio-interface quirks
2574     *
2575     * returns zero if no standard audio/MIDI parsing is needed.
2576     * returns a postive value if standard audio/midi interfaces are parsed
2577     * after this.
2578     * returns a negative value at error.
2579   */   */
2580  static int snd_usb_create_quirk(snd_usb_audio_t *chip,  static int snd_usb_create_quirk(snd_usb_audio_t *chip,
2581                                  struct usb_interface *iface,                                  struct usb_interface *iface,
# Line 2470  static int snd_usb_create_quirk(snd_usb_ Line 2590  static int snd_usb_create_quirk(snd_usb_
2590                  return create_composite_quirk(chip, iface, quirk);                  return create_composite_quirk(chip, iface, quirk);
2591          case QUIRK_AUDIO_FIXED_ENDPOINT:          case QUIRK_AUDIO_FIXED_ENDPOINT:
2592                  return create_fixed_stream_quirk(chip, iface, quirk);                  return create_fixed_stream_quirk(chip, iface, quirk);
2593          case QUIRK_STANDARD_INTERFACE:          case QUIRK_AUDIO_STANDARD_INTERFACE:
2594                  return create_standard_interface_quirk(chip, iface);          case QUIRK_MIDI_STANDARD_INTERFACE:
2595                    return create_standard_interface_quirk(chip, iface, quirk);
2596          default:          default:
2597                  snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);                  snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2598                  return -ENXIO;                  return -ENXIO;
# Line 2610  static void *snd_usb_audio_probe(struct Line 2731  static void *snd_usb_audio_probe(struct
2731          if (quirk && quirk->ifnum != QUIRK_ANY_INTERFACE && ifnum != quirk->ifnum)          if (quirk && quirk->ifnum != QUIRK_ANY_INTERFACE && ifnum != quirk->ifnum)
2732                  goto __err_val;                  goto __err_val;
2733    
         if (usb_set_configuration(dev, get_cfg_desc(config)->bConfigurationValue) < 0) {  
                 snd_printk(KERN_ERR "cannot set configuration (value 0x%x)\n", get_cfg_desc(config)->bConfigurationValue);  
                 goto __err_val;  
         }  
   
2734          /* SB Extigy needs special boot-up sequence */          /* SB Extigy needs special boot-up sequence */
2735          /* if more models come, this will go to the quirk list. */          /* if more models come, this will go to the quirk list. */
2736          if (dev->descriptor.idVendor == 0x041e && dev->descriptor.idProduct == 0x3000) {          if (dev->descriptor.idVendor == 0x041e && dev->descriptor.idProduct == 0x3000) {
# Line 2644  static void *snd_usb_audio_probe(struct Line 2760  static void *snd_usb_audio_probe(struct
2760                  /* it's a fresh one.                  /* it's a fresh one.
2761                   * now look for an empty slot and create a new card instance                   * now look for an empty slot and create a new card instance
2762                   */                   */
2763                    /* first, set the current configuration for this device */
2764                    if (usb_set_configuration(dev, get_cfg_desc(config)->bConfigurationValue) < 0) {
2765                            snd_printk(KERN_ERR "cannot set configuration (value 0x%x)\n", get_cfg_desc(config)->bConfigurationValue);
2766                            goto __error;
2767                    }
2768                  for (i = 0; i < SNDRV_CARDS; i++)                  for (i = 0; i < SNDRV_CARDS; i++)
2769                          if (enable[i] && ! usb_chip[i] &&                          if (enable[i] && ! usb_chip[i] &&
2770                              (vid[i] == -1 || vid[i] == dev->descriptor.idVendor) &&                              (vid[i] == -1 || vid[i] == dev->descriptor.idVendor) &&
# Line 2721  static void snd_usb_audio_disconnect(str Line 2842  static void snd_usb_audio_disconnect(str
2842                  snd_card_disconnect(card);                  snd_card_disconnect(card);
2843                  /* release the pcm resources */                  /* release the pcm resources */
2844                  list_for_each(p, &chip->pcm_list) {                  list_for_each(p, &chip->pcm_list) {
2845                          snd_usb_stream_t *as;                          snd_usb_stream_disconnect(p, &usb_audio_driver);
                         int idx;  
                         as = list_entry(p, snd_usb_stream_t, list);  
                         for (idx = 0; idx < 2; idx++) {  
                                 snd_usb_substream_t *subs;  
                                 subs = &as->substream[idx];  
                                 if (!subs->num_formats)  
                                         continue;  
                                 release_substream_urbs(subs, 1);  
                                 subs->interface = -1;  
                         }  
2846                  }                  }
2847                  /* release the midi resources */                  /* release the midi resources */
2848                  list_for_each(p, &chip->midi_list) {                  list_for_each(p, &chip->midi_list) {
2849                          snd_usbmidi_disconnect(p);                          snd_usbmidi_disconnect(p, &usb_audio_driver);
2850                  }                  }
2851                  up(&register_mutex);                  up(&register_mutex);
2852                  snd_card_free_in_thread(card);                  snd_card_free_in_thread(card);
# Line 2768  static void usb_audio_disconnect(struct Line 2879  static void usb_audio_disconnect(struct
2879    
2880  static int __init snd_usb_audio_init(void)  static int __init snd_usb_audio_init(void)
2881  {  {
2882            if (nrpacks < 2 || nrpacks > MAX_PACKS) {
2883                    printk(KERN_WARNING "invalid nrpacks value.\n");
2884                    return -EINVAL;
2885            }
2886          usb_register(&usb_audio_driver);          usb_register(&usb_audio_driver);
2887          return 0;          return 0;
2888  }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26