usbaudio.c 95 KB

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