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 <linux/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. res_max_size = min_t(size_t, res_max_size, sizeof(*hr));
  132. switch (hm->h.function) {
  133. case HPI_SUBSYS_CREATE_ADAPTER:
  134. case HPI_ADAPTER_DELETE:
  135. /* Application must not use these functions! */
  136. hr->h.size = sizeof(hr->h);
  137. hr->h.error = HPI_ERROR_INVALID_OPERATION;
  138. hr->h.function = hm->h.function;
  139. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  140. if (uncopied_bytes)
  141. err = -EFAULT;
  142. else
  143. err = 0;
  144. goto out;
  145. }
  146. hr->h.size = res_max_size;
  147. if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
  148. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  149. } else {
  150. u16 __user *ptr = NULL;
  151. u32 size = 0;
  152. /* -1=no data 0=read from user mem, 1=write to user mem */
  153. int wrflag = -1;
  154. struct hpi_adapter *pa = NULL;
  155. if (hm->h.adapter_index < ARRAY_SIZE(adapters))
  156. pa = &adapters[hm->h.adapter_index];
  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. a->interrupt_callback(a);
  281. return IRQ_HANDLED;
  282. }
  283. int asihpi_adapter_probe(struct pci_dev *pci_dev,
  284. const struct pci_device_id *pci_id)
  285. {
  286. int idx, nm, low_latency_mode = 0, irq_supported = 0;
  287. int adapter_index;
  288. unsigned int memlen;
  289. struct hpi_message hm;
  290. struct hpi_response hr;
  291. struct hpi_adapter adapter;
  292. struct hpi_pci pci;
  293. memset(&adapter, 0, sizeof(adapter));
  294. dev_printk(KERN_DEBUG, &pci_dev->dev,
  295. "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
  296. pci_dev->device, pci_dev->subsystem_vendor,
  297. pci_dev->subsystem_device, pci_dev->devfn);
  298. if (pci_enable_device(pci_dev) < 0) {
  299. dev_err(&pci_dev->dev,
  300. "pci_enable_device failed, disabling device\n");
  301. return -EIO;
  302. }
  303. pci_set_master(pci_dev); /* also sets latency timer if < 16 */
  304. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  305. HPI_SUBSYS_CREATE_ADAPTER);
  306. hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
  307. HPI_ERROR_PROCESSING_MESSAGE);
  308. hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
  309. nm = HPI_MAX_ADAPTER_MEM_SPACES;
  310. for (idx = 0; idx < nm; idx++) {
  311. HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
  312. &pci_dev->resource[idx]);
  313. if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
  314. memlen = pci_resource_len(pci_dev, idx);
  315. pci.ap_mem_base[idx] =
  316. ioremap(pci_resource_start(pci_dev, idx),
  317. memlen);
  318. if (!pci.ap_mem_base[idx]) {
  319. HPI_DEBUG_LOG(ERROR,
  320. "ioremap failed, aborting\n");
  321. /* unmap previously mapped pci mem space */
  322. goto err;
  323. }
  324. }
  325. }
  326. pci.pci_dev = pci_dev;
  327. hm.u.s.resource.bus_type = HPI_BUS_PCI;
  328. hm.u.s.resource.r.pci = &pci;
  329. /* call CreateAdapterObject on the relevant hpi module */
  330. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  331. if (hr.error)
  332. goto err;
  333. adapter_index = hr.u.s.adapter_index;
  334. adapter.adapter = hpi_find_adapter(adapter_index);
  335. if (prealloc_stream_buf) {
  336. adapter.p_buffer = vmalloc(prealloc_stream_buf);
  337. if (!adapter.p_buffer) {
  338. HPI_DEBUG_LOG(ERROR,
  339. "HPI could not allocate "
  340. "kernel buffer size %d\n",
  341. prealloc_stream_buf);
  342. goto err;
  343. }
  344. }
  345. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  346. HPI_ADAPTER_OPEN);
  347. hm.adapter_index = adapter.adapter->index;
  348. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  349. if (hr.error) {
  350. HPI_DEBUG_LOG(ERROR, "HPI_ADAPTER_OPEN failed, aborting\n");
  351. goto err;
  352. }
  353. /* Check if current mode == Low Latency mode */
  354. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  355. HPI_ADAPTER_GET_MODE);
  356. hm.adapter_index = adapter.adapter->index;
  357. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  358. if (!hr.error
  359. && hr.u.ax.mode.adapter_mode == HPI_ADAPTER_MODE_LOW_LATENCY)
  360. low_latency_mode = 1;
  361. else
  362. dev_info(&pci_dev->dev,
  363. "Adapter at index %d is not in low latency mode\n",
  364. adapter.adapter->index);
  365. /* Check if IRQs are supported */
  366. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  367. HPI_ADAPTER_GET_PROPERTY);
  368. hm.adapter_index = adapter.adapter->index;
  369. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_SUPPORTS_IRQ;
  370. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  371. if (hr.error || !hr.u.ax.property_get.parameter1) {
  372. dev_info(&pci_dev->dev,
  373. "IRQs not supported by adapter at index %d\n",
  374. adapter.adapter->index);
  375. } else {
  376. irq_supported = 1;
  377. }
  378. /* WARNING can't init mutex in 'adapter'
  379. * and then copy it to adapters[] ?!?!
  380. */
  381. adapters[adapter_index] = adapter;
  382. mutex_init(&adapters[adapter_index].mutex);
  383. pci_set_drvdata(pci_dev, &adapters[adapter_index]);
  384. if (low_latency_mode && irq_supported) {
  385. if (!adapter.adapter->irq_query_and_clear) {
  386. dev_err(&pci_dev->dev,
  387. "no IRQ handler for adapter %d, aborting\n",
  388. adapter.adapter->index);
  389. goto err;
  390. }
  391. /* Disable IRQ generation on DSP side by setting the rate to 0 */
  392. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  393. HPI_ADAPTER_SET_PROPERTY);
  394. hm.adapter_index = adapter.adapter->index;
  395. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
  396. hm.u.ax.property_set.parameter1 = 0;
  397. hm.u.ax.property_set.parameter2 = 0;
  398. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  399. if (hr.error) {
  400. HPI_DEBUG_LOG(ERROR,
  401. "HPI_ADAPTER_GET_MODE failed, aborting\n");
  402. goto err;
  403. }
  404. /* Note: request_irq calls asihpi_isr here */
  405. if (request_irq(pci_dev->irq, asihpi_isr, IRQF_SHARED,
  406. "asihpi", &adapters[adapter_index])) {
  407. dev_err(&pci_dev->dev, "request_irq(%d) failed\n",
  408. pci_dev->irq);
  409. goto err;
  410. }
  411. adapters[adapter_index].interrupt_mode = 1;
  412. dev_info(&pci_dev->dev, "using irq %d\n", pci_dev->irq);
  413. adapters[adapter_index].irq = pci_dev->irq;
  414. } else {
  415. dev_info(&pci_dev->dev, "using polled mode\n");
  416. }
  417. dev_info(&pci_dev->dev, "probe succeeded for ASI%04X HPI index %d\n",
  418. adapter.adapter->type, adapter_index);
  419. return 0;
  420. err:
  421. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  422. if (pci.ap_mem_base[idx]) {
  423. iounmap(pci.ap_mem_base[idx]);
  424. pci.ap_mem_base[idx] = NULL;
  425. }
  426. }
  427. if (adapter.p_buffer) {
  428. adapter.buffer_size = 0;
  429. vfree(adapter.p_buffer);
  430. }
  431. HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
  432. return -ENODEV;
  433. }
  434. void asihpi_adapter_remove(struct pci_dev *pci_dev)
  435. {
  436. int idx;
  437. struct hpi_message hm;
  438. struct hpi_response hr;
  439. struct hpi_adapter *pa;
  440. struct hpi_pci pci;
  441. pa = pci_get_drvdata(pci_dev);
  442. pci = pa->adapter->pci;
  443. /* Disable IRQ generation on DSP side */
  444. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  445. HPI_ADAPTER_SET_PROPERTY);
  446. hm.adapter_index = pa->adapter->index;
  447. hm.u.ax.property_set.property = HPI_ADAPTER_PROPERTY_IRQ_RATE;
  448. hm.u.ax.property_set.parameter1 = 0;
  449. hm.u.ax.property_set.parameter2 = 0;
  450. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  451. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  452. HPI_ADAPTER_DELETE);
  453. hm.adapter_index = pa->adapter->index;
  454. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  455. /* unmap PCI memory space, mapped during device init. */
  456. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; ++idx)
  457. iounmap(pci.ap_mem_base[idx]);
  458. if (pa->irq)
  459. free_irq(pa->irq, pa);
  460. vfree(pa->p_buffer);
  461. if (1)
  462. dev_info(&pci_dev->dev,
  463. "remove %04x:%04x,%04x:%04x,%04x, HPI index %d\n",
  464. pci_dev->vendor, pci_dev->device,
  465. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  466. pci_dev->devfn, pa->adapter->index);
  467. memset(pa, 0, sizeof(*pa));
  468. }
  469. void __init asihpi_init(void)
  470. {
  471. struct hpi_message hm;
  472. struct hpi_response hr;
  473. memset(adapters, 0, sizeof(adapters));
  474. printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
  475. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  476. HPI_SUBSYS_DRIVER_LOAD);
  477. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  478. }
  479. void asihpi_exit(void)
  480. {
  481. struct hpi_message hm;
  482. struct hpi_response hr;
  483. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  484. HPI_SUBSYS_DRIVER_UNLOAD);
  485. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  486. }