control_compat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. u64 names_ptr;
  79. u32 names_length;
  80. } enumerated;
  81. unsigned char reserved[128];
  82. } value;
  83. unsigned char reserved[64];
  84. } __attribute__((packed));
  85. static int snd_ctl_elem_info_compat(struct snd_ctl_file *ctl,
  86. struct snd_ctl_elem_info32 __user *data32)
  87. {
  88. struct snd_ctl_elem_info *data;
  89. int err;
  90. data = kzalloc(sizeof(*data), GFP_KERNEL);
  91. if (! data)
  92. return -ENOMEM;
  93. err = -EFAULT;
  94. /* copy id */
  95. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  96. goto error;
  97. /* we need to copy the item index.
  98. * hope this doesn't break anything..
  99. */
  100. if (get_user(data->value.enumerated.item, &data32->value.enumerated.item))
  101. goto error;
  102. snd_power_lock(ctl->card);
  103. err = snd_power_wait(ctl->card, SNDRV_CTL_POWER_D0);
  104. if (err >= 0)
  105. err = snd_ctl_elem_info(ctl, data);
  106. snd_power_unlock(ctl->card);
  107. if (err < 0)
  108. goto error;
  109. /* restore info to 32bit */
  110. err = -EFAULT;
  111. /* id, type, access, count */
  112. if (copy_to_user(&data32->id, &data->id, sizeof(data->id)) ||
  113. copy_to_user(&data32->type, &data->type, 3 * sizeof(u32)))
  114. goto error;
  115. if (put_user(data->owner, &data32->owner))
  116. goto error;
  117. switch (data->type) {
  118. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  119. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  120. if (put_user(data->value.integer.min, &data32->value.integer.min) ||
  121. put_user(data->value.integer.max, &data32->value.integer.max) ||
  122. put_user(data->value.integer.step, &data32->value.integer.step))
  123. goto error;
  124. break;
  125. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  126. if (copy_to_user(&data32->value.integer64,
  127. &data->value.integer64,
  128. sizeof(data->value.integer64)))
  129. goto error;
  130. break;
  131. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  132. if (copy_to_user(&data32->value.enumerated,
  133. &data->value.enumerated,
  134. sizeof(data->value.enumerated)))
  135. goto error;
  136. break;
  137. default:
  138. break;
  139. }
  140. err = 0;
  141. error:
  142. kfree(data);
  143. return err;
  144. }
  145. /* read / write */
  146. struct snd_ctl_elem_value32 {
  147. struct snd_ctl_elem_id id;
  148. unsigned int indirect; /* bit-field causes misalignment */
  149. union {
  150. s32 integer[128];
  151. unsigned char data[512];
  152. #ifndef CONFIG_X86_64
  153. s64 integer64[64];
  154. #endif
  155. } value;
  156. unsigned char reserved[128];
  157. };
  158. #ifdef CONFIG_X86_X32
  159. /* x32 has a different alignment for 64bit values from ia32 */
  160. struct snd_ctl_elem_value_x32 {
  161. struct snd_ctl_elem_id id;
  162. unsigned int indirect; /* bit-field causes misalignment */
  163. union {
  164. s32 integer[128];
  165. unsigned char data[512];
  166. s64 integer64[64];
  167. } value;
  168. unsigned char reserved[128];
  169. };
  170. #endif /* CONFIG_X86_X32 */
  171. /* get the value type and count of the control */
  172. static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,
  173. int *countp)
  174. {
  175. struct snd_kcontrol *kctl;
  176. struct snd_ctl_elem_info *info;
  177. int err;
  178. down_read(&card->controls_rwsem);
  179. kctl = snd_ctl_find_id(card, id);
  180. if (! kctl) {
  181. up_read(&card->controls_rwsem);
  182. return -ENXIO;
  183. }
  184. info = kzalloc(sizeof(*info), GFP_KERNEL);
  185. if (info == NULL) {
  186. up_read(&card->controls_rwsem);
  187. return -ENOMEM;
  188. }
  189. info->id = *id;
  190. err = kctl->info(kctl, info);
  191. up_read(&card->controls_rwsem);
  192. if (err >= 0) {
  193. err = info->type;
  194. *countp = info->count;
  195. }
  196. kfree(info);
  197. return err;
  198. }
  199. static int get_elem_size(int type, int count)
  200. {
  201. switch (type) {
  202. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  203. return sizeof(s64) * count;
  204. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  205. return sizeof(int) * count;
  206. case SNDRV_CTL_ELEM_TYPE_BYTES:
  207. return 512;
  208. case SNDRV_CTL_ELEM_TYPE_IEC958:
  209. return sizeof(struct snd_aes_iec958);
  210. default:
  211. return -1;
  212. }
  213. }
  214. static int copy_ctl_value_from_user(struct snd_card *card,
  215. struct snd_ctl_elem_value *data,
  216. void __user *userdata,
  217. void __user *valuep,
  218. int *typep, int *countp)
  219. {
  220. struct snd_ctl_elem_value32 __user *data32 = userdata;
  221. int i, type, size;
  222. int uninitialized_var(count);
  223. unsigned int indirect;
  224. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)))
  225. return -EFAULT;
  226. if (get_user(indirect, &data32->indirect))
  227. return -EFAULT;
  228. if (indirect)
  229. return -EINVAL;
  230. type = get_ctl_type(card, &data->id, &count);
  231. if (type < 0)
  232. return type;
  233. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  234. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  235. for (i = 0; i < count; i++) {
  236. s32 __user *intp = valuep;
  237. int val;
  238. if (get_user(val, &intp[i]))
  239. return -EFAULT;
  240. data->value.integer.value[i] = val;
  241. }
  242. } else {
  243. size = get_elem_size(type, count);
  244. if (size < 0) {
  245. dev_err(card->dev, "snd_ioctl32_ctl_elem_value: unknown type %d\n", type);
  246. return -EINVAL;
  247. }
  248. if (copy_from_user(data->value.bytes.data, valuep, size))
  249. return -EFAULT;
  250. }
  251. *typep = type;
  252. *countp = count;
  253. return 0;
  254. }
  255. /* restore the value to 32bit */
  256. static int copy_ctl_value_to_user(void __user *userdata,
  257. void __user *valuep,
  258. struct snd_ctl_elem_value *data,
  259. int type, int count)
  260. {
  261. int i, size;
  262. if (type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
  263. type == SNDRV_CTL_ELEM_TYPE_INTEGER) {
  264. for (i = 0; i < count; i++) {
  265. s32 __user *intp = valuep;
  266. int val;
  267. val = data->value.integer.value[i];
  268. if (put_user(val, &intp[i]))
  269. return -EFAULT;
  270. }
  271. } else {
  272. size = get_elem_size(type, count);
  273. if (copy_to_user(valuep, data->value.bytes.data, size))
  274. return -EFAULT;
  275. }
  276. return 0;
  277. }
  278. static int ctl_elem_read_user(struct snd_card *card,
  279. void __user *userdata, void __user *valuep)
  280. {
  281. struct snd_ctl_elem_value *data;
  282. int err, type, count;
  283. data = kzalloc(sizeof(*data), GFP_KERNEL);
  284. if (data == NULL)
  285. return -ENOMEM;
  286. err = copy_ctl_value_from_user(card, data, userdata, valuep,
  287. &type, &count);
  288. if (err < 0)
  289. goto error;
  290. snd_power_lock(card);
  291. err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  292. if (err >= 0)
  293. err = snd_ctl_elem_read(card, data);
  294. snd_power_unlock(card);
  295. if (err >= 0)
  296. err = copy_ctl_value_to_user(userdata, valuep, data,
  297. type, count);
  298. error:
  299. kfree(data);
  300. return err;
  301. }
  302. static int ctl_elem_write_user(struct snd_ctl_file *file,
  303. void __user *userdata, void __user *valuep)
  304. {
  305. struct snd_ctl_elem_value *data;
  306. struct snd_card *card = file->card;
  307. int err, type, count;
  308. data = kzalloc(sizeof(*data), GFP_KERNEL);
  309. if (data == NULL)
  310. return -ENOMEM;
  311. err = copy_ctl_value_from_user(card, data, userdata, valuep,
  312. &type, &count);
  313. if (err < 0)
  314. goto error;
  315. snd_power_lock(card);
  316. err = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  317. if (err >= 0)
  318. err = snd_ctl_elem_write(card, file, data);
  319. snd_power_unlock(card);
  320. if (err >= 0)
  321. err = copy_ctl_value_to_user(userdata, valuep, data,
  322. type, count);
  323. error:
  324. kfree(data);
  325. return err;
  326. }
  327. static int snd_ctl_elem_read_user_compat(struct snd_card *card,
  328. struct snd_ctl_elem_value32 __user *data32)
  329. {
  330. return ctl_elem_read_user(card, data32, &data32->value);
  331. }
  332. static int snd_ctl_elem_write_user_compat(struct snd_ctl_file *file,
  333. struct snd_ctl_elem_value32 __user *data32)
  334. {
  335. return ctl_elem_write_user(file, data32, &data32->value);
  336. }
  337. #ifdef CONFIG_X86_X32
  338. static int snd_ctl_elem_read_user_x32(struct snd_card *card,
  339. struct snd_ctl_elem_value_x32 __user *data32)
  340. {
  341. return ctl_elem_read_user(card, data32, &data32->value);
  342. }
  343. static int snd_ctl_elem_write_user_x32(struct snd_ctl_file *file,
  344. struct snd_ctl_elem_value_x32 __user *data32)
  345. {
  346. return ctl_elem_write_user(file, data32, &data32->value);
  347. }
  348. #endif /* CONFIG_X86_X32 */
  349. /* add or replace a user control */
  350. static int snd_ctl_elem_add_compat(struct snd_ctl_file *file,
  351. struct snd_ctl_elem_info32 __user *data32,
  352. int replace)
  353. {
  354. struct snd_ctl_elem_info *data;
  355. int err;
  356. data = kzalloc(sizeof(*data), GFP_KERNEL);
  357. if (! data)
  358. return -ENOMEM;
  359. err = -EFAULT;
  360. /* id, type, access, count */ \
  361. if (copy_from_user(&data->id, &data32->id, sizeof(data->id)) ||
  362. copy_from_user(&data->type, &data32->type, 3 * sizeof(u32)))
  363. goto error;
  364. if (get_user(data->owner, &data32->owner))
  365. goto error;
  366. switch (data->type) {
  367. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  368. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  369. if (get_user(data->value.integer.min, &data32->value.integer.min) ||
  370. get_user(data->value.integer.max, &data32->value.integer.max) ||
  371. get_user(data->value.integer.step, &data32->value.integer.step))
  372. goto error;
  373. break;
  374. case SNDRV_CTL_ELEM_TYPE_INTEGER64:
  375. if (copy_from_user(&data->value.integer64,
  376. &data32->value.integer64,
  377. sizeof(data->value.integer64)))
  378. goto error;
  379. break;
  380. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  381. if (copy_from_user(&data->value.enumerated,
  382. &data32->value.enumerated,
  383. sizeof(data->value.enumerated)))
  384. goto error;
  385. data->value.enumerated.names_ptr =
  386. (uintptr_t)compat_ptr(data->value.enumerated.names_ptr);
  387. break;
  388. default:
  389. break;
  390. }
  391. err = snd_ctl_elem_add(file, data, replace);
  392. error:
  393. kfree(data);
  394. return err;
  395. }
  396. enum {
  397. SNDRV_CTL_IOCTL_ELEM_LIST32 = _IOWR('U', 0x10, struct snd_ctl_elem_list32),
  398. SNDRV_CTL_IOCTL_ELEM_INFO32 = _IOWR('U', 0x11, struct snd_ctl_elem_info32),
  399. SNDRV_CTL_IOCTL_ELEM_READ32 = _IOWR('U', 0x12, struct snd_ctl_elem_value32),
  400. SNDRV_CTL_IOCTL_ELEM_WRITE32 = _IOWR('U', 0x13, struct snd_ctl_elem_value32),
  401. SNDRV_CTL_IOCTL_ELEM_ADD32 = _IOWR('U', 0x17, struct snd_ctl_elem_info32),
  402. SNDRV_CTL_IOCTL_ELEM_REPLACE32 = _IOWR('U', 0x18, struct snd_ctl_elem_info32),
  403. #ifdef CONFIG_X86_X32
  404. SNDRV_CTL_IOCTL_ELEM_READ_X32 = _IOWR('U', 0x12, struct snd_ctl_elem_value_x32),
  405. SNDRV_CTL_IOCTL_ELEM_WRITE_X32 = _IOWR('U', 0x13, struct snd_ctl_elem_value_x32),
  406. #endif /* CONFIG_X86_X32 */
  407. };
  408. static inline long snd_ctl_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
  409. {
  410. struct snd_ctl_file *ctl;
  411. struct snd_kctl_ioctl *p;
  412. void __user *argp = compat_ptr(arg);
  413. int err;
  414. ctl = file->private_data;
  415. if (snd_BUG_ON(!ctl || !ctl->card))
  416. return -ENXIO;
  417. switch (cmd) {
  418. case SNDRV_CTL_IOCTL_PVERSION:
  419. case SNDRV_CTL_IOCTL_CARD_INFO:
  420. case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
  421. case SNDRV_CTL_IOCTL_POWER:
  422. case SNDRV_CTL_IOCTL_POWER_STATE:
  423. case SNDRV_CTL_IOCTL_ELEM_LOCK:
  424. case SNDRV_CTL_IOCTL_ELEM_UNLOCK:
  425. case SNDRV_CTL_IOCTL_ELEM_REMOVE:
  426. case SNDRV_CTL_IOCTL_TLV_READ:
  427. case SNDRV_CTL_IOCTL_TLV_WRITE:
  428. case SNDRV_CTL_IOCTL_TLV_COMMAND:
  429. return snd_ctl_ioctl(file, cmd, (unsigned long)argp);
  430. case SNDRV_CTL_IOCTL_ELEM_LIST32:
  431. return snd_ctl_elem_list_compat(ctl->card, argp);
  432. case SNDRV_CTL_IOCTL_ELEM_INFO32:
  433. return snd_ctl_elem_info_compat(ctl, argp);
  434. case SNDRV_CTL_IOCTL_ELEM_READ32:
  435. return snd_ctl_elem_read_user_compat(ctl->card, argp);
  436. case SNDRV_CTL_IOCTL_ELEM_WRITE32:
  437. return snd_ctl_elem_write_user_compat(ctl, argp);
  438. case SNDRV_CTL_IOCTL_ELEM_ADD32:
  439. return snd_ctl_elem_add_compat(ctl, argp, 0);
  440. case SNDRV_CTL_IOCTL_ELEM_REPLACE32:
  441. return snd_ctl_elem_add_compat(ctl, argp, 1);
  442. #ifdef CONFIG_X86_X32
  443. case SNDRV_CTL_IOCTL_ELEM_READ_X32:
  444. return snd_ctl_elem_read_user_x32(ctl->card, argp);
  445. case SNDRV_CTL_IOCTL_ELEM_WRITE_X32:
  446. return snd_ctl_elem_write_user_x32(ctl, argp);
  447. #endif /* CONFIG_X86_X32 */
  448. }
  449. down_read(&snd_ioctl_rwsem);
  450. list_for_each_entry(p, &snd_control_compat_ioctls, list) {
  451. if (p->fioctl) {
  452. err = p->fioctl(ctl->card, ctl, cmd, arg);
  453. if (err != -ENOIOCTLCMD) {
  454. up_read(&snd_ioctl_rwsem);
  455. return err;
  456. }
  457. }
  458. }
  459. up_read(&snd_ioctl_rwsem);
  460. return -ENOIOCTLCMD;
  461. }