pcm_oss.c 87 KB

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