0ef9555676acbdc600d1ac622dad3cd254ce1ca0
[openwrt/svn-archive/archive.git] / package / lqtapi / src / tapi / tapi-port.c
1 #include <linux/cdev.h>
2 #include <linux/fs.h>
3 #include <linux/list.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/slab.h>
7
8 #include <linux/tapi/tapi.h>
9 #include <linux/tapi/tapi-ioctl.h>
10
11 static inline struct tapi_port *tapi_char_device_to_port(struct tapi_char_device *chrdev)
12 {
13 return container_of(chrdev, struct tapi_port, chrdev);
14 }
15
16 static int tapi_port_open(struct inode *inode, struct file *file)
17 {
18 struct tapi_device *tdev = cdev_to_tapi_char_device(inode->i_cdev)->tdev;
19
20 get_device(&tdev->dev);
21 file->private_data = cdev_to_tapi_char_device(inode->i_cdev);
22
23 return 0;
24 }
25
26 static int tapi_port_release(struct inode *inode, struct file *file)
27 {
28 struct tapi_device *tdev = cdev_to_tapi_char_device(inode->i_cdev)->tdev;
29
30 put_device(&tdev->dev);
31
32 return 0;
33 }
34
35 static long tapi_port_ioctl_get_endpoint(struct tapi_device *tdev,
36 struct tapi_port *port, unsigned long arg)
37 {
38 return port->ep.id;
39 }
40
41 static long tapi_port_ioctl_set_ring(struct tapi_device *tdev,
42 struct tapi_port *port, unsigned long arg)
43 {
44 tapi_port_set_ring(tdev, port, arg);
45 return 0;
46 }
47
48 static long tapi_port_ioctl(struct file *file, unsigned int cmd,
49 unsigned long arg)
50 {
51 int ret;
52 struct tapi_char_device *tchrdev = file->private_data;
53 struct tapi_device *tdev = tchrdev->tdev;
54 struct tapi_port *port = tapi_char_device_to_port(tchrdev);
55
56 switch (cmd) {
57 case TAPI_PORT_IOCTL_GET_ENDPOINT:
58 ret = tapi_port_ioctl_get_endpoint(tdev, port, arg);
59 break;
60 case TAPI_PORT_IOCTL_SET_RING:
61 ret = tapi_port_ioctl_set_ring(tdev, port, arg);
62 break;
63 default:
64 ret = -EINVAL;
65 break;
66 }
67
68 return ret;
69 }
70
71 static const struct file_operations tapi_port_file_ops = {
72 .owner = THIS_MODULE,
73 .open = tapi_port_open,
74 .release = tapi_port_release,
75 .unlocked_ioctl = tapi_port_ioctl,
76 };
77
78 int tapi_register_port_device(struct tapi_device* tdev, struct tapi_port *port)
79 {
80 dev_set_name(&port->chrdev.dev, "tapi%uP%u", tdev->id, port->id);
81 return tapi_char_device_register(tdev, &port->chrdev, &tapi_port_file_ops);
82 }