usbaudio.c 106 KB

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