usbaudio.c 93 KB

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