usbaudio.c 107 KB

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