hpioctl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*******************************************************************************
  2. AudioScience HPI driver
  3. Copyright (C) 1997-2010 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 "hpimsginit.h"
  19. #include "hpidebug.h"
  20. #include "hpimsgx.h"
  21. #include "hpioctl.h"
  22. #include <linux/fs.h>
  23. #include <linux/slab.h>
  24. #include <linux/moduleparam.h>
  25. #include <asm/uaccess.h>
  26. #include <linux/pci.h>
  27. #include <linux/stringify.h>
  28. #ifdef MODULE_FIRMWARE
  29. MODULE_FIRMWARE("asihpi/dsp5000.bin");
  30. MODULE_FIRMWARE("asihpi/dsp6200.bin");
  31. MODULE_FIRMWARE("asihpi/dsp6205.bin");
  32. MODULE_FIRMWARE("asihpi/dsp6400.bin");
  33. MODULE_FIRMWARE("asihpi/dsp6600.bin");
  34. MODULE_FIRMWARE("asihpi/dsp8700.bin");
  35. MODULE_FIRMWARE("asihpi/dsp8900.bin");
  36. #endif
  37. static int prealloc_stream_buf;
  38. module_param(prealloc_stream_buf, int, S_IRUGO);
  39. MODULE_PARM_DESC(prealloc_stream_buf,
  40. "Preallocate size for per-adapter stream buffer");
  41. /* Allow the debug level to be changed after module load.
  42. E.g. echo 2 > /sys/module/asihpi/parameters/hpiDebugLevel
  43. */
  44. module_param(hpi_debug_level, int, S_IRUGO | S_IWUSR);
  45. MODULE_PARM_DESC(hpi_debug_level, "debug verbosity 0..5");
  46. /* List of adapters found */
  47. static struct hpi_adapter adapters[HPI_MAX_ADAPTERS];
  48. /* Wrapper function to HPI_Message to enable dumping of the
  49. message and response types.
  50. */
  51. static void hpi_send_recv_f(struct hpi_message *phm, struct hpi_response *phr,
  52. struct file *file)
  53. {
  54. int adapter = phm->adapter_index;
  55. if ((adapter >= HPI_MAX_ADAPTERS || adapter < 0)
  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. struct hpi_adapter *pa = NULL;
  92. int err = 0;
  93. if (cmd != HPI_IOCTL_LINUX)
  94. return -EINVAL;
  95. hm = kmalloc(sizeof(*hm), GFP_KERNEL);
  96. hr = kmalloc(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. pa = &adapters[hm->h.adapter_index];
  133. hr->h.size = res_max_size;
  134. if (hm->h.object == HPI_OBJ_SUBSYSTEM) {
  135. switch (hm->h.function) {
  136. case HPI_SUBSYS_CREATE_ADAPTER:
  137. case HPI_SUBSYS_DELETE_ADAPTER:
  138. /* Application must not use these functions! */
  139. hr->h.size = sizeof(hr->h);
  140. hr->h.error = HPI_ERROR_INVALID_OPERATION;
  141. hr->h.function = hm->h.function;
  142. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  143. if (uncopied_bytes)
  144. err = -EFAULT;
  145. else
  146. err = 0;
  147. goto out;
  148. default:
  149. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  150. }
  151. } else {
  152. u16 __user *ptr = NULL;
  153. u32 size = 0;
  154. /* -1=no data 0=read from user mem, 1=write to user mem */
  155. int wrflag = -1;
  156. u32 adapter = hm->h.adapter_index;
  157. if ((hm->h.adapter_index > HPI_MAX_ADAPTERS) || (!pa->type)) {
  158. hpi_init_response(&hr->r0, HPI_OBJ_ADAPTER,
  159. HPI_ADAPTER_OPEN,
  160. 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(&adapters[adapter].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(&adapters
  203. [adapter].mutex);
  204. err = -EINVAL;
  205. goto out;
  206. }
  207. }
  208. hm->m0.u.d.u.data.pb_data = pa->p_buffer;
  209. if (hm->h.function == HPI_ISTREAM_READ)
  210. /* from card, WRITE to user mem */
  211. wrflag = 1;
  212. else
  213. wrflag = 0;
  214. break;
  215. }
  216. default:
  217. size = 0;
  218. break;
  219. }
  220. if (size && (wrflag == 0)) {
  221. uncopied_bytes =
  222. copy_from_user(pa->p_buffer, ptr, size);
  223. if (uncopied_bytes)
  224. HPI_DEBUG_LOG(WARNING,
  225. "Missed %d of %d "
  226. "bytes from user\n", uncopied_bytes,
  227. size);
  228. }
  229. hpi_send_recv_f(&hm->m0, &hr->r0, file);
  230. if (size && (wrflag == 1)) {
  231. uncopied_bytes =
  232. copy_to_user(ptr, pa->p_buffer, size);
  233. if (uncopied_bytes)
  234. HPI_DEBUG_LOG(WARNING,
  235. "Missed %d of %d " "bytes to user\n",
  236. uncopied_bytes, size);
  237. }
  238. mutex_unlock(&adapters[adapter].mutex);
  239. }
  240. /* on return response size must be set */
  241. /*printk(KERN_INFO "response size %d\n", hr->h.wSize); */
  242. if (!hr->h.size) {
  243. HPI_DEBUG_LOG(ERROR, "response zero size\n");
  244. err = -EFAULT;
  245. goto out;
  246. }
  247. if (hr->h.size > res_max_size) {
  248. HPI_DEBUG_LOG(ERROR, "response too big %d %d\n", hr->h.size,
  249. res_max_size);
  250. hr->h.error = HPI_ERROR_RESPONSE_BUFFER_TOO_SMALL;
  251. hr->h.specific_error = hr->h.size;
  252. hr->h.size = sizeof(hr->h);
  253. }
  254. uncopied_bytes = copy_to_user(puhr, hr, hr->h.size);
  255. if (uncopied_bytes) {
  256. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  257. err = -EFAULT;
  258. goto out;
  259. }
  260. out:
  261. kfree(hm);
  262. kfree(hr);
  263. return err;
  264. }
  265. int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev,
  266. const struct pci_device_id *pci_id)
  267. {
  268. int err, idx, nm;
  269. unsigned int memlen;
  270. struct hpi_message hm;
  271. struct hpi_response hr;
  272. struct hpi_adapter adapter;
  273. struct hpi_pci pci;
  274. memset(&adapter, 0, sizeof(adapter));
  275. dev_printk(KERN_DEBUG, &pci_dev->dev,
  276. "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
  277. pci_dev->device, pci_dev->subsystem_vendor,
  278. pci_dev->subsystem_device, pci_dev->devfn);
  279. if (pci_enable_device(pci_dev) < 0) {
  280. dev_printk(KERN_ERR, &pci_dev->dev,
  281. "pci_enable_device failed, disabling device\n");
  282. return -EIO;
  283. }
  284. pci_set_master(pci_dev); /* also sets latency timer if < 16 */
  285. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  286. HPI_SUBSYS_CREATE_ADAPTER);
  287. hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
  288. HPI_ERROR_PROCESSING_MESSAGE);
  289. hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
  290. adapter.pci = pci_dev;
  291. nm = HPI_MAX_ADAPTER_MEM_SPACES;
  292. for (idx = 0; idx < nm; idx++) {
  293. HPI_DEBUG_LOG(INFO, "resource %d %s %08llx-%08llx %04llx\n",
  294. idx, pci_dev->resource[idx].name,
  295. (unsigned long long)pci_resource_start(pci_dev, idx),
  296. (unsigned long long)pci_resource_end(pci_dev, idx),
  297. (unsigned long long)pci_resource_flags(pci_dev, idx));
  298. if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
  299. memlen = pci_resource_len(pci_dev, idx);
  300. adapter.ap_remapped_mem_base[idx] =
  301. ioremap(pci_resource_start(pci_dev, idx),
  302. memlen);
  303. if (!adapter.ap_remapped_mem_base[idx]) {
  304. HPI_DEBUG_LOG(ERROR,
  305. "ioremap failed, aborting\n");
  306. /* unmap previously mapped pci mem space */
  307. goto err;
  308. }
  309. }
  310. pci.ap_mem_base[idx] = adapter.ap_remapped_mem_base[idx];
  311. }
  312. pci.pci_dev = pci_dev;
  313. hm.u.s.resource.bus_type = HPI_BUS_PCI;
  314. hm.u.s.resource.r.pci = &pci;
  315. /* call CreateAdapterObject on the relevant hpi module */
  316. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  317. if (hr.error)
  318. goto err;
  319. if (prealloc_stream_buf) {
  320. adapter.p_buffer = vmalloc(prealloc_stream_buf);
  321. if (!adapter.p_buffer) {
  322. HPI_DEBUG_LOG(ERROR,
  323. "HPI could not allocate "
  324. "kernel buffer size %d\n",
  325. prealloc_stream_buf);
  326. goto err;
  327. }
  328. }
  329. adapter.index = hr.u.s.adapter_index;
  330. adapter.type = hr.u.s.adapter_type;
  331. hm.adapter_index = adapter.index;
  332. err = hpi_adapter_open(adapter.index);
  333. if (err)
  334. goto err;
  335. adapter.snd_card_asihpi = NULL;
  336. /* WARNING can't init mutex in 'adapter'
  337. * and then copy it to adapters[] ?!?!
  338. */
  339. adapters[hr.u.s.adapter_index] = adapter;
  340. mutex_init(&adapters[adapter.index].mutex);
  341. pci_set_drvdata(pci_dev, &adapters[adapter.index]);
  342. dev_printk(KERN_INFO, &pci_dev->dev,
  343. "probe succeeded for ASI%04X HPI index %d\n", adapter.type,
  344. adapter.index);
  345. return 0;
  346. err:
  347. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  348. if (adapter.ap_remapped_mem_base[idx]) {
  349. iounmap(adapter.ap_remapped_mem_base[idx]);
  350. adapter.ap_remapped_mem_base[idx] = NULL;
  351. }
  352. }
  353. if (adapter.p_buffer) {
  354. adapter.buffer_size = 0;
  355. vfree(adapter.p_buffer);
  356. }
  357. HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
  358. return -ENODEV;
  359. }
  360. void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev)
  361. {
  362. int idx;
  363. struct hpi_message hm;
  364. struct hpi_response hr;
  365. struct hpi_adapter *pa;
  366. pa = pci_get_drvdata(pci_dev);
  367. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  368. HPI_SUBSYS_DELETE_ADAPTER);
  369. hm.obj_index = pa->index;
  370. hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
  371. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  372. /* unmap PCI memory space, mapped during device init. */
  373. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  374. if (pa->ap_remapped_mem_base[idx]) {
  375. iounmap(pa->ap_remapped_mem_base[idx]);
  376. pa->ap_remapped_mem_base[idx] = NULL;
  377. }
  378. }
  379. if (pa->p_buffer)
  380. vfree(pa->p_buffer);
  381. pci_set_drvdata(pci_dev, NULL);
  382. if (1)
  383. dev_printk(KERN_INFO, &pci_dev->dev,
  384. "remove %04x:%04x,%04x:%04x,%04x," " HPI index %d.\n",
  385. pci_dev->vendor, pci_dev->device,
  386. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  387. pci_dev->devfn, pa->index);
  388. memset(pa, 0, sizeof(*pa));
  389. }
  390. void __init asihpi_init(void)
  391. {
  392. struct hpi_message hm;
  393. struct hpi_response hr;
  394. memset(adapters, 0, sizeof(adapters));
  395. printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
  396. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  397. HPI_SUBSYS_DRIVER_LOAD);
  398. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  399. }
  400. void asihpi_exit(void)
  401. {
  402. struct hpi_message hm;
  403. struct hpi_response hr;
  404. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  405. HPI_SUBSYS_DRIVER_UNLOAD);
  406. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  407. }