usbaudio.c 103 KB

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