pcm_oss.c 85 KB

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