control_compat.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * compat ioctls for control API
  3. *
  4. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  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. /* this file included from control.c */
  21. #include <linux/compat.h>
  22. #include <linux/slab.h>
  23. struct snd_ctl_elem_list32 {
  24. u32 offset;
  25. u32 space;
  26. u32 used;
  27. u32 count;
  28. u32 pids;
  29. unsigned char reserved[50];
  30. } /* don't set packed attribute here */;
  31. static int snd_ctl_elem_list_compat(struct snd_card *card,
  32. struct snd_ctl_elem_list32 __user *data32)
  33. {
  34. struct snd_ctl_elem_list __user *data;
  35. compat_caddr_t ptr;
  36. int err;
  37. data = compat_alloc_user_space(sizeof(*data));
  38. /* offset, space, used, count */
  39. if (copy_in_user(data, data32, 4 * sizeof(u32)))
  40. return -EFAULT;
  41. /* pids */
  42. if (get_user(ptr, &data32->pids) ||
  43. put_user(compat_ptr(ptr), &data->pids))
  44. return -EFAULT;
  45. err = snd_ctl_elem_list(card, data);
  46. if (err < 0)
  47. return err;
  48. /* copy the result */
  49. if (copy_in_user(data32, data, 4 * sizeof(u32)))
  50. return -EFAULT;
  51. return 0;
  52. }
  53. /*
  54. * control element info
  55. * it uses union, so the things are not easy..
  56. */
  57. struct snd_ctl_elem_info32 {
  58. struct snd_ctl_elem_id id; // the size of struct is same
  59. s32 type;
  60. u32 access;
  61. u32 count;
  62. s32 owner;
  63. union {
  64. struct {
  65. s32 min;
  66. s32 max;
  67. s32 step;
  68. } integer;
  69. struct {
  70. u64 min;
  71. u64 max;
  72. u64 step;
  73. } integer64;
  74. struct {
  75. u32 items;
  76. u32 item;
  77. char name[64];
  78. } enumerated;
  79. unsigned char reserved[128];
  80. } value;
  81. unsigned char reserved[64];
  82. } __attribute__((packed));
  83. static int snd_ctl_elem_info_compat(struct snd_ctl_file *ctl,
  84. struct snd_ctl_elem_info32 __user *data32)
  85. {
  86. struct snd_ctl_elem_info *data;
  87. int err;
  88. data = kzalloc(sizeof(*data), GFP_KERNEL);
  89. if (! data)
  90. return -ENOMEM;
  91. err = -EFAULT;
  92. /* copy id */
  93. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  94. goto error;
  95. /* we need to copy the item index.
  96. * hope this doesn't break anything..
  97. */
  98. if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
  99. goto error;
  100. snd_power_lock(ctl->card);
  101. err = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
  102. if (err >= 0)
  103. err = snd_ctl_elem_info(ctl, data);
  104. snd_power_unlock(ctl->card);
  105. if (err < 0)
  106. goto error;
  107. /* restore info to 32bit */
  108. err = -EFAULT;
  109. /* id, type, access, count */
  110. if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
  111. copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
  112. goto error;
  113. if (put_user(data->owner, &data32->owner))
  114. goto error;
  115. switch (data->type) {
  116. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  117. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  118. if (put_user(data->value.integer.min, &data32->value.integer.min) ||
  119. put_user(data->value.integer.max, &data32->value.integer.max) ||
  120. put_user(data->value.integer.step, &data32->value.integer.step))
  121. goto error;
  122. break;
  123. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  124. if (copy_to_user(&data32->value.integer64,
  125. &data->value.integer64,
  126. sizeof(data->value.integer64)))
  127. goto error;
  128. break;
  129. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  130. if (copy_to_user(&data32->value.enumerated,
  131. &data->value.enumerated,
  132. sizeof(data->value.enumerated)))
  133. goto error;
  134. break;
  135. default:
  136. break;
  137. }
  138. err = 0;
  139. error:
  140. kfree(data);
  141. return err;
  142. }
  143. /* read / write */
  144. struct snd_ctl_elem_value32 {
  145. struct snd_ctl_elem_id id;
  146. unsigned int indirect; /* bit-field causes misalignment */
  147. union {
  148. s32 integer[128];
  149. unsigned char data[512];
  150. #ifndef CONFIG_X86_64
  151. s64 integer64[64];
  152. #endif
  153. } value;
  154. unsigned char reserved[128];
  155. };
  156. /* get the value type and count of the control */
  157. static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
  158. int *countp)
  159. {
  160. struct snd_kcontrol *kctl;
  161. struct snd_ctl_elem_info *info;
  162. int err;
  163. down_read(&card->controls_rwsem);
  164. kctl = snd_ctl_find_id(card, id);
  165. if (! kctl) {
  166. up_read(&card->controls_rwsem);
  167. return -ENXIO;
  168. }
  169. info = kzalloc(sizeof(*info), GFP_KERNEL);
  170. if (info == NULL) {
  171. up_read(&card->controls_rwsem);
  172. return -ENOMEM;
  173. }
  174. info->id = *id;
  175. err = kctl->info(kctl, info);
  176. up_read(&card->controls_rwsem);
  177. if (err >= 0) {
  178. err = info->type;
  179. *countp = info->count;
  180. }
  181. kfree(info);
  182. return err;
  183. }
  184. static int get_elem_size(int type, int count)
  185. {
  186. switch (type) {
  187. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  188. return sizeof(s64) * count;
  189. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  190. return sizeof(int) * count;
  191. case SNDRV_CTL_ELEM_TYPE_BYTES:
  192. return 512;
  193. case SNDRV_CTL_ELEM_TYPE_IEC958:
  194. return sizeof(struct snd_aes_iec958);
  195. default:
  196. return -1;
  197. }
  198. }
  199. static int copy_ctl_value_from_user(struct snd_card *card,
  200. struct snd_ctl_elem_value *data,
  201. struct snd_ctl_elem_value32 __user *data32,
  202. int *typep, int *countp)
  203. {
  204. int i, type, size;
  205. int uninitialized_var(count);
  206. unsigned int indirect;
  207. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  208. return -EFAULT;
  209. if (get_user(indirect, &data32->indirect))
  210. return -EFAULT;
  211. if (indirect)
  212. return -EINVAL;
  213. type = get_ctl_type(card, &data->id, &count);
  214. if (type < 0)
  215. return type;
  216. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  217. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  218. for (i = 0; i < count; i++) {
  219. int val;
  220. if (get_user(val, &data32->value.integer[i]))
  221. return -EFAULT;
  222. data->value.integer.value[i] = val;
  223. }
  224. } else {
  225. size = get_elem_size(type, count);
  226. if (size < 0) {
  227. printk(KERN_ERR "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
  228. return -EINVAL;
  229. }
  230. if (copy_from_user(data->value.bytes.data,
  231. data32->value.data, size))
  232. return -EFAULT;
  233. }
  234. *typep = type;
  235. *countp = count;
  236. return 0;
  237. }
  238. /* restore the value to 32bit */
  239. static int copy_ctl_value_to_user(struct snd_ctl_elem_value32 __user *data32,
  240. struct snd_ctl_elem_value *data,
  241. int type, int count)
  242. {
  243. int i, size;
  244. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  245. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  246. for (i = 0; i < count; i++) {
  247. int val;
  248. val = data->value.integer.value[i];
  249. if (put_user(val, &data32->value.integer[i]))
  250. return -EFAULT;
  251. }
  252. } else {
  253. size = get_elem_size(type, count);
  254. if (copy_to_user(data32->value.data,
  255. data->value.bytes.data, size))
  256. return -EFAULT;
  257. }
  258. return 0;
  259. }
  260. static int snd_ctl_elem_read_user_compat(struct snd_card *card,
  261. struct snd_ctl_elem_value32 __user *data32)
  262. {
  263. struct snd_ctl_elem_value *data;
  264. int err, type, count;
  265. data = kzalloc(sizeof(*data), GFP_KERNEL);
  266. if (data == NULL)
  267. return -ENOMEM;
  268. if ((err = copy_ctl_value_from_user(card, data, data32, &type, &count)) < 0)
  269. goto error;
  270. snd_power_lock(card);
  271. err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  272. if (err >= 0)
  273. err = snd_ctl_elem_read(card, data);
  274. snd_power_unlock(card);
  275. if (err >= 0)
  276. err = copy_ctl_value_to_user(data32, data, type, count);
  277. error:
  278. kfree(data);
  279. return err;
  280. }
  281. static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
  282. struct snd_ctl_elem_value32 __user *data32)
  283. {
  284. struct snd_ctl_elem_value *data;
  285. struct snd_card *card = file->card;
  286. int err, type, count;
  287. data = kzalloc(sizeof(*data), GFP_KERNEL);
  288. if (data == NULL)
  289. return -ENOMEM;
  290. if ((err = copy_ctl_value_from_user(card, data, data32, &type, &count)) < 0)
  291. goto error;
  292. snd_power_lock(card);
  293. err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  294. if (err >= 0)
  295. err = snd_ctl_elem_write(card, file, data);
  296. snd_power_unlock(card);
  297. if (err >= 0)
  298. err = copy_ctl_value_to_user(data32, data, type, count);
  299. error:
  300. kfree(data);
  301. return err;
  302. }
  303. /* add or replace a user control */
  304. static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
  305. struct snd_ctl_elem_info32 __user *data32,
  306. int replace)
  307. {
  308. struct snd_ctl_elem_info *data;
  309. int err;
  310. data = kzalloc(sizeof(*data), GFP_KERNEL);
  311. if (! data)
  312. return -ENOMEM;
  313. err = -EFAULT;
  314. /* id, type, access, count */ \
  315. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
  316. copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
  317. goto error;
  318. if (get_user(data->owner, &data32->owner) ||
  319. get_user(data->type, &data32->type))
  320. goto error;
  321. switch (data->type) {
  322. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  323. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  324. if (get_user(data->value.integer.min, &data32->value.integer.min) ||
  325. get_user(data->value.integer.max, &data32->value.integer.max) ||
  326. get_user(data->value.integer.step, &data32->value.integer.step))
  327. goto error;
  328. break;
  329. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  330. if (copy_from_user(&data->value.integer64,
  331. &data32->value.integer64,
  332. sizeof(data->value.integer64)))
  333. goto error;
  334. break;
  335. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  336. if (copy_from_user(&data->value.enumerated,
  337. &data32->value.enumerated,
  338. sizeof(data->value.enumerated)))
  339. goto error;
  340. break;
  341. default:
  342. break;
  343. }
  344. err = snd_ctl_elem_add(file, data, replace);
  345. error:
  346. kfree(data);
  347. return err;
  348. }
  349. enum {
  350. SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
  351. SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
  352. SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
  353. SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
  354. SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
  355. SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
  356. };
  357. static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  358. {
  359. struct snd_ctl_file *ctl;
  360. struct snd_kctl_ioctl *p;
  361. void __user *argp = compat_ptr(arg);
  362. int err;
  363. ctl = file->private_data;
  364. if (snd_BUG_ON(!ctl || !ctl->card))
  365. return -ENXIO;
  366. switch (cmd) {
  367. case SNDRV_CTL_IOCTL_PVERSION:
  368. case SNDRV_CTL_IOCTL_CARD_INFO:
  369. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  370. case SNDRV_CTL_IOCTL_POWER:
  371. case SNDRV_CTL_IOCTL_POWER_STATE:
  372. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  373. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  374. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  375. case SNDRV_CTL_IOCTL_TLV_READ:
  376. case SNDRV_CTL_IOCTL_TLV_WRITE:
  377. case SNDRV_CTL_IOCTL_TLV_COMMAND:
  378. return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
  379. case SNDRV_CTL_IOCTL_ELEM_LIST32:
  380. return snd_ctl_elem_list_compat(ctl->card, argp);
  381. case SNDRV_CTL_IOCTL_ELEM_INFO32:
  382. return snd_ctl_elem_info_compat(ctl, argp);
  383. case SNDRV_CTL_IOCTL_ELEM_READ32:
  384. return snd_ctl_elem_read_user_compat(ctl->card, argp);
  385. case SNDRV_CTL_IOCTL_ELEM_WRITE32:
  386. return snd_ctl_elem_write_user_compat(ctl, argp);
  387. case SNDRV_CTL_IOCTL_ELEM_ADD32:
  388. return snd_ctl_elem_add_compat(ctl, argp, 0);
  389. case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
  390. return snd_ctl_elem_add_compat(ctl, argp, 1);
  391. }
  392. down_read(&snd_ioctl_rwsem);
  393. list_for_each_entry(p, &snd_control_compat_ioctls, list) {
  394. if (p->fioctl) {
  395. err = p->fioctl(ctl->card, ctl, cmd, arg);
  396. if (err != -ENOIOCTLCMD) {
  397. up_read(&snd_ioctl_rwsem);
  398. return err;
  399. }
  400. }
  401. }
  402. up_read(&snd_ioctl_rwsem);
  403. return -ENOIOCTLCMD;
  404. }