pcm_oss.c 88 KB

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