f3824641099ae5a1403105b159a9d2f900b07c43
[cascardo/linux.git] / drivers / usb / serial / omninet.c
1 /*
2  * USB ZyXEL omni.net LCD PLUS driver
3  *
4  *      This program is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU General Public License version
6  *      2 as published by the Free Software Foundation.
7  *
8  * See Documentation/usb/usb-serial.txt for more information on using this
9  * driver
10  *
11  * Please report both successes and troubles to the author at omninet@kroah.com
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/tty.h>
19 #include <linux/tty_driver.h>
20 #include <linux/tty_flip.h>
21 #include <linux/module.h>
22 #include <linux/uaccess.h>
23 #include <linux/usb.h>
24 #include <linux/usb/serial.h>
25
26 static int debug;
27
28 /*
29  * Version Information
30  */
31 #define DRIVER_VERSION "v1.1"
32 #define DRIVER_AUTHOR "Alessandro Zummo"
33 #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
34
35 #define ZYXEL_VENDOR_ID         0x0586
36 #define ZYXEL_OMNINET_ID        0x1000
37 /* This one seems to be a re-branded ZyXEL device */
38 #define BT_IGNITIONPRO_ID       0x2000
39
40 /* function prototypes */
41 static int  omninet_open(struct tty_struct *tty, struct usb_serial_port *port);
42 static void omninet_close(struct usb_serial_port *port);
43 static void omninet_read_bulk_callback(struct urb *urb);
44 static void omninet_write_bulk_callback(struct urb *urb);
45 static int  omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
46                                 const unsigned char *buf, int count);
47 static int  omninet_write_room(struct tty_struct *tty);
48 static void omninet_disconnect(struct usb_serial *serial);
49 static void omninet_release(struct usb_serial *serial);
50 static int omninet_attach(struct usb_serial *serial);
51
52 static const struct usb_device_id id_table[] = {
53         { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
54         { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
55         { }                                             /* Terminating entry */
56 };
57
58 MODULE_DEVICE_TABLE(usb, id_table);
59
60 static struct usb_driver omninet_driver = {
61         .name =         "omninet",
62         .probe =        usb_serial_probe,
63         .disconnect =   usb_serial_disconnect,
64         .id_table =     id_table,
65         .no_dynamic_id =        1,
66 };
67
68
69 static struct usb_serial_driver zyxel_omninet_device = {
70         .driver = {
71                 .owner =        THIS_MODULE,
72                 .name =         "omninet",
73         },
74         .description =          "ZyXEL - omni.net lcd plus usb",
75         .usb_driver =           &omninet_driver,
76         .id_table =             id_table,
77         .num_ports =            1,
78         .attach =               omninet_attach,
79         .open =                 omninet_open,
80         .close =                omninet_close,
81         .write =                omninet_write,
82         .write_room =           omninet_write_room,
83         .read_bulk_callback =   omninet_read_bulk_callback,
84         .write_bulk_callback =  omninet_write_bulk_callback,
85         .disconnect =           omninet_disconnect,
86         .release =              omninet_release,
87 };
88
89
90 /* The protocol.
91  *
92  * The omni.net always exchange 64 bytes of data with the host. The first
93  * four bytes are the control header, you can see it in the above structure.
94  *
95  * oh_seq is a sequence number. Don't know if/how it's used.
96  * oh_len is the length of the data bytes in the packet.
97  * oh_xxx Bit-mapped, related to handshaking and status info.
98  *      I normally set it to 0x03 in trasmitted frames.
99  *      7: Active when the TA is in a CONNECTed state.
100  *      6: unknown
101  *      5: handshaking, unknown
102  *      4: handshaking, unknown
103  *      3: unknown, usually 0
104  *      2: unknown, usually 0
105  *      1: handshaking, unknown, usually set to 1 in trasmitted frames
106  *      0: handshaking, unknown, usually set to 1 in trasmitted frames
107  * oh_pad Probably a pad byte.
108  *
109  * After the header you will find data bytes if oh_len was greater than zero.
110  *
111  */
112
113 struct omninet_header {
114         __u8    oh_seq;
115         __u8    oh_len;
116         __u8    oh_xxx;
117         __u8    oh_pad;
118 };
119
120 struct omninet_data {
121         __u8    od_outseq;      /* Sequence number for bulk_out URBs */
122 };
123
124 static int omninet_attach(struct usb_serial *serial)
125 {
126         struct omninet_data *od;
127         struct usb_serial_port *port = serial->port[0];
128
129         od = kmalloc(sizeof(struct omninet_data), GFP_KERNEL);
130         if (!od) {
131                 dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n",
132                         __func__, sizeof(struct omninet_data));
133                 return -ENOMEM;
134         }
135         usb_set_serial_port_data(port, od);
136         return 0;
137 }
138
139 static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port)
140 {
141         struct usb_serial       *serial = port->serial;
142         struct usb_serial_port  *wport;
143         int                     result = 0;
144
145         dbg("%s - port %d", __func__, port->number);
146
147         wport = serial->port[1];
148         tty_port_tty_set(&wport->port, tty);
149
150         /* Start reading from the device */
151         usb_fill_bulk_urb(port->read_urb, serial->dev,
152                         usb_rcvbulkpipe(serial->dev,
153                                 port->bulk_in_endpointAddress),
154                         port->read_urb->transfer_buffer,
155                         port->read_urb->transfer_buffer_length,
156                         omninet_read_bulk_callback, port);
157
158         result = usb_submit_urb(port->read_urb, GFP_KERNEL);
159         if (result)
160                 dev_err(&port->dev,
161                         "%s - failed submitting read urb, error %d\n",
162                         __func__, result);
163         return result;
164 }
165
166 static void omninet_close(struct usb_serial_port *port)
167 {
168         dbg("%s - port %d", __func__, port->number);
169         usb_kill_urb(port->read_urb);
170 }
171
172
173 #define OMNINET_DATAOFFSET      0x04
174 #define OMNINET_HEADERLEN       sizeof(struct omninet_header)
175 #define OMNINET_BULKOUTSIZE     (64 - OMNINET_HEADERLEN)
176
177 static void omninet_read_bulk_callback(struct urb *urb)
178 {
179         struct usb_serial_port  *port   = urb->context;
180         unsigned char           *data   = urb->transfer_buffer;
181         struct omninet_header   *header = (struct omninet_header *) &data[0];
182         int status = urb->status;
183         int result;
184         int i;
185
186         dbg("%s - port %d", __func__, port->number);
187
188         if (status) {
189                 dbg("%s - nonzero read bulk status received: %d",
190                     __func__, status);
191                 return;
192         }
193
194         if (debug && header->oh_xxx != 0x30) {
195                 if (urb->actual_length) {
196                         printk(KERN_DEBUG "%s: omninet_read %d: ",
197                                __FILE__, header->oh_len);
198                         for (i = 0; i < (header->oh_len +
199                                                 OMNINET_HEADERLEN); i++)
200                                 printk("%.2x ", data[i]);
201                         printk("\n");
202                 }
203         }
204
205         if (urb->actual_length && header->oh_len) {
206                 struct tty_struct *tty = tty_port_tty_get(&port->port);
207                 tty_insert_flip_string(tty, data + OMNINET_DATAOFFSET,
208                                                         header->oh_len);
209                 tty_flip_buffer_push(tty);
210                 tty_kref_put(tty);
211         }
212
213         /* Continue trying to always read  */
214         usb_fill_bulk_urb(urb, port->serial->dev,
215                         usb_rcvbulkpipe(port->serial->dev,
216                                         port->bulk_in_endpointAddress),
217                         urb->transfer_buffer, urb->transfer_buffer_length,
218                         omninet_read_bulk_callback, port);
219         result = usb_submit_urb(urb, GFP_ATOMIC);
220         if (result)
221                 dev_err(&port->dev,
222                         "%s - failed resubmitting read urb, error %d\n",
223                         __func__, result);
224 }
225
226 static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
227                                         const unsigned char *buf, int count)
228 {
229         struct usb_serial *serial = port->serial;
230         struct usb_serial_port *wport = serial->port[1];
231
232         struct omninet_data *od = usb_get_serial_port_data(port);
233         struct omninet_header *header = (struct omninet_header *)
234                                         wport->write_urb->transfer_buffer;
235
236         int                     result;
237
238         dbg("%s - port %d", __func__, port->number);
239
240         if (count == 0) {
241                 dbg("%s - write request of 0 bytes", __func__);
242                 return 0;
243         }
244
245         if (!test_and_clear_bit(0, &port->write_urbs_free)) {
246                 dbg("%s - already writing", __func__);
247                 return 0;
248         }
249
250         count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
251
252         memcpy(wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET,
253                                                                 buf, count);
254
255         usb_serial_debug_data(debug, &port->dev, __func__, count,
256                                         wport->write_urb->transfer_buffer);
257
258         header->oh_seq  = od->od_outseq++;
259         header->oh_len  = count;
260         header->oh_xxx  = 0x03;
261         header->oh_pad  = 0x00;
262
263         /* send the data out the bulk port, always 64 bytes */
264         wport->write_urb->transfer_buffer_length = 64;
265
266         result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
267         if (result) {
268                 set_bit(0, &wport->write_urbs_free);
269                 dev_err(&port->dev,
270                         "%s - failed submitting write urb, error %d\n",
271                         __func__, result);
272         } else
273                 result = count;
274
275         return result;
276 }
277
278
279 static int omninet_write_room(struct tty_struct *tty)
280 {
281         struct usb_serial_port *port = tty->driver_data;
282         struct usb_serial       *serial = port->serial;
283         struct usb_serial_port  *wport  = serial->port[1];
284
285         int room = 0; /* Default: no room */
286
287         if (test_bit(0, &wport->write_urbs_free))
288                 room = wport->bulk_out_size - OMNINET_HEADERLEN;
289
290         dbg("%s - returns %d", __func__, room);
291
292         return room;
293 }
294
295 static void omninet_write_bulk_callback(struct urb *urb)
296 {
297 /*      struct omninet_header   *header = (struct omninet_header  *)
298                                                 urb->transfer_buffer; */
299         struct usb_serial_port  *port   =  urb->context;
300         int status = urb->status;
301
302         dbg("%s - port %0x", __func__, port->number);
303
304         set_bit(0, &port->write_urbs_free);
305         if (status) {
306                 dbg("%s - nonzero write bulk status received: %d",
307                     __func__, status);
308                 return;
309         }
310
311         usb_serial_port_softint(port);
312 }
313
314
315 static void omninet_disconnect(struct usb_serial *serial)
316 {
317         struct usb_serial_port *wport = serial->port[1];
318
319         dbg("%s", __func__);
320
321         usb_kill_urb(wport->write_urb);
322 }
323
324
325 static void omninet_release(struct usb_serial *serial)
326 {
327         struct usb_serial_port *port = serial->port[0];
328
329         dbg("%s", __func__);
330
331         kfree(usb_get_serial_port_data(port));
332 }
333
334
335 static int __init omninet_init(void)
336 {
337         int retval;
338         retval = usb_serial_register(&zyxel_omninet_device);
339         if (retval)
340                 goto failed_usb_serial_register;
341         retval = usb_register(&omninet_driver);
342         if (retval)
343                 goto failed_usb_register;
344         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
345                DRIVER_DESC "\n");
346         return 0;
347 failed_usb_register:
348         usb_serial_deregister(&zyxel_omninet_device);
349 failed_usb_serial_register:
350         return retval;
351 }
352
353
354 static void __exit omninet_exit(void)
355 {
356         usb_deregister(&omninet_driver);
357         usb_serial_deregister(&zyxel_omninet_device);
358 }
359
360
361 module_init(omninet_init);
362 module_exit(omninet_exit);
363
364 MODULE_AUTHOR(DRIVER_AUTHOR);
365 MODULE_DESCRIPTION(DRIVER_DESC);
366 MODULE_LICENSE("GPL");
367
368 module_param(debug, bool, S_IRUGO | S_IWUSR);
369 MODULE_PARM_DESC(debug, "Debug enabled or not");