1 /*
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
29 * driver
30 *
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>
53 #include <linux/uaccess.h>
54 #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
61 static struct usb_device_id id_table [] = {
62 { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
63 { }
64 };
65
66 MODULE_DEVICE_TABLE(usb, id_table);
67
68 static 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
76 static 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
83 struct oti6858_buf {
84 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 */
101 struct oti6858_control_pkt {
102 __le16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */
103 #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) \
139 (((a)->divisor == (priv)->pending_setup.divisor) \
140 && ((a)->control == (priv)->pending_setup.control) \
141 && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
142
143 /* function prototypes */
144 static int oti6858_open(struct tty_struct *tty,
145 struct usb_serial_port *port, struct file *filp);
146 static void oti6858_close(struct usb_serial_port *port);
147 static void oti6858_set_termios(struct tty_struct *tty,
148 struct usb_serial_port *port, struct ktermios *old);
149 static void oti6858_init_termios(struct tty_struct *tty);
150 static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
151 unsigned int cmd, unsigned long arg);
152 static void oti6858_read_int_callback(struct urb *urb);
153 static void oti6858_read_bulk_callback(struct urb *urb);
154 static void oti6858_write_bulk_callback(struct urb *urb);
155 static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
156 const unsigned char *buf, int count);
157 static int oti6858_write_room(struct tty_struct *tty);
158 static int oti6858_chars_in_buffer(struct tty_struct *tty);
159 static int oti6858_tiocmget(struct tty_struct *tty, struct file *file);
160 static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
161 unsigned int set, unsigned int clear);
162 static int oti6858_startup(struct usb_serial *serial);
163 static void oti6858_release(struct usb_serial *serial);
164
165 /* functions operating on buffers */
166 static struct oti6858_buf *oti6858_buf_alloc(unsigned int size);
167 static void oti6858_buf_free(struct oti6858_buf *pb);
168 static void oti6858_buf_clear(struct oti6858_buf *pb);
169 static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb);
170 static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb);
171 static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
172 unsigned int count);
173 static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
174 unsigned int count);
175
176
177 /* device info */
178 static struct usb_serial_driver oti6858_device = {
179 .driver = {
180 .owner = THIS_MODULE,
181 .name = "oti6858",
182 },
183 .id_table = id_table,
184 .num_ports = 1,
185 .open = oti6858_open,
186 .close = oti6858_close,
187 .write = oti6858_write,
188 .ioctl = oti6858_ioctl,
189 .set_termios = oti6858_set_termios,
190 .init_termios = oti6858_init_termios,
191 .tiocmget = oti6858_tiocmget,
192 .tiocmset = oti6858_tiocmset,
193 .read_bulk_callback = oti6858_read_bulk_callback,
194 .read_int_callback = oti6858_read_int_callback,
195 .write_bulk_callback = oti6858_write_bulk_callback,
196 .write_room = oti6858_write_room,
197 .chars_in_buffer = oti6858_chars_in_buffer,
198 .attach = oti6858_startup,
199 .release = oti6858_release,
200 };
201
202 struct oti6858_private {
203 spinlock_t lock;
204
205 struct oti6858_buf *buf;
206 struct oti6858_control_pkt status;
207
208 struct {
209 u8 read_urb_in_use;
210 u8 write_urb_in_use;
211 } flags;
212 struct delayed_work delayed_write_work;
213
214 struct {
215 __le16 divisor;
216 u8 frame_fmt;
217 u8 control;
218 } pending_setup;
219 u8 transient;
220 u8 setup_done;
221 struct delayed_work delayed_setup_work;
222
223 wait_queue_head_t intr_wait;
224 struct usb_serial_port *port; /* USB port with which associated */
225 };
226
227 static void setup_line(struct work_struct *work)
228 {
229 struct oti6858_private *priv = container_of(work,
230 struct oti6858_private, delayed_setup_work.work);
231 struct usb_serial_port *port = priv->port;
232 struct oti6858_control_pkt *new_setup;
233 unsigned long flags;
234 int result;
235
236 dbg("%s(port = %d)", __func__, port->number);
237
238 new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
239 if (new_setup == NULL) {
240 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
241 /* we will try again */
242 schedule_delayed_work(&priv->delayed_setup_work,
243 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) {
256 dev_err(&port->dev, "%s(): error reading status\n", __func__);
257 kfree(new_setup);
258 /* we will try again */
259 schedule_delayed_work(&priv->delayed_setup_work,
260 msecs_to_jiffies(2));
261 return;
262 }
263
264 spin_lock_irqsave(&priv->lock, flags);
265 if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
266 new_setup->divisor = priv->pending_setup.divisor;
267 new_setup->control = priv->pending_setup.control;
268 new_setup->frame_fmt = priv->pending_setup.frame_fmt;
269
270 spin_unlock_irqrestore(&priv->lock, flags);
271 result = usb_control_msg(port->serial->dev,
272 usb_sndctrlpipe(port->serial->dev, 0),
273 OTI6858_REQ_T_SET_LINE,
274 OTI6858_REQ_SET_LINE,
275 0, 0,
276 new_setup, OTI6858_CTRL_PKT_SIZE,
277 100);
278 } else {
279 spin_unlock_irqrestore(&priv->lock, flags);
280 result = 0;
281 }
282 kfree(new_setup);
283
284 spin_lock_irqsave(&priv->lock, flags);
285 if (result != OTI6858_CTRL_PKT_SIZE)
286 priv->transient = 0;
287 priv->setup_done = 1;
288 spin_unlock_irqrestore(&priv->lock, flags);
289
290 dbg("%s(): submitting interrupt urb", __func__);
291 port->interrupt_in_urb->dev = port->serial->dev;
292 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
293 if (result != 0) {
294 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
295 " with error %d\n", __func__, result);
296 }
297 }
298
299 void send_data(struct work_struct *work)
300 {
301 struct oti6858_private *priv = container_of(work,
302 struct oti6858_private, delayed_write_work.work);
303 struct usb_serial_port *port = priv->port;
304 int count = 0, result;
305 unsigned long flags;
306 unsigned char allow;
307
308 dbg("%s(port = %d)", __func__, port->number);
309
310 spin_lock_irqsave(&priv->lock, flags);
311 if (priv->flags.write_urb_in_use) {
312 spin_unlock_irqrestore(&priv->lock, flags);
313 schedule_delayed_work(&priv->delayed_write_work,
314 msecs_to_jiffies(2));
315 return;
316 }
317 priv->flags.write_urb_in_use = 1;
318
319 count = oti6858_buf_data_avail(priv->buf);
320 spin_unlock_irqrestore(&priv->lock, flags);
321 if (count > port->bulk_out_size)
322 count = port->bulk_out_size;
323
324 if (count != 0) {
325 result = usb_control_msg(port->serial->dev,
326 usb_rcvctrlpipe(port->serial->dev, 0),
327 OTI6858_REQ_T_CHECK_TXBUFF,
328 OTI6858_REQ_CHECK_TXBUFF,
329 count, 0, &allow, 1, 100);
330 if (result != 1 || allow != 0)
331 count = 0;
332 }
333
334 if (count == 0) {
335 priv->flags.write_urb_in_use = 0;
336
337 dbg("%s(): submitting interrupt urb", __func__);
338 port->interrupt_in_urb->dev = port->serial->dev;
339 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
340 if (result != 0) {
341 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
342 " with error %d\n", __func__, result);
343 }
344 return;
345 }
346
347 spin_lock_irqsave(&priv->lock, flags);
348 oti6858_buf_get(priv->buf, port->write_urb->transfer_buffer, count);
349 spin_unlock_irqrestore(&priv->lock, flags);
350
351 port->write_urb->transfer_buffer_length = count;
352 port->write_urb->dev = port->serial->dev;
353 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
354 if (result != 0) {
355 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
356 " with error %d\n", __func__, result);
357 priv->flags.write_urb_in_use = 0;
358 }
359
360 usb_serial_port_softint(port);
361 }
362
363 static int oti6858_startup(struct usb_serial *serial)
364 {
365 struct usb_serial_port *port = serial->port[0];
366 struct oti6858_private *priv;
367 int i;
368
369 for (i = 0; i < serial->num_ports; ++i) {
370 priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
371 if (!priv)
372 break;
373 priv->buf = oti6858_buf_alloc(PL2303_BUF_SIZE);
374 if (priv->buf == NULL) {
375 kfree(priv);
376 break;
377 }
378
379 spin_lock_init(&priv->lock);
380 init_waitqueue_head(&priv->intr_wait);
381 /* INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
382 /* INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
383 priv->port = port;
384 INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
385 INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
386
387 usb_set_serial_port_data(serial->port[i], priv);
388 }
389 if (i == serial->num_ports)
390 return 0;
391
392 for (--i; i >= 0; --i) {
393 priv = usb_get_serial_port_data(serial->port[i]);
394 oti6858_buf_free(priv->buf);
395 kfree(priv);
396 usb_set_serial_port_data(serial->port[i], NULL);
397 }
398 return -ENOMEM;
399 }
400
401 static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
402 const unsigned char *buf, int count)
403 {
404 struct oti6858_private *priv = usb_get_serial_port_data(port);
405 unsigned long flags;
406
407 dbg("%s(port = %d, count = %d)", __func__, port->number, count);
408
409 if (!count)
410 return count;
411
412 spin_lock_irqsave(&priv->lock, flags);
413 count = oti6858_buf_put(priv->buf, buf, count);
414 spin_unlock_irqrestore(&priv->lock, flags);
415
416 return count;
417 }
418
419 static int oti6858_write_room(struct tty_struct *tty)
420 {
421 struct usb_serial_port *port = tty->driver_data;
422 struct oti6858_private *priv = usb_get_serial_port_data(port);
423 int room = 0;
424 unsigned long flags;
425
426 dbg("%s(port = %d)", __func__, port->number);
427
428 spin_lock_irqsave(&priv->lock, flags);
429 room = oti6858_buf_space_avail(priv->buf);
430 spin_unlock_irqrestore(&priv->lock, flags);
431
432 return room;
433 }
434
435 static int oti6858_chars_in_buffer(struct tty_struct *tty)
436 {
437 struct usb_serial_port *port = tty->driver_data;
438 struct oti6858_private *priv = usb_get_serial_port_data(port);
439 int chars = 0;
440 unsigned long flags;
441
442 dbg("%s(port = %d)", __func__, port->number);
443
444 spin_lock_irqsave(&priv->lock, flags);
445 chars = oti6858_buf_data_avail(priv->buf);
446 spin_unlock_irqrestore(&priv->lock, flags);
447
448 return chars;
449 }
450
451 static void oti6858_init_termios(struct tty_struct *tty)
452 {
453 *(tty->termios) = tty_std_termios;
454 tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
455 tty->termios->c_ispeed = 38400;
456 tty->termios->c_ospeed = 38400;
457 }
458
459 static void oti6858_set_termios(struct tty_struct *tty,
460 struct usb_serial_port *port, struct ktermios *old_termios)
461 {
462 struct oti6858_private *priv = usb_get_serial_port_data(port);
463 unsigned long flags;
464 unsigned int cflag;
465 u8 frame_fmt, control;
466 __le16 divisor;
467 int br;
468
469 dbg("%s(port = %d)", __func__, port->number);
470
471 if (!tty) {
472 dbg("%s(): no tty structures", __func__);
473 return;
474 }
475
476 cflag = tty->termios->c_cflag;
477
478 spin_lock_irqsave(&priv->lock, flags);
479 divisor = priv->pending_setup.divisor;
480 frame_fmt = priv->pending_setup.frame_fmt;
481 control = priv->pending_setup.control;
482 spin_unlock_irqrestore(&priv->lock, flags);
483
484 frame_fmt &= ~FMT_DATA_BITS_MASK;
485 switch (cflag & CSIZE) {
486 case CS5:
487 frame_fmt |= FMT_DATA_BITS_5;
488 break;
489 case CS6:
490 frame_fmt |= FMT_DATA_BITS_6;
491 break;
492 case CS7:
493 frame_fmt |= FMT_DATA_BITS_7;
494 break;
495 default:
496 case CS8:
497 frame_fmt |= FMT_DATA_BITS_8;
498 break;
499 }
500
501 /* manufacturer claims that this device can work with baud rates
502 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
503 * guarantee that any other baud rate will work (especially
504 * the higher ones)
505 */
506 br = tty_get_baud_rate(tty);
507 if (br == 0) {
508 divisor = 0;
509 } else {
510 int real_br;
511 int new_divisor;
512 br = min(br, OTI6858_MAX_BAUD_RATE);
513
514 new_divisor = (96000000 + 8 * br) / (16 * br);
515 real_br = 96000000 / (16 * new_divisor);
516 divisor = cpu_to_le16(new_divisor);
517 tty_encode_baud_rate(tty, real_br, real_br);
518 }
519
520 frame_fmt &= ~FMT_STOP_BITS_MASK;
521 if ((cflag & CSTOPB) != 0)
522 frame_fmt |= FMT_STOP_BITS_2;
523 else
524 frame_fmt |= FMT_STOP_BITS_1;
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 } else {
533 frame_fmt |= FMT_PARITY_NONE;
534 }
535
536 control &= ~CONTROL_MASK;
537 if ((cflag & CRTSCTS) != 0)
538 control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
539
540 /* change control lines if we are switching to or from B0 */
541 /* FIXME:
542 spin_lock_irqsave(&priv->lock, flags);
543 control = priv->line_control;
544 if ((cflag & CBAUD) == B0)
545 priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
546 else
547 priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
548 if (control != priv->line_control) {
549 control = priv->line_control;
550 spin_unlock_irqrestore(&priv->lock, flags);
551 set_control_lines(serial->dev, control);
552 } else {
553 spin_unlock_irqrestore(&priv->lock, flags);
554 }
555 */
556
557 spin_lock_irqsave(&priv->lock, flags);
558 if (divisor != priv->pending_setup.divisor
559 || control != priv->pending_setup.control
560 || frame_fmt != priv->pending_setup.frame_fmt) {
561 priv->pending_setup.divisor = divisor;
562 priv->pending_setup.control = control;
563 priv->pending_setup.frame_fmt = frame_fmt;
564 }
565 spin_unlock_irqrestore(&priv->lock, flags);
566 }
567
568 static int oti6858_open(struct tty_struct *tty,
569 struct usb_serial_port *port, struct file *filp)
570 {
571 struct oti6858_private *priv = usb_get_serial_port_data(port);
572 struct ktermios tmp_termios;
573 struct usb_serial *serial = port->serial;
574 struct oti6858_control_pkt *buf;
575 unsigned long flags;
576 int result;
577
578 dbg("%s(port = %d)", __func__, port->number);
579
580 usb_clear_halt(serial->dev, port->write_urb->pipe);
581 usb_clear_halt(serial->dev, port->read_urb->pipe);
582
583 if (port->port.count != 1)
584 return 0;
585
586 buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
587 if (buf == NULL) {
588 dev_err(&port->dev, "%s(): out of memory!\n", __func__);
589 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
617 dbg("%s(): submitting interrupt urb", __func__);
618 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"
622 " with error %d\n", __func__, result);
623 oti6858_close(port);
624 return -EPROTO;
625 }
626
627 /* setup termios */
628 if (tty)
629 oti6858_set_termios(tty, port, &tmp_termios);
630 port->port.drain_delay = 256; /* FIXME: check the FIFO length */
631 return 0;
632 }
633
634 static void oti6858_close(struct usb_serial_port *port)
635 {
636 struct oti6858_private *priv = usb_get_serial_port_data(port);
637 unsigned long flags;
638
639 dbg("%s(port = %d)", __func__, port->number);
640
641 spin_lock_irqsave(&priv->lock, flags);
642 /* clear out any remaining data in the buffer */
643 oti6858_buf_clear(priv->buf);
644 spin_unlock_irqrestore(&priv->lock, flags);
645
646 dbg("%s(): after buf_clear()", __func__);
647
648 /* cancel scheduled setup */
649 cancel_delayed_work(&priv->delayed_setup_work);
650 cancel_delayed_work(&priv->delayed_write_work);
651 flush_scheduled_work();
652
653 /* shutdown our urbs */
654 dbg("%s(): shutting down urbs", __func__);
655 usb_kill_urb(port->write_urb);
656 usb_kill_urb(port->read_urb);
657 usb_kill_urb(port->interrupt_in_urb);
658 }
659
660 static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
661 unsigned int set, unsigned int clear)
662 {
663 struct usb_serial_port *port = tty->driver_data;
664 struct oti6858_private *priv = usb_get_serial_port_data(port);
665 unsigned long flags;
666 u8 control;
667
668 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
669 __func__, port->number, set, clear);
670
671 if (!usb_get_intfdata(port->serial->interface))
672 return -ENODEV;
673
674 /* FIXME: check if this is correct (active high/low) */
675 spin_lock_irqsave(&priv->lock, flags);
676 control = priv->pending_setup.control;
677 if ((set & TIOCM_RTS) != 0)
678 control |= CONTROL_RTS_HIGH;
679 if ((set & TIOCM_DTR) != 0)
680 control |= CONTROL_DTR_HIGH;
681 if ((clear & TIOCM_RTS) != 0)
682 control &= ~CONTROL_RTS_HIGH;
683 if ((clear & TIOCM_DTR) != 0)
684 control &= ~CONTROL_DTR_HIGH;
685
686 if (control != priv->pending_setup.control)
687 priv->pending_setup.control = control;
688
689 spin_unlock_irqrestore(&priv->lock, flags);
690 return 0;
691 }
692
693 static int oti6858_tiocmget(struct tty_struct *tty, struct file *file)
694 {
695 struct usb_serial_port *port = tty->driver_data;
696 struct oti6858_private *priv = usb_get_serial_port_data(port);
697 unsigned long flags;
698 unsigned pin_state;
699 unsigned result = 0;
700
701 dbg("%s(port = %d)", __func__, port->number);
702
703 if (!usb_get_intfdata(port->serial->interface))
704 return -ENODEV;
705
706 spin_lock_irqsave(&priv->lock, flags);
707 pin_state = priv->status.pin_state & PIN_MASK;
708 spin_unlock_irqrestore(&priv->lock, flags);
709
710 /* FIXME: check if this is correct (active high/low) */
711 if ((pin_state & PIN_RTS) != 0)
712 result |= TIOCM_RTS;
713 if ((pin_state & PIN_CTS) != 0)
714 result |= TIOCM_CTS;
715 if ((pin_state & PIN_DSR) != 0)
716 result |= TIOCM_DSR;
717 if ((pin_state & PIN_DTR) != 0)
718 result |= TIOCM_DTR;
719 if ((pin_state & PIN_RI) != 0)
720 result |= TIOCM_RI;
721 if ((pin_state & PIN_DCD) != 0)
722 result |= TIOCM_CD;
723
724 dbg("%s() = 0x%08x", __func__, result);
725
726 return result;
727 }
728
729 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
730 {
731 struct oti6858_private *priv = usb_get_serial_port_data(port);
732 unsigned long flags;
733 unsigned int prev, status;
734 unsigned int changed;
735
736 spin_lock_irqsave(&priv->lock, flags);
737 prev = priv->status.pin_state;
738 spin_unlock_irqrestore(&priv->lock, flags);
739
740 while (1) {
741 wait_event_interruptible(priv->intr_wait,
742 priv->status.pin_state != prev);
743 if (signal_pending(current))
744 return -ERESTARTSYS;
745
746 spin_lock_irqsave(&priv->lock, flags);
747 status = priv->status.pin_state & PIN_MASK;
748 spin_unlock_irqrestore(&priv->lock, flags);
749
750 changed = prev ^ status;
751 /* FIXME: check if this is correct (active high/low) */
752 if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
753 ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
754 ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
755 ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
756 return 0;
757 prev = status;
758 }
759
760 /* NOTREACHED */
761 return 0;
762 }
763
764 static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
765 unsigned int cmd, unsigned long arg)
766 {
767 struct usb_serial_port *port = tty->driver_data;
768
769 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
770 __func__, port->number, cmd, arg);
771
772 switch (cmd) {
773 case TIOCMIWAIT:
774 dbg("%s(): TIOCMIWAIT", __func__);
775 return wait_modem_info(port, arg);
776 default:
777 dbg("%s(): 0x%04x not supported", __func__, cmd);
778 break;
779 }
780 return -ENOIOCTLCMD;
781 }
782
783
784 static void oti6858_release(struct usb_serial *serial)
785 {
786 struct oti6858_private *priv;
787 int i;
788
789 dbg("%s()", __func__);
790
791 for (i = 0; i < serial->num_ports; ++i) {
792 priv = usb_get_serial_port_data(serial->port[i]);
793 if (priv) {
794 oti6858_buf_free(priv->buf);
795 kfree(priv);
796 }
797 }
798 }
799
800 static void oti6858_read_int_callback(struct urb *urb)
801 {
802 struct usb_serial_port *port = urb->context;
803 struct oti6858_private *priv = usb_get_serial_port_data(port);
804 int transient = 0, can_recv = 0, resubmit = 1;
805 int status = urb->status;
806
807 dbg("%s(port = %d, status = %d)",
808 __func__, port->number, status);
809
810 switch (status) {
811 case 0:
812 /* success */
813 break;
814 case -ECONNRESET:
815 case -ENOENT:
816 case -ESHUTDOWN:
817 /* this urb is terminated, clean up */
818 dbg("%s(): urb shutting down with status: %d",
819 __func__, status);
820 return;
821 default:
822 dbg("%s(): nonzero urb status received: %d",
823 __func__, status);
824 break;
825 }
826
827 if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
828 struct oti6858_control_pkt *xs = urb->transfer_buffer;
829 unsigned long flags;
830
831 spin_lock_irqsave(&priv->lock, flags);
832
833 if (!priv->transient) {
834 if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
835 if (xs->rx_bytes_avail == 0) {
836 priv->transient = 4;
837 priv->setup_done = 0;
838 resubmit = 0;
839 dbg("%s(): scheduling setup_line()",
840 __func__);
841 schedule_delayed_work(&priv->delayed_setup_work, 0);
842 }
843 }
844 } else {
845 if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
846 priv->transient = 0;
847 } else if (!priv->setup_done) {
848 resubmit = 0;
849 } else if (--priv->transient == 0) {
850 if (xs->rx_bytes_avail == 0) {
851 priv->transient = 4;
852 priv->setup_done = 0;
853 resubmit = 0;
854 dbg("%s(): scheduling setup_line()",
855 __func__);
856 schedule_delayed_work(&priv->delayed_setup_work, 0);
857 }
858 }
859 }
860
861 if (!priv->transient) {
862 if (xs->pin_state != priv->status.pin_state)
863 wake_up_interruptible(&priv->intr_wait);
864 memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
865 }
866
867 if (!priv->transient && xs->rx_bytes_avail != 0) {
868 can_recv = xs->rx_bytes_avail;
869 priv->flags.read_urb_in_use = 1;
870 }
871
872 transient = priv->transient;
873 spin_unlock_irqrestore(&priv->lock, flags);
874 }
875
876 if (can_recv) {
877 int result;
878
879 port->read_urb->dev = port->serial->dev;
880 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
881 if (result != 0) {
882 priv->flags.read_urb_in_use = 0;
883 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
884 " error %d\n", __func__, result);
885 } else {
886 resubmit = 0;
887 }
888 } else if (!transient) {
889 unsigned long flags;
890
891 spin_lock_irqsave(&priv->lock, flags);
892 if (priv->flags.write_urb_in_use == 0
893 && oti6858_buf_data_avail(priv->buf) != 0) {
894 schedule_delayed_work(&priv->delayed_write_work, 0);
895 resubmit = 0;
896 }
897 spin_unlock_irqrestore(&priv->lock, flags);
898 }
899
900 if (resubmit) {
901 int result;
902
903 /* dbg("%s(): submitting interrupt urb", __func__); */
904 urb->dev = port->serial->dev;
905 result = usb_submit_urb(urb, GFP_ATOMIC);
906 if (result != 0) {
907 dev_err(&urb->dev->dev,
908 "%s(): usb_submit_urb() failed with"
909 " error %d\n", __func__, result);
910 }
911 }
912 }
913
914 static void oti6858_read_bulk_callback(struct urb *urb)
915 {
916 struct usb_serial_port *port = urb->context;
917 struct oti6858_private *priv = usb_get_serial_port_data(port);
918 struct tty_struct *tty;
919 unsigned char *data = urb->transfer_buffer;
920 unsigned long flags;
921 int status = urb->status;
922 int result;
923
924 dbg("%s(port = %d, status = %d)",
925 __func__, port->number, status);
926
927 spin_lock_irqsave(&priv->lock, flags);
928 priv->flags.read_urb_in_use = 0;
929 spin_unlock_irqrestore(&priv->lock, flags);
930
931 if (status != 0) {
932 if (!port->port.count) {
933 dbg("%s(): port is closed, exiting", __func__);
934 return;
935 }
936 /*
937 if (status == -EPROTO) {
938 * PL2303 mysteriously fails with -EPROTO reschedule
939 the read *
940 dbg("%s - caught -EPROTO, resubmitting the urb",
941 __func__);
942 result = usb_submit_urb(urb, GFP_ATOMIC);
943 if (result)
944 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
945 return;
946 }
947 */
948 dbg("%s(): unable to handle the error, exiting", __func__);
949 return;
950 }
951
952 tty = tty_port_tty_get(&port->port);
953 if (tty != NULL && urb->actual_length > 0) {
954 tty_insert_flip_string(tty, data, urb->actual_length);
955 tty_flip_buffer_push(tty);
956 }
957 tty_kref_put(tty);
958
959 /* schedule the interrupt urb if we are still open */
960 if (port->port.count != 0) {
961 port->interrupt_in_urb->dev = port->serial->dev;
962 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
963 if (result != 0) {
964 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
965 " error %d\n", __func__, result);
966 }
967 }
968 }
969
970 static void oti6858_write_bulk_callback(struct urb *urb)
971 {
972 struct usb_serial_port *port = urb->context;
973 struct oti6858_private *priv = usb_get_serial_port_data(port);
974 int status = urb->status;
975 int result;
976
977 dbg("%s(port = %d, status = %d)",
978 __func__, port->number, status);
979
980 switch (status) {
981 case 0:
982 /* success */
983 break;
984 case -ECONNRESET:
985 case -ENOENT:
986 case -ESHUTDOWN:
987 /* this urb is terminated, clean up */
988 dbg("%s(): urb shutting down with status: %d",
989 __func__, status);
990 priv->flags.write_urb_in_use = 0;
991 return;
992 default:
993 /* error in the urb, so we have to resubmit it */
994 dbg("%s(): nonzero write bulk status received: %d",
995 __func__, status);
996 dbg("%s(): overflow in write", __func__);
997
998 port->write_urb->transfer_buffer_length = 1;
999 port->write_urb->dev = port->serial->dev;
1000 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1001 if (result) {
1002 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
1003 " error %d\n", __func__, result);
1004 } else {
1005 return;
1006 }
1007 }
1008
1009 priv->flags.write_urb_in_use = 0;
1010
1011 /* schedule the interrupt urb if we are still open */
1012 port->interrupt_in_urb->dev = port->serial->dev;
1013 dbg("%s(): submitting interrupt urb", __func__);
1014 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1015 if (result != 0) {
1016 dev_err(&port->dev, "%s(): failed submitting int urb,"
1017 " error %d\n", __func__, result);
1018 }
1019 }
1020
1021
1022 /*
1023 * oti6858_buf_alloc
1024 *
1025 * Allocate a circular buffer and all associated memory.
1026 */
1027 static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
1028 {
1029 struct oti6858_buf *pb;
1030
1031 if (size == 0)
1032 return NULL;
1033
1034 pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
1035 if (pb == NULL)
1036 return NULL;
1037
1038 pb->buf_buf = kmalloc(size, GFP_KERNEL);
1039 if (pb->buf_buf == NULL) {
1040 kfree(pb);
1041 return NULL;
1042 }
1043
1044 pb->buf_size = size;
1045 pb->buf_get = pb->buf_put = pb->buf_buf;
1046
1047 return pb;
1048 }
1049
1050 /*
1051 * oti6858_buf_free
1052 *
1053 * Free the buffer and all associated memory.
1054 */
1055 static void oti6858_buf_free(struct oti6858_buf *pb)
1056 {
1057 if (pb) {
1058 kfree(pb->buf_buf);
1059 kfree(pb);
1060 }
1061 }
1062
1063 /*
1064 * oti6858_buf_clear
1065 *
1066 * Clear out all data in the circular buffer.
1067 */
1068 static void oti6858_buf_clear(struct oti6858_buf *pb)
1069 {
1070 if (pb != NULL) {
1071 /* equivalent to a get of all data available */
1072 pb->buf_get = pb->buf_put;
1073 }
1074 }
1075
1076 /*
1077 * oti6858_buf_data_avail
1078 *
1079 * Return the number of bytes of data available in the circular
1080 * buffer.
1081 */
1082 static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
1083 {
1084 if (pb == NULL)
1085 return 0;
1086 return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size;
1087 }
1088
1089 /*
1090 * oti6858_buf_space_avail
1091 *
1092 * Return the number of bytes of space available in the circular
1093 * buffer.
1094 */
1095 static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
1096 {
1097 if (pb == NULL)
1098 return 0;
1099 return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size;
1100 }
1101
1102 /*
1103 * oti6858_buf_put
1104 *
1105 * Copy data data from a user buffer and put it into the circular buffer.
1106 * Restrict to the amount of space available.
1107 *
1108 * Return the number of bytes copied.
1109 */
1110 static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
1111 unsigned int count)
1112 {
1113 unsigned int len;
1114
1115 if (pb == NULL)
1116 return 0;
1117
1118 len = oti6858_buf_space_avail(pb);
1119 if (count > len)
1120 count = len;
1121
1122 if (count == 0)
1123 return 0;
1124
1125 len = pb->buf_buf + pb->buf_size - pb->buf_put;
1126 if (count > len) {
1127 memcpy(pb->buf_put, buf, len);
1128 memcpy(pb->buf_buf, buf+len, count - len);
1129 pb->buf_put = pb->buf_buf + count - len;
1130 } else {
1131 memcpy(pb->buf_put, buf, count);
1132 if (count < len)
1133 pb->buf_put += count;
1134 else /* count == len */
1135 pb->buf_put = pb->buf_buf;
1136 }
1137
1138 return count;
1139 }
1140
1141 /*
1142 * oti6858_buf_get
1143 *
1144 * Get data from the circular buffer and copy to the given buffer.
1145 * Restrict to the amount of data available.
1146 *
1147 * Return the number of bytes copied.
1148 */
1149 static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
1150 unsigned int count)
1151 {
1152 unsigned int len;
1153
1154 if (pb == NULL)
1155 return 0;
1156
1157 len = oti6858_buf_data_avail(pb);
1158 if (count > len)
1159 count = len;
1160
1161 if (count == 0)
1162 return 0;
1163
1164 len = pb->buf_buf + pb->buf_size - pb->buf_get;
1165 if (count > len) {
1166 memcpy(buf, pb->buf_get, len);
1167 memcpy(buf+len, pb->buf_buf, count - len);
1168 pb->buf_get = pb->buf_buf + count - len;
1169 } else {
1170 memcpy(buf, pb->buf_get, count);
1171 if (count < len)
1172 pb->buf_get += count;
1173 else /* count == len */
1174 pb->buf_get = pb->buf_buf;
1175 }
1176
1177 return count;
1178 }
1179
1180 /* module description and (de)initialization */
1181
1182 static int __init oti6858_init(void)
1183 {
1184 int retval;
1185
1186 retval = usb_serial_register(&oti6858_device);
1187 if (retval == 0) {
1188 retval = usb_register(&oti6858_driver);
1189 if (retval)
1190 usb_serial_deregister(&oti6858_device);
1191 }
1192 return retval;
1193 }
1194
1195 static void __exit oti6858_exit(void)
1196 {
1197 usb_deregister(&oti6858_driver);
1198 usb_serial_deregister(&oti6858_device);
1199 }
1200
1201 module_init(oti6858_init);
1202 module_exit(oti6858_exit);
1203
1204 MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
1205 MODULE_AUTHOR(OTI6858_AUTHOR);
1206 MODULE_VERSION(OTI6858_VERSION);
1207 MODULE_LICENSE("GPL");
1208
1209 module_param(debug, bool, S_IRUGO | S_IWUSR);
1210 MODULE_PARM_DESC(debug, "enable debug output");
1211
1212
|
This page was automatically generated by the
LXR engine.
|