1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
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 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16 /*
17 *
18 * This driver will work with any USB UART function in these Exar devices:
19 * XR21V1410/1412/1414
20 * XR21B1411
21 * XR21B1420/1422/1424
22 * XR22801/802/804
23 *
24 * The driver has been tested on various kernel versions from 3.6.x to 5.11.x.
25 * This driver may work on newer versions as well. There is a different driver available
26 * from www.exar.com that will work with kernel versions 2.6.18 to 3.4.x.
27 *
28 * ChangeLog:
29 * Version 1B - Initial released version.
30 * Version 1C - Add 9-bit mode support
31 * Version 1D - GPIO support & Fixes. Check Readme for details.
32 */
33
34 //#undef DEBUG
35 #undef VERBOSE_DEBUG
36
37 #include <linux/kernel.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/tty.h>
42 #include <linux/serial.h>
43 #include <linux/tty_driver.h>
44 #include <linux/tty_flip.h>
45 #include <linux/module.h>
46 #include <linux/mutex.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/usb/cdc.h>
50 #include <asm/byteorder.h>
51 #include <asm/unaligned.h>
52 #include <linux/list.h>
53 #include "linux/version.h"
54 #include <asm/io.h>
55 #include <linux/gpio.h>
56 #include <linux/idr.h>
57 #include <linux/delay.h>
58
59 #ifdef CONFIG_COMPAT
60 #include <linux/compat.h>
61 #endif
62
63 #include "xr_usb_serial_common.h"
64 #include "xr_usb_serial_ioctl.h"
65
66 #define DRIVER_AUTHOR "<uarttechsupport@exar.com>"
67 #define DRIVER_DESC "Exar/MxL USB UART (serial port) driver version 1D"
68
69 static struct usb_driver xr_usb_serial_driver;
70 static struct tty_driver *xr_usb_serial_tty_driver;
71 static struct xr_usb_serial *xr_usb_serial_table[XR_USB_SERIAL_TTY_MINORS];
72
73 static DEFINE_MUTEX(xr_usb_serial_table_lock);
74
75 /*
76 * xr_usb_serial_table accessors
77 */
78
79 /*
80 * Look up an XR_USB_SERIAL structure by index. If found and not disconnected, increment
81 * its refcount and return it with its mutex held.
82 */
xr_usb_serial_get_by_index(unsigned index)83 static struct xr_usb_serial *xr_usb_serial_get_by_index(unsigned index)
84 {
85 struct xr_usb_serial *xr_usb_serial;
86
87 mutex_lock(&xr_usb_serial_table_lock);
88 xr_usb_serial = xr_usb_serial_table[index];
89 if (xr_usb_serial) {
90 mutex_lock(&xr_usb_serial->mutex);
91 if (xr_usb_serial->disconnected) {
92 mutex_unlock(&xr_usb_serial->mutex);
93 xr_usb_serial = NULL;
94 } else {
95 tty_port_get(&xr_usb_serial->port);
96 mutex_unlock(&xr_usb_serial->mutex);
97 }
98 }
99 mutex_unlock(&xr_usb_serial_table_lock);
100 return xr_usb_serial;
101 }
102
103 /*
104 * Try to find an available minor number and if found, associate it with 'xr_usb_serial'.
105 */
xr_usb_serial_alloc_minor(struct xr_usb_serial * xr_usb_serial)106 static int xr_usb_serial_alloc_minor(struct xr_usb_serial *xr_usb_serial)
107 {
108 int minor;
109
110 mutex_lock(&xr_usb_serial_table_lock);
111 for (minor = 0; minor < XR_USB_SERIAL_TTY_MINORS; minor++) {
112 if (!xr_usb_serial_table[minor]) {
113 xr_usb_serial_table[minor] = xr_usb_serial;
114 break;
115 }
116 }
117 mutex_unlock(&xr_usb_serial_table_lock);
118
119 return minor;
120 }
121
122 /* Release the minor number associated with 'xr_usb_serial'. */
xr_usb_serial_release_minor(struct xr_usb_serial * xr_usb_serial)123 static void xr_usb_serial_release_minor(struct xr_usb_serial *xr_usb_serial)
124 {
125 mutex_lock(&xr_usb_serial_table_lock);
126 xr_usb_serial_table[xr_usb_serial->minor] = NULL;
127 mutex_unlock(&xr_usb_serial_table_lock);
128 }
129
130 /*
131 * Functions for XR_USB_SERIAL control messages.
132 */
133
xr_usb_serial_ctrl_msg(struct xr_usb_serial * xr_usb_serial,int request,int value,void * buf,int len)134 static int xr_usb_serial_ctrl_msg(struct xr_usb_serial *xr_usb_serial, int request, int value,
135 void *buf, int len)
136 {
137 int retval = usb_control_msg(xr_usb_serial->dev, usb_sndctrlpipe(xr_usb_serial->dev, 0),
138 request, USB_RT_XR_USB_SERIAL, value,
139 xr_usb_serial->control->altsetting[0].desc.bInterfaceNumber,
140 buf, len, 5000);
141 dev_dbg(&xr_usb_serial->control->dev,
142 "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
143 __func__, request, value, len, retval);
144 return retval < 0 ? retval : 0;
145 }
146
147 #include "xr_usb_serial_hal.c"
148
149 /*
150 * Write buffer management.
151 * All of these assume proper locks taken by the caller.
152 */
153
xr_usb_serial_wb_alloc(struct xr_usb_serial * xr_usb_serial)154 static int xr_usb_serial_wb_alloc(struct xr_usb_serial *xr_usb_serial)
155 {
156 int i, wbn;
157 struct xr_usb_serial_wb *wb;
158
159 wbn = 0;
160 i = 0;
161 for (;;) {
162 wb = &xr_usb_serial->wb[wbn];
163 if (!wb->use) {
164 wb->use = 1;
165 return wbn;
166 }
167 wbn = (wbn + 1) % XR_USB_SERIAL_NW;
168 if (++i >= XR_USB_SERIAL_NW)
169 return -1;
170 }
171 }
172
xr_usb_serial_wb_is_avail(struct xr_usb_serial * xr_usb_serial)173 static int xr_usb_serial_wb_is_avail(struct xr_usb_serial *xr_usb_serial)
174 {
175 int i, n;
176 unsigned long flags;
177
178 n = XR_USB_SERIAL_NW;
179 spin_lock_irqsave(&xr_usb_serial->write_lock, flags);
180 for (i = 0; i < XR_USB_SERIAL_NW; i++)
181 n -= xr_usb_serial->wb[i].use;
182 spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
183 return n;
184 }
185
186 /*
187 * Finish write. Caller must hold xr_usb_serial->write_lock
188 */
xr_usb_serial_write_done(struct xr_usb_serial * xr_usb_serial,struct xr_usb_serial_wb * wb)189 static void xr_usb_serial_write_done(struct xr_usb_serial *xr_usb_serial, struct xr_usb_serial_wb *wb)
190 {
191 wb->use = 0;
192 xr_usb_serial->transmitting--;
193 usb_autopm_put_interface_async(xr_usb_serial->control);
194 }
195
196 /*
197 * Poke write.
198 *
199 * the caller is responsible for locking
200 */
201
xr_usb_serial_start_wb(struct xr_usb_serial * xr_usb_serial,struct xr_usb_serial_wb * wb)202 static int xr_usb_serial_start_wb(struct xr_usb_serial *xr_usb_serial, struct xr_usb_serial_wb *wb)
203 {
204 int rc;
205
206 xr_usb_serial->transmitting++;
207
208 wb->urb->transfer_buffer = wb->buf;
209 wb->urb->transfer_dma = wb->dmah;
210 wb->urb->transfer_buffer_length = wb->len;
211 wb->urb->dev = xr_usb_serial->dev;
212
213 rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
214 if (rc < 0) {
215 dev_err(&xr_usb_serial->data->dev,
216 "%s - usb_submit_urb(write bulk) failed: %d\n",
217 __func__, rc);
218 xr_usb_serial_write_done(xr_usb_serial, wb);
219 }
220 return rc;
221 }
222
223 /*
224 * attributes exported through sysfs
225 */
show_caps(struct device * dev,struct device_attribute * attr,char * buf)226 static ssize_t show_caps
227 (struct device *dev, struct device_attribute *attr, char *buf)
228 {
229 struct usb_interface *intf = to_usb_interface(dev);
230 struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf);
231
232 return sprintf(buf, "%d", xr_usb_serial->ctrl_caps);
233 }
234 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
235
show_country_codes(struct device * dev,struct device_attribute * attr,char * buf)236 static ssize_t show_country_codes
237 (struct device *dev, struct device_attribute *attr, char *buf)
238 {
239 struct usb_interface *intf = to_usb_interface(dev);
240 struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf);
241
242 memcpy(buf, xr_usb_serial->country_codes, xr_usb_serial->country_code_size);
243 return xr_usb_serial->country_code_size;
244 }
245
246 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
247
show_country_rel_date(struct device * dev,struct device_attribute * attr,char * buf)248 static ssize_t show_country_rel_date
249 (struct device *dev, struct device_attribute *attr, char *buf)
250 {
251 struct usb_interface *intf = to_usb_interface(dev);
252 struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf);
253
254 return sprintf(buf, "%d", xr_usb_serial->country_rel_date);
255 }
256
257 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
258 /*
259 * Interrupt handlers for various XR_USB_SERIAL device responses
260 */
261
262 /* control interface reports status changes with "interrupt" transfers */
xr_usb_serial_ctrl_irq(struct urb * urb)263 static void xr_usb_serial_ctrl_irq(struct urb *urb)
264 {
265 struct xr_usb_serial *xr_usb_serial = urb->context;
266 struct usb_cdc_notification *dr = urb->transfer_buffer;
267 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
268 #else
269 struct tty_struct *tty;
270 #endif
271 unsigned char *data;
272 int newctrl;
273 int retval;
274 int status = urb->status;
275 int i;
276 unsigned char *p;
277
278 switch (status) {
279 case 0:
280 p = (unsigned char *)(urb->transfer_buffer);
281 for(i=0;i<urb->actual_length;i++)
282 {
283 dev_dbg(&xr_usb_serial->control->dev,"0x%02x\n",p[i]);
284 }
285 /* success */
286 break;
287 case -ECONNRESET:
288 case -ENOENT:
289 case -ESHUTDOWN:
290 /* this urb is terminated, clean up */
291 dev_dbg(&xr_usb_serial->control->dev,
292 "%s - urb shutting down with status: %d\n",
293 __func__, status);
294 return;
295 default:
296 dev_dbg(&xr_usb_serial->control->dev,
297 "%s - nonzero urb status received: %d\n",
298 __func__, status);
299 goto exit;
300 }
301
302 usb_mark_last_busy(xr_usb_serial->dev);
303
304 data = (unsigned char *)(dr + 1);
305 switch (dr->bNotificationType) {
306 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
307 dev_dbg(&xr_usb_serial->control->dev, "%s - network connection: %d\n",
308 __func__, dr->wValue);
309 break;
310
311 case USB_CDC_NOTIFY_SERIAL_STATE:
312 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
313 newctrl = get_unaligned_le16(data);
314 if (!xr_usb_serial->clocal && (xr_usb_serial->ctrlin & ~newctrl & XR_USB_SERIAL_CTRL_DCD)) {
315 dev_dbg(&xr_usb_serial->control->dev, "%s - calling hangup\n",
316 __func__);
317 tty_port_tty_hangup(&xr_usb_serial->port, false);
318 }
319 #else
320 tty = tty_port_tty_get(&xr_usb_serial->port);
321 newctrl = get_unaligned_le16(data);
322 if (tty)
323 {
324 if (!xr_usb_serial->clocal &&
325 (xr_usb_serial->ctrlin & ~newctrl & XR_USB_SERIAL_CTRL_DCD)) {
326 dev_dbg(&xr_usb_serial->control->dev,
327 "%s - calling hangup\n", __func__);
328 tty_hangup(tty);
329 }
330 tty_kref_put(tty);
331 }
332 #endif
333 xr_usb_serial->ctrlin = newctrl;
334
335 dev_dbg(&xr_usb_serial->control->dev,
336 "%s - input control lines: dcd%c dsr%c break%c "
337 "ring%c framing%c parity%c overrun%c\n",
338 __func__,
339 xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_DCD ? '+' : '-',
340 xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_DSR ? '+' : '-',
341 xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_BRK ? '+' : '-',
342 xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_RI ? '+' : '-',
343 xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_FRAMING ? '+' : '-',
344 xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_PARITY ? '+' : '-',
345 xr_usb_serial->ctrlin & XR_USB_SERIAL_CTRL_OVERRUN ? '+' : '-');
346 break;
347
348 default:
349 dev_dbg(&xr_usb_serial->control->dev,
350 "%s - unknown notification %d received: index %d "
351 "len %d data0 %d data1 %d\n",
352 __func__,
353 dr->bNotificationType, dr->wIndex,
354 dr->wLength, data[0], data[1]);
355 break;
356 }
357 exit:
358 retval = usb_submit_urb(urb, GFP_ATOMIC);
359 if (retval)
360 dev_err(&xr_usb_serial->control->dev, "%s - usb_submit_urb failed: %d\n",
361 __func__, retval);
362 }
363
xr_usb_serial_submit_read_urb(struct xr_usb_serial * xr_usb_serial,int index,gfp_t mem_flags)364 static int xr_usb_serial_submit_read_urb(struct xr_usb_serial *xr_usb_serial, int index, gfp_t mem_flags)
365 {
366 int res;
367
368 if (!test_and_clear_bit(index, &xr_usb_serial->read_urbs_free))
369 return 0;
370
371 dev_vdbg(&xr_usb_serial->data->dev, "%s - urb %d\n", __func__, index);
372
373 res = usb_submit_urb(xr_usb_serial->read_urbs[index], mem_flags);
374 if (res) {
375 if (res != -EPERM) {
376 dev_err(&xr_usb_serial->data->dev,
377 "%s - usb_submit_urb failed: %d\n",
378 __func__, res);
379 }
380 set_bit(index, &xr_usb_serial->read_urbs_free);
381 return res;
382 }
383
384 return 0;
385 }
386
xr_usb_serial_submit_read_urbs(struct xr_usb_serial * xr_usb_serial,gfp_t mem_flags)387 static int xr_usb_serial_submit_read_urbs(struct xr_usb_serial *xr_usb_serial, gfp_t mem_flags)
388 {
389 int res;
390 int i;
391
392 for (i = 0; i < xr_usb_serial->rx_buflimit; ++i) {
393 res = xr_usb_serial_submit_read_urb(xr_usb_serial, i, mem_flags);
394 if (res)
395 return res;
396 }
397
398 return 0;
399 }
xr_usb_serial_process_read_urb(struct xr_usb_serial * xr_usb_serial,struct urb * urb)400 static void xr_usb_serial_process_read_urb(struct xr_usb_serial *xr_usb_serial, struct urb *urb)
401 {
402 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
403 #else
404 struct tty_struct *tty;
405 #endif
406 int preciseflags = xr_usb_serial->preciseflags;
407 int have_extra_byte;
408 int length;
409
410 if (!urb->actual_length)
411 return;
412
413 if (preciseflags)
414 {
415 char *dp = urb->transfer_buffer;
416 int i, ch, ch_flags;
417
418 length = urb->actual_length;
419 length = length + (xr_usb_serial->have_extra_byte ? 1 : 0);
420 have_extra_byte = (preciseflags && (length & 1));
421 length = (preciseflags) ? (length / 2) : length;
422 for (i = 0; i < length; ++i)
423 {
424 char tty_flag;
425 if (i == 0)
426 {
427 if (xr_usb_serial->have_extra_byte)
428 {
429 ch = xr_usb_serial->extra_byte;
430 }
431 else
432 {
433 ch = *dp++;
434 }
435 }
436 else
437 {
438 ch = *dp++;
439 }
440 ch_flags = *dp++;
441 if (ch_flags & RAMCTL_BUFFER_PARITY)
442 tty_flag = TTY_PARITY;
443 else if (ch_flags & RAMCTL_BUFFER_BREAK)
444 tty_flag = TTY_BREAK;
445 else if (ch_flags & RAMCTL_BUFFER_FRAME)
446 tty_flag = TTY_FRAME;
447 else if (ch_flags & RAMCTL_BUFFER_OVERRUN)
448 tty_flag = TTY_OVERRUN;
449 else
450 tty_flag = TTY_NORMAL;
451
452 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
453 tty_insert_flip_char(&xr_usb_serial->port, ch, tty_flag);
454 tty_flip_buffer_push(&xr_usb_serial->port);
455 #else
456 tty = tty_port_tty_get(&xr_usb_serial->port);
457 if (!tty)
458 return;
459 tty_insert_flip_char(&xr_usb_serial->port, ch, tty_flag);
460 tty_flip_buffer_push(tty);
461 tty_kref_put(tty);
462 #endif
463 }
464 }
465 else
466 {
467 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
468 tty_insert_flip_string(&xr_usb_serial->port, urb->transfer_buffer,
469 urb->actual_length);
470 tty_flip_buffer_push(&xr_usb_serial->port);
471 #else
472 tty = tty_port_tty_get(&xr_usb_serial->port);
473 if (!tty)
474 return;
475 tty_insert_flip_string(tty, urb->transfer_buffer, urb->actual_length);
476 tty_flip_buffer_push(tty);
477 tty_kref_put(tty);
478 #endif
479 }
480 }
481
xr_usb_serial_read_bulk_callback(struct urb * urb)482 static void xr_usb_serial_read_bulk_callback(struct urb *urb)
483 {
484 struct xr_usb_serial_rb *rb = urb->context;
485 struct xr_usb_serial *xr_usb_serial = rb->instance;
486 unsigned long flags;
487
488 dev_vdbg(&xr_usb_serial->data->dev, "%s - urb %d, len %d\n", __func__,
489 rb->index, urb->actual_length);
490 set_bit(rb->index, &xr_usb_serial->read_urbs_free);
491
492 if (!xr_usb_serial->dev) {
493 dev_dbg(&xr_usb_serial->data->dev, "%s - disconnected\n", __func__);
494 return;
495 }
496 usb_mark_last_busy(xr_usb_serial->dev);
497
498 if (urb->status) {
499 dev_dbg(&xr_usb_serial->data->dev, "%s - non-zero urb status: %d\n",
500 __func__, urb->status);
501 //return;
502 }
503 xr_usb_serial_process_read_urb(xr_usb_serial, urb);
504
505 /* throttle device if requested by tty */
506 spin_lock_irqsave(&xr_usb_serial->read_lock, flags);
507 xr_usb_serial->throttled = xr_usb_serial->throttle_req;
508 if (!xr_usb_serial->throttled && !xr_usb_serial->susp_count) {
509 spin_unlock_irqrestore(&xr_usb_serial->read_lock, flags);
510 xr_usb_serial_submit_read_urb(xr_usb_serial, rb->index, GFP_ATOMIC);
511 } else {
512 spin_unlock_irqrestore(&xr_usb_serial->read_lock, flags);
513 }
514 }
515
516 /* data interface wrote those outgoing bytes */
xr_usb_serial_write_bulk(struct urb * urb)517 static void xr_usb_serial_write_bulk(struct urb *urb)
518 {
519 struct xr_usb_serial_wb *wb = urb->context;
520 struct xr_usb_serial *xr_usb_serial = wb->instance;
521 unsigned long flags;
522
523 if (urb->status || (urb->actual_length != urb->transfer_buffer_length))
524 dev_vdbg(&xr_usb_serial->data->dev, "%s - len %d/%d, status %d\n",
525 __func__,
526 urb->actual_length,
527 urb->transfer_buffer_length,
528 urb->status);
529
530 spin_lock_irqsave(&xr_usb_serial->write_lock, flags);
531 xr_usb_serial_write_done(xr_usb_serial, wb);
532 spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
533 schedule_work(&xr_usb_serial->work);
534 }
535
xr_usb_serial_softint(struct work_struct * work)536 static void xr_usb_serial_softint(struct work_struct *work)
537 {
538 struct xr_usb_serial *xr_usb_serial = container_of(work, struct xr_usb_serial, work);
539 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
540 #else
541 struct tty_struct *tty;
542 #endif
543
544 dev_vdbg(&xr_usb_serial->data->dev, "%s\n", __func__);
545 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
546 tty_port_tty_wakeup(&xr_usb_serial->port);
547 #else
548 tty = tty_port_tty_get(&xr_usb_serial->port);
549 if (!tty)
550 return;
551 tty_wakeup(tty);
552 tty_kref_put(tty);
553 #endif
554 }
555
556 /*
557 * TTY handlers
558 */
559
xr_usb_serial_tty_install(struct tty_driver * driver,struct tty_struct * tty)560 static int xr_usb_serial_tty_install(struct tty_driver *driver, struct tty_struct *tty)
561 {
562 struct xr_usb_serial *xr_usb_serial;
563 int retval;
564
565 dev_dbg(tty->dev, "%s\n", __func__);
566
567 xr_usb_serial = xr_usb_serial_get_by_index(tty->index);
568 if (!xr_usb_serial)
569 return -ENODEV;
570
571 retval = tty_standard_install(driver, tty);
572 if (retval)
573 goto error_init_termios;
574
575 tty->driver_data = xr_usb_serial;
576
577 return 0;
578
579 error_init_termios:
580 tty_port_put(&xr_usb_serial->port);
581 return retval;
582 }
583
xr_usb_serial_tty_open(struct tty_struct * tty,struct file * filp)584 static int xr_usb_serial_tty_open(struct tty_struct *tty, struct file *filp)
585 {
586 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
587 int result;
588
589 result = xr_usb_serial_fifo_reset(xr_usb_serial);
590 dev_dbg(tty->dev, "%s\n", __func__);
591
592 return tty_port_open(&xr_usb_serial->port, tty, filp);
593 }
594
xr_usb_serial_port_activate(struct tty_port * port,struct tty_struct * tty)595 static int xr_usb_serial_port_activate(struct tty_port *port, struct tty_struct *tty)
596 {
597 struct xr_usb_serial *xr_usb_serial = container_of(port, struct xr_usb_serial, port);
598 int retval = -ENODEV;
599
600 dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
601
602 mutex_lock(&xr_usb_serial->mutex);
603 if (xr_usb_serial->disconnected)
604 goto disconnected;
605
606 retval = usb_autopm_get_interface(xr_usb_serial->control);
607 if (retval)
608 goto error_get_interface;
609
610 /*
611 * FIXME: Why do we need this? Allocating 64K of physically contiguous
612 * memory is really nasty...
613 */
614 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
615 xr_usb_serial->control->needs_remote_wakeup = 1;
616
617 xr_usb_serial->ctrlurb->dev = xr_usb_serial->dev;
618 if (usb_submit_urb(xr_usb_serial->ctrlurb, GFP_KERNEL)) {
619 dev_err(&xr_usb_serial->control->dev,
620 "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
621 goto error_submit_urb;
622 }
623
624 xr_usb_serial->ctrlout = XR_USB_SERIAL_CTRL_DTR | XR_USB_SERIAL_CTRL_RTS;
625 if (xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout) < 0 &&
626 (xr_usb_serial->ctrl_caps & USB_CDC_CAP_LINE))
627 goto error_set_control;
628
629 usb_autopm_put_interface(xr_usb_serial->control);
630
631 /*
632 * Unthrottle device in case the TTY was closed while throttled.
633 */
634 spin_lock_irq(&xr_usb_serial->read_lock);
635 xr_usb_serial->throttled = 0;
636 xr_usb_serial->throttle_req = 0;
637 spin_unlock_irq(&xr_usb_serial->read_lock);
638
639 if (xr_usb_serial_submit_read_urbs(xr_usb_serial, GFP_KERNEL))
640 goto error_submit_read_urbs;
641
642 mutex_unlock(&xr_usb_serial->mutex);
643
644 return 0;
645
646 error_submit_read_urbs:
647 xr_usb_serial->ctrlout = 0;
648 xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout);
649 error_set_control:
650 usb_kill_urb(xr_usb_serial->ctrlurb);
651 error_submit_urb:
652 usb_autopm_put_interface(xr_usb_serial->control);
653 error_get_interface:
654 disconnected:
655 mutex_unlock(&xr_usb_serial->mutex);
656 return retval;
657 }
658
xr_usb_serial_port_destruct(struct tty_port * port)659 static void xr_usb_serial_port_destruct(struct tty_port *port)
660 {
661 struct xr_usb_serial *xr_usb_serial = container_of(port, struct xr_usb_serial, port);
662
663 dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
664 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
665 tty_unregister_device(xr_usb_serial_tty_driver, xr_usb_serial->minor);
666 #endif
667 #ifdef CONFIG_GPIOLIBA
668 if (xr_usb_serial->rv_gpio_created == 0)
669 gpiochip_remove(&xr_usb_serial->xr_gpio);
670 #endif
671 xr_usb_serial_release_minor(xr_usb_serial);
672 usb_put_intf(xr_usb_serial->control);
673 kfree(xr_usb_serial->country_codes);
674 kfree(xr_usb_serial);
675 }
676
xr_usb_serial_port_shutdown(struct tty_port * port)677 static void xr_usb_serial_port_shutdown(struct tty_port *port)
678 {
679 struct xr_usb_serial *xr_usb_serial = container_of(port, struct xr_usb_serial, port);
680 int i;
681
682 dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
683
684 mutex_lock(&xr_usb_serial->mutex);
685 if (!xr_usb_serial->disconnected) {
686 usb_autopm_get_interface(xr_usb_serial->control);
687 xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout = 0);
688 usb_kill_urb(xr_usb_serial->ctrlurb);
689 for (i = 0; i < XR_USB_SERIAL_NW; i++)
690 usb_kill_urb(xr_usb_serial->wb[i].urb);
691 for (i = 0; i < xr_usb_serial->rx_buflimit; i++)
692 usb_kill_urb(xr_usb_serial->read_urbs[i]);
693 xr_usb_serial->control->needs_remote_wakeup = 0;
694 usb_autopm_put_interface(xr_usb_serial->control);
695 }
696 mutex_unlock(&xr_usb_serial->mutex);
697 }
698
xr_usb_serial_tty_cleanup(struct tty_struct * tty)699 static void xr_usb_serial_tty_cleanup(struct tty_struct *tty)
700 {
701 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
702 dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
703 tty_port_put(&xr_usb_serial->port);
704 }
705
xr_usb_serial_tty_hangup(struct tty_struct * tty)706 static void xr_usb_serial_tty_hangup(struct tty_struct *tty)
707 {
708 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
709 dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
710 tty_port_hangup(&xr_usb_serial->port);
711 }
712
xr_usb_serial_tty_close(struct tty_struct * tty,struct file * filp)713 static void xr_usb_serial_tty_close(struct tty_struct *tty, struct file *filp)
714 {
715 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
716 dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
717 tty_port_close(&xr_usb_serial->port, tty, filp);
718 }
719
xr_usb_serial_tty_write(struct tty_struct * tty,const unsigned char * buf,int count)720 static int xr_usb_serial_tty_write(struct tty_struct *tty,
721 const unsigned char *buf, int count)
722 {
723 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
724 int stat;
725 unsigned long flags;
726 int wbn;
727 struct xr_usb_serial_wb *wb;
728
729 if (!count)
730 return 0;
731
732 //dev_vdbg(&xr_usb_serial->data->dev, "%s - count %d\n", __func__, count);
733
734 spin_lock_irqsave(&xr_usb_serial->write_lock, flags);
735 wbn = xr_usb_serial_wb_alloc(xr_usb_serial);
736 if (wbn < 0) {
737 spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
738 return 0;
739 }
740 wb = &xr_usb_serial->wb[wbn];
741
742 if (!xr_usb_serial->dev) {
743 wb->use = 0;
744 spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
745 return -ENODEV;
746 }
747
748 count = (count > xr_usb_serial->writesize) ? xr_usb_serial->writesize : count;
749 //dev_vdbg(&xr_usb_serial->data->dev, "%s - write %d\n", __func__, count);
750 memcpy(wb->buf, buf, count);
751 wb->len = count;
752
753 usb_autopm_get_interface_async(xr_usb_serial->control);
754 if (xr_usb_serial->susp_count) {
755 if (!xr_usb_serial->delayed_wb)
756 xr_usb_serial->delayed_wb = wb;
757 else
758 usb_autopm_put_interface_async(xr_usb_serial->control);
759 spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
760 return count; /* A white lie */
761 }
762 usb_mark_last_busy(xr_usb_serial->dev);
763
764 stat = xr_usb_serial_start_wb(xr_usb_serial, wb);
765 spin_unlock_irqrestore(&xr_usb_serial->write_lock, flags);
766
767 if (stat < 0)
768 return stat;
769 return count;
770 }
771
xr_usb_serial_tty_write_room(struct tty_struct * tty)772 static int xr_usb_serial_tty_write_room(struct tty_struct *tty)
773 {
774 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
775 /*
776 * Do not let the line discipline to know that we have a reserve,
777 * or it might get too enthusiastic.
778 */
779 return xr_usb_serial_wb_is_avail(xr_usb_serial) ? xr_usb_serial->writesize : 0;
780 }
781
xr_usb_serial_tty_chars_in_buffer(struct tty_struct * tty)782 static int xr_usb_serial_tty_chars_in_buffer(struct tty_struct *tty)
783 {
784 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
785 /*
786 * if the device was unplugged then any remaining characters fell out
787 * of the connector ;)
788 */
789 if (xr_usb_serial->disconnected)
790 return 0;
791 /*
792 * This is inaccurate (overcounts), but it works.
793 */
794 return (XR_USB_SERIAL_NW - xr_usb_serial_wb_is_avail(xr_usb_serial)) * xr_usb_serial->writesize;
795 }
796
xr_usb_serial_tty_throttle(struct tty_struct * tty)797 static void xr_usb_serial_tty_throttle(struct tty_struct *tty)
798 {
799 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
800
801 spin_lock_irq(&xr_usb_serial->read_lock);
802 xr_usb_serial->throttle_req = 1;
803 spin_unlock_irq(&xr_usb_serial->read_lock);
804 }
805
xr_usb_serial_tty_unthrottle(struct tty_struct * tty)806 static void xr_usb_serial_tty_unthrottle(struct tty_struct *tty)
807 {
808 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
809 unsigned int was_throttled;
810
811 spin_lock_irq(&xr_usb_serial->read_lock);
812 was_throttled = xr_usb_serial->throttled;
813 xr_usb_serial->throttled = 0;
814 xr_usb_serial->throttle_req = 0;
815 spin_unlock_irq(&xr_usb_serial->read_lock);
816
817 if (was_throttled)
818 xr_usb_serial_submit_read_urbs(xr_usb_serial, GFP_KERNEL);
819 }
820
xr_usb_serial_tty_break_ctl(struct tty_struct * tty,int state)821 static int xr_usb_serial_tty_break_ctl(struct tty_struct *tty, int state)
822 {
823 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
824 int retval;
825
826 retval = xr_usb_serial_send_break(xr_usb_serial, state ? 0xffff : 0);
827 if (retval < 0)
828 dev_err(&xr_usb_serial->control->dev, "%s - send break failed\n",
829 __func__);
830 return retval;
831 }
832
xr_usb_serial_tty_tiocmget(struct tty_struct * tty)833 static int xr_usb_serial_tty_tiocmget(struct tty_struct *tty)
834 {
835 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
836 //dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_tty_tiocmget\n");
837 return xr_usb_serial_tiocmget(xr_usb_serial);
838 }
839
xr_usb_serial_tty_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)840 static int xr_usb_serial_tty_tiocmset(struct tty_struct *tty,
841 unsigned int set, unsigned int clear)
842 {
843 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
844 //dev_dbg(&xr_usb_serial->control->dev, "xr_usb_serial_tty_tiocmset set=0x%x clear=0x%x\n",set,clear);
845 return xr_usb_serial_tiocmset(xr_usb_serial,set,clear);
846 }
847
get_serial_info(struct xr_usb_serial * xr_usb_serial,struct serial_struct __user * info)848 static int get_serial_info(struct xr_usb_serial *xr_usb_serial, struct serial_struct __user *info)
849 {
850 struct serial_struct tmp;
851
852 if (!info)
853 return -EINVAL;
854
855 memset(&tmp, 0, sizeof(tmp));
856 tmp.flags = ASYNC_LOW_LATENCY;
857 tmp.xmit_fifo_size = xr_usb_serial->writesize;
858 tmp.baud_base = le32_to_cpu(xr_usb_serial->line.dwDTERate);
859 tmp.close_delay = xr_usb_serial->port.close_delay / 10;
860 tmp.closing_wait = xr_usb_serial->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
861 ASYNC_CLOSING_WAIT_NONE :
862 xr_usb_serial->port.closing_wait / 10;
863
864 if (copy_to_user(info, &tmp, sizeof(tmp)))
865 return -EFAULT;
866 else
867 return 0;
868 }
869
set_serial_info(struct xr_usb_serial * xr_usb_serial,struct serial_struct __user * newinfo)870 static int set_serial_info(struct xr_usb_serial *xr_usb_serial,
871 struct serial_struct __user *newinfo)
872 {
873 struct serial_struct new_serial;
874 unsigned int closing_wait, close_delay;
875 int retval = 0;
876
877 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
878 return -EFAULT;
879
880 close_delay = new_serial.close_delay * 10;
881 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
882 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
883
884 mutex_lock(&xr_usb_serial->port.mutex);
885
886 if (!capable(CAP_SYS_ADMIN)) {
887 if ((close_delay != xr_usb_serial->port.close_delay) ||
888 (closing_wait != xr_usb_serial->port.closing_wait))
889 retval = -EPERM;
890 else
891 retval = -EOPNOTSUPP;
892 } else {
893 xr_usb_serial->port.close_delay = close_delay;
894 xr_usb_serial->port.closing_wait = closing_wait;
895 }
896
897 mutex_unlock(&xr_usb_serial->port.mutex);
898 return retval;
899 }
900
xr_usb_serial_tty_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)901 static int xr_usb_serial_tty_ioctl(struct tty_struct *tty,
902 unsigned int cmd, unsigned long arg)
903 {
904 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
905 int rv = -ENOIOCTLCMD;
906 unsigned int channel, reg, val,preciseflags;
907 int baud_rate = 0;
908 struct usb_cdc_line_coding newline;
909 short *data;
910 switch (cmd) {
911 case TIOCGSERIAL: /* gets serial port data */
912 rv = get_serial_info(xr_usb_serial, (struct serial_struct __user *) arg);
913 break;
914 case TIOCSSERIAL:
915 rv = set_serial_info(xr_usb_serial, (struct serial_struct __user *) arg);
916 break;
917 case XR_USB_SERIAL_GET_REG:
918 if (get_user(channel, (int __user *)arg))
919 return -EFAULT;
920 if (get_user(reg, (int __user *)(arg + sizeof(int))))
921 return -EFAULT;
922
923 data = kmalloc(2, GFP_KERNEL);
924 if (data == NULL) {
925 dev_err(&xr_usb_serial->control->dev, "%s - Cannot allocate USB buffer.\n", __func__);
926 return -ENOMEM;
927 }
928
929 if (channel == -1)
930 {
931 rv = xr_usb_serial_get_reg(xr_usb_serial,reg, data);
932 }
933 else
934 {
935 rv = xr_usb_serial_get_reg_ext(xr_usb_serial,channel,reg, data);
936 }
937 if (rv < 0)
938 {
939 dev_err(&xr_usb_serial->control->dev, "Cannot get register (%d)\n", rv);
940 kfree(data);
941 return -EFAULT;
942 }
943 if (put_user(le16_to_cpu(*data), (int __user *)(arg + 2 * sizeof(int))))
944 {
945 dev_err(&xr_usb_serial->control->dev, "Cannot put user result\n");
946 kfree(data);
947 return -EFAULT;
948 }
949 rv = 0;
950 kfree(data);
951 break;
952 case XR_USB_SERIAL_SET_REG:
953 if (get_user(channel, (int __user *)arg))
954 return -EFAULT;
955 if (get_user(reg, (int __user *)(arg + sizeof(int))))
956 return -EFAULT;
957 if (get_user(val, (int __user *)(arg + 2 * sizeof(int))))
958 return -EFAULT;
959
960 if (channel == -1)
961 {
962 rv = xr_usb_serial_set_reg(xr_usb_serial,reg, val);
963 }
964 else
965 {
966 rv = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,reg, val);
967 }
968 if (rv < 0)
969 return -EFAULT;
970 rv = 0;
971 break;
972 case XR_USB_SERIAL_LOOPBACK:
973 if (get_user(channel, (int __user *)arg))
974 return -EFAULT;
975 if (channel == -1)
976 channel = xr_usb_serial->channel;
977 rv = xr_usb_serial_set_loopback(xr_usb_serial,channel);
978 if (rv < 0)
979 return -EFAULT;
980 rv = 0;
981 break;
982 case XR_USB_SERIAL_SET_GPIO_MODE_REG:
983 xr_usb_serial_disable(xr_usb_serial);
984 if (get_user(channel, (int __user *)arg))
985 return -EFAULT;
986 if (get_user(val, (int __user *)(arg + sizeof(int))))
987 return -EFAULT;
988 if (channel == -1)
989 {
990 //block = portdata->block;
991 rv = xr_usb_serial_set_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_mode_addr, val);
992 }
993 else
994 {
995 rv = xr_usb_serial_set_reg_ext(xr_usb_serial,channel,xr_usb_serial->reg_map.uart_gpio_mode_addr, val);
996 }
997
998 dev_dbg(&xr_usb_serial->control->dev, "XR_USB_SERIAL_SET_GPIO_MODE_REG 0x%x val:0x%x \n", xr_usb_serial->reg_map.uart_gpio_mode_addr,val);
999 xr_usb_serial_enable(xr_usb_serial);
1000 if (rv < 0)
1001 return -EFAULT;
1002 break;
1003 case XR_USB_SERIAL_GET_GPIO_MODE_REG:
1004 xr_usb_serial_disable(xr_usb_serial);
1005 if (get_user(channel, (int __user *)arg))
1006 return -EFAULT;
1007
1008 data = kmalloc(2, GFP_KERNEL);
1009 if (data == NULL) {
1010 dev_err(&xr_usb_serial->control->dev, "%s - Cannot allocate USB buffer.\n", __func__);
1011 return -ENOMEM;
1012 }
1013
1014 if (channel == -1)
1015 {
1016 rv = xr_usb_serial_get_reg(xr_usb_serial,xr_usb_serial->reg_map.uart_gpio_mode_addr, data);
1017 }
1018 else
1019 {
1020 rv = xr_usb_serial_get_reg_ext(xr_usb_serial,channel,xr_usb_serial->reg_map.uart_gpio_mode_addr,data);
1021 }
1022
1023 xr_usb_serial_enable(xr_usb_serial);
1024
1025 dev_dbg(&xr_usb_serial->control->dev, "XR_USB_SERIAL_GET_GPIO_MODE_REG 0x%x val:0x%x \n", xr_usb_serial->reg_map.uart_gpio_mode_addr,*data);
1026
1027 if (rv < 0 ) {
1028 dev_err(&xr_usb_serial->control->dev, "Cannot get register (%d) channel=%d \n", rv,channel);
1029 kfree(data);
1030 return -EFAULT;
1031 }
1032
1033 if (put_user(data[0], (int __user *)(arg + sizeof(int)))) {
1034 dev_err(&xr_usb_serial->control->dev, "Cannot put user result\n");
1035 kfree(data);
1036 return -EFAULT;
1037 }
1038
1039 kfree(data);
1040 break;
1041 case XRIOC_SET_ANY_BAUD_RATE:
1042 if (get_user(baud_rate, (int __user *)arg)) {
1043 dev_dbg(&xr_usb_serial->control->dev, "get_user errot \n");
1044 return -EFAULT;
1045 }
1046 xr_usb_serial->line.dwDTERate = baud_rate;
1047 memcpy(&newline,&(xr_usb_serial->line),sizeof(struct usb_cdc_line_coding));
1048 xr_usb_serial_disable(xr_usb_serial);
1049 rv = xr_usb_serial_set_line(xr_usb_serial,&newline);
1050 xr_usb_serial_enable(xr_usb_serial);
1051 dev_dbg(&xr_usb_serial->control->dev, "XRIOC_SET_ANY_BAUD_RATE set baud_rate:%d ret=%d\n", baud_rate,rv);
1052 break;
1053 case XRIOC_SET_PRECISE_FLAGS:
1054 preciseflags = arg;
1055 dev_dbg(&xr_usb_serial->control->dev, "%s VIOC_SET_PRECISE_FLAGS %d\n", __func__, preciseflags);
1056 xr_usb_serial_disable(xr_usb_serial);
1057 if (preciseflags)
1058 {
1059 xr_usb_serial->preciseflags = 1;
1060 }
1061 else
1062 {
1063 xr_usb_serial->preciseflags = 0;
1064 }
1065 xr_usb_serial_set_wide_mode(xr_usb_serial,xr_usb_serial->preciseflags);
1066 xr_usb_serial_enable(xr_usb_serial);
1067 break;
1068 }
1069
1070 return rv;
1071 }
1072
1073 #ifdef CONFIG_COMPAT
xr_usb_serial_tty_compat_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)1074 static long xr_usb_serial_tty_compat_ioctl(struct tty_struct *tty,
1075 unsigned int cmd, unsigned long arg)
1076 {
1077 void __user *up = compat_ptr(arg);
1078
1079 switch (cmd) {
1080 case TIOCGSERIAL: /* gets serial port data */
1081 case TIOCSSERIAL:
1082 case XR_USB_SERIAL_GET_REG:
1083 case XR_USB_SERIAL_SET_REG:
1084 case XR_USB_SERIAL_LOOPBACK:
1085 case XR_USB_SERIAL_SET_GPIO_MODE_REG:
1086 case XR_USB_SERIAL_GET_GPIO_MODE_REG:
1087 case XRIOC_SET_ANY_BAUD_RATE:
1088 case XRIOC_SET_PRECISE_FLAGS:
1089 return xr_usb_serial_tty_ioctl(tty, cmd, arg);
1090
1091 /*
1092 * the rest has a compatible data structure behind arg,
1093 * but we have to convert it to a proper 64 bit pointer.
1094 */
1095 default:
1096 return xr_usb_serial_tty_ioctl(tty, cmd, (unsigned long)up);
1097 }
1098 }
1099 #endif
1100
xr_usb_serial_tty_set_termios(struct tty_struct * tty,struct ktermios * termios_old)1101 static void xr_usb_serial_tty_set_termios(struct tty_struct *tty,
1102 struct ktermios *termios_old)
1103 {
1104 struct xr_usb_serial *xr_usb_serial = tty->driver_data;
1105 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
1106 struct ktermios *termios = tty->termios;
1107 #else
1108 struct ktermios *termios = &tty->termios;
1109 #endif
1110 unsigned int cflag = termios->c_cflag;
1111 struct usb_cdc_line_coding newline;
1112 int newctrl = xr_usb_serial->ctrlout;
1113 xr_usb_serial_disable(xr_usb_serial);
1114 newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
1115 newline.bCharFormat = termios->c_cflag & CSTOPB ? 1 : 0;
1116 newline.bParityType = termios->c_cflag & PARENB ?
1117 (termios->c_cflag & PARODD ? 1 : 2) +
1118 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
1119 xr_usb_serial->trans9 = 0;
1120 switch (termios->c_cflag & CSIZE) {
1121 case CS5:/*using CS5 replace of the 9 bit data mode*/
1122 newline.bDataBits = 9;
1123 xr_usb_serial->trans9 =1;
1124 break;
1125 case CS6:
1126 newline.bDataBits = 6;
1127 break;
1128 case CS7:
1129 newline.bDataBits = 7;
1130 break;
1131 case CS8:
1132 default:
1133 newline.bDataBits = 8;
1134 break;
1135 }
1136 /* FIXME: Needs to clear unsupported bits in the termios */
1137 xr_usb_serial->clocal = ((termios->c_cflag & CLOCAL) != 0);
1138
1139 if (!newline.dwDTERate) {
1140 newline.dwDTERate = xr_usb_serial->line.dwDTERate;
1141 newctrl &= ~XR_USB_SERIAL_CTRL_DTR;
1142 } else
1143 newctrl |= XR_USB_SERIAL_CTRL_DTR;
1144
1145 if (newctrl != xr_usb_serial->ctrlout)
1146 xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout = newctrl);
1147
1148 if((cflag & CRTSCTS) != (termios_old->c_cflag & CRTSCTS))
1149 {
1150 /* Set the serial flow mode only when needed */
1151 xr_usb_serial_set_flow_mode(xr_usb_serial,tty,cflag);
1152 }
1153
1154 if (xr_usb_serial->trans9)
1155 {
1156 /* Turn on wide mode if we're 9-bit transparent. */
1157 xr_usb_serial_set_wide_mode(xr_usb_serial,1);
1158 }
1159 else if (!xr_usb_serial->preciseflags)
1160 {
1161 xr_usb_serial_set_wide_mode(xr_usb_serial,0);
1162 }
1163
1164 if (memcmp(&xr_usb_serial->line, &newline, sizeof newline))
1165 {
1166 memcpy(&xr_usb_serial->line, &newline, sizeof newline);
1167 dev_dbg(&xr_usb_serial->control->dev, "%s - set line: %d %d %d %d\n",
1168 __func__,
1169 le32_to_cpu(newline.dwDTERate),
1170 newline.bCharFormat, newline.bParityType,
1171 newline.bDataBits);
1172 xr_usb_serial_set_line(xr_usb_serial, &xr_usb_serial->line);
1173 }
1174 xr_usb_serial_enable(xr_usb_serial);
1175 }
1176
1177 static const struct tty_port_operations xr_usb_serial_port_ops = {
1178 .shutdown = xr_usb_serial_port_shutdown,
1179 .activate = xr_usb_serial_port_activate,
1180 .destruct = xr_usb_serial_port_destruct,
1181 };
1182
1183 /*
1184 * USB probe and disconnect routines.
1185 */
1186
1187 /* Little helpers: write/read buffers free */
xr_usb_serial_write_buffers_free(struct xr_usb_serial * xr_usb_serial)1188 static void xr_usb_serial_write_buffers_free(struct xr_usb_serial *xr_usb_serial)
1189 {
1190 int i;
1191 struct xr_usb_serial_wb *wb;
1192 struct usb_device *usb_dev = interface_to_usbdev(xr_usb_serial->control);
1193
1194 for (wb = &xr_usb_serial->wb[0], i = 0; i < XR_USB_SERIAL_NW; i++, wb++)
1195 usb_free_coherent(usb_dev, xr_usb_serial->writesize, wb->buf, wb->dmah);
1196 }
1197
xr_usb_serial_read_buffers_free(struct xr_usb_serial * xr_usb_serial)1198 static void xr_usb_serial_read_buffers_free(struct xr_usb_serial *xr_usb_serial)
1199 {
1200 struct usb_device *usb_dev = interface_to_usbdev(xr_usb_serial->control);
1201 int i;
1202
1203 for (i = 0; i < xr_usb_serial->rx_buflimit; i++)
1204 usb_free_coherent(usb_dev, xr_usb_serial->readsize,
1205 xr_usb_serial->read_buffers[i].base, xr_usb_serial->read_buffers[i].dma);
1206 }
1207
1208 /* Little helper: write buffers allocate */
xr_usb_serial_write_buffers_alloc(struct xr_usb_serial * xr_usb_serial)1209 static int xr_usb_serial_write_buffers_alloc(struct xr_usb_serial *xr_usb_serial)
1210 {
1211 int i;
1212 struct xr_usb_serial_wb *wb;
1213
1214 for (wb = &xr_usb_serial->wb[0], i = 0; i < XR_USB_SERIAL_NW; i++, wb++) {
1215 wb->buf = usb_alloc_coherent(xr_usb_serial->dev, xr_usb_serial->writesize, GFP_KERNEL,
1216 &wb->dmah);
1217 if (!wb->buf) {
1218 while (i != 0) {
1219 --i;
1220 --wb;
1221 usb_free_coherent(xr_usb_serial->dev, xr_usb_serial->writesize,
1222 wb->buf, wb->dmah);
1223 }
1224 return -ENOMEM;
1225 }
1226 }
1227 return 0;
1228 }
1229
1230 #ifdef CONFIG_GPIOLIBA
xr_usb_gpio_get(struct gpio_chip * chip,unsigned int offset)1231 static int xr_usb_gpio_get(struct gpio_chip *chip, unsigned int offset)
1232 {
1233 struct xr_usb_serial *xr_usb_serial = container_of(chip, struct xr_usb_serial, xr_gpio);
1234 int rv;
1235 short gpio_status;
1236
1237 rv = xr_usb_serial_get_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_status_addr,
1238 &gpio_status);
1239 if (gpio_status&(1 << offset))
1240 return 1;
1241 else
1242 return 0;
1243 }
1244
xr_usb_gpio_set(struct gpio_chip * chip,unsigned int offset,int val)1245 static void xr_usb_gpio_set(struct gpio_chip *chip, unsigned int offset, int val)
1246 {
1247 struct xr_usb_serial *xr_usb_serial = container_of(chip, struct xr_usb_serial, xr_gpio);
1248 int rv, tmp;
1249
1250 tmp = 1 << offset;
1251 if (val)
1252 rv = xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_set_addr, tmp);
1253 else
1254 rv = xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_clr_addr, tmp);
1255 }
1256
xr_usb_gpio_dir_input(struct gpio_chip * chip,unsigned int offset)1257 static int xr_usb_gpio_dir_input(struct gpio_chip *chip, unsigned int offset)
1258 {
1259 int rv;
1260 short dir_value;
1261 struct xr_usb_serial *xr_usb_serial = container_of(chip, struct xr_usb_serial, xr_gpio);
1262
1263 rv = xr_usb_serial_get_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_dir_addr, &dir_value);
1264 dir_value &= ~(1 << offset);
1265 rv = xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_dir_addr, (int)dir_value);
1266 return 0;
1267 }
1268
xr_usb_gpio_dir_output(struct gpio_chip * chip,unsigned int offset,int val)1269 static int xr_usb_gpio_dir_output(struct gpio_chip *chip,
1270 unsigned int offset, int val)
1271 {
1272 int rv;
1273 short tmp;
1274 struct xr_usb_serial *xr_usb_serial = container_of(chip, struct xr_usb_serial, xr_gpio);
1275
1276 rv = xr_usb_serial_get_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_dir_addr, &tmp);
1277 printk ("gpio_dir_output, offset = %d\n", offset);
1278 printk ("gpio_dir_output before set_reg = 0x%02x\n", tmp);
1279 tmp |= (1 << offset);
1280 rv = xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_dir_addr, (int)tmp);
1281 rv = xr_usb_serial_get_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_dir_addr, &tmp);
1282 printk ("gpio_dir_output after set_reg = 0x%02x\n", tmp);
1283
1284 if (offset > 7) {
1285 rv = xr_usb_serial_get_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_mode_addr, &tmp);
1286 tmp &= ~(1 << offset);
1287 rv = xr_usb_serial_set_reg(xr_usb_serial, xr_usb_serial->reg_map.uart_gpio_mode_addr,
1288 (int)tmp);
1289 }
1290
1291 return 0;
1292 }
1293 #endif
1294
xr_usb_serial_probe(struct usb_interface * intf,const struct usb_device_id * id)1295 static int xr_usb_serial_probe(struct usb_interface *intf,
1296 const struct usb_device_id *id)
1297 {
1298 struct usb_cdc_union_desc *union_header = NULL;
1299 struct usb_cdc_country_functional_desc *cfd = NULL;
1300 unsigned char *buffer = intf->altsetting->extra;
1301 int buflen = intf->altsetting->extralen;
1302 struct usb_interface *control_interface;
1303 struct usb_interface *data_interface;
1304 struct usb_endpoint_descriptor *epctrl = NULL;
1305 struct usb_endpoint_descriptor *epread = NULL;
1306 struct usb_endpoint_descriptor *epwrite = NULL;
1307 struct usb_device *usb_dev = interface_to_usbdev(intf);
1308 struct xr_usb_serial *xr_usb_serial;
1309 int minor;
1310 int ctrlsize, readsize;
1311 u8 *buf;
1312 u8 ac_management_function = 0;
1313 u8 call_management_function = 0;
1314 int call_interface_num = -1;
1315 int data_interface_num = -1;
1316 unsigned long quirks;
1317 int num_rx_buf;
1318 int i;
1319 int combined_interfaces = 0;
1320 struct device *tty_dev;
1321 int rv = -ENOMEM;
1322 #ifdef CONFIG_GPIOLIBA
1323 int gpiochip_base;
1324 #endif
1325
1326 /* normal quirks */
1327 quirks = (unsigned long)id->driver_info;
1328
1329 if (quirks == IGNORE_DEVICE)
1330 return -ENODEV;
1331
1332 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : XR_USB_SERIAL_NR;
1333
1334 dev_dbg(&intf->dev, "USB_device_id idVendor:%04x, idProduct %04x\n",id->idVendor,id->idProduct);
1335
1336 /* handle quirks deadly to normal probing*/
1337 if (quirks == NO_UNION_NORMAL) {
1338 data_interface = usb_ifnum_to_if(usb_dev, 1);
1339 control_interface = usb_ifnum_to_if(usb_dev, 0);
1340 goto skip_normal_probe;
1341 }
1342
1343 /* normal probing*/
1344 if (!buffer) {
1345 dev_err(&intf->dev, "Weird descriptor references\n");
1346 return -EINVAL;
1347 }
1348
1349 if (!buflen) {
1350 if (intf->cur_altsetting->endpoint &&
1351 intf->cur_altsetting->endpoint->extralen &&
1352 intf->cur_altsetting->endpoint->extra) {
1353 dev_dbg(&intf->dev,
1354 "Seeking extra descriptors on endpoint\n");
1355 buflen = intf->cur_altsetting->endpoint->extralen;
1356 buffer = intf->cur_altsetting->endpoint->extra;
1357 } else {
1358 dev_err(&intf->dev,
1359 "Zero length descriptor references\n");
1360 return -EINVAL;
1361 }
1362 }
1363
1364 while (buflen > 0) {
1365 if (buffer[1] != USB_DT_CS_INTERFACE) {
1366 dev_err(&intf->dev, "skipping garbage\n");
1367 goto next_desc;
1368 }
1369
1370 switch (buffer[2]) {
1371 case USB_CDC_UNION_TYPE: /* we've found it */
1372 if (union_header) {
1373 dev_err(&intf->dev, "More than one "
1374 "union descriptor, skipping ...\n");
1375 goto next_desc;
1376 }
1377 union_header = (struct usb_cdc_union_desc *)buffer;
1378 break;
1379 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
1380 cfd = (struct usb_cdc_country_functional_desc *)buffer;
1381 break;
1382 case USB_CDC_HEADER_TYPE: /* maybe check version */
1383 break; /* for now we ignore it */
1384 case USB_CDC_ACM_TYPE:
1385 ac_management_function = buffer[3];
1386 break;
1387 case USB_CDC_CALL_MANAGEMENT_TYPE:
1388 call_management_function = buffer[3];
1389 call_interface_num = buffer[4];
1390 if ((quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
1391 dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
1392 break;
1393 default:
1394 /* there are LOTS more CDC descriptors that
1395 * could legitimately be found here.
1396 */
1397 dev_dbg(&intf->dev, "Ignoring descriptor: "
1398 "type %02x, length %d\n",
1399 buffer[2], buffer[0]);
1400 break;
1401 }
1402 next_desc:
1403 buflen -= buffer[0];
1404 buffer += buffer[0];
1405 }
1406
1407 if (!union_header) {
1408 if (call_interface_num > 0) {
1409 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
1410 /* quirks for Droids MuIn LCD */
1411 if (quirks & NO_DATA_INTERFACE)
1412 data_interface = usb_ifnum_to_if(usb_dev, 0);
1413 else
1414 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
1415 control_interface = intf;
1416 } else {
1417 if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
1418 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1419 return -ENODEV;
1420 } else {
1421 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1422 combined_interfaces = 1;
1423 control_interface = data_interface = intf;
1424 goto look_for_collapsed_interface;
1425 }
1426 }
1427 } else {
1428 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1429 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
1430 if (!control_interface || !data_interface) {
1431 dev_dbg(&intf->dev, "no interfaces\n");
1432 return -ENODEV;
1433 }
1434 }
1435
1436 if (data_interface_num != call_interface_num)
1437 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
1438
1439 if (control_interface == data_interface) {
1440 /* some broken devices designed for windows work this way */
1441 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1442 combined_interfaces = 1;
1443 /* a popular other OS doesn't use it */
1444 quirks |= NO_CAP_LINE;
1445 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1446 dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1447 return -EINVAL;
1448 }
1449 look_for_collapsed_interface:
1450 for (i = 0; i < 3; i++) {
1451 struct usb_endpoint_descriptor *ep;
1452 ep = &data_interface->cur_altsetting->endpoint[i].desc;
1453
1454 if (usb_endpoint_is_int_in(ep))
1455 epctrl = ep;
1456 else if (usb_endpoint_is_bulk_out(ep))
1457 epwrite = ep;
1458 else if (usb_endpoint_is_bulk_in(ep))
1459 epread = ep;
1460 else
1461 return -EINVAL;
1462 }
1463 if (!epctrl || !epread || !epwrite)
1464 return -ENODEV;
1465 else
1466 goto made_compressed_probe;
1467 }
1468
1469 skip_normal_probe:
1470
1471 /*workaround for switched interfaces */
1472 if (data_interface->cur_altsetting->desc.bInterfaceClass
1473 != CDC_DATA_INTERFACE_TYPE) {
1474 if (control_interface->cur_altsetting->desc.bInterfaceClass
1475 == CDC_DATA_INTERFACE_TYPE) {
1476 struct usb_interface *t;
1477 dev_dbg(&intf->dev,
1478 "Your device has switched interfaces.\n");
1479 t = control_interface;
1480 control_interface = data_interface;
1481 data_interface = t;
1482 } else {
1483 return -EINVAL;
1484 }
1485 }
1486
1487 /* Accept probe requests only for the control interface */
1488 if (!combined_interfaces && intf != control_interface)
1489 return -ENODEV;
1490
1491 if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1492 /* valid in this context */
1493 dev_dbg(&intf->dev, "The data interface isn't available\n");
1494 return -EBUSY;
1495 }
1496
1497
1498 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1499 control_interface->cur_altsetting->desc.bNumEndpoints == 0)
1500 return -EINVAL;
1501
1502 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1503 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1504 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1505
1506
1507 /* workaround for switched endpoints */
1508 if (!usb_endpoint_dir_in(epread)) {
1509 /* descriptors are swapped */
1510 struct usb_endpoint_descriptor *t;
1511 dev_dbg(&intf->dev,
1512 "The data interface has switched endpoints\n");
1513 t = epread;
1514 epread = epwrite;
1515 epwrite = t;
1516 }
1517 made_compressed_probe:
1518 dev_dbg(&intf->dev, "interfaces are valid\n");
1519
1520 xr_usb_serial = kzalloc(sizeof(struct xr_usb_serial), GFP_KERNEL);
1521 if (xr_usb_serial == NULL) {
1522 dev_err(&intf->dev, "out of memory (xr_usb_serial kzalloc)\n");
1523 goto alloc_fail;
1524 }
1525
1526 minor = xr_usb_serial_alloc_minor(xr_usb_serial);
1527 if (minor == XR_USB_SERIAL_TTY_MINORS) {
1528 dev_err(&intf->dev, "no more free xr_usb_serial devices\n");
1529 kfree(xr_usb_serial);
1530 return -ENODEV;
1531 }
1532
1533 ctrlsize = usb_endpoint_maxp(epctrl);
1534 readsize = usb_endpoint_maxp(epread) *
1535 (quirks == SINGLE_RX_URB ? 1 : 2);
1536 xr_usb_serial->combined_interfaces = combined_interfaces;
1537 xr_usb_serial->writesize = usb_endpoint_maxp(epwrite) * 20;
1538 xr_usb_serial->control = control_interface;
1539 xr_usb_serial->data = data_interface;
1540 xr_usb_serial->minor = minor;
1541 xr_usb_serial->dev = usb_dev;
1542 xr_usb_serial->ctrl_caps = ac_management_function;
1543 if (quirks & NO_CAP_LINE)
1544 xr_usb_serial->ctrl_caps &= ~USB_CDC_CAP_LINE;
1545 xr_usb_serial->ctrlsize = ctrlsize;
1546 xr_usb_serial->readsize = readsize;
1547 xr_usb_serial->rx_buflimit = num_rx_buf;
1548 INIT_WORK(&xr_usb_serial->work, xr_usb_serial_softint);
1549 spin_lock_init(&xr_usb_serial->write_lock);
1550 spin_lock_init(&xr_usb_serial->read_lock);
1551 mutex_init(&xr_usb_serial->mutex);
1552 xr_usb_serial->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1553 xr_usb_serial->is_int_ep = usb_endpoint_xfer_int(epread);
1554 if (xr_usb_serial->is_int_ep)
1555 xr_usb_serial->bInterval = epread->bInterval;
1556 tty_port_init(&xr_usb_serial->port);
1557 xr_usb_serial->port.ops = &xr_usb_serial_port_ops;
1558 xr_usb_serial->DeviceVendor = id->idVendor;
1559 xr_usb_serial->DeviceProduct = id->idProduct;
1560 #if 0
1561 if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1410)
1562 {//map the serial port A B C D to blocknum 0 1 2 3 for the xr21v141x device
1563 xr_usb_serial->channel = epwrite->bEndpointAddress - 1;
1564 }
1565 else if((xr_usb_serial->DeviceProduct&0xfff0) == 0x1420)
1566 {//map the serial port A B C D to blocknum 0 2 4 6 for the xr21B142x device
1567 xr_usb_serial->channel = (epwrite->bEndpointAddress - 4)*2;
1568 }
1569 else
1570 {
1571 xr_usb_serial->channel = epwrite->bEndpointAddress;
1572 }
1573 #else
1574 xr_usb_serial->channel = epwrite->bEndpointAddress;
1575 dev_dbg(&intf->dev, "epwrite->bEndpointAddress =%d\n",epwrite->bEndpointAddress);
1576 #endif
1577 buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &xr_usb_serial->ctrl_dma);
1578 if (!buf) {
1579 dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1580 goto alloc_fail2;
1581 }
1582 xr_usb_serial->ctrl_buffer = buf;
1583
1584 if (xr_usb_serial_write_buffers_alloc(xr_usb_serial) < 0) {
1585 dev_err(&intf->dev, "out of memory (write buffer alloc)\n");
1586 goto alloc_fail4;
1587 }
1588
1589 xr_usb_serial->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1590 if (!xr_usb_serial->ctrlurb) {
1591 dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1592 goto alloc_fail5;
1593 }
1594 for (i = 0; i < num_rx_buf; i++) {
1595 struct xr_usb_serial_rb *rb = &(xr_usb_serial->read_buffers[i]);
1596 struct urb *urb;
1597
1598 rb->base = usb_alloc_coherent(xr_usb_serial->dev, readsize, GFP_KERNEL,
1599 &rb->dma);
1600 if (!rb->base) {
1601 dev_err(&intf->dev, "out of memory "
1602 "(read bufs usb_alloc_coherent)\n");
1603 goto alloc_fail6;
1604 }
1605 rb->index = i;
1606 rb->instance = xr_usb_serial;
1607
1608 urb = usb_alloc_urb(0, GFP_KERNEL);
1609 if (!urb) {
1610 dev_err(&intf->dev,
1611 "out of memory (read urbs usb_alloc_urb)\n");
1612 goto alloc_fail6;
1613 }
1614 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1615 urb->transfer_dma = rb->dma;
1616 if (xr_usb_serial->is_int_ep) {
1617 usb_fill_int_urb(urb, xr_usb_serial->dev,
1618 xr_usb_serial->rx_endpoint,
1619 rb->base,
1620 xr_usb_serial->readsize,
1621 xr_usb_serial_read_bulk_callback, rb,
1622 xr_usb_serial->bInterval);
1623 } else {
1624 usb_fill_bulk_urb(urb, xr_usb_serial->dev,
1625 xr_usb_serial->rx_endpoint,
1626 rb->base,
1627 xr_usb_serial->readsize,
1628 xr_usb_serial_read_bulk_callback, rb);
1629 }
1630
1631 xr_usb_serial->read_urbs[i] = urb;
1632 __set_bit(i, &xr_usb_serial->read_urbs_free);
1633 }
1634 for (i = 0; i < XR_USB_SERIAL_NW; i++) {
1635 struct xr_usb_serial_wb *snd = &(xr_usb_serial->wb[i]);
1636
1637 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
1638 if (snd->urb == NULL) {
1639 dev_err(&intf->dev,
1640 "out of memory (write urbs usb_alloc_urb)\n");
1641 goto alloc_fail7;
1642 }
1643
1644 if (usb_endpoint_xfer_int(epwrite))
1645 usb_fill_int_urb(snd->urb, usb_dev,
1646 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
1647 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1648 #else
1649 usb_sndintpipe(usb_dev, epwrite->bEndpointAddress),
1650 #endif
1651 NULL, xr_usb_serial->writesize, xr_usb_serial_write_bulk, snd, epwrite->bInterval);
1652 else
1653 usb_fill_bulk_urb(snd->urb, usb_dev,
1654 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1655 NULL, xr_usb_serial->writesize, xr_usb_serial_write_bulk, snd);
1656 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1657 snd->instance = xr_usb_serial;
1658 }
1659
1660 usb_set_intfdata(intf, xr_usb_serial);
1661
1662 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1663 if (i < 0)
1664 goto alloc_fail7;
1665
1666 if (cfd) { /* export the country data */
1667 xr_usb_serial->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1668 if (!xr_usb_serial->country_codes)
1669 goto skip_countries;
1670 xr_usb_serial->country_code_size = cfd->bLength - 4;
1671 memcpy(xr_usb_serial->country_codes, (u8 *)&cfd->wCountyCode0,
1672 cfd->bLength - 4);
1673 xr_usb_serial->country_rel_date = cfd->iCountryCodeRelDate;
1674
1675 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1676 if (i < 0) {
1677 kfree(xr_usb_serial->country_codes);
1678 xr_usb_serial->country_codes = NULL;
1679 xr_usb_serial->country_code_size = 0;
1680 goto skip_countries;
1681 }
1682
1683 i = device_create_file(&intf->dev,
1684 &dev_attr_iCountryCodeRelDate);
1685 if (i < 0) {
1686 device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
1687 kfree(xr_usb_serial->country_codes);
1688 xr_usb_serial->country_codes = NULL;
1689 xr_usb_serial->country_code_size = 0;
1690 goto skip_countries;
1691 }
1692 }
1693
1694 skip_countries:
1695 usb_fill_int_urb(xr_usb_serial->ctrlurb, usb_dev,
1696 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1697 xr_usb_serial->ctrl_buffer, ctrlsize, xr_usb_serial_ctrl_irq, xr_usb_serial,
1698 /* works around buggy devices */
1699 epctrl->bInterval ? epctrl->bInterval : 0xff);
1700 xr_usb_serial->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1701 xr_usb_serial->ctrlurb->transfer_dma = xr_usb_serial->ctrl_dma;
1702
1703 dev_info(&intf->dev, "ttyXR_USB_SERIAL%d: USB XR_USB_SERIAL device\n", minor);
1704
1705 xr_usb_serial_pre_setup(xr_usb_serial);
1706
1707 xr_usb_serial_set_control(xr_usb_serial, xr_usb_serial->ctrlout);
1708
1709 xr_usb_serial->line.dwDTERate = cpu_to_le32(9600);
1710 xr_usb_serial->line.bDataBits = 8;
1711 xr_usb_serial_set_line(xr_usb_serial, &xr_usb_serial->line);
1712
1713 usb_driver_claim_interface(&xr_usb_serial_driver, data_interface, xr_usb_serial);
1714 usb_set_intfdata(data_interface, xr_usb_serial);
1715
1716 usb_get_intf(control_interface);
1717 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
1718 tty_register_device(xr_usb_serial_tty_driver, minor, &control_interface->dev);
1719 #else
1720 tty_dev = tty_port_register_device(&xr_usb_serial->port, xr_usb_serial_tty_driver, minor,
1721 &control_interface->dev);
1722 if (IS_ERR(tty_dev)) {
1723 rv = PTR_ERR(tty_dev);
1724 goto alloc_fail8;
1725 }
1726 #endif
1727
1728 #ifdef CONFIG_GPIOLIBA
1729 /* Setup GPIO cotroller */
1730 gpiochip_base = 0;
1731
1732 xr_usb_serial->xr_gpio.owner = THIS_MODULE;
1733 xr_usb_serial->xr_gpio.label = dev_name(&control_interface->dev);
1734 xr_usb_serial->xr_gpio.direction_input = xr_usb_gpio_dir_input;
1735 xr_usb_serial->xr_gpio.get = xr_usb_gpio_get;
1736 xr_usb_serial->xr_gpio.direction_output = xr_usb_gpio_dir_output;
1737 xr_usb_serial->xr_gpio.set = xr_usb_gpio_set;
1738 xr_usb_serial->xr_gpio.base = gpiochip_base;
1739 xr_usb_serial->xr_gpio.ngpio = 10;
1740 xr_usb_serial->xr_gpio.can_sleep = 1;
1741
1742 rv = gpiochip_add(&xr_usb_serial->xr_gpio);
1743
1744 if (rv != 0) {
1745 // gpiochip numbers not available, start from 0
1746 xr_usb_serial->xr_gpio.base = 0;
1747 }
1748
1749 while (rv != 0) {
1750 xr_usb_serial->xr_gpio.base += 10;
1751
1752 if (xr_usb_serial->xr_gpio.base > 502) {
1753 // max gpio number = 512
1754 // we ran out of gpios??
1755 break;
1756 }
1757 rv = gpiochip_add(&xr_usb_serial->xr_gpio);
1758 }
1759 xr_usb_serial->rv_gpio_created = rv;
1760 if (rv == 0) {
1761 dev_dbg(&xr_usb_serial->control->dev, "gpiochip%d added",
1762 xr_usb_serial->xr_gpio.base);
1763 } else {
1764 dev_dbg(&xr_usb_serial->control->dev, "failed to add gpiochip\n");
1765 }
1766
1767 #endif
1768
1769 return 0;
1770 alloc_fail8:
1771 if (xr_usb_serial->country_codes) {
1772 device_remove_file(&xr_usb_serial->control->dev,
1773 &dev_attr_wCountryCodes);
1774 device_remove_file(&xr_usb_serial->control->dev,
1775 &dev_attr_iCountryCodeRelDate);
1776 }
1777 device_remove_file(&xr_usb_serial->control->dev, &dev_attr_bmCapabilities);
1778 alloc_fail7:
1779 usb_set_intfdata(intf, NULL);
1780 for (i = 0; i < XR_USB_SERIAL_NW; i++)
1781 usb_free_urb(xr_usb_serial->wb[i].urb);
1782 alloc_fail6:
1783 for (i = 0; i < num_rx_buf; i++)
1784 usb_free_urb(xr_usb_serial->read_urbs[i]);
1785 xr_usb_serial_read_buffers_free(xr_usb_serial);
1786 usb_free_urb(xr_usb_serial->ctrlurb);
1787 alloc_fail5:
1788 xr_usb_serial_write_buffers_free(xr_usb_serial);
1789 alloc_fail4:
1790 usb_free_coherent(usb_dev, ctrlsize, xr_usb_serial->ctrl_buffer, xr_usb_serial->ctrl_dma);
1791 alloc_fail2:
1792 xr_usb_serial_release_minor(xr_usb_serial);
1793 kfree(xr_usb_serial);
1794 alloc_fail:
1795 return rv;
1796 }
1797
stop_data_traffic(struct xr_usb_serial * xr_usb_serial)1798 static void stop_data_traffic(struct xr_usb_serial *xr_usb_serial)
1799 {
1800 int i;
1801
1802 dev_dbg(&xr_usb_serial->control->dev, "%s\n", __func__);
1803
1804 usb_kill_urb(xr_usb_serial->ctrlurb);
1805 for (i = 0; i < XR_USB_SERIAL_NW; i++)
1806 usb_kill_urb(xr_usb_serial->wb[i].urb);
1807 for (i = 0; i < xr_usb_serial->rx_buflimit; i++)
1808 usb_kill_urb(xr_usb_serial->read_urbs[i]);
1809
1810 cancel_work_sync(&xr_usb_serial->work);
1811 }
1812
xr_usb_serial_disconnect(struct usb_interface * intf)1813 static void xr_usb_serial_disconnect(struct usb_interface *intf)
1814 {
1815 struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf);
1816 struct usb_device *usb_dev = interface_to_usbdev(intf);
1817 struct tty_struct *tty;
1818 int i;
1819
1820 dev_dbg(&intf->dev, "%s\n", __func__);
1821
1822 /* sibling interface is already cleaning up */
1823 if (!xr_usb_serial)
1824 return;
1825
1826 mutex_lock(&xr_usb_serial->mutex);
1827 xr_usb_serial->disconnected = true;
1828 if (xr_usb_serial->country_codes) {
1829 device_remove_file(&xr_usb_serial->control->dev,
1830 &dev_attr_wCountryCodes);
1831 device_remove_file(&xr_usb_serial->control->dev,
1832 &dev_attr_iCountryCodeRelDate);
1833 }
1834 device_remove_file(&xr_usb_serial->control->dev, &dev_attr_bmCapabilities);
1835 usb_set_intfdata(xr_usb_serial->control, NULL);
1836 usb_set_intfdata(xr_usb_serial->data, NULL);
1837 mutex_unlock(&xr_usb_serial->mutex);
1838
1839 tty = tty_port_tty_get(&xr_usb_serial->port);
1840 if (tty) {
1841 tty_vhangup(tty);
1842 tty_kref_put(tty);
1843 }
1844 stop_data_traffic(xr_usb_serial);
1845 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 7, 0)
1846 tty_unregister_device(xr_usb_serial_tty_driver, xr_usb_serial->minor);
1847 #endif
1848
1849 usb_free_urb(xr_usb_serial->ctrlurb);
1850 for (i = 0; i < XR_USB_SERIAL_NW; i++)
1851 usb_free_urb(xr_usb_serial->wb[i].urb);
1852 for (i = 0; i < xr_usb_serial->rx_buflimit; i++)
1853 usb_free_urb(xr_usb_serial->read_urbs[i]);
1854 xr_usb_serial_write_buffers_free(xr_usb_serial);
1855 usb_free_coherent(usb_dev, xr_usb_serial->ctrlsize, xr_usb_serial->ctrl_buffer, xr_usb_serial->ctrl_dma);
1856 xr_usb_serial_read_buffers_free(xr_usb_serial);
1857
1858 if (!xr_usb_serial->combined_interfaces)
1859 usb_driver_release_interface(&xr_usb_serial_driver, intf == xr_usb_serial->control ?
1860 xr_usb_serial->data : xr_usb_serial->control);
1861
1862 tty_port_put(&xr_usb_serial->port);
1863 }
1864
1865 #ifdef CONFIG_PM
xr_usb_serial_suspend(struct usb_interface * intf,pm_message_t message)1866 static int xr_usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1867 {
1868 struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf);
1869 int cnt;
1870
1871 if (PMSG_IS_AUTO(message)) {
1872 int b;
1873
1874 spin_lock_irq(&xr_usb_serial->write_lock);
1875 b = xr_usb_serial->transmitting;
1876 spin_unlock_irq(&xr_usb_serial->write_lock);
1877 if (b)
1878 return -EBUSY;
1879 }
1880
1881 spin_lock_irq(&xr_usb_serial->read_lock);
1882 spin_lock(&xr_usb_serial->write_lock);
1883 cnt = xr_usb_serial->susp_count++;
1884 spin_unlock(&xr_usb_serial->write_lock);
1885 spin_unlock_irq(&xr_usb_serial->read_lock);
1886
1887 if (cnt)
1888 return 0;
1889
1890 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 2)
1891 if (test_bit(ASYNCB_INITIALIZED, &xr_usb_serial->port.flags))
1892 #endif
1893 stop_data_traffic(xr_usb_serial);
1894
1895 return 0;
1896 }
1897
xr_usb_serial_resume(struct usb_interface * intf)1898 static int xr_usb_serial_resume(struct usb_interface *intf)
1899 {
1900 struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf);
1901 struct xr_usb_serial_wb *wb;
1902 int rv = 0;
1903 int cnt;
1904
1905 spin_lock_irq(&xr_usb_serial->read_lock);
1906 xr_usb_serial->susp_count -= 1;
1907 cnt = xr_usb_serial->susp_count;
1908 spin_unlock_irq(&xr_usb_serial->read_lock);
1909
1910 if (cnt)
1911 return 0;
1912
1913 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)
1914 if (test_bit(ASYNCB_INITIALIZED, &xr_usb_serial->port.flags)) {
1915 #else
1916 if (tty_port_initialized(&xr_usb_serial->port)) {
1917 #endif
1918 rv = usb_submit_urb(xr_usb_serial->ctrlurb, GFP_NOIO);
1919
1920 spin_lock_irq(&xr_usb_serial->write_lock);
1921 if (xr_usb_serial->delayed_wb) {
1922 wb = xr_usb_serial->delayed_wb;
1923 xr_usb_serial->delayed_wb = NULL;
1924 spin_unlock_irq(&xr_usb_serial->write_lock);
1925 xr_usb_serial_start_wb(xr_usb_serial, wb);
1926 } else {
1927 spin_unlock_irq(&xr_usb_serial->write_lock);
1928 }
1929
1930 /*
1931 * delayed error checking because we must
1932 * do the write path at all cost
1933 */
1934 if (rv < 0)
1935 goto err_out;
1936
1937 rv = xr_usb_serial_submit_read_urbs(xr_usb_serial, GFP_NOIO);
1938 }
1939
1940 err_out:
1941 return rv;
1942 }
1943
1944 static int xr_usb_serial_reset_resume(struct usb_interface *intf)
1945 {
1946 struct xr_usb_serial *xr_usb_serial = usb_get_intfdata(intf);
1947 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
1948 #else
1949 struct tty_struct *tty;
1950 #endif
1951 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 7, 0)
1952 if (test_bit(ASYNCB_INITIALIZED, &xr_usb_serial->port.flags)){
1953 #else
1954 if (tty_port_initialized(&xr_usb_serial->port)) {
1955 #endif
1956 #if LINUX_VERSION_CODE > KERNEL_VERSION(3, 9, 0)
1957 tty_port_tty_hangup(&xr_usb_serial->port, false);
1958 #else
1959 tty = tty_port_tty_get(&xr_usb_serial->port);
1960 if (tty) {
1961 tty_hangup(tty);
1962 tty_kref_put(tty);
1963 }
1964 #endif
1965 }
1966 return xr_usb_serial_resume(intf);
1967 }
1968
1969 #endif /* CONFIG_PM */
1970
1971 /*
1972 * USB driver structure.
1973 */
1974 static const struct usb_device_id xr_usb_serial_ids[] = {
1975 { USB_DEVICE(0x04e2, 0x1410)},
1976 { USB_DEVICE(0x04e2, 0x1411)},
1977 { USB_DEVICE(0x04e2, 0x1412)},
1978 { USB_DEVICE(0x04e2, 0x1414)},
1979 { USB_DEVICE(0x04e2, 0x1420)},
1980 { USB_DEVICE(0x04e2, 0x1421)},
1981 { USB_DEVICE(0x04e2, 0x1422)},
1982 { USB_DEVICE(0x04e2, 0x1424)},
1983 { USB_DEVICE(0x04e2, 0x1400)},
1984 { USB_DEVICE(0x04e2, 0x1401)},
1985 { USB_DEVICE(0x04e2, 0x1402)},
1986 { USB_DEVICE(0x04e2, 0x1403)},
1987 { }
1988 };
1989
1990 MODULE_DEVICE_TABLE(usb, xr_usb_serial_ids);
1991
1992 static struct usb_driver xr_usb_serial_driver = {
1993 .name = "cdc_xr_usb_serial",
1994 .probe = xr_usb_serial_probe,
1995 .disconnect = xr_usb_serial_disconnect,
1996 #ifdef CONFIG_PM
1997 .suspend = xr_usb_serial_suspend,
1998 .resume = xr_usb_serial_resume,
1999 .reset_resume = xr_usb_serial_reset_resume,
2000 #endif
2001 .id_table = xr_usb_serial_ids,
2002 #ifdef CONFIG_PM
2003 .supports_autosuspend = 1,
2004 #endif
2005 .disable_hub_initiated_lpm = 1,
2006 };
2007
2008 /*
2009 * TTY driver structures.
2010 */
2011
2012 static const struct tty_operations xr_usb_serial_ops = {
2013 .install = xr_usb_serial_tty_install,
2014 .open = xr_usb_serial_tty_open,
2015 .close = xr_usb_serial_tty_close,
2016 .cleanup = xr_usb_serial_tty_cleanup,
2017 .hangup = xr_usb_serial_tty_hangup,
2018 .write = xr_usb_serial_tty_write,
2019 .write_room = xr_usb_serial_tty_write_room,
2020 #ifdef CONFIG_COMPAT
2021 .compat_ioctl = xr_usb_serial_tty_compat_ioctl,
2022 #endif
2023 .ioctl = xr_usb_serial_tty_ioctl,
2024 .throttle = xr_usb_serial_tty_throttle,
2025 .unthrottle = xr_usb_serial_tty_unthrottle,
2026 .chars_in_buffer = xr_usb_serial_tty_chars_in_buffer,
2027 .break_ctl = xr_usb_serial_tty_break_ctl,
2028 .set_termios = xr_usb_serial_tty_set_termios,
2029 .tiocmget = xr_usb_serial_tty_tiocmget,
2030 .tiocmset = xr_usb_serial_tty_tiocmset,
2031 };
2032
2033 /*
2034 * Init / exit.
2035 */
2036
2037 static int __init xr_usb_serial_init(void)
2038 {
2039 int retval;
2040 xr_usb_serial_tty_driver = alloc_tty_driver(XR_USB_SERIAL_TTY_MINORS);
2041 if (!xr_usb_serial_tty_driver)
2042 return -ENOMEM;
2043 xr_usb_serial_tty_driver->driver_name = "xr_usb_serial",
2044 xr_usb_serial_tty_driver->name = "ttyXRUSB",
2045 xr_usb_serial_tty_driver->major = XR_USB_SERIAL_TTY_MAJOR,
2046 xr_usb_serial_tty_driver->minor_start = 0,
2047 xr_usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
2048 xr_usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL,
2049 xr_usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
2050 xr_usb_serial_tty_driver->init_termios = tty_std_termios;
2051 xr_usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
2052 HUPCL | CLOCAL;
2053 tty_set_operations(xr_usb_serial_tty_driver, &xr_usb_serial_ops);
2054
2055 retval = tty_register_driver(xr_usb_serial_tty_driver);
2056 if (retval) {
2057 put_tty_driver(xr_usb_serial_tty_driver);
2058 return retval;
2059 }
2060
2061 retval = usb_register(&xr_usb_serial_driver);
2062 if (retval) {
2063 tty_unregister_driver(xr_usb_serial_tty_driver);
2064 put_tty_driver(xr_usb_serial_tty_driver);
2065 return retval;
2066 }
2067
2068 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
2069
2070 return 0;
2071 }
2072
2073 static void __exit xr_usb_serial_exit(void)
2074 {
2075 usb_deregister(&xr_usb_serial_driver);
2076 tty_unregister_driver(xr_usb_serial_tty_driver);
2077 put_tty_driver(xr_usb_serial_tty_driver);
2078 }
2079
2080 module_init(xr_usb_serial_init);
2081 module_exit(xr_usb_serial_exit);
2082
2083 MODULE_AUTHOR(DRIVER_AUTHOR);
2084 MODULE_DESCRIPTION(DRIVER_DESC);
2085 MODULE_LICENSE("GPL");
2086 MODULE_ALIAS_CHARDEV_MAJOR(XR_USB_SERIAL_TTY_MAJOR);
2087