usbaudio.c 97 KB

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