usbaudio.c 94 KB

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