usbaudio.c 106 KB

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