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