usbaudio.c 95 KB

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