usbaudio.c 105 KB

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