usbaudio.c 113 KB

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