usbaudio.c 95 KB

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