pcm_oss.c 71 KB

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