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