pcm_oss.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  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. snd_pcm_oss_setup_t *setup = substream->oss.setup;
  1419. if (setup && setup->buggyptr)
  1420. info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
  1421. else
  1422. info.blocks = (delay + fixup) / runtime->oss.period_bytes;
  1423. info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
  1424. } else {
  1425. delay += fixup;
  1426. info.blocks = delay / runtime->oss.period_bytes;
  1427. info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
  1428. }
  1429. }
  1430. if (copy_to_user(_info, &info, sizeof(info)))
  1431. return -EFAULT;
  1432. return 0;
  1433. }
  1434. static int snd_pcm_oss_get_space(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
  1435. {
  1436. snd_pcm_substream_t *substream;
  1437. snd_pcm_runtime_t *runtime;
  1438. snd_pcm_sframes_t avail;
  1439. int fixup;
  1440. struct audio_buf_info info;
  1441. int err;
  1442. if (_info == NULL)
  1443. return -EFAULT;
  1444. substream = pcm_oss_file->streams[stream];
  1445. if (substream == NULL)
  1446. return -EINVAL;
  1447. runtime = substream->runtime;
  1448. if (runtime->oss.params &&
  1449. (err = snd_pcm_oss_change_params(substream)) < 0)
  1450. return err;
  1451. info.fragsize = runtime->oss.period_bytes;
  1452. info.fragstotal = runtime->periods;
  1453. if (runtime->oss.prepare) {
  1454. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1455. info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
  1456. info.fragments = runtime->oss.periods;
  1457. } else {
  1458. info.bytes = 0;
  1459. info.fragments = 0;
  1460. }
  1461. } else {
  1462. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1463. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
  1464. if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
  1465. avail = runtime->buffer_size;
  1466. err = 0;
  1467. fixup = 0;
  1468. } else {
  1469. avail = runtime->buffer_size - avail;
  1470. fixup = -runtime->oss.buffer_used;
  1471. }
  1472. } else {
  1473. err = snd_pcm_oss_capture_position_fixup(substream, &avail);
  1474. fixup = runtime->oss.buffer_used;
  1475. }
  1476. if (err < 0)
  1477. return err;
  1478. info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
  1479. info.fragments = info.bytes / runtime->oss.period_bytes;
  1480. }
  1481. #ifdef OSS_DEBUG
  1482. printk("pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n", info.bytes, info.fragments, info.fragstotal, info.fragsize);
  1483. #endif
  1484. if (copy_to_user(_info, &info, sizeof(info)))
  1485. return -EFAULT;
  1486. return 0;
  1487. }
  1488. static int snd_pcm_oss_get_mapbuf(snd_pcm_oss_file_t *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
  1489. {
  1490. // it won't be probably implemented
  1491. // snd_printd("TODO: snd_pcm_oss_get_mapbuf\n");
  1492. return -EINVAL;
  1493. }
  1494. static snd_pcm_oss_setup_t *snd_pcm_oss_look_for_setup(snd_pcm_t *pcm, int stream, const char *task_name)
  1495. {
  1496. const char *ptr, *ptrl;
  1497. snd_pcm_oss_setup_t *setup;
  1498. down(&pcm->streams[stream].oss.setup_mutex);
  1499. for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
  1500. if (!strcmp(setup->task_name, task_name)) {
  1501. up(&pcm->streams[stream].oss.setup_mutex);
  1502. return setup;
  1503. }
  1504. }
  1505. ptr = ptrl = task_name;
  1506. while (*ptr) {
  1507. if (*ptr == '/')
  1508. ptrl = ptr + 1;
  1509. ptr++;
  1510. }
  1511. if (ptrl == task_name) {
  1512. goto __not_found;
  1513. return NULL;
  1514. }
  1515. for (setup = pcm->streams[stream].oss.setup_list; setup; setup = setup->next) {
  1516. if (!strcmp(setup->task_name, ptrl)) {
  1517. up(&pcm->streams[stream].oss.setup_mutex);
  1518. return setup;
  1519. }
  1520. }
  1521. __not_found:
  1522. up(&pcm->streams[stream].oss.setup_mutex);
  1523. return NULL;
  1524. }
  1525. static void snd_pcm_oss_init_substream(snd_pcm_substream_t *substream,
  1526. snd_pcm_oss_setup_t *setup,
  1527. int minor)
  1528. {
  1529. snd_pcm_runtime_t *runtime;
  1530. substream->oss.oss = 1;
  1531. substream->oss.setup = setup;
  1532. runtime = substream->runtime;
  1533. runtime->oss.params = 1;
  1534. runtime->oss.trigger = 1;
  1535. runtime->oss.rate = 8000;
  1536. switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
  1537. case SNDRV_MINOR_OSS_PCM_8:
  1538. runtime->oss.format = AFMT_U8;
  1539. break;
  1540. case SNDRV_MINOR_OSS_PCM_16:
  1541. runtime->oss.format = AFMT_S16_LE;
  1542. break;
  1543. default:
  1544. runtime->oss.format = AFMT_MU_LAW;
  1545. }
  1546. runtime->oss.channels = 1;
  1547. runtime->oss.fragshift = 0;
  1548. runtime->oss.maxfrags = 0;
  1549. runtime->oss.subdivision = 0;
  1550. }
  1551. static void snd_pcm_oss_release_substream(snd_pcm_substream_t *substream)
  1552. {
  1553. snd_pcm_runtime_t *runtime;
  1554. runtime = substream->runtime;
  1555. vfree(runtime->oss.buffer);
  1556. snd_pcm_oss_plugin_clear(substream);
  1557. substream->oss.file = NULL;
  1558. substream->oss.oss = 0;
  1559. }
  1560. static int snd_pcm_oss_release_file(snd_pcm_oss_file_t *pcm_oss_file)
  1561. {
  1562. int cidx;
  1563. snd_assert(pcm_oss_file != NULL, return -ENXIO);
  1564. for (cidx = 0; cidx < 2; ++cidx) {
  1565. snd_pcm_substream_t *substream = pcm_oss_file->streams[cidx];
  1566. snd_pcm_runtime_t *runtime;
  1567. if (substream == NULL)
  1568. continue;
  1569. runtime = substream->runtime;
  1570. snd_pcm_stream_lock_irq(substream);
  1571. if (snd_pcm_running(substream))
  1572. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1573. snd_pcm_stream_unlock_irq(substream);
  1574. if (substream->ffile != NULL) {
  1575. if (substream->ops->hw_free != NULL)
  1576. substream->ops->hw_free(substream);
  1577. substream->ops->close(substream);
  1578. substream->ffile = NULL;
  1579. }
  1580. snd_pcm_oss_release_substream(substream);
  1581. snd_pcm_release_substream(substream);
  1582. }
  1583. kfree(pcm_oss_file);
  1584. return 0;
  1585. }
  1586. static int snd_pcm_oss_open_file(struct file *file,
  1587. snd_pcm_t *pcm,
  1588. snd_pcm_oss_file_t **rpcm_oss_file,
  1589. int minor,
  1590. snd_pcm_oss_setup_t *psetup,
  1591. snd_pcm_oss_setup_t *csetup)
  1592. {
  1593. int err = 0;
  1594. snd_pcm_oss_file_t *pcm_oss_file;
  1595. snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
  1596. unsigned int f_mode = file->f_mode;
  1597. snd_assert(rpcm_oss_file != NULL, return -EINVAL);
  1598. *rpcm_oss_file = NULL;
  1599. pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
  1600. if (pcm_oss_file == NULL)
  1601. return -ENOMEM;
  1602. if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
  1603. (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
  1604. f_mode = FMODE_WRITE;
  1605. if ((f_mode & FMODE_WRITE) && !(psetup && psetup->disable)) {
  1606. if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1607. &psubstream)) < 0) {
  1608. snd_pcm_oss_release_file(pcm_oss_file);
  1609. return err;
  1610. }
  1611. pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK] = psubstream;
  1612. }
  1613. if ((f_mode & FMODE_READ) && !(csetup && csetup->disable)) {
  1614. if ((err = snd_pcm_open_substream(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1615. &csubstream)) < 0) {
  1616. if (!(f_mode & FMODE_WRITE) || err != -ENODEV) {
  1617. snd_pcm_oss_release_file(pcm_oss_file);
  1618. return err;
  1619. } else {
  1620. csubstream = NULL;
  1621. }
  1622. }
  1623. pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE] = csubstream;
  1624. }
  1625. if (psubstream == NULL && csubstream == NULL) {
  1626. snd_pcm_oss_release_file(pcm_oss_file);
  1627. return -EINVAL;
  1628. }
  1629. if (psubstream != NULL) {
  1630. psubstream->oss.file = pcm_oss_file;
  1631. err = snd_pcm_hw_constraints_init(psubstream);
  1632. if (err < 0) {
  1633. snd_printd("snd_pcm_hw_constraint_init failed\n");
  1634. snd_pcm_oss_release_file(pcm_oss_file);
  1635. return err;
  1636. }
  1637. if ((err = psubstream->ops->open(psubstream)) < 0) {
  1638. snd_pcm_oss_release_file(pcm_oss_file);
  1639. return err;
  1640. }
  1641. psubstream->ffile = file;
  1642. err = snd_pcm_hw_constraints_complete(psubstream);
  1643. if (err < 0) {
  1644. snd_printd("snd_pcm_hw_constraint_complete failed\n");
  1645. snd_pcm_oss_release_file(pcm_oss_file);
  1646. return err;
  1647. }
  1648. snd_pcm_oss_init_substream(psubstream, psetup, minor);
  1649. }
  1650. if (csubstream != NULL) {
  1651. csubstream->oss.file = pcm_oss_file;
  1652. err = snd_pcm_hw_constraints_init(csubstream);
  1653. if (err < 0) {
  1654. snd_printd("snd_pcm_hw_constraint_init failed\n");
  1655. snd_pcm_oss_release_file(pcm_oss_file);
  1656. return err;
  1657. }
  1658. if ((err = csubstream->ops->open(csubstream)) < 0) {
  1659. snd_pcm_oss_release_file(pcm_oss_file);
  1660. return err;
  1661. }
  1662. csubstream->ffile = file;
  1663. err = snd_pcm_hw_constraints_complete(csubstream);
  1664. if (err < 0) {
  1665. snd_printd("snd_pcm_hw_constraint_complete failed\n");
  1666. snd_pcm_oss_release_file(pcm_oss_file);
  1667. return err;
  1668. }
  1669. snd_pcm_oss_init_substream(csubstream, csetup, minor);
  1670. }
  1671. file->private_data = pcm_oss_file;
  1672. *rpcm_oss_file = pcm_oss_file;
  1673. return 0;
  1674. }
  1675. static int snd_pcm_oss_open(struct inode *inode, struct file *file)
  1676. {
  1677. int minor = iminor(inode);
  1678. int cardnum = SNDRV_MINOR_OSS_CARD(minor);
  1679. int device;
  1680. int err;
  1681. char task_name[32];
  1682. snd_pcm_t *pcm;
  1683. snd_pcm_oss_file_t *pcm_oss_file;
  1684. snd_pcm_oss_setup_t *psetup = NULL, *csetup = NULL;
  1685. int nonblock;
  1686. wait_queue_t wait;
  1687. snd_assert(cardnum >= 0 && cardnum < SNDRV_CARDS, return -ENXIO);
  1688. device = SNDRV_MINOR_OSS_DEVICE(minor) == SNDRV_MINOR_OSS_PCM1 ?
  1689. adsp_map[cardnum] : dsp_map[cardnum];
  1690. pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + device];
  1691. if (pcm == NULL) {
  1692. err = -ENODEV;
  1693. goto __error1;
  1694. }
  1695. err = snd_card_file_add(pcm->card, file);
  1696. if (err < 0)
  1697. goto __error1;
  1698. if (!try_module_get(pcm->card->module)) {
  1699. err = -EFAULT;
  1700. goto __error2;
  1701. }
  1702. if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
  1703. err = -EFAULT;
  1704. goto __error;
  1705. }
  1706. if (file->f_mode & FMODE_WRITE)
  1707. psetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK, task_name);
  1708. if (file->f_mode & FMODE_READ)
  1709. csetup = snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE, task_name);
  1710. nonblock = !!(file->f_flags & O_NONBLOCK);
  1711. if (psetup && !psetup->disable) {
  1712. if (psetup->nonblock)
  1713. nonblock = 1;
  1714. else if (psetup->block)
  1715. nonblock = 0;
  1716. } else if (csetup && !csetup->disable) {
  1717. if (csetup->nonblock)
  1718. nonblock = 1;
  1719. else if (csetup->block)
  1720. nonblock = 0;
  1721. }
  1722. if (!nonblock)
  1723. nonblock = nonblock_open;
  1724. init_waitqueue_entry(&wait, current);
  1725. add_wait_queue(&pcm->open_wait, &wait);
  1726. down(&pcm->open_mutex);
  1727. while (1) {
  1728. err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
  1729. minor, psetup, csetup);
  1730. if (err >= 0)
  1731. break;
  1732. if (err == -EAGAIN) {
  1733. if (nonblock) {
  1734. err = -EBUSY;
  1735. break;
  1736. }
  1737. } else
  1738. break;
  1739. set_current_state(TASK_INTERRUPTIBLE);
  1740. up(&pcm->open_mutex);
  1741. schedule();
  1742. down(&pcm->open_mutex);
  1743. if (signal_pending(current)) {
  1744. err = -ERESTARTSYS;
  1745. break;
  1746. }
  1747. }
  1748. remove_wait_queue(&pcm->open_wait, &wait);
  1749. up(&pcm->open_mutex);
  1750. if (err < 0)
  1751. goto __error;
  1752. return err;
  1753. __error:
  1754. module_put(pcm->card->module);
  1755. __error2:
  1756. snd_card_file_remove(pcm->card, file);
  1757. __error1:
  1758. return err;
  1759. }
  1760. static int snd_pcm_oss_release(struct inode *inode, struct file *file)
  1761. {
  1762. snd_pcm_t *pcm;
  1763. snd_pcm_substream_t *substream;
  1764. snd_pcm_oss_file_t *pcm_oss_file;
  1765. pcm_oss_file = file->private_data;
  1766. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1767. if (substream == NULL)
  1768. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1769. snd_assert(substream != NULL, return -ENXIO);
  1770. pcm = substream->pcm;
  1771. snd_pcm_oss_sync(pcm_oss_file);
  1772. down(&pcm->open_mutex);
  1773. snd_pcm_oss_release_file(pcm_oss_file);
  1774. up(&pcm->open_mutex);
  1775. wake_up(&pcm->open_wait);
  1776. module_put(pcm->card->module);
  1777. snd_card_file_remove(pcm->card, file);
  1778. return 0;
  1779. }
  1780. static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1781. {
  1782. snd_pcm_oss_file_t *pcm_oss_file;
  1783. int __user *p = (int __user *)arg;
  1784. int res;
  1785. pcm_oss_file = file->private_data;
  1786. if (cmd == OSS_GETVERSION)
  1787. return put_user(SNDRV_OSS_VERSION, p);
  1788. if (cmd == OSS_ALSAEMULVER)
  1789. return put_user(1, p);
  1790. #if defined(CONFIG_SND_MIXER_OSS) || (defined(MODULE) && defined(CONFIG_SND_MIXER_OSS_MODULE))
  1791. if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
  1792. snd_pcm_substream_t *substream;
  1793. int idx;
  1794. for (idx = 0; idx < 2; ++idx) {
  1795. substream = pcm_oss_file->streams[idx];
  1796. if (substream != NULL)
  1797. break;
  1798. }
  1799. snd_assert(substream != NULL, return -ENXIO);
  1800. return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
  1801. }
  1802. #endif
  1803. if (((cmd >> 8) & 0xff) != 'P')
  1804. return -EINVAL;
  1805. #ifdef OSS_DEBUG
  1806. printk("pcm_oss: ioctl = 0x%x\n", cmd);
  1807. #endif
  1808. switch (cmd) {
  1809. case SNDCTL_DSP_RESET:
  1810. return snd_pcm_oss_reset(pcm_oss_file);
  1811. case SNDCTL_DSP_SYNC:
  1812. return snd_pcm_oss_sync(pcm_oss_file);
  1813. case SNDCTL_DSP_SPEED:
  1814. if (get_user(res, p))
  1815. return -EFAULT;
  1816. if ((res = snd_pcm_oss_set_rate(pcm_oss_file, res))<0)
  1817. return res;
  1818. return put_user(res, p);
  1819. case SOUND_PCM_READ_RATE:
  1820. res = snd_pcm_oss_get_rate(pcm_oss_file);
  1821. if (res < 0)
  1822. return res;
  1823. return put_user(res, p);
  1824. case SNDCTL_DSP_STEREO:
  1825. if (get_user(res, p))
  1826. return -EFAULT;
  1827. res = res > 0 ? 2 : 1;
  1828. if ((res = snd_pcm_oss_set_channels(pcm_oss_file, res)) < 0)
  1829. return res;
  1830. return put_user(--res, p);
  1831. case SNDCTL_DSP_GETBLKSIZE:
  1832. res = snd_pcm_oss_get_block_size(pcm_oss_file);
  1833. if (res < 0)
  1834. return res;
  1835. return put_user(res, p);
  1836. case SNDCTL_DSP_SETFMT:
  1837. if (get_user(res, p))
  1838. return -EFAULT;
  1839. res = snd_pcm_oss_set_format(pcm_oss_file, res);
  1840. if (res < 0)
  1841. return res;
  1842. return put_user(res, p);
  1843. case SOUND_PCM_READ_BITS:
  1844. res = snd_pcm_oss_get_format(pcm_oss_file);
  1845. if (res < 0)
  1846. return res;
  1847. return put_user(res, p);
  1848. case SNDCTL_DSP_CHANNELS:
  1849. if (get_user(res, p))
  1850. return -EFAULT;
  1851. res = snd_pcm_oss_set_channels(pcm_oss_file, res);
  1852. if (res < 0)
  1853. return res;
  1854. return put_user(res, p);
  1855. case SOUND_PCM_READ_CHANNELS:
  1856. res = snd_pcm_oss_get_channels(pcm_oss_file);
  1857. if (res < 0)
  1858. return res;
  1859. return put_user(res, p);
  1860. case SOUND_PCM_WRITE_FILTER:
  1861. case SOUND_PCM_READ_FILTER:
  1862. return -EIO;
  1863. case SNDCTL_DSP_POST:
  1864. return snd_pcm_oss_post(pcm_oss_file);
  1865. case SNDCTL_DSP_SUBDIVIDE:
  1866. if (get_user(res, p))
  1867. return -EFAULT;
  1868. res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
  1869. if (res < 0)
  1870. return res;
  1871. return put_user(res, p);
  1872. case SNDCTL_DSP_SETFRAGMENT:
  1873. if (get_user(res, p))
  1874. return -EFAULT;
  1875. return snd_pcm_oss_set_fragment(pcm_oss_file, res);
  1876. case SNDCTL_DSP_GETFMTS:
  1877. res = snd_pcm_oss_get_formats(pcm_oss_file);
  1878. if (res < 0)
  1879. return res;
  1880. return put_user(res, p);
  1881. case SNDCTL_DSP_GETOSPACE:
  1882. case SNDCTL_DSP_GETISPACE:
  1883. return snd_pcm_oss_get_space(pcm_oss_file,
  1884. cmd == SNDCTL_DSP_GETISPACE ?
  1885. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  1886. (struct audio_buf_info __user *) arg);
  1887. case SNDCTL_DSP_NONBLOCK:
  1888. return snd_pcm_oss_nonblock(file);
  1889. case SNDCTL_DSP_GETCAPS:
  1890. res = snd_pcm_oss_get_caps(pcm_oss_file);
  1891. if (res < 0)
  1892. return res;
  1893. return put_user(res, p);
  1894. case SNDCTL_DSP_GETTRIGGER:
  1895. res = snd_pcm_oss_get_trigger(pcm_oss_file);
  1896. if (res < 0)
  1897. return res;
  1898. return put_user(res, p);
  1899. case SNDCTL_DSP_SETTRIGGER:
  1900. if (get_user(res, p))
  1901. return -EFAULT;
  1902. return snd_pcm_oss_set_trigger(pcm_oss_file, res);
  1903. case SNDCTL_DSP_GETIPTR:
  1904. case SNDCTL_DSP_GETOPTR:
  1905. return snd_pcm_oss_get_ptr(pcm_oss_file,
  1906. cmd == SNDCTL_DSP_GETIPTR ?
  1907. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  1908. (struct count_info __user *) arg);
  1909. case SNDCTL_DSP_MAPINBUF:
  1910. case SNDCTL_DSP_MAPOUTBUF:
  1911. return snd_pcm_oss_get_mapbuf(pcm_oss_file,
  1912. cmd == SNDCTL_DSP_MAPINBUF ?
  1913. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  1914. (struct buffmem_desc __user *) arg);
  1915. case SNDCTL_DSP_SETSYNCRO:
  1916. /* stop DMA now.. */
  1917. return 0;
  1918. case SNDCTL_DSP_SETDUPLEX:
  1919. if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
  1920. return 0;
  1921. return -EIO;
  1922. case SNDCTL_DSP_GETODELAY:
  1923. res = snd_pcm_oss_get_odelay(pcm_oss_file);
  1924. if (res < 0) {
  1925. /* it's for sure, some broken apps don't check for error codes */
  1926. put_user(0, p);
  1927. return res;
  1928. }
  1929. return put_user(res, p);
  1930. case SNDCTL_DSP_PROFILE:
  1931. return 0; /* silently ignore */
  1932. default:
  1933. snd_printd("pcm_oss: unknown command = 0x%x\n", cmd);
  1934. }
  1935. return -EINVAL;
  1936. }
  1937. #ifdef CONFIG_COMPAT
  1938. /* all compatible */
  1939. #define snd_pcm_oss_ioctl_compat snd_pcm_oss_ioctl
  1940. #else
  1941. #define snd_pcm_oss_ioctl_compat NULL
  1942. #endif
  1943. static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  1944. {
  1945. snd_pcm_oss_file_t *pcm_oss_file;
  1946. snd_pcm_substream_t *substream;
  1947. pcm_oss_file = file->private_data;
  1948. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1949. if (substream == NULL)
  1950. return -ENXIO;
  1951. #ifndef OSS_DEBUG
  1952. return snd_pcm_oss_read1(substream, buf, count);
  1953. #else
  1954. {
  1955. ssize_t res = snd_pcm_oss_read1(substream, buf, count);
  1956. printk("pcm_oss: read %li bytes (returned %li bytes)\n", (long)count, (long)res);
  1957. return res;
  1958. }
  1959. #endif
  1960. }
  1961. static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
  1962. {
  1963. snd_pcm_oss_file_t *pcm_oss_file;
  1964. snd_pcm_substream_t *substream;
  1965. long result;
  1966. pcm_oss_file = file->private_data;
  1967. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1968. if (substream == NULL)
  1969. return -ENXIO;
  1970. up(&file->f_dentry->d_inode->i_sem);
  1971. result = snd_pcm_oss_write1(substream, buf, count);
  1972. down(&file->f_dentry->d_inode->i_sem);
  1973. #ifdef OSS_DEBUG
  1974. printk("pcm_oss: write %li bytes (wrote %li bytes)\n", (long)count, (long)result);
  1975. #endif
  1976. return result;
  1977. }
  1978. static int snd_pcm_oss_playback_ready(snd_pcm_substream_t *substream)
  1979. {
  1980. snd_pcm_runtime_t *runtime = substream->runtime;
  1981. if (atomic_read(&runtime->mmap_count))
  1982. return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
  1983. else
  1984. return snd_pcm_playback_avail(runtime) >= runtime->oss.period_frames;
  1985. }
  1986. static int snd_pcm_oss_capture_ready(snd_pcm_substream_t *substream)
  1987. {
  1988. snd_pcm_runtime_t *runtime = substream->runtime;
  1989. if (atomic_read(&runtime->mmap_count))
  1990. return runtime->oss.prev_hw_ptr_interrupt != runtime->hw_ptr_interrupt;
  1991. else
  1992. return snd_pcm_capture_avail(runtime) >= runtime->oss.period_frames;
  1993. }
  1994. static unsigned int snd_pcm_oss_poll(struct file *file, poll_table * wait)
  1995. {
  1996. snd_pcm_oss_file_t *pcm_oss_file;
  1997. unsigned int mask;
  1998. snd_pcm_substream_t *psubstream = NULL, *csubstream = NULL;
  1999. pcm_oss_file = file->private_data;
  2000. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2001. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2002. mask = 0;
  2003. if (psubstream != NULL) {
  2004. snd_pcm_runtime_t *runtime = psubstream->runtime;
  2005. poll_wait(file, &runtime->sleep, wait);
  2006. snd_pcm_stream_lock_irq(psubstream);
  2007. if (runtime->status->state != SNDRV_PCM_STATE_DRAINING &&
  2008. (runtime->status->state != SNDRV_PCM_STATE_RUNNING ||
  2009. snd_pcm_oss_playback_ready(psubstream)))
  2010. mask |= POLLOUT | POLLWRNORM;
  2011. snd_pcm_stream_unlock_irq(psubstream);
  2012. }
  2013. if (csubstream != NULL) {
  2014. snd_pcm_runtime_t *runtime = csubstream->runtime;
  2015. enum sndrv_pcm_state ostate;
  2016. poll_wait(file, &runtime->sleep, wait);
  2017. snd_pcm_stream_lock_irq(csubstream);
  2018. if ((ostate = runtime->status->state) != SNDRV_PCM_STATE_RUNNING ||
  2019. snd_pcm_oss_capture_ready(csubstream))
  2020. mask |= POLLIN | POLLRDNORM;
  2021. snd_pcm_stream_unlock_irq(csubstream);
  2022. if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
  2023. snd_pcm_oss_file_t ofile;
  2024. memset(&ofile, 0, sizeof(ofile));
  2025. ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2026. runtime->oss.trigger = 0;
  2027. snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
  2028. }
  2029. }
  2030. return mask;
  2031. }
  2032. static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
  2033. {
  2034. snd_pcm_oss_file_t *pcm_oss_file;
  2035. snd_pcm_substream_t *substream = NULL;
  2036. snd_pcm_runtime_t *runtime;
  2037. int err;
  2038. #ifdef OSS_DEBUG
  2039. printk("pcm_oss: mmap begin\n");
  2040. #endif
  2041. pcm_oss_file = file->private_data;
  2042. switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
  2043. case VM_READ | VM_WRITE:
  2044. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2045. if (substream)
  2046. break;
  2047. /* Fall through */
  2048. case VM_READ:
  2049. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2050. break;
  2051. case VM_WRITE:
  2052. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2053. break;
  2054. default:
  2055. return -EINVAL;
  2056. }
  2057. /* set VM_READ access as well to fix memset() routines that do
  2058. reads before writes (to improve performance) */
  2059. area->vm_flags |= VM_READ;
  2060. if (substream == NULL)
  2061. return -ENXIO;
  2062. runtime = substream->runtime;
  2063. if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
  2064. return -EIO;
  2065. if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
  2066. runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
  2067. else
  2068. return -EIO;
  2069. if (runtime->oss.params) {
  2070. if ((err = snd_pcm_oss_change_params(substream)) < 0)
  2071. return err;
  2072. }
  2073. if (runtime->oss.plugin_first != NULL)
  2074. return -EIO;
  2075. if (area->vm_pgoff != 0)
  2076. return -EINVAL;
  2077. err = snd_pcm_mmap_data(substream, file, area);
  2078. if (err < 0)
  2079. return err;
  2080. runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
  2081. runtime->silence_threshold = 0;
  2082. runtime->silence_size = 0;
  2083. #ifdef OSS_DEBUG
  2084. printk("pcm_oss: mmap ok, bytes = 0x%x\n", runtime->oss.mmap_bytes);
  2085. #endif
  2086. /* In mmap mode we never stop */
  2087. runtime->stop_threshold = runtime->boundary;
  2088. return 0;
  2089. }
  2090. /*
  2091. * /proc interface
  2092. */
  2093. static void snd_pcm_oss_proc_read(snd_info_entry_t *entry,
  2094. snd_info_buffer_t * buffer)
  2095. {
  2096. snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
  2097. snd_pcm_oss_setup_t *setup = pstr->oss.setup_list;
  2098. down(&pstr->oss.setup_mutex);
  2099. while (setup) {
  2100. snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
  2101. setup->task_name,
  2102. setup->periods,
  2103. setup->period_size,
  2104. setup->disable ? " disable" : "",
  2105. setup->direct ? " direct" : "",
  2106. setup->block ? " block" : "",
  2107. setup->nonblock ? " non-block" : "",
  2108. setup->partialfrag ? " partial-frag" : "",
  2109. setup->nosilence ? " no-silence" : "");
  2110. setup = setup->next;
  2111. }
  2112. up(&pstr->oss.setup_mutex);
  2113. }
  2114. static void snd_pcm_oss_proc_free_setup_list(snd_pcm_str_t * pstr)
  2115. {
  2116. unsigned int idx;
  2117. snd_pcm_substream_t *substream;
  2118. snd_pcm_oss_setup_t *setup, *setupn;
  2119. for (idx = 0, substream = pstr->substream;
  2120. idx < pstr->substream_count; idx++, substream = substream->next)
  2121. substream->oss.setup = NULL;
  2122. for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
  2123. setup; setup = setupn) {
  2124. setupn = setup->next;
  2125. kfree(setup->task_name);
  2126. kfree(setup);
  2127. }
  2128. pstr->oss.setup_list = NULL;
  2129. }
  2130. static void snd_pcm_oss_proc_write(snd_info_entry_t *entry,
  2131. snd_info_buffer_t * buffer)
  2132. {
  2133. snd_pcm_str_t *pstr = (snd_pcm_str_t *)entry->private_data;
  2134. char line[128], str[32], task_name[32], *ptr;
  2135. int idx1;
  2136. snd_pcm_oss_setup_t *setup, *setup1, template;
  2137. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  2138. down(&pstr->oss.setup_mutex);
  2139. memset(&template, 0, sizeof(template));
  2140. ptr = snd_info_get_str(task_name, line, sizeof(task_name));
  2141. if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
  2142. snd_pcm_oss_proc_free_setup_list(pstr);
  2143. up(&pstr->oss.setup_mutex);
  2144. continue;
  2145. }
  2146. for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
  2147. if (!strcmp(setup->task_name, task_name)) {
  2148. template = *setup;
  2149. break;
  2150. }
  2151. }
  2152. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2153. template.periods = simple_strtoul(str, NULL, 10);
  2154. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2155. template.period_size = simple_strtoul(str, NULL, 10);
  2156. for (idx1 = 31; idx1 >= 0; idx1--)
  2157. if (template.period_size & (1 << idx1))
  2158. break;
  2159. for (idx1--; idx1 >= 0; idx1--)
  2160. template.period_size &= ~(1 << idx1);
  2161. do {
  2162. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2163. if (!strcmp(str, "disable")) {
  2164. template.disable = 1;
  2165. } else if (!strcmp(str, "direct")) {
  2166. template.direct = 1;
  2167. } else if (!strcmp(str, "block")) {
  2168. template.block = 1;
  2169. } else if (!strcmp(str, "non-block")) {
  2170. template.nonblock = 1;
  2171. } else if (!strcmp(str, "partial-frag")) {
  2172. template.partialfrag = 1;
  2173. } else if (!strcmp(str, "no-silence")) {
  2174. template.nosilence = 1;
  2175. } else if (!strcmp(str, "buggy-ptr")) {
  2176. template.buggyptr = 1;
  2177. }
  2178. } while (*str);
  2179. if (setup == NULL) {
  2180. setup = (snd_pcm_oss_setup_t *) kmalloc(sizeof(snd_pcm_oss_setup_t), GFP_KERNEL);
  2181. if (setup) {
  2182. if (pstr->oss.setup_list == NULL) {
  2183. pstr->oss.setup_list = setup;
  2184. } else {
  2185. for (setup1 = pstr->oss.setup_list; setup1->next; setup1 = setup1->next);
  2186. setup1->next = setup;
  2187. }
  2188. template.task_name = kstrdup(task_name, GFP_KERNEL);
  2189. } else {
  2190. buffer->error = -ENOMEM;
  2191. }
  2192. }
  2193. if (setup)
  2194. *setup = template;
  2195. up(&pstr->oss.setup_mutex);
  2196. }
  2197. }
  2198. static void snd_pcm_oss_proc_init(snd_pcm_t *pcm)
  2199. {
  2200. int stream;
  2201. for (stream = 0; stream < 2; ++stream) {
  2202. snd_info_entry_t *entry;
  2203. snd_pcm_str_t *pstr = &pcm->streams[stream];
  2204. if (pstr->substream_count == 0)
  2205. continue;
  2206. if ((entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root)) != NULL) {
  2207. entry->content = SNDRV_INFO_CONTENT_TEXT;
  2208. entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
  2209. entry->c.text.read_size = 8192;
  2210. entry->c.text.read = snd_pcm_oss_proc_read;
  2211. entry->c.text.write_size = 8192;
  2212. entry->c.text.write = snd_pcm_oss_proc_write;
  2213. entry->private_data = pstr;
  2214. if (snd_info_register(entry) < 0) {
  2215. snd_info_free_entry(entry);
  2216. entry = NULL;
  2217. }
  2218. }
  2219. pstr->oss.proc_entry = entry;
  2220. }
  2221. }
  2222. static void snd_pcm_oss_proc_done(snd_pcm_t *pcm)
  2223. {
  2224. int stream;
  2225. for (stream = 0; stream < 2; ++stream) {
  2226. snd_pcm_str_t *pstr = &pcm->streams[stream];
  2227. if (pstr->oss.proc_entry) {
  2228. snd_info_unregister(pstr->oss.proc_entry);
  2229. pstr->oss.proc_entry = NULL;
  2230. snd_pcm_oss_proc_free_setup_list(pstr);
  2231. }
  2232. }
  2233. }
  2234. /*
  2235. * ENTRY functions
  2236. */
  2237. static struct file_operations snd_pcm_oss_f_reg =
  2238. {
  2239. .owner = THIS_MODULE,
  2240. .read = snd_pcm_oss_read,
  2241. .write = snd_pcm_oss_write,
  2242. .open = snd_pcm_oss_open,
  2243. .release = snd_pcm_oss_release,
  2244. .poll = snd_pcm_oss_poll,
  2245. .unlocked_ioctl = snd_pcm_oss_ioctl,
  2246. .compat_ioctl = snd_pcm_oss_ioctl_compat,
  2247. .mmap = snd_pcm_oss_mmap,
  2248. };
  2249. static snd_minor_t snd_pcm_oss_reg =
  2250. {
  2251. .comment = "digital audio",
  2252. .f_ops = &snd_pcm_oss_f_reg,
  2253. };
  2254. static void register_oss_dsp(snd_pcm_t *pcm, int index)
  2255. {
  2256. char name[128];
  2257. sprintf(name, "dsp%i%i", pcm->card->number, pcm->device);
  2258. if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2259. pcm->card, index, &snd_pcm_oss_reg,
  2260. name) < 0) {
  2261. snd_printk("unable to register OSS PCM device %i:%i\n", pcm->card->number, pcm->device);
  2262. }
  2263. }
  2264. static int snd_pcm_oss_register_minor(snd_pcm_t * pcm)
  2265. {
  2266. pcm->oss.reg = 0;
  2267. if (dsp_map[pcm->card->number] == (int)pcm->device) {
  2268. char name[128];
  2269. int duplex;
  2270. register_oss_dsp(pcm, 0);
  2271. duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
  2272. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
  2273. !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
  2274. sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
  2275. #ifdef SNDRV_OSS_INFO_DEV_AUDIO
  2276. snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
  2277. pcm->card->number,
  2278. name);
  2279. #endif
  2280. pcm->oss.reg++;
  2281. pcm->oss.reg_mask |= 1;
  2282. }
  2283. if (adsp_map[pcm->card->number] == (int)pcm->device) {
  2284. register_oss_dsp(pcm, 1);
  2285. pcm->oss.reg++;
  2286. pcm->oss.reg_mask |= 2;
  2287. }
  2288. if (pcm->oss.reg)
  2289. snd_pcm_oss_proc_init(pcm);
  2290. return 0;
  2291. }
  2292. static int snd_pcm_oss_disconnect_minor(snd_pcm_t * pcm)
  2293. {
  2294. if (pcm->oss.reg) {
  2295. if (pcm->oss.reg_mask & 1) {
  2296. pcm->oss.reg_mask &= ~1;
  2297. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2298. pcm->card, 0);
  2299. }
  2300. if (pcm->oss.reg_mask & 2) {
  2301. pcm->oss.reg_mask &= ~2;
  2302. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2303. pcm->card, 1);
  2304. }
  2305. }
  2306. return 0;
  2307. }
  2308. static int snd_pcm_oss_unregister_minor(snd_pcm_t * pcm)
  2309. {
  2310. snd_pcm_oss_disconnect_minor(pcm);
  2311. if (pcm->oss.reg) {
  2312. if (dsp_map[pcm->card->number] == (int)pcm->device) {
  2313. #ifdef SNDRV_OSS_INFO_DEV_AUDIO
  2314. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
  2315. #endif
  2316. }
  2317. pcm->oss.reg = 0;
  2318. snd_pcm_oss_proc_done(pcm);
  2319. }
  2320. return 0;
  2321. }
  2322. static snd_pcm_notify_t snd_pcm_oss_notify =
  2323. {
  2324. .n_register = snd_pcm_oss_register_minor,
  2325. .n_disconnect = snd_pcm_oss_disconnect_minor,
  2326. .n_unregister = snd_pcm_oss_unregister_minor,
  2327. };
  2328. static int __init alsa_pcm_oss_init(void)
  2329. {
  2330. int i;
  2331. int err;
  2332. /* check device map table */
  2333. for (i = 0; i < SNDRV_CARDS; i++) {
  2334. if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
  2335. snd_printk("invalid dsp_map[%d] = %d\n", i, dsp_map[i]);
  2336. dsp_map[i] = 0;
  2337. }
  2338. if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
  2339. snd_printk("invalid adsp_map[%d] = %d\n", i, adsp_map[i]);
  2340. adsp_map[i] = 1;
  2341. }
  2342. }
  2343. if ((err = snd_pcm_notify(&snd_pcm_oss_notify, 0)) < 0)
  2344. return err;
  2345. return 0;
  2346. }
  2347. static void __exit alsa_pcm_oss_exit(void)
  2348. {
  2349. snd_pcm_notify(&snd_pcm_oss_notify, 1);
  2350. }
  2351. module_init(alsa_pcm_oss_init)
  2352. module_exit(alsa_pcm_oss_exit)