blob: e90f88a3b04061bad536f1941a7b48a1d7c4c17c [file] [log] [blame]
Kees Lemmens49cdee02007-03-27 12:34:30 +02001/*
2 * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
3 *
4 * Copyleft (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
5 * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
6 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2003 IBM Corp.
8 *
9 * Many thanks to the authors of pl2303 driver: all functions in this file
10 * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
11 *
12 * Warning! You use this driver on your own risk! The only official
13 * description of this device I have is datasheet from manufacturer,
14 * and it doesn't contain almost any information needed to write a driver.
15 * Almost all knowlegde used while writing this driver was gathered by:
16 * - analyzing traffic between device and the M$ Windows 2000 driver,
17 * - trying different bit combinations and checking pin states
18 * with a voltmeter,
19 * - receiving malformed frames and producing buffer overflows
20 * to learn how errors are reported,
21 * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
22 * CONNECTED TO IT!
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License.
27 *
Alan Cox2a77c812008-07-22 11:15:36 +010028 * See Documentation/usb/usb-serial.txt for more information on using this
29 * driver
Kees Lemmens49cdee02007-03-27 12:34:30 +020030 *
31 * TODO:
32 * - implement correct flushing for ioctls and oti6858_close()
33 * - check how errors (rx overflow, parity error, framing error) are reported
34 * - implement oti6858_break_ctl()
35 * - implement more ioctls
36 * - test/implement flow control
37 * - allow setting custom baud rates
38 */
39
40#include <linux/kernel.h>
41#include <linux/errno.h>
42#include <linux/init.h>
43#include <linux/slab.h>
44#include <linux/tty.h>
45#include <linux/tty_driver.h>
46#include <linux/tty_flip.h>
47#include <linux/serial.h>
48#include <linux/module.h>
49#include <linux/moduleparam.h>
50#include <linux/spinlock.h>
51#include <linux/usb.h>
52#include <linux/usb/serial.h>
Alan Cox2a77c812008-07-22 11:15:36 +010053#include <linux/uaccess.h>
Kees Lemmens49cdee02007-03-27 12:34:30 +020054#include "oti6858.h"
55
56#define OTI6858_DESCRIPTION \
57 "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
58#define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
59#define OTI6858_VERSION "0.1"
60
61static struct usb_device_id id_table [] = {
62 { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
63 { }
64};
65
66MODULE_DEVICE_TABLE(usb, id_table);
67
68static struct usb_driver oti6858_driver = {
69 .name = "oti6858",
70 .probe = usb_serial_probe,
71 .disconnect = usb_serial_disconnect,
72 .id_table = id_table,
73 .no_dynamic_id = 1,
74};
75
76static int debug;
77
78
79/* buffering code, copied from pl2303 driver */
80#define PL2303_BUF_SIZE 1024
81#define PL2303_TMP_BUF_SIZE 1024
82
Alan Coxb0a239d2008-01-19 16:02:37 +000083struct oti6858_buf {
Kees Lemmens49cdee02007-03-27 12:34:30 +020084 unsigned int buf_size;
85 char *buf_buf;
86 char *buf_get;
87 char *buf_put;
88};
89
90/* requests */
91#define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
92#define OTI6858_REQ_T_GET_STATUS 0x01
93
94#define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
95#define OTI6858_REQ_T_SET_LINE 0x00
96
97#define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
98#define OTI6858_REQ_T_CHECK_TXBUFF 0x00
99
100/* format of the control packet */
101struct oti6858_control_pkt {
Al Virofd05e722008-04-28 07:00:16 +0100102 __le16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200103#define OTI6858_MAX_BAUD_RATE 3000000
104 u8 frame_fmt;
105#define FMT_STOP_BITS_MASK 0xc0
106#define FMT_STOP_BITS_1 0x00
107#define FMT_STOP_BITS_2 0x40 /* 1.5 stop bits if FMT_DATA_BITS_5 */
108#define FMT_PARITY_MASK 0x38
109#define FMT_PARITY_NONE 0x00
110#define FMT_PARITY_ODD 0x08
111#define FMT_PARITY_EVEN 0x18
112#define FMT_PARITY_MARK 0x28
113#define FMT_PARITY_SPACE 0x38
114#define FMT_DATA_BITS_MASK 0x03
115#define FMT_DATA_BITS_5 0x00
116#define FMT_DATA_BITS_6 0x01
117#define FMT_DATA_BITS_7 0x02
118#define FMT_DATA_BITS_8 0x03
119 u8 something; /* always equals 0x43 */
120 u8 control; /* settings of flow control lines */
121#define CONTROL_MASK 0x0c
122#define CONTROL_DTR_HIGH 0x08
123#define CONTROL_RTS_HIGH 0x04
124 u8 tx_status;
125#define TX_BUFFER_EMPTIED 0x09
126 u8 pin_state;
127#define PIN_MASK 0x3f
128#define PIN_RTS 0x20 /* output pin */
129#define PIN_CTS 0x10 /* input pin, active low */
130#define PIN_DSR 0x08 /* input pin, active low */
131#define PIN_DTR 0x04 /* output pin */
132#define PIN_RI 0x02 /* input pin, active low */
133#define PIN_DCD 0x01 /* input pin, active low */
134 u8 rx_bytes_avail; /* number of bytes in rx buffer */;
135};
136
137#define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt)
138#define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
Alan Cox2a77c812008-07-22 11:15:36 +0100139 (((a)->divisor == (priv)->pending_setup.divisor) \
Kees Lemmens49cdee02007-03-27 12:34:30 +0200140 && ((a)->control == (priv)->pending_setup.control) \
Alan Cox2a77c812008-07-22 11:15:36 +0100141 && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
Kees Lemmens49cdee02007-03-27 12:34:30 +0200142
143/* function prototypes */
Alan Coxa509a7e2009-09-19 13:13:26 -0700144static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100145static void oti6858_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +0100146static void oti6858_set_termios(struct tty_struct *tty,
147 struct usb_serial_port *port, struct ktermios *old);
148static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200149 unsigned int cmd, unsigned long arg);
150static void oti6858_read_int_callback(struct urb *urb);
151static void oti6858_read_bulk_callback(struct urb *urb);
152static void oti6858_write_bulk_callback(struct urb *urb);
Alan Cox95da3102008-07-22 11:09:07 +0100153static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200154 const unsigned char *buf, int count);
Alan Cox95da3102008-07-22 11:09:07 +0100155static int oti6858_write_room(struct tty_struct *tty);
156static int oti6858_chars_in_buffer(struct tty_struct *tty);
157static int oti6858_tiocmget(struct tty_struct *tty, struct file *file);
158static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200159 unsigned int set, unsigned int clear);
160static int oti6858_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400161static void oti6858_release(struct usb_serial *serial);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200162
163/* functions operating on buffers */
Alan Coxb0a239d2008-01-19 16:02:37 +0000164static struct oti6858_buf *oti6858_buf_alloc(unsigned int size);
165static void oti6858_buf_free(struct oti6858_buf *pb);
166static void oti6858_buf_clear(struct oti6858_buf *pb);
167static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb);
168static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb);
169static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200170 unsigned int count);
Alan Coxb0a239d2008-01-19 16:02:37 +0000171static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200172 unsigned int count);
173
174
175/* device info */
176static struct usb_serial_driver oti6858_device = {
177 .driver = {
178 .owner = THIS_MODULE,
179 .name = "oti6858",
180 },
181 .id_table = id_table,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200182 .num_ports = 1,
183 .open = oti6858_open,
184 .close = oti6858_close,
185 .write = oti6858_write,
186 .ioctl = oti6858_ioctl,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200187 .set_termios = oti6858_set_termios,
188 .tiocmget = oti6858_tiocmget,
189 .tiocmset = oti6858_tiocmset,
190 .read_bulk_callback = oti6858_read_bulk_callback,
191 .read_int_callback = oti6858_read_int_callback,
192 .write_bulk_callback = oti6858_write_bulk_callback,
193 .write_room = oti6858_write_room,
194 .chars_in_buffer = oti6858_chars_in_buffer,
195 .attach = oti6858_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400196 .release = oti6858_release,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200197};
198
199struct oti6858_private {
200 spinlock_t lock;
201
Alan Coxb0a239d2008-01-19 16:02:37 +0000202 struct oti6858_buf *buf;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200203 struct oti6858_control_pkt status;
204
205 struct {
206 u8 read_urb_in_use;
207 u8 write_urb_in_use;
208 u8 termios_initialized;
209 } flags;
210 struct delayed_work delayed_write_work;
211
212 struct {
Al Virofd05e722008-04-28 07:00:16 +0100213 __le16 divisor;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200214 u8 frame_fmt;
215 u8 control;
216 } pending_setup;
217 u8 transient;
218 u8 setup_done;
219 struct delayed_work delayed_setup_work;
220
221 wait_queue_head_t intr_wait;
Alan Cox2a77c812008-07-22 11:15:36 +0100222 struct usb_serial_port *port; /* USB port with which associated */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200223};
224
Kees Lemmens49cdee02007-03-27 12:34:30 +0200225static void setup_line(struct work_struct *work)
226{
Alan Cox2a77c812008-07-22 11:15:36 +0100227 struct oti6858_private *priv = container_of(work,
228 struct oti6858_private, delayed_setup_work.work);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200229 struct usb_serial_port *port = priv->port;
230 struct oti6858_control_pkt *new_setup;
231 unsigned long flags;
232 int result;
233
Harvey Harrison441b62c2008-03-03 16:08:34 -0800234 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200235
Alan Cox2a77c812008-07-22 11:15:36 +0100236 new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
237 if (new_setup == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800238 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200239 /* we will try again */
Alan Cox2a77c812008-07-22 11:15:36 +0100240 schedule_delayed_work(&priv->delayed_setup_work,
241 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200242 return;
243 }
244
245 result = usb_control_msg(port->serial->dev,
246 usb_rcvctrlpipe(port->serial->dev, 0),
247 OTI6858_REQ_T_GET_STATUS,
248 OTI6858_REQ_GET_STATUS,
249 0, 0,
250 new_setup, OTI6858_CTRL_PKT_SIZE,
251 100);
252
253 if (result != OTI6858_CTRL_PKT_SIZE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800254 dev_err(&port->dev, "%s(): error reading status\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200255 kfree(new_setup);
256 /* we will try again */
Alan Cox2a77c812008-07-22 11:15:36 +0100257 schedule_delayed_work(&priv->delayed_setup_work,
258 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200259 return;
260 }
261
262 spin_lock_irqsave(&priv->lock, flags);
263 if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
264 new_setup->divisor = priv->pending_setup.divisor;
265 new_setup->control = priv->pending_setup.control;
266 new_setup->frame_fmt = priv->pending_setup.frame_fmt;
267
268 spin_unlock_irqrestore(&priv->lock, flags);
269 result = usb_control_msg(port->serial->dev,
270 usb_sndctrlpipe(port->serial->dev, 0),
271 OTI6858_REQ_T_SET_LINE,
272 OTI6858_REQ_SET_LINE,
273 0, 0,
274 new_setup, OTI6858_CTRL_PKT_SIZE,
275 100);
276 } else {
277 spin_unlock_irqrestore(&priv->lock, flags);
278 result = 0;
279 }
280 kfree(new_setup);
281
282 spin_lock_irqsave(&priv->lock, flags);
283 if (result != OTI6858_CTRL_PKT_SIZE)
284 priv->transient = 0;
285 priv->setup_done = 1;
286 spin_unlock_irqrestore(&priv->lock, flags);
287
Harvey Harrison441b62c2008-03-03 16:08:34 -0800288 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200289 port->interrupt_in_urb->dev = port->serial->dev;
290 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
291 if (result != 0) {
292 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800293 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200294 }
295}
296
297void send_data(struct work_struct *work)
298{
Alan Cox2a77c812008-07-22 11:15:36 +0100299 struct oti6858_private *priv = container_of(work,
300 struct oti6858_private, delayed_write_work.work);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200301 struct usb_serial_port *port = priv->port;
302 int count = 0, result;
303 unsigned long flags;
304 unsigned char allow;
305
Harvey Harrison441b62c2008-03-03 16:08:34 -0800306 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200307
308 spin_lock_irqsave(&priv->lock, flags);
309 if (priv->flags.write_urb_in_use) {
310 spin_unlock_irqrestore(&priv->lock, flags);
Alan Cox2a77c812008-07-22 11:15:36 +0100311 schedule_delayed_work(&priv->delayed_write_work,
312 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200313 return;
314 }
315 priv->flags.write_urb_in_use = 1;
316
Alan Coxb0a239d2008-01-19 16:02:37 +0000317 count = oti6858_buf_data_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200318 spin_unlock_irqrestore(&priv->lock, flags);
319 if (count > port->bulk_out_size)
320 count = port->bulk_out_size;
321
322 if (count != 0) {
323 result = usb_control_msg(port->serial->dev,
324 usb_rcvctrlpipe(port->serial->dev, 0),
325 OTI6858_REQ_T_CHECK_TXBUFF,
326 OTI6858_REQ_CHECK_TXBUFF,
327 count, 0, &allow, 1, 100);
328 if (result != 1 || allow != 0)
329 count = 0;
330 }
331
332 if (count == 0) {
333 priv->flags.write_urb_in_use = 0;
334
Harvey Harrison441b62c2008-03-03 16:08:34 -0800335 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200336 port->interrupt_in_urb->dev = port->serial->dev;
337 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
338 if (result != 0) {
339 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800340 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200341 }
342 return;
343 }
344
345 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000346 oti6858_buf_get(priv->buf, port->write_urb->transfer_buffer, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200347 spin_unlock_irqrestore(&priv->lock, flags);
348
349 port->write_urb->transfer_buffer_length = count;
350 port->write_urb->dev = port->serial->dev;
351 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
352 if (result != 0) {
353 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800354 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200355 priv->flags.write_urb_in_use = 0;
356 }
357
358 usb_serial_port_softint(port);
359}
360
361static int oti6858_startup(struct usb_serial *serial)
362{
Alan Cox2a77c812008-07-22 11:15:36 +0100363 struct usb_serial_port *port = serial->port[0];
364 struct oti6858_private *priv;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200365 int i;
366
367 for (i = 0; i < serial->num_ports; ++i) {
368 priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
369 if (!priv)
370 break;
Alan Coxb0a239d2008-01-19 16:02:37 +0000371 priv->buf = oti6858_buf_alloc(PL2303_BUF_SIZE);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200372 if (priv->buf == NULL) {
373 kfree(priv);
374 break;
375 }
376
377 spin_lock_init(&priv->lock);
378 init_waitqueue_head(&priv->intr_wait);
Alan Cox2a77c812008-07-22 11:15:36 +0100379/* INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
380/* INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200381 priv->port = port;
382 INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
383 INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
384
385 usb_set_serial_port_data(serial->port[i], priv);
386 }
387 if (i == serial->num_ports)
388 return 0;
389
390 for (--i; i >= 0; --i) {
391 priv = usb_get_serial_port_data(serial->port[i]);
Alan Coxb0a239d2008-01-19 16:02:37 +0000392 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200393 kfree(priv);
394 usb_set_serial_port_data(serial->port[i], NULL);
395 }
396 return -ENOMEM;
397}
398
Alan Cox95da3102008-07-22 11:09:07 +0100399static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200400 const unsigned char *buf, int count)
401{
402 struct oti6858_private *priv = usb_get_serial_port_data(port);
403 unsigned long flags;
404
Harvey Harrison441b62c2008-03-03 16:08:34 -0800405 dbg("%s(port = %d, count = %d)", __func__, port->number, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200406
407 if (!count)
408 return count;
409
410 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000411 count = oti6858_buf_put(priv->buf, buf, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200412 spin_unlock_irqrestore(&priv->lock, flags);
413
414 return count;
415}
416
Alan Cox95da3102008-07-22 11:09:07 +0100417static int oti6858_write_room(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200418{
Alan Cox95da3102008-07-22 11:09:07 +0100419 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200420 struct oti6858_private *priv = usb_get_serial_port_data(port);
421 int room = 0;
422 unsigned long flags;
423
Harvey Harrison441b62c2008-03-03 16:08:34 -0800424 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200425
426 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000427 room = oti6858_buf_space_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200428 spin_unlock_irqrestore(&priv->lock, flags);
429
430 return room;
431}
432
Alan Cox95da3102008-07-22 11:09:07 +0100433static int oti6858_chars_in_buffer(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200434{
Alan Cox95da3102008-07-22 11:09:07 +0100435 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200436 struct oti6858_private *priv = usb_get_serial_port_data(port);
437 int chars = 0;
438 unsigned long flags;
439
Harvey Harrison441b62c2008-03-03 16:08:34 -0800440 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200441
442 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000443 chars = oti6858_buf_data_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200444 spin_unlock_irqrestore(&priv->lock, flags);
445
446 return chars;
447}
448
Alan Cox95da3102008-07-22 11:09:07 +0100449static void oti6858_set_termios(struct tty_struct *tty,
450 struct usb_serial_port *port, struct ktermios *old_termios)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200451{
452 struct oti6858_private *priv = usb_get_serial_port_data(port);
453 unsigned long flags;
454 unsigned int cflag;
455 u8 frame_fmt, control;
Al Virofd05e722008-04-28 07:00:16 +0100456 __le16 divisor;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200457 int br;
458
Harvey Harrison441b62c2008-03-03 16:08:34 -0800459 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200460
Alan Cox95da3102008-07-22 11:09:07 +0100461 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800462 dbg("%s(): no tty structures", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200463 return;
464 }
465
466 spin_lock_irqsave(&priv->lock, flags);
467 if (!priv->flags.termios_initialized) {
Alan Cox95da3102008-07-22 11:09:07 +0100468 *(tty->termios) = tty_std_termios;
469 tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
470 tty->termios->c_ispeed = 38400;
471 tty->termios->c_ospeed = 38400;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200472 priv->flags.termios_initialized = 1;
473 }
474 spin_unlock_irqrestore(&priv->lock, flags);
475
Alan Cox95da3102008-07-22 11:09:07 +0100476 cflag = tty->termios->c_cflag;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200477
478 spin_lock_irqsave(&priv->lock, flags);
479 divisor = priv->pending_setup.divisor;
480 frame_fmt = priv->pending_setup.frame_fmt;
481 control = priv->pending_setup.control;
482 spin_unlock_irqrestore(&priv->lock, flags);
483
484 frame_fmt &= ~FMT_DATA_BITS_MASK;
485 switch (cflag & CSIZE) {
Alan Cox2a77c812008-07-22 11:15:36 +0100486 case CS5:
487 frame_fmt |= FMT_DATA_BITS_5;
488 break;
489 case CS6:
490 frame_fmt |= FMT_DATA_BITS_6;
491 break;
492 case CS7:
493 frame_fmt |= FMT_DATA_BITS_7;
494 break;
495 default:
496 case CS8:
497 frame_fmt |= FMT_DATA_BITS_8;
498 break;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200499 }
500
501 /* manufacturer claims that this device can work with baud rates
502 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
503 * guarantee that any other baud rate will work (especially
504 * the higher ones)
505 */
Alan Cox95da3102008-07-22 11:09:07 +0100506 br = tty_get_baud_rate(tty);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200507 if (br == 0) {
508 divisor = 0;
Alan Coxb0a239d2008-01-19 16:02:37 +0000509 } else {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200510 int real_br;
Al Virofd05e722008-04-28 07:00:16 +0100511 int new_divisor;
Alan Coxb0a239d2008-01-19 16:02:37 +0000512 br = min(br, OTI6858_MAX_BAUD_RATE);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200513
Al Virofd05e722008-04-28 07:00:16 +0100514 new_divisor = (96000000 + 8 * br) / (16 * br);
515 real_br = 96000000 / (16 * new_divisor);
516 divisor = cpu_to_le16(new_divisor);
Alan Cox95da3102008-07-22 11:09:07 +0100517 tty_encode_baud_rate(tty, real_br, real_br);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200518 }
519
520 frame_fmt &= ~FMT_STOP_BITS_MASK;
Alan Cox2a77c812008-07-22 11:15:36 +0100521 if ((cflag & CSTOPB) != 0)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200522 frame_fmt |= FMT_STOP_BITS_2;
Alan Cox2a77c812008-07-22 11:15:36 +0100523 else
Kees Lemmens49cdee02007-03-27 12:34:30 +0200524 frame_fmt |= FMT_STOP_BITS_1;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200525
526 frame_fmt &= ~FMT_PARITY_MASK;
527 if ((cflag & PARENB) != 0) {
Alan Cox2a77c812008-07-22 11:15:36 +0100528 if ((cflag & PARODD) != 0)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200529 frame_fmt |= FMT_PARITY_ODD;
Alan Cox2a77c812008-07-22 11:15:36 +0100530 else
Kees Lemmens49cdee02007-03-27 12:34:30 +0200531 frame_fmt |= FMT_PARITY_EVEN;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200532 } else {
533 frame_fmt |= FMT_PARITY_NONE;
534 }
535
536 control &= ~CONTROL_MASK;
537 if ((cflag & CRTSCTS) != 0)
538 control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
539
540 /* change control lines if we are switching to or from B0 */
541 /* FIXME:
542 spin_lock_irqsave(&priv->lock, flags);
543 control = priv->line_control;
544 if ((cflag & CBAUD) == B0)
545 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
546 else
547 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
548 if (control != priv->line_control) {
549 control = priv->line_control;
550 spin_unlock_irqrestore(&priv->lock, flags);
551 set_control_lines(serial->dev, control);
552 } else {
553 spin_unlock_irqrestore(&priv->lock, flags);
554 }
555 */
556
557 spin_lock_irqsave(&priv->lock, flags);
558 if (divisor != priv->pending_setup.divisor
559 || control != priv->pending_setup.control
560 || frame_fmt != priv->pending_setup.frame_fmt) {
561 priv->pending_setup.divisor = divisor;
562 priv->pending_setup.control = control;
563 priv->pending_setup.frame_fmt = frame_fmt;
564 }
565 spin_unlock_irqrestore(&priv->lock, flags);
566}
567
Alan Coxa509a7e2009-09-19 13:13:26 -0700568static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200569{
570 struct oti6858_private *priv = usb_get_serial_port_data(port);
571 struct ktermios tmp_termios;
572 struct usb_serial *serial = port->serial;
573 struct oti6858_control_pkt *buf;
574 unsigned long flags;
575 int result;
576
Harvey Harrison441b62c2008-03-03 16:08:34 -0800577 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200578
579 usb_clear_halt(serial->dev, port->write_urb->pipe);
580 usb_clear_halt(serial->dev, port->read_urb->pipe);
581
Alan Cox95da3102008-07-22 11:09:07 +0100582 if (port->port.count != 1)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200583 return 0;
584
Alan Cox2a77c812008-07-22 11:15:36 +0100585 buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
586 if (buf == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800587 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200588 return -ENOMEM;
589 }
590
591 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
592 OTI6858_REQ_T_GET_STATUS,
593 OTI6858_REQ_GET_STATUS,
594 0, 0,
595 buf, OTI6858_CTRL_PKT_SIZE,
596 100);
597 if (result != OTI6858_CTRL_PKT_SIZE) {
598 /* assume default (after power-on reset) values */
599 buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
600 buf->frame_fmt = 0x03; /* 8N1 */
601 buf->something = 0x43;
602 buf->control = 0x4c; /* DTR, RTS */
603 buf->tx_status = 0x00;
604 buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
605 buf->rx_bytes_avail = 0x00;
606 }
607
608 spin_lock_irqsave(&priv->lock, flags);
609 memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
610 priv->pending_setup.divisor = buf->divisor;
611 priv->pending_setup.frame_fmt = buf->frame_fmt;
612 priv->pending_setup.control = buf->control;
613 spin_unlock_irqrestore(&priv->lock, flags);
614 kfree(buf);
615
Harvey Harrison441b62c2008-03-03 16:08:34 -0800616 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200617 port->interrupt_in_urb->dev = serial->dev;
618 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
619 if (result != 0) {
620 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800621 " with error %d\n", __func__, result);
Alan Cox335f8512009-06-11 12:26:29 +0100622 oti6858_close(port);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200623 return -EPROTO;
624 }
625
626 /* setup termios */
Alan Cox95da3102008-07-22 11:09:07 +0100627 if (tty)
628 oti6858_set_termios(tty, port, &tmp_termios);
Alan Cox335f8512009-06-11 12:26:29 +0100629 port->port.drain_delay = 256; /* FIXME: check the FIFO length */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200630 return 0;
631}
632
Alan Cox335f8512009-06-11 12:26:29 +0100633static void oti6858_close(struct usb_serial_port *port)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200634{
635 struct oti6858_private *priv = usb_get_serial_port_data(port);
636 unsigned long flags;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200637
Harvey Harrison441b62c2008-03-03 16:08:34 -0800638 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200639
Kees Lemmens49cdee02007-03-27 12:34:30 +0200640 spin_lock_irqsave(&priv->lock, flags);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200641 /* clear out any remaining data in the buffer */
Alan Coxb0a239d2008-01-19 16:02:37 +0000642 oti6858_buf_clear(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200643 spin_unlock_irqrestore(&priv->lock, flags);
644
Alan Cox335f8512009-06-11 12:26:29 +0100645 dbg("%s(): after buf_clear()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200646
647 /* cancel scheduled setup */
648 cancel_delayed_work(&priv->delayed_setup_work);
649 cancel_delayed_work(&priv->delayed_write_work);
650 flush_scheduled_work();
651
652 /* shutdown our urbs */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800653 dbg("%s(): shutting down urbs", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200654 usb_kill_urb(port->write_urb);
655 usb_kill_urb(port->read_urb);
656 usb_kill_urb(port->interrupt_in_urb);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200657}
658
Alan Cox95da3102008-07-22 11:09:07 +0100659static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200660 unsigned int set, unsigned int clear)
661{
Alan Cox95da3102008-07-22 11:09:07 +0100662 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200663 struct oti6858_private *priv = usb_get_serial_port_data(port);
664 unsigned long flags;
665 u8 control;
666
667 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800668 __func__, port->number, set, clear);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200669
670 if (!usb_get_intfdata(port->serial->interface))
671 return -ENODEV;
672
673 /* FIXME: check if this is correct (active high/low) */
674 spin_lock_irqsave(&priv->lock, flags);
675 control = priv->pending_setup.control;
676 if ((set & TIOCM_RTS) != 0)
677 control |= CONTROL_RTS_HIGH;
678 if ((set & TIOCM_DTR) != 0)
679 control |= CONTROL_DTR_HIGH;
680 if ((clear & TIOCM_RTS) != 0)
681 control &= ~CONTROL_RTS_HIGH;
682 if ((clear & TIOCM_DTR) != 0)
683 control &= ~CONTROL_DTR_HIGH;
684
Alan Cox2a77c812008-07-22 11:15:36 +0100685 if (control != priv->pending_setup.control)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200686 priv->pending_setup.control = control;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200687
Alan Cox2a77c812008-07-22 11:15:36 +0100688 spin_unlock_irqrestore(&priv->lock, flags);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200689 return 0;
690}
691
Alan Cox95da3102008-07-22 11:09:07 +0100692static int oti6858_tiocmget(struct tty_struct *tty, struct file *file)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200693{
Alan Cox95da3102008-07-22 11:09:07 +0100694 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200695 struct oti6858_private *priv = usb_get_serial_port_data(port);
696 unsigned long flags;
697 unsigned pin_state;
698 unsigned result = 0;
699
Harvey Harrison441b62c2008-03-03 16:08:34 -0800700 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200701
702 if (!usb_get_intfdata(port->serial->interface))
703 return -ENODEV;
704
705 spin_lock_irqsave(&priv->lock, flags);
706 pin_state = priv->status.pin_state & PIN_MASK;
707 spin_unlock_irqrestore(&priv->lock, flags);
708
709 /* FIXME: check if this is correct (active high/low) */
710 if ((pin_state & PIN_RTS) != 0)
711 result |= TIOCM_RTS;
712 if ((pin_state & PIN_CTS) != 0)
713 result |= TIOCM_CTS;
714 if ((pin_state & PIN_DSR) != 0)
715 result |= TIOCM_DSR;
716 if ((pin_state & PIN_DTR) != 0)
717 result |= TIOCM_DTR;
718 if ((pin_state & PIN_RI) != 0)
719 result |= TIOCM_RI;
720 if ((pin_state & PIN_DCD) != 0)
721 result |= TIOCM_CD;
722
Harvey Harrison441b62c2008-03-03 16:08:34 -0800723 dbg("%s() = 0x%08x", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200724
725 return result;
726}
727
728static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
729{
730 struct oti6858_private *priv = usb_get_serial_port_data(port);
731 unsigned long flags;
732 unsigned int prev, status;
733 unsigned int changed;
734
735 spin_lock_irqsave(&priv->lock, flags);
736 prev = priv->status.pin_state;
737 spin_unlock_irqrestore(&priv->lock, flags);
738
739 while (1) {
Alan Cox2a77c812008-07-22 11:15:36 +0100740 wait_event_interruptible(priv->intr_wait,
741 priv->status.pin_state != prev);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200742 if (signal_pending(current))
743 return -ERESTARTSYS;
744
745 spin_lock_irqsave(&priv->lock, flags);
746 status = priv->status.pin_state & PIN_MASK;
747 spin_unlock_irqrestore(&priv->lock, flags);
748
749 changed = prev ^ status;
750 /* FIXME: check if this is correct (active high/low) */
Alan Cox2a77c812008-07-22 11:15:36 +0100751 if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
752 ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
753 ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
754 ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
755 return 0;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200756 prev = status;
757 }
758
759 /* NOTREACHED */
760 return 0;
761}
762
Alan Cox95da3102008-07-22 11:09:07 +0100763static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200764 unsigned int cmd, unsigned long arg)
765{
Alan Cox95da3102008-07-22 11:09:07 +0100766 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200767
768 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800769 __func__, port->number, cmd, arg);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200770
771 switch (cmd) {
Alan Cox2a77c812008-07-22 11:15:36 +0100772 case TIOCMIWAIT:
773 dbg("%s(): TIOCMIWAIT", __func__);
774 return wait_modem_info(port, arg);
775 default:
776 dbg("%s(): 0x%04x not supported", __func__, cmd);
777 break;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200778 }
Kees Lemmens49cdee02007-03-27 12:34:30 +0200779 return -ENOIOCTLCMD;
780}
781
Kees Lemmens49cdee02007-03-27 12:34:30 +0200782
Alan Sternf9c99bb2009-06-02 11:53:55 -0400783static void oti6858_release(struct usb_serial *serial)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200784{
785 struct oti6858_private *priv;
786 int i;
787
Harvey Harrison441b62c2008-03-03 16:08:34 -0800788 dbg("%s()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200789
790 for (i = 0; i < serial->num_ports; ++i) {
791 priv = usb_get_serial_port_data(serial->port[i]);
792 if (priv) {
Alan Coxb0a239d2008-01-19 16:02:37 +0000793 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200794 kfree(priv);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200795 }
796 }
797}
798
799static void oti6858_read_int_callback(struct urb *urb)
800{
Ming Leicdc97792008-02-24 18:41:47 +0800801 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200802 struct oti6858_private *priv = usb_get_serial_port_data(port);
803 int transient = 0, can_recv = 0, resubmit = 1;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700804 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200805
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700806 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800807 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200808
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700809 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200810 case 0:
811 /* success */
812 break;
813 case -ECONNRESET:
814 case -ENOENT:
815 case -ESHUTDOWN:
816 /* this urb is terminated, clean up */
817 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800818 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200819 return;
820 default:
821 dbg("%s(): nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800822 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200823 break;
824 }
825
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700826 if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200827 struct oti6858_control_pkt *xs = urb->transfer_buffer;
828 unsigned long flags;
829
830 spin_lock_irqsave(&priv->lock, flags);
831
832 if (!priv->transient) {
833 if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
834 if (xs->rx_bytes_avail == 0) {
835 priv->transient = 4;
836 priv->setup_done = 0;
837 resubmit = 0;
838 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800839 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200840 schedule_delayed_work(&priv->delayed_setup_work, 0);
841 }
842 }
843 } else {
844 if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
845 priv->transient = 0;
846 } else if (!priv->setup_done) {
847 resubmit = 0;
848 } else if (--priv->transient == 0) {
849 if (xs->rx_bytes_avail == 0) {
850 priv->transient = 4;
851 priv->setup_done = 0;
852 resubmit = 0;
853 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800854 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200855 schedule_delayed_work(&priv->delayed_setup_work, 0);
856 }
857 }
858 }
859
860 if (!priv->transient) {
861 if (xs->pin_state != priv->status.pin_state)
862 wake_up_interruptible(&priv->intr_wait);
863 memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
864 }
865
866 if (!priv->transient && xs->rx_bytes_avail != 0) {
867 can_recv = xs->rx_bytes_avail;
868 priv->flags.read_urb_in_use = 1;
869 }
870
871 transient = priv->transient;
872 spin_unlock_irqrestore(&priv->lock, flags);
873 }
874
875 if (can_recv) {
876 int result;
877
878 port->read_urb->dev = port->serial->dev;
879 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
880 if (result != 0) {
881 priv->flags.read_urb_in_use = 0;
882 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800883 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200884 } else {
885 resubmit = 0;
886 }
887 } else if (!transient) {
888 unsigned long flags;
889
890 spin_lock_irqsave(&priv->lock, flags);
891 if (priv->flags.write_urb_in_use == 0
Alan Coxb0a239d2008-01-19 16:02:37 +0000892 && oti6858_buf_data_avail(priv->buf) != 0) {
Alan Cox2a77c812008-07-22 11:15:36 +0100893 schedule_delayed_work(&priv->delayed_write_work, 0);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200894 resubmit = 0;
895 }
896 spin_unlock_irqrestore(&priv->lock, flags);
897 }
898
899 if (resubmit) {
900 int result;
901
Alan Cox2a77c812008-07-22 11:15:36 +0100902/* dbg("%s(): submitting interrupt urb", __func__); */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200903 urb->dev = port->serial->dev;
904 result = usb_submit_urb(urb, GFP_ATOMIC);
905 if (result != 0) {
906 dev_err(&urb->dev->dev,
907 "%s(): usb_submit_urb() failed with"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800908 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200909 }
910 }
911}
912
913static void oti6858_read_bulk_callback(struct urb *urb)
914{
Ming Leicdc97792008-02-24 18:41:47 +0800915 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200916 struct oti6858_private *priv = usb_get_serial_port_data(port);
917 struct tty_struct *tty;
918 unsigned char *data = urb->transfer_buffer;
919 unsigned long flags;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700920 int status = urb->status;
Alan Coxb0a239d2008-01-19 16:02:37 +0000921 int result;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200922
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700923 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800924 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200925
926 spin_lock_irqsave(&priv->lock, flags);
927 priv->flags.read_urb_in_use = 0;
928 spin_unlock_irqrestore(&priv->lock, flags);
929
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700930 if (status != 0) {
Alan Cox95da3102008-07-22 11:09:07 +0100931 if (!port->port.count) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800932 dbg("%s(): port is closed, exiting", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200933 return;
934 }
935 /*
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700936 if (status == -EPROTO) {
Alan Cox2a77c812008-07-22 11:15:36 +0100937 * PL2303 mysteriously fails with -EPROTO reschedule
938 the read *
939 dbg("%s - caught -EPROTO, resubmitting the urb",
940 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200941 result = usb_submit_urb(urb, GFP_ATOMIC);
942 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800943 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200944 return;
945 }
946 */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800947 dbg("%s(): unable to handle the error, exiting", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200948 return;
949 }
950
Alan Cox4a90f092008-10-13 10:39:46 +0100951 tty = tty_port_tty_get(&port->port);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200952 if (tty != NULL && urb->actual_length > 0) {
Alan Coxb0a239d2008-01-19 16:02:37 +0000953 tty_insert_flip_string(tty, data, urb->actual_length);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200954 tty_flip_buffer_push(tty);
955 }
Alan Cox4a90f092008-10-13 10:39:46 +0100956 tty_kref_put(tty);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200957
Alan Cox2a77c812008-07-22 11:15:36 +0100958 /* schedule the interrupt urb if we are still open */
Alan Cox95da3102008-07-22 11:09:07 +0100959 if (port->port.count != 0) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200960 port->interrupt_in_urb->dev = port->serial->dev;
961 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
962 if (result != 0) {
963 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800964 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200965 }
966 }
967}
968
969static void oti6858_write_bulk_callback(struct urb *urb)
970{
Ming Leicdc97792008-02-24 18:41:47 +0800971 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200972 struct oti6858_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700973 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200974 int result;
975
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700976 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800977 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200978
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700979 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200980 case 0:
981 /* success */
982 break;
983 case -ECONNRESET:
984 case -ENOENT:
985 case -ESHUTDOWN:
986 /* this urb is terminated, clean up */
987 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800988 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200989 priv->flags.write_urb_in_use = 0;
990 return;
991 default:
992 /* error in the urb, so we have to resubmit it */
993 dbg("%s(): nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800994 __func__, status);
995 dbg("%s(): overflow in write", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200996
997 port->write_urb->transfer_buffer_length = 1;
998 port->write_urb->dev = port->serial->dev;
999 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1000 if (result) {
1001 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001002 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001003 } else {
1004 return;
1005 }
1006 }
1007
1008 priv->flags.write_urb_in_use = 0;
1009
Alan Cox2a77c812008-07-22 11:15:36 +01001010 /* schedule the interrupt urb if we are still open */
Kees Lemmens49cdee02007-03-27 12:34:30 +02001011 port->interrupt_in_urb->dev = port->serial->dev;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001012 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001013 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1014 if (result != 0) {
1015 dev_err(&port->dev, "%s(): failed submitting int urb,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001016 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001017 }
1018}
1019
1020
1021/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001022 * oti6858_buf_alloc
Kees Lemmens49cdee02007-03-27 12:34:30 +02001023 *
1024 * Allocate a circular buffer and all associated memory.
1025 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001026static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001027{
Alan Coxb0a239d2008-01-19 16:02:37 +00001028 struct oti6858_buf *pb;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001029
1030 if (size == 0)
1031 return NULL;
1032
Alan Coxb0a239d2008-01-19 16:02:37 +00001033 pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001034 if (pb == NULL)
1035 return NULL;
1036
1037 pb->buf_buf = kmalloc(size, GFP_KERNEL);
1038 if (pb->buf_buf == NULL) {
1039 kfree(pb);
1040 return NULL;
1041 }
1042
1043 pb->buf_size = size;
1044 pb->buf_get = pb->buf_put = pb->buf_buf;
1045
1046 return pb;
1047}
1048
1049/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001050 * oti6858_buf_free
Kees Lemmens49cdee02007-03-27 12:34:30 +02001051 *
1052 * Free the buffer and all associated memory.
1053 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001054static void oti6858_buf_free(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001055{
1056 if (pb) {
1057 kfree(pb->buf_buf);
1058 kfree(pb);
1059 }
1060}
1061
1062/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001063 * oti6858_buf_clear
Kees Lemmens49cdee02007-03-27 12:34:30 +02001064 *
1065 * Clear out all data in the circular buffer.
1066 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001067static void oti6858_buf_clear(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001068{
1069 if (pb != NULL) {
1070 /* equivalent to a get of all data available */
1071 pb->buf_get = pb->buf_put;
1072 }
1073}
1074
1075/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001076 * oti6858_buf_data_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001077 *
1078 * Return the number of bytes of data available in the circular
1079 * buffer.
1080 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001081static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001082{
1083 if (pb == NULL)
1084 return 0;
Alan Cox2a77c812008-07-22 11:15:36 +01001085 return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001086}
1087
1088/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001089 * oti6858_buf_space_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001090 *
1091 * Return the number of bytes of space available in the circular
1092 * buffer.
1093 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001094static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001095{
1096 if (pb == NULL)
1097 return 0;
Alan Cox2a77c812008-07-22 11:15:36 +01001098 return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001099}
1100
1101/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001102 * oti6858_buf_put
Kees Lemmens49cdee02007-03-27 12:34:30 +02001103 *
1104 * Copy data data from a user buffer and put it into the circular buffer.
1105 * Restrict to the amount of space available.
1106 *
1107 * Return the number of bytes copied.
1108 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001109static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001110 unsigned int count)
1111{
1112 unsigned int len;
1113
1114 if (pb == NULL)
1115 return 0;
1116
Alan Coxb0a239d2008-01-19 16:02:37 +00001117 len = oti6858_buf_space_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001118 if (count > len)
1119 count = len;
1120
1121 if (count == 0)
1122 return 0;
1123
1124 len = pb->buf_buf + pb->buf_size - pb->buf_put;
1125 if (count > len) {
1126 memcpy(pb->buf_put, buf, len);
1127 memcpy(pb->buf_buf, buf+len, count - len);
1128 pb->buf_put = pb->buf_buf + count - len;
1129 } else {
1130 memcpy(pb->buf_put, buf, count);
1131 if (count < len)
1132 pb->buf_put += count;
1133 else /* count == len */
1134 pb->buf_put = pb->buf_buf;
1135 }
1136
1137 return count;
1138}
1139
1140/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001141 * oti6858_buf_get
Kees Lemmens49cdee02007-03-27 12:34:30 +02001142 *
1143 * Get data from the circular buffer and copy to the given buffer.
1144 * Restrict to the amount of data available.
1145 *
1146 * Return the number of bytes copied.
1147 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001148static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001149 unsigned int count)
1150{
1151 unsigned int len;
1152
1153 if (pb == NULL)
1154 return 0;
1155
Alan Coxb0a239d2008-01-19 16:02:37 +00001156 len = oti6858_buf_data_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001157 if (count > len)
1158 count = len;
1159
1160 if (count == 0)
1161 return 0;
1162
1163 len = pb->buf_buf + pb->buf_size - pb->buf_get;
1164 if (count > len) {
1165 memcpy(buf, pb->buf_get, len);
1166 memcpy(buf+len, pb->buf_buf, count - len);
1167 pb->buf_get = pb->buf_buf + count - len;
1168 } else {
1169 memcpy(buf, pb->buf_get, count);
1170 if (count < len)
1171 pb->buf_get += count;
1172 else /* count == len */
1173 pb->buf_get = pb->buf_buf;
1174 }
1175
1176 return count;
1177}
1178
1179/* module description and (de)initialization */
1180
1181static int __init oti6858_init(void)
1182{
1183 int retval;
1184
Alan Cox2a77c812008-07-22 11:15:36 +01001185 retval = usb_serial_register(&oti6858_device);
1186 if (retval == 0) {
1187 retval = usb_register(&oti6858_driver);
1188 if (retval)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001189 usb_serial_deregister(&oti6858_device);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001190 }
Kees Lemmens49cdee02007-03-27 12:34:30 +02001191 return retval;
1192}
1193
1194static void __exit oti6858_exit(void)
1195{
1196 usb_deregister(&oti6858_driver);
1197 usb_serial_deregister(&oti6858_device);
1198}
1199
1200module_init(oti6858_init);
1201module_exit(oti6858_exit);
1202
1203MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
1204MODULE_AUTHOR(OTI6858_AUTHOR);
1205MODULE_VERSION(OTI6858_VERSION);
1206MODULE_LICENSE("GPL");
1207
1208module_param(debug, bool, S_IRUGO | S_IWUSR);
1209MODULE_PARM_DESC(debug, "enable debug output");
1210