divasi.c 12 KB

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