usbaudio.c 105 KB

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