hpioctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 <linux/uaccess.h>
  26. #include <linux/pci.h>
  27. #include <linux/stringify.h>
  28. #include <linux/module.h>
  29. #include <linux/vmalloc.h>
  30. #ifdef MODULE_FIRMWARE
  31. MODULE_FIRMWARE("asihpi/dsp5000.bin");
  32. MODULE_FIRMWARE("asihpi/dsp6200.bin");
  33. MODULE_FIRMWARE("asihpi/dsp6205.bin");
  34. MODULE_FIRMWARE("asihpi/dsp6400.bin");
  35. MODULE_FIRMWARE("asihpi/dsp6600.bin");
  36. MODULE_FIRMWARE("asihpi/dsp8700.bin");
  37. MODULE_FIRMWARE("asihpi/dsp8900.bin");
  38. #endif
  39. static int prealloc_stream_buf;
  40. module_param(prealloc_stream_buf, int, S_IRUGO);
  41. MODULE_PARM_DESC(prealloc_stream_buf,
  42. "Preallocate size for per-adapter stream buffer");
  43. /* Allow the debug level to be changed after module load.
  44. E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
  45. */
  46. module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
  47. MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
  48. /* List of adapters found */
  49. static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
  50. /* Wrapper function to HPI_Message to enable dumping of the
  51. message and response types.
  52. */
  53. static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
  54. struct file *file)
  55. {
  56. if ((phm->adapter_index >= HPI_MAX_ADAPTERS)
  57. && (phm->object != HPI_OBJ_SUBSYSTEM))
  58. phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
  59. else
  60. hpi_send_recv_ex(phm, phr, file);
  61. }
  62. /* This is called from hpifunc.c functions, called by ALSA
  63. * (or other kernel process) In this case there is no file descriptor
  64. * available for the message cache code
  65. */
  66. void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
  67. {
  68. hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
  69. }
  70. EXPORT_SYMBOL(hpi_send_recv);
  71. /* for radio-asihpi */
  72. int asihpi_hpi_release(struct file *file)
  73. {
  74. struct hpi_message hm;
  75. struct hpi_response hr;
  76. /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
  77. /* close the subsystem just in case the application forgot to. */
  78. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  79. HPI_SUBSYS_CLOSE);
  80. hpi_send_recv_ex(&hm, &hr, file);
  81. return 0;
  82. }
  83. long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  84. {
  85. struct hpi_ioctl_linux __user *phpi_ioctl_data;
  86. void __user *puhm;
  87. void __user *puhr;
  88. union hpi_message_buffer_v1 *hm;
  89. union hpi_response_buffer_v1 *hr;
  90. u16 res_max_size;
  91. u32 uncopied_bytes;
  92. int err = 0;
  93. if (cmd != HPI_IOCTL_LINUX)
  94. return -EINVAL;
  95. hm = kmalloc(sizeof(*hm), GFP_KERNEL);
  96. hr = kzalloc(sizeof(*hr), GFP_KERNEL);
  97. if (!hm || !hr) {
  98. err = -ENOMEM;
  99. goto out;
  100. }
  101. phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
  102. /* Read the message and response pointers from user space. */
  103. if (get_user(puhm, &phpi_ioctl_data->phm)
  104. || get_user(puhr, &phpi_ioctl_data->phr)) {
  105. err = -EFAULT;
  106. goto out;
  107. }
  108. /* Now read the message size and data from user space. */
  109. if (get_user(hm->h.size, (u16 __user *)puhm)) {
  110. err = -EFAULT;
  111. goto out;
  112. }
  113. if (hm->h.size > sizeof(*hm))
  114. hm->h.size = sizeof(*hm);
  115. /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
  116. uncopied_bytes = copy_from_user(hm, puhm, hm->h.size);
  117. if (uncopied_bytes) {
  118. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  119. err = -EFAULT;
  120. goto out;
  121. }
  122. if (get_user(res_max_size, (u16 __user *)puhr)) {
  123. err = -EFAULT;
  124. goto out;
  125. }
  126. /* printk(KERN_INFO "user response size %d\n", res_max_size); */
  127. if (res_max_size < sizeof(struct hpi_response_header)) {
  128. HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
  129. err = -EFAULT;
  130. goto out;
  131. }
  132. res_max_size = min_t(size_t, res_max_size, sizeof(*hr));
  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. static int asihpi_irq_count;
  265. static irqreturn_t asihpi_isr(int irq, void *dev_id)
  266. {
  267. struct hpi_adapter *a = dev_id;
  268. int handled;
  269. if (!a->adapter->irq_query_and_clear) {
  270. pr_err("asihpi_isr ASI%04X:%d no handler\n", a->adapter->type,
  271. a->adapter->index);
  272. return IRQ_NONE;
  273. }
  274. handled = a->adapter->irq_query_and_clear(a->adapter, 0);
  275. if (!handled)
  276. return IRQ_NONE;
  277. asihpi_irq_count++;
  278. /* printk(KERN_INFO "asihpi_isr %d ASI%04X:%d irq handled\n",
  279. asihpi_irq_count, a->adapter->type, a->adapter->index); */
  280. if (a->interrupt_callback)
  281. a->interrupt_callback(a);
  282. return IRQ_HANDLED;
  283. }
  284. int asihpi_adapter_probe(struct pci_dev *pci_dev,
  285. const struct pci_device_id *pci_id)
  286. {
  287. int idx, nm, low_latency_mode = 0, irq_supported = 0;
  288. int adapter_index;
  289. unsigned int memlen;
  290. struct hpi_message hm;
  291. struct hpi_response hr;
  292. struct hpi_adapter adapter;
  293. struct hpi_pci pci;
  294. memset(&adapter, 0, sizeof(adapter));
  295. dev_printk(KERN_DEBUG, &pci_dev->dev,
  296. "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
  297. pci_dev->device, pci_dev->subsystem_vendor,
  298. pci_dev->subsystem_device, pci_dev->devfn);
  299. if (pci_enable_device(pci_dev) < 0) {
  300. dev_err(&pci_dev->dev,
  301. "pci_enable_device failed, disabling device\n");
  302. return -EIO;
  303. }
  304. pci_set_master(pci_dev); /* also sets latency timer if < 16 */
  305. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  306. HPI_SUBSYS_CREATE_ADAPTER);
  307. hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
  308. HPI_ERROR_PROCESSING_MESSAGE);
  309. hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
  310. nm = HPI_MAX_ADAPTER_MEM_SPACES;
  311. for (idx = 0; idx < nm; idx++) {
  312. HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
  313. &pci_dev->resource[idx]);
  314. if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
  315. memlen = pci_resource_len(pci_dev, idx);
  316. pci.ap_mem_base[idx] =
  317. ioremap(pci_resource_start(pci_dev, idx),
  318. memlen);
  319. if (!pci.ap_mem_base[idx]) {
  320. HPI_DEBUG_LOG(ERROR,
  321. "ioremap failed, aborting\n");
  322. /* unmap previously mapped pci mem space */
  323. goto err;
  324. }
  325. }
  326. }
  327. pci.pci_dev = pci_dev;
  328. hm.u.s.resource.bus_type = HPI_BUS_PCI;
  329. hm.u.s.resource.r.pci = &pci;
  330. /* call CreateAdapterObject on the relevant hpi module */
  331. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  332. if (hr.error)
  333. goto err;
  334. adapter_index = hr.u.s.adapter_index;
  335. adapter.adapter = hpi_find_adapter(adapter_index);
  336. if (prealloc_stream_buf) {
  337. adapter.p_buffer = vmalloc(prealloc_stream_buf);
  338. if (!adapter.p_buffer) {
  339. HPI_DEBUG_LOG(ERROR,
  340. "HPI could not allocate "
  341. "kernel buffer size %d\n",
  342. prealloc_stream_buf);
  343. goto err;
  344. }
  345. }
  346. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  347. HPI_ADAPTER_OPEN);
  348. hm.adapter_index = adapter.adapter->index;
  349. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  350. if (hr.error) {
  351. HPI_DEBUG_LOG(ERROR, "HPI_ADAPTER_OPEN failed, aborting\n");
  352. goto err;
  353. }
  354. /* Check if current mode == Low Latency mode */
  355. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  356. HPI_ADAPTER_GET_MODE);
  357. hm.adapter_index = adapter.adapter->index;
  358. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  359. if (!hr.error
  360. && hr.u.ax.mode.adapter_mode == HPI_ADAPTER_MODE_LOW_LATENCY)
  361. low_latency_mode = 1;
  362. else
  363. dev_info(&pci_dev->dev,
  364. "Adapter at index %d is not in low latency mode\n",
  365. adapter.adapter->index);
  366. /* Check if IRQs are supported */
  367. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  368. HPI_ADAPTER_GET_PROPERTY);
  369. hm.adapter_index = adapter.adapter->index;
  370. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_SUPPORTS_IRQ;
  371. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  372. if (hr.error || !hr.u.ax.property_get.parameter1) {
  373. dev_info(&pci_dev->dev,
  374. "IRQs not supported by adapter at index %d\n",
  375. adapter.adapter->index);
  376. } else {
  377. irq_supported = 1;
  378. }
  379. /* WARNING can't init mutex in 'adapter'
  380. * and then copy it to adapters[] ?!?!
  381. */
  382. adapters[adapter_index] = adapter;
  383. mutex_init(&adapters[adapter_index].mutex);
  384. pci_set_drvdata(pci_dev, &adapters[adapter_index]);
  385. if (low_latency_mode && irq_supported) {
  386. if (!adapter.adapter->irq_query_and_clear) {
  387. dev_err(&pci_dev->dev,
  388. "no IRQ handler for adapter %d, aborting\n",
  389. adapter.adapter->index);
  390. goto err;
  391. }
  392. /* Disable IRQ generation on DSP side by setting the rate to 0 */
  393. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  394. HPI_ADAPTER_SET_PROPERTY);
  395. hm.adapter_index = adapter.adapter->index;
  396. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
  397. hm.u.ax.property_set.parameter1 = 0;
  398. hm.u.ax.property_set.parameter2 = 0;
  399. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  400. if (hr.error) {
  401. HPI_DEBUG_LOG(ERROR,
  402. "HPI_ADAPTER_GET_MODE failed, aborting\n");
  403. goto err;
  404. }
  405. /* Note: request_irq calls asihpi_isr here */
  406. if (request_irq(pci_dev->irq, asihpi_isr, IRQF_SHARED,
  407. "asihpi", &adapters[adapter_index])) {
  408. dev_err(&pci_dev->dev, "request_irq(%d) failed\n",
  409. pci_dev->irq);
  410. goto err;
  411. }
  412. adapters[adapter_index].interrupt_mode = 1;
  413. dev_info(&pci_dev->dev, "using irq %d\n", pci_dev->irq);
  414. adapters[adapter_index].irq = pci_dev->irq;
  415. } else {
  416. dev_info(&pci_dev->dev, "using polled mode\n");
  417. }
  418. dev_info(&pci_dev->dev, "probe succeeded for ASI%04X HPI index %d\n",
  419. adapter.adapter->type, adapter_index);
  420. return 0;
  421. err:
  422. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  423. if (pci.ap_mem_base[idx]) {
  424. iounmap(pci.ap_mem_base[idx]);
  425. pci.ap_mem_base[idx] = NULL;
  426. }
  427. }
  428. if (adapter.p_buffer) {
  429. adapter.buffer_size = 0;
  430. vfree(adapter.p_buffer);
  431. }
  432. HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
  433. return -ENODEV;
  434. }
  435. void asihpi_adapter_remove(struct pci_dev *pci_dev)
  436. {
  437. int idx;
  438. struct hpi_message hm;
  439. struct hpi_response hr;
  440. struct hpi_adapter *pa;
  441. struct hpi_pci pci;
  442. pa = pci_get_drvdata(pci_dev);
  443. pci = pa->adapter->pci;
  444. /* Disable IRQ generation on DSP side */
  445. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  446. HPI_ADAPTER_SET_PROPERTY);
  447. hm.adapter_index = pa->adapter->index;
  448. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
  449. hm.u.ax.property_set.parameter1 = 0;
  450. hm.u.ax.property_set.parameter2 = 0;
  451. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  452. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  453. HPI_ADAPTER_DELETE);
  454. hm.adapter_index = pa->adapter->index;
  455. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  456. /* unmap PCI memory space, mapped during device init. */
  457. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; ++idx)
  458. iounmap(pci.ap_mem_base[idx]);
  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. }