usbaudio.c 107 KB

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