hpioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*******************************************************************************
  2. AudioScience HPI driver
  3. Copyright (C) 1997-2011 AudioScience Inc. <support@audioscience.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of version 2 of the GNU General Public License as
  6. published by the Free Software Foundation;
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. Common Linux HPI ioctl and module probe/remove functions
  15. *******************************************************************************/
  16. #define SOURCEFILE_NAME "hpioctl.c"
  17. #include "hpi_internal.h"
  18. #include "hpi_version.h"
  19. #include "hpimsginit.h"
  20. #include "hpidebug.h"
  21. #include "hpimsgx.h"
  22. #include "hpioctl.h"
  23. #include "hpicmn.h"
  24. #include <linux/fs.h>
  25. #include <linux/slab.h>
  26. #include <linux/moduleparam.h>
  27. #include <asm/uaccess.h>
  28. #include <linux/pci.h>
  29. #include <linux/stringify.h>
  30. #include <linux/module.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, S_IRUGO);
  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, S_IRUGO | S_IWUSR);
  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. int adapter = phm->adapter_index;
  58. if ((adapter >= HPI_MAX_ADAPTERS || adapter < 0)
  59. && (phm->object != HPI_OBJ_SUBSYSTEM))
  60. phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
  61. else
  62. hpi_send_recv_ex(phm, phr, file);
  63. }
  64. /* This is called from hpifunc.c functions, called by ALSA
  65. * (or other kernel process) In this case there is no file descriptor
  66. * available for the message cache code
  67. */
  68. void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
  69. {
  70. hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
  71. }
  72. EXPORT_SYMBOL(hpi_send_recv);
  73. /* for radio-asihpi */
  74. int asihpi_hpi_release(struct file *file)
  75. {
  76. struct hpi_message hm;
  77. struct hpi_response hr;
  78. /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
  79. /* close the subsystem just in case the application forgot to. */
  80. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  81. HPI_SUBSYS_CLOSE);
  82. hpi_send_recv_ex(&hm, &hr, file);
  83. return 0;
  84. }
  85. long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  86. {
  87. struct hpi_ioctl_linux __user *phpi_ioctl_data;
  88. void __user *puhm;
  89. void __user *puhr;
  90. union hpi_message_buffer_v1 *hm;
  91. union hpi_response_buffer_v1 *hr;
  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 = kmalloc(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(hm->h.size, (u16 __user *)puhm)) {
  112. err = -EFAULT;
  113. goto out;
  114. }
  115. if (hm->h.size > sizeof(*hm))
  116. hm->h.size = sizeof(*hm);
  117. /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
  118. uncopied_bytes = copy_from_user(hm, puhm, hm->h.size);
  119. if (uncopied_bytes) {
  120. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  121. err = -EFAULT;
  122. goto out;
  123. }
  124. if (get_user(res_max_size, (u16 __user *)puhr)) {
  125. err = -EFAULT;
  126. goto out;
  127. }
  128. /* printk(KERN_INFO "user response size %d\n", res_max_size); */
  129. if (res_max_size < sizeof(struct hpi_response_header)) {
  130. HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
  131. err = -EFAULT;
  132. goto out;
  133. }
  134. switch (hm->h.function) {
  135. case HPI_SUBSYS_CREATE_ADAPTER:
  136. case HPI_ADAPTER_DELETE:
  137. /* Application must not use these functions! */
  138. hr->h.size = sizeof(hr->h);
  139. hr->h.error = HPI_ERROR_INVALID_OPERATION;
  140. hr->h.function = hm->h.function;
  141. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  142. if (uncopied_bytes)
  143. err = -EFAULT;
  144. else
  145. err = 0;
  146. goto out;
  147. }
  148. hr->h.size = res_max_size;
  149. if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
  150. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  151. } else {
  152. u16 __user *ptr = NULL;
  153. u32 size = 0;
  154. u32 adapter_present;
  155. /* -1=no data 0=read from user mem, 1=write to user mem */
  156. int wrflag = -1;
  157. struct hpi_adapter *pa;
  158. if (hm->h.adapter_index < HPI_MAX_ADAPTERS) {
  159. pa = &adapters[hm->h.adapter_index];
  160. adapter_present = pa->type;
  161. } else {
  162. adapter_present = 0;
  163. }
  164. if (!adapter_present) {
  165. hpi_init_response(&hr->r0, hm->h.object,
  166. hm->h.function, HPI_ERROR_BAD_ADAPTER_NUMBER);
  167. uncopied_bytes =
  168. copy_to_user(puhr, hr, sizeof(hr->h));
  169. if (uncopied_bytes)
  170. err = -EFAULT;
  171. else
  172. err = 0;
  173. goto out;
  174. }
  175. if (mutex_lock_interruptible(&pa->mutex)) {
  176. err = -EINTR;
  177. goto out;
  178. }
  179. /* Dig out any pointers embedded in the message. */
  180. switch (hm->h.function) {
  181. case HPI_OSTREAM_WRITE:
  182. case HPI_ISTREAM_READ:{
  183. /* Yes, sparse, this is correct. */
  184. ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
  185. size = hm->m0.u.d.u.data.data_size;
  186. /* Allocate buffer according to application request.
  187. ?Is it better to alloc/free for the duration
  188. of the transaction?
  189. */
  190. if (pa->buffer_size < size) {
  191. HPI_DEBUG_LOG(DEBUG,
  192. "Realloc adapter %d stream "
  193. "buffer from %zd to %d\n",
  194. hm->h.adapter_index,
  195. pa->buffer_size, size);
  196. if (pa->p_buffer) {
  197. pa->buffer_size = 0;
  198. vfree(pa->p_buffer);
  199. }
  200. pa->p_buffer = vmalloc(size);
  201. if (pa->p_buffer)
  202. pa->buffer_size = size;
  203. else {
  204. HPI_DEBUG_LOG(ERROR,
  205. "HPI could not allocate "
  206. "stream buffer size %d\n",
  207. size);
  208. mutex_unlock(&pa->mutex);
  209. err = -EINVAL;
  210. goto out;
  211. }
  212. }
  213. hm->m0.u.d.u.data.pb_data = pa->p_buffer;
  214. if (hm->h.function == HPI_ISTREAM_READ)
  215. /* from card, WRITE to user mem */
  216. wrflag = 1;
  217. else
  218. wrflag = 0;
  219. break;
  220. }
  221. default:
  222. size = 0;
  223. break;
  224. }
  225. if (size && (wrflag == 0)) {
  226. uncopied_bytes =
  227. copy_from_user(pa->p_buffer, ptr, size);
  228. if (uncopied_bytes)
  229. HPI_DEBUG_LOG(WARNING,
  230. "Missed %d of %d "
  231. "bytes from user\n", uncopied_bytes,
  232. size);
  233. }
  234. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  235. if (size && (wrflag == 1)) {
  236. uncopied_bytes =
  237. copy_to_user(ptr, pa->p_buffer, size);
  238. if (uncopied_bytes)
  239. HPI_DEBUG_LOG(WARNING,
  240. "Missed %d of %d " "bytes to user\n",
  241. uncopied_bytes, size);
  242. }
  243. mutex_unlock(&pa->mutex);
  244. }
  245. /* on return response size must be set */
  246. /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
  247. if (!hr->h.size) {
  248. HPI_DEBUG_LOG(ERROR, "response zero size\n");
  249. err = -EFAULT;
  250. goto out;
  251. }
  252. if (hr->h.size > res_max_size) {
  253. HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
  254. res_max_size);
  255. hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
  256. hr->h.specific_error = hr->h.size;
  257. hr->h.size = sizeof(hr->h);
  258. }
  259. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  260. if (uncopied_bytes) {
  261. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  262. err = -EFAULT;
  263. goto out;
  264. }
  265. out:
  266. kfree(hm);
  267. kfree(hr);
  268. return err;
  269. }
  270. int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev,
  271. const struct pci_device_id *pci_id)
  272. {
  273. int idx, nm;
  274. unsigned int memlen;
  275. struct hpi_message hm;
  276. struct hpi_response hr;
  277. struct hpi_adapter adapter;
  278. struct hpi_pci pci;
  279. memset(&adapter, 0, sizeof(adapter));
  280. dev_printk(KERN_DEBUG, &pci_dev->dev,
  281. "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
  282. pci_dev->device, pci_dev->subsystem_vendor,
  283. pci_dev->subsystem_device, pci_dev->devfn);
  284. if (pci_enable_device(pci_dev) < 0) {
  285. dev_printk(KERN_ERR, &pci_dev->dev,
  286. "pci_enable_device failed, disabling device\n");
  287. return -EIO;
  288. }
  289. pci_set_master(pci_dev); /* also sets latency timer if < 16 */
  290. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  291. HPI_SUBSYS_CREATE_ADAPTER);
  292. hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
  293. HPI_ERROR_PROCESSING_MESSAGE);
  294. hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
  295. adapter.pci = pci_dev;
  296. nm = HPI_MAX_ADAPTER_MEM_SPACES;
  297. for (idx = 0; idx < nm; idx++) {
  298. HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
  299. &pci_dev->resource[idx]);
  300. if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
  301. memlen = pci_resource_len(pci_dev, idx);
  302. adapter.ap_remapped_mem_base[idx] =
  303. ioremap(pci_resource_start(pci_dev, idx),
  304. memlen);
  305. if (!adapter.ap_remapped_mem_base[idx]) {
  306. HPI_DEBUG_LOG(ERROR,
  307. "ioremap failed, aborting\n");
  308. /* unmap previously mapped pci mem space */
  309. goto err;
  310. }
  311. }
  312. pci.ap_mem_base[idx] = adapter.ap_remapped_mem_base[idx];
  313. }
  314. pci.pci_dev = pci_dev;
  315. hm.u.s.resource.bus_type = HPI_BUS_PCI;
  316. hm.u.s.resource.r.pci = &pci;
  317. /* call CreateAdapterObject on the relevant hpi module */
  318. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  319. if (hr.error)
  320. goto err;
  321. if (prealloc_stream_buf) {
  322. adapter.p_buffer = vmalloc(prealloc_stream_buf);
  323. if (!adapter.p_buffer) {
  324. HPI_DEBUG_LOG(ERROR,
  325. "HPI could not allocate "
  326. "kernel buffer size %d\n",
  327. prealloc_stream_buf);
  328. goto err;
  329. }
  330. }
  331. adapter.index = hr.u.s.adapter_index;
  332. adapter.type = hr.u.s.adapter_type;
  333. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  334. HPI_ADAPTER_OPEN);
  335. hm.adapter_index = adapter.index;
  336. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  337. if (hr.error)
  338. goto err;
  339. adapter.snd_card_asihpi = NULL;
  340. /* WARNING can't init mutex in 'adapter'
  341. * and then copy it to adapters[] ?!?!
  342. */
  343. adapters[adapter.index] = adapter;
  344. mutex_init(&adapters[adapter.index].mutex);
  345. pci_set_drvdata(pci_dev, &adapters[adapter.index]);
  346. dev_printk(KERN_INFO, &pci_dev->dev,
  347. "probe succeeded for ASI%04X HPI index %d\n", adapter.type,
  348. adapter.index);
  349. return 0;
  350. err:
  351. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  352. if (adapter.ap_remapped_mem_base[idx]) {
  353. iounmap(adapter.ap_remapped_mem_base[idx]);
  354. adapter.ap_remapped_mem_base[idx] = NULL;
  355. }
  356. }
  357. if (adapter.p_buffer) {
  358. adapter.buffer_size = 0;
  359. vfree(adapter.p_buffer);
  360. }
  361. HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
  362. return -ENODEV;
  363. }
  364. void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev)
  365. {
  366. int idx;
  367. struct hpi_message hm;
  368. struct hpi_response hr;
  369. struct hpi_adapter *pa;
  370. pa = pci_get_drvdata(pci_dev);
  371. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  372. HPI_ADAPTER_DELETE);
  373. hm.adapter_index = pa->index;
  374. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  375. /* unmap PCI memory space, mapped during device init. */
  376. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  377. if (pa->ap_remapped_mem_base[idx]) {
  378. iounmap(pa->ap_remapped_mem_base[idx]);
  379. pa->ap_remapped_mem_base[idx] = NULL;
  380. }
  381. }
  382. if (pa->p_buffer)
  383. vfree(pa->p_buffer);
  384. pci_set_drvdata(pci_dev, NULL);
  385. if (1)
  386. dev_printk(KERN_INFO, &pci_dev->dev,
  387. "remove %04x:%04x,%04x:%04x,%04x," " HPI index %d.\n",
  388. pci_dev->vendor, pci_dev->device,
  389. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  390. pci_dev->devfn, pa->index);
  391. memset(pa, 0, sizeof(*pa));
  392. }
  393. void __init asihpi_init(void)
  394. {
  395. struct hpi_message hm;
  396. struct hpi_response hr;
  397. memset(adapters, 0, sizeof(adapters));
  398. printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
  399. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  400. HPI_SUBSYS_DRIVER_LOAD);
  401. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  402. }
  403. void asihpi_exit(void)
  404. {
  405. struct hpi_message hm;
  406. struct hpi_response hr;
  407. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  408. HPI_SUBSYS_DRIVER_UNLOAD);
  409. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  410. }