blob: 525d8523df95b5a832aac7106c912b3972c5e698 [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
Németh Márton7d40d7e2010-01-10 15:34:24 +010061static const struct usb_device_id id_table[] = {
Kees Lemmens49cdee02007-03-27 12:34:30 +020062 { 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;
Oliver Neukum40d28582009-10-07 18:07:10 +0200291 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200292 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
Bill Pemberton7d7917b2010-04-28 16:59:35 -0400298static void send_data(struct work_struct *work)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200299{
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;
Johan Hovoldd2126322009-12-28 23:01:56 +0100305 u8 *allow;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200306
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) {
Johan Hovoldd2126322009-12-28 23:01:56 +0100324 allow = kmalloc(1, GFP_KERNEL);
325 if (!allow) {
326 dev_err(&port->dev, "%s(): kmalloc failed\n",
327 __func__);
328 return;
329 }
Kees Lemmens49cdee02007-03-27 12:34:30 +0200330 result = usb_control_msg(port->serial->dev,
331 usb_rcvctrlpipe(port->serial->dev, 0),
332 OTI6858_REQ_T_CHECK_TXBUFF,
333 OTI6858_REQ_CHECK_TXBUFF,
Johan Hovoldd2126322009-12-28 23:01:56 +0100334 count, 0, allow, 1, 100);
335 if (result != 1 || *allow != 0)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200336 count = 0;
Johan Hovoldd2126322009-12-28 23:01:56 +0100337 kfree(allow);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200338 }
339
340 if (count == 0) {
341 priv->flags.write_urb_in_use = 0;
342
Harvey Harrison441b62c2008-03-03 16:08:34 -0800343 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200344 port->interrupt_in_urb->dev = port->serial->dev;
Oliver Neukum40d28582009-10-07 18:07:10 +0200345 result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200346 if (result != 0) {
347 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800348 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200349 }
350 return;
351 }
352
353 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000354 oti6858_buf_get(priv->buf, port->write_urb->transfer_buffer, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200355 spin_unlock_irqrestore(&priv->lock, flags);
356
357 port->write_urb->transfer_buffer_length = count;
358 port->write_urb->dev = port->serial->dev;
Oliver Neukum40d28582009-10-07 18:07:10 +0200359 result = usb_submit_urb(port->write_urb, GFP_NOIO);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200360 if (result != 0) {
361 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800362 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200363 priv->flags.write_urb_in_use = 0;
364 }
365
366 usb_serial_port_softint(port);
367}
368
369static int oti6858_startup(struct usb_serial *serial)
370{
Alan Cox2a77c812008-07-22 11:15:36 +0100371 struct usb_serial_port *port = serial->port[0];
372 struct oti6858_private *priv;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200373 int i;
374
375 for (i = 0; i < serial->num_ports; ++i) {
376 priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
377 if (!priv)
378 break;
Alan Coxb0a239d2008-01-19 16:02:37 +0000379 priv->buf = oti6858_buf_alloc(PL2303_BUF_SIZE);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200380 if (priv->buf == NULL) {
381 kfree(priv);
382 break;
383 }
384
385 spin_lock_init(&priv->lock);
386 init_waitqueue_head(&priv->intr_wait);
Alan Cox2a77c812008-07-22 11:15:36 +0100387/* INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
388/* INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200389 priv->port = port;
390 INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
391 INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
392
393 usb_set_serial_port_data(serial->port[i], priv);
394 }
395 if (i == serial->num_ports)
396 return 0;
397
398 for (--i; i >= 0; --i) {
399 priv = usb_get_serial_port_data(serial->port[i]);
Alan Coxb0a239d2008-01-19 16:02:37 +0000400 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200401 kfree(priv);
402 usb_set_serial_port_data(serial->port[i], NULL);
403 }
404 return -ENOMEM;
405}
406
Alan Cox95da3102008-07-22 11:09:07 +0100407static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200408 const unsigned char *buf, int count)
409{
410 struct oti6858_private *priv = usb_get_serial_port_data(port);
411 unsigned long flags;
412
Harvey Harrison441b62c2008-03-03 16:08:34 -0800413 dbg("%s(port = %d, count = %d)", __func__, port->number, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200414
415 if (!count)
416 return count;
417
418 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000419 count = oti6858_buf_put(priv->buf, buf, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200420 spin_unlock_irqrestore(&priv->lock, flags);
421
422 return count;
423}
424
Alan Cox95da3102008-07-22 11:09:07 +0100425static int oti6858_write_room(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200426{
Alan Cox95da3102008-07-22 11:09:07 +0100427 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200428 struct oti6858_private *priv = usb_get_serial_port_data(port);
429 int room = 0;
430 unsigned long flags;
431
Harvey Harrison441b62c2008-03-03 16:08:34 -0800432 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200433
434 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000435 room = oti6858_buf_space_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200436 spin_unlock_irqrestore(&priv->lock, flags);
437
438 return room;
439}
440
Alan Cox95da3102008-07-22 11:09:07 +0100441static int oti6858_chars_in_buffer(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200442{
Alan Cox95da3102008-07-22 11:09:07 +0100443 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200444 struct oti6858_private *priv = usb_get_serial_port_data(port);
445 int chars = 0;
446 unsigned long flags;
447
Harvey Harrison441b62c2008-03-03 16:08:34 -0800448 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200449
450 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000451 chars = oti6858_buf_data_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200452 spin_unlock_irqrestore(&priv->lock, flags);
453
454 return chars;
455}
456
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700457static void oti6858_init_termios(struct tty_struct *tty)
458{
459 *(tty->termios) = tty_std_termios;
460 tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
461 tty->termios->c_ispeed = 38400;
462 tty->termios->c_ospeed = 38400;
463}
464
Alan Cox95da3102008-07-22 11:09:07 +0100465static void oti6858_set_termios(struct tty_struct *tty,
466 struct usb_serial_port *port, struct ktermios *old_termios)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200467{
468 struct oti6858_private *priv = usb_get_serial_port_data(port);
469 unsigned long flags;
470 unsigned int cflag;
471 u8 frame_fmt, control;
Al Virofd05e722008-04-28 07:00:16 +0100472 __le16 divisor;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200473 int br;
474
Harvey Harrison441b62c2008-03-03 16:08:34 -0800475 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200476
Alan Cox95da3102008-07-22 11:09:07 +0100477 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800478 dbg("%s(): no tty structures", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200479 return;
480 }
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 Coxa509a7e2009-09-19 13:13:26 -0700574static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200575{
576 struct oti6858_private *priv = usb_get_serial_port_data(port);
577 struct ktermios tmp_termios;
578 struct usb_serial *serial = port->serial;
579 struct oti6858_control_pkt *buf;
580 unsigned long flags;
581 int result;
582
Harvey Harrison441b62c2008-03-03 16:08:34 -0800583 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200584
585 usb_clear_halt(serial->dev, port->write_urb->pipe);
586 usb_clear_halt(serial->dev, port->read_urb->pipe);
587
Alan Cox2a77c812008-07-22 11:15:36 +0100588 buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
589 if (buf == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800590 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200591 return -ENOMEM;
592 }
593
594 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
595 OTI6858_REQ_T_GET_STATUS,
596 OTI6858_REQ_GET_STATUS,
597 0, 0,
598 buf, OTI6858_CTRL_PKT_SIZE,
599 100);
600 if (result != OTI6858_CTRL_PKT_SIZE) {
601 /* assume default (after power-on reset) values */
602 buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
603 buf->frame_fmt = 0x03; /* 8N1 */
604 buf->something = 0x43;
605 buf->control = 0x4c; /* DTR, RTS */
606 buf->tx_status = 0x00;
607 buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
608 buf->rx_bytes_avail = 0x00;
609 }
610
611 spin_lock_irqsave(&priv->lock, flags);
612 memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
613 priv->pending_setup.divisor = buf->divisor;
614 priv->pending_setup.frame_fmt = buf->frame_fmt;
615 priv->pending_setup.control = buf->control;
616 spin_unlock_irqrestore(&priv->lock, flags);
617 kfree(buf);
618
Harvey Harrison441b62c2008-03-03 16:08:34 -0800619 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200620 port->interrupt_in_urb->dev = serial->dev;
621 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
622 if (result != 0) {
623 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800624 " with error %d\n", __func__, result);
Alan Cox335f8512009-06-11 12:26:29 +0100625 oti6858_close(port);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200626 return -EPROTO;
627 }
628
629 /* setup termios */
Alan Cox95da3102008-07-22 11:09:07 +0100630 if (tty)
631 oti6858_set_termios(tty, port, &tmp_termios);
Alan Cox335f8512009-06-11 12:26:29 +0100632 port->port.drain_delay = 256; /* FIXME: check the FIFO length */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200633 return 0;
634}
635
Alan Cox335f8512009-06-11 12:26:29 +0100636static void oti6858_close(struct usb_serial_port *port)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200637{
638 struct oti6858_private *priv = usb_get_serial_port_data(port);
639 unsigned long flags;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200640
Harvey Harrison441b62c2008-03-03 16:08:34 -0800641 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200642
Kees Lemmens49cdee02007-03-27 12:34:30 +0200643 spin_lock_irqsave(&priv->lock, flags);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200644 /* clear out any remaining data in the buffer */
Alan Coxb0a239d2008-01-19 16:02:37 +0000645 oti6858_buf_clear(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200646 spin_unlock_irqrestore(&priv->lock, flags);
647
Alan Cox335f8512009-06-11 12:26:29 +0100648 dbg("%s(): after buf_clear()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200649
650 /* cancel scheduled setup */
651 cancel_delayed_work(&priv->delayed_setup_work);
652 cancel_delayed_work(&priv->delayed_write_work);
653 flush_scheduled_work();
654
655 /* shutdown our urbs */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800656 dbg("%s(): shutting down urbs", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200657 usb_kill_urb(port->write_urb);
658 usb_kill_urb(port->read_urb);
659 usb_kill_urb(port->interrupt_in_urb);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200660}
661
Alan Cox95da3102008-07-22 11:09:07 +0100662static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200663 unsigned int set, unsigned int clear)
664{
Alan Cox95da3102008-07-22 11:09:07 +0100665 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200666 struct oti6858_private *priv = usb_get_serial_port_data(port);
667 unsigned long flags;
668 u8 control;
669
670 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800671 __func__, port->number, set, clear);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200672
673 if (!usb_get_intfdata(port->serial->interface))
674 return -ENODEV;
675
676 /* FIXME: check if this is correct (active high/low) */
677 spin_lock_irqsave(&priv->lock, flags);
678 control = priv->pending_setup.control;
679 if ((set & TIOCM_RTS) != 0)
680 control |= CONTROL_RTS_HIGH;
681 if ((set & TIOCM_DTR) != 0)
682 control |= CONTROL_DTR_HIGH;
683 if ((clear & TIOCM_RTS) != 0)
684 control &= ~CONTROL_RTS_HIGH;
685 if ((clear & TIOCM_DTR) != 0)
686 control &= ~CONTROL_DTR_HIGH;
687
Alan Cox2a77c812008-07-22 11:15:36 +0100688 if (control != priv->pending_setup.control)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200689 priv->pending_setup.control = control;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200690
Alan Cox2a77c812008-07-22 11:15:36 +0100691 spin_unlock_irqrestore(&priv->lock, flags);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200692 return 0;
693}
694
Alan Cox95da3102008-07-22 11:09:07 +0100695static int oti6858_tiocmget(struct tty_struct *tty, struct file *file)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200696{
Alan Cox95da3102008-07-22 11:09:07 +0100697 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200698 struct oti6858_private *priv = usb_get_serial_port_data(port);
699 unsigned long flags;
700 unsigned pin_state;
701 unsigned result = 0;
702
Harvey Harrison441b62c2008-03-03 16:08:34 -0800703 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200704
705 if (!usb_get_intfdata(port->serial->interface))
706 return -ENODEV;
707
708 spin_lock_irqsave(&priv->lock, flags);
709 pin_state = priv->status.pin_state & PIN_MASK;
710 spin_unlock_irqrestore(&priv->lock, flags);
711
712 /* FIXME: check if this is correct (active high/low) */
713 if ((pin_state & PIN_RTS) != 0)
714 result |= TIOCM_RTS;
715 if ((pin_state & PIN_CTS) != 0)
716 result |= TIOCM_CTS;
717 if ((pin_state & PIN_DSR) != 0)
718 result |= TIOCM_DSR;
719 if ((pin_state & PIN_DTR) != 0)
720 result |= TIOCM_DTR;
721 if ((pin_state & PIN_RI) != 0)
722 result |= TIOCM_RI;
723 if ((pin_state & PIN_DCD) != 0)
724 result |= TIOCM_CD;
725
Harvey Harrison441b62c2008-03-03 16:08:34 -0800726 dbg("%s() = 0x%08x", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200727
728 return result;
729}
730
731static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
732{
733 struct oti6858_private *priv = usb_get_serial_port_data(port);
734 unsigned long flags;
735 unsigned int prev, status;
736 unsigned int changed;
737
738 spin_lock_irqsave(&priv->lock, flags);
739 prev = priv->status.pin_state;
740 spin_unlock_irqrestore(&priv->lock, flags);
741
742 while (1) {
Alan Cox2a77c812008-07-22 11:15:36 +0100743 wait_event_interruptible(priv->intr_wait,
744 priv->status.pin_state != prev);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200745 if (signal_pending(current))
746 return -ERESTARTSYS;
747
748 spin_lock_irqsave(&priv->lock, flags);
749 status = priv->status.pin_state & PIN_MASK;
750 spin_unlock_irqrestore(&priv->lock, flags);
751
752 changed = prev ^ status;
753 /* FIXME: check if this is correct (active high/low) */
Alan Cox2a77c812008-07-22 11:15:36 +0100754 if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
755 ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
756 ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
757 ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
758 return 0;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200759 prev = status;
760 }
761
762 /* NOTREACHED */
763 return 0;
764}
765
Alan Cox95da3102008-07-22 11:09:07 +0100766static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200767 unsigned int cmd, unsigned long arg)
768{
Alan Cox95da3102008-07-22 11:09:07 +0100769 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200770
771 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800772 __func__, port->number, cmd, arg);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200773
774 switch (cmd) {
Alan Cox2a77c812008-07-22 11:15:36 +0100775 case TIOCMIWAIT:
776 dbg("%s(): TIOCMIWAIT", __func__);
777 return wait_modem_info(port, arg);
778 default:
779 dbg("%s(): 0x%04x not supported", __func__, cmd);
780 break;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200781 }
Kees Lemmens49cdee02007-03-27 12:34:30 +0200782 return -ENOIOCTLCMD;
783}
784
Kees Lemmens49cdee02007-03-27 12:34:30 +0200785
Alan Sternf9c99bb2009-06-02 11:53:55 -0400786static void oti6858_release(struct usb_serial *serial)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200787{
788 struct oti6858_private *priv;
789 int i;
790
Harvey Harrison441b62c2008-03-03 16:08:34 -0800791 dbg("%s()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200792
793 for (i = 0; i < serial->num_ports; ++i) {
794 priv = usb_get_serial_port_data(serial->port[i]);
795 if (priv) {
Alan Coxb0a239d2008-01-19 16:02:37 +0000796 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200797 kfree(priv);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200798 }
799 }
800}
801
802static void oti6858_read_int_callback(struct urb *urb)
803{
Ming Leicdc97792008-02-24 18:41:47 +0800804 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200805 struct oti6858_private *priv = usb_get_serial_port_data(port);
806 int transient = 0, can_recv = 0, resubmit = 1;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700807 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200808
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700809 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800810 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200811
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700812 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200813 case 0:
814 /* success */
815 break;
816 case -ECONNRESET:
817 case -ENOENT:
818 case -ESHUTDOWN:
819 /* this urb is terminated, clean up */
820 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800821 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200822 return;
823 default:
824 dbg("%s(): nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800825 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200826 break;
827 }
828
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700829 if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200830 struct oti6858_control_pkt *xs = urb->transfer_buffer;
831 unsigned long flags;
832
833 spin_lock_irqsave(&priv->lock, flags);
834
835 if (!priv->transient) {
836 if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
837 if (xs->rx_bytes_avail == 0) {
838 priv->transient = 4;
839 priv->setup_done = 0;
840 resubmit = 0;
841 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800842 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200843 schedule_delayed_work(&priv->delayed_setup_work, 0);
844 }
845 }
846 } else {
847 if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
848 priv->transient = 0;
849 } else if (!priv->setup_done) {
850 resubmit = 0;
851 } else if (--priv->transient == 0) {
852 if (xs->rx_bytes_avail == 0) {
853 priv->transient = 4;
854 priv->setup_done = 0;
855 resubmit = 0;
856 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800857 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200858 schedule_delayed_work(&priv->delayed_setup_work, 0);
859 }
860 }
861 }
862
863 if (!priv->transient) {
864 if (xs->pin_state != priv->status.pin_state)
865 wake_up_interruptible(&priv->intr_wait);
866 memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
867 }
868
869 if (!priv->transient && xs->rx_bytes_avail != 0) {
870 can_recv = xs->rx_bytes_avail;
871 priv->flags.read_urb_in_use = 1;
872 }
873
874 transient = priv->transient;
875 spin_unlock_irqrestore(&priv->lock, flags);
876 }
877
878 if (can_recv) {
879 int result;
880
881 port->read_urb->dev = port->serial->dev;
882 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
883 if (result != 0) {
884 priv->flags.read_urb_in_use = 0;
885 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800886 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200887 } else {
888 resubmit = 0;
889 }
890 } else if (!transient) {
891 unsigned long flags;
892
893 spin_lock_irqsave(&priv->lock, flags);
894 if (priv->flags.write_urb_in_use == 0
Alan Coxb0a239d2008-01-19 16:02:37 +0000895 && oti6858_buf_data_avail(priv->buf) != 0) {
Alan Cox2a77c812008-07-22 11:15:36 +0100896 schedule_delayed_work(&priv->delayed_write_work, 0);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200897 resubmit = 0;
898 }
899 spin_unlock_irqrestore(&priv->lock, flags);
900 }
901
902 if (resubmit) {
903 int result;
904
Alan Cox2a77c812008-07-22 11:15:36 +0100905/* dbg("%s(): submitting interrupt urb", __func__); */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200906 urb->dev = port->serial->dev;
907 result = usb_submit_urb(urb, GFP_ATOMIC);
908 if (result != 0) {
909 dev_err(&urb->dev->dev,
910 "%s(): usb_submit_urb() failed with"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800911 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200912 }
913 }
914}
915
916static void oti6858_read_bulk_callback(struct urb *urb)
917{
Ming Leicdc97792008-02-24 18:41:47 +0800918 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200919 struct oti6858_private *priv = usb_get_serial_port_data(port);
920 struct tty_struct *tty;
921 unsigned char *data = urb->transfer_buffer;
922 unsigned long flags;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700923 int status = urb->status;
Alan Coxb0a239d2008-01-19 16:02:37 +0000924 int result;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200925
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700926 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800927 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200928
929 spin_lock_irqsave(&priv->lock, flags);
930 priv->flags.read_urb_in_use = 0;
931 spin_unlock_irqrestore(&priv->lock, flags);
932
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700933 if (status != 0) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200934 /*
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 Stern1f871582010-02-17 10:05:47 -0500957 /* schedule the interrupt urb */
958 port->interrupt_in_urb->dev = port->serial->dev;
959 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
960 if (result != 0 && result != -EPERM) {
961 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
962 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200963 }
964}
965
966static void oti6858_write_bulk_callback(struct urb *urb)
967{
Ming Leicdc97792008-02-24 18:41:47 +0800968 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200969 struct oti6858_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700970 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200971 int result;
972
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700973 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800974 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200975
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700976 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200977 case 0:
978 /* success */
979 break;
980 case -ECONNRESET:
981 case -ENOENT:
982 case -ESHUTDOWN:
983 /* this urb is terminated, clean up */
984 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800985 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200986 priv->flags.write_urb_in_use = 0;
987 return;
988 default:
989 /* error in the urb, so we have to resubmit it */
990 dbg("%s(): nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800991 __func__, status);
992 dbg("%s(): overflow in write", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200993
994 port->write_urb->transfer_buffer_length = 1;
995 port->write_urb->dev = port->serial->dev;
996 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
997 if (result) {
998 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800999 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001000 } else {
1001 return;
1002 }
1003 }
1004
1005 priv->flags.write_urb_in_use = 0;
1006
Alan Cox2a77c812008-07-22 11:15:36 +01001007 /* schedule the interrupt urb if we are still open */
Kees Lemmens49cdee02007-03-27 12:34:30 +02001008 port->interrupt_in_urb->dev = port->serial->dev;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001009 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001010 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1011 if (result != 0) {
1012 dev_err(&port->dev, "%s(): failed submitting int urb,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001013 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001014 }
1015}
1016
1017
1018/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001019 * oti6858_buf_alloc
Kees Lemmens49cdee02007-03-27 12:34:30 +02001020 *
1021 * Allocate a circular buffer and all associated memory.
1022 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001023static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001024{
Alan Coxb0a239d2008-01-19 16:02:37 +00001025 struct oti6858_buf *pb;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001026
1027 if (size == 0)
1028 return NULL;
1029
Alan Coxb0a239d2008-01-19 16:02:37 +00001030 pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001031 if (pb == NULL)
1032 return NULL;
1033
1034 pb->buf_buf = kmalloc(size, GFP_KERNEL);
1035 if (pb->buf_buf == NULL) {
1036 kfree(pb);
1037 return NULL;
1038 }
1039
1040 pb->buf_size = size;
1041 pb->buf_get = pb->buf_put = pb->buf_buf;
1042
1043 return pb;
1044}
1045
1046/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001047 * oti6858_buf_free
Kees Lemmens49cdee02007-03-27 12:34:30 +02001048 *
1049 * Free the buffer and all associated memory.
1050 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001051static void oti6858_buf_free(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001052{
1053 if (pb) {
1054 kfree(pb->buf_buf);
1055 kfree(pb);
1056 }
1057}
1058
1059/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001060 * oti6858_buf_clear
Kees Lemmens49cdee02007-03-27 12:34:30 +02001061 *
1062 * Clear out all data in the circular buffer.
1063 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001064static void oti6858_buf_clear(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001065{
1066 if (pb != NULL) {
1067 /* equivalent to a get of all data available */
1068 pb->buf_get = pb->buf_put;
1069 }
1070}
1071
1072/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001073 * oti6858_buf_data_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001074 *
1075 * Return the number of bytes of data available in the circular
1076 * buffer.
1077 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001078static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001079{
1080 if (pb == NULL)
1081 return 0;
Alan Cox2a77c812008-07-22 11:15:36 +01001082 return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001083}
1084
1085/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001086 * oti6858_buf_space_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001087 *
1088 * Return the number of bytes of space available in the circular
1089 * buffer.
1090 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001091static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001092{
1093 if (pb == NULL)
1094 return 0;
Alan Cox2a77c812008-07-22 11:15:36 +01001095 return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001096}
1097
1098/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001099 * oti6858_buf_put
Kees Lemmens49cdee02007-03-27 12:34:30 +02001100 *
1101 * Copy data data from a user buffer and put it into the circular buffer.
1102 * Restrict to the amount of space available.
1103 *
1104 * Return the number of bytes copied.
1105 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001106static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001107 unsigned int count)
1108{
1109 unsigned int len;
1110
1111 if (pb == NULL)
1112 return 0;
1113
Alan Coxb0a239d2008-01-19 16:02:37 +00001114 len = oti6858_buf_space_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001115 if (count > len)
1116 count = len;
1117
1118 if (count == 0)
1119 return 0;
1120
1121 len = pb->buf_buf + pb->buf_size - pb->buf_put;
1122 if (count > len) {
1123 memcpy(pb->buf_put, buf, len);
1124 memcpy(pb->buf_buf, buf+len, count - len);
1125 pb->buf_put = pb->buf_buf + count - len;
1126 } else {
1127 memcpy(pb->buf_put, buf, count);
1128 if (count < len)
1129 pb->buf_put += count;
1130 else /* count == len */
1131 pb->buf_put = pb->buf_buf;
1132 }
1133
1134 return count;
1135}
1136
1137/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001138 * oti6858_buf_get
Kees Lemmens49cdee02007-03-27 12:34:30 +02001139 *
1140 * Get data from the circular buffer and copy to the given buffer.
1141 * Restrict to the amount of data available.
1142 *
1143 * Return the number of bytes copied.
1144 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001145static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001146 unsigned int count)
1147{
1148 unsigned int len;
1149
1150 if (pb == NULL)
1151 return 0;
1152
Alan Coxb0a239d2008-01-19 16:02:37 +00001153 len = oti6858_buf_data_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001154 if (count > len)
1155 count = len;
1156
1157 if (count == 0)
1158 return 0;
1159
1160 len = pb->buf_buf + pb->buf_size - pb->buf_get;
1161 if (count > len) {
1162 memcpy(buf, pb->buf_get, len);
1163 memcpy(buf+len, pb->buf_buf, count - len);
1164 pb->buf_get = pb->buf_buf + count - len;
1165 } else {
1166 memcpy(buf, pb->buf_get, count);
1167 if (count < len)
1168 pb->buf_get += count;
1169 else /* count == len */
1170 pb->buf_get = pb->buf_buf;
1171 }
1172
1173 return count;
1174}
1175
1176/* module description and (de)initialization */
1177
1178static int __init oti6858_init(void)
1179{
1180 int retval;
1181
Alan Cox2a77c812008-07-22 11:15:36 +01001182 retval = usb_serial_register(&oti6858_device);
1183 if (retval == 0) {
1184 retval = usb_register(&oti6858_driver);
1185 if (retval)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001186 usb_serial_deregister(&oti6858_device);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001187 }
Kees Lemmens49cdee02007-03-27 12:34:30 +02001188 return retval;
1189}
1190
1191static void __exit oti6858_exit(void)
1192{
1193 usb_deregister(&oti6858_driver);
1194 usb_serial_deregister(&oti6858_device);
1195}
1196
1197module_init(oti6858_init);
1198module_exit(oti6858_exit);
1199
1200MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
1201MODULE_AUTHOR(OTI6858_AUTHOR);
1202MODULE_VERSION(OTI6858_VERSION);
1203MODULE_LICENSE("GPL");
1204
1205module_param(debug, bool, S_IRUGO | S_IWUSR);
1206MODULE_PARM_DESC(debug, "enable debug output");
1207