blob: 6835ddcc4f2d5be29552a8d0a8e5199d1b0693ef [file] [log] [blame]
Hemant Kumar0cac8ed2012-01-31 14:39:23 -08001/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Steve Mucklef132c6c2012-06-06 18:30:57 -070013#include <linux/module.h>
Hemant Kumar0cac8ed2012-01-31 14:39:23 -080014#include <linux/slab.h>
15#include <linux/tty.h>
16#include <linux/serial.h>
17#include <linux/tty_flip.h>
18#include <linux/uaccess.h>
19#include <linux/usb.h>
20#include <linux/usb/ch9.h>
21#include <linux/usb/cdc.h>
22#include <linux/usb/serial.h>
23#include <asm/unaligned.h>
24
25
26/* output control lines*/
27#define CSVT_CTRL_DTR 0x01
28#define CSVT_CTRL_RTS 0x02
29
30/* input control lines*/
31#define CSVT_CTRL_CTS 0x01
32#define CSVT_CTRL_DSR 0x02
33#define CSVT_CTRL_RI 0x04
34#define CSVT_CTRL_CD 0x08
35
36static int debug;
37module_param(debug, int, S_IRUGO | S_IWUSR);
38
39struct csvt_ctrl_dev {
40 struct mutex dev_lock;
41
42 /* input control lines (DSR, CTS, CD, RI) */
43 unsigned int cbits_tolocal;
44
45 /* output control lines (DTR, RTS) */
46 unsigned int cbits_tomdm;
47};
48
49static const struct usb_device_id id_table[] = {
Hemant Kumar31aed6a2012-03-05 14:56:26 -080050 { USB_DEVICE_AND_INTERFACE_INFO(0x05c6 , 0x904c, 0xff, 0xfe, 0xff)},
Hemant Kumarce3c5bf2012-12-06 15:52:02 -080051 { USB_DEVICE_AND_INTERFACE_INFO(0x05c6 , 0x9075, 0xff, 0xfe, 0xff)},
Hemant Kumar0cac8ed2012-01-31 14:39:23 -080052 {}, /* terminating entry */
53};
54MODULE_DEVICE_TABLE(usb, id_table);
55
56static struct usb_driver csvt_driver = {
57 .name = "qc_csvt",
58 .probe = usb_serial_probe,
59 .disconnect = usb_serial_disconnect,
60 .id_table = id_table,
61 .suspend = usb_serial_suspend,
62 .resume = usb_serial_resume,
63 .supports_autosuspend = true,
64};
65
66#define CSVT_IFC_NUM 4
67
68static int csvt_probe(struct usb_serial *serial, const struct usb_device_id *id)
69{
70 struct usb_host_interface *intf =
71 serial->interface->cur_altsetting;
72
73 pr_debug("%s:\n", __func__);
74
75 if (intf->desc.bInterfaceNumber != CSVT_IFC_NUM)
76 return -ENODEV;
77
78 usb_enable_autosuspend(serial->dev);
79
80 return 0;
81}
82
83static int csvt_ctrl_write_cmd(struct csvt_ctrl_dev *dev,
84 struct usb_serial_port *port)
85{
86 struct usb_device *udev = port->serial->dev;
87 struct usb_interface *iface = port->serial->interface;
88 unsigned int iface_num;
89 int retval = 0;
90
91 retval = usb_autopm_get_interface(iface);
92 if (retval < 0) {
93 dev_err(&port->dev, "%s: Unable to resume interface: %d\n",
94 __func__, retval);
95 return retval;
96 }
97
98 dev_dbg(&port->dev, "%s: cbits to mdm 0x%x\n", __func__,
99 dev->cbits_tomdm);
100
101 iface_num = iface->cur_altsetting->desc.bInterfaceNumber;
102
103 retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
104 USB_CDC_REQ_SET_CONTROL_LINE_STATE,
105 (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE),
106 dev->cbits_tomdm,
107 iface_num,
108 NULL, 0, USB_CTRL_SET_TIMEOUT);
109 usb_autopm_put_interface(iface);
110
111 return retval;
112}
113
114static void csvt_ctrl_dtr_rts(struct usb_serial_port *port, int on)
115{
116 struct csvt_ctrl_dev *dev = usb_get_serial_port_data(port);
117
118 if (!dev)
119 return;
120
121 dev_dbg(&port->dev, "%s", __func__);
122
123 mutex_lock(&dev->dev_lock);
124 if (on) {
125 dev->cbits_tomdm |= CSVT_CTRL_DTR;
126 dev->cbits_tomdm |= CSVT_CTRL_RTS;
127 } else {
128 dev->cbits_tomdm &= ~CSVT_CTRL_DTR;
129 dev->cbits_tomdm &= ~CSVT_CTRL_RTS;
130 }
131 mutex_unlock(&dev->dev_lock);
132
133 csvt_ctrl_write_cmd(dev, port);
134}
135
136static int get_serial_info(struct usb_serial_port *port,
137 struct serial_struct __user *retinfo)
138{
139 struct serial_struct tmp;
140
141 if (!retinfo)
142 return -EFAULT;
143
144 memset(&tmp, 0, sizeof(tmp));
145 tmp.line = port->serial->minor;
146 tmp.port = port->number;
147 tmp.baud_base = tty_get_baud_rate(port->port.tty);
148 tmp.close_delay = port->port.close_delay / 10;
149 tmp.closing_wait =
150 port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
151 ASYNC_CLOSING_WAIT_NONE :
152 port->port.closing_wait / 10;
153
154 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
155 return -EFAULT;
156 return 0;
157}
158
159static int set_serial_info(struct usb_serial_port *port,
160 struct serial_struct __user *newinfo)
161{
162 struct serial_struct new_serial;
163 unsigned int closing_wait;
164 unsigned int close_delay;
165 int retval = 0;
166
167 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
168 return -EFAULT;
169
170 close_delay = new_serial.close_delay * 10;
171 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
172 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
173
174 mutex_lock(&port->port.mutex);
175
176 if (!capable(CAP_SYS_ADMIN)) {
177 if ((close_delay != port->port.close_delay) ||
178 (closing_wait != port->port.closing_wait))
179 retval = -EPERM;
180 else
181 retval = -EOPNOTSUPP;
182 } else {
183 port->port.close_delay = close_delay;
184 port->port.closing_wait = closing_wait;
185 }
186
187 mutex_unlock(&port->port.mutex);
188 return retval;
189}
190
191static int csvt_ctrl_ioctl(struct tty_struct *tty, unsigned int cmd,
192 unsigned long arg)
193{
194 struct usb_serial_port *port = tty->driver_data;
195
196 dev_dbg(&port->dev, "%s cmd 0x%04x", __func__, cmd);
197
198 switch (cmd) {
199 case TIOCGSERIAL:
200 return get_serial_info(port,
201 (struct serial_struct __user *) arg);
202 case TIOCSSERIAL:
203 return set_serial_info(port,
204 (struct serial_struct __user *) arg);
205 default:
206 break;
207 }
208
209 dev_err(&port->dev, "%s arg not supported", __func__);
210
211 return -ENOIOCTLCMD;
212}
213
214static int csvt_ctrl_tiocmget(struct tty_struct *tty)
215{
216 struct usb_serial_port *port = tty->driver_data;
217 struct csvt_ctrl_dev *dev = usb_get_serial_port_data(port);
218 unsigned int control_state = 0;
219
220 if (!dev)
221 return -ENODEV;
222
223 mutex_lock(&dev->dev_lock);
224 control_state = (dev->cbits_tomdm & CSVT_CTRL_DTR ? TIOCM_DTR : 0) |
225 (dev->cbits_tomdm & CSVT_CTRL_RTS ? TIOCM_RTS : 0) |
226 (dev->cbits_tolocal & CSVT_CTRL_DSR ? TIOCM_DSR : 0) |
227 (dev->cbits_tolocal & CSVT_CTRL_RI ? TIOCM_RI : 0) |
228 (dev->cbits_tolocal & CSVT_CTRL_CD ? TIOCM_CD : 0) |
229 (dev->cbits_tolocal & CSVT_CTRL_CTS ? TIOCM_CTS : 0);
230 mutex_unlock(&dev->dev_lock);
231
232 dev_dbg(&port->dev, "%s -- %x", __func__, control_state);
233
234 return control_state;
235}
236
237static int csvt_ctrl_tiocmset(struct tty_struct *tty,
238 unsigned int set, unsigned int clear)
239{
240 struct usb_serial_port *port = tty->driver_data;
241 struct csvt_ctrl_dev *dev = usb_get_serial_port_data(port);
242
243 if (!dev)
244 return -ENODEV;
245
246 dev_dbg(&port->dev, "%s\n", __func__);
247
248 mutex_lock(&dev->dev_lock);
249 if (set & CSVT_CTRL_DTR)
250 dev->cbits_tomdm |= TIOCM_DTR;
251 if (set & CSVT_CTRL_RTS)
252 dev->cbits_tomdm |= TIOCM_RTS;
253
254 if (clear & CSVT_CTRL_DTR)
255 dev->cbits_tomdm &= ~TIOCM_DTR;
256 if (clear & CSVT_CTRL_RTS)
257 dev->cbits_tomdm &= ~TIOCM_RTS;
258 mutex_unlock(&dev->dev_lock);
259
260 return csvt_ctrl_write_cmd(dev, port);
261}
262
263static void csvt_ctrl_set_termios(struct tty_struct *tty,
264 struct usb_serial_port *port,
265 struct ktermios *old_termios)
266{
267 struct csvt_ctrl_dev *dev = usb_get_serial_port_data(port);
268
269 if (!dev)
270 return;
271
272 dev_dbg(&port->dev, "%s", __func__);
273
274 /* Doesn't support option setting */
275 tty_termios_copy_hw(tty->termios, old_termios);
276
277 csvt_ctrl_write_cmd(dev, port);
278}
279
280static void csvt_ctrl_int_cb(struct urb *urb)
281{
282 int status;
283 struct usb_cdc_notification *ctrl;
284 struct usb_serial_port *port = urb->context;
285 struct csvt_ctrl_dev *dev;
286 unsigned int ctrl_bits;
287 unsigned char *data;
288
289 switch (urb->status) {
290 case 0:
291 /*success*/
292 break;
293 case -ESHUTDOWN:
294 case -ENOENT:
295 case -ECONNRESET:
296 case -EPROTO:
297 /* unplug */
298 return;
299 case -EPIPE:
300 dev_err(&port->dev, "%s: stall on int endpoint\n", __func__);
301 /* TBD : halt to be cleared in work */
302 case -EOVERFLOW:
303 default:
304 pr_debug_ratelimited("%s: non zero urb status = %d\n",
305 __func__, urb->status);
306 goto resubmit_int_urb;
307 }
308
309 dev = usb_get_serial_port_data(port);
310 if (!dev)
311 return;
312
313 ctrl = urb->transfer_buffer;
314 data = (unsigned char *)(ctrl + 1);
315
316 usb_serial_debug_data(debug, &port->dev, __func__,
317 urb->actual_length, data);
318
319 switch (ctrl->bNotificationType) {
320 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
321 dev_dbg(&port->dev, "%s network\n", ctrl->wValue ?
322 "connected to" : "disconnected from");
323 break;
324 case USB_CDC_NOTIFY_SERIAL_STATE:
325 ctrl_bits = get_unaligned_le16(data);
326 dev_dbg(&port->dev, "serial state: %d\n", ctrl_bits);
327 dev->cbits_tolocal = ctrl_bits;
328 break;
329 default:
330 dev_err(&port->dev, "%s: unknown notification %d received:"
331 "index %d len %d data0 %d data1 %d",
332 __func__, ctrl->bNotificationType, ctrl->wIndex,
333 ctrl->wLength, data[0], data[1]);
334 }
335
336resubmit_int_urb:
337 status = usb_submit_urb(urb, GFP_ATOMIC);
338 if (status)
339 dev_err(&port->dev, "%s: Error re-submitting Int URB %d\n",
340 __func__, status);
341
342}
343
344static int csvt_ctrl_open(struct tty_struct *tty,
345 struct usb_serial_port *port)
346{
347 int retval;
348
349 dev_dbg(&port->dev, "%s port %d", __func__, port->number);
350
351 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
352 if (retval) {
353 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
354 return retval;
355 }
356
357 retval = usb_serial_generic_open(tty, port);
358 if (retval)
359 usb_kill_urb(port->interrupt_in_urb);
360
361 return retval;
362}
363
364static void csvt_ctrl_close(struct usb_serial_port *port)
365{
366 dev_dbg(&port->dev, "%s port %d", __func__, port->number);
367
368 usb_serial_generic_close(port);
369 usb_kill_urb(port->interrupt_in_urb);
370}
371
372static int csvt_ctrl_attach(struct usb_serial *serial)
373{
374 struct csvt_ctrl_dev *dev;
375
376 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
377 if (!dev)
378 return -ENOMEM;
379
380 mutex_init(&dev->dev_lock);
381 usb_set_serial_port_data(serial->port[0], dev);
382
383 return 0;
384}
385
386static void csvt_ctrl_release(struct usb_serial *serial)
387{
388 struct usb_serial_port *port = serial->port[0];
389 struct csvt_ctrl_dev *dev = usb_get_serial_port_data(port);
390
391 dev_dbg(&port->dev, "%s", __func__);
392
393 kfree(dev);
394 usb_set_serial_port_data(port, NULL);
395}
396
397static struct usb_serial_driver csvt_device = {
398 .driver = {
399 .owner = THIS_MODULE,
400 .name = "qc_csvt",
401 },
402 .description = "qc_csvt",
403 .id_table = id_table,
Hemant Kumar0cac8ed2012-01-31 14:39:23 -0800404 .num_ports = 1,
405 .open = csvt_ctrl_open,
406 .close = csvt_ctrl_close,
407 .probe = csvt_probe,
408 .dtr_rts = csvt_ctrl_dtr_rts,
409 .tiocmget = csvt_ctrl_tiocmget,
410 .tiocmset = csvt_ctrl_tiocmset,
411 .ioctl = csvt_ctrl_ioctl,
412 .set_termios = csvt_ctrl_set_termios,
413 .read_int_callback = csvt_ctrl_int_cb,
414 .attach = csvt_ctrl_attach,
415 .release = csvt_ctrl_release,
416};
417
Steve Mucklef132c6c2012-06-06 18:30:57 -0700418static struct usb_serial_driver * const serial_drivers[] = {
419 &csvt_device,
420 NULL,
421};
422
Hemant Kumar0cac8ed2012-01-31 14:39:23 -0800423static int __init csvt_init(void)
424{
425 int retval;
426
Steve Mucklef132c6c2012-06-06 18:30:57 -0700427 retval = usb_serial_register_drivers(&csvt_driver, serial_drivers);
Hemant Kumar0cac8ed2012-01-31 14:39:23 -0800428 if (retval) {
429 err("%s: usb serial register failed\n", __func__);
430 return retval;
431 }
432
Hemant Kumar0cac8ed2012-01-31 14:39:23 -0800433 return 0;
434}
435
436static void __exit csvt_exit(void)
437{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700438 usb_serial_deregister_drivers(&csvt_driver, serial_drivers);
Hemant Kumar0cac8ed2012-01-31 14:39:23 -0800439}
440
441module_init(csvt_init);
442module_exit(csvt_exit);
443
444MODULE_LICENSE("GPL v2");