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