ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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 modify
5  *      it under the terms of the GNU General Public License as published by
6  *      the Free Software Foundation; either version 2 of the License, or
7  *      (at your option) any later version.
8  *
9  * See Documentation/usb/usb-serial.txt for more information on using this driver
10  *
11  * Please report both successes and troubles to the author at omninet@kroah.com
12  * 
13  * (05/30/2001) gkh
14  *      switched from using spinlock to a semaphore, which fixes lots of problems.
15  *
16  * (04/08/2001) gb
17  *      Identify version on module load.
18  *
19  * (11/01/2000) Adam J. Richter
20  *      usb_device_id table support
21  * 
22  * (10/05/2000) gkh
23  *      Fixed bug with urb->dev not being set properly, now that the usb
24  *      core needs it.
25  * 
26  * (08/28/2000) gkh
27  *      Added locks for SMP safeness.
28  *      Fixed MOD_INC and MOD_DEC logic and the ability to open a port more 
29  *      than once.
30  *      Fixed potential race in omninet_write_bulk_callback
31  *
32  * (07/19/2000) gkh
33  *      Added module_init and module_exit functions to handle the fact that this
34  *      driver is a loadable module now.
35  *
36  */
37
38 #include <linux/config.h>
39 #include <linux/kernel.h>
40 #include <linux/errno.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/tty.h>
44 #include <linux/tty_driver.h>
45 #include <linux/tty_flip.h>
46 #include <linux/module.h>
47 #include <linux/spinlock.h>
48 #include <asm/uaccess.h>
49 #include <linux/usb.h>
50
51 #ifdef CONFIG_USB_SERIAL_DEBUG
52         static int debug = 1;
53 #else
54         static int debug;
55 #endif
56
57 #include "usb-serial.h"
58
59
60 /*
61  * Version Information
62  */
63 #define DRIVER_VERSION "v1.1"
64 #define DRIVER_AUTHOR "Alessandro Zummo"
65 #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
66
67 #define ZYXEL_VENDOR_ID         0x0586
68 #define ZYXEL_OMNINET_ID        0x1000
69 #define BT_IGNITIONPRO_ID       0x2000  /* This one seems to be a re-branded ZyXEL device */
70
71 /* function prototypes */
72 static int  omninet_open                (struct usb_serial_port *port, struct file *filp);
73 static void omninet_close               (struct usb_serial_port *port, struct file *filp);
74 static void omninet_read_bulk_callback  (struct urb *urb, struct pt_regs *regs);
75 static void omninet_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
76 static int  omninet_write               (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
77 static int  omninet_write_room          (struct usb_serial_port *port);
78 static void omninet_shutdown            (struct usb_serial *serial);
79
80 static struct usb_device_id id_table [] = {
81         { USB_DEVICE(ZYXEL_VENDOR_ID, ZYXEL_OMNINET_ID) },
82         { USB_DEVICE(ZYXEL_VENDOR_ID, BT_IGNITIONPRO_ID) },
83         { }                                             /* Terminating entry */
84 };
85
86 MODULE_DEVICE_TABLE (usb, id_table);
87
88 static struct usb_driver omninet_driver = {
89         .owner =        THIS_MODULE,
90         .name =         "omninet",
91         .probe =        usb_serial_probe,
92         .disconnect =   usb_serial_disconnect,
93         .id_table =     id_table,
94 };
95
96
97 static struct usb_serial_device_type zyxel_omninet_device = {
98         .owner =                THIS_MODULE,
99         .name =                 "ZyXEL - omni.net lcd plus usb",
100         .short_name =           "omninet",
101         .id_table =             id_table,
102         .num_interrupt_in =     1,
103         .num_bulk_in =          1,
104         .num_bulk_out =         2,
105         .num_ports =            1,
106         .open =                 omninet_open,
107         .close =                omninet_close,
108         .write =                omninet_write,
109         .write_room =           omninet_write_room,
110         .read_bulk_callback =   omninet_read_bulk_callback,
111         .write_bulk_callback =  omninet_write_bulk_callback,
112         .shutdown =             omninet_shutdown,
113 };
114
115
116 /* The protocol.
117  *
118  * The omni.net always exchange 64 bytes of data with the host. The first
119  * four bytes are the control header, you can see it in the above structure.
120  *
121  * oh_seq is a sequence number. Don't know if/how it's used.
122  * oh_len is the length of the data bytes in the packet.
123  * oh_xxx Bit-mapped, related to handshaking and status info.
124  *      I normally set it to 0x03 in trasmitted frames.
125  *      7: Active when the TA is in a CONNECTed state.
126  *      6: unknown
127  *      5: handshaking, unknown
128  *      4: handshaking, unknown
129  *      3: unknown, usually 0
130  *      2: unknown, usually 0
131  *      1: handshaking, unknown, usually set to 1 in trasmitted frames
132  *      0: handshaking, unknown, usually set to 1 in trasmitted frames
133  * oh_pad Probably a pad byte.
134  *
135  * After the header you will find data bytes if oh_len was greater than zero.
136  *
137  */
138
139 struct omninet_header
140 {
141         __u8    oh_seq;
142         __u8    oh_len;
143         __u8    oh_xxx;
144         __u8    oh_pad;
145 };
146
147 struct omninet_data
148 {
149         __u8    od_outseq;      // Sequence number for bulk_out URBs
150 };
151
152 static int omninet_open (struct usb_serial_port *port, struct file *filp)
153 {
154         struct usb_serial       *serial;
155         struct usb_serial_port  *wport;
156         struct omninet_data     *od;
157         int                     result = 0;
158
159         if (port_paranoia_check (port, __FUNCTION__))
160                 return -ENODEV;
161
162         dbg("%s - port %d", __FUNCTION__, port->number);
163
164         serial = get_usb_serial (port, __FUNCTION__);
165         if (!serial)
166                 return -ENODEV;
167
168         od = kmalloc( sizeof(struct omninet_data), GFP_KERNEL );
169         if( !od ) {
170                 err("%s- kmalloc(%Zd) failed.", __FUNCTION__, sizeof(struct omninet_data));
171                 return -ENOMEM;
172         }
173
174         usb_set_serial_port_data(port, od);
175         wport = serial->port[1];
176         wport->tty = port->tty;
177
178         /* Start reading from the device */
179         usb_fill_bulk_urb(port->read_urb, serial->dev, 
180                       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
181                       port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
182                       omninet_read_bulk_callback, port);
183         result = usb_submit_urb(port->read_urb, GFP_KERNEL);
184         if (result)
185                 err("%s - failed submitting read urb, error %d", __FUNCTION__, result);
186
187         return result;
188 }
189
190 static void omninet_close (struct usb_serial_port *port, struct file * filp)
191 {
192         struct usb_serial       *serial;
193         struct usb_serial_port  *wport;
194         struct omninet_data     *od;
195
196         if (port_paranoia_check (port, __FUNCTION__))
197                 return;
198
199         dbg("%s - port %d", __FUNCTION__, port->number);
200
201         serial = get_usb_serial (port, __FUNCTION__);
202         if (!serial)
203                 return;
204
205         if (serial->dev) {
206                 wport = serial->port[1];
207                 usb_unlink_urb (wport->write_urb);
208                 usb_unlink_urb (port->read_urb);
209         }
210
211         od = usb_get_serial_port_data(port);
212         if (od)
213                 kfree(od);
214 }
215
216
217 #define OMNINET_DATAOFFSET      0x04
218 #define OMNINET_HEADERLEN       sizeof(struct omninet_header)
219 #define OMNINET_BULKOUTSIZE     (64 - OMNINET_HEADERLEN)
220
221 static void omninet_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
222 {
223         struct usb_serial_port  *port   = (struct usb_serial_port *)urb->context;
224         struct usb_serial       *serial = get_usb_serial (port, __FUNCTION__);
225
226         unsigned char           *data   = urb->transfer_buffer;
227         struct omninet_header   *header = (struct omninet_header *) &data[0];
228
229         int i;
230         int result;
231
232 //      dbg("omninet_read_bulk_callback");
233
234         if (!serial) {
235                 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
236                 return;
237         }
238
239         if (urb->status) {
240                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
241                 return;
242         }
243
244         if ((debug) && (header->oh_xxx != 0x30)) {
245                 if (urb->actual_length) {
246                         printk (KERN_DEBUG __FILE__ ": omninet_read %d: ", header->oh_len);
247                         for (i = 0; i < (header->oh_len + OMNINET_HEADERLEN); i++) {
248                                 printk ("%.2x ", data[i]);
249                         }
250                         printk ("\n");
251                 }
252         }
253
254         if (urb->actual_length && header->oh_len) {
255                 for (i = 0; i < header->oh_len; i++) {
256                          tty_insert_flip_char(port->tty, data[OMNINET_DATAOFFSET + i], 0);
257                 }
258                 tty_flip_buffer_push(port->tty);
259         }
260
261         /* Continue trying to always read  */
262         usb_fill_bulk_urb(urb, serial->dev, 
263                       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
264                       urb->transfer_buffer, urb->transfer_buffer_length,
265                       omninet_read_bulk_callback, port);
266         result = usb_submit_urb(urb, GFP_ATOMIC);
267         if (result)
268                 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
269
270         return;
271 }
272
273 static int omninet_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
274 {
275         struct usb_serial       *serial = port->serial;
276         struct usb_serial_port  *wport  = serial->port[1];
277
278         struct omninet_data     *od     = usb_get_serial_port_data(port);
279         struct omninet_header   *header = (struct omninet_header *) wport->write_urb->transfer_buffer;
280
281         int                     result;
282
283 //      dbg("omninet_write port %d", port->number);
284
285         if (count == 0) {
286                 dbg("%s - write request of 0 bytes", __FUNCTION__);
287                 return (0);
288         }
289         if (wport->write_urb->status == -EINPROGRESS) {
290                 dbg("%s - already writing", __FUNCTION__);
291                 return (0);
292         }
293
294         count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
295
296         if (from_user) {
297                 if (copy_from_user(wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET, buf, count) != 0) {
298                         result = -EFAULT;
299                         goto exit;
300                 }
301         }
302         else {
303                 memcpy (wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET, buf, count);
304         }
305
306         usb_serial_debug_data (__FILE__, __FUNCTION__, count, wport->write_urb->transfer_buffer);
307
308         header->oh_seq  = od->od_outseq++;
309         header->oh_len  = count;
310         header->oh_xxx  = 0x03;
311         header->oh_pad  = 0x00;
312
313         /* send the data out the bulk port, always 64 bytes */
314         wport->write_urb->transfer_buffer_length = 64;
315
316         wport->write_urb->dev = serial->dev;
317         result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
318         if (result)
319                 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
320         else
321                 result = count;
322
323 exit:   
324         return result;
325 }
326
327
328 static int omninet_write_room (struct usb_serial_port *port)
329 {
330         struct usb_serial       *serial = port->serial;
331         struct usb_serial_port  *wport  = serial->port[1];
332
333         int room = 0; // Default: no room
334
335         if (wport->write_urb->status != -EINPROGRESS)
336                 room = wport->bulk_out_size - OMNINET_HEADERLEN;
337
338 //      dbg("omninet_write_room returns %d", room);
339
340         return (room);
341 }
342
343 static void omninet_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
344 {
345 /*      struct omninet_header   *header = (struct omninet_header  *) urb->transfer_buffer; */
346         struct usb_serial_port  *port   = (struct usb_serial_port *) urb->context;
347         struct usb_serial       *serial;
348
349 //      dbg("omninet_write_bulk_callback, port %0x\n", port);
350
351
352         if (port_paranoia_check (port, __FUNCTION__)) {
353                 return;
354         }
355
356         serial = port->serial;
357         if (serial_paranoia_check (serial, __FUNCTION__)) {
358                 return;
359         }
360
361         if (urb->status) {
362                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
363                 return;
364         }
365
366         schedule_work(&port->work);
367
368 //      dbg("omninet_write_bulk_callback, tty %0x\n", tty);
369 }
370
371
372 static void omninet_shutdown (struct usb_serial *serial)
373 {
374         dbg ("%s", __FUNCTION__);
375 }
376
377
378 static int __init omninet_init (void)
379 {
380         int retval;
381         retval = usb_serial_register(&zyxel_omninet_device);
382         if (retval)
383                 goto failed_usb_serial_register;
384         retval = usb_register(&omninet_driver);
385         if (retval)
386                 goto failed_usb_register;
387         info(DRIVER_VERSION ":" DRIVER_DESC);
388         return 0;
389 failed_usb_register:
390         usb_serial_deregister(&zyxel_omninet_device);
391 failed_usb_serial_register:
392         return retval;
393 }
394
395
396 static void __exit omninet_exit (void)
397 {
398         usb_deregister (&omninet_driver);
399         usb_serial_deregister (&zyxel_omninet_device);
400 }
401
402
403 module_init(omninet_init);
404 module_exit(omninet_exit);
405
406 MODULE_AUTHOR( DRIVER_AUTHOR );
407 MODULE_DESCRIPTION( DRIVER_DESC );
408 MODULE_LICENSE("GPL");
409
410 MODULE_PARM(debug, "i");
411 MODULE_PARM_DESC(debug, "Debug enabled or not");
412