control_compat.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * compat ioctls for control API
  4. *
  5. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  6. */
  7. /* this file included from control.c */
  8. #include <linux/compat.h>
  9. #include <linux/slab.h>
  10. struct snd_ctl_elem_list32 {
  11. u32 offset;
  12. u32 space;
  13. u32 used;
  14. u32 count;
  15. u32 pids;
  16. unsigned char reserved[50];
  17. } /* don't set packed attribute here */;
  18. static int snd_ctl_elem_list_compat(struct snd_card *card,
  19. struct snd_ctl_elem_list32 __user *data32)
  20. {
  21. struct snd_ctl_elem_list data = {};
  22. compat_caddr_t ptr;
  23. int err;
  24. /* offset, space, used, count */
  25. if (copy_from_user(&data, data32, 4 * sizeof(u32)))
  26. return -EFAULT;
  27. /* pids */
  28. if (get_user(ptr, &data32->pids))
  29. return -EFAULT;
  30. data.pids = compat_ptr(ptr);
  31. err = snd_ctl_elem_list(card, &data);
  32. if (err < 0)
  33. return err;
  34. /* copy the result */
  35. if (copy_to_user(data32, &data, 4 * sizeof(u32)))
  36. return -EFAULT;
  37. return 0;
  38. }
  39. /*
  40. * control element info
  41. * it uses union, so the things are not easy..
  42. */
  43. struct snd_ctl_elem_info32 {
  44. struct snd_ctl_elem_id id; // the size of struct is same
  45. s32 type;
  46. u32 access;
  47. u32 count;
  48. s32 owner;
  49. union {
  50. struct {
  51. s32 min;
  52. s32 max;
  53. s32 step;
  54. } integer;
  55. struct {
  56. u64 min;
  57. u64 max;
  58. u64 step;
  59. } integer64;
  60. struct {
  61. u32 items;
  62. u32 item;
  63. char name[64];
  64. u64 names_ptr;
  65. u32 names_length;
  66. } enumerated;
  67. unsigned char reserved[128];
  68. } value;
  69. unsigned char reserved[64];
  70. } __attribute__((packed));
  71. static int snd_ctl_elem_info_compat(struct snd_ctl_file *ctl,
  72. struct snd_ctl_elem_info32 __user *data32)
  73. {
  74. struct snd_ctl_elem_info *data;
  75. int err;
  76. data = kzalloc(sizeof(*data), GFP_KERNEL);
  77. if (! data)
  78. return -ENOMEM;
  79. err = -EFAULT;
  80. /* copy id */
  81. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  82. goto error;
  83. /* we need to copy the item index.
  84. * hope this doesn't break anything..
  85. */
  86. if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
  87. goto error;
  88. err = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
  89. if (err < 0)
  90. goto error;
  91. err = snd_ctl_elem_info(ctl, data);
  92. if (err < 0)
  93. goto error;
  94. /* restore info to 32bit */
  95. err = -EFAULT;
  96. /* id, type, access, count */
  97. if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
  98. copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
  99. goto error;
  100. if (put_user(data->owner, &data32->owner))
  101. goto error;
  102. switch (data->type) {
  103. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  104. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  105. if (put_user(data->value.integer.min, &data32->value.integer.min) ||
  106. put_user(data->value.integer.max, &data32->value.integer.max) ||
  107. put_user(data->value.integer.step, &data32->value.integer.step))
  108. goto error;
  109. break;
  110. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  111. if (copy_to_user(&data32->value.integer64,
  112. &data->value.integer64,
  113. sizeof(data->value.integer64)))
  114. goto error;
  115. break;
  116. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  117. if (copy_to_user(&data32->value.enumerated,
  118. &data->value.enumerated,
  119. sizeof(data->value.enumerated)))
  120. goto error;
  121. break;
  122. default:
  123. break;
  124. }
  125. err = 0;
  126. error:
  127. kfree(data);
  128. return err;
  129. }
  130. /* read / write */
  131. struct snd_ctl_elem_value32 {
  132. struct snd_ctl_elem_id id;
  133. unsigned int indirect; /* bit-field causes misalignment */
  134. union {
  135. s32 integer[128];
  136. unsigned char data[512];
  137. #ifndef CONFIG_X86_64
  138. s64 integer64[64];
  139. #endif
  140. } value;
  141. unsigned char reserved[128];
  142. };
  143. #ifdef CONFIG_X86_X32
  144. /* x32 has a different alignment for 64bit values from ia32 */
  145. struct snd_ctl_elem_value_x32 {
  146. struct snd_ctl_elem_id id;
  147. unsigned int indirect; /* bit-field causes misalignment */
  148. union {
  149. s32 integer[128];
  150. unsigned char data[512];
  151. s64 integer64[64];
  152. } value;
  153. unsigned char reserved[128];
  154. };
  155. #endif /* CONFIG_X86_X32 */
  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 -ENOENT;
  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. void __user *userdata,
  202. void __user *valuep,
  203. int *typep, int *countp)
  204. {
  205. struct snd_ctl_elem_value32 __user *data32 = userdata;
  206. int i, type, size;
  207. int count;
  208. unsigned int indirect;
  209. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  210. return -EFAULT;
  211. if (get_user(indirect, &data32->indirect))
  212. return -EFAULT;
  213. if (indirect)
  214. return -EINVAL;
  215. type = get_ctl_type(card, &data->id, &count);
  216. if (type < 0)
  217. return type;
  218. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  219. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  220. for (i = 0; i < count; i++) {
  221. s32 __user *intp = valuep;
  222. int val;
  223. if (get_user(val, &intp[i]))
  224. return -EFAULT;
  225. data->value.integer.value[i] = val;
  226. }
  227. } else {
  228. size = get_elem_size(type, count);
  229. if (size < 0) {
  230. dev_err(card->dev, "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
  231. return -EINVAL;
  232. }
  233. if (copy_from_user(data->value.bytes.data, valuep, size))
  234. return -EFAULT;
  235. }
  236. *typep = type;
  237. *countp = count;
  238. return 0;
  239. }
  240. /* restore the value to 32bit */
  241. static int copy_ctl_value_to_user(void __user *userdata,
  242. void __user *valuep,
  243. struct snd_ctl_elem_value *data,
  244. int type, int count)
  245. {
  246. struct snd_ctl_elem_value32 __user *data32 = userdata;
  247. int i, size;
  248. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  249. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  250. for (i = 0; i < count; i++) {
  251. s32 __user *intp = valuep;
  252. int val;
  253. val = data->value.integer.value[i];
  254. if (put_user(val, &intp[i]))
  255. return -EFAULT;
  256. }
  257. } else {
  258. size = get_elem_size(type, count);
  259. if (copy_to_user(valuep, data->value.bytes.data, size))
  260. return -EFAULT;
  261. }
  262. if (copy_to_user(&data32->id, &data->id, sizeof(data32->id)))
  263. return -EFAULT;
  264. return 0;
  265. }
  266. static int ctl_elem_read_user(struct snd_card *card,
  267. void __user *userdata, void __user *valuep)
  268. {
  269. struct snd_ctl_elem_value *data;
  270. int err, type, count;
  271. data = kzalloc(sizeof(*data), GFP_KERNEL);
  272. if (data == NULL)
  273. return -ENOMEM;
  274. err = copy_ctl_value_from_user(card, data, userdata, valuep,
  275. &type, &count);
  276. if (err < 0)
  277. goto error;
  278. err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  279. if (err < 0)
  280. goto error;
  281. err = snd_ctl_elem_read(card, data);
  282. if (err < 0)
  283. goto error;
  284. err = copy_ctl_value_to_user(userdata, valuep, data, type, count);
  285. error:
  286. kfree(data);
  287. return err;
  288. }
  289. static int ctl_elem_write_user(struct snd_ctl_file *file,
  290. void __user *userdata, void __user *valuep)
  291. {
  292. struct snd_ctl_elem_value *data;
  293. struct snd_card *card = file->card;
  294. int err, type, count;
  295. data = kzalloc(sizeof(*data), GFP_KERNEL);
  296. if (data == NULL)
  297. return -ENOMEM;
  298. err = copy_ctl_value_from_user(card, data, userdata, valuep,
  299. &type, &count);
  300. if (err < 0)
  301. goto error;
  302. err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  303. if (err < 0)
  304. goto error;
  305. err = snd_ctl_elem_write(card, file, data);
  306. if (err < 0)
  307. goto error;
  308. err = copy_ctl_value_to_user(userdata, valuep, data, type, count);
  309. error:
  310. kfree(data);
  311. return err;
  312. }
  313. static int snd_ctl_elem_read_user_compat(struct snd_card *card,
  314. struct snd_ctl_elem_value32 __user *data32)
  315. {
  316. return ctl_elem_read_user(card, data32, &data32->value);
  317. }
  318. static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
  319. struct snd_ctl_elem_value32 __user *data32)
  320. {
  321. return ctl_elem_write_user(file, data32, &data32->value);
  322. }
  323. #ifdef CONFIG_X86_X32
  324. static int snd_ctl_elem_read_user_x32(struct snd_card *card,
  325. struct snd_ctl_elem_value_x32 __user *data32)
  326. {
  327. return ctl_elem_read_user(card, data32, &data32->value);
  328. }
  329. static int snd_ctl_elem_write_user_x32(struct snd_ctl_file *file,
  330. struct snd_ctl_elem_value_x32 __user *data32)
  331. {
  332. return ctl_elem_write_user(file, data32, &data32->value);
  333. }
  334. #endif /* CONFIG_X86_X32 */
  335. /* add or replace a user control */
  336. static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
  337. struct snd_ctl_elem_info32 __user *data32,
  338. int replace)
  339. {
  340. struct snd_ctl_elem_info *data;
  341. int err;
  342. data = kzalloc(sizeof(*data), GFP_KERNEL);
  343. if (! data)
  344. return -ENOMEM;
  345. err = -EFAULT;
  346. /* id, type, access, count */ \
  347. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
  348. copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
  349. goto error;
  350. if (get_user(data->owner, &data32->owner))
  351. goto error;
  352. switch (data->type) {
  353. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  354. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  355. if (get_user(data->value.integer.min, &data32->value.integer.min) ||
  356. get_user(data->value.integer.max, &data32->value.integer.max) ||
  357. get_user(data->value.integer.step, &data32->value.integer.step))
  358. goto error;
  359. break;
  360. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  361. if (copy_from_user(&data->value.integer64,
  362. &data32->value.integer64,
  363. sizeof(data->value.integer64)))
  364. goto error;
  365. break;
  366. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  367. if (copy_from_user(&data->value.enumerated,
  368. &data32->value.enumerated,
  369. sizeof(data->value.enumerated)))
  370. goto error;
  371. data->value.enumerated.names_ptr =
  372. (uintptr_t)compat_ptr(data->value.enumerated.names_ptr);
  373. break;
  374. default:
  375. break;
  376. }
  377. err = snd_ctl_elem_add(file, data, replace);
  378. error:
  379. kfree(data);
  380. return err;
  381. }
  382. enum {
  383. SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
  384. SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
  385. SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
  386. SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
  387. SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
  388. SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
  389. #ifdef CONFIG_X86_X32
  390. SNDRV_CTL_IOCTL_ELEM_READ_X32 = _IOWR('U', 0x12, struct snd_ctl_elem_value_x32),
  391. SNDRV_CTL_IOCTL_ELEM_WRITE_X32 = _IOWR('U', 0x13, struct snd_ctl_elem_value_x32),
  392. #endif /* CONFIG_X86_X32 */
  393. };
  394. static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  395. {
  396. struct snd_ctl_file *ctl;
  397. struct snd_kctl_ioctl *p;
  398. void __user *argp = compat_ptr(arg);
  399. int err;
  400. ctl = file->private_data;
  401. if (snd_BUG_ON(!ctl || !ctl->card))
  402. return -ENXIO;
  403. switch (cmd) {
  404. case SNDRV_CTL_IOCTL_PVERSION:
  405. case SNDRV_CTL_IOCTL_CARD_INFO:
  406. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  407. case SNDRV_CTL_IOCTL_POWER:
  408. case SNDRV_CTL_IOCTL_POWER_STATE:
  409. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  410. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  411. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  412. case SNDRV_CTL_IOCTL_TLV_READ:
  413. case SNDRV_CTL_IOCTL_TLV_WRITE:
  414. case SNDRV_CTL_IOCTL_TLV_COMMAND:
  415. return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
  416. case SNDRV_CTL_IOCTL_ELEM_LIST32:
  417. return snd_ctl_elem_list_compat(ctl->card, argp);
  418. case SNDRV_CTL_IOCTL_ELEM_INFO32:
  419. return snd_ctl_elem_info_compat(ctl, argp);
  420. case SNDRV_CTL_IOCTL_ELEM_READ32:
  421. return snd_ctl_elem_read_user_compat(ctl->card, argp);
  422. case SNDRV_CTL_IOCTL_ELEM_WRITE32:
  423. return snd_ctl_elem_write_user_compat(ctl, argp);
  424. case SNDRV_CTL_IOCTL_ELEM_ADD32:
  425. return snd_ctl_elem_add_compat(ctl, argp, 0);
  426. case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
  427. return snd_ctl_elem_add_compat(ctl, argp, 1);
  428. #ifdef CONFIG_X86_X32
  429. case SNDRV_CTL_IOCTL_ELEM_READ_X32:
  430. return snd_ctl_elem_read_user_x32(ctl->card, argp);
  431. case SNDRV_CTL_IOCTL_ELEM_WRITE_X32:
  432. return snd_ctl_elem_write_user_x32(ctl, argp);
  433. #endif /* CONFIG_X86_X32 */
  434. }
  435. down_read(&snd_ioctl_rwsem);
  436. list_for_each_entry(p, &snd_control_compat_ioctls, list) {
  437. if (p->fioctl) {
  438. err = p->fioctl(ctl->card, ctl, cmd, arg);
  439. if (err != -ENOIOCTLCMD) {
  440. up_read(&snd_ioctl_rwsem);
  441. return err;
  442. }
  443. }
  444. }
  445. up_read(&snd_ioctl_rwsem);
  446. return -ENOIOCTLCMD;
  447. }