control_compat.c 11 KB

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