pcm_oss.c 72 KB

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