pcm_oss.c 84 KB

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