usbaudio.c 96 KB

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