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