usbaudio.c 106 KB

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