hpioctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. #include <linux/nospec.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, 0444);
  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, 0644);
  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 msg_size;
  92. u16 res_max_size;
  93. u32 uncopied_bytes;
  94. int err = 0;
  95. if (cmd != HPI_IOCTL_LINUX)
  96. return -EINVAL;
  97. hm = kmalloc(sizeof(*hm), GFP_KERNEL);
  98. hr = kzalloc(sizeof(*hr), GFP_KERNEL);
  99. if (!hm || !hr) {
  100. err = -ENOMEM;
  101. goto out;
  102. }
  103. phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
  104. /* Read the message and response pointers from user space. */
  105. if (get_user(puhm, &phpi_ioctl_data->phm)
  106. || get_user(puhr, &phpi_ioctl_data->phr)) {
  107. err = -EFAULT;
  108. goto out;
  109. }
  110. /* Now read the message size and data from user space. */
  111. if (get_user(msg_size, (u16 __user *)puhm)) {
  112. err = -EFAULT;
  113. goto out;
  114. }
  115. if (msg_size > sizeof(*hm))
  116. msg_size = sizeof(*hm);
  117. /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
  118. uncopied_bytes = copy_from_user(hm, puhm, msg_size);
  119. if (uncopied_bytes) {
  120. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  121. err = -EFAULT;
  122. goto out;
  123. }
  124. /* Override h.size in case it is changed between two userspace fetches */
  125. hm->h.size = msg_size;
  126. if (get_user(res_max_size, (u16 __user *)puhr)) {
  127. err = -EFAULT;
  128. goto out;
  129. }
  130. /* printk(KERN_INFO "user response size %d\n", res_max_size); */
  131. if (res_max_size < sizeof(struct hpi_response_header)) {
  132. HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
  133. err = -EFAULT;
  134. goto out;
  135. }
  136. res_max_size = min_t(size_t, res_max_size, sizeof(*hr));
  137. switch (hm->h.function) {
  138. case HPI_SUBSYS_CREATE_ADAPTER:
  139. case HPI_ADAPTER_DELETE:
  140. /* Application must not use these functions! */
  141. hr->h.size = sizeof(hr->h);
  142. hr->h.error = HPI_ERROR_INVALID_OPERATION;
  143. hr->h.function = hm->h.function;
  144. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  145. if (uncopied_bytes)
  146. err = -EFAULT;
  147. else
  148. err = 0;
  149. goto out;
  150. }
  151. hr->h.size = res_max_size;
  152. if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
  153. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  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. struct hpi_adapter *pa = NULL;
  160. if (hm->h.adapter_index < ARRAY_SIZE(adapters))
  161. pa = &adapters[array_index_nospec(hm->h.adapter_index,
  162. ARRAY_SIZE(adapters))];
  163. if (!pa || !pa->adapter || !pa->adapter->type) {
  164. hpi_init_response(&hr->r0, hm->h.object,
  165. hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
  166. uncopied_bytes =
  167. copy_to_user(puhr, hr, sizeof(hr->h));
  168. if (uncopied_bytes)
  169. err = -EFAULT;
  170. else
  171. err = 0;
  172. goto out;
  173. }
  174. if (mutex_lock_interruptible(&pa->mutex)) {
  175. err = -EINTR;
  176. goto out;
  177. }
  178. /* Dig out any pointers embedded in the message. */
  179. switch (hm->h.function) {
  180. case HPI_OSTREAM_WRITE:
  181. case HPI_ISTREAM_READ:{
  182. /* Yes, sparse, this is correct. */
  183. ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
  184. size = hm->m0.u.d.u.data.data_size;
  185. /* Allocate buffer according to application request.
  186. ?Is it better to alloc/free for the duration
  187. of the transaction?
  188. */
  189. if (pa->buffer_size < size) {
  190. HPI_DEBUG_LOG(DEBUG,
  191. "Realloc adapter %d stream "
  192. "buffer from %zd to %d\n",
  193. hm->h.adapter_index,
  194. pa->buffer_size, size);
  195. if (pa->p_buffer) {
  196. pa->buffer_size = 0;
  197. vfree(pa->p_buffer);
  198. }
  199. pa->p_buffer = vmalloc(size);
  200. if (pa->p_buffer)
  201. pa->buffer_size = size;
  202. else {
  203. HPI_DEBUG_LOG(ERROR,
  204. "HPI could not allocate "
  205. "stream buffer size %d\n",
  206. size);
  207. mutex_unlock(&pa->mutex);
  208. err = -EINVAL;
  209. goto out;
  210. }
  211. }
  212. hm->m0.u.d.u.data.pb_data = pa->p_buffer;
  213. if (hm->h.function == HPI_ISTREAM_READ)
  214. /* from card, WRITE to user mem */
  215. wrflag = 1;
  216. else
  217. wrflag = 0;
  218. break;
  219. }
  220. default:
  221. size = 0;
  222. break;
  223. }
  224. if (size && (wrflag == 0)) {
  225. uncopied_bytes =
  226. copy_from_user(pa->p_buffer, ptr, size);
  227. if (uncopied_bytes)
  228. HPI_DEBUG_LOG(WARNING,
  229. "Missed %d of %d "
  230. "bytes from user\n", uncopied_bytes,
  231. size);
  232. }
  233. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  234. if (size && (wrflag == 1)) {
  235. uncopied_bytes =
  236. copy_to_user(ptr, pa->p_buffer, size);
  237. if (uncopied_bytes)
  238. HPI_DEBUG_LOG(WARNING,
  239. "Missed %d of %d " "bytes to user\n",
  240. uncopied_bytes, size);
  241. }
  242. mutex_unlock(&pa->mutex);
  243. }
  244. /* on return response size must be set */
  245. /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
  246. if (!hr->h.size) {
  247. HPI_DEBUG_LOG(ERROR, "response zero size\n");
  248. err = -EFAULT;
  249. goto out;
  250. }
  251. if (hr->h.size > res_max_size) {
  252. HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
  253. res_max_size);
  254. hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
  255. hr->h.specific_error = hr->h.size;
  256. hr->h.size = sizeof(hr->h);
  257. }
  258. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  259. if (uncopied_bytes) {
  260. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  261. err = -EFAULT;
  262. goto out;
  263. }
  264. out:
  265. kfree(hm);
  266. kfree(hr);
  267. return err;
  268. }
  269. static int asihpi_irq_count;
  270. static irqreturn_t asihpi_isr(int irq, void *dev_id)
  271. {
  272. struct hpi_adapter *a = dev_id;
  273. int handled;
  274. if (!a->adapter->irq_query_and_clear) {
  275. pr_err("asihpi_isr ASI%04X:%d no handler\n", a->adapter->type,
  276. a->adapter->index);
  277. return IRQ_NONE;
  278. }
  279. handled = a->adapter->irq_query_and_clear(a->adapter, 0);
  280. if (!handled)
  281. return IRQ_NONE;
  282. asihpi_irq_count++;
  283. /* printk(KERN_INFO "asihpi_isr %d ASI%04X:%d irq handled\n",
  284. asihpi_irq_count, a->adapter->type, a->adapter->index); */
  285. if (a->interrupt_callback)
  286. a->interrupt_callback(a);
  287. return IRQ_HANDLED;
  288. }
  289. int asihpi_adapter_probe(struct pci_dev *pci_dev,
  290. const struct pci_device_id *pci_id)
  291. {
  292. int idx, nm, low_latency_mode = 0, irq_supported = 0;
  293. int adapter_index;
  294. unsigned int memlen;
  295. struct hpi_message hm;
  296. struct hpi_response hr;
  297. struct hpi_adapter adapter;
  298. struct hpi_pci pci = { 0 };
  299. memset(&adapter, 0, sizeof(adapter));
  300. dev_printk(KERN_DEBUG, &pci_dev->dev,
  301. "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
  302. pci_dev->device, pci_dev->subsystem_vendor,
  303. pci_dev->subsystem_device, pci_dev->devfn);
  304. if (pci_enable_device(pci_dev) < 0) {
  305. dev_err(&pci_dev->dev,
  306. "pci_enable_device failed, disabling device\n");
  307. return -EIO;
  308. }
  309. pci_set_master(pci_dev); /* also sets latency timer if < 16 */
  310. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  311. HPI_SUBSYS_CREATE_ADAPTER);
  312. hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
  313. HPI_ERROR_PROCESSING_MESSAGE);
  314. hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
  315. nm = HPI_MAX_ADAPTER_MEM_SPACES;
  316. for (idx = 0; idx < nm; idx++) {
  317. HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
  318. &pci_dev->resource[idx]);
  319. if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
  320. memlen = pci_resource_len(pci_dev, idx);
  321. pci.ap_mem_base[idx] =
  322. ioremap(pci_resource_start(pci_dev, idx),
  323. memlen);
  324. if (!pci.ap_mem_base[idx]) {
  325. HPI_DEBUG_LOG(ERROR,
  326. "ioremap failed, aborting\n");
  327. /* unmap previously mapped pci mem space */
  328. goto err;
  329. }
  330. }
  331. }
  332. pci.pci_dev = pci_dev;
  333. hm.u.s.resource.bus_type = HPI_BUS_PCI;
  334. hm.u.s.resource.r.pci = &pci;
  335. /* call CreateAdapterObject on the relevant hpi module */
  336. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  337. if (hr.error)
  338. goto err;
  339. adapter_index = hr.u.s.adapter_index;
  340. adapter.adapter = hpi_find_adapter(adapter_index);
  341. if (prealloc_stream_buf) {
  342. adapter.p_buffer = vmalloc(prealloc_stream_buf);
  343. if (!adapter.p_buffer) {
  344. HPI_DEBUG_LOG(ERROR,
  345. "HPI could not allocate "
  346. "kernel buffer size %d\n",
  347. prealloc_stream_buf);
  348. goto err;
  349. }
  350. }
  351. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  352. HPI_ADAPTER_OPEN);
  353. hm.adapter_index = adapter.adapter->index;
  354. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  355. if (hr.error) {
  356. HPI_DEBUG_LOG(ERROR, "HPI_ADAPTER_OPEN failed, aborting\n");
  357. goto err;
  358. }
  359. /* Check if current mode == Low Latency mode */
  360. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  361. HPI_ADAPTER_GET_MODE);
  362. hm.adapter_index = adapter.adapter->index;
  363. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  364. if (!hr.error
  365. && hr.u.ax.mode.adapter_mode == HPI_ADAPTER_MODE_LOW_LATENCY)
  366. low_latency_mode = 1;
  367. else
  368. dev_info(&pci_dev->dev,
  369. "Adapter at index %d is not in low latency mode\n",
  370. adapter.adapter->index);
  371. /* Check if IRQs are supported */
  372. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  373. HPI_ADAPTER_GET_PROPERTY);
  374. hm.adapter_index = adapter.adapter->index;
  375. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_SUPPORTS_IRQ;
  376. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  377. if (hr.error || !hr.u.ax.property_get.parameter1) {
  378. dev_info(&pci_dev->dev,
  379. "IRQs not supported by adapter at index %d\n",
  380. adapter.adapter->index);
  381. } else {
  382. irq_supported = 1;
  383. }
  384. /* WARNING can't init mutex in 'adapter'
  385. * and then copy it to adapters[] ?!?!
  386. */
  387. adapters[adapter_index] = adapter;
  388. mutex_init(&adapters[adapter_index].mutex);
  389. pci_set_drvdata(pci_dev, &adapters[adapter_index]);
  390. if (low_latency_mode && irq_supported) {
  391. if (!adapter.adapter->irq_query_and_clear) {
  392. dev_err(&pci_dev->dev,
  393. "no IRQ handler for adapter %d, aborting\n",
  394. adapter.adapter->index);
  395. goto err;
  396. }
  397. /* Disable IRQ generation on DSP side by setting the rate to 0 */
  398. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  399. HPI_ADAPTER_SET_PROPERTY);
  400. hm.adapter_index = adapter.adapter->index;
  401. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
  402. hm.u.ax.property_set.parameter1 = 0;
  403. hm.u.ax.property_set.parameter2 = 0;
  404. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  405. if (hr.error) {
  406. HPI_DEBUG_LOG(ERROR,
  407. "HPI_ADAPTER_GET_MODE failed, aborting\n");
  408. goto err;
  409. }
  410. /* Note: request_irq calls asihpi_isr here */
  411. if (request_irq(pci_dev->irq, asihpi_isr, IRQF_SHARED,
  412. "asihpi", &adapters[adapter_index])) {
  413. dev_err(&pci_dev->dev, "request_irq(%d) failed\n",
  414. pci_dev->irq);
  415. goto err;
  416. }
  417. adapters[adapter_index].interrupt_mode = 1;
  418. dev_info(&pci_dev->dev, "using irq %d\n", pci_dev->irq);
  419. adapters[adapter_index].irq = pci_dev->irq;
  420. } else {
  421. dev_info(&pci_dev->dev, "using polled mode\n");
  422. }
  423. dev_info(&pci_dev->dev, "probe succeeded for ASI%04X HPI index %d\n",
  424. adapter.adapter->type, adapter_index);
  425. return 0;
  426. err:
  427. while (--idx >= 0) {
  428. if (pci.ap_mem_base[idx]) {
  429. iounmap(pci.ap_mem_base[idx]);
  430. pci.ap_mem_base[idx] = NULL;
  431. }
  432. }
  433. if (adapter.p_buffer) {
  434. adapter.buffer_size = 0;
  435. vfree(adapter.p_buffer);
  436. }
  437. HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
  438. return -ENODEV;
  439. }
  440. void asihpi_adapter_remove(struct pci_dev *pci_dev)
  441. {
  442. int idx;
  443. struct hpi_message hm;
  444. struct hpi_response hr;
  445. struct hpi_adapter *pa;
  446. struct hpi_pci pci;
  447. pa = pci_get_drvdata(pci_dev);
  448. pci = pa->adapter->pci;
  449. /* Disable IRQ generation on DSP side */
  450. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  451. HPI_ADAPTER_SET_PROPERTY);
  452. hm.adapter_index = pa->adapter->index;
  453. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
  454. hm.u.ax.property_set.parameter1 = 0;
  455. hm.u.ax.property_set.parameter2 = 0;
  456. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  457. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  458. HPI_ADAPTER_DELETE);
  459. hm.adapter_index = pa->adapter->index;
  460. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  461. /* unmap PCI memory space, mapped during device init. */
  462. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; ++idx)
  463. iounmap(pci.ap_mem_base[idx]);
  464. if (pa->irq)
  465. free_irq(pa->irq, pa);
  466. vfree(pa->p_buffer);
  467. if (1)
  468. dev_info(&pci_dev->dev,
  469. "remove %04x:%04x,%04x:%04x,%04x, HPI index %d\n",
  470. pci_dev->vendor, pci_dev->device,
  471. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  472. pci_dev->devfn, pa->adapter->index);
  473. memset(pa, 0, sizeof(*pa));
  474. }
  475. void __init asihpi_init(void)
  476. {
  477. struct hpi_message hm;
  478. struct hpi_response hr;
  479. memset(adapters, 0, sizeof(adapters));
  480. printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
  481. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  482. HPI_SUBSYS_DRIVER_LOAD);
  483. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  484. }
  485. void asihpi_exit(void)
  486. {
  487. struct hpi_message hm;
  488. struct hpi_response hr;
  489. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  490. HPI_SUBSYS_DRIVER_UNLOAD);
  491. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  492. }