core.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * GNSS receiver core
  4. *
  5. * Copyright (C) 2018 Johan Hovold <[email protected]>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/cdev.h>
  9. #include <linux/errno.h>
  10. #include <linux/fs.h>
  11. #include <linux/gnss.h>
  12. #include <linux/idr.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/poll.h>
  17. #include <linux/slab.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/wait.h>
  20. #define GNSS_FLAG_HAS_WRITE_RAW BIT(0)
  21. #define GNSS_MINORS 16
  22. static DEFINE_IDA(gnss_minors);
  23. static dev_t gnss_first;
  24. /* FIFO size must be a power of two */
  25. #define GNSS_READ_FIFO_SIZE 4096
  26. #define GNSS_WRITE_BUF_SIZE 1024
  27. #define to_gnss_device(d) container_of((d), struct gnss_device, dev)
  28. static int gnss_open(struct inode *inode, struct file *file)
  29. {
  30. struct gnss_device *gdev;
  31. int ret = 0;
  32. gdev = container_of(inode->i_cdev, struct gnss_device, cdev);
  33. get_device(&gdev->dev);
  34. stream_open(inode, file);
  35. file->private_data = gdev;
  36. down_write(&gdev->rwsem);
  37. if (gdev->disconnected) {
  38. ret = -ENODEV;
  39. goto unlock;
  40. }
  41. if (gdev->count++ == 0) {
  42. ret = gdev->ops->open(gdev);
  43. if (ret)
  44. gdev->count--;
  45. }
  46. unlock:
  47. up_write(&gdev->rwsem);
  48. if (ret)
  49. put_device(&gdev->dev);
  50. return ret;
  51. }
  52. static int gnss_release(struct inode *inode, struct file *file)
  53. {
  54. struct gnss_device *gdev = file->private_data;
  55. down_write(&gdev->rwsem);
  56. if (gdev->disconnected)
  57. goto unlock;
  58. if (--gdev->count == 0) {
  59. gdev->ops->close(gdev);
  60. kfifo_reset(&gdev->read_fifo);
  61. }
  62. unlock:
  63. up_write(&gdev->rwsem);
  64. put_device(&gdev->dev);
  65. return 0;
  66. }
  67. static ssize_t gnss_read(struct file *file, char __user *buf,
  68. size_t count, loff_t *pos)
  69. {
  70. struct gnss_device *gdev = file->private_data;
  71. unsigned int copied;
  72. int ret;
  73. mutex_lock(&gdev->read_mutex);
  74. while (kfifo_is_empty(&gdev->read_fifo)) {
  75. mutex_unlock(&gdev->read_mutex);
  76. if (gdev->disconnected)
  77. return 0;
  78. if (file->f_flags & O_NONBLOCK)
  79. return -EAGAIN;
  80. ret = wait_event_interruptible(gdev->read_queue,
  81. gdev->disconnected ||
  82. !kfifo_is_empty(&gdev->read_fifo));
  83. if (ret)
  84. return -ERESTARTSYS;
  85. mutex_lock(&gdev->read_mutex);
  86. }
  87. ret = kfifo_to_user(&gdev->read_fifo, buf, count, &copied);
  88. if (ret == 0)
  89. ret = copied;
  90. mutex_unlock(&gdev->read_mutex);
  91. return ret;
  92. }
  93. static ssize_t gnss_write(struct file *file, const char __user *buf,
  94. size_t count, loff_t *pos)
  95. {
  96. struct gnss_device *gdev = file->private_data;
  97. size_t written = 0;
  98. int ret;
  99. if (gdev->disconnected)
  100. return -EIO;
  101. if (!count)
  102. return 0;
  103. if (!(gdev->flags & GNSS_FLAG_HAS_WRITE_RAW))
  104. return -EIO;
  105. /* Ignoring O_NONBLOCK, write_raw() is synchronous. */
  106. ret = mutex_lock_interruptible(&gdev->write_mutex);
  107. if (ret)
  108. return -ERESTARTSYS;
  109. for (;;) {
  110. size_t n = count - written;
  111. if (n > GNSS_WRITE_BUF_SIZE)
  112. n = GNSS_WRITE_BUF_SIZE;
  113. if (copy_from_user(gdev->write_buf, buf, n)) {
  114. ret = -EFAULT;
  115. goto out_unlock;
  116. }
  117. /*
  118. * Assumes write_raw can always accept GNSS_WRITE_BUF_SIZE
  119. * bytes.
  120. *
  121. * FIXME: revisit
  122. */
  123. down_read(&gdev->rwsem);
  124. if (!gdev->disconnected)
  125. ret = gdev->ops->write_raw(gdev, gdev->write_buf, n);
  126. else
  127. ret = -EIO;
  128. up_read(&gdev->rwsem);
  129. if (ret < 0)
  130. break;
  131. written += ret;
  132. buf += ret;
  133. if (written == count)
  134. break;
  135. }
  136. if (written)
  137. ret = written;
  138. out_unlock:
  139. mutex_unlock(&gdev->write_mutex);
  140. return ret;
  141. }
  142. static __poll_t gnss_poll(struct file *file, poll_table *wait)
  143. {
  144. struct gnss_device *gdev = file->private_data;
  145. __poll_t mask = 0;
  146. poll_wait(file, &gdev->read_queue, wait);
  147. if (!kfifo_is_empty(&gdev->read_fifo))
  148. mask |= EPOLLIN | EPOLLRDNORM;
  149. if (gdev->disconnected)
  150. mask |= EPOLLHUP;
  151. return mask;
  152. }
  153. static const struct file_operations gnss_fops = {
  154. .owner = THIS_MODULE,
  155. .open = gnss_open,
  156. .release = gnss_release,
  157. .read = gnss_read,
  158. .write = gnss_write,
  159. .poll = gnss_poll,
  160. .llseek = no_llseek,
  161. };
  162. static struct class *gnss_class;
  163. static void gnss_device_release(struct device *dev)
  164. {
  165. struct gnss_device *gdev = to_gnss_device(dev);
  166. kfree(gdev->write_buf);
  167. kfifo_free(&gdev->read_fifo);
  168. ida_free(&gnss_minors, gdev->id);
  169. kfree(gdev);
  170. }
  171. struct gnss_device *gnss_allocate_device(struct device *parent)
  172. {
  173. struct gnss_device *gdev;
  174. struct device *dev;
  175. int id;
  176. int ret;
  177. gdev = kzalloc(sizeof(*gdev), GFP_KERNEL);
  178. if (!gdev)
  179. return NULL;
  180. id = ida_alloc_max(&gnss_minors, GNSS_MINORS - 1, GFP_KERNEL);
  181. if (id < 0) {
  182. kfree(gdev);
  183. return NULL;
  184. }
  185. gdev->id = id;
  186. dev = &gdev->dev;
  187. device_initialize(dev);
  188. dev->devt = gnss_first + id;
  189. dev->class = gnss_class;
  190. dev->parent = parent;
  191. dev->release = gnss_device_release;
  192. dev_set_drvdata(dev, gdev);
  193. dev_set_name(dev, "gnss%d", id);
  194. init_rwsem(&gdev->rwsem);
  195. mutex_init(&gdev->read_mutex);
  196. mutex_init(&gdev->write_mutex);
  197. init_waitqueue_head(&gdev->read_queue);
  198. ret = kfifo_alloc(&gdev->read_fifo, GNSS_READ_FIFO_SIZE, GFP_KERNEL);
  199. if (ret)
  200. goto err_put_device;
  201. gdev->write_buf = kzalloc(GNSS_WRITE_BUF_SIZE, GFP_KERNEL);
  202. if (!gdev->write_buf)
  203. goto err_put_device;
  204. cdev_init(&gdev->cdev, &gnss_fops);
  205. gdev->cdev.owner = THIS_MODULE;
  206. return gdev;
  207. err_put_device:
  208. put_device(dev);
  209. return NULL;
  210. }
  211. EXPORT_SYMBOL_GPL(gnss_allocate_device);
  212. void gnss_put_device(struct gnss_device *gdev)
  213. {
  214. put_device(&gdev->dev);
  215. }
  216. EXPORT_SYMBOL_GPL(gnss_put_device);
  217. int gnss_register_device(struct gnss_device *gdev)
  218. {
  219. int ret;
  220. /* Set a flag which can be accessed without holding the rwsem. */
  221. if (gdev->ops->write_raw != NULL)
  222. gdev->flags |= GNSS_FLAG_HAS_WRITE_RAW;
  223. ret = cdev_device_add(&gdev->cdev, &gdev->dev);
  224. if (ret) {
  225. dev_err(&gdev->dev, "failed to add device: %d\n", ret);
  226. return ret;
  227. }
  228. return 0;
  229. }
  230. EXPORT_SYMBOL_GPL(gnss_register_device);
  231. void gnss_deregister_device(struct gnss_device *gdev)
  232. {
  233. down_write(&gdev->rwsem);
  234. gdev->disconnected = true;
  235. if (gdev->count) {
  236. wake_up_interruptible(&gdev->read_queue);
  237. gdev->ops->close(gdev);
  238. }
  239. up_write(&gdev->rwsem);
  240. cdev_device_del(&gdev->cdev, &gdev->dev);
  241. }
  242. EXPORT_SYMBOL_GPL(gnss_deregister_device);
  243. /*
  244. * Caller guarantees serialisation.
  245. *
  246. * Must not be called for a closed device.
  247. */
  248. int gnss_insert_raw(struct gnss_device *gdev, const unsigned char *buf,
  249. size_t count)
  250. {
  251. int ret;
  252. ret = kfifo_in(&gdev->read_fifo, buf, count);
  253. wake_up_interruptible(&gdev->read_queue);
  254. return ret;
  255. }
  256. EXPORT_SYMBOL_GPL(gnss_insert_raw);
  257. static const char * const gnss_type_names[GNSS_TYPE_COUNT] = {
  258. [GNSS_TYPE_NMEA] = "NMEA",
  259. [GNSS_TYPE_SIRF] = "SiRF",
  260. [GNSS_TYPE_UBX] = "UBX",
  261. [GNSS_TYPE_MTK] = "MTK",
  262. };
  263. static const char *gnss_type_name(struct gnss_device *gdev)
  264. {
  265. const char *name = NULL;
  266. if (gdev->type < GNSS_TYPE_COUNT)
  267. name = gnss_type_names[gdev->type];
  268. if (!name)
  269. dev_WARN(&gdev->dev, "type name not defined\n");
  270. return name;
  271. }
  272. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  273. char *buf)
  274. {
  275. struct gnss_device *gdev = to_gnss_device(dev);
  276. return sprintf(buf, "%s\n", gnss_type_name(gdev));
  277. }
  278. static DEVICE_ATTR_RO(type);
  279. static struct attribute *gnss_attrs[] = {
  280. &dev_attr_type.attr,
  281. NULL,
  282. };
  283. ATTRIBUTE_GROUPS(gnss);
  284. static int gnss_uevent(struct device *dev, struct kobj_uevent_env *env)
  285. {
  286. struct gnss_device *gdev = to_gnss_device(dev);
  287. int ret;
  288. ret = add_uevent_var(env, "GNSS_TYPE=%s", gnss_type_name(gdev));
  289. if (ret)
  290. return ret;
  291. return 0;
  292. }
  293. static int __init gnss_module_init(void)
  294. {
  295. int ret;
  296. ret = alloc_chrdev_region(&gnss_first, 0, GNSS_MINORS, "gnss");
  297. if (ret < 0) {
  298. pr_err("failed to allocate device numbers: %d\n", ret);
  299. return ret;
  300. }
  301. gnss_class = class_create(THIS_MODULE, "gnss");
  302. if (IS_ERR(gnss_class)) {
  303. ret = PTR_ERR(gnss_class);
  304. pr_err("failed to create class: %d\n", ret);
  305. goto err_unregister_chrdev;
  306. }
  307. gnss_class->dev_groups = gnss_groups;
  308. gnss_class->dev_uevent = gnss_uevent;
  309. pr_info("GNSS driver registered with major %d\n", MAJOR(gnss_first));
  310. return 0;
  311. err_unregister_chrdev:
  312. unregister_chrdev_region(gnss_first, GNSS_MINORS);
  313. return ret;
  314. }
  315. module_init(gnss_module_init);
  316. static void __exit gnss_module_exit(void)
  317. {
  318. class_destroy(gnss_class);
  319. unregister_chrdev_region(gnss_first, GNSS_MINORS);
  320. ida_destroy(&gnss_minors);
  321. }
  322. module_exit(gnss_module_exit);
  323. MODULE_AUTHOR("Johan Hovold <[email protected]>");
  324. MODULE_DESCRIPTION("GNSS receiver core");
  325. MODULE_LICENSE("GPL v2");