rawmidi.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. /*
  2. * Abstract layer for MIDI v1.0 stream
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.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. #include <sound/core.h>
  22. #include <linux/major.h>
  23. #include <linux/init.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/time.h>
  27. #include <linux/wait.h>
  28. #include <linux/mutex.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/delay.h>
  31. #include <sound/rawmidi.h>
  32. #include <sound/info.h>
  33. #include <sound/control.h>
  34. #include <sound/minors.h>
  35. #include <sound/initval.h>
  36. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  37. MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
  38. MODULE_LICENSE("GPL");
  39. #ifdef CONFIG_SND_OSSEMUL
  40. static int midi_map[SNDRV_CARDS];
  41. static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
  42. module_param_array(midi_map, int, NULL, 0444);
  43. MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
  44. module_param_array(amidi_map, int, NULL, 0444);
  45. MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
  46. #endif /* CONFIG_SND_OSSEMUL */
  47. static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
  48. static int snd_rawmidi_dev_free(struct snd_device *device);
  49. static int snd_rawmidi_dev_register(struct snd_device *device);
  50. static int snd_rawmidi_dev_disconnect(struct snd_device *device);
  51. static LIST_HEAD(snd_rawmidi_devices);
  52. static DEFINE_MUTEX(register_mutex);
  53. static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
  54. {
  55. struct snd_rawmidi *rawmidi;
  56. list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
  57. if (rawmidi->card == card && rawmidi->device == device)
  58. return rawmidi;
  59. return NULL;
  60. }
  61. static inline unsigned short snd_rawmidi_file_flags(struct file *file)
  62. {
  63. switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
  64. case FMODE_WRITE:
  65. return SNDRV_RAWMIDI_LFLG_OUTPUT;
  66. case FMODE_READ:
  67. return SNDRV_RAWMIDI_LFLG_INPUT;
  68. default:
  69. return SNDRV_RAWMIDI_LFLG_OPEN;
  70. }
  71. }
  72. static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
  73. {
  74. struct snd_rawmidi_runtime *runtime = substream->runtime;
  75. return runtime->avail >= runtime->avail_min;
  76. }
  77. static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
  78. size_t count)
  79. {
  80. struct snd_rawmidi_runtime *runtime = substream->runtime;
  81. return runtime->avail >= runtime->avail_min &&
  82. (!substream->append || runtime->avail >= count);
  83. }
  84. static void snd_rawmidi_input_event_tasklet(unsigned long data)
  85. {
  86. struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
  87. substream->runtime->event(substream);
  88. }
  89. static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
  90. {
  91. struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
  92. substream->ops->trigger(substream, 1);
  93. }
  94. static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
  95. {
  96. struct snd_rawmidi_runtime *runtime;
  97. if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
  98. return -ENOMEM;
  99. spin_lock_init(&runtime->lock);
  100. init_waitqueue_head(&runtime->sleep);
  101. if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
  102. tasklet_init(&runtime->tasklet,
  103. snd_rawmidi_input_event_tasklet,
  104. (unsigned long)substream);
  105. else
  106. tasklet_init(&runtime->tasklet,
  107. snd_rawmidi_output_trigger_tasklet,
  108. (unsigned long)substream);
  109. runtime->event = NULL;
  110. runtime->buffer_size = PAGE_SIZE;
  111. runtime->avail_min = 1;
  112. if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
  113. runtime->avail = 0;
  114. else
  115. runtime->avail = runtime->buffer_size;
  116. if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
  117. kfree(runtime);
  118. return -ENOMEM;
  119. }
  120. runtime->appl_ptr = runtime->hw_ptr = 0;
  121. substream->runtime = runtime;
  122. return 0;
  123. }
  124. static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
  125. {
  126. struct snd_rawmidi_runtime *runtime = substream->runtime;
  127. kfree(runtime->buffer);
  128. kfree(runtime);
  129. substream->runtime = NULL;
  130. return 0;
  131. }
  132. static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
  133. {
  134. if (!substream->opened)
  135. return;
  136. if (up) {
  137. tasklet_schedule(&substream->runtime->tasklet);
  138. } else {
  139. tasklet_kill(&substream->runtime->tasklet);
  140. substream->ops->trigger(substream, 0);
  141. }
  142. }
  143. static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  144. {
  145. if (!substream->opened)
  146. return;
  147. substream->ops->trigger(substream, up);
  148. if (!up && substream->runtime->event)
  149. tasklet_kill(&substream->runtime->tasklet);
  150. }
  151. int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
  152. {
  153. unsigned long flags;
  154. struct snd_rawmidi_runtime *runtime = substream->runtime;
  155. snd_rawmidi_output_trigger(substream, 0);
  156. runtime->drain = 0;
  157. spin_lock_irqsave(&runtime->lock, flags);
  158. runtime->appl_ptr = runtime->hw_ptr = 0;
  159. runtime->avail = runtime->buffer_size;
  160. spin_unlock_irqrestore(&runtime->lock, flags);
  161. return 0;
  162. }
  163. int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
  164. {
  165. int err;
  166. long timeout;
  167. struct snd_rawmidi_runtime *runtime = substream->runtime;
  168. err = 0;
  169. runtime->drain = 1;
  170. timeout = wait_event_interruptible_timeout(runtime->sleep,
  171. (runtime->avail >= runtime->buffer_size),
  172. 10*HZ);
  173. if (signal_pending(current))
  174. err = -ERESTARTSYS;
  175. if (runtime->avail < runtime->buffer_size && !timeout) {
  176. snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
  177. err = -EIO;
  178. }
  179. runtime->drain = 0;
  180. if (err != -ERESTARTSYS) {
  181. /* we need wait a while to make sure that Tx FIFOs are empty */
  182. if (substream->ops->drain)
  183. substream->ops->drain(substream);
  184. else
  185. msleep(50);
  186. snd_rawmidi_drop_output(substream);
  187. }
  188. return err;
  189. }
  190. int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
  191. {
  192. unsigned long flags;
  193. struct snd_rawmidi_runtime *runtime = substream->runtime;
  194. snd_rawmidi_input_trigger(substream, 0);
  195. runtime->drain = 0;
  196. spin_lock_irqsave(&runtime->lock, flags);
  197. runtime->appl_ptr = runtime->hw_ptr = 0;
  198. runtime->avail = 0;
  199. spin_unlock_irqrestore(&runtime->lock, flags);
  200. return 0;
  201. }
  202. /* look for an available substream for the given stream direction;
  203. * if a specific subdevice is given, try to assign it
  204. */
  205. static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
  206. int stream, int mode,
  207. struct snd_rawmidi_substream **sub_ret)
  208. {
  209. struct snd_rawmidi_substream *substream;
  210. struct snd_rawmidi_str *s = &rmidi->streams[stream];
  211. static unsigned int info_flags[2] = {
  212. [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
  213. [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
  214. };
  215. if (!(rmidi->info_flags & info_flags[stream]))
  216. return -ENXIO;
  217. if (subdevice >= 0 && subdevice >= s->substream_count)
  218. return -ENODEV;
  219. if (s->substream_opened >= s->substream_count)
  220. return -EAGAIN;
  221. list_for_each_entry(substream, &s->substreams, list) {
  222. if (substream->opened) {
  223. if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
  224. !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
  225. !substream->append)
  226. continue;
  227. }
  228. if (subdevice < 0 || subdevice == substream->number) {
  229. *sub_ret = substream;
  230. return 0;
  231. }
  232. }
  233. return -EAGAIN;
  234. }
  235. /* open and do ref-counting for the given substream */
  236. static int open_substream(struct snd_rawmidi *rmidi,
  237. struct snd_rawmidi_substream *substream,
  238. int mode)
  239. {
  240. int err;
  241. if (substream->use_count == 0) {
  242. err = snd_rawmidi_runtime_create(substream);
  243. if (err < 0)
  244. return err;
  245. err = substream->ops->open(substream);
  246. if (err < 0) {
  247. snd_rawmidi_runtime_free(substream);
  248. return err;
  249. }
  250. substream->opened = 1;
  251. substream->active_sensing = 0;
  252. if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
  253. substream->append = 1;
  254. }
  255. substream->use_count++;
  256. rmidi->streams[substream->stream].substream_opened++;
  257. return 0;
  258. }
  259. static void close_substream(struct snd_rawmidi *rmidi,
  260. struct snd_rawmidi_substream *substream,
  261. int cleanup);
  262. static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
  263. struct snd_rawmidi_file *rfile)
  264. {
  265. struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
  266. int err;
  267. rfile->input = rfile->output = NULL;
  268. if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
  269. err = assign_substream(rmidi, subdevice,
  270. SNDRV_RAWMIDI_STREAM_INPUT,
  271. mode, &sinput);
  272. if (err < 0)
  273. return err;
  274. }
  275. if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
  276. err = assign_substream(rmidi, subdevice,
  277. SNDRV_RAWMIDI_STREAM_OUTPUT,
  278. mode, &soutput);
  279. if (err < 0)
  280. return err;
  281. }
  282. if (sinput) {
  283. err = open_substream(rmidi, sinput, mode);
  284. if (err < 0)
  285. return err;
  286. }
  287. if (soutput) {
  288. err = open_substream(rmidi, soutput, mode);
  289. if (err < 0) {
  290. if (sinput)
  291. close_substream(rmidi, sinput, 0);
  292. return err;
  293. }
  294. }
  295. rfile->rmidi = rmidi;
  296. rfile->input = sinput;
  297. rfile->output = soutput;
  298. return 0;
  299. }
  300. /* called from sound/core/seq/seq_midi.c */
  301. int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
  302. int mode, struct snd_rawmidi_file * rfile)
  303. {
  304. struct snd_rawmidi *rmidi;
  305. int err;
  306. if (snd_BUG_ON(!rfile))
  307. return -EINVAL;
  308. mutex_lock(&register_mutex);
  309. rmidi = snd_rawmidi_search(card, device);
  310. if (rmidi == NULL) {
  311. mutex_unlock(&register_mutex);
  312. return -ENODEV;
  313. }
  314. if (!try_module_get(rmidi->card->module)) {
  315. mutex_unlock(&register_mutex);
  316. return -ENXIO;
  317. }
  318. mutex_unlock(&register_mutex);
  319. mutex_lock(&rmidi->open_mutex);
  320. err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
  321. mutex_unlock(&rmidi->open_mutex);
  322. if (err < 0)
  323. module_put(rmidi->card->module);
  324. return err;
  325. }
  326. static int snd_rawmidi_open(struct inode *inode, struct file *file)
  327. {
  328. int maj = imajor(inode);
  329. struct snd_card *card;
  330. int subdevice;
  331. unsigned short fflags;
  332. int err;
  333. struct snd_rawmidi *rmidi;
  334. struct snd_rawmidi_file *rawmidi_file = NULL;
  335. wait_queue_t wait;
  336. struct snd_ctl_file *kctl;
  337. if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
  338. return -EINVAL; /* invalid combination */
  339. if (maj == snd_major) {
  340. rmidi = snd_lookup_minor_data(iminor(inode),
  341. SNDRV_DEVICE_TYPE_RAWMIDI);
  342. #ifdef CONFIG_SND_OSSEMUL
  343. } else if (maj == SOUND_MAJOR) {
  344. rmidi = snd_lookup_oss_minor_data(iminor(inode),
  345. SNDRV_OSS_DEVICE_TYPE_MIDI);
  346. #endif
  347. } else
  348. return -ENXIO;
  349. if (rmidi == NULL)
  350. return -ENODEV;
  351. if (!try_module_get(rmidi->card->module))
  352. return -ENXIO;
  353. mutex_lock(&rmidi->open_mutex);
  354. card = rmidi->card;
  355. err = snd_card_file_add(card, file);
  356. if (err < 0)
  357. goto __error_card;
  358. fflags = snd_rawmidi_file_flags(file);
  359. if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
  360. fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
  361. rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
  362. if (rawmidi_file == NULL) {
  363. err = -ENOMEM;
  364. goto __error;
  365. }
  366. init_waitqueue_entry(&wait, current);
  367. add_wait_queue(&rmidi->open_wait, &wait);
  368. while (1) {
  369. subdevice = -1;
  370. read_lock(&card->ctl_files_rwlock);
  371. list_for_each_entry(kctl, &card->ctl_files, list) {
  372. if (kctl->pid == current->pid) {
  373. subdevice = kctl->prefer_rawmidi_subdevice;
  374. if (subdevice != -1)
  375. break;
  376. }
  377. }
  378. read_unlock(&card->ctl_files_rwlock);
  379. err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
  380. if (err >= 0)
  381. break;
  382. if (err == -EAGAIN) {
  383. if (file->f_flags & O_NONBLOCK) {
  384. err = -EBUSY;
  385. break;
  386. }
  387. } else
  388. break;
  389. set_current_state(TASK_INTERRUPTIBLE);
  390. mutex_unlock(&rmidi->open_mutex);
  391. schedule();
  392. mutex_lock(&rmidi->open_mutex);
  393. if (signal_pending(current)) {
  394. err = -ERESTARTSYS;
  395. break;
  396. }
  397. }
  398. remove_wait_queue(&rmidi->open_wait, &wait);
  399. if (err < 0) {
  400. kfree(rawmidi_file);
  401. goto __error;
  402. }
  403. #ifdef CONFIG_SND_OSSEMUL
  404. if (rawmidi_file->input && rawmidi_file->input->runtime)
  405. rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
  406. if (rawmidi_file->output && rawmidi_file->output->runtime)
  407. rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
  408. #endif
  409. file->private_data = rawmidi_file;
  410. mutex_unlock(&rmidi->open_mutex);
  411. return 0;
  412. __error:
  413. snd_card_file_remove(card, file);
  414. __error_card:
  415. mutex_unlock(&rmidi->open_mutex);
  416. module_put(rmidi->card->module);
  417. return err;
  418. }
  419. static void close_substream(struct snd_rawmidi *rmidi,
  420. struct snd_rawmidi_substream *substream,
  421. int cleanup)
  422. {
  423. rmidi->streams[substream->stream].substream_opened--;
  424. if (--substream->use_count)
  425. return;
  426. if (cleanup) {
  427. if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
  428. snd_rawmidi_input_trigger(substream, 0);
  429. else {
  430. if (substream->active_sensing) {
  431. unsigned char buf = 0xfe;
  432. /* sending single active sensing message
  433. * to shut the device up
  434. */
  435. snd_rawmidi_kernel_write(substream, &buf, 1);
  436. }
  437. if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
  438. snd_rawmidi_output_trigger(substream, 0);
  439. }
  440. }
  441. substream->ops->close(substream);
  442. if (substream->runtime->private_free)
  443. substream->runtime->private_free(substream);
  444. snd_rawmidi_runtime_free(substream);
  445. substream->opened = 0;
  446. substream->append = 0;
  447. }
  448. static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
  449. {
  450. struct snd_rawmidi *rmidi;
  451. rmidi = rfile->rmidi;
  452. mutex_lock(&rmidi->open_mutex);
  453. if (rfile->input) {
  454. close_substream(rmidi, rfile->input, 1);
  455. rfile->input = NULL;
  456. }
  457. if (rfile->output) {
  458. close_substream(rmidi, rfile->output, 1);
  459. rfile->output = NULL;
  460. }
  461. rfile->rmidi = NULL;
  462. mutex_unlock(&rmidi->open_mutex);
  463. wake_up(&rmidi->open_wait);
  464. }
  465. /* called from sound/core/seq/seq_midi.c */
  466. int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
  467. {
  468. struct snd_rawmidi *rmidi;
  469. if (snd_BUG_ON(!rfile))
  470. return -ENXIO;
  471. rmidi = rfile->rmidi;
  472. rawmidi_release_priv(rfile);
  473. module_put(rmidi->card->module);
  474. return 0;
  475. }
  476. static int snd_rawmidi_release(struct inode *inode, struct file *file)
  477. {
  478. struct snd_rawmidi_file *rfile;
  479. struct snd_rawmidi *rmidi;
  480. rfile = file->private_data;
  481. rmidi = rfile->rmidi;
  482. rawmidi_release_priv(rfile);
  483. kfree(rfile);
  484. snd_card_file_remove(rmidi->card, file);
  485. module_put(rmidi->card->module);
  486. return 0;
  487. }
  488. static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
  489. struct snd_rawmidi_info *info)
  490. {
  491. struct snd_rawmidi *rmidi;
  492. if (substream == NULL)
  493. return -ENODEV;
  494. rmidi = substream->rmidi;
  495. memset(info, 0, sizeof(*info));
  496. info->card = rmidi->card->number;
  497. info->device = rmidi->device;
  498. info->subdevice = substream->number;
  499. info->stream = substream->stream;
  500. info->flags = rmidi->info_flags;
  501. strcpy(info->id, rmidi->id);
  502. strcpy(info->name, rmidi->name);
  503. strcpy(info->subname, substream->name);
  504. info->subdevices_count = substream->pstr->substream_count;
  505. info->subdevices_avail = (substream->pstr->substream_count -
  506. substream->pstr->substream_opened);
  507. return 0;
  508. }
  509. static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
  510. struct snd_rawmidi_info __user * _info)
  511. {
  512. struct snd_rawmidi_info info;
  513. int err;
  514. if ((err = snd_rawmidi_info(substream, &info)) < 0)
  515. return err;
  516. if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
  517. return -EFAULT;
  518. return 0;
  519. }
  520. int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
  521. {
  522. struct snd_rawmidi *rmidi;
  523. struct snd_rawmidi_str *pstr;
  524. struct snd_rawmidi_substream *substream;
  525. mutex_lock(&register_mutex);
  526. rmidi = snd_rawmidi_search(card, info->device);
  527. mutex_unlock(&register_mutex);
  528. if (!rmidi)
  529. return -ENXIO;
  530. if (info->stream < 0 || info->stream > 1)
  531. return -EINVAL;
  532. pstr = &rmidi->streams[info->stream];
  533. if (pstr->substream_count == 0)
  534. return -ENOENT;
  535. if (info->subdevice >= pstr->substream_count)
  536. return -ENXIO;
  537. list_for_each_entry(substream, &pstr->substreams, list) {
  538. if ((unsigned int)substream->number == info->subdevice)
  539. return snd_rawmidi_info(substream, info);
  540. }
  541. return -ENXIO;
  542. }
  543. static int snd_rawmidi_info_select_user(struct snd_card *card,
  544. struct snd_rawmidi_info __user *_info)
  545. {
  546. int err;
  547. struct snd_rawmidi_info info;
  548. if (get_user(info.device, &_info->device))
  549. return -EFAULT;
  550. if (get_user(info.stream, &_info->stream))
  551. return -EFAULT;
  552. if (get_user(info.subdevice, &_info->subdevice))
  553. return -EFAULT;
  554. if ((err = snd_rawmidi_info_select(card, &info)) < 0)
  555. return err;
  556. if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
  557. return -EFAULT;
  558. return 0;
  559. }
  560. int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
  561. struct snd_rawmidi_params * params)
  562. {
  563. char *newbuf;
  564. struct snd_rawmidi_runtime *runtime = substream->runtime;
  565. if (substream->append && substream->use_count > 1)
  566. return -EBUSY;
  567. snd_rawmidi_drain_output(substream);
  568. if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
  569. return -EINVAL;
  570. }
  571. if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
  572. return -EINVAL;
  573. }
  574. if (params->buffer_size != runtime->buffer_size) {
  575. newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
  576. if (!newbuf)
  577. return -ENOMEM;
  578. kfree(runtime->buffer);
  579. runtime->buffer = newbuf;
  580. runtime->buffer_size = params->buffer_size;
  581. runtime->avail = runtime->buffer_size;
  582. }
  583. runtime->avail_min = params->avail_min;
  584. substream->active_sensing = !params->no_active_sensing;
  585. return 0;
  586. }
  587. int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
  588. struct snd_rawmidi_params * params)
  589. {
  590. char *newbuf;
  591. struct snd_rawmidi_runtime *runtime = substream->runtime;
  592. snd_rawmidi_drain_input(substream);
  593. if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
  594. return -EINVAL;
  595. }
  596. if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
  597. return -EINVAL;
  598. }
  599. if (params->buffer_size != runtime->buffer_size) {
  600. newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
  601. if (!newbuf)
  602. return -ENOMEM;
  603. kfree(runtime->buffer);
  604. runtime->buffer = newbuf;
  605. runtime->buffer_size = params->buffer_size;
  606. }
  607. runtime->avail_min = params->avail_min;
  608. return 0;
  609. }
  610. static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
  611. struct snd_rawmidi_status * status)
  612. {
  613. struct snd_rawmidi_runtime *runtime = substream->runtime;
  614. memset(status, 0, sizeof(*status));
  615. status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
  616. spin_lock_irq(&runtime->lock);
  617. status->avail = runtime->avail;
  618. spin_unlock_irq(&runtime->lock);
  619. return 0;
  620. }
  621. static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
  622. struct snd_rawmidi_status * status)
  623. {
  624. struct snd_rawmidi_runtime *runtime = substream->runtime;
  625. memset(status, 0, sizeof(*status));
  626. status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
  627. spin_lock_irq(&runtime->lock);
  628. status->avail = runtime->avail;
  629. status->xruns = runtime->xruns;
  630. runtime->xruns = 0;
  631. spin_unlock_irq(&runtime->lock);
  632. return 0;
  633. }
  634. static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  635. {
  636. struct snd_rawmidi_file *rfile;
  637. void __user *argp = (void __user *)arg;
  638. rfile = file->private_data;
  639. if (((cmd >> 8) & 0xff) != 'W')
  640. return -ENOTTY;
  641. switch (cmd) {
  642. case SNDRV_RAWMIDI_IOCTL_PVERSION:
  643. return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
  644. case SNDRV_RAWMIDI_IOCTL_INFO:
  645. {
  646. int stream;
  647. struct snd_rawmidi_info __user *info = argp;
  648. if (get_user(stream, &info->stream))
  649. return -EFAULT;
  650. switch (stream) {
  651. case SNDRV_RAWMIDI_STREAM_INPUT:
  652. return snd_rawmidi_info_user(rfile->input, info);
  653. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  654. return snd_rawmidi_info_user(rfile->output, info);
  655. default:
  656. return -EINVAL;
  657. }
  658. }
  659. case SNDRV_RAWMIDI_IOCTL_PARAMS:
  660. {
  661. struct snd_rawmidi_params params;
  662. if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
  663. return -EFAULT;
  664. switch (params.stream) {
  665. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  666. if (rfile->output == NULL)
  667. return -EINVAL;
  668. return snd_rawmidi_output_params(rfile->output, &params);
  669. case SNDRV_RAWMIDI_STREAM_INPUT:
  670. if (rfile->input == NULL)
  671. return -EINVAL;
  672. return snd_rawmidi_input_params(rfile->input, &params);
  673. default:
  674. return -EINVAL;
  675. }
  676. }
  677. case SNDRV_RAWMIDI_IOCTL_STATUS:
  678. {
  679. int err = 0;
  680. struct snd_rawmidi_status status;
  681. if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
  682. return -EFAULT;
  683. switch (status.stream) {
  684. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  685. if (rfile->output == NULL)
  686. return -EINVAL;
  687. err = snd_rawmidi_output_status(rfile->output, &status);
  688. break;
  689. case SNDRV_RAWMIDI_STREAM_INPUT:
  690. if (rfile->input == NULL)
  691. return -EINVAL;
  692. err = snd_rawmidi_input_status(rfile->input, &status);
  693. break;
  694. default:
  695. return -EINVAL;
  696. }
  697. if (err < 0)
  698. return err;
  699. if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
  700. return -EFAULT;
  701. return 0;
  702. }
  703. case SNDRV_RAWMIDI_IOCTL_DROP:
  704. {
  705. int val;
  706. if (get_user(val, (int __user *) argp))
  707. return -EFAULT;
  708. switch (val) {
  709. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  710. if (rfile->output == NULL)
  711. return -EINVAL;
  712. return snd_rawmidi_drop_output(rfile->output);
  713. default:
  714. return -EINVAL;
  715. }
  716. }
  717. case SNDRV_RAWMIDI_IOCTL_DRAIN:
  718. {
  719. int val;
  720. if (get_user(val, (int __user *) argp))
  721. return -EFAULT;
  722. switch (val) {
  723. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  724. if (rfile->output == NULL)
  725. return -EINVAL;
  726. return snd_rawmidi_drain_output(rfile->output);
  727. case SNDRV_RAWMIDI_STREAM_INPUT:
  728. if (rfile->input == NULL)
  729. return -EINVAL;
  730. return snd_rawmidi_drain_input(rfile->input);
  731. default:
  732. return -EINVAL;
  733. }
  734. }
  735. #ifdef CONFIG_SND_DEBUG
  736. default:
  737. snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
  738. #endif
  739. }
  740. return -ENOTTY;
  741. }
  742. static int snd_rawmidi_control_ioctl(struct snd_card *card,
  743. struct snd_ctl_file *control,
  744. unsigned int cmd,
  745. unsigned long arg)
  746. {
  747. void __user *argp = (void __user *)arg;
  748. switch (cmd) {
  749. case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
  750. {
  751. int device;
  752. if (get_user(device, (int __user *)argp))
  753. return -EFAULT;
  754. mutex_lock(&register_mutex);
  755. device = device < 0 ? 0 : device + 1;
  756. while (device < SNDRV_RAWMIDI_DEVICES) {
  757. if (snd_rawmidi_search(card, device))
  758. break;
  759. device++;
  760. }
  761. if (device == SNDRV_RAWMIDI_DEVICES)
  762. device = -1;
  763. mutex_unlock(&register_mutex);
  764. if (put_user(device, (int __user *)argp))
  765. return -EFAULT;
  766. return 0;
  767. }
  768. case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
  769. {
  770. int val;
  771. if (get_user(val, (int __user *)argp))
  772. return -EFAULT;
  773. control->prefer_rawmidi_subdevice = val;
  774. return 0;
  775. }
  776. case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
  777. return snd_rawmidi_info_select_user(card, argp);
  778. }
  779. return -ENOIOCTLCMD;
  780. }
  781. /**
  782. * snd_rawmidi_receive - receive the input data from the device
  783. * @substream: the rawmidi substream
  784. * @buffer: the buffer pointer
  785. * @count: the data size to read
  786. *
  787. * Reads the data from the internal buffer.
  788. *
  789. * Returns the size of read data, or a negative error code on failure.
  790. */
  791. int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
  792. const unsigned char *buffer, int count)
  793. {
  794. unsigned long flags;
  795. int result = 0, count1;
  796. struct snd_rawmidi_runtime *runtime = substream->runtime;
  797. if (!substream->opened)
  798. return -EBADFD;
  799. if (runtime->buffer == NULL) {
  800. snd_printd("snd_rawmidi_receive: input is not active!!!\n");
  801. return -EINVAL;
  802. }
  803. spin_lock_irqsave(&runtime->lock, flags);
  804. if (count == 1) { /* special case, faster code */
  805. substream->bytes++;
  806. if (runtime->avail < runtime->buffer_size) {
  807. runtime->buffer[runtime->hw_ptr++] = buffer[0];
  808. runtime->hw_ptr %= runtime->buffer_size;
  809. runtime->avail++;
  810. result++;
  811. } else {
  812. runtime->xruns++;
  813. }
  814. } else {
  815. substream->bytes += count;
  816. count1 = runtime->buffer_size - runtime->hw_ptr;
  817. if (count1 > count)
  818. count1 = count;
  819. if (count1 > (int)(runtime->buffer_size - runtime->avail))
  820. count1 = runtime->buffer_size - runtime->avail;
  821. memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
  822. runtime->hw_ptr += count1;
  823. runtime->hw_ptr %= runtime->buffer_size;
  824. runtime->avail += count1;
  825. count -= count1;
  826. result += count1;
  827. if (count > 0) {
  828. buffer += count1;
  829. count1 = count;
  830. if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
  831. count1 = runtime->buffer_size - runtime->avail;
  832. runtime->xruns += count - count1;
  833. }
  834. if (count1 > 0) {
  835. memcpy(runtime->buffer, buffer, count1);
  836. runtime->hw_ptr = count1;
  837. runtime->avail += count1;
  838. result += count1;
  839. }
  840. }
  841. }
  842. if (result > 0) {
  843. if (runtime->event)
  844. tasklet_schedule(&runtime->tasklet);
  845. else if (snd_rawmidi_ready(substream))
  846. wake_up(&runtime->sleep);
  847. }
  848. spin_unlock_irqrestore(&runtime->lock, flags);
  849. return result;
  850. }
  851. static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
  852. unsigned char __user *userbuf,
  853. unsigned char *kernelbuf, long count)
  854. {
  855. unsigned long flags;
  856. long result = 0, count1;
  857. struct snd_rawmidi_runtime *runtime = substream->runtime;
  858. while (count > 0 && runtime->avail) {
  859. count1 = runtime->buffer_size - runtime->appl_ptr;
  860. if (count1 > count)
  861. count1 = count;
  862. spin_lock_irqsave(&runtime->lock, flags);
  863. if (count1 > (int)runtime->avail)
  864. count1 = runtime->avail;
  865. if (kernelbuf)
  866. memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
  867. if (userbuf) {
  868. spin_unlock_irqrestore(&runtime->lock, flags);
  869. if (copy_to_user(userbuf + result,
  870. runtime->buffer + runtime->appl_ptr, count1)) {
  871. return result > 0 ? result : -EFAULT;
  872. }
  873. spin_lock_irqsave(&runtime->lock, flags);
  874. }
  875. runtime->appl_ptr += count1;
  876. runtime->appl_ptr %= runtime->buffer_size;
  877. runtime->avail -= count1;
  878. spin_unlock_irqrestore(&runtime->lock, flags);
  879. result += count1;
  880. count -= count1;
  881. }
  882. return result;
  883. }
  884. long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
  885. unsigned char *buf, long count)
  886. {
  887. snd_rawmidi_input_trigger(substream, 1);
  888. return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
  889. }
  890. static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
  891. loff_t *offset)
  892. {
  893. long result;
  894. int count1;
  895. struct snd_rawmidi_file *rfile;
  896. struct snd_rawmidi_substream *substream;
  897. struct snd_rawmidi_runtime *runtime;
  898. rfile = file->private_data;
  899. substream = rfile->input;
  900. if (substream == NULL)
  901. return -EIO;
  902. runtime = substream->runtime;
  903. snd_rawmidi_input_trigger(substream, 1);
  904. result = 0;
  905. while (count > 0) {
  906. spin_lock_irq(&runtime->lock);
  907. while (!snd_rawmidi_ready(substream)) {
  908. wait_queue_t wait;
  909. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  910. spin_unlock_irq(&runtime->lock);
  911. return result > 0 ? result : -EAGAIN;
  912. }
  913. init_waitqueue_entry(&wait, current);
  914. add_wait_queue(&runtime->sleep, &wait);
  915. set_current_state(TASK_INTERRUPTIBLE);
  916. spin_unlock_irq(&runtime->lock);
  917. schedule();
  918. remove_wait_queue(&runtime->sleep, &wait);
  919. if (signal_pending(current))
  920. return result > 0 ? result : -ERESTARTSYS;
  921. if (!runtime->avail)
  922. return result > 0 ? result : -EIO;
  923. spin_lock_irq(&runtime->lock);
  924. }
  925. spin_unlock_irq(&runtime->lock);
  926. count1 = snd_rawmidi_kernel_read1(substream,
  927. (unsigned char __user *)buf,
  928. NULL/*kernelbuf*/,
  929. count);
  930. if (count1 < 0)
  931. return result > 0 ? result : count1;
  932. result += count1;
  933. buf += count1;
  934. count -= count1;
  935. }
  936. return result;
  937. }
  938. /**
  939. * snd_rawmidi_transmit_empty - check whether the output buffer is empty
  940. * @substream: the rawmidi substream
  941. *
  942. * Returns 1 if the internal output buffer is empty, 0 if not.
  943. */
  944. int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
  945. {
  946. struct snd_rawmidi_runtime *runtime = substream->runtime;
  947. int result;
  948. unsigned long flags;
  949. if (runtime->buffer == NULL) {
  950. snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
  951. return 1;
  952. }
  953. spin_lock_irqsave(&runtime->lock, flags);
  954. result = runtime->avail >= runtime->buffer_size;
  955. spin_unlock_irqrestore(&runtime->lock, flags);
  956. return result;
  957. }
  958. /**
  959. * snd_rawmidi_transmit_peek - copy data from the internal buffer
  960. * @substream: the rawmidi substream
  961. * @buffer: the buffer pointer
  962. * @count: data size to transfer
  963. *
  964. * Copies data from the internal output buffer to the given buffer.
  965. *
  966. * Call this in the interrupt handler when the midi output is ready,
  967. * and call snd_rawmidi_transmit_ack() after the transmission is
  968. * finished.
  969. *
  970. * Returns the size of copied data, or a negative error code on failure.
  971. */
  972. int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
  973. unsigned char *buffer, int count)
  974. {
  975. unsigned long flags;
  976. int result, count1;
  977. struct snd_rawmidi_runtime *runtime = substream->runtime;
  978. if (runtime->buffer == NULL) {
  979. snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
  980. return -EINVAL;
  981. }
  982. result = 0;
  983. spin_lock_irqsave(&runtime->lock, flags);
  984. if (runtime->avail >= runtime->buffer_size) {
  985. /* warning: lowlevel layer MUST trigger down the hardware */
  986. goto __skip;
  987. }
  988. if (count == 1) { /* special case, faster code */
  989. *buffer = runtime->buffer[runtime->hw_ptr];
  990. result++;
  991. } else {
  992. count1 = runtime->buffer_size - runtime->hw_ptr;
  993. if (count1 > count)
  994. count1 = count;
  995. if (count1 > (int)(runtime->buffer_size - runtime->avail))
  996. count1 = runtime->buffer_size - runtime->avail;
  997. memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
  998. count -= count1;
  999. result += count1;
  1000. if (count > 0) {
  1001. if (count > (int)(runtime->buffer_size - runtime->avail - count1))
  1002. count = runtime->buffer_size - runtime->avail - count1;
  1003. memcpy(buffer + count1, runtime->buffer, count);
  1004. result += count;
  1005. }
  1006. }
  1007. __skip:
  1008. spin_unlock_irqrestore(&runtime->lock, flags);
  1009. return result;
  1010. }
  1011. /**
  1012. * snd_rawmidi_transmit_ack - acknowledge the transmission
  1013. * @substream: the rawmidi substream
  1014. * @count: the tranferred count
  1015. *
  1016. * Advances the hardware pointer for the internal output buffer with
  1017. * the given size and updates the condition.
  1018. * Call after the transmission is finished.
  1019. *
  1020. * Returns the advanced size if successful, or a negative error code on failure.
  1021. */
  1022. int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
  1023. {
  1024. unsigned long flags;
  1025. struct snd_rawmidi_runtime *runtime = substream->runtime;
  1026. if (runtime->buffer == NULL) {
  1027. snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
  1028. return -EINVAL;
  1029. }
  1030. spin_lock_irqsave(&runtime->lock, flags);
  1031. snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
  1032. runtime->hw_ptr += count;
  1033. runtime->hw_ptr %= runtime->buffer_size;
  1034. runtime->avail += count;
  1035. substream->bytes += count;
  1036. if (count > 0) {
  1037. if (runtime->drain || snd_rawmidi_ready(substream))
  1038. wake_up(&runtime->sleep);
  1039. }
  1040. spin_unlock_irqrestore(&runtime->lock, flags);
  1041. return count;
  1042. }
  1043. /**
  1044. * snd_rawmidi_transmit - copy from the buffer to the device
  1045. * @substream: the rawmidi substream
  1046. * @buffer: the buffer pointer
  1047. * @count: the data size to transfer
  1048. *
  1049. * Copies data from the buffer to the device and advances the pointer.
  1050. *
  1051. * Returns the copied size if successful, or a negative error code on failure.
  1052. */
  1053. int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
  1054. unsigned char *buffer, int count)
  1055. {
  1056. if (!substream->opened)
  1057. return -EBADFD;
  1058. count = snd_rawmidi_transmit_peek(substream, buffer, count);
  1059. if (count < 0)
  1060. return count;
  1061. return snd_rawmidi_transmit_ack(substream, count);
  1062. }
  1063. static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
  1064. const unsigned char __user *userbuf,
  1065. const unsigned char *kernelbuf,
  1066. long count)
  1067. {
  1068. unsigned long flags;
  1069. long count1, result;
  1070. struct snd_rawmidi_runtime *runtime = substream->runtime;
  1071. if (snd_BUG_ON(!kernelbuf && !userbuf))
  1072. return -EINVAL;
  1073. if (snd_BUG_ON(!runtime->buffer))
  1074. return -EINVAL;
  1075. result = 0;
  1076. spin_lock_irqsave(&runtime->lock, flags);
  1077. if (substream->append) {
  1078. if ((long)runtime->avail < count) {
  1079. spin_unlock_irqrestore(&runtime->lock, flags);
  1080. return -EAGAIN;
  1081. }
  1082. }
  1083. while (count > 0 && runtime->avail > 0) {
  1084. count1 = runtime->buffer_size - runtime->appl_ptr;
  1085. if (count1 > count)
  1086. count1 = count;
  1087. if (count1 > (long)runtime->avail)
  1088. count1 = runtime->avail;
  1089. if (kernelbuf)
  1090. memcpy(runtime->buffer + runtime->appl_ptr,
  1091. kernelbuf + result, count1);
  1092. else if (userbuf) {
  1093. spin_unlock_irqrestore(&runtime->lock, flags);
  1094. if (copy_from_user(runtime->buffer + runtime->appl_ptr,
  1095. userbuf + result, count1)) {
  1096. spin_lock_irqsave(&runtime->lock, flags);
  1097. result = result > 0 ? result : -EFAULT;
  1098. goto __end;
  1099. }
  1100. spin_lock_irqsave(&runtime->lock, flags);
  1101. }
  1102. runtime->appl_ptr += count1;
  1103. runtime->appl_ptr %= runtime->buffer_size;
  1104. runtime->avail -= count1;
  1105. result += count1;
  1106. count -= count1;
  1107. }
  1108. __end:
  1109. count1 = runtime->avail < runtime->buffer_size;
  1110. spin_unlock_irqrestore(&runtime->lock, flags);
  1111. if (count1)
  1112. snd_rawmidi_output_trigger(substream, 1);
  1113. return result;
  1114. }
  1115. long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
  1116. const unsigned char *buf, long count)
  1117. {
  1118. return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
  1119. }
  1120. static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
  1121. size_t count, loff_t *offset)
  1122. {
  1123. long result, timeout;
  1124. int count1;
  1125. struct snd_rawmidi_file *rfile;
  1126. struct snd_rawmidi_runtime *runtime;
  1127. struct snd_rawmidi_substream *substream;
  1128. rfile = file->private_data;
  1129. substream = rfile->output;
  1130. runtime = substream->runtime;
  1131. /* we cannot put an atomic message to our buffer */
  1132. if (substream->append && count > runtime->buffer_size)
  1133. return -EIO;
  1134. result = 0;
  1135. while (count > 0) {
  1136. spin_lock_irq(&runtime->lock);
  1137. while (!snd_rawmidi_ready_append(substream, count)) {
  1138. wait_queue_t wait;
  1139. if (file->f_flags & O_NONBLOCK) {
  1140. spin_unlock_irq(&runtime->lock);
  1141. return result > 0 ? result : -EAGAIN;
  1142. }
  1143. init_waitqueue_entry(&wait, current);
  1144. add_wait_queue(&runtime->sleep, &wait);
  1145. set_current_state(TASK_INTERRUPTIBLE);
  1146. spin_unlock_irq(&runtime->lock);
  1147. timeout = schedule_timeout(30 * HZ);
  1148. remove_wait_queue(&runtime->sleep, &wait);
  1149. if (signal_pending(current))
  1150. return result > 0 ? result : -ERESTARTSYS;
  1151. if (!runtime->avail && !timeout)
  1152. return result > 0 ? result : -EIO;
  1153. spin_lock_irq(&runtime->lock);
  1154. }
  1155. spin_unlock_irq(&runtime->lock);
  1156. count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
  1157. if (count1 < 0)
  1158. return result > 0 ? result : count1;
  1159. result += count1;
  1160. buf += count1;
  1161. if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
  1162. break;
  1163. count -= count1;
  1164. }
  1165. if (file->f_flags & O_SYNC) {
  1166. spin_lock_irq(&runtime->lock);
  1167. while (runtime->avail != runtime->buffer_size) {
  1168. wait_queue_t wait;
  1169. unsigned int last_avail = runtime->avail;
  1170. init_waitqueue_entry(&wait, current);
  1171. add_wait_queue(&runtime->sleep, &wait);
  1172. set_current_state(TASK_INTERRUPTIBLE);
  1173. spin_unlock_irq(&runtime->lock);
  1174. timeout = schedule_timeout(30 * HZ);
  1175. remove_wait_queue(&runtime->sleep, &wait);
  1176. if (signal_pending(current))
  1177. return result > 0 ? result : -ERESTARTSYS;
  1178. if (runtime->avail == last_avail && !timeout)
  1179. return result > 0 ? result : -EIO;
  1180. spin_lock_irq(&runtime->lock);
  1181. }
  1182. spin_unlock_irq(&runtime->lock);
  1183. }
  1184. return result;
  1185. }
  1186. static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
  1187. {
  1188. struct snd_rawmidi_file *rfile;
  1189. struct snd_rawmidi_runtime *runtime;
  1190. unsigned int mask;
  1191. rfile = file->private_data;
  1192. if (rfile->input != NULL) {
  1193. runtime = rfile->input->runtime;
  1194. snd_rawmidi_input_trigger(rfile->input, 1);
  1195. poll_wait(file, &runtime->sleep, wait);
  1196. }
  1197. if (rfile->output != NULL) {
  1198. runtime = rfile->output->runtime;
  1199. poll_wait(file, &runtime->sleep, wait);
  1200. }
  1201. mask = 0;
  1202. if (rfile->input != NULL) {
  1203. if (snd_rawmidi_ready(rfile->input))
  1204. mask |= POLLIN | POLLRDNORM;
  1205. }
  1206. if (rfile->output != NULL) {
  1207. if (snd_rawmidi_ready(rfile->output))
  1208. mask |= POLLOUT | POLLWRNORM;
  1209. }
  1210. return mask;
  1211. }
  1212. /*
  1213. */
  1214. #ifdef CONFIG_COMPAT
  1215. #include "rawmidi_compat.c"
  1216. #else
  1217. #define snd_rawmidi_ioctl_compat NULL
  1218. #endif
  1219. /*
  1220. */
  1221. static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
  1222. struct snd_info_buffer *buffer)
  1223. {
  1224. struct snd_rawmidi *rmidi;
  1225. struct snd_rawmidi_substream *substream;
  1226. struct snd_rawmidi_runtime *runtime;
  1227. rmidi = entry->private_data;
  1228. snd_iprintf(buffer, "%s\n\n", rmidi->name);
  1229. mutex_lock(&rmidi->open_mutex);
  1230. if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
  1231. list_for_each_entry(substream,
  1232. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
  1233. list) {
  1234. snd_iprintf(buffer,
  1235. "Output %d\n"
  1236. " Tx bytes : %lu\n",
  1237. substream->number,
  1238. (unsigned long) substream->bytes);
  1239. if (substream->opened) {
  1240. runtime = substream->runtime;
  1241. snd_iprintf(buffer,
  1242. " Mode : %s\n"
  1243. " Buffer size : %lu\n"
  1244. " Avail : %lu\n",
  1245. runtime->oss ? "OSS compatible" : "native",
  1246. (unsigned long) runtime->buffer_size,
  1247. (unsigned long) runtime->avail);
  1248. }
  1249. }
  1250. }
  1251. if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
  1252. list_for_each_entry(substream,
  1253. &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
  1254. list) {
  1255. snd_iprintf(buffer,
  1256. "Input %d\n"
  1257. " Rx bytes : %lu\n",
  1258. substream->number,
  1259. (unsigned long) substream->bytes);
  1260. if (substream->opened) {
  1261. runtime = substream->runtime;
  1262. snd_iprintf(buffer,
  1263. " Buffer size : %lu\n"
  1264. " Avail : %lu\n"
  1265. " Overruns : %lu\n",
  1266. (unsigned long) runtime->buffer_size,
  1267. (unsigned long) runtime->avail,
  1268. (unsigned long) runtime->xruns);
  1269. }
  1270. }
  1271. }
  1272. mutex_unlock(&rmidi->open_mutex);
  1273. }
  1274. /*
  1275. * Register functions
  1276. */
  1277. static const struct file_operations snd_rawmidi_f_ops =
  1278. {
  1279. .owner = THIS_MODULE,
  1280. .read = snd_rawmidi_read,
  1281. .write = snd_rawmidi_write,
  1282. .open = snd_rawmidi_open,
  1283. .release = snd_rawmidi_release,
  1284. .poll = snd_rawmidi_poll,
  1285. .unlocked_ioctl = snd_rawmidi_ioctl,
  1286. .compat_ioctl = snd_rawmidi_ioctl_compat,
  1287. };
  1288. static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
  1289. struct snd_rawmidi_str *stream,
  1290. int direction,
  1291. int count)
  1292. {
  1293. struct snd_rawmidi_substream *substream;
  1294. int idx;
  1295. for (idx = 0; idx < count; idx++) {
  1296. substream = kzalloc(sizeof(*substream), GFP_KERNEL);
  1297. if (substream == NULL) {
  1298. snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
  1299. return -ENOMEM;
  1300. }
  1301. substream->stream = direction;
  1302. substream->number = idx;
  1303. substream->rmidi = rmidi;
  1304. substream->pstr = stream;
  1305. list_add_tail(&substream->list, &stream->substreams);
  1306. stream->substream_count++;
  1307. }
  1308. return 0;
  1309. }
  1310. /**
  1311. * snd_rawmidi_new - create a rawmidi instance
  1312. * @card: the card instance
  1313. * @id: the id string
  1314. * @device: the device index
  1315. * @output_count: the number of output streams
  1316. * @input_count: the number of input streams
  1317. * @rrawmidi: the pointer to store the new rawmidi instance
  1318. *
  1319. * Creates a new rawmidi instance.
  1320. * Use snd_rawmidi_set_ops() to set the operators to the new instance.
  1321. *
  1322. * Returns zero if successful, or a negative error code on failure.
  1323. */
  1324. int snd_rawmidi_new(struct snd_card *card, char *id, int device,
  1325. int output_count, int input_count,
  1326. struct snd_rawmidi ** rrawmidi)
  1327. {
  1328. struct snd_rawmidi *rmidi;
  1329. int err;
  1330. static struct snd_device_ops ops = {
  1331. .dev_free = snd_rawmidi_dev_free,
  1332. .dev_register = snd_rawmidi_dev_register,
  1333. .dev_disconnect = snd_rawmidi_dev_disconnect,
  1334. };
  1335. if (snd_BUG_ON(!card))
  1336. return -ENXIO;
  1337. if (rrawmidi)
  1338. *rrawmidi = NULL;
  1339. rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
  1340. if (rmidi == NULL) {
  1341. snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
  1342. return -ENOMEM;
  1343. }
  1344. rmidi->card = card;
  1345. rmidi->device = device;
  1346. mutex_init(&rmidi->open_mutex);
  1347. init_waitqueue_head(&rmidi->open_wait);
  1348. INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
  1349. INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
  1350. if (id != NULL)
  1351. strlcpy(rmidi->id, id, sizeof(rmidi->id));
  1352. if ((err = snd_rawmidi_alloc_substreams(rmidi,
  1353. &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
  1354. SNDRV_RAWMIDI_STREAM_INPUT,
  1355. input_count)) < 0) {
  1356. snd_rawmidi_free(rmidi);
  1357. return err;
  1358. }
  1359. if ((err = snd_rawmidi_alloc_substreams(rmidi,
  1360. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
  1361. SNDRV_RAWMIDI_STREAM_OUTPUT,
  1362. output_count)) < 0) {
  1363. snd_rawmidi_free(rmidi);
  1364. return err;
  1365. }
  1366. if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
  1367. snd_rawmidi_free(rmidi);
  1368. return err;
  1369. }
  1370. if (rrawmidi)
  1371. *rrawmidi = rmidi;
  1372. return 0;
  1373. }
  1374. static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
  1375. {
  1376. struct snd_rawmidi_substream *substream;
  1377. while (!list_empty(&stream->substreams)) {
  1378. substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
  1379. list_del(&substream->list);
  1380. kfree(substream);
  1381. }
  1382. }
  1383. static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
  1384. {
  1385. if (!rmidi)
  1386. return 0;
  1387. snd_info_free_entry(rmidi->proc_entry);
  1388. rmidi->proc_entry = NULL;
  1389. mutex_lock(&register_mutex);
  1390. if (rmidi->ops && rmidi->ops->dev_unregister)
  1391. rmidi->ops->dev_unregister(rmidi);
  1392. mutex_unlock(&register_mutex);
  1393. snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
  1394. snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
  1395. if (rmidi->private_free)
  1396. rmidi->private_free(rmidi);
  1397. kfree(rmidi);
  1398. return 0;
  1399. }
  1400. static int snd_rawmidi_dev_free(struct snd_device *device)
  1401. {
  1402. struct snd_rawmidi *rmidi = device->device_data;
  1403. return snd_rawmidi_free(rmidi);
  1404. }
  1405. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  1406. static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
  1407. {
  1408. struct snd_rawmidi *rmidi = device->private_data;
  1409. rmidi->seq_dev = NULL;
  1410. }
  1411. #endif
  1412. static int snd_rawmidi_dev_register(struct snd_device *device)
  1413. {
  1414. int err;
  1415. struct snd_info_entry *entry;
  1416. char name[16];
  1417. struct snd_rawmidi *rmidi = device->device_data;
  1418. if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
  1419. return -ENOMEM;
  1420. mutex_lock(&register_mutex);
  1421. if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
  1422. mutex_unlock(&register_mutex);
  1423. return -EBUSY;
  1424. }
  1425. list_add_tail(&rmidi->list, &snd_rawmidi_devices);
  1426. sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
  1427. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
  1428. rmidi->card, rmidi->device,
  1429. &snd_rawmidi_f_ops, rmidi, name)) < 0) {
  1430. snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
  1431. list_del(&rmidi->list);
  1432. mutex_unlock(&register_mutex);
  1433. return err;
  1434. }
  1435. if (rmidi->ops && rmidi->ops->dev_register &&
  1436. (err = rmidi->ops->dev_register(rmidi)) < 0) {
  1437. snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
  1438. list_del(&rmidi->list);
  1439. mutex_unlock(&register_mutex);
  1440. return err;
  1441. }
  1442. #ifdef CONFIG_SND_OSSEMUL
  1443. rmidi->ossreg = 0;
  1444. if ((int)rmidi->device == midi_map[rmidi->card->number]) {
  1445. if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
  1446. rmidi->card, 0, &snd_rawmidi_f_ops,
  1447. rmidi, name) < 0) {
  1448. snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
  1449. } else {
  1450. rmidi->ossreg++;
  1451. #ifdef SNDRV_OSS_INFO_DEV_MIDI
  1452. snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
  1453. #endif
  1454. }
  1455. }
  1456. if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
  1457. if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
  1458. rmidi->card, 1, &snd_rawmidi_f_ops,
  1459. rmidi, name) < 0) {
  1460. snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
  1461. } else {
  1462. rmidi->ossreg++;
  1463. }
  1464. }
  1465. #endif /* CONFIG_SND_OSSEMUL */
  1466. mutex_unlock(&register_mutex);
  1467. sprintf(name, "midi%d", rmidi->device);
  1468. entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
  1469. if (entry) {
  1470. entry->private_data = rmidi;
  1471. entry->c.text.read = snd_rawmidi_proc_info_read;
  1472. if (snd_info_register(entry) < 0) {
  1473. snd_info_free_entry(entry);
  1474. entry = NULL;
  1475. }
  1476. }
  1477. rmidi->proc_entry = entry;
  1478. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  1479. if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
  1480. if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
  1481. rmidi->seq_dev->private_data = rmidi;
  1482. rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
  1483. sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
  1484. snd_device_register(rmidi->card, rmidi->seq_dev);
  1485. }
  1486. }
  1487. #endif
  1488. return 0;
  1489. }
  1490. static int snd_rawmidi_dev_disconnect(struct snd_device *device)
  1491. {
  1492. struct snd_rawmidi *rmidi = device->device_data;
  1493. mutex_lock(&register_mutex);
  1494. list_del_init(&rmidi->list);
  1495. #ifdef CONFIG_SND_OSSEMUL
  1496. if (rmidi->ossreg) {
  1497. if ((int)rmidi->device == midi_map[rmidi->card->number]) {
  1498. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
  1499. #ifdef SNDRV_OSS_INFO_DEV_MIDI
  1500. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
  1501. #endif
  1502. }
  1503. if ((int)rmidi->device == amidi_map[rmidi->card->number])
  1504. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
  1505. rmidi->ossreg = 0;
  1506. }
  1507. #endif /* CONFIG_SND_OSSEMUL */
  1508. snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
  1509. mutex_unlock(&register_mutex);
  1510. return 0;
  1511. }
  1512. /**
  1513. * snd_rawmidi_set_ops - set the rawmidi operators
  1514. * @rmidi: the rawmidi instance
  1515. * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
  1516. * @ops: the operator table
  1517. *
  1518. * Sets the rawmidi operators for the given stream direction.
  1519. */
  1520. void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
  1521. struct snd_rawmidi_ops *ops)
  1522. {
  1523. struct snd_rawmidi_substream *substream;
  1524. list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
  1525. substream->ops = ops;
  1526. }
  1527. /*
  1528. * ENTRY functions
  1529. */
  1530. static int __init alsa_rawmidi_init(void)
  1531. {
  1532. snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
  1533. snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
  1534. #ifdef CONFIG_SND_OSSEMUL
  1535. { int i;
  1536. /* check device map table */
  1537. for (i = 0; i < SNDRV_CARDS; i++) {
  1538. if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
  1539. snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
  1540. midi_map[i] = 0;
  1541. }
  1542. if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
  1543. snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
  1544. amidi_map[i] = 1;
  1545. }
  1546. }
  1547. }
  1548. #endif /* CONFIG_SND_OSSEMUL */
  1549. return 0;
  1550. }
  1551. static void __exit alsa_rawmidi_exit(void)
  1552. {
  1553. snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
  1554. snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
  1555. }
  1556. module_init(alsa_rawmidi_init)
  1557. module_exit(alsa_rawmidi_exit)
  1558. EXPORT_SYMBOL(snd_rawmidi_output_params);
  1559. EXPORT_SYMBOL(snd_rawmidi_input_params);
  1560. EXPORT_SYMBOL(snd_rawmidi_drop_output);
  1561. EXPORT_SYMBOL(snd_rawmidi_drain_output);
  1562. EXPORT_SYMBOL(snd_rawmidi_drain_input);
  1563. EXPORT_SYMBOL(snd_rawmidi_receive);
  1564. EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
  1565. EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
  1566. EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
  1567. EXPORT_SYMBOL(snd_rawmidi_transmit);
  1568. EXPORT_SYMBOL(snd_rawmidi_new);
  1569. EXPORT_SYMBOL(snd_rawmidi_set_ops);
  1570. EXPORT_SYMBOL(snd_rawmidi_info_select);
  1571. EXPORT_SYMBOL(snd_rawmidi_kernel_open);
  1572. EXPORT_SYMBOL(snd_rawmidi_kernel_release);
  1573. EXPORT_SYMBOL(snd_rawmidi_kernel_read);
  1574. EXPORT_SYMBOL(snd_rawmidi_kernel_write);