pcm_oss.c 85 KB

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