usbaudio.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050
  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 <linux/bitops.h>
  40. #include <linux/init.h>
  41. #include <linux/list.h>
  42. #include <linux/slab.h>
  43. #include <linux/string.h>
  44. #include <linux/usb.h>
  45. #include <linux/moduleparam.h>
  46. #include <linux/mutex.h>
  47. #include <linux/usb/audio.h>
  48. #include <linux/usb/ch9.h>
  49. #include <sound/core.h>
  50. #include <sound/info.h>
  51. #include <sound/pcm.h>
  52. #include <sound/pcm_params.h>
  53. #include <sound/initval.h>
  54. #include "usbaudio.h"
  55. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  56. MODULE_DESCRIPTION("USB Audio");
  57. MODULE_LICENSE("GPL");
  58. MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
  59. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  60. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  61. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
  62. /* Vendor/product IDs for this card */
  63. static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
  64. static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
  65. static int nrpacks = 8; /* max. number of packets per urb */
  66. static int async_unlink = 1;
  67. static int device_setup[SNDRV_CARDS]; /* device parameter for this card*/
  68. static int ignore_ctl_error;
  69. module_param_array(index, int, NULL, 0444);
  70. MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
  71. module_param_array(id, charp, NULL, 0444);
  72. MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
  73. module_param_array(enable, bool, NULL, 0444);
  74. MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
  75. module_param_array(vid, int, NULL, 0444);
  76. MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
  77. module_param_array(pid, int, NULL, 0444);
  78. MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
  79. module_param(nrpacks, int, 0644);
  80. MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
  81. module_param(async_unlink, bool, 0444);
  82. MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
  83. module_param_array(device_setup, int, NULL, 0444);
  84. MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
  85. module_param(ignore_ctl_error, bool, 0444);
  86. MODULE_PARM_DESC(ignore_ctl_error,
  87. "Ignore errors from USB controller for mixer interfaces.");
  88. /*
  89. * debug the h/w constraints
  90. */
  91. /* #define HW_CONST_DEBUG */
  92. /*
  93. *
  94. */
  95. #define MAX_PACKS 20
  96. #define MAX_PACKS_HS (MAX_PACKS * 8) /* in high speed mode */
  97. #define MAX_URBS 8
  98. #define SYNC_URBS 4 /* always four urbs for sync */
  99. #define MAX_QUEUE 24 /* try not to exceed this queue length, in ms */
  100. struct audioformat {
  101. struct list_head list;
  102. snd_pcm_format_t format; /* format type */
  103. unsigned int channels; /* # channels */
  104. unsigned int fmt_type; /* USB audio format type (1-3) */
  105. unsigned int frame_size; /* samples per frame for non-audio */
  106. int iface; /* interface number */
  107. unsigned char altsetting; /* corresponding alternate setting */
  108. unsigned char altset_idx; /* array index of altenate setting */
  109. unsigned char attributes; /* corresponding attributes of cs endpoint */
  110. unsigned char endpoint; /* endpoint */
  111. unsigned char ep_attr; /* endpoint attributes */
  112. unsigned char datainterval; /* log_2 of data packet interval */
  113. unsigned int maxpacksize; /* max. packet size */
  114. unsigned int rates; /* rate bitmasks */
  115. unsigned int rate_min, rate_max; /* min/max rates */
  116. unsigned int nr_rates; /* number of rate table entries */
  117. unsigned int *rate_table; /* rate table */
  118. };
  119. struct snd_usb_substream;
  120. struct snd_urb_ctx {
  121. struct urb *urb;
  122. unsigned int buffer_size; /* size of data buffer, if data URB */
  123. struct snd_usb_substream *subs;
  124. int index; /* index for urb array */
  125. int packets; /* number of packets per urb */
  126. };
  127. struct snd_urb_ops {
  128. int (*prepare)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
  129. int (*retire)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
  130. int (*prepare_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
  131. int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u);
  132. };
  133. struct snd_usb_substream {
  134. struct snd_usb_stream *stream;
  135. struct usb_device *dev;
  136. struct snd_pcm_substream *pcm_substream;
  137. int direction; /* playback or capture */
  138. int interface; /* current interface */
  139. int endpoint; /* assigned endpoint */
  140. struct audioformat *cur_audiofmt; /* current audioformat pointer (for hw_params callback) */
  141. unsigned int cur_rate; /* current rate (for hw_params callback) */
  142. unsigned int period_bytes; /* current period bytes (for hw_params callback) */
  143. unsigned int format; /* USB data format */
  144. unsigned int datapipe; /* the data i/o pipe */
  145. unsigned int syncpipe; /* 1 - async out or adaptive in */
  146. unsigned int datainterval; /* log_2 of data packet interval */
  147. unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */
  148. unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */
  149. unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */
  150. unsigned int freqmax; /* maximum sampling rate, used for buffer management */
  151. unsigned int phase; /* phase accumulator */
  152. unsigned int maxpacksize; /* max packet size in bytes */
  153. unsigned int maxframesize; /* max packet size in frames */
  154. unsigned int curpacksize; /* current packet size in bytes (for capture) */
  155. unsigned int curframesize; /* current packet size in frames (for capture) */
  156. unsigned int fill_max: 1; /* fill max packet size always */
  157. unsigned int txfr_quirk:1; /* allow sub-frame alignment */
  158. unsigned int fmt_type; /* USB audio format type (1-3) */
  159. unsigned int running: 1; /* running status */
  160. unsigned int hwptr_done; /* processed byte position in the buffer */
  161. unsigned int transfer_done; /* processed frames since last period update */
  162. unsigned long active_mask; /* bitmask of active urbs */
  163. unsigned long unlink_mask; /* bitmask of unlinked urbs */
  164. unsigned int nurbs; /* # urbs */
  165. struct snd_urb_ctx dataurb[MAX_URBS]; /* data urb table */
  166. struct snd_urb_ctx syncurb[SYNC_URBS]; /* sync urb table */
  167. char *syncbuf; /* sync buffer for all sync URBs */
  168. dma_addr_t sync_dma; /* DMA address of syncbuf */
  169. u64 formats; /* format bitmasks (all or'ed) */
  170. unsigned int num_formats; /* number of supported audio formats (list) */
  171. struct list_head fmt_list; /* format list */
  172. struct snd_pcm_hw_constraint_list rate_list; /* limited rates */
  173. spinlock_t lock;
  174. struct snd_urb_ops ops; /* callbacks (must be filled at init) */
  175. };
  176. struct snd_usb_stream {
  177. struct snd_usb_audio *chip;
  178. struct snd_pcm *pcm;
  179. int pcm_index;
  180. unsigned int fmt_type; /* USB audio format type (1-3) */
  181. struct snd_usb_substream substream[2];
  182. struct list_head list;
  183. };
  184. /*
  185. * we keep the snd_usb_audio_t instances by ourselves for merging
  186. * the all interfaces on the same card as one sound device.
  187. */
  188. static DEFINE_MUTEX(register_mutex);
  189. static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
  190. /*
  191. * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
  192. * this will overflow at approx 524 kHz
  193. */
  194. static inline unsigned get_usb_full_speed_rate(unsigned int rate)
  195. {
  196. return ((rate << 13) + 62) / 125;
  197. }
  198. /*
  199. * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
  200. * this will overflow at approx 4 MHz
  201. */
  202. static inline unsigned get_usb_high_speed_rate(unsigned int rate)
  203. {
  204. return ((rate << 10) + 62) / 125;
  205. }
  206. /* convert our full speed USB rate into sampling rate in Hz */
  207. static inline unsigned get_full_speed_hz(unsigned int usb_rate)
  208. {
  209. return (usb_rate * 125 + (1 << 12)) >> 13;
  210. }
  211. /* convert our high speed USB rate into sampling rate in Hz */
  212. static inline unsigned get_high_speed_hz(unsigned int usb_rate)
  213. {
  214. return (usb_rate * 125 + (1 << 9)) >> 10;
  215. }
  216. /*
  217. * prepare urb for full speed capture sync pipe
  218. *
  219. * fill the length and offset of each urb descriptor.
  220. * the fixed 10.14 frequency is passed through the pipe.
  221. */
  222. static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
  223. struct snd_pcm_runtime *runtime,
  224. struct urb *urb)
  225. {
  226. unsigned char *cp = urb->transfer_buffer;
  227. struct snd_urb_ctx *ctx = urb->context;
  228. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  229. urb->iso_frame_desc[0].length = 3;
  230. urb->iso_frame_desc[0].offset = 0;
  231. cp[0] = subs->freqn >> 2;
  232. cp[1] = subs->freqn >> 10;
  233. cp[2] = subs->freqn >> 18;
  234. return 0;
  235. }
  236. /*
  237. * prepare urb for high speed capture sync pipe
  238. *
  239. * fill the length and offset of each urb descriptor.
  240. * the fixed 12.13 frequency is passed as 16.16 through the pipe.
  241. */
  242. static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
  243. struct snd_pcm_runtime *runtime,
  244. struct urb *urb)
  245. {
  246. unsigned char *cp = urb->transfer_buffer;
  247. struct snd_urb_ctx *ctx = urb->context;
  248. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  249. urb->iso_frame_desc[0].length = 4;
  250. urb->iso_frame_desc[0].offset = 0;
  251. cp[0] = subs->freqn;
  252. cp[1] = subs->freqn >> 8;
  253. cp[2] = subs->freqn >> 16;
  254. cp[3] = subs->freqn >> 24;
  255. return 0;
  256. }
  257. /*
  258. * process after capture sync complete
  259. * - nothing to do
  260. */
  261. static int retire_capture_sync_urb(struct snd_usb_substream *subs,
  262. struct snd_pcm_runtime *runtime,
  263. struct urb *urb)
  264. {
  265. return 0;
  266. }
  267. /*
  268. * prepare urb for capture data pipe
  269. *
  270. * fill the offset and length of each descriptor.
  271. *
  272. * we use a temporary buffer to write the captured data.
  273. * since the length of written data is determined by host, we cannot
  274. * write onto the pcm buffer directly... the data is thus copied
  275. * later at complete callback to the global buffer.
  276. */
  277. static int prepare_capture_urb(struct snd_usb_substream *subs,
  278. struct snd_pcm_runtime *runtime,
  279. struct urb *urb)
  280. {
  281. int i, offs;
  282. struct snd_urb_ctx *ctx = urb->context;
  283. offs = 0;
  284. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  285. for (i = 0; i < ctx->packets; i++) {
  286. urb->iso_frame_desc[i].offset = offs;
  287. urb->iso_frame_desc[i].length = subs->curpacksize;
  288. offs += subs->curpacksize;
  289. }
  290. urb->transfer_buffer_length = offs;
  291. urb->number_of_packets = ctx->packets;
  292. return 0;
  293. }
  294. /*
  295. * process after capture complete
  296. *
  297. * copy the data from each desctiptor to the pcm buffer, and
  298. * update the current position.
  299. */
  300. static int retire_capture_urb(struct snd_usb_substream *subs,
  301. struct snd_pcm_runtime *runtime,
  302. struct urb *urb)
  303. {
  304. unsigned long flags;
  305. unsigned char *cp;
  306. int i;
  307. unsigned int stride, frames, bytes, oldptr;
  308. int period_elapsed = 0;
  309. stride = runtime->frame_bits >> 3;
  310. for (i = 0; i < urb->number_of_packets; i++) {
  311. cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  312. if (urb->iso_frame_desc[i].status) {
  313. snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
  314. // continue;
  315. }
  316. bytes = urb->iso_frame_desc[i].actual_length;
  317. frames = bytes / stride;
  318. if (!subs->txfr_quirk)
  319. bytes = frames * stride;
  320. if (bytes % (runtime->sample_bits >> 3) != 0) {
  321. #ifdef CONFIG_SND_DEBUG_VERBOSE
  322. int oldbytes = bytes;
  323. #endif
  324. bytes = frames * stride;
  325. snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
  326. oldbytes, bytes);
  327. }
  328. /* update the current pointer */
  329. spin_lock_irqsave(&subs->lock, flags);
  330. oldptr = subs->hwptr_done;
  331. subs->hwptr_done += bytes;
  332. if (subs->hwptr_done >= runtime->buffer_size * stride)
  333. subs->hwptr_done -= runtime->buffer_size * stride;
  334. frames = (bytes + (oldptr % stride)) / stride;
  335. subs->transfer_done += frames;
  336. if (subs->transfer_done >= runtime->period_size) {
  337. subs->transfer_done -= runtime->period_size;
  338. period_elapsed = 1;
  339. }
  340. spin_unlock_irqrestore(&subs->lock, flags);
  341. /* copy a data chunk */
  342. if (oldptr + bytes > runtime->buffer_size * stride) {
  343. unsigned int bytes1 =
  344. runtime->buffer_size * stride - oldptr;
  345. memcpy(runtime->dma_area + oldptr, cp, bytes1);
  346. memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
  347. } else {
  348. memcpy(runtime->dma_area + oldptr, cp, bytes);
  349. }
  350. }
  351. if (period_elapsed)
  352. snd_pcm_period_elapsed(subs->pcm_substream);
  353. return 0;
  354. }
  355. /*
  356. * Process after capture complete when paused. Nothing to do.
  357. */
  358. static int retire_paused_capture_urb(struct snd_usb_substream *subs,
  359. struct snd_pcm_runtime *runtime,
  360. struct urb *urb)
  361. {
  362. return 0;
  363. }
  364. /*
  365. * prepare urb for full speed playback sync pipe
  366. *
  367. * set up the offset and length to receive the current frequency.
  368. */
  369. static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
  370. struct snd_pcm_runtime *runtime,
  371. struct urb *urb)
  372. {
  373. struct snd_urb_ctx *ctx = urb->context;
  374. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  375. urb->iso_frame_desc[0].length = 3;
  376. urb->iso_frame_desc[0].offset = 0;
  377. return 0;
  378. }
  379. /*
  380. * prepare urb for high speed playback sync pipe
  381. *
  382. * set up the offset and length to receive the current frequency.
  383. */
  384. static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
  385. struct snd_pcm_runtime *runtime,
  386. struct urb *urb)
  387. {
  388. struct snd_urb_ctx *ctx = urb->context;
  389. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  390. urb->iso_frame_desc[0].length = 4;
  391. urb->iso_frame_desc[0].offset = 0;
  392. return 0;
  393. }
  394. /*
  395. * process after full speed playback sync complete
  396. *
  397. * retrieve the current 10.14 frequency from pipe, and set it.
  398. * the value is referred in prepare_playback_urb().
  399. */
  400. static int retire_playback_sync_urb(struct snd_usb_substream *subs,
  401. struct snd_pcm_runtime *runtime,
  402. struct urb *urb)
  403. {
  404. unsigned int f;
  405. unsigned long flags;
  406. if (urb->iso_frame_desc[0].status == 0 &&
  407. urb->iso_frame_desc[0].actual_length == 3) {
  408. f = combine_triple((u8*)urb->transfer_buffer) << 2;
  409. if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
  410. spin_lock_irqsave(&subs->lock, flags);
  411. subs->freqm = f;
  412. spin_unlock_irqrestore(&subs->lock, flags);
  413. }
  414. }
  415. return 0;
  416. }
  417. /*
  418. * process after high speed playback sync complete
  419. *
  420. * retrieve the current 12.13 frequency from pipe, and set it.
  421. * the value is referred in prepare_playback_urb().
  422. */
  423. static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
  424. struct snd_pcm_runtime *runtime,
  425. struct urb *urb)
  426. {
  427. unsigned int f;
  428. unsigned long flags;
  429. if (urb->iso_frame_desc[0].status == 0 &&
  430. urb->iso_frame_desc[0].actual_length == 4) {
  431. f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
  432. if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
  433. spin_lock_irqsave(&subs->lock, flags);
  434. subs->freqm = f;
  435. spin_unlock_irqrestore(&subs->lock, flags);
  436. }
  437. }
  438. return 0;
  439. }
  440. /*
  441. * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete
  442. *
  443. * These devices return the number of samples per packet instead of the number
  444. * of samples per microframe.
  445. */
  446. static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,
  447. struct snd_pcm_runtime *runtime,
  448. struct urb *urb)
  449. {
  450. unsigned int f;
  451. unsigned long flags;
  452. if (urb->iso_frame_desc[0].status == 0 &&
  453. urb->iso_frame_desc[0].actual_length == 4) {
  454. f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
  455. f >>= subs->datainterval;
  456. if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
  457. spin_lock_irqsave(&subs->lock, flags);
  458. subs->freqm = f;
  459. spin_unlock_irqrestore(&subs->lock, flags);
  460. }
  461. }
  462. return 0;
  463. }
  464. /* determine the number of frames in the next packet */
  465. static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
  466. {
  467. if (subs->fill_max)
  468. return subs->maxframesize;
  469. else {
  470. subs->phase = (subs->phase & 0xffff)
  471. + (subs->freqm << subs->datainterval);
  472. return min(subs->phase >> 16, subs->maxframesize);
  473. }
  474. }
  475. /*
  476. * Prepare urb for streaming before playback starts or when paused.
  477. *
  478. * We don't have any data, so we send silence.
  479. */
  480. static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
  481. struct snd_pcm_runtime *runtime,
  482. struct urb *urb)
  483. {
  484. unsigned int i, offs, counts;
  485. struct snd_urb_ctx *ctx = urb->context;
  486. int stride = runtime->frame_bits >> 3;
  487. offs = 0;
  488. urb->dev = ctx->subs->dev;
  489. for (i = 0; i < ctx->packets; ++i) {
  490. counts = snd_usb_audio_next_packet_size(subs);
  491. urb->iso_frame_desc[i].offset = offs * stride;
  492. urb->iso_frame_desc[i].length = counts * stride;
  493. offs += counts;
  494. }
  495. urb->number_of_packets = ctx->packets;
  496. urb->transfer_buffer_length = offs * stride;
  497. memset(urb->transfer_buffer,
  498. subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
  499. offs * stride);
  500. return 0;
  501. }
  502. /*
  503. * prepare urb for playback data pipe
  504. *
  505. * Since a URB can handle only a single linear buffer, we must use double
  506. * buffering when the data to be transferred overflows the buffer boundary.
  507. * To avoid inconsistencies when updating hwptr_done, we use double buffering
  508. * for all URBs.
  509. */
  510. static int prepare_playback_urb(struct snd_usb_substream *subs,
  511. struct snd_pcm_runtime *runtime,
  512. struct urb *urb)
  513. {
  514. int i, stride;
  515. unsigned int counts, frames, bytes;
  516. unsigned long flags;
  517. int period_elapsed = 0;
  518. struct snd_urb_ctx *ctx = urb->context;
  519. stride = runtime->frame_bits >> 3;
  520. frames = 0;
  521. urb->dev = ctx->subs->dev; /* we need to set this at each time */
  522. urb->number_of_packets = 0;
  523. spin_lock_irqsave(&subs->lock, flags);
  524. for (i = 0; i < ctx->packets; i++) {
  525. counts = snd_usb_audio_next_packet_size(subs);
  526. /* set up descriptor */
  527. urb->iso_frame_desc[i].offset = frames * stride;
  528. urb->iso_frame_desc[i].length = counts * stride;
  529. frames += counts;
  530. urb->number_of_packets++;
  531. subs->transfer_done += counts;
  532. if (subs->transfer_done >= runtime->period_size) {
  533. subs->transfer_done -= runtime->period_size;
  534. period_elapsed = 1;
  535. if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
  536. if (subs->transfer_done > 0) {
  537. /* FIXME: fill-max mode is not
  538. * supported yet */
  539. frames -= subs->transfer_done;
  540. counts -= subs->transfer_done;
  541. urb->iso_frame_desc[i].length =
  542. counts * stride;
  543. subs->transfer_done = 0;
  544. }
  545. i++;
  546. if (i < ctx->packets) {
  547. /* add a transfer delimiter */
  548. urb->iso_frame_desc[i].offset =
  549. frames * stride;
  550. urb->iso_frame_desc[i].length = 0;
  551. urb->number_of_packets++;
  552. }
  553. break;
  554. }
  555. }
  556. if (period_elapsed) /* finish at the period boundary */
  557. break;
  558. }
  559. bytes = frames * stride;
  560. if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
  561. /* err, the transferred area goes over buffer boundary. */
  562. unsigned int bytes1 =
  563. runtime->buffer_size * stride - subs->hwptr_done;
  564. memcpy(urb->transfer_buffer,
  565. runtime->dma_area + subs->hwptr_done, bytes1);
  566. memcpy(urb->transfer_buffer + bytes1,
  567. runtime->dma_area, bytes - bytes1);
  568. } else {
  569. memcpy(urb->transfer_buffer,
  570. runtime->dma_area + subs->hwptr_done, bytes);
  571. }
  572. subs->hwptr_done += bytes;
  573. if (subs->hwptr_done >= runtime->buffer_size * stride)
  574. subs->hwptr_done -= runtime->buffer_size * stride;
  575. runtime->delay += frames;
  576. spin_unlock_irqrestore(&subs->lock, flags);
  577. urb->transfer_buffer_length = bytes;
  578. if (period_elapsed)
  579. snd_pcm_period_elapsed(subs->pcm_substream);
  580. return 0;
  581. }
  582. /*
  583. * process after playback data complete
  584. * - decrease the delay count again
  585. */
  586. static int retire_playback_urb(struct snd_usb_substream *subs,
  587. struct snd_pcm_runtime *runtime,
  588. struct urb *urb)
  589. {
  590. unsigned long flags;
  591. int stride = runtime->frame_bits >> 3;
  592. int processed = urb->transfer_buffer_length / stride;
  593. spin_lock_irqsave(&subs->lock, flags);
  594. if (processed > runtime->delay)
  595. runtime->delay = 0;
  596. else
  597. runtime->delay -= processed;
  598. spin_unlock_irqrestore(&subs->lock, flags);
  599. return 0;
  600. }
  601. /*
  602. */
  603. static struct snd_urb_ops audio_urb_ops[2] = {
  604. {
  605. .prepare = prepare_nodata_playback_urb,
  606. .retire = retire_playback_urb,
  607. .prepare_sync = prepare_playback_sync_urb,
  608. .retire_sync = retire_playback_sync_urb,
  609. },
  610. {
  611. .prepare = prepare_capture_urb,
  612. .retire = retire_capture_urb,
  613. .prepare_sync = prepare_capture_sync_urb,
  614. .retire_sync = retire_capture_sync_urb,
  615. },
  616. };
  617. static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
  618. {
  619. .prepare = prepare_nodata_playback_urb,
  620. .retire = retire_playback_urb,
  621. .prepare_sync = prepare_playback_sync_urb_hs,
  622. .retire_sync = retire_playback_sync_urb_hs,
  623. },
  624. {
  625. .prepare = prepare_capture_urb,
  626. .retire = retire_capture_urb,
  627. .prepare_sync = prepare_capture_sync_urb_hs,
  628. .retire_sync = retire_capture_sync_urb,
  629. },
  630. };
  631. /*
  632. * complete callback from data urb
  633. */
  634. static void snd_complete_urb(struct urb *urb)
  635. {
  636. struct snd_urb_ctx *ctx = urb->context;
  637. struct snd_usb_substream *subs = ctx->subs;
  638. struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
  639. int err = 0;
  640. if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
  641. !subs->running || /* can be stopped during retire callback */
  642. (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
  643. (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  644. clear_bit(ctx->index, &subs->active_mask);
  645. if (err < 0) {
  646. snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
  647. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  648. }
  649. }
  650. }
  651. /*
  652. * complete callback from sync urb
  653. */
  654. static void snd_complete_sync_urb(struct urb *urb)
  655. {
  656. struct snd_urb_ctx *ctx = urb->context;
  657. struct snd_usb_substream *subs = ctx->subs;
  658. struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
  659. int err = 0;
  660. if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
  661. !subs->running || /* can be stopped during retire callback */
  662. (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
  663. (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  664. clear_bit(ctx->index + 16, &subs->active_mask);
  665. if (err < 0) {
  666. snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
  667. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  668. }
  669. }
  670. }
  671. /*
  672. * unlink active urbs.
  673. */
  674. static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
  675. {
  676. unsigned int i;
  677. int async;
  678. subs->running = 0;
  679. if (!force && subs->stream->chip->shutdown) /* to be sure... */
  680. return -EBADFD;
  681. async = !can_sleep && async_unlink;
  682. if (!async && in_interrupt())
  683. return 0;
  684. for (i = 0; i < subs->nurbs; i++) {
  685. if (test_bit(i, &subs->active_mask)) {
  686. if (!test_and_set_bit(i, &subs->unlink_mask)) {
  687. struct urb *u = subs->dataurb[i].urb;
  688. if (async)
  689. usb_unlink_urb(u);
  690. else
  691. usb_kill_urb(u);
  692. }
  693. }
  694. }
  695. if (subs->syncpipe) {
  696. for (i = 0; i < SYNC_URBS; i++) {
  697. if (test_bit(i+16, &subs->active_mask)) {
  698. if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
  699. struct urb *u = subs->syncurb[i].urb;
  700. if (async)
  701. usb_unlink_urb(u);
  702. else
  703. usb_kill_urb(u);
  704. }
  705. }
  706. }
  707. }
  708. return 0;
  709. }
  710. static const char *usb_error_string(int err)
  711. {
  712. switch (err) {
  713. case -ENODEV:
  714. return "no device";
  715. case -ENOENT:
  716. return "endpoint not enabled";
  717. case -EPIPE:
  718. return "endpoint stalled";
  719. case -ENOSPC:
  720. return "not enough bandwidth";
  721. case -ESHUTDOWN:
  722. return "device disabled";
  723. case -EHOSTUNREACH:
  724. return "device suspended";
  725. case -EINVAL:
  726. case -EAGAIN:
  727. case -EFBIG:
  728. case -EMSGSIZE:
  729. return "internal error";
  730. default:
  731. return "unknown error";
  732. }
  733. }
  734. /*
  735. * set up and start data/sync urbs
  736. */
  737. static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
  738. {
  739. unsigned int i;
  740. int err;
  741. if (subs->stream->chip->shutdown)
  742. return -EBADFD;
  743. for (i = 0; i < subs->nurbs; i++) {
  744. if (snd_BUG_ON(!subs->dataurb[i].urb))
  745. return -EINVAL;
  746. if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
  747. snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
  748. goto __error;
  749. }
  750. }
  751. if (subs->syncpipe) {
  752. for (i = 0; i < SYNC_URBS; i++) {
  753. if (snd_BUG_ON(!subs->syncurb[i].urb))
  754. return -EINVAL;
  755. if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
  756. snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
  757. goto __error;
  758. }
  759. }
  760. }
  761. subs->active_mask = 0;
  762. subs->unlink_mask = 0;
  763. subs->running = 1;
  764. for (i = 0; i < subs->nurbs; i++) {
  765. err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
  766. if (err < 0) {
  767. snd_printk(KERN_ERR "cannot submit datapipe "
  768. "for urb %d, error %d: %s\n",
  769. i, err, usb_error_string(err));
  770. goto __error;
  771. }
  772. set_bit(i, &subs->active_mask);
  773. }
  774. if (subs->syncpipe) {
  775. for (i = 0; i < SYNC_URBS; i++) {
  776. err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
  777. if (err < 0) {
  778. snd_printk(KERN_ERR "cannot submit syncpipe "
  779. "for urb %d, error %d: %s\n",
  780. i, err, usb_error_string(err));
  781. goto __error;
  782. }
  783. set_bit(i + 16, &subs->active_mask);
  784. }
  785. }
  786. return 0;
  787. __error:
  788. // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
  789. deactivate_urbs(subs, 0, 0);
  790. return -EPIPE;
  791. }
  792. /*
  793. * wait until all urbs are processed.
  794. */
  795. static int wait_clear_urbs(struct snd_usb_substream *subs)
  796. {
  797. unsigned long end_time = jiffies + msecs_to_jiffies(1000);
  798. unsigned int i;
  799. int alive;
  800. do {
  801. alive = 0;
  802. for (i = 0; i < subs->nurbs; i++) {
  803. if (test_bit(i, &subs->active_mask))
  804. alive++;
  805. }
  806. if (subs->syncpipe) {
  807. for (i = 0; i < SYNC_URBS; i++) {
  808. if (test_bit(i + 16, &subs->active_mask))
  809. alive++;
  810. }
  811. }
  812. if (! alive)
  813. break;
  814. schedule_timeout_uninterruptible(1);
  815. } while (time_before(jiffies, end_time));
  816. if (alive)
  817. snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
  818. return 0;
  819. }
  820. /*
  821. * return the current pcm pointer. just based on the hwptr_done value.
  822. */
  823. static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
  824. {
  825. struct snd_usb_substream *subs;
  826. unsigned int hwptr_done;
  827. subs = (struct snd_usb_substream *)substream->runtime->private_data;
  828. spin_lock(&subs->lock);
  829. hwptr_done = subs->hwptr_done;
  830. spin_unlock(&subs->lock);
  831. return hwptr_done / (substream->runtime->frame_bits >> 3);
  832. }
  833. /*
  834. * start/stop playback substream
  835. */
  836. static int snd_usb_pcm_playback_trigger(struct snd_pcm_substream *substream,
  837. int cmd)
  838. {
  839. struct snd_usb_substream *subs = substream->runtime->private_data;
  840. switch (cmd) {
  841. case SNDRV_PCM_TRIGGER_START:
  842. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  843. subs->ops.prepare = prepare_playback_urb;
  844. return 0;
  845. case SNDRV_PCM_TRIGGER_STOP:
  846. return deactivate_urbs(subs, 0, 0);
  847. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  848. subs->ops.prepare = prepare_nodata_playback_urb;
  849. return 0;
  850. default:
  851. return -EINVAL;
  852. }
  853. }
  854. /*
  855. * start/stop capture substream
  856. */
  857. static int snd_usb_pcm_capture_trigger(struct snd_pcm_substream *substream,
  858. int cmd)
  859. {
  860. struct snd_usb_substream *subs = substream->runtime->private_data;
  861. switch (cmd) {
  862. case SNDRV_PCM_TRIGGER_START:
  863. subs->ops.retire = retire_capture_urb;
  864. return start_urbs(subs, substream->runtime);
  865. case SNDRV_PCM_TRIGGER_STOP:
  866. return deactivate_urbs(subs, 0, 0);
  867. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  868. subs->ops.retire = retire_paused_capture_urb;
  869. return 0;
  870. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  871. subs->ops.retire = retire_capture_urb;
  872. return 0;
  873. default:
  874. return -EINVAL;
  875. }
  876. }
  877. /*
  878. * release a urb data
  879. */
  880. static void release_urb_ctx(struct snd_urb_ctx *u)
  881. {
  882. if (u->urb) {
  883. if (u->buffer_size)
  884. usb_buffer_free(u->subs->dev, u->buffer_size,
  885. u->urb->transfer_buffer,
  886. u->urb->transfer_dma);
  887. usb_free_urb(u->urb);
  888. u->urb = NULL;
  889. }
  890. }
  891. /*
  892. * release a substream
  893. */
  894. static void release_substream_urbs(struct snd_usb_substream *subs, int force)
  895. {
  896. int i;
  897. /* stop urbs (to be sure) */
  898. deactivate_urbs(subs, force, 1);
  899. wait_clear_urbs(subs);
  900. for (i = 0; i < MAX_URBS; i++)
  901. release_urb_ctx(&subs->dataurb[i]);
  902. for (i = 0; i < SYNC_URBS; i++)
  903. release_urb_ctx(&subs->syncurb[i]);
  904. usb_buffer_free(subs->dev, SYNC_URBS * 4,
  905. subs->syncbuf, subs->sync_dma);
  906. subs->syncbuf = NULL;
  907. subs->nurbs = 0;
  908. }
  909. /*
  910. * initialize a substream for plaback/capture
  911. */
  912. static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes,
  913. unsigned int rate, unsigned int frame_bits)
  914. {
  915. unsigned int maxsize, i;
  916. int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  917. unsigned int urb_packs, total_packs, packs_per_ms;
  918. /* calculate the frequency in 16.16 format */
  919. if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  920. subs->freqn = get_usb_full_speed_rate(rate);
  921. else
  922. subs->freqn = get_usb_high_speed_rate(rate);
  923. subs->freqm = subs->freqn;
  924. /* calculate max. frequency */
  925. if (subs->maxpacksize) {
  926. /* whatever fits into a max. size packet */
  927. maxsize = subs->maxpacksize;
  928. subs->freqmax = (maxsize / (frame_bits >> 3))
  929. << (16 - subs->datainterval);
  930. } else {
  931. /* no max. packet size: just take 25% higher than nominal */
  932. subs->freqmax = subs->freqn + (subs->freqn >> 2);
  933. maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
  934. >> (16 - subs->datainterval);
  935. }
  936. subs->phase = 0;
  937. if (subs->fill_max)
  938. subs->curpacksize = subs->maxpacksize;
  939. else
  940. subs->curpacksize = maxsize;
  941. if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
  942. packs_per_ms = 8 >> subs->datainterval;
  943. else
  944. packs_per_ms = 1;
  945. if (is_playback) {
  946. urb_packs = max(nrpacks, 1);
  947. urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
  948. } else
  949. urb_packs = 1;
  950. urb_packs *= packs_per_ms;
  951. if (subs->syncpipe)
  952. urb_packs = min(urb_packs, 1U << subs->syncinterval);
  953. /* decide how many packets to be used */
  954. if (is_playback) {
  955. unsigned int minsize, maxpacks;
  956. /* determine how small a packet can be */
  957. minsize = (subs->freqn >> (16 - subs->datainterval))
  958. * (frame_bits >> 3);
  959. /* with sync from device, assume it can be 12% lower */
  960. if (subs->syncpipe)
  961. minsize -= minsize >> 3;
  962. minsize = max(minsize, 1u);
  963. total_packs = (period_bytes + minsize - 1) / minsize;
  964. /* we need at least two URBs for queueing */
  965. if (total_packs < 2) {
  966. total_packs = 2;
  967. } else {
  968. /* and we don't want too long a queue either */
  969. maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
  970. total_packs = min(total_packs, maxpacks);
  971. }
  972. } else {
  973. while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
  974. urb_packs >>= 1;
  975. total_packs = MAX_URBS * urb_packs;
  976. }
  977. subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
  978. if (subs->nurbs > MAX_URBS) {
  979. /* too much... */
  980. subs->nurbs = MAX_URBS;
  981. total_packs = MAX_URBS * urb_packs;
  982. } else if (subs->nurbs < 2) {
  983. /* too little - we need at least two packets
  984. * to ensure contiguous playback/capture
  985. */
  986. subs->nurbs = 2;
  987. }
  988. /* allocate and initialize data urbs */
  989. for (i = 0; i < subs->nurbs; i++) {
  990. struct snd_urb_ctx *u = &subs->dataurb[i];
  991. u->index = i;
  992. u->subs = subs;
  993. u->packets = (i + 1) * total_packs / subs->nurbs
  994. - i * total_packs / subs->nurbs;
  995. u->buffer_size = maxsize * u->packets;
  996. if (subs->fmt_type == UAC_FORMAT_TYPE_II)
  997. u->packets++; /* for transfer delimiter */
  998. u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
  999. if (!u->urb)
  1000. goto out_of_memory;
  1001. u->urb->transfer_buffer =
  1002. usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,
  1003. &u->urb->transfer_dma);
  1004. if (!u->urb->transfer_buffer)
  1005. goto out_of_memory;
  1006. u->urb->pipe = subs->datapipe;
  1007. u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  1008. u->urb->interval = 1 << subs->datainterval;
  1009. u->urb->context = u;
  1010. u->urb->complete = snd_complete_urb;
  1011. }
  1012. if (subs->syncpipe) {
  1013. /* allocate and initialize sync urbs */
  1014. subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,
  1015. GFP_KERNEL, &subs->sync_dma);
  1016. if (!subs->syncbuf)
  1017. goto out_of_memory;
  1018. for (i = 0; i < SYNC_URBS; i++) {
  1019. struct snd_urb_ctx *u = &subs->syncurb[i];
  1020. u->index = i;
  1021. u->subs = subs;
  1022. u->packets = 1;
  1023. u->urb = usb_alloc_urb(1, GFP_KERNEL);
  1024. if (!u->urb)
  1025. goto out_of_memory;
  1026. u->urb->transfer_buffer = subs->syncbuf + i * 4;
  1027. u->urb->transfer_dma = subs->sync_dma + i * 4;
  1028. u->urb->transfer_buffer_length = 4;
  1029. u->urb->pipe = subs->syncpipe;
  1030. u->urb->transfer_flags = URB_ISO_ASAP |
  1031. URB_NO_TRANSFER_DMA_MAP;
  1032. u->urb->number_of_packets = 1;
  1033. u->urb->interval = 1 << subs->syncinterval;
  1034. u->urb->context = u;
  1035. u->urb->complete = snd_complete_sync_urb;
  1036. }
  1037. }
  1038. return 0;
  1039. out_of_memory:
  1040. release_substream_urbs(subs, 0);
  1041. return -ENOMEM;
  1042. }
  1043. /*
  1044. * find a matching audio format
  1045. */
  1046. static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format,
  1047. unsigned int rate, unsigned int channels)
  1048. {
  1049. struct list_head *p;
  1050. struct audioformat *found = NULL;
  1051. int cur_attr = 0, attr;
  1052. list_for_each(p, &subs->fmt_list) {
  1053. struct audioformat *fp;
  1054. fp = list_entry(p, struct audioformat, list);
  1055. if (fp->format != format || fp->channels != channels)
  1056. continue;
  1057. if (rate < fp->rate_min || rate > fp->rate_max)
  1058. continue;
  1059. if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
  1060. unsigned int i;
  1061. for (i = 0; i < fp->nr_rates; i++)
  1062. if (fp->rate_table[i] == rate)
  1063. break;
  1064. if (i >= fp->nr_rates)
  1065. continue;
  1066. }
  1067. attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
  1068. if (! found) {
  1069. found = fp;
  1070. cur_attr = attr;
  1071. continue;
  1072. }
  1073. /* avoid async out and adaptive in if the other method
  1074. * supports the same format.
  1075. * this is a workaround for the case like
  1076. * M-audio audiophile USB.
  1077. */
  1078. if (attr != cur_attr) {
  1079. if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
  1080. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  1081. (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  1082. subs->direction == SNDRV_PCM_STREAM_CAPTURE))
  1083. continue;
  1084. if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
  1085. subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
  1086. (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
  1087. subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
  1088. found = fp;
  1089. cur_attr = attr;
  1090. continue;
  1091. }
  1092. }
  1093. /* find the format with the largest max. packet size */
  1094. if (fp->maxpacksize > found->maxpacksize) {
  1095. found = fp;
  1096. cur_attr = attr;
  1097. }
  1098. }
  1099. return found;
  1100. }
  1101. /*
  1102. * initialize the picth control and sample rate
  1103. */
  1104. static int init_usb_pitch(struct usb_device *dev, int iface,
  1105. struct usb_host_interface *alts,
  1106. struct audioformat *fmt)
  1107. {
  1108. unsigned int ep;
  1109. unsigned char data[1];
  1110. int err;
  1111. ep = get_endpoint(alts, 0)->bEndpointAddress;
  1112. /* if endpoint has pitch control, enable it */
  1113. if (fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL) {
  1114. data[0] = 1;
  1115. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  1116. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  1117. UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) {
  1118. snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
  1119. dev->devnum, iface, ep);
  1120. return err;
  1121. }
  1122. }
  1123. return 0;
  1124. }
  1125. static int init_usb_sample_rate(struct usb_device *dev, int iface,
  1126. struct usb_host_interface *alts,
  1127. struct audioformat *fmt, int rate)
  1128. {
  1129. unsigned int ep;
  1130. unsigned char data[3];
  1131. int err;
  1132. ep = get_endpoint(alts, 0)->bEndpointAddress;
  1133. /* if endpoint has sampling rate control, set it */
  1134. if (fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE) {
  1135. int crate;
  1136. data[0] = rate;
  1137. data[1] = rate >> 8;
  1138. data[2] = rate >> 16;
  1139. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  1140. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
  1141. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {
  1142. snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n",
  1143. dev->devnum, iface, fmt->altsetting, rate, ep);
  1144. return err;
  1145. }
  1146. if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
  1147. USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN,
  1148. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) {
  1149. snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n",
  1150. dev->devnum, iface, fmt->altsetting, ep);
  1151. return 0; /* some devices don't support reading */
  1152. }
  1153. crate = data[0] | (data[1] << 8) | (data[2] << 16);
  1154. if (crate != rate) {
  1155. snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate);
  1156. // runtime->rate = crate;
  1157. }
  1158. }
  1159. return 0;
  1160. }
  1161. /*
  1162. * For E-Mu 0404USB/0202USB/TrackerPre sample rate should be set for device,
  1163. * not for interface.
  1164. */
  1165. static void set_format_emu_quirk(struct snd_usb_substream *subs,
  1166. struct audioformat *fmt)
  1167. {
  1168. unsigned char emu_samplerate_id = 0;
  1169. /* When capture is active
  1170. * sample rate shouldn't be changed
  1171. * by playback substream
  1172. */
  1173. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
  1174. if (subs->stream->substream[SNDRV_PCM_STREAM_CAPTURE].interface != -1)
  1175. return;
  1176. }
  1177. switch (fmt->rate_min) {
  1178. case 48000:
  1179. emu_samplerate_id = EMU_QUIRK_SR_48000HZ;
  1180. break;
  1181. case 88200:
  1182. emu_samplerate_id = EMU_QUIRK_SR_88200HZ;
  1183. break;
  1184. case 96000:
  1185. emu_samplerate_id = EMU_QUIRK_SR_96000HZ;
  1186. break;
  1187. case 176400:
  1188. emu_samplerate_id = EMU_QUIRK_SR_176400HZ;
  1189. break;
  1190. case 192000:
  1191. emu_samplerate_id = EMU_QUIRK_SR_192000HZ;
  1192. break;
  1193. default:
  1194. emu_samplerate_id = EMU_QUIRK_SR_44100HZ;
  1195. break;
  1196. }
  1197. snd_emuusb_set_samplerate(subs->stream->chip, emu_samplerate_id);
  1198. }
  1199. /*
  1200. * find a matching format and set up the interface
  1201. */
  1202. static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
  1203. {
  1204. struct usb_device *dev = subs->dev;
  1205. struct usb_host_interface *alts;
  1206. struct usb_interface_descriptor *altsd;
  1207. struct usb_interface *iface;
  1208. unsigned int ep, attr;
  1209. int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
  1210. int err;
  1211. iface = usb_ifnum_to_if(dev, fmt->iface);
  1212. if (WARN_ON(!iface))
  1213. return -EINVAL;
  1214. alts = &iface->altsetting[fmt->altset_idx];
  1215. altsd = get_iface_desc(alts);
  1216. if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
  1217. return -EINVAL;
  1218. if (fmt == subs->cur_audiofmt)
  1219. return 0;
  1220. /* close the old interface */
  1221. if (subs->interface >= 0 && subs->interface != fmt->iface) {
  1222. if (usb_set_interface(subs->dev, subs->interface, 0) < 0) {
  1223. snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n",
  1224. dev->devnum, fmt->iface, fmt->altsetting);
  1225. return -EIO;
  1226. }
  1227. subs->interface = -1;
  1228. subs->format = 0;
  1229. }
  1230. /* set interface */
  1231. if (subs->interface != fmt->iface || subs->format != fmt->altset_idx) {
  1232. if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) {
  1233. snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n",
  1234. dev->devnum, fmt->iface, fmt->altsetting);
  1235. return -EIO;
  1236. }
  1237. snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting);
  1238. subs->interface = fmt->iface;
  1239. subs->format = fmt->altset_idx;
  1240. }
  1241. /* create a data pipe */
  1242. ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK;
  1243. if (is_playback)
  1244. subs->datapipe = usb_sndisocpipe(dev, ep);
  1245. else
  1246. subs->datapipe = usb_rcvisocpipe(dev, ep);
  1247. subs->datainterval = fmt->datainterval;
  1248. subs->syncpipe = subs->syncinterval = 0;
  1249. subs->maxpacksize = fmt->maxpacksize;
  1250. subs->fill_max = 0;
  1251. /* we need a sync pipe in async OUT or adaptive IN mode */
  1252. /* check the number of EP, since some devices have broken
  1253. * descriptors which fool us. if it has only one EP,
  1254. * assume it as adaptive-out or sync-in.
  1255. */
  1256. attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
  1257. if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
  1258. (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
  1259. altsd->bNumEndpoints >= 2) {
  1260. /* check sync-pipe endpoint */
  1261. /* ... and check descriptor size before accessing bSynchAddress
  1262. because there is a version of the SB Audigy 2 NX firmware lacking
  1263. the audio fields in the endpoint descriptors */
  1264. if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 ||
  1265. (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  1266. get_endpoint(alts, 1)->bSynchAddress != 0)) {
  1267. snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
  1268. dev->devnum, fmt->iface, fmt->altsetting);
  1269. return -EINVAL;
  1270. }
  1271. ep = get_endpoint(alts, 1)->bEndpointAddress;
  1272. if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  1273. (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
  1274. (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
  1275. snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n",
  1276. dev->devnum, fmt->iface, fmt->altsetting);
  1277. return -EINVAL;
  1278. }
  1279. ep &= USB_ENDPOINT_NUMBER_MASK;
  1280. if (is_playback)
  1281. subs->syncpipe = usb_rcvisocpipe(dev, ep);
  1282. else
  1283. subs->syncpipe = usb_sndisocpipe(dev, ep);
  1284. if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
  1285. get_endpoint(alts, 1)->bRefresh >= 1 &&
  1286. get_endpoint(alts, 1)->bRefresh <= 9)
  1287. subs->syncinterval = get_endpoint(alts, 1)->bRefresh;
  1288. else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
  1289. subs->syncinterval = 1;
  1290. else if (get_endpoint(alts, 1)->bInterval >= 1 &&
  1291. get_endpoint(alts, 1)->bInterval <= 16)
  1292. subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
  1293. else
  1294. subs->syncinterval = 3;
  1295. }
  1296. /* always fill max packet size */
  1297. if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)
  1298. subs->fill_max = 1;
  1299. if ((err = init_usb_pitch(dev, subs->interface, alts, fmt)) < 0)
  1300. return err;
  1301. subs->cur_audiofmt = fmt;
  1302. switch (subs->stream->chip->usb_id) {
  1303. case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
  1304. case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
  1305. case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
  1306. set_format_emu_quirk(subs, fmt);
  1307. break;
  1308. }
  1309. #if 0
  1310. printk(KERN_DEBUG
  1311. "setting done: format = %d, rate = %d..%d, channels = %d\n",
  1312. fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels);
  1313. printk(KERN_DEBUG
  1314. " datapipe = 0x%0x, syncpipe = 0x%0x\n",
  1315. subs->datapipe, subs->syncpipe);
  1316. #endif
  1317. return 0;
  1318. }
  1319. /*
  1320. * hw_params callback
  1321. *
  1322. * allocate a buffer and set the given audio format.
  1323. *
  1324. * so far we use a physically linear buffer although packetize transfer
  1325. * doesn't need a continuous area.
  1326. * if sg buffer is supported on the later version of alsa, we'll follow
  1327. * that.
  1328. */
  1329. static int snd_usb_hw_params(struct snd_pcm_substream *substream,
  1330. struct snd_pcm_hw_params *hw_params)
  1331. {
  1332. struct snd_usb_substream *subs = substream->runtime->private_data;
  1333. struct audioformat *fmt;
  1334. unsigned int channels, rate, format;
  1335. int ret, changed;
  1336. ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
  1337. params_buffer_bytes(hw_params));
  1338. if (ret < 0)
  1339. return ret;
  1340. format = params_format(hw_params);
  1341. rate = params_rate(hw_params);
  1342. channels = params_channels(hw_params);
  1343. fmt = find_format(subs, format, rate, channels);
  1344. if (!fmt) {
  1345. snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
  1346. format, rate, channels);
  1347. return -EINVAL;
  1348. }
  1349. changed = subs->cur_audiofmt != fmt ||
  1350. subs->period_bytes != params_period_bytes(hw_params) ||
  1351. subs->cur_rate != rate;
  1352. if ((ret = set_format(subs, fmt)) < 0)
  1353. return ret;
  1354. if (subs->cur_rate != rate) {
  1355. struct usb_host_interface *alts;
  1356. struct usb_interface *iface;
  1357. iface = usb_ifnum_to_if(subs->dev, fmt->iface);
  1358. alts = &iface->altsetting[fmt->altset_idx];
  1359. ret = init_usb_sample_rate(subs->dev, subs->interface, alts, fmt, rate);
  1360. if (ret < 0)
  1361. return ret;
  1362. subs->cur_rate = rate;
  1363. }
  1364. if (changed) {
  1365. /* format changed */
  1366. release_substream_urbs(subs, 0);
  1367. /* influenced: period_bytes, channels, rate, format, */
  1368. ret = init_substream_urbs(subs, params_period_bytes(hw_params),
  1369. params_rate(hw_params),
  1370. snd_pcm_format_physical_width(params_format(hw_params)) * params_channels(hw_params));
  1371. }
  1372. return ret;
  1373. }
  1374. /*
  1375. * hw_free callback
  1376. *
  1377. * reset the audio format and release the buffer
  1378. */
  1379. static int snd_usb_hw_free(struct snd_pcm_substream *substream)
  1380. {
  1381. struct snd_usb_substream *subs = substream->runtime->private_data;
  1382. subs->cur_audiofmt = NULL;
  1383. subs->cur_rate = 0;
  1384. subs->period_bytes = 0;
  1385. if (!subs->stream->chip->shutdown)
  1386. release_substream_urbs(subs, 0);
  1387. return snd_pcm_lib_free_vmalloc_buffer(substream);
  1388. }
  1389. /*
  1390. * prepare callback
  1391. *
  1392. * only a few subtle things...
  1393. */
  1394. static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
  1395. {
  1396. struct snd_pcm_runtime *runtime = substream->runtime;
  1397. struct snd_usb_substream *subs = runtime->private_data;
  1398. if (! subs->cur_audiofmt) {
  1399. snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
  1400. return -ENXIO;
  1401. }
  1402. /* some unit conversions in runtime */
  1403. subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize);
  1404. subs->curframesize = bytes_to_frames(runtime, subs->curpacksize);
  1405. /* reset the pointer */
  1406. subs->hwptr_done = 0;
  1407. subs->transfer_done = 0;
  1408. subs->phase = 0;
  1409. runtime->delay = 0;
  1410. /* clear urbs (to be sure) */
  1411. deactivate_urbs(subs, 0, 1);
  1412. wait_clear_urbs(subs);
  1413. /* for playback, submit the URBs now; otherwise, the first hwptr_done
  1414. * updates for all URBs would happen at the same time when starting */
  1415. if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
  1416. subs->ops.prepare = prepare_nodata_playback_urb;
  1417. return start_urbs(subs, runtime);
  1418. } else
  1419. return 0;
  1420. }
  1421. static struct snd_pcm_hardware snd_usb_hardware =
  1422. {
  1423. .info = SNDRV_PCM_INFO_MMAP |
  1424. SNDRV_PCM_INFO_MMAP_VALID |
  1425. SNDRV_PCM_INFO_BATCH |
  1426. SNDRV_PCM_INFO_INTERLEAVED |
  1427. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1428. SNDRV_PCM_INFO_PAUSE,
  1429. .buffer_bytes_max = 1024 * 1024,
  1430. .period_bytes_min = 64,
  1431. .period_bytes_max = 512 * 1024,
  1432. .periods_min = 2,
  1433. .periods_max = 1024,
  1434. };
  1435. /*
  1436. * h/w constraints
  1437. */
  1438. #ifdef HW_CONST_DEBUG
  1439. #define hwc_debug(fmt, args...) printk(KERN_DEBUG fmt, ##args)
  1440. #else
  1441. #define hwc_debug(fmt, args...) /**/
  1442. #endif
  1443. static int hw_check_valid_format(struct snd_usb_substream *subs,
  1444. struct snd_pcm_hw_params *params,
  1445. struct audioformat *fp)
  1446. {
  1447. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  1448. struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  1449. struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1450. struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  1451. unsigned int ptime;
  1452. /* check the format */
  1453. if (!snd_mask_test(fmts, fp->format)) {
  1454. hwc_debug(" > check: no supported format %d\n", fp->format);
  1455. return 0;
  1456. }
  1457. /* check the channels */
  1458. if (fp->channels < ct->min || fp->channels > ct->max) {
  1459. hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
  1460. return 0;
  1461. }
  1462. /* check the rate is within the range */
  1463. if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
  1464. hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
  1465. return 0;
  1466. }
  1467. if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
  1468. hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
  1469. return 0;
  1470. }
  1471. /* check whether the period time is >= the data packet interval */
  1472. if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) {
  1473. ptime = 125 * (1 << fp->datainterval);
  1474. if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
  1475. hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
  1476. return 0;
  1477. }
  1478. }
  1479. return 1;
  1480. }
  1481. static int hw_rule_rate(struct snd_pcm_hw_params *params,
  1482. struct snd_pcm_hw_rule *rule)
  1483. {
  1484. struct snd_usb_substream *subs = rule->private;
  1485. struct list_head *p;
  1486. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  1487. unsigned int rmin, rmax;
  1488. int changed;
  1489. hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
  1490. changed = 0;
  1491. rmin = rmax = 0;
  1492. list_for_each(p, &subs->fmt_list) {
  1493. struct audioformat *fp;
  1494. fp = list_entry(p, struct audioformat, list);
  1495. if (!hw_check_valid_format(subs, params, fp))
  1496. continue;
  1497. if (changed++) {
  1498. if (rmin > fp->rate_min)
  1499. rmin = fp->rate_min;
  1500. if (rmax < fp->rate_max)
  1501. rmax = fp->rate_max;
  1502. } else {
  1503. rmin = fp->rate_min;
  1504. rmax = fp->rate_max;
  1505. }
  1506. }
  1507. if (!changed) {
  1508. hwc_debug(" --> get empty\n");
  1509. it->empty = 1;
  1510. return -EINVAL;
  1511. }
  1512. changed = 0;
  1513. if (it->min < rmin) {
  1514. it->min = rmin;
  1515. it->openmin = 0;
  1516. changed = 1;
  1517. }
  1518. if (it->max > rmax) {
  1519. it->max = rmax;
  1520. it->openmax = 0;
  1521. changed = 1;
  1522. }
  1523. if (snd_interval_checkempty(it)) {
  1524. it->empty = 1;
  1525. return -EINVAL;
  1526. }
  1527. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  1528. return changed;
  1529. }
  1530. static int hw_rule_channels(struct snd_pcm_hw_params *params,
  1531. struct snd_pcm_hw_rule *rule)
  1532. {
  1533. struct snd_usb_substream *subs = rule->private;
  1534. struct list_head *p;
  1535. struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  1536. unsigned int rmin, rmax;
  1537. int changed;
  1538. hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
  1539. changed = 0;
  1540. rmin = rmax = 0;
  1541. list_for_each(p, &subs->fmt_list) {
  1542. struct audioformat *fp;
  1543. fp = list_entry(p, struct audioformat, list);
  1544. if (!hw_check_valid_format(subs, params, fp))
  1545. continue;
  1546. if (changed++) {
  1547. if (rmin > fp->channels)
  1548. rmin = fp->channels;
  1549. if (rmax < fp->channels)
  1550. rmax = fp->channels;
  1551. } else {
  1552. rmin = fp->channels;
  1553. rmax = fp->channels;
  1554. }
  1555. }
  1556. if (!changed) {
  1557. hwc_debug(" --> get empty\n");
  1558. it->empty = 1;
  1559. return -EINVAL;
  1560. }
  1561. changed = 0;
  1562. if (it->min < rmin) {
  1563. it->min = rmin;
  1564. it->openmin = 0;
  1565. changed = 1;
  1566. }
  1567. if (it->max > rmax) {
  1568. it->max = rmax;
  1569. it->openmax = 0;
  1570. changed = 1;
  1571. }
  1572. if (snd_interval_checkempty(it)) {
  1573. it->empty = 1;
  1574. return -EINVAL;
  1575. }
  1576. hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
  1577. return changed;
  1578. }
  1579. static int hw_rule_format(struct snd_pcm_hw_params *params,
  1580. struct snd_pcm_hw_rule *rule)
  1581. {
  1582. struct snd_usb_substream *subs = rule->private;
  1583. struct list_head *p;
  1584. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1585. u64 fbits;
  1586. u32 oldbits[2];
  1587. int changed;
  1588. hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
  1589. fbits = 0;
  1590. list_for_each(p, &subs->fmt_list) {
  1591. struct audioformat *fp;
  1592. fp = list_entry(p, struct audioformat, list);
  1593. if (!hw_check_valid_format(subs, params, fp))
  1594. continue;
  1595. fbits |= (1ULL << fp->format);
  1596. }
  1597. oldbits[0] = fmt->bits[0];
  1598. oldbits[1] = fmt->bits[1];
  1599. fmt->bits[0] &= (u32)fbits;
  1600. fmt->bits[1] &= (u32)(fbits >> 32);
  1601. if (!fmt->bits[0] && !fmt->bits[1]) {
  1602. hwc_debug(" --> get empty\n");
  1603. return -EINVAL;
  1604. }
  1605. changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
  1606. hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
  1607. return changed;
  1608. }
  1609. static int hw_rule_period_time(struct snd_pcm_hw_params *params,
  1610. struct snd_pcm_hw_rule *rule)
  1611. {
  1612. struct snd_usb_substream *subs = rule->private;
  1613. struct audioformat *fp;
  1614. struct snd_interval *it;
  1615. unsigned char min_datainterval;
  1616. unsigned int pmin;
  1617. int changed;
  1618. it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
  1619. hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
  1620. min_datainterval = 0xff;
  1621. list_for_each_entry(fp, &subs->fmt_list, list) {
  1622. if (!hw_check_valid_format(subs, params, fp))
  1623. continue;
  1624. min_datainterval = min(min_datainterval, fp->datainterval);
  1625. }
  1626. if (min_datainterval == 0xff) {
  1627. hwc_debug(" --> get emtpy\n");
  1628. it->empty = 1;
  1629. return -EINVAL;
  1630. }
  1631. pmin = 125 * (1 << min_datainterval);
  1632. changed = 0;
  1633. if (it->min < pmin) {
  1634. it->min = pmin;
  1635. it->openmin = 0;
  1636. changed = 1;
  1637. }
  1638. if (snd_interval_checkempty(it)) {
  1639. it->empty = 1;
  1640. return -EINVAL;
  1641. }
  1642. hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
  1643. return changed;
  1644. }
  1645. /*
  1646. * If the device supports unusual bit rates, does the request meet these?
  1647. */
  1648. static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
  1649. struct snd_usb_substream *subs)
  1650. {
  1651. struct audioformat *fp;
  1652. int count = 0, needs_knot = 0;
  1653. int err;
  1654. list_for_each_entry(fp, &subs->fmt_list, list) {
  1655. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
  1656. return 0;
  1657. count += fp->nr_rates;
  1658. if (fp->rates & SNDRV_PCM_RATE_KNOT)
  1659. needs_knot = 1;
  1660. }
  1661. if (!needs_knot)
  1662. return 0;
  1663. subs->rate_list.count = count;
  1664. subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL);
  1665. subs->rate_list.mask = 0;
  1666. count = 0;
  1667. list_for_each_entry(fp, &subs->fmt_list, list) {
  1668. int i;
  1669. for (i = 0; i < fp->nr_rates; i++)
  1670. subs->rate_list.list[count++] = fp->rate_table[i];
  1671. }
  1672. err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1673. &subs->rate_list);
  1674. if (err < 0)
  1675. return err;
  1676. return 0;
  1677. }
  1678. /*
  1679. * set up the runtime hardware information.
  1680. */
  1681. static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
  1682. {
  1683. struct list_head *p;
  1684. unsigned int pt, ptmin;
  1685. int param_period_time_if_needed;
  1686. int err;
  1687. runtime->hw.formats = subs->formats;
  1688. runtime->hw.rate_min = 0x7fffffff;
  1689. runtime->hw.rate_max = 0;
  1690. runtime->hw.channels_min = 256;
  1691. runtime->hw.channels_max = 0;
  1692. runtime->hw.rates = 0;
  1693. ptmin = UINT_MAX;
  1694. /* check min/max rates and channels */
  1695. list_for_each(p, &subs->fmt_list) {
  1696. struct audioformat *fp;
  1697. fp = list_entry(p, struct audioformat, list);
  1698. runtime->hw.rates |= fp->rates;
  1699. if (runtime->hw.rate_min > fp->rate_min)
  1700. runtime->hw.rate_min = fp->rate_min;
  1701. if (runtime->hw.rate_max < fp->rate_max)
  1702. runtime->hw.rate_max = fp->rate_max;
  1703. if (runtime->hw.channels_min > fp->channels)
  1704. runtime->hw.channels_min = fp->channels;
  1705. if (runtime->hw.channels_max < fp->channels)
  1706. runtime->hw.channels_max = fp->channels;
  1707. if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
  1708. /* FIXME: there might be more than one audio formats... */
  1709. runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
  1710. fp->frame_size;
  1711. }
  1712. pt = 125 * (1 << fp->datainterval);
  1713. ptmin = min(ptmin, pt);
  1714. }
  1715. param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
  1716. if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH)
  1717. /* full speed devices have fixed data packet interval */
  1718. ptmin = 1000;
  1719. if (ptmin == 1000)
  1720. /* if period time doesn't go below 1 ms, no rules needed */
  1721. param_period_time_if_needed = -1;
  1722. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1723. ptmin, UINT_MAX);
  1724. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1725. hw_rule_rate, subs,
  1726. SNDRV_PCM_HW_PARAM_FORMAT,
  1727. SNDRV_PCM_HW_PARAM_CHANNELS,
  1728. param_period_time_if_needed,
  1729. -1)) < 0)
  1730. return err;
  1731. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  1732. hw_rule_channels, subs,
  1733. SNDRV_PCM_HW_PARAM_FORMAT,
  1734. SNDRV_PCM_HW_PARAM_RATE,
  1735. param_period_time_if_needed,
  1736. -1)) < 0)
  1737. return err;
  1738. if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  1739. hw_rule_format, subs,
  1740. SNDRV_PCM_HW_PARAM_RATE,
  1741. SNDRV_PCM_HW_PARAM_CHANNELS,
  1742. param_period_time_if_needed,
  1743. -1)) < 0)
  1744. return err;
  1745. if (param_period_time_if_needed >= 0) {
  1746. err = snd_pcm_hw_rule_add(runtime, 0,
  1747. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1748. hw_rule_period_time, subs,
  1749. SNDRV_PCM_HW_PARAM_FORMAT,
  1750. SNDRV_PCM_HW_PARAM_CHANNELS,
  1751. SNDRV_PCM_HW_PARAM_RATE,
  1752. -1);
  1753. if (err < 0)
  1754. return err;
  1755. }
  1756. if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
  1757. return err;
  1758. return 0;
  1759. }
  1760. static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
  1761. {
  1762. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  1763. struct snd_pcm_runtime *runtime = substream->runtime;
  1764. struct snd_usb_substream *subs = &as->substream[direction];
  1765. subs->interface = -1;
  1766. subs->format = 0;
  1767. runtime->hw = snd_usb_hardware;
  1768. runtime->private_data = subs;
  1769. subs->pcm_substream = substream;
  1770. return setup_hw_info(runtime, subs);
  1771. }
  1772. static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
  1773. {
  1774. struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
  1775. struct snd_usb_substream *subs = &as->substream[direction];
  1776. if (!as->chip->shutdown && subs->interface >= 0) {
  1777. usb_set_interface(subs->dev, subs->interface, 0);
  1778. subs->interface = -1;
  1779. }
  1780. subs->pcm_substream = NULL;
  1781. return 0;
  1782. }
  1783. static int snd_usb_playback_open(struct snd_pcm_substream *substream)
  1784. {
  1785. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1786. }
  1787. static int snd_usb_playback_close(struct snd_pcm_substream *substream)
  1788. {
  1789. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
  1790. }
  1791. static int snd_usb_capture_open(struct snd_pcm_substream *substream)
  1792. {
  1793. return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
  1794. }
  1795. static int snd_usb_capture_close(struct snd_pcm_substream *substream)
  1796. {
  1797. return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
  1798. }
  1799. static struct snd_pcm_ops snd_usb_playback_ops = {
  1800. .open = snd_usb_playback_open,
  1801. .close = snd_usb_playback_close,
  1802. .ioctl = snd_pcm_lib_ioctl,
  1803. .hw_params = snd_usb_hw_params,
  1804. .hw_free = snd_usb_hw_free,
  1805. .prepare = snd_usb_pcm_prepare,
  1806. .trigger = snd_usb_pcm_playback_trigger,
  1807. .pointer = snd_usb_pcm_pointer,
  1808. .page = snd_pcm_lib_get_vmalloc_page,
  1809. .mmap = snd_pcm_lib_mmap_vmalloc,
  1810. };
  1811. static struct snd_pcm_ops snd_usb_capture_ops = {
  1812. .open = snd_usb_capture_open,
  1813. .close = snd_usb_capture_close,
  1814. .ioctl = snd_pcm_lib_ioctl,
  1815. .hw_params = snd_usb_hw_params,
  1816. .hw_free = snd_usb_hw_free,
  1817. .prepare = snd_usb_pcm_prepare,
  1818. .trigger = snd_usb_pcm_capture_trigger,
  1819. .pointer = snd_usb_pcm_pointer,
  1820. .page = snd_pcm_lib_get_vmalloc_page,
  1821. .mmap = snd_pcm_lib_mmap_vmalloc,
  1822. };
  1823. /*
  1824. * helper functions
  1825. */
  1826. /*
  1827. * combine bytes and get an integer value
  1828. */
  1829. unsigned int snd_usb_combine_bytes(unsigned char *bytes, int size)
  1830. {
  1831. switch (size) {
  1832. case 1: return *bytes;
  1833. case 2: return combine_word(bytes);
  1834. case 3: return combine_triple(bytes);
  1835. case 4: return combine_quad(bytes);
  1836. default: return 0;
  1837. }
  1838. }
  1839. /*
  1840. * parse descriptor buffer and return the pointer starting the given
  1841. * descriptor type.
  1842. */
  1843. void *snd_usb_find_desc(void *descstart, int desclen, void *after, u8 dtype)
  1844. {
  1845. u8 *p, *end, *next;
  1846. p = descstart;
  1847. end = p + desclen;
  1848. for (; p < end;) {
  1849. if (p[0] < 2)
  1850. return NULL;
  1851. next = p + p[0];
  1852. if (next > end)
  1853. return NULL;
  1854. if (p[1] == dtype && (!after || (void *)p > after)) {
  1855. return p;
  1856. }
  1857. p = next;
  1858. }
  1859. return NULL;
  1860. }
  1861. /*
  1862. * find a class-specified interface descriptor with the given subtype.
  1863. */
  1864. void *snd_usb_find_csint_desc(void *buffer, int buflen, void *after, u8 dsubtype)
  1865. {
  1866. unsigned char *p = after;
  1867. while ((p = snd_usb_find_desc(buffer, buflen, p,
  1868. USB_DT_CS_INTERFACE)) != NULL) {
  1869. if (p[0] >= 3 && p[2] == dsubtype)
  1870. return p;
  1871. }
  1872. return NULL;
  1873. }
  1874. /*
  1875. * Wrapper for usb_control_msg().
  1876. * Allocates a temp buffer to prevent dmaing from/to the stack.
  1877. */
  1878. int snd_usb_ctl_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
  1879. __u8 requesttype, __u16 value, __u16 index, void *data,
  1880. __u16 size, int timeout)
  1881. {
  1882. int err;
  1883. void *buf = NULL;
  1884. if (size > 0) {
  1885. buf = kmemdup(data, size, GFP_KERNEL);
  1886. if (!buf)
  1887. return -ENOMEM;
  1888. }
  1889. err = usb_control_msg(dev, pipe, request, requesttype,
  1890. value, index, buf, size, timeout);
  1891. if (size > 0) {
  1892. memcpy(data, buf, size);
  1893. kfree(buf);
  1894. }
  1895. return err;
  1896. }
  1897. /*
  1898. * entry point for linux usb interface
  1899. */
  1900. static int usb_audio_probe(struct usb_interface *intf,
  1901. const struct usb_device_id *id);
  1902. static void usb_audio_disconnect(struct usb_interface *intf);
  1903. #ifdef CONFIG_PM
  1904. static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message);
  1905. static int usb_audio_resume(struct usb_interface *intf);
  1906. #else
  1907. #define usb_audio_suspend NULL
  1908. #define usb_audio_resume NULL
  1909. #endif
  1910. static struct usb_device_id usb_audio_ids [] = {
  1911. #include "usbquirks.h"
  1912. { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
  1913. .bInterfaceClass = USB_CLASS_AUDIO,
  1914. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
  1915. { } /* Terminating entry */
  1916. };
  1917. MODULE_DEVICE_TABLE (usb, usb_audio_ids);
  1918. static struct usb_driver usb_audio_driver = {
  1919. .name = "snd-usb-audio",
  1920. .probe = usb_audio_probe,
  1921. .disconnect = usb_audio_disconnect,
  1922. .suspend = usb_audio_suspend,
  1923. .resume = usb_audio_resume,
  1924. .id_table = usb_audio_ids,
  1925. };
  1926. #if defined(CONFIG_PROC_FS) && defined(CONFIG_SND_VERBOSE_PROCFS)
  1927. /*
  1928. * proc interface for list the supported pcm formats
  1929. */
  1930. static void proc_dump_substream_formats(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
  1931. {
  1932. struct list_head *p;
  1933. static char *sync_types[4] = {
  1934. "NONE", "ASYNC", "ADAPTIVE", "SYNC"
  1935. };
  1936. list_for_each(p, &subs->fmt_list) {
  1937. struct audioformat *fp;
  1938. fp = list_entry(p, struct audioformat, list);
  1939. snd_iprintf(buffer, " Interface %d\n", fp->iface);
  1940. snd_iprintf(buffer, " Altset %d\n", fp->altsetting);
  1941. snd_iprintf(buffer, " Format: %s\n",
  1942. snd_pcm_format_name(fp->format));
  1943. snd_iprintf(buffer, " Channels: %d\n", fp->channels);
  1944. snd_iprintf(buffer, " Endpoint: %d %s (%s)\n",
  1945. fp->endpoint & USB_ENDPOINT_NUMBER_MASK,
  1946. fp->endpoint & USB_DIR_IN ? "IN" : "OUT",
  1947. sync_types[(fp->ep_attr & USB_ENDPOINT_SYNCTYPE) >> 2]);
  1948. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
  1949. snd_iprintf(buffer, " Rates: %d - %d (continuous)\n",
  1950. fp->rate_min, fp->rate_max);
  1951. } else {
  1952. unsigned int i;
  1953. snd_iprintf(buffer, " Rates: ");
  1954. for (i = 0; i < fp->nr_rates; i++) {
  1955. if (i > 0)
  1956. snd_iprintf(buffer, ", ");
  1957. snd_iprintf(buffer, "%d", fp->rate_table[i]);
  1958. }
  1959. snd_iprintf(buffer, "\n");
  1960. }
  1961. if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
  1962. snd_iprintf(buffer, " Data packet interval: %d us\n",
  1963. 125 * (1 << fp->datainterval));
  1964. // snd_iprintf(buffer, " Max Packet Size = %d\n", fp->maxpacksize);
  1965. // snd_iprintf(buffer, " EP Attribute = %#x\n", fp->attributes);
  1966. }
  1967. }
  1968. static void proc_dump_substream_status(struct snd_usb_substream *subs, struct snd_info_buffer *buffer)
  1969. {
  1970. if (subs->running) {
  1971. unsigned int i;
  1972. snd_iprintf(buffer, " Status: Running\n");
  1973. snd_iprintf(buffer, " Interface = %d\n", subs->interface);
  1974. snd_iprintf(buffer, " Altset = %d\n", subs->format);
  1975. snd_iprintf(buffer, " URBs = %d [ ", subs->nurbs);
  1976. for (i = 0; i < subs->nurbs; i++)
  1977. snd_iprintf(buffer, "%d ", subs->dataurb[i].packets);
  1978. snd_iprintf(buffer, "]\n");
  1979. snd_iprintf(buffer, " Packet Size = %d\n", subs->curpacksize);
  1980. snd_iprintf(buffer, " Momentary freq = %u Hz (%#x.%04x)\n",
  1981. snd_usb_get_speed(subs->dev) == USB_SPEED_FULL
  1982. ? get_full_speed_hz(subs->freqm)
  1983. : get_high_speed_hz(subs->freqm),
  1984. subs->freqm >> 16, subs->freqm & 0xffff);
  1985. } else {
  1986. snd_iprintf(buffer, " Status: Stop\n");
  1987. }
  1988. }
  1989. static void proc_pcm_format_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  1990. {
  1991. struct snd_usb_stream *stream = entry->private_data;
  1992. snd_iprintf(buffer, "%s : %s\n", stream->chip->card->longname, stream->pcm->name);
  1993. if (stream->substream[SNDRV_PCM_STREAM_PLAYBACK].num_formats) {
  1994. snd_iprintf(buffer, "\nPlayback:\n");
  1995. proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
  1996. proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_PLAYBACK], buffer);
  1997. }
  1998. if (stream->substream[SNDRV_PCM_STREAM_CAPTURE].num_formats) {
  1999. snd_iprintf(buffer, "\nCapture:\n");
  2000. proc_dump_substream_status(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
  2001. proc_dump_substream_formats(&stream->substream[SNDRV_PCM_STREAM_CAPTURE], buffer);
  2002. }
  2003. }
  2004. static void proc_pcm_format_add(struct snd_usb_stream *stream)
  2005. {
  2006. struct snd_info_entry *entry;
  2007. char name[32];
  2008. struct snd_card *card = stream->chip->card;
  2009. sprintf(name, "stream%d", stream->pcm_index);
  2010. if (!snd_card_proc_new(card, name, &entry))
  2011. snd_info_set_text_ops(entry, stream, proc_pcm_format_read);
  2012. }
  2013. #else
  2014. static inline void proc_pcm_format_add(struct snd_usb_stream *stream)
  2015. {
  2016. }
  2017. #endif
  2018. /*
  2019. * initialize the substream instance.
  2020. */
  2021. static void init_substream(struct snd_usb_stream *as, int stream, struct audioformat *fp)
  2022. {
  2023. struct snd_usb_substream *subs = &as->substream[stream];
  2024. INIT_LIST_HEAD(&subs->fmt_list);
  2025. spin_lock_init(&subs->lock);
  2026. subs->stream = as;
  2027. subs->direction = stream;
  2028. subs->dev = as->chip->dev;
  2029. subs->txfr_quirk = as->chip->txfr_quirk;
  2030. if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {
  2031. subs->ops = audio_urb_ops[stream];
  2032. } else {
  2033. subs->ops = audio_urb_ops_high_speed[stream];
  2034. switch (as->chip->usb_id) {
  2035. case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
  2036. case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
  2037. case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
  2038. subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;
  2039. break;
  2040. }
  2041. }
  2042. snd_pcm_set_ops(as->pcm, stream,
  2043. stream == SNDRV_PCM_STREAM_PLAYBACK ?
  2044. &snd_usb_playback_ops : &snd_usb_capture_ops);
  2045. list_add_tail(&fp->list, &subs->fmt_list);
  2046. subs->formats |= 1ULL << fp->format;
  2047. subs->endpoint = fp->endpoint;
  2048. subs->num_formats++;
  2049. subs->fmt_type = fp->fmt_type;
  2050. }
  2051. /*
  2052. * free a substream
  2053. */
  2054. static void free_substream(struct snd_usb_substream *subs)
  2055. {
  2056. struct list_head *p, *n;
  2057. if (!subs->num_formats)
  2058. return; /* not initialized */
  2059. list_for_each_safe(p, n, &subs->fmt_list) {
  2060. struct audioformat *fp = list_entry(p, struct audioformat, list);
  2061. kfree(fp->rate_table);
  2062. kfree(fp);
  2063. }
  2064. kfree(subs->rate_list.list);
  2065. }
  2066. /*
  2067. * free a usb stream instance
  2068. */
  2069. static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
  2070. {
  2071. free_substream(&stream->substream[0]);
  2072. free_substream(&stream->substream[1]);
  2073. list_del(&stream->list);
  2074. kfree(stream);
  2075. }
  2076. static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
  2077. {
  2078. struct snd_usb_stream *stream = pcm->private_data;
  2079. if (stream) {
  2080. stream->pcm = NULL;
  2081. snd_usb_audio_stream_free(stream);
  2082. }
  2083. }
  2084. /*
  2085. * add this endpoint to the chip instance.
  2086. * if a stream with the same endpoint already exists, append to it.
  2087. * if not, create a new pcm stream.
  2088. */
  2089. static int add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp)
  2090. {
  2091. struct list_head *p;
  2092. struct snd_usb_stream *as;
  2093. struct snd_usb_substream *subs;
  2094. struct snd_pcm *pcm;
  2095. int err;
  2096. list_for_each(p, &chip->pcm_list) {
  2097. as = list_entry(p, struct snd_usb_stream, list);
  2098. if (as->fmt_type != fp->fmt_type)
  2099. continue;
  2100. subs = &as->substream[stream];
  2101. if (!subs->endpoint)
  2102. continue;
  2103. if (subs->endpoint == fp->endpoint) {
  2104. list_add_tail(&fp->list, &subs->fmt_list);
  2105. subs->num_formats++;
  2106. subs->formats |= 1ULL << fp->format;
  2107. return 0;
  2108. }
  2109. }
  2110. /* look for an empty stream */
  2111. list_for_each(p, &chip->pcm_list) {
  2112. as = list_entry(p, struct snd_usb_stream, list);
  2113. if (as->fmt_type != fp->fmt_type)
  2114. continue;
  2115. subs = &as->substream[stream];
  2116. if (subs->endpoint)
  2117. continue;
  2118. err = snd_pcm_new_stream(as->pcm, stream, 1);
  2119. if (err < 0)
  2120. return err;
  2121. init_substream(as, stream, fp);
  2122. return 0;
  2123. }
  2124. /* create a new pcm */
  2125. as = kzalloc(sizeof(*as), GFP_KERNEL);
  2126. if (!as)
  2127. return -ENOMEM;
  2128. as->pcm_index = chip->pcm_devs;
  2129. as->chip = chip;
  2130. as->fmt_type = fp->fmt_type;
  2131. err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
  2132. stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
  2133. stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
  2134. &pcm);
  2135. if (err < 0) {
  2136. kfree(as);
  2137. return err;
  2138. }
  2139. as->pcm = pcm;
  2140. pcm->private_data = as;
  2141. pcm->private_free = snd_usb_audio_pcm_free;
  2142. pcm->info_flags = 0;
  2143. if (chip->pcm_devs > 0)
  2144. sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
  2145. else
  2146. strcpy(pcm->name, "USB Audio");
  2147. init_substream(as, stream, fp);
  2148. list_add(&as->list, &chip->pcm_list);
  2149. chip->pcm_devs++;
  2150. proc_pcm_format_add(as);
  2151. return 0;
  2152. }
  2153. /*
  2154. * check if the device uses big-endian samples
  2155. */
  2156. static int is_big_endian_format(struct snd_usb_audio *chip, struct audioformat *fp)
  2157. {
  2158. switch (chip->usb_id) {
  2159. case USB_ID(0x0763, 0x2001): /* M-Audio Quattro: captured data only */
  2160. if (fp->endpoint & USB_DIR_IN)
  2161. return 1;
  2162. break;
  2163. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  2164. if (device_setup[chip->index] == 0x00 ||
  2165. fp->altsetting==1 || fp->altsetting==2 || fp->altsetting==3)
  2166. return 1;
  2167. }
  2168. return 0;
  2169. }
  2170. /*
  2171. * parse the audio format type I descriptor
  2172. * and returns the corresponding pcm format
  2173. *
  2174. * @dev: usb device
  2175. * @fp: audioformat record
  2176. * @format: the format tag (wFormatTag)
  2177. * @fmt: the format type descriptor
  2178. */
  2179. static int parse_audio_format_i_type(struct snd_usb_audio *chip,
  2180. struct audioformat *fp,
  2181. int format, void *_fmt,
  2182. int protocol)
  2183. {
  2184. int pcm_format, i;
  2185. int sample_width, sample_bytes;
  2186. switch (protocol) {
  2187. case UAC_VERSION_1: {
  2188. struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
  2189. sample_width = fmt->bBitResolution;
  2190. sample_bytes = fmt->bSubframeSize;
  2191. break;
  2192. }
  2193. case UAC_VERSION_2: {
  2194. struct uac_format_type_i_ext_descriptor *fmt = _fmt;
  2195. sample_width = fmt->bBitResolution;
  2196. sample_bytes = fmt->bSubslotSize;
  2197. /*
  2198. * FIXME
  2199. * USB audio class v2 devices specify a bitmap of possible
  2200. * audio formats rather than one fix value. For now, we just
  2201. * pick one of them and report that as the only possible
  2202. * value for this setting.
  2203. * The bit allocation map is in fact compatible to the
  2204. * wFormatTag of the v1 AS streaming descriptors, which is why
  2205. * we can simply map the matrix.
  2206. */
  2207. for (i = 0; i < 5; i++)
  2208. if (format & (1UL << i)) {
  2209. format = i + 1;
  2210. break;
  2211. }
  2212. break;
  2213. }
  2214. default:
  2215. return -EINVAL;
  2216. }
  2217. /* FIXME: correct endianess and sign? */
  2218. pcm_format = -1;
  2219. switch (format) {
  2220. case UAC_FORMAT_TYPE_I_UNDEFINED: /* some devices don't define this correctly... */
  2221. snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
  2222. chip->dev->devnum, fp->iface, fp->altsetting);
  2223. /* fall-through */
  2224. case UAC_FORMAT_TYPE_I_PCM:
  2225. if (sample_width > sample_bytes * 8) {
  2226. snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
  2227. chip->dev->devnum, fp->iface, fp->altsetting,
  2228. sample_width, sample_bytes);
  2229. }
  2230. /* check the format byte size */
  2231. switch (sample_bytes) {
  2232. case 1:
  2233. pcm_format = SNDRV_PCM_FORMAT_S8;
  2234. break;
  2235. case 2:
  2236. if (is_big_endian_format(chip, fp))
  2237. pcm_format = SNDRV_PCM_FORMAT_S16_BE; /* grrr, big endian!! */
  2238. else
  2239. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  2240. break;
  2241. case 3:
  2242. if (is_big_endian_format(chip, fp))
  2243. pcm_format = SNDRV_PCM_FORMAT_S24_3BE; /* grrr, big endian!! */
  2244. else
  2245. pcm_format = SNDRV_PCM_FORMAT_S24_3LE;
  2246. break;
  2247. case 4:
  2248. pcm_format = SNDRV_PCM_FORMAT_S32_LE;
  2249. break;
  2250. default:
  2251. snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
  2252. chip->dev->devnum, fp->iface, fp->altsetting,
  2253. sample_width, sample_bytes);
  2254. break;
  2255. }
  2256. break;
  2257. case UAC_FORMAT_TYPE_I_PCM8:
  2258. pcm_format = SNDRV_PCM_FORMAT_U8;
  2259. /* Dallas DS4201 workaround: it advertises U8 format, but really
  2260. supports S8. */
  2261. if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  2262. pcm_format = SNDRV_PCM_FORMAT_S8;
  2263. break;
  2264. case UAC_FORMAT_TYPE_I_IEEE_FLOAT:
  2265. pcm_format = SNDRV_PCM_FORMAT_FLOAT_LE;
  2266. break;
  2267. case UAC_FORMAT_TYPE_I_ALAW:
  2268. pcm_format = SNDRV_PCM_FORMAT_A_LAW;
  2269. break;
  2270. case UAC_FORMAT_TYPE_I_MULAW:
  2271. pcm_format = SNDRV_PCM_FORMAT_MU_LAW;
  2272. break;
  2273. default:
  2274. snd_printk(KERN_INFO "%d:%u:%d : unsupported format type %d\n",
  2275. chip->dev->devnum, fp->iface, fp->altsetting, format);
  2276. break;
  2277. }
  2278. return pcm_format;
  2279. }
  2280. /*
  2281. * parse the format descriptor and stores the possible sample rates
  2282. * on the audioformat table (audio class v1).
  2283. *
  2284. * @dev: usb device
  2285. * @fp: audioformat record
  2286. * @fmt: the format descriptor
  2287. * @offset: the start offset of descriptor pointing the rate type
  2288. * (7 for type I and II, 8 for type II)
  2289. */
  2290. static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
  2291. unsigned char *fmt, int offset)
  2292. {
  2293. int nr_rates = fmt[offset];
  2294. if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
  2295. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  2296. chip->dev->devnum, fp->iface, fp->altsetting);
  2297. return -1;
  2298. }
  2299. if (nr_rates) {
  2300. /*
  2301. * build the rate table and bitmap flags
  2302. */
  2303. int r, idx;
  2304. fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
  2305. if (fp->rate_table == NULL) {
  2306. snd_printk(KERN_ERR "cannot malloc\n");
  2307. return -1;
  2308. }
  2309. fp->nr_rates = 0;
  2310. fp->rate_min = fp->rate_max = 0;
  2311. for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
  2312. unsigned int rate = combine_triple(&fmt[idx]);
  2313. if (!rate)
  2314. continue;
  2315. /* C-Media CM6501 mislabels its 96 kHz altsetting */
  2316. if (rate == 48000 && nr_rates == 1 &&
  2317. (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
  2318. chip->usb_id == USB_ID(0x0d8c, 0x0102)) &&
  2319. fp->altsetting == 5 && fp->maxpacksize == 392)
  2320. rate = 96000;
  2321. /* Creative VF0470 Live Cam reports 16 kHz instead of 8kHz */
  2322. if (rate == 16000 && chip->usb_id == USB_ID(0x041e, 0x4068))
  2323. rate = 8000;
  2324. fp->rate_table[fp->nr_rates] = rate;
  2325. if (!fp->rate_min || rate < fp->rate_min)
  2326. fp->rate_min = rate;
  2327. if (!fp->rate_max || rate > fp->rate_max)
  2328. fp->rate_max = rate;
  2329. fp->rates |= snd_pcm_rate_to_rate_bit(rate);
  2330. fp->nr_rates++;
  2331. }
  2332. if (!fp->nr_rates) {
  2333. hwc_debug("All rates were zero. Skipping format!\n");
  2334. return -1;
  2335. }
  2336. } else {
  2337. /* continuous rates */
  2338. fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
  2339. fp->rate_min = combine_triple(&fmt[offset + 1]);
  2340. fp->rate_max = combine_triple(&fmt[offset + 4]);
  2341. }
  2342. return 0;
  2343. }
  2344. /*
  2345. * parse the format descriptor and stores the possible sample rates
  2346. * on the audioformat table (audio class v2).
  2347. */
  2348. static int parse_audio_format_rates_v2(struct snd_usb_audio *chip,
  2349. struct audioformat *fp,
  2350. struct usb_host_interface *iface)
  2351. {
  2352. struct usb_device *dev = chip->dev;
  2353. unsigned char tmp[2], *data;
  2354. int i, nr_rates, data_size, ret = 0;
  2355. /* get the number of sample rates first by only fetching 2 bytes */
  2356. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  2357. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  2358. 0x0100, chip->clock_id << 8, tmp, sizeof(tmp), 1000);
  2359. if (ret < 0) {
  2360. snd_printk(KERN_ERR "unable to retrieve number of sample rates\n");
  2361. goto err;
  2362. }
  2363. nr_rates = (tmp[1] << 8) | tmp[0];
  2364. data_size = 2 + 12 * nr_rates;
  2365. data = kzalloc(data_size, GFP_KERNEL);
  2366. if (!data) {
  2367. ret = -ENOMEM;
  2368. goto err;
  2369. }
  2370. /* now get the full information */
  2371. ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
  2372. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  2373. 0x0100, chip->clock_id << 8, data, data_size, 1000);
  2374. if (ret < 0) {
  2375. snd_printk(KERN_ERR "unable to retrieve sample rate range\n");
  2376. ret = -EINVAL;
  2377. goto err_free;
  2378. }
  2379. fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
  2380. if (!fp->rate_table) {
  2381. ret = -ENOMEM;
  2382. goto err_free;
  2383. }
  2384. fp->nr_rates = 0;
  2385. fp->rate_min = fp->rate_max = 0;
  2386. for (i = 0; i < nr_rates; i++) {
  2387. int rate = combine_quad(&data[2 + 12 * i]);
  2388. fp->rate_table[fp->nr_rates] = rate;
  2389. if (!fp->rate_min || rate < fp->rate_min)
  2390. fp->rate_min = rate;
  2391. if (!fp->rate_max || rate > fp->rate_max)
  2392. fp->rate_max = rate;
  2393. fp->rates |= snd_pcm_rate_to_rate_bit(rate);
  2394. fp->nr_rates++;
  2395. }
  2396. err_free:
  2397. kfree(data);
  2398. err:
  2399. return ret;
  2400. }
  2401. /*
  2402. * parse the format type I and III descriptors
  2403. */
  2404. static int parse_audio_format_i(struct snd_usb_audio *chip,
  2405. struct audioformat *fp,
  2406. int format, void *_fmt,
  2407. struct usb_host_interface *iface)
  2408. {
  2409. struct usb_interface_descriptor *altsd = get_iface_desc(iface);
  2410. struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
  2411. int protocol = altsd->bInterfaceProtocol;
  2412. int pcm_format, ret;
  2413. if (fmt->bFormatType == UAC_FORMAT_TYPE_III) {
  2414. /* FIXME: the format type is really IECxxx
  2415. * but we give normal PCM format to get the existing
  2416. * apps working...
  2417. */
  2418. switch (chip->usb_id) {
  2419. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  2420. if (device_setup[chip->index] == 0x00 &&
  2421. fp->altsetting == 6)
  2422. pcm_format = SNDRV_PCM_FORMAT_S16_BE;
  2423. else
  2424. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  2425. break;
  2426. default:
  2427. pcm_format = SNDRV_PCM_FORMAT_S16_LE;
  2428. }
  2429. } else {
  2430. pcm_format = parse_audio_format_i_type(chip, fp, format, fmt, protocol);
  2431. if (pcm_format < 0)
  2432. return -1;
  2433. }
  2434. fp->format = pcm_format;
  2435. /* gather possible sample rates */
  2436. /* audio class v1 reports possible sample rates as part of the
  2437. * proprietary class specific descriptor.
  2438. * audio class v2 uses class specific EP0 range requests for that.
  2439. */
  2440. switch (protocol) {
  2441. case UAC_VERSION_1:
  2442. fp->channels = fmt->bNrChannels;
  2443. ret = parse_audio_format_rates_v1(chip, fp, _fmt, 7);
  2444. break;
  2445. case UAC_VERSION_2:
  2446. /* fp->channels is already set in this case */
  2447. ret = parse_audio_format_rates_v2(chip, fp, iface);
  2448. break;
  2449. }
  2450. if (fp->channels < 1) {
  2451. snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
  2452. chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
  2453. return -1;
  2454. }
  2455. return ret;
  2456. }
  2457. /*
  2458. * parse the format type II descriptor
  2459. */
  2460. static int parse_audio_format_ii(struct snd_usb_audio *chip,
  2461. struct audioformat *fp,
  2462. int format, void *_fmt,
  2463. struct usb_host_interface *iface)
  2464. {
  2465. int brate, framesize, ret;
  2466. struct usb_interface_descriptor *altsd = get_iface_desc(iface);
  2467. int protocol = altsd->bInterfaceProtocol;
  2468. switch (format) {
  2469. case UAC_FORMAT_TYPE_II_AC3:
  2470. /* FIXME: there is no AC3 format defined yet */
  2471. // fp->format = SNDRV_PCM_FORMAT_AC3;
  2472. fp->format = SNDRV_PCM_FORMAT_U8; /* temporarily hack to receive byte streams */
  2473. break;
  2474. case UAC_FORMAT_TYPE_II_MPEG:
  2475. fp->format = SNDRV_PCM_FORMAT_MPEG;
  2476. break;
  2477. default:
  2478. snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
  2479. chip->dev->devnum, fp->iface, fp->altsetting, format);
  2480. fp->format = SNDRV_PCM_FORMAT_MPEG;
  2481. break;
  2482. }
  2483. fp->channels = 1;
  2484. switch (protocol) {
  2485. case UAC_VERSION_1: {
  2486. struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
  2487. brate = le16_to_cpu(fmt->wMaxBitRate);
  2488. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  2489. snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  2490. fp->frame_size = framesize;
  2491. ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
  2492. break;
  2493. }
  2494. case UAC_VERSION_2: {
  2495. struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
  2496. brate = le16_to_cpu(fmt->wMaxBitRate);
  2497. framesize = le16_to_cpu(fmt->wSamplesPerFrame);
  2498. snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
  2499. fp->frame_size = framesize;
  2500. ret = parse_audio_format_rates_v2(chip, fp, iface);
  2501. break;
  2502. }
  2503. }
  2504. return ret;
  2505. }
  2506. static int parse_audio_format(struct snd_usb_audio *chip, struct audioformat *fp,
  2507. int format, unsigned char *fmt, int stream,
  2508. struct usb_host_interface *iface)
  2509. {
  2510. int err;
  2511. switch (fmt[3]) {
  2512. case UAC_FORMAT_TYPE_I:
  2513. case UAC_FORMAT_TYPE_III:
  2514. err = parse_audio_format_i(chip, fp, format, fmt, iface);
  2515. break;
  2516. case UAC_FORMAT_TYPE_II:
  2517. err = parse_audio_format_ii(chip, fp, format, fmt, iface);
  2518. break;
  2519. default:
  2520. snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
  2521. chip->dev->devnum, fp->iface, fp->altsetting, fmt[3]);
  2522. return -1;
  2523. }
  2524. fp->fmt_type = fmt[3];
  2525. if (err < 0)
  2526. return err;
  2527. #if 1
  2528. /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
  2529. /* extigy apparently supports sample rates other than 48k
  2530. * but not in ordinary way. so we enable only 48k atm.
  2531. */
  2532. if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
  2533. chip->usb_id == USB_ID(0x041e, 0x3020) ||
  2534. chip->usb_id == USB_ID(0x041e, 0x3061)) {
  2535. if (fmt[3] == UAC_FORMAT_TYPE_I &&
  2536. fp->rates != SNDRV_PCM_RATE_48000 &&
  2537. fp->rates != SNDRV_PCM_RATE_96000)
  2538. return -1;
  2539. }
  2540. #endif
  2541. return 0;
  2542. }
  2543. static unsigned char parse_datainterval(struct snd_usb_audio *chip,
  2544. struct usb_host_interface *alts)
  2545. {
  2546. if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH &&
  2547. get_endpoint(alts, 0)->bInterval >= 1 &&
  2548. get_endpoint(alts, 0)->bInterval <= 4)
  2549. return get_endpoint(alts, 0)->bInterval - 1;
  2550. else
  2551. return 0;
  2552. }
  2553. static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
  2554. int iface, int altno);
  2555. static int parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no)
  2556. {
  2557. struct usb_device *dev;
  2558. struct usb_interface *iface;
  2559. struct usb_host_interface *alts;
  2560. struct usb_interface_descriptor *altsd;
  2561. int i, altno, err, stream;
  2562. int format = 0, num_channels = 0;
  2563. struct audioformat *fp = NULL;
  2564. unsigned char *fmt, *csep;
  2565. int num, protocol;
  2566. dev = chip->dev;
  2567. /* parse the interface's altsettings */
  2568. iface = usb_ifnum_to_if(dev, iface_no);
  2569. num = iface->num_altsetting;
  2570. /*
  2571. * Dallas DS4201 workaround: It presents 5 altsettings, but the last
  2572. * one misses syncpipe, and does not produce any sound.
  2573. */
  2574. if (chip->usb_id == USB_ID(0x04fa, 0x4201))
  2575. num = 4;
  2576. for (i = 0; i < num; i++) {
  2577. alts = &iface->altsetting[i];
  2578. altsd = get_iface_desc(alts);
  2579. protocol = altsd->bInterfaceProtocol;
  2580. /* skip invalid one */
  2581. if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  2582. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  2583. (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
  2584. altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
  2585. altsd->bNumEndpoints < 1 ||
  2586. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
  2587. continue;
  2588. /* must be isochronous */
  2589. if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  2590. USB_ENDPOINT_XFER_ISOC)
  2591. continue;
  2592. /* check direction */
  2593. stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
  2594. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  2595. altno = altsd->bAlternateSetting;
  2596. /* audiophile usb: skip altsets incompatible with device_setup
  2597. */
  2598. if (chip->usb_id == USB_ID(0x0763, 0x2003) &&
  2599. audiophile_skip_setting_quirk(chip, iface_no, altno))
  2600. continue;
  2601. /* get audio formats */
  2602. switch (protocol) {
  2603. case UAC_VERSION_1: {
  2604. struct uac_as_header_descriptor_v1 *as =
  2605. snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  2606. if (!as) {
  2607. snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  2608. dev->devnum, iface_no, altno);
  2609. continue;
  2610. }
  2611. if (as->bLength < sizeof(*as)) {
  2612. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  2613. dev->devnum, iface_no, altno);
  2614. continue;
  2615. }
  2616. format = le16_to_cpu(as->wFormatTag); /* remember the format value */
  2617. break;
  2618. }
  2619. case UAC_VERSION_2: {
  2620. struct uac_as_header_descriptor_v2 *as =
  2621. snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
  2622. if (!as) {
  2623. snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
  2624. dev->devnum, iface_no, altno);
  2625. continue;
  2626. }
  2627. if (as->bLength < sizeof(*as)) {
  2628. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
  2629. dev->devnum, iface_no, altno);
  2630. continue;
  2631. }
  2632. num_channels = as->bNrChannels;
  2633. format = le32_to_cpu(as->bmFormats);
  2634. break;
  2635. }
  2636. default:
  2637. snd_printk(KERN_ERR "%d:%u:%d : unknown interface protocol %04x\n",
  2638. dev->devnum, iface_no, altno, protocol);
  2639. continue;
  2640. }
  2641. /* get format type */
  2642. fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
  2643. if (!fmt) {
  2644. snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
  2645. dev->devnum, iface_no, altno);
  2646. continue;
  2647. }
  2648. if (((protocol == UAC_VERSION_1) && (fmt[0] < 8)) ||
  2649. ((protocol == UAC_VERSION_2) && (fmt[0] != 6))) {
  2650. snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
  2651. dev->devnum, iface_no, altno);
  2652. continue;
  2653. }
  2654. /*
  2655. * Blue Microphones workaround: The last altsetting is identical
  2656. * with the previous one, except for a larger packet size, but
  2657. * is actually a mislabeled two-channel setting; ignore it.
  2658. */
  2659. if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 &&
  2660. fp && fp->altsetting == 1 && fp->channels == 1 &&
  2661. fp->format == SNDRV_PCM_FORMAT_S16_LE &&
  2662. protocol == UAC_VERSION_1 &&
  2663. le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
  2664. fp->maxpacksize * 2)
  2665. continue;
  2666. csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
  2667. /* Creamware Noah has this descriptor after the 2nd endpoint */
  2668. if (!csep && altsd->bNumEndpoints >= 2)
  2669. csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
  2670. if (!csep || csep[0] < 7 || csep[2] != UAC_EP_GENERAL) {
  2671. snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
  2672. " class specific endpoint descriptor\n",
  2673. dev->devnum, iface_no, altno);
  2674. csep = NULL;
  2675. }
  2676. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  2677. if (! fp) {
  2678. snd_printk(KERN_ERR "cannot malloc\n");
  2679. return -ENOMEM;
  2680. }
  2681. fp->iface = iface_no;
  2682. fp->altsetting = altno;
  2683. fp->altset_idx = i;
  2684. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  2685. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  2686. fp->datainterval = parse_datainterval(chip, alts);
  2687. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  2688. /* num_channels is only set for v2 interfaces */
  2689. fp->channels = num_channels;
  2690. if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
  2691. fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
  2692. * (fp->maxpacksize & 0x7ff);
  2693. fp->attributes = csep ? csep[3] : 0;
  2694. /* some quirks for attributes here */
  2695. switch (chip->usb_id) {
  2696. case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
  2697. /* Optoplay sets the sample rate attribute although
  2698. * it seems not supporting it in fact.
  2699. */
  2700. fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
  2701. break;
  2702. case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
  2703. case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
  2704. /* doesn't set the sample rate attribute, but supports it */
  2705. fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
  2706. break;
  2707. case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
  2708. case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
  2709. an older model 77d:223) */
  2710. /*
  2711. * plantronics headset and Griffin iMic have set adaptive-in
  2712. * although it's really not...
  2713. */
  2714. fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
  2715. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  2716. fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
  2717. else
  2718. fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
  2719. break;
  2720. }
  2721. /* ok, let's parse further... */
  2722. if (parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
  2723. kfree(fp->rate_table);
  2724. kfree(fp);
  2725. continue;
  2726. }
  2727. snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
  2728. err = add_audio_endpoint(chip, stream, fp);
  2729. if (err < 0) {
  2730. kfree(fp->rate_table);
  2731. kfree(fp);
  2732. return err;
  2733. }
  2734. /* try to set the interface... */
  2735. usb_set_interface(chip->dev, iface_no, altno);
  2736. init_usb_pitch(chip->dev, iface_no, alts, fp);
  2737. init_usb_sample_rate(chip->dev, iface_no, alts, fp, fp->rate_max);
  2738. }
  2739. return 0;
  2740. }
  2741. /*
  2742. * disconnect streams
  2743. * called from snd_usb_audio_disconnect()
  2744. */
  2745. static void snd_usb_stream_disconnect(struct list_head *head)
  2746. {
  2747. int idx;
  2748. struct snd_usb_stream *as;
  2749. struct snd_usb_substream *subs;
  2750. as = list_entry(head, struct snd_usb_stream, list);
  2751. for (idx = 0; idx < 2; idx++) {
  2752. subs = &as->substream[idx];
  2753. if (!subs->num_formats)
  2754. return;
  2755. release_substream_urbs(subs, 1);
  2756. subs->interface = -1;
  2757. }
  2758. }
  2759. static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
  2760. {
  2761. struct usb_device *dev = chip->dev;
  2762. struct usb_host_interface *alts;
  2763. struct usb_interface_descriptor *altsd;
  2764. struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
  2765. if (!iface) {
  2766. snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
  2767. dev->devnum, ctrlif, interface);
  2768. return -EINVAL;
  2769. }
  2770. if (usb_interface_claimed(iface)) {
  2771. snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",
  2772. dev->devnum, ctrlif, interface);
  2773. return -EINVAL;
  2774. }
  2775. alts = &iface->altsetting[0];
  2776. altsd = get_iface_desc(alts);
  2777. if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
  2778. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
  2779. altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
  2780. int err = snd_usbmidi_create(chip->card, iface,
  2781. &chip->midi_list, NULL);
  2782. if (err < 0) {
  2783. snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",
  2784. dev->devnum, ctrlif, interface);
  2785. return -EINVAL;
  2786. }
  2787. usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
  2788. return 0;
  2789. }
  2790. if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  2791. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  2792. altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
  2793. snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",
  2794. dev->devnum, ctrlif, interface, altsd->bInterfaceClass);
  2795. /* skip non-supported classes */
  2796. return -EINVAL;
  2797. }
  2798. if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
  2799. snd_printk(KERN_ERR "low speed audio streaming not supported\n");
  2800. return -EINVAL;
  2801. }
  2802. if (! parse_audio_endpoints(chip, interface)) {
  2803. usb_set_interface(dev, interface, 0); /* reset the current interface */
  2804. usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
  2805. return -EINVAL;
  2806. }
  2807. return 0;
  2808. }
  2809. /*
  2810. * parse audio control descriptor and create pcm/midi streams
  2811. */
  2812. static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
  2813. {
  2814. struct usb_device *dev = chip->dev;
  2815. struct usb_host_interface *host_iface;
  2816. struct usb_interface_descriptor *altsd;
  2817. void *control_header;
  2818. int i, protocol;
  2819. /* find audiocontrol interface */
  2820. host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
  2821. control_header = snd_usb_find_csint_desc(host_iface->extra,
  2822. host_iface->extralen,
  2823. NULL, UAC_HEADER);
  2824. altsd = get_iface_desc(host_iface);
  2825. protocol = altsd->bInterfaceProtocol;
  2826. if (!control_header) {
  2827. snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
  2828. return -EINVAL;
  2829. }
  2830. switch (protocol) {
  2831. case UAC_VERSION_1: {
  2832. struct uac_ac_header_descriptor_v1 *h1 = control_header;
  2833. if (!h1->bInCollection) {
  2834. snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
  2835. return -EINVAL;
  2836. }
  2837. if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
  2838. snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
  2839. return -EINVAL;
  2840. }
  2841. for (i = 0; i < h1->bInCollection; i++)
  2842. snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
  2843. break;
  2844. }
  2845. case UAC_VERSION_2: {
  2846. struct uac_clock_source_descriptor *cs;
  2847. struct usb_interface_assoc_descriptor *assoc =
  2848. usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
  2849. if (!assoc) {
  2850. snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
  2851. return -EINVAL;
  2852. }
  2853. /* FIXME: for now, we expect there is at least one clock source
  2854. * descriptor and we always take the first one.
  2855. * We should properly support devices with multiple clock sources,
  2856. * clock selectors and sample rate conversion units. */
  2857. cs = snd_usb_find_csint_desc(host_iface->extra, host_iface->extralen,
  2858. NULL, UAC_CLOCK_SOURCE);
  2859. if (!cs) {
  2860. snd_printk(KERN_ERR "CLOCK_SOURCE descriptor not found\n");
  2861. return -EINVAL;
  2862. }
  2863. chip->clock_id = cs->bClockID;
  2864. for (i = 0; i < assoc->bInterfaceCount; i++) {
  2865. int intf = assoc->bFirstInterface + i;
  2866. if (intf != ctrlif)
  2867. snd_usb_create_stream(chip, ctrlif, intf);
  2868. }
  2869. break;
  2870. }
  2871. default:
  2872. snd_printk(KERN_ERR "unknown protocol version 0x%02x\n", protocol);
  2873. return -EINVAL;
  2874. }
  2875. return 0;
  2876. }
  2877. /*
  2878. * create a stream for an endpoint/altsetting without proper descriptors
  2879. */
  2880. static int create_fixed_stream_quirk(struct snd_usb_audio *chip,
  2881. struct usb_interface *iface,
  2882. const struct snd_usb_audio_quirk *quirk)
  2883. {
  2884. struct audioformat *fp;
  2885. struct usb_host_interface *alts;
  2886. int stream, err;
  2887. unsigned *rate_table = NULL;
  2888. fp = kmemdup(quirk->data, sizeof(*fp), GFP_KERNEL);
  2889. if (! fp) {
  2890. snd_printk(KERN_ERR "cannot memdup\n");
  2891. return -ENOMEM;
  2892. }
  2893. if (fp->nr_rates > 0) {
  2894. rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
  2895. if (!rate_table) {
  2896. kfree(fp);
  2897. return -ENOMEM;
  2898. }
  2899. memcpy(rate_table, fp->rate_table, sizeof(int) * fp->nr_rates);
  2900. fp->rate_table = rate_table;
  2901. }
  2902. stream = (fp->endpoint & USB_DIR_IN)
  2903. ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  2904. err = add_audio_endpoint(chip, stream, fp);
  2905. if (err < 0) {
  2906. kfree(fp);
  2907. kfree(rate_table);
  2908. return err;
  2909. }
  2910. if (fp->iface != get_iface_desc(&iface->altsetting[0])->bInterfaceNumber ||
  2911. fp->altset_idx >= iface->num_altsetting) {
  2912. kfree(fp);
  2913. kfree(rate_table);
  2914. return -EINVAL;
  2915. }
  2916. alts = &iface->altsetting[fp->altset_idx];
  2917. fp->datainterval = parse_datainterval(chip, alts);
  2918. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  2919. usb_set_interface(chip->dev, fp->iface, 0);
  2920. init_usb_pitch(chip->dev, fp->iface, alts, fp);
  2921. init_usb_sample_rate(chip->dev, fp->iface, alts, fp, fp->rate_max);
  2922. return 0;
  2923. }
  2924. /*
  2925. * create a stream for an interface with proper descriptors
  2926. */
  2927. static int create_standard_audio_quirk(struct snd_usb_audio *chip,
  2928. struct usb_interface *iface,
  2929. const struct snd_usb_audio_quirk *quirk)
  2930. {
  2931. struct usb_host_interface *alts;
  2932. struct usb_interface_descriptor *altsd;
  2933. int err;
  2934. alts = &iface->altsetting[0];
  2935. altsd = get_iface_desc(alts);
  2936. err = parse_audio_endpoints(chip, altsd->bInterfaceNumber);
  2937. if (err < 0) {
  2938. snd_printk(KERN_ERR "cannot setup if %d: error %d\n",
  2939. altsd->bInterfaceNumber, err);
  2940. return err;
  2941. }
  2942. /* reset the current interface */
  2943. usb_set_interface(chip->dev, altsd->bInterfaceNumber, 0);
  2944. return 0;
  2945. }
  2946. /*
  2947. * Create a stream for an Edirol UA-700/UA-25/UA-4FX interface.
  2948. * The only way to detect the sample rate is by looking at wMaxPacketSize.
  2949. */
  2950. static int create_uaxx_quirk(struct snd_usb_audio *chip,
  2951. struct usb_interface *iface,
  2952. const struct snd_usb_audio_quirk *quirk)
  2953. {
  2954. static const struct audioformat ua_format = {
  2955. .format = SNDRV_PCM_FORMAT_S24_3LE,
  2956. .channels = 2,
  2957. .fmt_type = UAC_FORMAT_TYPE_I,
  2958. .altsetting = 1,
  2959. .altset_idx = 1,
  2960. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  2961. };
  2962. struct usb_host_interface *alts;
  2963. struct usb_interface_descriptor *altsd;
  2964. struct audioformat *fp;
  2965. int stream, err;
  2966. /* both PCM and MIDI interfaces have 2 or more altsettings */
  2967. if (iface->num_altsetting < 2)
  2968. return -ENXIO;
  2969. alts = &iface->altsetting[1];
  2970. altsd = get_iface_desc(alts);
  2971. if (altsd->bNumEndpoints == 2) {
  2972. static const struct snd_usb_midi_endpoint_info ua700_ep = {
  2973. .out_cables = 0x0003,
  2974. .in_cables = 0x0003
  2975. };
  2976. static const struct snd_usb_audio_quirk ua700_quirk = {
  2977. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  2978. .data = &ua700_ep
  2979. };
  2980. static const struct snd_usb_midi_endpoint_info uaxx_ep = {
  2981. .out_cables = 0x0001,
  2982. .in_cables = 0x0001
  2983. };
  2984. static const struct snd_usb_audio_quirk uaxx_quirk = {
  2985. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  2986. .data = &uaxx_ep
  2987. };
  2988. const struct snd_usb_audio_quirk *quirk =
  2989. chip->usb_id == USB_ID(0x0582, 0x002b)
  2990. ? &ua700_quirk : &uaxx_quirk;
  2991. return snd_usbmidi_create(chip->card, iface,
  2992. &chip->midi_list, quirk);
  2993. }
  2994. if (altsd->bNumEndpoints != 1)
  2995. return -ENXIO;
  2996. fp = kmalloc(sizeof(*fp), GFP_KERNEL);
  2997. if (!fp)
  2998. return -ENOMEM;
  2999. memcpy(fp, &ua_format, sizeof(*fp));
  3000. fp->iface = altsd->bInterfaceNumber;
  3001. fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
  3002. fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
  3003. fp->datainterval = 0;
  3004. fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
  3005. switch (fp->maxpacksize) {
  3006. case 0x120:
  3007. fp->rate_max = fp->rate_min = 44100;
  3008. break;
  3009. case 0x138:
  3010. case 0x140:
  3011. fp->rate_max = fp->rate_min = 48000;
  3012. break;
  3013. case 0x258:
  3014. case 0x260:
  3015. fp->rate_max = fp->rate_min = 96000;
  3016. break;
  3017. default:
  3018. snd_printk(KERN_ERR "unknown sample rate\n");
  3019. kfree(fp);
  3020. return -ENXIO;
  3021. }
  3022. stream = (fp->endpoint & USB_DIR_IN)
  3023. ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
  3024. err = add_audio_endpoint(chip, stream, fp);
  3025. if (err < 0) {
  3026. kfree(fp);
  3027. return err;
  3028. }
  3029. usb_set_interface(chip->dev, fp->iface, 0);
  3030. return 0;
  3031. }
  3032. static int snd_usb_create_quirk(struct snd_usb_audio *chip,
  3033. struct usb_interface *iface,
  3034. const struct snd_usb_audio_quirk *quirk);
  3035. /*
  3036. * handle the quirks for the contained interfaces
  3037. */
  3038. static int create_composite_quirk(struct snd_usb_audio *chip,
  3039. struct usb_interface *iface,
  3040. const struct snd_usb_audio_quirk *quirk)
  3041. {
  3042. int probed_ifnum = get_iface_desc(iface->altsetting)->bInterfaceNumber;
  3043. int err;
  3044. for (quirk = quirk->data; quirk->ifnum >= 0; ++quirk) {
  3045. iface = usb_ifnum_to_if(chip->dev, quirk->ifnum);
  3046. if (!iface)
  3047. continue;
  3048. if (quirk->ifnum != probed_ifnum &&
  3049. usb_interface_claimed(iface))
  3050. continue;
  3051. err = snd_usb_create_quirk(chip, iface, quirk);
  3052. if (err < 0)
  3053. return err;
  3054. if (quirk->ifnum != probed_ifnum)
  3055. usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
  3056. }
  3057. return 0;
  3058. }
  3059. static int ignore_interface_quirk(struct snd_usb_audio *chip,
  3060. struct usb_interface *iface,
  3061. const struct snd_usb_audio_quirk *quirk)
  3062. {
  3063. return 0;
  3064. }
  3065. /*
  3066. * Allow alignment on audio sub-slot (channel samples) rather than
  3067. * on audio slots (audio frames)
  3068. */
  3069. static int create_align_transfer_quirk(struct snd_usb_audio *chip,
  3070. struct usb_interface *iface,
  3071. const struct snd_usb_audio_quirk *quirk)
  3072. {
  3073. chip->txfr_quirk = 1;
  3074. return 1; /* Continue with creating streams and mixer */
  3075. }
  3076. /*
  3077. * boot quirks
  3078. */
  3079. #define EXTIGY_FIRMWARE_SIZE_OLD 794
  3080. #define EXTIGY_FIRMWARE_SIZE_NEW 483
  3081. static int snd_usb_extigy_boot_quirk(struct usb_device *dev, struct usb_interface *intf)
  3082. {
  3083. struct usb_host_config *config = dev->actconfig;
  3084. int err;
  3085. if (le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_OLD ||
  3086. le16_to_cpu(get_cfg_desc(config)->wTotalLength) == EXTIGY_FIRMWARE_SIZE_NEW) {
  3087. snd_printdd("sending Extigy boot sequence...\n");
  3088. /* Send message to force it to reconnect with full interface. */
  3089. err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev,0),
  3090. 0x10, 0x43, 0x0001, 0x000a, NULL, 0, 1000);
  3091. if (err < 0) snd_printdd("error sending boot message: %d\n", err);
  3092. err = usb_get_descriptor(dev, USB_DT_DEVICE, 0,
  3093. &dev->descriptor, sizeof(dev->descriptor));
  3094. config = dev->actconfig;
  3095. if (err < 0) snd_printdd("error usb_get_descriptor: %d\n", err);
  3096. err = usb_reset_configuration(dev);
  3097. if (err < 0) snd_printdd("error usb_reset_configuration: %d\n", err);
  3098. snd_printdd("extigy_boot: new boot length = %d\n",
  3099. le16_to_cpu(get_cfg_desc(config)->wTotalLength));
  3100. return -ENODEV; /* quit this anyway */
  3101. }
  3102. return 0;
  3103. }
  3104. static int snd_usb_audigy2nx_boot_quirk(struct usb_device *dev)
  3105. {
  3106. u8 buf = 1;
  3107. snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), 0x2a,
  3108. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  3109. 0, 0, &buf, 1, 1000);
  3110. if (buf == 0) {
  3111. snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), 0x29,
  3112. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
  3113. 1, 2000, NULL, 0, 1000);
  3114. return -ENODEV;
  3115. }
  3116. return 0;
  3117. }
  3118. /*
  3119. * C-Media CM106/CM106+ have four 16-bit internal registers that are nicely
  3120. * documented in the device's data sheet.
  3121. */
  3122. static int snd_usb_cm106_write_int_reg(struct usb_device *dev, int reg, u16 value)
  3123. {
  3124. u8 buf[4];
  3125. buf[0] = 0x20;
  3126. buf[1] = value & 0xff;
  3127. buf[2] = (value >> 8) & 0xff;
  3128. buf[3] = reg;
  3129. return snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), USB_REQ_SET_CONFIGURATION,
  3130. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
  3131. 0, 0, &buf, 4, 1000);
  3132. }
  3133. static int snd_usb_cm106_boot_quirk(struct usb_device *dev)
  3134. {
  3135. /*
  3136. * Enable line-out driver mode, set headphone source to front
  3137. * channels, enable stereo mic.
  3138. */
  3139. return snd_usb_cm106_write_int_reg(dev, 2, 0x8004);
  3140. }
  3141. /*
  3142. * C-Media CM6206 is based on CM106 with two additional
  3143. * registers that are not documented in the data sheet.
  3144. * Values here are chosen based on sniffing USB traffic
  3145. * under Windows.
  3146. */
  3147. static int snd_usb_cm6206_boot_quirk(struct usb_device *dev)
  3148. {
  3149. int err, reg;
  3150. int val[] = {0x200c, 0x3000, 0xf800, 0x143f, 0x0000, 0x3000};
  3151. for (reg = 0; reg < ARRAY_SIZE(val); reg++) {
  3152. err = snd_usb_cm106_write_int_reg(dev, reg, val[reg]);
  3153. if (err < 0)
  3154. return err;
  3155. }
  3156. return err;
  3157. }
  3158. /*
  3159. * This call will put the synth in "USB send" mode, i.e it will send MIDI
  3160. * messages through USB (this is disabled at startup). The synth will
  3161. * acknowledge by sending a sysex on endpoint 0x85 and by displaying a USB
  3162. * sign on its LCD. Values here are chosen based on sniffing USB traffic
  3163. * under Windows.
  3164. */
  3165. static int snd_usb_accessmusic_boot_quirk(struct usb_device *dev)
  3166. {
  3167. int err, actual_length;
  3168. /* "midi send" enable */
  3169. static const u8 seq[] = { 0x4e, 0x73, 0x52, 0x01 };
  3170. void *buf = kmemdup(seq, ARRAY_SIZE(seq), GFP_KERNEL);
  3171. if (!buf)
  3172. return -ENOMEM;
  3173. err = usb_interrupt_msg(dev, usb_sndintpipe(dev, 0x05), buf,
  3174. ARRAY_SIZE(seq), &actual_length, 1000);
  3175. kfree(buf);
  3176. if (err < 0)
  3177. return err;
  3178. return 0;
  3179. }
  3180. /*
  3181. * Setup quirks
  3182. */
  3183. #define AUDIOPHILE_SET 0x01 /* if set, parse device_setup */
  3184. #define AUDIOPHILE_SET_DTS 0x02 /* if set, enable DTS Digital Output */
  3185. #define AUDIOPHILE_SET_96K 0x04 /* 48-96KHz rate if set, 8-48KHz otherwise */
  3186. #define AUDIOPHILE_SET_24B 0x08 /* 24bits sample if set, 16bits otherwise */
  3187. #define AUDIOPHILE_SET_DI 0x10 /* if set, enable Digital Input */
  3188. #define AUDIOPHILE_SET_MASK 0x1F /* bit mask for setup value */
  3189. #define AUDIOPHILE_SET_24B_48K_DI 0x19 /* value for 24bits+48KHz+Digital Input */
  3190. #define AUDIOPHILE_SET_24B_48K_NOTDI 0x09 /* value for 24bits+48KHz+No Digital Input */
  3191. #define AUDIOPHILE_SET_16B_48K_DI 0x11 /* value for 16bits+48KHz+Digital Input */
  3192. #define AUDIOPHILE_SET_16B_48K_NOTDI 0x01 /* value for 16bits+48KHz+No Digital Input */
  3193. static int audiophile_skip_setting_quirk(struct snd_usb_audio *chip,
  3194. int iface, int altno)
  3195. {
  3196. /* Reset ALL ifaces to 0 altsetting.
  3197. * Call it for every possible altsetting of every interface.
  3198. */
  3199. usb_set_interface(chip->dev, iface, 0);
  3200. if (device_setup[chip->index] & AUDIOPHILE_SET) {
  3201. if ((device_setup[chip->index] & AUDIOPHILE_SET_DTS)
  3202. && altno != 6)
  3203. return 1; /* skip this altsetting */
  3204. if ((device_setup[chip->index] & AUDIOPHILE_SET_96K)
  3205. && altno != 1)
  3206. return 1; /* skip this altsetting */
  3207. if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
  3208. AUDIOPHILE_SET_24B_48K_DI && altno != 2)
  3209. return 1; /* skip this altsetting */
  3210. if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
  3211. AUDIOPHILE_SET_24B_48K_NOTDI && altno != 3)
  3212. return 1; /* skip this altsetting */
  3213. if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
  3214. AUDIOPHILE_SET_16B_48K_DI && altno != 4)
  3215. return 1; /* skip this altsetting */
  3216. if ((device_setup[chip->index] & AUDIOPHILE_SET_MASK) ==
  3217. AUDIOPHILE_SET_16B_48K_NOTDI && altno != 5)
  3218. return 1; /* skip this altsetting */
  3219. }
  3220. return 0; /* keep this altsetting */
  3221. }
  3222. static int create_any_midi_quirk(struct snd_usb_audio *chip,
  3223. struct usb_interface *intf,
  3224. const struct snd_usb_audio_quirk *quirk)
  3225. {
  3226. return snd_usbmidi_create(chip->card, intf, &chip->midi_list, quirk);
  3227. }
  3228. /*
  3229. * audio-interface quirks
  3230. *
  3231. * returns zero if no standard audio/MIDI parsing is needed.
  3232. * returns a postive value if standard audio/midi interfaces are parsed
  3233. * after this.
  3234. * returns a negative value at error.
  3235. */
  3236. static int snd_usb_create_quirk(struct snd_usb_audio *chip,
  3237. struct usb_interface *iface,
  3238. const struct snd_usb_audio_quirk *quirk)
  3239. {
  3240. typedef int (*quirk_func_t)(struct snd_usb_audio *, struct usb_interface *,
  3241. const struct snd_usb_audio_quirk *);
  3242. static const quirk_func_t quirk_funcs[] = {
  3243. [QUIRK_IGNORE_INTERFACE] = ignore_interface_quirk,
  3244. [QUIRK_COMPOSITE] = create_composite_quirk,
  3245. [QUIRK_MIDI_STANDARD_INTERFACE] = create_any_midi_quirk,
  3246. [QUIRK_MIDI_FIXED_ENDPOINT] = create_any_midi_quirk,
  3247. [QUIRK_MIDI_YAMAHA] = create_any_midi_quirk,
  3248. [QUIRK_MIDI_MIDIMAN] = create_any_midi_quirk,
  3249. [QUIRK_MIDI_NOVATION] = create_any_midi_quirk,
  3250. [QUIRK_MIDI_FASTLANE] = create_any_midi_quirk,
  3251. [QUIRK_MIDI_EMAGIC] = create_any_midi_quirk,
  3252. [QUIRK_MIDI_CME] = create_any_midi_quirk,
  3253. [QUIRK_AUDIO_STANDARD_INTERFACE] = create_standard_audio_quirk,
  3254. [QUIRK_AUDIO_FIXED_ENDPOINT] = create_fixed_stream_quirk,
  3255. [QUIRK_AUDIO_EDIROL_UAXX] = create_uaxx_quirk,
  3256. [QUIRK_AUDIO_ALIGN_TRANSFER] = create_align_transfer_quirk
  3257. };
  3258. if (quirk->type < QUIRK_TYPE_COUNT) {
  3259. return quirk_funcs[quirk->type](chip, iface, quirk);
  3260. } else {
  3261. snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
  3262. return -ENXIO;
  3263. }
  3264. }
  3265. /*
  3266. * common proc files to show the usb device info
  3267. */
  3268. static void proc_audio_usbbus_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  3269. {
  3270. struct snd_usb_audio *chip = entry->private_data;
  3271. if (!chip->shutdown)
  3272. snd_iprintf(buffer, "%03d/%03d\n", chip->dev->bus->busnum, chip->dev->devnum);
  3273. }
  3274. static void proc_audio_usbid_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  3275. {
  3276. struct snd_usb_audio *chip = entry->private_data;
  3277. if (!chip->shutdown)
  3278. snd_iprintf(buffer, "%04x:%04x\n",
  3279. USB_ID_VENDOR(chip->usb_id),
  3280. USB_ID_PRODUCT(chip->usb_id));
  3281. }
  3282. static void snd_usb_audio_create_proc(struct snd_usb_audio *chip)
  3283. {
  3284. struct snd_info_entry *entry;
  3285. if (!snd_card_proc_new(chip->card, "usbbus", &entry))
  3286. snd_info_set_text_ops(entry, chip, proc_audio_usbbus_read);
  3287. if (!snd_card_proc_new(chip->card, "usbid", &entry))
  3288. snd_info_set_text_ops(entry, chip, proc_audio_usbid_read);
  3289. }
  3290. /*
  3291. * free the chip instance
  3292. *
  3293. * here we have to do not much, since pcm and controls are already freed
  3294. *
  3295. */
  3296. static int snd_usb_audio_free(struct snd_usb_audio *chip)
  3297. {
  3298. kfree(chip);
  3299. return 0;
  3300. }
  3301. static int snd_usb_audio_dev_free(struct snd_device *device)
  3302. {
  3303. struct snd_usb_audio *chip = device->device_data;
  3304. return snd_usb_audio_free(chip);
  3305. }
  3306. /*
  3307. * create a chip instance and set its names.
  3308. */
  3309. static int snd_usb_audio_create(struct usb_device *dev, int idx,
  3310. const struct snd_usb_audio_quirk *quirk,
  3311. struct snd_usb_audio **rchip)
  3312. {
  3313. struct snd_card *card;
  3314. struct snd_usb_audio *chip;
  3315. int err, len;
  3316. char component[14];
  3317. static struct snd_device_ops ops = {
  3318. .dev_free = snd_usb_audio_dev_free,
  3319. };
  3320. *rchip = NULL;
  3321. if (snd_usb_get_speed(dev) != USB_SPEED_LOW &&
  3322. snd_usb_get_speed(dev) != USB_SPEED_FULL &&
  3323. snd_usb_get_speed(dev) != USB_SPEED_HIGH) {
  3324. snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
  3325. return -ENXIO;
  3326. }
  3327. err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
  3328. if (err < 0) {
  3329. snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
  3330. return err;
  3331. }
  3332. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  3333. if (! chip) {
  3334. snd_card_free(card);
  3335. return -ENOMEM;
  3336. }
  3337. chip->index = idx;
  3338. chip->dev = dev;
  3339. chip->card = card;
  3340. chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  3341. le16_to_cpu(dev->descriptor.idProduct));
  3342. INIT_LIST_HEAD(&chip->pcm_list);
  3343. INIT_LIST_HEAD(&chip->midi_list);
  3344. INIT_LIST_HEAD(&chip->mixer_list);
  3345. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  3346. snd_usb_audio_free(chip);
  3347. snd_card_free(card);
  3348. return err;
  3349. }
  3350. strcpy(card->driver, "USB-Audio");
  3351. sprintf(component, "USB%04x:%04x",
  3352. USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
  3353. snd_component_add(card, component);
  3354. /* retrieve the device string as shortname */
  3355. if (quirk && quirk->product_name) {
  3356. strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
  3357. } else {
  3358. if (!dev->descriptor.iProduct ||
  3359. usb_string(dev, dev->descriptor.iProduct,
  3360. card->shortname, sizeof(card->shortname)) <= 0) {
  3361. /* no name available from anywhere, so use ID */
  3362. sprintf(card->shortname, "USB Device %#04x:%#04x",
  3363. USB_ID_VENDOR(chip->usb_id),
  3364. USB_ID_PRODUCT(chip->usb_id));
  3365. }
  3366. }
  3367. /* retrieve the vendor and device strings as longname */
  3368. if (quirk && quirk->vendor_name) {
  3369. len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
  3370. } else {
  3371. if (dev->descriptor.iManufacturer)
  3372. len = usb_string(dev, dev->descriptor.iManufacturer,
  3373. card->longname, sizeof(card->longname));
  3374. else
  3375. len = 0;
  3376. /* we don't really care if there isn't any vendor string */
  3377. }
  3378. if (len > 0)
  3379. strlcat(card->longname, " ", sizeof(card->longname));
  3380. strlcat(card->longname, card->shortname, sizeof(card->longname));
  3381. len = strlcat(card->longname, " at ", sizeof(card->longname));
  3382. if (len < sizeof(card->longname))
  3383. usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
  3384. strlcat(card->longname,
  3385. snd_usb_get_speed(dev) == USB_SPEED_LOW ? ", low speed" :
  3386. snd_usb_get_speed(dev) == USB_SPEED_FULL ? ", full speed" :
  3387. ", high speed",
  3388. sizeof(card->longname));
  3389. snd_usb_audio_create_proc(chip);
  3390. *rchip = chip;
  3391. return 0;
  3392. }
  3393. /*
  3394. * probe the active usb device
  3395. *
  3396. * note that this can be called multiple times per a device, when it
  3397. * includes multiple audio control interfaces.
  3398. *
  3399. * thus we check the usb device pointer and creates the card instance
  3400. * only at the first time. the successive calls of this function will
  3401. * append the pcm interface to the corresponding card.
  3402. */
  3403. static void *snd_usb_audio_probe(struct usb_device *dev,
  3404. struct usb_interface *intf,
  3405. const struct usb_device_id *usb_id)
  3406. {
  3407. const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
  3408. int i, err;
  3409. struct snd_usb_audio *chip;
  3410. struct usb_host_interface *alts;
  3411. int ifnum;
  3412. u32 id;
  3413. alts = &intf->altsetting[0];
  3414. ifnum = get_iface_desc(alts)->bInterfaceNumber;
  3415. id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  3416. le16_to_cpu(dev->descriptor.idProduct));
  3417. if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
  3418. goto __err_val;
  3419. /* SB Extigy needs special boot-up sequence */
  3420. /* if more models come, this will go to the quirk list. */
  3421. if (id == USB_ID(0x041e, 0x3000)) {
  3422. if (snd_usb_extigy_boot_quirk(dev, intf) < 0)
  3423. goto __err_val;
  3424. }
  3425. /* SB Audigy 2 NX needs its own boot-up magic, too */
  3426. if (id == USB_ID(0x041e, 0x3020)) {
  3427. if (snd_usb_audigy2nx_boot_quirk(dev) < 0)
  3428. goto __err_val;
  3429. }
  3430. /* C-Media CM106 / Turtle Beach Audio Advantage Roadie */
  3431. if (id == USB_ID(0x10f5, 0x0200)) {
  3432. if (snd_usb_cm106_boot_quirk(dev) < 0)
  3433. goto __err_val;
  3434. }
  3435. /* C-Media CM6206 / CM106-Like Sound Device */
  3436. if (id == USB_ID(0x0d8c, 0x0102)) {
  3437. if (snd_usb_cm6206_boot_quirk(dev) < 0)
  3438. goto __err_val;
  3439. }
  3440. /* Access Music VirusTI Desktop */
  3441. if (id == USB_ID(0x133e, 0x0815)) {
  3442. if (snd_usb_accessmusic_boot_quirk(dev) < 0)
  3443. goto __err_val;
  3444. }
  3445. /*
  3446. * found a config. now register to ALSA
  3447. */
  3448. /* check whether it's already registered */
  3449. chip = NULL;
  3450. mutex_lock(&register_mutex);
  3451. for (i = 0; i < SNDRV_CARDS; i++) {
  3452. if (usb_chip[i] && usb_chip[i]->dev == dev) {
  3453. if (usb_chip[i]->shutdown) {
  3454. snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
  3455. goto __error;
  3456. }
  3457. chip = usb_chip[i];
  3458. break;
  3459. }
  3460. }
  3461. if (! chip) {
  3462. /* it's a fresh one.
  3463. * now look for an empty slot and create a new card instance
  3464. */
  3465. for (i = 0; i < SNDRV_CARDS; i++)
  3466. if (enable[i] && ! usb_chip[i] &&
  3467. (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
  3468. (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
  3469. if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
  3470. goto __error;
  3471. }
  3472. snd_card_set_dev(chip->card, &intf->dev);
  3473. break;
  3474. }
  3475. if (!chip) {
  3476. printk(KERN_ERR "no available usb audio device\n");
  3477. goto __error;
  3478. }
  3479. }
  3480. chip->txfr_quirk = 0;
  3481. err = 1; /* continue */
  3482. if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
  3483. /* need some special handlings */
  3484. if ((err = snd_usb_create_quirk(chip, intf, quirk)) < 0)
  3485. goto __error;
  3486. }
  3487. if (err > 0) {
  3488. /* create normal USB audio interfaces */
  3489. if (snd_usb_create_streams(chip, ifnum) < 0 ||
  3490. snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
  3491. goto __error;
  3492. }
  3493. }
  3494. /* we are allowed to call snd_card_register() many times */
  3495. if (snd_card_register(chip->card) < 0) {
  3496. goto __error;
  3497. }
  3498. usb_chip[chip->index] = chip;
  3499. chip->num_interfaces++;
  3500. mutex_unlock(&register_mutex);
  3501. return chip;
  3502. __error:
  3503. if (chip && !chip->num_interfaces)
  3504. snd_card_free(chip->card);
  3505. mutex_unlock(&register_mutex);
  3506. __err_val:
  3507. return NULL;
  3508. }
  3509. /*
  3510. * we need to take care of counter, since disconnection can be called also
  3511. * many times as well as usb_audio_probe().
  3512. */
  3513. static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
  3514. {
  3515. struct snd_usb_audio *chip;
  3516. struct snd_card *card;
  3517. struct list_head *p;
  3518. if (ptr == (void *)-1L)
  3519. return;
  3520. chip = ptr;
  3521. card = chip->card;
  3522. mutex_lock(&register_mutex);
  3523. chip->shutdown = 1;
  3524. chip->num_interfaces--;
  3525. if (chip->num_interfaces <= 0) {
  3526. snd_card_disconnect(card);
  3527. /* release the pcm resources */
  3528. list_for_each(p, &chip->pcm_list) {
  3529. snd_usb_stream_disconnect(p);
  3530. }
  3531. /* release the midi resources */
  3532. list_for_each(p, &chip->midi_list) {
  3533. snd_usbmidi_disconnect(p);
  3534. }
  3535. /* release mixer resources */
  3536. list_for_each(p, &chip->mixer_list) {
  3537. snd_usb_mixer_disconnect(p);
  3538. }
  3539. usb_chip[chip->index] = NULL;
  3540. mutex_unlock(&register_mutex);
  3541. snd_card_free_when_closed(card);
  3542. } else {
  3543. mutex_unlock(&register_mutex);
  3544. }
  3545. }
  3546. /*
  3547. * new 2.5 USB kernel API
  3548. */
  3549. static int usb_audio_probe(struct usb_interface *intf,
  3550. const struct usb_device_id *id)
  3551. {
  3552. void *chip;
  3553. chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
  3554. if (chip) {
  3555. usb_set_intfdata(intf, chip);
  3556. return 0;
  3557. } else
  3558. return -EIO;
  3559. }
  3560. static void usb_audio_disconnect(struct usb_interface *intf)
  3561. {
  3562. snd_usb_audio_disconnect(interface_to_usbdev(intf),
  3563. usb_get_intfdata(intf));
  3564. }
  3565. #ifdef CONFIG_PM
  3566. static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
  3567. {
  3568. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  3569. struct list_head *p;
  3570. struct snd_usb_stream *as;
  3571. if (chip == (void *)-1L)
  3572. return 0;
  3573. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
  3574. if (!chip->num_suspended_intf++) {
  3575. list_for_each(p, &chip->pcm_list) {
  3576. as = list_entry(p, struct snd_usb_stream, list);
  3577. snd_pcm_suspend_all(as->pcm);
  3578. }
  3579. }
  3580. return 0;
  3581. }
  3582. static int usb_audio_resume(struct usb_interface *intf)
  3583. {
  3584. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  3585. if (chip == (void *)-1L)
  3586. return 0;
  3587. if (--chip->num_suspended_intf)
  3588. return 0;
  3589. /*
  3590. * ALSA leaves material resumption to user space
  3591. * we just notify
  3592. */
  3593. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
  3594. return 0;
  3595. }
  3596. #endif /* CONFIG_PM */
  3597. static int __init snd_usb_audio_init(void)
  3598. {
  3599. if (nrpacks < 1 || nrpacks > MAX_PACKS) {
  3600. printk(KERN_WARNING "invalid nrpacks value.\n");
  3601. return -EINVAL;
  3602. }
  3603. return usb_register(&usb_audio_driver);
  3604. }
  3605. static void __exit snd_usb_audio_cleanup(void)
  3606. {
  3607. usb_deregister(&usb_audio_driver);
  3608. }
  3609. module_init(snd_usb_audio_init);
  3610. module_exit(snd_usb_audio_cleanup);