usbaudio.c 95 KB

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