usbaudio.c 93 KB

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