usbaudio.c 106 KB

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