hpioctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*******************************************************************************
  3. AudioScience HPI driver
  4. Common Linux HPI ioctl and module probe/remove functions
  5. Copyright (C) 1997-2014 AudioScience Inc. <[email protected]>
  6. *******************************************************************************/
  7. #define SOURCEFILE_NAME "hpioctl.c"
  8. #include "hpi_internal.h"
  9. #include "hpi_version.h"
  10. #include "hpimsginit.h"
  11. #include "hpidebug.h"
  12. #include "hpimsgx.h"
  13. #include "hpioctl.h"
  14. #include "hpicmn.h"
  15. #include <linux/fs.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/slab.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/pci.h>
  21. #include <linux/stringify.h>
  22. #include <linux/module.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/nospec.h>
  25. #ifdef MODULE_FIRMWARE
  26. MODULE_FIRMWARE("asihpi/dsp5000.bin");
  27. MODULE_FIRMWARE("asihpi/dsp6200.bin");
  28. MODULE_FIRMWARE("asihpi/dsp6205.bin");
  29. MODULE_FIRMWARE("asihpi/dsp6400.bin");
  30. MODULE_FIRMWARE("asihpi/dsp6600.bin");
  31. MODULE_FIRMWARE("asihpi/dsp8700.bin");
  32. MODULE_FIRMWARE("asihpi/dsp8900.bin");
  33. #endif
  34. static int prealloc_stream_buf;
  35. module_param(prealloc_stream_buf, int, 0444);
  36. MODULE_PARM_DESC(prealloc_stream_buf,
  37. "Preallocate size for per-adapter stream buffer");
  38. /* Allow the debug level to be changed after module load.
  39. E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
  40. */
  41. module_param(hpi_debug_level, int, 0644);
  42. MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
  43. /* List of adapters found */
  44. static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
  45. /* Wrapper function to HPI_Message to enable dumping of the
  46. message and response types.
  47. */
  48. static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
  49. struct file *file)
  50. {
  51. if ((phm->adapter_index >= HPI_MAX_ADAPTERS)
  52. && (phm->object != HPI_OBJ_SUBSYSTEM))
  53. phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
  54. else
  55. hpi_send_recv_ex(phm, phr, file);
  56. }
  57. /* This is called from hpifunc.c functions, called by ALSA
  58. * (or other kernel process) In this case there is no file descriptor
  59. * available for the message cache code
  60. */
  61. void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
  62. {
  63. hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
  64. }
  65. EXPORT_SYMBOL(hpi_send_recv);
  66. /* for radio-asihpi */
  67. int asihpi_hpi_release(struct file *file)
  68. {
  69. struct hpi_message hm;
  70. struct hpi_response hr;
  71. /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
  72. /* close the subsystem just in case the application forgot to. */
  73. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  74. HPI_SUBSYS_CLOSE);
  75. hpi_send_recv_ex(&hm, &hr, file);
  76. return 0;
  77. }
  78. long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  79. {
  80. struct hpi_ioctl_linux __user *phpi_ioctl_data;
  81. void __user *puhm;
  82. void __user *puhr;
  83. union hpi_message_buffer_v1 *hm;
  84. union hpi_response_buffer_v1 *hr;
  85. u16 msg_size;
  86. u16 res_max_size;
  87. u32 uncopied_bytes;
  88. int err = 0;
  89. if (cmd != HPI_IOCTL_LINUX)
  90. return -EINVAL;
  91. hm = kmalloc(sizeof(*hm), GFP_KERNEL);
  92. hr = kzalloc(sizeof(*hr), GFP_KERNEL);
  93. if (!hm || !hr) {
  94. err = -ENOMEM;
  95. goto out;
  96. }
  97. phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
  98. /* Read the message and response pointers from user space. */
  99. if (get_user(puhm, &phpi_ioctl_data->phm)
  100. || get_user(puhr, &phpi_ioctl_data->phr)) {
  101. err = -EFAULT;
  102. goto out;
  103. }
  104. /* Now read the message size and data from user space. */
  105. if (get_user(msg_size, (u16 __user *)puhm)) {
  106. err = -EFAULT;
  107. goto out;
  108. }
  109. if (msg_size > sizeof(*hm))
  110. msg_size = sizeof(*hm);
  111. /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
  112. uncopied_bytes = copy_from_user(hm, puhm, msg_size);
  113. if (uncopied_bytes) {
  114. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  115. err = -EFAULT;
  116. goto out;
  117. }
  118. /* Override h.size in case it is changed between two userspace fetches */
  119. hm->h.size = msg_size;
  120. if (get_user(res_max_size, (u16 __user *)puhr)) {
  121. err = -EFAULT;
  122. goto out;
  123. }
  124. /* printk(KERN_INFO "user response size %d\n", res_max_size); */
  125. if (res_max_size < sizeof(struct hpi_response_header)) {
  126. HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
  127. err = -EFAULT;
  128. goto out;
  129. }
  130. res_max_size = min_t(size_t, res_max_size, sizeof(*hr));
  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[array_index_nospec(hm->h.adapter_index,
  156. ARRAY_SIZE(adapters))];
  157. if (!pa || !pa->adapter || !pa->adapter->type) {
  158. hpi_init_response(&hr->r0, hm->h.object,
  159. hm->h.function, 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(&pa->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(&pa->mutex);
  202. err = -EINVAL;
  203. goto out;
  204. }
  205. }
  206. hm->m0.u.d.u.data.pb_data = pa->p_buffer;
  207. if (hm->h.function == HPI_ISTREAM_READ)
  208. /* from card, WRITE to user mem */
  209. wrflag = 1;
  210. else
  211. wrflag = 0;
  212. break;
  213. }
  214. default:
  215. size = 0;
  216. break;
  217. }
  218. if (size && (wrflag == 0)) {
  219. uncopied_bytes =
  220. copy_from_user(pa->p_buffer, ptr, size);
  221. if (uncopied_bytes)
  222. HPI_DEBUG_LOG(WARNING,
  223. "Missed %d of %d "
  224. "bytes from user\n", uncopied_bytes,
  225. size);
  226. }
  227. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  228. if (size && (wrflag == 1)) {
  229. uncopied_bytes =
  230. copy_to_user(ptr, pa->p_buffer, size);
  231. if (uncopied_bytes)
  232. HPI_DEBUG_LOG(WARNING,
  233. "Missed %d of %d " "bytes to user\n",
  234. uncopied_bytes, size);
  235. }
  236. mutex_unlock(&pa->mutex);
  237. }
  238. /* on return response size must be set */
  239. /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
  240. if (!hr->h.size) {
  241. HPI_DEBUG_LOG(ERROR, "response zero size\n");
  242. err = -EFAULT;
  243. goto out;
  244. }
  245. if (hr->h.size > res_max_size) {
  246. HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
  247. res_max_size);
  248. hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
  249. hr->h.specific_error = hr->h.size;
  250. hr->h.size = sizeof(hr->h);
  251. }
  252. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  253. if (uncopied_bytes) {
  254. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  255. err = -EFAULT;
  256. goto out;
  257. }
  258. out:
  259. kfree(hm);
  260. kfree(hr);
  261. return err;
  262. }
  263. static int asihpi_irq_count;
  264. static irqreturn_t asihpi_isr(int irq, void *dev_id)
  265. {
  266. struct hpi_adapter *a = dev_id;
  267. int handled;
  268. if (!a->adapter->irq_query_and_clear) {
  269. pr_err("asihpi_isr ASI%04X:%d no handler\n", a->adapter->type,
  270. a->adapter->index);
  271. return IRQ_NONE;
  272. }
  273. handled = a->adapter->irq_query_and_clear(a->adapter, 0);
  274. if (!handled)
  275. return IRQ_NONE;
  276. asihpi_irq_count++;
  277. /* printk(KERN_INFO "asihpi_isr %d ASI%04X:%d irq handled\n",
  278. asihpi_irq_count, a->adapter->type, a->adapter->index); */
  279. if (a->interrupt_callback)
  280. return IRQ_WAKE_THREAD;
  281. return IRQ_HANDLED;
  282. }
  283. static irqreturn_t asihpi_isr_thread(int irq, void *dev_id)
  284. {
  285. struct hpi_adapter *a = dev_id;
  286. if (a->interrupt_callback)
  287. a->interrupt_callback(a);
  288. return IRQ_HANDLED;
  289. }
  290. int asihpi_adapter_probe(struct pci_dev *pci_dev,
  291. const struct pci_device_id *pci_id)
  292. {
  293. int idx, nm, low_latency_mode = 0, irq_supported = 0;
  294. int adapter_index;
  295. unsigned int memlen;
  296. struct hpi_message hm;
  297. struct hpi_response hr;
  298. struct hpi_adapter adapter;
  299. struct hpi_pci pci = { 0 };
  300. memset(&adapter, 0, sizeof(adapter));
  301. dev_printk(KERN_DEBUG, &pci_dev->dev,
  302. "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
  303. pci_dev->device, pci_dev->subsystem_vendor,
  304. pci_dev->subsystem_device, pci_dev->devfn);
  305. if (pcim_enable_device(pci_dev) < 0) {
  306. dev_err(&pci_dev->dev,
  307. "pci_enable_device failed, disabling device\n");
  308. return -EIO;
  309. }
  310. pci_set_master(pci_dev); /* also sets latency timer if < 16 */
  311. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  312. HPI_SUBSYS_CREATE_ADAPTER);
  313. hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
  314. HPI_ERROR_PROCESSING_MESSAGE);
  315. hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
  316. nm = HPI_MAX_ADAPTER_MEM_SPACES;
  317. for (idx = 0; idx < nm; idx++) {
  318. HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
  319. &pci_dev->resource[idx]);
  320. if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
  321. memlen = pci_resource_len(pci_dev, idx);
  322. pci.ap_mem_base[idx] =
  323. ioremap(pci_resource_start(pci_dev, idx),
  324. memlen);
  325. if (!pci.ap_mem_base[idx]) {
  326. HPI_DEBUG_LOG(ERROR,
  327. "ioremap failed, aborting\n");
  328. /* unmap previously mapped pci mem space */
  329. goto err;
  330. }
  331. }
  332. }
  333. pci.pci_dev = pci_dev;
  334. hm.u.s.resource.bus_type = HPI_BUS_PCI;
  335. hm.u.s.resource.r.pci = &pci;
  336. /* call CreateAdapterObject on the relevant hpi module */
  337. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  338. if (hr.error)
  339. goto err;
  340. adapter_index = hr.u.s.adapter_index;
  341. adapter.adapter = hpi_find_adapter(adapter_index);
  342. if (prealloc_stream_buf) {
  343. adapter.p_buffer = vmalloc(prealloc_stream_buf);
  344. if (!adapter.p_buffer) {
  345. HPI_DEBUG_LOG(ERROR,
  346. "HPI could not allocate "
  347. "kernel buffer size %d\n",
  348. prealloc_stream_buf);
  349. goto err;
  350. }
  351. }
  352. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  353. HPI_ADAPTER_OPEN);
  354. hm.adapter_index = adapter.adapter->index;
  355. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  356. if (hr.error) {
  357. HPI_DEBUG_LOG(ERROR, "HPI_ADAPTER_OPEN failed, aborting\n");
  358. goto err;
  359. }
  360. /* Check if current mode == Low Latency mode */
  361. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  362. HPI_ADAPTER_GET_MODE);
  363. hm.adapter_index = adapter.adapter->index;
  364. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  365. if (!hr.error
  366. && hr.u.ax.mode.adapter_mode == HPI_ADAPTER_MODE_LOW_LATENCY)
  367. low_latency_mode = 1;
  368. else
  369. dev_info(&pci_dev->dev,
  370. "Adapter at index %d is not in low latency mode\n",
  371. adapter.adapter->index);
  372. /* Check if IRQs are supported */
  373. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  374. HPI_ADAPTER_GET_PROPERTY);
  375. hm.adapter_index = adapter.adapter->index;
  376. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_SUPPORTS_IRQ;
  377. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  378. if (hr.error || !hr.u.ax.property_get.parameter1) {
  379. dev_info(&pci_dev->dev,
  380. "IRQs not supported by adapter at index %d\n",
  381. adapter.adapter->index);
  382. } else {
  383. irq_supported = 1;
  384. }
  385. /* WARNING can't init mutex in 'adapter'
  386. * and then copy it to adapters[] ?!?!
  387. */
  388. adapters[adapter_index] = adapter;
  389. mutex_init(&adapters[adapter_index].mutex);
  390. pci_set_drvdata(pci_dev, &adapters[adapter_index]);
  391. if (low_latency_mode && irq_supported) {
  392. if (!adapter.adapter->irq_query_and_clear) {
  393. dev_err(&pci_dev->dev,
  394. "no IRQ handler for adapter %d, aborting\n",
  395. adapter.adapter->index);
  396. goto err;
  397. }
  398. /* Disable IRQ generation on DSP side by setting the rate to 0 */
  399. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  400. HPI_ADAPTER_SET_PROPERTY);
  401. hm.adapter_index = adapter.adapter->index;
  402. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
  403. hm.u.ax.property_set.parameter1 = 0;
  404. hm.u.ax.property_set.parameter2 = 0;
  405. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  406. if (hr.error) {
  407. HPI_DEBUG_LOG(ERROR,
  408. "HPI_ADAPTER_GET_MODE failed, aborting\n");
  409. goto err;
  410. }
  411. /* Note: request_irq calls asihpi_isr here */
  412. if (request_threaded_irq(pci_dev->irq, asihpi_isr,
  413. asihpi_isr_thread, IRQF_SHARED,
  414. "asihpi", &adapters[adapter_index])) {
  415. dev_err(&pci_dev->dev, "request_irq(%d) failed\n",
  416. pci_dev->irq);
  417. goto err;
  418. }
  419. adapters[adapter_index].interrupt_mode = 1;
  420. dev_info(&pci_dev->dev, "using irq %d\n", pci_dev->irq);
  421. adapters[adapter_index].irq = pci_dev->irq;
  422. } else {
  423. dev_info(&pci_dev->dev, "using polled mode\n");
  424. }
  425. dev_info(&pci_dev->dev, "probe succeeded for ASI%04X HPI index %d\n",
  426. adapter.adapter->type, adapter_index);
  427. return 0;
  428. err:
  429. while (--idx >= 0) {
  430. if (pci.ap_mem_base[idx]) {
  431. iounmap(pci.ap_mem_base[idx]);
  432. pci.ap_mem_base[idx] = NULL;
  433. }
  434. }
  435. if (adapter.p_buffer) {
  436. adapter.buffer_size = 0;
  437. vfree(adapter.p_buffer);
  438. }
  439. HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
  440. return -ENODEV;
  441. }
  442. void asihpi_adapter_remove(struct pci_dev *pci_dev)
  443. {
  444. int idx;
  445. struct hpi_message hm;
  446. struct hpi_response hr;
  447. struct hpi_adapter *pa;
  448. struct hpi_pci pci;
  449. pa = pci_get_drvdata(pci_dev);
  450. pci = pa->adapter->pci;
  451. /* Disable IRQ generation on DSP side */
  452. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  453. HPI_ADAPTER_SET_PROPERTY);
  454. hm.adapter_index = pa->adapter->index;
  455. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
  456. hm.u.ax.property_set.parameter1 = 0;
  457. hm.u.ax.property_set.parameter2 = 0;
  458. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  459. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  460. HPI_ADAPTER_DELETE);
  461. hm.adapter_index = pa->adapter->index;
  462. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  463. /* unmap PCI memory space, mapped during device init. */
  464. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; ++idx)
  465. iounmap(pci.ap_mem_base[idx]);
  466. if (pa->irq)
  467. free_irq(pa->irq, pa);
  468. vfree(pa->p_buffer);
  469. if (1)
  470. dev_info(&pci_dev->dev,
  471. "remove %04x:%04x,%04x:%04x,%04x, HPI index %d\n",
  472. pci_dev->vendor, pci_dev->device,
  473. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  474. pci_dev->devfn, pa->adapter->index);
  475. memset(pa, 0, sizeof(*pa));
  476. }
  477. void __init asihpi_init(void)
  478. {
  479. struct hpi_message hm;
  480. struct hpi_response hr;
  481. memset(adapters, 0, sizeof(adapters));
  482. printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
  483. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  484. HPI_SUBSYS_DRIVER_LOAD);
  485. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  486. }
  487. void asihpi_exit(void)
  488. {
  489. struct hpi_message hm;
  490. struct hpi_response hr;
  491. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  492. HPI_SUBSYS_DRIVER_UNLOAD);
  493. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  494. }