usbaudio.c 104 KB

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