pcm_oss.c 84 KB

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