divasi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /* $Id: divasi.c,v 1.25.6.2 2005/01/31 12:22:20 armin Exp $
  2. *
  3. * Driver for Eicon DIVA Server ISDN cards.
  4. * User Mode IDI Interface
  5. *
  6. * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
  7. * Copyright 2000-2003 Cytronics & Melware (info@melware.de)
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU General Public License, incorporated herein by reference.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/poll.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/seq_file.h>
  20. #include <asm/uaccess.h>
  21. #include "platform.h"
  22. #include "di_defs.h"
  23. #include "divasync.h"
  24. #include "um_xdi.h"
  25. #include "um_idi.h"
  26. static char *main_revision = "$Revision: 1.25.6.2 $";
  27. static int major;
  28. MODULE_DESCRIPTION("User IDI Interface for Eicon ISDN cards");
  29. MODULE_AUTHOR("Cytronics & Melware, Eicon Networks");
  30. MODULE_SUPPORTED_DEVICE("DIVA card driver");
  31. MODULE_LICENSE("GPL");
  32. typedef struct _diva_um_idi_os_context {
  33. wait_queue_head_t read_wait;
  34. wait_queue_head_t close_wait;
  35. struct timer_list diva_timer_id;
  36. int aborted;
  37. int adapter_nr;
  38. } diva_um_idi_os_context_t;
  39. static char *DRIVERNAME = "Eicon DIVA - User IDI (http://www.melware.net)";
  40. static char *DRIVERLNAME = "diva_idi";
  41. static char *DEVNAME = "DivasIDI";
  42. char *DRIVERRELEASE_IDI = "2.0";
  43. extern int idifunc_init(void);
  44. extern void idifunc_finit(void);
  45. /*
  46. * helper functions
  47. */
  48. static char *getrev(const char *revision)
  49. {
  50. char *rev;
  51. char *p;
  52. if ((p = strchr(revision, ':'))) {
  53. rev = p + 2;
  54. p = strchr(rev, '$');
  55. *--p = 0;
  56. } else
  57. rev = "1.0";
  58. return rev;
  59. }
  60. /*
  61. * LOCALS
  62. */
  63. static ssize_t um_idi_read(struct file *file, char __user *buf, size_t count,
  64. loff_t *offset);
  65. static ssize_t um_idi_write(struct file *file, const char __user *buf,
  66. size_t count, loff_t *offset);
  67. static unsigned int um_idi_poll(struct file *file, poll_table *wait);
  68. static int um_idi_open(struct inode *inode, struct file *file);
  69. static int um_idi_release(struct inode *inode, struct file *file);
  70. static int remove_entity(void *entity);
  71. static void diva_um_timer_function(unsigned long data);
  72. /*
  73. * proc entry
  74. */
  75. extern struct proc_dir_entry *proc_net_eicon;
  76. static struct proc_dir_entry *um_idi_proc_entry = NULL;
  77. static int um_idi_proc_show(struct seq_file *m, void *v)
  78. {
  79. char tmprev[32];
  80. seq_printf(m, "%s\n", DRIVERNAME);
  81. seq_printf(m, "name : %s\n", DRIVERLNAME);
  82. seq_printf(m, "release : %s\n", DRIVERRELEASE_IDI);
  83. strcpy(tmprev, main_revision);
  84. seq_printf(m, "revision : %s\n", getrev(tmprev));
  85. seq_printf(m, "build : %s\n", DIVA_BUILD);
  86. seq_printf(m, "major : %d\n", major);
  87. return 0;
  88. }
  89. static int um_idi_proc_open(struct inode *inode, struct file *file)
  90. {
  91. return single_open(file, um_idi_proc_show, NULL);
  92. }
  93. static const struct file_operations um_idi_proc_fops = {
  94. .owner = THIS_MODULE,
  95. .open = um_idi_proc_open,
  96. .read = seq_read,
  97. .llseek = seq_lseek,
  98. .release = single_release,
  99. };
  100. static int __init create_um_idi_proc(void)
  101. {
  102. um_idi_proc_entry = proc_create(DRIVERLNAME, S_IRUGO, proc_net_eicon,
  103. &um_idi_proc_fops);
  104. if (!um_idi_proc_entry)
  105. return (0);
  106. return (1);
  107. }
  108. static void remove_um_idi_proc(void)
  109. {
  110. if (um_idi_proc_entry) {
  111. remove_proc_entry(DRIVERLNAME, proc_net_eicon);
  112. um_idi_proc_entry = NULL;
  113. }
  114. }
  115. static const struct file_operations divas_idi_fops = {
  116. .owner = THIS_MODULE,
  117. .llseek = no_llseek,
  118. .read = um_idi_read,
  119. .write = um_idi_write,
  120. .poll = um_idi_poll,
  121. .open = um_idi_open,
  122. .release = um_idi_release
  123. };
  124. static void divas_idi_unregister_chrdev(void)
  125. {
  126. unregister_chrdev(major, DEVNAME);
  127. }
  128. static int __init divas_idi_register_chrdev(void)
  129. {
  130. if ((major = register_chrdev(0, DEVNAME, &divas_idi_fops)) < 0)
  131. {
  132. printk(KERN_ERR "%s: failed to create /dev entry.\n",
  133. DRIVERLNAME);
  134. return (0);
  135. }
  136. return (1);
  137. }
  138. /*
  139. ** Driver Load
  140. */
  141. static int __init divasi_init(void)
  142. {
  143. char tmprev[50];
  144. int ret = 0;
  145. printk(KERN_INFO "%s\n", DRIVERNAME);
  146. printk(KERN_INFO "%s: Rel:%s Rev:", DRIVERLNAME, DRIVERRELEASE_IDI);
  147. strcpy(tmprev, main_revision);
  148. printk("%s Build: %s\n", getrev(tmprev), DIVA_BUILD);
  149. if (!divas_idi_register_chrdev()) {
  150. ret = -EIO;
  151. goto out;
  152. }
  153. if (!create_um_idi_proc()) {
  154. divas_idi_unregister_chrdev();
  155. printk(KERN_ERR "%s: failed to create proc entry.\n",
  156. DRIVERLNAME);
  157. ret = -EIO;
  158. goto out;
  159. }
  160. if (!(idifunc_init())) {
  161. remove_um_idi_proc();
  162. divas_idi_unregister_chrdev();
  163. printk(KERN_ERR "%s: failed to connect to DIDD.\n",
  164. DRIVERLNAME);
  165. ret = -EIO;
  166. goto out;
  167. }
  168. printk(KERN_INFO "%s: started with major %d\n", DRIVERLNAME, major);
  169. out:
  170. return (ret);
  171. }
  172. /*
  173. ** Driver Unload
  174. */
  175. static void __exit divasi_exit(void)
  176. {
  177. idifunc_finit();
  178. remove_um_idi_proc();
  179. divas_idi_unregister_chrdev();
  180. printk(KERN_INFO "%s: module unloaded.\n", DRIVERLNAME);
  181. }
  182. module_init(divasi_init);
  183. module_exit(divasi_exit);
  184. /*
  185. * FILE OPERATIONS
  186. */
  187. static int
  188. divas_um_idi_copy_to_user(void *os_handle, void *dst, const void *src,
  189. int length)
  190. {
  191. memcpy(dst, src, length);
  192. return (length);
  193. }
  194. static ssize_t
  195. um_idi_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  196. {
  197. diva_um_idi_os_context_t *p_os;
  198. int ret = -EINVAL;
  199. void *data;
  200. if (!file->private_data) {
  201. return (-ENODEV);
  202. }
  203. if (!
  204. (p_os =
  205. (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
  206. private_data)))
  207. {
  208. return (-ENODEV);
  209. }
  210. if (p_os->aborted) {
  211. return (-ENODEV);
  212. }
  213. if (!(data = diva_os_malloc(0, count))) {
  214. return (-ENOMEM);
  215. }
  216. ret = diva_um_idi_read(file->private_data,
  217. file, data, count,
  218. divas_um_idi_copy_to_user);
  219. switch (ret) {
  220. case 0: /* no message available */
  221. ret = (-EAGAIN);
  222. break;
  223. case (-1): /* adapter was removed */
  224. ret = (-ENODEV);
  225. break;
  226. case (-2): /* message_length > length of user buffer */
  227. ret = (-EFAULT);
  228. break;
  229. }
  230. if (ret > 0) {
  231. if (copy_to_user(buf, data, ret)) {
  232. ret = (-EFAULT);
  233. }
  234. }
  235. diva_os_free(0, data);
  236. DBG_TRC(("read: ret %d", ret));
  237. return (ret);
  238. }
  239. static int
  240. divas_um_idi_copy_from_user(void *os_handle, void *dst, const void *src,
  241. int length)
  242. {
  243. memcpy(dst, src, length);
  244. return (length);
  245. }
  246. static int um_idi_open_adapter(struct file *file, int adapter_nr)
  247. {
  248. diva_um_idi_os_context_t *p_os;
  249. void *e =
  250. divas_um_idi_create_entity((dword) adapter_nr, (void *) file);
  251. if (!(file->private_data = e)) {
  252. return (0);
  253. }
  254. p_os = (diva_um_idi_os_context_t *) diva_um_id_get_os_context(e);
  255. init_waitqueue_head(&p_os->read_wait);
  256. init_waitqueue_head(&p_os->close_wait);
  257. init_timer(&p_os->diva_timer_id);
  258. p_os->diva_timer_id.function = (void *) diva_um_timer_function;
  259. p_os->diva_timer_id.data = (unsigned long) p_os;
  260. p_os->aborted = 0;
  261. p_os->adapter_nr = adapter_nr;
  262. return (1);
  263. }
  264. static ssize_t
  265. um_idi_write(struct file *file, const char __user *buf, size_t count,
  266. loff_t *offset)
  267. {
  268. diva_um_idi_os_context_t *p_os;
  269. int ret = -EINVAL;
  270. void *data;
  271. int adapter_nr = 0;
  272. if (!file->private_data) {
  273. /* the first write() selects the adapter_nr */
  274. if (count == sizeof(int)) {
  275. if (copy_from_user
  276. ((void *) &adapter_nr, buf,
  277. count)) return (-EFAULT);
  278. if (!(um_idi_open_adapter(file, adapter_nr)))
  279. return (-ENODEV);
  280. return (count);
  281. } else
  282. return (-ENODEV);
  283. }
  284. if (!(p_os =
  285. (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->
  286. private_data)))
  287. {
  288. return (-ENODEV);
  289. }
  290. if (p_os->aborted) {
  291. return (-ENODEV);
  292. }
  293. if (!(data = diva_os_malloc(0, count))) {
  294. return (-ENOMEM);
  295. }
  296. if (copy_from_user(data, buf, count)) {
  297. ret = -EFAULT;
  298. } else {
  299. ret = diva_um_idi_write(file->private_data,
  300. file, data, count,
  301. divas_um_idi_copy_from_user);
  302. switch (ret) {
  303. case 0: /* no space available */
  304. ret = (-EAGAIN);
  305. break;
  306. case (-1): /* adapter was removed */
  307. ret = (-ENODEV);
  308. break;
  309. case (-2): /* length of user buffer > max message_length */
  310. ret = (-EFAULT);
  311. break;
  312. }
  313. }
  314. diva_os_free(0, data);
  315. DBG_TRC(("write: ret %d", ret));
  316. return (ret);
  317. }
  318. static unsigned int um_idi_poll(struct file *file, poll_table *wait)
  319. {
  320. diva_um_idi_os_context_t *p_os;
  321. if (!file->private_data) {
  322. return (POLLERR);
  323. }
  324. if ((!(p_os =
  325. (diva_um_idi_os_context_t *)
  326. diva_um_id_get_os_context(file->private_data)))
  327. || p_os->aborted) {
  328. return (POLLERR);
  329. }
  330. poll_wait(file, &p_os->read_wait, wait);
  331. if (p_os->aborted) {
  332. return (POLLERR);
  333. }
  334. switch (diva_user_mode_idi_ind_ready(file->private_data, file)) {
  335. case (-1):
  336. return (POLLERR);
  337. case 0:
  338. return (0);
  339. }
  340. return (POLLIN | POLLRDNORM);
  341. }
  342. static int um_idi_open(struct inode *inode, struct file *file)
  343. {
  344. return (0);
  345. }
  346. static int um_idi_release(struct inode *inode, struct file *file)
  347. {
  348. diva_um_idi_os_context_t *p_os;
  349. unsigned int adapter_nr;
  350. int ret = 0;
  351. if (!(file->private_data)) {
  352. ret = -ENODEV;
  353. goto out;
  354. }
  355. if (!(p_os =
  356. (diva_um_idi_os_context_t *) diva_um_id_get_os_context(file->private_data))) {
  357. ret = -ENODEV;
  358. goto out;
  359. }
  360. adapter_nr = p_os->adapter_nr;
  361. if ((ret = remove_entity(file->private_data))) {
  362. goto out;
  363. }
  364. if (divas_um_idi_delete_entity
  365. ((int) adapter_nr, file->private_data)) {
  366. ret = -ENODEV;
  367. goto out;
  368. }
  369. out:
  370. return (ret);
  371. }
  372. int diva_os_get_context_size(void)
  373. {
  374. return (sizeof(diva_um_idi_os_context_t));
  375. }
  376. void diva_os_wakeup_read(void *os_context)
  377. {
  378. diva_um_idi_os_context_t *p_os =
  379. (diva_um_idi_os_context_t *) os_context;
  380. wake_up_interruptible(&p_os->read_wait);
  381. }
  382. void diva_os_wakeup_close(void *os_context)
  383. {
  384. diva_um_idi_os_context_t *p_os =
  385. (diva_um_idi_os_context_t *) os_context;
  386. wake_up_interruptible(&p_os->close_wait);
  387. }
  388. static
  389. void diva_um_timer_function(unsigned long data)
  390. {
  391. diva_um_idi_os_context_t *p_os = (diva_um_idi_os_context_t *) data;
  392. p_os->aborted = 1;
  393. wake_up_interruptible(&p_os->read_wait);
  394. wake_up_interruptible(&p_os->close_wait);
  395. DBG_ERR(("entity removal watchdog"))
  396. }
  397. /*
  398. ** If application exits without entity removal this function will remove
  399. ** entity and block until removal is complete
  400. */
  401. static int remove_entity(void *entity)
  402. {
  403. struct task_struct *curtask = current;
  404. diva_um_idi_os_context_t *p_os;
  405. diva_um_idi_stop_wdog(entity);
  406. if (!entity) {
  407. DBG_FTL(("Zero entity on remove"))
  408. return (0);
  409. }
  410. if (!(p_os =
  411. (diva_um_idi_os_context_t *)
  412. diva_um_id_get_os_context(entity))) {
  413. DBG_FTL(("Zero entity os context on remove"))
  414. return (0);
  415. }
  416. if (!divas_um_idi_entity_assigned(entity) || p_os->aborted) {
  417. /*
  418. Entity is not assigned, also can be removed
  419. */
  420. return (0);
  421. }
  422. DBG_TRC(("E(%08x) check remove", entity))
  423. /*
  424. If adapter not answers on remove request inside of
  425. 10 Sec, then adapter is dead
  426. */
  427. diva_um_idi_start_wdog(entity);
  428. {
  429. DECLARE_WAITQUEUE(wait, curtask);
  430. add_wait_queue(&p_os->close_wait, &wait);
  431. for (;;) {
  432. set_current_state(TASK_INTERRUPTIBLE);
  433. if (!divas_um_idi_entity_start_remove(entity)
  434. || p_os->aborted) {
  435. break;
  436. }
  437. schedule();
  438. }
  439. set_current_state(TASK_RUNNING);
  440. remove_wait_queue(&p_os->close_wait, &wait);
  441. }
  442. DBG_TRC(("E(%08x) start remove", entity))
  443. {
  444. DECLARE_WAITQUEUE(wait, curtask);
  445. add_wait_queue(&p_os->close_wait, &wait);
  446. for (;;) {
  447. set_current_state(TASK_INTERRUPTIBLE);
  448. if (!divas_um_idi_entity_assigned(entity)
  449. || p_os->aborted) {
  450. break;
  451. }
  452. schedule();
  453. }
  454. set_current_state(TASK_RUNNING);
  455. remove_wait_queue(&p_os->close_wait, &wait);
  456. }
  457. DBG_TRC(("E(%08x) remove complete, aborted:%d", entity,
  458. p_os->aborted))
  459. diva_um_idi_stop_wdog(entity);
  460. p_os->aborted = 0;
  461. return (0);
  462. }
  463. /*
  464. * timer watchdog
  465. */
  466. void diva_um_idi_start_wdog(void *entity)
  467. {
  468. diva_um_idi_os_context_t *p_os;
  469. if (entity &&
  470. ((p_os =
  471. (diva_um_idi_os_context_t *)
  472. diva_um_id_get_os_context(entity)))) {
  473. mod_timer(&p_os->diva_timer_id, jiffies + 10 * HZ);
  474. }
  475. }
  476. void diva_um_idi_stop_wdog(void *entity)
  477. {
  478. diva_um_idi_os_context_t *p_os;
  479. if (entity &&
  480. ((p_os =
  481. (diva_um_idi_os_context_t *)
  482. diva_um_id_get_os_context(entity)))) {
  483. del_timer(&p_os->diva_timer_id);
  484. }
  485. }