PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/pci/asihpi/hpioctl.c

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