usbaudio.c 101 KB

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