pcm_oss.c 72 KB

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