usbaudio.c 99 KB

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