serial_acm.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * serial_acm.c -- USB modem serial driver
  3. *
  4. * Copyright 2008 (C) Samsung Electronics
  5. *
  6. */
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/fs.h>
  11. #include <linux/errno.h>
  12. #include <linux/types.h>
  13. #include <linux/fcntl.h>
  14. #include <linux/device.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/io.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/sched.h>
  20. #include <linux/wait.h>
  21. #include <linux/poll.h>
  22. #include <linux/usb/cdc.h>
  23. static void acm_notify(void *dev, u16 state);
  24. static wait_queue_head_t modem_wait_q;
  25. static unsigned int read_state;
  26. static unsigned int control_line_state;
  27. static void *acm_data;
  28. static int modem_open(struct inode *inode, struct file *file)
  29. {
  30. read_state = 0;
  31. return 0;
  32. }
  33. static int modem_close(struct inode *inode, struct file *file)
  34. {
  35. return 0;
  36. }
  37. static ssize_t modem_read(struct file *file, char __user *buf,
  38. size_t count, loff_t *ppos)
  39. {
  40. int ret = 0;
  41. if (file->f_flags & O_NONBLOCK)
  42. return -EAGAIN;
  43. ret = wait_event_interruptible(modem_wait_q, read_state);
  44. if (ret)
  45. return ret;
  46. if (copy_to_user(buf, &control_line_state, sizeof(u32)))
  47. return -EFAULT;
  48. read_state = 0;
  49. return sizeof(u32);
  50. }
  51. static unsigned int modem_poll(struct file *file, poll_table *wait)
  52. {
  53. int ret;
  54. poll_wait(file, &modem_wait_q, wait);
  55. ret = (read_state ? (POLLIN | POLLRDNORM) : 0);
  56. return ret;
  57. }
  58. void notify_control_line_state(u32 value)
  59. {
  60. control_line_state = value;
  61. read_state = 1;
  62. wake_up_interruptible(&modem_wait_q);
  63. }
  64. EXPORT_SYMBOL(notify_control_line_state);
  65. #define GS_CDC_NOTIFY_SERIAL_STATE _IOW('S', 1, int)
  66. #define GS_IOC_NOTIFY_DTR_TEST _IOW('S', 3, int)
  67. static long
  68. modem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  69. {
  70. /* handle ioctls */
  71. switch (cmd) {
  72. case GS_CDC_NOTIFY_SERIAL_STATE:
  73. acm_notify(acm_data, __constant_cpu_to_le16(arg));
  74. break;
  75. case GS_IOC_NOTIFY_DTR_TEST:
  76. {
  77. printk(KERN_ALERT"DUN : DTR %d\n", (int)arg);
  78. notify_control_line_state((int)arg);
  79. break;
  80. }
  81. default:
  82. printk(KERN_INFO "modem_ioctl: Unknown ioctl cmd(0x%x).\n",
  83. cmd);
  84. return -ENOIOCTLCMD;
  85. }
  86. return 0;
  87. }
  88. static const struct file_operations modem_fops = {
  89. .owner = THIS_MODULE,
  90. .open = modem_open,
  91. .release = modem_close,
  92. .read = modem_read,
  93. .poll = modem_poll,
  94. .llseek = no_llseek,
  95. .unlocked_ioctl = modem_ioctl,
  96. };
  97. static struct miscdevice modem_device = {
  98. .minor = 123,
  99. .name = "dun",
  100. .fops = &modem_fops,
  101. };
  102. int modem_register(void *data)
  103. {
  104. if (data == NULL) {
  105. printk(KERN_INFO "DUN register failed. data is null.\n");
  106. return -1;
  107. }
  108. acm_data = data;
  109. printk(KERN_INFO "DUN is registerd\n");
  110. return 0;
  111. }
  112. EXPORT_SYMBOL(modem_register);
  113. static int modem_misc_register(void)
  114. {
  115. int ret;
  116. ret = misc_register(&modem_device);
  117. if (ret) {
  118. printk(KERN_ERR "DUN register is failed, ret = %d\n", ret);
  119. return ret;
  120. }
  121. init_waitqueue_head(&modem_wait_q);
  122. return ret;
  123. }
  124. void modem_unregister(void)
  125. {
  126. acm_data = NULL;
  127. read_state = 1;
  128. wake_up_interruptible(&modem_wait_q);
  129. printk(KERN_INFO "DUN is unregisterd\n");
  130. }
  131. EXPORT_SYMBOL(modem_unregister);