USB: oti6858: fix TCFLSH ioctl handling
[deliverable/linux.git] / drivers / usb / serial / oti6858.c
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 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
60 static struct usb_device_id id_table [] = {
61 { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
62 { }
63 };
64
65 MODULE_DEVICE_TABLE(usb, id_table);
66
67 static 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
75 static 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
82 struct oti6858_buf {
83 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 */
100 struct oti6858_control_pkt {
101 u16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */
102 #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 */
143 static int oti6858_open(struct usb_serial_port *port, struct file *filp);
144 static void oti6858_close(struct usb_serial_port *port, struct file *filp);
145 static void oti6858_set_termios(struct usb_serial_port *port,
146 struct ktermios *old);
147 static int oti6858_ioctl(struct usb_serial_port *port, struct file *file,
148 unsigned int cmd, unsigned long arg);
149 static void oti6858_read_int_callback(struct urb *urb);
150 static void oti6858_read_bulk_callback(struct urb *urb);
151 static void oti6858_write_bulk_callback(struct urb *urb);
152 static int oti6858_write(struct usb_serial_port *port,
153 const unsigned char *buf, int count);
154 static int oti6858_write_room(struct usb_serial_port *port);
155 static void oti6858_break_ctl(struct usb_serial_port *port, int break_state);
156 static int oti6858_chars_in_buffer(struct usb_serial_port *port);
157 static int oti6858_tiocmget(struct usb_serial_port *port, struct file *file);
158 static int oti6858_tiocmset(struct usb_serial_port *port, struct file *file,
159 unsigned int set, unsigned int clear);
160 static int oti6858_startup(struct usb_serial *serial);
161 static void oti6858_shutdown(struct usb_serial *serial);
162
163 /* functions operating on buffers */
164 static struct oti6858_buf *oti6858_buf_alloc(unsigned int size);
165 static void oti6858_buf_free(struct oti6858_buf *pb);
166 static void oti6858_buf_clear(struct oti6858_buf *pb);
167 static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb);
168 static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb);
169 static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
170 unsigned int count);
171 static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
172 unsigned int count);
173
174
175 /* device info */
176 static struct usb_serial_driver oti6858_device = {
177 .driver = {
178 .owner = THIS_MODULE,
179 .name = "oti6858",
180 },
181 .id_table = id_table,
182 .num_interrupt_in = 1,
183 .num_bulk_in = 1,
184 .num_bulk_out = 1,
185 .num_ports = 1,
186 .open = oti6858_open,
187 .close = oti6858_close,
188 .write = oti6858_write,
189 .ioctl = oti6858_ioctl,
190 .break_ctl = oti6858_break_ctl,
191 .set_termios = oti6858_set_termios,
192 .tiocmget = oti6858_tiocmget,
193 .tiocmset = oti6858_tiocmset,
194 .read_bulk_callback = oti6858_read_bulk_callback,
195 .read_int_callback = oti6858_read_int_callback,
196 .write_bulk_callback = oti6858_write_bulk_callback,
197 .write_room = oti6858_write_room,
198 .chars_in_buffer = oti6858_chars_in_buffer,
199 .attach = oti6858_startup,
200 .shutdown = oti6858_shutdown,
201 };
202
203 struct oti6858_private {
204 spinlock_t lock;
205
206 struct oti6858_buf *buf;
207 struct oti6858_control_pkt status;
208
209 struct {
210 u8 read_urb_in_use;
211 u8 write_urb_in_use;
212 u8 termios_initialized;
213 } flags;
214 struct delayed_work delayed_write_work;
215
216 struct {
217 u16 divisor;
218 u8 frame_fmt;
219 u8 control;
220 } pending_setup;
221 u8 transient;
222 u8 setup_done;
223 struct delayed_work delayed_setup_work;
224
225 wait_queue_head_t intr_wait;
226 struct usb_serial_port *port; /* USB port with which associated */
227 };
228
229 #undef dbg
230 /* #define dbg(format, arg...) printk(KERN_INFO "%s: " format "\n", __FILE__, ## arg) */
231 #define dbg(format, arg...) printk(KERN_INFO "" format "\n", ## arg)
232
233 static void setup_line(struct work_struct *work)
234 {
235 struct oti6858_private *priv = container_of(work, struct oti6858_private, delayed_setup_work.work);
236 struct usb_serial_port *port = priv->port;
237 struct oti6858_control_pkt *new_setup;
238 unsigned long flags;
239 int result;
240
241 dbg("%s(port = %d)", __FUNCTION__, port->number);
242
243 if ((new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
244 dev_err(&port->dev, "%s(): out of memory!\n", __FUNCTION__);
245 /* we will try again */
246 schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
247 return;
248 }
249
250 result = usb_control_msg(port->serial->dev,
251 usb_rcvctrlpipe(port->serial->dev, 0),
252 OTI6858_REQ_T_GET_STATUS,
253 OTI6858_REQ_GET_STATUS,
254 0, 0,
255 new_setup, OTI6858_CTRL_PKT_SIZE,
256 100);
257
258 if (result != OTI6858_CTRL_PKT_SIZE) {
259 dev_err(&port->dev, "%s(): error reading status\n", __FUNCTION__);
260 kfree(new_setup);
261 /* we will try again */
262 schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
263 return;
264 }
265
266 spin_lock_irqsave(&priv->lock, flags);
267 if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
268 new_setup->divisor = priv->pending_setup.divisor;
269 new_setup->control = priv->pending_setup.control;
270 new_setup->frame_fmt = priv->pending_setup.frame_fmt;
271
272 spin_unlock_irqrestore(&priv->lock, flags);
273 result = usb_control_msg(port->serial->dev,
274 usb_sndctrlpipe(port->serial->dev, 0),
275 OTI6858_REQ_T_SET_LINE,
276 OTI6858_REQ_SET_LINE,
277 0, 0,
278 new_setup, OTI6858_CTRL_PKT_SIZE,
279 100);
280 } else {
281 spin_unlock_irqrestore(&priv->lock, flags);
282 result = 0;
283 }
284 kfree(new_setup);
285
286 spin_lock_irqsave(&priv->lock, flags);
287 if (result != OTI6858_CTRL_PKT_SIZE)
288 priv->transient = 0;
289 priv->setup_done = 1;
290 spin_unlock_irqrestore(&priv->lock, flags);
291
292 dbg("%s(): submitting interrupt urb", __FUNCTION__);
293 port->interrupt_in_urb->dev = port->serial->dev;
294 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
295 if (result != 0) {
296 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
297 " with error %d\n", __FUNCTION__, result);
298 }
299 }
300
301 void send_data(struct work_struct *work)
302 {
303 struct oti6858_private *priv = container_of(work, struct oti6858_private, delayed_write_work.work);
304 struct usb_serial_port *port = priv->port;
305 int count = 0, result;
306 unsigned long flags;
307 unsigned char allow;
308
309 dbg("%s(port = %d)", __FUNCTION__, port->number);
310
311 spin_lock_irqsave(&priv->lock, flags);
312 if (priv->flags.write_urb_in_use) {
313 spin_unlock_irqrestore(&priv->lock, flags);
314 schedule_delayed_work(&priv->delayed_write_work, 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", __FUNCTION__);
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", __FUNCTION__, 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", __FUNCTION__, 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 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)", __FUNCTION__, 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 usb_serial_port *port)
420 {
421 struct oti6858_private *priv = usb_get_serial_port_data(port);
422 int room = 0;
423 unsigned long flags;
424
425 dbg("%s(port = %d)", __FUNCTION__, port->number);
426
427 spin_lock_irqsave(&priv->lock, flags);
428 room = oti6858_buf_space_avail(priv->buf);
429 spin_unlock_irqrestore(&priv->lock, flags);
430
431 return room;
432 }
433
434 static int oti6858_chars_in_buffer(struct usb_serial_port *port)
435 {
436 struct oti6858_private *priv = usb_get_serial_port_data(port);
437 int chars = 0;
438 unsigned long flags;
439
440 dbg("%s(port = %d)", __FUNCTION__, port->number);
441
442 spin_lock_irqsave(&priv->lock, flags);
443 chars = oti6858_buf_data_avail(priv->buf);
444 spin_unlock_irqrestore(&priv->lock, flags);
445
446 return chars;
447 }
448
449 static void oti6858_set_termios(struct usb_serial_port *port,
450 struct ktermios *old_termios)
451 {
452 struct oti6858_private *priv = usb_get_serial_port_data(port);
453 unsigned long flags;
454 unsigned int cflag;
455 u8 frame_fmt, control;
456 u16 divisor;
457 int br;
458
459 dbg("%s(port = %d)", __FUNCTION__, port->number);
460
461 if (!port->tty || !port->tty->termios) {
462 dbg("%s(): no tty structures", __FUNCTION__);
463 return;
464 }
465
466 spin_lock_irqsave(&priv->lock, flags);
467 if (!priv->flags.termios_initialized) {
468 *(port->tty->termios) = tty_std_termios;
469 port->tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
470 priv->flags.termios_initialized = 1;
471 port->tty->termios->c_ispeed = 38400;
472 port->tty->termios->c_ospeed = 38400;
473 }
474 spin_unlock_irqrestore(&priv->lock, flags);
475
476 cflag = port->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(port->tty);
507 if (br == 0) {
508 divisor = 0;
509 } else {
510 int real_br;
511 br = min(br, OTI6858_MAX_BAUD_RATE);
512
513 divisor = (96000000 + 8 * br) / (16 * br);
514 real_br = 96000000 / (16 * divisor);
515 divisor = cpu_to_le16(divisor);
516 tty_encode_baud_rate(port->tty, real_br, real_br);
517 }
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
569 static int oti6858_open(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)", __FUNCTION__, 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->open_count != 1)
584 return 0;
585
586 if ((buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
587 dev_err(&port->dev, "%s(): out of memory!\n", __FUNCTION__);
588 return -ENOMEM;
589 }
590
591 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
592 OTI6858_REQ_T_GET_STATUS,
593 OTI6858_REQ_GET_STATUS,
594 0, 0,
595 buf, OTI6858_CTRL_PKT_SIZE,
596 100);
597 if (result != OTI6858_CTRL_PKT_SIZE) {
598 /* assume default (after power-on reset) values */
599 buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
600 buf->frame_fmt = 0x03; /* 8N1 */
601 buf->something = 0x43;
602 buf->control = 0x4c; /* DTR, RTS */
603 buf->tx_status = 0x00;
604 buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
605 buf->rx_bytes_avail = 0x00;
606 }
607
608 spin_lock_irqsave(&priv->lock, flags);
609 memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
610 priv->pending_setup.divisor = buf->divisor;
611 priv->pending_setup.frame_fmt = buf->frame_fmt;
612 priv->pending_setup.control = buf->control;
613 spin_unlock_irqrestore(&priv->lock, flags);
614 kfree(buf);
615
616 dbg("%s(): submitting interrupt urb", __FUNCTION__);
617 port->interrupt_in_urb->dev = serial->dev;
618 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
619 if (result != 0) {
620 dev_err(&port->dev, "%s(): usb_submit_urb() failed"
621 " with error %d\n", __FUNCTION__, result);
622 oti6858_close(port, NULL);
623 return -EPROTO;
624 }
625
626 /* setup termios */
627 if (port->tty)
628 oti6858_set_termios(port, &tmp_termios);
629
630 return 0;
631 }
632
633 static void oti6858_close(struct usb_serial_port *port, struct file *filp)
634 {
635 struct oti6858_private *priv = usb_get_serial_port_data(port);
636 unsigned long flags;
637 long timeout;
638 wait_queue_t wait;
639
640 dbg("%s(port = %d)", __FUNCTION__, port->number);
641
642 /* wait for data to drain from the buffer */
643 spin_lock_irqsave(&priv->lock, flags);
644 timeout = 30 * HZ; /* PL2303_CLOSING_WAIT */
645 init_waitqueue_entry(&wait, current);
646 add_wait_queue(&port->tty->write_wait, &wait);
647 dbg("%s(): entering wait loop", __FUNCTION__);
648 for (;;) {
649 set_current_state(TASK_INTERRUPTIBLE);
650 if (oti6858_buf_data_avail(priv->buf) == 0
651 || timeout == 0 || signal_pending(current)
652 || port->serial->disconnected)
653 break;
654 spin_unlock_irqrestore(&priv->lock, flags);
655 timeout = schedule_timeout(timeout);
656 spin_lock_irqsave(&priv->lock, flags);
657 }
658 set_current_state(TASK_RUNNING);
659 remove_wait_queue(&port->tty->write_wait, &wait);
660 dbg("%s(): after wait loop", __FUNCTION__);
661
662 /* clear out any remaining data in the buffer */
663 oti6858_buf_clear(priv->buf);
664 spin_unlock_irqrestore(&priv->lock, flags);
665
666 /* wait for characters to drain from the device */
667 /* (this is long enough for the entire 256 byte */
668 /* pl2303 hardware buffer to drain with no flow */
669 /* control for data rates of 1200 bps or more, */
670 /* for lower rates we should really know how much */
671 /* data is in the buffer to compute a delay */
672 /* that is not unnecessarily long) */
673 /* FIXME
674 bps = tty_get_baud_rate(port->tty);
675 if (bps > 1200)
676 timeout = max((HZ*2560)/bps,HZ/10);
677 else
678 */
679 timeout = 2*HZ;
680 schedule_timeout_interruptible(timeout);
681 dbg("%s(): after schedule_timeout_interruptible()", __FUNCTION__);
682
683 /* cancel scheduled setup */
684 cancel_delayed_work(&priv->delayed_setup_work);
685 cancel_delayed_work(&priv->delayed_write_work);
686 flush_scheduled_work();
687
688 /* shutdown our urbs */
689 dbg("%s(): shutting down urbs", __FUNCTION__);
690 usb_kill_urb(port->write_urb);
691 usb_kill_urb(port->read_urb);
692 usb_kill_urb(port->interrupt_in_urb);
693
694 /*
695 if (port->tty && (port->tty->termios->c_cflag) & HUPCL) {
696 // drop DTR and RTS
697 spin_lock_irqsave(&priv->lock, flags);
698 priv->pending_setup.control &= ~CONTROL_MASK;
699 spin_unlock_irqrestore(&priv->lock, flags);
700 }
701 */
702 }
703
704 static int oti6858_tiocmset(struct usb_serial_port *port, struct file *file,
705 unsigned int set, unsigned int clear)
706 {
707 struct oti6858_private *priv = usb_get_serial_port_data(port);
708 unsigned long flags;
709 u8 control;
710
711 dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
712 __FUNCTION__, port->number, set, clear);
713
714 if (!usb_get_intfdata(port->serial->interface))
715 return -ENODEV;
716
717 /* FIXME: check if this is correct (active high/low) */
718 spin_lock_irqsave(&priv->lock, flags);
719 control = priv->pending_setup.control;
720 if ((set & TIOCM_RTS) != 0)
721 control |= CONTROL_RTS_HIGH;
722 if ((set & TIOCM_DTR) != 0)
723 control |= CONTROL_DTR_HIGH;
724 if ((clear & TIOCM_RTS) != 0)
725 control &= ~CONTROL_RTS_HIGH;
726 if ((clear & TIOCM_DTR) != 0)
727 control &= ~CONTROL_DTR_HIGH;
728
729 if (control != priv->pending_setup.control) {
730 priv->pending_setup.control = control;
731 }
732 spin_unlock_irqrestore(&priv->lock, flags);
733
734 return 0;
735 }
736
737 static int oti6858_tiocmget(struct usb_serial_port *port, struct file *file)
738 {
739 struct oti6858_private *priv = usb_get_serial_port_data(port);
740 unsigned long flags;
741 unsigned pin_state;
742 unsigned result = 0;
743
744 dbg("%s(port = %d)", __FUNCTION__, port->number);
745
746 if (!usb_get_intfdata(port->serial->interface))
747 return -ENODEV;
748
749 spin_lock_irqsave(&priv->lock, flags);
750 pin_state = priv->status.pin_state & PIN_MASK;
751 spin_unlock_irqrestore(&priv->lock, flags);
752
753 /* FIXME: check if this is correct (active high/low) */
754 if ((pin_state & PIN_RTS) != 0)
755 result |= TIOCM_RTS;
756 if ((pin_state & PIN_CTS) != 0)
757 result |= TIOCM_CTS;
758 if ((pin_state & PIN_DSR) != 0)
759 result |= TIOCM_DSR;
760 if ((pin_state & PIN_DTR) != 0)
761 result |= TIOCM_DTR;
762 if ((pin_state & PIN_RI) != 0)
763 result |= TIOCM_RI;
764 if ((pin_state & PIN_DCD) != 0)
765 result |= TIOCM_CD;
766
767 dbg("%s() = 0x%08x", __FUNCTION__, result);
768
769 return result;
770 }
771
772 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
773 {
774 struct oti6858_private *priv = usb_get_serial_port_data(port);
775 unsigned long flags;
776 unsigned int prev, status;
777 unsigned int changed;
778
779 spin_lock_irqsave(&priv->lock, flags);
780 prev = priv->status.pin_state;
781 spin_unlock_irqrestore(&priv->lock, flags);
782
783 while (1) {
784 wait_event_interruptible(priv->intr_wait, priv->status.pin_state != prev);
785 if (signal_pending(current))
786 return -ERESTARTSYS;
787
788 spin_lock_irqsave(&priv->lock, flags);
789 status = priv->status.pin_state & PIN_MASK;
790 spin_unlock_irqrestore(&priv->lock, flags);
791
792 changed = prev ^ status;
793 /* FIXME: check if this is correct (active high/low) */
794 if ( ((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
795 ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
796 ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
797 ((arg & TIOCM_CTS) && (changed & PIN_CTS))) {
798 return 0;
799 }
800 prev = status;
801 }
802
803 /* NOTREACHED */
804 return 0;
805 }
806
807 static int oti6858_ioctl(struct usb_serial_port *port, struct file *file,
808 unsigned int cmd, unsigned long arg)
809 {
810 void __user *user_arg = (void __user *) arg;
811 unsigned int x;
812
813 dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
814 __FUNCTION__, port->number, cmd, arg);
815
816 switch (cmd) {
817 case TIOCMBIS:
818 if (copy_from_user(&x, user_arg, sizeof(x)))
819 return -EFAULT;
820 return oti6858_tiocmset(port, NULL, x, 0);
821
822 case TIOCMBIC:
823 if (copy_from_user(&x, user_arg, sizeof(x)))
824 return -EFAULT;
825 return oti6858_tiocmset(port, NULL, 0, x);
826
827 case TIOCMIWAIT:
828 dbg("%s(): TIOCMIWAIT", __FUNCTION__);
829 return wait_modem_info(port, arg);
830
831 default:
832 dbg("%s(): 0x%04x not supported", __FUNCTION__, cmd);
833 break;
834 }
835
836 return -ENOIOCTLCMD;
837 }
838
839 static void oti6858_break_ctl(struct usb_serial_port *port, int break_state)
840 {
841 int state;
842
843 dbg("%s(port = %d)", __FUNCTION__, port->number);
844
845 state = (break_state == 0) ? 0 : 1;
846 dbg("%s(): turning break %s", __FUNCTION__, state ? "on" : "off");
847
848 /* FIXME */
849 /*
850 result = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
851 BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
852 0, NULL, 0, 100);
853 if (result != 0)
854 dbg("%s(): error sending break", __FUNCTION__);
855 */
856 }
857
858 static void oti6858_shutdown(struct usb_serial *serial)
859 {
860 struct oti6858_private *priv;
861 int i;
862
863 dbg("%s()", __FUNCTION__);
864
865 for (i = 0; i < serial->num_ports; ++i) {
866 priv = usb_get_serial_port_data(serial->port[i]);
867 if (priv) {
868 oti6858_buf_free(priv->buf);
869 kfree(priv);
870 usb_set_serial_port_data(serial->port[i], NULL);
871 }
872 }
873 }
874
875 static void oti6858_read_int_callback(struct urb *urb)
876 {
877 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
878 struct oti6858_private *priv = usb_get_serial_port_data(port);
879 int transient = 0, can_recv = 0, resubmit = 1;
880 int status = urb->status;
881
882 dbg("%s(port = %d, status = %d)",
883 __FUNCTION__, port->number, status);
884
885 switch (status) {
886 case 0:
887 /* success */
888 break;
889 case -ECONNRESET:
890 case -ENOENT:
891 case -ESHUTDOWN:
892 /* this urb is terminated, clean up */
893 dbg("%s(): urb shutting down with status: %d",
894 __FUNCTION__, status);
895 return;
896 default:
897 dbg("%s(): nonzero urb status received: %d",
898 __FUNCTION__, status);
899 break;
900 }
901
902 if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
903 struct oti6858_control_pkt *xs = urb->transfer_buffer;
904 unsigned long flags;
905
906 spin_lock_irqsave(&priv->lock, flags);
907
908 if (!priv->transient) {
909 if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
910 if (xs->rx_bytes_avail == 0) {
911 priv->transient = 4;
912 priv->setup_done = 0;
913 resubmit = 0;
914 dbg("%s(): scheduling setup_line()",
915 __FUNCTION__);
916 schedule_delayed_work(&priv->delayed_setup_work, 0);
917 }
918 }
919 } else {
920 if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
921 priv->transient = 0;
922 } else if (!priv->setup_done) {
923 resubmit = 0;
924 } else if (--priv->transient == 0) {
925 if (xs->rx_bytes_avail == 0) {
926 priv->transient = 4;
927 priv->setup_done = 0;
928 resubmit = 0;
929 dbg("%s(): scheduling setup_line()",
930 __FUNCTION__);
931 schedule_delayed_work(&priv->delayed_setup_work, 0);
932 }
933 }
934 }
935
936 if (!priv->transient) {
937 if (xs->pin_state != priv->status.pin_state)
938 wake_up_interruptible(&priv->intr_wait);
939 memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
940 }
941
942 if (!priv->transient && xs->rx_bytes_avail != 0) {
943 can_recv = xs->rx_bytes_avail;
944 priv->flags.read_urb_in_use = 1;
945 }
946
947 transient = priv->transient;
948 spin_unlock_irqrestore(&priv->lock, flags);
949 }
950
951 if (can_recv) {
952 int result;
953
954 port->read_urb->dev = port->serial->dev;
955 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
956 if (result != 0) {
957 priv->flags.read_urb_in_use = 0;
958 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
959 " error %d\n", __FUNCTION__, result);
960 } else {
961 resubmit = 0;
962 }
963 } else if (!transient) {
964 unsigned long flags;
965
966 spin_lock_irqsave(&priv->lock, flags);
967 if (priv->flags.write_urb_in_use == 0
968 && oti6858_buf_data_avail(priv->buf) != 0) {
969 schedule_delayed_work(&priv->delayed_write_work,0);
970 resubmit = 0;
971 }
972 spin_unlock_irqrestore(&priv->lock, flags);
973 }
974
975 if (resubmit) {
976 int result;
977
978 // dbg("%s(): submitting interrupt urb", __FUNCTION__);
979 urb->dev = port->serial->dev;
980 result = usb_submit_urb(urb, GFP_ATOMIC);
981 if (result != 0) {
982 dev_err(&urb->dev->dev,
983 "%s(): usb_submit_urb() failed with"
984 " error %d\n", __FUNCTION__, result);
985 }
986 }
987 }
988
989 static void oti6858_read_bulk_callback(struct urb *urb)
990 {
991 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
992 struct oti6858_private *priv = usb_get_serial_port_data(port);
993 struct tty_struct *tty;
994 unsigned char *data = urb->transfer_buffer;
995 unsigned long flags;
996 int status = urb->status;
997 int result;
998
999 dbg("%s(port = %d, status = %d)",
1000 __FUNCTION__, port->number, status);
1001
1002 spin_lock_irqsave(&priv->lock, flags);
1003 priv->flags.read_urb_in_use = 0;
1004 spin_unlock_irqrestore(&priv->lock, flags);
1005
1006 if (status != 0) {
1007 if (!port->open_count) {
1008 dbg("%s(): port is closed, exiting", __FUNCTION__);
1009 return;
1010 }
1011 /*
1012 if (status == -EPROTO) {
1013 // PL2303 mysteriously fails with -EPROTO reschedule the read
1014 dbg("%s - caught -EPROTO, resubmitting the urb", __FUNCTION__);
1015 result = usb_submit_urb(urb, GFP_ATOMIC);
1016 if (result)
1017 dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
1018 return;
1019 }
1020 */
1021 dbg("%s(): unable to handle the error, exiting", __FUNCTION__);
1022 return;
1023 }
1024
1025 tty = port->tty;
1026 if (tty != NULL && urb->actual_length > 0) {
1027 tty_insert_flip_string(tty, data, urb->actual_length);
1028 tty_flip_buffer_push(tty);
1029 }
1030
1031 // schedule the interrupt urb if we are still open */
1032 if (port->open_count != 0) {
1033 port->interrupt_in_urb->dev = port->serial->dev;
1034 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1035 if (result != 0) {
1036 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
1037 " error %d\n", __FUNCTION__, result);
1038 }
1039 }
1040 }
1041
1042 static void oti6858_write_bulk_callback(struct urb *urb)
1043 {
1044 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
1045 struct oti6858_private *priv = usb_get_serial_port_data(port);
1046 int status = urb->status;
1047 int result;
1048
1049 dbg("%s(port = %d, status = %d)",
1050 __FUNCTION__, port->number, status);
1051
1052 switch (status) {
1053 case 0:
1054 /* success */
1055 break;
1056 case -ECONNRESET:
1057 case -ENOENT:
1058 case -ESHUTDOWN:
1059 /* this urb is terminated, clean up */
1060 dbg("%s(): urb shutting down with status: %d",
1061 __FUNCTION__, status);
1062 priv->flags.write_urb_in_use = 0;
1063 return;
1064 default:
1065 /* error in the urb, so we have to resubmit it */
1066 dbg("%s(): nonzero write bulk status received: %d",
1067 __FUNCTION__, status);
1068 dbg("%s(): overflow in write", __FUNCTION__);
1069
1070 port->write_urb->transfer_buffer_length = 1;
1071 port->write_urb->dev = port->serial->dev;
1072 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1073 if (result) {
1074 dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
1075 " error %d\n", __FUNCTION__, result);
1076 } else {
1077 return;
1078 }
1079 }
1080
1081 priv->flags.write_urb_in_use = 0;
1082
1083 // schedule the interrupt urb if we are still open */
1084 port->interrupt_in_urb->dev = port->serial->dev;
1085 dbg("%s(): submitting interrupt urb", __FUNCTION__);
1086 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
1087 if (result != 0) {
1088 dev_err(&port->dev, "%s(): failed submitting int urb,"
1089 " error %d\n", __FUNCTION__, result);
1090 }
1091 }
1092
1093
1094 /*
1095 * oti6858_buf_alloc
1096 *
1097 * Allocate a circular buffer and all associated memory.
1098 */
1099 static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
1100 {
1101 struct oti6858_buf *pb;
1102
1103 if (size == 0)
1104 return NULL;
1105
1106 pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
1107 if (pb == NULL)
1108 return NULL;
1109
1110 pb->buf_buf = kmalloc(size, GFP_KERNEL);
1111 if (pb->buf_buf == NULL) {
1112 kfree(pb);
1113 return NULL;
1114 }
1115
1116 pb->buf_size = size;
1117 pb->buf_get = pb->buf_put = pb->buf_buf;
1118
1119 return pb;
1120 }
1121
1122 /*
1123 * oti6858_buf_free
1124 *
1125 * Free the buffer and all associated memory.
1126 */
1127 static void oti6858_buf_free(struct oti6858_buf *pb)
1128 {
1129 if (pb) {
1130 kfree(pb->buf_buf);
1131 kfree(pb);
1132 }
1133 }
1134
1135 /*
1136 * oti6858_buf_clear
1137 *
1138 * Clear out all data in the circular buffer.
1139 */
1140 static void oti6858_buf_clear(struct oti6858_buf *pb)
1141 {
1142 if (pb != NULL) {
1143 /* equivalent to a get of all data available */
1144 pb->buf_get = pb->buf_put;
1145 }
1146 }
1147
1148 /*
1149 * oti6858_buf_data_avail
1150 *
1151 * Return the number of bytes of data available in the circular
1152 * buffer.
1153 */
1154 static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
1155 {
1156 if (pb == NULL)
1157 return 0;
1158 return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size);
1159 }
1160
1161 /*
1162 * oti6858_buf_space_avail
1163 *
1164 * Return the number of bytes of space available in the circular
1165 * buffer.
1166 */
1167 static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
1168 {
1169 if (pb == NULL)
1170 return 0;
1171 return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size);
1172 }
1173
1174 /*
1175 * oti6858_buf_put
1176 *
1177 * Copy data data from a user buffer and put it into the circular buffer.
1178 * Restrict to the amount of space available.
1179 *
1180 * Return the number of bytes copied.
1181 */
1182 static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
1183 unsigned int count)
1184 {
1185 unsigned int len;
1186
1187 if (pb == NULL)
1188 return 0;
1189
1190 len = oti6858_buf_space_avail(pb);
1191 if (count > len)
1192 count = len;
1193
1194 if (count == 0)
1195 return 0;
1196
1197 len = pb->buf_buf + pb->buf_size - pb->buf_put;
1198 if (count > len) {
1199 memcpy(pb->buf_put, buf, len);
1200 memcpy(pb->buf_buf, buf+len, count - len);
1201 pb->buf_put = pb->buf_buf + count - len;
1202 } else {
1203 memcpy(pb->buf_put, buf, count);
1204 if (count < len)
1205 pb->buf_put += count;
1206 else /* count == len */
1207 pb->buf_put = pb->buf_buf;
1208 }
1209
1210 return count;
1211 }
1212
1213 /*
1214 * oti6858_buf_get
1215 *
1216 * Get data from the circular buffer and copy to the given buffer.
1217 * Restrict to the amount of data available.
1218 *
1219 * Return the number of bytes copied.
1220 */
1221 static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
1222 unsigned int count)
1223 {
1224 unsigned int len;
1225
1226 if (pb == NULL)
1227 return 0;
1228
1229 len = oti6858_buf_data_avail(pb);
1230 if (count > len)
1231 count = len;
1232
1233 if (count == 0)
1234 return 0;
1235
1236 len = pb->buf_buf + pb->buf_size - pb->buf_get;
1237 if (count > len) {
1238 memcpy(buf, pb->buf_get, len);
1239 memcpy(buf+len, pb->buf_buf, count - len);
1240 pb->buf_get = pb->buf_buf + count - len;
1241 } else {
1242 memcpy(buf, pb->buf_get, count);
1243 if (count < len)
1244 pb->buf_get += count;
1245 else /* count == len */
1246 pb->buf_get = pb->buf_buf;
1247 }
1248
1249 return count;
1250 }
1251
1252 /* module description and (de)initialization */
1253
1254 static int __init oti6858_init(void)
1255 {
1256 int retval;
1257
1258 if ((retval = usb_serial_register(&oti6858_device)) == 0) {
1259 if ((retval = usb_register(&oti6858_driver)) != 0)
1260 usb_serial_deregister(&oti6858_device);
1261 else
1262 return 0;
1263 }
1264
1265 return retval;
1266 }
1267
1268 static void __exit oti6858_exit(void)
1269 {
1270 usb_deregister(&oti6858_driver);
1271 usb_serial_deregister(&oti6858_device);
1272 }
1273
1274 module_init(oti6858_init);
1275 module_exit(oti6858_exit);
1276
1277 MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
1278 MODULE_AUTHOR(OTI6858_AUTHOR);
1279 MODULE_VERSION(OTI6858_VERSION);
1280 MODULE_LICENSE("GPL");
1281
1282 module_param(debug, bool, S_IRUGO | S_IWUSR);
1283 MODULE_PARM_DESC(debug, "enable debug output");
1284
This page took 0.058391 seconds and 5 git commands to generate.