Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / usb / serial / omninet.c
blob27c9d06f82117a9665526c41e21b9144acf12d9b
1 /*
2 * USB ZyXEL omni.net LCD PLUS driver
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.
8 * See Documentation/usb/usb-serial.txt for more information on using this
9 * driver
11 * Please report both successes and troubles to the author at omninet@kroah.com
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>
26 static bool debug;
29 * Version Information
31 #define DRIVER_VERSION "v1.1"
32 #define DRIVER_AUTHOR "Alessandro Zummo"
33 #define DRIVER_DESC "USB ZyXEL omni.net LCD PLUS Driver"
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
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 int omninet_port_probe(struct usb_serial_port *port);
50 static int omninet_port_remove(struct usb_serial_port *port);
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 */
57 MODULE_DEVICE_TABLE(usb, id_table);
59 static struct usb_serial_driver zyxel_omninet_device = {
60 .driver = {
61 .owner = THIS_MODULE,
62 .name = "omninet",
64 .description = "ZyXEL - omni.net lcd plus usb",
65 .id_table = id_table,
66 .num_ports = 1,
67 .port_probe = omninet_port_probe,
68 .port_remove = omninet_port_remove,
69 .open = omninet_open,
70 .close = omninet_close,
71 .write = omninet_write,
72 .write_room = omninet_write_room,
73 .read_bulk_callback = omninet_read_bulk_callback,
74 .write_bulk_callback = omninet_write_bulk_callback,
75 .disconnect = omninet_disconnect,
78 static struct usb_serial_driver * const serial_drivers[] = {
79 &zyxel_omninet_device, NULL
83 /* The protocol.
85 * The omni.net always exchange 64 bytes of data with the host. The first
86 * four bytes are the control header, you can see it in the above structure.
88 * oh_seq is a sequence number. Don't know if/how it's used.
89 * oh_len is the length of the data bytes in the packet.
90 * oh_xxx Bit-mapped, related to handshaking and status info.
91 * I normally set it to 0x03 in trasmitted frames.
92 * 7: Active when the TA is in a CONNECTed state.
93 * 6: unknown
94 * 5: handshaking, unknown
95 * 4: handshaking, unknown
96 * 3: unknown, usually 0
97 * 2: unknown, usually 0
98 * 1: handshaking, unknown, usually set to 1 in trasmitted frames
99 * 0: handshaking, unknown, usually set to 1 in trasmitted frames
100 * oh_pad Probably a pad byte.
102 * After the header you will find data bytes if oh_len was greater than zero.
106 struct omninet_header {
107 __u8 oh_seq;
108 __u8 oh_len;
109 __u8 oh_xxx;
110 __u8 oh_pad;
113 struct omninet_data {
114 __u8 od_outseq; /* Sequence number for bulk_out URBs */
117 static int omninet_port_probe(struct usb_serial_port *port)
119 struct omninet_data *od;
121 od = kmalloc(sizeof(struct omninet_data), GFP_KERNEL);
122 if (!od)
123 return -ENOMEM;
125 usb_set_serial_port_data(port, od);
127 return 0;
130 static int omninet_port_remove(struct usb_serial_port *port)
132 struct omninet_data *od;
134 od = usb_get_serial_port_data(port);
135 kfree(od);
137 return 0;
140 static int omninet_open(struct tty_struct *tty, struct usb_serial_port *port)
142 struct usb_serial *serial = port->serial;
143 struct usb_serial_port *wport;
144 int result = 0;
146 wport = serial->port[1];
147 tty_port_tty_set(&wport->port, tty);
149 /* Start reading from the device */
150 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
151 if (result)
152 dev_err(&port->dev,
153 "%s - failed submitting read urb, error %d\n",
154 __func__, result);
155 return result;
158 static void omninet_close(struct usb_serial_port *port)
160 usb_kill_urb(port->read_urb);
164 #define OMNINET_DATAOFFSET 0x04
165 #define OMNINET_HEADERLEN sizeof(struct omninet_header)
166 #define OMNINET_BULKOUTSIZE (64 - OMNINET_HEADERLEN)
168 static void omninet_read_bulk_callback(struct urb *urb)
170 struct usb_serial_port *port = urb->context;
171 unsigned char *data = urb->transfer_buffer;
172 struct omninet_header *header = (struct omninet_header *) &data[0];
173 int status = urb->status;
174 int result;
175 int i;
177 if (status) {
178 dbg("%s - nonzero read bulk status received: %d",
179 __func__, status);
180 return;
183 if (debug && header->oh_xxx != 0x30) {
184 if (urb->actual_length) {
185 printk(KERN_DEBUG "%s: omninet_read %d: ",
186 __FILE__, header->oh_len);
187 for (i = 0; i < (header->oh_len +
188 OMNINET_HEADERLEN); i++)
189 printk("%.2x ", data[i]);
190 printk("\n");
194 if (urb->actual_length && header->oh_len) {
195 struct tty_struct *tty = tty_port_tty_get(&port->port);
196 tty_insert_flip_string(tty, data + OMNINET_DATAOFFSET,
197 header->oh_len);
198 tty_flip_buffer_push(tty);
199 tty_kref_put(tty);
202 /* Continue trying to always read */
203 result = usb_submit_urb(urb, GFP_ATOMIC);
204 if (result)
205 dev_err(&port->dev,
206 "%s - failed resubmitting read urb, error %d\n",
207 __func__, result);
210 static int omninet_write(struct tty_struct *tty, struct usb_serial_port *port,
211 const unsigned char *buf, int count)
213 struct usb_serial *serial = port->serial;
214 struct usb_serial_port *wport = serial->port[1];
216 struct omninet_data *od = usb_get_serial_port_data(port);
217 struct omninet_header *header = (struct omninet_header *)
218 wport->write_urb->transfer_buffer;
220 int result;
222 if (count == 0) {
223 dbg("%s - write request of 0 bytes", __func__);
224 return 0;
227 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
228 dbg("%s - already writing", __func__);
229 return 0;
232 count = (count > OMNINET_BULKOUTSIZE) ? OMNINET_BULKOUTSIZE : count;
234 memcpy(wport->write_urb->transfer_buffer + OMNINET_DATAOFFSET,
235 buf, count);
237 usb_serial_debug_data(debug, &port->dev, __func__, count,
238 wport->write_urb->transfer_buffer);
240 header->oh_seq = od->od_outseq++;
241 header->oh_len = count;
242 header->oh_xxx = 0x03;
243 header->oh_pad = 0x00;
245 /* send the data out the bulk port, always 64 bytes */
246 wport->write_urb->transfer_buffer_length = 64;
248 result = usb_submit_urb(wport->write_urb, GFP_ATOMIC);
249 if (result) {
250 set_bit(0, &wport->write_urbs_free);
251 dev_err_console(port,
252 "%s - failed submitting write urb, error %d\n",
253 __func__, result);
254 } else
255 result = count;
257 return result;
261 static int omninet_write_room(struct tty_struct *tty)
263 struct usb_serial_port *port = tty->driver_data;
264 struct usb_serial *serial = port->serial;
265 struct usb_serial_port *wport = serial->port[1];
267 int room = 0; /* Default: no room */
269 if (test_bit(0, &wport->write_urbs_free))
270 room = wport->bulk_out_size - OMNINET_HEADERLEN;
272 dbg("%s - returns %d", __func__, room);
274 return room;
277 static void omninet_write_bulk_callback(struct urb *urb)
279 /* struct omninet_header *header = (struct omninet_header *)
280 urb->transfer_buffer; */
281 struct usb_serial_port *port = urb->context;
282 int status = urb->status;
284 set_bit(0, &port->write_urbs_free);
285 if (status) {
286 dbg("%s - nonzero write bulk status received: %d",
287 __func__, status);
288 return;
291 usb_serial_port_softint(port);
295 static void omninet_disconnect(struct usb_serial *serial)
297 struct usb_serial_port *wport = serial->port[1];
299 usb_kill_urb(wport->write_urb);
302 module_usb_serial_driver(serial_drivers, id_table);
304 MODULE_AUTHOR(DRIVER_AUTHOR);
305 MODULE_DESCRIPTION(DRIVER_DESC);
306 MODULE_LICENSE("GPL");
308 module_param(debug, bool, S_IRUGO | S_IWUSR);
309 MODULE_PARM_DESC(debug, "Debug enabled or not");