hpioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 "hpicmn.h"
  23. #include <linux/fs.h>
  24. #include <linux/slab.h>
  25. #include <linux/moduleparam.h>
  26. #include <asm/uaccess.h>
  27. #include <linux/pci.h>
  28. #include <linux/stringify.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. int adapter = phm->adapter_index;
  56. if ((adapter >= HPI_MAX_ADAPTERS || adapter < 0)
  57. && (phm->object != HPI_OBJ_SUBSYSTEM))
  58. phr->error = HPI_ERROR_INVALID_OBJ_INDEX;
  59. else
  60. hpi_send_recv_ex(phm, phr, file);
  61. }
  62. /* This is called from hpifunc.c functions, called by ALSA
  63. * (or other kernel process) In this case there is no file descriptor
  64. * available for the message cache code
  65. */
  66. void hpi_send_recv(struct hpi_message *phm, struct hpi_response *phr)
  67. {
  68. hpi_send_recv_f(phm, phr, HOWNER_KERNEL);
  69. }
  70. EXPORT_SYMBOL(hpi_send_recv);
  71. /* for radio-asihpi */
  72. int asihpi_hpi_release(struct file *file)
  73. {
  74. struct hpi_message hm;
  75. struct hpi_response hr;
  76. /* HPI_DEBUG_LOG(INFO,"hpi_release file %p, pid %d\n", file, current->pid); */
  77. /* close the subsystem just in case the application forgot to. */
  78. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  79. HPI_SUBSYS_CLOSE);
  80. hpi_send_recv_ex(&hm, &hr, file);
  81. return 0;
  82. }
  83. long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  84. {
  85. struct hpi_ioctl_linux __user *phpi_ioctl_data;
  86. void __user *puhm;
  87. void __user *puhr;
  88. union hpi_message_buffer_v1 *hm;
  89. union hpi_response_buffer_v1 *hr;
  90. u16 res_max_size;
  91. u32 uncopied_bytes;
  92. struct hpi_adapter *pa = NULL;
  93. int err = 0;
  94. if (cmd != HPI_IOCTL_LINUX)
  95. return -EINVAL;
  96. hm = kmalloc(sizeof(*hm), GFP_KERNEL);
  97. hr = kmalloc(sizeof(*hr), GFP_KERNEL);
  98. if (!hm || !hr) {
  99. err = -ENOMEM;
  100. goto out;
  101. }
  102. phpi_ioctl_data = (struct hpi_ioctl_linux __user *)arg;
  103. /* Read the message and response pointers from user space. */
  104. if (get_user(puhm, &phpi_ioctl_data->phm)
  105. || get_user(puhr, &phpi_ioctl_data->phr)) {
  106. err = -EFAULT;
  107. goto out;
  108. }
  109. /* Now read the message size and data from user space. */
  110. if (get_user(hm->h.size, (u16 __user *)puhm)) {
  111. err = -EFAULT;
  112. goto out;
  113. }
  114. if (hm->h.size > sizeof(*hm))
  115. hm->h.size = sizeof(*hm);
  116. /* printk(KERN_INFO "message size %d\n", hm->h.wSize); */
  117. uncopied_bytes = copy_from_user(hm, puhm, hm->h.size);
  118. if (uncopied_bytes) {
  119. HPI_DEBUG_LOG(ERROR, "uncopied bytes %d\n", uncopied_bytes);
  120. err = -EFAULT;
  121. goto out;
  122. }
  123. if (get_user(res_max_size, (u16 __user *)puhr)) {
  124. err = -EFAULT;
  125. goto out;
  126. }
  127. /* printk(KERN_INFO "user response size %d\n", res_max_size); */
  128. if (res_max_size < sizeof(struct hpi_response_header)) {
  129. HPI_DEBUG_LOG(WARNING, "small res size %d\n", res_max_size);
  130. err = -EFAULT;
  131. goto out;
  132. }
  133. if (hm->h.adapter_index >= HPI_MAX_ADAPTERS) {
  134. err = -EINVAL;
  135. goto out;
  136. }
  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. u32 adapter = hm->h.adapter_index;
  160. pa = &adapters[adapter];
  161. if ((adapter > HPI_MAX_ADAPTERS) || (!pa->type)) {
  162. hpi_init_response(&hr->r0, HPI_OBJ_ADAPTER,
  163. HPI_ADAPTER_OPEN,
  164. HPI_ERROR_BAD_ADAPTER_NUMBER);
  165. uncopied_bytes =
  166. copy_to_user(puhr, hr, sizeof(hr->h));
  167. if (uncopied_bytes)
  168. err = -EFAULT;
  169. else
  170. err = 0;
  171. goto out;
  172. }
  173. if (mutex_lock_interruptible(&adapters[adapter].mutex)) {
  174. err = -EINTR;
  175. goto out;
  176. }
  177. /* Dig out any pointers embedded in the message. */
  178. switch (hm->h.function) {
  179. case HPI_OSTREAM_WRITE:
  180. case HPI_ISTREAM_READ:{
  181. /* Yes, sparse, this is correct. */
  182. ptr = (u16 __user *)hm->m0.u.d.u.data.pb_data;
  183. size = hm->m0.u.d.u.data.data_size;
  184. /* Allocate buffer according to application request.
  185. ?Is it better to alloc/free for the duration
  186. of the transaction?
  187. */
  188. if (pa->buffer_size < size) {
  189. HPI_DEBUG_LOG(DEBUG,
  190. "Realloc adapter %d stream "
  191. "buffer from %zd to %d\n",
  192. hm->h.adapter_index,
  193. pa->buffer_size, size);
  194. if (pa->p_buffer) {
  195. pa->buffer_size = 0;
  196. vfree(pa->p_buffer);
  197. }
  198. pa->p_buffer = vmalloc(size);
  199. if (pa->p_buffer)
  200. pa->buffer_size = size;
  201. else {
  202. HPI_DEBUG_LOG(ERROR,
  203. "HPI could not allocate "
  204. "stream buffer size %d\n",
  205. size);
  206. mutex_unlock(&adapters
  207. [adapter].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(&adapters[adapter].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. int __devinit asihpi_adapter_probe(struct pci_dev *pci_dev,
  270. const struct pci_device_id *pci_id)
  271. {
  272. int idx, nm;
  273. unsigned int memlen;
  274. struct hpi_message hm;
  275. struct hpi_response hr;
  276. struct hpi_adapter adapter;
  277. struct hpi_pci pci;
  278. memset(&adapter, 0, sizeof(adapter));
  279. dev_printk(KERN_DEBUG, &pci_dev->dev,
  280. "probe %04x:%04x,%04x:%04x,%04x\n", pci_dev->vendor,
  281. pci_dev->device, pci_dev->subsystem_vendor,
  282. pci_dev->subsystem_device, pci_dev->devfn);
  283. if (pci_enable_device(pci_dev) < 0) {
  284. dev_printk(KERN_ERR, &pci_dev->dev,
  285. "pci_enable_device failed, disabling device\n");
  286. return -EIO;
  287. }
  288. pci_set_master(pci_dev); /* also sets latency timer if < 16 */
  289. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  290. HPI_SUBSYS_CREATE_ADAPTER);
  291. hpi_init_response(&hr, HPI_OBJ_SUBSYSTEM, HPI_SUBSYS_CREATE_ADAPTER,
  292. HPI_ERROR_PROCESSING_MESSAGE);
  293. hm.adapter_index = HPI_ADAPTER_INDEX_INVALID;
  294. adapter.pci = pci_dev;
  295. nm = HPI_MAX_ADAPTER_MEM_SPACES;
  296. for (idx = 0; idx < nm; idx++) {
  297. HPI_DEBUG_LOG(INFO, "resource %d %pR\n", idx,
  298. &pci_dev->resource[idx]);
  299. if (pci_resource_flags(pci_dev, idx) & IORESOURCE_MEM) {
  300. memlen = pci_resource_len(pci_dev, idx);
  301. adapter.ap_remapped_mem_base[idx] =
  302. ioremap(pci_resource_start(pci_dev, idx),
  303. memlen);
  304. if (!adapter.ap_remapped_mem_base[idx]) {
  305. HPI_DEBUG_LOG(ERROR,
  306. "ioremap failed, aborting\n");
  307. /* unmap previously mapped pci mem space */
  308. goto err;
  309. }
  310. }
  311. pci.ap_mem_base[idx] = adapter.ap_remapped_mem_base[idx];
  312. }
  313. pci.pci_dev = pci_dev;
  314. hm.u.s.resource.bus_type = HPI_BUS_PCI;
  315. hm.u.s.resource.r.pci = &pci;
  316. /* call CreateAdapterObject on the relevant hpi module */
  317. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  318. if (hr.error)
  319. goto err;
  320. if (prealloc_stream_buf) {
  321. adapter.p_buffer = vmalloc(prealloc_stream_buf);
  322. if (!adapter.p_buffer) {
  323. HPI_DEBUG_LOG(ERROR,
  324. "HPI could not allocate "
  325. "kernel buffer size %d\n",
  326. prealloc_stream_buf);
  327. goto err;
  328. }
  329. }
  330. adapter.index = hr.u.s.adapter_index;
  331. adapter.type = hr.u.s.adapter_type;
  332. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  333. HPI_ADAPTER_OPEN);
  334. hm.adapter_index = adapter.index;
  335. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  336. if (hr.error)
  337. goto err;
  338. adapter.snd_card_asihpi = NULL;
  339. /* WARNING can't init mutex in 'adapter'
  340. * and then copy it to adapters[] ?!?!
  341. */
  342. adapters[adapter.index] = adapter;
  343. mutex_init(&adapters[adapter.index].mutex);
  344. pci_set_drvdata(pci_dev, &adapters[adapter.index]);
  345. dev_printk(KERN_INFO, &pci_dev->dev,
  346. "probe succeeded for ASI%04X HPI index %d\n", adapter.type,
  347. adapter.index);
  348. return 0;
  349. err:
  350. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  351. if (adapter.ap_remapped_mem_base[idx]) {
  352. iounmap(adapter.ap_remapped_mem_base[idx]);
  353. adapter.ap_remapped_mem_base[idx] = NULL;
  354. }
  355. }
  356. if (adapter.p_buffer) {
  357. adapter.buffer_size = 0;
  358. vfree(adapter.p_buffer);
  359. }
  360. HPI_DEBUG_LOG(ERROR, "adapter_probe failed\n");
  361. return -ENODEV;
  362. }
  363. void __devexit asihpi_adapter_remove(struct pci_dev *pci_dev)
  364. {
  365. int idx;
  366. struct hpi_message hm;
  367. struct hpi_response hr;
  368. struct hpi_adapter *pa;
  369. pa = pci_get_drvdata(pci_dev);
  370. hpi_init_message_response(&hm, &hr, HPI_OBJ_ADAPTER,
  371. HPI_ADAPTER_DELETE);
  372. hm.adapter_index = pa->index;
  373. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  374. /* unmap PCI memory space, mapped during device init. */
  375. for (idx = 0; idx < HPI_MAX_ADAPTER_MEM_SPACES; idx++) {
  376. if (pa->ap_remapped_mem_base[idx]) {
  377. iounmap(pa->ap_remapped_mem_base[idx]);
  378. pa->ap_remapped_mem_base[idx] = NULL;
  379. }
  380. }
  381. if (pa->p_buffer)
  382. vfree(pa->p_buffer);
  383. pci_set_drvdata(pci_dev, NULL);
  384. if (1)
  385. dev_printk(KERN_INFO, &pci_dev->dev,
  386. "remove %04x:%04x,%04x:%04x,%04x," " HPI index %d.\n",
  387. pci_dev->vendor, pci_dev->device,
  388. pci_dev->subsystem_vendor, pci_dev->subsystem_device,
  389. pci_dev->devfn, pa->index);
  390. memset(pa, 0, sizeof(*pa));
  391. }
  392. void __init asihpi_init(void)
  393. {
  394. struct hpi_message hm;
  395. struct hpi_response hr;
  396. memset(adapters, 0, sizeof(adapters));
  397. printk(KERN_INFO "ASIHPI driver " HPI_VER_STRING "\n");
  398. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  399. HPI_SUBSYS_DRIVER_LOAD);
  400. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  401. }
  402. void asihpi_exit(void)
  403. {
  404. struct hpi_message hm;
  405. struct hpi_response hr;
  406. hpi_init_message_response(&hm, &hr, HPI_OBJ_SUBSYSTEM,
  407. HPI_SUBSYS_DRIVER_UNLOAD);
  408. hpi_send_recv_ex(&hm, &hr, HOWNER_KERNEL);
  409. }