pcm_oss.c 84 KB

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