blob: 069d276a5276f72a5697133b5022f9928b54cac0 [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 *
28 * See Documentation/usb/usb-serial.txt for more information on using this driver
29 *
30 * TODO:
31 * - implement correct flushing for ioctls and oti6858_close()
32 * - check how errors (rx overflow, parity error, framing error) are reported
33 * - implement oti6858_break_ctl()
34 * - implement more ioctls
35 * - test/implement flow control
36 * - allow setting custom baud rates
37 */
38
39#include <linux/kernel.h>
40#include <linux/errno.h>
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/tty.h>
44#include <linux/tty_driver.h>
45#include <linux/tty_flip.h>
46#include <linux/serial.h>
47#include <linux/module.h>
48#include <linux/moduleparam.h>
49#include <linux/spinlock.h>
50#include <linux/usb.h>
51#include <linux/usb/serial.h>
52#include <asm/uaccess.h>
53#include "oti6858.h"
54
55#define OTI6858_DESCRIPTION \
56 "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
57#define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
58#define OTI6858_VERSION "0.1"
59
60static struct usb_device_id id_table [] = {
61 { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
62 { }
63};
64
65MODULE_DEVICE_TABLE(usb, id_table);
66
67static struct usb_driver oti6858_driver = {
68 .name = "oti6858",
69 .probe = usb_serial_probe,
70 .disconnect = usb_serial_disconnect,
71 .id_table = id_table,
72 .no_dynamic_id = 1,
73};
74
75static int debug;
76
77
78/* buffering code, copied from pl2303 driver */
79#define PL2303_BUF_SIZE 1024
80#define PL2303_TMP_BUF_SIZE 1024
81
Alan Coxb0a239d2008-01-19 16:02:37 +000082struct oti6858_buf {
Kees Lemmens49cdee02007-03-27 12:34:30 +020083 unsigned int buf_size;
84 char *buf_buf;
85 char *buf_get;
86 char *buf_put;
87};
88
89/* requests */
90#define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
91#define OTI6858_REQ_T_GET_STATUS 0x01
92
93#define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
94#define OTI6858_REQ_T_SET_LINE 0x00
95
96#define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
97#define OTI6858_REQ_T_CHECK_TXBUFF 0x00
98
99/* format of the control packet */
100struct oti6858_control_pkt {
Al Virofd05e722008-04-28 07:00:16 +0100101 __le16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */
Kees Lemmens49cdee02007-03-27 12:34:30 +0200102#define OTI6858_MAX_BAUD_RATE 3000000
103 u8 frame_fmt;
104#define FMT_STOP_BITS_MASK 0xc0
105#define FMT_STOP_BITS_1 0x00
106#define FMT_STOP_BITS_2 0x40 /* 1.5 stop bits if FMT_DATA_BITS_5 */
107#define FMT_PARITY_MASK 0x38
108#define FMT_PARITY_NONE 0x00
109#define FMT_PARITY_ODD 0x08
110#define FMT_PARITY_EVEN 0x18
111#define FMT_PARITY_MARK 0x28
112#define FMT_PARITY_SPACE 0x38
113#define FMT_DATA_BITS_MASK 0x03
114#define FMT_DATA_BITS_5 0x00
115#define FMT_DATA_BITS_6 0x01
116#define FMT_DATA_BITS_7 0x02
117#define FMT_DATA_BITS_8 0x03
118 u8 something; /* always equals 0x43 */
119 u8 control; /* settings of flow control lines */
120#define CONTROL_MASK 0x0c
121#define CONTROL_DTR_HIGH 0x08
122#define CONTROL_RTS_HIGH 0x04
123 u8 tx_status;
124#define TX_BUFFER_EMPTIED 0x09
125 u8 pin_state;
126#define PIN_MASK 0x3f
127#define PIN_RTS 0x20 /* output pin */
128#define PIN_CTS 0x10 /* input pin, active low */
129#define PIN_DSR 0x08 /* input pin, active low */
130#define PIN_DTR 0x04 /* output pin */
131#define PIN_RI 0x02 /* input pin, active low */
132#define PIN_DCD 0x01 /* input pin, active low */
133 u8 rx_bytes_avail; /* number of bytes in rx buffer */;
134};
135
136#define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt)
137#define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
138 ( ((a)->divisor == (priv)->pending_setup.divisor) \
139 && ((a)->control == (priv)->pending_setup.control) \
140 && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt) )
141
142/* function prototypes */
Alan Cox95da3102008-07-22 11:09:07 +0100143static int oti6858_open(struct tty_struct *tty,
144 struct usb_serial_port *port, struct file *filp);
145static void oti6858_close(struct tty_struct *tty,
146 struct usb_serial_port *port, struct file *filp);
147static void oti6858_set_termios(struct tty_struct *tty,
148 struct usb_serial_port *port, struct ktermios *old);
149static 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);
162static void oti6858_shutdown(struct usb_serial *serial);
163
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,
189 .tiocmget = oti6858_tiocmget,
190 .tiocmset = oti6858_tiocmset,
191 .read_bulk_callback = oti6858_read_bulk_callback,
192 .read_int_callback = oti6858_read_int_callback,
193 .write_bulk_callback = oti6858_write_bulk_callback,
194 .write_room = oti6858_write_room,
195 .chars_in_buffer = oti6858_chars_in_buffer,
196 .attach = oti6858_startup,
197 .shutdown = oti6858_shutdown,
198};
199
200struct oti6858_private {
201 spinlock_t lock;
202
Alan Coxb0a239d2008-01-19 16:02:37 +0000203 struct oti6858_buf *buf;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200204 struct oti6858_control_pkt status;
205
206 struct {
207 u8 read_urb_in_use;
208 u8 write_urb_in_use;
209 u8 termios_initialized;
210 } 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;
223 struct usb_serial_port *port; /* USB port with which associated */
224};
225
226#undef dbg
227/* #define dbg(format, arg...) printk(KERN_INFO "%s: " format "\n", __FILE__, ## arg) */
228#define dbg(format, arg...) printk(KERN_INFO "" format "\n", ## arg)
229
230static void setup_line(struct work_struct *work)
231{
232 struct oti6858_private *priv = container_of(work, struct oti6858_private, delayed_setup_work.work);
233 struct usb_serial_port *port = priv->port;
234 struct oti6858_control_pkt *new_setup;
235 unsigned long flags;
236 int result;
237
Harvey Harrison441b62c2008-03-03 16:08:34 -0800238 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200239
240 if ((new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800241 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200242 /* we will try again */
243 schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
244 return;
245 }
246
247 result = usb_control_msg(port->serial->dev,
248 usb_rcvctrlpipe(port->serial->dev, 0),
249 OTI6858_REQ_T_GET_STATUS,
250 OTI6858_REQ_GET_STATUS,
251 0, 0,
252 new_setup, OTI6858_CTRL_PKT_SIZE,
253 100);
254
255 if (result != OTI6858_CTRL_PKT_SIZE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800256 dev_err(&port->dev, "%s(): error reading status\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200257 kfree(new_setup);
258 /* we will try again */
259 schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
260 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{
300 struct oti6858_private *priv = container_of(work, struct oti6858_private, delayed_write_work.work);
301 struct usb_serial_port *port = priv->port;
302 int count = 0, result;
303 unsigned long flags;
304 unsigned char allow;
305
Harvey Harrison441b62c2008-03-03 16:08:34 -0800306 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200307
308 spin_lock_irqsave(&priv->lock, flags);
309 if (priv->flags.write_urb_in_use) {
310 spin_unlock_irqrestore(&priv->lock, flags);
311 schedule_delayed_work(&priv->delayed_write_work, msecs_to_jiffies(2));
312 return;
313 }
314 priv->flags.write_urb_in_use = 1;
315
Alan Coxb0a239d2008-01-19 16:02:37 +0000316 count = oti6858_buf_data_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200317 spin_unlock_irqrestore(&priv->lock, flags);
318 if (count > port->bulk_out_size)
319 count = port->bulk_out_size;
320
321 if (count != 0) {
322 result = usb_control_msg(port->serial->dev,
323 usb_rcvctrlpipe(port->serial->dev, 0),
324 OTI6858_REQ_T_CHECK_TXBUFF,
325 OTI6858_REQ_CHECK_TXBUFF,
326 count, 0, &allow, 1, 100);
327 if (result != 1 || allow != 0)
328 count = 0;
329 }
330
331 if (count == 0) {
332 priv->flags.write_urb_in_use = 0;
333
Harvey Harrison441b62c2008-03-03 16:08:34 -0800334 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200335 port->interrupt_in_urb->dev = port->serial->dev;
336 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
337 if (result != 0) {
338 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800339 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200340 }
341 return;
342 }
343
344 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000345 oti6858_buf_get(priv->buf, port->write_urb->transfer_buffer, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200346 spin_unlock_irqrestore(&priv->lock, flags);
347
348 port->write_urb->transfer_buffer_length = count;
349 port->write_urb->dev = port->serial->dev;
350 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
351 if (result != 0) {
352 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800353 " with error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200354 priv->flags.write_urb_in_use = 0;
355 }
356
357 usb_serial_port_softint(port);
358}
359
360static int oti6858_startup(struct usb_serial *serial)
361{
362 struct usb_serial_port *port = serial->port[0];
363 struct oti6858_private *priv;
364 int i;
365
366 for (i = 0; i < serial->num_ports; ++i) {
367 priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
368 if (!priv)
369 break;
Alan Coxb0a239d2008-01-19 16:02:37 +0000370 priv->buf = oti6858_buf_alloc(PL2303_BUF_SIZE);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200371 if (priv->buf == NULL) {
372 kfree(priv);
373 break;
374 }
375
376 spin_lock_init(&priv->lock);
377 init_waitqueue_head(&priv->intr_wait);
378// INIT_WORK(&priv->setup_work, setup_line, serial->port[i]);
379// INIT_WORK(&priv->write_work, send_data, serial->port[i]);
380 priv->port = port;
381 INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
382 INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
383
384 usb_set_serial_port_data(serial->port[i], priv);
385 }
386 if (i == serial->num_ports)
387 return 0;
388
389 for (--i; i >= 0; --i) {
390 priv = usb_get_serial_port_data(serial->port[i]);
Alan Coxb0a239d2008-01-19 16:02:37 +0000391 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200392 kfree(priv);
393 usb_set_serial_port_data(serial->port[i], NULL);
394 }
395 return -ENOMEM;
396}
397
Alan Cox95da3102008-07-22 11:09:07 +0100398static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200399 const unsigned char *buf, int count)
400{
401 struct oti6858_private *priv = usb_get_serial_port_data(port);
402 unsigned long flags;
403
Harvey Harrison441b62c2008-03-03 16:08:34 -0800404 dbg("%s(port = %d, count = %d)", __func__, port->number, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200405
406 if (!count)
407 return count;
408
409 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000410 count = oti6858_buf_put(priv->buf, buf, count);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200411 spin_unlock_irqrestore(&priv->lock, flags);
412
413 return count;
414}
415
Alan Cox95da3102008-07-22 11:09:07 +0100416static int oti6858_write_room(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200417{
Alan Cox95da3102008-07-22 11:09:07 +0100418 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200419 struct oti6858_private *priv = usb_get_serial_port_data(port);
420 int room = 0;
421 unsigned long flags;
422
Harvey Harrison441b62c2008-03-03 16:08:34 -0800423 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200424
425 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000426 room = oti6858_buf_space_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200427 spin_unlock_irqrestore(&priv->lock, flags);
428
429 return room;
430}
431
Alan Cox95da3102008-07-22 11:09:07 +0100432static int oti6858_chars_in_buffer(struct tty_struct *tty)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200433{
Alan Cox95da3102008-07-22 11:09:07 +0100434 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200435 struct oti6858_private *priv = usb_get_serial_port_data(port);
436 int chars = 0;
437 unsigned long flags;
438
Harvey Harrison441b62c2008-03-03 16:08:34 -0800439 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200440
441 spin_lock_irqsave(&priv->lock, flags);
Alan Coxb0a239d2008-01-19 16:02:37 +0000442 chars = oti6858_buf_data_avail(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200443 spin_unlock_irqrestore(&priv->lock, flags);
444
445 return chars;
446}
447
Alan Cox95da3102008-07-22 11:09:07 +0100448static void oti6858_set_termios(struct tty_struct *tty,
449 struct usb_serial_port *port, struct ktermios *old_termios)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200450{
451 struct oti6858_private *priv = usb_get_serial_port_data(port);
452 unsigned long flags;
453 unsigned int cflag;
454 u8 frame_fmt, control;
Al Virofd05e722008-04-28 07:00:16 +0100455 __le16 divisor;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200456 int br;
457
Harvey Harrison441b62c2008-03-03 16:08:34 -0800458 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200459
Alan Cox95da3102008-07-22 11:09:07 +0100460 if (!tty) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800461 dbg("%s(): no tty structures", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200462 return;
463 }
464
465 spin_lock_irqsave(&priv->lock, flags);
466 if (!priv->flags.termios_initialized) {
Alan Cox95da3102008-07-22 11:09:07 +0100467 *(tty->termios) = tty_std_termios;
468 tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
469 tty->termios->c_ispeed = 38400;
470 tty->termios->c_ospeed = 38400;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200471 priv->flags.termios_initialized = 1;
472 }
473 spin_unlock_irqrestore(&priv->lock, flags);
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) {
485 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;
498 }
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;
520 if ((cflag & CSTOPB) != 0) {
521 frame_fmt |= FMT_STOP_BITS_2;
522 } else {
523 frame_fmt |= FMT_STOP_BITS_1;
524 }
525
526 frame_fmt &= ~FMT_PARITY_MASK;
527 if ((cflag & PARENB) != 0) {
528 if ((cflag & PARODD) != 0) {
529 frame_fmt |= FMT_PARITY_ODD;
530 } else {
531 frame_fmt |= FMT_PARITY_EVEN;
532 }
533 } else {
534 frame_fmt |= FMT_PARITY_NONE;
535 }
536
537 control &= ~CONTROL_MASK;
538 if ((cflag & CRTSCTS) != 0)
539 control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
540
541 /* change control lines if we are switching to or from B0 */
542 /* FIXME:
543 spin_lock_irqsave(&priv->lock, flags);
544 control = priv->line_control;
545 if ((cflag & CBAUD) == B0)
546 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
547 else
548 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
549 if (control != priv->line_control) {
550 control = priv->line_control;
551 spin_unlock_irqrestore(&priv->lock, flags);
552 set_control_lines(serial->dev, control);
553 } else {
554 spin_unlock_irqrestore(&priv->lock, flags);
555 }
556 */
557
558 spin_lock_irqsave(&priv->lock, flags);
559 if (divisor != priv->pending_setup.divisor
560 || control != priv->pending_setup.control
561 || frame_fmt != priv->pending_setup.frame_fmt) {
562 priv->pending_setup.divisor = divisor;
563 priv->pending_setup.control = control;
564 priv->pending_setup.frame_fmt = frame_fmt;
565 }
566 spin_unlock_irqrestore(&priv->lock, flags);
567}
568
Alan Cox95da3102008-07-22 11:09:07 +0100569static int oti6858_open(struct tty_struct *tty,
570 struct usb_serial_port *port, struct file *filp)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200571{
572 struct oti6858_private *priv = usb_get_serial_port_data(port);
573 struct ktermios tmp_termios;
574 struct usb_serial *serial = port->serial;
575 struct oti6858_control_pkt *buf;
576 unsigned long flags;
577 int result;
578
Harvey Harrison441b62c2008-03-03 16:08:34 -0800579 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200580
581 usb_clear_halt(serial->dev, port->write_urb->pipe);
582 usb_clear_halt(serial->dev, port->read_urb->pipe);
583
Alan Cox95da3102008-07-22 11:09:07 +0100584 if (port->port.count != 1)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200585 return 0;
586
587 if ((buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800588 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200589 return -ENOMEM;
590 }
591
592 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
593 OTI6858_REQ_T_GET_STATUS,
594 OTI6858_REQ_GET_STATUS,
595 0, 0,
596 buf, OTI6858_CTRL_PKT_SIZE,
597 100);
598 if (result != OTI6858_CTRL_PKT_SIZE) {
599 /* assume default (after power-on reset) values */
600 buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
601 buf->frame_fmt = 0x03; /* 8N1 */
602 buf->something = 0x43;
603 buf->control = 0x4c; /* DTR, RTS */
604 buf->tx_status = 0x00;
605 buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
606 buf->rx_bytes_avail = 0x00;
607 }
608
609 spin_lock_irqsave(&priv->lock, flags);
610 memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
611 priv->pending_setup.divisor = buf->divisor;
612 priv->pending_setup.frame_fmt = buf->frame_fmt;
613 priv->pending_setup.control = buf->control;
614 spin_unlock_irqrestore(&priv->lock, flags);
615 kfree(buf);
616
Harvey Harrison441b62c2008-03-03 16:08:34 -0800617 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200618 port->interrupt_in_urb->dev = serial->dev;
619 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
620 if (result != 0) {
621 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800622 " with error %d\n", __func__, result);
Alan Cox95da3102008-07-22 11:09:07 +0100623 oti6858_close(tty, port, NULL);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200624 return -EPROTO;
625 }
626
627 /* setup termios */
Alan Cox95da3102008-07-22 11:09:07 +0100628 if (tty)
629 oti6858_set_termios(tty, port, &tmp_termios);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200630
631 return 0;
632}
633
Alan Cox95da3102008-07-22 11:09:07 +0100634static void oti6858_close(struct tty_struct *tty,
635 struct usb_serial_port *port, struct file *filp)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200636{
637 struct oti6858_private *priv = usb_get_serial_port_data(port);
638 unsigned long flags;
639 long timeout;
640 wait_queue_t wait;
641
Harvey Harrison441b62c2008-03-03 16:08:34 -0800642 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200643
644 /* wait for data to drain from the buffer */
645 spin_lock_irqsave(&priv->lock, flags);
646 timeout = 30 * HZ; /* PL2303_CLOSING_WAIT */
647 init_waitqueue_entry(&wait, current);
Alan Cox95da3102008-07-22 11:09:07 +0100648 add_wait_queue(&tty->write_wait, &wait);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800649 dbg("%s(): entering wait loop", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200650 for (;;) {
651 set_current_state(TASK_INTERRUPTIBLE);
Alan Coxb0a239d2008-01-19 16:02:37 +0000652 if (oti6858_buf_data_avail(priv->buf) == 0
Kees Lemmens49cdee02007-03-27 12:34:30 +0200653 || timeout == 0 || signal_pending(current)
Oliver Neukum0915f492008-01-23 12:28:45 +0100654 || port->serial->disconnected)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200655 break;
656 spin_unlock_irqrestore(&priv->lock, flags);
657 timeout = schedule_timeout(timeout);
658 spin_lock_irqsave(&priv->lock, flags);
659 }
660 set_current_state(TASK_RUNNING);
Alan Cox95da3102008-07-22 11:09:07 +0100661 remove_wait_queue(&tty->write_wait, &wait);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800662 dbg("%s(): after wait loop", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200663
664 /* clear out any remaining data in the buffer */
Alan Coxb0a239d2008-01-19 16:02:37 +0000665 oti6858_buf_clear(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200666 spin_unlock_irqrestore(&priv->lock, flags);
667
668 /* wait for characters to drain from the device */
669 /* (this is long enough for the entire 256 byte */
670 /* pl2303 hardware buffer to drain with no flow */
671 /* control for data rates of 1200 bps or more, */
672 /* for lower rates we should really know how much */
673 /* data is in the buffer to compute a delay */
674 /* that is not unnecessarily long) */
675 /* FIXME
Alan Cox95da3102008-07-22 11:09:07 +0100676 bps = tty_get_baud_rate(tty);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200677 if (bps > 1200)
678 timeout = max((HZ*2560)/bps,HZ/10);
679 else
680 */
681 timeout = 2*HZ;
682 schedule_timeout_interruptible(timeout);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800683 dbg("%s(): after schedule_timeout_interruptible()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200684
685 /* cancel scheduled setup */
686 cancel_delayed_work(&priv->delayed_setup_work);
687 cancel_delayed_work(&priv->delayed_write_work);
688 flush_scheduled_work();
689
690 /* shutdown our urbs */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800691 dbg("%s(): shutting down urbs", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200692 usb_kill_urb(port->write_urb);
693 usb_kill_urb(port->read_urb);
694 usb_kill_urb(port->interrupt_in_urb);
695
696 /*
Alan Cox95da3102008-07-22 11:09:07 +0100697 if (tty && (tty->termios->c_cflag) & HUPCL) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200698 // drop DTR and RTS
699 spin_lock_irqsave(&priv->lock, flags);
700 priv->pending_setup.control &= ~CONTROL_MASK;
701 spin_unlock_irqrestore(&priv->lock, flags);
702 }
703 */
704}
705
Alan Cox95da3102008-07-22 11:09:07 +0100706static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200707 unsigned int set, unsigned int clear)
708{
Alan Cox95da3102008-07-22 11:09:07 +0100709 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200710 struct oti6858_private *priv = usb_get_serial_port_data(port);
711 unsigned long flags;
712 u8 control;
713
714 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800715 __func__, port->number, set, clear);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200716
717 if (!usb_get_intfdata(port->serial->interface))
718 return -ENODEV;
719
720 /* FIXME: check if this is correct (active high/low) */
721 spin_lock_irqsave(&priv->lock, flags);
722 control = priv->pending_setup.control;
723 if ((set & TIOCM_RTS) != 0)
724 control |= CONTROL_RTS_HIGH;
725 if ((set & TIOCM_DTR) != 0)
726 control |= CONTROL_DTR_HIGH;
727 if ((clear & TIOCM_RTS) != 0)
728 control &= ~CONTROL_RTS_HIGH;
729 if ((clear & TIOCM_DTR) != 0)
730 control &= ~CONTROL_DTR_HIGH;
731
732 if (control != priv->pending_setup.control) {
733 priv->pending_setup.control = control;
734 }
735 spin_unlock_irqrestore(&priv->lock, flags);
736
737 return 0;
738}
739
Alan Cox95da3102008-07-22 11:09:07 +0100740static int oti6858_tiocmget(struct tty_struct *tty, struct file *file)
Kees Lemmens49cdee02007-03-27 12:34:30 +0200741{
Alan Cox95da3102008-07-22 11:09:07 +0100742 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200743 struct oti6858_private *priv = usb_get_serial_port_data(port);
744 unsigned long flags;
745 unsigned pin_state;
746 unsigned result = 0;
747
Harvey Harrison441b62c2008-03-03 16:08:34 -0800748 dbg("%s(port = %d)", __func__, port->number);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200749
750 if (!usb_get_intfdata(port->serial->interface))
751 return -ENODEV;
752
753 spin_lock_irqsave(&priv->lock, flags);
754 pin_state = priv->status.pin_state & PIN_MASK;
755 spin_unlock_irqrestore(&priv->lock, flags);
756
757 /* FIXME: check if this is correct (active high/low) */
758 if ((pin_state & PIN_RTS) != 0)
759 result |= TIOCM_RTS;
760 if ((pin_state & PIN_CTS) != 0)
761 result |= TIOCM_CTS;
762 if ((pin_state & PIN_DSR) != 0)
763 result |= TIOCM_DSR;
764 if ((pin_state & PIN_DTR) != 0)
765 result |= TIOCM_DTR;
766 if ((pin_state & PIN_RI) != 0)
767 result |= TIOCM_RI;
768 if ((pin_state & PIN_DCD) != 0)
769 result |= TIOCM_CD;
770
Harvey Harrison441b62c2008-03-03 16:08:34 -0800771 dbg("%s() = 0x%08x", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200772
773 return result;
774}
775
776static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
777{
778 struct oti6858_private *priv = usb_get_serial_port_data(port);
779 unsigned long flags;
780 unsigned int prev, status;
781 unsigned int changed;
782
783 spin_lock_irqsave(&priv->lock, flags);
784 prev = priv->status.pin_state;
785 spin_unlock_irqrestore(&priv->lock, flags);
786
787 while (1) {
788 wait_event_interruptible(priv->intr_wait, priv->status.pin_state != prev);
789 if (signal_pending(current))
790 return -ERESTARTSYS;
791
792 spin_lock_irqsave(&priv->lock, flags);
793 status = priv->status.pin_state & PIN_MASK;
794 spin_unlock_irqrestore(&priv->lock, flags);
795
796 changed = prev ^ status;
797 /* FIXME: check if this is correct (active high/low) */
798 if ( ((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
799 ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
800 ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
801 ((arg & TIOCM_CTS) && (changed & PIN_CTS))) {
802 return 0;
803 }
804 prev = status;
805 }
806
807 /* NOTREACHED */
808 return 0;
809}
810
Alan Cox95da3102008-07-22 11:09:07 +0100811static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
Kees Lemmens49cdee02007-03-27 12:34:30 +0200812 unsigned int cmd, unsigned long arg)
813{
Alan Cox95da3102008-07-22 11:09:07 +0100814 struct usb_serial_port *port = tty->driver_data;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200815
816 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800817 __func__, port->number, cmd, arg);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200818
819 switch (cmd) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200820 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800821 dbg("%s(): TIOCMIWAIT", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200822 return wait_modem_info(port, arg);
823
824 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800825 dbg("%s(): 0x%04x not supported", __func__, cmd);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200826 break;
827 }
828
829 return -ENOIOCTLCMD;
830}
831
Kees Lemmens49cdee02007-03-27 12:34:30 +0200832
833static void oti6858_shutdown(struct usb_serial *serial)
834{
835 struct oti6858_private *priv;
836 int i;
837
Harvey Harrison441b62c2008-03-03 16:08:34 -0800838 dbg("%s()", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200839
840 for (i = 0; i < serial->num_ports; ++i) {
841 priv = usb_get_serial_port_data(serial->port[i]);
842 if (priv) {
Alan Coxb0a239d2008-01-19 16:02:37 +0000843 oti6858_buf_free(priv->buf);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200844 kfree(priv);
845 usb_set_serial_port_data(serial->port[i], NULL);
846 }
847 }
848}
849
850static void oti6858_read_int_callback(struct urb *urb)
851{
Ming Leicdc97792008-02-24 18:41:47 +0800852 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200853 struct oti6858_private *priv = usb_get_serial_port_data(port);
854 int transient = 0, can_recv = 0, resubmit = 1;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700855 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200856
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700857 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800858 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200859
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700860 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200861 case 0:
862 /* success */
863 break;
864 case -ECONNRESET:
865 case -ENOENT:
866 case -ESHUTDOWN:
867 /* this urb is terminated, clean up */
868 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800869 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200870 return;
871 default:
872 dbg("%s(): nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800873 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200874 break;
875 }
876
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700877 if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200878 struct oti6858_control_pkt *xs = urb->transfer_buffer;
879 unsigned long flags;
880
881 spin_lock_irqsave(&priv->lock, flags);
882
883 if (!priv->transient) {
884 if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
885 if (xs->rx_bytes_avail == 0) {
886 priv->transient = 4;
887 priv->setup_done = 0;
888 resubmit = 0;
889 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800890 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200891 schedule_delayed_work(&priv->delayed_setup_work, 0);
892 }
893 }
894 } else {
895 if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
896 priv->transient = 0;
897 } else if (!priv->setup_done) {
898 resubmit = 0;
899 } else if (--priv->transient == 0) {
900 if (xs->rx_bytes_avail == 0) {
901 priv->transient = 4;
902 priv->setup_done = 0;
903 resubmit = 0;
904 dbg("%s(): scheduling setup_line()",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800905 __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200906 schedule_delayed_work(&priv->delayed_setup_work, 0);
907 }
908 }
909 }
910
911 if (!priv->transient) {
912 if (xs->pin_state != priv->status.pin_state)
913 wake_up_interruptible(&priv->intr_wait);
914 memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
915 }
916
917 if (!priv->transient && xs->rx_bytes_avail != 0) {
918 can_recv = xs->rx_bytes_avail;
919 priv->flags.read_urb_in_use = 1;
920 }
921
922 transient = priv->transient;
923 spin_unlock_irqrestore(&priv->lock, flags);
924 }
925
926 if (can_recv) {
927 int result;
928
929 port->read_urb->dev = port->serial->dev;
930 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
931 if (result != 0) {
932 priv->flags.read_urb_in_use = 0;
933 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800934 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200935 } else {
936 resubmit = 0;
937 }
938 } else if (!transient) {
939 unsigned long flags;
940
941 spin_lock_irqsave(&priv->lock, flags);
942 if (priv->flags.write_urb_in_use == 0
Alan Coxb0a239d2008-01-19 16:02:37 +0000943 && oti6858_buf_data_avail(priv->buf) != 0) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200944 schedule_delayed_work(&priv->delayed_write_work,0);
945 resubmit = 0;
946 }
947 spin_unlock_irqrestore(&priv->lock, flags);
948 }
949
950 if (resubmit) {
951 int result;
952
Harvey Harrison441b62c2008-03-03 16:08:34 -0800953// dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200954 urb->dev = port->serial->dev;
955 result = usb_submit_urb(urb, GFP_ATOMIC);
956 if (result != 0) {
957 dev_err(&urb->dev->dev,
958 "%s(): usb_submit_urb() failed with"
Harvey Harrison441b62c2008-03-03 16:08:34 -0800959 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200960 }
961 }
962}
963
964static void oti6858_read_bulk_callback(struct urb *urb)
965{
Ming Leicdc97792008-02-24 18:41:47 +0800966 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200967 struct oti6858_private *priv = usb_get_serial_port_data(port);
968 struct tty_struct *tty;
969 unsigned char *data = urb->transfer_buffer;
970 unsigned long flags;
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700971 int status = urb->status;
Alan Coxb0a239d2008-01-19 16:02:37 +0000972 int result;
Kees Lemmens49cdee02007-03-27 12:34:30 +0200973
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700974 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800975 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200976
977 spin_lock_irqsave(&priv->lock, flags);
978 priv->flags.read_urb_in_use = 0;
979 spin_unlock_irqrestore(&priv->lock, flags);
980
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700981 if (status != 0) {
Alan Cox95da3102008-07-22 11:09:07 +0100982 if (!port->port.count) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800983 dbg("%s(): port is closed, exiting", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200984 return;
985 }
986 /*
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -0700987 if (status == -EPROTO) {
Kees Lemmens49cdee02007-03-27 12:34:30 +0200988 // PL2303 mysteriously fails with -EPROTO reschedule the read
Harvey Harrison441b62c2008-03-03 16:08:34 -0800989 dbg("%s - caught -EPROTO, resubmitting the urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200990 result = usb_submit_urb(urb, GFP_ATOMIC);
991 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800992 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200993 return;
994 }
995 */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800996 dbg("%s(): unable to handle the error, exiting", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +0200997 return;
998 }
999
Alan Cox95da3102008-07-22 11:09:07 +01001000 tty = port->port.tty;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001001 if (tty != NULL && urb->actual_length > 0) {
Alan Coxb0a239d2008-01-19 16:02:37 +00001002 tty_insert_flip_string(tty, data, urb->actual_length);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001003 tty_flip_buffer_push(tty);
1004 }
1005
1006 // schedule the interrupt urb if we are still open */
Alan Cox95da3102008-07-22 11:09:07 +01001007 if (port->port.count != 0) {
Kees Lemmens49cdee02007-03-27 12:34:30 +02001008 port->interrupt_in_urb->dev = port->serial->dev;
1009 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1010 if (result != 0) {
1011 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001012 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001013 }
1014 }
1015}
1016
1017static void oti6858_write_bulk_callback(struct urb *urb)
1018{
Ming Leicdc97792008-02-24 18:41:47 +08001019 struct usb_serial_port *port = urb->context;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001020 struct oti6858_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -07001021 int status = urb->status;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001022 int result;
1023
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -07001024 dbg("%s(port = %d, status = %d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001025 __func__, port->number, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001026
Greg Kroah-Hartman78c26ae2007-06-15 15:44:13 -07001027 switch (status) {
Kees Lemmens49cdee02007-03-27 12:34:30 +02001028 case 0:
1029 /* success */
1030 break;
1031 case -ECONNRESET:
1032 case -ENOENT:
1033 case -ESHUTDOWN:
1034 /* this urb is terminated, clean up */
1035 dbg("%s(): urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001036 __func__, status);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001037 priv->flags.write_urb_in_use = 0;
1038 return;
1039 default:
1040 /* error in the urb, so we have to resubmit it */
1041 dbg("%s(): nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001042 __func__, status);
1043 dbg("%s(): overflow in write", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001044
1045 port->write_urb->transfer_buffer_length = 1;
1046 port->write_urb->dev = port->serial->dev;
1047 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1048 if (result) {
1049 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001050 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001051 } else {
1052 return;
1053 }
1054 }
1055
1056 priv->flags.write_urb_in_use = 0;
1057
1058 // schedule the interrupt urb if we are still open */
1059 port->interrupt_in_urb->dev = port->serial->dev;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001060 dbg("%s(): submitting interrupt urb", __func__);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001061 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1062 if (result != 0) {
1063 dev_err(&port->dev, "%s(): failed submitting int urb,"
Harvey Harrison441b62c2008-03-03 16:08:34 -08001064 " error %d\n", __func__, result);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001065 }
1066}
1067
1068
1069/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001070 * oti6858_buf_alloc
Kees Lemmens49cdee02007-03-27 12:34:30 +02001071 *
1072 * Allocate a circular buffer and all associated memory.
1073 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001074static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001075{
Alan Coxb0a239d2008-01-19 16:02:37 +00001076 struct oti6858_buf *pb;
Kees Lemmens49cdee02007-03-27 12:34:30 +02001077
1078 if (size == 0)
1079 return NULL;
1080
Alan Coxb0a239d2008-01-19 16:02:37 +00001081 pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001082 if (pb == NULL)
1083 return NULL;
1084
1085 pb->buf_buf = kmalloc(size, GFP_KERNEL);
1086 if (pb->buf_buf == NULL) {
1087 kfree(pb);
1088 return NULL;
1089 }
1090
1091 pb->buf_size = size;
1092 pb->buf_get = pb->buf_put = pb->buf_buf;
1093
1094 return pb;
1095}
1096
1097/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001098 * oti6858_buf_free
Kees Lemmens49cdee02007-03-27 12:34:30 +02001099 *
1100 * Free the buffer and all associated memory.
1101 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001102static void oti6858_buf_free(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001103{
1104 if (pb) {
1105 kfree(pb->buf_buf);
1106 kfree(pb);
1107 }
1108}
1109
1110/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001111 * oti6858_buf_clear
Kees Lemmens49cdee02007-03-27 12:34:30 +02001112 *
1113 * Clear out all data in the circular buffer.
1114 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001115static void oti6858_buf_clear(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001116{
1117 if (pb != NULL) {
1118 /* equivalent to a get of all data available */
1119 pb->buf_get = pb->buf_put;
1120 }
1121}
1122
1123/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001124 * oti6858_buf_data_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001125 *
1126 * Return the number of bytes of data available in the circular
1127 * buffer.
1128 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001129static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001130{
1131 if (pb == NULL)
1132 return 0;
1133 return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size);
1134}
1135
1136/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001137 * oti6858_buf_space_avail
Kees Lemmens49cdee02007-03-27 12:34:30 +02001138 *
1139 * Return the number of bytes of space available in the circular
1140 * buffer.
1141 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001142static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
Kees Lemmens49cdee02007-03-27 12:34:30 +02001143{
1144 if (pb == NULL)
1145 return 0;
1146 return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size);
1147}
1148
1149/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001150 * oti6858_buf_put
Kees Lemmens49cdee02007-03-27 12:34:30 +02001151 *
1152 * Copy data data from a user buffer and put it into the circular buffer.
1153 * Restrict to the amount of space available.
1154 *
1155 * Return the number of bytes copied.
1156 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001157static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001158 unsigned int count)
1159{
1160 unsigned int len;
1161
1162 if (pb == NULL)
1163 return 0;
1164
Alan Coxb0a239d2008-01-19 16:02:37 +00001165 len = oti6858_buf_space_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001166 if (count > len)
1167 count = len;
1168
1169 if (count == 0)
1170 return 0;
1171
1172 len = pb->buf_buf + pb->buf_size - pb->buf_put;
1173 if (count > len) {
1174 memcpy(pb->buf_put, buf, len);
1175 memcpy(pb->buf_buf, buf+len, count - len);
1176 pb->buf_put = pb->buf_buf + count - len;
1177 } else {
1178 memcpy(pb->buf_put, buf, count);
1179 if (count < len)
1180 pb->buf_put += count;
1181 else /* count == len */
1182 pb->buf_put = pb->buf_buf;
1183 }
1184
1185 return count;
1186}
1187
1188/*
Alan Coxb0a239d2008-01-19 16:02:37 +00001189 * oti6858_buf_get
Kees Lemmens49cdee02007-03-27 12:34:30 +02001190 *
1191 * Get data from the circular buffer and copy to the given buffer.
1192 * Restrict to the amount of data available.
1193 *
1194 * Return the number of bytes copied.
1195 */
Alan Coxb0a239d2008-01-19 16:02:37 +00001196static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
Kees Lemmens49cdee02007-03-27 12:34:30 +02001197 unsigned int count)
1198{
1199 unsigned int len;
1200
1201 if (pb == NULL)
1202 return 0;
1203
Alan Coxb0a239d2008-01-19 16:02:37 +00001204 len = oti6858_buf_data_avail(pb);
Kees Lemmens49cdee02007-03-27 12:34:30 +02001205 if (count > len)
1206 count = len;
1207
1208 if (count == 0)
1209 return 0;
1210
1211 len = pb->buf_buf + pb->buf_size - pb->buf_get;
1212 if (count > len) {
1213 memcpy(buf, pb->buf_get, len);
1214 memcpy(buf+len, pb->buf_buf, count - len);
1215 pb->buf_get = pb->buf_buf + count - len;
1216 } else {
1217 memcpy(buf, pb->buf_get, count);
1218 if (count < len)
1219 pb->buf_get += count;
1220 else /* count == len */
1221 pb->buf_get = pb->buf_buf;
1222 }
1223
1224 return count;
1225}
1226
1227/* module description and (de)initialization */
1228
1229static int __init oti6858_init(void)
1230{
1231 int retval;
1232
1233 if ((retval = usb_serial_register(&oti6858_device)) == 0) {
1234 if ((retval = usb_register(&oti6858_driver)) != 0)
1235 usb_serial_deregister(&oti6858_device);
1236 else
1237 return 0;
1238 }
1239
1240 return retval;
1241}
1242
1243static void __exit oti6858_exit(void)
1244{
1245 usb_deregister(&oti6858_driver);
1246 usb_serial_deregister(&oti6858_device);
1247}
1248
1249module_init(oti6858_init);
1250module_exit(oti6858_exit);
1251
1252MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
1253MODULE_AUTHOR(OTI6858_AUTHOR);
1254MODULE_VERSION(OTI6858_VERSION);
1255MODULE_LICENSE("GPL");
1256
1257module_param(debug, bool, S_IRUGO | S_IWUSR);
1258MODULE_PARM_DESC(debug, "enable debug output");
1259