usbaudio.c 106 KB

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