usbaudio.c 106 KB

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