usbaudio.c 114 KB

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