PageRenderTime 46ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/pci/asihpi/hpioctl.c

https://bitbucket.org/codefirex/kernel_samsung_jf
C | 482 lines | 352 code | 75 blank | 55 comment | 58 complexity | 6432b0492c8f7d17042fd25e238c2621 MD5 | raw file
  1. /*******************************************************************************
  2. AudioScience HPI driver
  3. Copyright (C) 1997-2011 AudioScience Inc. <support@audioscience.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of version 2 of the GNU General Public License as
  6. published by the Free Software Foundation;
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. Common Linux HPI ioctl and module probe/remove functions
  15. *******************************************************************************/
  16. #define SOURCEFILE_NAME "hpioctl.c"
  17. #include "hpi_internal.h"
  18. #include "hpi_version.h"
  19. #include "hpimsginit.h"
  20. #include "hpidebug.h"
  21. #include "hpimsgx.h"
  22. #include "hpioctl.h"
  23. #include "hpicmn.h"
  24. #include <linux/fs.h>
  25. #include <linux/slab.h>
  26. #include <linux/moduleparam.h>
  27. #include <asm/uaccess.h>
  28. #include <linux/pci.h>
  29. #include <linux/stringify.h>
  30. #include <linux/module.h>
  31. #ifdef MODULE_FIRMWARE
  32. MODULE_FIRMWARE("asihpi/dsp5000.bin");
  33. MODULE_FIRMWARE("asihpi/dsp6200.bin");
  34. MODULE_FIRMWARE("asihpi/dsp6205.bin");
  35. MODULE_FIRMWARE("asihpi/dsp6400.bin");
  36. MODULE_FIRMWARE("asihpi/dsp6600.bin");
  37. MODULE_FIRMWARE("asihpi/dsp8700.bin");
  38. MODULE_FIRMWARE("asihpi/dsp8900.bin");
  39. #endif
  40. static int prealloc_stream_buf;
  41. module_param(prealloc_stream_buf, int, S_IRUGO);
  42. MODULE_PARM_DESC(prealloc_stream_buf,
  43. "Preallocate size for per-adapter stream buffer");
  44. /* Allow the debug level to be changed after module load.
  45. E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
  46. */
  47. module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
  48. MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
  49. /* List of adapters found */
  50. static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
  51. /* Wrapper function to HPI_Message to enable dumping of the
  52. message and response types.
  53. */
  54. static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
  55. struct file *file)
  56. {
  57. if ((phm->adapter_index >= HPI_MAX_ADAPTERS)
  58. && (phm->object != HPI_OBJ_SUBSYSTEM))
  59. phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
  60. else
  61. hpi_send_recv_ex(phm, phr, file);
  62. }
  63. /* This is called from hpifunc.c functions, called by ALSA
  64. * (or other kernel process) In this case there is no file descriptor
  65. * available for the message cache code
  66. */
  67. void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
  68. {
  69. hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
  70. }
  71. EXPORT_SYMBOL(hpi_send_recv);
  72. /* for radio-asihpi */
  73. int asihpi_hpi_release(struct file *file)
  74. {
  75. struct hpi_message hm;
  76. struct hpi_response hr;
  77. /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
  78. /* close the subsystem just in case the application forgot to. */
  79. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  80. HPI_SUBSYS_CLOSE);
  81. hpi_send_recv_ex(&hm, &hr, file);
  82. return 0;
  83. }
  84. long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  85. {
  86. struct hpi_ioctl_linux __user *phpi_ioctl_data;
  87. void __user *puhm;
  88. void __user *puhr;
  89. union hpi_message_buffer_v1 *hm;
  90. union hpi_response_buffer_v1 *hr;
  91. u16 res_max_size;
  92. u32 uncopied_bytes;
  93. int err = 0;
  94. if (cmd != HPI_IOCTL_LINUX)
  95. return -EINVAL;
  96. hm = kmalloc(sizeof(*hm), GFP_KERNEL);
  97. hr = kmalloc(sizeof(*hr), GFP_KERNEL);
  98. if (!hm || !hr) {
  99. err = -ENOMEM;
  100. goto out;
  101. }
  102. phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
  103. /* Read the message and response pointers from user space. */
  104. if (get_user(puhm, &phpi_ioctl_data->phm)
  105. || get_user(puhr, &phpi_ioctl_data->phr)) {
  106. err = -EFAULT;
  107. goto out;
  108. }
  109. /* Now read the message size and data from user space. */
  110. if (get_user(hm->h.size, (u16 __user *)puhm)) {
  111. err = -EFAULT;
  112. goto out;
  113. }
  114. if (hm->h.size > sizeof(*hm))
  115. hm->h.size = sizeof(*hm);
  116. /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
  117. uncopied_bytes = copy_from_user(hm, puhm, hm->h.size);
  118. if (uncopied_bytes) {
  119. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  120. err = -EFAULT;
  121. goto out;
  122. }
  123. if (get_user(res_max_size, (u16 __user *)puhr)) {
  124. err = -EFAULT;
  125. goto out;
  126. }
  127. /* printk(KERN_INFO "user response size %d\n", res_max_size); */
  128. if (res_max_size < sizeof(struct hpi_response_header)) {
  129. HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
  130. err = -EFAULT;
  131. goto out;
  132. }
  133. switch (hm->h.function) {
  134. case HPI_SUBSYS_CREATE_ADAPTER:
  135. case HPI_ADAPTER_DELETE:
  136. /* Application must not use these functions! */
  137. hr->h.size = sizeof(hr->h);
  138. hr->h.error = HPI_ERROR_INVALID_OPERATION;
  139. hr->h.function = hm->h.function;
  140. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  141. if (uncopied_bytes)
  142. err = -EFAULT;
  143. else
  144. err = 0;
  145. goto out;
  146. }
  147. hr->h.size = res_max_size;
  148. if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
  149. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  150. } else {
  151. u16 __user *ptr = NULL;
  152. u32 size = 0;
  153. /* -1=no data 0=read from user mem, 1=write to user mem */
  154. int wrflag = -1;
  155. struct hpi_adapter *pa = NULL;
  156. if (hm->h.adapter_index < ARRAY_SIZE(adapters))
  157. pa = &adapters[hm->h.adapter_index];
  158. if (!pa || !pa->adapter || !pa->adapter->type) {
  159. hpi_init_response(&hr->r0, hm->h.object,
  160. hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
  161. uncopied_bytes =
  162. copy_to_user(puhr, hr, sizeof(hr->h));
  163. if (uncopied_bytes)
  164. err = -EFAULT;
  165. else
  166. err = 0;
  167. goto out;
  168. }
  169. if (mutex_lock_interruptible(&pa->mutex)) {
  170. err = -EINTR;
  171. goto out;
  172. }
  173. /* Dig out any pointers embedded in the message. */
  174. switch (hm->h.function) {
  175. case HPI_OSTREAM_WRITE:
  176. case HPI_ISTREAM_READ:{
  177. /* Yes, sparse, this is correct. */
  178. ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
  179. size = hm->m0.u.d.u.data.data_size;
  180. /* Allocate buffer according to application request.
  181. ?Is it better to alloc/free for the duration
  182. of the transaction?
  183. */
  184. if (pa->buffer_size < size) {
  185. HPI_DEBUG_LOG(DEBUG,
  186. "Realloc adapter %d stream "
  187. "buffer from %zd to %d\n",
  188. hm->h.adapter_index,
  189. pa->buffer_size, size);
  190. if (pa->p_buffer) {
  191. pa->buffer_size = 0;
  192. vfree(pa->p_buffer);
  193. }
  194. pa->p_buffer = vmalloc(size);
  195. if (pa->p_buffer)
  196. pa->buffer_size = size;
  197. else {
  198. HPI_DEBUG_LOG(ERROR,
  199. "HPI could not allocate "
  200. "stream buffer size %d\n",
  201. size);
  202. mutex_unlock(&pa->mutex);
  203. err = -EINVAL;
  204. goto out;
  205. }
  206. }
  207. hm->m0.u.d.u.data.pb_data = pa->p_buffer;
  208. if (hm->h.function == HPI_ISTREAM_READ)
  209. /* from card, WRITE to user mem */
  210. wrflag = 1;
  211. else
  212. wrflag = 0;
  213. break;
  214. }
  215. default:
  216. size = 0;
  217. break;
  218. }
  219. if (size && (wrflag == 0)) {
  220. uncopied_bytes =
  221. copy_from_user(pa->p_buffer, ptr, size);
  222. if (uncopied_bytes)
  223. HPI_DEBUG_LOG(WARNING,
  224. "Missed %d of %d "
  225. "bytes from user\n", uncopied_bytes,
  226. size);
  227. }
  228. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  229. if (size && (wrflag == 1)) {
  230. uncopied_bytes =
  231. copy_to_user(ptr, pa->p_buffer, size);
  232. if (uncopied_bytes)
  233. HPI_DEBUG_LOG(WARNING,
  234. "Missed %d of %d " "bytes to user\n",
  235. uncopied_bytes, size);
  236. }
  237. mutex_unlock(&pa->mutex);
  238. }
  239. /* on return response size must be set */
  240. /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
  241. if (!hr->h.size) {
  242. HPI_DEBUG_LOG(ERROR, "response zero size\n");
  243. err = -EFAULT;
  244. goto out;
  245. }
  246. if (hr->h.size > res_max_size) {
  247. HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
  248. res_max_size);
  249. hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
  250. hr->h.specific_error = hr->h.size;
  251. hr->h.size = sizeof(hr->h);
  252. }
  253. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  254. if (uncopied_bytes) {
  255. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  256. err = -EFAULT;
  257. goto out;
  258. }
  259. out:
  260. kfree(hm);
  261. kfree(hr);
  262. return err;
  263. }
  264. int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev,
  265. const struct pci_device_id *pci_id)
  266. {
  267. int idx, nm;
  268. int adapter_index;
  269. unsigned int memlen;
  270. struct hpi_message hm;
  271. struct hpi_response hr;
  272. struct hpi_adapter adapter;
  273. struct hpi_pci pci;
  274. memset(&adapter, 0, sizeof(adapter));
  275. dev_printk(KERN_DEBUG, &pci_dev->dev,
  276. "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
  277. pci_dev->device, pci_dev->subsystem_vendor,
  278. pci_dev->subsystem_device, pci_dev->devfn);
  279. if (pci_enable_device(pci_dev) < 0) {
  280. dev_printk(KERN_ERR, &pci_dev->dev,
  281. "pci_enable_device failed, disabling device\n");
  282. return -EIO;
  283. }
  284. pci_set_master(pci_dev); /* also sets latency timer if < 16 */
  285. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  286. HPI_SUBSYS_CREATE_ADAPTER);
  287. hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
  288. HPI_ERROR_PROCESSING_MESSAGE);
  289. hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
  290. nm = HPI_MAX_ADAPTER_MEM_SPACES;
  291. for (idx = 0; idx < nm; idx++) {
  292. HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
  293. &pci_dev->resource[idx]);
  294. if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
  295. memlen = pci_resource_len(pci_dev, idx);
  296. pci.ap_mem_base[idx] =
  297. ioremap(pci_resource_start(pci_dev, idx),
  298. memlen);
  299. if (!pci.ap_mem_base[idx]) {
  300. HPI_DEBUG_LOG(ERROR,
  301. "ioremap failed, aborting\n");
  302. /* unmap previously mapped pci mem space */
  303. goto err;
  304. }
  305. }
  306. }
  307. pci.pci_dev = pci_dev;
  308. hm.u.s.resource.bus_type = HPI_BUS_PCI;
  309. hm.u.s.resource.r.pci = &pci;
  310. /* call CreateAdapterObject on the relevant hpi module */
  311. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  312. if (hr.error)
  313. goto err;
  314. adapter_index = hr.u.s.adapter_index;
  315. adapter.adapter = hpi_find_adapter(adapter_index);
  316. if (prealloc_stream_buf) {
  317. adapter.p_buffer = vmalloc(prealloc_stream_buf);
  318. if (!adapter.p_buffer) {
  319. HPI_DEBUG_LOG(ERROR,
  320. "HPI could not allocate "
  321. "kernel buffer size %d\n",
  322. prealloc_stream_buf);
  323. goto err;
  324. }
  325. }
  326. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  327. HPI_ADAPTER_OPEN);
  328. hm.adapter_index = adapter.adapter->index;
  329. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  330. if (hr.error)
  331. goto err;
  332. /* WARNING can't init mutex in 'adapter'
  333. * and then copy it to adapters[] ?!?!
  334. */
  335. adapters[adapter_index] = adapter;
  336. mutex_init(&adapters[adapter_index].mutex);
  337. pci_set_drvdata(pci_dev, &adapters[adapter_index]);
  338. dev_printk(KERN_INFO, &pci_dev->dev,
  339. "probe succeeded for ASI%04X HPI index %d\n",
  340. adapter.adapter->type, adapter_index);
  341. return 0;
  342. err:
  343. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  344. if (pci.ap_mem_base[idx]) {
  345. iounmap(pci.ap_mem_base[idx]);
  346. pci.ap_mem_base[idx] = NULL;
  347. }
  348. }
  349. if (adapter.p_buffer) {
  350. adapter.buffer_size = 0;
  351. vfree(adapter.p_buffer);
  352. }
  353. HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
  354. return -ENODEV;
  355. }
  356. void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev)
  357. {
  358. int idx;
  359. struct hpi_message hm;
  360. struct hpi_response hr;
  361. struct hpi_adapter *pa;
  362. struct hpi_pci pci;
  363. pa = pci_get_drvdata(pci_dev);
  364. pci = pa->adapter->pci;
  365. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  366. HPI_ADAPTER_DELETE);
  367. hm.adapter_index = pa->adapter->index;
  368. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  369. /* unmap PCI memory space, mapped during device init. */
  370. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  371. if (pci.ap_mem_base[idx])
  372. iounmap(pci.ap_mem_base[idx]);
  373. }
  374. if (pa->p_buffer)
  375. vfree(pa->p_buffer);
  376. pci_set_drvdata(pci_dev, NULL);
  377. if (1)
  378. dev_printk(KERN_INFO, &pci_dev->dev,
  379. "remove %04x:%04x,%04x:%04x,%04x," " HPI index %d.\n",
  380. pci_dev->vendor, pci_dev->device,
  381. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  382. pci_dev->devfn, pa->adapter->index);
  383. memset(pa, 0, sizeof(*pa));
  384. }
  385. void __init asihpi_init(void)
  386. {
  387. struct hpi_message hm;
  388. struct hpi_response hr;
  389. memset(adapters, 0, sizeof(adapters));
  390. printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
  391. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  392. HPI_SUBSYS_DRIVER_LOAD);
  393. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  394. }
  395. void asihpi_exit(void)
  396. {
  397. struct hpi_message hm;
  398. struct hpi_response hr;
  399. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  400. HPI_SUBSYS_DRIVER_UNLOAD);
  401. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  402. }