usbaudio.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333
  1. /*
  2. * (Tentative) USB Audio Driver for ALSA
  3. *
  4. * Main and PCM part
  5. *
  6. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  7. *
  8. * Many codes borrowed from audio.c by
  9. * Alan Cox (alan@lxorguk.ukuu.org.uk)
  10. * Thomas Sailer (sailer@ife.ee.ethz.ch)
  11. *
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. *
  28. * NOTES:
  29. *
  30. * - async unlink should be used for avoiding the sleep inside lock.
  31. * 2.4.22 usb-uhci seems buggy for async unlinking and results in
  32. * oops. in such a cse, pass async_unlink=0 option.
  33. * - the linked URBs would be preferred but not used so far because of
  34. * the instability of unlinking.
  35. * - type II is not supported properly. there is no device which supports
  36. * this type *correctly*. SB extigy looks as if it supports, but it's
  37. * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
  38. */
  39. #include <sound/driver.h>
  40. #include <linux/bitops.h>
  41. #include <linux/init.h>
  42. #include <linux/list.h>
  43. #include <linux/slab.h>
  44. #include <linux/string.h>
  45. #include <linux/usb.h>
  46. #include <linux/moduleparam.h>
  47. #include <sound/core.h>
  48. #include <sound/info.h>
  49. #include <sound/pcm.h>
  50. #include <sound/pcm_params.h>
  51. #include <sound/initval.h>
  52. #include "usbaudio.h"
  53. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  54. MODULE_DESCRIPTION("USB Audio");
  55. MODULE_LICENSE("GPL");
  56. MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
  57. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  58. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  59. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  60. static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Vendor ID for this card */
  61. static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; /* Product ID for this card */
  62. static int nrpacks = 4; /* max. number of packets per urb */
  63. static int async_unlink = 1;
  64. module_param_array(index, int, NULL, 0444);
  65. MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
  66. module_param_array(id, charp, NULL, 0444);
  67. MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
  68. module_param_array(enable, bool, NULL, 0444);
  69. MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
  70. module_param_array(vid, int, NULL, 0444);
  71. MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
  72. module_param_array(pid, int, NULL, 0444);
  73. MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
  74. module_param(nrpacks, int, 0444);
  75. MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
  76. module_param(async_unlink, bool, 0444);
  77. MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
  78. /*
  79. * debug the h/w constraints
  80. */
  81. /* #define HW_CONST_DEBUG */
  82. /*
  83. *
  84. */
  85. #define MAX_PACKS 10
  86. #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
  87. #define MAX_URBS 5 /* max. 20ms long packets */
  88. #define SYNC_URBS 4 /* always four urbs for sync */
  89. #define MIN_PACKS_URB 1 /* minimum 1 packet per urb */
  90. typedef struct snd_usb_substream snd_usb_substream_t;
  91. typedef struct snd_usb_stream snd_usb_stream_t;
  92. typedef struct snd_urb_ctx snd_urb_ctx_t;
  93. struct audioformat {
  94. struct list_head list;
  95. snd_pcm_format_t format; /* format type */
  96. unsigned int channels; /* # channels */
  97. unsigned int fmt_type; /* USB audio format type (1-3) */
  98. unsigned int frame_size; /* samples per frame for non-audio */
  99. int iface; /* interface number */
  100. unsigned char altsetting; /* corresponding alternate setting */
  101. unsigned char altset_idx; /* array index of altenate setting */
  102. unsigned char attributes; /* corresponding attributes of cs endpoint */
  103. unsigned char endpoint; /* endpoint */
  104. unsigned char ep_attr; /* endpoint attributes */
  105. unsigned int maxpacksize; /* max. packet size */
  106. unsigned int rates; /* rate bitmasks */
  107. unsigned int rate_min, rate_max; /* min/max rates */
  108. unsigned int nr_rates; /* number of rate table entries */
  109. unsigned int *rate_table; /* rate table */
  110. };
  111. struct snd_urb_ctx {
  112. struct urb *urb;
  113. snd_usb_substream_t *subs;
  114. int index; /* index for urb array */
  115. int packets; /* number of packets per urb */
  116. int transfer; /* transferred size */
  117. char *buf; /* buffer for capture */
  118. };
  119. struct snd_urb_ops {
  120. int (*prepare)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
  121. int (*retire)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
  122. int (*prepare_sync)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
  123. int (*retire_sync)(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime, struct urb *u);
  124. };
  125. struct snd_usb_substream {
  126. snd_usb_stream_t *stream;
  127. struct usb_device *dev;
  128. snd_pcm_substream_t *pcm_substream;
  129. int direction; /* playback or capture */
  130. int interface; /* current interface */
  131. int endpoint; /* assigned endpoint */
  132. struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
  133. unsigned int cur_rate; /* current rate (for hw_params callback) */
  134. unsigned int period_bytes; /* current period bytes (for hw_params callback) */
  135. unsigned int format; /* USB data format */
  136. unsigned int datapipe; /* the data i/o pipe */
  137. unsigned int syncpipe; /* 1 - async out or adaptive in */
  138. unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
  139. unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
  140. unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
  141. unsigned int freqmax; /* maximum sampling rate, used for buffer management */
  142. unsigned int phase; /* phase accumulator */
  143. unsigned int maxpacksize; /* max packet size in bytes */
  144. unsigned int maxframesize; /* max packet size in frames */
  145. unsigned int curpacksize; /* current packet size in bytes (for capture) */
  146. unsigned int curframesize; /* current packet size in frames (for capture) */
  147. unsigned int fill_max: 1; /* fill max packet size always */
  148. unsigned int fmt_type; /* USB audio format type (1-3) */
  149. unsigned int running: 1; /* running status */
  150. unsigned int hwptr; /* free frame position in the buffer (only for playback) */
  151. unsigned int hwptr_done; /* processed frame position in the buffer */
  152. unsigned int transfer_sched; /* scheduled frames since last period (for playback) */
  153. unsigned int transfer_done; /* processed frames since last period update */
  154. unsigned long active_mask; /* bitmask of active urbs */
  155. unsigned long unlink_mask; /* bitmask of unlinked urbs */
  156. unsigned int nurbs; /* # urbs */
  157. snd_urb_ctx_t dataurb[MAX_URBS]; /* data urb table */
  158. snd_urb_ctx_t syncurb[SYNC_URBS]; /* sync urb table */
  159. char syncbuf[SYNC_URBS * 4]; /* sync buffer; it's so small - let's get static */
  160. char *tmpbuf; /* temporary buffer for playback */
  161. u64 formats; /* format bitmasks (all or'ed) */
  162. unsigned int num_formats; /* number of supported audio formats (list) */
  163. struct list_head fmt_list; /* format list */
  164. spinlock_t lock;
  165. struct snd_urb_ops ops; /* callbacks (must be filled at init) */
  166. };
  167. struct snd_usb_stream {
  168. snd_usb_audio_t *chip;
  169. snd_pcm_t *pcm;
  170. int pcm_index;
  171. unsigned int fmt_type; /* USB audio format type (1-3) */
  172. snd_usb_substream_t substream[2];
  173. struct list_head list;
  174. };
  175. /*
  176. * we keep the snd_usb_audio_t instances by ourselves for merging
  177. * the all interfaces on the same card as one sound device.
  178. */
  179. static DECLARE_MUTEX(register_mutex);
  180. static snd_usb_audio_t *usb_chip[SNDRV_CARDS];
  181. /*
  182. * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
  183. * this will overflow at approx 524 kHz
  184. */
  185. inline static unsigned get_usb_full_speed_rate(unsigned int rate)
  186. {
  187. return ((rate << 13) + 62) / 125;
  188. }
  189. /*
  190. * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
  191. * this will overflow at approx 4 MHz
  192. */
  193. inline static unsigned get_usb_high_speed_rate(unsigned int rate)
  194. {
  195. return ((rate << 10) + 62) / 125;
  196. }
  197. /* convert our full speed USB rate into sampling rate in Hz */
  198. inline static unsigned get_full_speed_hz(unsigned int usb_rate)
  199. {
  200. return (usb_rate * 125 + (1 << 12)) >> 13;
  201. }
  202. /* convert our high speed USB rate into sampling rate in Hz */
  203. inline static unsigned get_high_speed_hz(unsigned int usb_rate)
  204. {
  205. return (usb_rate * 125 + (1 << 9)) >> 10;
  206. }
  207. /*
  208. * prepare urb for full speed capture sync pipe
  209. *
  210. * fill the length and offset of each urb descriptor.
  211. * the fixed 10.14 frequency is passed through the pipe.
  212. */
  213. static int prepare_capture_sync_urb(snd_usb_substream_t *subs,
  214. snd_pcm_runtime_t *runtime,
  215. struct urb *urb)
  216. {
  217. unsigned char *cp = urb->transfer_buffer;
  218. snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
  219. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  220. urb->iso_frame_desc[0].length = 3;
  221. urb->iso_frame_desc[0].offset = 0;
  222. cp[0] = subs->freqn >> 2;
  223. cp[1] = subs->freqn >> 10;
  224. cp[2] = subs->freqn >> 18;
  225. return 0;
  226. }
  227. /*
  228. * prepare urb for high speed capture sync pipe
  229. *
  230. * fill the length and offset of each urb descriptor.
  231. * the fixed 12.13 frequency is passed as 16.16 through the pipe.
  232. */
  233. static int prepare_capture_sync_urb_hs(snd_usb_substream_t *subs,
  234. snd_pcm_runtime_t *runtime,
  235. struct urb *urb)
  236. {
  237. unsigned char *cp = urb->transfer_buffer;
  238. snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
  239. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  240. urb->iso_frame_desc[0].length = 4;
  241. urb->iso_frame_desc[0].offset = 0;
  242. cp[0] = subs->freqn;
  243. cp[1] = subs->freqn >> 8;
  244. cp[2] = subs->freqn >> 16;
  245. cp[3] = subs->freqn >> 24;
  246. return 0;
  247. }
  248. /*
  249. * process after capture sync complete
  250. * - nothing to do
  251. */
  252. static int retire_capture_sync_urb(snd_usb_substream_t *subs,
  253. snd_pcm_runtime_t *runtime,
  254. struct urb *urb)
  255. {
  256. return 0;
  257. }
  258. /*
  259. * prepare urb for capture data pipe
  260. *
  261. * fill the offset and length of each descriptor.
  262. *
  263. * we use a temporary buffer to write the captured data.
  264. * since the length of written data is determined by host, we cannot
  265. * write onto the pcm buffer directly... the data is thus copied
  266. * later at complete callback to the global buffer.
  267. */
  268. static int prepare_capture_urb(snd_usb_substream_t *subs,
  269. snd_pcm_runtime_t *runtime,
  270. struct urb *urb)
  271. {
  272. int i, offs;
  273. unsigned long flags;
  274. snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
  275. offs = 0;
  276. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  277. urb->number_of_packets = 0;
  278. spin_lock_irqsave(&subs->lock, flags);
  279. for (i = 0; i < ctx->packets; i++) {
  280. urb->iso_frame_desc[i].offset = offs;
  281. urb->iso_frame_desc[i].length = subs->curpacksize;
  282. offs += subs->curpacksize;
  283. urb->number_of_packets++;
  284. subs->transfer_sched += subs->curframesize;
  285. if (subs->transfer_sched >= runtime->period_size) {
  286. subs->transfer_sched -= runtime->period_size;
  287. break;
  288. }
  289. }
  290. spin_unlock_irqrestore(&subs->lock, flags);
  291. urb->transfer_buffer = ctx->buf;
  292. urb->transfer_buffer_length = offs;
  293. #if 0 // for check
  294. if (! urb->bandwidth) {
  295. int bustime;
  296. bustime = usb_check_bandwidth(urb->dev, urb);
  297. if (bustime < 0)
  298. return bustime;
  299. printk("urb %d: bandwidth = %d (packets = %d)\n", ctx->index, bustime, urb->number_of_packets);
  300. usb_claim_bandwidth(urb->dev, urb, bustime, 1);
  301. }
  302. #endif // for check
  303. return 0;
  304. }
  305. /*
  306. * process after capture complete
  307. *
  308. * copy the data from each desctiptor to the pcm buffer, and
  309. * update the current position.
  310. */
  311. static int retire_capture_urb(snd_usb_substream_t *subs,
  312. snd_pcm_runtime_t *runtime,
  313. struct urb *urb)
  314. {
  315. unsigned long flags;
  316. unsigned char *cp;
  317. int i;
  318. unsigned int stride, len, oldptr;
  319. stride = runtime->frame_bits >> 3;
  320. for (i = 0; i < urb->number_of_packets; i++) {
  321. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  322. if (urb->iso_frame_desc[i].status) {
  323. snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
  324. // continue;
  325. }
  326. len = urb->iso_frame_desc[i].actual_length / stride;
  327. if (! len)
  328. continue;
  329. /* update the current pointer */
  330. spin_lock_irqsave(&subs->lock, flags);
  331. oldptr = subs->hwptr_done;
  332. subs->hwptr_done += len;
  333. if (subs->hwptr_done >= runtime->buffer_size)
  334. subs->hwptr_done -= runtime->buffer_size;
  335. subs->transfer_done += len;
  336. spin_unlock_irqrestore(&subs->lock, flags);
  337. /* copy a data chunk */
  338. if (oldptr + len > runtime->buffer_size) {
  339. unsigned int cnt = runtime->buffer_size - oldptr;
  340. unsigned int blen = cnt * stride;
  341. memcpy(runtime->dma_area + oldptr * stride, cp, blen);
  342. memcpy(runtime->dma_area, cp + blen, len * stride - blen);
  343. } else {
  344. memcpy(runtime->dma_area + oldptr * stride, cp, len * stride);
  345. }
  346. /* update the pointer, call callback if necessary */
  347. spin_lock_irqsave(&subs->lock, flags);
  348. if (subs->transfer_done >= runtime->period_size) {
  349. subs->transfer_done -= runtime->period_size;
  350. spin_unlock_irqrestore(&subs->lock, flags);
  351. snd_pcm_period_elapsed(subs->pcm_substream);
  352. } else
  353. spin_unlock_irqrestore(&subs->lock, flags);
  354. }
  355. return 0;
  356. }
  357. /*
  358. * prepare urb for full speed playback sync pipe
  359. *
  360. * set up the offset and length to receive the current frequency.
  361. */
  362. static int prepare_playback_sync_urb(snd_usb_substream_t *subs,
  363. snd_pcm_runtime_t *runtime,
  364. struct urb *urb)
  365. {
  366. snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
  367. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  368. urb->iso_frame_desc[0].length = 3;
  369. urb->iso_frame_desc[0].offset = 0;
  370. return 0;
  371. }
  372. /*
  373. * prepare urb for high speed playback sync pipe
  374. *
  375. * set up the offset and length to receive the current frequency.
  376. */
  377. static int prepare_playback_sync_urb_hs(snd_usb_substream_t *subs,
  378. snd_pcm_runtime_t *runtime,
  379. struct urb *urb)
  380. {
  381. snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
  382. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  383. urb->iso_frame_desc[0].length = 4;
  384. urb->iso_frame_desc[0].offset = 0;
  385. return 0;
  386. }
  387. /*
  388. * process after full speed playback sync complete
  389. *
  390. * retrieve the current 10.14 frequency from pipe, and set it.
  391. * the value is referred in prepare_playback_urb().
  392. */
  393. static int retire_playback_sync_urb(snd_usb_substream_t *subs,
  394. snd_pcm_runtime_t *runtime,
  395. struct urb *urb)
  396. {
  397. unsigned int f;
  398. unsigned long flags;
  399. if (urb->iso_frame_desc[0].status == 0 &&
  400. urb->iso_frame_desc[0].actual_length == 3) {
  401. f = combine_triple((u8*)urb->transfer_buffer) << 2;
  402. if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
  403. spin_lock_irqsave(&subs->lock, flags);
  404. subs->freqm = f;
  405. spin_unlock_irqrestore(&subs->lock, flags);
  406. }
  407. }
  408. return 0;
  409. }
  410. /*
  411. * process after high speed playback sync complete
  412. *
  413. * retrieve the current 12.13 frequency from pipe, and set it.
  414. * the value is referred in prepare_playback_urb().
  415. */
  416. static int retire_playback_sync_urb_hs(snd_usb_substream_t *subs,
  417. snd_pcm_runtime_t *runtime,
  418. struct urb *urb)
  419. {
  420. unsigned int f;
  421. unsigned long flags;
  422. if (urb->iso_frame_desc[0].status == 0 &&
  423. urb->iso_frame_desc[0].actual_length == 4) {
  424. f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
  425. if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
  426. spin_lock_irqsave(&subs->lock, flags);
  427. subs->freqm = f;
  428. spin_unlock_irqrestore(&subs->lock, flags);
  429. }
  430. }
  431. return 0;
  432. }
  433. /*
  434. * prepare urb for playback data pipe
  435. *
  436. * we copy the data directly from the pcm buffer.
  437. * the current position to be copied is held in hwptr field.
  438. * since a urb can handle only a single linear buffer, if the total
  439. * transferred area overflows the buffer boundary, we cannot send
  440. * it directly from the buffer. thus the data is once copied to
  441. * a temporary buffer and urb points to that.
  442. */
  443. static int prepare_playback_urb(snd_usb_substream_t *subs,
  444. snd_pcm_runtime_t *runtime,
  445. struct urb *urb)
  446. {
  447. int i, stride, offs;
  448. unsigned int counts;
  449. unsigned long flags;
  450. snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
  451. stride = runtime->frame_bits >> 3;
  452. offs = 0;
  453. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  454. urb->number_of_packets = 0;
  455. spin_lock_irqsave(&subs->lock, flags);
  456. for (i = 0; i < ctx->packets; i++) {
  457. /* calculate the size of a packet */
  458. if (subs->fill_max)
  459. counts = subs->maxframesize; /* fixed */
  460. else {
  461. subs->phase = (subs->phase & 0xffff) + subs->freqm;
  462. counts = subs->phase >> 16;
  463. if (counts > subs->maxframesize)
  464. counts = subs->maxframesize;
  465. }
  466. /* set up descriptor */
  467. urb->iso_frame_desc[i].offset = offs * stride;
  468. urb->iso_frame_desc[i].length = counts * stride;
  469. offs += counts;
  470. urb->number_of_packets++;
  471. subs->transfer_sched += counts;
  472. if (subs->transfer_sched >= runtime->period_size) {
  473. subs->transfer_sched -= runtime->period_size;
  474. if (subs->fmt_type == USB_FORMAT_TYPE_II) {
  475. if (subs->transfer_sched > 0) {
  476. /* FIXME: fill-max mode is not supported yet */
  477. offs -= subs->transfer_sched;
  478. counts -= subs->transfer_sched;
  479. urb->iso_frame_desc[i].length = counts * stride;
  480. subs->transfer_sched = 0;
  481. }
  482. i++;
  483. if (i < ctx->packets) {
  484. /* add a transfer delimiter */
  485. urb->iso_frame_desc[i].offset = offs * stride;
  486. urb->iso_frame_desc[i].length = 0;
  487. urb->number_of_packets++;
  488. }
  489. }
  490. break;
  491. }
  492. }
  493. if (subs->hwptr + offs > runtime->buffer_size) {
  494. /* err, the transferred area goes over buffer boundary.
  495. * copy the data to the temp buffer.
  496. */
  497. int len;
  498. len = runtime->buffer_size - subs->hwptr;
  499. urb->transfer_buffer = subs->tmpbuf;
  500. memcpy(subs->tmpbuf, runtime->dma_area + subs->hwptr * stride, len * stride);
  501. memcpy(subs->tmpbuf + len * stride, runtime->dma_area, (offs - len) * stride);
  502. subs->hwptr += offs;
  503. subs->hwptr -= runtime->buffer_size;
  504. } else {
  505. /* set the buffer pointer */
  506. urb->transfer_buffer = runtime->dma_area + subs->hwptr * stride;
  507. subs->hwptr += offs;
  508. if (subs->hwptr == runtime->buffer_size)
  509. subs->hwptr = 0;
  510. }
  511. spin_unlock_irqrestore(&subs->lock, flags);
  512. urb->transfer_buffer_length = offs * stride;
  513. ctx->transfer = offs;
  514. return 0;
  515. }
  516. /*
  517. * process after playback data complete
  518. *
  519. * update the current position and call callback if a period is processed.
  520. */
  521. static int retire_playback_urb(snd_usb_substream_t *subs,
  522. snd_pcm_runtime_t *runtime,
  523. struct urb *urb)
  524. {
  525. unsigned long flags;
  526. snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
  527. spin_lock_irqsave(&subs->lock, flags);
  528. subs->transfer_done += ctx->transfer;
  529. subs->hwptr_done += ctx->transfer;
  530. ctx->transfer = 0;
  531. if (subs->hwptr_done >= runtime->buffer_size)
  532. subs->hwptr_done -= runtime->buffer_size;
  533. if (subs->transfer_done >= runtime->period_size) {
  534. subs->transfer_done -= runtime->period_size;
  535. spin_unlock_irqrestore(&subs->lock, flags);
  536. snd_pcm_period_elapsed(subs->pcm_substream);
  537. } else
  538. spin_unlock_irqrestore(&subs->lock, flags);
  539. return 0;
  540. }
  541. /*
  542. */
  543. static struct snd_urb_ops audio_urb_ops[2] = {
  544. {
  545. .prepare = prepare_playback_urb,
  546. .retire = retire_playback_urb,
  547. .prepare_sync = prepare_playback_sync_urb,
  548. .retire_sync = retire_playback_sync_urb,
  549. },
  550. {
  551. .prepare = prepare_capture_urb,
  552. .retire = retire_capture_urb,
  553. .prepare_sync = prepare_capture_sync_urb,
  554. .retire_sync = retire_capture_sync_urb,
  555. },
  556. };
  557. static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
  558. {
  559. .prepare = prepare_playback_urb,
  560. .retire = retire_playback_urb,
  561. .prepare_sync = prepare_playback_sync_urb_hs,
  562. .retire_sync = retire_playback_sync_urb_hs,
  563. },
  564. {
  565. .prepare = prepare_capture_urb,
  566. .retire = retire_capture_urb,
  567. .prepare_sync = prepare_capture_sync_urb_hs,
  568. .retire_sync = retire_capture_sync_urb,
  569. },
  570. };
  571. /*
  572. * complete callback from data urb
  573. */
  574. static void snd_complete_urb(struct urb *urb, struct pt_regs *regs)
  575. {
  576. snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
  577. snd_usb_substream_t *subs = ctx->subs;
  578. snd_pcm_substream_t *substream = ctx->subs->pcm_substream;
  579. int err = 0;
  580. if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
  581. ! subs->running || /* can be stopped during retire callback */
  582. (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
  583. (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  584. clear_bit(ctx->index, &subs->active_mask);
  585. if (err < 0) {
  586. snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
  587. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  588. }
  589. }
  590. }
  591. /*
  592. * complete callback from sync urb
  593. */
  594. static void snd_complete_sync_urb(struct urb *urb, struct pt_regs *regs)
  595. {
  596. snd_urb_ctx_t *ctx = (snd_urb_ctx_t *)urb->context;
  597. snd_usb_substream_t *subs = ctx->subs;
  598. snd_pcm_substream_t *substream = ctx->subs->pcm_substream;
  599. int err = 0;
  600. if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
  601. ! subs->running || /* can be stopped during retire callback */
  602. (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
  603. (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  604. clear_bit(ctx->index + 16, &subs->active_mask);
  605. if (err < 0) {
  606. snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
  607. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  608. }
  609. }
  610. }
  611. /*
  612. * unlink active urbs.
  613. */
  614. static int deactivate_urbs(snd_usb_substream_t *subs, int force, int can_sleep)
  615. {
  616. unsigned int i;
  617. int async;
  618. subs->running = 0;
  619. if (!force && subs->stream->chip->shutdown) /* to be sure... */
  620. return -EBADFD;
  621. async = !can_sleep && async_unlink;
  622. if (! async && in_interrupt())
  623. return 0;
  624. for (i = 0; i < subs->nurbs; i++) {
  625. if (test_bit(i, &subs->active_mask)) {
  626. if (! test_and_set_bit(i, &subs->unlink_mask)) {
  627. struct urb *u = subs->dataurb[i].urb;
  628. if (async) {
  629. u->transfer_flags |= URB_ASYNC_UNLINK;
  630. usb_unlink_urb(u);
  631. } else
  632. usb_kill_urb(u);
  633. }
  634. }
  635. }
  636. if (subs->syncpipe) {
  637. for (i = 0; i < SYNC_URBS; i++) {
  638. if (test_bit(i+16, &subs->active_mask)) {
  639. if (! test_and_set_bit(i+16, &subs->unlink_mask)) {
  640. struct urb *u = subs->syncurb[i].urb;
  641. if (async) {
  642. u->transfer_flags |= URB_ASYNC_UNLINK;
  643. usb_unlink_urb(u);
  644. } else
  645. usb_kill_urb(u);
  646. }
  647. }
  648. }
  649. }
  650. return 0;
  651. }
  652. /*
  653. * set up and start data/sync urbs
  654. */
  655. static int start_urbs(snd_usb_substream_t *subs, snd_pcm_runtime_t *runtime)
  656. {
  657. unsigned int i;
  658. int err;
  659. if (subs->stream->chip->shutdown)
  660. return -EBADFD;
  661. for (i = 0; i < subs->nurbs; i++) {
  662. snd_assert(subs->dataurb[i].urb, return -EINVAL);
  663. if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
  664. snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
  665. goto __error;
  666. }
  667. }
  668. if (subs->syncpipe) {
  669. for (i = 0; i < SYNC_URBS; i++) {
  670. snd_assert(subs->syncurb[i].urb, return -EINVAL);
  671. if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
  672. snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
  673. goto __error;
  674. }
  675. }
  676. }
  677. subs->active_mask = 0;
  678. subs->unlink_mask = 0;
  679. subs->running = 1;
  680. for (i = 0; i < subs->nurbs; i++) {
  681. if ((err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC)) < 0) {
  682. snd_printk(KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
  683. goto __error;
  684. }
  685. set_bit(i, &subs->active_mask);
  686. }
  687. if (subs->syncpipe) {
  688. for (i = 0; i < SYNC_URBS; i++) {
  689. if ((err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC)) < 0) {
  690. snd_printk(KERN_ERR "cannot submit syncpipe for urb %d, err = %d\n", i, err);
  691. goto __error;
  692. }
  693. set_bit(i + 16, &subs->active_mask);
  694. }
  695. }
  696. return 0;
  697. __error:
  698. // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
  699. deactivate_urbs(subs, 0, 0);
  700. return -EPIPE;
  701. }
  702. /*
  703. * wait until all urbs are processed.
  704. */
  705. static int wait_clear_urbs(snd_usb_substream_t *subs)
  706. {
  707. int timeout = HZ;
  708. unsigned int i;
  709. int alive;
  710. do {
  711. alive = 0;
  712. for (i = 0; i < subs->nurbs; i++) {
  713. if (test_bit(i, &subs->active_mask))
  714. alive++;
  715. }
  716. if (subs->syncpipe) {
  717. for (i = 0; i < SYNC_URBS; i++) {
  718. if (test_bit(i + 16, &subs->active_mask))
  719. alive++;
  720. }
  721. }
  722. if (! alive)
  723. break;
  724. set_current_state(TASK_UNINTERRUPTIBLE);
  725. schedule_timeout(1);
  726. } while (--timeout > 0);
  727. if (alive)
  728. snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
  729. return 0;
  730. }
  731. /*
  732. * return the current pcm pointer. just return the hwptr_done value.
  733. */
  734. static snd_pcm_uframes_t snd_usb_pcm_pointer(snd_pcm_substream_t *substream)
  735. {
  736. snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
  737. return subs->hwptr_done;
  738. }
  739. /*
  740. * start/stop substream
  741. */
  742. static int snd_usb_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
  743. {
  744. snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
  745. int err;
  746. switch (cmd) {
  747. case SNDRV_PCM_TRIGGER_START:
  748. err = start_urbs(subs, substream->runtime);
  749. break;
  750. case SNDRV_PCM_TRIGGER_STOP:
  751. err = deactivate_urbs(subs, 0, 0);
  752. break;
  753. default:
  754. err = -EINVAL;
  755. break;
  756. }
  757. return err < 0 ? err : 0;
  758. }
  759. /*
  760. * release a urb data
  761. */
  762. static void release_urb_ctx(snd_urb_ctx_t *u)
  763. {
  764. if (u->urb) {
  765. usb_free_urb(u->urb);
  766. u->urb = NULL;
  767. }
  768. kfree(u->buf);
  769. u->buf = NULL;
  770. }
  771. /*
  772. * release a substream
  773. */
  774. static void release_substream_urbs(snd_usb_substream_t *subs, int force)
  775. {
  776. int i;
  777. /* stop urbs (to be sure) */
  778. deactivate_urbs(subs, force, 1);
  779. wait_clear_urbs(subs);
  780. for (i = 0; i < MAX_URBS; i++)
  781. release_urb_ctx(&subs->dataurb[i]);
  782. for (i = 0; i < SYNC_URBS; i++)
  783. release_urb_ctx(&subs->syncurb[i]);
  784. kfree(subs->tmpbuf);
  785. subs->tmpbuf = NULL;
  786. subs->nurbs = 0;
  787. }
  788. /*
  789. * initialize a substream for plaback/capture
  790. */
  791. static int init_substream_urbs(snd_usb_substream_t *subs, unsigned int period_bytes,
  792. unsigned int rate, unsigned int frame_bits)
  793. {
  794. unsigned int maxsize, n, i;
  795. int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  796. unsigned int npacks[MAX_URBS], urb_packs, total_packs;
  797. /* calculate the frequency in 16.16 format */
  798. if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  799. subs->freqn = get_usb_full_speed_rate(rate);
  800. else
  801. subs->freqn = get_usb_high_speed_rate(rate);
  802. subs->freqm = subs->freqn;
  803. subs->freqmax = subs->freqn + (subs->freqn >> 2); /* max. allowed frequency */
  804. subs->phase = 0;
  805. /* calculate the max. size of packet */
  806. maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3)) >> 16;
  807. if (subs->maxpacksize && maxsize > subs->maxpacksize) {
  808. //snd_printd(KERN_DEBUG "maxsize %d is greater than defined size %d\n",
  809. // maxsize, subs->maxpacksize);
  810. maxsize = subs->maxpacksize;
  811. }
  812. if (subs->fill_max)
  813. subs->curpacksize = subs->maxpacksize;
  814. else
  815. subs->curpacksize = maxsize;
  816. if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  817. urb_packs = nrpacks;
  818. else
  819. urb_packs = nrpacks * 8;
  820. /* allocate a temporary buffer for playback */
  821. if (is_playback) {
  822. subs->tmpbuf = kmalloc(maxsize * urb_packs, GFP_KERNEL);
  823. if (! subs->tmpbuf) {
  824. snd_printk(KERN_ERR "cannot malloc tmpbuf\n");
  825. return -ENOMEM;
  826. }
  827. }
  828. /* decide how many packets to be used */
  829. total_packs = (period_bytes + maxsize - 1) / maxsize;
  830. if (total_packs < 2 * MIN_PACKS_URB)
  831. total_packs = 2 * MIN_PACKS_URB;
  832. subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
  833. if (subs->nurbs > MAX_URBS) {
  834. /* too much... */
  835. subs->nurbs = MAX_URBS;
  836. total_packs = MAX_URBS * urb_packs;
  837. }
  838. n = total_packs;
  839. for (i = 0; i < subs->nurbs; i++) {
  840. npacks[i] = n > urb_packs ? urb_packs : n;
  841. n -= urb_packs;
  842. }
  843. if (subs->nurbs <= 1) {
  844. /* too little - we need at least two packets
  845. * to ensure contiguous playback/capture
  846. */
  847. subs->nurbs = 2;
  848. npacks[0] = (total_packs + 1) / 2;
  849. npacks[1] = total_packs - npacks[0];
  850. } else if (npacks[subs->nurbs-1] < MIN_PACKS_URB) {
  851. /* the last packet is too small.. */
  852. if (subs->nurbs > 2) {
  853. /* merge to the first one */
  854. npacks[0] += npacks[subs->nurbs - 1];
  855. subs->nurbs--;
  856. } else {
  857. /* divide to two */
  858. subs->nurbs = 2;
  859. npacks[0] = (total_packs + 1) / 2;
  860. npacks[1] = total_packs - npacks[0];
  861. }
  862. }
  863. /* allocate and initialize data urbs */
  864. for (i = 0; i < subs->nurbs; i++) {
  865. snd_urb_ctx_t *u = &subs->dataurb[i];
  866. u->index = i;
  867. u->subs = subs;
  868. u->transfer = 0;
  869. u->packets = npacks[i];
  870. if (subs->fmt_type == USB_FORMAT_TYPE_II)
  871. u->packets++; /* for transfer delimiter */
  872. if (! is_playback) {
  873. /* allocate a capture buffer per urb */
  874. u->buf = kmalloc(maxsize * u->packets, GFP_KERNEL);
  875. if (! u->buf) {
  876. release_substream_urbs(subs, 0);
  877. return -ENOMEM;
  878. }
  879. }
  880. u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
  881. if (! u->urb) {
  882. release_substream_urbs(subs, 0);
  883. return -ENOMEM;
  884. }
  885. u->urb->dev = subs->dev;
  886. u->urb->pipe = subs->datapipe;
  887. u->urb->transfer_flags = URB_ISO_ASAP;
  888. u->urb->number_of_packets = u->packets;
  889. u->urb->interval = 1;
  890. u->urb->context = u;
  891. u->urb->complete = snd_usb_complete_callback(snd_complete_urb);
  892. }
  893. if (subs->syncpipe) {
  894. /* allocate and initialize sync urbs */
  895. for (i = 0; i < SYNC_URBS; i++) {
  896. snd_urb_ctx_t *u = &subs->syncurb[i];
  897. u->index = i;
  898. u->subs = subs;
  899. u->packets = 1;
  900. u->urb = usb_alloc_urb(1, GFP_KERNEL);
  901. if (! u->urb) {
  902. release_substream_urbs(subs, 0);
  903. return -ENOMEM;
  904. }
  905. u->urb->transfer_buffer = subs->syncbuf + i * 4;
  906. u->urb->transfer_buffer_length = 4;
  907. u->urb->dev = subs->dev;
  908. u->urb->pipe = subs->syncpipe;
  909. u->urb->transfer_flags = URB_ISO_ASAP;
  910. u->urb->number_of_packets = 1;
  911. u->urb->interval = 1 << subs->syncinterval;
  912. u->urb->context = u;
  913. u->urb->complete = snd_usb_complete_callback(snd_complete_sync_urb);
  914. }
  915. }
  916. return 0;
  917. }
  918. /*
  919. * find a matching audio format
  920. */
  921. static struct audioformat *find_format(snd_usb_substream_t *subs, unsigned int format,
  922. unsigned int rate, unsigned int channels)
  923. {
  924. struct list_head *p;
  925. struct audioformat *found = NULL;
  926. int cur_attr = 0, attr;
  927. list_for_each(p, &subs->fmt_list) {
  928. struct audioformat *fp;
  929. fp = list_entry(p, struct audioformat, list);
  930. if (fp->format != format || fp->channels != channels)
  931. continue;
  932. if (rate < fp->rate_min || rate > fp->rate_max)
  933. continue;
  934. if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
  935. unsigned int i;
  936. for (i = 0; i < fp->nr_rates; i++)
  937. if (fp->rate_table[i] == rate)
  938. break;
  939. if (i >= fp->nr_rates)
  940. continue;
  941. }
  942. attr = fp->ep_attr & EP_ATTR_MASK;
  943. if (! found) {
  944. found = fp;
  945. cur_attr = attr;
  946. continue;
  947. }
  948. /* avoid async out and adaptive in if the other method
  949. * supports the same format.
  950. * this is a workaround for the case like
  951. * M-audio audiophile USB.
  952. */
  953. if (attr != cur_attr) {
  954. if ((attr == EP_ATTR_ASYNC &&
  955. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  956. (attr == EP_ATTR_ADAPTIVE &&
  957. subs->direction == SNDRV_PCM_STREAM_CAPTURE))
  958. continue;
  959. if ((cur_attr == EP_ATTR_ASYNC &&
  960. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  961. (cur_attr == EP_ATTR_ADAPTIVE &&
  962. subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
  963. found = fp;
  964. cur_attr = attr;
  965. continue;
  966. }
  967. }
  968. /* find the format with the largest max. packet size */
  969. if (fp->maxpacksize > found->maxpacksize) {
  970. found = fp;
  971. cur_attr = attr;
  972. }
  973. }
  974. return found;
  975. }
  976. /*
  977. * initialize the picth control and sample rate
  978. */
  979. static int init_usb_pitch(struct usb_device *dev, int iface,
  980. struct usb_host_interface *alts,
  981. struct audioformat *fmt)
  982. {
  983. unsigned int ep;
  984. unsigned char data[1];
  985. int err;
  986. ep = get_endpoint(alts, 0)->bEndpointAddress;
  987. /* if endpoint has pitch control, enable it */
  988. if (fmt->attributes & EP_CS_ATTR_PITCH_CONTROL) {
  989. data[0] = 1;
  990. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
  991. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  992. PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
  993. snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
  994. dev->devnum, iface, ep);
  995. return err;
  996. }
  997. }
  998. return 0;
  999. }
  1000. static int init_usb_sample_rate(struct usb_device *dev, int iface,
  1001. struct usb_host_interface *alts,
  1002. struct audioformat *fmt, int rate)
  1003. {
  1004. unsigned int ep;
  1005. unsigned char data[3];
  1006. int err;
  1007. ep = get_endpoint(alts, 0)->bEndpointAddress;
  1008. /* if endpoint has sampling rate control, set it */
  1009. if (fmt->attributes & EP_CS_ATTR_SAMPLE_RATE) {
  1010. int crate;
  1011. data[0] = rate;
  1012. data[1] = rate >> 8;
  1013. data[2] = rate >> 16;
  1014. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), SET_CUR,
  1015. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  1016. SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
  1017. snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep 0x%x\n",
  1018. dev->devnum, iface, fmt->altsetting, rate, ep);
  1019. return err;
  1020. }
  1021. if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), GET_CUR,
  1022. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
  1023. SAMPLING_FREQ_CONTROL << 8, ep, data, 3, 1000)) < 0) {
  1024. snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep 0x%x\n",
  1025. dev->devnum, iface, fmt->altsetting, ep);
  1026. return 0; /* some devices don't support reading */
  1027. }
  1028. crate = data[0] | (data[1] << 8) | (data[2] << 16);
  1029. if (crate != rate) {
  1030. snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
  1031. // runtime->rate = crate;
  1032. }
  1033. }
  1034. return 0;
  1035. }
  1036. /*
  1037. * find a matching format and set up the interface
  1038. */
  1039. static int set_format(snd_usb_substream_t *subs, struct audioformat *fmt)
  1040. {
  1041. struct usb_device *dev = subs->dev;
  1042. struct usb_host_interface *alts;
  1043. struct usb_interface_descriptor *altsd;
  1044. struct usb_interface *iface;
  1045. unsigned int ep, attr;
  1046. int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  1047. int err;
  1048. iface = usb_ifnum_to_if(dev, fmt->iface);
  1049. snd_assert(iface, return -EINVAL);
  1050. alts = &iface->altsetting[fmt->altset_idx];
  1051. altsd = get_iface_desc(alts);
  1052. snd_assert(altsd->bAlternateSetting == fmt->altsetting, return -EINVAL);
  1053. if (fmt == subs->cur_audiofmt)
  1054. return 0;
  1055. /* close the old interface */
  1056. if (subs->interface >= 0 && subs->interface != fmt->iface) {
  1057. usb_set_interface(subs->dev, subs->interface, 0);
  1058. subs->interface = -1;
  1059. subs->format = 0;
  1060. }
  1061. /* set interface */
  1062. if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
  1063. if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
  1064. snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
  1065. dev->devnum, fmt->iface, fmt->altsetting);
  1066. return -EIO;
  1067. }
  1068. snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
  1069. subs->interface = fmt->iface;
  1070. subs->format = fmt->altset_idx;
  1071. }
  1072. /* create a data pipe */
  1073. ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
  1074. if (is_playback)
  1075. subs->datapipe = usb_sndisocpipe(dev, ep);
  1076. else
  1077. subs->datapipe = usb_rcvisocpipe(dev, ep);
  1078. subs->syncpipe = subs->syncinterval = 0;
  1079. subs->maxpacksize = fmt->maxpacksize;
  1080. subs->fill_max = 0;
  1081. /* we need a sync pipe in async OUT or adaptive IN mode */
  1082. /* check the number of EP, since some devices have broken
  1083. * descriptors which fool us. if it has only one EP,
  1084. * assume it as adaptive-out or sync-in.
  1085. */
  1086. attr = fmt->ep_attr & EP_ATTR_MASK;
  1087. if (((is_playback && attr == EP_ATTR_ASYNC) ||
  1088. (! is_playback && attr == EP_ATTR_ADAPTIVE)) &&
  1089. altsd->bNumEndpoints >= 2) {
  1090. /* check sync-pipe endpoint */
  1091. /* ... and check descriptor size before accessing bSynchAddress
  1092. because there is a version of the SB Audigy 2 NX firmware lacking
  1093. the audio fields in the endpoint descriptors */
  1094. if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
  1095. (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  1096. get_endpoint(alts, 1)->bSynchAddress != 0)) {
  1097. snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
  1098. dev->devnum, fmt->iface, fmt->altsetting);
  1099. return -EINVAL;
  1100. }
  1101. ep = get_endpoint(alts, 1)->bEndpointAddress;
  1102. if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  1103. (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
  1104. (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
  1105. snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
  1106. dev->devnum, fmt->iface, fmt->altsetting);
  1107. return -EINVAL;
  1108. }
  1109. ep &= USB_ENDPOINT_NUMBER_MASK;
  1110. if (is_playback)
  1111. subs->syncpipe = usb_rcvisocpipe(dev, ep);
  1112. else
  1113. subs->syncpipe = usb_sndisocpipe(dev, ep);
  1114. if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  1115. get_endpoint(alts, 1)->bRefresh >= 1 &&
  1116. get_endpoint(alts, 1)->bRefresh <= 9)
  1117. subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
  1118. else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  1119. subs->syncinterval = 1;
  1120. else if (get_endpoint(alts, 1)->bInterval >= 1 &&
  1121. get_endpoint(alts, 1)->bInterval <= 16)
  1122. subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
  1123. else
  1124. subs->syncinterval = 3;
  1125. }
  1126. /* always fill max packet size */
  1127. if (fmt->attributes & EP_CS_ATTR_FILL_MAX)
  1128. subs->fill_max = 1;
  1129. if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
  1130. return err;
  1131. subs->cur_audiofmt = fmt;
  1132. #if 0
  1133. printk("setting done: format = %d, rate = %d, channels = %d\n",
  1134. fmt->format, fmt->rate, fmt->channels);
  1135. printk(" datapipe = 0x%0x, syncpipe = 0x%0x\n",
  1136. subs->datapipe, subs->syncpipe);
  1137. #endif
  1138. return 0;
  1139. }
  1140. /*
  1141. * hw_params callback
  1142. *
  1143. * allocate a buffer and set the given audio format.
  1144. *
  1145. * so far we use a physically linear buffer although packetize transfer
  1146. * doesn't need a continuous area.
  1147. * if sg buffer is supported on the later version of alsa, we'll follow
  1148. * that.
  1149. */
  1150. static int snd_usb_hw_params(snd_pcm_substream_t *substream,
  1151. snd_pcm_hw_params_t *hw_params)
  1152. {
  1153. snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
  1154. struct audioformat *fmt;
  1155. unsigned int channels, rate, format;
  1156. int ret, changed;
  1157. ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  1158. if (ret < 0)
  1159. return ret;
  1160. format = params_format(hw_params);
  1161. rate = params_rate(hw_params);
  1162. channels = params_channels(hw_params);
  1163. fmt = find_format(subs, format, rate, channels);
  1164. if (! fmt) {
  1165. snd_printd(KERN_DEBUG "cannot set format: format = %s, rate = %d, channels = %d\n",
  1166. snd_pcm_format_name(format), rate, channels);
  1167. return -EINVAL;
  1168. }
  1169. changed = subs->cur_audiofmt != fmt ||
  1170. subs->period_bytes != params_period_bytes(hw_params) ||
  1171. subs->cur_rate != rate;
  1172. if ((ret = set_format(subs, fmt)) < 0)
  1173. return ret;
  1174. if (subs->cur_rate != rate) {
  1175. struct usb_host_interface *alts;
  1176. struct usb_interface *iface;
  1177. iface = usb_ifnum_to_if(subs->dev, fmt->iface);
  1178. alts = &iface->altsetting[fmt->altset_idx];
  1179. ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
  1180. if (ret < 0)
  1181. return ret;
  1182. subs->cur_rate = rate;
  1183. }
  1184. if (changed) {
  1185. /* format changed */
  1186. release_substream_urbs(subs, 0);
  1187. /* influenced: period_bytes, channels, rate, format, */
  1188. ret = init_substream_urbs(subs, params_period_bytes(hw_params),
  1189. params_rate(hw_params),
  1190. snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
  1191. }
  1192. return ret;
  1193. }
  1194. /*
  1195. * hw_free callback
  1196. *
  1197. * reset the audio format and release the buffer
  1198. */
  1199. static int snd_usb_hw_free(snd_pcm_substream_t *substream)
  1200. {
  1201. snd_usb_substream_t *subs = (snd_usb_substream_t *)substream->runtime->private_data;
  1202. subs->cur_audiofmt = NULL;
  1203. subs->cur_rate = 0;
  1204. subs->period_bytes = 0;
  1205. release_substream_urbs(subs, 0);
  1206. return snd_pcm_lib_free_pages(substream);
  1207. }
  1208. /*
  1209. * prepare callback
  1210. *
  1211. * only a few subtle things...
  1212. */
  1213. static int snd_usb_pcm_prepare(snd_pcm_substream_t *substream)
  1214. {
  1215. snd_pcm_runtime_t *runtime = substream->runtime;
  1216. snd_usb_substream_t *subs = (snd_usb_substream_t *)runtime->private_data;
  1217. if (! subs->cur_audiofmt) {
  1218. snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
  1219. return -ENXIO;
  1220. }
  1221. /* some unit conversions in runtime */
  1222. subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
  1223. subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
  1224. /* reset the pointer */
  1225. subs->hwptr = 0;
  1226. subs->hwptr_done = 0;
  1227. subs->transfer_sched = 0;
  1228. subs->transfer_done = 0;
  1229. subs->phase = 0;
  1230. /* clear urbs (to be sure) */
  1231. deactivate_urbs(subs, 0, 1);
  1232. wait_clear_urbs(subs);
  1233. return 0;
  1234. }
  1235. static snd_pcm_hardware_t snd_usb_playback =
  1236. {
  1237. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1238. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1239. SNDRV_PCM_INFO_MMAP_VALID),
  1240. .buffer_bytes_max = (128*1024),
  1241. .period_bytes_min = 64,
  1242. .period_bytes_max = (128*1024),
  1243. .periods_min = 2,
  1244. .periods_max = 1024,
  1245. };
  1246. static snd_pcm_hardware_t snd_usb_capture =
  1247. {
  1248. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1249. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1250. SNDRV_PCM_INFO_MMAP_VALID),
  1251. .buffer_bytes_max = (128*1024),
  1252. .period_bytes_min = 64,
  1253. .period_bytes_max = (128*1024),
  1254. .periods_min = 2,
  1255. .periods_max = 1024,
  1256. };
  1257. /*
  1258. * h/w constraints
  1259. */
  1260. #ifdef HW_CONST_DEBUG
  1261. #define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
  1262. #else
  1263. #define hwc_debug(fmt, args...) /**/
  1264. #endif
  1265. static int hw_check_valid_format(snd_pcm_hw_params_t *params, struct audioformat *fp)
  1266. {
  1267. snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  1268. snd_interval_t *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  1269. snd_mask_t *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1270. /* check the format */
  1271. if (! snd_mask_test(fmts, fp->format)) {
  1272. hwc_debug(" > check: no supported format %d\n", fp->format);
  1273. return 0;
  1274. }
  1275. /* check the channels */
  1276. if (fp->channels < ct->min || fp->channels > ct->max) {
  1277. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  1278. return 0;
  1279. }
  1280. /* check the rate is within the range */
  1281. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  1282. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  1283. return 0;
  1284. }
  1285. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  1286. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  1287. return 0;
  1288. }
  1289. return 1;
  1290. }
  1291. static int hw_rule_rate(snd_pcm_hw_params_t *params,
  1292. snd_pcm_hw_rule_t *rule)
  1293. {
  1294. snd_usb_substream_t *subs = rule->private;
  1295. struct list_head *p;
  1296. snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  1297. unsigned int rmin, rmax;
  1298. int changed;
  1299. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  1300. changed = 0;
  1301. rmin = rmax = 0;
  1302. list_for_each(p, &subs->fmt_list) {
  1303. struct audioformat *fp;
  1304. fp = list_entry(p, struct audioformat, list);
  1305. if (! hw_check_valid_format(params, fp))
  1306. continue;
  1307. if (changed++) {
  1308. if (rmin > fp->rate_min)
  1309. rmin = fp->rate_min;
  1310. if (rmax < fp->rate_max)
  1311. rmax = fp->rate_max;
  1312. } else {
  1313. rmin = fp->rate_min;
  1314. rmax = fp->rate_max;
  1315. }
  1316. }
  1317. if (! changed) {
  1318. hwc_debug(" --> get empty\n");
  1319. it->empty = 1;
  1320. return -EINVAL;
  1321. }
  1322. changed = 0;
  1323. if (it->min < rmin) {
  1324. it->min = rmin;
  1325. it->openmin = 0;
  1326. changed = 1;
  1327. }
  1328. if (it->max > rmax) {
  1329. it->max = rmax;
  1330. it->openmax = 0;
  1331. changed = 1;
  1332. }
  1333. if (snd_interval_checkempty(it)) {
  1334. it->empty = 1;
  1335. return -EINVAL;
  1336. }
  1337. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  1338. return changed;
  1339. }
  1340. static int hw_rule_channels(snd_pcm_hw_params_t *params,
  1341. snd_pcm_hw_rule_t *rule)
  1342. {
  1343. snd_usb_substream_t *subs = rule->private;
  1344. struct list_head *p;
  1345. snd_interval_t *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  1346. unsigned int rmin, rmax;
  1347. int changed;
  1348. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  1349. changed = 0;
  1350. rmin = rmax = 0;
  1351. list_for_each(p, &subs->fmt_list) {
  1352. struct audioformat *fp;
  1353. fp = list_entry(p, struct audioformat, list);
  1354. if (! hw_check_valid_format(params, fp))
  1355. continue;
  1356. if (changed++) {
  1357. if (rmin > fp->channels)
  1358. rmin = fp->channels;
  1359. if (rmax < fp->channels)
  1360. rmax = fp->channels;
  1361. } else {
  1362. rmin = fp->channels;
  1363. rmax = fp->channels;
  1364. }
  1365. }
  1366. if (! changed) {
  1367. hwc_debug(" --> get empty\n");
  1368. it->empty = 1;
  1369. return -EINVAL;
  1370. }
  1371. changed = 0;
  1372. if (it->min < rmin) {
  1373. it->min = rmin;
  1374. it->openmin = 0;
  1375. changed = 1;
  1376. }
  1377. if (it->max > rmax) {
  1378. it->max = rmax;
  1379. it->openmax = 0;
  1380. changed = 1;
  1381. }
  1382. if (snd_interval_checkempty(it)) {
  1383. it->empty = 1;
  1384. return -EINVAL;
  1385. }
  1386. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  1387. return changed;
  1388. }
  1389. static int hw_rule_format(snd_pcm_hw_params_t *params,
  1390. snd_pcm_hw_rule_t *rule)
  1391. {
  1392. snd_usb_substream_t *subs = rule->private;
  1393. struct list_head *p;
  1394. snd_mask_t *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1395. u64 fbits;
  1396. u32 oldbits[2];
  1397. int changed;
  1398. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  1399. fbits = 0;
  1400. list_for_each(p, &subs->fmt_list) {
  1401. struct audioformat *fp;
  1402. fp = list_entry(p, struct audioformat, list);
  1403. if (! hw_check_valid_format(params, fp))
  1404. continue;
  1405. fbits |= (1ULL << fp->format);
  1406. }
  1407. oldbits[0] = fmt->bits[0];
  1408. oldbits[1] = fmt->bits[1];
  1409. fmt->bits[0] &= (u32)fbits;
  1410. fmt->bits[1] &= (u32)(fbits >> 32);
  1411. if (! fmt->bits[0] && ! fmt->bits[1]) {
  1412. hwc_debug(" --> get empty\n");
  1413. return -EINVAL;
  1414. }
  1415. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  1416. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  1417. return changed;
  1418. }
  1419. #define MAX_MASK 64
  1420. /*
  1421. * check whether the registered audio formats need special hw-constraints
  1422. */
  1423. static int check_hw_params_convention(snd_usb_substream_t *subs)
  1424. {
  1425. int i;
  1426. u32 *channels;
  1427. u32 *rates;
  1428. u32 cmaster, rmaster;
  1429. u32 rate_min = 0, rate_max = 0;
  1430. struct list_head *p;
  1431. int err = 1;
  1432. channels = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
  1433. rates = kcalloc(MAX_MASK, sizeof(u32), GFP_KERNEL);
  1434. list_for_each(p, &subs->fmt_list) {
  1435. struct audioformat *f;
  1436. f = list_entry(p, struct audioformat, list);
  1437. /* unconventional channels? */
  1438. if (f->channels > 32)
  1439. goto __out;
  1440. /* continuous rate min/max matches? */
  1441. if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
  1442. if (rate_min && f->rate_min != rate_min)
  1443. goto __out;
  1444. if (rate_max && f->rate_max != rate_max)
  1445. goto __out;
  1446. rate_min = f->rate_min;
  1447. rate_max = f->rate_max;
  1448. }
  1449. /* combination of continuous rates and fixed rates? */
  1450. if (rates[f->format] & SNDRV_PCM_RATE_CONTINUOUS) {
  1451. if (f->rates != rates[f->format])
  1452. goto __out;
  1453. }
  1454. if (f->rates & SNDRV_PCM_RATE_CONTINUOUS) {
  1455. if (rates[f->format] && rates[f->format] != f->rates)
  1456. goto __out;
  1457. }
  1458. channels[f->format] |= (1 << f->channels);
  1459. rates[f->format] |= f->rates;
  1460. }
  1461. /* check whether channels and rates match for all formats */
  1462. cmaster = rmaster = 0;
  1463. for (i = 0; i < MAX_MASK; i++) {
  1464. if (cmaster != channels[i] && cmaster && channels[i])
  1465. goto __out;
  1466. if (rmaster != rates[i] && rmaster && rates[i])
  1467. goto __out;
  1468. if (channels[i])
  1469. cmaster = channels[i];
  1470. if (rates[i])
  1471. rmaster = rates[i];
  1472. }
  1473. /* check whether channels match for all distinct rates */
  1474. memset(channels, 0, MAX_MASK * sizeof(u32));
  1475. list_for_each(p, &subs->fmt_list) {
  1476. struct audioformat *f;
  1477. f = list_entry(p, struct audioformat, list);
  1478. if (f->rates & SNDRV_PCM_RATE_CONTINUOUS)
  1479. continue;
  1480. for (i = 0; i < 32; i++) {
  1481. if (f->rates & (1 << i))
  1482. channels[i] |= (1 << f->channels);
  1483. }
  1484. }
  1485. cmaster = 0;
  1486. for (i = 0; i < 32; i++) {
  1487. if (cmaster != channels[i] && cmaster && channels[i])
  1488. goto __out;
  1489. if (channels[i])
  1490. cmaster = channels[i];
  1491. }
  1492. err = 0;
  1493. __out:
  1494. kfree(channels);
  1495. kfree(rates);
  1496. return err;
  1497. }
  1498. /*
  1499. * set up the runtime hardware information.
  1500. */
  1501. static int setup_hw_info(snd_pcm_runtime_t *runtime, snd_usb_substream_t *subs)
  1502. {
  1503. struct list_head *p;
  1504. int err;
  1505. runtime->hw.formats = subs->formats;
  1506. runtime->hw.rate_min = 0x7fffffff;
  1507. runtime->hw.rate_max = 0;
  1508. runtime->hw.channels_min = 256;
  1509. runtime->hw.channels_max = 0;
  1510. runtime->hw.rates = 0;
  1511. /* check min/max rates and channels */
  1512. list_for_each(p, &subs->fmt_list) {
  1513. struct audioformat *fp;
  1514. fp = list_entry(p, struct audioformat, list);
  1515. runtime->hw.rates |= fp->rates;
  1516. if (runtime->hw.rate_min > fp->rate_min)
  1517. runtime->hw.rate_min = fp->rate_min;
  1518. if (runtime->hw.rate_max < fp->rate_max)
  1519. runtime->hw.rate_max = fp->rate_max;
  1520. if (runtime->hw.channels_min > fp->channels)
  1521. runtime->hw.channels_min = fp->channels;
  1522. if (runtime->hw.channels_max < fp->channels)
  1523. runtime->hw.channels_max = fp->channels;
  1524. if (fp->fmt_type == USB_FORMAT_TYPE_II && fp->frame_size > 0) {
  1525. /* FIXME: there might be more than one audio formats... */
  1526. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  1527. fp->frame_size;
  1528. }
  1529. }
  1530. /* set the period time minimum 1ms */
  1531. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1532. 1000 * MIN_PACKS_URB,
  1533. /*(nrpacks * MAX_URBS) * 1000*/ UINT_MAX);
  1534. if (check_hw_params_convention(subs)) {
  1535. hwc_debug("setting extra hw constraints...\n");
  1536. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1537. hw_rule_rate, subs,
  1538. SNDRV_PCM_HW_PARAM_FORMAT,
  1539. SNDRV_PCM_HW_PARAM_CHANNELS,
  1540. -1)) < 0)
  1541. return err;
  1542. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  1543. hw_rule_channels, subs,
  1544. SNDRV_PCM_HW_PARAM_FORMAT,
  1545. SNDRV_PCM_HW_PARAM_RATE,
  1546. -1)) < 0)
  1547. return err;
  1548. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  1549. hw_rule_format, subs,
  1550. SNDRV_PCM_HW_PARAM_RATE,
  1551. SNDRV_PCM_HW_PARAM_CHANNELS,
  1552. -1)) < 0)
  1553. return err;
  1554. }
  1555. return 0;
  1556. }
  1557. static int snd_usb_pcm_open(snd_pcm_substream_t *substream, int direction,
  1558. snd_pcm_hardware_t *hw)
  1559. {
  1560. snd_usb_stream_t *as = snd_pcm_substream_chip(substream);
  1561. snd_pcm_runtime_t *runtime = substream->runtime;
  1562. snd_usb_substream_t *subs = &as->substream[direction];
  1563. subs->interface = -1;
  1564. subs->format = 0;
  1565. runtime->hw = *hw;
  1566. runtime->private_data = subs;
  1567. subs->pcm_substream = substream;
  1568. return setup_hw_info(runtime, subs);
  1569. }
  1570. static int snd_usb_pcm_close(snd_pcm_substream_t *substream, int direction)
  1571. {
  1572. snd_usb_stream_t *as = snd_pcm_substream_chip(substream);
  1573. snd_usb_substream_t *subs = &as->substream[direction];
  1574. if (subs->interface >= 0) {
  1575. usb_set_interface(subs->dev, subs->interface, 0);
  1576. subs->interface = -1;
  1577. }
  1578. subs->pcm_substream = NULL;
  1579. return 0;
  1580. }
  1581. static int snd_usb_playback_open(snd_pcm_substream_t *substream)
  1582. {
  1583. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK, &snd_usb_playback);
  1584. }
  1585. static int snd_usb_playback_close(snd_pcm_substream_t *substream)
  1586. {
  1587. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1588. }
  1589. static int snd_usb_capture_open(snd_pcm_substream_t *substream)
  1590. {
  1591. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE, &snd_usb_capture);
  1592. }
  1593. static int snd_usb_capture_close(snd_pcm_substream_t *substream)
  1594. {
  1595. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  1596. }
  1597. static snd_pcm_ops_t snd_usb_playback_ops = {
  1598. .open = snd_usb_playback_open,
  1599. .close = snd_usb_playback_close,
  1600. .ioctl = snd_pcm_lib_ioctl,
  1601. .hw_params = snd_usb_hw_params,
  1602. .hw_free = snd_usb_hw_free,
  1603. .prepare = snd_usb_pcm_prepare,
  1604. .trigger = snd_usb_pcm_trigger,
  1605. .pointer = snd_usb_pcm_pointer,
  1606. };
  1607. static snd_pcm_ops_t snd_usb_capture_ops = {
  1608. .open = snd_usb_capture_open,
  1609. .close = snd_usb_capture_close,
  1610. .ioctl = snd_pcm_lib_ioctl,
  1611. .hw_params = snd_usb_hw_params,
  1612. .hw_free = snd_usb_hw_free,
  1613. .prepare = snd_usb_pcm_prepare,
  1614. .trigger = snd_usb_pcm_trigger,
  1615. .pointer = snd_usb_pcm_pointer,
  1616. };
  1617. /*
  1618. * helper functions
  1619. */
  1620. /*
  1621. * combine bytes and get an integer value
  1622. */
  1623. unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
  1624. {
  1625. switch (size) {
  1626. case 1: return *bytes;
  1627. case 2: return combine_word(bytes);
  1628. case 3: return combine_triple(bytes);
  1629. case 4: return combine_quad(bytes);
  1630. default: return 0;
  1631. }
  1632. }
  1633. /*
  1634. * parse descriptor buffer and return the pointer starting the given
  1635. * descriptor type.
  1636. */
  1637. void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
  1638. {
  1639. u8 *p, *end, *next;
  1640. p = descstart;
  1641. end = p + desclen;
  1642. for (; p < end;) {
  1643. if (p[0] < 2)
  1644. return NULL;
  1645. next = p + p[0];
  1646. if (next > end)
  1647. return NULL;
  1648. if (p[1] == dtype && (!after || (void *)p > after)) {
  1649. return p;
  1650. }
  1651. p = next;
  1652. }
  1653. return NULL;
  1654. }
  1655. /*
  1656. * find a class-specified interface descriptor with the given subtype.
  1657. */
  1658. void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
  1659. {
  1660. unsigned char *p = after;
  1661. while ((p = snd_usb_find_desc(buffer, buflen, p,
  1662. USB_DT_CS_INTERFACE)) != NULL) {
  1663. if (p[0] >= 3 && p[2] == dsubtype)
  1664. return p;
  1665. }
  1666. return NULL;
  1667. }
  1668. /*
  1669. * Wrapper for usb_control_msg().
  1670. * Allocates a temp buffer to prevent dmaing from/to the stack.
  1671. */
  1672. int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
  1673. __u8 requesttype, __u16 value, __u16 index, void *data,
  1674. __u16 size, int timeout)
  1675. {
  1676. int err;
  1677. void *buf = NULL;
  1678. if (size > 0) {
  1679. buf = kmalloc(size, GFP_KERNEL);
  1680. if (!buf)
  1681. return -ENOMEM;
  1682. memcpy(buf, data, size);
  1683. }
  1684. err = usb_control_msg(dev, pipe, request, requesttype,
  1685. value, index, buf, size, timeout);
  1686. if (size > 0) {
  1687. memcpy(data, buf, size);
  1688. kfree(buf);
  1689. }
  1690. return err;
  1691. }
  1692. /*
  1693. * entry point for linux usb interface
  1694. */
  1695. static int usb_audio_probe(struct usb_interface *intf,
  1696. const struct usb_device_id *id);
  1697. static void usb_audio_disconnect(struct usb_interface *intf);
  1698. static struct usb_device_id usb_audio_ids [] = {
  1699. #include "usbquirks.h"
  1700. { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
  1701. .bInterfaceClass = USB_CLASS_AUDIO,
  1702. .bInterfaceSubClass = USB_SUBCLASS_AUDIO_CONTROL },
  1703. { } /* Terminating entry */
  1704. };
  1705. MODULE_DEVICE_TABLE (usb, usb_audio_ids);
  1706. static struct usb_driver usb_audio_driver = {
  1707. .owner = THIS_MODULE,
  1708. .name = "snd-usb-audio",
  1709. .probe = usb_audio_probe,
  1710. .disconnect = usb_audio_disconnect,
  1711. .id_table = usb_audio_ids,
  1712. };
  1713. /*
  1714. * proc interface for list the supported pcm formats
  1715. */
  1716. static void proc_dump_substream_formats(snd_usb_substream_t *subs, snd_info_buffer_t *buffer)
  1717. {
  1718. struct list_head *p;
  1719. static char *sync_types[4] = {
  1720. "NONE", "ASYNC", "ADAPTIVE", "SYNC"
  1721. };
  1722. list_for_each(p, &subs->fmt_list) {
  1723. struct audioformat *fp;
  1724. fp = list_entry(p, struct audioformat, list);
  1725. snd_iprintf(buffer, " Interface %d\n", fp->iface);
  1726. snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
  1727. snd_iprintf(buffer, " Format: %s\n", snd_pcm_format_name(fp->format));
  1728. snd_iprintf(buffer, " Channels: %d\n", fp->channels);
  1729. snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
  1730. fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
  1731. fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
  1732. sync_types[(fp->ep_attr & EP_ATTR_MASK) >> 2]);
  1733. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
  1734. snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
  1735. fp->rate_min, fp->rate_max);
  1736. } else {
  1737. unsigned int i;
  1738. snd_iprintf(buffer, " Rates: ");
  1739. for (i = 0; i < fp->nr_rates; i++) {
  1740. if (i > 0)
  1741. snd_iprintf(buffer, ", ");
  1742. snd_iprintf(buffer, "%d", fp->rate_table[i]);
  1743. }
  1744. snd_iprintf(buffer, "\n");
  1745. }
  1746. // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
  1747. // snd_iprintf(buffer, " EP Attribute = 0x%x\n", fp->attributes);
  1748. }
  1749. }
  1750. static void proc_dump_substream_status(snd_usb_substream_t *subs, snd_info_buffer_t *buffer)
  1751. {
  1752. if (subs->running) {
  1753. unsigned int i;
  1754. snd_iprintf(buffer, " Status: Running\n");
  1755. snd_iprintf(buffer, " Interface = %d\n", subs->interface);
  1756. snd_iprintf(buffer, " Altset = %d\n", subs->format);
  1757. snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
  1758. for (i = 0; i < subs->nurbs; i++)
  1759. snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
  1760. snd_iprintf(buffer, "]\n");
  1761. snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
  1762. snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
  1763. snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
  1764. ? get_full_speed_hz(subs->freqm)
  1765. : get_high_speed_hz(subs->freqm),
  1766. subs->freqm >> 16, subs->freqm & 0xffff);
  1767. } else {
  1768. snd_iprintf(buffer, " Status: Stop\n");
  1769. }
  1770. }
  1771. static void proc_pcm_format_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
  1772. {
  1773. snd_usb_stream_t *stream = entry->private_data;
  1774. snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
  1775. if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
  1776. snd_iprintf(buffer, "\nPlayback:\n");
  1777. proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
  1778. proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
  1779. }
  1780. if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
  1781. snd_iprintf(buffer, "\nCapture:\n");
  1782. proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
  1783. proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
  1784. }
  1785. }
  1786. static void proc_pcm_format_add(snd_usb_stream_t *stream)
  1787. {
  1788. snd_info_entry_t *entry;
  1789. char name[32];
  1790. snd_card_t *card = stream->chip->card;
  1791. sprintf(name, "stream%d", stream->pcm_index);
  1792. if (! snd_card_proc_new(card, name, &entry))
  1793. snd_info_set_text_ops(entry, stream, 1024, proc_pcm_format_read);
  1794. }
  1795. /*
  1796. * initialize the substream instance.
  1797. */
  1798. static void init_substream(snd_usb_stream_t *as, int stream, struct audioformat *fp)
  1799. {
  1800. snd_usb_substream_t *subs = &as->substream[stream];
  1801. INIT_LIST_HEAD(&subs->fmt_list);
  1802. spin_lock_init(&subs->lock);
  1803. subs->stream = as;
  1804. subs->direction = stream;
  1805. subs->dev = as->chip->dev;
  1806. if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  1807. subs->ops = audio_urb_ops[stream];
  1808. else
  1809. subs->ops = audio_urb_ops_high_speed[stream];
  1810. snd_pcm_lib_preallocate_pages(as->pcm->streams[stream].substream,
  1811. SNDRV_DMA_TYPE_CONTINUOUS,
  1812. snd_dma_continuous_data(GFP_KERNEL),
  1813. 64 * 1024, 128 * 1024);
  1814. snd_pcm_set_ops(as->pcm, stream,
  1815. stream == SNDRV_PCM_STREAM_PLAYBACK ?
  1816. &snd_usb_playback_ops : &snd_usb_capture_ops);
  1817. list_add_tail(&fp->list, &subs->fmt_list);
  1818. subs->formats |= 1ULL << fp->format;
  1819. subs->endpoint = fp->endpoint;
  1820. subs->num_formats++;
  1821. subs->fmt_type = fp->fmt_type;
  1822. }
  1823. /*
  1824. * free a substream
  1825. */
  1826. static void free_substream(snd_usb_substream_t *subs)
  1827. {
  1828. struct list_head *p, *n;
  1829. if (! subs->num_formats)
  1830. return; /* not initialized */
  1831. list_for_each_safe(p, n, &subs->fmt_list) {
  1832. struct audioformat *fp = list_entry(p, struct audioformat, list);
  1833. kfree(fp->rate_table);
  1834. kfree(fp);
  1835. }
  1836. }
  1837. /*
  1838. * free a usb stream instance
  1839. */
  1840. static void snd_usb_audio_stream_free(snd_usb_stream_t *stream)
  1841. {
  1842. free_substream(&stream->substream[0]);
  1843. free_substream(&stream->substream[1]);
  1844. list_del(&stream->list);
  1845. kfree(stream);
  1846. }
  1847. static void snd_usb_audio_pcm_free(snd_pcm_t *pcm)
  1848. {
  1849. snd_usb_stream_t *stream = pcm->private_data;
  1850. if (stream) {
  1851. stream->pcm = NULL;
  1852. snd_pcm_lib_preallocate_free_for_all(pcm);
  1853. snd_usb_audio_stream_free(stream);
  1854. }
  1855. }
  1856. /*
  1857. * add this endpoint to the chip instance.
  1858. * if a stream with the same endpoint already exists, append to it.
  1859. * if not, create a new pcm stream.
  1860. */
  1861. static int add_audio_endpoint(snd_usb_audio_t *chip, int stream, struct audioformat *fp)
  1862. {
  1863. struct list_head *p;
  1864. snd_usb_stream_t *as;
  1865. snd_usb_substream_t *subs;
  1866. snd_pcm_t *pcm;
  1867. int err;
  1868. list_for_each(p, &chip->pcm_list) {
  1869. as = list_entry(p, snd_usb_stream_t, list);
  1870. if (as->fmt_type != fp->fmt_type)
  1871. continue;
  1872. subs = &as->substream[stream];
  1873. if (! subs->endpoint)
  1874. continue;
  1875. if (subs->endpoint == fp->endpoint) {
  1876. list_add_tail(&fp->list, &subs->fmt_list);
  1877. subs->num_formats++;
  1878. subs->formats |= 1ULL << fp->format;
  1879. return 0;
  1880. }
  1881. }
  1882. /* look for an empty stream */
  1883. list_for_each(p, &chip->pcm_list) {
  1884. as = list_entry(p, snd_usb_stream_t, list);
  1885. if (as->fmt_type != fp->fmt_type)
  1886. continue;
  1887. subs = &as->substream[stream];
  1888. if (subs->endpoint)
  1889. continue;
  1890. err = snd_pcm_new_stream(as->pcm, stream, 1);
  1891. if (err < 0)
  1892. return err;
  1893. init_substream(as, stream, fp);
  1894. return 0;
  1895. }
  1896. /* create a new pcm */
  1897. as = kmalloc(sizeof(*as), GFP_KERNEL);
  1898. if (! as)
  1899. return -ENOMEM;
  1900. memset(as, 0, sizeof(*as));
  1901. as->pcm_index = chip->pcm_devs;
  1902. as->chip = chip;
  1903. as->fmt_type = fp->fmt_type;
  1904. err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
  1905. stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
  1906. stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
  1907. &pcm);
  1908. if (err < 0) {
  1909. kfree(as);
  1910. return err;
  1911. }
  1912. as->pcm = pcm;
  1913. pcm->private_data = as;
  1914. pcm->private_free = snd_usb_audio_pcm_free;
  1915. pcm->info_flags = 0;
  1916. if (chip->pcm_devs > 0)
  1917. sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
  1918. else
  1919. strcpy(pcm->name, "USB Audio");
  1920. init_substream(as, stream, fp);
  1921. list_add(&as->list, &chip->pcm_list);
  1922. chip->pcm_devs++;
  1923. proc_pcm_format_add(as);
  1924. return 0;
  1925. }
  1926. /*
  1927. * check if the device uses big-endian samples
  1928. */
  1929. static int is_big_endian_format(snd_usb_audio_t *chip, struct audioformat *fp)
  1930. {
  1931. switch (chip->usb_id) {
  1932. case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
  1933. if (fp->endpoint & USB_DIR_IN)
  1934. return 1;
  1935. break;
  1936. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  1937. return 1;
  1938. }
  1939. return 0;
  1940. }
  1941. /*
  1942. * parse the audio format type I descriptor
  1943. * and returns the corresponding pcm format
  1944. *
  1945. * @dev: usb device
  1946. * @fp: audioformat record
  1947. * @format: the format tag (wFormatTag)
  1948. * @fmt: the format type descriptor
  1949. */
  1950. static int parse_audio_format_i_type(snd_usb_audio_t *chip, struct audioformat *fp,
  1951. int format, unsigned char *fmt)
  1952. {
  1953. int pcm_format;
  1954. int sample_width, sample_bytes;
  1955. /* FIXME: correct endianess and sign? */
  1956. pcm_format = -1;
  1957. sample_width = fmt[6];
  1958. sample_bytes = fmt[5];
  1959. switch (format) {
  1960. case 0: /* some devices don't define this correctly... */
  1961. snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
  1962. chip->dev->devnum, fp->iface, fp->altsetting);
  1963. /* fall-through */
  1964. case USB_AUDIO_FORMAT_PCM:
  1965. if (sample_width > sample_bytes * 8) {
  1966. snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
  1967. chip->dev->devnum, fp->iface, fp->altsetting,
  1968. sample_width, sample_bytes);
  1969. }
  1970. /* check the format byte size */
  1971. switch (fmt[5]) {
  1972. case 1:
  1973. pcm_format = SNDRV_PCM_FORMAT_S8;
  1974. break;
  1975. case 2:
  1976. if (is_big_endian_format(chip, fp))
  1977. pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
  1978. else
  1979. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  1980. break;
  1981. case 3:
  1982. if (is_big_endian_format(chip, fp))
  1983. pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
  1984. else
  1985. pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
  1986. break;
  1987. case 4:
  1988. pcm_format = SNDRV_PCM_FORMAT_S32_LE;
  1989. break;
  1990. default:
  1991. snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
  1992. chip->dev->devnum, fp->iface,
  1993. fp->altsetting, sample_width, sample_bytes);
  1994. break;
  1995. }
  1996. break;
  1997. case USB_AUDIO_FORMAT_PCM8:
  1998. /* Dallas DS4201 workaround */
  1999. if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  2000. pcm_format = SNDRV_PCM_FORMAT_S8;
  2001. else
  2002. pcm_format = SNDRV_PCM_FORMAT_U8;
  2003. break;
  2004. case USB_AUDIO_FORMAT_IEEE_FLOAT:
  2005. pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
  2006. break;
  2007. case USB_AUDIO_FORMAT_ALAW:
  2008. pcm_format = SNDRV_PCM_FORMAT_A_LAW;
  2009. break;
  2010. case USB_AUDIO_FORMAT_MU_LAW:
  2011. pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
  2012. break;
  2013. default:
  2014. snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
  2015. chip->dev->devnum, fp->iface, fp->altsetting, format);
  2016. break;
  2017. }
  2018. return pcm_format;
  2019. }
  2020. /*
  2021. * parse the format descriptor and stores the possible sample rates
  2022. * on the audioformat table.
  2023. *
  2024. * @dev: usb device
  2025. * @fp: audioformat record
  2026. * @fmt: the format descriptor
  2027. * @offset: the start offset of descriptor pointing the rate type
  2028. * (7 for type I and II, 8 for type II)
  2029. */
  2030. static int parse_audio_format_rates(snd_usb_audio_t *chip, struct audioformat *fp,
  2031. unsigned char *fmt, int offset)
  2032. {
  2033. int nr_rates = fmt[offset];
  2034. if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
  2035. snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
  2036. chip->dev->devnum, fp->iface, fp->altsetting);
  2037. return -1;
  2038. }
  2039. if (nr_rates) {
  2040. /*
  2041. * build the rate table and bitmap flags
  2042. */
  2043. int r, idx, c;
  2044. /* this table corresponds to the SNDRV_PCM_RATE_XXX bit */
  2045. static unsigned int conv_rates[] = {
  2046. 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000,
  2047. 64000, 88200, 96000, 176400, 192000
  2048. };
  2049. fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
  2050. if (fp->rate_table == NULL) {
  2051. snd_printk(KERN_ERR "cannot malloc\n");
  2052. return -1;
  2053. }
  2054. fp->nr_rates = nr_rates;
  2055. fp->rate_min = fp->rate_max = combine_triple(&fmt[8]);
  2056. for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
  2057. unsigned int rate = fp->rate_table[r] = combine_triple(&fmt[idx]);
  2058. if (rate < fp->rate_min)
  2059. fp->rate_min = rate;
  2060. else if (rate > fp->rate_max)
  2061. fp->rate_max = rate;
  2062. for (c = 0; c < (int)ARRAY_SIZE(conv_rates); c++) {
  2063. if (rate == conv_rates[c]) {
  2064. fp->rates |= (1 << c);
  2065. break;
  2066. }
  2067. }
  2068. }
  2069. } else {
  2070. /* continuous rates */
  2071. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  2072. fp->rate_min = combine_triple(&fmt[offset + 1]);
  2073. fp->rate_max = combine_triple(&fmt[offset + 4]);
  2074. }
  2075. return 0;
  2076. }
  2077. /*
  2078. * parse the format type I and III descriptors
  2079. */
  2080. static int parse_audio_format_i(snd_usb_audio_t *chip, struct audioformat *fp,
  2081. int format, unsigned char *fmt)
  2082. {
  2083. int pcm_format;
  2084. if (fmt[3] == USB_FORMAT_TYPE_III) {
  2085. /* FIXME: the format type is really IECxxx
  2086. * but we give normal PCM format to get the existing
  2087. * apps working...
  2088. */
  2089. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  2090. } else {
  2091. pcm_format = parse_audio_format_i_type(chip, fp, format, fmt);
  2092. if (pcm_format < 0)
  2093. return -1;
  2094. }
  2095. fp->format = pcm_format;
  2096. fp->channels = fmt[4];
  2097. if (fp->channels < 1) {
  2098. snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
  2099. chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
  2100. return -1;
  2101. }
  2102. return parse_audio_format_rates(chip, fp, fmt, 7);
  2103. }
  2104. /*
  2105. * prase the format type II descriptor
  2106. */
  2107. static int parse_audio_format_ii(snd_usb_audio_t *chip, struct audioformat *fp,
  2108. int format, unsigned char *fmt)
  2109. {
  2110. int brate, framesize;
  2111. switch (format) {
  2112. case USB_AUDIO_FORMAT_AC3:
  2113. /* FIXME: there is no AC3 format defined yet */
  2114. // fp->format = SNDRV_PCM_FORMAT_AC3;
  2115. fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
  2116. break;
  2117. case USB_AUDIO_FORMAT_MPEG:
  2118. fp->format = SNDRV_PCM_FORMAT_MPEG;
  2119. break;
  2120. default:
  2121. snd_printd(KERN_INFO "%d:%u:%d : unknown format tag 0x%x is detected. processed as MPEG.\n",
  2122. chip->dev->devnum, fp->iface, fp->altsetting, format);
  2123. fp->format = SNDRV_PCM_FORMAT_MPEG;
  2124. break;
  2125. }
  2126. fp->channels = 1;
  2127. brate = combine_word(&fmt[4]); /* fmt[4,5] : wMaxBitRate (in kbps) */
  2128. framesize = combine_word(&fmt[6]); /* fmt[6,7]: wSamplesPerFrame */
  2129. snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  2130. fp->frame_size = framesize;
  2131. return parse_audio_format_rates(chip, fp, fmt, 8); /* fmt[8..] sample rates */
  2132. }
  2133. static int parse_audio_format(snd_usb_audio_t *chip, struct audioformat *fp,
  2134. int format, unsigned char *fmt, int stream)
  2135. {
  2136. int err;
  2137. switch (fmt[3]) {
  2138. case USB_FORMAT_TYPE_I:
  2139. case USB_FORMAT_TYPE_III:
  2140. err = parse_audio_format_i(chip, fp, format, fmt);
  2141. break;
  2142. case USB_FORMAT_TYPE_II:
  2143. err = parse_audio_format_ii(chip, fp, format, fmt);
  2144. break;
  2145. default:
  2146. snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
  2147. chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);
  2148. return -1;
  2149. }
  2150. fp->fmt_type = fmt[3];
  2151. if (err < 0)
  2152. return err;
  2153. #if 1
  2154. /* FIXME: temporary hack for extigy/audigy 2 nx */
  2155. /* extigy apparently supports sample rates other than 48k
  2156. * but not in ordinary way. so we enable only 48k atm.
  2157. */
  2158. if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
  2159. chip->usb_id == USB_ID(0x041e, 0x3020)) {
  2160. if (fmt[3] == USB_FORMAT_TYPE_I &&
  2161. stream == SNDRV_PCM_STREAM_PLAYBACK &&
  2162. fp->rates != SNDRV_PCM_RATE_48000 &&
  2163. fp->rates != SNDRV_PCM_RATE_96000)
  2164. return -1; /* use 48k only */
  2165. }
  2166. #endif
  2167. return 0;
  2168. }
  2169. static int parse_audio_endpoints(snd_usb_audio_t *chip, int iface_no)
  2170. {
  2171. struct usb_device *dev;
  2172. struct usb_interface *iface;
  2173. struct usb_host_interface *alts;
  2174. struct usb_interface_descriptor *altsd;
  2175. int i, altno, err, stream;
  2176. int format;
  2177. struct audioformat *fp;
  2178. unsigned char *fmt, *csep;
  2179. dev = chip->dev;
  2180. /* parse the interface's altsettings */
  2181. iface = usb_ifnum_to_if(dev, iface_no);
  2182. for (i = 0; i < iface->num_altsetting; i++) {
  2183. alts = &iface->altsetting[i];
  2184. altsd = get_iface_desc(alts);
  2185. /* skip invalid one */
  2186. if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  2187. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  2188. (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING &&
  2189. altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
  2190. altsd->bNumEndpoints < 1 ||
  2191. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
  2192. continue;
  2193. /* must be isochronous */
  2194. if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  2195. USB_ENDPOINT_XFER_ISOC)
  2196. continue;
  2197. /* check direction */
  2198. stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
  2199. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  2200. altno = altsd->bAlternateSetting;
  2201. /* get audio formats */
  2202. fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, AS_GENERAL);
  2203. if (!fmt) {
  2204. snd_printk(KERN_ERR "%d:%u:%d : AS_GENERAL descriptor not found\n",
  2205. dev->devnum, iface_no, altno);
  2206. continue;
  2207. }
  2208. if (fmt[0] < 7) {
  2209. snd_printk(KERN_ERR "%d:%u:%d : invalid AS_GENERAL desc\n",
  2210. dev->devnum, iface_no, altno);
  2211. continue;
  2212. }
  2213. format = (fmt[6] << 8) | fmt[5]; /* remember the format value */
  2214. /* get format type */
  2215. fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, FORMAT_TYPE);
  2216. if (!fmt) {
  2217. snd_printk(KERN_ERR "%d:%u:%d : no FORMAT_TYPE desc\n",
  2218. dev->devnum, iface_no, altno);
  2219. continue;
  2220. }
  2221. if (fmt[0] < 8) {
  2222. snd_printk(KERN_ERR "%d:%u:%d : invalid FORMAT_TYPE desc\n",
  2223. dev->devnum, iface_no, altno);
  2224. continue;
  2225. }
  2226. csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
  2227. /* Creamware Noah has this descriptor after the 2nd endpoint */
  2228. if (!csep && altsd->bNumEndpoints >= 2)
  2229. csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
  2230. if (!csep || csep[0] < 7 || csep[2] != EP_GENERAL) {
  2231. snd_printk(KERN_ERR "%d:%u:%d : no or invalid class specific endpoint descriptor\n",
  2232. dev->devnum, iface_no, altno);
  2233. continue;
  2234. }
  2235. fp = kmalloc(sizeof(*fp), GFP_KERNEL);
  2236. if (! fp) {
  2237. snd_printk(KERN_ERR "cannot malloc\n");
  2238. return -ENOMEM;
  2239. }
  2240. memset(fp, 0, sizeof(*fp));
  2241. fp->iface = iface_no;
  2242. fp->altsetting = altno;
  2243. fp->altset_idx = i;
  2244. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  2245. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  2246. /* FIXME: decode wMaxPacketSize of high bandwith endpoints */
  2247. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  2248. fp->attributes = csep[3];
  2249. /* some quirks for attributes here */
  2250. switch (chip->usb_id) {
  2251. case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
  2252. /* Optoplay sets the sample rate attribute although
  2253. * it seems not supporting it in fact.
  2254. */
  2255. fp->attributes &= ~EP_CS_ATTR_SAMPLE_RATE;
  2256. break;
  2257. case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
  2258. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  2259. /* doesn't set the sample rate attribute, but supports it */
  2260. fp->attributes |= EP_CS_ATTR_SAMPLE_RATE;
  2261. break;
  2262. case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
  2263. case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
  2264. an older model 77d:223) */
  2265. /*
  2266. * plantronics headset and Griffin iMic have set adaptive-in
  2267. * although it's really not...
  2268. */
  2269. fp->ep_attr &= ~EP_ATTR_MASK;
  2270. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  2271. fp->ep_attr |= EP_ATTR_ADAPTIVE;
  2272. else
  2273. fp->ep_attr |= EP_ATTR_SYNC;
  2274. break;
  2275. }
  2276. /* ok, let's parse further... */
  2277. if (parse_audio_format(chip, fp, format, fmt, stream) < 0) {
  2278. kfree(fp->rate_table);
  2279. kfree(fp);
  2280. continue;
  2281. }
  2282. snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint 0x%x\n", dev->devnum, iface_no, i, fp->endpoint);
  2283. err = add_audio_endpoint(chip, stream, fp);
  2284. if (err < 0) {
  2285. kfree(fp->rate_table);
  2286. kfree(fp);
  2287. return err;
  2288. }
  2289. /* try to set the interface... */
  2290. usb_set_interface(chip->dev, iface_no, altno);
  2291. init_usb_pitch(chip->dev, iface_no, alts, fp);
  2292. init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
  2293. }
  2294. return 0;
  2295. }
  2296. /*
  2297. * disconnect streams
  2298. * called from snd_usb_audio_disconnect()
  2299. */
  2300. static void snd_usb_stream_disconnect(struct list_head *head)
  2301. {
  2302. int idx;
  2303. snd_usb_stream_t *as;
  2304. snd_usb_substream_t *subs;
  2305. as = list_entry(head, snd_usb_stream_t, list);
  2306. for (idx = 0; idx < 2; idx++) {
  2307. subs = &as->substream[idx];
  2308. if (!subs->num_formats)
  2309. return;
  2310. release_substream_urbs(subs, 1);
  2311. subs->interface = -1;
  2312. }
  2313. }
  2314. /*
  2315. * parse audio control descriptor and create pcm/midi streams
  2316. */
  2317. static int snd_usb_create_streams(snd_usb_audio_t *chip, int ctrlif)
  2318. {
  2319. struct usb_device *dev = chip->dev;
  2320. struct usb_host_interface *host_iface;
  2321. struct usb_interface *iface;
  2322. unsigned char *p1;
  2323. int i, j;
  2324. /* find audiocontrol interface */
  2325. host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
  2326. if (!(p1 = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen, NULL, HEADER))) {
  2327. snd_printk(KERN_ERR "cannot find HEADER\n");
  2328. return -EINVAL;
  2329. }
  2330. if (! p1[7] || p1[0] < 8 + p1[7]) {
  2331. snd_printk(KERN_ERR "invalid HEADER\n");
  2332. return -EINVAL;
  2333. }
  2334. /*
  2335. * parse all USB audio streaming interfaces
  2336. */
  2337. for (i = 0; i < p1[7]; i++) {
  2338. struct usb_host_interface *alts;
  2339. struct usb_interface_descriptor *altsd;
  2340. j = p1[8 + i];
  2341. iface = usb_ifnum_to_if(dev, j);
  2342. if (!iface) {
  2343. snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
  2344. dev->devnum, ctrlif, j);
  2345. continue;
  2346. }
  2347. if (usb_interface_claimed(iface)) {
  2348. snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n", dev->devnum, ctrlif, j);
  2349. continue;
  2350. }
  2351. alts = &iface->altsetting[0];
  2352. altsd = get_iface_desc(alts);
  2353. if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
  2354. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
  2355. altsd->bInterfaceSubClass == USB_SUBCLASS_MIDI_STREAMING) {
  2356. if (snd_usb_create_midi_interface(chip, iface, NULL) < 0) {
  2357. snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n", dev->devnum, ctrlif, j);
  2358. continue;
  2359. }
  2360. usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
  2361. continue;
  2362. }
  2363. if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  2364. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  2365. altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIO_STREAMING) {
  2366. snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n", dev->devnum, ctrlif, j, altsd->bInterfaceClass);
  2367. /* skip non-supported classes */
  2368. continue;
  2369. }
  2370. if (! parse_audio_endpoints(chip, j)) {
  2371. usb_set_interface(dev, j, 0); /* reset the current interface */
  2372. usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
  2373. }
  2374. }
  2375. return 0;
  2376. }
  2377. /*
  2378. * create a stream for an endpoint/altsetting without proper descriptors
  2379. */
  2380. static int create_fixed_stream_quirk(snd_usb_audio_t *chip,
  2381. struct usb_interface *iface,
  2382. const snd_usb_audio_quirk_t *quirk)
  2383. {
  2384. struct audioformat *fp;
  2385. struct usb_host_interface *alts;
  2386. int stream, err;
  2387. int *rate_table = NULL;
  2388. fp = kmalloc(sizeof(*fp), GFP_KERNEL);
  2389. if (! fp) {
  2390. snd_printk(KERN_ERR "cannot malloc\n");
  2391. return -ENOMEM;
  2392. }
  2393. memcpy(fp, quirk->data, sizeof(*fp));
  2394. if (fp->nr_rates > 0) {
  2395. rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
  2396. if (!rate_table) {
  2397. kfree(fp);
  2398. return -ENOMEM;
  2399. }
  2400. memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
  2401. fp->rate_table = rate_table;
  2402. }
  2403. stream = (fp->endpoint & USB_DIR_IN)
  2404. ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  2405. err = add_audio_endpoint(chip, stream, fp);
  2406. if (err < 0) {
  2407. kfree(fp);
  2408. kfree(rate_table);
  2409. return err;
  2410. }
  2411. if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
  2412. fp->altset_idx >= iface->num_altsetting) {
  2413. kfree(fp);
  2414. kfree(rate_table);
  2415. return -EINVAL;
  2416. }
  2417. alts = &iface->altsetting[fp->altset_idx];
  2418. usb_set_interface(chip->dev, fp->iface, 0);
  2419. init_usb_pitch(chip->dev, fp->iface, alts, fp);
  2420. init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
  2421. return 0;
  2422. }
  2423. /*
  2424. * create a stream for an interface with proper descriptors
  2425. */
  2426. static int create_standard_interface_quirk(snd_usb_audio_t *chip,
  2427. struct usb_interface *iface,
  2428. const snd_usb_audio_quirk_t *quirk)
  2429. {
  2430. struct usb_host_interface *alts;
  2431. struct usb_interface_descriptor *altsd;
  2432. int err;
  2433. alts = &iface->altsetting[0];
  2434. altsd = get_iface_desc(alts);
  2435. switch (quirk->type) {
  2436. case QUIRK_AUDIO_STANDARD_INTERFACE:
  2437. err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
  2438. if (!err)
  2439. usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0); /* reset the current interface */
  2440. break;
  2441. case QUIRK_MIDI_STANDARD_INTERFACE:
  2442. err = snd_usb_create_midi_interface(chip, iface, NULL);
  2443. break;
  2444. default:
  2445. snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
  2446. return -ENXIO;
  2447. }
  2448. if (err < 0) {
  2449. snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
  2450. altsd->bInterfaceNumber, err);
  2451. return err;
  2452. }
  2453. return 0;
  2454. }
  2455. /*
  2456. * Create a stream for an Edirol UA-700/UA-25 interface. The only way
  2457. * to detect the sample rate is by looking at wMaxPacketSize.
  2458. */
  2459. static int create_ua700_ua25_quirk(snd_usb_audio_t *chip,
  2460. struct usb_interface *iface)
  2461. {
  2462. static const struct audioformat ua_format = {
  2463. .format = SNDRV_PCM_FORMAT_S24_3LE,
  2464. .channels = 2,
  2465. .fmt_type = USB_FORMAT_TYPE_I,
  2466. .altsetting = 1,
  2467. .altset_idx = 1,
  2468. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  2469. };
  2470. struct usb_host_interface *alts;
  2471. struct usb_interface_descriptor *altsd;
  2472. struct audioformat *fp;
  2473. int stream, err;
  2474. /* both PCM and MIDI interfaces have 2 altsettings */
  2475. if (iface->num_altsetting != 2)
  2476. return -ENXIO;
  2477. alts = &iface->altsetting[1];
  2478. altsd = get_iface_desc(alts);
  2479. if (altsd->bNumEndpoints == 2) {
  2480. static const snd_usb_midi_endpoint_info_t ua700_ep = {
  2481. .out_cables = 0x0003,
  2482. .in_cables = 0x0003
  2483. };
  2484. static const snd_usb_audio_quirk_t ua700_quirk = {
  2485. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  2486. .data = &ua700_ep
  2487. };
  2488. static const snd_usb_midi_endpoint_info_t ua25_ep = {
  2489. .out_cables = 0x0001,
  2490. .in_cables = 0x0001
  2491. };
  2492. static const snd_usb_audio_quirk_t ua25_quirk = {
  2493. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  2494. .data = &ua25_ep
  2495. };
  2496. if (chip->usb_id == USB_ID(0x0582, 0x002b))
  2497. return snd_usb_create_midi_interface(chip, iface,
  2498. &ua700_quirk);
  2499. else
  2500. return snd_usb_create_midi_interface(chip, iface,
  2501. &ua25_quirk);
  2502. }
  2503. if (altsd->bNumEndpoints != 1)
  2504. return -ENXIO;
  2505. fp = kmalloc(sizeof(*fp), GFP_KERNEL);
  2506. if (!fp)
  2507. return -ENOMEM;
  2508. memcpy(fp, &ua_format, sizeof(*fp));
  2509. fp->iface = altsd->bInterfaceNumber;
  2510. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  2511. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  2512. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  2513. switch (fp->maxpacksize) {
  2514. case 0x120:
  2515. fp->rate_max = fp->rate_min = 44100;
  2516. break;
  2517. case 0x138:
  2518. case 0x140:
  2519. fp->rate_max = fp->rate_min = 48000;
  2520. break;
  2521. case 0x258:
  2522. case 0x260:
  2523. fp->rate_max = fp->rate_min = 96000;
  2524. break;
  2525. default:
  2526. snd_printk(KERN_ERR "unknown sample rate\n");
  2527. kfree(fp);
  2528. return -ENXIO;
  2529. }
  2530. stream = (fp->endpoint & USB_DIR_IN)
  2531. ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  2532. err = add_audio_endpoint(chip, stream, fp);
  2533. if (err < 0) {
  2534. kfree(fp);
  2535. return err;
  2536. }
  2537. usb_set_interface(chip->dev, fp->iface, 0);
  2538. return 0;
  2539. }
  2540. /*
  2541. * Create a stream for an Edirol UA-1000 interface.
  2542. */
  2543. static int create_ua1000_quirk(snd_usb_audio_t *chip, struct usb_interface *iface)
  2544. {
  2545. static const struct audioformat ua1000_format = {
  2546. .format = SNDRV_PCM_FORMAT_S32_LE,
  2547. .fmt_type = USB_FORMAT_TYPE_I,
  2548. .altsetting = 1,
  2549. .altset_idx = 1,
  2550. .attributes = 0,
  2551. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  2552. };
  2553. struct usb_host_interface *alts;
  2554. struct usb_interface_descriptor *altsd;
  2555. struct audioformat *fp;
  2556. int stream, err;
  2557. if (iface->num_altsetting != 2)
  2558. return -ENXIO;
  2559. alts = &iface->altsetting[1];
  2560. altsd = get_iface_desc(alts);
  2561. if (alts->extralen != 11 || alts->extra[1] != CS_AUDIO_INTERFACE ||
  2562. altsd->bNumEndpoints != 1)
  2563. return -ENXIO;
  2564. fp = kmalloc(sizeof(*fp), GFP_KERNEL);
  2565. if (!fp)
  2566. return -ENOMEM;
  2567. memcpy(fp, &ua1000_format, sizeof(*fp));
  2568. fp->channels = alts->extra[4];
  2569. fp->iface = altsd->bInterfaceNumber;
  2570. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  2571. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  2572. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  2573. fp->rate_max = fp->rate_min = combine_triple(&alts->extra[8]);
  2574. stream = (fp->endpoint & USB_DIR_IN)
  2575. ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  2576. err = add_audio_endpoint(chip, stream, fp);
  2577. if (err < 0) {
  2578. kfree(fp);
  2579. return err;
  2580. }
  2581. /* FIXME: playback must be synchronized to capture */
  2582. usb_set_interface(chip->dev, fp->iface, 0);
  2583. return 0;
  2584. }
  2585. static int snd_usb_create_quirk(snd_usb_audio_t *chip,
  2586. struct usb_interface *iface,
  2587. const snd_usb_audio_quirk_t *quirk);
  2588. /*
  2589. * handle the quirks for the contained interfaces
  2590. */
  2591. static int create_composite_quirk(snd_usb_audio_t *chip,
  2592. struct usb_interface *iface,
  2593. const snd_usb_audio_quirk_t *quirk)
  2594. {
  2595. int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
  2596. int err;
  2597. for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
  2598. iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
  2599. if (!iface)
  2600. continue;
  2601. if (quirk->ifnum != probed_ifnum &&
  2602. usb_interface_claimed(iface))
  2603. continue;
  2604. err = snd_usb_create_quirk(chip, iface, quirk);
  2605. if (err < 0)
  2606. return err;
  2607. if (quirk->ifnum != probed_ifnum)
  2608. usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
  2609. }
  2610. return 0;
  2611. }
  2612. /*
  2613. * boot quirks
  2614. */
  2615. #define EXTIGY_FIRMWARE_SIZE_OLD 794
  2616. #define EXTIGY_FIRMWARE_SIZE_NEW 483
  2617. static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
  2618. {
  2619. struct usb_host_config *config = dev->actconfig;
  2620. int err;
  2621. if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
  2622. le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
  2623. snd_printdd("sending Extigy boot sequence...\n");
  2624. /* Send message to force it to reconnect with full interface. */
  2625. err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
  2626. 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
  2627. if (err < 0) snd_printdd("error sending boot message: %d\n", err);
  2628. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
  2629. &dev->descriptor, sizeof(dev->descriptor));
  2630. config = dev->actconfig;
  2631. if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
  2632. err = usb_reset_configuration(dev);
  2633. if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
  2634. snd_printdd("extigy_boot: new boot length = %d\n",
  2635. le16_to_cpu(get_cfg_desc(config)->wTotalLength));
  2636. return -ENODEV; /* quit this anyway */
  2637. }
  2638. return 0;
  2639. }
  2640. static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
  2641. {
  2642. #if 0
  2643. /* TODO: enable this when high speed synchronization actually works */
  2644. u8 buf = 1;
  2645. snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
  2646. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  2647. 0, 0, &buf, 1, 1000);
  2648. if (buf == 0) {
  2649. snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
  2650. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  2651. 1, 2000, NULL, 0, 1000);
  2652. return -ENODEV;
  2653. }
  2654. #endif
  2655. return 0;
  2656. }
  2657. /*
  2658. * audio-interface quirks
  2659. *
  2660. * returns zero if no standard audio/MIDI parsing is needed.
  2661. * returns a postive value if standard audio/midi interfaces are parsed
  2662. * after this.
  2663. * returns a negative value at error.
  2664. */
  2665. static int snd_usb_create_quirk(snd_usb_audio_t *chip,
  2666. struct usb_interface *iface,
  2667. const snd_usb_audio_quirk_t *quirk)
  2668. {
  2669. switch (quirk->type) {
  2670. case QUIRK_MIDI_FIXED_ENDPOINT:
  2671. case QUIRK_MIDI_YAMAHA:
  2672. case QUIRK_MIDI_MIDIMAN:
  2673. case QUIRK_MIDI_NOVATION:
  2674. case QUIRK_MIDI_MOTU:
  2675. case QUIRK_MIDI_EMAGIC:
  2676. return snd_usb_create_midi_interface(chip, iface, quirk);
  2677. case QUIRK_COMPOSITE:
  2678. return create_composite_quirk(chip, iface, quirk);
  2679. case QUIRK_AUDIO_FIXED_ENDPOINT:
  2680. return create_fixed_stream_quirk(chip, iface, quirk);
  2681. case QUIRK_AUDIO_STANDARD_INTERFACE:
  2682. case QUIRK_MIDI_STANDARD_INTERFACE:
  2683. return create_standard_interface_quirk(chip, iface, quirk);
  2684. case QUIRK_AUDIO_EDIROL_UA700_UA25:
  2685. return create_ua700_ua25_quirk(chip, iface);
  2686. case QUIRK_AUDIO_EDIROL_UA1000:
  2687. return create_ua1000_quirk(chip, iface);
  2688. case QUIRK_IGNORE_INTERFACE:
  2689. return 0;
  2690. default:
  2691. snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
  2692. return -ENXIO;
  2693. }
  2694. }
  2695. /*
  2696. * common proc files to show the usb device info
  2697. */
  2698. static void proc_audio_usbbus_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
  2699. {
  2700. snd_usb_audio_t *chip = entry->private_data;
  2701. if (! chip->shutdown)
  2702. snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
  2703. }
  2704. static void proc_audio_usbid_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
  2705. {
  2706. snd_usb_audio_t *chip = entry->private_data;
  2707. if (! chip->shutdown)
  2708. snd_iprintf(buffer, "%04x:%04x\n",
  2709. USB_ID_VENDOR(chip->usb_id),
  2710. USB_ID_PRODUCT(chip->usb_id));
  2711. }
  2712. static void snd_usb_audio_create_proc(snd_usb_audio_t *chip)
  2713. {
  2714. snd_info_entry_t *entry;
  2715. if (! snd_card_proc_new(chip->card, "usbbus", &entry))
  2716. snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbbus_read);
  2717. if (! snd_card_proc_new(chip->card, "usbid", &entry))
  2718. snd_info_set_text_ops(entry, chip, 1024, proc_audio_usbid_read);
  2719. }
  2720. /*
  2721. * free the chip instance
  2722. *
  2723. * here we have to do not much, since pcm and controls are already freed
  2724. *
  2725. */
  2726. static int snd_usb_audio_free(snd_usb_audio_t *chip)
  2727. {
  2728. kfree(chip);
  2729. return 0;
  2730. }
  2731. static int snd_usb_audio_dev_free(snd_device_t *device)
  2732. {
  2733. snd_usb_audio_t *chip = device->device_data;
  2734. return snd_usb_audio_free(chip);
  2735. }
  2736. /*
  2737. * create a chip instance and set its names.
  2738. */
  2739. static int snd_usb_audio_create(struct usb_device *dev, int idx,
  2740. const snd_usb_audio_quirk_t *quirk,
  2741. snd_usb_audio_t **rchip)
  2742. {
  2743. snd_card_t *card;
  2744. snd_usb_audio_t *chip;
  2745. int err, len;
  2746. char component[14];
  2747. static snd_device_ops_t ops = {
  2748. .dev_free = snd_usb_audio_dev_free,
  2749. };
  2750. *rchip = NULL;
  2751. if (snd_usb_get_speed(dev) != USB_SPEED_FULL &&
  2752. snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
  2753. snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
  2754. return -ENXIO;
  2755. }
  2756. card = snd_card_new(index[idx], id[idx], THIS_MODULE, 0);
  2757. if (card == NULL) {
  2758. snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
  2759. return -ENOMEM;
  2760. }
  2761. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  2762. if (! chip) {
  2763. snd_card_free(card);
  2764. return -ENOMEM;
  2765. }
  2766. chip->index = idx;
  2767. chip->dev = dev;
  2768. chip->card = card;
  2769. chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  2770. le16_to_cpu(dev->descriptor.idProduct));
  2771. INIT_LIST_HEAD(&chip->pcm_list);
  2772. INIT_LIST_HEAD(&chip->midi_list);
  2773. INIT_LIST_HEAD(&chip->mixer_list);
  2774. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  2775. snd_usb_audio_free(chip);
  2776. snd_card_free(card);
  2777. return err;
  2778. }
  2779. strcpy(card->driver, "USB-Audio");
  2780. sprintf(component, "USB%04x:%04x",
  2781. USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
  2782. snd_component_add(card, component);
  2783. /* retrieve the device string as shortname */
  2784. if (quirk && quirk->product_name) {
  2785. strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
  2786. } else {
  2787. if (!dev->descriptor.iProduct ||
  2788. usb_string(dev, dev->descriptor.iProduct,
  2789. card->shortname, sizeof(card->shortname)) <= 0) {
  2790. /* no name available from anywhere, so use ID */
  2791. sprintf(card->shortname, "USB Device %#04x:%#04x",
  2792. USB_ID_VENDOR(chip->usb_id),
  2793. USB_ID_PRODUCT(chip->usb_id));
  2794. }
  2795. }
  2796. /* retrieve the vendor and device strings as longname */
  2797. if (quirk && quirk->vendor_name) {
  2798. len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
  2799. } else {
  2800. if (dev->descriptor.iManufacturer)
  2801. len = usb_string(dev, dev->descriptor.iManufacturer,
  2802. card->longname, sizeof(card->longname));
  2803. else
  2804. len = 0;
  2805. /* we don't really care if there isn't any vendor string */
  2806. }
  2807. if (len > 0)
  2808. strlcat(card->longname, " ", sizeof(card->longname));
  2809. strlcat(card->longname, card->shortname, sizeof(card->longname));
  2810. len = strlcat(card->longname, " at ", sizeof(card->longname));
  2811. if (len < sizeof(card->longname))
  2812. usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
  2813. strlcat(card->longname,
  2814. snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" : ", high speed",
  2815. sizeof(card->longname));
  2816. snd_usb_audio_create_proc(chip);
  2817. *rchip = chip;
  2818. return 0;
  2819. }
  2820. /*
  2821. * probe the active usb device
  2822. *
  2823. * note that this can be called multiple times per a device, when it
  2824. * includes multiple audio control interfaces.
  2825. *
  2826. * thus we check the usb device pointer and creates the card instance
  2827. * only at the first time. the successive calls of this function will
  2828. * append the pcm interface to the corresponding card.
  2829. */
  2830. static void *snd_usb_audio_probe(struct usb_device *dev,
  2831. struct usb_interface *intf,
  2832. const struct usb_device_id *usb_id)
  2833. {
  2834. struct usb_host_config *config = dev->actconfig;
  2835. const snd_usb_audio_quirk_t *quirk = (const snd_usb_audio_quirk_t *)usb_id->driver_info;
  2836. int i, err;
  2837. snd_usb_audio_t *chip;
  2838. struct usb_host_interface *alts;
  2839. int ifnum;
  2840. u32 id;
  2841. alts = &intf->altsetting[0];
  2842. ifnum = get_iface_desc(alts)->bInterfaceNumber;
  2843. id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  2844. le16_to_cpu(dev->descriptor.idProduct));
  2845. if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
  2846. goto __err_val;
  2847. /* SB Extigy needs special boot-up sequence */
  2848. /* if more models come, this will go to the quirk list. */
  2849. if (id == USB_ID(0x041e, 0x3000)) {
  2850. if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
  2851. goto __err_val;
  2852. config = dev->actconfig;
  2853. }
  2854. /* SB Audigy 2 NX needs its own boot-up magic, too */
  2855. if (id == USB_ID(0x041e, 0x3020)) {
  2856. if (snd_usb_audigy2nx_boot_quirk(dev) < 0)
  2857. goto __err_val;
  2858. }
  2859. /*
  2860. * found a config. now register to ALSA
  2861. */
  2862. /* check whether it's already registered */
  2863. chip = NULL;
  2864. down(&register_mutex);
  2865. for (i = 0; i < SNDRV_CARDS; i++) {
  2866. if (usb_chip[i] && usb_chip[i]->dev == dev) {
  2867. if (usb_chip[i]->shutdown) {
  2868. snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
  2869. goto __error;
  2870. }
  2871. chip = usb_chip[i];
  2872. break;
  2873. }
  2874. }
  2875. if (! chip) {
  2876. /* it's a fresh one.
  2877. * now look for an empty slot and create a new card instance
  2878. */
  2879. /* first, set the current configuration for this device */
  2880. if (usb_reset_configuration(dev) < 0) {
  2881. snd_printk(KERN_ERR "cannot reset configuration (value 0x%x)\n", get_cfg_desc(config)->bConfigurationValue);
  2882. goto __error;
  2883. }
  2884. for (i = 0; i < SNDRV_CARDS; i++)
  2885. if (enable[i] && ! usb_chip[i] &&
  2886. (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
  2887. (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
  2888. if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
  2889. goto __error;
  2890. }
  2891. snd_card_set_dev(chip->card, &intf->dev);
  2892. break;
  2893. }
  2894. if (! chip) {
  2895. snd_printk(KERN_ERR "no available usb audio device\n");
  2896. goto __error;
  2897. }
  2898. }
  2899. err = 1; /* continue */
  2900. if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
  2901. /* need some special handlings */
  2902. if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
  2903. goto __error;
  2904. }
  2905. if (err > 0) {
  2906. /* create normal USB audio interfaces */
  2907. if (snd_usb_create_streams(chip, ifnum) < 0 ||
  2908. snd_usb_create_mixer(chip, ifnum) < 0) {
  2909. goto __error;
  2910. }
  2911. }
  2912. /* we are allowed to call snd_card_register() many times */
  2913. if (snd_card_register(chip->card) < 0) {
  2914. goto __error;
  2915. }
  2916. usb_chip[chip->index] = chip;
  2917. chip->num_interfaces++;
  2918. up(&register_mutex);
  2919. return chip;
  2920. __error:
  2921. if (chip && !chip->num_interfaces)
  2922. snd_card_free(chip->card);
  2923. up(&register_mutex);
  2924. __err_val:
  2925. return NULL;
  2926. }
  2927. /*
  2928. * we need to take care of counter, since disconnection can be called also
  2929. * many times as well as usb_audio_probe().
  2930. */
  2931. static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
  2932. {
  2933. snd_usb_audio_t *chip;
  2934. snd_card_t *card;
  2935. struct list_head *p;
  2936. if (ptr == (void *)-1L)
  2937. return;
  2938. chip = ptr;
  2939. card = chip->card;
  2940. down(&register_mutex);
  2941. chip->shutdown = 1;
  2942. chip->num_interfaces--;
  2943. if (chip->num_interfaces <= 0) {
  2944. snd_card_disconnect(card);
  2945. /* release the pcm resources */
  2946. list_for_each(p, &chip->pcm_list) {
  2947. snd_usb_stream_disconnect(p);
  2948. }
  2949. /* release the midi resources */
  2950. list_for_each(p, &chip->midi_list) {
  2951. snd_usbmidi_disconnect(p);
  2952. }
  2953. /* release mixer resources */
  2954. list_for_each(p, &chip->mixer_list) {
  2955. snd_usb_mixer_disconnect(p);
  2956. }
  2957. usb_chip[chip->index] = NULL;
  2958. up(&register_mutex);
  2959. snd_card_free(card);
  2960. } else {
  2961. up(&register_mutex);
  2962. }
  2963. }
  2964. /*
  2965. * new 2.5 USB kernel API
  2966. */
  2967. static int usb_audio_probe(struct usb_interface *intf,
  2968. const struct usb_device_id *id)
  2969. {
  2970. void *chip;
  2971. chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
  2972. if (chip) {
  2973. dev_set_drvdata(&intf->dev, chip);
  2974. return 0;
  2975. } else
  2976. return -EIO;
  2977. }
  2978. static void usb_audio_disconnect(struct usb_interface *intf)
  2979. {
  2980. snd_usb_audio_disconnect(interface_to_usbdev(intf),
  2981. dev_get_drvdata(&intf->dev));
  2982. }
  2983. static int __init snd_usb_audio_init(void)
  2984. {
  2985. if (nrpacks < MIN_PACKS_URB || nrpacks > MAX_PACKS) {
  2986. printk(KERN_WARNING "invalid nrpacks value.\n");
  2987. return -EINVAL;
  2988. }
  2989. usb_register(&usb_audio_driver);
  2990. return 0;
  2991. }
  2992. static void __exit snd_usb_audio_cleanup(void)
  2993. {
  2994. usb_deregister(&usb_audio_driver);
  2995. }
  2996. module_init(snd_usb_audio_init);
  2997. module_exit(snd_usb_audio_cleanup);