usbaudio.c 94 KB

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