blob: ba551f00f16ff1ec30fbcd90422f1e1112e8c00a [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 Cox95da3102008-07-22 11:09:07 +0100144static int oti6858_open(struct tty_struct *tty,
145 struct usb_serial_port *port, struct file *filp);
146static void oti6858_close(struct tty_struct *tty,
147 struct usb_serial_port *port, struct file *filp);
148static void oti6858_set_termios(struct tty_struct *tty,
149 struct usb_serial_port *port, struct ktermios *old);
150static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200151 unsigned int cmd, unsigned long arg);
152static void oti6858_read_int_callback(struct urb *urb);
153static void oti6858_read_bulk_callback(struct urb *urb);
154static void oti6858_write_bulk_callback(struct urb *urb);
Alan Cox95da3102008-07-22 11:09:07 +0100155static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200156 const unsigned char *buf, int count);
Alan Cox95da3102008-07-22 11:09:07 +0100157static int oti6858_write_room(struct tty_struct *tty);
158static int oti6858_chars_in_buffer(struct tty_struct *tty);
159static int oti6858_tiocmget(struct tty_struct *tty, struct file *file);
160static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200161 unsigned int set, unsigned int clear);
162static int oti6858_startup(struct usb_serial *serial);
163static void oti6858_shutdown(struct usb_serial *serial);
164
165/* functions operating on buffers */
Alan Coxb0a239d2008-01-19 16:02:37 +0000166static struct oti6858_buf *oti6858_buf_alloc(unsigned int size);
167static void oti6858_buf_free(struct oti6858_buf *pb);
168static void oti6858_buf_clear(struct oti6858_buf *pb);
169static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb);
170static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb);
171static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200172 unsigned int count);
Alan Coxb0a239d2008-01-19 16:02:37 +0000173static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200174 unsigned int count);
175
176
177/* device info */
178static struct usb_serial_driver oti6858_device = {
179 .driver = {
180 .owner = THIS_MODULE,
181 .name = "oti6858",
182 },
183 .id_table = id_table,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200184 .num_ports = 1,
185 .open = oti6858_open,
186 .close = oti6858_close,
187 .write = oti6858_write,
188 .ioctl = oti6858_ioctl,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200189 .set_termios = oti6858_set_termios,
190 .tiocmget = oti6858_tiocmget,
191 .tiocmset = oti6858_tiocmset,
192 .read_bulk_callback = oti6858_read_bulk_callback,
193 .read_int_callback = oti6858_read_int_callback,
194 .write_bulk_callback = oti6858_write_bulk_callback,
195 .write_room = oti6858_write_room,
196 .chars_in_buffer = oti6858_chars_in_buffer,
197 .attach = oti6858_startup,
198 .shutdown = oti6858_shutdown,
199};
200
201struct oti6858_private {
202 spinlock_t lock;
203
Alan Coxb0a239d2008-01-19 16:02:37 +0000204 struct oti6858_buf *buf;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200205 struct oti6858_control_pkt status;
206
207 struct {
208 u8 read_urb_in_use;
209 u8 write_urb_in_use;
210 u8 termios_initialized;
211 } flags;
212 struct delayed_work delayed_write_work;
213
214 struct {
Al Virofd05e722008-04-28 07:00:16 +0100215 __le16 divisor;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200216 u8 frame_fmt;
217 u8 control;
218 } pending_setup;
219 u8 transient;
220 u8 setup_done;
221 struct delayed_work delayed_setup_work;
222
223 wait_queue_head_t intr_wait;
Alan Cox2a77c812008-07-22 11:15:36 +0100224 struct usb_serial_port *port; /* USB port with which associated */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200225};
226
Kees Lemmens49cdee02007-03-27 12:34:30 +0200227static void setup_line(struct work_struct *work)
228{
Alan Cox2a77c812008-07-22 11:15:36 +0100229 struct oti6858_private *priv = container_of(work,
230 struct oti6858_private, delayed_setup_work.work);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200231 struct usb_serial_port *port = priv->port;
232 struct oti6858_control_pkt *new_setup;
233 unsigned long flags;
234 int result;
235
Harvey Harrison441b62c2008-03-03 16:08:34 -0800236 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200237
Alan Cox2a77c812008-07-22 11:15:36 +0100238 new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
239 if (new_setup == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800240 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200241 /* we will try again */
Alan Cox2a77c812008-07-22 11:15:36 +0100242 schedule_delayed_work(&priv->delayed_setup_work,
243 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200244 return;
245 }
246
247 result = usb_control_msg(port->serial->dev,
248 usb_rcvctrlpipe(port->serial->dev, 0),
249 OTI6858_REQ_T_GET_STATUS,
250 OTI6858_REQ_GET_STATUS,
251 0, 0,
252 new_setup, OTI6858_CTRL_PKT_SIZE,
253 100);
254
255 if (result != OTI6858_CTRL_PKT_SIZE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800256 dev_err(&port->dev, "%s(): error reading status\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200257 kfree(new_setup);
258 /* we will try again */
Alan Cox2a77c812008-07-22 11:15:36 +0100259 schedule_delayed_work(&priv->delayed_setup_work,
260 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200261 return;
262 }
263
264 spin_lock_irqsave(&priv->lock, flags);
265 if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
266 new_setup->divisor = priv->pending_setup.divisor;
267 new_setup->control = priv->pending_setup.control;
268 new_setup->frame_fmt = priv->pending_setup.frame_fmt;
269
270 spin_unlock_irqrestore(&priv->lock, flags);
271 result = usb_control_msg(port->serial->dev,
272 usb_sndctrlpipe(port->serial->dev, 0),
273 OTI6858_REQ_T_SET_LINE,
274 OTI6858_REQ_SET_LINE,
275 0, 0,
276 new_setup, OTI6858_CTRL_PKT_SIZE,
277 100);
278 } else {
279 spin_unlock_irqrestore(&priv->lock, flags);
280 result = 0;
281 }
282 kfree(new_setup);
283
284 spin_lock_irqsave(&priv->lock, flags);
285 if (result != OTI6858_CTRL_PKT_SIZE)
286 priv->transient = 0;
287 priv->setup_done = 1;
288 spin_unlock_irqrestore(&priv->lock, flags);
289
Harvey Harrison441b62c2008-03-03 16:08:34 -0800290 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200291 port->interrupt_in_urb->dev = port->serial->dev;
292 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
293 if (result != 0) {
294 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800295 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200296 }
297}
298
299void send_data(struct work_struct *work)
300{
Alan Cox2a77c812008-07-22 11:15:36 +0100301 struct oti6858_private *priv = container_of(work,
302 struct oti6858_private, delayed_write_work.work);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200303 struct usb_serial_port *port = priv->port;
304 int count = 0, result;
305 unsigned long flags;
306 unsigned char allow;
307
Harvey Harrison441b62c2008-03-03 16:08:34 -0800308 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200309
310 spin_lock_irqsave(&priv->lock, flags);
311 if (priv->flags.write_urb_in_use) {
312 spin_unlock_irqrestore(&priv->lock, flags);
Alan Cox2a77c812008-07-22 11:15:36 +0100313 schedule_delayed_work(&priv->delayed_write_work,
314 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200315 return;
316 }
317 priv->flags.write_urb_in_use = 1;
318
Alan Coxb0a239d2008-01-19 16:02:37 +0000319 count = oti6858_buf_data_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200320 spin_unlock_irqrestore(&priv->lock, flags);
321 if (count > port->bulk_out_size)
322 count = port->bulk_out_size;
323
324 if (count != 0) {
325 result = usb_control_msg(port->serial->dev,
326 usb_rcvctrlpipe(port->serial->dev, 0),
327 OTI6858_REQ_T_CHECK_TXBUFF,
328 OTI6858_REQ_CHECK_TXBUFF,
329 count, 0, &allow, 1, 100);
330 if (result != 1 || allow != 0)
331 count = 0;
332 }
333
334 if (count == 0) {
335 priv->flags.write_urb_in_use = 0;
336
Harvey Harrison441b62c2008-03-03 16:08:34 -0800337 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200338 port->interrupt_in_urb->dev = port->serial->dev;
339 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
340 if (result != 0) {
341 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800342 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200343 }
344 return;
345 }
346
347 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000348 oti6858_buf_get(priv->buf, port->write_urb->transfer_buffer, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200349 spin_unlock_irqrestore(&priv->lock, flags);
350
351 port->write_urb->transfer_buffer_length = count;
352 port->write_urb->dev = port->serial->dev;
353 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
354 if (result != 0) {
355 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800356 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200357 priv->flags.write_urb_in_use = 0;
358 }
359
360 usb_serial_port_softint(port);
361}
362
363static int oti6858_startup(struct usb_serial *serial)
364{
Alan Cox2a77c812008-07-22 11:15:36 +0100365 struct usb_serial_port *port = serial->port[0];
366 struct oti6858_private *priv;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200367 int i;
368
369 for (i = 0; i < serial->num_ports; ++i) {
370 priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
371 if (!priv)
372 break;
Alan Coxb0a239d2008-01-19 16:02:37 +0000373 priv->buf = oti6858_buf_alloc(PL2303_BUF_SIZE);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200374 if (priv->buf == NULL) {
375 kfree(priv);
376 break;
377 }
378
379 spin_lock_init(&priv->lock);
380 init_waitqueue_head(&priv->intr_wait);
Alan Cox2a77c812008-07-22 11:15:36 +0100381/* INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
382/* INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200383 priv->port = port;
384 INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
385 INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
386
387 usb_set_serial_port_data(serial->port[i], priv);
388 }
389 if (i == serial->num_ports)
390 return 0;
391
392 for (--i; i >= 0; --i) {
393 priv = usb_get_serial_port_data(serial->port[i]);
Alan Coxb0a239d2008-01-19 16:02:37 +0000394 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200395 kfree(priv);
396 usb_set_serial_port_data(serial->port[i], NULL);
397 }
398 return -ENOMEM;
399}
400
Alan Cox95da3102008-07-22 11:09:07 +0100401static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200402 const unsigned char *buf, int count)
403{
404 struct oti6858_private *priv = usb_get_serial_port_data(port);
405 unsigned long flags;
406
Harvey Harrison441b62c2008-03-03 16:08:34 -0800407 dbg("%s(port = %d, count = %d)", __func__, port->number, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200408
409 if (!count)
410 return count;
411
412 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000413 count = oti6858_buf_put(priv->buf, buf, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200414 spin_unlock_irqrestore(&priv->lock, flags);
415
416 return count;
417}
418
Alan Cox95da3102008-07-22 11:09:07 +0100419static int oti6858_write_room(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200420{
Alan Cox95da3102008-07-22 11:09:07 +0100421 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200422 struct oti6858_private *priv = usb_get_serial_port_data(port);
423 int room = 0;
424 unsigned long flags;
425
Harvey Harrison441b62c2008-03-03 16:08:34 -0800426 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200427
428 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000429 room = oti6858_buf_space_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200430 spin_unlock_irqrestore(&priv->lock, flags);
431
432 return room;
433}
434
Alan Cox95da3102008-07-22 11:09:07 +0100435static int oti6858_chars_in_buffer(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200436{
Alan Cox95da3102008-07-22 11:09:07 +0100437 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200438 struct oti6858_private *priv = usb_get_serial_port_data(port);
439 int chars = 0;
440 unsigned long flags;
441
Harvey Harrison441b62c2008-03-03 16:08:34 -0800442 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200443
444 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000445 chars = oti6858_buf_data_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200446 spin_unlock_irqrestore(&priv->lock, flags);
447
448 return chars;
449}
450
Alan Cox95da3102008-07-22 11:09:07 +0100451static void oti6858_set_termios(struct tty_struct *tty,
452 struct usb_serial_port *port, struct ktermios *old_termios)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200453{
454 struct oti6858_private *priv = usb_get_serial_port_data(port);
455 unsigned long flags;
456 unsigned int cflag;
457 u8 frame_fmt, control;
Al Virofd05e722008-04-28 07:00:16 +0100458 __le16 divisor;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200459 int br;
460
Harvey Harrison441b62c2008-03-03 16:08:34 -0800461 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200462
Alan Cox95da3102008-07-22 11:09:07 +0100463 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800464 dbg("%s(): no tty structures", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200465 return;
466 }
467
468 spin_lock_irqsave(&priv->lock, flags);
469 if (!priv->flags.termios_initialized) {
Alan Cox95da3102008-07-22 11:09:07 +0100470 *(tty->termios) = tty_std_termios;
471 tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
472 tty->termios->c_ispeed = 38400;
473 tty->termios->c_ospeed = 38400;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200474 priv->flags.termios_initialized = 1;
475 }
476 spin_unlock_irqrestore(&priv->lock, flags);
477
Alan Cox95da3102008-07-22 11:09:07 +0100478 cflag = tty->termios->c_cflag;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200479
480 spin_lock_irqsave(&priv->lock, flags);
481 divisor = priv->pending_setup.divisor;
482 frame_fmt = priv->pending_setup.frame_fmt;
483 control = priv->pending_setup.control;
484 spin_unlock_irqrestore(&priv->lock, flags);
485
486 frame_fmt &= ~FMT_DATA_BITS_MASK;
487 switch (cflag & CSIZE) {
Alan Cox2a77c812008-07-22 11:15:36 +0100488 case CS5:
489 frame_fmt |= FMT_DATA_BITS_5;
490 break;
491 case CS6:
492 frame_fmt |= FMT_DATA_BITS_6;
493 break;
494 case CS7:
495 frame_fmt |= FMT_DATA_BITS_7;
496 break;
497 default:
498 case CS8:
499 frame_fmt |= FMT_DATA_BITS_8;
500 break;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200501 }
502
503 /* manufacturer claims that this device can work with baud rates
504 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
505 * guarantee that any other baud rate will work (especially
506 * the higher ones)
507 */
Alan Cox95da3102008-07-22 11:09:07 +0100508 br = tty_get_baud_rate(tty);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200509 if (br == 0) {
510 divisor = 0;
Alan Coxb0a239d2008-01-19 16:02:37 +0000511 } else {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200512 int real_br;
Al Virofd05e722008-04-28 07:00:16 +0100513 int new_divisor;
Alan Coxb0a239d2008-01-19 16:02:37 +0000514 br = min(br, OTI6858_MAX_BAUD_RATE);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200515
Al Virofd05e722008-04-28 07:00:16 +0100516 new_divisor = (96000000 + 8 * br) / (16 * br);
517 real_br = 96000000 / (16 * new_divisor);
518 divisor = cpu_to_le16(new_divisor);
Alan Cox95da3102008-07-22 11:09:07 +0100519 tty_encode_baud_rate(tty, real_br, real_br);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200520 }
521
522 frame_fmt &= ~FMT_STOP_BITS_MASK;
Alan Cox2a77c812008-07-22 11:15:36 +0100523 if ((cflag & CSTOPB) != 0)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200524 frame_fmt |= FMT_STOP_BITS_2;
Alan Cox2a77c812008-07-22 11:15:36 +0100525 else
Kees Lemmens49cdee02007-03-27 12:34:30 +0200526 frame_fmt |= FMT_STOP_BITS_1;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200527
528 frame_fmt &= ~FMT_PARITY_MASK;
529 if ((cflag & PARENB) != 0) {
Alan Cox2a77c812008-07-22 11:15:36 +0100530 if ((cflag & PARODD) != 0)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200531 frame_fmt |= FMT_PARITY_ODD;
Alan Cox2a77c812008-07-22 11:15:36 +0100532 else
Kees Lemmens49cdee02007-03-27 12:34:30 +0200533 frame_fmt |= FMT_PARITY_EVEN;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200534 } else {
535 frame_fmt |= FMT_PARITY_NONE;
536 }
537
538 control &= ~CONTROL_MASK;
539 if ((cflag & CRTSCTS) != 0)
540 control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
541
542 /* change control lines if we are switching to or from B0 */
543 /* FIXME:
544 spin_lock_irqsave(&priv->lock, flags);
545 control = priv->line_control;
546 if ((cflag & CBAUD) == B0)
547 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
548 else
549 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
550 if (control != priv->line_control) {
551 control = priv->line_control;
552 spin_unlock_irqrestore(&priv->lock, flags);
553 set_control_lines(serial->dev, control);
554 } else {
555 spin_unlock_irqrestore(&priv->lock, flags);
556 }
557 */
558
559 spin_lock_irqsave(&priv->lock, flags);
560 if (divisor != priv->pending_setup.divisor
561 || control != priv->pending_setup.control
562 || frame_fmt != priv->pending_setup.frame_fmt) {
563 priv->pending_setup.divisor = divisor;
564 priv->pending_setup.control = control;
565 priv->pending_setup.frame_fmt = frame_fmt;
566 }
567 spin_unlock_irqrestore(&priv->lock, flags);
568}
569
Alan Cox95da3102008-07-22 11:09:07 +0100570static int oti6858_open(struct tty_struct *tty,
571 struct usb_serial_port *port, struct file *filp)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200572{
573 struct oti6858_private *priv = usb_get_serial_port_data(port);
574 struct ktermios tmp_termios;
575 struct usb_serial *serial = port->serial;
576 struct oti6858_control_pkt *buf;
577 unsigned long flags;
578 int result;
579
Harvey Harrison441b62c2008-03-03 16:08:34 -0800580 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200581
582 usb_clear_halt(serial->dev, port->write_urb->pipe);
583 usb_clear_halt(serial->dev, port->read_urb->pipe);
584
Alan Cox95da3102008-07-22 11:09:07 +0100585 if (port->port.count != 1)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200586 return 0;
587
Alan Cox2a77c812008-07-22 11:15:36 +0100588 buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
589 if (buf == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800590 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200591 return -ENOMEM;
592 }
593
594 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
595 OTI6858_REQ_T_GET_STATUS,
596 OTI6858_REQ_GET_STATUS,
597 0, 0,
598 buf, OTI6858_CTRL_PKT_SIZE,
599 100);
600 if (result != OTI6858_CTRL_PKT_SIZE) {
601 /* assume default (after power-on reset) values */
602 buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
603 buf->frame_fmt = 0x03; /* 8N1 */
604 buf->something = 0x43;
605 buf->control = 0x4c; /* DTR, RTS */
606 buf->tx_status = 0x00;
607 buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
608 buf->rx_bytes_avail = 0x00;
609 }
610
611 spin_lock_irqsave(&priv->lock, flags);
612 memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
613 priv->pending_setup.divisor = buf->divisor;
614 priv->pending_setup.frame_fmt = buf->frame_fmt;
615 priv->pending_setup.control = buf->control;
616 spin_unlock_irqrestore(&priv->lock, flags);
617 kfree(buf);
618
Harvey Harrison441b62c2008-03-03 16:08:34 -0800619 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200620 port->interrupt_in_urb->dev = serial->dev;
621 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
622 if (result != 0) {
623 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800624 " with error %d\n", __func__, result);
Alan Cox95da3102008-07-22 11:09:07 +0100625 oti6858_close(tty, port, NULL);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200626 return -EPROTO;
627 }
628
629 /* setup termios */
Alan Cox95da3102008-07-22 11:09:07 +0100630 if (tty)
631 oti6858_set_termios(tty, port, &tmp_termios);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200632
633 return 0;
634}
635
Alan Cox95da3102008-07-22 11:09:07 +0100636static void oti6858_close(struct tty_struct *tty,
637 struct usb_serial_port *port, struct file *filp)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200638{
639 struct oti6858_private *priv = usb_get_serial_port_data(port);
640 unsigned long flags;
641 long timeout;
642 wait_queue_t wait;
643
Harvey Harrison441b62c2008-03-03 16:08:34 -0800644 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200645
646 /* wait for data to drain from the buffer */
647 spin_lock_irqsave(&priv->lock, flags);
648 timeout = 30 * HZ; /* PL2303_CLOSING_WAIT */
649 init_waitqueue_entry(&wait, current);
Alan Cox95da3102008-07-22 11:09:07 +0100650 add_wait_queue(&tty->write_wait, &wait);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800651 dbg("%s(): entering wait loop", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200652 for (;;) {
653 set_current_state(TASK_INTERRUPTIBLE);
Alan Coxb0a239d2008-01-19 16:02:37 +0000654 if (oti6858_buf_data_avail(priv->buf) == 0
Kees Lemmens49cdee02007-03-27 12:34:30 +0200655 || timeout == 0 || signal_pending(current)
Oliver Neukum0915f492008-01-23 12:28:45 +0100656 || port->serial->disconnected)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200657 break;
658 spin_unlock_irqrestore(&priv->lock, flags);
659 timeout = schedule_timeout(timeout);
660 spin_lock_irqsave(&priv->lock, flags);
661 }
662 set_current_state(TASK_RUNNING);
Alan Cox95da3102008-07-22 11:09:07 +0100663 remove_wait_queue(&tty->write_wait, &wait);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800664 dbg("%s(): after wait loop", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200665
666 /* clear out any remaining data in the buffer */
Alan Coxb0a239d2008-01-19 16:02:37 +0000667 oti6858_buf_clear(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200668 spin_unlock_irqrestore(&priv->lock, flags);
669
670 /* wait for characters to drain from the device */
671 /* (this is long enough for the entire 256 byte */
672 /* pl2303 hardware buffer to drain with no flow */
673 /* control for data rates of 1200 bps or more, */
674 /* for lower rates we should really know how much */
675 /* data is in the buffer to compute a delay */
676 /* that is not unnecessarily long) */
677 /* FIXME
Alan Cox95da3102008-07-22 11:09:07 +0100678 bps = tty_get_baud_rate(tty);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200679 if (bps > 1200)
680 timeout = max((HZ*2560)/bps,HZ/10);
681 else
682 */
683 timeout = 2*HZ;
684 schedule_timeout_interruptible(timeout);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800685 dbg("%s(): after schedule_timeout_interruptible()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200686
687 /* cancel scheduled setup */
688 cancel_delayed_work(&priv->delayed_setup_work);
689 cancel_delayed_work(&priv->delayed_write_work);
690 flush_scheduled_work();
691
692 /* shutdown our urbs */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800693 dbg("%s(): shutting down urbs", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200694 usb_kill_urb(port->write_urb);
695 usb_kill_urb(port->read_urb);
696 usb_kill_urb(port->interrupt_in_urb);
697
698 /*
Alan Cox95da3102008-07-22 11:09:07 +0100699 if (tty && (tty->termios->c_cflag) & HUPCL) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200700 // drop DTR and RTS
701 spin_lock_irqsave(&priv->lock, flags);
702 priv->pending_setup.control &= ~CONTROL_MASK;
703 spin_unlock_irqrestore(&priv->lock, flags);
704 }
705 */
706}
707
Alan Cox95da3102008-07-22 11:09:07 +0100708static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200709 unsigned int set, unsigned int clear)
710{
Alan Cox95da3102008-07-22 11:09:07 +0100711 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200712 struct oti6858_private *priv = usb_get_serial_port_data(port);
713 unsigned long flags;
714 u8 control;
715
716 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800717 __func__, port->number, set, clear);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200718
719 if (!usb_get_intfdata(port->serial->interface))
720 return -ENODEV;
721
722 /* FIXME: check if this is correct (active high/low) */
723 spin_lock_irqsave(&priv->lock, flags);
724 control = priv->pending_setup.control;
725 if ((set & TIOCM_RTS) != 0)
726 control |= CONTROL_RTS_HIGH;
727 if ((set & TIOCM_DTR) != 0)
728 control |= CONTROL_DTR_HIGH;
729 if ((clear & TIOCM_RTS) != 0)
730 control &= ~CONTROL_RTS_HIGH;
731 if ((clear & TIOCM_DTR) != 0)
732 control &= ~CONTROL_DTR_HIGH;
733
Alan Cox2a77c812008-07-22 11:15:36 +0100734 if (control != priv->pending_setup.control)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200735 priv->pending_setup.control = control;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200736
Alan Cox2a77c812008-07-22 11:15:36 +0100737 spin_unlock_irqrestore(&priv->lock, flags);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200738 return 0;
739}
740
Alan Cox95da3102008-07-22 11:09:07 +0100741static int oti6858_tiocmget(struct tty_struct *tty, struct file *file)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200742{
Alan Cox95da3102008-07-22 11:09:07 +0100743 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200744 struct oti6858_private *priv = usb_get_serial_port_data(port);
745 unsigned long flags;
746 unsigned pin_state;
747 unsigned result = 0;
748
Harvey Harrison441b62c2008-03-03 16:08:34 -0800749 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200750
751 if (!usb_get_intfdata(port->serial->interface))
752 return -ENODEV;
753
754 spin_lock_irqsave(&priv->lock, flags);
755 pin_state = priv->status.pin_state & PIN_MASK;
756 spin_unlock_irqrestore(&priv->lock, flags);
757
758 /* FIXME: check if this is correct (active high/low) */
759 if ((pin_state & PIN_RTS) != 0)
760 result |= TIOCM_RTS;
761 if ((pin_state & PIN_CTS) != 0)
762 result |= TIOCM_CTS;
763 if ((pin_state & PIN_DSR) != 0)
764 result |= TIOCM_DSR;
765 if ((pin_state & PIN_DTR) != 0)
766 result |= TIOCM_DTR;
767 if ((pin_state & PIN_RI) != 0)
768 result |= TIOCM_RI;
769 if ((pin_state & PIN_DCD) != 0)
770 result |= TIOCM_CD;
771
Harvey Harrison441b62c2008-03-03 16:08:34 -0800772 dbg("%s() = 0x%08x", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200773
774 return result;
775}
776
777static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
778{
779 struct oti6858_private *priv = usb_get_serial_port_data(port);
780 unsigned long flags;
781 unsigned int prev, status;
782 unsigned int changed;
783
784 spin_lock_irqsave(&priv->lock, flags);
785 prev = priv->status.pin_state;
786 spin_unlock_irqrestore(&priv->lock, flags);
787
788 while (1) {
Alan Cox2a77c812008-07-22 11:15:36 +0100789 wait_event_interruptible(priv->intr_wait,
790 priv->status.pin_state != prev);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200791 if (signal_pending(current))
792 return -ERESTARTSYS;
793
794 spin_lock_irqsave(&priv->lock, flags);
795 status = priv->status.pin_state & PIN_MASK;
796 spin_unlock_irqrestore(&priv->lock, flags);
797
798 changed = prev ^ status;
799 /* FIXME: check if this is correct (active high/low) */
Alan Cox2a77c812008-07-22 11:15:36 +0100800 if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
801 ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
802 ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
803 ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
804 return 0;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200805 prev = status;
806 }
807
808 /* NOTREACHED */
809 return 0;
810}
811
Alan Cox95da3102008-07-22 11:09:07 +0100812static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200813 unsigned int cmd, unsigned long arg)
814{
Alan Cox95da3102008-07-22 11:09:07 +0100815 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200816
817 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800818 __func__, port->number, cmd, arg);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200819
820 switch (cmd) {
Alan Cox2a77c812008-07-22 11:15:36 +0100821 case TIOCMIWAIT:
822 dbg("%s(): TIOCMIWAIT", __func__);
823 return wait_modem_info(port, arg);
824 default:
825 dbg("%s(): 0x%04x not supported", __func__, cmd);
826 break;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200827 }
Kees Lemmens49cdee02007-03-27 12:34:30 +0200828 return -ENOIOCTLCMD;
829}
830
Kees Lemmens49cdee02007-03-27 12:34:30 +0200831
832static void oti6858_shutdown(struct usb_serial *serial)
833{
834 struct oti6858_private *priv;
835 int i;
836
Harvey Harrison441b62c2008-03-03 16:08:34 -0800837 dbg("%s()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200838
839 for (i = 0; i < serial->num_ports; ++i) {
840 priv = usb_get_serial_port_data(serial->port[i]);
841 if (priv) {
Alan Coxb0a239d2008-01-19 16:02:37 +0000842 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200843 kfree(priv);
844 usb_set_serial_port_data(serial->port[i], NULL);
845 }
846 }
847}
848
849static void oti6858_read_int_callback(struct urb *urb)
850{
Ming Leicdc97792008-02-24 18:41:47 +0800851 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200852 struct oti6858_private *priv = usb_get_serial_port_data(port);
853 int transient = 0, can_recv = 0, resubmit = 1;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700854 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200855
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700856 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800857 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200858
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700859 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200860 case 0:
861 /* success */
862 break;
863 case -ECONNRESET:
864 case -ENOENT:
865 case -ESHUTDOWN:
866 /* this urb is terminated, clean up */
867 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800868 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200869 return;
870 default:
871 dbg("%s(): nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800872 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200873 break;
874 }
875
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700876 if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200877 struct oti6858_control_pkt *xs = urb->transfer_buffer;
878 unsigned long flags;
879
880 spin_lock_irqsave(&priv->lock, flags);
881
882 if (!priv->transient) {
883 if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
884 if (xs->rx_bytes_avail == 0) {
885 priv->transient = 4;
886 priv->setup_done = 0;
887 resubmit = 0;
888 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800889 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200890 schedule_delayed_work(&priv->delayed_setup_work, 0);
891 }
892 }
893 } else {
894 if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
895 priv->transient = 0;
896 } else if (!priv->setup_done) {
897 resubmit = 0;
898 } else if (--priv->transient == 0) {
899 if (xs->rx_bytes_avail == 0) {
900 priv->transient = 4;
901 priv->setup_done = 0;
902 resubmit = 0;
903 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800904 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200905 schedule_delayed_work(&priv->delayed_setup_work, 0);
906 }
907 }
908 }
909
910 if (!priv->transient) {
911 if (xs->pin_state != priv->status.pin_state)
912 wake_up_interruptible(&priv->intr_wait);
913 memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
914 }
915
916 if (!priv->transient && xs->rx_bytes_avail != 0) {
917 can_recv = xs->rx_bytes_avail;
918 priv->flags.read_urb_in_use = 1;
919 }
920
921 transient = priv->transient;
922 spin_unlock_irqrestore(&priv->lock, flags);
923 }
924
925 if (can_recv) {
926 int result;
927
928 port->read_urb->dev = port->serial->dev;
929 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
930 if (result != 0) {
931 priv->flags.read_urb_in_use = 0;
932 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800933 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200934 } else {
935 resubmit = 0;
936 }
937 } else if (!transient) {
938 unsigned long flags;
939
940 spin_lock_irqsave(&priv->lock, flags);
941 if (priv->flags.write_urb_in_use == 0
Alan Coxb0a239d2008-01-19 16:02:37 +0000942 && oti6858_buf_data_avail(priv->buf) != 0) {
Alan Cox2a77c812008-07-22 11:15:36 +0100943 schedule_delayed_work(&priv->delayed_write_work, 0);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200944 resubmit = 0;
945 }
946 spin_unlock_irqrestore(&priv->lock, flags);
947 }
948
949 if (resubmit) {
950 int result;
951
Alan Cox2a77c812008-07-22 11:15:36 +0100952/* dbg("%s(): submitting interrupt urb", __func__); */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200953 urb->dev = port->serial->dev;
954 result = usb_submit_urb(urb, GFP_ATOMIC);
955 if (result != 0) {
956 dev_err(&urb->dev->dev,
957 "%s(): usb_submit_urb() failed with"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800958 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200959 }
960 }
961}
962
963static void oti6858_read_bulk_callback(struct urb *urb)
964{
Ming Leicdc97792008-02-24 18:41:47 +0800965 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200966 struct oti6858_private *priv = usb_get_serial_port_data(port);
967 struct tty_struct *tty;
968 unsigned char *data = urb->transfer_buffer;
969 unsigned long flags;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700970 int status = urb->status;
Alan Coxb0a239d2008-01-19 16:02:37 +0000971 int result;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200972
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700973 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800974 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200975
976 spin_lock_irqsave(&priv->lock, flags);
977 priv->flags.read_urb_in_use = 0;
978 spin_unlock_irqrestore(&priv->lock, flags);
979
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700980 if (status != 0) {
Alan Cox95da3102008-07-22 11:09:07 +0100981 if (!port->port.count) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800982 dbg("%s(): port is closed, exiting", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200983 return;
984 }
985 /*
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700986 if (status == -EPROTO) {
Alan Cox2a77c812008-07-22 11:15:36 +0100987 * PL2303 mysteriously fails with -EPROTO reschedule
988 the read *
989 dbg("%s - caught -EPROTO, resubmitting the urb",
990 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200991 result = usb_submit_urb(urb, GFP_ATOMIC);
992 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800993 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200994 return;
995 }
996 */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800997 dbg("%s(): unable to handle the error, exiting", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200998 return;
999 }
1000
Alan Cox4a90f092008-10-13 10:39:46 +01001001 tty = tty_port_tty_get(&port->port);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001002 if (tty != NULL && urb->actual_length > 0) {
Alan Coxb0a239d2008-01-19 16:02:37 +00001003 tty_insert_flip_string(tty, data, urb->actual_length);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001004 tty_flip_buffer_push(tty);
1005 }
Alan Cox4a90f092008-10-13 10:39:46 +01001006 tty_kref_put(tty);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001007
Alan Cox2a77c812008-07-22 11:15:36 +01001008 /* schedule the interrupt urb if we are still open */
Alan Cox95da3102008-07-22 11:09:07 +01001009 if (port->port.count != 0) {
Kees Lemmens49cdee02007-03-27 12:34:30 +02001010 port->interrupt_in_urb->dev = port->serial->dev;
1011 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1012 if (result != 0) {
1013 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001014 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001015 }
1016 }
1017}
1018
1019static void oti6858_write_bulk_callback(struct urb *urb)
1020{
Ming Leicdc97792008-02-24 18:41:47 +08001021 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001022 struct oti6858_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -07001023 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001024 int result;
1025
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -07001026 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001027 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001028
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -07001029 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +02001030 case 0:
1031 /* success */
1032 break;
1033 case -ECONNRESET:
1034 case -ENOENT:
1035 case -ESHUTDOWN:
1036 /* this urb is terminated, clean up */
1037 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001038 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001039 priv->flags.write_urb_in_use = 0;
1040 return;
1041 default:
1042 /* error in the urb, so we have to resubmit it */
1043 dbg("%s(): nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001044 __func__, status);
1045 dbg("%s(): overflow in write", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001046
1047 port->write_urb->transfer_buffer_length = 1;
1048 port->write_urb->dev = port->serial->dev;
1049 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1050 if (result) {
1051 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001052 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001053 } else {
1054 return;
1055 }
1056 }
1057
1058 priv->flags.write_urb_in_use = 0;
1059
Alan Cox2a77c812008-07-22 11:15:36 +01001060 /* schedule the interrupt urb if we are still open */
Kees Lemmens49cdee02007-03-27 12:34:30 +02001061 port->interrupt_in_urb->dev = port->serial->dev;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001062 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001063 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1064 if (result != 0) {
1065 dev_err(&port->dev, "%s(): failed submitting int urb,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001066 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001067 }
1068}
1069
1070
1071/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001072 * oti6858_buf_alloc
Kees Lemmens49cdee02007-03-27 12:34:30 +02001073 *
1074 * Allocate a circular buffer and all associated memory.
1075 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001076static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001077{
Alan Coxb0a239d2008-01-19 16:02:37 +00001078 struct oti6858_buf *pb;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001079
1080 if (size == 0)
1081 return NULL;
1082
Alan Coxb0a239d2008-01-19 16:02:37 +00001083 pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001084 if (pb == NULL)
1085 return NULL;
1086
1087 pb->buf_buf = kmalloc(size, GFP_KERNEL);
1088 if (pb->buf_buf == NULL) {
1089 kfree(pb);
1090 return NULL;
1091 }
1092
1093 pb->buf_size = size;
1094 pb->buf_get = pb->buf_put = pb->buf_buf;
1095
1096 return pb;
1097}
1098
1099/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001100 * oti6858_buf_free
Kees Lemmens49cdee02007-03-27 12:34:30 +02001101 *
1102 * Free the buffer and all associated memory.
1103 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001104static void oti6858_buf_free(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001105{
1106 if (pb) {
1107 kfree(pb->buf_buf);
1108 kfree(pb);
1109 }
1110}
1111
1112/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001113 * oti6858_buf_clear
Kees Lemmens49cdee02007-03-27 12:34:30 +02001114 *
1115 * Clear out all data in the circular buffer.
1116 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001117static void oti6858_buf_clear(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001118{
1119 if (pb != NULL) {
1120 /* equivalent to a get of all data available */
1121 pb->buf_get = pb->buf_put;
1122 }
1123}
1124
1125/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001126 * oti6858_buf_data_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001127 *
1128 * Return the number of bytes of data available in the circular
1129 * buffer.
1130 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001131static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001132{
1133 if (pb == NULL)
1134 return 0;
Alan Cox2a77c812008-07-22 11:15:36 +01001135 return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001136}
1137
1138/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001139 * oti6858_buf_space_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001140 *
1141 * Return the number of bytes of space available in the circular
1142 * buffer.
1143 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001144static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001145{
1146 if (pb == NULL)
1147 return 0;
Alan Cox2a77c812008-07-22 11:15:36 +01001148 return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001149}
1150
1151/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001152 * oti6858_buf_put
Kees Lemmens49cdee02007-03-27 12:34:30 +02001153 *
1154 * Copy data data from a user buffer and put it into the circular buffer.
1155 * Restrict to the amount of space available.
1156 *
1157 * Return the number of bytes copied.
1158 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001159static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001160 unsigned int count)
1161{
1162 unsigned int len;
1163
1164 if (pb == NULL)
1165 return 0;
1166
Alan Coxb0a239d2008-01-19 16:02:37 +00001167 len = oti6858_buf_space_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001168 if (count > len)
1169 count = len;
1170
1171 if (count == 0)
1172 return 0;
1173
1174 len = pb->buf_buf + pb->buf_size - pb->buf_put;
1175 if (count > len) {
1176 memcpy(pb->buf_put, buf, len);
1177 memcpy(pb->buf_buf, buf+len, count - len);
1178 pb->buf_put = pb->buf_buf + count - len;
1179 } else {
1180 memcpy(pb->buf_put, buf, count);
1181 if (count < len)
1182 pb->buf_put += count;
1183 else /* count == len */
1184 pb->buf_put = pb->buf_buf;
1185 }
1186
1187 return count;
1188}
1189
1190/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001191 * oti6858_buf_get
Kees Lemmens49cdee02007-03-27 12:34:30 +02001192 *
1193 * Get data from the circular buffer and copy to the given buffer.
1194 * Restrict to the amount of data available.
1195 *
1196 * Return the number of bytes copied.
1197 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001198static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001199 unsigned int count)
1200{
1201 unsigned int len;
1202
1203 if (pb == NULL)
1204 return 0;
1205
Alan Coxb0a239d2008-01-19 16:02:37 +00001206 len = oti6858_buf_data_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001207 if (count > len)
1208 count = len;
1209
1210 if (count == 0)
1211 return 0;
1212
1213 len = pb->buf_buf + pb->buf_size - pb->buf_get;
1214 if (count > len) {
1215 memcpy(buf, pb->buf_get, len);
1216 memcpy(buf+len, pb->buf_buf, count - len);
1217 pb->buf_get = pb->buf_buf + count - len;
1218 } else {
1219 memcpy(buf, pb->buf_get, count);
1220 if (count < len)
1221 pb->buf_get += count;
1222 else /* count == len */
1223 pb->buf_get = pb->buf_buf;
1224 }
1225
1226 return count;
1227}
1228
1229/* module description and (de)initialization */
1230
1231static int __init oti6858_init(void)
1232{
1233 int retval;
1234
Alan Cox2a77c812008-07-22 11:15:36 +01001235 retval = usb_serial_register(&oti6858_device);
1236 if (retval == 0) {
1237 retval = usb_register(&oti6858_driver);
1238 if (retval)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001239 usb_serial_deregister(&oti6858_device);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001240 }
Kees Lemmens49cdee02007-03-27 12:34:30 +02001241 return retval;
1242}
1243
1244static void __exit oti6858_exit(void)
1245{
1246 usb_deregister(&oti6858_driver);
1247 usb_serial_deregister(&oti6858_device);
1248}
1249
1250module_init(oti6858_init);
1251module_exit(oti6858_exit);
1252
1253MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
1254MODULE_AUTHOR(OTI6858_AUTHOR);
1255MODULE_VERSION(OTI6858_VERSION);
1256MODULE_LICENSE("GPL");
1257
1258module_param(debug, bool, S_IRUGO | S_IWUSR);
1259MODULE_PARM_DESC(debug, "enable debug output");
1260