blob: 81db5715ee25ef5d0769494a11318c4cf866e0ad [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
227#undef dbg
228/* #define dbg(format, arg...) printk(KERN_INFO "%s: " format "\n", __FILE__, ## arg) */
229#define dbg(format, arg...) printk(KERN_INFO "" format "\n", ## arg)
230
231static void setup_line(struct work_struct *work)
232{
Alan Cox2a77c812008-07-22 11:15:36 +0100233 struct oti6858_private *priv = container_of(work,
234 struct oti6858_private, delayed_setup_work.work);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200235 struct usb_serial_port *port = priv->port;
236 struct oti6858_control_pkt *new_setup;
237 unsigned long flags;
238 int result;
239
Harvey Harrison441b62c2008-03-03 16:08:34 -0800240 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200241
Alan Cox2a77c812008-07-22 11:15:36 +0100242 new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
243 if (new_setup == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800244 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200245 /* we will try again */
Alan Cox2a77c812008-07-22 11:15:36 +0100246 schedule_delayed_work(&priv->delayed_setup_work,
247 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200248 return;
249 }
250
251 result = usb_control_msg(port->serial->dev,
252 usb_rcvctrlpipe(port->serial->dev, 0),
253 OTI6858_REQ_T_GET_STATUS,
254 OTI6858_REQ_GET_STATUS,
255 0, 0,
256 new_setup, OTI6858_CTRL_PKT_SIZE,
257 100);
258
259 if (result != OTI6858_CTRL_PKT_SIZE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800260 dev_err(&port->dev, "%s(): error reading status\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200261 kfree(new_setup);
262 /* we will try again */
Alan Cox2a77c812008-07-22 11:15:36 +0100263 schedule_delayed_work(&priv->delayed_setup_work,
264 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200265 return;
266 }
267
268 spin_lock_irqsave(&priv->lock, flags);
269 if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
270 new_setup->divisor = priv->pending_setup.divisor;
271 new_setup->control = priv->pending_setup.control;
272 new_setup->frame_fmt = priv->pending_setup.frame_fmt;
273
274 spin_unlock_irqrestore(&priv->lock, flags);
275 result = usb_control_msg(port->serial->dev,
276 usb_sndctrlpipe(port->serial->dev, 0),
277 OTI6858_REQ_T_SET_LINE,
278 OTI6858_REQ_SET_LINE,
279 0, 0,
280 new_setup, OTI6858_CTRL_PKT_SIZE,
281 100);
282 } else {
283 spin_unlock_irqrestore(&priv->lock, flags);
284 result = 0;
285 }
286 kfree(new_setup);
287
288 spin_lock_irqsave(&priv->lock, flags);
289 if (result != OTI6858_CTRL_PKT_SIZE)
290 priv->transient = 0;
291 priv->setup_done = 1;
292 spin_unlock_irqrestore(&priv->lock, flags);
293
Harvey Harrison441b62c2008-03-03 16:08:34 -0800294 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200295 port->interrupt_in_urb->dev = port->serial->dev;
296 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
297 if (result != 0) {
298 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800299 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200300 }
301}
302
303void send_data(struct work_struct *work)
304{
Alan Cox2a77c812008-07-22 11:15:36 +0100305 struct oti6858_private *priv = container_of(work,
306 struct oti6858_private, delayed_write_work.work);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200307 struct usb_serial_port *port = priv->port;
308 int count = 0, result;
309 unsigned long flags;
310 unsigned char allow;
311
Harvey Harrison441b62c2008-03-03 16:08:34 -0800312 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200313
314 spin_lock_irqsave(&priv->lock, flags);
315 if (priv->flags.write_urb_in_use) {
316 spin_unlock_irqrestore(&priv->lock, flags);
Alan Cox2a77c812008-07-22 11:15:36 +0100317 schedule_delayed_work(&priv->delayed_write_work,
318 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200319 return;
320 }
321 priv->flags.write_urb_in_use = 1;
322
Alan Coxb0a239d2008-01-19 16:02:37 +0000323 count = oti6858_buf_data_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200324 spin_unlock_irqrestore(&priv->lock, flags);
325 if (count > port->bulk_out_size)
326 count = port->bulk_out_size;
327
328 if (count != 0) {
329 result = usb_control_msg(port->serial->dev,
330 usb_rcvctrlpipe(port->serial->dev, 0),
331 OTI6858_REQ_T_CHECK_TXBUFF,
332 OTI6858_REQ_CHECK_TXBUFF,
333 count, 0, &allow, 1, 100);
334 if (result != 1 || allow != 0)
335 count = 0;
336 }
337
338 if (count == 0) {
339 priv->flags.write_urb_in_use = 0;
340
Harvey Harrison441b62c2008-03-03 16:08:34 -0800341 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200342 port->interrupt_in_urb->dev = port->serial->dev;
343 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
344 if (result != 0) {
345 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800346 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200347 }
348 return;
349 }
350
351 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000352 oti6858_buf_get(priv->buf, port->write_urb->transfer_buffer, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200353 spin_unlock_irqrestore(&priv->lock, flags);
354
355 port->write_urb->transfer_buffer_length = count;
356 port->write_urb->dev = port->serial->dev;
357 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
358 if (result != 0) {
359 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800360 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200361 priv->flags.write_urb_in_use = 0;
362 }
363
364 usb_serial_port_softint(port);
365}
366
367static int oti6858_startup(struct usb_serial *serial)
368{
Alan Cox2a77c812008-07-22 11:15:36 +0100369 struct usb_serial_port *port = serial->port[0];
370 struct oti6858_private *priv;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200371 int i;
372
373 for (i = 0; i < serial->num_ports; ++i) {
374 priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
375 if (!priv)
376 break;
Alan Coxb0a239d2008-01-19 16:02:37 +0000377 priv->buf = oti6858_buf_alloc(PL2303_BUF_SIZE);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200378 if (priv->buf == NULL) {
379 kfree(priv);
380 break;
381 }
382
383 spin_lock_init(&priv->lock);
384 init_waitqueue_head(&priv->intr_wait);
Alan Cox2a77c812008-07-22 11:15:36 +0100385/* INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
386/* INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200387 priv->port = port;
388 INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
389 INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
390
391 usb_set_serial_port_data(serial->port[i], priv);
392 }
393 if (i == serial->num_ports)
394 return 0;
395
396 for (--i; i >= 0; --i) {
397 priv = usb_get_serial_port_data(serial->port[i]);
Alan Coxb0a239d2008-01-19 16:02:37 +0000398 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200399 kfree(priv);
400 usb_set_serial_port_data(serial->port[i], NULL);
401 }
402 return -ENOMEM;
403}
404
Alan Cox95da3102008-07-22 11:09:07 +0100405static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200406 const unsigned char *buf, int count)
407{
408 struct oti6858_private *priv = usb_get_serial_port_data(port);
409 unsigned long flags;
410
Harvey Harrison441b62c2008-03-03 16:08:34 -0800411 dbg("%s(port = %d, count = %d)", __func__, port->number, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200412
413 if (!count)
414 return count;
415
416 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000417 count = oti6858_buf_put(priv->buf, buf, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200418 spin_unlock_irqrestore(&priv->lock, flags);
419
420 return count;
421}
422
Alan Cox95da3102008-07-22 11:09:07 +0100423static int oti6858_write_room(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200424{
Alan Cox95da3102008-07-22 11:09:07 +0100425 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200426 struct oti6858_private *priv = usb_get_serial_port_data(port);
427 int room = 0;
428 unsigned long flags;
429
Harvey Harrison441b62c2008-03-03 16:08:34 -0800430 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200431
432 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000433 room = oti6858_buf_space_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200434 spin_unlock_irqrestore(&priv->lock, flags);
435
436 return room;
437}
438
Alan Cox95da3102008-07-22 11:09:07 +0100439static int oti6858_chars_in_buffer(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200440{
Alan Cox95da3102008-07-22 11:09:07 +0100441 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200442 struct oti6858_private *priv = usb_get_serial_port_data(port);
443 int chars = 0;
444 unsigned long flags;
445
Harvey Harrison441b62c2008-03-03 16:08:34 -0800446 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200447
448 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000449 chars = oti6858_buf_data_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200450 spin_unlock_irqrestore(&priv->lock, flags);
451
452 return chars;
453}
454
Alan Cox95da3102008-07-22 11:09:07 +0100455static void oti6858_set_termios(struct tty_struct *tty,
456 struct usb_serial_port *port, struct ktermios *old_termios)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200457{
458 struct oti6858_private *priv = usb_get_serial_port_data(port);
459 unsigned long flags;
460 unsigned int cflag;
461 u8 frame_fmt, control;
Al Virofd05e722008-04-28 07:00:16 +0100462 __le16 divisor;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200463 int br;
464
Harvey Harrison441b62c2008-03-03 16:08:34 -0800465 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200466
Alan Cox95da3102008-07-22 11:09:07 +0100467 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800468 dbg("%s(): no tty structures", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200469 return;
470 }
471
472 spin_lock_irqsave(&priv->lock, flags);
473 if (!priv->flags.termios_initialized) {
Alan Cox95da3102008-07-22 11:09:07 +0100474 *(tty->termios) = tty_std_termios;
475 tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
476 tty->termios->c_ispeed = 38400;
477 tty->termios->c_ospeed = 38400;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200478 priv->flags.termios_initialized = 1;
479 }
480 spin_unlock_irqrestore(&priv->lock, flags);
481
Alan Cox95da3102008-07-22 11:09:07 +0100482 cflag = tty->termios->c_cflag;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200483
484 spin_lock_irqsave(&priv->lock, flags);
485 divisor = priv->pending_setup.divisor;
486 frame_fmt = priv->pending_setup.frame_fmt;
487 control = priv->pending_setup.control;
488 spin_unlock_irqrestore(&priv->lock, flags);
489
490 frame_fmt &= ~FMT_DATA_BITS_MASK;
491 switch (cflag & CSIZE) {
Alan Cox2a77c812008-07-22 11:15:36 +0100492 case CS5:
493 frame_fmt |= FMT_DATA_BITS_5;
494 break;
495 case CS6:
496 frame_fmt |= FMT_DATA_BITS_6;
497 break;
498 case CS7:
499 frame_fmt |= FMT_DATA_BITS_7;
500 break;
501 default:
502 case CS8:
503 frame_fmt |= FMT_DATA_BITS_8;
504 break;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200505 }
506
507 /* manufacturer claims that this device can work with baud rates
508 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
509 * guarantee that any other baud rate will work (especially
510 * the higher ones)
511 */
Alan Cox95da3102008-07-22 11:09:07 +0100512 br = tty_get_baud_rate(tty);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200513 if (br == 0) {
514 divisor = 0;
Alan Coxb0a239d2008-01-19 16:02:37 +0000515 } else {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200516 int real_br;
Al Virofd05e722008-04-28 07:00:16 +0100517 int new_divisor;
Alan Coxb0a239d2008-01-19 16:02:37 +0000518 br = min(br, OTI6858_MAX_BAUD_RATE);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200519
Al Virofd05e722008-04-28 07:00:16 +0100520 new_divisor = (96000000 + 8 * br) / (16 * br);
521 real_br = 96000000 / (16 * new_divisor);
522 divisor = cpu_to_le16(new_divisor);
Alan Cox95da3102008-07-22 11:09:07 +0100523 tty_encode_baud_rate(tty, real_br, real_br);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200524 }
525
526 frame_fmt &= ~FMT_STOP_BITS_MASK;
Alan Cox2a77c812008-07-22 11:15:36 +0100527 if ((cflag & CSTOPB) != 0)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200528 frame_fmt |= FMT_STOP_BITS_2;
Alan Cox2a77c812008-07-22 11:15:36 +0100529 else
Kees Lemmens49cdee02007-03-27 12:34:30 +0200530 frame_fmt |= FMT_STOP_BITS_1;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200531
532 frame_fmt &= ~FMT_PARITY_MASK;
533 if ((cflag & PARENB) != 0) {
Alan Cox2a77c812008-07-22 11:15:36 +0100534 if ((cflag & PARODD) != 0)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200535 frame_fmt |= FMT_PARITY_ODD;
Alan Cox2a77c812008-07-22 11:15:36 +0100536 else
Kees Lemmens49cdee02007-03-27 12:34:30 +0200537 frame_fmt |= FMT_PARITY_EVEN;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200538 } else {
539 frame_fmt |= FMT_PARITY_NONE;
540 }
541
542 control &= ~CONTROL_MASK;
543 if ((cflag & CRTSCTS) != 0)
544 control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
545
546 /* change control lines if we are switching to or from B0 */
547 /* FIXME:
548 spin_lock_irqsave(&priv->lock, flags);
549 control = priv->line_control;
550 if ((cflag & CBAUD) == B0)
551 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
552 else
553 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
554 if (control != priv->line_control) {
555 control = priv->line_control;
556 spin_unlock_irqrestore(&priv->lock, flags);
557 set_control_lines(serial->dev, control);
558 } else {
559 spin_unlock_irqrestore(&priv->lock, flags);
560 }
561 */
562
563 spin_lock_irqsave(&priv->lock, flags);
564 if (divisor != priv->pending_setup.divisor
565 || control != priv->pending_setup.control
566 || frame_fmt != priv->pending_setup.frame_fmt) {
567 priv->pending_setup.divisor = divisor;
568 priv->pending_setup.control = control;
569 priv->pending_setup.frame_fmt = frame_fmt;
570 }
571 spin_unlock_irqrestore(&priv->lock, flags);
572}
573
Alan Cox95da3102008-07-22 11:09:07 +0100574static int oti6858_open(struct tty_struct *tty,
575 struct usb_serial_port *port, struct file *filp)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200576{
577 struct oti6858_private *priv = usb_get_serial_port_data(port);
578 struct ktermios tmp_termios;
579 struct usb_serial *serial = port->serial;
580 struct oti6858_control_pkt *buf;
581 unsigned long flags;
582 int result;
583
Harvey Harrison441b62c2008-03-03 16:08:34 -0800584 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200585
586 usb_clear_halt(serial->dev, port->write_urb->pipe);
587 usb_clear_halt(serial->dev, port->read_urb->pipe);
588
Alan Cox95da3102008-07-22 11:09:07 +0100589 if (port->port.count != 1)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200590 return 0;
591
Alan Cox2a77c812008-07-22 11:15:36 +0100592 buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
593 if (buf == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800594 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200595 return -ENOMEM;
596 }
597
598 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
599 OTI6858_REQ_T_GET_STATUS,
600 OTI6858_REQ_GET_STATUS,
601 0, 0,
602 buf, OTI6858_CTRL_PKT_SIZE,
603 100);
604 if (result != OTI6858_CTRL_PKT_SIZE) {
605 /* assume default (after power-on reset) values */
606 buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
607 buf->frame_fmt = 0x03; /* 8N1 */
608 buf->something = 0x43;
609 buf->control = 0x4c; /* DTR, RTS */
610 buf->tx_status = 0x00;
611 buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
612 buf->rx_bytes_avail = 0x00;
613 }
614
615 spin_lock_irqsave(&priv->lock, flags);
616 memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
617 priv->pending_setup.divisor = buf->divisor;
618 priv->pending_setup.frame_fmt = buf->frame_fmt;
619 priv->pending_setup.control = buf->control;
620 spin_unlock_irqrestore(&priv->lock, flags);
621 kfree(buf);
622
Harvey Harrison441b62c2008-03-03 16:08:34 -0800623 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200624 port->interrupt_in_urb->dev = serial->dev;
625 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
626 if (result != 0) {
627 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800628 " with error %d\n", __func__, result);
Alan Cox95da3102008-07-22 11:09:07 +0100629 oti6858_close(tty, port, NULL);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200630 return -EPROTO;
631 }
632
633 /* setup termios */
Alan Cox95da3102008-07-22 11:09:07 +0100634 if (tty)
635 oti6858_set_termios(tty, port, &tmp_termios);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200636
637 return 0;
638}
639
Alan Cox95da3102008-07-22 11:09:07 +0100640static void oti6858_close(struct tty_struct *tty,
641 struct usb_serial_port *port, struct file *filp)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200642{
643 struct oti6858_private *priv = usb_get_serial_port_data(port);
644 unsigned long flags;
645 long timeout;
646 wait_queue_t wait;
647
Harvey Harrison441b62c2008-03-03 16:08:34 -0800648 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200649
650 /* wait for data to drain from the buffer */
651 spin_lock_irqsave(&priv->lock, flags);
652 timeout = 30 * HZ; /* PL2303_CLOSING_WAIT */
653 init_waitqueue_entry(&wait, current);
Alan Cox95da3102008-07-22 11:09:07 +0100654 add_wait_queue(&tty->write_wait, &wait);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800655 dbg("%s(): entering wait loop", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200656 for (;;) {
657 set_current_state(TASK_INTERRUPTIBLE);
Alan Coxb0a239d2008-01-19 16:02:37 +0000658 if (oti6858_buf_data_avail(priv->buf) == 0
Kees Lemmens49cdee02007-03-27 12:34:30 +0200659 || timeout == 0 || signal_pending(current)
Oliver Neukum0915f492008-01-23 12:28:45 +0100660 || port->serial->disconnected)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200661 break;
662 spin_unlock_irqrestore(&priv->lock, flags);
663 timeout = schedule_timeout(timeout);
664 spin_lock_irqsave(&priv->lock, flags);
665 }
666 set_current_state(TASK_RUNNING);
Alan Cox95da3102008-07-22 11:09:07 +0100667 remove_wait_queue(&tty->write_wait, &wait);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800668 dbg("%s(): after wait loop", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200669
670 /* clear out any remaining data in the buffer */
Alan Coxb0a239d2008-01-19 16:02:37 +0000671 oti6858_buf_clear(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200672 spin_unlock_irqrestore(&priv->lock, flags);
673
674 /* wait for characters to drain from the device */
675 /* (this is long enough for the entire 256 byte */
676 /* pl2303 hardware buffer to drain with no flow */
677 /* control for data rates of 1200 bps or more, */
678 /* for lower rates we should really know how much */
679 /* data is in the buffer to compute a delay */
680 /* that is not unnecessarily long) */
681 /* FIXME
Alan Cox95da3102008-07-22 11:09:07 +0100682 bps = tty_get_baud_rate(tty);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200683 if (bps > 1200)
684 timeout = max((HZ*2560)/bps,HZ/10);
685 else
686 */
687 timeout = 2*HZ;
688 schedule_timeout_interruptible(timeout);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800689 dbg("%s(): after schedule_timeout_interruptible()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200690
691 /* cancel scheduled setup */
692 cancel_delayed_work(&priv->delayed_setup_work);
693 cancel_delayed_work(&priv->delayed_write_work);
694 flush_scheduled_work();
695
696 /* shutdown our urbs */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800697 dbg("%s(): shutting down urbs", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200698 usb_kill_urb(port->write_urb);
699 usb_kill_urb(port->read_urb);
700 usb_kill_urb(port->interrupt_in_urb);
701
702 /*
Alan Cox95da3102008-07-22 11:09:07 +0100703 if (tty && (tty->termios->c_cflag) & HUPCL) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200704 // drop DTR and RTS
705 spin_lock_irqsave(&priv->lock, flags);
706 priv->pending_setup.control &= ~CONTROL_MASK;
707 spin_unlock_irqrestore(&priv->lock, flags);
708 }
709 */
710}
711
Alan Cox95da3102008-07-22 11:09:07 +0100712static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200713 unsigned int set, unsigned int clear)
714{
Alan Cox95da3102008-07-22 11:09:07 +0100715 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200716 struct oti6858_private *priv = usb_get_serial_port_data(port);
717 unsigned long flags;
718 u8 control;
719
720 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800721 __func__, port->number, set, clear);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200722
723 if (!usb_get_intfdata(port->serial->interface))
724 return -ENODEV;
725
726 /* FIXME: check if this is correct (active high/low) */
727 spin_lock_irqsave(&priv->lock, flags);
728 control = priv->pending_setup.control;
729 if ((set & TIOCM_RTS) != 0)
730 control |= CONTROL_RTS_HIGH;
731 if ((set & TIOCM_DTR) != 0)
732 control |= CONTROL_DTR_HIGH;
733 if ((clear & TIOCM_RTS) != 0)
734 control &= ~CONTROL_RTS_HIGH;
735 if ((clear & TIOCM_DTR) != 0)
736 control &= ~CONTROL_DTR_HIGH;
737
Alan Cox2a77c812008-07-22 11:15:36 +0100738 if (control != priv->pending_setup.control)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200739 priv->pending_setup.control = control;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200740
Alan Cox2a77c812008-07-22 11:15:36 +0100741 spin_unlock_irqrestore(&priv->lock, flags);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200742 return 0;
743}
744
Alan Cox95da3102008-07-22 11:09:07 +0100745static int oti6858_tiocmget(struct tty_struct *tty, struct file *file)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200746{
Alan Cox95da3102008-07-22 11:09:07 +0100747 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200748 struct oti6858_private *priv = usb_get_serial_port_data(port);
749 unsigned long flags;
750 unsigned pin_state;
751 unsigned result = 0;
752
Harvey Harrison441b62c2008-03-03 16:08:34 -0800753 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200754
755 if (!usb_get_intfdata(port->serial->interface))
756 return -ENODEV;
757
758 spin_lock_irqsave(&priv->lock, flags);
759 pin_state = priv->status.pin_state & PIN_MASK;
760 spin_unlock_irqrestore(&priv->lock, flags);
761
762 /* FIXME: check if this is correct (active high/low) */
763 if ((pin_state & PIN_RTS) != 0)
764 result |= TIOCM_RTS;
765 if ((pin_state & PIN_CTS) != 0)
766 result |= TIOCM_CTS;
767 if ((pin_state & PIN_DSR) != 0)
768 result |= TIOCM_DSR;
769 if ((pin_state & PIN_DTR) != 0)
770 result |= TIOCM_DTR;
771 if ((pin_state & PIN_RI) != 0)
772 result |= TIOCM_RI;
773 if ((pin_state & PIN_DCD) != 0)
774 result |= TIOCM_CD;
775
Harvey Harrison441b62c2008-03-03 16:08:34 -0800776 dbg("%s() = 0x%08x", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200777
778 return result;
779}
780
781static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
782{
783 struct oti6858_private *priv = usb_get_serial_port_data(port);
784 unsigned long flags;
785 unsigned int prev, status;
786 unsigned int changed;
787
788 spin_lock_irqsave(&priv->lock, flags);
789 prev = priv->status.pin_state;
790 spin_unlock_irqrestore(&priv->lock, flags);
791
792 while (1) {
Alan Cox2a77c812008-07-22 11:15:36 +0100793 wait_event_interruptible(priv->intr_wait,
794 priv->status.pin_state != prev);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200795 if (signal_pending(current))
796 return -ERESTARTSYS;
797
798 spin_lock_irqsave(&priv->lock, flags);
799 status = priv->status.pin_state & PIN_MASK;
800 spin_unlock_irqrestore(&priv->lock, flags);
801
802 changed = prev ^ status;
803 /* FIXME: check if this is correct (active high/low) */
Alan Cox2a77c812008-07-22 11:15:36 +0100804 if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
805 ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
806 ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
807 ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
808 return 0;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200809 prev = status;
810 }
811
812 /* NOTREACHED */
813 return 0;
814}
815
Alan Cox95da3102008-07-22 11:09:07 +0100816static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200817 unsigned int cmd, unsigned long arg)
818{
Alan Cox95da3102008-07-22 11:09:07 +0100819 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200820
821 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800822 __func__, port->number, cmd, arg);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200823
824 switch (cmd) {
Alan Cox2a77c812008-07-22 11:15:36 +0100825 case TIOCMIWAIT:
826 dbg("%s(): TIOCMIWAIT", __func__);
827 return wait_modem_info(port, arg);
828 default:
829 dbg("%s(): 0x%04x not supported", __func__, cmd);
830 break;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200831 }
Kees Lemmens49cdee02007-03-27 12:34:30 +0200832 return -ENOIOCTLCMD;
833}
834
Kees Lemmens49cdee02007-03-27 12:34:30 +0200835
836static void oti6858_shutdown(struct usb_serial *serial)
837{
838 struct oti6858_private *priv;
839 int i;
840
Harvey Harrison441b62c2008-03-03 16:08:34 -0800841 dbg("%s()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200842
843 for (i = 0; i < serial->num_ports; ++i) {
844 priv = usb_get_serial_port_data(serial->port[i]);
845 if (priv) {
Alan Coxb0a239d2008-01-19 16:02:37 +0000846 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200847 kfree(priv);
848 usb_set_serial_port_data(serial->port[i], NULL);
849 }
850 }
851}
852
853static void oti6858_read_int_callback(struct urb *urb)
854{
Ming Leicdc97792008-02-24 18:41:47 +0800855 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200856 struct oti6858_private *priv = usb_get_serial_port_data(port);
857 int transient = 0, can_recv = 0, resubmit = 1;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700858 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200859
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700860 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800861 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200862
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700863 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200864 case 0:
865 /* success */
866 break;
867 case -ECONNRESET:
868 case -ENOENT:
869 case -ESHUTDOWN:
870 /* this urb is terminated, clean up */
871 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800872 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200873 return;
874 default:
875 dbg("%s(): nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800876 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200877 break;
878 }
879
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700880 if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200881 struct oti6858_control_pkt *xs = urb->transfer_buffer;
882 unsigned long flags;
883
884 spin_lock_irqsave(&priv->lock, flags);
885
886 if (!priv->transient) {
887 if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
888 if (xs->rx_bytes_avail == 0) {
889 priv->transient = 4;
890 priv->setup_done = 0;
891 resubmit = 0;
892 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800893 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200894 schedule_delayed_work(&priv->delayed_setup_work, 0);
895 }
896 }
897 } else {
898 if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
899 priv->transient = 0;
900 } else if (!priv->setup_done) {
901 resubmit = 0;
902 } else if (--priv->transient == 0) {
903 if (xs->rx_bytes_avail == 0) {
904 priv->transient = 4;
905 priv->setup_done = 0;
906 resubmit = 0;
907 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800908 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200909 schedule_delayed_work(&priv->delayed_setup_work, 0);
910 }
911 }
912 }
913
914 if (!priv->transient) {
915 if (xs->pin_state != priv->status.pin_state)
916 wake_up_interruptible(&priv->intr_wait);
917 memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
918 }
919
920 if (!priv->transient && xs->rx_bytes_avail != 0) {
921 can_recv = xs->rx_bytes_avail;
922 priv->flags.read_urb_in_use = 1;
923 }
924
925 transient = priv->transient;
926 spin_unlock_irqrestore(&priv->lock, flags);
927 }
928
929 if (can_recv) {
930 int result;
931
932 port->read_urb->dev = port->serial->dev;
933 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
934 if (result != 0) {
935 priv->flags.read_urb_in_use = 0;
936 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800937 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200938 } else {
939 resubmit = 0;
940 }
941 } else if (!transient) {
942 unsigned long flags;
943
944 spin_lock_irqsave(&priv->lock, flags);
945 if (priv->flags.write_urb_in_use == 0
Alan Coxb0a239d2008-01-19 16:02:37 +0000946 && oti6858_buf_data_avail(priv->buf) != 0) {
Alan Cox2a77c812008-07-22 11:15:36 +0100947 schedule_delayed_work(&priv->delayed_write_work, 0);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200948 resubmit = 0;
949 }
950 spin_unlock_irqrestore(&priv->lock, flags);
951 }
952
953 if (resubmit) {
954 int result;
955
Alan Cox2a77c812008-07-22 11:15:36 +0100956/* dbg("%s(): submitting interrupt urb", __func__); */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200957 urb->dev = port->serial->dev;
958 result = usb_submit_urb(urb, GFP_ATOMIC);
959 if (result != 0) {
960 dev_err(&urb->dev->dev,
961 "%s(): usb_submit_urb() failed with"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800962 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200963 }
964 }
965}
966
967static void oti6858_read_bulk_callback(struct urb *urb)
968{
Ming Leicdc97792008-02-24 18:41:47 +0800969 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200970 struct oti6858_private *priv = usb_get_serial_port_data(port);
971 struct tty_struct *tty;
972 unsigned char *data = urb->transfer_buffer;
973 unsigned long flags;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700974 int status = urb->status;
Alan Coxb0a239d2008-01-19 16:02:37 +0000975 int result;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200976
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700977 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800978 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200979
980 spin_lock_irqsave(&priv->lock, flags);
981 priv->flags.read_urb_in_use = 0;
982 spin_unlock_irqrestore(&priv->lock, flags);
983
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700984 if (status != 0) {
Alan Cox95da3102008-07-22 11:09:07 +0100985 if (!port->port.count) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800986 dbg("%s(): port is closed, exiting", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200987 return;
988 }
989 /*
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700990 if (status == -EPROTO) {
Alan Cox2a77c812008-07-22 11:15:36 +0100991 * PL2303 mysteriously fails with -EPROTO reschedule
992 the read *
993 dbg("%s - caught -EPROTO, resubmitting the urb",
994 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200995 result = usb_submit_urb(urb, GFP_ATOMIC);
996 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800997 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200998 return;
999 }
1000 */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001001 dbg("%s(): unable to handle the error, exiting", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001002 return;
1003 }
1004
Alan Cox95da3102008-07-22 11:09:07 +01001005 tty = port->port.tty;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001006 if (tty != NULL && urb->actual_length > 0) {
Alan Coxb0a239d2008-01-19 16:02:37 +00001007 tty_insert_flip_string(tty, data, urb->actual_length);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001008 tty_flip_buffer_push(tty);
1009 }
1010
Alan Cox2a77c812008-07-22 11:15:36 +01001011 /* schedule the interrupt urb if we are still open */
Alan Cox95da3102008-07-22 11:09:07 +01001012 if (port->port.count != 0) {
Kees Lemmens49cdee02007-03-27 12:34:30 +02001013 port->interrupt_in_urb->dev = port->serial->dev;
1014 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1015 if (result != 0) {
1016 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001017 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001018 }
1019 }
1020}
1021
1022static void oti6858_write_bulk_callback(struct urb *urb)
1023{
Ming Leicdc97792008-02-24 18:41:47 +08001024 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001025 struct oti6858_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -07001026 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001027 int result;
1028
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -07001029 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001030 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001031
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -07001032 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +02001033 case 0:
1034 /* success */
1035 break;
1036 case -ECONNRESET:
1037 case -ENOENT:
1038 case -ESHUTDOWN:
1039 /* this urb is terminated, clean up */
1040 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001041 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001042 priv->flags.write_urb_in_use = 0;
1043 return;
1044 default:
1045 /* error in the urb, so we have to resubmit it */
1046 dbg("%s(): nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001047 __func__, status);
1048 dbg("%s(): overflow in write", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001049
1050 port->write_urb->transfer_buffer_length = 1;
1051 port->write_urb->dev = port->serial->dev;
1052 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1053 if (result) {
1054 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001055 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001056 } else {
1057 return;
1058 }
1059 }
1060
1061 priv->flags.write_urb_in_use = 0;
1062
Alan Cox2a77c812008-07-22 11:15:36 +01001063 /* schedule the interrupt urb if we are still open */
Kees Lemmens49cdee02007-03-27 12:34:30 +02001064 port->interrupt_in_urb->dev = port->serial->dev;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001065 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001066 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1067 if (result != 0) {
1068 dev_err(&port->dev, "%s(): failed submitting int urb,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001069 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001070 }
1071}
1072
1073
1074/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001075 * oti6858_buf_alloc
Kees Lemmens49cdee02007-03-27 12:34:30 +02001076 *
1077 * Allocate a circular buffer and all associated memory.
1078 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001079static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001080{
Alan Coxb0a239d2008-01-19 16:02:37 +00001081 struct oti6858_buf *pb;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001082
1083 if (size == 0)
1084 return NULL;
1085
Alan Coxb0a239d2008-01-19 16:02:37 +00001086 pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001087 if (pb == NULL)
1088 return NULL;
1089
1090 pb->buf_buf = kmalloc(size, GFP_KERNEL);
1091 if (pb->buf_buf == NULL) {
1092 kfree(pb);
1093 return NULL;
1094 }
1095
1096 pb->buf_size = size;
1097 pb->buf_get = pb->buf_put = pb->buf_buf;
1098
1099 return pb;
1100}
1101
1102/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001103 * oti6858_buf_free
Kees Lemmens49cdee02007-03-27 12:34:30 +02001104 *
1105 * Free the buffer and all associated memory.
1106 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001107static void oti6858_buf_free(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001108{
1109 if (pb) {
1110 kfree(pb->buf_buf);
1111 kfree(pb);
1112 }
1113}
1114
1115/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001116 * oti6858_buf_clear
Kees Lemmens49cdee02007-03-27 12:34:30 +02001117 *
1118 * Clear out all data in the circular buffer.
1119 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001120static void oti6858_buf_clear(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001121{
1122 if (pb != NULL) {
1123 /* equivalent to a get of all data available */
1124 pb->buf_get = pb->buf_put;
1125 }
1126}
1127
1128/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001129 * oti6858_buf_data_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001130 *
1131 * Return the number of bytes of data available in the circular
1132 * buffer.
1133 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001134static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001135{
1136 if (pb == NULL)
1137 return 0;
Alan Cox2a77c812008-07-22 11:15:36 +01001138 return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001139}
1140
1141/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001142 * oti6858_buf_space_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001143 *
1144 * Return the number of bytes of space available in the circular
1145 * buffer.
1146 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001147static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001148{
1149 if (pb == NULL)
1150 return 0;
Alan Cox2a77c812008-07-22 11:15:36 +01001151 return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001152}
1153
1154/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001155 * oti6858_buf_put
Kees Lemmens49cdee02007-03-27 12:34:30 +02001156 *
1157 * Copy data data from a user buffer and put it into the circular buffer.
1158 * Restrict to the amount of space available.
1159 *
1160 * Return the number of bytes copied.
1161 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001162static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001163 unsigned int count)
1164{
1165 unsigned int len;
1166
1167 if (pb == NULL)
1168 return 0;
1169
Alan Coxb0a239d2008-01-19 16:02:37 +00001170 len = oti6858_buf_space_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001171 if (count > len)
1172 count = len;
1173
1174 if (count == 0)
1175 return 0;
1176
1177 len = pb->buf_buf + pb->buf_size - pb->buf_put;
1178 if (count > len) {
1179 memcpy(pb->buf_put, buf, len);
1180 memcpy(pb->buf_buf, buf+len, count - len);
1181 pb->buf_put = pb->buf_buf + count - len;
1182 } else {
1183 memcpy(pb->buf_put, buf, count);
1184 if (count < len)
1185 pb->buf_put += count;
1186 else /* count == len */
1187 pb->buf_put = pb->buf_buf;
1188 }
1189
1190 return count;
1191}
1192
1193/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001194 * oti6858_buf_get
Kees Lemmens49cdee02007-03-27 12:34:30 +02001195 *
1196 * Get data from the circular buffer and copy to the given buffer.
1197 * Restrict to the amount of data available.
1198 *
1199 * Return the number of bytes copied.
1200 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001201static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001202 unsigned int count)
1203{
1204 unsigned int len;
1205
1206 if (pb == NULL)
1207 return 0;
1208
Alan Coxb0a239d2008-01-19 16:02:37 +00001209 len = oti6858_buf_data_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001210 if (count > len)
1211 count = len;
1212
1213 if (count == 0)
1214 return 0;
1215
1216 len = pb->buf_buf + pb->buf_size - pb->buf_get;
1217 if (count > len) {
1218 memcpy(buf, pb->buf_get, len);
1219 memcpy(buf+len, pb->buf_buf, count - len);
1220 pb->buf_get = pb->buf_buf + count - len;
1221 } else {
1222 memcpy(buf, pb->buf_get, count);
1223 if (count < len)
1224 pb->buf_get += count;
1225 else /* count == len */
1226 pb->buf_get = pb->buf_buf;
1227 }
1228
1229 return count;
1230}
1231
1232/* module description and (de)initialization */
1233
1234static int __init oti6858_init(void)
1235{
1236 int retval;
1237
Alan Cox2a77c812008-07-22 11:15:36 +01001238 retval = usb_serial_register(&oti6858_device);
1239 if (retval == 0) {
1240 retval = usb_register(&oti6858_driver);
1241 if (retval)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001242 usb_serial_deregister(&oti6858_device);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001243 }
Kees Lemmens49cdee02007-03-27 12:34:30 +02001244 return retval;
1245}
1246
1247static void __exit oti6858_exit(void)
1248{
1249 usb_deregister(&oti6858_driver);
1250 usb_serial_deregister(&oti6858_device);
1251}
1252
1253module_init(oti6858_init);
1254module_exit(oti6858_exit);
1255
1256MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
1257MODULE_AUTHOR(OTI6858_AUTHOR);
1258MODULE_VERSION(OTI6858_VERSION);
1259MODULE_LICENSE("GPL");
1260
1261module_param(debug, bool, S_IRUGO | S_IWUSR);
1262MODULE_PARM_DESC(debug, "enable debug output");
1263