Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[linux-2.6] / drivers / usb / serial / navman.c
1 /*
2  * Navman Serial USB driver
3  *
4  * Copyright (C) 2006 Greg Kroah-Hartman <gregkh@suse.de>
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      version 2 as published by the Free Software Foundation.
9  *
10  * TODO:
11  *      Add termios method that uses copy_hw but also kills all echo
12  *      flags as the navman is rx only so cannot echo.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/tty.h>
18 #include <linux/tty_flip.h>
19 #include <linux/module.h>
20 #include <linux/usb.h>
21 #include <linux/usb/serial.h>
22
23 static int debug;
24
25 static struct usb_device_id id_table [] = {
26         { USB_DEVICE(0x0a99, 0x0001) }, /* Talon Technology device */
27         { },
28 };
29 MODULE_DEVICE_TABLE(usb, id_table);
30
31 static struct usb_driver navman_driver = {
32         .name =         "navman",
33         .probe =        usb_serial_probe,
34         .disconnect =   usb_serial_disconnect,
35         .id_table =     id_table,
36         .no_dynamic_id =        1,
37 };
38
39 static void navman_read_int_callback(struct urb *urb)
40 {
41         struct usb_serial_port *port = urb->context;
42         unsigned char *data = urb->transfer_buffer;
43         struct tty_struct *tty;
44         int status = urb->status;
45         int result;
46
47         switch (status) {
48         case 0:
49                 /* success */
50                 break;
51         case -ECONNRESET:
52         case -ENOENT:
53         case -ESHUTDOWN:
54                 /* this urb is terminated, clean up */
55                 dbg("%s - urb shutting down with status: %d",
56                     __func__, status);
57                 return;
58         default:
59                 dbg("%s - nonzero urb status received: %d",
60                     __func__, status);
61                 goto exit;
62         }
63
64         usb_serial_debug_data(debug, &port->dev, __func__,
65                               urb->actual_length, data);
66
67         tty = tty_port_tty_get(&port->port);
68         if (tty && urb->actual_length) {
69                 tty_buffer_request_room(tty, urb->actual_length);
70                 tty_insert_flip_string(tty, data, urb->actual_length);
71                 tty_flip_buffer_push(tty);
72         }
73         tty_kref_put(tty);
74
75 exit:
76         result = usb_submit_urb(urb, GFP_ATOMIC);
77         if (result)
78                 dev_err(&urb->dev->dev,
79                         "%s - Error %d submitting interrupt urb\n",
80                         __func__, result);
81 }
82
83 static int navman_open(struct tty_struct *tty,
84                         struct usb_serial_port *port, struct file *filp)
85 {
86         int result = 0;
87
88         dbg("%s - port %d", __func__, port->number);
89
90         if (port->interrupt_in_urb) {
91                 dbg("%s - adding interrupt input for treo", __func__);
92                 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
93                 if (result)
94                         dev_err(&port->dev,
95                                 "%s - failed submitting interrupt urb, error %d\n",
96                                 __func__, result);
97         }
98         return result;
99 }
100
101 static void navman_close(struct tty_struct *tty,
102                         struct usb_serial_port *port, struct file *filp)
103 {
104         dbg("%s - port %d", __func__, port->number);
105
106         usb_kill_urb(port->interrupt_in_urb);
107 }
108
109 static int navman_write(struct tty_struct *tty, struct usb_serial_port *port,
110                         const unsigned char *buf, int count)
111 {
112         dbg("%s - port %d", __func__, port->number);
113
114         /*
115          * This device can't write any data, only read from the device
116          */
117         return -EOPNOTSUPP;
118 }
119
120 static struct usb_serial_driver navman_device = {
121         .driver = {
122                 .owner =        THIS_MODULE,
123                 .name =         "navman",
124         },
125         .id_table =             id_table,
126         .usb_driver =           &navman_driver,
127         .num_ports =            1,
128         .open =                 navman_open,
129         .close =                navman_close,
130         .write =                navman_write,
131         .read_int_callback =    navman_read_int_callback,
132 };
133
134 static int __init navman_init(void)
135 {
136         int retval;
137
138         retval = usb_serial_register(&navman_device);
139         if (retval)
140                 return retval;
141         retval = usb_register(&navman_driver);
142         if (retval)
143                 usb_serial_deregister(&navman_device);
144         return retval;
145 }
146
147 static void __exit navman_exit(void)
148 {
149         usb_deregister(&navman_driver);
150         usb_serial_deregister(&navman_device);
151 }
152
153 module_init(navman_init);
154 module_exit(navman_exit);
155 MODULE_LICENSE("GPL");
156
157 module_param(debug, bool, S_IRUGO | S_IWUSR);
158 MODULE_PARM_DESC(debug, "Debug enabled or not");