blob: 0f4a70ce382372170291b36188ac93b6ee635ea8 [file] [log] [blame]
Kees Lemmens49cdee02007-03-27 12:34:30 +02001/*
2 * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
3 *
4 * Copyleft (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
5 * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
6 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2003 IBM Corp.
8 *
9 * Many thanks to the authors of pl2303 driver: all functions in this file
10 * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
11 *
12 * Warning! You use this driver on your own risk! The only official
13 * description of this device I have is datasheet from manufacturer,
14 * and it doesn't contain almost any information needed to write a driver.
15 * Almost all knowlegde used while writing this driver was gathered by:
16 * - analyzing traffic between device and the M$ Windows 2000 driver,
17 * - trying different bit combinations and checking pin states
18 * with a voltmeter,
19 * - receiving malformed frames and producing buffer overflows
20 * to learn how errors are reported,
21 * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
22 * CONNECTED TO IT!
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License.
27 *
Alan Cox2a77c812008-07-22 11:15:36 +010028 * See Documentation/usb/usb-serial.txt for more information on using this
29 * driver
Kees Lemmens49cdee02007-03-27 12:34:30 +020030 *
31 * TODO:
32 * - implement correct flushing for ioctls and oti6858_close()
33 * - check how errors (rx overflow, parity error, framing error) are reported
34 * - implement oti6858_break_ctl()
35 * - implement more ioctls
36 * - test/implement flow control
37 * - allow setting custom baud rates
38 */
39
40#include <linux/kernel.h>
41#include <linux/errno.h>
42#include <linux/init.h>
43#include <linux/slab.h>
44#include <linux/tty.h>
45#include <linux/tty_driver.h>
46#include <linux/tty_flip.h>
47#include <linux/serial.h>
48#include <linux/module.h>
49#include <linux/moduleparam.h>
50#include <linux/spinlock.h>
51#include <linux/usb.h>
52#include <linux/usb/serial.h>
Alan Cox2a77c812008-07-22 11:15:36 +010053#include <linux/uaccess.h>
Kees Lemmens49cdee02007-03-27 12:34:30 +020054#include "oti6858.h"
55
56#define OTI6858_DESCRIPTION \
57 "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
58#define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
59#define OTI6858_VERSION "0.1"
60
61static struct usb_device_id id_table [] = {
62 { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
63 { }
64};
65
66MODULE_DEVICE_TABLE(usb, id_table);
67
68static struct usb_driver oti6858_driver = {
69 .name = "oti6858",
70 .probe = usb_serial_probe,
71 .disconnect = usb_serial_disconnect,
72 .id_table = id_table,
73 .no_dynamic_id = 1,
74};
75
76static int debug;
77
78
79/* buffering code, copied from pl2303 driver */
80#define PL2303_BUF_SIZE 1024
81#define PL2303_TMP_BUF_SIZE 1024
82
Alan Coxb0a239d2008-01-19 16:02:37 +000083struct oti6858_buf {
Kees Lemmens49cdee02007-03-27 12:34:30 +020084 unsigned int buf_size;
85 char *buf_buf;
86 char *buf_get;
87 char *buf_put;
88};
89
90/* requests */
91#define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
92#define OTI6858_REQ_T_GET_STATUS 0x01
93
94#define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
95#define OTI6858_REQ_T_SET_LINE 0x00
96
97#define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
98#define OTI6858_REQ_T_CHECK_TXBUFF 0x00
99
100/* format of the control packet */
101struct oti6858_control_pkt {
Al Virofd05e722008-04-28 07:00:16 +0100102 __le16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200103#define OTI6858_MAX_BAUD_RATE 3000000
104 u8 frame_fmt;
105#define FMT_STOP_BITS_MASK 0xc0
106#define FMT_STOP_BITS_1 0x00
107#define FMT_STOP_BITS_2 0x40 /* 1.5 stop bits if FMT_DATA_BITS_5 */
108#define FMT_PARITY_MASK 0x38
109#define FMT_PARITY_NONE 0x00
110#define FMT_PARITY_ODD 0x08
111#define FMT_PARITY_EVEN 0x18
112#define FMT_PARITY_MARK 0x28
113#define FMT_PARITY_SPACE 0x38
114#define FMT_DATA_BITS_MASK 0x03
115#define FMT_DATA_BITS_5 0x00
116#define FMT_DATA_BITS_6 0x01
117#define FMT_DATA_BITS_7 0x02
118#define FMT_DATA_BITS_8 0x03
119 u8 something; /* always equals 0x43 */
120 u8 control; /* settings of flow control lines */
121#define CONTROL_MASK 0x0c
122#define CONTROL_DTR_HIGH 0x08
123#define CONTROL_RTS_HIGH 0x04
124 u8 tx_status;
125#define TX_BUFFER_EMPTIED 0x09
126 u8 pin_state;
127#define PIN_MASK 0x3f
128#define PIN_RTS 0x20 /* output pin */
129#define PIN_CTS 0x10 /* input pin, active low */
130#define PIN_DSR 0x08 /* input pin, active low */
131#define PIN_DTR 0x04 /* output pin */
132#define PIN_RI 0x02 /* input pin, active low */
133#define PIN_DCD 0x01 /* input pin, active low */
134 u8 rx_bytes_avail; /* number of bytes in rx buffer */;
135};
136
137#define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt)
138#define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
Alan Cox2a77c812008-07-22 11:15:36 +0100139 (((a)->divisor == (priv)->pending_setup.divisor) \
Kees Lemmens49cdee02007-03-27 12:34:30 +0200140 && ((a)->control == (priv)->pending_setup.control) \
Alan Cox2a77c812008-07-22 11:15:36 +0100141 && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
Kees Lemmens49cdee02007-03-27 12:34:30 +0200142
143/* function prototypes */
Alan Coxa509a7e2009-09-19 13:13:26 -0700144static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100145static void oti6858_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +0100146static void oti6858_set_termios(struct tty_struct *tty,
147 struct usb_serial_port *port, struct ktermios *old);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700148static void oti6858_init_termios(struct tty_struct *tty);
Alan Cox95da3102008-07-22 11:09:07 +0100149static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200150 unsigned int cmd, unsigned long arg);
151static void oti6858_read_int_callback(struct urb *urb);
152static void oti6858_read_bulk_callback(struct urb *urb);
153static void oti6858_write_bulk_callback(struct urb *urb);
Alan Cox95da3102008-07-22 11:09:07 +0100154static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200155 const unsigned char *buf, int count);
Alan Cox95da3102008-07-22 11:09:07 +0100156static int oti6858_write_room(struct tty_struct *tty);
157static int oti6858_chars_in_buffer(struct tty_struct *tty);
158static int oti6858_tiocmget(struct tty_struct *tty, struct file *file);
159static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200160 unsigned int set, unsigned int clear);
161static int oti6858_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400162static void oti6858_release(struct usb_serial *serial);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200163
164/* functions operating on buffers */
Alan Coxb0a239d2008-01-19 16:02:37 +0000165static struct oti6858_buf *oti6858_buf_alloc(unsigned int size);
166static void oti6858_buf_free(struct oti6858_buf *pb);
167static void oti6858_buf_clear(struct oti6858_buf *pb);
168static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb);
169static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb);
170static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200171 unsigned int count);
Alan Coxb0a239d2008-01-19 16:02:37 +0000172static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200173 unsigned int count);
174
175
176/* device info */
177static struct usb_serial_driver oti6858_device = {
178 .driver = {
179 .owner = THIS_MODULE,
180 .name = "oti6858",
181 },
182 .id_table = id_table,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200183 .num_ports = 1,
184 .open = oti6858_open,
185 .close = oti6858_close,
186 .write = oti6858_write,
187 .ioctl = oti6858_ioctl,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200188 .set_termios = oti6858_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700189 .init_termios = oti6858_init_termios,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200190 .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,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400198 .release = oti6858_release,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200199};
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;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200210 } flags;
211 struct delayed_work delayed_write_work;
212
213 struct {
Al Virofd05e722008-04-28 07:00:16 +0100214 __le16 divisor;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200215 u8 frame_fmt;
216 u8 control;
217 } pending_setup;
218 u8 transient;
219 u8 setup_done;
220 struct delayed_work delayed_setup_work;
221
222 wait_queue_head_t intr_wait;
Alan Cox2a77c812008-07-22 11:15:36 +0100223 struct usb_serial_port *port; /* USB port with which associated */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200224};
225
Kees Lemmens49cdee02007-03-27 12:34:30 +0200226static void setup_line(struct work_struct *work)
227{
Alan Cox2a77c812008-07-22 11:15:36 +0100228 struct oti6858_private *priv = container_of(work,
229 struct oti6858_private, delayed_setup_work.work);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200230 struct usb_serial_port *port = priv->port;
231 struct oti6858_control_pkt *new_setup;
232 unsigned long flags;
233 int result;
234
Harvey Harrison441b62c2008-03-03 16:08:34 -0800235 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200236
Alan Cox2a77c812008-07-22 11:15:36 +0100237 new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
238 if (new_setup == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800239 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200240 /* we will try again */
Alan Cox2a77c812008-07-22 11:15:36 +0100241 schedule_delayed_work(&priv->delayed_setup_work,
242 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200243 return;
244 }
245
246 result = usb_control_msg(port->serial->dev,
247 usb_rcvctrlpipe(port->serial->dev, 0),
248 OTI6858_REQ_T_GET_STATUS,
249 OTI6858_REQ_GET_STATUS,
250 0, 0,
251 new_setup, OTI6858_CTRL_PKT_SIZE,
252 100);
253
254 if (result != OTI6858_CTRL_PKT_SIZE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800255 dev_err(&port->dev, "%s(): error reading status\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200256 kfree(new_setup);
257 /* we will try again */
Alan Cox2a77c812008-07-22 11:15:36 +0100258 schedule_delayed_work(&priv->delayed_setup_work,
259 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200260 return;
261 }
262
263 spin_lock_irqsave(&priv->lock, flags);
264 if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
265 new_setup->divisor = priv->pending_setup.divisor;
266 new_setup->control = priv->pending_setup.control;
267 new_setup->frame_fmt = priv->pending_setup.frame_fmt;
268
269 spin_unlock_irqrestore(&priv->lock, flags);
270 result = usb_control_msg(port->serial->dev,
271 usb_sndctrlpipe(port->serial->dev, 0),
272 OTI6858_REQ_T_SET_LINE,
273 OTI6858_REQ_SET_LINE,
274 0, 0,
275 new_setup, OTI6858_CTRL_PKT_SIZE,
276 100);
277 } else {
278 spin_unlock_irqrestore(&priv->lock, flags);
279 result = 0;
280 }
281 kfree(new_setup);
282
283 spin_lock_irqsave(&priv->lock, flags);
284 if (result != OTI6858_CTRL_PKT_SIZE)
285 priv->transient = 0;
286 priv->setup_done = 1;
287 spin_unlock_irqrestore(&priv->lock, flags);
288
Harvey Harrison441b62c2008-03-03 16:08:34 -0800289 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200290 port->interrupt_in_urb->dev = port->serial->dev;
291 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
292 if (result != 0) {
293 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800294 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200295 }
296}
297
298void send_data(struct work_struct *work)
299{
Alan Cox2a77c812008-07-22 11:15:36 +0100300 struct oti6858_private *priv = container_of(work,
301 struct oti6858_private, delayed_write_work.work);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200302 struct usb_serial_port *port = priv->port;
303 int count = 0, result;
304 unsigned long flags;
305 unsigned char allow;
306
Harvey Harrison441b62c2008-03-03 16:08:34 -0800307 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200308
309 spin_lock_irqsave(&priv->lock, flags);
310 if (priv->flags.write_urb_in_use) {
311 spin_unlock_irqrestore(&priv->lock, flags);
Alan Cox2a77c812008-07-22 11:15:36 +0100312 schedule_delayed_work(&priv->delayed_write_work,
313 msecs_to_jiffies(2));
Kees Lemmens49cdee02007-03-27 12:34:30 +0200314 return;
315 }
316 priv->flags.write_urb_in_use = 1;
317
Alan Coxb0a239d2008-01-19 16:02:37 +0000318 count = oti6858_buf_data_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200319 spin_unlock_irqrestore(&priv->lock, flags);
320 if (count > port->bulk_out_size)
321 count = port->bulk_out_size;
322
323 if (count != 0) {
324 result = usb_control_msg(port->serial->dev,
325 usb_rcvctrlpipe(port->serial->dev, 0),
326 OTI6858_REQ_T_CHECK_TXBUFF,
327 OTI6858_REQ_CHECK_TXBUFF,
328 count, 0, &allow, 1, 100);
329 if (result != 1 || allow != 0)
330 count = 0;
331 }
332
333 if (count == 0) {
334 priv->flags.write_urb_in_use = 0;
335
Harvey Harrison441b62c2008-03-03 16:08:34 -0800336 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200337 port->interrupt_in_urb->dev = port->serial->dev;
338 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
339 if (result != 0) {
340 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800341 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200342 }
343 return;
344 }
345
346 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000347 oti6858_buf_get(priv->buf, port->write_urb->transfer_buffer, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200348 spin_unlock_irqrestore(&priv->lock, flags);
349
350 port->write_urb->transfer_buffer_length = count;
351 port->write_urb->dev = port->serial->dev;
352 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
353 if (result != 0) {
354 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800355 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200356 priv->flags.write_urb_in_use = 0;
357 }
358
359 usb_serial_port_softint(port);
360}
361
362static int oti6858_startup(struct usb_serial *serial)
363{
Alan Cox2a77c812008-07-22 11:15:36 +0100364 struct usb_serial_port *port = serial->port[0];
365 struct oti6858_private *priv;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200366 int i;
367
368 for (i = 0; i < serial->num_ports; ++i) {
369 priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
370 if (!priv)
371 break;
Alan Coxb0a239d2008-01-19 16:02:37 +0000372 priv->buf = oti6858_buf_alloc(PL2303_BUF_SIZE);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200373 if (priv->buf == NULL) {
374 kfree(priv);
375 break;
376 }
377
378 spin_lock_init(&priv->lock);
379 init_waitqueue_head(&priv->intr_wait);
Alan Cox2a77c812008-07-22 11:15:36 +0100380/* INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
381/* INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200382 priv->port = port;
383 INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
384 INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
385
386 usb_set_serial_port_data(serial->port[i], priv);
387 }
388 if (i == serial->num_ports)
389 return 0;
390
391 for (--i; i >= 0; --i) {
392 priv = usb_get_serial_port_data(serial->port[i]);
Alan Coxb0a239d2008-01-19 16:02:37 +0000393 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200394 kfree(priv);
395 usb_set_serial_port_data(serial->port[i], NULL);
396 }
397 return -ENOMEM;
398}
399
Alan Cox95da3102008-07-22 11:09:07 +0100400static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200401 const unsigned char *buf, int count)
402{
403 struct oti6858_private *priv = usb_get_serial_port_data(port);
404 unsigned long flags;
405
Harvey Harrison441b62c2008-03-03 16:08:34 -0800406 dbg("%s(port = %d, count = %d)", __func__, port->number, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200407
408 if (!count)
409 return count;
410
411 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000412 count = oti6858_buf_put(priv->buf, buf, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200413 spin_unlock_irqrestore(&priv->lock, flags);
414
415 return count;
416}
417
Alan Cox95da3102008-07-22 11:09:07 +0100418static int oti6858_write_room(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200419{
Alan Cox95da3102008-07-22 11:09:07 +0100420 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200421 struct oti6858_private *priv = usb_get_serial_port_data(port);
422 int room = 0;
423 unsigned long flags;
424
Harvey Harrison441b62c2008-03-03 16:08:34 -0800425 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200426
427 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000428 room = oti6858_buf_space_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200429 spin_unlock_irqrestore(&priv->lock, flags);
430
431 return room;
432}
433
Alan Cox95da3102008-07-22 11:09:07 +0100434static int oti6858_chars_in_buffer(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200435{
Alan Cox95da3102008-07-22 11:09:07 +0100436 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200437 struct oti6858_private *priv = usb_get_serial_port_data(port);
438 int chars = 0;
439 unsigned long flags;
440
Harvey Harrison441b62c2008-03-03 16:08:34 -0800441 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200442
443 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000444 chars = oti6858_buf_data_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200445 spin_unlock_irqrestore(&priv->lock, flags);
446
447 return chars;
448}
449
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700450static void oti6858_init_termios(struct tty_struct *tty)
451{
452 *(tty->termios) = tty_std_termios;
453 tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
454 tty->termios->c_ispeed = 38400;
455 tty->termios->c_ospeed = 38400;
456}
457
Alan Cox95da3102008-07-22 11:09:07 +0100458static void oti6858_set_termios(struct tty_struct *tty,
459 struct usb_serial_port *port, struct ktermios *old_termios)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200460{
461 struct oti6858_private *priv = usb_get_serial_port_data(port);
462 unsigned long flags;
463 unsigned int cflag;
464 u8 frame_fmt, control;
Al Virofd05e722008-04-28 07:00:16 +0100465 __le16 divisor;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200466 int br;
467
Harvey Harrison441b62c2008-03-03 16:08:34 -0800468 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200469
Alan Cox95da3102008-07-22 11:09:07 +0100470 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800471 dbg("%s(): no tty structures", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200472 return;
473 }
474
Alan Cox95da3102008-07-22 11:09:07 +0100475 cflag = tty->termios->c_cflag;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200476
477 spin_lock_irqsave(&priv->lock, flags);
478 divisor = priv->pending_setup.divisor;
479 frame_fmt = priv->pending_setup.frame_fmt;
480 control = priv->pending_setup.control;
481 spin_unlock_irqrestore(&priv->lock, flags);
482
483 frame_fmt &= ~FMT_DATA_BITS_MASK;
484 switch (cflag & CSIZE) {
Alan Cox2a77c812008-07-22 11:15:36 +0100485 case CS5:
486 frame_fmt |= FMT_DATA_BITS_5;
487 break;
488 case CS6:
489 frame_fmt |= FMT_DATA_BITS_6;
490 break;
491 case CS7:
492 frame_fmt |= FMT_DATA_BITS_7;
493 break;
494 default:
495 case CS8:
496 frame_fmt |= FMT_DATA_BITS_8;
497 break;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200498 }
499
500 /* manufacturer claims that this device can work with baud rates
501 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
502 * guarantee that any other baud rate will work (especially
503 * the higher ones)
504 */
Alan Cox95da3102008-07-22 11:09:07 +0100505 br = tty_get_baud_rate(tty);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200506 if (br == 0) {
507 divisor = 0;
Alan Coxb0a239d2008-01-19 16:02:37 +0000508 } else {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200509 int real_br;
Al Virofd05e722008-04-28 07:00:16 +0100510 int new_divisor;
Alan Coxb0a239d2008-01-19 16:02:37 +0000511 br = min(br, OTI6858_MAX_BAUD_RATE);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200512
Al Virofd05e722008-04-28 07:00:16 +0100513 new_divisor = (96000000 + 8 * br) / (16 * br);
514 real_br = 96000000 / (16 * new_divisor);
515 divisor = cpu_to_le16(new_divisor);
Alan Cox95da3102008-07-22 11:09:07 +0100516 tty_encode_baud_rate(tty, real_br, real_br);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200517 }
518
519 frame_fmt &= ~FMT_STOP_BITS_MASK;
Alan Cox2a77c812008-07-22 11:15:36 +0100520 if ((cflag & CSTOPB) != 0)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200521 frame_fmt |= FMT_STOP_BITS_2;
Alan Cox2a77c812008-07-22 11:15:36 +0100522 else
Kees Lemmens49cdee02007-03-27 12:34:30 +0200523 frame_fmt |= FMT_STOP_BITS_1;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200524
525 frame_fmt &= ~FMT_PARITY_MASK;
526 if ((cflag & PARENB) != 0) {
Alan Cox2a77c812008-07-22 11:15:36 +0100527 if ((cflag & PARODD) != 0)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200528 frame_fmt |= FMT_PARITY_ODD;
Alan Cox2a77c812008-07-22 11:15:36 +0100529 else
Kees Lemmens49cdee02007-03-27 12:34:30 +0200530 frame_fmt |= FMT_PARITY_EVEN;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200531 } else {
532 frame_fmt |= FMT_PARITY_NONE;
533 }
534
535 control &= ~CONTROL_MASK;
536 if ((cflag & CRTSCTS) != 0)
537 control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
538
539 /* change control lines if we are switching to or from B0 */
540 /* FIXME:
541 spin_lock_irqsave(&priv->lock, flags);
542 control = priv->line_control;
543 if ((cflag & CBAUD) == B0)
544 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
545 else
546 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
547 if (control != priv->line_control) {
548 control = priv->line_control;
549 spin_unlock_irqrestore(&priv->lock, flags);
550 set_control_lines(serial->dev, control);
551 } else {
552 spin_unlock_irqrestore(&priv->lock, flags);
553 }
554 */
555
556 spin_lock_irqsave(&priv->lock, flags);
557 if (divisor != priv->pending_setup.divisor
558 || control != priv->pending_setup.control
559 || frame_fmt != priv->pending_setup.frame_fmt) {
560 priv->pending_setup.divisor = divisor;
561 priv->pending_setup.control = control;
562 priv->pending_setup.frame_fmt = frame_fmt;
563 }
564 spin_unlock_irqrestore(&priv->lock, flags);
565}
566
Alan Coxa509a7e2009-09-19 13:13:26 -0700567static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200568{
569 struct oti6858_private *priv = usb_get_serial_port_data(port);
570 struct ktermios tmp_termios;
571 struct usb_serial *serial = port->serial;
572 struct oti6858_control_pkt *buf;
573 unsigned long flags;
574 int result;
575
Harvey Harrison441b62c2008-03-03 16:08:34 -0800576 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200577
578 usb_clear_halt(serial->dev, port->write_urb->pipe);
579 usb_clear_halt(serial->dev, port->read_urb->pipe);
580
Alan Cox95da3102008-07-22 11:09:07 +0100581 if (port->port.count != 1)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200582 return 0;
583
Alan Cox2a77c812008-07-22 11:15:36 +0100584 buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
585 if (buf == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800586 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200587 return -ENOMEM;
588 }
589
590 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
591 OTI6858_REQ_T_GET_STATUS,
592 OTI6858_REQ_GET_STATUS,
593 0, 0,
594 buf, OTI6858_CTRL_PKT_SIZE,
595 100);
596 if (result != OTI6858_CTRL_PKT_SIZE) {
597 /* assume default (after power-on reset) values */
598 buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
599 buf->frame_fmt = 0x03; /* 8N1 */
600 buf->something = 0x43;
601 buf->control = 0x4c; /* DTR, RTS */
602 buf->tx_status = 0x00;
603 buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
604 buf->rx_bytes_avail = 0x00;
605 }
606
607 spin_lock_irqsave(&priv->lock, flags);
608 memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
609 priv->pending_setup.divisor = buf->divisor;
610 priv->pending_setup.frame_fmt = buf->frame_fmt;
611 priv->pending_setup.control = buf->control;
612 spin_unlock_irqrestore(&priv->lock, flags);
613 kfree(buf);
614
Harvey Harrison441b62c2008-03-03 16:08:34 -0800615 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200616 port->interrupt_in_urb->dev = serial->dev;
617 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
618 if (result != 0) {
619 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800620 " with error %d\n", __func__, result);
Alan Cox335f8512009-06-11 12:26:29 +0100621 oti6858_close(port);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200622 return -EPROTO;
623 }
624
625 /* setup termios */
Alan Cox95da3102008-07-22 11:09:07 +0100626 if (tty)
627 oti6858_set_termios(tty, port, &tmp_termios);
Alan Cox335f8512009-06-11 12:26:29 +0100628 port->port.drain_delay = 256; /* FIXME: check the FIFO length */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200629 return 0;
630}
631
Alan Cox335f8512009-06-11 12:26:29 +0100632static void oti6858_close(struct usb_serial_port *port)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200633{
634 struct oti6858_private *priv = usb_get_serial_port_data(port);
635 unsigned long flags;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200636
Harvey Harrison441b62c2008-03-03 16:08:34 -0800637 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200638
Kees Lemmens49cdee02007-03-27 12:34:30 +0200639 spin_lock_irqsave(&priv->lock, flags);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200640 /* clear out any remaining data in the buffer */
Alan Coxb0a239d2008-01-19 16:02:37 +0000641 oti6858_buf_clear(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200642 spin_unlock_irqrestore(&priv->lock, flags);
643
Alan Cox335f8512009-06-11 12:26:29 +0100644 dbg("%s(): after buf_clear()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200645
646 /* cancel scheduled setup */
647 cancel_delayed_work(&priv->delayed_setup_work);
648 cancel_delayed_work(&priv->delayed_write_work);
649 flush_scheduled_work();
650
651 /* shutdown our urbs */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800652 dbg("%s(): shutting down urbs", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200653 usb_kill_urb(port->write_urb);
654 usb_kill_urb(port->read_urb);
655 usb_kill_urb(port->interrupt_in_urb);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200656}
657
Alan Cox95da3102008-07-22 11:09:07 +0100658static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200659 unsigned int set, unsigned int clear)
660{
Alan Cox95da3102008-07-22 11:09:07 +0100661 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200662 struct oti6858_private *priv = usb_get_serial_port_data(port);
663 unsigned long flags;
664 u8 control;
665
666 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800667 __func__, port->number, set, clear);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200668
669 if (!usb_get_intfdata(port->serial->interface))
670 return -ENODEV;
671
672 /* FIXME: check if this is correct (active high/low) */
673 spin_lock_irqsave(&priv->lock, flags);
674 control = priv->pending_setup.control;
675 if ((set & TIOCM_RTS) != 0)
676 control |= CONTROL_RTS_HIGH;
677 if ((set & TIOCM_DTR) != 0)
678 control |= CONTROL_DTR_HIGH;
679 if ((clear & TIOCM_RTS) != 0)
680 control &= ~CONTROL_RTS_HIGH;
681 if ((clear & TIOCM_DTR) != 0)
682 control &= ~CONTROL_DTR_HIGH;
683
Alan Cox2a77c812008-07-22 11:15:36 +0100684 if (control != priv->pending_setup.control)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200685 priv->pending_setup.control = control;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200686
Alan Cox2a77c812008-07-22 11:15:36 +0100687 spin_unlock_irqrestore(&priv->lock, flags);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200688 return 0;
689}
690
Alan Cox95da3102008-07-22 11:09:07 +0100691static int oti6858_tiocmget(struct tty_struct *tty, struct file *file)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200692{
Alan Cox95da3102008-07-22 11:09:07 +0100693 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200694 struct oti6858_private *priv = usb_get_serial_port_data(port);
695 unsigned long flags;
696 unsigned pin_state;
697 unsigned result = 0;
698
Harvey Harrison441b62c2008-03-03 16:08:34 -0800699 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200700
701 if (!usb_get_intfdata(port->serial->interface))
702 return -ENODEV;
703
704 spin_lock_irqsave(&priv->lock, flags);
705 pin_state = priv->status.pin_state & PIN_MASK;
706 spin_unlock_irqrestore(&priv->lock, flags);
707
708 /* FIXME: check if this is correct (active high/low) */
709 if ((pin_state & PIN_RTS) != 0)
710 result |= TIOCM_RTS;
711 if ((pin_state & PIN_CTS) != 0)
712 result |= TIOCM_CTS;
713 if ((pin_state & PIN_DSR) != 0)
714 result |= TIOCM_DSR;
715 if ((pin_state & PIN_DTR) != 0)
716 result |= TIOCM_DTR;
717 if ((pin_state & PIN_RI) != 0)
718 result |= TIOCM_RI;
719 if ((pin_state & PIN_DCD) != 0)
720 result |= TIOCM_CD;
721
Harvey Harrison441b62c2008-03-03 16:08:34 -0800722 dbg("%s() = 0x%08x", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200723
724 return result;
725}
726
727static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
728{
729 struct oti6858_private *priv = usb_get_serial_port_data(port);
730 unsigned long flags;
731 unsigned int prev, status;
732 unsigned int changed;
733
734 spin_lock_irqsave(&priv->lock, flags);
735 prev = priv->status.pin_state;
736 spin_unlock_irqrestore(&priv->lock, flags);
737
738 while (1) {
Alan Cox2a77c812008-07-22 11:15:36 +0100739 wait_event_interruptible(priv->intr_wait,
740 priv->status.pin_state != prev);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200741 if (signal_pending(current))
742 return -ERESTARTSYS;
743
744 spin_lock_irqsave(&priv->lock, flags);
745 status = priv->status.pin_state & PIN_MASK;
746 spin_unlock_irqrestore(&priv->lock, flags);
747
748 changed = prev ^ status;
749 /* FIXME: check if this is correct (active high/low) */
Alan Cox2a77c812008-07-22 11:15:36 +0100750 if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
751 ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
752 ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
753 ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
754 return 0;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200755 prev = status;
756 }
757
758 /* NOTREACHED */
759 return 0;
760}
761
Alan Cox95da3102008-07-22 11:09:07 +0100762static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200763 unsigned int cmd, unsigned long arg)
764{
Alan Cox95da3102008-07-22 11:09:07 +0100765 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200766
767 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800768 __func__, port->number, cmd, arg);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200769
770 switch (cmd) {
Alan Cox2a77c812008-07-22 11:15:36 +0100771 case TIOCMIWAIT:
772 dbg("%s(): TIOCMIWAIT", __func__);
773 return wait_modem_info(port, arg);
774 default:
775 dbg("%s(): 0x%04x not supported", __func__, cmd);
776 break;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200777 }
Kees Lemmens49cdee02007-03-27 12:34:30 +0200778 return -ENOIOCTLCMD;
779}
780
Kees Lemmens49cdee02007-03-27 12:34:30 +0200781
Alan Sternf9c99bb2009-06-02 11:53:55 -0400782static void oti6858_release(struct usb_serial *serial)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200783{
784 struct oti6858_private *priv;
785 int i;
786
Harvey Harrison441b62c2008-03-03 16:08:34 -0800787 dbg("%s()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200788
789 for (i = 0; i < serial->num_ports; ++i) {
790 priv = usb_get_serial_port_data(serial->port[i]);
791 if (priv) {
Alan Coxb0a239d2008-01-19 16:02:37 +0000792 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200793 kfree(priv);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200794 }
795 }
796}
797
798static void oti6858_read_int_callback(struct urb *urb)
799{
Ming Leicdc97792008-02-24 18:41:47 +0800800 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200801 struct oti6858_private *priv = usb_get_serial_port_data(port);
802 int transient = 0, can_recv = 0, resubmit = 1;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700803 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200804
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700805 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800806 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200807
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700808 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200809 case 0:
810 /* success */
811 break;
812 case -ECONNRESET:
813 case -ENOENT:
814 case -ESHUTDOWN:
815 /* this urb is terminated, clean up */
816 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800817 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200818 return;
819 default:
820 dbg("%s(): nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800821 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200822 break;
823 }
824
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700825 if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200826 struct oti6858_control_pkt *xs = urb->transfer_buffer;
827 unsigned long flags;
828
829 spin_lock_irqsave(&priv->lock, flags);
830
831 if (!priv->transient) {
832 if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
833 if (xs->rx_bytes_avail == 0) {
834 priv->transient = 4;
835 priv->setup_done = 0;
836 resubmit = 0;
837 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800838 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200839 schedule_delayed_work(&priv->delayed_setup_work, 0);
840 }
841 }
842 } else {
843 if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
844 priv->transient = 0;
845 } else if (!priv->setup_done) {
846 resubmit = 0;
847 } else if (--priv->transient == 0) {
848 if (xs->rx_bytes_avail == 0) {
849 priv->transient = 4;
850 priv->setup_done = 0;
851 resubmit = 0;
852 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800853 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200854 schedule_delayed_work(&priv->delayed_setup_work, 0);
855 }
856 }
857 }
858
859 if (!priv->transient) {
860 if (xs->pin_state != priv->status.pin_state)
861 wake_up_interruptible(&priv->intr_wait);
862 memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
863 }
864
865 if (!priv->transient && xs->rx_bytes_avail != 0) {
866 can_recv = xs->rx_bytes_avail;
867 priv->flags.read_urb_in_use = 1;
868 }
869
870 transient = priv->transient;
871 spin_unlock_irqrestore(&priv->lock, flags);
872 }
873
874 if (can_recv) {
875 int result;
876
877 port->read_urb->dev = port->serial->dev;
878 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
879 if (result != 0) {
880 priv->flags.read_urb_in_use = 0;
881 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800882 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200883 } else {
884 resubmit = 0;
885 }
886 } else if (!transient) {
887 unsigned long flags;
888
889 spin_lock_irqsave(&priv->lock, flags);
890 if (priv->flags.write_urb_in_use == 0
Alan Coxb0a239d2008-01-19 16:02:37 +0000891 && oti6858_buf_data_avail(priv->buf) != 0) {
Alan Cox2a77c812008-07-22 11:15:36 +0100892 schedule_delayed_work(&priv->delayed_write_work, 0);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200893 resubmit = 0;
894 }
895 spin_unlock_irqrestore(&priv->lock, flags);
896 }
897
898 if (resubmit) {
899 int result;
900
Alan Cox2a77c812008-07-22 11:15:36 +0100901/* dbg("%s(): submitting interrupt urb", __func__); */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200902 urb->dev = port->serial->dev;
903 result = usb_submit_urb(urb, GFP_ATOMIC);
904 if (result != 0) {
905 dev_err(&urb->dev->dev,
906 "%s(): usb_submit_urb() failed with"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800907 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200908 }
909 }
910}
911
912static void oti6858_read_bulk_callback(struct urb *urb)
913{
Ming Leicdc97792008-02-24 18:41:47 +0800914 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200915 struct oti6858_private *priv = usb_get_serial_port_data(port);
916 struct tty_struct *tty;
917 unsigned char *data = urb->transfer_buffer;
918 unsigned long flags;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700919 int status = urb->status;
Alan Coxb0a239d2008-01-19 16:02:37 +0000920 int result;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200921
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700922 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800923 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200924
925 spin_lock_irqsave(&priv->lock, flags);
926 priv->flags.read_urb_in_use = 0;
927 spin_unlock_irqrestore(&priv->lock, flags);
928
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700929 if (status != 0) {
Alan Cox95da3102008-07-22 11:09:07 +0100930 if (!port->port.count) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800931 dbg("%s(): port is closed, exiting", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200932 return;
933 }
934 /*
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700935 if (status == -EPROTO) {
Alan Cox2a77c812008-07-22 11:15:36 +0100936 * PL2303 mysteriously fails with -EPROTO reschedule
937 the read *
938 dbg("%s - caught -EPROTO, resubmitting the urb",
939 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200940 result = usb_submit_urb(urb, GFP_ATOMIC);
941 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800942 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200943 return;
944 }
945 */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800946 dbg("%s(): unable to handle the error, exiting", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200947 return;
948 }
949
Alan Cox4a90f092008-10-13 10:39:46 +0100950 tty = tty_port_tty_get(&port->port);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200951 if (tty != NULL && urb->actual_length > 0) {
Alan Coxb0a239d2008-01-19 16:02:37 +0000952 tty_insert_flip_string(tty, data, urb->actual_length);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200953 tty_flip_buffer_push(tty);
954 }
Alan Cox4a90f092008-10-13 10:39:46 +0100955 tty_kref_put(tty);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200956
Alan Cox2a77c812008-07-22 11:15:36 +0100957 /* schedule the interrupt urb if we are still open */
Alan Cox95da3102008-07-22 11:09:07 +0100958 if (port->port.count != 0) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200959 port->interrupt_in_urb->dev = port->serial->dev;
960 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
961 if (result != 0) {
962 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800963 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200964 }
965 }
966}
967
968static void oti6858_write_bulk_callback(struct urb *urb)
969{
Ming Leicdc97792008-02-24 18:41:47 +0800970 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200971 struct oti6858_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700972 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200973 int result;
974
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700975 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800976 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200977
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700978 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200979 case 0:
980 /* success */
981 break;
982 case -ECONNRESET:
983 case -ENOENT:
984 case -ESHUTDOWN:
985 /* this urb is terminated, clean up */
986 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800987 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200988 priv->flags.write_urb_in_use = 0;
989 return;
990 default:
991 /* error in the urb, so we have to resubmit it */
992 dbg("%s(): nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800993 __func__, status);
994 dbg("%s(): overflow in write", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200995
996 port->write_urb->transfer_buffer_length = 1;
997 port->write_urb->dev = port->serial->dev;
998 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
999 if (result) {
1000 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001001 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001002 } else {
1003 return;
1004 }
1005 }
1006
1007 priv->flags.write_urb_in_use = 0;
1008
Alan Cox2a77c812008-07-22 11:15:36 +01001009 /* schedule the interrupt urb if we are still open */
Kees Lemmens49cdee02007-03-27 12:34:30 +02001010 port->interrupt_in_urb->dev = port->serial->dev;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001011 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001012 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1013 if (result != 0) {
1014 dev_err(&port->dev, "%s(): failed submitting int urb,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001015 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001016 }
1017}
1018
1019
1020/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001021 * oti6858_buf_alloc
Kees Lemmens49cdee02007-03-27 12:34:30 +02001022 *
1023 * Allocate a circular buffer and all associated memory.
1024 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001025static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001026{
Alan Coxb0a239d2008-01-19 16:02:37 +00001027 struct oti6858_buf *pb;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001028
1029 if (size == 0)
1030 return NULL;
1031
Alan Coxb0a239d2008-01-19 16:02:37 +00001032 pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001033 if (pb == NULL)
1034 return NULL;
1035
1036 pb->buf_buf = kmalloc(size, GFP_KERNEL);
1037 if (pb->buf_buf == NULL) {
1038 kfree(pb);
1039 return NULL;
1040 }
1041
1042 pb->buf_size = size;
1043 pb->buf_get = pb->buf_put = pb->buf_buf;
1044
1045 return pb;
1046}
1047
1048/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001049 * oti6858_buf_free
Kees Lemmens49cdee02007-03-27 12:34:30 +02001050 *
1051 * Free the buffer and all associated memory.
1052 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001053static void oti6858_buf_free(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001054{
1055 if (pb) {
1056 kfree(pb->buf_buf);
1057 kfree(pb);
1058 }
1059}
1060
1061/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001062 * oti6858_buf_clear
Kees Lemmens49cdee02007-03-27 12:34:30 +02001063 *
1064 * Clear out all data in the circular buffer.
1065 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001066static void oti6858_buf_clear(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001067{
1068 if (pb != NULL) {
1069 /* equivalent to a get of all data available */
1070 pb->buf_get = pb->buf_put;
1071 }
1072}
1073
1074/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001075 * oti6858_buf_data_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001076 *
1077 * Return the number of bytes of data available in the circular
1078 * buffer.
1079 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001080static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001081{
1082 if (pb == NULL)
1083 return 0;
Alan Cox2a77c812008-07-22 11:15:36 +01001084 return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001085}
1086
1087/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001088 * oti6858_buf_space_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001089 *
1090 * Return the number of bytes of space available in the circular
1091 * buffer.
1092 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001093static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001094{
1095 if (pb == NULL)
1096 return 0;
Alan Cox2a77c812008-07-22 11:15:36 +01001097 return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001098}
1099
1100/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001101 * oti6858_buf_put
Kees Lemmens49cdee02007-03-27 12:34:30 +02001102 *
1103 * Copy data data from a user buffer and put it into the circular buffer.
1104 * Restrict to the amount of space available.
1105 *
1106 * Return the number of bytes copied.
1107 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001108static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001109 unsigned int count)
1110{
1111 unsigned int len;
1112
1113 if (pb == NULL)
1114 return 0;
1115
Alan Coxb0a239d2008-01-19 16:02:37 +00001116 len = oti6858_buf_space_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001117 if (count > len)
1118 count = len;
1119
1120 if (count == 0)
1121 return 0;
1122
1123 len = pb->buf_buf + pb->buf_size - pb->buf_put;
1124 if (count > len) {
1125 memcpy(pb->buf_put, buf, len);
1126 memcpy(pb->buf_buf, buf+len, count - len);
1127 pb->buf_put = pb->buf_buf + count - len;
1128 } else {
1129 memcpy(pb->buf_put, buf, count);
1130 if (count < len)
1131 pb->buf_put += count;
1132 else /* count == len */
1133 pb->buf_put = pb->buf_buf;
1134 }
1135
1136 return count;
1137}
1138
1139/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001140 * oti6858_buf_get
Kees Lemmens49cdee02007-03-27 12:34:30 +02001141 *
1142 * Get data from the circular buffer and copy to the given buffer.
1143 * Restrict to the amount of data available.
1144 *
1145 * Return the number of bytes copied.
1146 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001147static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001148 unsigned int count)
1149{
1150 unsigned int len;
1151
1152 if (pb == NULL)
1153 return 0;
1154
Alan Coxb0a239d2008-01-19 16:02:37 +00001155 len = oti6858_buf_data_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001156 if (count > len)
1157 count = len;
1158
1159 if (count == 0)
1160 return 0;
1161
1162 len = pb->buf_buf + pb->buf_size - pb->buf_get;
1163 if (count > len) {
1164 memcpy(buf, pb->buf_get, len);
1165 memcpy(buf+len, pb->buf_buf, count - len);
1166 pb->buf_get = pb->buf_buf + count - len;
1167 } else {
1168 memcpy(buf, pb->buf_get, count);
1169 if (count < len)
1170 pb->buf_get += count;
1171 else /* count == len */
1172 pb->buf_get = pb->buf_buf;
1173 }
1174
1175 return count;
1176}
1177
1178/* module description and (de)initialization */
1179
1180static int __init oti6858_init(void)
1181{
1182 int retval;
1183
Alan Cox2a77c812008-07-22 11:15:36 +01001184 retval = usb_serial_register(&oti6858_device);
1185 if (retval == 0) {
1186 retval = usb_register(&oti6858_driver);
1187 if (retval)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001188 usb_serial_deregister(&oti6858_device);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001189 }
Kees Lemmens49cdee02007-03-27 12:34:30 +02001190 return retval;
1191}
1192
1193static void __exit oti6858_exit(void)
1194{
1195 usb_deregister(&oti6858_driver);
1196 usb_serial_deregister(&oti6858_device);
1197}
1198
1199module_init(oti6858_init);
1200module_exit(oti6858_exit);
1201
1202MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
1203MODULE_AUTHOR(OTI6858_AUTHOR);
1204MODULE_VERSION(OTI6858_VERSION);
1205MODULE_LICENSE("GPL");
1206
1207module_param(debug, bool, S_IRUGO | S_IWUSR);
1208MODULE_PARM_DESC(debug, "enable debug output");
1209