pcm_oss.c 82 KB

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