control_compat.c 11 KB

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