hpioctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*******************************************************************************
  2. AudioScience HPI driver
  3. Common Linux HPI ioctl and module probe/remove functions
  4. Copyright (C) 1997-2014 AudioScience Inc. <support@audioscience.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of version 2 of the GNU General Public License as
  7. published by the Free Software Foundation;
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. *******************************************************************************/
  13. #define SOURCEFILE_NAME "hpioctl.c"
  14. #include "hpi_internal.h"
  15. #include "hpi_version.h"
  16. #include "hpimsginit.h"
  17. #include "hpidebug.h"
  18. #include "hpimsgx.h"
  19. #include "hpioctl.h"
  20. #include "hpicmn.h"
  21. #include <linux/fs.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/slab.h>
  24. #include <linux/moduleparam.h>
  25. #include <asm/uaccess.h>
  26. #include <linux/pci.h>
  27. #include <linux/stringify.h>
  28. #include <linux/module.h>
  29. #ifdef MODULE_FIRMWARE
  30. MODULE_FIRMWARE("asihpi/dsp5000.bin");
  31. MODULE_FIRMWARE("asihpi/dsp6200.bin");
  32. MODULE_FIRMWARE("asihpi/dsp6205.bin");
  33. MODULE_FIRMWARE("asihpi/dsp6400.bin");
  34. MODULE_FIRMWARE("asihpi/dsp6600.bin");
  35. MODULE_FIRMWARE("asihpi/dsp8700.bin");
  36. MODULE_FIRMWARE("asihpi/dsp8900.bin");
  37. #endif
  38. static int prealloc_stream_buf;
  39. module_param(prealloc_stream_buf, int, S_IRUGO);
  40. MODULE_PARM_DESC(prealloc_stream_buf,
  41. "Preallocate size for per-adapter stream buffer");
  42. /* Allow the debug level to be changed after module load.
  43. E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
  44. */
  45. module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
  46. MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
  47. /* List of adapters found */
  48. static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
  49. /* Wrapper function to HPI_Message to enable dumping of the
  50. message and response types.
  51. */
  52. static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
  53. struct file *file)
  54. {
  55. if ((phm->adapter_index >= HPI_MAX_ADAPTERS)
  56. && (phm->object != HPI_OBJ_SUBSYSTEM))
  57. phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
  58. else
  59. hpi_send_recv_ex(phm, phr, file);
  60. }
  61. /* This is called from hpifunc.c functions, called by ALSA
  62. * (or other kernel process) In this case there is no file descriptor
  63. * available for the message cache code
  64. */
  65. void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
  66. {
  67. hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
  68. }
  69. EXPORT_SYMBOL(hpi_send_recv);
  70. /* for radio-asihpi */
  71. int asihpi_hpi_release(struct file *file)
  72. {
  73. struct hpi_message hm;
  74. struct hpi_response hr;
  75. /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
  76. /* close the subsystem just in case the application forgot to. */
  77. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  78. HPI_SUBSYS_CLOSE);
  79. hpi_send_recv_ex(&hm, &hr, file);
  80. return 0;
  81. }
  82. long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  83. {
  84. struct hpi_ioctl_linux __user *phpi_ioctl_data;
  85. void __user *puhm;
  86. void __user *puhr;
  87. union hpi_message_buffer_v1 *hm;
  88. union hpi_response_buffer_v1 *hr;
  89. u16 res_max_size;
  90. u32 uncopied_bytes;
  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. switch (hm->h.function) {
  132. case HPI_SUBSYS_CREATE_ADAPTER:
  133. case HPI_ADAPTER_DELETE:
  134. /* Application must not use these functions! */
  135. hr->h.size = sizeof(hr->h);
  136. hr->h.error = HPI_ERROR_INVALID_OPERATION;
  137. hr->h.function = hm->h.function;
  138. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  139. if (uncopied_bytes)
  140. err = -EFAULT;
  141. else
  142. err = 0;
  143. goto out;
  144. }
  145. hr->h.size = res_max_size;
  146. if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
  147. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  148. } else {
  149. u16 __user *ptr = NULL;
  150. u32 size = 0;
  151. /* -1=no data 0=read from user mem, 1=write to user mem */
  152. int wrflag = -1;
  153. struct hpi_adapter *pa = NULL;
  154. if (hm->h.adapter_index < ARRAY_SIZE(adapters))
  155. pa = &adapters[hm->h.adapter_index];
  156. if (!pa || !pa->adapter || !pa->adapter->type) {
  157. hpi_init_response(&hr->r0, hm->h.object,
  158. hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
  159. uncopied_bytes =
  160. copy_to_user(puhr, hr, sizeof(hr->h));
  161. if (uncopied_bytes)
  162. err = -EFAULT;
  163. else
  164. err = 0;
  165. goto out;
  166. }
  167. if (mutex_lock_interruptible(&pa->mutex)) {
  168. err = -EINTR;
  169. goto out;
  170. }
  171. /* Dig out any pointers embedded in the message. */
  172. switch (hm->h.function) {
  173. case HPI_OSTREAM_WRITE:
  174. case HPI_ISTREAM_READ:{
  175. /* Yes, sparse, this is correct. */
  176. ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
  177. size = hm->m0.u.d.u.data.data_size;
  178. /* Allocate buffer according to application request.
  179. ?Is it better to alloc/free for the duration
  180. of the transaction?
  181. */
  182. if (pa->buffer_size < size) {
  183. HPI_DEBUG_LOG(DEBUG,
  184. "Realloc adapter %d stream "
  185. "buffer from %zd to %d\n",
  186. hm->h.adapter_index,
  187. pa->buffer_size, size);
  188. if (pa->p_buffer) {
  189. pa->buffer_size = 0;
  190. vfree(pa->p_buffer);
  191. }
  192. pa->p_buffer = vmalloc(size);
  193. if (pa->p_buffer)
  194. pa->buffer_size = size;
  195. else {
  196. HPI_DEBUG_LOG(ERROR,
  197. "HPI could not allocate "
  198. "stream buffer size %d\n",
  199. size);
  200. mutex_unlock(&pa->mutex);
  201. err = -EINVAL;
  202. goto out;
  203. }
  204. }
  205. hm->m0.u.d.u.data.pb_data = pa->p_buffer;
  206. if (hm->h.function == HPI_ISTREAM_READ)
  207. /* from card, WRITE to user mem */
  208. wrflag = 1;
  209. else
  210. wrflag = 0;
  211. break;
  212. }
  213. default:
  214. size = 0;
  215. break;
  216. }
  217. if (size && (wrflag == 0)) {
  218. uncopied_bytes =
  219. copy_from_user(pa->p_buffer, ptr, size);
  220. if (uncopied_bytes)
  221. HPI_DEBUG_LOG(WARNING,
  222. "Missed %d of %d "
  223. "bytes from user\n", uncopied_bytes,
  224. size);
  225. }
  226. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  227. if (size && (wrflag == 1)) {
  228. uncopied_bytes =
  229. copy_to_user(ptr, pa->p_buffer, size);
  230. if (uncopied_bytes)
  231. HPI_DEBUG_LOG(WARNING,
  232. "Missed %d of %d " "bytes to user\n",
  233. uncopied_bytes, size);
  234. }
  235. mutex_unlock(&pa->mutex);
  236. }
  237. /* on return response size must be set */
  238. /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
  239. if (!hr->h.size) {
  240. HPI_DEBUG_LOG(ERROR, "response zero size\n");
  241. err = -EFAULT;
  242. goto out;
  243. }
  244. if (hr->h.size > res_max_size) {
  245. HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
  246. res_max_size);
  247. hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
  248. hr->h.specific_error = hr->h.size;
  249. hr->h.size = sizeof(hr->h);
  250. }
  251. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  252. if (uncopied_bytes) {
  253. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  254. err = -EFAULT;
  255. goto out;
  256. }
  257. out:
  258. kfree(hm);
  259. kfree(hr);
  260. return err;
  261. }
  262. static int asihpi_irq_count;
  263. static irqreturn_t asihpi_isr(int irq, void *dev_id)
  264. {
  265. struct hpi_adapter *a = dev_id;
  266. int handled;
  267. if (!a->adapter->irq_query_and_clear) {
  268. pr_err("asihpi_isr ASI%04X:%d no handler\n", a->adapter->type,
  269. a->adapter->index);
  270. return IRQ_NONE;
  271. }
  272. handled = a->adapter->irq_query_and_clear(a->adapter, 0);
  273. if (!handled)
  274. return IRQ_NONE;
  275. asihpi_irq_count++;
  276. /* printk(KERN_INFO "asihpi_isr %d ASI%04X:%d irq handled\n",
  277. asihpi_irq_count, a->adapter->type, a->adapter->index); */
  278. if (a->interrupt_callback)
  279. a->interrupt_callback(a);
  280. return IRQ_HANDLED;
  281. }
  282. int asihpi_adapter_probe(struct pci_dev *pci_dev,
  283. const struct pci_device_id *pci_id)
  284. {
  285. int idx, nm, low_latency_mode = 0, irq_supported = 0;
  286. int adapter_index;
  287. unsigned int memlen;
  288. struct hpi_message hm;
  289. struct hpi_response hr;
  290. struct hpi_adapter adapter;
  291. struct hpi_pci pci;
  292. memset(&adapter, 0, sizeof(adapter));
  293. dev_printk(KERN_DEBUG, &pci_dev->dev,
  294. "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
  295. pci_dev->device, pci_dev->subsystem_vendor,
  296. pci_dev->subsystem_device, pci_dev->devfn);
  297. if (pci_enable_device(pci_dev) < 0) {
  298. dev_err(&pci_dev->dev,
  299. "pci_enable_device failed, disabling device\n");
  300. return -EIO;
  301. }
  302. pci_set_master(pci_dev); /* also sets latency timer if < 16 */
  303. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  304. HPI_SUBSYS_CREATE_ADAPTER);
  305. hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
  306. HPI_ERROR_PROCESSING_MESSAGE);
  307. hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
  308. nm = HPI_MAX_ADAPTER_MEM_SPACES;
  309. for (idx = 0; idx < nm; idx++) {
  310. HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
  311. &pci_dev->resource[idx]);
  312. if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
  313. memlen = pci_resource_len(pci_dev, idx);
  314. pci.ap_mem_base[idx] =
  315. ioremap(pci_resource_start(pci_dev, idx),
  316. memlen);
  317. if (!pci.ap_mem_base[idx]) {
  318. HPI_DEBUG_LOG(ERROR,
  319. "ioremap failed, aborting\n");
  320. /* unmap previously mapped pci mem space */
  321. goto err;
  322. }
  323. }
  324. }
  325. pci.pci_dev = pci_dev;
  326. hm.u.s.resource.bus_type = HPI_BUS_PCI;
  327. hm.u.s.resource.r.pci = &pci;
  328. /* call CreateAdapterObject on the relevant hpi module */
  329. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  330. if (hr.error)
  331. goto err;
  332. adapter_index = hr.u.s.adapter_index;
  333. adapter.adapter = hpi_find_adapter(adapter_index);
  334. if (prealloc_stream_buf) {
  335. adapter.p_buffer = vmalloc(prealloc_stream_buf);
  336. if (!adapter.p_buffer) {
  337. HPI_DEBUG_LOG(ERROR,
  338. "HPI could not allocate "
  339. "kernel buffer size %d\n",
  340. prealloc_stream_buf);
  341. goto err;
  342. }
  343. }
  344. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  345. HPI_ADAPTER_OPEN);
  346. hm.adapter_index = adapter.adapter->index;
  347. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  348. if (hr.error) {
  349. HPI_DEBUG_LOG(ERROR, "HPI_ADAPTER_OPEN failed, aborting\n");
  350. goto err;
  351. }
  352. /* Check if current mode == Low Latency mode */
  353. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  354. HPI_ADAPTER_GET_MODE);
  355. hm.adapter_index = adapter.adapter->index;
  356. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  357. if (!hr.error
  358. && hr.u.ax.mode.adapter_mode == HPI_ADAPTER_MODE_LOW_LATENCY)
  359. low_latency_mode = 1;
  360. else
  361. dev_info(&pci_dev->dev,
  362. "Adapter at index %d is not in low latency mode\n",
  363. adapter.adapter->index);
  364. /* Check if IRQs are supported */
  365. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  366. HPI_ADAPTER_GET_PROPERTY);
  367. hm.adapter_index = adapter.adapter->index;
  368. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_SUPPORTS_IRQ;
  369. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  370. if (hr.error || !hr.u.ax.property_get.parameter1) {
  371. dev_info(&pci_dev->dev,
  372. "IRQs not supported by adapter at index %d\n",
  373. adapter.adapter->index);
  374. } else {
  375. irq_supported = 1;
  376. }
  377. /* WARNING can't init mutex in 'adapter'
  378. * and then copy it to adapters[] ?!?!
  379. */
  380. adapters[adapter_index] = adapter;
  381. mutex_init(&adapters[adapter_index].mutex);
  382. pci_set_drvdata(pci_dev, &adapters[adapter_index]);
  383. if (low_latency_mode && irq_supported) {
  384. if (!adapter.adapter->irq_query_and_clear) {
  385. dev_err(&pci_dev->dev,
  386. "no IRQ handler for adapter %d, aborting\n",
  387. adapter.adapter->index);
  388. goto err;
  389. }
  390. /* Disable IRQ generation on DSP side by setting the rate to 0 */
  391. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  392. HPI_ADAPTER_SET_PROPERTY);
  393. hm.adapter_index = adapter.adapter->index;
  394. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
  395. hm.u.ax.property_set.parameter1 = 0;
  396. hm.u.ax.property_set.parameter2 = 0;
  397. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  398. if (hr.error) {
  399. HPI_DEBUG_LOG(ERROR,
  400. "HPI_ADAPTER_GET_MODE failed, aborting\n");
  401. goto err;
  402. }
  403. /* Note: request_irq calls asihpi_isr here */
  404. if (request_irq(pci_dev->irq, asihpi_isr, IRQF_SHARED,
  405. "asihpi", &adapters[adapter_index])) {
  406. dev_err(&pci_dev->dev, "request_irq(%d) failed\n",
  407. pci_dev->irq);
  408. goto err;
  409. }
  410. adapters[adapter_index].interrupt_mode = 1;
  411. dev_info(&pci_dev->dev, "using irq %d\n", pci_dev->irq);
  412. adapters[adapter_index].irq = pci_dev->irq;
  413. } else {
  414. dev_info(&pci_dev->dev, "using polled mode\n");
  415. }
  416. dev_info(&pci_dev->dev, "probe succeeded for ASI%04X HPI index %d\n",
  417. adapter.adapter->type, adapter_index);
  418. return 0;
  419. err:
  420. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  421. if (pci.ap_mem_base[idx]) {
  422. iounmap(pci.ap_mem_base[idx]);
  423. pci.ap_mem_base[idx] = NULL;
  424. }
  425. }
  426. if (adapter.p_buffer) {
  427. adapter.buffer_size = 0;
  428. vfree(adapter.p_buffer);
  429. }
  430. HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
  431. return -ENODEV;
  432. }
  433. void asihpi_adapter_remove(struct pci_dev *pci_dev)
  434. {
  435. int idx;
  436. struct hpi_message hm;
  437. struct hpi_response hr;
  438. struct hpi_adapter *pa;
  439. struct hpi_pci pci;
  440. pa = pci_get_drvdata(pci_dev);
  441. pci = pa->adapter->pci;
  442. /* Disable IRQ generation on DSP side */
  443. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  444. HPI_ADAPTER_SET_PROPERTY);
  445. hm.adapter_index = pa->adapter->index;
  446. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
  447. hm.u.ax.property_set.parameter1 = 0;
  448. hm.u.ax.property_set.parameter2 = 0;
  449. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  450. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  451. HPI_ADAPTER_DELETE);
  452. hm.adapter_index = pa->adapter->index;
  453. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  454. /* unmap PCI memory space, mapped during device init. */
  455. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  456. if (pci.ap_mem_base[idx])
  457. iounmap(pci.ap_mem_base[idx]);
  458. }
  459. if (pa->irq)
  460. free_irq(pa->irq, pa);
  461. vfree(pa->p_buffer);
  462. if (1)
  463. dev_info(&pci_dev->dev,
  464. "remove %04x:%04x,%04x:%04x,%04x, HPI index %d\n",
  465. pci_dev->vendor, pci_dev->device,
  466. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  467. pci_dev->devfn, pa->adapter->index);
  468. memset(pa, 0, sizeof(*pa));
  469. }
  470. void __init asihpi_init(void)
  471. {
  472. struct hpi_message hm;
  473. struct hpi_response hr;
  474. memset(adapters, 0, sizeof(adapters));
  475. printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
  476. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  477. HPI_SUBSYS_DRIVER_LOAD);
  478. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  479. }
  480. void asihpi_exit(void)
  481. {
  482. struct hpi_message hm;
  483. struct hpi_response hr;
  484. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  485. HPI_SUBSYS_DRIVER_UNLOAD);
  486. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  487. }