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