hpioctl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. pa = &adapters[hm->h.adapter_index];
  132. hr->h.size = 0;
  133. if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
  134. switch (hm->h.function) {
  135. case HPI_SUBSYS_CREATE_ADAPTER:
  136. case HPI_SUBSYS_DELETE_ADAPTER:
  137. /* Application must not use these functions! */
  138. hr->h.size = sizeof(hr->h);
  139. hr->h.error = HPI_ERROR_INVALID_OPERATION;
  140. hr->h.function = hm->h.function;
  141. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  142. if (uncopied_bytes)
  143. err = -EFAULT;
  144. else
  145. err = 0;
  146. goto out;
  147. default:
  148. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  149. }
  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. u32 adapter = hm->h.adapter_index;
  156. if ((hm->h.adapter_index > HPI_MAX_ADAPTERS) || (!pa->type)) {
  157. hpi_init_response(&hr->r0, HPI_OBJ_ADAPTER,
  158. HPI_ADAPTER_OPEN,
  159. HPI_ERROR_BAD_ADAPTER_NUMBER);
  160. uncopied_bytes =
  161. copy_to_user(puhr, hr, sizeof(hr->h));
  162. if (uncopied_bytes)
  163. err = -EFAULT;
  164. else
  165. err = 0;
  166. goto out;
  167. }
  168. if (mutex_lock_interruptible(&adapters[adapter].mutex)) {
  169. err = -EINTR;
  170. goto out;
  171. }
  172. /* Dig out any pointers embedded in the message. */
  173. switch (hm->h.function) {
  174. case HPI_OSTREAM_WRITE:
  175. case HPI_ISTREAM_READ:{
  176. /* Yes, sparse, this is correct. */
  177. ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
  178. size = hm->m0.u.d.u.data.data_size;
  179. /* Allocate buffer according to application request.
  180. ?Is it better to alloc/free for the duration
  181. of the transaction?
  182. */
  183. if (pa->buffer_size < size) {
  184. HPI_DEBUG_LOG(DEBUG,
  185. "realloc adapter %d stream "
  186. "buffer from %zd to %d\n",
  187. hm->h.adapter_index,
  188. pa->buffer_size, size);
  189. if (pa->p_buffer) {
  190. pa->buffer_size = 0;
  191. vfree(pa->p_buffer);
  192. }
  193. pa->p_buffer = vmalloc(size);
  194. if (pa->p_buffer)
  195. pa->buffer_size = size;
  196. else {
  197. HPI_DEBUG_LOG(ERROR,
  198. "HPI could not allocate "
  199. "stream buffer size %d\n",
  200. size);
  201. mutex_unlock(&adapters
  202. [adapter].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(&adapters[adapter].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. /*HPI_DEBUG_MESSAGE(ERROR, hm); */
  250. err = -EFAULT;
  251. goto out;
  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 err, idx, nm;
  268. unsigned int memlen;
  269. struct hpi_message hm;
  270. struct hpi_response hr;
  271. struct hpi_adapter adapter;
  272. struct hpi_pci pci;
  273. memset(&adapter, 0, sizeof(adapter));
  274. printk(KERN_DEBUG "probe PCI device (%04x:%04x,%04x:%04x,%04x)\n",
  275. pci_dev->vendor, pci_dev->device, pci_dev->subsystem_vendor,
  276. pci_dev->subsystem_device, pci_dev->devfn);
  277. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  278. HPI_SUBSYS_CREATE_ADAPTER);
  279. hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
  280. HPI_ERROR_PROCESSING_MESSAGE);
  281. hm.adapter_index = -1; /* an invalid index */
  282. /* fill in HPI_PCI information from kernel provided information */
  283. adapter.pci = pci_dev;
  284. nm = HPI_MAX_ADAPTER_MEM_SPACES;
  285. for (idx = 0; idx < nm; idx++) {
  286. HPI_DEBUG_LOG(INFO, "resource %d %s %08llx-%08llx %04llx\n",
  287. idx, pci_dev->resource[idx].name,
  288. (unsigned long long)pci_resource_start(pci_dev, idx),
  289. (unsigned long long)pci_resource_end(pci_dev, idx),
  290. (unsigned long long)pci_resource_flags(pci_dev, idx));
  291. if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
  292. memlen = pci_resource_len(pci_dev, idx);
  293. adapter.ap_remapped_mem_base[idx] =
  294. ioremap(pci_resource_start(pci_dev, idx),
  295. memlen);
  296. if (!adapter.ap_remapped_mem_base[idx]) {
  297. HPI_DEBUG_LOG(ERROR,
  298. "ioremap failed, aborting\n");
  299. /* unmap previously mapped pci mem space */
  300. goto err;
  301. }
  302. }
  303. pci.ap_mem_base[idx] = adapter.ap_remapped_mem_base[idx];
  304. }
  305. /* could replace Pci with direct pointer to pci_dev for linux
  306. Instead wrap accessor functions for IDs etc.
  307. Would it work for windows?
  308. */
  309. pci.bus_number = pci_dev->bus->number;
  310. pci.vendor_id = (u16)pci_dev->vendor;
  311. pci.device_id = (u16)pci_dev->device;
  312. pci.subsys_vendor_id = (u16)(pci_dev->subsystem_vendor & 0xffff);
  313. pci.subsys_device_id = (u16)(pci_dev->subsystem_device & 0xffff);
  314. pci.device_number = pci_dev->devfn;
  315. pci.interrupt = pci_dev->irq;
  316. pci.p_os_data = pci_dev;
  317. hm.u.s.resource.bus_type = HPI_BUS_PCI;
  318. hm.u.s.resource.r.pci = &pci;
  319. /* call CreateAdapterObject on the relevant hpi module */
  320. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  321. if (hr.error)
  322. goto err;
  323. if (prealloc_stream_buf) {
  324. adapter.p_buffer = vmalloc(prealloc_stream_buf);
  325. if (!adapter.p_buffer) {
  326. HPI_DEBUG_LOG(ERROR,
  327. "HPI could not allocate "
  328. "kernel buffer size %d\n",
  329. prealloc_stream_buf);
  330. goto err;
  331. }
  332. }
  333. adapter.index = hr.u.s.adapter_index;
  334. adapter.type = hr.u.s.aw_adapter_list[adapter.index];
  335. hm.adapter_index = adapter.index;
  336. err = hpi_adapter_open(NULL, adapter.index);
  337. if (err)
  338. goto err;
  339. adapter.snd_card_asihpi = NULL;
  340. /* WARNING can't init mutex in 'adapter'
  341. * and then copy it to adapters[] ?!?!
  342. */
  343. adapters[hr.u.s.adapter_index] = adapter;
  344. mutex_init(&adapters[adapter.index].mutex);
  345. pci_set_drvdata(pci_dev, &adapters[adapter.index]);
  346. printk(KERN_INFO "probe found adapter ASI%04X HPI index #%d.\n",
  347. adapter.type, adapter.index);
  348. return 0;
  349. err:
  350. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  351. if (adapter.ap_remapped_mem_base[idx]) {
  352. iounmap(adapter.ap_remapped_mem_base[idx]);
  353. adapter.ap_remapped_mem_base[idx] = NULL;
  354. }
  355. }
  356. if (adapter.p_buffer) {
  357. adapter.buffer_size = 0;
  358. vfree(adapter.p_buffer);
  359. }
  360. HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
  361. return -ENODEV;
  362. }
  363. void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev)
  364. {
  365. int idx;
  366. struct hpi_message hm;
  367. struct hpi_response hr;
  368. struct hpi_adapter *pa;
  369. pa = (struct hpi_adapter *)pci_get_drvdata(pci_dev);
  370. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  371. HPI_SUBSYS_DELETE_ADAPTER);
  372. hm.adapter_index = pa->index;
  373. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  374. /* unmap PCI memory space, mapped during device init. */
  375. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  376. if (pa->ap_remapped_mem_base[idx]) {
  377. iounmap(pa->ap_remapped_mem_base[idx]);
  378. pa->ap_remapped_mem_base[idx] = NULL;
  379. }
  380. }
  381. if (pa->p_buffer) {
  382. pa->buffer_size = 0;
  383. vfree(pa->p_buffer);
  384. }
  385. pci_set_drvdata(pci_dev, NULL);
  386. /*
  387. printk(KERN_INFO "PCI device (%04x:%04x,%04x:%04x,%04x),"
  388. " HPI index # %d, removed.\n",
  389. pci_dev->vendor, pci_dev->device,
  390. pci_dev->subsystem_vendor,
  391. pci_dev->subsystem_device, pci_dev->devfn,
  392. pa->index);
  393. */
  394. }
  395. void __init asihpi_init(void)
  396. {
  397. struct hpi_message hm;
  398. struct hpi_response hr;
  399. memset(adapters, 0, sizeof(adapters));
  400. printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
  401. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  402. HPI_SUBSYS_DRIVER_LOAD);
  403. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  404. }
  405. void asihpi_exit(void)
  406. {
  407. struct hpi_message hm;
  408. struct hpi_response hr;
  409. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  410. HPI_SUBSYS_DRIVER_UNLOAD);
  411. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  412. }