usbaudio.c 106 KB

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