usbaudio.c 106 KB

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