pcm_oss.c 87 KB

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