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