pcm_oss.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255
  1. /*
  2. * Digital Audio (PCM) abstract layer / OSS compatible
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #if 0
  22. #define PLUGIN_DEBUG
  23. #endif
  24. #if 0
  25. #define OSS_DEBUG
  26. #endif
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/time.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/module.h>
  32. #include <linux/math64.h>
  33. #include <linux/string.h>
  34. #include <linux/compat.h>
  35. #include <sound/core.h>
  36. #include <sound/minors.h>
  37. #include <sound/pcm.h>
  38. #include <sound/pcm_params.h>
  39. #include "pcm_plugin.h"
  40. #include <sound/info.h>
  41. #include <linux/soundcard.h>
  42. #include <sound/initval.h>
  43. #include <sound/mixer_oss.h>
  44. #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
  45. static int dsp_map[SNDRV_CARDS];
  46. static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
  47. static bool nonblock_open = 1;
  48. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
  49. MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
  50. MODULE_LICENSE("GPL");
  51. module_param_array(dsp_map, int, NULL, 0444);
  52. MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
  53. module_param_array(adsp_map, int, NULL, 0444);
  54. MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
  55. module_param(nonblock_open, bool, 0644);
  56. MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
  57. MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
  58. MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
  59. static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
  60. static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
  61. static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
  62. static inline mm_segment_t snd_enter_user(void)
  63. {
  64. mm_segment_t fs = get_fs();
  65. set_fs(get_ds());
  66. return fs;
  67. }
  68. static inline void snd_leave_user(mm_segment_t fs)
  69. {
  70. set_fs(fs);
  71. }
  72. /*
  73. * helper functions to process hw_params
  74. */
  75. static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
  76. {
  77. int changed = 0;
  78. if (i->min < min) {
  79. i->min = min;
  80. i->openmin = openmin;
  81. changed = 1;
  82. } else if (i->min == min && !i->openmin && openmin) {
  83. i->openmin = 1;
  84. changed = 1;
  85. }
  86. if (i->integer) {
  87. if (i->openmin) {
  88. i->min++;
  89. i->openmin = 0;
  90. }
  91. }
  92. if (snd_interval_checkempty(i)) {
  93. snd_interval_none(i);
  94. return -EINVAL;
  95. }
  96. return changed;
  97. }
  98. static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
  99. {
  100. int changed = 0;
  101. if (i->max > max) {
  102. i->max = max;
  103. i->openmax = openmax;
  104. changed = 1;
  105. } else if (i->max == max && !i->openmax && openmax) {
  106. i->openmax = 1;
  107. changed = 1;
  108. }
  109. if (i->integer) {
  110. if (i->openmax) {
  111. i->max--;
  112. i->openmax = 0;
  113. }
  114. }
  115. if (snd_interval_checkempty(i)) {
  116. snd_interval_none(i);
  117. return -EINVAL;
  118. }
  119. return changed;
  120. }
  121. static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
  122. {
  123. struct snd_interval t;
  124. t.empty = 0;
  125. t.min = t.max = val;
  126. t.openmin = t.openmax = 0;
  127. t.integer = 1;
  128. return snd_interval_refine(i, &t);
  129. }
  130. /**
  131. * snd_pcm_hw_param_value_min
  132. * @params: the hw_params instance
  133. * @var: parameter to retrieve
  134. * @dir: pointer to the direction (-1,0,1) or NULL
  135. *
  136. * Return the minimum value for field PAR.
  137. */
  138. static unsigned int
  139. snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
  140. snd_pcm_hw_param_t var, int *dir)
  141. {
  142. if (hw_is_mask(var)) {
  143. if (dir)
  144. *dir = 0;
  145. return snd_mask_min(hw_param_mask_c(params, var));
  146. }
  147. if (hw_is_interval(var)) {
  148. const struct snd_interval *i = hw_param_interval_c(params, var);
  149. if (dir)
  150. *dir = i->openmin;
  151. return snd_interval_min(i);
  152. }
  153. return -EINVAL;
  154. }
  155. /**
  156. * snd_pcm_hw_param_value_max
  157. * @params: the hw_params instance
  158. * @var: parameter to retrieve
  159. * @dir: pointer to the direction (-1,0,1) or NULL
  160. *
  161. * Return the maximum value for field PAR.
  162. */
  163. static unsigned int
  164. snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
  165. snd_pcm_hw_param_t var, int *dir)
  166. {
  167. if (hw_is_mask(var)) {
  168. if (dir)
  169. *dir = 0;
  170. return snd_mask_max(hw_param_mask_c(params, var));
  171. }
  172. if (hw_is_interval(var)) {
  173. const struct snd_interval *i = hw_param_interval_c(params, var);
  174. if (dir)
  175. *dir = - (int) i->openmax;
  176. return snd_interval_max(i);
  177. }
  178. return -EINVAL;
  179. }
  180. static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
  181. snd_pcm_hw_param_t var,
  182. const struct snd_mask *val)
  183. {
  184. int changed;
  185. changed = snd_mask_refine(hw_param_mask(params, var), val);
  186. if (changed) {
  187. params->cmask |= 1 << var;
  188. params->rmask |= 1 << var;
  189. }
  190. return changed;
  191. }
  192. static int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm,
  193. struct snd_pcm_hw_params *params,
  194. snd_pcm_hw_param_t var,
  195. const struct snd_mask *val)
  196. {
  197. int changed = _snd_pcm_hw_param_mask(params, var, val);
  198. if (changed < 0)
  199. return changed;
  200. if (params->rmask) {
  201. int err = snd_pcm_hw_refine(pcm, params);
  202. if (err < 0)
  203. return err;
  204. }
  205. return 0;
  206. }
  207. static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
  208. snd_pcm_hw_param_t var, unsigned int val,
  209. int dir)
  210. {
  211. int changed;
  212. int open = 0;
  213. if (dir) {
  214. if (dir > 0) {
  215. open = 1;
  216. } else if (dir < 0) {
  217. if (val > 0) {
  218. open = 1;
  219. val--;
  220. }
  221. }
  222. }
  223. if (hw_is_mask(var))
  224. changed = snd_mask_refine_min(hw_param_mask(params, var),
  225. val + !!open);
  226. else if (hw_is_interval(var))
  227. changed = snd_interval_refine_min(hw_param_interval(params, var),
  228. val, open);
  229. else
  230. return -EINVAL;
  231. if (changed) {
  232. params->cmask |= 1 << var;
  233. params->rmask |= 1 << var;
  234. }
  235. return changed;
  236. }
  237. /**
  238. * snd_pcm_hw_param_min
  239. * @pcm: PCM instance
  240. * @params: the hw_params instance
  241. * @var: parameter to retrieve
  242. * @val: minimal value
  243. * @dir: pointer to the direction (-1,0,1) or NULL
  244. *
  245. * Inside configuration space defined by PARAMS remove from PAR all
  246. * values < VAL. Reduce configuration space accordingly.
  247. * Return new minimum or -EINVAL if the configuration space is empty
  248. */
  249. static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm,
  250. struct snd_pcm_hw_params *params,
  251. snd_pcm_hw_param_t var, unsigned int val,
  252. int *dir)
  253. {
  254. int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
  255. if (changed < 0)
  256. return changed;
  257. if (params->rmask) {
  258. int err = snd_pcm_hw_refine(pcm, params);
  259. if (err < 0)
  260. return err;
  261. }
  262. return snd_pcm_hw_param_value_min(params, var, dir);
  263. }
  264. static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
  265. snd_pcm_hw_param_t var, unsigned int val,
  266. int dir)
  267. {
  268. int changed;
  269. int open = 0;
  270. if (dir) {
  271. if (dir < 0) {
  272. open = 1;
  273. } else if (dir > 0) {
  274. open = 1;
  275. val++;
  276. }
  277. }
  278. if (hw_is_mask(var)) {
  279. if (val == 0 && open) {
  280. snd_mask_none(hw_param_mask(params, var));
  281. changed = -EINVAL;
  282. } else
  283. changed = snd_mask_refine_max(hw_param_mask(params, var),
  284. val - !!open);
  285. } else if (hw_is_interval(var))
  286. changed = snd_interval_refine_max(hw_param_interval(params, var),
  287. val, open);
  288. else
  289. return -EINVAL;
  290. if (changed) {
  291. params->cmask |= 1 << var;
  292. params->rmask |= 1 << var;
  293. }
  294. return changed;
  295. }
  296. /**
  297. * snd_pcm_hw_param_max
  298. * @pcm: PCM instance
  299. * @params: the hw_params instance
  300. * @var: parameter to retrieve
  301. * @val: maximal value
  302. * @dir: pointer to the direction (-1,0,1) or NULL
  303. *
  304. * Inside configuration space defined by PARAMS remove from PAR all
  305. * values >= VAL + 1. Reduce configuration space accordingly.
  306. * Return new maximum or -EINVAL if the configuration space is empty
  307. */
  308. static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm,
  309. struct snd_pcm_hw_params *params,
  310. snd_pcm_hw_param_t var, unsigned int val,
  311. int *dir)
  312. {
  313. int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
  314. if (changed < 0)
  315. return changed;
  316. if (params->rmask) {
  317. int err = snd_pcm_hw_refine(pcm, params);
  318. if (err < 0)
  319. return err;
  320. }
  321. return snd_pcm_hw_param_value_max(params, var, dir);
  322. }
  323. static int boundary_sub(int a, int adir,
  324. int b, int bdir,
  325. int *c, int *cdir)
  326. {
  327. adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
  328. bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
  329. *c = a - b;
  330. *cdir = adir - bdir;
  331. if (*cdir == -2) {
  332. (*c)--;
  333. } else if (*cdir == 2) {
  334. (*c)++;
  335. }
  336. return 0;
  337. }
  338. static int boundary_lt(unsigned int a, int adir,
  339. unsigned int b, int bdir)
  340. {
  341. if (adir < 0) {
  342. a--;
  343. adir = 1;
  344. } else if (adir > 0)
  345. adir = 1;
  346. if (bdir < 0) {
  347. b--;
  348. bdir = 1;
  349. } else if (bdir > 0)
  350. bdir = 1;
  351. return a < b || (a == b && adir < bdir);
  352. }
  353. /* Return 1 if min is nearer to best than max */
  354. static int boundary_nearer(int min, int mindir,
  355. int best, int bestdir,
  356. int max, int maxdir)
  357. {
  358. int dmin, dmindir;
  359. int dmax, dmaxdir;
  360. boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
  361. boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
  362. return boundary_lt(dmin, dmindir, dmax, dmaxdir);
  363. }
  364. /**
  365. * snd_pcm_hw_param_near
  366. * @pcm: PCM instance
  367. * @params: the hw_params instance
  368. * @var: parameter to retrieve
  369. * @best: value to set
  370. * @dir: pointer to the direction (-1,0,1) or NULL
  371. *
  372. * Inside configuration space defined by PARAMS set PAR to the available value
  373. * nearest to VAL. Reduce configuration space accordingly.
  374. * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
  375. * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
  376. * Return the value found.
  377. */
  378. static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
  379. struct snd_pcm_hw_params *params,
  380. snd_pcm_hw_param_t var, unsigned int best,
  381. int *dir)
  382. {
  383. struct snd_pcm_hw_params *save = NULL;
  384. int v;
  385. unsigned int saved_min;
  386. int last = 0;
  387. int min, max;
  388. int mindir, maxdir;
  389. int valdir = dir ? *dir : 0;
  390. /* FIXME */
  391. if (best > INT_MAX)
  392. best = INT_MAX;
  393. min = max = best;
  394. mindir = maxdir = valdir;
  395. if (maxdir > 0)
  396. maxdir = 0;
  397. else if (maxdir == 0)
  398. maxdir = -1;
  399. else {
  400. maxdir = 1;
  401. max--;
  402. }
  403. save = kmalloc(sizeof(*save), GFP_KERNEL);
  404. if (save == NULL)
  405. return -ENOMEM;
  406. *save = *params;
  407. saved_min = min;
  408. min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
  409. if (min >= 0) {
  410. struct snd_pcm_hw_params *params1;
  411. if (max < 0)
  412. goto _end;
  413. if ((unsigned int)min == saved_min && mindir == valdir)
  414. goto _end;
  415. params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
  416. if (params1 == NULL) {
  417. kfree(save);
  418. return -ENOMEM;
  419. }
  420. *params1 = *save;
  421. max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
  422. if (max < 0) {
  423. kfree(params1);
  424. goto _end;
  425. }
  426. if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
  427. *params = *params1;
  428. last = 1;
  429. }
  430. kfree(params1);
  431. } else {
  432. *params = *save;
  433. max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
  434. if (max < 0) {
  435. kfree(save);
  436. return max;
  437. }
  438. last = 1;
  439. }
  440. _end:
  441. kfree(save);
  442. if (last)
  443. v = snd_pcm_hw_param_last(pcm, params, var, dir);
  444. else
  445. v = snd_pcm_hw_param_first(pcm, params, var, dir);
  446. return v;
  447. }
  448. static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
  449. snd_pcm_hw_param_t var, unsigned int val,
  450. int dir)
  451. {
  452. int changed;
  453. if (hw_is_mask(var)) {
  454. struct snd_mask *m = hw_param_mask(params, var);
  455. if (val == 0 && dir < 0) {
  456. changed = -EINVAL;
  457. snd_mask_none(m);
  458. } else {
  459. if (dir > 0)
  460. val++;
  461. else if (dir < 0)
  462. val--;
  463. changed = snd_mask_refine_set(hw_param_mask(params, var), val);
  464. }
  465. } else if (hw_is_interval(var)) {
  466. struct snd_interval *i = hw_param_interval(params, var);
  467. if (val == 0 && dir < 0) {
  468. changed = -EINVAL;
  469. snd_interval_none(i);
  470. } else if (dir == 0)
  471. changed = snd_interval_refine_set(i, val);
  472. else {
  473. struct snd_interval t;
  474. t.openmin = 1;
  475. t.openmax = 1;
  476. t.empty = 0;
  477. t.integer = 0;
  478. if (dir < 0) {
  479. t.min = val - 1;
  480. t.max = val;
  481. } else {
  482. t.min = val;
  483. t.max = val+1;
  484. }
  485. changed = snd_interval_refine(i, &t);
  486. }
  487. } else
  488. return -EINVAL;
  489. if (changed) {
  490. params->cmask |= 1 << var;
  491. params->rmask |= 1 << var;
  492. }
  493. return changed;
  494. }
  495. /**
  496. * snd_pcm_hw_param_set
  497. * @pcm: PCM instance
  498. * @params: the hw_params instance
  499. * @var: parameter to retrieve
  500. * @val: value to set
  501. * @dir: pointer to the direction (-1,0,1) or NULL
  502. *
  503. * Inside configuration space defined by PARAMS remove from PAR all
  504. * values != VAL. Reduce configuration space accordingly.
  505. * Return VAL or -EINVAL if the configuration space is empty
  506. */
  507. static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm,
  508. struct snd_pcm_hw_params *params,
  509. snd_pcm_hw_param_t var, unsigned int val,
  510. int dir)
  511. {
  512. int changed = _snd_pcm_hw_param_set(params, var, val, dir);
  513. if (changed < 0)
  514. return changed;
  515. if (params->rmask) {
  516. int err = snd_pcm_hw_refine(pcm, params);
  517. if (err < 0)
  518. return err;
  519. }
  520. return snd_pcm_hw_param_value(params, var, NULL);
  521. }
  522. static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
  523. snd_pcm_hw_param_t var)
  524. {
  525. int changed;
  526. changed = snd_interval_setinteger(hw_param_interval(params, var));
  527. if (changed) {
  528. params->cmask |= 1 << var;
  529. params->rmask |= 1 << var;
  530. }
  531. return changed;
  532. }
  533. /*
  534. * plugin
  535. */
  536. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  537. static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
  538. {
  539. struct snd_pcm_runtime *runtime = substream->runtime;
  540. struct snd_pcm_plugin *plugin, *next;
  541. plugin = runtime->oss.plugin_first;
  542. while (plugin) {
  543. next = plugin->next;
  544. snd_pcm_plugin_free(plugin);
  545. plugin = next;
  546. }
  547. runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
  548. return 0;
  549. }
  550. static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
  551. {
  552. struct snd_pcm_runtime *runtime = plugin->plug->runtime;
  553. plugin->next = runtime->oss.plugin_first;
  554. plugin->prev = NULL;
  555. if (runtime->oss.plugin_first) {
  556. runtime->oss.plugin_first->prev = plugin;
  557. runtime->oss.plugin_first = plugin;
  558. } else {
  559. runtime->oss.plugin_last =
  560. runtime->oss.plugin_first = plugin;
  561. }
  562. return 0;
  563. }
  564. int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
  565. {
  566. struct snd_pcm_runtime *runtime = plugin->plug->runtime;
  567. plugin->next = NULL;
  568. plugin->prev = runtime->oss.plugin_last;
  569. if (runtime->oss.plugin_last) {
  570. runtime->oss.plugin_last->next = plugin;
  571. runtime->oss.plugin_last = plugin;
  572. } else {
  573. runtime->oss.plugin_last =
  574. runtime->oss.plugin_first = plugin;
  575. }
  576. return 0;
  577. }
  578. #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
  579. static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
  580. {
  581. struct snd_pcm_runtime *runtime = substream->runtime;
  582. long buffer_size = snd_pcm_lib_buffer_bytes(substream);
  583. long bytes = frames_to_bytes(runtime, frames);
  584. if (buffer_size == runtime->oss.buffer_bytes)
  585. return bytes;
  586. #if BITS_PER_LONG >= 64
  587. return runtime->oss.buffer_bytes * bytes / buffer_size;
  588. #else
  589. {
  590. u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
  591. return div_u64(bsize, buffer_size);
  592. }
  593. #endif
  594. }
  595. static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
  596. {
  597. struct snd_pcm_runtime *runtime = substream->runtime;
  598. long buffer_size = snd_pcm_lib_buffer_bytes(substream);
  599. if (buffer_size == runtime->oss.buffer_bytes)
  600. return bytes_to_frames(runtime, bytes);
  601. return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
  602. }
  603. static inline
  604. snd_pcm_uframes_t get_hw_ptr_period(struct snd_pcm_runtime *runtime)
  605. {
  606. return runtime->hw_ptr_interrupt;
  607. }
  608. /* define extended formats in the recent OSS versions (if any) */
  609. /* linear formats */
  610. #define AFMT_S32_LE 0x00001000
  611. #define AFMT_S32_BE 0x00002000
  612. #define AFMT_S24_LE 0x00008000
  613. #define AFMT_S24_BE 0x00010000
  614. #define AFMT_S24_PACKED 0x00040000
  615. /* other supported formats */
  616. #define AFMT_FLOAT 0x00004000
  617. #define AFMT_SPDIF_RAW 0x00020000
  618. /* unsupported formats */
  619. #define AFMT_AC3 0x00000400
  620. #define AFMT_VORBIS 0x00000800
  621. static snd_pcm_format_t snd_pcm_oss_format_from(int format)
  622. {
  623. switch (format) {
  624. case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
  625. case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
  626. case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
  627. case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
  628. case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
  629. case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
  630. case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
  631. case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
  632. case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
  633. case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
  634. case AFMT_S32_LE: return SNDRV_PCM_FORMAT_S32_LE;
  635. case AFMT_S32_BE: return SNDRV_PCM_FORMAT_S32_BE;
  636. case AFMT_S24_LE: return SNDRV_PCM_FORMAT_S24_LE;
  637. case AFMT_S24_BE: return SNDRV_PCM_FORMAT_S24_BE;
  638. case AFMT_S24_PACKED: return SNDRV_PCM_FORMAT_S24_3LE;
  639. case AFMT_FLOAT: return SNDRV_PCM_FORMAT_FLOAT;
  640. case AFMT_SPDIF_RAW: return SNDRV_PCM_FORMAT_IEC958_SUBFRAME;
  641. default: return SNDRV_PCM_FORMAT_U8;
  642. }
  643. }
  644. static int snd_pcm_oss_format_to(snd_pcm_format_t format)
  645. {
  646. switch (format) {
  647. case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
  648. case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
  649. case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
  650. case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
  651. case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
  652. case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
  653. case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
  654. case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
  655. case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
  656. case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
  657. case SNDRV_PCM_FORMAT_S32_LE: return AFMT_S32_LE;
  658. case SNDRV_PCM_FORMAT_S32_BE: return AFMT_S32_BE;
  659. case SNDRV_PCM_FORMAT_S24_LE: return AFMT_S24_LE;
  660. case SNDRV_PCM_FORMAT_S24_BE: return AFMT_S24_BE;
  661. case SNDRV_PCM_FORMAT_S24_3LE: return AFMT_S24_PACKED;
  662. case SNDRV_PCM_FORMAT_FLOAT: return AFMT_FLOAT;
  663. case SNDRV_PCM_FORMAT_IEC958_SUBFRAME: return AFMT_SPDIF_RAW;
  664. default: return -EINVAL;
  665. }
  666. }
  667. static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
  668. struct snd_pcm_hw_params *oss_params,
  669. struct snd_pcm_hw_params *slave_params)
  670. {
  671. size_t s;
  672. size_t oss_buffer_size, oss_period_size, oss_periods;
  673. size_t min_period_size, max_period_size;
  674. struct snd_pcm_runtime *runtime = substream->runtime;
  675. size_t oss_frame_size;
  676. oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
  677. params_channels(oss_params) / 8;
  678. oss_buffer_size = snd_pcm_plug_client_size(substream,
  679. snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;
  680. oss_buffer_size = rounddown_pow_of_two(oss_buffer_size);
  681. if (atomic_read(&substream->mmap_count)) {
  682. if (oss_buffer_size > runtime->oss.mmap_bytes)
  683. oss_buffer_size = runtime->oss.mmap_bytes;
  684. }
  685. if (substream->oss.setup.period_size > 16)
  686. oss_period_size = substream->oss.setup.period_size;
  687. else if (runtime->oss.fragshift) {
  688. oss_period_size = 1 << runtime->oss.fragshift;
  689. if (oss_period_size > oss_buffer_size / 2)
  690. oss_period_size = oss_buffer_size / 2;
  691. } else {
  692. int sd;
  693. size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
  694. oss_period_size = oss_buffer_size;
  695. do {
  696. oss_period_size /= 2;
  697. } while (oss_period_size > bytes_per_sec);
  698. if (runtime->oss.subdivision == 0) {
  699. sd = 4;
  700. if (oss_period_size / sd > 4096)
  701. sd *= 2;
  702. if (oss_period_size / sd < 4096)
  703. sd = 1;
  704. } else
  705. sd = runtime->oss.subdivision;
  706. oss_period_size /= sd;
  707. if (oss_period_size < 16)
  708. oss_period_size = 16;
  709. }
  710. min_period_size = snd_pcm_plug_client_size(substream,
  711. snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
  712. min_period_size *= oss_frame_size;
  713. min_period_size = roundup_pow_of_two(min_period_size);
  714. if (oss_period_size < min_period_size)
  715. oss_period_size = min_period_size;
  716. max_period_size = snd_pcm_plug_client_size(substream,
  717. snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
  718. max_period_size *= oss_frame_size;
  719. max_period_size = rounddown_pow_of_two(max_period_size);
  720. if (oss_period_size > max_period_size)
  721. oss_period_size = max_period_size;
  722. oss_periods = oss_buffer_size / oss_period_size;
  723. if (substream->oss.setup.periods > 1)
  724. oss_periods = substream->oss.setup.periods;
  725. s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
  726. if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)
  727. s = runtime->oss.maxfrags;
  728. if (oss_periods > s)
  729. oss_periods = s;
  730. s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
  731. if (s < 2)
  732. s = 2;
  733. if (oss_periods < s)
  734. oss_periods = s;
  735. while (oss_period_size * oss_periods > oss_buffer_size)
  736. oss_period_size /= 2;
  737. if (oss_period_size < 16)
  738. return -EINVAL;
  739. runtime->oss.period_bytes = oss_period_size;
  740. runtime->oss.period_frames = 1;
  741. runtime->oss.periods = oss_periods;
  742. return 0;
  743. }
  744. static int choose_rate(struct snd_pcm_substream *substream,
  745. struct snd_pcm_hw_params *params, unsigned int best_rate)
  746. {
  747. struct snd_interval *it;
  748. struct snd_pcm_hw_params *save;
  749. unsigned int rate, prev;
  750. save = kmalloc(sizeof(*save), GFP_KERNEL);
  751. if (save == NULL)
  752. return -ENOMEM;
  753. *save = *params;
  754. it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);
  755. /* try multiples of the best rate */
  756. rate = best_rate;
  757. for (;;) {
  758. if (it->max < rate || (it->max == rate && it->openmax))
  759. break;
  760. if (it->min < rate || (it->min == rate && !it->openmin)) {
  761. int ret;
  762. ret = snd_pcm_hw_param_set(substream, params,
  763. SNDRV_PCM_HW_PARAM_RATE,
  764. rate, 0);
  765. if (ret == (int)rate) {
  766. kfree(save);
  767. return rate;
  768. }
  769. *params = *save;
  770. }
  771. prev = rate;
  772. rate += best_rate;
  773. if (rate <= prev)
  774. break;
  775. }
  776. /* not found, use the nearest rate */
  777. kfree(save);
  778. return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
  779. }
  780. /* parameter locking: returns immediately if tried during streaming */
  781. static int lock_params(struct snd_pcm_runtime *runtime)
  782. {
  783. if (mutex_lock_interruptible(&runtime->oss.params_lock))
  784. return -ERESTARTSYS;
  785. if (atomic_read(&runtime->oss.rw_ref)) {
  786. mutex_unlock(&runtime->oss.params_lock);
  787. return -EBUSY;
  788. }
  789. return 0;
  790. }
  791. static void unlock_params(struct snd_pcm_runtime *runtime)
  792. {
  793. mutex_unlock(&runtime->oss.params_lock);
  794. }
  795. /* call with params_lock held */
  796. static int snd_pcm_oss_change_params_locked(struct snd_pcm_substream *substream)
  797. {
  798. struct snd_pcm_runtime *runtime = substream->runtime;
  799. struct snd_pcm_hw_params *params, *sparams;
  800. struct snd_pcm_sw_params *sw_params;
  801. ssize_t oss_buffer_size, oss_period_size;
  802. size_t oss_frame_size;
  803. int err;
  804. int direct;
  805. snd_pcm_format_t format, sformat;
  806. int n;
  807. struct snd_mask sformat_mask;
  808. struct snd_mask mask;
  809. if (!runtime->oss.params)
  810. return 0;
  811. sw_params = kzalloc(sizeof(*sw_params), GFP_KERNEL);
  812. params = kmalloc(sizeof(*params), GFP_KERNEL);
  813. sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
  814. if (!sw_params || !params || !sparams) {
  815. err = -ENOMEM;
  816. goto failure;
  817. }
  818. if (atomic_read(&substream->mmap_count))
  819. direct = 1;
  820. else
  821. direct = substream->oss.setup.direct;
  822. _snd_pcm_hw_params_any(sparams);
  823. _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
  824. _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
  825. snd_mask_none(&mask);
  826. if (atomic_read(&substream->mmap_count))
  827. snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
  828. else {
  829. snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED);
  830. if (!direct)
  831. snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
  832. }
  833. err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
  834. if (err < 0) {
  835. pcm_dbg(substream->pcm, "No usable accesses\n");
  836. err = -EINVAL;
  837. goto failure;
  838. }
  839. choose_rate(substream, sparams, runtime->oss.rate);
  840. snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);
  841. format = snd_pcm_oss_format_from(runtime->oss.format);
  842. sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
  843. if (direct)
  844. sformat = format;
  845. else
  846. sformat = snd_pcm_plug_slave_format(format, &sformat_mask);
  847. if ((__force int)sformat < 0 ||
  848. !snd_mask_test(&sformat_mask, (__force int)sformat)) {
  849. for (sformat = (__force snd_pcm_format_t)0;
  850. (__force int)sformat <= (__force int)SNDRV_PCM_FORMAT_LAST;
  851. sformat = (__force snd_pcm_format_t)((__force int)sformat + 1)) {
  852. if (snd_mask_test(&sformat_mask, (__force int)sformat) &&
  853. snd_pcm_oss_format_to(sformat) >= 0)
  854. break;
  855. }
  856. if ((__force int)sformat > (__force int)SNDRV_PCM_FORMAT_LAST) {
  857. pcm_dbg(substream->pcm, "Cannot find a format!!!\n");
  858. err = -EINVAL;
  859. goto failure;
  860. }
  861. }
  862. err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, (__force int)sformat, 0);
  863. if (err < 0)
  864. goto failure;
  865. if (direct) {
  866. memcpy(params, sparams, sizeof(*params));
  867. } else {
  868. _snd_pcm_hw_params_any(params);
  869. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
  870. (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
  871. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
  872. (__force int)snd_pcm_oss_format_from(runtime->oss.format), 0);
  873. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
  874. runtime->oss.channels, 0);
  875. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
  876. runtime->oss.rate, 0);
  877. pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
  878. params_access(params), params_format(params),
  879. params_channels(params), params_rate(params));
  880. }
  881. pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
  882. params_access(sparams), params_format(sparams),
  883. params_channels(sparams), params_rate(sparams));
  884. oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
  885. params_channels(params) / 8;
  886. err = snd_pcm_oss_period_size(substream, params, sparams);
  887. if (err < 0)
  888. goto failure;
  889. n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
  890. err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
  891. if (err < 0)
  892. goto failure;
  893. err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
  894. runtime->oss.periods, NULL);
  895. if (err < 0)
  896. goto failure;
  897. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  898. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams);
  899. if (err < 0) {
  900. pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err);
  901. goto failure;
  902. }
  903. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  904. snd_pcm_oss_plugin_clear(substream);
  905. if (!direct) {
  906. /* add necessary plugins */
  907. snd_pcm_oss_plugin_clear(substream);
  908. if ((err = snd_pcm_plug_format_plugins(substream,
  909. params,
  910. sparams)) < 0) {
  911. pcm_dbg(substream->pcm,
  912. "snd_pcm_plug_format_plugins failed: %i\n", err);
  913. snd_pcm_oss_plugin_clear(substream);
  914. goto failure;
  915. }
  916. if (runtime->oss.plugin_first) {
  917. struct snd_pcm_plugin *plugin;
  918. if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {
  919. pcm_dbg(substream->pcm,
  920. "snd_pcm_plugin_build_io failed: %i\n", err);
  921. snd_pcm_oss_plugin_clear(substream);
  922. goto failure;
  923. }
  924. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  925. err = snd_pcm_plugin_append(plugin);
  926. } else {
  927. err = snd_pcm_plugin_insert(plugin);
  928. }
  929. if (err < 0) {
  930. snd_pcm_oss_plugin_clear(substream);
  931. goto failure;
  932. }
  933. }
  934. }
  935. #endif
  936. if (runtime->oss.trigger) {
  937. sw_params->start_threshold = 1;
  938. } else {
  939. sw_params->start_threshold = runtime->boundary;
  940. }
  941. if (atomic_read(&substream->mmap_count) ||
  942. substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  943. sw_params->stop_threshold = runtime->boundary;
  944. else
  945. sw_params->stop_threshold = runtime->buffer_size;
  946. sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
  947. sw_params->period_step = 1;
  948. sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  949. 1 : runtime->period_size;
  950. if (atomic_read(&substream->mmap_count) ||
  951. substream->oss.setup.nosilence) {
  952. sw_params->silence_threshold = 0;
  953. sw_params->silence_size = 0;
  954. } else {
  955. snd_pcm_uframes_t frames;
  956. frames = runtime->period_size + 16;
  957. if (frames > runtime->buffer_size)
  958. frames = runtime->buffer_size;
  959. sw_params->silence_threshold = frames;
  960. sw_params->silence_size = frames;
  961. }
  962. if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
  963. pcm_dbg(substream->pcm, "SW_PARAMS failed: %i\n", err);
  964. goto failure;
  965. }
  966. runtime->oss.periods = params_periods(sparams);
  967. oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
  968. if (oss_period_size < 0) {
  969. err = -EINVAL;
  970. goto failure;
  971. }
  972. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  973. if (runtime->oss.plugin_first) {
  974. err = snd_pcm_plug_alloc(substream, oss_period_size);
  975. if (err < 0)
  976. goto failure;
  977. }
  978. #endif
  979. oss_period_size *= oss_frame_size;
  980. oss_buffer_size = oss_period_size * runtime->oss.periods;
  981. if (oss_buffer_size < 0) {
  982. err = -EINVAL;
  983. goto failure;
  984. }
  985. runtime->oss.period_bytes = oss_period_size;
  986. runtime->oss.buffer_bytes = oss_buffer_size;
  987. pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
  988. runtime->oss.period_bytes,
  989. runtime->oss.buffer_bytes);
  990. pdprintf("slave: period_size = %i, buffer_size = %i\n",
  991. params_period_size(sparams),
  992. params_buffer_size(sparams));
  993. runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
  994. runtime->oss.channels = params_channels(params);
  995. runtime->oss.rate = params_rate(params);
  996. vfree(runtime->oss.buffer);
  997. runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
  998. if (!runtime->oss.buffer) {
  999. err = -ENOMEM;
  1000. goto failure;
  1001. }
  1002. runtime->oss.params = 0;
  1003. runtime->oss.prepare = 1;
  1004. runtime->oss.buffer_used = 0;
  1005. if (runtime->dma_area)
  1006. snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
  1007. runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
  1008. err = 0;
  1009. failure:
  1010. kfree(sw_params);
  1011. kfree(params);
  1012. kfree(sparams);
  1013. return err;
  1014. }
  1015. /* this one takes the lock by itself */
  1016. static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream,
  1017. bool trylock)
  1018. {
  1019. struct snd_pcm_runtime *runtime = substream->runtime;
  1020. int err;
  1021. if (trylock) {
  1022. if (!(mutex_trylock(&runtime->oss.params_lock)))
  1023. return -EAGAIN;
  1024. } else if (mutex_lock_interruptible(&runtime->oss.params_lock))
  1025. return -ERESTARTSYS;
  1026. err = snd_pcm_oss_change_params_locked(substream);
  1027. mutex_unlock(&runtime->oss.params_lock);
  1028. return err;
  1029. }
  1030. static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
  1031. {
  1032. int idx, err;
  1033. struct snd_pcm_substream *asubstream = NULL, *substream;
  1034. for (idx = 0; idx < 2; idx++) {
  1035. substream = pcm_oss_file->streams[idx];
  1036. if (substream == NULL)
  1037. continue;
  1038. if (asubstream == NULL)
  1039. asubstream = substream;
  1040. if (substream->runtime->oss.params) {
  1041. err = snd_pcm_oss_change_params(substream, false);
  1042. if (err < 0)
  1043. return err;
  1044. }
  1045. }
  1046. if (!asubstream)
  1047. return -EIO;
  1048. if (r_substream)
  1049. *r_substream = asubstream;
  1050. return 0;
  1051. }
  1052. /* call with params_lock held */
  1053. /* NOTE: this always call PREPARE unconditionally no matter whether
  1054. * runtime->oss.prepare is set or not
  1055. */
  1056. static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
  1057. {
  1058. int err;
  1059. struct snd_pcm_runtime *runtime = substream->runtime;
  1060. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
  1061. if (err < 0) {
  1062. pcm_dbg(substream->pcm,
  1063. "snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
  1064. return err;
  1065. }
  1066. runtime->oss.prepare = 0;
  1067. runtime->oss.prev_hw_ptr_period = 0;
  1068. runtime->oss.period_ptr = 0;
  1069. runtime->oss.buffer_used = 0;
  1070. return 0;
  1071. }
  1072. static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
  1073. {
  1074. struct snd_pcm_runtime *runtime;
  1075. int err;
  1076. runtime = substream->runtime;
  1077. if (runtime->oss.params) {
  1078. err = snd_pcm_oss_change_params(substream, false);
  1079. if (err < 0)
  1080. return err;
  1081. }
  1082. if (runtime->oss.prepare) {
  1083. if (mutex_lock_interruptible(&runtime->oss.params_lock))
  1084. return -ERESTARTSYS;
  1085. err = snd_pcm_oss_prepare(substream);
  1086. mutex_unlock(&runtime->oss.params_lock);
  1087. if (err < 0)
  1088. return err;
  1089. }
  1090. return 0;
  1091. }
  1092. /* call with params_lock held */
  1093. static int snd_pcm_oss_make_ready_locked(struct snd_pcm_substream *substream)
  1094. {
  1095. struct snd_pcm_runtime *runtime;
  1096. int err;
  1097. runtime = substream->runtime;
  1098. if (runtime->oss.params) {
  1099. err = snd_pcm_oss_change_params_locked(substream);
  1100. if (err < 0)
  1101. return err;
  1102. }
  1103. if (runtime->oss.prepare) {
  1104. err = snd_pcm_oss_prepare(substream);
  1105. if (err < 0)
  1106. return err;
  1107. }
  1108. return 0;
  1109. }
  1110. static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
  1111. {
  1112. struct snd_pcm_runtime *runtime;
  1113. snd_pcm_uframes_t frames;
  1114. int err = 0;
  1115. while (1) {
  1116. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
  1117. if (err < 0)
  1118. break;
  1119. runtime = substream->runtime;
  1120. if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
  1121. break;
  1122. /* in case of overrun, skip whole periods like OSS/Linux driver does */
  1123. /* until avail(delay) <= buffer_size */
  1124. frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
  1125. frames /= runtime->period_size;
  1126. frames *= runtime->period_size;
  1127. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
  1128. if (err < 0)
  1129. break;
  1130. }
  1131. return err;
  1132. }
  1133. snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
  1134. {
  1135. struct snd_pcm_runtime *runtime = substream->runtime;
  1136. int ret;
  1137. while (1) {
  1138. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1139. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1140. #ifdef OSS_DEBUG
  1141. pcm_dbg(substream->pcm,
  1142. "pcm_oss: write: recovering from %s\n",
  1143. runtime->status->state == SNDRV_PCM_STATE_XRUN ?
  1144. "XRUN" : "SUSPEND");
  1145. #endif
  1146. ret = snd_pcm_oss_prepare(substream);
  1147. if (ret < 0)
  1148. break;
  1149. }
  1150. if (in_kernel) {
  1151. mm_segment_t fs;
  1152. fs = snd_enter_user();
  1153. ret = snd_pcm_lib_write(substream, (void __force __user *)ptr, frames);
  1154. snd_leave_user(fs);
  1155. } else {
  1156. ret = snd_pcm_lib_write(substream, (void __force __user *)ptr, frames);
  1157. }
  1158. if (ret != -EPIPE && ret != -ESTRPIPE)
  1159. break;
  1160. /* test, if we can't store new data, because the stream */
  1161. /* has not been started */
  1162. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
  1163. return -EAGAIN;
  1164. }
  1165. return ret;
  1166. }
  1167. snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
  1168. {
  1169. struct snd_pcm_runtime *runtime = substream->runtime;
  1170. snd_pcm_sframes_t delay;
  1171. int ret;
  1172. while (1) {
  1173. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1174. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1175. #ifdef OSS_DEBUG
  1176. pcm_dbg(substream->pcm,
  1177. "pcm_oss: read: recovering from %s\n",
  1178. runtime->status->state == SNDRV_PCM_STATE_XRUN ?
  1179. "XRUN" : "SUSPEND");
  1180. #endif
  1181. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1182. if (ret < 0)
  1183. break;
  1184. } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
  1185. ret = snd_pcm_oss_prepare(substream);
  1186. if (ret < 0)
  1187. break;
  1188. }
  1189. ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
  1190. if (ret < 0)
  1191. break;
  1192. if (in_kernel) {
  1193. mm_segment_t fs;
  1194. fs = snd_enter_user();
  1195. ret = snd_pcm_lib_read(substream, (void __force __user *)ptr, frames);
  1196. snd_leave_user(fs);
  1197. } else {
  1198. ret = snd_pcm_lib_read(substream, (void __force __user *)ptr, frames);
  1199. }
  1200. if (ret == -EPIPE) {
  1201. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  1202. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1203. if (ret < 0)
  1204. break;
  1205. }
  1206. continue;
  1207. }
  1208. if (ret != -ESTRPIPE)
  1209. break;
  1210. }
  1211. return ret;
  1212. }
  1213. snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
  1214. {
  1215. struct snd_pcm_runtime *runtime = substream->runtime;
  1216. int ret;
  1217. while (1) {
  1218. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1219. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1220. #ifdef OSS_DEBUG
  1221. pcm_dbg(substream->pcm,
  1222. "pcm_oss: writev: recovering from %s\n",
  1223. runtime->status->state == SNDRV_PCM_STATE_XRUN ?
  1224. "XRUN" : "SUSPEND");
  1225. #endif
  1226. ret = snd_pcm_oss_prepare(substream);
  1227. if (ret < 0)
  1228. break;
  1229. }
  1230. if (in_kernel) {
  1231. mm_segment_t fs;
  1232. fs = snd_enter_user();
  1233. ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
  1234. snd_leave_user(fs);
  1235. } else {
  1236. ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
  1237. }
  1238. if (ret != -EPIPE && ret != -ESTRPIPE)
  1239. break;
  1240. /* test, if we can't store new data, because the stream */
  1241. /* has not been started */
  1242. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
  1243. return -EAGAIN;
  1244. }
  1245. return ret;
  1246. }
  1247. snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
  1248. {
  1249. struct snd_pcm_runtime *runtime = substream->runtime;
  1250. int ret;
  1251. while (1) {
  1252. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1253. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1254. #ifdef OSS_DEBUG
  1255. pcm_dbg(substream->pcm,
  1256. "pcm_oss: readv: recovering from %s\n",
  1257. runtime->status->state == SNDRV_PCM_STATE_XRUN ?
  1258. "XRUN" : "SUSPEND");
  1259. #endif
  1260. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1261. if (ret < 0)
  1262. break;
  1263. } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
  1264. ret = snd_pcm_oss_prepare(substream);
  1265. if (ret < 0)
  1266. break;
  1267. }
  1268. if (in_kernel) {
  1269. mm_segment_t fs;
  1270. fs = snd_enter_user();
  1271. ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
  1272. snd_leave_user(fs);
  1273. } else {
  1274. ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
  1275. }
  1276. if (ret != -EPIPE && ret != -ESTRPIPE)
  1277. break;
  1278. }
  1279. return ret;
  1280. }
  1281. static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
  1282. {
  1283. struct snd_pcm_runtime *runtime = substream->runtime;
  1284. snd_pcm_sframes_t frames, frames1;
  1285. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  1286. if (runtime->oss.plugin_first) {
  1287. struct snd_pcm_plugin_channel *channels;
  1288. size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
  1289. if (!in_kernel) {
  1290. if (copy_from_user(runtime->oss.buffer, (const char __force __user *)buf, bytes))
  1291. return -EFAULT;
  1292. buf = runtime->oss.buffer;
  1293. }
  1294. frames = bytes / oss_frame_bytes;
  1295. frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
  1296. if (frames1 < 0)
  1297. return frames1;
  1298. frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
  1299. if (frames1 <= 0)
  1300. return frames1;
  1301. bytes = frames1 * oss_frame_bytes;
  1302. } else
  1303. #endif
  1304. {
  1305. frames = bytes_to_frames(runtime, bytes);
  1306. frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
  1307. if (frames1 <= 0)
  1308. return frames1;
  1309. bytes = frames_to_bytes(runtime, frames1);
  1310. }
  1311. return bytes;
  1312. }
  1313. static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
  1314. {
  1315. size_t xfer = 0;
  1316. ssize_t tmp = 0;
  1317. struct snd_pcm_runtime *runtime = substream->runtime;
  1318. if (atomic_read(&substream->mmap_count))
  1319. return -ENXIO;
  1320. atomic_inc(&runtime->oss.rw_ref);
  1321. while (bytes > 0) {
  1322. if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
  1323. tmp = -ERESTARTSYS;
  1324. break;
  1325. }
  1326. tmp = snd_pcm_oss_make_ready_locked(substream);
  1327. if (tmp < 0)
  1328. goto err;
  1329. if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
  1330. tmp = bytes;
  1331. if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
  1332. tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
  1333. if (tmp > 0) {
  1334. if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
  1335. tmp = -EFAULT;
  1336. goto err;
  1337. }
  1338. }
  1339. runtime->oss.buffer_used += tmp;
  1340. buf += tmp;
  1341. bytes -= tmp;
  1342. xfer += tmp;
  1343. if (substream->oss.setup.partialfrag ||
  1344. runtime->oss.buffer_used == runtime->oss.period_bytes) {
  1345. tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
  1346. runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
  1347. if (tmp <= 0)
  1348. goto err;
  1349. runtime->oss.bytes += tmp;
  1350. runtime->oss.period_ptr += tmp;
  1351. runtime->oss.period_ptr %= runtime->oss.period_bytes;
  1352. if (runtime->oss.period_ptr == 0 ||
  1353. runtime->oss.period_ptr == runtime->oss.buffer_used)
  1354. runtime->oss.buffer_used = 0;
  1355. else if ((substream->f_flags & O_NONBLOCK) != 0) {
  1356. tmp = -EAGAIN;
  1357. goto err;
  1358. }
  1359. }
  1360. } else {
  1361. tmp = snd_pcm_oss_write2(substream,
  1362. (const char __force *)buf,
  1363. runtime->oss.period_bytes, 0);
  1364. if (tmp <= 0)
  1365. goto err;
  1366. runtime->oss.bytes += tmp;
  1367. buf += tmp;
  1368. bytes -= tmp;
  1369. xfer += tmp;
  1370. if ((substream->f_flags & O_NONBLOCK) != 0 &&
  1371. tmp != runtime->oss.period_bytes)
  1372. tmp = -EAGAIN;
  1373. }
  1374. err:
  1375. mutex_unlock(&runtime->oss.params_lock);
  1376. if (tmp < 0)
  1377. break;
  1378. if (signal_pending(current)) {
  1379. tmp = -ERESTARTSYS;
  1380. break;
  1381. }
  1382. tmp = 0;
  1383. }
  1384. atomic_dec(&runtime->oss.rw_ref);
  1385. return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
  1386. }
  1387. static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
  1388. {
  1389. struct snd_pcm_runtime *runtime = substream->runtime;
  1390. snd_pcm_sframes_t frames, frames1;
  1391. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  1392. char __user *final_dst = (char __force __user *)buf;
  1393. if (runtime->oss.plugin_first) {
  1394. struct snd_pcm_plugin_channel *channels;
  1395. size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
  1396. if (!in_kernel)
  1397. buf = runtime->oss.buffer;
  1398. frames = bytes / oss_frame_bytes;
  1399. frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
  1400. if (frames1 < 0)
  1401. return frames1;
  1402. frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
  1403. if (frames1 <= 0)
  1404. return frames1;
  1405. bytes = frames1 * oss_frame_bytes;
  1406. if (!in_kernel && copy_to_user(final_dst, buf, bytes))
  1407. return -EFAULT;
  1408. } else
  1409. #endif
  1410. {
  1411. frames = bytes_to_frames(runtime, bytes);
  1412. frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
  1413. if (frames1 <= 0)
  1414. return frames1;
  1415. bytes = frames_to_bytes(runtime, frames1);
  1416. }
  1417. return bytes;
  1418. }
  1419. static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
  1420. {
  1421. size_t xfer = 0;
  1422. ssize_t tmp = 0;
  1423. struct snd_pcm_runtime *runtime = substream->runtime;
  1424. if (atomic_read(&substream->mmap_count))
  1425. return -ENXIO;
  1426. atomic_inc(&runtime->oss.rw_ref);
  1427. while (bytes > 0) {
  1428. if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
  1429. tmp = -ERESTARTSYS;
  1430. break;
  1431. }
  1432. tmp = snd_pcm_oss_make_ready_locked(substream);
  1433. if (tmp < 0)
  1434. goto err;
  1435. if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
  1436. if (runtime->oss.buffer_used == 0) {
  1437. tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
  1438. if (tmp <= 0)
  1439. goto err;
  1440. runtime->oss.bytes += tmp;
  1441. runtime->oss.period_ptr = tmp;
  1442. runtime->oss.buffer_used = tmp;
  1443. }
  1444. tmp = bytes;
  1445. if ((size_t) tmp > runtime->oss.buffer_used)
  1446. tmp = runtime->oss.buffer_used;
  1447. if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
  1448. tmp = -EFAULT;
  1449. goto err;
  1450. }
  1451. buf += tmp;
  1452. bytes -= tmp;
  1453. xfer += tmp;
  1454. runtime->oss.buffer_used -= tmp;
  1455. } else {
  1456. tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
  1457. runtime->oss.period_bytes, 0);
  1458. if (tmp <= 0)
  1459. goto err;
  1460. runtime->oss.bytes += tmp;
  1461. buf += tmp;
  1462. bytes -= tmp;
  1463. xfer += tmp;
  1464. }
  1465. err:
  1466. mutex_unlock(&runtime->oss.params_lock);
  1467. if (tmp < 0)
  1468. break;
  1469. if (signal_pending(current)) {
  1470. tmp = -ERESTARTSYS;
  1471. break;
  1472. }
  1473. tmp = 0;
  1474. }
  1475. atomic_dec(&runtime->oss.rw_ref);
  1476. return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
  1477. }
  1478. static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
  1479. {
  1480. struct snd_pcm_substream *substream;
  1481. struct snd_pcm_runtime *runtime;
  1482. int i;
  1483. for (i = 0; i < 2; i++) {
  1484. substream = pcm_oss_file->streams[i];
  1485. if (!substream)
  1486. continue;
  1487. runtime = substream->runtime;
  1488. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1489. mutex_lock(&runtime->oss.params_lock);
  1490. runtime->oss.prepare = 1;
  1491. runtime->oss.buffer_used = 0;
  1492. runtime->oss.prev_hw_ptr_period = 0;
  1493. runtime->oss.period_ptr = 0;
  1494. mutex_unlock(&runtime->oss.params_lock);
  1495. }
  1496. return 0;
  1497. }
  1498. static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
  1499. {
  1500. struct snd_pcm_substream *substream;
  1501. int err;
  1502. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1503. if (substream != NULL) {
  1504. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1505. return err;
  1506. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
  1507. }
  1508. /* note: all errors from the start action are ignored */
  1509. /* OSS apps do not know, how to handle them */
  1510. return 0;
  1511. }
  1512. static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
  1513. {
  1514. struct snd_pcm_runtime *runtime;
  1515. ssize_t result = 0;
  1516. snd_pcm_state_t state;
  1517. long res;
  1518. wait_queue_t wait;
  1519. runtime = substream->runtime;
  1520. init_waitqueue_entry(&wait, current);
  1521. add_wait_queue(&runtime->sleep, &wait);
  1522. #ifdef OSS_DEBUG
  1523. pcm_dbg(substream->pcm, "sync1: size = %li\n", size);
  1524. #endif
  1525. while (1) {
  1526. result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
  1527. if (result > 0) {
  1528. runtime->oss.buffer_used = 0;
  1529. result = 0;
  1530. break;
  1531. }
  1532. if (result != 0 && result != -EAGAIN)
  1533. break;
  1534. result = 0;
  1535. set_current_state(TASK_INTERRUPTIBLE);
  1536. snd_pcm_stream_lock_irq(substream);
  1537. state = runtime->status->state;
  1538. snd_pcm_stream_unlock_irq(substream);
  1539. if (state != SNDRV_PCM_STATE_RUNNING) {
  1540. set_current_state(TASK_RUNNING);
  1541. break;
  1542. }
  1543. res = schedule_timeout(10 * HZ);
  1544. if (signal_pending(current)) {
  1545. result = -ERESTARTSYS;
  1546. break;
  1547. }
  1548. if (res == 0) {
  1549. pcm_err(substream->pcm,
  1550. "OSS sync error - DMA timeout\n");
  1551. result = -EIO;
  1552. break;
  1553. }
  1554. }
  1555. remove_wait_queue(&runtime->sleep, &wait);
  1556. return result;
  1557. }
  1558. static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
  1559. {
  1560. int err = 0;
  1561. unsigned int saved_f_flags;
  1562. struct snd_pcm_substream *substream;
  1563. struct snd_pcm_runtime *runtime;
  1564. snd_pcm_format_t format;
  1565. unsigned long width;
  1566. size_t size;
  1567. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1568. if (substream != NULL) {
  1569. runtime = substream->runtime;
  1570. if (atomic_read(&substream->mmap_count))
  1571. goto __direct;
  1572. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1573. return err;
  1574. atomic_inc(&runtime->oss.rw_ref);
  1575. if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
  1576. atomic_dec(&runtime->oss.rw_ref);
  1577. return -ERESTARTSYS;
  1578. }
  1579. format = snd_pcm_oss_format_from(runtime->oss.format);
  1580. width = snd_pcm_format_physical_width(format);
  1581. if (runtime->oss.buffer_used > 0) {
  1582. #ifdef OSS_DEBUG
  1583. pcm_dbg(substream->pcm, "sync: buffer_used\n");
  1584. #endif
  1585. size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
  1586. snd_pcm_format_set_silence(format,
  1587. runtime->oss.buffer + runtime->oss.buffer_used,
  1588. size);
  1589. err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
  1590. if (err < 0)
  1591. goto unlock;
  1592. } else if (runtime->oss.period_ptr > 0) {
  1593. #ifdef OSS_DEBUG
  1594. pcm_dbg(substream->pcm, "sync: period_ptr\n");
  1595. #endif
  1596. size = runtime->oss.period_bytes - runtime->oss.period_ptr;
  1597. snd_pcm_format_set_silence(format,
  1598. runtime->oss.buffer,
  1599. size * 8 / width);
  1600. err = snd_pcm_oss_sync1(substream, size);
  1601. if (err < 0)
  1602. goto unlock;
  1603. }
  1604. /*
  1605. * The ALSA's period might be a bit large than OSS one.
  1606. * Fill the remain portion of ALSA period with zeros.
  1607. */
  1608. size = runtime->control->appl_ptr % runtime->period_size;
  1609. if (size > 0) {
  1610. size = runtime->period_size - size;
  1611. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
  1612. size = (runtime->frame_bits * size) / 8;
  1613. while (size > 0) {
  1614. mm_segment_t fs;
  1615. size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
  1616. size -= size1;
  1617. size1 *= 8;
  1618. size1 /= runtime->sample_bits;
  1619. snd_pcm_format_set_silence(runtime->format,
  1620. runtime->oss.buffer,
  1621. size1);
  1622. size1 /= runtime->channels; /* frames */
  1623. fs = snd_enter_user();
  1624. snd_pcm_lib_write(substream, (void __force __user *)runtime->oss.buffer, size1);
  1625. snd_leave_user(fs);
  1626. }
  1627. } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
  1628. void __user *buffers[runtime->channels];
  1629. memset(buffers, 0, runtime->channels * sizeof(void *));
  1630. snd_pcm_lib_writev(substream, buffers, size);
  1631. }
  1632. }
  1633. unlock:
  1634. mutex_unlock(&runtime->oss.params_lock);
  1635. atomic_dec(&runtime->oss.rw_ref);
  1636. if (err < 0)
  1637. return err;
  1638. /*
  1639. * finish sync: drain the buffer
  1640. */
  1641. __direct:
  1642. saved_f_flags = substream->f_flags;
  1643. substream->f_flags &= ~O_NONBLOCK;
  1644. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1645. substream->f_flags = saved_f_flags;
  1646. if (err < 0)
  1647. return err;
  1648. mutex_lock(&runtime->oss.params_lock);
  1649. runtime->oss.prepare = 1;
  1650. mutex_unlock(&runtime->oss.params_lock);
  1651. }
  1652. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1653. if (substream != NULL) {
  1654. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1655. return err;
  1656. runtime = substream->runtime;
  1657. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1658. if (err < 0)
  1659. return err;
  1660. mutex_lock(&runtime->oss.params_lock);
  1661. runtime->oss.buffer_used = 0;
  1662. runtime->oss.prepare = 1;
  1663. mutex_unlock(&runtime->oss.params_lock);
  1664. }
  1665. return 0;
  1666. }
  1667. static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
  1668. {
  1669. int idx;
  1670. for (idx = 1; idx >= 0; --idx) {
  1671. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1672. struct snd_pcm_runtime *runtime;
  1673. int err;
  1674. if (substream == NULL)
  1675. continue;
  1676. runtime = substream->runtime;
  1677. if (rate < 1000)
  1678. rate = 1000;
  1679. else if (rate > 192000)
  1680. rate = 192000;
  1681. err = lock_params(runtime);
  1682. if (err < 0)
  1683. return err;
  1684. if (runtime->oss.rate != rate) {
  1685. runtime->oss.params = 1;
  1686. runtime->oss.rate = rate;
  1687. }
  1688. unlock_params(runtime);
  1689. }
  1690. return snd_pcm_oss_get_rate(pcm_oss_file);
  1691. }
  1692. static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
  1693. {
  1694. struct snd_pcm_substream *substream;
  1695. int err;
  1696. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1697. return err;
  1698. return substream->runtime->oss.rate;
  1699. }
  1700. static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
  1701. {
  1702. int idx;
  1703. if (channels < 1)
  1704. channels = 1;
  1705. if (channels > 128)
  1706. return -EINVAL;
  1707. for (idx = 1; idx >= 0; --idx) {
  1708. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1709. struct snd_pcm_runtime *runtime;
  1710. int err;
  1711. if (substream == NULL)
  1712. continue;
  1713. runtime = substream->runtime;
  1714. err = lock_params(runtime);
  1715. if (err < 0)
  1716. return err;
  1717. if (runtime->oss.channels != channels) {
  1718. runtime->oss.params = 1;
  1719. runtime->oss.channels = channels;
  1720. }
  1721. unlock_params(runtime);
  1722. }
  1723. return snd_pcm_oss_get_channels(pcm_oss_file);
  1724. }
  1725. static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
  1726. {
  1727. struct snd_pcm_substream *substream;
  1728. int err;
  1729. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1730. return err;
  1731. return substream->runtime->oss.channels;
  1732. }
  1733. static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
  1734. {
  1735. struct snd_pcm_substream *substream;
  1736. int err;
  1737. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1738. return err;
  1739. return substream->runtime->oss.period_bytes;
  1740. }
  1741. static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
  1742. {
  1743. struct snd_pcm_substream *substream;
  1744. int err;
  1745. int direct;
  1746. struct snd_pcm_hw_params *params;
  1747. unsigned int formats = 0;
  1748. struct snd_mask format_mask;
  1749. int fmt;
  1750. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1751. return err;
  1752. if (atomic_read(&substream->mmap_count))
  1753. direct = 1;
  1754. else
  1755. direct = substream->oss.setup.direct;
  1756. if (!direct)
  1757. return AFMT_MU_LAW | AFMT_U8 |
  1758. AFMT_S16_LE | AFMT_S16_BE |
  1759. AFMT_S8 | AFMT_U16_LE |
  1760. AFMT_U16_BE |
  1761. AFMT_S32_LE | AFMT_S32_BE |
  1762. AFMT_S24_LE | AFMT_S24_BE |
  1763. AFMT_S24_PACKED;
  1764. params = kmalloc(sizeof(*params), GFP_KERNEL);
  1765. if (!params)
  1766. return -ENOMEM;
  1767. _snd_pcm_hw_params_any(params);
  1768. err = snd_pcm_hw_refine(substream, params);
  1769. if (err < 0)
  1770. goto error;
  1771. format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1772. for (fmt = 0; fmt < 32; ++fmt) {
  1773. if (snd_mask_test(&format_mask, fmt)) {
  1774. int f = snd_pcm_oss_format_to(fmt);
  1775. if (f >= 0)
  1776. formats |= f;
  1777. }
  1778. }
  1779. error:
  1780. kfree(params);
  1781. return err < 0 ? err : formats;
  1782. }
  1783. static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
  1784. {
  1785. int formats, idx;
  1786. int err;
  1787. if (format != AFMT_QUERY) {
  1788. formats = snd_pcm_oss_get_formats(pcm_oss_file);
  1789. if (formats < 0)
  1790. return formats;
  1791. if (!(formats & format))
  1792. format = AFMT_U8;
  1793. for (idx = 1; idx >= 0; --idx) {
  1794. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1795. struct snd_pcm_runtime *runtime;
  1796. if (substream == NULL)
  1797. continue;
  1798. runtime = substream->runtime;
  1799. err = lock_params(runtime);
  1800. if (err < 0)
  1801. return err;
  1802. if (runtime->oss.format != format) {
  1803. runtime->oss.params = 1;
  1804. runtime->oss.format = format;
  1805. }
  1806. unlock_params(runtime);
  1807. }
  1808. }
  1809. return snd_pcm_oss_get_format(pcm_oss_file);
  1810. }
  1811. static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
  1812. {
  1813. struct snd_pcm_substream *substream;
  1814. int err;
  1815. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1816. return err;
  1817. return substream->runtime->oss.format;
  1818. }
  1819. static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
  1820. {
  1821. struct snd_pcm_runtime *runtime;
  1822. runtime = substream->runtime;
  1823. if (subdivide == 0) {
  1824. subdivide = runtime->oss.subdivision;
  1825. if (subdivide == 0)
  1826. subdivide = 1;
  1827. return subdivide;
  1828. }
  1829. if (runtime->oss.subdivision || runtime->oss.fragshift)
  1830. return -EINVAL;
  1831. if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
  1832. subdivide != 8 && subdivide != 16)
  1833. return -EINVAL;
  1834. runtime->oss.subdivision = subdivide;
  1835. runtime->oss.params = 1;
  1836. return subdivide;
  1837. }
  1838. static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
  1839. {
  1840. int err = -EINVAL, idx;
  1841. for (idx = 1; idx >= 0; --idx) {
  1842. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1843. struct snd_pcm_runtime *runtime;
  1844. if (substream == NULL)
  1845. continue;
  1846. runtime = substream->runtime;
  1847. err = lock_params(runtime);
  1848. if (err < 0)
  1849. return err;
  1850. err = snd_pcm_oss_set_subdivide1(substream, subdivide);
  1851. unlock_params(runtime);
  1852. if (err < 0)
  1853. return err;
  1854. }
  1855. return err;
  1856. }
  1857. static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
  1858. {
  1859. struct snd_pcm_runtime *runtime;
  1860. runtime = substream->runtime;
  1861. if (runtime->oss.subdivision || runtime->oss.fragshift)
  1862. return -EINVAL;
  1863. runtime->oss.fragshift = val & 0xffff;
  1864. runtime->oss.maxfrags = (val >> 16) & 0xffff;
  1865. if (runtime->oss.fragshift < 4) /* < 16 */
  1866. runtime->oss.fragshift = 4;
  1867. if (runtime->oss.maxfrags < 2)
  1868. runtime->oss.maxfrags = 2;
  1869. runtime->oss.params = 1;
  1870. return 0;
  1871. }
  1872. static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
  1873. {
  1874. int err = -EINVAL, idx;
  1875. for (idx = 1; idx >= 0; --idx) {
  1876. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1877. struct snd_pcm_runtime *runtime;
  1878. if (substream == NULL)
  1879. continue;
  1880. runtime = substream->runtime;
  1881. err = lock_params(runtime);
  1882. if (err < 0)
  1883. return err;
  1884. err = snd_pcm_oss_set_fragment1(substream, val);
  1885. unlock_params(runtime);
  1886. if (err < 0)
  1887. return err;
  1888. }
  1889. return err;
  1890. }
  1891. static int snd_pcm_oss_nonblock(struct file * file)
  1892. {
  1893. spin_lock(&file->f_lock);
  1894. file->f_flags |= O_NONBLOCK;
  1895. spin_unlock(&file->f_lock);
  1896. return 0;
  1897. }
  1898. static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
  1899. {
  1900. if (substream == NULL) {
  1901. res &= ~DSP_CAP_DUPLEX;
  1902. return res;
  1903. }
  1904. #ifdef DSP_CAP_MULTI
  1905. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1906. if (substream->pstr->substream_count > 1)
  1907. res |= DSP_CAP_MULTI;
  1908. #endif
  1909. /* DSP_CAP_REALTIME is set all times: */
  1910. /* all ALSA drivers can return actual pointer in ring buffer */
  1911. #if defined(DSP_CAP_REALTIME) && 0
  1912. {
  1913. struct snd_pcm_runtime *runtime = substream->runtime;
  1914. if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
  1915. res &= ~DSP_CAP_REALTIME;
  1916. }
  1917. #endif
  1918. return res;
  1919. }
  1920. static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
  1921. {
  1922. int result, idx;
  1923. result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
  1924. for (idx = 0; idx < 2; idx++) {
  1925. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1926. result = snd_pcm_oss_get_caps1(substream, result);
  1927. }
  1928. result |= 0x0001; /* revision - same as SB AWE 64 */
  1929. return result;
  1930. }
  1931. static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream,
  1932. snd_pcm_uframes_t hw_ptr)
  1933. {
  1934. struct snd_pcm_runtime *runtime = substream->runtime;
  1935. snd_pcm_uframes_t appl_ptr;
  1936. appl_ptr = hw_ptr + runtime->buffer_size;
  1937. appl_ptr %= runtime->boundary;
  1938. runtime->control->appl_ptr = appl_ptr;
  1939. }
  1940. static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
  1941. {
  1942. struct snd_pcm_runtime *runtime;
  1943. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  1944. int err, cmd;
  1945. #ifdef OSS_DEBUG
  1946. pcm_dbg(substream->pcm, "pcm_oss: trigger = 0x%x\n", trigger);
  1947. #endif
  1948. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1949. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1950. if (psubstream) {
  1951. if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
  1952. return err;
  1953. }
  1954. if (csubstream) {
  1955. if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
  1956. return err;
  1957. }
  1958. if (psubstream) {
  1959. runtime = psubstream->runtime;
  1960. cmd = 0;
  1961. if (mutex_lock_interruptible(&runtime->oss.params_lock))
  1962. return -ERESTARTSYS;
  1963. if (trigger & PCM_ENABLE_OUTPUT) {
  1964. if (runtime->oss.trigger)
  1965. goto _skip1;
  1966. if (atomic_read(&psubstream->mmap_count))
  1967. snd_pcm_oss_simulate_fill(psubstream,
  1968. get_hw_ptr_period(runtime));
  1969. runtime->oss.trigger = 1;
  1970. runtime->start_threshold = 1;
  1971. cmd = SNDRV_PCM_IOCTL_START;
  1972. } else {
  1973. if (!runtime->oss.trigger)
  1974. goto _skip1;
  1975. runtime->oss.trigger = 0;
  1976. runtime->start_threshold = runtime->boundary;
  1977. cmd = SNDRV_PCM_IOCTL_DROP;
  1978. runtime->oss.prepare = 1;
  1979. }
  1980. _skip1:
  1981. mutex_unlock(&runtime->oss.params_lock);
  1982. if (cmd) {
  1983. err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
  1984. if (err < 0)
  1985. return err;
  1986. }
  1987. }
  1988. if (csubstream) {
  1989. runtime = csubstream->runtime;
  1990. cmd = 0;
  1991. if (mutex_lock_interruptible(&runtime->oss.params_lock))
  1992. return -ERESTARTSYS;
  1993. if (trigger & PCM_ENABLE_INPUT) {
  1994. if (runtime->oss.trigger)
  1995. goto _skip2;
  1996. runtime->oss.trigger = 1;
  1997. runtime->start_threshold = 1;
  1998. cmd = SNDRV_PCM_IOCTL_START;
  1999. } else {
  2000. if (!runtime->oss.trigger)
  2001. goto _skip2;
  2002. runtime->oss.trigger = 0;
  2003. runtime->start_threshold = runtime->boundary;
  2004. cmd = SNDRV_PCM_IOCTL_DROP;
  2005. runtime->oss.prepare = 1;
  2006. }
  2007. _skip2:
  2008. mutex_unlock(&runtime->oss.params_lock);
  2009. if (cmd) {
  2010. err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
  2011. if (err < 0)
  2012. return err;
  2013. }
  2014. }
  2015. return 0;
  2016. }
  2017. static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
  2018. {
  2019. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  2020. int result = 0;
  2021. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2022. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2023. if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
  2024. result |= PCM_ENABLE_OUTPUT;
  2025. if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
  2026. result |= PCM_ENABLE_INPUT;
  2027. return result;
  2028. }
  2029. static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
  2030. {
  2031. struct snd_pcm_substream *substream;
  2032. struct snd_pcm_runtime *runtime;
  2033. snd_pcm_sframes_t delay;
  2034. int err;
  2035. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2036. if (substream == NULL)
  2037. return -EINVAL;
  2038. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  2039. return err;
  2040. runtime = substream->runtime;
  2041. if (runtime->oss.params || runtime->oss.prepare)
  2042. return 0;
  2043. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
  2044. if (err == -EPIPE)
  2045. delay = 0; /* hack for broken OSS applications */
  2046. else if (err < 0)
  2047. return err;
  2048. return snd_pcm_oss_bytes(substream, delay);
  2049. }
  2050. static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
  2051. {
  2052. struct snd_pcm_substream *substream;
  2053. struct snd_pcm_runtime *runtime;
  2054. snd_pcm_sframes_t delay;
  2055. int fixup;
  2056. struct count_info info;
  2057. int err;
  2058. if (_info == NULL)
  2059. return -EFAULT;
  2060. substream = pcm_oss_file->streams[stream];
  2061. if (substream == NULL)
  2062. return -EINVAL;
  2063. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  2064. return err;
  2065. runtime = substream->runtime;
  2066. if (runtime->oss.params || runtime->oss.prepare) {
  2067. memset(&info, 0, sizeof(info));
  2068. if (copy_to_user(_info, &info, sizeof(info)))
  2069. return -EFAULT;
  2070. return 0;
  2071. }
  2072. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2073. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
  2074. if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
  2075. err = 0;
  2076. delay = 0;
  2077. fixup = 0;
  2078. } else {
  2079. fixup = runtime->oss.buffer_used;
  2080. }
  2081. } else {
  2082. err = snd_pcm_oss_capture_position_fixup(substream, &delay);
  2083. fixup = -runtime->oss.buffer_used;
  2084. }
  2085. if (err < 0)
  2086. return err;
  2087. info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
  2088. if (atomic_read(&substream->mmap_count)) {
  2089. snd_pcm_sframes_t n;
  2090. delay = get_hw_ptr_period(runtime);
  2091. n = delay - runtime->oss.prev_hw_ptr_period;
  2092. if (n < 0)
  2093. n += runtime->boundary;
  2094. info.blocks = n / runtime->period_size;
  2095. runtime->oss.prev_hw_ptr_period = delay;
  2096. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  2097. snd_pcm_oss_simulate_fill(substream, delay);
  2098. info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
  2099. } else {
  2100. delay = snd_pcm_oss_bytes(substream, delay);
  2101. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2102. if (substream->oss.setup.buggyptr)
  2103. info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
  2104. else
  2105. info.blocks = (delay + fixup) / runtime->oss.period_bytes;
  2106. info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
  2107. } else {
  2108. delay += fixup;
  2109. info.blocks = delay / runtime->oss.period_bytes;
  2110. info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
  2111. }
  2112. }
  2113. if (copy_to_user(_info, &info, sizeof(info)))
  2114. return -EFAULT;
  2115. return 0;
  2116. }
  2117. static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
  2118. {
  2119. struct snd_pcm_substream *substream;
  2120. struct snd_pcm_runtime *runtime;
  2121. snd_pcm_sframes_t avail;
  2122. int fixup;
  2123. struct audio_buf_info info;
  2124. int err;
  2125. if (_info == NULL)
  2126. return -EFAULT;
  2127. substream = pcm_oss_file->streams[stream];
  2128. if (substream == NULL)
  2129. return -EINVAL;
  2130. runtime = substream->runtime;
  2131. if (runtime->oss.params &&
  2132. (err = snd_pcm_oss_change_params(substream, false)) < 0)
  2133. return err;
  2134. info.fragsize = runtime->oss.period_bytes;
  2135. info.fragstotal = runtime->periods;
  2136. if (runtime->oss.prepare) {
  2137. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2138. info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
  2139. info.fragments = runtime->oss.periods;
  2140. } else {
  2141. info.bytes = 0;
  2142. info.fragments = 0;
  2143. }
  2144. } else {
  2145. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2146. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
  2147. if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
  2148. avail = runtime->buffer_size;
  2149. err = 0;
  2150. fixup = 0;
  2151. } else {
  2152. avail = runtime->buffer_size - avail;
  2153. fixup = -runtime->oss.buffer_used;
  2154. }
  2155. } else {
  2156. err = snd_pcm_oss_capture_position_fixup(substream, &avail);
  2157. fixup = runtime->oss.buffer_used;
  2158. }
  2159. if (err < 0)
  2160. return err;
  2161. info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
  2162. info.fragments = info.bytes / runtime->oss.period_bytes;
  2163. }
  2164. #ifdef OSS_DEBUG
  2165. pcm_dbg(substream->pcm,
  2166. "pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n",
  2167. info.bytes, info.fragments, info.fragstotal, info.fragsize);
  2168. #endif
  2169. if (copy_to_user(_info, &info, sizeof(info)))
  2170. return -EFAULT;
  2171. return 0;
  2172. }
  2173. static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
  2174. {
  2175. // it won't be probably implemented
  2176. // pr_debug("TODO: snd_pcm_oss_get_mapbuf\n");
  2177. return -EINVAL;
  2178. }
  2179. static const char *strip_task_path(const char *path)
  2180. {
  2181. const char *ptr, *ptrl = NULL;
  2182. for (ptr = path; *ptr; ptr++) {
  2183. if (*ptr == '/')
  2184. ptrl = ptr + 1;
  2185. }
  2186. return ptrl;
  2187. }
  2188. static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
  2189. const char *task_name,
  2190. struct snd_pcm_oss_setup *rsetup)
  2191. {
  2192. struct snd_pcm_oss_setup *setup;
  2193. mutex_lock(&pcm->streams[stream].oss.setup_mutex);
  2194. do {
  2195. for (setup = pcm->streams[stream].oss.setup_list; setup;
  2196. setup = setup->next) {
  2197. if (!strcmp(setup->task_name, task_name))
  2198. goto out;
  2199. }
  2200. } while ((task_name = strip_task_path(task_name)) != NULL);
  2201. out:
  2202. if (setup)
  2203. *rsetup = *setup;
  2204. mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
  2205. }
  2206. static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
  2207. {
  2208. struct snd_pcm_runtime *runtime;
  2209. runtime = substream->runtime;
  2210. vfree(runtime->oss.buffer);
  2211. runtime->oss.buffer = NULL;
  2212. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  2213. snd_pcm_oss_plugin_clear(substream);
  2214. #endif
  2215. substream->oss.oss = 0;
  2216. }
  2217. static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
  2218. struct snd_pcm_oss_setup *setup,
  2219. int minor)
  2220. {
  2221. struct snd_pcm_runtime *runtime;
  2222. substream->oss.oss = 1;
  2223. substream->oss.setup = *setup;
  2224. if (setup->nonblock)
  2225. substream->f_flags |= O_NONBLOCK;
  2226. else if (setup->block)
  2227. substream->f_flags &= ~O_NONBLOCK;
  2228. runtime = substream->runtime;
  2229. runtime->oss.params = 1;
  2230. runtime->oss.trigger = 1;
  2231. runtime->oss.rate = 8000;
  2232. mutex_init(&runtime->oss.params_lock);
  2233. switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
  2234. case SNDRV_MINOR_OSS_PCM_8:
  2235. runtime->oss.format = AFMT_U8;
  2236. break;
  2237. case SNDRV_MINOR_OSS_PCM_16:
  2238. runtime->oss.format = AFMT_S16_LE;
  2239. break;
  2240. default:
  2241. runtime->oss.format = AFMT_MU_LAW;
  2242. }
  2243. runtime->oss.channels = 1;
  2244. runtime->oss.fragshift = 0;
  2245. runtime->oss.maxfrags = 0;
  2246. runtime->oss.subdivision = 0;
  2247. substream->pcm_release = snd_pcm_oss_release_substream;
  2248. atomic_set(&runtime->oss.rw_ref, 0);
  2249. }
  2250. static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
  2251. {
  2252. int cidx;
  2253. if (!pcm_oss_file)
  2254. return 0;
  2255. for (cidx = 0; cidx < 2; ++cidx) {
  2256. struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
  2257. if (substream)
  2258. snd_pcm_release_substream(substream);
  2259. }
  2260. kfree(pcm_oss_file);
  2261. return 0;
  2262. }
  2263. static int snd_pcm_oss_open_file(struct file *file,
  2264. struct snd_pcm *pcm,
  2265. struct snd_pcm_oss_file **rpcm_oss_file,
  2266. int minor,
  2267. struct snd_pcm_oss_setup *setup)
  2268. {
  2269. int idx, err;
  2270. struct snd_pcm_oss_file *pcm_oss_file;
  2271. struct snd_pcm_substream *substream;
  2272. fmode_t f_mode = file->f_mode;
  2273. if (rpcm_oss_file)
  2274. *rpcm_oss_file = NULL;
  2275. pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
  2276. if (pcm_oss_file == NULL)
  2277. return -ENOMEM;
  2278. if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
  2279. (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
  2280. f_mode = FMODE_WRITE;
  2281. file->f_flags &= ~O_APPEND;
  2282. for (idx = 0; idx < 2; idx++) {
  2283. if (setup[idx].disable)
  2284. continue;
  2285. if (! pcm->streams[idx].substream_count)
  2286. continue; /* no matching substream */
  2287. if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
  2288. if (! (f_mode & FMODE_WRITE))
  2289. continue;
  2290. } else {
  2291. if (! (f_mode & FMODE_READ))
  2292. continue;
  2293. }
  2294. err = snd_pcm_open_substream(pcm, idx, file, &substream);
  2295. if (err < 0) {
  2296. snd_pcm_oss_release_file(pcm_oss_file);
  2297. return err;
  2298. }
  2299. pcm_oss_file->streams[idx] = substream;
  2300. substream->file = pcm_oss_file;
  2301. snd_pcm_oss_init_substream(substream, &setup[idx], minor);
  2302. }
  2303. if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
  2304. snd_pcm_oss_release_file(pcm_oss_file);
  2305. return -EINVAL;
  2306. }
  2307. file->private_data = pcm_oss_file;
  2308. if (rpcm_oss_file)
  2309. *rpcm_oss_file = pcm_oss_file;
  2310. return 0;
  2311. }
  2312. static int snd_task_name(struct task_struct *task, char *name, size_t size)
  2313. {
  2314. unsigned int idx;
  2315. if (snd_BUG_ON(!task || !name || size < 2))
  2316. return -EINVAL;
  2317. for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
  2318. name[idx] = task->comm[idx];
  2319. name[idx] = '\0';
  2320. return 0;
  2321. }
  2322. static int snd_pcm_oss_open(struct inode *inode, struct file *file)
  2323. {
  2324. int err;
  2325. char task_name[32];
  2326. struct snd_pcm *pcm;
  2327. struct snd_pcm_oss_file *pcm_oss_file;
  2328. struct snd_pcm_oss_setup setup[2];
  2329. int nonblock;
  2330. wait_queue_t wait;
  2331. err = nonseekable_open(inode, file);
  2332. if (err < 0)
  2333. return err;
  2334. pcm = snd_lookup_oss_minor_data(iminor(inode),
  2335. SNDRV_OSS_DEVICE_TYPE_PCM);
  2336. if (pcm == NULL) {
  2337. err = -ENODEV;
  2338. goto __error1;
  2339. }
  2340. err = snd_card_file_add(pcm->card, file);
  2341. if (err < 0)
  2342. goto __error1;
  2343. if (!try_module_get(pcm->card->module)) {
  2344. err = -EFAULT;
  2345. goto __error2;
  2346. }
  2347. if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
  2348. err = -EFAULT;
  2349. goto __error;
  2350. }
  2351. memset(setup, 0, sizeof(setup));
  2352. if (file->f_mode & FMODE_WRITE)
  2353. snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  2354. task_name, &setup[0]);
  2355. if (file->f_mode & FMODE_READ)
  2356. snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
  2357. task_name, &setup[1]);
  2358. nonblock = !!(file->f_flags & O_NONBLOCK);
  2359. if (!nonblock)
  2360. nonblock = nonblock_open;
  2361. init_waitqueue_entry(&wait, current);
  2362. add_wait_queue(&pcm->open_wait, &wait);
  2363. mutex_lock(&pcm->open_mutex);
  2364. while (1) {
  2365. err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
  2366. iminor(inode), setup);
  2367. if (err >= 0)
  2368. break;
  2369. if (err == -EAGAIN) {
  2370. if (nonblock) {
  2371. err = -EBUSY;
  2372. break;
  2373. }
  2374. } else
  2375. break;
  2376. set_current_state(TASK_INTERRUPTIBLE);
  2377. mutex_unlock(&pcm->open_mutex);
  2378. schedule();
  2379. mutex_lock(&pcm->open_mutex);
  2380. if (pcm->card->shutdown) {
  2381. err = -ENODEV;
  2382. break;
  2383. }
  2384. if (signal_pending(current)) {
  2385. err = -ERESTARTSYS;
  2386. break;
  2387. }
  2388. }
  2389. remove_wait_queue(&pcm->open_wait, &wait);
  2390. mutex_unlock(&pcm->open_mutex);
  2391. if (err < 0)
  2392. goto __error;
  2393. snd_card_unref(pcm->card);
  2394. return err;
  2395. __error:
  2396. module_put(pcm->card->module);
  2397. __error2:
  2398. snd_card_file_remove(pcm->card, file);
  2399. __error1:
  2400. if (pcm)
  2401. snd_card_unref(pcm->card);
  2402. return err;
  2403. }
  2404. static int snd_pcm_oss_release(struct inode *inode, struct file *file)
  2405. {
  2406. struct snd_pcm *pcm;
  2407. struct snd_pcm_substream *substream;
  2408. struct snd_pcm_oss_file *pcm_oss_file;
  2409. pcm_oss_file = file->private_data;
  2410. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2411. if (substream == NULL)
  2412. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2413. if (snd_BUG_ON(!substream))
  2414. return -ENXIO;
  2415. pcm = substream->pcm;
  2416. if (!pcm->card->shutdown)
  2417. snd_pcm_oss_sync(pcm_oss_file);
  2418. mutex_lock(&pcm->open_mutex);
  2419. snd_pcm_oss_release_file(pcm_oss_file);
  2420. mutex_unlock(&pcm->open_mutex);
  2421. wake_up(&pcm->open_wait);
  2422. module_put(pcm->card->module);
  2423. snd_card_file_remove(pcm->card, file);
  2424. return 0;
  2425. }
  2426. static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2427. {
  2428. struct snd_pcm_oss_file *pcm_oss_file;
  2429. int __user *p = (int __user *)arg;
  2430. int res;
  2431. pcm_oss_file = file->private_data;
  2432. if (cmd == OSS_GETVERSION)
  2433. return put_user(SNDRV_OSS_VERSION, p);
  2434. if (cmd == OSS_ALSAEMULVER)
  2435. return put_user(1, p);
  2436. #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
  2437. if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
  2438. struct snd_pcm_substream *substream;
  2439. int idx;
  2440. for (idx = 0; idx < 2; ++idx) {
  2441. substream = pcm_oss_file->streams[idx];
  2442. if (substream != NULL)
  2443. break;
  2444. }
  2445. if (snd_BUG_ON(idx >= 2))
  2446. return -ENXIO;
  2447. return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
  2448. }
  2449. #endif
  2450. if (((cmd >> 8) & 0xff) != 'P')
  2451. return -EINVAL;
  2452. #ifdef OSS_DEBUG
  2453. pr_debug("pcm_oss: ioctl = 0x%x\n", cmd);
  2454. #endif
  2455. switch (cmd) {
  2456. case SNDCTL_DSP_RESET:
  2457. return snd_pcm_oss_reset(pcm_oss_file);
  2458. case SNDCTL_DSP_SYNC:
  2459. return snd_pcm_oss_sync(pcm_oss_file);
  2460. case SNDCTL_DSP_SPEED:
  2461. if (get_user(res, p))
  2462. return -EFAULT;
  2463. if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
  2464. return res;
  2465. return put_user(res, p);
  2466. case SOUND_PCM_READ_RATE:
  2467. res = snd_pcm_oss_get_rate(pcm_oss_file);
  2468. if (res < 0)
  2469. return res;
  2470. return put_user(res, p);
  2471. case SNDCTL_DSP_STEREO:
  2472. if (get_user(res, p))
  2473. return -EFAULT;
  2474. res = res > 0 ? 2 : 1;
  2475. if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
  2476. return res;
  2477. return put_user(--res, p);
  2478. case SNDCTL_DSP_GETBLKSIZE:
  2479. res = snd_pcm_oss_get_block_size(pcm_oss_file);
  2480. if (res < 0)
  2481. return res;
  2482. return put_user(res, p);
  2483. case SNDCTL_DSP_SETFMT:
  2484. if (get_user(res, p))
  2485. return -EFAULT;
  2486. res = snd_pcm_oss_set_format(pcm_oss_file, res);
  2487. if (res < 0)
  2488. return res;
  2489. return put_user(res, p);
  2490. case SOUND_PCM_READ_BITS:
  2491. res = snd_pcm_oss_get_format(pcm_oss_file);
  2492. if (res < 0)
  2493. return res;
  2494. return put_user(res, p);
  2495. case SNDCTL_DSP_CHANNELS:
  2496. if (get_user(res, p))
  2497. return -EFAULT;
  2498. res = snd_pcm_oss_set_channels(pcm_oss_file, res);
  2499. if (res < 0)
  2500. return res;
  2501. return put_user(res, p);
  2502. case SOUND_PCM_READ_CHANNELS:
  2503. res = snd_pcm_oss_get_channels(pcm_oss_file);
  2504. if (res < 0)
  2505. return res;
  2506. return put_user(res, p);
  2507. case SOUND_PCM_WRITE_FILTER:
  2508. case SOUND_PCM_READ_FILTER:
  2509. return -EIO;
  2510. case SNDCTL_DSP_POST:
  2511. return snd_pcm_oss_post(pcm_oss_file);
  2512. case SNDCTL_DSP_SUBDIVIDE:
  2513. if (get_user(res, p))
  2514. return -EFAULT;
  2515. res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
  2516. if (res < 0)
  2517. return res;
  2518. return put_user(res, p);
  2519. case SNDCTL_DSP_SETFRAGMENT:
  2520. if (get_user(res, p))
  2521. return -EFAULT;
  2522. return snd_pcm_oss_set_fragment(pcm_oss_file, res);
  2523. case SNDCTL_DSP_GETFMTS:
  2524. res = snd_pcm_oss_get_formats(pcm_oss_file);
  2525. if (res < 0)
  2526. return res;
  2527. return put_user(res, p);
  2528. case SNDCTL_DSP_GETOSPACE:
  2529. case SNDCTL_DSP_GETISPACE:
  2530. return snd_pcm_oss_get_space(pcm_oss_file,
  2531. cmd == SNDCTL_DSP_GETISPACE ?
  2532. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2533. (struct audio_buf_info __user *) arg);
  2534. case SNDCTL_DSP_NONBLOCK:
  2535. return snd_pcm_oss_nonblock(file);
  2536. case SNDCTL_DSP_GETCAPS:
  2537. res = snd_pcm_oss_get_caps(pcm_oss_file);
  2538. if (res < 0)
  2539. return res;
  2540. return put_user(res, p);
  2541. case SNDCTL_DSP_GETTRIGGER:
  2542. res = snd_pcm_oss_get_trigger(pcm_oss_file);
  2543. if (res < 0)
  2544. return res;
  2545. return put_user(res, p);
  2546. case SNDCTL_DSP_SETTRIGGER:
  2547. if (get_user(res, p))
  2548. return -EFAULT;
  2549. return snd_pcm_oss_set_trigger(pcm_oss_file, res);
  2550. case SNDCTL_DSP_GETIPTR:
  2551. case SNDCTL_DSP_GETOPTR:
  2552. return snd_pcm_oss_get_ptr(pcm_oss_file,
  2553. cmd == SNDCTL_DSP_GETIPTR ?
  2554. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2555. (struct count_info __user *) arg);
  2556. case SNDCTL_DSP_MAPINBUF:
  2557. case SNDCTL_DSP_MAPOUTBUF:
  2558. return snd_pcm_oss_get_mapbuf(pcm_oss_file,
  2559. cmd == SNDCTL_DSP_MAPINBUF ?
  2560. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2561. (struct buffmem_desc __user *) arg);
  2562. case SNDCTL_DSP_SETSYNCRO:
  2563. /* stop DMA now.. */
  2564. return 0;
  2565. case SNDCTL_DSP_SETDUPLEX:
  2566. if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
  2567. return 0;
  2568. return -EIO;
  2569. case SNDCTL_DSP_GETODELAY:
  2570. res = snd_pcm_oss_get_odelay(pcm_oss_file);
  2571. if (res < 0) {
  2572. /* it's for sure, some broken apps don't check for error codes */
  2573. put_user(0, p);
  2574. return res;
  2575. }
  2576. return put_user(res, p);
  2577. case SNDCTL_DSP_PROFILE:
  2578. return 0; /* silently ignore */
  2579. default:
  2580. pr_debug("pcm_oss: unknown command = 0x%x\n", cmd);
  2581. }
  2582. return -EINVAL;
  2583. }
  2584. #ifdef CONFIG_COMPAT
  2585. /* all compatible */
  2586. static long snd_pcm_oss_ioctl_compat(struct file *file, unsigned int cmd,
  2587. unsigned long arg)
  2588. {
  2589. return snd_pcm_oss_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  2590. }
  2591. #else
  2592. #define snd_pcm_oss_ioctl_compat NULL
  2593. #endif
  2594. static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  2595. {
  2596. struct snd_pcm_oss_file *pcm_oss_file;
  2597. struct snd_pcm_substream *substream;
  2598. pcm_oss_file = file->private_data;
  2599. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2600. if (substream == NULL)
  2601. return -ENXIO;
  2602. substream->f_flags = file->f_flags & O_NONBLOCK;
  2603. #ifndef OSS_DEBUG
  2604. return snd_pcm_oss_read1(substream, buf, count);
  2605. #else
  2606. {
  2607. ssize_t res = snd_pcm_oss_read1(substream, buf, count);
  2608. pcm_dbg(substream->pcm,
  2609. "pcm_oss: read %li bytes (returned %li bytes)\n",
  2610. (long)count, (long)res);
  2611. return res;
  2612. }
  2613. #endif
  2614. }
  2615. static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
  2616. {
  2617. struct snd_pcm_oss_file *pcm_oss_file;
  2618. struct snd_pcm_substream *substream;
  2619. long result;
  2620. pcm_oss_file = file->private_data;
  2621. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2622. if (substream == NULL)
  2623. return -ENXIO;
  2624. substream->f_flags = file->f_flags & O_NONBLOCK;
  2625. result = snd_pcm_oss_write1(substream, buf, count);
  2626. #ifdef OSS_DEBUG
  2627. pcm_dbg(substream->pcm, "pcm_oss: write %li bytes (wrote %li bytes)\n",
  2628. (long)count, (long)result);
  2629. #endif
  2630. return result;
  2631. }
  2632. static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
  2633. {
  2634. struct snd_pcm_runtime *runtime = substream->runtime;
  2635. if (atomic_read(&substream->mmap_count))
  2636. return runtime->oss.prev_hw_ptr_period !=
  2637. get_hw_ptr_period(runtime);
  2638. else
  2639. return snd_pcm_playback_avail(runtime) >=
  2640. runtime->oss.period_frames;
  2641. }
  2642. static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
  2643. {
  2644. struct snd_pcm_runtime *runtime = substream->runtime;
  2645. if (atomic_read(&substream->mmap_count))
  2646. return runtime->oss.prev_hw_ptr_period !=
  2647. get_hw_ptr_period(runtime);
  2648. else
  2649. return snd_pcm_capture_avail(runtime) >=
  2650. runtime->oss.period_frames;
  2651. }
  2652. static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
  2653. {
  2654. struct snd_pcm_oss_file *pcm_oss_file;
  2655. unsigned int mask;
  2656. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  2657. pcm_oss_file = file->private_data;
  2658. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2659. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2660. mask = 0;
  2661. if (psubstream != NULL) {
  2662. struct snd_pcm_runtime *runtime = psubstream->runtime;
  2663. poll_wait(file, &runtime->sleep, wait);
  2664. snd_pcm_stream_lock_irq(psubstream);
  2665. if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
  2666. (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
  2667. snd_pcm_oss_playback_ready(psubstream)))
  2668. mask |= POLLOUT | POLLWRNORM;
  2669. snd_pcm_stream_unlock_irq(psubstream);
  2670. }
  2671. if (csubstream != NULL) {
  2672. struct snd_pcm_runtime *runtime = csubstream->runtime;
  2673. snd_pcm_state_t ostate;
  2674. poll_wait(file, &runtime->sleep, wait);
  2675. snd_pcm_stream_lock_irq(csubstream);
  2676. if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
  2677. snd_pcm_oss_capture_ready(csubstream))
  2678. mask |= POLLIN | POLLRDNORM;
  2679. snd_pcm_stream_unlock_irq(csubstream);
  2680. if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
  2681. struct snd_pcm_oss_file ofile;
  2682. memset(&ofile, 0, sizeof(ofile));
  2683. ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2684. runtime->oss.trigger = 0;
  2685. snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
  2686. }
  2687. }
  2688. return mask;
  2689. }
  2690. static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
  2691. {
  2692. struct snd_pcm_oss_file *pcm_oss_file;
  2693. struct snd_pcm_substream *substream = NULL;
  2694. struct snd_pcm_runtime *runtime;
  2695. int err;
  2696. #ifdef OSS_DEBUG
  2697. pr_debug("pcm_oss: mmap begin\n");
  2698. #endif
  2699. pcm_oss_file = file->private_data;
  2700. switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
  2701. case VM_READ | VM_WRITE:
  2702. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2703. if (substream)
  2704. break;
  2705. /* Fall through */
  2706. case VM_READ:
  2707. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2708. break;
  2709. case VM_WRITE:
  2710. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2711. break;
  2712. default:
  2713. return -EINVAL;
  2714. }
  2715. /* set VM_READ access as well to fix memset() routines that do
  2716. reads before writes (to improve performance) */
  2717. area->vm_flags |= VM_READ;
  2718. if (substream == NULL)
  2719. return -ENXIO;
  2720. runtime = substream->runtime;
  2721. if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
  2722. return -EIO;
  2723. if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
  2724. runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
  2725. else
  2726. return -EIO;
  2727. if (runtime->oss.params) {
  2728. /* use mutex_trylock() for params_lock for avoiding a deadlock
  2729. * between mmap_sem and params_lock taken by
  2730. * copy_from/to_user() in snd_pcm_oss_write/read()
  2731. */
  2732. err = snd_pcm_oss_change_params(substream, true);
  2733. if (err < 0)
  2734. return err;
  2735. }
  2736. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  2737. if (runtime->oss.plugin_first != NULL)
  2738. return -EIO;
  2739. #endif
  2740. if (area->vm_pgoff != 0)
  2741. return -EINVAL;
  2742. err = snd_pcm_mmap_data(substream, file, area);
  2743. if (err < 0)
  2744. return err;
  2745. runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
  2746. runtime->silence_threshold = 0;
  2747. runtime->silence_size = 0;
  2748. #ifdef OSS_DEBUG
  2749. pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n",
  2750. runtime->oss.mmap_bytes);
  2751. #endif
  2752. /* In mmap mode we never stop */
  2753. runtime->stop_threshold = runtime->boundary;
  2754. return 0;
  2755. }
  2756. #ifdef CONFIG_SND_VERBOSE_PROCFS
  2757. /*
  2758. * /proc interface
  2759. */
  2760. static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
  2761. struct snd_info_buffer *buffer)
  2762. {
  2763. struct snd_pcm_str *pstr = entry->private_data;
  2764. struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
  2765. mutex_lock(&pstr->oss.setup_mutex);
  2766. while (setup) {
  2767. snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
  2768. setup->task_name,
  2769. setup->periods,
  2770. setup->period_size,
  2771. setup->disable ? " disable" : "",
  2772. setup->direct ? " direct" : "",
  2773. setup->block ? " block" : "",
  2774. setup->nonblock ? " non-block" : "",
  2775. setup->partialfrag ? " partial-frag" : "",
  2776. setup->nosilence ? " no-silence" : "");
  2777. setup = setup->next;
  2778. }
  2779. mutex_unlock(&pstr->oss.setup_mutex);
  2780. }
  2781. static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
  2782. {
  2783. struct snd_pcm_oss_setup *setup, *setupn;
  2784. for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
  2785. setup; setup = setupn) {
  2786. setupn = setup->next;
  2787. kfree(setup->task_name);
  2788. kfree(setup);
  2789. }
  2790. pstr->oss.setup_list = NULL;
  2791. }
  2792. static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
  2793. struct snd_info_buffer *buffer)
  2794. {
  2795. struct snd_pcm_str *pstr = entry->private_data;
  2796. char line[128], str[32], task_name[32];
  2797. const char *ptr;
  2798. int idx1;
  2799. struct snd_pcm_oss_setup *setup, *setup1, template;
  2800. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  2801. mutex_lock(&pstr->oss.setup_mutex);
  2802. memset(&template, 0, sizeof(template));
  2803. ptr = snd_info_get_str(task_name, line, sizeof(task_name));
  2804. if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
  2805. snd_pcm_oss_proc_free_setup_list(pstr);
  2806. mutex_unlock(&pstr->oss.setup_mutex);
  2807. continue;
  2808. }
  2809. for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
  2810. if (!strcmp(setup->task_name, task_name)) {
  2811. template = *setup;
  2812. break;
  2813. }
  2814. }
  2815. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2816. template.periods = simple_strtoul(str, NULL, 10);
  2817. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2818. template.period_size = simple_strtoul(str, NULL, 10);
  2819. for (idx1 = 31; idx1 >= 0; idx1--)
  2820. if (template.period_size & (1 << idx1))
  2821. break;
  2822. for (idx1--; idx1 >= 0; idx1--)
  2823. template.period_size &= ~(1 << idx1);
  2824. do {
  2825. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2826. if (!strcmp(str, "disable")) {
  2827. template.disable = 1;
  2828. } else if (!strcmp(str, "direct")) {
  2829. template.direct = 1;
  2830. } else if (!strcmp(str, "block")) {
  2831. template.block = 1;
  2832. } else if (!strcmp(str, "non-block")) {
  2833. template.nonblock = 1;
  2834. } else if (!strcmp(str, "partial-frag")) {
  2835. template.partialfrag = 1;
  2836. } else if (!strcmp(str, "no-silence")) {
  2837. template.nosilence = 1;
  2838. } else if (!strcmp(str, "buggy-ptr")) {
  2839. template.buggyptr = 1;
  2840. }
  2841. } while (*str);
  2842. if (setup == NULL) {
  2843. setup = kmalloc(sizeof(*setup), GFP_KERNEL);
  2844. if (! setup) {
  2845. buffer->error = -ENOMEM;
  2846. mutex_unlock(&pstr->oss.setup_mutex);
  2847. return;
  2848. }
  2849. if (pstr->oss.setup_list == NULL)
  2850. pstr->oss.setup_list = setup;
  2851. else {
  2852. for (setup1 = pstr->oss.setup_list;
  2853. setup1->next; setup1 = setup1->next);
  2854. setup1->next = setup;
  2855. }
  2856. template.task_name = kstrdup(task_name, GFP_KERNEL);
  2857. if (! template.task_name) {
  2858. kfree(setup);
  2859. buffer->error = -ENOMEM;
  2860. mutex_unlock(&pstr->oss.setup_mutex);
  2861. return;
  2862. }
  2863. }
  2864. *setup = template;
  2865. mutex_unlock(&pstr->oss.setup_mutex);
  2866. }
  2867. }
  2868. static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
  2869. {
  2870. int stream;
  2871. for (stream = 0; stream < 2; ++stream) {
  2872. struct snd_info_entry *entry;
  2873. struct snd_pcm_str *pstr = &pcm->streams[stream];
  2874. if (pstr->substream_count == 0)
  2875. continue;
  2876. if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
  2877. entry->content = SNDRV_INFO_CONTENT_TEXT;
  2878. entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
  2879. entry->c.text.read = snd_pcm_oss_proc_read;
  2880. entry->c.text.write = snd_pcm_oss_proc_write;
  2881. entry->private_data = pstr;
  2882. if (snd_info_register(entry) < 0) {
  2883. snd_info_free_entry(entry);
  2884. entry = NULL;
  2885. }
  2886. }
  2887. pstr->oss.proc_entry = entry;
  2888. }
  2889. }
  2890. static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
  2891. {
  2892. int stream;
  2893. for (stream = 0; stream < 2; ++stream) {
  2894. struct snd_pcm_str *pstr = &pcm->streams[stream];
  2895. snd_info_free_entry(pstr->oss.proc_entry);
  2896. pstr->oss.proc_entry = NULL;
  2897. snd_pcm_oss_proc_free_setup_list(pstr);
  2898. }
  2899. }
  2900. #else /* !CONFIG_SND_VERBOSE_PROCFS */
  2901. #define snd_pcm_oss_proc_init(pcm)
  2902. #define snd_pcm_oss_proc_done(pcm)
  2903. #endif /* CONFIG_SND_VERBOSE_PROCFS */
  2904. /*
  2905. * ENTRY functions
  2906. */
  2907. static const struct file_operations snd_pcm_oss_f_reg =
  2908. {
  2909. .owner = THIS_MODULE,
  2910. .read = snd_pcm_oss_read,
  2911. .write = snd_pcm_oss_write,
  2912. .open = snd_pcm_oss_open,
  2913. .release = snd_pcm_oss_release,
  2914. .llseek = no_llseek,
  2915. .poll = snd_pcm_oss_poll,
  2916. .unlocked_ioctl = snd_pcm_oss_ioctl,
  2917. .compat_ioctl = snd_pcm_oss_ioctl_compat,
  2918. .mmap = snd_pcm_oss_mmap,
  2919. };
  2920. static void register_oss_dsp(struct snd_pcm *pcm, int index)
  2921. {
  2922. if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2923. pcm->card, index, &snd_pcm_oss_f_reg,
  2924. pcm) < 0) {
  2925. pcm_err(pcm, "unable to register OSS PCM device %i:%i\n",
  2926. pcm->card->number, pcm->device);
  2927. }
  2928. }
  2929. static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
  2930. {
  2931. pcm->oss.reg = 0;
  2932. if (dsp_map[pcm->card->number] == (int)pcm->device) {
  2933. char name[128];
  2934. int duplex;
  2935. register_oss_dsp(pcm, 0);
  2936. duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
  2937. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
  2938. !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
  2939. sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
  2940. #ifdef SNDRV_OSS_INFO_DEV_AUDIO
  2941. snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
  2942. pcm->card->number,
  2943. name);
  2944. #endif
  2945. pcm->oss.reg++;
  2946. pcm->oss.reg_mask |= 1;
  2947. }
  2948. if (adsp_map[pcm->card->number] == (int)pcm->device) {
  2949. register_oss_dsp(pcm, 1);
  2950. pcm->oss.reg++;
  2951. pcm->oss.reg_mask |= 2;
  2952. }
  2953. if (pcm->oss.reg)
  2954. snd_pcm_oss_proc_init(pcm);
  2955. return 0;
  2956. }
  2957. static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
  2958. {
  2959. if (pcm->oss.reg) {
  2960. if (pcm->oss.reg_mask & 1) {
  2961. pcm->oss.reg_mask &= ~1;
  2962. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2963. pcm->card, 0);
  2964. }
  2965. if (pcm->oss.reg_mask & 2) {
  2966. pcm->oss.reg_mask &= ~2;
  2967. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2968. pcm->card, 1);
  2969. }
  2970. if (dsp_map[pcm->card->number] == (int)pcm->device) {
  2971. #ifdef SNDRV_OSS_INFO_DEV_AUDIO
  2972. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
  2973. #endif
  2974. }
  2975. pcm->oss.reg = 0;
  2976. }
  2977. return 0;
  2978. }
  2979. static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
  2980. {
  2981. snd_pcm_oss_disconnect_minor(pcm);
  2982. snd_pcm_oss_proc_done(pcm);
  2983. return 0;
  2984. }
  2985. static struct snd_pcm_notify snd_pcm_oss_notify =
  2986. {
  2987. .n_register = snd_pcm_oss_register_minor,
  2988. .n_disconnect = snd_pcm_oss_disconnect_minor,
  2989. .n_unregister = snd_pcm_oss_unregister_minor,
  2990. };
  2991. static int __init alsa_pcm_oss_init(void)
  2992. {
  2993. int i;
  2994. int err;
  2995. /* check device map table */
  2996. for (i = 0; i < SNDRV_CARDS; i++) {
  2997. if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
  2998. pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n",
  2999. i, dsp_map[i]);
  3000. dsp_map[i] = 0;
  3001. }
  3002. if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
  3003. pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n",
  3004. i, adsp_map[i]);
  3005. adsp_map[i] = 1;
  3006. }
  3007. }
  3008. if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
  3009. return err;
  3010. return 0;
  3011. }
  3012. static void __exit alsa_pcm_oss_exit(void)
  3013. {
  3014. snd_pcm_notify(&snd_pcm_oss_notify, 1);
  3015. }
  3016. module_init(alsa_pcm_oss_init)
  3017. module_exit(alsa_pcm_oss_exit)