pcm_oss.c 72 KB

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