usbaudio.c 96 KB

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