blob: 4342a8a0eac9d150feda3df3f7ae588ed3e721b1 [file] [log] [blame]
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001/*
2 * mos7720.c
3 * Controls the Moschip 7720 usb to dual port serial convertor
4 *
5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
10 *
11 * Developed by:
Greg Kroah-Hartman50d2dc72007-06-25 01:08:01 -070012 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070015 *
16 * Cleaned up from the original by:
17 * Greg Kroah-Hartman <gregkh@suse.de>
18 *
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22 */
23#include <linux/kernel.h>
24#include <linux/errno.h>
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/tty.h>
28#include <linux/tty_driver.h>
29#include <linux/tty_flip.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <linux/serial.h>
33#include <linux/serial_reg.h>
34#include <linux/usb.h>
35#include <linux/usb/serial.h>
Alan Cox4da1a172008-07-22 11:16:21 +010036#include <linux/uaccess.h>
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070037
38
39/*
40 * Version Information
41 */
42#define DRIVER_VERSION "1.0.0.4F"
43#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
44#define DRIVER_DESC "Moschip USB Serial Driver"
45
46/* default urb timeout */
47#define MOS_WDR_TIMEOUT (HZ * 5)
48
49#define MOS_PORT1 0x0200
50#define MOS_PORT2 0x0300
51#define MOS_VENREG 0x0000
52#define MOS_MAX_PORT 0x02
53#define MOS_WRITE 0x0E
54#define MOS_READ 0x0D
55
56/* Interrupt Rotinue Defines */
57#define SERIAL_IIR_RLS 0x06
58#define SERIAL_IIR_RDA 0x04
59#define SERIAL_IIR_CTI 0x0c
60#define SERIAL_IIR_THR 0x02
61#define SERIAL_IIR_MS 0x00
62
63#define NUM_URBS 16 /* URB Count */
64#define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
65
66/* This structure holds all of the local port information */
Alan Cox4da1a172008-07-22 11:16:21 +010067struct moschip_port {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070068 __u8 shadowLCR; /* last LCR value received */
69 __u8 shadowMCR; /* last MCR value received */
70 __u8 shadowMSR; /* last MSR value received */
71 char open;
72 struct async_icount icount;
73 struct usb_serial_port *port; /* loop back to the owner */
74 struct urb *write_urb_pool[NUM_URBS];
75};
76
77/* This structure holds all of the individual serial device information */
Alan Cox4da1a172008-07-22 11:16:21 +010078struct moschip_serial {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070079 int interrupt_started;
80};
81
82static int debug;
83
84#define USB_VENDOR_ID_MOSCHIP 0x9710
85#define MOSCHIP_DEVICE_ID_7720 0x7720
86#define MOSCHIP_DEVICE_ID_7715 0x7715
87
Kees Schoenmakers0f608f82009-09-19 13:13:18 -070088static struct usb_device_id moschip_port_id_table[] = {
Alan Cox4da1a172008-07-22 11:16:21 +010089 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -070090 { } /* terminating entry */
91};
92MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
93
94
95/*
96 * mos7720_interrupt_callback
97 * this is the callback function for when we have received data on the
98 * interrupt endpoint.
99 */
100static void mos7720_interrupt_callback(struct urb *urb)
101{
102 int result;
103 int length;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700104 int status = urb->status;
Oliver Neukum325b70c2007-03-19 13:58:29 +0100105 __u8 *data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700106 __u8 sp1;
107 __u8 sp2;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700108
Alan Cox4da1a172008-07-22 11:16:21 +0100109 dbg("%s", " : Entering\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700110
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700111 switch (status) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700112 case 0:
113 /* success */
114 break;
115 case -ECONNRESET:
116 case -ENOENT:
117 case -ESHUTDOWN:
118 /* this urb is terminated, clean up */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800119 dbg("%s - urb shutting down with status: %d", __func__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700120 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700121 return;
122 default:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800123 dbg("%s - nonzero urb status received: %d", __func__,
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700124 status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700125 goto exit;
126 }
127
128 length = urb->actual_length;
129 data = urb->transfer_buffer;
130
131 /* Moschip get 4 bytes
132 * Byte 1 IIR Port 1 (port.number is 0)
133 * Byte 2 IIR Port 2 (port.number is 1)
134 * Byte 3 --------------
135 * Byte 4 FIFO status for both */
Oliver Neukum325b70c2007-03-19 13:58:29 +0100136
137 /* the above description is inverted
138 * oneukum 2007-03-14 */
139
140 if (unlikely(length != 4)) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700141 dbg("Wrong data !!!");
142 return;
143 }
144
Oliver Neukum325b70c2007-03-19 13:58:29 +0100145 sp1 = data[3];
146 sp2 = data[2];
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700147
Oliver Neukum325b70c2007-03-19 13:58:29 +0100148 if ((sp1 | sp2) & 0x01) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700149 /* No Interrupt Pending in both the ports */
150 dbg("No Interrupt !!!");
151 } else {
152 switch (sp1 & 0x0f) {
153 case SERIAL_IIR_RLS:
154 dbg("Serial Port 1: Receiver status error or address "
155 "bit detected in 9-bit mode\n");
156 break;
157 case SERIAL_IIR_CTI:
158 dbg("Serial Port 1: Receiver time out");
159 break;
160 case SERIAL_IIR_MS:
161 dbg("Serial Port 1: Modem status change");
162 break;
163 }
164
165 switch (sp2 & 0x0f) {
166 case SERIAL_IIR_RLS:
167 dbg("Serial Port 2: Receiver status error or address "
168 "bit detected in 9-bit mode");
169 break;
170 case SERIAL_IIR_CTI:
171 dbg("Serial Port 2: Receiver time out");
172 break;
173 case SERIAL_IIR_MS:
174 dbg("Serial Port 2: Modem status change");
175 break;
176 }
177 }
178
179exit:
180 result = usb_submit_urb(urb, GFP_ATOMIC);
181 if (result)
182 dev_err(&urb->dev->dev,
183 "%s - Error %d submitting control urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800184 __func__, result);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700185 return;
186}
187
188/*
189 * mos7720_bulk_in_callback
190 * this is the callback function for when we have received data on the
191 * bulk in endpoint.
192 */
193static void mos7720_bulk_in_callback(struct urb *urb)
194{
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700195 int retval;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700196 unsigned char *data ;
197 struct usb_serial_port *port;
198 struct moschip_port *mos7720_port;
199 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700200 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700201
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700202 if (status) {
203 dbg("nonzero read bulk status received: %d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700204 return;
205 }
206
207 mos7720_port = urb->context;
208 if (!mos7720_port) {
Alan Cox4da1a172008-07-22 11:16:21 +0100209 dbg("%s", "NULL mos7720_port pointer \n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700210 return ;
211 }
212
213 port = mos7720_port->port;
214
Harvey Harrison441b62c2008-03-03 16:08:34 -0800215 dbg("Entering...%s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700216
217 data = urb->transfer_buffer;
218
Alan Cox4a90f092008-10-13 10:39:46 +0100219 tty = tty_port_tty_get(&port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700220 if (tty && urb->actual_length) {
221 tty_buffer_request_room(tty, urb->actual_length);
222 tty_insert_flip_string(tty, data, urb->actual_length);
223 tty_flip_buffer_push(tty);
224 }
Alan Cox4a90f092008-10-13 10:39:46 +0100225 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700226
227 if (!port->read_urb) {
228 dbg("URB KILLED !!!");
229 return;
230 }
231
232 if (port->read_urb->status != -EINPROGRESS) {
233 port->read_urb->dev = port->serial->dev;
234
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700235 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
236 if (retval)
237 dbg("usb_submit_urb(read bulk) failed, retval = %d",
238 retval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700239 }
240}
241
242/*
243 * mos7720_bulk_out_data_callback
244 * this is the callback function for when we have finished sending serial
245 * data on the bulk out endpoint.
246 */
247static void mos7720_bulk_out_data_callback(struct urb *urb)
248{
249 struct moschip_port *mos7720_port;
250 struct tty_struct *tty;
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700251 int status = urb->status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700252
Greg Kroah-Hartman81105982007-06-15 15:44:13 -0700253 if (status) {
254 dbg("nonzero write bulk status received:%d", status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700255 return;
256 }
257
258 mos7720_port = urb->context;
259 if (!mos7720_port) {
260 dbg("NULL mos7720_port pointer");
261 return ;
262 }
263
264 dbg("Entering .........");
265
Alan Cox4a90f092008-10-13 10:39:46 +0100266 tty = tty_port_tty_get(&mos7720_port->port->port);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700267
Jiri Slabyb963a842007-02-10 01:44:55 -0800268 if (tty && mos7720_port->open)
269 tty_wakeup(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100270 tty_kref_put(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700271}
272
273/*
274 * send_mos_cmd
275 * this function will be used for sending command to device
276 */
277static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
278 __u16 index, void *data)
279{
280 int status;
281 unsigned int pipe;
282 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
283 __u8 requesttype;
284 __u16 size = 0x0000;
285
286 if (value < MOS_MAX_PORT) {
Alan Cox4da1a172008-07-22 11:16:21 +0100287 if (product == MOSCHIP_DEVICE_ID_7715)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700288 value = value*0x100+0x100;
Alan Cox4da1a172008-07-22 11:16:21 +0100289 else
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700290 value = value*0x100+0x200;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700291 } else {
292 value = 0x0000;
293 if ((product == MOSCHIP_DEVICE_ID_7715) &&
294 (index != 0x08)) {
295 dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
Alan Cox4da1a172008-07-22 11:16:21 +0100296 /* index = 0x01 ; */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700297 }
298 }
299
300 if (request == MOS_WRITE) {
301 request = (__u8)MOS_WRITE;
302 requesttype = (__u8)0x40;
303 value = value + (__u16)*((unsigned char *)data);
304 data = NULL;
305 pipe = usb_sndctrlpipe(serial->dev, 0);
306 } else {
307 request = (__u8)MOS_READ;
308 requesttype = (__u8)0xC0;
309 size = 0x01;
Alan Cox4da1a172008-07-22 11:16:21 +0100310 pipe = usb_rcvctrlpipe(serial->dev, 0);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700311 }
312
313 status = usb_control_msg(serial->dev, pipe, request, requesttype,
314 value, index, data, size, MOS_WDR_TIMEOUT);
315
316 if (status < 0)
Alan Cox4da1a172008-07-22 11:16:21 +0100317 dbg("Command Write failed Value %x index %x\n", value, index);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700318
319 return status;
320}
321
Alan Cox95da3102008-07-22 11:09:07 +0100322static int mos7720_open(struct tty_struct *tty,
Alan Cox4da1a172008-07-22 11:16:21 +0100323 struct usb_serial_port *port, struct file *filp)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700324{
325 struct usb_serial *serial;
326 struct usb_serial_port *port0;
327 struct urb *urb;
328 struct moschip_serial *mos7720_serial;
329 struct moschip_port *mos7720_port;
330 int response;
331 int port_number;
332 char data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100333 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700334 int j;
335
336 serial = port->serial;
337
338 mos7720_port = usb_get_serial_port_data(port);
339 if (mos7720_port == NULL)
340 return -ENODEV;
341
342 port0 = serial->port[0];
343
344 mos7720_serial = usb_get_serial_data(serial);
345
346 if (mos7720_serial == NULL || port0 == NULL)
347 return -ENODEV;
348
349 usb_clear_halt(serial->dev, port->write_urb->pipe);
350 usb_clear_halt(serial->dev, port->read_urb->pipe);
351
352 /* Initialising the write urb pool */
353 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +0100354 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700355 mos7720_port->write_urb_pool[j] = urb;
356
357 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700358 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700359 continue;
360 }
361
362 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
363 GFP_KERNEL);
364 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700365 dev_err(&port->dev,
366 "%s-out of memory for urb buffers.\n",
367 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100368 usb_free_urb(mos7720_port->write_urb_pool[j]);
369 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700370 continue;
371 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100372 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700373 }
374
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100375 if (!allocated_urbs)
376 return -ENOMEM;
377
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700378 /* Initialize MCS7720 -- Write Init values to corresponding Registers
379 *
380 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -0700381 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700382 * 1 : IER
383 * 2 : FCR
384 * 3 : LCR
385 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -0700386 * 5 : LSR
387 * 6 : MSR
388 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700389 *
390 * 0x08 : SP1/2 Control Reg
391 */
392 port_number = port->number - port->serial->minor;
393 send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
Alan Cox4da1a172008-07-22 11:16:21 +0100394 dbg("SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700395
396 dbg("Check:Sending Command ..........");
397
398 data = 0x02;
399 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
400 data = 0x02;
401 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
402
403 data = 0x00;
404 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
405 data = 0x00;
406 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
407
408 data = 0xCF;
409 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
410 data = 0x03;
Alan Cox4da1a172008-07-22 11:16:21 +0100411 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700412 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
413 data = 0x0b;
Alan Cox4da1a172008-07-22 11:16:21 +0100414 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700415 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
416 data = 0x0b;
417 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
418
419 data = 0x00;
420 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
421 data = 0x00;
422 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
423
424/* data = 0x00;
425 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
426 data = 0x03;
427 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
428 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100429 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
430 port_number + 1, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700431*/
432 data = 0x00;
433 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
434
435 data = data | (port->number - port->serial->minor + 1);
436 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
437
438 data = 0x83;
Alan Cox4da1a172008-07-22 11:16:21 +0100439 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700440 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
441 data = 0x0c;
442 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
443 data = 0x00;
444 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
445 data = 0x03;
Alan Cox4da1a172008-07-22 11:16:21 +0100446 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700447 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
448 data = 0x0c;
449 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
450 data = 0x0c;
451 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
452
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700453 /* see if we've set up our endpoint info yet *
454 * (can't set it up in mos7720_startup as the *
455 * structures were not set up at that time.) */
456 if (!mos7720_serial->interrupt_started) {
457 dbg("Interrupt buffer NULL !!!");
458
459 /* not set up yet, so do it now */
460 mos7720_serial->interrupt_started = 1;
461
462 dbg("To Submit URB !!!");
463
464 /* set up our interrupt urb */
465 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100466 usb_rcvintpipe(serial->dev,
467 port->interrupt_in_endpointAddress),
468 port0->interrupt_in_buffer,
469 port0->interrupt_in_urb->transfer_buffer_length,
470 mos7720_interrupt_callback, mos7720_port,
471 port0->interrupt_in_urb->interval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700472
473 /* start interrupt read for this mos7720 this interrupt *
Alan Cox4da1a172008-07-22 11:16:21 +0100474 * will continue as long as the mos7720 is connected */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700475 dbg("Submit URB over !!!");
476 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
477 if (response)
478 dev_err(&port->dev,
Joe Perches898eb712007-10-18 03:06:30 -0700479 "%s - Error %d submitting control urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800480 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700481 }
482
483 /* set up our bulk in urb */
484 usb_fill_bulk_urb(port->read_urb, serial->dev,
485 usb_rcvbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100486 port->bulk_in_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700487 port->bulk_in_buffer,
488 port->read_urb->transfer_buffer_length,
489 mos7720_bulk_in_callback, mos7720_port);
490 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
491 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +0100492 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
493 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700494
495 /* initialize our icount structure */
496 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
497
498 /* initialize our port settings */
499 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
500
501 /* send a open port command */
502 mos7720_port->open = 1;
503
504 return 0;
505}
506
507/*
508 * mos7720_chars_in_buffer
509 * this function is called by the tty driver when it wants to know how many
510 * bytes of data we currently have outstanding in the port (data that has
511 * been written, but hasn't made it out the port yet)
512 * If successful, we return the number of bytes left to be written in the
513 * system,
514 * Otherwise we return a negative error number.
515 */
Alan Cox95da3102008-07-22 11:09:07 +0100516static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700517{
Alan Cox95da3102008-07-22 11:09:07 +0100518 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700519 int i;
520 int chars = 0;
521 struct moschip_port *mos7720_port;
522
Harvey Harrison441b62c2008-03-03 16:08:34 -0800523 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700524
525 mos7720_port = usb_get_serial_port_data(port);
526 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800527 dbg("%s:leaving ...........", __func__);
Alan Cox23198fd2009-07-20 16:05:27 +0100528 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700529 }
530
531 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100532 if (mos7720_port->write_urb_pool[i] &&
533 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700534 chars += URB_TRANSFER_BUFFER_SIZE;
535 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800536 dbg("%s - returns %d", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700537 return chars;
538}
539
Alan Cox335f8512009-06-11 12:26:29 +0100540static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700541{
542 struct usb_serial *serial;
543 struct moschip_port *mos7720_port;
544 char data;
545 int j;
546
547 dbg("mos7720_close:entering...");
548
549 serial = port->serial;
550
551 mos7720_port = usb_get_serial_port_data(port);
552 if (mos7720_port == NULL)
553 return;
554
555 for (j = 0; j < NUM_URBS; ++j)
556 usb_kill_urb(mos7720_port->write_urb_pool[j]);
557
558 /* Freeing Write URBs */
559 for (j = 0; j < NUM_URBS; ++j) {
560 if (mos7720_port->write_urb_pool[j]) {
561 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
562 usb_free_urb(mos7720_port->write_urb_pool[j]);
563 }
564 }
565
566 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100567 * and interrupt read if they exists, otherwise nop */
568 dbg("Shutdown bulk write");
569 usb_kill_urb(port->write_urb);
570 dbg("Shutdown bulk read");
571 usb_kill_urb(port->read_urb);
572
573 mutex_lock(&serial->disc_mutex);
574 /* these commands must not be issued if the device has
575 * been disconnected */
576 if (!serial->disconnected) {
577 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100578 send_mos_cmd(serial, MOS_WRITE,
579 port->number - port->serial->minor, 0x04, &data);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100580
581 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100582 send_mos_cmd(serial, MOS_WRITE,
583 port->number - port->serial->minor, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700584 }
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100585 mutex_unlock(&serial->disc_mutex);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700586 mos7720_port->open = 0;
587
Harvey Harrison441b62c2008-03-03 16:08:34 -0800588 dbg("Leaving %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700589}
590
Alan Cox95da3102008-07-22 11:09:07 +0100591static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700592{
Alan Cox95da3102008-07-22 11:09:07 +0100593 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +0100594 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700595 struct usb_serial *serial;
596 struct moschip_port *mos7720_port;
597
Harvey Harrison441b62c2008-03-03 16:08:34 -0800598 dbg("Entering %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700599
600 serial = port->serial;
601
602 mos7720_port = usb_get_serial_port_data(port);
603 if (mos7720_port == NULL)
604 return;
605
606 if (break_state == -1)
607 data = mos7720_port->shadowLCR | UART_LCR_SBC;
608 else
609 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
610
611 mos7720_port->shadowLCR = data;
612 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
613 0x03, &data);
614
615 return;
616}
617
618/*
619 * mos7720_write_room
620 * this function is called by the tty driver when it wants to know how many
621 * bytes of data we can accept for a specific port.
622 * If successful, we return the amount of room that we have for this port
623 * Otherwise we return a negative error number.
624 */
Alan Cox95da3102008-07-22 11:09:07 +0100625static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700626{
Alan Cox95da3102008-07-22 11:09:07 +0100627 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700628 struct moschip_port *mos7720_port;
629 int room = 0;
630 int i;
631
Harvey Harrison441b62c2008-03-03 16:08:34 -0800632 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700633
634 mos7720_port = usb_get_serial_port_data(port);
635 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800636 dbg("%s:leaving ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700637 return -ENODEV;
638 }
639
Alan Coxa5b6f602008-04-08 17:16:06 +0100640 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700641 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100642 if (mos7720_port->write_urb_pool[i] &&
643 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700644 room += URB_TRANSFER_BUFFER_SIZE;
645 }
646
Harvey Harrison441b62c2008-03-03 16:08:34 -0800647 dbg("%s - returns %d", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700648 return room;
649}
650
Alan Cox95da3102008-07-22 11:09:07 +0100651static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
652 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700653{
654 int status;
655 int i;
656 int bytes_sent = 0;
657 int transfer_size;
658
659 struct moschip_port *mos7720_port;
660 struct usb_serial *serial;
661 struct urb *urb;
662 const unsigned char *current_position = data;
663
Harvey Harrison441b62c2008-03-03 16:08:34 -0800664 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700665
666 serial = port->serial;
667
668 mos7720_port = usb_get_serial_port_data(port);
669 if (mos7720_port == NULL) {
670 dbg("mos7720_port is NULL");
671 return -ENODEV;
672 }
673
674 /* try to find a free urb in the list */
675 urb = NULL;
676
677 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100678 if (mos7720_port->write_urb_pool[i] &&
679 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700680 urb = mos7720_port->write_urb_pool[i];
Alan Cox4da1a172008-07-22 11:16:21 +0100681 dbg("URB:%d", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700682 break;
683 }
684 }
685
686 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800687 dbg("%s - no more free urbs", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700688 goto exit;
689 }
690
691 if (urb->transfer_buffer == NULL) {
692 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
693 GFP_KERNEL);
694 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700695 dev_err(&port->dev, "%s no more kernel memory...\n",
696 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700697 goto exit;
698 }
699 }
Alan Cox4da1a172008-07-22 11:16:21 +0100700 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700701
702 memcpy(urb->transfer_buffer, current_position, transfer_size);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800703 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700704 urb->transfer_buffer);
705
706 /* fill urb with data and submit */
707 usb_fill_bulk_urb(urb, serial->dev,
708 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100709 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700710 urb->transfer_buffer, transfer_size,
711 mos7720_bulk_out_data_callback, mos7720_port);
712
713 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +0100714 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700715 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700716 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
717 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700718 bytes_sent = status;
719 goto exit;
720 }
721 bytes_sent = transfer_size;
722
723exit:
724 return bytes_sent;
725}
726
Alan Cox95da3102008-07-22 11:09:07 +0100727static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700728{
Alan Cox95da3102008-07-22 11:09:07 +0100729 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700730 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700731 int status;
732
Harvey Harrison441b62c2008-03-03 16:08:34 -0800733 dbg("%s- port %d\n", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700734
735 mos7720_port = usb_get_serial_port_data(port);
736
737 if (mos7720_port == NULL)
738 return;
739
740 if (!mos7720_port->open) {
741 dbg("port not opened");
742 return;
743 }
744
Harvey Harrison441b62c2008-03-03 16:08:34 -0800745 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700746
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700747 /* if we are implementing XON/XOFF, send the stop character */
748 if (I_IXOFF(tty)) {
749 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +0100750 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700751 if (status <= 0)
752 return;
753 }
754
755 /* if we are implementing RTS/CTS, toggle that line */
756 if (tty->termios->c_cflag & CRTSCTS) {
757 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
758 status = send_mos_cmd(port->serial, MOS_WRITE,
759 port->number - port->serial->minor,
760 UART_MCR, &mos7720_port->shadowMCR);
761 if (status != 0)
762 return;
763 }
764}
765
Alan Cox95da3102008-07-22 11:09:07 +0100766static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700767{
Alan Cox95da3102008-07-22 11:09:07 +0100768 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700769 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +0100770 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700771
772 if (mos7720_port == NULL)
773 return;
774
775 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800776 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700777 return;
778 }
779
Harvey Harrison441b62c2008-03-03 16:08:34 -0800780 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700781
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700782 /* if we are implementing XON/XOFF, send the start character */
783 if (I_IXOFF(tty)) {
784 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +0100785 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700786 if (status <= 0)
787 return;
788 }
789
790 /* if we are implementing RTS/CTS, toggle that line */
791 if (tty->termios->c_cflag & CRTSCTS) {
792 mos7720_port->shadowMCR |= UART_MCR_RTS;
793 status = send_mos_cmd(port->serial, MOS_WRITE,
794 port->number - port->serial->minor,
795 UART_MCR, &mos7720_port->shadowMCR);
796 if (status != 0)
797 return;
798 }
799}
800
801static int set_higher_rates(struct moschip_port *mos7720_port,
802 unsigned int baud)
803{
804 unsigned char data;
805 struct usb_serial_port *port;
806 struct usb_serial *serial;
807 int port_number;
808
809 if (mos7720_port == NULL)
810 return -EINVAL;
811
812 port = mos7720_port->port;
813 serial = port->serial;
814
Alan Cox4da1a172008-07-22 11:16:21 +0100815 /***********************************************
816 * Init Sequence for higher rates
817 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700818 dbg("Sending Setting Commands ..........");
819 port_number = port->number - port->serial->minor;
820
821 data = 0x000;
822 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
823 data = 0x000;
824 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
825 data = 0x0CF;
826 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
827 data = 0x00b;
Alan Cox4da1a172008-07-22 11:16:21 +0100828 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700829 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
830 data = 0x00b;
831 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
832
833 data = 0x000;
834 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
835 data = 0x000;
836 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
837
838
Alan Cox4da1a172008-07-22 11:16:21 +0100839 /***********************************************
840 * Set for higher rates *
841 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700842
843 data = baud * 0x10;
Alan Cox4da1a172008-07-22 11:16:21 +0100844 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700845
846 data = 0x003;
847 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
848 data = 0x003;
849 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
850
851 data = 0x02b;
Alan Cox4da1a172008-07-22 11:16:21 +0100852 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700853 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
854 data = 0x02b;
855 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
856
Alan Cox4da1a172008-07-22 11:16:21 +0100857 /***********************************************
858 * Set DLL/DLM
859 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700860
861 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
Alan Cox4da1a172008-07-22 11:16:21 +0100862 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700863 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
864
865 data = 0x001; /* DLL */
Alan Cox4da1a172008-07-22 11:16:21 +0100866 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700867 data = 0x000; /* DLM */
Alan Cox4da1a172008-07-22 11:16:21 +0100868 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700869
870 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
Alan Cox4da1a172008-07-22 11:16:21 +0100871 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700872 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
873
874 return 0;
875}
876
877/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +0100878struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700879 __u32 baudrate;
880 __u16 divisor;
881};
882
883/* Define table of divisors for moschip 7720 hardware *
884 * These assume a 3.6864MHz crystal, the standard /16, and *
885 * MCR.7 = 0. */
886static struct divisor_table_entry divisor_table[] = {
887 { 50, 2304},
888 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
889 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
890 { 150, 768},
891 { 300, 384},
892 { 600, 192},
893 { 1200, 96},
894 { 1800, 64},
895 { 2400, 48},
896 { 4800, 24},
897 { 7200, 16},
898 { 9600, 12},
899 { 19200, 6},
900 { 38400, 3},
901 { 57600, 2},
902 { 115200, 1},
903};
904
905/*****************************************************************************
906 * calc_baud_rate_divisor
907 * this function calculates the proper baud rate divisor for the specified
908 * baud rate.
909 *****************************************************************************/
910static int calc_baud_rate_divisor(int baudrate, int *divisor)
911{
912 int i;
913 __u16 custom;
914 __u16 round1;
915 __u16 round;
916
917
Harvey Harrison441b62c2008-03-03 16:08:34 -0800918 dbg("%s - %d", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700919
920 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
921 if (divisor_table[i].baudrate == baudrate) {
922 *divisor = divisor_table[i].divisor;
923 return 0;
924 }
925 }
926
Alan Cox4da1a172008-07-22 11:16:21 +0100927 /* After trying for all the standard baud rates *
928 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700929 if (baudrate > 75 && baudrate < 230400) {
930 /* get the divisor */
931 custom = (__u16)(230400L / baudrate);
932
933 /* Check for round off */
934 round1 = (__u16)(2304000L / baudrate);
935 round = (__u16)(round1 - (custom * 10));
936 if (round > 4)
937 custom++;
938 *divisor = custom;
939
Alan Cox4da1a172008-07-22 11:16:21 +0100940 dbg("Baud %d = %d", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700941 return 0;
942 }
943
944 dbg("Baud calculation Failed...");
945 return -EINVAL;
946}
947
948/*
949 * send_cmd_write_baud_rate
950 * this function sends the proper command to change the baud rate of the
951 * specified port.
952 */
953static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
954 int baudrate)
955{
956 struct usb_serial_port *port;
957 struct usb_serial *serial;
958 int divisor;
959 int status;
960 unsigned char data;
961 unsigned char number;
962
963 if (mos7720_port == NULL)
964 return -1;
965
966 port = mos7720_port->port;
967 serial = port->serial;
968
Harvey Harrison441b62c2008-03-03 16:08:34 -0800969 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700970
971 number = port->number - port->serial->minor;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800972 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700973
Alan Cox4da1a172008-07-22 11:16:21 +0100974 /* Calculate the Divisor */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700975 status = calc_baud_rate_divisor(baudrate, &divisor);
976 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700977 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700978 return status;
979 }
980
Alan Cox4da1a172008-07-22 11:16:21 +0100981 /* Enable access to divisor latch */
982 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
983 mos7720_port->shadowLCR = data;
984 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700985
986 /* Write the divisor */
987 data = ((unsigned char)(divisor & 0xff));
Alan Cox4da1a172008-07-22 11:16:21 +0100988 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700989
990 data = ((unsigned char)((divisor & 0xff00) >> 8));
Alan Cox4da1a172008-07-22 11:16:21 +0100991 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700992
Alan Cox4da1a172008-07-22 11:16:21 +0100993 /* Disable access to divisor latch */
994 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
995 mos7720_port->shadowLCR = data;
996 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700997
998 return status;
999}
1000
1001/*
1002 * change_port_settings
1003 * This routine is called to set the UART on the device to match
1004 * the specified new settings.
1005 */
Alan Cox95da3102008-07-22 11:09:07 +01001006static void change_port_settings(struct tty_struct *tty,
1007 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001008 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001009{
1010 struct usb_serial_port *port;
1011 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001012 int baud;
1013 unsigned cflag;
1014 unsigned iflag;
1015 __u8 mask = 0xff;
1016 __u8 lData;
1017 __u8 lParity;
1018 __u8 lStop;
1019 int status;
1020 int port_number;
1021 char data;
1022
1023 if (mos7720_port == NULL)
1024 return ;
1025
1026 port = mos7720_port->port;
1027 serial = port->serial;
1028 port_number = port->number - port->serial->minor;
1029
Harvey Harrison441b62c2008-03-03 16:08:34 -08001030 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001031
1032 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001033 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001034 return;
1035 }
1036
Harvey Harrison441b62c2008-03-03 16:08:34 -08001037 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001038
1039 lData = UART_LCR_WLEN8;
1040 lStop = 0x00; /* 1 stop bit */
1041 lParity = 0x00; /* No parity */
1042
1043 cflag = tty->termios->c_cflag;
1044 iflag = tty->termios->c_iflag;
1045
1046 /* Change the number of bits */
1047 switch (cflag & CSIZE) {
1048 case CS5:
1049 lData = UART_LCR_WLEN5;
1050 mask = 0x1f;
1051 break;
1052
1053 case CS6:
1054 lData = UART_LCR_WLEN6;
1055 mask = 0x3f;
1056 break;
1057
1058 case CS7:
1059 lData = UART_LCR_WLEN7;
1060 mask = 0x7f;
1061 break;
1062 default:
1063 case CS8:
1064 lData = UART_LCR_WLEN8;
1065 break;
1066 }
1067
1068 /* Change the Parity bit */
1069 if (cflag & PARENB) {
1070 if (cflag & PARODD) {
1071 lParity = UART_LCR_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001072 dbg("%s - parity = odd", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001073 } else {
1074 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001075 dbg("%s - parity = even", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001076 }
1077
1078 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001079 dbg("%s - parity = none", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001080 }
1081
1082 if (cflag & CMSPAR)
1083 lParity = lParity | 0x20;
1084
1085 /* Change the Stop bit */
1086 if (cflag & CSTOPB) {
1087 lStop = UART_LCR_STOP;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001088 dbg("%s - stop bits = 2", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001089 } else {
1090 lStop = 0x00;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001091 dbg("%s - stop bits = 1", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001092 }
1093
1094#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1095#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1096#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1097
1098 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001099 mos7720_port->shadowLCR &=
1100 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001101 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1102
1103
1104 /* Disable Interrupts */
1105 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +01001106 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
1107 UART_IER, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001108
1109 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +01001110 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001111
1112 data = 0xcf;
Alan Cox4da1a172008-07-22 11:16:21 +01001113 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001114
1115 /* Send the updated LCR value to the mos7720 */
1116 data = mos7720_port->shadowLCR;
Alan Cox4da1a172008-07-22 11:16:21 +01001117 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001118
Alan Cox4da1a172008-07-22 11:16:21 +01001119 data = 0x00b;
1120 mos7720_port->shadowMCR = data;
1121 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1122 data = 0x00b;
1123 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001124
1125 /* set up the MCR register and send it to the mos7720 */
1126 mos7720_port->shadowMCR = UART_MCR_OUT2;
1127 if (cflag & CBAUD)
1128 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1129
1130 if (cflag & CRTSCTS) {
1131 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001132 /* To set hardware flow control to the specified *
1133 * serial port, in SP1/2_CONTROL_REG */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001134 if (port->number) {
1135 data = 0x001;
1136 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1137 0x08, &data);
1138 } else {
1139 data = 0x002;
1140 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1141 0x08, &data);
1142 }
1143 } else {
1144 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1145 }
1146
1147 data = mos7720_port->shadowMCR;
1148 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1149
1150 /* Determine divisor based on baud rate */
1151 baud = tty_get_baud_rate(tty);
1152 if (!baud) {
1153 /* pick a default, any default... */
1154 dbg("Picked default baud...");
1155 baud = 9600;
1156 }
1157
1158 if (baud >= 230400) {
1159 set_higher_rates(mos7720_port, baud);
1160 /* Enable Interrupts */
1161 data = 0x0c;
1162 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1163 return;
1164 }
1165
Harvey Harrison441b62c2008-03-03 16:08:34 -08001166 dbg("%s - baud rate = %d", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001167 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063a2008-01-03 17:01:18 +00001168 /* FIXME: needs to write actual resulting baud back not just
1169 blindly do so */
1170 if (cflag & CBAUD)
1171 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001172 /* Enable Interrupts */
1173 data = 0x0c;
1174 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1175
1176 if (port->read_urb->status != -EINPROGRESS) {
1177 port->read_urb->dev = serial->dev;
1178
1179 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1180 if (status)
1181 dbg("usb_submit_urb(read bulk) failed, status = %d",
1182 status);
1183 }
1184 return;
1185}
1186
1187/*
1188 * mos7720_set_termios
1189 * this function is called by the tty driver when it wants to change the
1190 * termios structure.
1191 */
Alan Cox95da3102008-07-22 11:09:07 +01001192static void mos7720_set_termios(struct tty_struct *tty,
1193 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001194{
1195 int status;
1196 unsigned int cflag;
1197 struct usb_serial *serial;
1198 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001199
1200 serial = port->serial;
1201
1202 mos7720_port = usb_get_serial_port_data(port);
1203
1204 if (mos7720_port == NULL)
1205 return;
1206
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001207 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001208 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001209 return;
1210 }
1211
Alan Cox4da1a172008-07-22 11:16:21 +01001212 dbg("%s\n", "setting termios - ASPIRE");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001213
1214 cflag = tty->termios->c_cflag;
1215
Harvey Harrison441b62c2008-03-03 16:08:34 -08001216 dbg("%s - cflag %08x iflag %08x", __func__,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001217 tty->termios->c_cflag,
1218 RELEVANT_IFLAG(tty->termios->c_iflag));
1219
Harvey Harrison441b62c2008-03-03 16:08:34 -08001220 dbg("%s - old cflag %08x old iflag %08x", __func__,
Alan Cox65d063a2008-01-03 17:01:18 +00001221 old_termios->c_cflag,
1222 RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001223
Harvey Harrison441b62c2008-03-03 16:08:34 -08001224 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001225
1226 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001227 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001228
Alan Cox4da1a172008-07-22 11:16:21 +01001229 if (!port->read_urb) {
1230 dbg("%s", "URB KILLED !!!!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001231 return;
1232 }
1233
Alan Cox4da1a172008-07-22 11:16:21 +01001234 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001235 port->read_urb->dev = serial->dev;
1236 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1237 if (status)
1238 dbg("usb_submit_urb(read bulk) failed, status = %d",
1239 status);
1240 }
1241 return;
1242}
1243
1244/*
1245 * get_lsr_info - get line status register info
1246 *
1247 * Purpose: Let user call ioctl() to get info when the UART physically
1248 * is emptied. On bus types like RS485, the transmitter must
1249 * release the bus after transmitting. This must be done when
1250 * the transmit shift register is empty, not be done when the
1251 * transmit holding register is empty. This functionality
1252 * allows an RS485 driver to be written in user space.
1253 */
Alan Cox4da1a172008-07-22 11:16:21 +01001254static int get_lsr_info(struct tty_struct *tty,
1255 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001256{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001257 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001258 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001259 unsigned char data = 0;
1260 int port_number = port->number - port->serial->minor;
1261 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001262
Alan Cox95da3102008-07-22 11:09:07 +01001263 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001264 if (count == 0) {
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001265 send_mos_cmd(port->serial, MOS_READ, port_number,
1266 UART_LSR, &data);
1267 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1268 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1269 dbg("%s -- Empty", __func__);
1270 result = TIOCSER_TEMT;
1271 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001272 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001273 if (copy_to_user(value, &result, sizeof(int)))
1274 return -EFAULT;
1275 return 0;
1276}
1277
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001278static int mos7720_tiocmget(struct tty_struct *tty, struct file *file)
1279{
1280 struct usb_serial_port *port = tty->driver_data;
1281 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1282 unsigned int result = 0;
1283 unsigned int mcr ;
1284 unsigned int msr ;
1285
1286 dbg("%s - port %d", __func__, port->number);
1287
1288 mcr = mos7720_port->shadowMCR;
1289 msr = mos7720_port->shadowMSR;
1290
1291 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1292 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1293 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1294 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1295 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1296 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1297
1298 dbg("%s -- %x", __func__, result);
1299
1300 return result;
1301}
1302
1303static int mos7720_tiocmset(struct tty_struct *tty, struct file *file,
1304 unsigned int set, unsigned int clear)
1305{
1306 struct usb_serial_port *port = tty->driver_data;
1307 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1308 unsigned int mcr ;
1309 unsigned char lmcr;
1310
1311 dbg("%s - port %d", __func__, port->number);
1312 dbg("he was at tiocmget");
1313
1314 mcr = mos7720_port->shadowMCR;
1315
1316 if (set & TIOCM_RTS)
1317 mcr |= UART_MCR_RTS;
1318 if (set & TIOCM_DTR)
1319 mcr |= UART_MCR_DTR;
1320 if (set & TIOCM_LOOP)
1321 mcr |= UART_MCR_LOOP;
1322
1323 if (clear & TIOCM_RTS)
1324 mcr &= ~UART_MCR_RTS;
1325 if (clear & TIOCM_DTR)
1326 mcr &= ~UART_MCR_DTR;
1327 if (clear & TIOCM_LOOP)
1328 mcr &= ~UART_MCR_LOOP;
1329
1330 mos7720_port->shadowMCR = mcr;
1331 lmcr = mos7720_port->shadowMCR;
1332
1333 send_mos_cmd(port->serial, MOS_WRITE,
1334 port->number - port->serial->minor, UART_MCR, &lmcr);
1335
1336 return 0;
1337}
1338
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001339static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1340 unsigned int __user *value)
1341{
1342 unsigned int mcr ;
1343 unsigned int arg;
1344 unsigned char data;
1345
1346 struct usb_serial_port *port;
1347
1348 if (mos7720_port == NULL)
1349 return -1;
1350
Alan Cox4da1a172008-07-22 11:16:21 +01001351 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001352 mcr = mos7720_port->shadowMCR;
1353
1354 if (copy_from_user(&arg, value, sizeof(int)))
1355 return -EFAULT;
1356
1357 switch (cmd) {
1358 case TIOCMBIS:
1359 if (arg & TIOCM_RTS)
1360 mcr |= UART_MCR_RTS;
1361 if (arg & TIOCM_DTR)
1362 mcr |= UART_MCR_RTS;
1363 if (arg & TIOCM_LOOP)
1364 mcr |= UART_MCR_LOOP;
1365 break;
1366
1367 case TIOCMBIC:
1368 if (arg & TIOCM_RTS)
1369 mcr &= ~UART_MCR_RTS;
1370 if (arg & TIOCM_DTR)
1371 mcr &= ~UART_MCR_RTS;
1372 if (arg & TIOCM_LOOP)
1373 mcr &= ~UART_MCR_LOOP;
1374 break;
1375
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001376 }
1377
1378 mos7720_port->shadowMCR = mcr;
1379
1380 data = mos7720_port->shadowMCR;
1381 send_mos_cmd(port->serial, MOS_WRITE,
1382 port->number - port->serial->minor, UART_MCR, &data);
1383
1384 return 0;
1385}
1386
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001387static int get_serial_info(struct moschip_port *mos7720_port,
1388 struct serial_struct __user *retinfo)
1389{
1390 struct serial_struct tmp;
1391
1392 if (!retinfo)
1393 return -EFAULT;
1394
1395 memset(&tmp, 0, sizeof(tmp));
1396
1397 tmp.type = PORT_16550A;
1398 tmp.line = mos7720_port->port->serial->minor;
1399 tmp.port = mos7720_port->port->number;
1400 tmp.irq = 0;
1401 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001402 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001403 tmp.baud_base = 9600;
1404 tmp.close_delay = 5*HZ;
1405 tmp.closing_wait = 30*HZ;
1406
1407 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1408 return -EFAULT;
1409 return 0;
1410}
1411
Alan Cox95da3102008-07-22 11:09:07 +01001412static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001413 unsigned int cmd, unsigned long arg)
1414{
Alan Cox95da3102008-07-22 11:09:07 +01001415 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001416 struct moschip_port *mos7720_port;
1417 struct async_icount cnow;
1418 struct async_icount cprev;
1419 struct serial_icounter_struct icount;
1420
1421 mos7720_port = usb_get_serial_port_data(port);
1422 if (mos7720_port == NULL)
1423 return -ENODEV;
1424
Harvey Harrison441b62c2008-03-03 16:08:34 -08001425 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001426
1427 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001428 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001429 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox4da1a172008-07-22 11:16:21 +01001430 return get_lsr_info(tty, mos7720_port,
1431 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001432 return 0;
1433
Alan Cox95da3102008-07-22 11:09:07 +01001434 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001435 case TIOCMBIS:
1436 case TIOCMBIC:
Alan Cox4da1a172008-07-22 11:16:21 +01001437 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
1438 __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001439 return set_modem_info(mos7720_port, cmd,
1440 (unsigned int __user *)arg);
1441
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001442 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001443 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001444 return get_serial_info(mos7720_port,
1445 (struct serial_struct __user *)arg);
1446
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001447 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001448 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001449 cprev = mos7720_port->icount;
1450 while (1) {
1451 if (signal_pending(current))
1452 return -ERESTARTSYS;
1453 cnow = mos7720_port->icount;
1454 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1455 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1456 return -EIO; /* no change => error */
1457 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1458 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1459 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
Alan Cox4da1a172008-07-22 11:16:21 +01001460 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001461 return 0;
1462 }
1463 cprev = cnow;
1464 }
1465 /* NOTREACHED */
1466 break;
1467
1468 case TIOCGICOUNT:
1469 cnow = mos7720_port->icount;
1470 icount.cts = cnow.cts;
1471 icount.dsr = cnow.dsr;
1472 icount.rng = cnow.rng;
1473 icount.dcd = cnow.dcd;
1474 icount.rx = cnow.rx;
1475 icount.tx = cnow.tx;
1476 icount.frame = cnow.frame;
1477 icount.overrun = cnow.overrun;
1478 icount.parity = cnow.parity;
1479 icount.brk = cnow.brk;
1480 icount.buf_overrun = cnow.buf_overrun;
1481
Harvey Harrison441b62c2008-03-03 16:08:34 -08001482 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
Alan Cox4da1a172008-07-22 11:16:21 +01001483 port->number, icount.rx, icount.tx);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001484 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1485 return -EFAULT;
1486 return 0;
1487 }
1488
1489 return -ENOIOCTLCMD;
1490}
1491
1492static int mos7720_startup(struct usb_serial *serial)
1493{
1494 struct moschip_serial *mos7720_serial;
1495 struct moschip_port *mos7720_port;
1496 struct usb_device *dev;
1497 int i;
1498 char data;
1499
Harvey Harrison441b62c2008-03-03 16:08:34 -08001500 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001501
1502 if (!serial) {
1503 dbg("Invalid Handler");
1504 return -ENODEV;
1505 }
1506
1507 dev = serial->dev;
1508
1509 /* create our private serial structure */
1510 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1511 if (mos7720_serial == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001512 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001513 return -ENOMEM;
1514 }
1515
1516 usb_set_serial_data(serial, mos7720_serial);
1517
1518 /* we set up the pointers to the endpoints in the mos7720_open *
1519 * function, as the structures aren't created yet. */
1520
1521 /* set up port private structures */
1522 for (i = 0; i < serial->num_ports; ++i) {
1523 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1524 if (mos7720_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001525 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001526 usb_set_serial_data(serial, NULL);
1527 kfree(mos7720_serial);
1528 return -ENOMEM;
1529 }
1530
1531 /* Initialize all port interrupt end point to port 0 int
1532 * endpoint. Our device has only one interrupt endpoint
1533 * comman to all ports */
Alan Cox4da1a172008-07-22 11:16:21 +01001534 serial->port[i]->interrupt_in_endpointAddress =
1535 serial->port[0]->interrupt_in_endpointAddress;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001536
1537 mos7720_port->port = serial->port[i];
1538 usb_set_serial_port_data(serial->port[i], mos7720_port);
1539
1540 dbg("port number is %d", serial->port[i]->number);
1541 dbg("serial number is %d", serial->minor);
1542 }
1543
1544
1545 /* setting configuration feature to one */
1546 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox4da1a172008-07-22 11:16:21 +01001547 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001548
Alan Cox4da1a172008-07-22 11:16:21 +01001549 /* LSR For Port 1 */
1550 send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
1551 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001552
Alan Cox4da1a172008-07-22 11:16:21 +01001553 /* LSR For Port 2 */
1554 send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
1555 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001556
1557 return 0;
1558}
1559
Alan Sternf9c99bb2009-06-02 11:53:55 -04001560static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001561{
1562 int i;
1563
1564 /* free private structure allocated for serial port */
Alan Sternf9c99bb2009-06-02 11:53:55 -04001565 for (i = 0; i < serial->num_ports; ++i)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001566 kfree(usb_get_serial_port_data(serial->port[i]));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001567
1568 /* free private structure allocated for serial device */
1569 kfree(usb_get_serial_data(serial));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001570}
1571
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001572static struct usb_driver usb_driver = {
1573 .name = "moschip7720",
1574 .probe = usb_serial_probe,
1575 .disconnect = usb_serial_disconnect,
1576 .id_table = moschip_port_id_table,
1577 .no_dynamic_id = 1,
1578};
1579
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001580static struct usb_serial_driver moschip7720_2port_driver = {
1581 .driver = {
1582 .owner = THIS_MODULE,
1583 .name = "moschip7720",
1584 },
1585 .description = "Moschip 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001586 .usb_driver = &usb_driver,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001587 .id_table = moschip_port_id_table,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001588 .num_ports = 2,
1589 .open = mos7720_open,
1590 .close = mos7720_close,
1591 .throttle = mos7720_throttle,
1592 .unthrottle = mos7720_unthrottle,
1593 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001594 .release = mos7720_release,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001595 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001596 .tiocmget = mos7720_tiocmget,
1597 .tiocmset = mos7720_tiocmset,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001598 .set_termios = mos7720_set_termios,
1599 .write = mos7720_write,
1600 .write_room = mos7720_write_room,
1601 .chars_in_buffer = mos7720_chars_in_buffer,
1602 .break_ctl = mos7720_break,
1603 .read_bulk_callback = mos7720_bulk_in_callback,
Oliver Neukume8e30c72007-03-14 11:11:08 +01001604 .read_int_callback = mos7720_interrupt_callback,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001605};
1606
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001607static int __init moschip7720_init(void)
1608{
1609 int retval;
1610
Harvey Harrison441b62c2008-03-03 16:08:34 -08001611 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001612
1613 /* Register with the usb serial */
1614 retval = usb_serial_register(&moschip7720_2port_driver);
1615 if (retval)
1616 goto failed_port_device_register;
1617
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001618 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1619 DRIVER_DESC "\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001620
1621 /* Register with the usb */
1622 retval = usb_register(&usb_driver);
1623 if (retval)
1624 goto failed_usb_register;
1625
1626 return 0;
1627
1628failed_usb_register:
1629 usb_serial_deregister(&moschip7720_2port_driver);
1630
1631failed_port_device_register:
1632 return retval;
1633}
1634
1635static void __exit moschip7720_exit(void)
1636{
1637 usb_deregister(&usb_driver);
1638 usb_serial_deregister(&moschip7720_2port_driver);
1639}
1640
1641module_init(moschip7720_init);
1642module_exit(moschip7720_exit);
1643
1644/* Module information */
Alan Cox4da1a172008-07-22 11:16:21 +01001645MODULE_AUTHOR(DRIVER_AUTHOR);
1646MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001647MODULE_LICENSE("GPL");
1648
1649module_param(debug, bool, S_IRUGO | S_IWUSR);
1650MODULE_PARM_DESC(debug, "Debug enabled or not");