blob: 763e32a44be02eb790c0c7f66ed25e0bae947f19 [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 Coxa509a7e2009-09-19 13:13:26 -0700322static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700323{
324 struct usb_serial *serial;
325 struct usb_serial_port *port0;
326 struct urb *urb;
327 struct moschip_serial *mos7720_serial;
328 struct moschip_port *mos7720_port;
329 int response;
330 int port_number;
331 char data;
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100332 int allocated_urbs = 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700333 int j;
334
335 serial = port->serial;
336
337 mos7720_port = usb_get_serial_port_data(port);
338 if (mos7720_port == NULL)
339 return -ENODEV;
340
341 port0 = serial->port[0];
342
343 mos7720_serial = usb_get_serial_data(serial);
344
345 if (mos7720_serial == NULL || port0 == NULL)
346 return -ENODEV;
347
348 usb_clear_halt(serial->dev, port->write_urb->pipe);
349 usb_clear_halt(serial->dev, port->read_urb->pipe);
350
351 /* Initialising the write urb pool */
352 for (j = 0; j < NUM_URBS; ++j) {
Alan Cox4da1a172008-07-22 11:16:21 +0100353 urb = usb_alloc_urb(0, GFP_KERNEL);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700354 mos7720_port->write_urb_pool[j] = urb;
355
356 if (urb == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700357 dev_err(&port->dev, "No more urbs???\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700358 continue;
359 }
360
361 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
362 GFP_KERNEL);
363 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700364 dev_err(&port->dev,
365 "%s-out of memory for urb buffers.\n",
366 __func__);
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100367 usb_free_urb(mos7720_port->write_urb_pool[j]);
368 mos7720_port->write_urb_pool[j] = NULL;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700369 continue;
370 }
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100371 allocated_urbs++;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700372 }
373
Oliver Neukumfe4b65e2007-03-14 15:22:25 +0100374 if (!allocated_urbs)
375 return -ENOMEM;
376
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700377 /* Initialize MCS7720 -- Write Init values to corresponding Registers
378 *
379 * Register Index
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -0700380 * 0 : THR/RHR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700381 * 1 : IER
382 * 2 : FCR
383 * 3 : LCR
384 * 4 : MCR
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -0700385 * 5 : LSR
386 * 6 : MSR
387 * 7 : SPR
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700388 *
389 * 0x08 : SP1/2 Control Reg
390 */
391 port_number = port->number - port->serial->minor;
392 send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
Alan Cox4da1a172008-07-22 11:16:21 +0100393 dbg("SS::%p LSR:%x\n", mos7720_port, data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700394
395 dbg("Check:Sending Command ..........");
396
397 data = 0x02;
398 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
399 data = 0x02;
400 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
401
402 data = 0x00;
403 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
404 data = 0x00;
405 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
406
407 data = 0xCF;
408 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
409 data = 0x03;
Alan Cox4da1a172008-07-22 11:16:21 +0100410 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700411 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
412 data = 0x0b;
Alan Cox4da1a172008-07-22 11:16:21 +0100413 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700414 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
415 data = 0x0b;
416 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
417
418 data = 0x00;
419 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
420 data = 0x00;
421 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
422
423/* data = 0x00;
424 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
425 data = 0x03;
426 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
427 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100428 send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
429 port_number + 1, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700430*/
431 data = 0x00;
432 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
433
434 data = data | (port->number - port->serial->minor + 1);
435 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
436
437 data = 0x83;
Alan Cox4da1a172008-07-22 11:16:21 +0100438 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700439 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
440 data = 0x0c;
441 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
442 data = 0x00;
443 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
444 data = 0x03;
Alan Cox4da1a172008-07-22 11:16:21 +0100445 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700446 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
447 data = 0x0c;
448 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
449 data = 0x0c;
450 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
451
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700452 /* see if we've set up our endpoint info yet *
453 * (can't set it up in mos7720_startup as the *
454 * structures were not set up at that time.) */
455 if (!mos7720_serial->interrupt_started) {
456 dbg("Interrupt buffer NULL !!!");
457
458 /* not set up yet, so do it now */
459 mos7720_serial->interrupt_started = 1;
460
461 dbg("To Submit URB !!!");
462
463 /* set up our interrupt urb */
464 usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100465 usb_rcvintpipe(serial->dev,
466 port->interrupt_in_endpointAddress),
467 port0->interrupt_in_buffer,
468 port0->interrupt_in_urb->transfer_buffer_length,
469 mos7720_interrupt_callback, mos7720_port,
470 port0->interrupt_in_urb->interval);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700471
472 /* start interrupt read for this mos7720 this interrupt *
Alan Cox4da1a172008-07-22 11:16:21 +0100473 * will continue as long as the mos7720 is connected */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700474 dbg("Submit URB over !!!");
475 response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
476 if (response)
477 dev_err(&port->dev,
Joe Perches898eb712007-10-18 03:06:30 -0700478 "%s - Error %d submitting control urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800479 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700480 }
481
482 /* set up our bulk in urb */
483 usb_fill_bulk_urb(port->read_urb, serial->dev,
484 usb_rcvbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100485 port->bulk_in_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700486 port->bulk_in_buffer,
487 port->read_urb->transfer_buffer_length,
488 mos7720_bulk_in_callback, mos7720_port);
489 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
490 if (response)
Alan Cox4da1a172008-07-22 11:16:21 +0100491 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
492 __func__, response);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700493
494 /* initialize our icount structure */
495 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
496
497 /* initialize our port settings */
498 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
499
500 /* send a open port command */
501 mos7720_port->open = 1;
502
503 return 0;
504}
505
506/*
507 * mos7720_chars_in_buffer
508 * this function is called by the tty driver when it wants to know how many
509 * bytes of data we currently have outstanding in the port (data that has
510 * been written, but hasn't made it out the port yet)
511 * If successful, we return the number of bytes left to be written in the
512 * system,
513 * Otherwise we return a negative error number.
514 */
Alan Cox95da3102008-07-22 11:09:07 +0100515static int mos7720_chars_in_buffer(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700516{
Alan Cox95da3102008-07-22 11:09:07 +0100517 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700518 int i;
519 int chars = 0;
520 struct moschip_port *mos7720_port;
521
Harvey Harrison441b62c2008-03-03 16:08:34 -0800522 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700523
524 mos7720_port = usb_get_serial_port_data(port);
525 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800526 dbg("%s:leaving ...........", __func__);
Alan Cox23198fd2009-07-20 16:05:27 +0100527 return 0;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700528 }
529
530 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100531 if (mos7720_port->write_urb_pool[i] &&
532 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700533 chars += URB_TRANSFER_BUFFER_SIZE;
534 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800535 dbg("%s - returns %d", __func__, chars);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700536 return chars;
537}
538
Alan Cox335f8512009-06-11 12:26:29 +0100539static void mos7720_close(struct usb_serial_port *port)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700540{
541 struct usb_serial *serial;
542 struct moschip_port *mos7720_port;
543 char data;
544 int j;
545
546 dbg("mos7720_close:entering...");
547
548 serial = port->serial;
549
550 mos7720_port = usb_get_serial_port_data(port);
551 if (mos7720_port == NULL)
552 return;
553
554 for (j = 0; j < NUM_URBS; ++j)
555 usb_kill_urb(mos7720_port->write_urb_pool[j]);
556
557 /* Freeing Write URBs */
558 for (j = 0; j < NUM_URBS; ++j) {
559 if (mos7720_port->write_urb_pool[j]) {
560 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
561 usb_free_urb(mos7720_port->write_urb_pool[j]);
562 }
563 }
564
565 /* While closing port, shutdown all bulk read, write *
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100566 * and interrupt read if they exists, otherwise nop */
567 dbg("Shutdown bulk write");
568 usb_kill_urb(port->write_urb);
569 dbg("Shutdown bulk read");
570 usb_kill_urb(port->read_urb);
571
572 mutex_lock(&serial->disc_mutex);
573 /* these commands must not be issued if the device has
574 * been disconnected */
575 if (!serial->disconnected) {
576 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100577 send_mos_cmd(serial, MOS_WRITE,
578 port->number - port->serial->minor, 0x04, &data);
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100579
580 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +0100581 send_mos_cmd(serial, MOS_WRITE,
582 port->number - port->serial->minor, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700583 }
Oliver Neukuma1cd7e92008-01-16 17:18:52 +0100584 mutex_unlock(&serial->disc_mutex);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700585 mos7720_port->open = 0;
586
Harvey Harrison441b62c2008-03-03 16:08:34 -0800587 dbg("Leaving %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700588}
589
Alan Cox95da3102008-07-22 11:09:07 +0100590static void mos7720_break(struct tty_struct *tty, int break_state)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700591{
Alan Cox95da3102008-07-22 11:09:07 +0100592 struct usb_serial_port *port = tty->driver_data;
Alan Cox4da1a172008-07-22 11:16:21 +0100593 unsigned char data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700594 struct usb_serial *serial;
595 struct moschip_port *mos7720_port;
596
Harvey Harrison441b62c2008-03-03 16:08:34 -0800597 dbg("Entering %s", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700598
599 serial = port->serial;
600
601 mos7720_port = usb_get_serial_port_data(port);
602 if (mos7720_port == NULL)
603 return;
604
605 if (break_state == -1)
606 data = mos7720_port->shadowLCR | UART_LCR_SBC;
607 else
608 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
609
610 mos7720_port->shadowLCR = data;
611 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
612 0x03, &data);
613
614 return;
615}
616
617/*
618 * mos7720_write_room
619 * this function is called by the tty driver when it wants to know how many
620 * bytes of data we can accept for a specific port.
621 * If successful, we return the amount of room that we have for this port
622 * Otherwise we return a negative error number.
623 */
Alan Cox95da3102008-07-22 11:09:07 +0100624static int mos7720_write_room(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700625{
Alan Cox95da3102008-07-22 11:09:07 +0100626 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700627 struct moschip_port *mos7720_port;
628 int room = 0;
629 int i;
630
Harvey Harrison441b62c2008-03-03 16:08:34 -0800631 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700632
633 mos7720_port = usb_get_serial_port_data(port);
634 if (mos7720_port == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800635 dbg("%s:leaving ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700636 return -ENODEV;
637 }
638
Alan Coxa5b6f602008-04-08 17:16:06 +0100639 /* FIXME: Locking */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700640 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100641 if (mos7720_port->write_urb_pool[i] &&
642 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700643 room += URB_TRANSFER_BUFFER_SIZE;
644 }
645
Harvey Harrison441b62c2008-03-03 16:08:34 -0800646 dbg("%s - returns %d", __func__, room);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700647 return room;
648}
649
Alan Cox95da3102008-07-22 11:09:07 +0100650static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
651 const unsigned char *data, int count)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700652{
653 int status;
654 int i;
655 int bytes_sent = 0;
656 int transfer_size;
657
658 struct moschip_port *mos7720_port;
659 struct usb_serial *serial;
660 struct urb *urb;
661 const unsigned char *current_position = data;
662
Harvey Harrison441b62c2008-03-03 16:08:34 -0800663 dbg("%s:entering ...........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700664
665 serial = port->serial;
666
667 mos7720_port = usb_get_serial_port_data(port);
668 if (mos7720_port == NULL) {
669 dbg("mos7720_port is NULL");
670 return -ENODEV;
671 }
672
673 /* try to find a free urb in the list */
674 urb = NULL;
675
676 for (i = 0; i < NUM_URBS; ++i) {
Alan Cox4da1a172008-07-22 11:16:21 +0100677 if (mos7720_port->write_urb_pool[i] &&
678 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700679 urb = mos7720_port->write_urb_pool[i];
Alan Cox4da1a172008-07-22 11:16:21 +0100680 dbg("URB:%d", i);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700681 break;
682 }
683 }
684
685 if (urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800686 dbg("%s - no more free urbs", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700687 goto exit;
688 }
689
690 if (urb->transfer_buffer == NULL) {
691 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
692 GFP_KERNEL);
693 if (urb->transfer_buffer == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700694 dev_err(&port->dev, "%s no more kernel memory...\n",
695 __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700696 goto exit;
697 }
698 }
Alan Cox4da1a172008-07-22 11:16:21 +0100699 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700700
701 memcpy(urb->transfer_buffer, current_position, transfer_size);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800702 usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700703 urb->transfer_buffer);
704
705 /* fill urb with data and submit */
706 usb_fill_bulk_urb(urb, serial->dev,
707 usb_sndbulkpipe(serial->dev,
Alan Cox4da1a172008-07-22 11:16:21 +0100708 port->bulk_out_endpointAddress),
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700709 urb->transfer_buffer, transfer_size,
710 mos7720_bulk_out_data_callback, mos7720_port);
711
712 /* send it down the pipe */
Alan Cox4da1a172008-07-22 11:16:21 +0100713 status = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700714 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700715 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
716 "with status = %d\n", __func__, status);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700717 bytes_sent = status;
718 goto exit;
719 }
720 bytes_sent = transfer_size;
721
722exit:
723 return bytes_sent;
724}
725
Alan Cox95da3102008-07-22 11:09:07 +0100726static void mos7720_throttle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700727{
Alan Cox95da3102008-07-22 11:09:07 +0100728 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700729 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700730 int status;
731
Harvey Harrison441b62c2008-03-03 16:08:34 -0800732 dbg("%s- port %d\n", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700733
734 mos7720_port = usb_get_serial_port_data(port);
735
736 if (mos7720_port == NULL)
737 return;
738
739 if (!mos7720_port->open) {
740 dbg("port not opened");
741 return;
742 }
743
Harvey Harrison441b62c2008-03-03 16:08:34 -0800744 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700745
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700746 /* if we are implementing XON/XOFF, send the stop character */
747 if (I_IXOFF(tty)) {
748 unsigned char stop_char = STOP_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +0100749 status = mos7720_write(tty, port, &stop_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700750 if (status <= 0)
751 return;
752 }
753
754 /* if we are implementing RTS/CTS, toggle that line */
755 if (tty->termios->c_cflag & CRTSCTS) {
756 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
757 status = send_mos_cmd(port->serial, MOS_WRITE,
758 port->number - port->serial->minor,
759 UART_MCR, &mos7720_port->shadowMCR);
760 if (status != 0)
761 return;
762 }
763}
764
Alan Cox95da3102008-07-22 11:09:07 +0100765static void mos7720_unthrottle(struct tty_struct *tty)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700766{
Alan Cox95da3102008-07-22 11:09:07 +0100767 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700768 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +0100769 int status;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700770
771 if (mos7720_port == NULL)
772 return;
773
774 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800775 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700776 return;
777 }
778
Harvey Harrison441b62c2008-03-03 16:08:34 -0800779 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700780
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700781 /* if we are implementing XON/XOFF, send the start character */
782 if (I_IXOFF(tty)) {
783 unsigned char start_char = START_CHAR(tty);
Alan Cox95da3102008-07-22 11:09:07 +0100784 status = mos7720_write(tty, port, &start_char, 1);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700785 if (status <= 0)
786 return;
787 }
788
789 /* if we are implementing RTS/CTS, toggle that line */
790 if (tty->termios->c_cflag & CRTSCTS) {
791 mos7720_port->shadowMCR |= UART_MCR_RTS;
792 status = send_mos_cmd(port->serial, MOS_WRITE,
793 port->number - port->serial->minor,
794 UART_MCR, &mos7720_port->shadowMCR);
795 if (status != 0)
796 return;
797 }
798}
799
800static int set_higher_rates(struct moschip_port *mos7720_port,
801 unsigned int baud)
802{
803 unsigned char data;
804 struct usb_serial_port *port;
805 struct usb_serial *serial;
806 int port_number;
807
808 if (mos7720_port == NULL)
809 return -EINVAL;
810
811 port = mos7720_port->port;
812 serial = port->serial;
813
Alan Cox4da1a172008-07-22 11:16:21 +0100814 /***********************************************
815 * Init Sequence for higher rates
816 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700817 dbg("Sending Setting Commands ..........");
818 port_number = port->number - port->serial->minor;
819
820 data = 0x000;
821 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
822 data = 0x000;
823 send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
824 data = 0x0CF;
825 send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
826 data = 0x00b;
Alan Cox4da1a172008-07-22 11:16:21 +0100827 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700828 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
829 data = 0x00b;
830 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
831
832 data = 0x000;
833 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
834 data = 0x000;
835 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
836
837
Alan Cox4da1a172008-07-22 11:16:21 +0100838 /***********************************************
839 * Set for higher rates *
840 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700841
842 data = baud * 0x10;
Alan Cox4da1a172008-07-22 11:16:21 +0100843 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700844
845 data = 0x003;
846 send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
847 data = 0x003;
848 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
849
850 data = 0x02b;
Alan Cox4da1a172008-07-22 11:16:21 +0100851 mos7720_port->shadowMCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700852 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
853 data = 0x02b;
854 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
855
Alan Cox4da1a172008-07-22 11:16:21 +0100856 /***********************************************
857 * Set DLL/DLM
858 ***********************************************/
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700859
860 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
Alan Cox4da1a172008-07-22 11:16:21 +0100861 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700862 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
863
864 data = 0x001; /* DLL */
Alan Cox4da1a172008-07-22 11:16:21 +0100865 send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700866 data = 0x000; /* DLM */
Alan Cox4da1a172008-07-22 11:16:21 +0100867 send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700868
869 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
Alan Cox4da1a172008-07-22 11:16:21 +0100870 mos7720_port->shadowLCR = data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700871 send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
872
873 return 0;
874}
875
876/* baud rate information */
Alan Cox4da1a172008-07-22 11:16:21 +0100877struct divisor_table_entry {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700878 __u32 baudrate;
879 __u16 divisor;
880};
881
882/* Define table of divisors for moschip 7720 hardware *
883 * These assume a 3.6864MHz crystal, the standard /16, and *
884 * MCR.7 = 0. */
885static struct divisor_table_entry divisor_table[] = {
886 { 50, 2304},
887 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
888 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
889 { 150, 768},
890 { 300, 384},
891 { 600, 192},
892 { 1200, 96},
893 { 1800, 64},
894 { 2400, 48},
895 { 4800, 24},
896 { 7200, 16},
897 { 9600, 12},
898 { 19200, 6},
899 { 38400, 3},
900 { 57600, 2},
901 { 115200, 1},
902};
903
904/*****************************************************************************
905 * calc_baud_rate_divisor
906 * this function calculates the proper baud rate divisor for the specified
907 * baud rate.
908 *****************************************************************************/
909static int calc_baud_rate_divisor(int baudrate, int *divisor)
910{
911 int i;
912 __u16 custom;
913 __u16 round1;
914 __u16 round;
915
916
Harvey Harrison441b62c2008-03-03 16:08:34 -0800917 dbg("%s - %d", __func__, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700918
919 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
920 if (divisor_table[i].baudrate == baudrate) {
921 *divisor = divisor_table[i].divisor;
922 return 0;
923 }
924 }
925
Alan Cox4da1a172008-07-22 11:16:21 +0100926 /* After trying for all the standard baud rates *
927 * Try calculating the divisor for this baud rate */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700928 if (baudrate > 75 && baudrate < 230400) {
929 /* get the divisor */
930 custom = (__u16)(230400L / baudrate);
931
932 /* Check for round off */
933 round1 = (__u16)(2304000L / baudrate);
934 round = (__u16)(round1 - (custom * 10));
935 if (round > 4)
936 custom++;
937 *divisor = custom;
938
Alan Cox4da1a172008-07-22 11:16:21 +0100939 dbg("Baud %d = %d", baudrate, custom);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700940 return 0;
941 }
942
943 dbg("Baud calculation Failed...");
944 return -EINVAL;
945}
946
947/*
948 * send_cmd_write_baud_rate
949 * this function sends the proper command to change the baud rate of the
950 * specified port.
951 */
952static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
953 int baudrate)
954{
955 struct usb_serial_port *port;
956 struct usb_serial *serial;
957 int divisor;
958 int status;
959 unsigned char data;
960 unsigned char number;
961
962 if (mos7720_port == NULL)
963 return -1;
964
965 port = mos7720_port->port;
966 serial = port->serial;
967
Harvey Harrison441b62c2008-03-03 16:08:34 -0800968 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700969
970 number = port->number - port->serial->minor;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800971 dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700972
Alan Cox4da1a172008-07-22 11:16:21 +0100973 /* Calculate the Divisor */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700974 status = calc_baud_rate_divisor(baudrate, &divisor);
975 if (status) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700976 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700977 return status;
978 }
979
Alan Cox4da1a172008-07-22 11:16:21 +0100980 /* Enable access to divisor latch */
981 data = mos7720_port->shadowLCR | UART_LCR_DLAB;
982 mos7720_port->shadowLCR = data;
983 send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700984
985 /* Write the divisor */
986 data = ((unsigned char)(divisor & 0xff));
Alan Cox4da1a172008-07-22 11:16:21 +0100987 send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700988
989 data = ((unsigned char)((divisor & 0xff00) >> 8));
Alan Cox4da1a172008-07-22 11:16:21 +0100990 send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700991
Alan Cox4da1a172008-07-22 11:16:21 +0100992 /* Disable access to divisor latch */
993 data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
994 mos7720_port->shadowLCR = data;
995 send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -0700996
997 return status;
998}
999
1000/*
1001 * change_port_settings
1002 * This routine is called to set the UART on the device to match
1003 * the specified new settings.
1004 */
Alan Cox95da3102008-07-22 11:09:07 +01001005static void change_port_settings(struct tty_struct *tty,
1006 struct moschip_port *mos7720_port,
Alan Cox606d0992006-12-08 02:38:45 -08001007 struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001008{
1009 struct usb_serial_port *port;
1010 struct usb_serial *serial;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001011 int baud;
1012 unsigned cflag;
1013 unsigned iflag;
1014 __u8 mask = 0xff;
1015 __u8 lData;
1016 __u8 lParity;
1017 __u8 lStop;
1018 int status;
1019 int port_number;
1020 char data;
1021
1022 if (mos7720_port == NULL)
1023 return ;
1024
1025 port = mos7720_port->port;
1026 serial = port->serial;
1027 port_number = port->number - port->serial->minor;
1028
Harvey Harrison441b62c2008-03-03 16:08:34 -08001029 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001030
1031 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001032 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001033 return;
1034 }
1035
Harvey Harrison441b62c2008-03-03 16:08:34 -08001036 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001037
1038 lData = UART_LCR_WLEN8;
1039 lStop = 0x00; /* 1 stop bit */
1040 lParity = 0x00; /* No parity */
1041
1042 cflag = tty->termios->c_cflag;
1043 iflag = tty->termios->c_iflag;
1044
1045 /* Change the number of bits */
1046 switch (cflag & CSIZE) {
1047 case CS5:
1048 lData = UART_LCR_WLEN5;
1049 mask = 0x1f;
1050 break;
1051
1052 case CS6:
1053 lData = UART_LCR_WLEN6;
1054 mask = 0x3f;
1055 break;
1056
1057 case CS7:
1058 lData = UART_LCR_WLEN7;
1059 mask = 0x7f;
1060 break;
1061 default:
1062 case CS8:
1063 lData = UART_LCR_WLEN8;
1064 break;
1065 }
1066
1067 /* Change the Parity bit */
1068 if (cflag & PARENB) {
1069 if (cflag & PARODD) {
1070 lParity = UART_LCR_PARITY;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001071 dbg("%s - parity = odd", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001072 } else {
1073 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001074 dbg("%s - parity = even", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001075 }
1076
1077 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001078 dbg("%s - parity = none", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001079 }
1080
1081 if (cflag & CMSPAR)
1082 lParity = lParity | 0x20;
1083
1084 /* Change the Stop bit */
1085 if (cflag & CSTOPB) {
1086 lStop = UART_LCR_STOP;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001087 dbg("%s - stop bits = 2", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001088 } else {
1089 lStop = 0x00;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001090 dbg("%s - stop bits = 1", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001091 }
1092
1093#define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1094#define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1095#define LCR_PAR_MASK 0x38 /* Mask for parity field */
1096
1097 /* Update the LCR with the correct value */
Alan Cox4da1a172008-07-22 11:16:21 +01001098 mos7720_port->shadowLCR &=
1099 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001100 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1101
1102
1103 /* Disable Interrupts */
1104 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +01001105 send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
1106 UART_IER, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001107
1108 data = 0x00;
Alan Cox4da1a172008-07-22 11:16:21 +01001109 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001110
1111 data = 0xcf;
Alan Cox4da1a172008-07-22 11:16:21 +01001112 send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001113
1114 /* Send the updated LCR value to the mos7720 */
1115 data = mos7720_port->shadowLCR;
Alan Cox4da1a172008-07-22 11:16:21 +01001116 send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001117
Alan Cox4da1a172008-07-22 11:16:21 +01001118 data = 0x00b;
1119 mos7720_port->shadowMCR = data;
1120 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
1121 data = 0x00b;
1122 send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001123
1124 /* set up the MCR register and send it to the mos7720 */
1125 mos7720_port->shadowMCR = UART_MCR_OUT2;
1126 if (cflag & CBAUD)
1127 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1128
1129 if (cflag & CRTSCTS) {
1130 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
Alan Cox4da1a172008-07-22 11:16:21 +01001131 /* To set hardware flow control to the specified *
1132 * serial port, in SP1/2_CONTROL_REG */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001133 if (port->number) {
1134 data = 0x001;
1135 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1136 0x08, &data);
1137 } else {
1138 data = 0x002;
1139 send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
1140 0x08, &data);
1141 }
1142 } else {
1143 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1144 }
1145
1146 data = mos7720_port->shadowMCR;
1147 send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
1148
1149 /* Determine divisor based on baud rate */
1150 baud = tty_get_baud_rate(tty);
1151 if (!baud) {
1152 /* pick a default, any default... */
1153 dbg("Picked default baud...");
1154 baud = 9600;
1155 }
1156
1157 if (baud >= 230400) {
1158 set_higher_rates(mos7720_port, baud);
1159 /* Enable Interrupts */
1160 data = 0x0c;
1161 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1162 return;
1163 }
1164
Harvey Harrison441b62c2008-03-03 16:08:34 -08001165 dbg("%s - baud rate = %d", __func__, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001166 status = send_cmd_write_baud_rate(mos7720_port, baud);
Alan Cox65d063a2008-01-03 17:01:18 +00001167 /* FIXME: needs to write actual resulting baud back not just
1168 blindly do so */
1169 if (cflag & CBAUD)
1170 tty_encode_baud_rate(tty, baud, baud);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001171 /* Enable Interrupts */
1172 data = 0x0c;
1173 send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
1174
1175 if (port->read_urb->status != -EINPROGRESS) {
1176 port->read_urb->dev = serial->dev;
1177
1178 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1179 if (status)
1180 dbg("usb_submit_urb(read bulk) failed, status = %d",
1181 status);
1182 }
1183 return;
1184}
1185
1186/*
1187 * mos7720_set_termios
1188 * this function is called by the tty driver when it wants to change the
1189 * termios structure.
1190 */
Alan Cox95da3102008-07-22 11:09:07 +01001191static void mos7720_set_termios(struct tty_struct *tty,
1192 struct usb_serial_port *port, struct ktermios *old_termios)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001193{
1194 int status;
1195 unsigned int cflag;
1196 struct usb_serial *serial;
1197 struct moschip_port *mos7720_port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001198
1199 serial = port->serial;
1200
1201 mos7720_port = usb_get_serial_port_data(port);
1202
1203 if (mos7720_port == NULL)
1204 return;
1205
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001206 if (!mos7720_port->open) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001207 dbg("%s - port not opened", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001208 return;
1209 }
1210
Alan Cox4da1a172008-07-22 11:16:21 +01001211 dbg("%s\n", "setting termios - ASPIRE");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001212
1213 cflag = tty->termios->c_cflag;
1214
Harvey Harrison441b62c2008-03-03 16:08:34 -08001215 dbg("%s - cflag %08x iflag %08x", __func__,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001216 tty->termios->c_cflag,
1217 RELEVANT_IFLAG(tty->termios->c_iflag));
1218
Harvey Harrison441b62c2008-03-03 16:08:34 -08001219 dbg("%s - old cflag %08x old iflag %08x", __func__,
Alan Cox65d063a2008-01-03 17:01:18 +00001220 old_termios->c_cflag,
1221 RELEVANT_IFLAG(old_termios->c_iflag));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001222
Harvey Harrison441b62c2008-03-03 16:08:34 -08001223 dbg("%s - port %d", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001224
1225 /* change the port settings to the new ones specified */
Alan Cox95da3102008-07-22 11:09:07 +01001226 change_port_settings(tty, mos7720_port, old_termios);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001227
Alan Cox4da1a172008-07-22 11:16:21 +01001228 if (!port->read_urb) {
1229 dbg("%s", "URB KILLED !!!!!\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001230 return;
1231 }
1232
Alan Cox4da1a172008-07-22 11:16:21 +01001233 if (port->read_urb->status != -EINPROGRESS) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001234 port->read_urb->dev = serial->dev;
1235 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1236 if (status)
1237 dbg("usb_submit_urb(read bulk) failed, status = %d",
1238 status);
1239 }
1240 return;
1241}
1242
1243/*
1244 * get_lsr_info - get line status register info
1245 *
1246 * Purpose: Let user call ioctl() to get info when the UART physically
1247 * is emptied. On bus types like RS485, the transmitter must
1248 * release the bus after transmitting. This must be done when
1249 * the transmit shift register is empty, not be done when the
1250 * transmit holding register is empty. This functionality
1251 * allows an RS485 driver to be written in user space.
1252 */
Alan Cox4da1a172008-07-22 11:16:21 +01001253static int get_lsr_info(struct tty_struct *tty,
1254 struct moschip_port *mos7720_port, unsigned int __user *value)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001255{
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001256 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001257 unsigned int result = 0;
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001258 unsigned char data = 0;
1259 int port_number = port->number - port->serial->minor;
1260 int count;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001261
Alan Cox95da3102008-07-22 11:09:07 +01001262 count = mos7720_chars_in_buffer(tty);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001263 if (count == 0) {
Kees Schoenmakers2f9ea552009-09-19 13:13:18 -07001264 send_mos_cmd(port->serial, MOS_READ, port_number,
1265 UART_LSR, &data);
1266 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1267 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1268 dbg("%s -- Empty", __func__);
1269 result = TIOCSER_TEMT;
1270 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001271 }
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001272 if (copy_to_user(value, &result, sizeof(int)))
1273 return -EFAULT;
1274 return 0;
1275}
1276
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001277static int mos7720_tiocmget(struct tty_struct *tty, struct file *file)
1278{
1279 struct usb_serial_port *port = tty->driver_data;
1280 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1281 unsigned int result = 0;
1282 unsigned int mcr ;
1283 unsigned int msr ;
1284
1285 dbg("%s - port %d", __func__, port->number);
1286
1287 mcr = mos7720_port->shadowMCR;
1288 msr = mos7720_port->shadowMSR;
1289
1290 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1291 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1292 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1293 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1294 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1295 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1296
1297 dbg("%s -- %x", __func__, result);
1298
1299 return result;
1300}
1301
1302static int mos7720_tiocmset(struct tty_struct *tty, struct file *file,
1303 unsigned int set, unsigned int clear)
1304{
1305 struct usb_serial_port *port = tty->driver_data;
1306 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1307 unsigned int mcr ;
1308 unsigned char lmcr;
1309
1310 dbg("%s - port %d", __func__, port->number);
1311 dbg("he was at tiocmget");
1312
1313 mcr = mos7720_port->shadowMCR;
1314
1315 if (set & TIOCM_RTS)
1316 mcr |= UART_MCR_RTS;
1317 if (set & TIOCM_DTR)
1318 mcr |= UART_MCR_DTR;
1319 if (set & TIOCM_LOOP)
1320 mcr |= UART_MCR_LOOP;
1321
1322 if (clear & TIOCM_RTS)
1323 mcr &= ~UART_MCR_RTS;
1324 if (clear & TIOCM_DTR)
1325 mcr &= ~UART_MCR_DTR;
1326 if (clear & TIOCM_LOOP)
1327 mcr &= ~UART_MCR_LOOP;
1328
1329 mos7720_port->shadowMCR = mcr;
1330 lmcr = mos7720_port->shadowMCR;
1331
1332 send_mos_cmd(port->serial, MOS_WRITE,
1333 port->number - port->serial->minor, UART_MCR, &lmcr);
1334
1335 return 0;
1336}
1337
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001338static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1339 unsigned int __user *value)
1340{
1341 unsigned int mcr ;
1342 unsigned int arg;
1343 unsigned char data;
1344
1345 struct usb_serial_port *port;
1346
1347 if (mos7720_port == NULL)
1348 return -1;
1349
Alan Cox4da1a172008-07-22 11:16:21 +01001350 port = (struct usb_serial_port *)mos7720_port->port;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001351 mcr = mos7720_port->shadowMCR;
1352
1353 if (copy_from_user(&arg, value, sizeof(int)))
1354 return -EFAULT;
1355
1356 switch (cmd) {
1357 case TIOCMBIS:
1358 if (arg & TIOCM_RTS)
1359 mcr |= UART_MCR_RTS;
1360 if (arg & TIOCM_DTR)
1361 mcr |= UART_MCR_RTS;
1362 if (arg & TIOCM_LOOP)
1363 mcr |= UART_MCR_LOOP;
1364 break;
1365
1366 case TIOCMBIC:
1367 if (arg & TIOCM_RTS)
1368 mcr &= ~UART_MCR_RTS;
1369 if (arg & TIOCM_DTR)
1370 mcr &= ~UART_MCR_RTS;
1371 if (arg & TIOCM_LOOP)
1372 mcr &= ~UART_MCR_LOOP;
1373 break;
1374
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001375 }
1376
1377 mos7720_port->shadowMCR = mcr;
1378
1379 data = mos7720_port->shadowMCR;
1380 send_mos_cmd(port->serial, MOS_WRITE,
1381 port->number - port->serial->minor, UART_MCR, &data);
1382
1383 return 0;
1384}
1385
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001386static int get_serial_info(struct moschip_port *mos7720_port,
1387 struct serial_struct __user *retinfo)
1388{
1389 struct serial_struct tmp;
1390
1391 if (!retinfo)
1392 return -EFAULT;
1393
1394 memset(&tmp, 0, sizeof(tmp));
1395
1396 tmp.type = PORT_16550A;
1397 tmp.line = mos7720_port->port->serial->minor;
1398 tmp.port = mos7720_port->port->number;
1399 tmp.irq = 0;
1400 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Alan Cox4da1a172008-07-22 11:16:21 +01001401 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001402 tmp.baud_base = 9600;
1403 tmp.close_delay = 5*HZ;
1404 tmp.closing_wait = 30*HZ;
1405
1406 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1407 return -EFAULT;
1408 return 0;
1409}
1410
Alan Cox95da3102008-07-22 11:09:07 +01001411static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001412 unsigned int cmd, unsigned long arg)
1413{
Alan Cox95da3102008-07-22 11:09:07 +01001414 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001415 struct moschip_port *mos7720_port;
1416 struct async_icount cnow;
1417 struct async_icount cprev;
1418 struct serial_icounter_struct icount;
1419
1420 mos7720_port = usb_get_serial_port_data(port);
1421 if (mos7720_port == NULL)
1422 return -ENODEV;
1423
Harvey Harrison441b62c2008-03-03 16:08:34 -08001424 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001425
1426 switch (cmd) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001427 case TIOCSERGETLSR:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001428 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
Alan Cox4da1a172008-07-22 11:16:21 +01001429 return get_lsr_info(tty, mos7720_port,
1430 (unsigned int __user *)arg);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001431 return 0;
1432
Alan Cox95da3102008-07-22 11:09:07 +01001433 /* FIXME: These should be using the mode methods */
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001434 case TIOCMBIS:
1435 case TIOCMBIC:
Alan Cox4da1a172008-07-22 11:16:21 +01001436 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
1437 __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001438 return set_modem_info(mos7720_port, cmd,
1439 (unsigned int __user *)arg);
1440
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001441 case TIOCGSERIAL:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001442 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001443 return get_serial_info(mos7720_port,
1444 (struct serial_struct __user *)arg);
1445
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001446 case TIOCMIWAIT:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001447 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001448 cprev = mos7720_port->icount;
1449 while (1) {
1450 if (signal_pending(current))
1451 return -ERESTARTSYS;
1452 cnow = mos7720_port->icount;
1453 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1454 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1455 return -EIO; /* no change => error */
1456 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1457 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1458 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
Alan Cox4da1a172008-07-22 11:16:21 +01001459 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001460 return 0;
1461 }
1462 cprev = cnow;
1463 }
1464 /* NOTREACHED */
1465 break;
1466
1467 case TIOCGICOUNT:
1468 cnow = mos7720_port->icount;
1469 icount.cts = cnow.cts;
1470 icount.dsr = cnow.dsr;
1471 icount.rng = cnow.rng;
1472 icount.dcd = cnow.dcd;
1473 icount.rx = cnow.rx;
1474 icount.tx = cnow.tx;
1475 icount.frame = cnow.frame;
1476 icount.overrun = cnow.overrun;
1477 icount.parity = cnow.parity;
1478 icount.brk = cnow.brk;
1479 icount.buf_overrun = cnow.buf_overrun;
1480
Harvey Harrison441b62c2008-03-03 16:08:34 -08001481 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
Alan Cox4da1a172008-07-22 11:16:21 +01001482 port->number, icount.rx, icount.tx);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001483 if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
1484 return -EFAULT;
1485 return 0;
1486 }
1487
1488 return -ENOIOCTLCMD;
1489}
1490
1491static int mos7720_startup(struct usb_serial *serial)
1492{
1493 struct moschip_serial *mos7720_serial;
1494 struct moschip_port *mos7720_port;
1495 struct usb_device *dev;
1496 int i;
1497 char data;
1498
Harvey Harrison441b62c2008-03-03 16:08:34 -08001499 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001500
1501 if (!serial) {
1502 dbg("Invalid Handler");
1503 return -ENODEV;
1504 }
1505
1506 dev = serial->dev;
1507
1508 /* create our private serial structure */
1509 mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
1510 if (mos7720_serial == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001511 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001512 return -ENOMEM;
1513 }
1514
1515 usb_set_serial_data(serial, mos7720_serial);
1516
1517 /* we set up the pointers to the endpoints in the mos7720_open *
1518 * function, as the structures aren't created yet. */
1519
1520 /* set up port private structures */
1521 for (i = 0; i < serial->num_ports; ++i) {
1522 mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1523 if (mos7720_port == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001524 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001525 usb_set_serial_data(serial, NULL);
1526 kfree(mos7720_serial);
1527 return -ENOMEM;
1528 }
1529
1530 /* Initialize all port interrupt end point to port 0 int
1531 * endpoint. Our device has only one interrupt endpoint
1532 * comman to all ports */
Alan Cox4da1a172008-07-22 11:16:21 +01001533 serial->port[i]->interrupt_in_endpointAddress =
1534 serial->port[0]->interrupt_in_endpointAddress;
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001535
1536 mos7720_port->port = serial->port[i];
1537 usb_set_serial_port_data(serial->port[i], mos7720_port);
1538
1539 dbg("port number is %d", serial->port[i]->number);
1540 dbg("serial number is %d", serial->minor);
1541 }
1542
1543
1544 /* setting configuration feature to one */
1545 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Cox4da1a172008-07-22 11:16:21 +01001546 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001547
Alan Cox4da1a172008-07-22 11:16:21 +01001548 /* LSR For Port 1 */
1549 send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
1550 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001551
Alan Cox4da1a172008-07-22 11:16:21 +01001552 /* LSR For Port 2 */
1553 send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
1554 dbg("LSR:%x", data);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001555
1556 return 0;
1557}
1558
Alan Sternf9c99bb2009-06-02 11:53:55 -04001559static void mos7720_release(struct usb_serial *serial)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001560{
1561 int i;
1562
1563 /* free private structure allocated for serial port */
Alan Sternf9c99bb2009-06-02 11:53:55 -04001564 for (i = 0; i < serial->num_ports; ++i)
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001565 kfree(usb_get_serial_port_data(serial->port[i]));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001566
1567 /* free private structure allocated for serial device */
1568 kfree(usb_get_serial_data(serial));
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001569}
1570
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001571static struct usb_driver usb_driver = {
1572 .name = "moschip7720",
1573 .probe = usb_serial_probe,
1574 .disconnect = usb_serial_disconnect,
1575 .id_table = moschip_port_id_table,
1576 .no_dynamic_id = 1,
1577};
1578
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001579static struct usb_serial_driver moschip7720_2port_driver = {
1580 .driver = {
1581 .owner = THIS_MODULE,
1582 .name = "moschip7720",
1583 },
1584 .description = "Moschip 2 port adapter",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001585 .usb_driver = &usb_driver,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001586 .id_table = moschip_port_id_table,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001587 .num_ports = 2,
1588 .open = mos7720_open,
1589 .close = mos7720_close,
1590 .throttle = mos7720_throttle,
1591 .unthrottle = mos7720_unthrottle,
1592 .attach = mos7720_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001593 .release = mos7720_release,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001594 .ioctl = mos7720_ioctl,
Kees Schoenmakers0f608f82009-09-19 13:13:18 -07001595 .tiocmget = mos7720_tiocmget,
1596 .tiocmset = mos7720_tiocmset,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001597 .set_termios = mos7720_set_termios,
1598 .write = mos7720_write,
1599 .write_room = mos7720_write_room,
1600 .chars_in_buffer = mos7720_chars_in_buffer,
1601 .break_ctl = mos7720_break,
1602 .read_bulk_callback = mos7720_bulk_in_callback,
Oliver Neukume8e30c72007-03-14 11:11:08 +01001603 .read_int_callback = mos7720_interrupt_callback,
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001604};
1605
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001606static int __init moschip7720_init(void)
1607{
1608 int retval;
1609
Harvey Harrison441b62c2008-03-03 16:08:34 -08001610 dbg("%s: Entering ..........", __func__);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001611
1612 /* Register with the usb serial */
1613 retval = usb_serial_register(&moschip7720_2port_driver);
1614 if (retval)
1615 goto failed_port_device_register;
1616
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001617 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1618 DRIVER_DESC "\n");
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001619
1620 /* Register with the usb */
1621 retval = usb_register(&usb_driver);
1622 if (retval)
1623 goto failed_usb_register;
1624
1625 return 0;
1626
1627failed_usb_register:
1628 usb_serial_deregister(&moschip7720_2port_driver);
1629
1630failed_port_device_register:
1631 return retval;
1632}
1633
1634static void __exit moschip7720_exit(void)
1635{
1636 usb_deregister(&usb_driver);
1637 usb_serial_deregister(&moschip7720_2port_driver);
1638}
1639
1640module_init(moschip7720_init);
1641module_exit(moschip7720_exit);
1642
1643/* Module information */
Alan Cox4da1a172008-07-22 11:16:21 +01001644MODULE_AUTHOR(DRIVER_AUTHOR);
1645MODULE_DESCRIPTION(DRIVER_DESC);
Greg Kroah-Hartman0f644782002-04-09 12:14:34 -07001646MODULE_LICENSE("GPL");
1647
1648module_param(debug, bool, S_IRUGO | S_IWUSR);
1649MODULE_PARM_DESC(debug, "Debug enabled or not");