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];
  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(&substream->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(&substream->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(&substream->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(&substream->mmap_count) ||
  883. substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  884. sw_params->stop_threshold = runtime->boundary;
  885. else
  886. sw_params->stop_threshold = runtime->buffer_size;
  887. sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
  888. sw_params->period_step = 1;
  889. sw_params->sleep_min = 0;
  890. sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  891. 1 : runtime->period_size;
  892. sw_params->xfer_align = 1;
  893. if (atomic_read(&substream->mmap_count) ||
  894. substream->oss.setup.nosilence) {
  895. sw_params->silence_threshold = 0;
  896. sw_params->silence_size = 0;
  897. } else {
  898. snd_pcm_uframes_t frames;
  899. frames = runtime->period_size + 16;
  900. if (frames > runtime->buffer_size)
  901. frames = runtime->buffer_size;
  902. sw_params->silence_threshold = frames;
  903. sw_params->silence_size = frames;
  904. }
  905. if ((err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params)) < 0) {
  906. snd_printd("SW_PARAMS failed: %i\n", err);
  907. goto failure;
  908. }
  909. runtime->oss.periods = params_periods(sparams);
  910. oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
  911. snd_assert(oss_period_size >= 0, err = -EINVAL; goto failure);
  912. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  913. if (runtime->oss.plugin_first) {
  914. err = snd_pcm_plug_alloc(substream, oss_period_size);
  915. if (err < 0)
  916. goto failure;
  917. }
  918. #endif
  919. oss_period_size *= oss_frame_size;
  920. oss_buffer_size = oss_period_size * runtime->oss.periods;
  921. snd_assert(oss_buffer_size >= 0, err = -EINVAL; goto failure);
  922. runtime->oss.period_bytes = oss_period_size;
  923. runtime->oss.buffer_bytes = oss_buffer_size;
  924. pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
  925. runtime->oss.period_bytes,
  926. runtime->oss.buffer_bytes);
  927. pdprintf("slave: period_size = %i, buffer_size = %i\n",
  928. params_period_size(sparams),
  929. params_buffer_size(sparams));
  930. runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
  931. runtime->oss.channels = params_channels(params);
  932. runtime->oss.rate = params_rate(params);
  933. runtime->oss.params = 0;
  934. runtime->oss.prepare = 1;
  935. vfree(runtime->oss.buffer);
  936. runtime->oss.buffer = vmalloc(runtime->oss.period_bytes);
  937. runtime->oss.buffer_used = 0;
  938. if (runtime->dma_area)
  939. snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
  940. runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
  941. err = 0;
  942. failure:
  943. kfree(sw_params);
  944. kfree(params);
  945. kfree(sparams);
  946. return err;
  947. }
  948. static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
  949. {
  950. int idx, err;
  951. struct snd_pcm_substream *asubstream = NULL, *substream;
  952. for (idx = 0; idx < 2; idx++) {
  953. substream = pcm_oss_file->streams[idx];
  954. if (substream == NULL)
  955. continue;
  956. if (asubstream == NULL)
  957. asubstream = substream;
  958. if (substream->runtime->oss.params) {
  959. err = snd_pcm_oss_change_params(substream);
  960. if (err < 0)
  961. return err;
  962. }
  963. }
  964. snd_assert(asubstream != NULL, return -EIO);
  965. if (r_substream)
  966. *r_substream = asubstream;
  967. return 0;
  968. }
  969. static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
  970. {
  971. int err;
  972. struct snd_pcm_runtime *runtime = substream->runtime;
  973. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
  974. if (err < 0) {
  975. snd_printd("snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
  976. return err;
  977. }
  978. runtime->oss.prepare = 0;
  979. runtime->oss.prev_hw_ptr_interrupt = 0;
  980. runtime->oss.period_ptr = 0;
  981. runtime->oss.buffer_used = 0;
  982. return 0;
  983. }
  984. static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
  985. {
  986. struct snd_pcm_runtime *runtime;
  987. int err;
  988. if (substream == NULL)
  989. return 0;
  990. runtime = substream->runtime;
  991. if (runtime->oss.params) {
  992. err = snd_pcm_oss_change_params(substream);
  993. if (err < 0)
  994. return err;
  995. }
  996. if (runtime->oss.prepare) {
  997. err = snd_pcm_oss_prepare(substream);
  998. if (err < 0)
  999. return err;
  1000. }
  1001. return 0;
  1002. }
  1003. static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
  1004. {
  1005. struct snd_pcm_runtime *runtime;
  1006. snd_pcm_uframes_t frames;
  1007. int err = 0;
  1008. while (1) {
  1009. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
  1010. if (err < 0)
  1011. break;
  1012. runtime = substream->runtime;
  1013. if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
  1014. break;
  1015. /* in case of overrun, skip whole periods like OSS/Linux driver does */
  1016. /* until avail(delay) <= buffer_size */
  1017. frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
  1018. frames /= runtime->period_size;
  1019. frames *= runtime->period_size;
  1020. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
  1021. if (err < 0)
  1022. break;
  1023. }
  1024. return err;
  1025. }
  1026. snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
  1027. {
  1028. struct snd_pcm_runtime *runtime = substream->runtime;
  1029. int ret;
  1030. while (1) {
  1031. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1032. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1033. #ifdef OSS_DEBUG
  1034. if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
  1035. printk("pcm_oss: write: recovering from XRUN\n");
  1036. else
  1037. printk("pcm_oss: write: recovering from SUSPEND\n");
  1038. #endif
  1039. ret = snd_pcm_oss_prepare(substream);
  1040. if (ret < 0)
  1041. break;
  1042. }
  1043. if (in_kernel) {
  1044. mm_segment_t fs;
  1045. fs = snd_enter_user();
  1046. ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
  1047. snd_leave_user(fs);
  1048. } else {
  1049. ret = snd_pcm_lib_write(substream, (void __user *)ptr, frames);
  1050. }
  1051. if (ret != -EPIPE && ret != -ESTRPIPE)
  1052. break;
  1053. /* test, if we can't store new data, because the stream */
  1054. /* has not been started */
  1055. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
  1056. return -EAGAIN;
  1057. }
  1058. return ret;
  1059. }
  1060. snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
  1061. {
  1062. struct snd_pcm_runtime *runtime = substream->runtime;
  1063. snd_pcm_sframes_t delay;
  1064. int ret;
  1065. while (1) {
  1066. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1067. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1068. #ifdef OSS_DEBUG
  1069. if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
  1070. printk("pcm_oss: read: recovering from XRUN\n");
  1071. else
  1072. printk("pcm_oss: read: recovering from SUSPEND\n");
  1073. #endif
  1074. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1075. if (ret < 0)
  1076. break;
  1077. } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
  1078. ret = snd_pcm_oss_prepare(substream);
  1079. if (ret < 0)
  1080. break;
  1081. }
  1082. ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
  1083. if (ret < 0)
  1084. break;
  1085. if (in_kernel) {
  1086. mm_segment_t fs;
  1087. fs = snd_enter_user();
  1088. ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
  1089. snd_leave_user(fs);
  1090. } else {
  1091. ret = snd_pcm_lib_read(substream, (void __user *)ptr, frames);
  1092. }
  1093. if (ret == -EPIPE) {
  1094. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  1095. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1096. if (ret < 0)
  1097. break;
  1098. }
  1099. continue;
  1100. }
  1101. if (ret != -ESTRPIPE)
  1102. break;
  1103. }
  1104. return ret;
  1105. }
  1106. snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
  1107. {
  1108. struct snd_pcm_runtime *runtime = substream->runtime;
  1109. int ret;
  1110. while (1) {
  1111. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1112. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1113. #ifdef OSS_DEBUG
  1114. if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
  1115. printk("pcm_oss: writev: recovering from XRUN\n");
  1116. else
  1117. printk("pcm_oss: writev: recovering from SUSPEND\n");
  1118. #endif
  1119. ret = snd_pcm_oss_prepare(substream);
  1120. if (ret < 0)
  1121. break;
  1122. }
  1123. if (in_kernel) {
  1124. mm_segment_t fs;
  1125. fs = snd_enter_user();
  1126. ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
  1127. snd_leave_user(fs);
  1128. } else {
  1129. ret = snd_pcm_lib_writev(substream, (void __user **)bufs, frames);
  1130. }
  1131. if (ret != -EPIPE && ret != -ESTRPIPE)
  1132. break;
  1133. /* test, if we can't store new data, because the stream */
  1134. /* has not been started */
  1135. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED)
  1136. return -EAGAIN;
  1137. }
  1138. return ret;
  1139. }
  1140. snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames, int in_kernel)
  1141. {
  1142. struct snd_pcm_runtime *runtime = substream->runtime;
  1143. int ret;
  1144. while (1) {
  1145. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  1146. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1147. #ifdef OSS_DEBUG
  1148. if (runtime->status->state == SNDRV_PCM_STATE_XRUN)
  1149. printk("pcm_oss: readv: recovering from XRUN\n");
  1150. else
  1151. printk("pcm_oss: readv: recovering from SUSPEND\n");
  1152. #endif
  1153. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1154. if (ret < 0)
  1155. break;
  1156. } else if (runtime->status->state == SNDRV_PCM_STATE_SETUP) {
  1157. ret = snd_pcm_oss_prepare(substream);
  1158. if (ret < 0)
  1159. break;
  1160. }
  1161. if (in_kernel) {
  1162. mm_segment_t fs;
  1163. fs = snd_enter_user();
  1164. ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
  1165. snd_leave_user(fs);
  1166. } else {
  1167. ret = snd_pcm_lib_readv(substream, (void __user **)bufs, frames);
  1168. }
  1169. if (ret != -EPIPE && ret != -ESTRPIPE)
  1170. break;
  1171. }
  1172. return ret;
  1173. }
  1174. static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
  1175. {
  1176. struct snd_pcm_runtime *runtime = substream->runtime;
  1177. snd_pcm_sframes_t frames, frames1;
  1178. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  1179. if (runtime->oss.plugin_first) {
  1180. struct snd_pcm_plugin_channel *channels;
  1181. size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
  1182. if (!in_kernel) {
  1183. if (copy_from_user(runtime->oss.buffer, (const char __user *)buf, bytes))
  1184. return -EFAULT;
  1185. buf = runtime->oss.buffer;
  1186. }
  1187. frames = bytes / oss_frame_bytes;
  1188. frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
  1189. if (frames1 < 0)
  1190. return frames1;
  1191. frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
  1192. if (frames1 <= 0)
  1193. return frames1;
  1194. bytes = frames1 * oss_frame_bytes;
  1195. } else
  1196. #endif
  1197. {
  1198. frames = bytes_to_frames(runtime, bytes);
  1199. frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
  1200. if (frames1 <= 0)
  1201. return frames1;
  1202. bytes = frames_to_bytes(runtime, frames1);
  1203. }
  1204. return bytes;
  1205. }
  1206. static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
  1207. {
  1208. size_t xfer = 0;
  1209. ssize_t tmp;
  1210. struct snd_pcm_runtime *runtime = substream->runtime;
  1211. if (atomic_read(&substream->mmap_count))
  1212. return -ENXIO;
  1213. if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
  1214. return tmp;
  1215. while (bytes > 0) {
  1216. if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
  1217. tmp = bytes;
  1218. if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
  1219. tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
  1220. if (tmp > 0) {
  1221. if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
  1222. return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
  1223. }
  1224. runtime->oss.buffer_used += tmp;
  1225. buf += tmp;
  1226. bytes -= tmp;
  1227. xfer += tmp;
  1228. if (substream->oss.setup.partialfrag ||
  1229. runtime->oss.buffer_used == runtime->oss.period_bytes) {
  1230. tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
  1231. runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
  1232. if (tmp <= 0)
  1233. return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
  1234. runtime->oss.bytes += tmp;
  1235. runtime->oss.period_ptr += tmp;
  1236. runtime->oss.period_ptr %= runtime->oss.period_bytes;
  1237. if (runtime->oss.period_ptr == 0 ||
  1238. runtime->oss.period_ptr == runtime->oss.buffer_used)
  1239. runtime->oss.buffer_used = 0;
  1240. else if ((substream->f_flags & O_NONBLOCK) != 0)
  1241. return xfer > 0 ? xfer : -EAGAIN;
  1242. }
  1243. } else {
  1244. tmp = snd_pcm_oss_write2(substream,
  1245. (const char __force *)buf,
  1246. runtime->oss.period_bytes, 0);
  1247. if (tmp <= 0)
  1248. return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
  1249. runtime->oss.bytes += tmp;
  1250. buf += tmp;
  1251. bytes -= tmp;
  1252. xfer += tmp;
  1253. if ((substream->f_flags & O_NONBLOCK) != 0 &&
  1254. tmp != runtime->oss.period_bytes)
  1255. break;
  1256. }
  1257. }
  1258. return xfer;
  1259. }
  1260. static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
  1261. {
  1262. struct snd_pcm_runtime *runtime = substream->runtime;
  1263. snd_pcm_sframes_t frames, frames1;
  1264. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  1265. char __user *final_dst = (char __user *)buf;
  1266. if (runtime->oss.plugin_first) {
  1267. struct snd_pcm_plugin_channel *channels;
  1268. size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
  1269. if (!in_kernel)
  1270. buf = runtime->oss.buffer;
  1271. frames = bytes / oss_frame_bytes;
  1272. frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
  1273. if (frames1 < 0)
  1274. return frames1;
  1275. frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
  1276. if (frames1 <= 0)
  1277. return frames1;
  1278. bytes = frames1 * oss_frame_bytes;
  1279. if (!in_kernel && copy_to_user(final_dst, buf, bytes))
  1280. return -EFAULT;
  1281. } else
  1282. #endif
  1283. {
  1284. frames = bytes_to_frames(runtime, bytes);
  1285. frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
  1286. if (frames1 <= 0)
  1287. return frames1;
  1288. bytes = frames_to_bytes(runtime, frames1);
  1289. }
  1290. return bytes;
  1291. }
  1292. static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
  1293. {
  1294. size_t xfer = 0;
  1295. ssize_t tmp;
  1296. struct snd_pcm_runtime *runtime = substream->runtime;
  1297. if (atomic_read(&substream->mmap_count))
  1298. return -ENXIO;
  1299. if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
  1300. return tmp;
  1301. while (bytes > 0) {
  1302. if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
  1303. if (runtime->oss.buffer_used == 0) {
  1304. tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
  1305. if (tmp <= 0)
  1306. return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
  1307. runtime->oss.bytes += tmp;
  1308. runtime->oss.period_ptr = tmp;
  1309. runtime->oss.buffer_used = tmp;
  1310. }
  1311. tmp = bytes;
  1312. if ((size_t) tmp > runtime->oss.buffer_used)
  1313. tmp = runtime->oss.buffer_used;
  1314. if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
  1315. return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
  1316. buf += tmp;
  1317. bytes -= tmp;
  1318. xfer += tmp;
  1319. runtime->oss.buffer_used -= tmp;
  1320. } else {
  1321. tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
  1322. runtime->oss.period_bytes, 0);
  1323. if (tmp <= 0)
  1324. return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
  1325. runtime->oss.bytes += tmp;
  1326. buf += tmp;
  1327. bytes -= tmp;
  1328. xfer += tmp;
  1329. }
  1330. }
  1331. return xfer;
  1332. }
  1333. static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
  1334. {
  1335. struct snd_pcm_substream *substream;
  1336. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1337. if (substream != NULL) {
  1338. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1339. substream->runtime->oss.prepare = 1;
  1340. }
  1341. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1342. if (substream != NULL) {
  1343. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1344. substream->runtime->oss.prepare = 1;
  1345. }
  1346. return 0;
  1347. }
  1348. static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
  1349. {
  1350. struct snd_pcm_substream *substream;
  1351. int err;
  1352. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1353. if (substream != NULL) {
  1354. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1355. return err;
  1356. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
  1357. }
  1358. /* note: all errors from the start action are ignored */
  1359. /* OSS apps do not know, how to handle them */
  1360. return 0;
  1361. }
  1362. static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
  1363. {
  1364. struct snd_pcm_runtime *runtime;
  1365. ssize_t result = 0;
  1366. long res;
  1367. wait_queue_t wait;
  1368. runtime = substream->runtime;
  1369. init_waitqueue_entry(&wait, current);
  1370. add_wait_queue(&runtime->sleep, &wait);
  1371. #ifdef OSS_DEBUG
  1372. printk("sync1: size = %li\n", size);
  1373. #endif
  1374. while (1) {
  1375. result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
  1376. if (result > 0) {
  1377. runtime->oss.buffer_used = 0;
  1378. result = 0;
  1379. break;
  1380. }
  1381. if (result != 0 && result != -EAGAIN)
  1382. break;
  1383. result = 0;
  1384. set_current_state(TASK_INTERRUPTIBLE);
  1385. snd_pcm_stream_lock_irq(substream);
  1386. res = runtime->status->state;
  1387. snd_pcm_stream_unlock_irq(substream);
  1388. if (res != SNDRV_PCM_STATE_RUNNING) {
  1389. set_current_state(TASK_RUNNING);
  1390. break;
  1391. }
  1392. res = schedule_timeout(10 * HZ);
  1393. if (signal_pending(current)) {
  1394. result = -ERESTARTSYS;
  1395. break;
  1396. }
  1397. if (res == 0) {
  1398. snd_printk(KERN_ERR "OSS sync error - DMA timeout\n");
  1399. result = -EIO;
  1400. break;
  1401. }
  1402. }
  1403. remove_wait_queue(&runtime->sleep, &wait);
  1404. return result;
  1405. }
  1406. static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
  1407. {
  1408. int err = 0;
  1409. unsigned int saved_f_flags;
  1410. struct snd_pcm_substream *substream;
  1411. struct snd_pcm_runtime *runtime;
  1412. snd_pcm_format_t format;
  1413. unsigned long width;
  1414. size_t size;
  1415. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1416. if (substream != NULL) {
  1417. runtime = substream->runtime;
  1418. if (atomic_read(&substream->mmap_count))
  1419. goto __direct;
  1420. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1421. return err;
  1422. format = snd_pcm_oss_format_from(runtime->oss.format);
  1423. width = snd_pcm_format_physical_width(format);
  1424. if (runtime->oss.buffer_used > 0) {
  1425. #ifdef OSS_DEBUG
  1426. printk("sync: buffer_used\n");
  1427. #endif
  1428. size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
  1429. snd_pcm_format_set_silence(format,
  1430. runtime->oss.buffer + runtime->oss.buffer_used,
  1431. size);
  1432. err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
  1433. if (err < 0)
  1434. return err;
  1435. } else if (runtime->oss.period_ptr > 0) {
  1436. #ifdef OSS_DEBUG
  1437. printk("sync: period_ptr\n");
  1438. #endif
  1439. size = runtime->oss.period_bytes - runtime->oss.period_ptr;
  1440. snd_pcm_format_set_silence(format,
  1441. runtime->oss.buffer,
  1442. size * 8 / width);
  1443. err = snd_pcm_oss_sync1(substream, size);
  1444. if (err < 0)
  1445. return err;
  1446. }
  1447. /*
  1448. * The ALSA's period might be a bit large than OSS one.
  1449. * Fill the remain portion of ALSA period with zeros.
  1450. */
  1451. size = runtime->control->appl_ptr % runtime->period_size;
  1452. if (size > 0) {
  1453. size = runtime->period_size - size;
  1454. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
  1455. size = (runtime->frame_bits * size) / 8;
  1456. while (size > 0) {
  1457. mm_segment_t fs;
  1458. size_t size1 = size < runtime->oss.period_bytes ? size : runtime->oss.period_bytes;
  1459. size -= size1;
  1460. size1 *= 8;
  1461. size1 /= runtime->sample_bits;
  1462. snd_pcm_format_set_silence(runtime->format,
  1463. runtime->oss.buffer,
  1464. size1);
  1465. fs = snd_enter_user();
  1466. snd_pcm_lib_write(substream, (void __user *)runtime->oss.buffer, size1);
  1467. snd_leave_user(fs);
  1468. }
  1469. } else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
  1470. void __user *buffers[runtime->channels];
  1471. memset(buffers, 0, runtime->channels * sizeof(void *));
  1472. snd_pcm_lib_writev(substream, buffers, size);
  1473. }
  1474. }
  1475. /*
  1476. * finish sync: drain the buffer
  1477. */
  1478. __direct:
  1479. saved_f_flags = substream->f_flags;
  1480. substream->f_flags &= ~O_NONBLOCK;
  1481. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1482. substream->f_flags = saved_f_flags;
  1483. if (err < 0)
  1484. return err;
  1485. runtime->oss.prepare = 1;
  1486. }
  1487. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1488. if (substream != NULL) {
  1489. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1490. return err;
  1491. runtime = substream->runtime;
  1492. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1493. if (err < 0)
  1494. return err;
  1495. runtime->oss.buffer_used = 0;
  1496. runtime->oss.prepare = 1;
  1497. }
  1498. return 0;
  1499. }
  1500. static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
  1501. {
  1502. int idx;
  1503. for (idx = 1; idx >= 0; --idx) {
  1504. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1505. struct snd_pcm_runtime *runtime;
  1506. if (substream == NULL)
  1507. continue;
  1508. runtime = substream->runtime;
  1509. if (rate < 1000)
  1510. rate = 1000;
  1511. else if (rate > 192000)
  1512. rate = 192000;
  1513. if (runtime->oss.rate != rate) {
  1514. runtime->oss.params = 1;
  1515. runtime->oss.rate = rate;
  1516. }
  1517. }
  1518. return snd_pcm_oss_get_rate(pcm_oss_file);
  1519. }
  1520. static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
  1521. {
  1522. struct snd_pcm_substream *substream;
  1523. int err;
  1524. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1525. return err;
  1526. return substream->runtime->oss.rate;
  1527. }
  1528. static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
  1529. {
  1530. int idx;
  1531. if (channels < 1)
  1532. channels = 1;
  1533. if (channels > 128)
  1534. return -EINVAL;
  1535. for (idx = 1; idx >= 0; --idx) {
  1536. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1537. struct snd_pcm_runtime *runtime;
  1538. if (substream == NULL)
  1539. continue;
  1540. runtime = substream->runtime;
  1541. if (runtime->oss.channels != channels) {
  1542. runtime->oss.params = 1;
  1543. runtime->oss.channels = channels;
  1544. }
  1545. }
  1546. return snd_pcm_oss_get_channels(pcm_oss_file);
  1547. }
  1548. static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
  1549. {
  1550. struct snd_pcm_substream *substream;
  1551. int err;
  1552. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1553. return err;
  1554. return substream->runtime->oss.channels;
  1555. }
  1556. static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
  1557. {
  1558. struct snd_pcm_substream *substream;
  1559. int err;
  1560. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1561. return err;
  1562. return substream->runtime->oss.period_bytes;
  1563. }
  1564. static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
  1565. {
  1566. struct snd_pcm_substream *substream;
  1567. int err;
  1568. int direct;
  1569. struct snd_pcm_hw_params *params;
  1570. unsigned int formats = 0;
  1571. struct snd_mask format_mask;
  1572. int fmt;
  1573. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1574. return err;
  1575. if (atomic_read(&substream->mmap_count))
  1576. direct = 1;
  1577. else
  1578. direct = substream->oss.setup.direct;
  1579. if (!direct)
  1580. return AFMT_MU_LAW | AFMT_U8 |
  1581. AFMT_S16_LE | AFMT_S16_BE |
  1582. AFMT_S8 | AFMT_U16_LE |
  1583. AFMT_U16_BE;
  1584. params = kmalloc(sizeof(*params), GFP_KERNEL);
  1585. if (!params)
  1586. return -ENOMEM;
  1587. _snd_pcm_hw_params_any(params);
  1588. err = snd_pcm_hw_refine(substream, params);
  1589. format_mask = *hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1590. kfree(params);
  1591. snd_assert(err >= 0, return err);
  1592. for (fmt = 0; fmt < 32; ++fmt) {
  1593. if (snd_mask_test(&format_mask, fmt)) {
  1594. int f = snd_pcm_oss_format_to(fmt);
  1595. if (f >= 0)
  1596. formats |= f;
  1597. }
  1598. }
  1599. return formats;
  1600. }
  1601. static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
  1602. {
  1603. int formats, idx;
  1604. if (format != AFMT_QUERY) {
  1605. formats = snd_pcm_oss_get_formats(pcm_oss_file);
  1606. if (formats < 0)
  1607. return formats;
  1608. if (!(formats & format))
  1609. format = AFMT_U8;
  1610. for (idx = 1; idx >= 0; --idx) {
  1611. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1612. struct snd_pcm_runtime *runtime;
  1613. if (substream == NULL)
  1614. continue;
  1615. runtime = substream->runtime;
  1616. if (runtime->oss.format != format) {
  1617. runtime->oss.params = 1;
  1618. runtime->oss.format = format;
  1619. }
  1620. }
  1621. }
  1622. return snd_pcm_oss_get_format(pcm_oss_file);
  1623. }
  1624. static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
  1625. {
  1626. struct snd_pcm_substream *substream;
  1627. int err;
  1628. if ((err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream)) < 0)
  1629. return err;
  1630. return substream->runtime->oss.format;
  1631. }
  1632. static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
  1633. {
  1634. struct snd_pcm_runtime *runtime;
  1635. if (substream == NULL)
  1636. return 0;
  1637. runtime = substream->runtime;
  1638. if (subdivide == 0) {
  1639. subdivide = runtime->oss.subdivision;
  1640. if (subdivide == 0)
  1641. subdivide = 1;
  1642. return subdivide;
  1643. }
  1644. if (runtime->oss.subdivision || runtime->oss.fragshift)
  1645. return -EINVAL;
  1646. if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
  1647. subdivide != 8 && subdivide != 16)
  1648. return -EINVAL;
  1649. runtime->oss.subdivision = subdivide;
  1650. runtime->oss.params = 1;
  1651. return subdivide;
  1652. }
  1653. static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
  1654. {
  1655. int err = -EINVAL, idx;
  1656. for (idx = 1; idx >= 0; --idx) {
  1657. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1658. if (substream == NULL)
  1659. continue;
  1660. if ((err = snd_pcm_oss_set_subdivide1(substream, subdivide)) < 0)
  1661. return err;
  1662. }
  1663. return err;
  1664. }
  1665. static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
  1666. {
  1667. struct snd_pcm_runtime *runtime;
  1668. if (substream == NULL)
  1669. return 0;
  1670. runtime = substream->runtime;
  1671. if (runtime->oss.subdivision || runtime->oss.fragshift)
  1672. return -EINVAL;
  1673. runtime->oss.fragshift = val & 0xffff;
  1674. runtime->oss.maxfrags = (val >> 16) & 0xffff;
  1675. if (runtime->oss.fragshift < 4) /* < 16 */
  1676. runtime->oss.fragshift = 4;
  1677. if (runtime->oss.maxfrags < 2)
  1678. runtime->oss.maxfrags = 2;
  1679. runtime->oss.params = 1;
  1680. return 0;
  1681. }
  1682. static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
  1683. {
  1684. int err = -EINVAL, idx;
  1685. for (idx = 1; idx >= 0; --idx) {
  1686. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1687. if (substream == NULL)
  1688. continue;
  1689. if ((err = snd_pcm_oss_set_fragment1(substream, val)) < 0)
  1690. return err;
  1691. }
  1692. return err;
  1693. }
  1694. static int snd_pcm_oss_nonblock(struct file * file)
  1695. {
  1696. file->f_flags |= O_NONBLOCK;
  1697. return 0;
  1698. }
  1699. static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
  1700. {
  1701. if (substream == NULL) {
  1702. res &= ~DSP_CAP_DUPLEX;
  1703. return res;
  1704. }
  1705. #ifdef DSP_CAP_MULTI
  1706. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1707. if (substream->pstr->substream_count > 1)
  1708. res |= DSP_CAP_MULTI;
  1709. #endif
  1710. /* DSP_CAP_REALTIME is set all times: */
  1711. /* all ALSA drivers can return actual pointer in ring buffer */
  1712. #if defined(DSP_CAP_REALTIME) && 0
  1713. {
  1714. struct snd_pcm_runtime *runtime = substream->runtime;
  1715. if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
  1716. res &= ~DSP_CAP_REALTIME;
  1717. }
  1718. #endif
  1719. return res;
  1720. }
  1721. static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
  1722. {
  1723. int result, idx;
  1724. result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
  1725. for (idx = 0; idx < 2; idx++) {
  1726. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1727. result = snd_pcm_oss_get_caps1(substream, result);
  1728. }
  1729. result |= 0x0001; /* revision - same as SB AWE 64 */
  1730. return result;
  1731. }
  1732. static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream, snd_pcm_uframes_t hw_ptr)
  1733. {
  1734. struct snd_pcm_runtime *runtime = substream->runtime;
  1735. snd_pcm_uframes_t appl_ptr;
  1736. appl_ptr = hw_ptr + runtime->buffer_size;
  1737. appl_ptr %= runtime->boundary;
  1738. runtime->control->appl_ptr = appl_ptr;
  1739. }
  1740. static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
  1741. {
  1742. struct snd_pcm_runtime *runtime;
  1743. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  1744. int err, cmd;
  1745. #ifdef OSS_DEBUG
  1746. printk("pcm_oss: trigger = 0x%x\n", trigger);
  1747. #endif
  1748. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1749. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1750. if (psubstream) {
  1751. if ((err = snd_pcm_oss_make_ready(psubstream)) < 0)
  1752. return err;
  1753. }
  1754. if (csubstream) {
  1755. if ((err = snd_pcm_oss_make_ready(csubstream)) < 0)
  1756. return err;
  1757. }
  1758. if (psubstream) {
  1759. runtime = psubstream->runtime;
  1760. if (trigger & PCM_ENABLE_OUTPUT) {
  1761. if (runtime->oss.trigger)
  1762. goto _skip1;
  1763. if (atomic_read(&psubstream->mmap_count))
  1764. snd_pcm_oss_simulate_fill(psubstream, runtime->hw_ptr_interrupt);
  1765. runtime->oss.trigger = 1;
  1766. runtime->start_threshold = 1;
  1767. cmd = SNDRV_PCM_IOCTL_START;
  1768. } else {
  1769. if (!runtime->oss.trigger)
  1770. goto _skip1;
  1771. runtime->oss.trigger = 0;
  1772. runtime->start_threshold = runtime->boundary;
  1773. cmd = SNDRV_PCM_IOCTL_DROP;
  1774. runtime->oss.prepare = 1;
  1775. }
  1776. err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
  1777. if (err < 0)
  1778. return err;
  1779. }
  1780. _skip1:
  1781. if (csubstream) {
  1782. runtime = csubstream->runtime;
  1783. if (trigger & PCM_ENABLE_INPUT) {
  1784. if (runtime->oss.trigger)
  1785. goto _skip2;
  1786. runtime->oss.trigger = 1;
  1787. runtime->start_threshold = 1;
  1788. cmd = SNDRV_PCM_IOCTL_START;
  1789. } else {
  1790. if (!runtime->oss.trigger)
  1791. goto _skip2;
  1792. runtime->oss.trigger = 0;
  1793. runtime->start_threshold = runtime->boundary;
  1794. cmd = SNDRV_PCM_IOCTL_DROP;
  1795. runtime->oss.prepare = 1;
  1796. }
  1797. err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
  1798. if (err < 0)
  1799. return err;
  1800. }
  1801. _skip2:
  1802. return 0;
  1803. }
  1804. static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
  1805. {
  1806. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  1807. int result = 0;
  1808. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1809. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1810. if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
  1811. result |= PCM_ENABLE_OUTPUT;
  1812. if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
  1813. result |= PCM_ENABLE_INPUT;
  1814. return result;
  1815. }
  1816. static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
  1817. {
  1818. struct snd_pcm_substream *substream;
  1819. struct snd_pcm_runtime *runtime;
  1820. snd_pcm_sframes_t delay;
  1821. int err;
  1822. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1823. if (substream == NULL)
  1824. return -EINVAL;
  1825. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1826. return err;
  1827. runtime = substream->runtime;
  1828. if (runtime->oss.params || runtime->oss.prepare)
  1829. return 0;
  1830. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
  1831. if (err == -EPIPE)
  1832. delay = 0; /* hack for broken OSS applications */
  1833. else if (err < 0)
  1834. return err;
  1835. return snd_pcm_oss_bytes(substream, delay);
  1836. }
  1837. static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
  1838. {
  1839. struct snd_pcm_substream *substream;
  1840. struct snd_pcm_runtime *runtime;
  1841. snd_pcm_sframes_t delay;
  1842. int fixup;
  1843. struct count_info info;
  1844. int err;
  1845. if (_info == NULL)
  1846. return -EFAULT;
  1847. substream = pcm_oss_file->streams[stream];
  1848. if (substream == NULL)
  1849. return -EINVAL;
  1850. if ((err = snd_pcm_oss_make_ready(substream)) < 0)
  1851. return err;
  1852. runtime = substream->runtime;
  1853. if (runtime->oss.params || runtime->oss.prepare) {
  1854. memset(&info, 0, sizeof(info));
  1855. if (copy_to_user(_info, &info, sizeof(info)))
  1856. return -EFAULT;
  1857. return 0;
  1858. }
  1859. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1860. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
  1861. if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
  1862. err = 0;
  1863. delay = 0;
  1864. fixup = 0;
  1865. } else {
  1866. fixup = runtime->oss.buffer_used;
  1867. }
  1868. } else {
  1869. err = snd_pcm_oss_capture_position_fixup(substream, &delay);
  1870. fixup = -runtime->oss.buffer_used;
  1871. }
  1872. if (err < 0)
  1873. return err;
  1874. info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
  1875. if (atomic_read(&substream->mmap_count)) {
  1876. snd_pcm_sframes_t n;
  1877. n = (delay = runtime->hw_ptr_interrupt) - runtime->oss.prev_hw_ptr_interrupt;
  1878. if (n < 0)
  1879. n += runtime->boundary;
  1880. info.blocks = n / runtime->period_size;
  1881. runtime->oss.prev_hw_ptr_interrupt = delay;
  1882. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1883. snd_pcm_oss_simulate_fill(substream, delay);
  1884. info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
  1885. } else {
  1886. delay = snd_pcm_oss_bytes(substream, delay);
  1887. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1888. if (substream->oss.setup.buggyptr)
  1889. info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
  1890. else
  1891. info.blocks = (delay + fixup) / runtime->oss.period_bytes;
  1892. info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
  1893. } else {
  1894. delay += fixup;
  1895. info.blocks = delay / runtime->oss.period_bytes;
  1896. info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
  1897. }
  1898. }
  1899. if (copy_to_user(_info, &info, sizeof(info)))
  1900. return -EFAULT;
  1901. return 0;
  1902. }
  1903. static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
  1904. {
  1905. struct snd_pcm_substream *substream;
  1906. struct snd_pcm_runtime *runtime;
  1907. snd_pcm_sframes_t avail;
  1908. int fixup;
  1909. struct audio_buf_info info;
  1910. int err;
  1911. if (_info == NULL)
  1912. return -EFAULT;
  1913. substream = pcm_oss_file->streams[stream];
  1914. if (substream == NULL)
  1915. return -EINVAL;
  1916. runtime = substream->runtime;
  1917. if (runtime->oss.params &&
  1918. (err = snd_pcm_oss_change_params(substream)) < 0)
  1919. return err;
  1920. info.fragsize = runtime->oss.period_bytes;
  1921. info.fragstotal = runtime->periods;
  1922. if (runtime->oss.prepare) {
  1923. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1924. info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
  1925. info.fragments = runtime->oss.periods;
  1926. } else {
  1927. info.bytes = 0;
  1928. info.fragments = 0;
  1929. }
  1930. } else {
  1931. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1932. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
  1933. if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
  1934. avail = runtime->buffer_size;
  1935. err = 0;
  1936. fixup = 0;
  1937. } else {
  1938. avail = runtime->buffer_size - avail;
  1939. fixup = -runtime->oss.buffer_used;
  1940. }
  1941. } else {
  1942. err = snd_pcm_oss_capture_position_fixup(substream, &avail);
  1943. fixup = runtime->oss.buffer_used;
  1944. }
  1945. if (err < 0)
  1946. return err;
  1947. info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
  1948. info.fragments = info.bytes / runtime->oss.period_bytes;
  1949. }
  1950. #ifdef OSS_DEBUG
  1951. printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
  1952. #endif
  1953. if (copy_to_user(_info, &info, sizeof(info)))
  1954. return -EFAULT;
  1955. return 0;
  1956. }
  1957. static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
  1958. {
  1959. // it won't be probably implemented
  1960. // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
  1961. return -EINVAL;
  1962. }
  1963. static const char *strip_task_path(const char *path)
  1964. {
  1965. const char *ptr, *ptrl = NULL;
  1966. for (ptr = path; *ptr; ptr++) {
  1967. if (*ptr == '/')
  1968. ptrl = ptr + 1;
  1969. }
  1970. return ptrl;
  1971. }
  1972. static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
  1973. const char *task_name,
  1974. struct snd_pcm_oss_setup *rsetup)
  1975. {
  1976. struct snd_pcm_oss_setup *setup;
  1977. mutex_lock(&pcm->streams[stream].oss.setup_mutex);
  1978. do {
  1979. for (setup = pcm->streams[stream].oss.setup_list; setup;
  1980. setup = setup->next) {
  1981. if (!strcmp(setup->task_name, task_name))
  1982. goto out;
  1983. }
  1984. } while ((task_name = strip_task_path(task_name)) != NULL);
  1985. out:
  1986. if (setup)
  1987. *rsetup = *setup;
  1988. mutex_unlock(&pcm->streams[stream].oss.setup_mutex);
  1989. }
  1990. static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
  1991. {
  1992. struct snd_pcm_runtime *runtime;
  1993. runtime = substream->runtime;
  1994. vfree(runtime->oss.buffer);
  1995. runtime->oss.buffer = NULL;
  1996. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  1997. snd_pcm_oss_plugin_clear(substream);
  1998. #endif
  1999. substream->oss.oss = 0;
  2000. }
  2001. static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
  2002. struct snd_pcm_oss_setup *setup,
  2003. int minor)
  2004. {
  2005. struct snd_pcm_runtime *runtime;
  2006. substream->oss.oss = 1;
  2007. substream->oss.setup = *setup;
  2008. if (setup->nonblock)
  2009. substream->f_flags |= O_NONBLOCK;
  2010. else if (setup->block)
  2011. substream->f_flags &= ~O_NONBLOCK;
  2012. runtime = substream->runtime;
  2013. runtime->oss.params = 1;
  2014. runtime->oss.trigger = 1;
  2015. runtime->oss.rate = 8000;
  2016. switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
  2017. case SNDRV_MINOR_OSS_PCM_8:
  2018. runtime->oss.format = AFMT_U8;
  2019. break;
  2020. case SNDRV_MINOR_OSS_PCM_16:
  2021. runtime->oss.format = AFMT_S16_LE;
  2022. break;
  2023. default:
  2024. runtime->oss.format = AFMT_MU_LAW;
  2025. }
  2026. runtime->oss.channels = 1;
  2027. runtime->oss.fragshift = 0;
  2028. runtime->oss.maxfrags = 0;
  2029. runtime->oss.subdivision = 0;
  2030. substream->pcm_release = snd_pcm_oss_release_substream;
  2031. }
  2032. static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
  2033. {
  2034. int cidx;
  2035. snd_assert(pcm_oss_file != NULL, return -ENXIO);
  2036. for (cidx = 0; cidx < 2; ++cidx) {
  2037. struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
  2038. if (substream)
  2039. snd_pcm_release_substream(substream);
  2040. }
  2041. kfree(pcm_oss_file);
  2042. return 0;
  2043. }
  2044. static int snd_pcm_oss_open_file(struct file *file,
  2045. struct snd_pcm *pcm,
  2046. struct snd_pcm_oss_file **rpcm_oss_file,
  2047. int minor,
  2048. struct snd_pcm_oss_setup *setup)
  2049. {
  2050. int idx, err;
  2051. struct snd_pcm_oss_file *pcm_oss_file;
  2052. struct snd_pcm_substream *substream;
  2053. unsigned int f_mode = file->f_mode;
  2054. snd_assert(rpcm_oss_file != NULL, return -EINVAL);
  2055. *rpcm_oss_file = NULL;
  2056. pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
  2057. if (pcm_oss_file == NULL)
  2058. return -ENOMEM;
  2059. if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
  2060. (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
  2061. f_mode = FMODE_WRITE;
  2062. file->f_flags &= ~O_APPEND;
  2063. for (idx = 0; idx < 2; idx++) {
  2064. if (setup[idx].disable)
  2065. continue;
  2066. if (! pcm->streams[idx].substream_count)
  2067. continue; /* no matching substream */
  2068. if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
  2069. if (! (f_mode & FMODE_WRITE))
  2070. continue;
  2071. } else {
  2072. if (! (f_mode & FMODE_READ))
  2073. continue;
  2074. }
  2075. err = snd_pcm_open_substream(pcm, idx, file, &substream);
  2076. if (err < 0) {
  2077. snd_pcm_oss_release_file(pcm_oss_file);
  2078. return err;
  2079. }
  2080. pcm_oss_file->streams[idx] = substream;
  2081. substream->file = pcm_oss_file;
  2082. snd_pcm_oss_init_substream(substream, &setup[idx], minor);
  2083. }
  2084. if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
  2085. snd_pcm_oss_release_file(pcm_oss_file);
  2086. return -EINVAL;
  2087. }
  2088. file->private_data = pcm_oss_file;
  2089. *rpcm_oss_file = pcm_oss_file;
  2090. return 0;
  2091. }
  2092. static int snd_task_name(struct task_struct *task, char *name, size_t size)
  2093. {
  2094. unsigned int idx;
  2095. snd_assert(task != NULL && name != NULL && size >= 2, return -EINVAL);
  2096. for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
  2097. name[idx] = task->comm[idx];
  2098. name[idx] = '\0';
  2099. return 0;
  2100. }
  2101. static int snd_pcm_oss_open(struct inode *inode, struct file *file)
  2102. {
  2103. int err;
  2104. char task_name[32];
  2105. struct snd_pcm *pcm;
  2106. struct snd_pcm_oss_file *pcm_oss_file;
  2107. struct snd_pcm_oss_setup setup[2];
  2108. int nonblock;
  2109. wait_queue_t wait;
  2110. pcm = snd_lookup_oss_minor_data(iminor(inode),
  2111. SNDRV_OSS_DEVICE_TYPE_PCM);
  2112. if (pcm == NULL) {
  2113. err = -ENODEV;
  2114. goto __error1;
  2115. }
  2116. err = snd_card_file_add(pcm->card, file);
  2117. if (err < 0)
  2118. goto __error1;
  2119. if (!try_module_get(pcm->card->module)) {
  2120. err = -EFAULT;
  2121. goto __error2;
  2122. }
  2123. if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
  2124. err = -EFAULT;
  2125. goto __error;
  2126. }
  2127. memset(setup, 0, sizeof(setup));
  2128. if (file->f_mode & FMODE_WRITE)
  2129. snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  2130. task_name, &setup[0]);
  2131. if (file->f_mode & FMODE_READ)
  2132. snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
  2133. task_name, &setup[1]);
  2134. nonblock = !!(file->f_flags & O_NONBLOCK);
  2135. if (!nonblock)
  2136. nonblock = nonblock_open;
  2137. init_waitqueue_entry(&wait, current);
  2138. add_wait_queue(&pcm->open_wait, &wait);
  2139. mutex_lock(&pcm->open_mutex);
  2140. while (1) {
  2141. err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
  2142. iminor(inode), setup);
  2143. if (err >= 0)
  2144. break;
  2145. if (err == -EAGAIN) {
  2146. if (nonblock) {
  2147. err = -EBUSY;
  2148. break;
  2149. }
  2150. } else
  2151. break;
  2152. set_current_state(TASK_INTERRUPTIBLE);
  2153. mutex_unlock(&pcm->open_mutex);
  2154. schedule();
  2155. mutex_lock(&pcm->open_mutex);
  2156. if (signal_pending(current)) {
  2157. err = -ERESTARTSYS;
  2158. break;
  2159. }
  2160. }
  2161. remove_wait_queue(&pcm->open_wait, &wait);
  2162. mutex_unlock(&pcm->open_mutex);
  2163. if (err < 0)
  2164. goto __error;
  2165. return err;
  2166. __error:
  2167. module_put(pcm->card->module);
  2168. __error2:
  2169. snd_card_file_remove(pcm->card, file);
  2170. __error1:
  2171. return err;
  2172. }
  2173. static int snd_pcm_oss_release(struct inode *inode, struct file *file)
  2174. {
  2175. struct snd_pcm *pcm;
  2176. struct snd_pcm_substream *substream;
  2177. struct snd_pcm_oss_file *pcm_oss_file;
  2178. pcm_oss_file = file->private_data;
  2179. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2180. if (substream == NULL)
  2181. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2182. snd_assert(substream != NULL, return -ENXIO);
  2183. pcm = substream->pcm;
  2184. snd_pcm_oss_sync(pcm_oss_file);
  2185. mutex_lock(&pcm->open_mutex);
  2186. snd_pcm_oss_release_file(pcm_oss_file);
  2187. mutex_unlock(&pcm->open_mutex);
  2188. wake_up(&pcm->open_wait);
  2189. module_put(pcm->card->module);
  2190. snd_card_file_remove(pcm->card, file);
  2191. return 0;
  2192. }
  2193. static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2194. {
  2195. struct snd_pcm_oss_file *pcm_oss_file;
  2196. int __user *p = (int __user *)arg;
  2197. int res;
  2198. pcm_oss_file = file->private_data;
  2199. if (cmd == OSS_GETVERSION)
  2200. return put_user(SNDRV_OSS_VERSION, p);
  2201. if (cmd == OSS_ALSAEMULVER)
  2202. return put_user(1, p);
  2203. #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
  2204. if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
  2205. struct snd_pcm_substream *substream;
  2206. int idx;
  2207. for (idx = 0; idx < 2; ++idx) {
  2208. substream = pcm_oss_file->streams[idx];
  2209. if (substream != NULL)
  2210. break;
  2211. }
  2212. snd_assert(substream != NULL, return -ENXIO);
  2213. return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
  2214. }
  2215. #endif
  2216. if (((cmd >> 8) & 0xff) != 'P')
  2217. return -EINVAL;
  2218. #ifdef OSS_DEBUG
  2219. printk("pcm_oss: ioctl = 0x%x\n", cmd);
  2220. #endif
  2221. switch (cmd) {
  2222. case SNDCTL_DSP_RESET:
  2223. return snd_pcm_oss_reset(pcm_oss_file);
  2224. case SNDCTL_DSP_SYNC:
  2225. return snd_pcm_oss_sync(pcm_oss_file);
  2226. case SNDCTL_DSP_SPEED:
  2227. if (get_user(res, p))
  2228. return -EFAULT;
  2229. if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
  2230. return res;
  2231. return put_user(res, p);
  2232. case SOUND_PCM_READ_RATE:
  2233. res = snd_pcm_oss_get_rate(pcm_oss_file);
  2234. if (res < 0)
  2235. return res;
  2236. return put_user(res, p);
  2237. case SNDCTL_DSP_STEREO:
  2238. if (get_user(res, p))
  2239. return -EFAULT;
  2240. res = res > 0 ? 2 : 1;
  2241. if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
  2242. return res;
  2243. return put_user(--res, p);
  2244. case SNDCTL_DSP_GETBLKSIZE:
  2245. res = snd_pcm_oss_get_block_size(pcm_oss_file);
  2246. if (res < 0)
  2247. return res;
  2248. return put_user(res, p);
  2249. case SNDCTL_DSP_SETFMT:
  2250. if (get_user(res, p))
  2251. return -EFAULT;
  2252. res = snd_pcm_oss_set_format(pcm_oss_file, res);
  2253. if (res < 0)
  2254. return res;
  2255. return put_user(res, p);
  2256. case SOUND_PCM_READ_BITS:
  2257. res = snd_pcm_oss_get_format(pcm_oss_file);
  2258. if (res < 0)
  2259. return res;
  2260. return put_user(res, p);
  2261. case SNDCTL_DSP_CHANNELS:
  2262. if (get_user(res, p))
  2263. return -EFAULT;
  2264. res = snd_pcm_oss_set_channels(pcm_oss_file, res);
  2265. if (res < 0)
  2266. return res;
  2267. return put_user(res, p);
  2268. case SOUND_PCM_READ_CHANNELS:
  2269. res = snd_pcm_oss_get_channels(pcm_oss_file);
  2270. if (res < 0)
  2271. return res;
  2272. return put_user(res, p);
  2273. case SOUND_PCM_WRITE_FILTER:
  2274. case SOUND_PCM_READ_FILTER:
  2275. return -EIO;
  2276. case SNDCTL_DSP_POST:
  2277. return snd_pcm_oss_post(pcm_oss_file);
  2278. case SNDCTL_DSP_SUBDIVIDE:
  2279. if (get_user(res, p))
  2280. return -EFAULT;
  2281. res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
  2282. if (res < 0)
  2283. return res;
  2284. return put_user(res, p);
  2285. case SNDCTL_DSP_SETFRAGMENT:
  2286. if (get_user(res, p))
  2287. return -EFAULT;
  2288. return snd_pcm_oss_set_fragment(pcm_oss_file, res);
  2289. case SNDCTL_DSP_GETFMTS:
  2290. res = snd_pcm_oss_get_formats(pcm_oss_file);
  2291. if (res < 0)
  2292. return res;
  2293. return put_user(res, p);
  2294. case SNDCTL_DSP_GETOSPACE:
  2295. case SNDCTL_DSP_GETISPACE:
  2296. return snd_pcm_oss_get_space(pcm_oss_file,
  2297. cmd == SNDCTL_DSP_GETISPACE ?
  2298. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2299. (struct audio_buf_info __user *) arg);
  2300. case SNDCTL_DSP_NONBLOCK:
  2301. return snd_pcm_oss_nonblock(file);
  2302. case SNDCTL_DSP_GETCAPS:
  2303. res = snd_pcm_oss_get_caps(pcm_oss_file);
  2304. if (res < 0)
  2305. return res;
  2306. return put_user(res, p);
  2307. case SNDCTL_DSP_GETTRIGGER:
  2308. res = snd_pcm_oss_get_trigger(pcm_oss_file);
  2309. if (res < 0)
  2310. return res;
  2311. return put_user(res, p);
  2312. case SNDCTL_DSP_SETTRIGGER:
  2313. if (get_user(res, p))
  2314. return -EFAULT;
  2315. return snd_pcm_oss_set_trigger(pcm_oss_file, res);
  2316. case SNDCTL_DSP_GETIPTR:
  2317. case SNDCTL_DSP_GETOPTR:
  2318. return snd_pcm_oss_get_ptr(pcm_oss_file,
  2319. cmd == SNDCTL_DSP_GETIPTR ?
  2320. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2321. (struct count_info __user *) arg);
  2322. case SNDCTL_DSP_MAPINBUF:
  2323. case SNDCTL_DSP_MAPOUTBUF:
  2324. return snd_pcm_oss_get_mapbuf(pcm_oss_file,
  2325. cmd == SNDCTL_DSP_MAPINBUF ?
  2326. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2327. (struct buffmem_desc __user *) arg);
  2328. case SNDCTL_DSP_SETSYNCRO:
  2329. /* stop DMA now.. */
  2330. return 0;
  2331. case SNDCTL_DSP_SETDUPLEX:
  2332. if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
  2333. return 0;
  2334. return -EIO;
  2335. case SNDCTL_DSP_GETODELAY:
  2336. res = snd_pcm_oss_get_odelay(pcm_oss_file);
  2337. if (res < 0) {
  2338. /* it's for sure, some broken apps don't check for error codes */
  2339. put_user(0, p);
  2340. return res;
  2341. }
  2342. return put_user(res, p);
  2343. case SNDCTL_DSP_PROFILE:
  2344. return 0; /* silently ignore */
  2345. default:
  2346. snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
  2347. }
  2348. return -EINVAL;
  2349. }
  2350. #ifdef CONFIG_COMPAT
  2351. /* all compatible */
  2352. #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
  2353. #else
  2354. #define snd_pcm_oss_ioctl_compat NULL
  2355. #endif
  2356. static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  2357. {
  2358. struct snd_pcm_oss_file *pcm_oss_file;
  2359. struct snd_pcm_substream *substream;
  2360. pcm_oss_file = file->private_data;
  2361. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2362. if (substream == NULL)
  2363. return -ENXIO;
  2364. substream->f_flags = file->f_flags & O_NONBLOCK;
  2365. #ifndef OSS_DEBUG
  2366. return snd_pcm_oss_read1(substream, buf, count);
  2367. #else
  2368. {
  2369. ssize_t res = snd_pcm_oss_read1(substream, buf, count);
  2370. printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
  2371. return res;
  2372. }
  2373. #endif
  2374. }
  2375. static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
  2376. {
  2377. struct snd_pcm_oss_file *pcm_oss_file;
  2378. struct snd_pcm_substream *substream;
  2379. long result;
  2380. pcm_oss_file = file->private_data;
  2381. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2382. if (substream == NULL)
  2383. return -ENXIO;
  2384. substream->f_flags = file->f_flags & O_NONBLOCK;
  2385. result = snd_pcm_oss_write1(substream, buf, count);
  2386. #ifdef OSS_DEBUG
  2387. printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
  2388. #endif
  2389. return result;
  2390. }
  2391. static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
  2392. {
  2393. struct snd_pcm_runtime *runtime = substream->runtime;
  2394. if (atomic_read(&substream->mmap_count))
  2395. return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
  2396. else
  2397. return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
  2398. }
  2399. static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
  2400. {
  2401. struct snd_pcm_runtime *runtime = substream->runtime;
  2402. if (atomic_read(&substream->mmap_count))
  2403. return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
  2404. else
  2405. return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
  2406. }
  2407. static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
  2408. {
  2409. struct snd_pcm_oss_file *pcm_oss_file;
  2410. unsigned int mask;
  2411. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  2412. pcm_oss_file = file->private_data;
  2413. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2414. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2415. mask = 0;
  2416. if (psubstream != NULL) {
  2417. struct snd_pcm_runtime *runtime = psubstream->runtime;
  2418. poll_wait(file, &runtime->sleep, wait);
  2419. snd_pcm_stream_lock_irq(psubstream);
  2420. if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
  2421. (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
  2422. snd_pcm_oss_playback_ready(psubstream)))
  2423. mask |= POLLOUT | POLLWRNORM;
  2424. snd_pcm_stream_unlock_irq(psubstream);
  2425. }
  2426. if (csubstream != NULL) {
  2427. struct snd_pcm_runtime *runtime = csubstream->runtime;
  2428. snd_pcm_state_t ostate;
  2429. poll_wait(file, &runtime->sleep, wait);
  2430. snd_pcm_stream_lock_irq(csubstream);
  2431. if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
  2432. snd_pcm_oss_capture_ready(csubstream))
  2433. mask |= POLLIN | POLLRDNORM;
  2434. snd_pcm_stream_unlock_irq(csubstream);
  2435. if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
  2436. struct snd_pcm_oss_file ofile;
  2437. memset(&ofile, 0, sizeof(ofile));
  2438. ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2439. runtime->oss.trigger = 0;
  2440. snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
  2441. }
  2442. }
  2443. return mask;
  2444. }
  2445. static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
  2446. {
  2447. struct snd_pcm_oss_file *pcm_oss_file;
  2448. struct snd_pcm_substream *substream = NULL;
  2449. struct snd_pcm_runtime *runtime;
  2450. int err;
  2451. #ifdef OSS_DEBUG
  2452. printk("pcm_oss: mmap begin\n");
  2453. #endif
  2454. pcm_oss_file = file->private_data;
  2455. switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
  2456. case VM_READ | VM_WRITE:
  2457. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2458. if (substream)
  2459. break;
  2460. /* Fall through */
  2461. case VM_READ:
  2462. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2463. break;
  2464. case VM_WRITE:
  2465. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2466. break;
  2467. default:
  2468. return -EINVAL;
  2469. }
  2470. /* set VM_READ access as well to fix memset() routines that do
  2471. reads before writes (to improve performance) */
  2472. area->vm_flags |= VM_READ;
  2473. if (substream == NULL)
  2474. return -ENXIO;
  2475. runtime = substream->runtime;
  2476. if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
  2477. return -EIO;
  2478. if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
  2479. runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
  2480. else
  2481. return -EIO;
  2482. if (runtime->oss.params) {
  2483. if ((err = snd_pcm_oss_change_params(substream)) < 0)
  2484. return err;
  2485. }
  2486. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  2487. if (runtime->oss.plugin_first != NULL)
  2488. return -EIO;
  2489. #endif
  2490. if (area->vm_pgoff != 0)
  2491. return -EINVAL;
  2492. err = snd_pcm_mmap_data(substream, file, area);
  2493. if (err < 0)
  2494. return err;
  2495. runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
  2496. runtime->silence_threshold = 0;
  2497. runtime->silence_size = 0;
  2498. #ifdef OSS_DEBUG
  2499. printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
  2500. #endif
  2501. /* In mmap mode we never stop */
  2502. runtime->stop_threshold = runtime->boundary;
  2503. return 0;
  2504. }
  2505. #ifdef CONFIG_SND_VERBOSE_PROCFS
  2506. /*
  2507. * /proc interface
  2508. */
  2509. static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
  2510. struct snd_info_buffer *buffer)
  2511. {
  2512. struct snd_pcm_str *pstr = entry->private_data;
  2513. struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
  2514. mutex_lock(&pstr->oss.setup_mutex);
  2515. while (setup) {
  2516. snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
  2517. setup->task_name,
  2518. setup->periods,
  2519. setup->period_size,
  2520. setup->disable ? " disable" : "",
  2521. setup->direct ? " direct" : "",
  2522. setup->block ? " block" : "",
  2523. setup->nonblock ? " non-block" : "",
  2524. setup->partialfrag ? " partial-frag" : "",
  2525. setup->nosilence ? " no-silence" : "");
  2526. setup = setup->next;
  2527. }
  2528. mutex_unlock(&pstr->oss.setup_mutex);
  2529. }
  2530. static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
  2531. {
  2532. struct snd_pcm_oss_setup *setup, *setupn;
  2533. for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
  2534. setup; setup = setupn) {
  2535. setupn = setup->next;
  2536. kfree(setup->task_name);
  2537. kfree(setup);
  2538. }
  2539. pstr->oss.setup_list = NULL;
  2540. }
  2541. static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
  2542. struct snd_info_buffer *buffer)
  2543. {
  2544. struct snd_pcm_str *pstr = entry->private_data;
  2545. char line[128], str[32], task_name[32], *ptr;
  2546. int idx1;
  2547. struct snd_pcm_oss_setup *setup, *setup1, template;
  2548. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  2549. mutex_lock(&pstr->oss.setup_mutex);
  2550. memset(&template, 0, sizeof(template));
  2551. ptr = snd_info_get_str(task_name, line, sizeof(task_name));
  2552. if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
  2553. snd_pcm_oss_proc_free_setup_list(pstr);
  2554. mutex_unlock(&pstr->oss.setup_mutex);
  2555. continue;
  2556. }
  2557. for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
  2558. if (!strcmp(setup->task_name, task_name)) {
  2559. template = *setup;
  2560. break;
  2561. }
  2562. }
  2563. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2564. template.periods = simple_strtoul(str, NULL, 10);
  2565. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2566. template.period_size = simple_strtoul(str, NULL, 10);
  2567. for (idx1 = 31; idx1 >= 0; idx1--)
  2568. if (template.period_size & (1 << idx1))
  2569. break;
  2570. for (idx1--; idx1 >= 0; idx1--)
  2571. template.period_size &= ~(1 << idx1);
  2572. do {
  2573. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2574. if (!strcmp(str, "disable")) {
  2575. template.disable = 1;
  2576. } else if (!strcmp(str, "direct")) {
  2577. template.direct = 1;
  2578. } else if (!strcmp(str, "block")) {
  2579. template.block = 1;
  2580. } else if (!strcmp(str, "non-block")) {
  2581. template.nonblock = 1;
  2582. } else if (!strcmp(str, "partial-frag")) {
  2583. template.partialfrag = 1;
  2584. } else if (!strcmp(str, "no-silence")) {
  2585. template.nosilence = 1;
  2586. } else if (!strcmp(str, "buggy-ptr")) {
  2587. template.buggyptr = 1;
  2588. }
  2589. } while (*str);
  2590. if (setup == NULL) {
  2591. setup = kmalloc(sizeof(*setup), GFP_KERNEL);
  2592. if (! setup) {
  2593. buffer->error = -ENOMEM;
  2594. mutex_lock(&pstr->oss.setup_mutex);
  2595. return;
  2596. }
  2597. if (pstr->oss.setup_list == NULL)
  2598. pstr->oss.setup_list = setup;
  2599. else {
  2600. for (setup1 = pstr->oss.setup_list;
  2601. setup1->next; setup1 = setup1->next);
  2602. setup1->next = setup;
  2603. }
  2604. template.task_name = kstrdup(task_name, GFP_KERNEL);
  2605. if (! template.task_name) {
  2606. kfree(setup);
  2607. buffer->error = -ENOMEM;
  2608. mutex_lock(&pstr->oss.setup_mutex);
  2609. return;
  2610. }
  2611. }
  2612. *setup = template;
  2613. mutex_unlock(&pstr->oss.setup_mutex);
  2614. }
  2615. }
  2616. static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
  2617. {
  2618. int stream;
  2619. for (stream = 0; stream < 2; ++stream) {
  2620. struct snd_info_entry *entry;
  2621. struct snd_pcm_str *pstr = &pcm->streams[stream];
  2622. if (pstr->substream_count == 0)
  2623. continue;
  2624. if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
  2625. entry->content = SNDRV_INFO_CONTENT_TEXT;
  2626. entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
  2627. entry->c.text.read = snd_pcm_oss_proc_read;
  2628. entry->c.text.write = snd_pcm_oss_proc_write;
  2629. entry->private_data = pstr;
  2630. if (snd_info_register(entry) < 0) {
  2631. snd_info_free_entry(entry);
  2632. entry = NULL;
  2633. }
  2634. }
  2635. pstr->oss.proc_entry = entry;
  2636. }
  2637. }
  2638. static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
  2639. {
  2640. int stream;
  2641. for (stream = 0; stream < 2; ++stream) {
  2642. struct snd_pcm_str *pstr = &pcm->streams[stream];
  2643. snd_info_free_entry(pstr->oss.proc_entry);
  2644. pstr->oss.proc_entry = NULL;
  2645. snd_pcm_oss_proc_free_setup_list(pstr);
  2646. }
  2647. }
  2648. #else /* !CONFIG_SND_VERBOSE_PROCFS */
  2649. #define snd_pcm_oss_proc_init(pcm)
  2650. #define snd_pcm_oss_proc_done(pcm)
  2651. #endif /* CONFIG_SND_VERBOSE_PROCFS */
  2652. /*
  2653. * ENTRY functions
  2654. */
  2655. static struct file_operations snd_pcm_oss_f_reg =
  2656. {
  2657. .owner = THIS_MODULE,
  2658. .read = snd_pcm_oss_read,
  2659. .write = snd_pcm_oss_write,
  2660. .open = snd_pcm_oss_open,
  2661. .release = snd_pcm_oss_release,
  2662. .poll = snd_pcm_oss_poll,
  2663. .unlocked_ioctl = snd_pcm_oss_ioctl,
  2664. .compat_ioctl = snd_pcm_oss_ioctl_compat,
  2665. .mmap = snd_pcm_oss_mmap,
  2666. };
  2667. static void register_oss_dsp(struct snd_pcm *pcm, int index)
  2668. {
  2669. char name[128];
  2670. sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
  2671. if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2672. pcm->card, index, &snd_pcm_oss_f_reg,
  2673. pcm, name) < 0) {
  2674. snd_printk(KERN_ERR "unable to register OSS PCM device %i:%i\n",
  2675. pcm->card->number, pcm->device);
  2676. }
  2677. }
  2678. static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
  2679. {
  2680. pcm->oss.reg = 0;
  2681. if (dsp_map[pcm->card->number] == (int)pcm->device) {
  2682. char name[128];
  2683. int duplex;
  2684. register_oss_dsp(pcm, 0);
  2685. duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
  2686. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
  2687. !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
  2688. sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
  2689. #ifdef SNDRV_OSS_INFO_DEV_AUDIO
  2690. snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
  2691. pcm->card->number,
  2692. name);
  2693. #endif
  2694. pcm->oss.reg++;
  2695. pcm->oss.reg_mask |= 1;
  2696. }
  2697. if (adsp_map[pcm->card->number] == (int)pcm->device) {
  2698. register_oss_dsp(pcm, 1);
  2699. pcm->oss.reg++;
  2700. pcm->oss.reg_mask |= 2;
  2701. }
  2702. if (pcm->oss.reg)
  2703. snd_pcm_oss_proc_init(pcm);
  2704. return 0;
  2705. }
  2706. static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
  2707. {
  2708. if (pcm->oss.reg) {
  2709. if (pcm->oss.reg_mask & 1) {
  2710. pcm->oss.reg_mask &= ~1;
  2711. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2712. pcm->card, 0);
  2713. }
  2714. if (pcm->oss.reg_mask & 2) {
  2715. pcm->oss.reg_mask &= ~2;
  2716. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2717. pcm->card, 1);
  2718. }
  2719. if (dsp_map[pcm->card->number] == (int)pcm->device) {
  2720. #ifdef SNDRV_OSS_INFO_DEV_AUDIO
  2721. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
  2722. #endif
  2723. }
  2724. pcm->oss.reg = 0;
  2725. }
  2726. return 0;
  2727. }
  2728. static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
  2729. {
  2730. snd_pcm_oss_disconnect_minor(pcm);
  2731. snd_pcm_oss_proc_done(pcm);
  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)