]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/usb/serial/mos7840.c
USB: serial: mos7840: fix remote wakeup
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / serial / mos7840.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Clean ups from Moschip version and a few ioctl implementations by:
4 * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
5 *
6 * Originally based on drivers/usb/serial/io_edgeport.c which is:
7 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
8 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
9 *
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/tty.h>
16 #include <linux/tty_driver.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/serial.h>
20 #include <linux/usb.h>
21 #include <linux/usb/serial.h>
22 #include <linux/uaccess.h>
23
24 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
25
26 /*
27 * 16C50 UART register defines
28 */
29
30 #define LCR_BITS_5 0x00 /* 5 bits/char */
31 #define LCR_BITS_6 0x01 /* 6 bits/char */
32 #define LCR_BITS_7 0x02 /* 7 bits/char */
33 #define LCR_BITS_8 0x03 /* 8 bits/char */
34 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
35
36 #define LCR_STOP_1 0x00 /* 1 stop bit */
37 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
38 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
39 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
40
41 #define LCR_PAR_NONE 0x00 /* No parity */
42 #define LCR_PAR_ODD 0x08 /* Odd parity */
43 #define LCR_PAR_EVEN 0x18 /* Even parity */
44 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
45 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
46 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
47
48 #define LCR_SET_BREAK 0x40 /* Set Break condition */
49 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
50
51 #define MCR_DTR 0x01 /* Assert DTR */
52 #define MCR_RTS 0x02 /* Assert RTS */
53 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
54 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
55 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
56 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
57
58 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
59 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
60 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
61 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
62
63 /*
64 * Defines used for sending commands to port
65 */
66
67 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
68
69 #define MOS_PORT1 0x0200
70 #define MOS_PORT2 0x0300
71 #define MOS_VENREG 0x0000
72 #define MOS_MAX_PORT 0x02
73 #define MOS_WRITE 0x0E
74 #define MOS_READ 0x0D
75
76 /* Requests */
77 #define MCS_RD_RTYPE 0xC0
78 #define MCS_WR_RTYPE 0x40
79 #define MCS_RDREQ 0x0D
80 #define MCS_WRREQ 0x0E
81 #define MCS_CTRL_TIMEOUT 500
82 #define VENDOR_READ_LENGTH (0x01)
83
84 #define MAX_NAME_LEN 64
85
86 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
87 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
88
89 /* For higher baud Rates use TIOCEXBAUD */
90 #define TIOCEXBAUD 0x5462
91
92 /* vendor id and device id defines */
93
94 /* The native mos7840/7820 component */
95 #define USB_VENDOR_ID_MOSCHIP 0x9710
96 #define MOSCHIP_DEVICE_ID_7840 0x7840
97 #define MOSCHIP_DEVICE_ID_7820 0x7820
98 #define MOSCHIP_DEVICE_ID_7810 0x7810
99 /* The native component can have its vendor/device id's overridden
100 * in vendor-specific implementations. Such devices can be handled
101 * by making a change here, in id_table.
102 */
103 #define USB_VENDOR_ID_BANDB 0x0856
104 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
105 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
106 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
107 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
108 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
109 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
110 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
111 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
112 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
113 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
114 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
115 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
116 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
117
118 /* This driver also supports
119 * ATEN UC2324 device using Moschip MCS7840
120 * ATEN UC2322 device using Moschip MCS7820
121 * MOXA UPort 2210 device using Moschip MCS7820
122 */
123 #define USB_VENDOR_ID_ATENINTL 0x0557
124 #define ATENINTL_DEVICE_ID_UC2324 0x2011
125 #define ATENINTL_DEVICE_ID_UC2322 0x7820
126
127 #define USB_VENDOR_ID_MOXA 0x110a
128 #define MOXA_DEVICE_ID_2210 0x2210
129
130 /* Interrupt Routine Defines */
131
132 #define SERIAL_IIR_RLS 0x06
133 #define SERIAL_IIR_MS 0x00
134
135 /*
136 * Emulation of the bit mask on the LINE STATUS REGISTER.
137 */
138 #define SERIAL_LSR_DR 0x0001
139 #define SERIAL_LSR_OE 0x0002
140 #define SERIAL_LSR_PE 0x0004
141 #define SERIAL_LSR_FE 0x0008
142 #define SERIAL_LSR_BI 0x0010
143
144 #define MOS_MSR_DELTA_CTS 0x10
145 #define MOS_MSR_DELTA_DSR 0x20
146 #define MOS_MSR_DELTA_RI 0x40
147 #define MOS_MSR_DELTA_CD 0x80
148
149 /* Serial Port register Address */
150 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
151 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
152 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
153 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
154 #define LINE_STATUS_REGISTER ((__u16)(0x05))
155 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
156 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
157 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
158 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
159
160 #define CLK_MULTI_REGISTER ((__u16)(0x02))
161 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
162 #define GPIO_REGISTER ((__u16)(0x07))
163
164 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
165
166 /*
167 * URB POOL related defines
168 */
169 #define NUM_URBS 16 /* URB Count */
170 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
171
172 /* LED on/off milliseconds*/
173 #define LED_ON_MS 500
174 #define LED_OFF_MS 500
175
176 enum mos7840_flag {
177 MOS7840_FLAG_CTRL_BUSY,
178 MOS7840_FLAG_LED_BUSY,
179 };
180
181 static const struct usb_device_id id_table[] = {
182 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
183 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
184 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
185 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
186 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
187 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
188 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
189 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
190 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
191 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
192 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
193 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
198 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
199 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
200 {USB_DEVICE(USB_VENDOR_ID_MOXA, MOXA_DEVICE_ID_2210)},
201 {} /* terminating entry */
202 };
203 MODULE_DEVICE_TABLE(usb, id_table);
204
205 /* This structure holds all of the local port information */
206
207 struct moschip_port {
208 int port_num; /*Actual port number in the device(1,2,etc) */
209 struct urb *read_urb; /* read URB for this port */
210 __u8 shadowLCR; /* last LCR value received */
211 __u8 shadowMCR; /* last MCR value received */
212 char open;
213 char open_ports;
214 struct usb_serial_port *port; /* loop back to the owner of this object */
215
216 /* Offsets */
217 __u8 SpRegOffset;
218 __u8 ControlRegOffset;
219 __u8 DcrRegOffset;
220 /* for processing control URBS in interrupt context */
221 struct urb *control_urb;
222 struct usb_ctrlrequest *dr;
223 char *ctrl_buf;
224 int MsrLsr;
225
226 spinlock_t pool_lock;
227 struct urb *write_urb_pool[NUM_URBS];
228 char busy[NUM_URBS];
229 bool read_urb_busy;
230
231 /* For device(s) with LED indicator */
232 bool has_led;
233 struct timer_list led_timer1; /* Timer for LED on */
234 struct timer_list led_timer2; /* Timer for LED off */
235 struct urb *led_urb;
236 struct usb_ctrlrequest *led_dr;
237
238 unsigned long flags;
239 };
240
241 /*
242 * mos7840_set_reg_sync
243 * To set the Control register by calling usb_fill_control_urb function
244 * by passing usb_sndctrlpipe function as parameter.
245 */
246
247 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
248 __u16 val)
249 {
250 struct usb_device *dev = port->serial->dev;
251 val = val & 0x00ff;
252 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
253
254 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
255 MCS_WR_RTYPE, val, reg, NULL, 0,
256 MOS_WDR_TIMEOUT);
257 }
258
259 /*
260 * mos7840_get_reg_sync
261 * To set the Uart register by calling usb_fill_control_urb function by
262 * passing usb_rcvctrlpipe function as parameter.
263 */
264
265 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
266 __u16 *val)
267 {
268 struct usb_device *dev = port->serial->dev;
269 int ret = 0;
270 u8 *buf;
271
272 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
273 if (!buf)
274 return -ENOMEM;
275
276 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
277 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
278 MOS_WDR_TIMEOUT);
279 if (ret < VENDOR_READ_LENGTH) {
280 if (ret >= 0)
281 ret = -EIO;
282 goto out;
283 }
284
285 *val = buf[0];
286 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
287 out:
288 kfree(buf);
289 return ret;
290 }
291
292 /*
293 * mos7840_set_uart_reg
294 * To set the Uart register by calling usb_fill_control_urb function by
295 * passing usb_sndctrlpipe function as parameter.
296 */
297
298 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
299 __u16 val)
300 {
301
302 struct usb_device *dev = port->serial->dev;
303 val = val & 0x00ff;
304 /* For the UART control registers, the application number need
305 to be Or'ed */
306 if (port->serial->num_ports == 4) {
307 val |= ((__u16)port->port_number + 1) << 8;
308 } else {
309 if (port->port_number == 0) {
310 val |= ((__u16)port->port_number + 1) << 8;
311 } else {
312 val |= ((__u16)port->port_number + 2) << 8;
313 }
314 }
315 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
316 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
317 MCS_WR_RTYPE, val, reg, NULL, 0,
318 MOS_WDR_TIMEOUT);
319
320 }
321
322 /*
323 * mos7840_get_uart_reg
324 * To set the Control register by calling usb_fill_control_urb function
325 * by passing usb_rcvctrlpipe function as parameter.
326 */
327 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
328 __u16 *val)
329 {
330 struct usb_device *dev = port->serial->dev;
331 int ret = 0;
332 __u16 Wval;
333 u8 *buf;
334
335 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
336 if (!buf)
337 return -ENOMEM;
338
339 /* Wval is same as application number */
340 if (port->serial->num_ports == 4) {
341 Wval = ((__u16)port->port_number + 1) << 8;
342 } else {
343 if (port->port_number == 0) {
344 Wval = ((__u16)port->port_number + 1) << 8;
345 } else {
346 Wval = ((__u16)port->port_number + 2) << 8;
347 }
348 }
349 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
350 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
351 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
352 MOS_WDR_TIMEOUT);
353 if (ret < VENDOR_READ_LENGTH) {
354 if (ret >= 0)
355 ret = -EIO;
356 goto out;
357 }
358 *val = buf[0];
359 out:
360 kfree(buf);
361 return ret;
362 }
363
364 static void mos7840_dump_serial_port(struct usb_serial_port *port,
365 struct moschip_port *mos7840_port)
366 {
367
368 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
369 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
370 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
371
372 }
373
374 /************************************************************************/
375 /************************************************************************/
376 /* I N T E R F A C E F U N C T I O N S */
377 /* I N T E R F A C E F U N C T I O N S */
378 /************************************************************************/
379 /************************************************************************/
380
381 static inline void mos7840_set_port_private(struct usb_serial_port *port,
382 struct moschip_port *data)
383 {
384 usb_set_serial_port_data(port, (void *)data);
385 }
386
387 static inline struct moschip_port *mos7840_get_port_private(struct
388 usb_serial_port
389 *port)
390 {
391 return (struct moschip_port *)usb_get_serial_port_data(port);
392 }
393
394 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
395 {
396 struct moschip_port *mos7840_port;
397 struct async_icount *icount;
398 mos7840_port = port;
399 if (new_msr &
400 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
401 MOS_MSR_DELTA_CD)) {
402 icount = &mos7840_port->port->icount;
403
404 /* update input line counters */
405 if (new_msr & MOS_MSR_DELTA_CTS)
406 icount->cts++;
407 if (new_msr & MOS_MSR_DELTA_DSR)
408 icount->dsr++;
409 if (new_msr & MOS_MSR_DELTA_CD)
410 icount->dcd++;
411 if (new_msr & MOS_MSR_DELTA_RI)
412 icount->rng++;
413
414 wake_up_interruptible(&port->port->port.delta_msr_wait);
415 }
416 }
417
418 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
419 {
420 struct async_icount *icount;
421
422 if (new_lsr & SERIAL_LSR_BI) {
423 /*
424 * Parity and Framing errors only count if they
425 * occur exclusive of a break being
426 * received.
427 */
428 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
429 }
430
431 /* update input line counters */
432 icount = &port->port->icount;
433 if (new_lsr & SERIAL_LSR_BI)
434 icount->brk++;
435 if (new_lsr & SERIAL_LSR_OE)
436 icount->overrun++;
437 if (new_lsr & SERIAL_LSR_PE)
438 icount->parity++;
439 if (new_lsr & SERIAL_LSR_FE)
440 icount->frame++;
441 }
442
443 /************************************************************************/
444 /************************************************************************/
445 /* U S B C A L L B A C K F U N C T I O N S */
446 /* U S B C A L L B A C K F U N C T I O N S */
447 /************************************************************************/
448 /************************************************************************/
449
450 static void mos7840_control_callback(struct urb *urb)
451 {
452 unsigned char *data;
453 struct moschip_port *mos7840_port;
454 struct device *dev = &urb->dev->dev;
455 __u8 regval = 0x0;
456 int status = urb->status;
457
458 mos7840_port = urb->context;
459
460 switch (status) {
461 case 0:
462 /* success */
463 break;
464 case -ECONNRESET:
465 case -ENOENT:
466 case -ESHUTDOWN:
467 /* this urb is terminated, clean up */
468 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
469 goto out;
470 default:
471 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
472 goto out;
473 }
474
475 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
476 if (urb->actual_length < 1)
477 goto out;
478
479 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
480 mos7840_port->MsrLsr, mos7840_port->port_num);
481 data = urb->transfer_buffer;
482 regval = (__u8) data[0];
483 dev_dbg(dev, "%s data is %x\n", __func__, regval);
484 if (mos7840_port->MsrLsr == 0)
485 mos7840_handle_new_msr(mos7840_port, regval);
486 else if (mos7840_port->MsrLsr == 1)
487 mos7840_handle_new_lsr(mos7840_port, regval);
488 out:
489 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
490 }
491
492 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
493 __u16 *val)
494 {
495 struct usb_device *dev = mcs->port->serial->dev;
496 struct usb_ctrlrequest *dr = mcs->dr;
497 unsigned char *buffer = mcs->ctrl_buf;
498 int ret;
499
500 if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
501 return -EBUSY;
502
503 dr->bRequestType = MCS_RD_RTYPE;
504 dr->bRequest = MCS_RDREQ;
505 dr->wValue = cpu_to_le16(Wval); /* 0 */
506 dr->wIndex = cpu_to_le16(reg);
507 dr->wLength = cpu_to_le16(2);
508
509 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
510 (unsigned char *)dr, buffer, 2,
511 mos7840_control_callback, mcs);
512 mcs->control_urb->transfer_buffer_length = 2;
513 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
514 if (ret)
515 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
516
517 return ret;
518 }
519
520 static void mos7840_set_led_callback(struct urb *urb)
521 {
522 switch (urb->status) {
523 case 0:
524 /* Success */
525 break;
526 case -ECONNRESET:
527 case -ENOENT:
528 case -ESHUTDOWN:
529 /* This urb is terminated, clean up */
530 dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
531 __func__, urb->status);
532 break;
533 default:
534 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
535 __func__, urb->status);
536 }
537 }
538
539 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
540 __u16 reg)
541 {
542 struct usb_device *dev = mcs->port->serial->dev;
543 struct usb_ctrlrequest *dr = mcs->led_dr;
544
545 dr->bRequestType = MCS_WR_RTYPE;
546 dr->bRequest = MCS_WRREQ;
547 dr->wValue = cpu_to_le16(wval);
548 dr->wIndex = cpu_to_le16(reg);
549 dr->wLength = cpu_to_le16(0);
550
551 usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
552 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
553
554 usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
555 }
556
557 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
558 __u16 val)
559 {
560 struct usb_device *dev = port->serial->dev;
561
562 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
563 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
564 }
565
566 static void mos7840_led_off(struct timer_list *t)
567 {
568 struct moschip_port *mcs = from_timer(mcs, t, led_timer1);
569
570 /* Turn off LED */
571 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
572 mod_timer(&mcs->led_timer2,
573 jiffies + msecs_to_jiffies(LED_OFF_MS));
574 }
575
576 static void mos7840_led_flag_off(struct timer_list *t)
577 {
578 struct moschip_port *mcs = from_timer(mcs, t, led_timer2);
579
580 clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
581 }
582
583 static void mos7840_led_activity(struct usb_serial_port *port)
584 {
585 struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
586
587 if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
588 return;
589
590 mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
591 mod_timer(&mos7840_port->led_timer1,
592 jiffies + msecs_to_jiffies(LED_ON_MS));
593 }
594
595 /*****************************************************************************
596 * mos7840_interrupt_callback
597 * this is the callback function for when we have received data on the
598 * interrupt endpoint.
599 *****************************************************************************/
600
601 static void mos7840_interrupt_callback(struct urb *urb)
602 {
603 int result;
604 int length;
605 struct moschip_port *mos7840_port;
606 struct usb_serial *serial;
607 __u16 Data;
608 unsigned char *data;
609 __u8 sp[5], st;
610 int i, rv = 0;
611 __u16 wval, wreg = 0;
612 int status = urb->status;
613
614 switch (status) {
615 case 0:
616 /* success */
617 break;
618 case -ECONNRESET:
619 case -ENOENT:
620 case -ESHUTDOWN:
621 /* this urb is terminated, clean up */
622 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
623 __func__, status);
624 return;
625 default:
626 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
627 __func__, status);
628 goto exit;
629 }
630
631 length = urb->actual_length;
632 data = urb->transfer_buffer;
633
634 serial = urb->context;
635
636 /* Moschip get 5 bytes
637 * Byte 1 IIR Port 1 (port.number is 0)
638 * Byte 2 IIR Port 2 (port.number is 1)
639 * Byte 3 IIR Port 3 (port.number is 2)
640 * Byte 4 IIR Port 4 (port.number is 3)
641 * Byte 5 FIFO status for both */
642
643 if (length > 5) {
644 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
645 return;
646 }
647
648 sp[0] = (__u8) data[0];
649 sp[1] = (__u8) data[1];
650 sp[2] = (__u8) data[2];
651 sp[3] = (__u8) data[3];
652 st = (__u8) data[4];
653
654 for (i = 0; i < serial->num_ports; i++) {
655 mos7840_port = mos7840_get_port_private(serial->port[i]);
656 wval = ((__u16)serial->port[i]->port_number + 1) << 8;
657 if (mos7840_port->open) {
658 if (sp[i] & 0x01) {
659 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
660 } else {
661 switch (sp[i] & 0x0f) {
662 case SERIAL_IIR_RLS:
663 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
664 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
665 mos7840_port->MsrLsr = 1;
666 wreg = LINE_STATUS_REGISTER;
667 break;
668 case SERIAL_IIR_MS:
669 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
670 mos7840_port->MsrLsr = 0;
671 wreg = MODEM_STATUS_REGISTER;
672 break;
673 }
674 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
675 }
676 }
677 }
678 if (!(rv < 0))
679 /* the completion handler for the control urb will resubmit */
680 return;
681 exit:
682 result = usb_submit_urb(urb, GFP_ATOMIC);
683 if (result) {
684 dev_err(&urb->dev->dev,
685 "%s - Error %d submitting interrupt urb\n",
686 __func__, result);
687 }
688 }
689
690 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
691 const char *function)
692 {
693 if (!port) {
694 pr_debug("%s - port == NULL\n", function);
695 return -1;
696 }
697 if (!port->serial) {
698 pr_debug("%s - port->serial == NULL\n", function);
699 return -1;
700 }
701
702 return 0;
703 }
704
705 /* Inline functions to check the sanity of a pointer that is passed to us */
706 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
707 const char *function)
708 {
709 if (!serial) {
710 pr_debug("%s - serial == NULL\n", function);
711 return -1;
712 }
713 if (!serial->type) {
714 pr_debug("%s - serial->type == NULL!\n", function);
715 return -1;
716 }
717
718 return 0;
719 }
720
721 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
722 const char *function)
723 {
724 /* if no port was specified, or it fails a paranoia check */
725 if (!port ||
726 mos7840_port_paranoia_check(port, function) ||
727 mos7840_serial_paranoia_check(port->serial, function)) {
728 /* then say that we don't have a valid usb_serial thing,
729 * which will end up genrating -ENODEV return values */
730 return NULL;
731 }
732
733 return port->serial;
734 }
735
736 /*****************************************************************************
737 * mos7840_bulk_in_callback
738 * this is the callback function for when we have received data on the
739 * bulk in endpoint.
740 *****************************************************************************/
741
742 static void mos7840_bulk_in_callback(struct urb *urb)
743 {
744 int retval;
745 unsigned char *data;
746 struct usb_serial *serial;
747 struct usb_serial_port *port;
748 struct moschip_port *mos7840_port;
749 int status = urb->status;
750
751 mos7840_port = urb->context;
752 if (!mos7840_port)
753 return;
754
755 if (status) {
756 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
757 mos7840_port->read_urb_busy = false;
758 return;
759 }
760
761 port = mos7840_port->port;
762 if (mos7840_port_paranoia_check(port, __func__)) {
763 mos7840_port->read_urb_busy = false;
764 return;
765 }
766
767 serial = mos7840_get_usb_serial(port, __func__);
768 if (!serial) {
769 mos7840_port->read_urb_busy = false;
770 return;
771 }
772
773 data = urb->transfer_buffer;
774 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
775
776 if (urb->actual_length) {
777 struct tty_port *tport = &mos7840_port->port->port;
778 tty_insert_flip_string(tport, data, urb->actual_length);
779 tty_flip_buffer_push(tport);
780 port->icount.rx += urb->actual_length;
781 dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
782 }
783
784 if (!mos7840_port->read_urb) {
785 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
786 mos7840_port->read_urb_busy = false;
787 return;
788 }
789
790 if (mos7840_port->has_led)
791 mos7840_led_activity(port);
792
793 mos7840_port->read_urb_busy = true;
794 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
795
796 if (retval) {
797 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
798 mos7840_port->read_urb_busy = false;
799 }
800 }
801
802 /*****************************************************************************
803 * mos7840_bulk_out_data_callback
804 * this is the callback function for when we have finished sending
805 * serial data on the bulk out endpoint.
806 *****************************************************************************/
807
808 static void mos7840_bulk_out_data_callback(struct urb *urb)
809 {
810 struct moschip_port *mos7840_port;
811 struct usb_serial_port *port;
812 int status = urb->status;
813 int i;
814
815 mos7840_port = urb->context;
816 port = mos7840_port->port;
817 spin_lock(&mos7840_port->pool_lock);
818 for (i = 0; i < NUM_URBS; i++) {
819 if (urb == mos7840_port->write_urb_pool[i]) {
820 mos7840_port->busy[i] = 0;
821 break;
822 }
823 }
824 spin_unlock(&mos7840_port->pool_lock);
825
826 if (status) {
827 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
828 return;
829 }
830
831 if (mos7840_port_paranoia_check(port, __func__))
832 return;
833
834 if (mos7840_port->open)
835 tty_port_tty_wakeup(&port->port);
836
837 }
838
839 /************************************************************************/
840 /* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */
841 /************************************************************************/
842
843 /*****************************************************************************
844 * mos7840_open
845 * this function is called by the tty driver when a port is opened
846 * If successful, we return 0
847 * Otherwise we return a negative error number.
848 *****************************************************************************/
849
850 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
851 {
852 int response;
853 int j;
854 struct usb_serial *serial;
855 struct urb *urb;
856 __u16 Data;
857 int status;
858 struct moschip_port *mos7840_port;
859 struct moschip_port *port0;
860
861 if (mos7840_port_paranoia_check(port, __func__))
862 return -ENODEV;
863
864 serial = port->serial;
865
866 if (mos7840_serial_paranoia_check(serial, __func__))
867 return -ENODEV;
868
869 mos7840_port = mos7840_get_port_private(port);
870 port0 = mos7840_get_port_private(serial->port[0]);
871
872 if (mos7840_port == NULL || port0 == NULL)
873 return -ENODEV;
874
875 usb_clear_halt(serial->dev, port->write_urb->pipe);
876 usb_clear_halt(serial->dev, port->read_urb->pipe);
877 port0->open_ports++;
878
879 /* Initialising the write urb pool */
880 for (j = 0; j < NUM_URBS; ++j) {
881 urb = usb_alloc_urb(0, GFP_KERNEL);
882 mos7840_port->write_urb_pool[j] = urb;
883 if (!urb)
884 continue;
885
886 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
887 GFP_KERNEL);
888 if (!urb->transfer_buffer) {
889 usb_free_urb(urb);
890 mos7840_port->write_urb_pool[j] = NULL;
891 continue;
892 }
893 }
894
895 /*****************************************************************************
896 * Initialize MCS7840 -- Write Init values to corresponding Registers
897 *
898 * Register Index
899 * 1 : IER
900 * 2 : FCR
901 * 3 : LCR
902 * 4 : MCR
903 *
904 * 0x08 : SP1/2 Control Reg
905 *****************************************************************************/
906
907 /* NEED to check the following Block */
908
909 Data = 0x0;
910 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
911 if (status < 0) {
912 dev_dbg(&port->dev, "Reading Spreg failed\n");
913 goto err;
914 }
915 Data |= 0x80;
916 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
917 if (status < 0) {
918 dev_dbg(&port->dev, "writing Spreg failed\n");
919 goto err;
920 }
921
922 Data &= ~0x80;
923 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
924 if (status < 0) {
925 dev_dbg(&port->dev, "writing Spreg failed\n");
926 goto err;
927 }
928 /* End of block to be checked */
929
930 Data = 0x0;
931 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
932 &Data);
933 if (status < 0) {
934 dev_dbg(&port->dev, "Reading Controlreg failed\n");
935 goto err;
936 }
937 Data |= 0x08; /* Driver done bit */
938 Data |= 0x20; /* rx_disable */
939 status = mos7840_set_reg_sync(port,
940 mos7840_port->ControlRegOffset, Data);
941 if (status < 0) {
942 dev_dbg(&port->dev, "writing Controlreg failed\n");
943 goto err;
944 }
945 /* do register settings here */
946 /* Set all regs to the device default values. */
947 /***********************************
948 * First Disable all interrupts.
949 ***********************************/
950 Data = 0x00;
951 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
952 if (status < 0) {
953 dev_dbg(&port->dev, "disabling interrupts failed\n");
954 goto err;
955 }
956 /* Set FIFO_CONTROL_REGISTER to the default value */
957 Data = 0x00;
958 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
959 if (status < 0) {
960 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
961 goto err;
962 }
963
964 Data = 0xcf;
965 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
966 if (status < 0) {
967 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
968 goto err;
969 }
970
971 Data = 0x03;
972 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
973 mos7840_port->shadowLCR = Data;
974
975 Data = 0x0b;
976 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
977 mos7840_port->shadowMCR = Data;
978
979 Data = 0x00;
980 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
981 mos7840_port->shadowLCR = Data;
982
983 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
984 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
985
986 Data = 0x0c;
987 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
988
989 Data = 0x0;
990 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
991
992 Data = 0x00;
993 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
994
995 Data = Data & ~SERIAL_LCR_DLAB;
996 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
997 mos7840_port->shadowLCR = Data;
998
999 /* clearing Bulkin and Bulkout Fifo */
1000 Data = 0x0;
1001 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1002
1003 Data = Data | 0x0c;
1004 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1005
1006 Data = Data & ~0x0c;
1007 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1008 /* Finally enable all interrupts */
1009 Data = 0x0c;
1010 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1011
1012 /* clearing rx_disable */
1013 Data = 0x0;
1014 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1015 &Data);
1016 Data = Data & ~0x20;
1017 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1018 Data);
1019
1020 /* rx_negate */
1021 Data = 0x0;
1022 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1023 &Data);
1024 Data = Data | 0x10;
1025 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1026 Data);
1027
1028 /* Check to see if we've set up our endpoint info yet *
1029 * (can't set it up in mos7840_startup as the structures *
1030 * were not set up at that time.) */
1031 if (port0->open_ports == 1) {
1032 /* FIXME: Buffer never NULL, so URB is not submitted. */
1033 if (serial->port[0]->interrupt_in_buffer == NULL) {
1034 /* set up interrupt urb */
1035 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1036 serial->dev,
1037 usb_rcvintpipe(serial->dev,
1038 serial->port[0]->interrupt_in_endpointAddress),
1039 serial->port[0]->interrupt_in_buffer,
1040 serial->port[0]->interrupt_in_urb->
1041 transfer_buffer_length,
1042 mos7840_interrupt_callback,
1043 serial,
1044 serial->port[0]->interrupt_in_urb->interval);
1045
1046 /* start interrupt read for mos7840 */
1047 response =
1048 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1049 GFP_KERNEL);
1050 if (response) {
1051 dev_err(&port->dev, "%s - Error %d submitting "
1052 "interrupt urb\n", __func__, response);
1053 }
1054
1055 }
1056
1057 }
1058
1059 /* see if we've set up our endpoint info yet *
1060 * (can't set it up in mos7840_startup as the *
1061 * structures were not set up at that time.) */
1062
1063 dev_dbg(&port->dev, "port number is %d\n", port->port_number);
1064 dev_dbg(&port->dev, "minor number is %d\n", port->minor);
1065 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1066 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1067 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1068 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
1069 mos7840_port->read_urb = port->read_urb;
1070
1071 /* set up our bulk in urb */
1072 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1073 usb_fill_bulk_urb(mos7840_port->read_urb,
1074 serial->dev,
1075 usb_rcvbulkpipe(serial->dev,
1076 (port->bulk_in_endpointAddress) + 2),
1077 port->bulk_in_buffer,
1078 mos7840_port->read_urb->transfer_buffer_length,
1079 mos7840_bulk_in_callback, mos7840_port);
1080 } else {
1081 usb_fill_bulk_urb(mos7840_port->read_urb,
1082 serial->dev,
1083 usb_rcvbulkpipe(serial->dev,
1084 port->bulk_in_endpointAddress),
1085 port->bulk_in_buffer,
1086 mos7840_port->read_urb->transfer_buffer_length,
1087 mos7840_bulk_in_callback, mos7840_port);
1088 }
1089
1090 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
1091 mos7840_port->read_urb_busy = true;
1092 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1093 if (response) {
1094 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1095 __func__, response);
1096 mos7840_port->read_urb_busy = false;
1097 }
1098
1099 /* initialize our port settings */
1100 /* Must set to enable ints! */
1101 mos7840_port->shadowMCR = MCR_MASTER_IE;
1102 /* send a open port command */
1103 mos7840_port->open = 1;
1104 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1105
1106 return 0;
1107 err:
1108 for (j = 0; j < NUM_URBS; ++j) {
1109 urb = mos7840_port->write_urb_pool[j];
1110 if (!urb)
1111 continue;
1112 kfree(urb->transfer_buffer);
1113 usb_free_urb(urb);
1114 }
1115 return status;
1116 }
1117
1118 /*****************************************************************************
1119 * mos7840_chars_in_buffer
1120 * this function is called by the tty driver when it wants to know how many
1121 * bytes of data we currently have outstanding in the port (data that has
1122 * been written, but hasn't made it out the port yet)
1123 * If successful, we return the number of bytes left to be written in the
1124 * system,
1125 * Otherwise we return zero.
1126 *****************************************************************************/
1127
1128 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1129 {
1130 struct usb_serial_port *port = tty->driver_data;
1131 int i;
1132 int chars = 0;
1133 unsigned long flags;
1134 struct moschip_port *mos7840_port;
1135
1136 if (mos7840_port_paranoia_check(port, __func__))
1137 return 0;
1138
1139 mos7840_port = mos7840_get_port_private(port);
1140 if (mos7840_port == NULL)
1141 return 0;
1142
1143 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1144 for (i = 0; i < NUM_URBS; ++i) {
1145 if (mos7840_port->busy[i]) {
1146 struct urb *urb = mos7840_port->write_urb_pool[i];
1147 chars += urb->transfer_buffer_length;
1148 }
1149 }
1150 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1151 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1152 return chars;
1153
1154 }
1155
1156 /*****************************************************************************
1157 * mos7840_close
1158 * this function is called by the tty driver when a port is closed
1159 *****************************************************************************/
1160
1161 static void mos7840_close(struct usb_serial_port *port)
1162 {
1163 struct usb_serial *serial;
1164 struct moschip_port *mos7840_port;
1165 struct moschip_port *port0;
1166 int j;
1167 __u16 Data;
1168
1169 if (mos7840_port_paranoia_check(port, __func__))
1170 return;
1171
1172 serial = mos7840_get_usb_serial(port, __func__);
1173 if (!serial)
1174 return;
1175
1176 mos7840_port = mos7840_get_port_private(port);
1177 port0 = mos7840_get_port_private(serial->port[0]);
1178
1179 if (mos7840_port == NULL || port0 == NULL)
1180 return;
1181
1182 for (j = 0; j < NUM_URBS; ++j)
1183 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1184
1185 /* Freeing Write URBs */
1186 for (j = 0; j < NUM_URBS; ++j) {
1187 if (mos7840_port->write_urb_pool[j]) {
1188 kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
1189 usb_free_urb(mos7840_port->write_urb_pool[j]);
1190 }
1191 }
1192
1193 usb_kill_urb(mos7840_port->read_urb);
1194 mos7840_port->read_urb_busy = false;
1195
1196 port0->open_ports--;
1197 dev_dbg(&port->dev, "%s in close%d\n", __func__, port0->open_ports);
1198 if (port0->open_ports == 0) {
1199 if (serial->port[0]->interrupt_in_urb) {
1200 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1201 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1202 }
1203 }
1204
1205 Data = 0x0;
1206 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1207
1208 Data = 0x00;
1209 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1210
1211 mos7840_port->open = 0;
1212 }
1213
1214 /*****************************************************************************
1215 * mos7840_break
1216 * this function sends a break to the port
1217 *****************************************************************************/
1218 static void mos7840_break(struct tty_struct *tty, int break_state)
1219 {
1220 struct usb_serial_port *port = tty->driver_data;
1221 unsigned char data;
1222 struct usb_serial *serial;
1223 struct moschip_port *mos7840_port;
1224
1225 if (mos7840_port_paranoia_check(port, __func__))
1226 return;
1227
1228 serial = mos7840_get_usb_serial(port, __func__);
1229 if (!serial)
1230 return;
1231
1232 mos7840_port = mos7840_get_port_private(port);
1233
1234 if (mos7840_port == NULL)
1235 return;
1236
1237 if (break_state == -1)
1238 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1239 else
1240 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1241
1242 /* FIXME: no locking on shadowLCR anywhere in driver */
1243 mos7840_port->shadowLCR = data;
1244 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1245 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1246 mos7840_port->shadowLCR);
1247 }
1248
1249 /*****************************************************************************
1250 * mos7840_write_room
1251 * this function is called by the tty driver when it wants to know how many
1252 * bytes of data we can accept for a specific port.
1253 * If successful, we return the amount of room that we have for this port
1254 * Otherwise we return a negative error number.
1255 *****************************************************************************/
1256
1257 static int mos7840_write_room(struct tty_struct *tty)
1258 {
1259 struct usb_serial_port *port = tty->driver_data;
1260 int i;
1261 int room = 0;
1262 unsigned long flags;
1263 struct moschip_port *mos7840_port;
1264
1265 if (mos7840_port_paranoia_check(port, __func__))
1266 return -1;
1267
1268 mos7840_port = mos7840_get_port_private(port);
1269 if (mos7840_port == NULL)
1270 return -1;
1271
1272 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1273 for (i = 0; i < NUM_URBS; ++i) {
1274 if (!mos7840_port->busy[i])
1275 room += URB_TRANSFER_BUFFER_SIZE;
1276 }
1277 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1278
1279 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1280 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
1281 return room;
1282
1283 }
1284
1285 /*****************************************************************************
1286 * mos7840_write
1287 * this function is called by the tty driver when data should be written to
1288 * the port.
1289 * If successful, we return the number of bytes written, otherwise we
1290 * return a negative error number.
1291 *****************************************************************************/
1292
1293 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1294 const unsigned char *data, int count)
1295 {
1296 int status;
1297 int i;
1298 int bytes_sent = 0;
1299 int transfer_size;
1300 unsigned long flags;
1301
1302 struct moschip_port *mos7840_port;
1303 struct usb_serial *serial;
1304 struct urb *urb;
1305 /* __u16 Data; */
1306 const unsigned char *current_position = data;
1307 unsigned char *data1;
1308
1309 if (mos7840_port_paranoia_check(port, __func__))
1310 return -1;
1311
1312 serial = port->serial;
1313 if (mos7840_serial_paranoia_check(serial, __func__))
1314 return -1;
1315
1316 mos7840_port = mos7840_get_port_private(port);
1317 if (mos7840_port == NULL)
1318 return -1;
1319
1320 /* try to find a free urb in the list */
1321 urb = NULL;
1322
1323 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1324 for (i = 0; i < NUM_URBS; ++i) {
1325 if (!mos7840_port->busy[i]) {
1326 mos7840_port->busy[i] = 1;
1327 urb = mos7840_port->write_urb_pool[i];
1328 dev_dbg(&port->dev, "URB:%d\n", i);
1329 break;
1330 }
1331 }
1332 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1333
1334 if (urb == NULL) {
1335 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1336 goto exit;
1337 }
1338
1339 if (urb->transfer_buffer == NULL) {
1340 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1341 GFP_ATOMIC);
1342 if (!urb->transfer_buffer)
1343 goto exit;
1344 }
1345 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1346
1347 memcpy(urb->transfer_buffer, current_position, transfer_size);
1348
1349 /* fill urb with data and submit */
1350 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1351 usb_fill_bulk_urb(urb,
1352 serial->dev,
1353 usb_sndbulkpipe(serial->dev,
1354 (port->bulk_out_endpointAddress) + 2),
1355 urb->transfer_buffer,
1356 transfer_size,
1357 mos7840_bulk_out_data_callback, mos7840_port);
1358 } else {
1359 usb_fill_bulk_urb(urb,
1360 serial->dev,
1361 usb_sndbulkpipe(serial->dev,
1362 port->bulk_out_endpointAddress),
1363 urb->transfer_buffer,
1364 transfer_size,
1365 mos7840_bulk_out_data_callback, mos7840_port);
1366 }
1367
1368 data1 = urb->transfer_buffer;
1369 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
1370
1371 if (mos7840_port->has_led)
1372 mos7840_led_activity(port);
1373
1374 /* send it down the pipe */
1375 status = usb_submit_urb(urb, GFP_ATOMIC);
1376
1377 if (status) {
1378 mos7840_port->busy[i] = 0;
1379 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1380 "with status = %d\n", __func__, status);
1381 bytes_sent = status;
1382 goto exit;
1383 }
1384 bytes_sent = transfer_size;
1385 port->icount.tx += transfer_size;
1386 dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
1387 exit:
1388 return bytes_sent;
1389
1390 }
1391
1392 /*****************************************************************************
1393 * mos7840_throttle
1394 * this function is called by the tty driver when it wants to stop the data
1395 * being read from the port.
1396 *****************************************************************************/
1397
1398 static void mos7840_throttle(struct tty_struct *tty)
1399 {
1400 struct usb_serial_port *port = tty->driver_data;
1401 struct moschip_port *mos7840_port;
1402 int status;
1403
1404 if (mos7840_port_paranoia_check(port, __func__))
1405 return;
1406
1407 mos7840_port = mos7840_get_port_private(port);
1408
1409 if (mos7840_port == NULL)
1410 return;
1411
1412 if (!mos7840_port->open) {
1413 dev_dbg(&port->dev, "%s", "port not opened\n");
1414 return;
1415 }
1416
1417 /* if we are implementing XON/XOFF, send the stop character */
1418 if (I_IXOFF(tty)) {
1419 unsigned char stop_char = STOP_CHAR(tty);
1420 status = mos7840_write(tty, port, &stop_char, 1);
1421 if (status <= 0)
1422 return;
1423 }
1424 /* if we are implementing RTS/CTS, toggle that line */
1425 if (C_CRTSCTS(tty)) {
1426 mos7840_port->shadowMCR &= ~MCR_RTS;
1427 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1428 mos7840_port->shadowMCR);
1429 if (status < 0)
1430 return;
1431 }
1432 }
1433
1434 /*****************************************************************************
1435 * mos7840_unthrottle
1436 * this function is called by the tty driver when it wants to resume
1437 * the data being read from the port (called after mos7840_throttle is
1438 * called)
1439 *****************************************************************************/
1440 static void mos7840_unthrottle(struct tty_struct *tty)
1441 {
1442 struct usb_serial_port *port = tty->driver_data;
1443 int status;
1444 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1445
1446 if (mos7840_port_paranoia_check(port, __func__))
1447 return;
1448
1449 if (mos7840_port == NULL)
1450 return;
1451
1452 if (!mos7840_port->open) {
1453 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1454 return;
1455 }
1456
1457 /* if we are implementing XON/XOFF, send the start character */
1458 if (I_IXOFF(tty)) {
1459 unsigned char start_char = START_CHAR(tty);
1460 status = mos7840_write(tty, port, &start_char, 1);
1461 if (status <= 0)
1462 return;
1463 }
1464
1465 /* if we are implementing RTS/CTS, toggle that line */
1466 if (C_CRTSCTS(tty)) {
1467 mos7840_port->shadowMCR |= MCR_RTS;
1468 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1469 mos7840_port->shadowMCR);
1470 if (status < 0)
1471 return;
1472 }
1473 }
1474
1475 static int mos7840_tiocmget(struct tty_struct *tty)
1476 {
1477 struct usb_serial_port *port = tty->driver_data;
1478 struct moschip_port *mos7840_port;
1479 unsigned int result;
1480 __u16 msr;
1481 __u16 mcr;
1482 int status;
1483 mos7840_port = mos7840_get_port_private(port);
1484
1485 if (mos7840_port == NULL)
1486 return -ENODEV;
1487
1488 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1489 if (status < 0)
1490 return -EIO;
1491 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1492 if (status < 0)
1493 return -EIO;
1494 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1495 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1496 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1497 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1498 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1499 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1500 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1501
1502 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1503
1504 return result;
1505 }
1506
1507 static int mos7840_tiocmset(struct tty_struct *tty,
1508 unsigned int set, unsigned int clear)
1509 {
1510 struct usb_serial_port *port = tty->driver_data;
1511 struct moschip_port *mos7840_port;
1512 unsigned int mcr;
1513 int status;
1514
1515 mos7840_port = mos7840_get_port_private(port);
1516
1517 if (mos7840_port == NULL)
1518 return -ENODEV;
1519
1520 /* FIXME: What locks the port registers ? */
1521 mcr = mos7840_port->shadowMCR;
1522 if (clear & TIOCM_RTS)
1523 mcr &= ~MCR_RTS;
1524 if (clear & TIOCM_DTR)
1525 mcr &= ~MCR_DTR;
1526 if (clear & TIOCM_LOOP)
1527 mcr &= ~MCR_LOOPBACK;
1528
1529 if (set & TIOCM_RTS)
1530 mcr |= MCR_RTS;
1531 if (set & TIOCM_DTR)
1532 mcr |= MCR_DTR;
1533 if (set & TIOCM_LOOP)
1534 mcr |= MCR_LOOPBACK;
1535
1536 mos7840_port->shadowMCR = mcr;
1537
1538 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1539 if (status < 0) {
1540 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1541 return status;
1542 }
1543
1544 return 0;
1545 }
1546
1547 /*****************************************************************************
1548 * mos7840_calc_baud_rate_divisor
1549 * this function calculates the proper baud rate divisor for the specified
1550 * baud rate.
1551 *****************************************************************************/
1552 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1553 int baudRate, int *divisor,
1554 __u16 *clk_sel_val)
1555 {
1556 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1557
1558 if (baudRate <= 115200) {
1559 *divisor = 115200 / baudRate;
1560 *clk_sel_val = 0x0;
1561 }
1562 if ((baudRate > 115200) && (baudRate <= 230400)) {
1563 *divisor = 230400 / baudRate;
1564 *clk_sel_val = 0x10;
1565 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1566 *divisor = 403200 / baudRate;
1567 *clk_sel_val = 0x20;
1568 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1569 *divisor = 460800 / baudRate;
1570 *clk_sel_val = 0x30;
1571 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1572 *divisor = 806400 / baudRate;
1573 *clk_sel_val = 0x40;
1574 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1575 *divisor = 921600 / baudRate;
1576 *clk_sel_val = 0x50;
1577 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1578 *divisor = 1572864 / baudRate;
1579 *clk_sel_val = 0x60;
1580 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1581 *divisor = 3145728 / baudRate;
1582 *clk_sel_val = 0x70;
1583 }
1584 return 0;
1585 }
1586
1587 /*****************************************************************************
1588 * mos7840_send_cmd_write_baud_rate
1589 * this function sends the proper command to change the baud rate of the
1590 * specified port.
1591 *****************************************************************************/
1592
1593 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1594 int baudRate)
1595 {
1596 int divisor = 0;
1597 int status;
1598 __u16 Data;
1599 unsigned char number;
1600 __u16 clk_sel_val;
1601 struct usb_serial_port *port;
1602
1603 if (mos7840_port == NULL)
1604 return -1;
1605
1606 port = mos7840_port->port;
1607 if (mos7840_port_paranoia_check(port, __func__))
1608 return -1;
1609
1610 if (mos7840_serial_paranoia_check(port->serial, __func__))
1611 return -1;
1612
1613 number = mos7840_port->port->port_number;
1614
1615 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
1616 /* reset clk_uart_sel in spregOffset */
1617 if (baudRate > 115200) {
1618 #ifdef HW_flow_control
1619 /* NOTE: need to see the pther register to modify */
1620 /* setting h/w flow control bit to 1 */
1621 Data = 0x2b;
1622 mos7840_port->shadowMCR = Data;
1623 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1624 Data);
1625 if (status < 0) {
1626 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1627 return -1;
1628 }
1629 #endif
1630
1631 } else {
1632 #ifdef HW_flow_control
1633 /* setting h/w flow control bit to 0 */
1634 Data = 0xb;
1635 mos7840_port->shadowMCR = Data;
1636 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1637 Data);
1638 if (status < 0) {
1639 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1640 return -1;
1641 }
1642 #endif
1643
1644 }
1645
1646 if (1) { /* baudRate <= 115200) */
1647 clk_sel_val = 0x0;
1648 Data = 0x0;
1649 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1650 &clk_sel_val);
1651 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1652 &Data);
1653 if (status < 0) {
1654 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1655 return -1;
1656 }
1657 Data = (Data & 0x8f) | clk_sel_val;
1658 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1659 Data);
1660 if (status < 0) {
1661 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1662 return -1;
1663 }
1664 /* Calculate the Divisor */
1665
1666 if (status) {
1667 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1668 return status;
1669 }
1670 /* Enable access to divisor latch */
1671 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1672 mos7840_port->shadowLCR = Data;
1673 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1674
1675 /* Write the divisor */
1676 Data = (unsigned char)(divisor & 0xff);
1677 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1678 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1679
1680 Data = (unsigned char)((divisor & 0xff00) >> 8);
1681 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1682 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1683
1684 /* Disable access to divisor latch */
1685 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1686 mos7840_port->shadowLCR = Data;
1687 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1688
1689 }
1690 return status;
1691 }
1692
1693 /*****************************************************************************
1694 * mos7840_change_port_settings
1695 * This routine is called to set the UART on the device to match
1696 * the specified new settings.
1697 *****************************************************************************/
1698
1699 static void mos7840_change_port_settings(struct tty_struct *tty,
1700 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1701 {
1702 int baud;
1703 unsigned cflag;
1704 unsigned iflag;
1705 __u8 lData;
1706 __u8 lParity;
1707 __u8 lStop;
1708 int status;
1709 __u16 Data;
1710 struct usb_serial_port *port;
1711 struct usb_serial *serial;
1712
1713 if (mos7840_port == NULL)
1714 return;
1715
1716 port = mos7840_port->port;
1717
1718 if (mos7840_port_paranoia_check(port, __func__))
1719 return;
1720
1721 if (mos7840_serial_paranoia_check(port->serial, __func__))
1722 return;
1723
1724 serial = port->serial;
1725
1726 if (!mos7840_port->open) {
1727 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1728 return;
1729 }
1730
1731 lData = LCR_BITS_8;
1732 lStop = LCR_STOP_1;
1733 lParity = LCR_PAR_NONE;
1734
1735 cflag = tty->termios.c_cflag;
1736 iflag = tty->termios.c_iflag;
1737
1738 /* Change the number of bits */
1739 switch (cflag & CSIZE) {
1740 case CS5:
1741 lData = LCR_BITS_5;
1742 break;
1743
1744 case CS6:
1745 lData = LCR_BITS_6;
1746 break;
1747
1748 case CS7:
1749 lData = LCR_BITS_7;
1750 break;
1751
1752 default:
1753 case CS8:
1754 lData = LCR_BITS_8;
1755 break;
1756 }
1757
1758 /* Change the Parity bit */
1759 if (cflag & PARENB) {
1760 if (cflag & PARODD) {
1761 lParity = LCR_PAR_ODD;
1762 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1763 } else {
1764 lParity = LCR_PAR_EVEN;
1765 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1766 }
1767
1768 } else {
1769 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1770 }
1771
1772 if (cflag & CMSPAR)
1773 lParity = lParity | 0x20;
1774
1775 /* Change the Stop bit */
1776 if (cflag & CSTOPB) {
1777 lStop = LCR_STOP_2;
1778 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1779 } else {
1780 lStop = LCR_STOP_1;
1781 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1782 }
1783
1784 /* Update the LCR with the correct value */
1785 mos7840_port->shadowLCR &=
1786 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1787 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1788
1789 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1790 mos7840_port->shadowLCR);
1791 /* Disable Interrupts */
1792 Data = 0x00;
1793 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1794
1795 Data = 0x00;
1796 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1797
1798 Data = 0xcf;
1799 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1800
1801 /* Send the updated LCR value to the mos7840 */
1802 Data = mos7840_port->shadowLCR;
1803
1804 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1805
1806 Data = 0x00b;
1807 mos7840_port->shadowMCR = Data;
1808 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1809 Data = 0x00b;
1810 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1811
1812 /* set up the MCR register and send it to the mos7840 */
1813
1814 mos7840_port->shadowMCR = MCR_MASTER_IE;
1815 if (cflag & CBAUD)
1816 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
1817
1818 if (cflag & CRTSCTS)
1819 mos7840_port->shadowMCR |= (MCR_XON_ANY);
1820 else
1821 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
1822
1823 Data = mos7840_port->shadowMCR;
1824 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1825
1826 /* Determine divisor based on baud rate */
1827 baud = tty_get_baud_rate(tty);
1828
1829 if (!baud) {
1830 /* pick a default, any default... */
1831 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
1832 baud = 9600;
1833 }
1834
1835 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1836 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1837
1838 /* Enable Interrupts */
1839 Data = 0x0c;
1840 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1841
1842 if (!mos7840_port->read_urb_busy) {
1843 mos7840_port->read_urb_busy = true;
1844 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1845 if (status) {
1846 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1847 status);
1848 mos7840_port->read_urb_busy = false;
1849 }
1850 }
1851 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1852 mos7840_port->shadowLCR);
1853 }
1854
1855 /*****************************************************************************
1856 * mos7840_set_termios
1857 * this function is called by the tty driver when it wants to change
1858 * the termios structure
1859 *****************************************************************************/
1860
1861 static void mos7840_set_termios(struct tty_struct *tty,
1862 struct usb_serial_port *port,
1863 struct ktermios *old_termios)
1864 {
1865 int status;
1866 struct usb_serial *serial;
1867 struct moschip_port *mos7840_port;
1868
1869 if (mos7840_port_paranoia_check(port, __func__))
1870 return;
1871
1872 serial = port->serial;
1873
1874 if (mos7840_serial_paranoia_check(serial, __func__))
1875 return;
1876
1877 mos7840_port = mos7840_get_port_private(port);
1878
1879 if (mos7840_port == NULL)
1880 return;
1881
1882 if (!mos7840_port->open) {
1883 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1884 return;
1885 }
1886
1887 /* change the port settings to the new ones specified */
1888
1889 mos7840_change_port_settings(tty, mos7840_port, old_termios);
1890
1891 if (!mos7840_port->read_urb) {
1892 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
1893 return;
1894 }
1895
1896 if (!mos7840_port->read_urb_busy) {
1897 mos7840_port->read_urb_busy = true;
1898 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1899 if (status) {
1900 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1901 status);
1902 mos7840_port->read_urb_busy = false;
1903 }
1904 }
1905 }
1906
1907 /*****************************************************************************
1908 * mos7840_get_lsr_info - get line status register info
1909 *
1910 * Purpose: Let user call ioctl() to get info when the UART physically
1911 * is emptied. On bus types like RS485, the transmitter must
1912 * release the bus after transmitting. This must be done when
1913 * the transmit shift register is empty, not be done when the
1914 * transmit holding register is empty. This functionality
1915 * allows an RS485 driver to be written in user space.
1916 *****************************************************************************/
1917
1918 static int mos7840_get_lsr_info(struct tty_struct *tty,
1919 unsigned int __user *value)
1920 {
1921 int count;
1922 unsigned int result = 0;
1923
1924 count = mos7840_chars_in_buffer(tty);
1925 if (count == 0)
1926 result = TIOCSER_TEMT;
1927
1928 if (copy_to_user(value, &result, sizeof(int)))
1929 return -EFAULT;
1930 return 0;
1931 }
1932
1933 /*****************************************************************************
1934 * mos7840_get_serial_info
1935 * function to get information about serial port
1936 *****************************************************************************/
1937
1938 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
1939 struct serial_struct __user *retinfo)
1940 {
1941 struct serial_struct tmp;
1942
1943 if (mos7840_port == NULL)
1944 return -1;
1945
1946 memset(&tmp, 0, sizeof(tmp));
1947
1948 tmp.type = PORT_16550A;
1949 tmp.line = mos7840_port->port->minor;
1950 tmp.port = mos7840_port->port->port_number;
1951 tmp.irq = 0;
1952 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1953 tmp.baud_base = 9600;
1954 tmp.close_delay = 5 * HZ;
1955 tmp.closing_wait = 30 * HZ;
1956
1957 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1958 return -EFAULT;
1959 return 0;
1960 }
1961
1962 /*****************************************************************************
1963 * SerialIoctl
1964 * this function handles any ioctl calls to the driver
1965 *****************************************************************************/
1966
1967 static int mos7840_ioctl(struct tty_struct *tty,
1968 unsigned int cmd, unsigned long arg)
1969 {
1970 struct usb_serial_port *port = tty->driver_data;
1971 void __user *argp = (void __user *)arg;
1972 struct moschip_port *mos7840_port;
1973
1974 if (mos7840_port_paranoia_check(port, __func__))
1975 return -1;
1976
1977 mos7840_port = mos7840_get_port_private(port);
1978
1979 if (mos7840_port == NULL)
1980 return -1;
1981
1982 switch (cmd) {
1983 /* return number of bytes available */
1984
1985 case TIOCSERGETLSR:
1986 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1987 return mos7840_get_lsr_info(tty, argp);
1988
1989 case TIOCGSERIAL:
1990 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
1991 return mos7840_get_serial_info(mos7840_port, argp);
1992
1993 case TIOCSSERIAL:
1994 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
1995 break;
1996 default:
1997 break;
1998 }
1999 return -ENOIOCTLCMD;
2000 }
2001
2002 static int mos7810_check(struct usb_serial *serial)
2003 {
2004 int i, pass_count = 0;
2005 u8 *buf;
2006 __u16 data = 0, mcr_data = 0;
2007 __u16 test_pattern = 0x55AA;
2008 int res;
2009
2010 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2011 if (!buf)
2012 return 0; /* failed to identify 7810 */
2013
2014 /* Store MCR setting */
2015 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2016 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2017 buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2018 if (res == VENDOR_READ_LENGTH)
2019 mcr_data = *buf;
2020
2021 for (i = 0; i < 16; i++) {
2022 /* Send the 1-bit test pattern out to MCS7810 test pin */
2023 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2024 MCS_WRREQ, MCS_WR_RTYPE,
2025 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2026 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2027
2028 /* Read the test pattern back */
2029 res = usb_control_msg(serial->dev,
2030 usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
2031 MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2032 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2033 if (res == VENDOR_READ_LENGTH)
2034 data = *buf;
2035
2036 /* If this is a MCS7810 device, both test patterns must match */
2037 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2038 break;
2039
2040 pass_count++;
2041 }
2042
2043 /* Restore MCR setting */
2044 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2045 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2046 0, MOS_WDR_TIMEOUT);
2047
2048 kfree(buf);
2049
2050 if (pass_count == 16)
2051 return 1;
2052
2053 return 0;
2054 }
2055
2056 static int mos7840_probe(struct usb_serial *serial,
2057 const struct usb_device_id *id)
2058 {
2059 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
2060 u16 vid = le16_to_cpu(serial->dev->descriptor.idVendor);
2061 u8 *buf;
2062 int device_type;
2063
2064 if (product == MOSCHIP_DEVICE_ID_7810 ||
2065 product == MOSCHIP_DEVICE_ID_7820) {
2066 device_type = product;
2067 goto out;
2068 }
2069
2070 if (vid == USB_VENDOR_ID_MOXA && product == MOXA_DEVICE_ID_2210) {
2071 device_type = MOSCHIP_DEVICE_ID_7820;
2072 goto out;
2073 }
2074
2075 buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2076 if (!buf)
2077 return -ENOMEM;
2078
2079 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2080 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2081 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2082
2083 /* For a MCS7840 device GPIO0 must be set to 1 */
2084 if (buf[0] & 0x01)
2085 device_type = MOSCHIP_DEVICE_ID_7840;
2086 else if (mos7810_check(serial))
2087 device_type = MOSCHIP_DEVICE_ID_7810;
2088 else
2089 device_type = MOSCHIP_DEVICE_ID_7820;
2090
2091 kfree(buf);
2092 out:
2093 usb_set_serial_data(serial, (void *)(unsigned long)device_type);
2094
2095 return 0;
2096 }
2097
2098 static int mos7840_calc_num_ports(struct usb_serial *serial,
2099 struct usb_serial_endpoints *epds)
2100 {
2101 int device_type = (unsigned long)usb_get_serial_data(serial);
2102 int num_ports;
2103
2104 num_ports = (device_type >> 4) & 0x000F;
2105
2106 /*
2107 * num_ports is currently never zero as device_type is one of
2108 * MOSCHIP_DEVICE_ID_78{1,2,4}0.
2109 */
2110 if (num_ports == 0)
2111 return -ENODEV;
2112
2113 if (epds->num_bulk_in < num_ports || epds->num_bulk_out < num_ports) {
2114 dev_err(&serial->interface->dev, "missing endpoints\n");
2115 return -ENODEV;
2116 }
2117
2118 return num_ports;
2119 }
2120
2121 static int mos7840_port_probe(struct usb_serial_port *port)
2122 {
2123 struct usb_serial *serial = port->serial;
2124 int device_type = (unsigned long)usb_get_serial_data(serial);
2125 struct moschip_port *mos7840_port;
2126 int status;
2127 int pnum;
2128 __u16 Data;
2129
2130 /* we set up the pointers to the endpoints in the mos7840_open *
2131 * function, as the structures aren't created yet. */
2132
2133 pnum = port->port_number;
2134
2135 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2136 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2137 if (!mos7840_port)
2138 return -ENOMEM;
2139
2140 /* Initialize all port interrupt end point to port 0 int
2141 * endpoint. Our device has only one interrupt end point
2142 * common to all port */
2143
2144 mos7840_port->port = port;
2145 mos7840_set_port_private(port, mos7840_port);
2146 spin_lock_init(&mos7840_port->pool_lock);
2147
2148 /* minor is not initialised until later by
2149 * usb-serial.c:get_free_serial() and cannot therefore be used
2150 * to index device instances */
2151 mos7840_port->port_num = pnum + 1;
2152 dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
2153 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2154
2155 if (mos7840_port->port_num == 1) {
2156 mos7840_port->SpRegOffset = 0x0;
2157 mos7840_port->ControlRegOffset = 0x1;
2158 mos7840_port->DcrRegOffset = 0x4;
2159 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
2160 mos7840_port->SpRegOffset = 0x8;
2161 mos7840_port->ControlRegOffset = 0x9;
2162 mos7840_port->DcrRegOffset = 0x16;
2163 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
2164 mos7840_port->SpRegOffset = 0xa;
2165 mos7840_port->ControlRegOffset = 0xb;
2166 mos7840_port->DcrRegOffset = 0x19;
2167 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
2168 mos7840_port->SpRegOffset = 0xa;
2169 mos7840_port->ControlRegOffset = 0xb;
2170 mos7840_port->DcrRegOffset = 0x19;
2171 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
2172 mos7840_port->SpRegOffset = 0xc;
2173 mos7840_port->ControlRegOffset = 0xd;
2174 mos7840_port->DcrRegOffset = 0x1c;
2175 }
2176 mos7840_dump_serial_port(port, mos7840_port);
2177 mos7840_set_port_private(port, mos7840_port);
2178
2179 /* enable rx_disable bit in control register */
2180 status = mos7840_get_reg_sync(port,
2181 mos7840_port->ControlRegOffset, &Data);
2182 if (status < 0) {
2183 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2184 goto out;
2185 } else
2186 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2187 Data |= 0x08; /* setting driver done bit */
2188 Data |= 0x04; /* sp1_bit to have cts change reflect in
2189 modem status reg */
2190
2191 /* Data |= 0x20; //rx_disable bit */
2192 status = mos7840_set_reg_sync(port,
2193 mos7840_port->ControlRegOffset, Data);
2194 if (status < 0) {
2195 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2196 goto out;
2197 } else
2198 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2199
2200 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2201 and 0x24 in DCR3 */
2202 Data = 0x01;
2203 status = mos7840_set_reg_sync(port,
2204 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2205 if (status < 0) {
2206 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2207 goto out;
2208 } else
2209 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
2210
2211 Data = 0x05;
2212 status = mos7840_set_reg_sync(port,
2213 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2214 if (status < 0) {
2215 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2216 goto out;
2217 } else
2218 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
2219
2220 Data = 0x24;
2221 status = mos7840_set_reg_sync(port,
2222 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2223 if (status < 0) {
2224 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2225 goto out;
2226 } else
2227 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
2228
2229 /* write values in clkstart0x0 and clkmulti 0x20 */
2230 Data = 0x0;
2231 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2232 if (status < 0) {
2233 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2234 goto out;
2235 } else
2236 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2237
2238 Data = 0x20;
2239 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2240 if (status < 0) {
2241 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2242 goto error;
2243 } else
2244 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2245
2246 /* write value 0x0 to scratchpad register */
2247 Data = 0x00;
2248 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2249 if (status < 0) {
2250 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2251 goto out;
2252 } else
2253 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2254
2255 /* Zero Length flag register */
2256 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2257 Data = 0xff;
2258 status = mos7840_set_reg_sync(port,
2259 (__u16) (ZLP_REG1 +
2260 ((__u16)mos7840_port->port_num)), Data);
2261 dev_dbg(&port->dev, "ZLIP offset %x\n",
2262 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
2263 if (status < 0) {
2264 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2265 goto out;
2266 } else
2267 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2268 } else {
2269 Data = 0xff;
2270 status = mos7840_set_reg_sync(port,
2271 (__u16) (ZLP_REG1 +
2272 ((__u16)mos7840_port->port_num) - 0x1), Data);
2273 dev_dbg(&port->dev, "ZLIP offset %x\n",
2274 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
2275 if (status < 0) {
2276 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2277 goto out;
2278 } else
2279 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
2280
2281 }
2282 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2283 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2284 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2285 GFP_KERNEL);
2286 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2287 !mos7840_port->dr) {
2288 status = -ENOMEM;
2289 goto error;
2290 }
2291
2292 mos7840_port->has_led = false;
2293
2294 /* Initialize LED timers */
2295 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2296 mos7840_port->has_led = true;
2297
2298 mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
2299 mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
2300 GFP_KERNEL);
2301 if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
2302 status = -ENOMEM;
2303 goto error;
2304 }
2305
2306 timer_setup(&mos7840_port->led_timer1, mos7840_led_off, 0);
2307 mos7840_port->led_timer1.expires =
2308 jiffies + msecs_to_jiffies(LED_ON_MS);
2309 timer_setup(&mos7840_port->led_timer2, mos7840_led_flag_off,
2310 0);
2311 mos7840_port->led_timer2.expires =
2312 jiffies + msecs_to_jiffies(LED_OFF_MS);
2313
2314 /* Turn off LED */
2315 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2316 }
2317 out:
2318 if (pnum == serial->num_ports - 1) {
2319 /* Zero Length flag enable */
2320 Data = 0x0f;
2321 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2322 if (status < 0) {
2323 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2324 goto error;
2325 } else
2326 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2327 }
2328 return 0;
2329 error:
2330 kfree(mos7840_port->led_dr);
2331 usb_free_urb(mos7840_port->led_urb);
2332 kfree(mos7840_port->dr);
2333 kfree(mos7840_port->ctrl_buf);
2334 usb_free_urb(mos7840_port->control_urb);
2335 kfree(mos7840_port);
2336
2337 return status;
2338 }
2339
2340 static int mos7840_port_remove(struct usb_serial_port *port)
2341 {
2342 struct moschip_port *mos7840_port;
2343
2344 mos7840_port = mos7840_get_port_private(port);
2345
2346 if (mos7840_port->has_led) {
2347 /* Turn off LED */
2348 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2349
2350 del_timer_sync(&mos7840_port->led_timer1);
2351 del_timer_sync(&mos7840_port->led_timer2);
2352
2353 usb_kill_urb(mos7840_port->led_urb);
2354 usb_free_urb(mos7840_port->led_urb);
2355 kfree(mos7840_port->led_dr);
2356 }
2357 usb_kill_urb(mos7840_port->control_urb);
2358 usb_free_urb(mos7840_port->control_urb);
2359 kfree(mos7840_port->ctrl_buf);
2360 kfree(mos7840_port->dr);
2361 kfree(mos7840_port);
2362
2363 return 0;
2364 }
2365
2366 static struct usb_serial_driver moschip7840_4port_device = {
2367 .driver = {
2368 .owner = THIS_MODULE,
2369 .name = "mos7840",
2370 },
2371 .description = DRIVER_DESC,
2372 .id_table = id_table,
2373 .num_interrupt_in = 1,
2374 .open = mos7840_open,
2375 .close = mos7840_close,
2376 .write = mos7840_write,
2377 .write_room = mos7840_write_room,
2378 .chars_in_buffer = mos7840_chars_in_buffer,
2379 .throttle = mos7840_throttle,
2380 .unthrottle = mos7840_unthrottle,
2381 .calc_num_ports = mos7840_calc_num_ports,
2382 .probe = mos7840_probe,
2383 .ioctl = mos7840_ioctl,
2384 .set_termios = mos7840_set_termios,
2385 .break_ctl = mos7840_break,
2386 .tiocmget = mos7840_tiocmget,
2387 .tiocmset = mos7840_tiocmset,
2388 .tiocmiwait = usb_serial_generic_tiocmiwait,
2389 .get_icount = usb_serial_generic_get_icount,
2390 .port_probe = mos7840_port_probe,
2391 .port_remove = mos7840_port_remove,
2392 .read_bulk_callback = mos7840_bulk_in_callback,
2393 .read_int_callback = mos7840_interrupt_callback,
2394 };
2395
2396 static struct usb_serial_driver * const serial_drivers[] = {
2397 &moschip7840_4port_device, NULL
2398 };
2399
2400 module_usb_serial_driver(serial_drivers, id_table);
2401
2402 MODULE_DESCRIPTION(DRIVER_DESC);
2403 MODULE_LICENSE("GPL");