projects/linux·blob master · refs

3105 lines · 84.1 KiB

1// SPDX-License-Identifier: GPL-2.0+
2/*
3 Keyspan USB to Serial Converter driver
4
5 (C) Copyright (C) 2000-2001 Hugh Blemings <[email protected]>
6 (C) Copyright (C) 2002 Greg Kroah-Hartman <[email protected]>
7
8 See http://blemings.org/hugh/keyspan.html for more information.
9
10 Code in this driver inspired by and in a number of places taken
11 from Brian Warner's original Keyspan-PDA driver.
12
13 This driver has been put together with the support of Innosys, Inc.
14 and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
15 Thanks Guys :)
16
17 Thanks to Paulus for miscellaneous tidy ups, some largish chunks
18 of much nicer and/or completely new code and (perhaps most uniquely)
19 having the patience to sit down and explain why and where he'd changed
20 stuff.
21
22 Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
23 staff in their work on open source projects.
24*/
25
26
27#include <linux/kernel.h>
28#include <linux/jiffies.h>
29#include <linux/errno.h>
30#include <linux/slab.h>
31#include <linux/tty.h>
32#include <linux/tty_flip.h>
33#include <linux/module.h>
34#include <linux/spinlock.h>
35#include <linux/usb.h>
36#include <linux/usb/serial.h>
37#include <linux/usb/ezusb.h>
38
39#define DRIVER_AUTHOR "Hugh Blemings <[email protected]"
40#define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
41
42static void keyspan_send_setup(struct usb_serial_port *port, int reset_port);
43
44static int keyspan_usa19_calc_baud(struct usb_serial_port *port,
45 u32 baud_rate, u32 baudclk,
46 u8 *rate_hi, u8 *rate_low,
47 u8 *prescaler, int portnum);
48static int keyspan_usa19w_calc_baud(struct usb_serial_port *port,
49 u32 baud_rate, u32 baudclk,
50 u8 *rate_hi, u8 *rate_low,
51 u8 *prescaler, int portnum);
52static int keyspan_usa28_calc_baud(struct usb_serial_port *port,
53 u32 baud_rate, u32 baudclk,
54 u8 *rate_hi, u8 *rate_low,
55 u8 *prescaler, int portnum);
56static int keyspan_usa19hs_calc_baud(struct usb_serial_port *port,
57 u32 baud_rate, u32 baudclk,
58 u8 *rate_hi, u8 *rate_low,
59 u8 *prescaler, int portnum);
60
61static int keyspan_usa28_send_setup(struct usb_serial *serial,
62 struct usb_serial_port *port,
63 int reset_port);
64static int keyspan_usa26_send_setup(struct usb_serial *serial,
65 struct usb_serial_port *port,
66 int reset_port);
67static int keyspan_usa49_send_setup(struct usb_serial *serial,
68 struct usb_serial_port *port,
69 int reset_port);
70static int keyspan_usa90_send_setup(struct usb_serial *serial,
71 struct usb_serial_port *port,
72 int reset_port);
73static int keyspan_usa67_send_setup(struct usb_serial *serial,
74 struct usb_serial_port *port,
75 int reset_port);
76
77/* Values used for baud rate calculation - device specific */
78#define KEYSPAN_INVALID_BAUD_RATE (-1)
79#define KEYSPAN_BAUD_RATE_OK (0)
80#define KEYSPAN_USA18X_BAUDCLK (12000000L) /* a guess */
81#define KEYSPAN_USA19_BAUDCLK (12000000L)
82#define KEYSPAN_USA19W_BAUDCLK (24000000L)
83#define KEYSPAN_USA19HS_BAUDCLK (14769231L)
84#define KEYSPAN_USA28_BAUDCLK (1843200L)
85#define KEYSPAN_USA28X_BAUDCLK (12000000L)
86#define KEYSPAN_USA49W_BAUDCLK (48000000L)
87
88/* Some constants used to characterise each device. */
89#define KEYSPAN_MAX_NUM_PORTS (4)
90#define KEYSPAN_MAX_FLIPS (2)
91
92/*
93 * Device info for the Keyspan serial converter, used by the overall
94 * usb-serial probe function.
95 */
96#define KEYSPAN_VENDOR_ID (0x06cd)
97
98/* Product IDs for the products supported, pre-renumeration */
99#define keyspan_usa18x_pre_product_id 0x0105
100#define keyspan_usa19_pre_product_id 0x0103
101#define keyspan_usa19qi_pre_product_id 0x010b
102#define keyspan_mpr_pre_product_id 0x011b
103#define keyspan_usa19qw_pre_product_id 0x0118
104#define keyspan_usa19w_pre_product_id 0x0106
105#define keyspan_usa28_pre_product_id 0x0101
106#define keyspan_usa28x_pre_product_id 0x0102
107#define keyspan_usa28xa_pre_product_id 0x0114
108#define keyspan_usa28xb_pre_product_id 0x0113
109#define keyspan_usa49w_pre_product_id 0x0109
110#define keyspan_usa49wlc_pre_product_id 0x011a
111
112/*
113 * Product IDs post-renumeration. Note that the 28x and 28xb have the same
114 * id's post-renumeration but behave identically so it's not an issue. As
115 * such, the 28xb is not listed in any of the device tables.
116 */
117#define keyspan_usa18x_product_id 0x0112
118#define keyspan_usa19_product_id 0x0107
119#define keyspan_usa19qi_product_id 0x010c
120#define keyspan_usa19hs_product_id 0x0121
121#define keyspan_mpr_product_id 0x011c
122#define keyspan_usa19qw_product_id 0x0119
123#define keyspan_usa19w_product_id 0x0108
124#define keyspan_usa28_product_id 0x010f
125#define keyspan_usa28x_product_id 0x0110
126#define keyspan_usa28xa_product_id 0x0115
127#define keyspan_usa28xb_product_id 0x0110
128#define keyspan_usa28xg_product_id 0x0135
129#define keyspan_usa49w_product_id 0x010a
130#define keyspan_usa49wlc_product_id 0x012a
131#define keyspan_usa49wg_product_id 0x0131
132
133struct keyspan_device_details {
134 /* product ID value */
135 int product_id;
136
137 enum {msg_usa26, msg_usa28, msg_usa49, msg_usa90, msg_usa67} msg_format;
138
139 /* Number of physical ports */
140 int num_ports;
141
142 /* 1 if endpoint flipping used on input, 0 if not */
143 int indat_endp_flip;
144
145 /* 1 if endpoint flipping used on output, 0 if not */
146 int outdat_endp_flip;
147
148 /*
149 * Table mapping input data endpoint IDs to physical port
150 * number and flip if used
151 */
152 int indat_endpoints[KEYSPAN_MAX_NUM_PORTS];
153
154 /* Same for output endpoints */
155 int outdat_endpoints[KEYSPAN_MAX_NUM_PORTS];
156
157 /* Input acknowledge endpoints */
158 int inack_endpoints[KEYSPAN_MAX_NUM_PORTS];
159
160 /* Output control endpoints */
161 int outcont_endpoints[KEYSPAN_MAX_NUM_PORTS];
162
163 /* Endpoint used for input status */
164 int instat_endpoint;
165
166 /* Endpoint used for input data 49WG only */
167 int indat_endpoint;
168
169 /* Endpoint used for global control functions */
170 int glocont_endpoint;
171
172 int (*calculate_baud_rate)(struct usb_serial_port *port,
173 u32 baud_rate, u32 baudclk,
174 u8 *rate_hi, u8 *rate_low, u8 *prescaler,
175 int portnum);
176 u32 baudclk;
177};
178
179/*
180 * Now for each device type we setup the device detail structure with the
181 * appropriate information (provided in Keyspan's documentation)
182 */
183
184static const struct keyspan_device_details usa18x_device_details = {
185 .product_id = keyspan_usa18x_product_id,
186 .msg_format = msg_usa26,
187 .num_ports = 1,
188 .indat_endp_flip = 0,
189 .outdat_endp_flip = 1,
190 .indat_endpoints = {0x81},
191 .outdat_endpoints = {0x01},
192 .inack_endpoints = {0x85},
193 .outcont_endpoints = {0x05},
194 .instat_endpoint = 0x87,
195 .indat_endpoint = -1,
196 .glocont_endpoint = 0x07,
197 .calculate_baud_rate = keyspan_usa19w_calc_baud,
198 .baudclk = KEYSPAN_USA18X_BAUDCLK,
199};
200
201static const struct keyspan_device_details usa19_device_details = {
202 .product_id = keyspan_usa19_product_id,
203 .msg_format = msg_usa28,
204 .num_ports = 1,
205 .indat_endp_flip = 1,
206 .outdat_endp_flip = 1,
207 .indat_endpoints = {0x81},
208 .outdat_endpoints = {0x01},
209 .inack_endpoints = {0x83},
210 .outcont_endpoints = {0x03},
211 .instat_endpoint = 0x84,
212 .indat_endpoint = -1,
213 .glocont_endpoint = -1,
214 .calculate_baud_rate = keyspan_usa19_calc_baud,
215 .baudclk = KEYSPAN_USA19_BAUDCLK,
216};
217
218static const struct keyspan_device_details usa19qi_device_details = {
219 .product_id = keyspan_usa19qi_product_id,
220 .msg_format = msg_usa28,
221 .num_ports = 1,
222 .indat_endp_flip = 1,
223 .outdat_endp_flip = 1,
224 .indat_endpoints = {0x81},
225 .outdat_endpoints = {0x01},
226 .inack_endpoints = {0x83},
227 .outcont_endpoints = {0x03},
228 .instat_endpoint = 0x84,
229 .indat_endpoint = -1,
230 .glocont_endpoint = -1,
231 .calculate_baud_rate = keyspan_usa28_calc_baud,
232 .baudclk = KEYSPAN_USA19_BAUDCLK,
233};
234
235static const struct keyspan_device_details mpr_device_details = {
236 .product_id = keyspan_mpr_product_id,
237 .msg_format = msg_usa28,
238 .num_ports = 1,
239 .indat_endp_flip = 1,
240 .outdat_endp_flip = 1,
241 .indat_endpoints = {0x81},
242 .outdat_endpoints = {0x01},
243 .inack_endpoints = {0x83},
244 .outcont_endpoints = {0x03},
245 .instat_endpoint = 0x84,
246 .indat_endpoint = -1,
247 .glocont_endpoint = -1,
248 .calculate_baud_rate = keyspan_usa28_calc_baud,
249 .baudclk = KEYSPAN_USA19_BAUDCLK,
250};
251
252static const struct keyspan_device_details usa19qw_device_details = {
253 .product_id = keyspan_usa19qw_product_id,
254 .msg_format = msg_usa26,
255 .num_ports = 1,
256 .indat_endp_flip = 0,
257 .outdat_endp_flip = 1,
258 .indat_endpoints = {0x81},
259 .outdat_endpoints = {0x01},
260 .inack_endpoints = {0x85},
261 .outcont_endpoints = {0x05},
262 .instat_endpoint = 0x87,
263 .indat_endpoint = -1,
264 .glocont_endpoint = 0x07,
265 .calculate_baud_rate = keyspan_usa19w_calc_baud,
266 .baudclk = KEYSPAN_USA19W_BAUDCLK,
267};
268
269static const struct keyspan_device_details usa19w_device_details = {
270 .product_id = keyspan_usa19w_product_id,
271 .msg_format = msg_usa26,
272 .num_ports = 1,
273 .indat_endp_flip = 0,
274 .outdat_endp_flip = 1,
275 .indat_endpoints = {0x81},
276 .outdat_endpoints = {0x01},
277 .inack_endpoints = {0x85},
278 .outcont_endpoints = {0x05},
279 .instat_endpoint = 0x87,
280 .indat_endpoint = -1,
281 .glocont_endpoint = 0x07,
282 .calculate_baud_rate = keyspan_usa19w_calc_baud,
283 .baudclk = KEYSPAN_USA19W_BAUDCLK,
284};
285
286static const struct keyspan_device_details usa19hs_device_details = {
287 .product_id = keyspan_usa19hs_product_id,
288 .msg_format = msg_usa90,
289 .num_ports = 1,
290 .indat_endp_flip = 0,
291 .outdat_endp_flip = 0,
292 .indat_endpoints = {0x81},
293 .outdat_endpoints = {0x01},
294 .inack_endpoints = {-1},
295 .outcont_endpoints = {0x02},
296 .instat_endpoint = 0x82,
297 .indat_endpoint = -1,
298 .glocont_endpoint = -1,
299 .calculate_baud_rate = keyspan_usa19hs_calc_baud,
300 .baudclk = KEYSPAN_USA19HS_BAUDCLK,
301};
302
303static const struct keyspan_device_details usa28_device_details = {
304 .product_id = keyspan_usa28_product_id,
305 .msg_format = msg_usa28,
306 .num_ports = 2,
307 .indat_endp_flip = 1,
308 .outdat_endp_flip = 1,
309 .indat_endpoints = {0x81, 0x83},
310 .outdat_endpoints = {0x01, 0x03},
311 .inack_endpoints = {0x85, 0x86},
312 .outcont_endpoints = {0x05, 0x06},
313 .instat_endpoint = 0x87,
314 .indat_endpoint = -1,
315 .glocont_endpoint = 0x07,
316 .calculate_baud_rate = keyspan_usa28_calc_baud,
317 .baudclk = KEYSPAN_USA28_BAUDCLK,
318};
319
320static const struct keyspan_device_details usa28x_device_details = {
321 .product_id = keyspan_usa28x_product_id,
322 .msg_format = msg_usa26,
323 .num_ports = 2,
324 .indat_endp_flip = 0,
325 .outdat_endp_flip = 1,
326 .indat_endpoints = {0x81, 0x83},
327 .outdat_endpoints = {0x01, 0x03},
328 .inack_endpoints = {0x85, 0x86},
329 .outcont_endpoints = {0x05, 0x06},
330 .instat_endpoint = 0x87,
331 .indat_endpoint = -1,
332 .glocont_endpoint = 0x07,
333 .calculate_baud_rate = keyspan_usa19w_calc_baud,
334 .baudclk = KEYSPAN_USA28X_BAUDCLK,
335};
336
337static const struct keyspan_device_details usa28xa_device_details = {
338 .product_id = keyspan_usa28xa_product_id,
339 .msg_format = msg_usa26,
340 .num_ports = 2,
341 .indat_endp_flip = 0,
342 .outdat_endp_flip = 1,
343 .indat_endpoints = {0x81, 0x83},
344 .outdat_endpoints = {0x01, 0x03},
345 .inack_endpoints = {0x85, 0x86},
346 .outcont_endpoints = {0x05, 0x06},
347 .instat_endpoint = 0x87,
348 .indat_endpoint = -1,
349 .glocont_endpoint = 0x07,
350 .calculate_baud_rate = keyspan_usa19w_calc_baud,
351 .baudclk = KEYSPAN_USA28X_BAUDCLK,
352};
353
354static const struct keyspan_device_details usa28xg_device_details = {
355 .product_id = keyspan_usa28xg_product_id,
356 .msg_format = msg_usa67,
357 .num_ports = 2,
358 .indat_endp_flip = 0,
359 .outdat_endp_flip = 0,
360 .indat_endpoints = {0x84, 0x88},
361 .outdat_endpoints = {0x02, 0x06},
362 .inack_endpoints = {-1, -1},
363 .outcont_endpoints = {-1, -1},
364 .instat_endpoint = 0x81,
365 .indat_endpoint = -1,
366 .glocont_endpoint = 0x01,
367 .calculate_baud_rate = keyspan_usa19w_calc_baud,
368 .baudclk = KEYSPAN_USA28X_BAUDCLK,
369};
370/*
371 * We don't need a separate entry for the usa28xb as it appears as a 28x
372 * anyway.
373 */
374
375static const struct keyspan_device_details usa49w_device_details = {
376 .product_id = keyspan_usa49w_product_id,
377 .msg_format = msg_usa49,
378 .num_ports = 4,
379 .indat_endp_flip = 0,
380 .outdat_endp_flip = 0,
381 .indat_endpoints = {0x81, 0x82, 0x83, 0x84},
382 .outdat_endpoints = {0x01, 0x02, 0x03, 0x04},
383 .inack_endpoints = {-1, -1, -1, -1},
384 .outcont_endpoints = {-1, -1, -1, -1},
385 .instat_endpoint = 0x87,
386 .indat_endpoint = -1,
387 .glocont_endpoint = 0x07,
388 .calculate_baud_rate = keyspan_usa19w_calc_baud,
389 .baudclk = KEYSPAN_USA49W_BAUDCLK,
390};
391
392static const struct keyspan_device_details usa49wlc_device_details = {
393 .product_id = keyspan_usa49wlc_product_id,
394 .msg_format = msg_usa49,
395 .num_ports = 4,
396 .indat_endp_flip = 0,
397 .outdat_endp_flip = 0,
398 .indat_endpoints = {0x81, 0x82, 0x83, 0x84},
399 .outdat_endpoints = {0x01, 0x02, 0x03, 0x04},
400 .inack_endpoints = {-1, -1, -1, -1},
401 .outcont_endpoints = {-1, -1, -1, -1},
402 .instat_endpoint = 0x87,
403 .indat_endpoint = -1,
404 .glocont_endpoint = 0x07,
405 .calculate_baud_rate = keyspan_usa19w_calc_baud,
406 .baudclk = KEYSPAN_USA19W_BAUDCLK,
407};
408
409static const struct keyspan_device_details usa49wg_device_details = {
410 .product_id = keyspan_usa49wg_product_id,
411 .msg_format = msg_usa49,
412 .num_ports = 4,
413 .indat_endp_flip = 0,
414 .outdat_endp_flip = 0,
415 .indat_endpoints = {-1, -1, -1, -1}, /* single 'global' data in EP */
416 .outdat_endpoints = {0x01, 0x02, 0x04, 0x06},
417 .inack_endpoints = {-1, -1, -1, -1},
418 .outcont_endpoints = {-1, -1, -1, -1},
419 .instat_endpoint = 0x81,
420 .indat_endpoint = 0x88,
421 .glocont_endpoint = 0x00, /* uses control EP */
422 .calculate_baud_rate = keyspan_usa19w_calc_baud,
423 .baudclk = KEYSPAN_USA19W_BAUDCLK,
424};
425
426static const struct keyspan_device_details *keyspan_devices[] = {
427 &usa18x_device_details,
428 &usa19_device_details,
429 &usa19qi_device_details,
430 &mpr_device_details,
431 &usa19qw_device_details,
432 &usa19w_device_details,
433 &usa19hs_device_details,
434 &usa28_device_details,
435 &usa28x_device_details,
436 &usa28xa_device_details,
437 &usa28xg_device_details,
438 /* 28xb not required as it renumerates as a 28x */
439 &usa49w_device_details,
440 &usa49wlc_device_details,
441 &usa49wg_device_details,
442 NULL,
443};
444
445static const struct usb_device_id keyspan_ids_combined[] = {
446 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa18x_pre_product_id) },
447 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19_pre_product_id) },
448 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19w_pre_product_id) },
449 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qi_pre_product_id) },
450 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qw_pre_product_id) },
451 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_mpr_pre_product_id) },
452 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_pre_product_id) },
453 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_pre_product_id) },
454 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_pre_product_id) },
455 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xb_pre_product_id) },
456 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49w_pre_product_id) },
457 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wlc_pre_product_id) },
458 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa18x_product_id) },
459 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19_product_id) },
460 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19w_product_id) },
461 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qi_product_id) },
462 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qw_product_id) },
463 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19hs_product_id) },
464 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_mpr_product_id) },
465 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_product_id) },
466 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_product_id) },
467 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_product_id) },
468 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xg_product_id) },
469 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49w_product_id)},
470 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wlc_product_id)},
471 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wg_product_id)},
472 { } /* Terminating entry */
473};
474
475MODULE_DEVICE_TABLE(usb, keyspan_ids_combined);
476
477/* usb_device_id table for the pre-firmware download keyspan devices */
478static const struct usb_device_id keyspan_pre_ids[] = {
479 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa18x_pre_product_id) },
480 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19_pre_product_id) },
481 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qi_pre_product_id) },
482 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qw_pre_product_id) },
483 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19w_pre_product_id) },
484 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_mpr_pre_product_id) },
485 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_pre_product_id) },
486 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_pre_product_id) },
487 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_pre_product_id) },
488 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xb_pre_product_id) },
489 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49w_pre_product_id) },
490 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wlc_pre_product_id) },
491 { } /* Terminating entry */
492};
493
494static const struct usb_device_id keyspan_1port_ids[] = {
495 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa18x_product_id) },
496 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19_product_id) },
497 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qi_product_id) },
498 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qw_product_id) },
499 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19w_product_id) },
500 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19hs_product_id) },
501 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_mpr_product_id) },
502 { } /* Terminating entry */
503};
504
505static const struct usb_device_id keyspan_2port_ids[] = {
506 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_product_id) },
507 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_product_id) },
508 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_product_id) },
509 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xg_product_id) },
510 { } /* Terminating entry */
511};
512
513static const struct usb_device_id keyspan_4port_ids[] = {
514 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49w_product_id) },
515 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wlc_product_id)},
516 { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wg_product_id)},
517 { } /* Terminating entry */
518};
519
520#define INSTAT_BUFLEN 32
521#define GLOCONT_BUFLEN 64
522#define INDAT49W_BUFLEN 512
523#define IN_BUFLEN 64
524#define OUT_BUFLEN 64
525#define INACK_BUFLEN 1
526#define OUTCONT_BUFLEN 64
527
528 /* Per device and per port private data */
529struct keyspan_serial_private {
530 const struct keyspan_device_details *device_details;
531
532 struct urb *instat_urb;
533 char *instat_buf;
534
535 /* added to support 49wg, where data from all 4 ports comes in
536 on 1 EP and high-speed supported */
537 struct urb *indat_urb;
538 char *indat_buf;
539
540 /* XXX this one probably will need a lock */
541 struct urb *glocont_urb;
542 char *glocont_buf;
543 char *ctrl_buf; /* for EP0 control message */
544};
545
546struct keyspan_port_private {
547 /* Keep track of which input & output endpoints to use */
548 int in_flip;
549 int out_flip;
550
551 /* Keep duplicate of device details in each port
552 structure as well - simplifies some of the
553 callback functions etc. */
554 const struct keyspan_device_details *device_details;
555
556 /* Input endpoints and buffer for this port */
557 struct urb *in_urbs[2];
558 char *in_buffer[2];
559 /* Output endpoints and buffer for this port */
560 struct urb *out_urbs[2];
561 char *out_buffer[2];
562
563 /* Input ack endpoint */
564 struct urb *inack_urb;
565 char *inack_buffer;
566
567 /* Output control endpoint */
568 struct urb *outcont_urb;
569 char *outcont_buffer;
570
571 /* Settings for the port */
572 int baud;
573 int old_baud;
574 unsigned int cflag;
575 unsigned int old_cflag;
576 enum {flow_none, flow_cts, flow_xon} flow_control;
577 int rts_state; /* Handshaking pins (outputs) */
578 int dtr_state;
579 int cts_state; /* Handshaking pins (inputs) */
580 int dsr_state;
581 int dcd_state;
582 int ri_state;
583 int break_on;
584
585 unsigned long tx_start_time[2];
586 int resend_cont; /* need to resend control packet */
587};
588
589/* Include Keyspan message headers. All current Keyspan Adapters
590 make use of one of five message formats which are referred
591 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
592 within this driver. */
593#include "keyspan_usa26msg.h"
594#include "keyspan_usa28msg.h"
595#include "keyspan_usa49msg.h"
596#include "keyspan_usa90msg.h"
597#include "keyspan_usa67msg.h"
598
599
600static int keyspan_break_ctl(struct tty_struct *tty, int break_state)
601{
602 struct usb_serial_port *port = tty->driver_data;
603 struct keyspan_port_private *p_priv;
604
605 p_priv = usb_get_serial_port_data(port);
606
607 if (break_state == -1)
608 p_priv->break_on = 1;
609 else
610 p_priv->break_on = 0;
611
612 /* FIXME: return errors */
613 keyspan_send_setup(port, 0);
614
615 return 0;
616}
617
618
619static void keyspan_set_termios(struct tty_struct *tty,
620 struct usb_serial_port *port,
621 const struct ktermios *old_termios)
622{
623 int baud_rate, device_port;
624 struct keyspan_port_private *p_priv;
625 const struct keyspan_device_details *d_details;
626 unsigned int cflag;
627
628 p_priv = usb_get_serial_port_data(port);
629 d_details = p_priv->device_details;
630 cflag = tty->termios.c_cflag;
631 device_port = port->port_number;
632
633 /* Baud rate calculation takes baud rate as an integer
634 so other rates can be generated if desired. */
635 baud_rate = tty_get_baud_rate(tty);
636 /* If no match or invalid, don't change */
637 if (d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
638 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
639 /* FIXME - more to do here to ensure rate changes cleanly */
640 /* FIXME - calculate exact rate from divisor ? */
641 p_priv->baud = baud_rate;
642 } else
643 baud_rate = tty_termios_baud_rate(old_termios);
644
645 tty_encode_baud_rate(tty, baud_rate, baud_rate);
646 /* set CTS/RTS handshake etc. */
647 p_priv->cflag = cflag;
648 p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
649
650 /* Mark/Space not supported */
651 tty->termios.c_cflag &= ~CMSPAR;
652
653 keyspan_send_setup(port, 0);
654}
655
656static int keyspan_tiocmget(struct tty_struct *tty)
657{
658 struct usb_serial_port *port = tty->driver_data;
659 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
660 unsigned int value;
661
662 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
663 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
664 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
665 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
666 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
667 ((p_priv->ri_state) ? TIOCM_RNG : 0);
668
669 return value;
670}
671
672static int keyspan_tiocmset(struct tty_struct *tty,
673 unsigned int set, unsigned int clear)
674{
675 struct usb_serial_port *port = tty->driver_data;
676 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
677
678 if (set & TIOCM_RTS)
679 p_priv->rts_state = 1;
680 if (set & TIOCM_DTR)
681 p_priv->dtr_state = 1;
682 if (clear & TIOCM_RTS)
683 p_priv->rts_state = 0;
684 if (clear & TIOCM_DTR)
685 p_priv->dtr_state = 0;
686 keyspan_send_setup(port, 0);
687 return 0;
688}
689
690/* Write function is similar for the four protocols used
691 with only a minor change for usa90 (usa19hs) required */
692static int keyspan_write(struct tty_struct *tty,
693 struct usb_serial_port *port, const unsigned char *buf, int count)
694{
695 struct keyspan_port_private *p_priv;
696 const struct keyspan_device_details *d_details;
697 int flip;
698 int left, todo;
699 struct urb *this_urb;
700 int err, maxDataLen, dataOffset;
701
702 p_priv = usb_get_serial_port_data(port);
703 d_details = p_priv->device_details;
704
705 if (d_details->msg_format == msg_usa90) {
706 maxDataLen = 64;
707 dataOffset = 0;
708 } else {
709 maxDataLen = 63;
710 dataOffset = 1;
711 }
712
713 dev_dbg(&port->dev, "%s - %d chars, flip=%d\n", __func__, count,
714 p_priv->out_flip);
715
716 for (left = count; left > 0; left -= todo) {
717 todo = left;
718 if (todo > maxDataLen)
719 todo = maxDataLen;
720
721 flip = p_priv->out_flip;
722
723 /* Check we have a valid urb/endpoint before we use it... */
724 this_urb = p_priv->out_urbs[flip];
725 if (this_urb == NULL) {
726 /* no bulk out, so return 0 bytes written */
727 dev_dbg(&port->dev, "%s - no output urb :(\n", __func__);
728 return count;
729 }
730
731 dev_dbg(&port->dev, "%s - endpoint %x flip %d\n",
732 __func__, usb_pipeendpoint(this_urb->pipe), flip);
733
734 if (this_urb->status == -EINPROGRESS) {
735 if (time_before(jiffies,
736 p_priv->tx_start_time[flip] + 10 * HZ))
737 break;
738 usb_unlink_urb(this_urb);
739 break;
740 }
741
742 /* First byte in buffer is "last flag" (except for usa19hx)
743 - unused so for now so set to zero */
744 ((char *)this_urb->transfer_buffer)[0] = 0;
745
746 memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
747 buf += todo;
748
749 /* send the data out the bulk port */
750 this_urb->transfer_buffer_length = todo + dataOffset;
751
752 err = usb_submit_urb(this_urb, GFP_ATOMIC);
753 if (err != 0)
754 dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed (%d)\n", err);
755 p_priv->tx_start_time[flip] = jiffies;
756
757 /* Flip for next time if usa26 or usa28 interface
758 (not used on usa49) */
759 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
760 }
761
762 return count - left;
763}
764
765static void usa26_indat_callback(struct urb *urb)
766{
767 int i, err;
768 int endpoint;
769 struct usb_serial_port *port;
770 unsigned char *data = urb->transfer_buffer;
771 int status = urb->status;
772
773 endpoint = usb_pipeendpoint(urb->pipe);
774
775 if (status) {
776 dev_dbg(&urb->dev->dev, "%s - nonzero status %d on endpoint %x\n",
777 __func__, status, endpoint);
778 return;
779 }
780
781 port = urb->context;
782 if (urb->actual_length) {
783 /* 0x80 bit is error flag */
784 if ((data[0] & 0x80) == 0) {
785 /* no errors on individual bytes, only
786 possible overrun err */
787 if (data[0] & RXERROR_OVERRUN) {
788 tty_insert_flip_char(&port->port, 0,
789 TTY_OVERRUN);
790 }
791 for (i = 1; i < urb->actual_length ; ++i)
792 tty_insert_flip_char(&port->port, data[i],
793 TTY_NORMAL);
794 } else {
795 /* some bytes had errors, every byte has status */
796 dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
797 for (i = 0; i + 1 < urb->actual_length; i += 2) {
798 int stat = data[i];
799 int flag = TTY_NORMAL;
800
801 if (stat & RXERROR_OVERRUN) {
802 tty_insert_flip_char(&port->port, 0,
803 TTY_OVERRUN);
804 }
805 /* XXX should handle break (0x10) */
806 if (stat & RXERROR_PARITY)
807 flag = TTY_PARITY;
808 else if (stat & RXERROR_FRAMING)
809 flag = TTY_FRAME;
810
811 tty_insert_flip_char(&port->port, data[i+1],
812 flag);
813 }
814 }
815 tty_flip_buffer_push(&port->port);
816 }
817
818 /* Resubmit urb so we continue receiving */
819 err = usb_submit_urb(urb, GFP_ATOMIC);
820 if (err != 0)
821 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
822}
823
824/* Outdat handling is common for all devices */
825static void usa2x_outdat_callback(struct urb *urb)
826{
827 struct usb_serial_port *port;
828 struct keyspan_port_private *p_priv;
829
830 port = urb->context;
831 p_priv = usb_get_serial_port_data(port);
832 dev_dbg(&port->dev, "%s - urb %d\n", __func__, urb == p_priv->out_urbs[1]);
833
834 usb_serial_port_softint(port);
835}
836
837static void usa26_inack_callback(struct urb *urb)
838{
839}
840
841static void usa26_outcont_callback(struct urb *urb)
842{
843 struct usb_serial_port *port;
844 struct keyspan_port_private *p_priv;
845
846 port = urb->context;
847 p_priv = usb_get_serial_port_data(port);
848
849 if (p_priv->resend_cont) {
850 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
851 keyspan_usa26_send_setup(port->serial, port,
852 p_priv->resend_cont - 1);
853 }
854}
855
856static void usa26_instat_callback(struct urb *urb)
857{
858 unsigned char *data = urb->transfer_buffer;
859 struct keyspan_usa26_portStatusMessage *msg;
860 struct usb_serial *serial;
861 struct usb_serial_port *port;
862 struct keyspan_port_private *p_priv;
863 int old_dcd_state, err;
864 int status = urb->status;
865
866 serial = urb->context;
867
868 if (status) {
869 dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
870 __func__, status);
871 return;
872 }
873 if (urb->actual_length != 9) {
874 dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
875 goto exit;
876 }
877
878 msg = (struct keyspan_usa26_portStatusMessage *)data;
879
880 /* Check port number from message and retrieve private data */
881 if (msg->port >= serial->num_ports) {
882 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
883 goto exit;
884 }
885 port = serial->port[msg->port];
886 p_priv = usb_get_serial_port_data(port);
887 if (!p_priv)
888 goto resubmit;
889
890 /* Update handshaking pin state information */
891 old_dcd_state = p_priv->dcd_state;
892 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
893 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
894 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
895 p_priv->ri_state = ((msg->ri) ? 1 : 0);
896
897 if (old_dcd_state != p_priv->dcd_state)
898 tty_port_tty_hangup(&port->port, true);
899resubmit:
900 /* Resubmit urb so we continue receiving */
901 err = usb_submit_urb(urb, GFP_ATOMIC);
902 if (err != 0)
903 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
904exit: ;
905}
906
907static void usa26_glocont_callback(struct urb *urb)
908{
909}
910
911
912static void usa28_indat_callback(struct urb *urb)
913{
914 int err;
915 struct usb_serial_port *port;
916 unsigned char *data;
917 struct keyspan_port_private *p_priv;
918 int status = urb->status;
919
920 port = urb->context;
921 p_priv = usb_get_serial_port_data(port);
922
923 if (urb != p_priv->in_urbs[p_priv->in_flip])
924 return;
925
926 do {
927 if (status) {
928 dev_dbg(&urb->dev->dev, "%s - nonzero status %d on endpoint %x\n",
929 __func__, status, usb_pipeendpoint(urb->pipe));
930 return;
931 }
932
933 port = urb->context;
934 p_priv = usb_get_serial_port_data(port);
935 data = urb->transfer_buffer;
936
937 if (urb->actual_length) {
938 tty_insert_flip_string(&port->port, data,
939 urb->actual_length);
940 tty_flip_buffer_push(&port->port);
941 }
942
943 /* Resubmit urb so we continue receiving */
944 err = usb_submit_urb(urb, GFP_ATOMIC);
945 if (err != 0)
946 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n",
947 __func__, err);
948 p_priv->in_flip ^= 1;
949
950 urb = p_priv->in_urbs[p_priv->in_flip];
951 } while (urb->status != -EINPROGRESS);
952}
953
954static void usa28_inack_callback(struct urb *urb)
955{
956}
957
958static void usa28_outcont_callback(struct urb *urb)
959{
960 struct usb_serial_port *port;
961 struct keyspan_port_private *p_priv;
962
963 port = urb->context;
964 p_priv = usb_get_serial_port_data(port);
965
966 if (p_priv->resend_cont) {
967 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
968 keyspan_usa28_send_setup(port->serial, port,
969 p_priv->resend_cont - 1);
970 }
971}
972
973static void usa28_instat_callback(struct urb *urb)
974{
975 int err;
976 unsigned char *data = urb->transfer_buffer;
977 struct keyspan_usa28_portStatusMessage *msg;
978 struct usb_serial *serial;
979 struct usb_serial_port *port;
980 struct keyspan_port_private *p_priv;
981 int old_dcd_state;
982 int status = urb->status;
983
984 serial = urb->context;
985
986 if (status) {
987 dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
988 __func__, status);
989 return;
990 }
991
992 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
993 dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
994 goto exit;
995 }
996
997 msg = (struct keyspan_usa28_portStatusMessage *)data;
998
999 /* Check port number from message and retrieve private data */
1000 if (msg->port >= serial->num_ports) {
1001 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
1002 goto exit;
1003 }
1004 port = serial->port[msg->port];
1005 p_priv = usb_get_serial_port_data(port);
1006 if (!p_priv)
1007 goto resubmit;
1008
1009 /* Update handshaking pin state information */
1010 old_dcd_state = p_priv->dcd_state;
1011 p_priv->cts_state = ((msg->cts) ? 1 : 0);
1012 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1013 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1014 p_priv->ri_state = ((msg->ri) ? 1 : 0);
1015
1016 if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
1017 tty_port_tty_hangup(&port->port, true);
1018resubmit:
1019 /* Resubmit urb so we continue receiving */
1020 err = usb_submit_urb(urb, GFP_ATOMIC);
1021 if (err != 0)
1022 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
1023exit: ;
1024}
1025
1026static void usa28_glocont_callback(struct urb *urb)
1027{
1028}
1029
1030
1031static void usa49_glocont_callback(struct urb *urb)
1032{
1033 struct usb_serial *serial;
1034 struct usb_serial_port *port;
1035 struct keyspan_port_private *p_priv;
1036 int i;
1037
1038 serial = urb->context;
1039 for (i = 0; i < serial->num_ports; ++i) {
1040 port = serial->port[i];
1041 p_priv = usb_get_serial_port_data(port);
1042 if (!p_priv)
1043 continue;
1044
1045 if (p_priv->resend_cont) {
1046 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
1047 keyspan_usa49_send_setup(serial, port,
1048 p_priv->resend_cont - 1);
1049 break;
1050 }
1051 }
1052}
1053
1054 /* This is actually called glostat in the Keyspan
1055 doco */
1056static void usa49_instat_callback(struct urb *urb)
1057{
1058 int err;
1059 unsigned char *data = urb->transfer_buffer;
1060 struct keyspan_usa49_portStatusMessage *msg;
1061 struct usb_serial *serial;
1062 struct usb_serial_port *port;
1063 struct keyspan_port_private *p_priv;
1064 int old_dcd_state;
1065 int status = urb->status;
1066
1067 serial = urb->context;
1068
1069 if (status) {
1070 dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
1071 __func__, status);
1072 return;
1073 }
1074
1075 if (urb->actual_length !=
1076 sizeof(struct keyspan_usa49_portStatusMessage)) {
1077 dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
1078 goto exit;
1079 }
1080
1081 msg = (struct keyspan_usa49_portStatusMessage *)data;
1082
1083 /* Check port number from message and retrieve private data */
1084 if (msg->portNumber >= serial->num_ports) {
1085 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
1086 __func__, msg->portNumber);
1087 goto exit;
1088 }
1089 port = serial->port[msg->portNumber];
1090 p_priv = usb_get_serial_port_data(port);
1091 if (!p_priv)
1092 goto resubmit;
1093
1094 /* Update handshaking pin state information */
1095 old_dcd_state = p_priv->dcd_state;
1096 p_priv->cts_state = ((msg->cts) ? 1 : 0);
1097 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1098 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1099 p_priv->ri_state = ((msg->ri) ? 1 : 0);
1100
1101 if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
1102 tty_port_tty_hangup(&port->port, true);
1103resubmit:
1104 /* Resubmit urb so we continue receiving */
1105 err = usb_submit_urb(urb, GFP_ATOMIC);
1106 if (err != 0)
1107 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
1108exit: ;
1109}
1110
1111static void usa49_inack_callback(struct urb *urb)
1112{
1113}
1114
1115static void usa49_indat_callback(struct urb *urb)
1116{
1117 int i, err;
1118 int endpoint;
1119 struct usb_serial_port *port;
1120 unsigned char *data = urb->transfer_buffer;
1121 int status = urb->status;
1122
1123 endpoint = usb_pipeendpoint(urb->pipe);
1124
1125 if (status) {
1126 dev_dbg(&urb->dev->dev, "%s - nonzero status %d on endpoint %x\n",
1127 __func__, status, endpoint);
1128 return;
1129 }
1130
1131 port = urb->context;
1132 if (urb->actual_length) {
1133 /* 0x80 bit is error flag */
1134 if ((data[0] & 0x80) == 0) {
1135 /* no error on any byte */
1136 tty_insert_flip_string(&port->port, data + 1,
1137 urb->actual_length - 1);
1138 } else {
1139 /* some bytes had errors, every byte has status */
1140 for (i = 0; i + 1 < urb->actual_length; i += 2) {
1141 int stat = data[i];
1142 int flag = TTY_NORMAL;
1143
1144 if (stat & RXERROR_OVERRUN) {
1145 tty_insert_flip_char(&port->port, 0,
1146 TTY_OVERRUN);
1147 }
1148 /* XXX should handle break (0x10) */
1149 if (stat & RXERROR_PARITY)
1150 flag = TTY_PARITY;
1151 else if (stat & RXERROR_FRAMING)
1152 flag = TTY_FRAME;
1153
1154 tty_insert_flip_char(&port->port, data[i+1],
1155 flag);
1156 }
1157 }
1158 tty_flip_buffer_push(&port->port);
1159 }
1160
1161 /* Resubmit urb so we continue receiving */
1162 err = usb_submit_urb(urb, GFP_ATOMIC);
1163 if (err != 0)
1164 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
1165}
1166
1167static void usa49wg_indat_callback(struct urb *urb)
1168{
1169 int i, len, x, err;
1170 struct usb_serial *serial;
1171 struct usb_serial_port *port;
1172 unsigned char *data = urb->transfer_buffer;
1173 int status = urb->status;
1174
1175 serial = urb->context;
1176
1177 if (status) {
1178 dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
1179 __func__, status);
1180 return;
1181 }
1182
1183 /* inbound data is in the form P#, len, status, data */
1184 i = 0;
1185 len = 0;
1186
1187 while (i < urb->actual_length) {
1188 if (urb->actual_length - i < 3) {
1189 dev_warn_ratelimited(&urb->dev->dev, "malformed indat packet\n");
1190 break;
1191 }
1192
1193 /* Check port number from message */
1194 if (data[i] >= serial->num_ports) {
1195 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
1196 __func__, data[i]);
1197 return;
1198 }
1199 port = serial->port[data[i++]];
1200 len = data[i++];
1201
1202 /* 0x80 bit is error flag */
1203 if ((data[i] & 0x80) == 0) {
1204 /* no error on any byte */
1205 i++;
1206 for (x = 1; x < len && i < urb->actual_length; ++x)
1207 tty_insert_flip_char(&port->port,
1208 data[i++], 0);
1209 } else {
1210 /*
1211 * some bytes had errors, every byte has status
1212 */
1213 for (x = 0; x + 1 < len &&
1214 i + 1 < urb->actual_length; x += 2) {
1215 int stat = data[i];
1216 int flag = TTY_NORMAL;
1217
1218 if (stat & RXERROR_OVERRUN) {
1219 tty_insert_flip_char(&port->port, 0,
1220 TTY_OVERRUN);
1221 }
1222 /* XXX should handle break (0x10) */
1223 if (stat & RXERROR_PARITY)
1224 flag = TTY_PARITY;
1225 else if (stat & RXERROR_FRAMING)
1226 flag = TTY_FRAME;
1227
1228 tty_insert_flip_char(&port->port, data[i+1],
1229 flag);
1230 i += 2;
1231 }
1232 }
1233 tty_flip_buffer_push(&port->port);
1234 }
1235
1236 /* Resubmit urb so we continue receiving */
1237 err = usb_submit_urb(urb, GFP_ATOMIC);
1238 if (err != 0)
1239 dev_dbg(&urb->dev->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
1240}
1241
1242/* not used, usa-49 doesn't have per-port control endpoints */
1243static void usa49_outcont_callback(struct urb *urb)
1244{
1245}
1246
1247static void usa90_indat_callback(struct urb *urb)
1248{
1249 int i, err;
1250 int endpoint;
1251 struct usb_serial_port *port;
1252 struct keyspan_port_private *p_priv;
1253 unsigned char *data = urb->transfer_buffer;
1254 int status = urb->status;
1255
1256 endpoint = usb_pipeendpoint(urb->pipe);
1257
1258 if (status) {
1259 dev_dbg(&urb->dev->dev, "%s - nonzero status %d on endpoint %x\n",
1260 __func__, status, endpoint);
1261 return;
1262 }
1263
1264 port = urb->context;
1265 p_priv = usb_get_serial_port_data(port);
1266
1267 if (urb->actual_length) {
1268 /* if current mode is DMA, looks like usa28 format
1269 otherwise looks like usa26 data format */
1270
1271 if (p_priv->baud > 57600)
1272 tty_insert_flip_string(&port->port, data,
1273 urb->actual_length);
1274 else {
1275 /* 0x80 bit is error flag */
1276 if ((data[0] & 0x80) == 0) {
1277 /* no errors on individual bytes, only
1278 possible overrun err*/
1279 if (data[0] & RXERROR_OVERRUN) {
1280 tty_insert_flip_char(&port->port, 0,
1281 TTY_OVERRUN);
1282 }
1283 for (i = 1; i < urb->actual_length ; ++i)
1284 tty_insert_flip_char(&port->port,
1285 data[i], TTY_NORMAL);
1286 } else {
1287 /* some bytes had errors, every byte has status */
1288 dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
1289 for (i = 0; i + 1 < urb->actual_length; i += 2) {
1290 int stat = data[i];
1291 int flag = TTY_NORMAL;
1292
1293 if (stat & RXERROR_OVERRUN) {
1294 tty_insert_flip_char(
1295 &port->port, 0,
1296 TTY_OVERRUN);
1297 }
1298 /* XXX should handle break (0x10) */
1299 if (stat & RXERROR_PARITY)
1300 flag = TTY_PARITY;
1301 else if (stat & RXERROR_FRAMING)
1302 flag = TTY_FRAME;
1303
1304 tty_insert_flip_char(&port->port,
1305 data[i+1], flag);
1306 }
1307 }
1308 }
1309 tty_flip_buffer_push(&port->port);
1310 }
1311
1312 /* Resubmit urb so we continue receiving */
1313 err = usb_submit_urb(urb, GFP_ATOMIC);
1314 if (err != 0)
1315 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
1316}
1317
1318
1319static void usa90_instat_callback(struct urb *urb)
1320{
1321 unsigned char *data = urb->transfer_buffer;
1322 struct keyspan_usa90_portStatusMessage *msg;
1323 struct usb_serial *serial;
1324 struct usb_serial_port *port;
1325 struct keyspan_port_private *p_priv;
1326 int old_dcd_state, err;
1327 int status = urb->status;
1328
1329 serial = urb->context;
1330
1331 if (status) {
1332 dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
1333 __func__, status);
1334 return;
1335 }
1336 if (urb->actual_length < 14) {
1337 dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
1338 goto exit;
1339 }
1340
1341 msg = (struct keyspan_usa90_portStatusMessage *)data;
1342
1343 /* Now do something useful with the data */
1344
1345 port = serial->port[0];
1346 p_priv = usb_get_serial_port_data(port);
1347 if (!p_priv)
1348 goto resubmit;
1349
1350 /* Update handshaking pin state information */
1351 old_dcd_state = p_priv->dcd_state;
1352 p_priv->cts_state = ((msg->cts) ? 1 : 0);
1353 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1354 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1355 p_priv->ri_state = ((msg->ri) ? 1 : 0);
1356
1357 if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
1358 tty_port_tty_hangup(&port->port, true);
1359resubmit:
1360 /* Resubmit urb so we continue receiving */
1361 err = usb_submit_urb(urb, GFP_ATOMIC);
1362 if (err != 0)
1363 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
1364exit:
1365 ;
1366}
1367
1368static void usa90_outcont_callback(struct urb *urb)
1369{
1370 struct usb_serial_port *port;
1371 struct keyspan_port_private *p_priv;
1372
1373 port = urb->context;
1374 p_priv = usb_get_serial_port_data(port);
1375
1376 if (p_priv->resend_cont) {
1377 dev_dbg(&urb->dev->dev, "%s - sending setup\n", __func__);
1378 keyspan_usa90_send_setup(port->serial, port,
1379 p_priv->resend_cont - 1);
1380 }
1381}
1382
1383/* Status messages from the 28xg */
1384static void usa67_instat_callback(struct urb *urb)
1385{
1386 int err;
1387 unsigned char *data = urb->transfer_buffer;
1388 struct keyspan_usa67_portStatusMessage *msg;
1389 struct usb_serial *serial;
1390 struct usb_serial_port *port;
1391 struct keyspan_port_private *p_priv;
1392 int old_dcd_state;
1393 int status = urb->status;
1394
1395 serial = urb->context;
1396
1397 if (status) {
1398 dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
1399 __func__, status);
1400 return;
1401 }
1402
1403 if (urb->actual_length !=
1404 sizeof(struct keyspan_usa67_portStatusMessage)) {
1405 dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
1406 return;
1407 }
1408
1409
1410 /* Now do something useful with the data */
1411 msg = (struct keyspan_usa67_portStatusMessage *)data;
1412
1413 /* Check port number from message and retrieve private data */
1414 if (msg->port >= serial->num_ports) {
1415 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
1416 return;
1417 }
1418
1419 port = serial->port[msg->port];
1420 p_priv = usb_get_serial_port_data(port);
1421 if (!p_priv)
1422 goto resubmit;
1423
1424 /* Update handshaking pin state information */
1425 old_dcd_state = p_priv->dcd_state;
1426 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1427 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1428
1429 if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
1430 tty_port_tty_hangup(&port->port, true);
1431resubmit:
1432 /* Resubmit urb so we continue receiving */
1433 err = usb_submit_urb(urb, GFP_ATOMIC);
1434 if (err != 0)
1435 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
1436}
1437
1438static void usa67_glocont_callback(struct urb *urb)
1439{
1440 struct usb_serial *serial;
1441 struct usb_serial_port *port;
1442 struct keyspan_port_private *p_priv;
1443 int i;
1444
1445 serial = urb->context;
1446 for (i = 0; i < serial->num_ports; ++i) {
1447 port = serial->port[i];
1448 p_priv = usb_get_serial_port_data(port);
1449 if (!p_priv)
1450 continue;
1451
1452 if (p_priv->resend_cont) {
1453 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
1454 keyspan_usa67_send_setup(serial, port,
1455 p_priv->resend_cont - 1);
1456 break;
1457 }
1458 }
1459}
1460
1461static unsigned int keyspan_write_room(struct tty_struct *tty)
1462{
1463 struct usb_serial_port *port = tty->driver_data;
1464 struct keyspan_port_private *p_priv;
1465 const struct keyspan_device_details *d_details;
1466 int flip;
1467 unsigned int data_len;
1468 struct urb *this_urb;
1469
1470 p_priv = usb_get_serial_port_data(port);
1471 d_details = p_priv->device_details;
1472
1473 /* FIXME: locking */
1474 if (d_details->msg_format == msg_usa90)
1475 data_len = 64;
1476 else
1477 data_len = 63;
1478
1479 flip = p_priv->out_flip;
1480
1481 /* Check both endpoints to see if any are available. */
1482 this_urb = p_priv->out_urbs[flip];
1483 if (this_urb != NULL) {
1484 if (this_urb->status != -EINPROGRESS)
1485 return data_len;
1486 flip = (flip + 1) & d_details->outdat_endp_flip;
1487 this_urb = p_priv->out_urbs[flip];
1488 if (this_urb != NULL) {
1489 if (this_urb->status != -EINPROGRESS)
1490 return data_len;
1491 }
1492 }
1493 return 0;
1494}
1495
1496
1497static int keyspan_open(struct tty_struct *tty, struct usb_serial_port *port)
1498{
1499 struct keyspan_port_private *p_priv;
1500 const struct keyspan_device_details *d_details;
1501 int i, err;
1502 int baud_rate, device_port;
1503 struct urb *urb;
1504 unsigned int cflag = 0;
1505
1506 p_priv = usb_get_serial_port_data(port);
1507 d_details = p_priv->device_details;
1508
1509 /* Set some sane defaults */
1510 p_priv->rts_state = 1;
1511 p_priv->dtr_state = 1;
1512 p_priv->baud = 9600;
1513
1514 /* force baud and lcr to be set on open */
1515 p_priv->old_baud = 0;
1516 p_priv->old_cflag = 0;
1517
1518 p_priv->out_flip = 0;
1519 p_priv->in_flip = 0;
1520
1521 /* Reset low level data toggle and start reading from endpoints */
1522 for (i = 0; i < 2; i++) {
1523 urb = p_priv->in_urbs[i];
1524 if (urb == NULL)
1525 continue;
1526
1527 /* make sure endpoint data toggle is synchronized
1528 with the device */
1529 usb_clear_halt(urb->dev, urb->pipe);
1530 err = usb_submit_urb(urb, GFP_KERNEL);
1531 if (err != 0)
1532 dev_dbg(&port->dev, "%s - submit urb %d failed (%d)\n", __func__, i, err);
1533 }
1534
1535 /* Reset low level data toggle on out endpoints */
1536 for (i = 0; i < 2; i++) {
1537 urb = p_priv->out_urbs[i];
1538 if (urb == NULL)
1539 continue;
1540 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1541 usb_pipeout(urb->pipe), 0); */
1542 }
1543
1544 /* get the terminal config for the setup message now so we don't
1545 * need to send 2 of them */
1546
1547 device_port = port->port_number;
1548 if (tty) {
1549 cflag = tty->termios.c_cflag;
1550 /* Baud rate calculation takes baud rate as an integer
1551 so other rates can be generated if desired. */
1552 baud_rate = tty_get_baud_rate(tty);
1553 /* If no match or invalid, leave as default */
1554 if (baud_rate >= 0
1555 && d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
1556 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1557 p_priv->baud = baud_rate;
1558 }
1559 }
1560 /* set CTS/RTS handshake etc. */
1561 p_priv->cflag = cflag;
1562 p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
1563
1564 keyspan_send_setup(port, 1);
1565 /* mdelay(100); */
1566 /* keyspan_set_termios(port, NULL); */
1567
1568 return 0;
1569}
1570
1571static void keyspan_dtr_rts(struct usb_serial_port *port, int on)
1572{
1573 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
1574
1575 p_priv->rts_state = on;
1576 p_priv->dtr_state = on;
1577 keyspan_send_setup(port, 0);
1578}
1579
1580static void keyspan_close(struct usb_serial_port *port)
1581{
1582 int i;
1583 struct keyspan_port_private *p_priv;
1584
1585 p_priv = usb_get_serial_port_data(port);
1586
1587 p_priv->rts_state = 0;
1588 p_priv->dtr_state = 0;
1589
1590 keyspan_send_setup(port, 2);
1591 /* pilot-xfer seems to work best with this delay */
1592 mdelay(100);
1593
1594 p_priv->out_flip = 0;
1595 p_priv->in_flip = 0;
1596
1597 usb_kill_urb(p_priv->inack_urb);
1598 for (i = 0; i < 2; i++) {
1599 usb_kill_urb(p_priv->in_urbs[i]);
1600 usb_kill_urb(p_priv->out_urbs[i]);
1601 }
1602}
1603
1604/* download the firmware to a pre-renumeration device */
1605static int keyspan_fake_startup(struct usb_serial *serial)
1606{
1607 char *fw_name;
1608
1609 dev_dbg(&serial->dev->dev, "Keyspan startup version %04x product %04x\n",
1610 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1611 le16_to_cpu(serial->dev->descriptor.idProduct));
1612
1613 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
1614 != 0x8000) {
1615 dev_dbg(&serial->dev->dev, "Firmware already loaded. Quitting.\n");
1616 return 1;
1617 }
1618
1619 /* Select firmware image on the basis of idProduct */
1620 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1621 case keyspan_usa28_pre_product_id:
1622 fw_name = "keyspan/usa28.fw";
1623 break;
1624
1625 case keyspan_usa28x_pre_product_id:
1626 fw_name = "keyspan/usa28x.fw";
1627 break;
1628
1629 case keyspan_usa28xa_pre_product_id:
1630 fw_name = "keyspan/usa28xa.fw";
1631 break;
1632
1633 case keyspan_usa28xb_pre_product_id:
1634 fw_name = "keyspan/usa28xb.fw";
1635 break;
1636
1637 case keyspan_usa19_pre_product_id:
1638 fw_name = "keyspan/usa19.fw";
1639 break;
1640
1641 case keyspan_usa19qi_pre_product_id:
1642 fw_name = "keyspan/usa19qi.fw";
1643 break;
1644
1645 case keyspan_mpr_pre_product_id:
1646 fw_name = "keyspan/mpr.fw";
1647 break;
1648
1649 case keyspan_usa19qw_pre_product_id:
1650 fw_name = "keyspan/usa19qw.fw";
1651 break;
1652
1653 case keyspan_usa18x_pre_product_id:
1654 fw_name = "keyspan/usa18x.fw";
1655 break;
1656
1657 case keyspan_usa19w_pre_product_id:
1658 fw_name = "keyspan/usa19w.fw";
1659 break;
1660
1661 case keyspan_usa49w_pre_product_id:
1662 fw_name = "keyspan/usa49w.fw";
1663 break;
1664
1665 case keyspan_usa49wlc_pre_product_id:
1666 fw_name = "keyspan/usa49wlc.fw";
1667 break;
1668
1669 default:
1670 dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
1671 le16_to_cpu(serial->dev->descriptor.idProduct));
1672 return 1;
1673 }
1674
1675 dev_dbg(&serial->dev->dev, "Uploading Keyspan %s firmware.\n", fw_name);
1676
1677 if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
1678 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
1679 fw_name);
1680 return -ENOENT;
1681 }
1682
1683 /* after downloading firmware Renumeration will occur in a
1684 moment and the new device will bind to the real driver */
1685
1686 /* we don't want this device to have a driver assigned to it. */
1687 return 1;
1688}
1689
1690/* Helper functions used by keyspan_setup_urbs */
1691static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1692 int endpoint)
1693{
1694 struct usb_host_interface *iface_desc;
1695 struct usb_endpoint_descriptor *ep;
1696 int i;
1697
1698 iface_desc = serial->interface->cur_altsetting;
1699 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1700 ep = &iface_desc->endpoint[i].desc;
1701 if (ep->bEndpointAddress == endpoint)
1702 return ep;
1703 }
1704 dev_warn(&serial->interface->dev, "found no endpoint descriptor for endpoint %x\n",
1705 endpoint);
1706 return NULL;
1707}
1708
1709static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
1710 int dir, void *ctx, char *buf, int len,
1711 void (*callback)(struct urb *))
1712{
1713 struct urb *urb;
1714 struct usb_endpoint_descriptor const *ep_desc;
1715 char const *ep_type_name;
1716
1717 if (endpoint == -1)
1718 return NULL; /* endpoint not needed */
1719
1720 dev_dbg(&serial->interface->dev, "%s - alloc for endpoint %x\n",
1721 __func__, endpoint);
1722 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1723 if (!urb)
1724 return NULL;
1725
1726 if (endpoint == 0) {
1727 /* control EP filled in when used */
1728 return urb;
1729 }
1730
1731 ep_desc = find_ep(serial, endpoint);
1732 if (!ep_desc) {
1733 usb_free_urb(urb);
1734 return NULL;
1735 }
1736 if (usb_endpoint_xfer_int(ep_desc)) {
1737 ep_type_name = "INT";
1738 usb_fill_int_urb(urb, serial->dev,
1739 usb_sndintpipe(serial->dev, endpoint) | dir,
1740 buf, len, callback, ctx,
1741 ep_desc->bInterval);
1742 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1743 ep_type_name = "BULK";
1744 usb_fill_bulk_urb(urb, serial->dev,
1745 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1746 buf, len, callback, ctx);
1747 } else {
1748 dev_warn(&serial->interface->dev,
1749 "unsupported endpoint type %x\n",
1750 usb_endpoint_type(ep_desc));
1751 usb_free_urb(urb);
1752 return NULL;
1753 }
1754
1755 dev_dbg(&serial->interface->dev, "%s - using urb %p for %s endpoint %x\n",
1756 __func__, urb, ep_type_name, endpoint);
1757 return urb;
1758}
1759
1760static struct callbacks {
1761 void (*instat_callback)(struct urb *);
1762 void (*glocont_callback)(struct urb *);
1763 void (*indat_callback)(struct urb *);
1764 void (*outdat_callback)(struct urb *);
1765 void (*inack_callback)(struct urb *);
1766 void (*outcont_callback)(struct urb *);
1767} keyspan_callbacks[] = {
1768 {
1769 /* msg_usa26 callbacks */
1770 .instat_callback = usa26_instat_callback,
1771 .glocont_callback = usa26_glocont_callback,
1772 .indat_callback = usa26_indat_callback,
1773 .outdat_callback = usa2x_outdat_callback,
1774 .inack_callback = usa26_inack_callback,
1775 .outcont_callback = usa26_outcont_callback,
1776 }, {
1777 /* msg_usa28 callbacks */
1778 .instat_callback = usa28_instat_callback,
1779 .glocont_callback = usa28_glocont_callback,
1780 .indat_callback = usa28_indat_callback,
1781 .outdat_callback = usa2x_outdat_callback,
1782 .inack_callback = usa28_inack_callback,
1783 .outcont_callback = usa28_outcont_callback,
1784 }, {
1785 /* msg_usa49 callbacks */
1786 .instat_callback = usa49_instat_callback,
1787 .glocont_callback = usa49_glocont_callback,
1788 .indat_callback = usa49_indat_callback,
1789 .outdat_callback = usa2x_outdat_callback,
1790 .inack_callback = usa49_inack_callback,
1791 .outcont_callback = usa49_outcont_callback,
1792 }, {
1793 /* msg_usa90 callbacks */
1794 .instat_callback = usa90_instat_callback,
1795 .glocont_callback = usa28_glocont_callback,
1796 .indat_callback = usa90_indat_callback,
1797 .outdat_callback = usa2x_outdat_callback,
1798 .inack_callback = usa28_inack_callback,
1799 .outcont_callback = usa90_outcont_callback,
1800 }, {
1801 /* msg_usa67 callbacks */
1802 .instat_callback = usa67_instat_callback,
1803 .glocont_callback = usa67_glocont_callback,
1804 .indat_callback = usa26_indat_callback,
1805 .outdat_callback = usa2x_outdat_callback,
1806 .inack_callback = usa26_inack_callback,
1807 .outcont_callback = usa26_outcont_callback,
1808 }
1809};
1810
1811 /* Generic setup urbs function that uses
1812 data in device_details */
1813static void keyspan_setup_urbs(struct usb_serial *serial)
1814{
1815 struct keyspan_serial_private *s_priv;
1816 const struct keyspan_device_details *d_details;
1817 struct callbacks *cback;
1818
1819 s_priv = usb_get_serial_data(serial);
1820 d_details = s_priv->device_details;
1821
1822 /* Setup values for the various callback routines */
1823 cback = &keyspan_callbacks[d_details->msg_format];
1824
1825 /* Allocate and set up urbs for each one that is in use,
1826 starting with instat endpoints */
1827 s_priv->instat_urb = keyspan_setup_urb
1828 (serial, d_details->instat_endpoint, USB_DIR_IN,
1829 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1830 cback->instat_callback);
1831
1832 s_priv->indat_urb = keyspan_setup_urb
1833 (serial, d_details->indat_endpoint, USB_DIR_IN,
1834 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1835 usa49wg_indat_callback);
1836
1837 s_priv->glocont_urb = keyspan_setup_urb
1838 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1839 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1840 cback->glocont_callback);
1841}
1842
1843/* usa19 function doesn't require prescaler */
1844static int keyspan_usa19_calc_baud(struct usb_serial_port *port,
1845 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1846 u8 *rate_low, u8 *prescaler, int portnum)
1847{
1848 u32 b16, /* baud rate times 16 (actual rate used internally) */
1849 div, /* divisor */
1850 cnt; /* inverse of divisor (programmed into 8051) */
1851
1852 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1853
1854 /* prevent divide by zero... */
1855 b16 = baud_rate * 16L;
1856 if (b16 == 0)
1857 return KEYSPAN_INVALID_BAUD_RATE;
1858 /* Any "standard" rate over 57k6 is marginal on the USA-19
1859 as we run out of divisor resolution. */
1860 if (baud_rate > 57600)
1861 return KEYSPAN_INVALID_BAUD_RATE;
1862
1863 /* calculate the divisor and the counter (its inverse) */
1864 div = baudclk / b16;
1865 if (div == 0)
1866 return KEYSPAN_INVALID_BAUD_RATE;
1867 else
1868 cnt = 0 - div;
1869
1870 if (div > 0xffff)
1871 return KEYSPAN_INVALID_BAUD_RATE;
1872
1873 /* return the counter values if non-null */
1874 if (rate_low)
1875 *rate_low = (u8) (cnt & 0xff);
1876 if (rate_hi)
1877 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1878 if (rate_low && rate_hi)
1879 dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
1880 __func__, baud_rate, *rate_hi, *rate_low);
1881 return KEYSPAN_BAUD_RATE_OK;
1882}
1883
1884/* usa19hs function doesn't require prescaler */
1885static int keyspan_usa19hs_calc_baud(struct usb_serial_port *port,
1886 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1887 u8 *rate_low, u8 *prescaler, int portnum)
1888{
1889 u32 b16, /* baud rate times 16 (actual rate used internally) */
1890 div; /* divisor */
1891
1892 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1893
1894 /* prevent divide by zero... */
1895 b16 = baud_rate * 16L;
1896 if (b16 == 0)
1897 return KEYSPAN_INVALID_BAUD_RATE;
1898
1899 /* calculate the divisor */
1900 div = baudclk / b16;
1901 if (div == 0)
1902 return KEYSPAN_INVALID_BAUD_RATE;
1903
1904 if (div > 0xffff)
1905 return KEYSPAN_INVALID_BAUD_RATE;
1906
1907 /* return the counter values if non-null */
1908 if (rate_low)
1909 *rate_low = (u8) (div & 0xff);
1910
1911 if (rate_hi)
1912 *rate_hi = (u8) ((div >> 8) & 0xff);
1913
1914 if (rate_low && rate_hi)
1915 dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
1916 __func__, baud_rate, *rate_hi, *rate_low);
1917
1918 return KEYSPAN_BAUD_RATE_OK;
1919}
1920
1921static int keyspan_usa19w_calc_baud(struct usb_serial_port *port,
1922 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1923 u8 *rate_low, u8 *prescaler, int portnum)
1924{
1925 u32 b16, /* baud rate times 16 (actual rate used internally) */
1926 clk, /* clock with 13/8 prescaler */
1927 div, /* divisor using 13/8 prescaler */
1928 res, /* resulting baud rate using 13/8 prescaler */
1929 diff, /* error using 13/8 prescaler */
1930 smallest_diff;
1931 u8 best_prescaler;
1932 int i;
1933
1934 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1935
1936 /* prevent divide by zero */
1937 b16 = baud_rate * 16L;
1938 if (b16 == 0)
1939 return KEYSPAN_INVALID_BAUD_RATE;
1940
1941 /* Calculate prescaler by trying them all and looking
1942 for best fit */
1943
1944 /* start with largest possible difference */
1945 smallest_diff = 0xffffffff;
1946
1947 /* 0 is an invalid prescaler, used as a flag */
1948 best_prescaler = 0;
1949
1950 for (i = 8; i <= 0xff; ++i) {
1951 clk = (baudclk * 8) / (u32) i;
1952
1953 div = clk / b16;
1954 if (div == 0)
1955 continue;
1956
1957 res = clk / div;
1958 diff = (res > b16) ? (res-b16) : (b16-res);
1959
1960 if (diff < smallest_diff) {
1961 best_prescaler = i;
1962 smallest_diff = diff;
1963 }
1964 }
1965
1966 if (best_prescaler == 0)
1967 return KEYSPAN_INVALID_BAUD_RATE;
1968
1969 clk = (baudclk * 8) / (u32) best_prescaler;
1970 div = clk / b16;
1971
1972 /* return the divisor and prescaler if non-null */
1973 if (rate_low)
1974 *rate_low = (u8) (div & 0xff);
1975 if (rate_hi)
1976 *rate_hi = (u8) ((div >> 8) & 0xff);
1977 if (prescaler) {
1978 *prescaler = best_prescaler;
1979 /* dev_dbg(&port->dev, "%s - %d %d\n", __func__, *prescaler, div); */
1980 }
1981 return KEYSPAN_BAUD_RATE_OK;
1982}
1983
1984 /* USA-28 supports different maximum baud rates on each port */
1985static int keyspan_usa28_calc_baud(struct usb_serial_port *port,
1986 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1987 u8 *rate_low, u8 *prescaler, int portnum)
1988{
1989 u32 b16, /* baud rate times 16 (actual rate used internally) */
1990 div, /* divisor */
1991 cnt; /* inverse of divisor (programmed into 8051) */
1992
1993 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1994
1995 /* prevent divide by zero */
1996 b16 = baud_rate * 16L;
1997 if (b16 == 0)
1998 return KEYSPAN_INVALID_BAUD_RATE;
1999
2000 /* calculate the divisor and the counter (its inverse) */
2001 div = KEYSPAN_USA28_BAUDCLK / b16;
2002 if (div == 0)
2003 return KEYSPAN_INVALID_BAUD_RATE;
2004 else
2005 cnt = 0 - div;
2006
2007 /* check for out of range, based on portnum,
2008 and return result */
2009 if (portnum == 0) {
2010 if (div > 0xffff)
2011 return KEYSPAN_INVALID_BAUD_RATE;
2012 } else {
2013 if (portnum == 1) {
2014 if (div > 0xff)
2015 return KEYSPAN_INVALID_BAUD_RATE;
2016 } else
2017 return KEYSPAN_INVALID_BAUD_RATE;
2018 }
2019
2020 /* return the counter values if not NULL
2021 (port 1 will ignore retHi) */
2022 if (rate_low)
2023 *rate_low = (u8) (cnt & 0xff);
2024 if (rate_hi)
2025 *rate_hi = (u8) ((cnt >> 8) & 0xff);
2026 dev_dbg(&port->dev, "%s - %d OK.\n", __func__, baud_rate);
2027 return KEYSPAN_BAUD_RATE_OK;
2028}
2029
2030static int keyspan_usa26_send_setup(struct usb_serial *serial,
2031 struct usb_serial_port *port,
2032 int reset_port)
2033{
2034 struct keyspan_usa26_portControlMessage msg;
2035 struct keyspan_serial_private *s_priv;
2036 struct keyspan_port_private *p_priv;
2037 const struct keyspan_device_details *d_details;
2038 struct urb *this_urb;
2039 int device_port, err;
2040
2041 dev_dbg(&port->dev, "%s reset=%d\n", __func__, reset_port);
2042
2043 s_priv = usb_get_serial_data(serial);
2044 p_priv = usb_get_serial_port_data(port);
2045 d_details = s_priv->device_details;
2046 device_port = port->port_number;
2047
2048 this_urb = p_priv->outcont_urb;
2049
2050 /* Make sure we have an urb then send the message */
2051 if (this_urb == NULL) {
2052 dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
2053 return -1;
2054 }
2055
2056 dev_dbg(&port->dev, "%s - endpoint %x\n",
2057 __func__, usb_pipeendpoint(this_urb->pipe));
2058
2059 /* Save reset port val for resend.
2060 Don't overwrite resend for open/close condition. */
2061 if ((reset_port + 1) > p_priv->resend_cont)
2062 p_priv->resend_cont = reset_port + 1;
2063 if (this_urb->status == -EINPROGRESS) {
2064 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
2065 mdelay(5);
2066 return -1;
2067 }
2068
2069 memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));
2070
2071 /* Only set baud rate if it's changed */
2072 if (p_priv->old_baud != p_priv->baud) {
2073 p_priv->old_baud = p_priv->baud;
2074 msg.setClocking = 0xff;
2075 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2076 &msg.baudHi, &msg.baudLo, &msg.prescaler,
2077 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2078 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
2079 __func__, p_priv->baud);
2080 msg.baudLo = 0;
2081 msg.baudHi = 125; /* Values for 9600 baud */
2082 msg.prescaler = 10;
2083 }
2084 msg.setPrescaler = 0xff;
2085 }
2086
2087 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2088 switch (p_priv->cflag & CSIZE) {
2089 case CS5:
2090 msg.lcr |= USA_DATABITS_5;
2091 break;
2092 case CS6:
2093 msg.lcr |= USA_DATABITS_6;
2094 break;
2095 case CS7:
2096 msg.lcr |= USA_DATABITS_7;
2097 break;
2098 case CS8:
2099 msg.lcr |= USA_DATABITS_8;
2100 break;
2101 }
2102 if (p_priv->cflag & PARENB) {
2103 /* note USA_PARITY_NONE == 0 */
2104 msg.lcr |= (p_priv->cflag & PARODD) ?
2105 USA_PARITY_ODD : USA_PARITY_EVEN;
2106 }
2107 msg.setLcr = 0xff;
2108
2109 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2110 msg.xonFlowControl = 0;
2111 msg.setFlowControl = 0xff;
2112 msg.forwardingLength = 16;
2113 msg.xonChar = 17;
2114 msg.xoffChar = 19;
2115
2116 /* Opening port */
2117 if (reset_port == 1) {
2118 msg._txOn = 1;
2119 msg._txOff = 0;
2120 msg.txFlush = 0;
2121 msg.txBreak = 0;
2122 msg.rxOn = 1;
2123 msg.rxOff = 0;
2124 msg.rxFlush = 1;
2125 msg.rxForward = 0;
2126 msg.returnStatus = 0;
2127 msg.resetDataToggle = 0xff;
2128 }
2129
2130 /* Closing port */
2131 else if (reset_port == 2) {
2132 msg._txOn = 0;
2133 msg._txOff = 1;
2134 msg.txFlush = 0;
2135 msg.txBreak = 0;
2136 msg.rxOn = 0;
2137 msg.rxOff = 1;
2138 msg.rxFlush = 1;
2139 msg.rxForward = 0;
2140 msg.returnStatus = 0;
2141 msg.resetDataToggle = 0;
2142 }
2143
2144 /* Sending intermediate configs */
2145 else {
2146 msg._txOn = (!p_priv->break_on);
2147 msg._txOff = 0;
2148 msg.txFlush = 0;
2149 msg.txBreak = (p_priv->break_on);
2150 msg.rxOn = 0;
2151 msg.rxOff = 0;
2152 msg.rxFlush = 0;
2153 msg.rxForward = 0;
2154 msg.returnStatus = 0;
2155 msg.resetDataToggle = 0x0;
2156 }
2157
2158 /* Do handshaking outputs */
2159 msg.setTxTriState_setRts = 0xff;
2160 msg.txTriState_rts = p_priv->rts_state;
2161
2162 msg.setHskoa_setDtr = 0xff;
2163 msg.hskoa_dtr = p_priv->dtr_state;
2164
2165 p_priv->resend_cont = 0;
2166 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2167
2168 /* send the data out the device on control endpoint */
2169 this_urb->transfer_buffer_length = sizeof(msg);
2170
2171 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2172 if (err != 0)
2173 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2174 return 0;
2175}
2176
2177static int keyspan_usa28_send_setup(struct usb_serial *serial,
2178 struct usb_serial_port *port,
2179 int reset_port)
2180{
2181 struct keyspan_usa28_portControlMessage msg;
2182 struct keyspan_serial_private *s_priv;
2183 struct keyspan_port_private *p_priv;
2184 const struct keyspan_device_details *d_details;
2185 struct urb *this_urb;
2186 int device_port, err;
2187
2188 s_priv = usb_get_serial_data(serial);
2189 p_priv = usb_get_serial_port_data(port);
2190 d_details = s_priv->device_details;
2191 device_port = port->port_number;
2192
2193 /* only do something if we have a bulk out endpoint */
2194 this_urb = p_priv->outcont_urb;
2195 if (this_urb == NULL) {
2196 dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
2197 return -1;
2198 }
2199
2200 /* Save reset port val for resend.
2201 Don't overwrite resend for open/close condition. */
2202 if ((reset_port + 1) > p_priv->resend_cont)
2203 p_priv->resend_cont = reset_port + 1;
2204 if (this_urb->status == -EINPROGRESS) {
2205 dev_dbg(&port->dev, "%s already writing\n", __func__);
2206 mdelay(5);
2207 return -1;
2208 }
2209
2210 memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
2211
2212 msg.setBaudRate = 1;
2213 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2214 &msg.baudHi, &msg.baudLo, NULL,
2215 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2216 dev_dbg(&port->dev, "%s - Invalid baud rate requested %d.\n",
2217 __func__, p_priv->baud);
2218 msg.baudLo = 0xff;
2219 msg.baudHi = 0xb2; /* Values for 9600 baud */
2220 }
2221
2222 /* If parity is enabled, we must calculate it ourselves. */
2223 msg.parity = 0; /* XXX for now */
2224
2225 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2226 msg.xonFlowControl = 0;
2227
2228 /* Do handshaking outputs, DTR is inverted relative to RTS */
2229 msg.rts = p_priv->rts_state;
2230 msg.dtr = p_priv->dtr_state;
2231
2232 msg.forwardingLength = 16;
2233 msg.forwardMs = 10;
2234 msg.breakThreshold = 45;
2235 msg.xonChar = 17;
2236 msg.xoffChar = 19;
2237
2238 /*msg.returnStatus = 1;
2239 msg.resetDataToggle = 0xff;*/
2240 /* Opening port */
2241 if (reset_port == 1) {
2242 msg._txOn = 1;
2243 msg._txOff = 0;
2244 msg.txFlush = 0;
2245 msg.txForceXoff = 0;
2246 msg.txBreak = 0;
2247 msg.rxOn = 1;
2248 msg.rxOff = 0;
2249 msg.rxFlush = 1;
2250 msg.rxForward = 0;
2251 msg.returnStatus = 0;
2252 msg.resetDataToggle = 0xff;
2253 }
2254 /* Closing port */
2255 else if (reset_port == 2) {
2256 msg._txOn = 0;
2257 msg._txOff = 1;
2258 msg.txFlush = 0;
2259 msg.txForceXoff = 0;
2260 msg.txBreak = 0;
2261 msg.rxOn = 0;
2262 msg.rxOff = 1;
2263 msg.rxFlush = 1;
2264 msg.rxForward = 0;
2265 msg.returnStatus = 0;
2266 msg.resetDataToggle = 0;
2267 }
2268 /* Sending intermediate configs */
2269 else {
2270 msg._txOn = (!p_priv->break_on);
2271 msg._txOff = 0;
2272 msg.txFlush = 0;
2273 msg.txForceXoff = 0;
2274 msg.txBreak = (p_priv->break_on);
2275 msg.rxOn = 0;
2276 msg.rxOff = 0;
2277 msg.rxFlush = 0;
2278 msg.rxForward = 0;
2279 msg.returnStatus = 0;
2280 msg.resetDataToggle = 0x0;
2281 }
2282
2283 p_priv->resend_cont = 0;
2284 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2285
2286 /* send the data out the device on control endpoint */
2287 this_urb->transfer_buffer_length = sizeof(msg);
2288
2289 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2290 if (err != 0)
2291 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed\n", __func__);
2292
2293 return 0;
2294}
2295
2296static int keyspan_usa49_send_setup(struct usb_serial *serial,
2297 struct usb_serial_port *port,
2298 int reset_port)
2299{
2300 struct keyspan_usa49_portControlMessage msg;
2301 struct usb_ctrlrequest *dr = NULL;
2302 struct keyspan_serial_private *s_priv;
2303 struct keyspan_port_private *p_priv;
2304 const struct keyspan_device_details *d_details;
2305 struct urb *this_urb;
2306 int err, device_port;
2307
2308 s_priv = usb_get_serial_data(serial);
2309 p_priv = usb_get_serial_port_data(port);
2310 d_details = s_priv->device_details;
2311
2312 this_urb = s_priv->glocont_urb;
2313
2314 /* Work out which port within the device is being setup */
2315 device_port = port->port_number;
2316
2317 /* Make sure we have an urb then send the message */
2318 if (this_urb == NULL) {
2319 dev_dbg(&port->dev, "%s - oops no urb for port.\n", __func__);
2320 return -1;
2321 }
2322
2323 dev_dbg(&port->dev, "%s - endpoint %x (%d)\n",
2324 __func__, usb_pipeendpoint(this_urb->pipe), device_port);
2325
2326 /* Save reset port val for resend.
2327 Don't overwrite resend for open/close condition. */
2328 if ((reset_port + 1) > p_priv->resend_cont)
2329 p_priv->resend_cont = reset_port + 1;
2330
2331 if (this_urb->status == -EINPROGRESS) {
2332 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
2333 mdelay(5);
2334 return -1;
2335 }
2336
2337 memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
2338
2339 msg.portNumber = device_port;
2340
2341 /* Only set baud rate if it's changed */
2342 if (p_priv->old_baud != p_priv->baud) {
2343 p_priv->old_baud = p_priv->baud;
2344 msg.setClocking = 0xff;
2345 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2346 &msg.baudHi, &msg.baudLo, &msg.prescaler,
2347 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2348 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
2349 __func__, p_priv->baud);
2350 msg.baudLo = 0;
2351 msg.baudHi = 125; /* Values for 9600 baud */
2352 msg.prescaler = 10;
2353 }
2354 /* msg.setPrescaler = 0xff; */
2355 }
2356
2357 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2358 switch (p_priv->cflag & CSIZE) {
2359 case CS5:
2360 msg.lcr |= USA_DATABITS_5;
2361 break;
2362 case CS6:
2363 msg.lcr |= USA_DATABITS_6;
2364 break;
2365 case CS7:
2366 msg.lcr |= USA_DATABITS_7;
2367 break;
2368 case CS8:
2369 msg.lcr |= USA_DATABITS_8;
2370 break;
2371 }
2372 if (p_priv->cflag & PARENB) {
2373 /* note USA_PARITY_NONE == 0 */
2374 msg.lcr |= (p_priv->cflag & PARODD) ?
2375 USA_PARITY_ODD : USA_PARITY_EVEN;
2376 }
2377 msg.setLcr = 0xff;
2378
2379 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2380 msg.xonFlowControl = 0;
2381 msg.setFlowControl = 0xff;
2382
2383 msg.forwardingLength = 16;
2384 msg.xonChar = 17;
2385 msg.xoffChar = 19;
2386
2387 /* Opening port */
2388 if (reset_port == 1) {
2389 msg._txOn = 1;
2390 msg._txOff = 0;
2391 msg.txFlush = 0;
2392 msg.txBreak = 0;
2393 msg.rxOn = 1;
2394 msg.rxOff = 0;
2395 msg.rxFlush = 1;
2396 msg.rxForward = 0;
2397 msg.returnStatus = 0;
2398 msg.resetDataToggle = 0xff;
2399 msg.enablePort = 1;
2400 msg.disablePort = 0;
2401 }
2402 /* Closing port */
2403 else if (reset_port == 2) {
2404 msg._txOn = 0;
2405 msg._txOff = 1;
2406 msg.txFlush = 0;
2407 msg.txBreak = 0;
2408 msg.rxOn = 0;
2409 msg.rxOff = 1;
2410 msg.rxFlush = 1;
2411 msg.rxForward = 0;
2412 msg.returnStatus = 0;
2413 msg.resetDataToggle = 0;
2414 msg.enablePort = 0;
2415 msg.disablePort = 1;
2416 }
2417 /* Sending intermediate configs */
2418 else {
2419 msg._txOn = (!p_priv->break_on);
2420 msg._txOff = 0;
2421 msg.txFlush = 0;
2422 msg.txBreak = (p_priv->break_on);
2423 msg.rxOn = 0;
2424 msg.rxOff = 0;
2425 msg.rxFlush = 0;
2426 msg.rxForward = 0;
2427 msg.returnStatus = 0;
2428 msg.resetDataToggle = 0x0;
2429 msg.enablePort = 0;
2430 msg.disablePort = 0;
2431 }
2432
2433 /* Do handshaking outputs */
2434 msg.setRts = 0xff;
2435 msg.rts = p_priv->rts_state;
2436
2437 msg.setDtr = 0xff;
2438 msg.dtr = p_priv->dtr_state;
2439
2440 p_priv->resend_cont = 0;
2441
2442 /* if the device is a 49wg, we send control message on usb
2443 control EP 0 */
2444
2445 if (d_details->product_id == keyspan_usa49wg_product_id) {
2446 dr = (void *)(s_priv->ctrl_buf);
2447 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2448 dr->bRequest = 0xB0; /* 49wg control message */
2449 dr->wValue = 0;
2450 dr->wIndex = 0;
2451 dr->wLength = cpu_to_le16(sizeof(msg));
2452
2453 memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
2454
2455 usb_fill_control_urb(this_urb, serial->dev,
2456 usb_sndctrlpipe(serial->dev, 0),
2457 (unsigned char *)dr, s_priv->glocont_buf,
2458 sizeof(msg), usa49_glocont_callback, serial);
2459
2460 } else {
2461 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2462
2463 /* send the data out the device on control endpoint */
2464 this_urb->transfer_buffer_length = sizeof(msg);
2465 }
2466 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2467 if (err != 0)
2468 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2469
2470 return 0;
2471}
2472
2473static int keyspan_usa90_send_setup(struct usb_serial *serial,
2474 struct usb_serial_port *port,
2475 int reset_port)
2476{
2477 struct keyspan_usa90_portControlMessage msg;
2478 struct keyspan_serial_private *s_priv;
2479 struct keyspan_port_private *p_priv;
2480 const struct keyspan_device_details *d_details;
2481 struct urb *this_urb;
2482 int err;
2483 u8 prescaler;
2484
2485 s_priv = usb_get_serial_data(serial);
2486 p_priv = usb_get_serial_port_data(port);
2487 d_details = s_priv->device_details;
2488
2489 /* only do something if we have a bulk out endpoint */
2490 this_urb = p_priv->outcont_urb;
2491 if (this_urb == NULL) {
2492 dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
2493 return -1;
2494 }
2495
2496 /* Save reset port val for resend.
2497 Don't overwrite resend for open/close condition. */
2498 if ((reset_port + 1) > p_priv->resend_cont)
2499 p_priv->resend_cont = reset_port + 1;
2500 if (this_urb->status == -EINPROGRESS) {
2501 dev_dbg(&port->dev, "%s already writing\n", __func__);
2502 mdelay(5);
2503 return -1;
2504 }
2505
2506 memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
2507
2508 /* Only set baud rate if it's changed */
2509 if (p_priv->old_baud != p_priv->baud) {
2510 p_priv->old_baud = p_priv->baud;
2511 msg.setClocking = 0x01;
2512 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2513 &msg.baudHi, &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
2514 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
2515 __func__, p_priv->baud);
2516 p_priv->baud = 9600;
2517 d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2518 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2519 }
2520 msg.setRxMode = 1;
2521 msg.setTxMode = 1;
2522 }
2523
2524 /* modes must always be correctly specified */
2525 if (p_priv->baud > 57600) {
2526 msg.rxMode = RXMODE_DMA;
2527 msg.txMode = TXMODE_DMA;
2528 } else {
2529 msg.rxMode = RXMODE_BYHAND;
2530 msg.txMode = TXMODE_BYHAND;
2531 }
2532
2533 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2534 switch (p_priv->cflag & CSIZE) {
2535 case CS5:
2536 msg.lcr |= USA_DATABITS_5;
2537 break;
2538 case CS6:
2539 msg.lcr |= USA_DATABITS_6;
2540 break;
2541 case CS7:
2542 msg.lcr |= USA_DATABITS_7;
2543 break;
2544 case CS8:
2545 msg.lcr |= USA_DATABITS_8;
2546 break;
2547 }
2548 if (p_priv->cflag & PARENB) {
2549 /* note USA_PARITY_NONE == 0 */
2550 msg.lcr |= (p_priv->cflag & PARODD) ?
2551 USA_PARITY_ODD : USA_PARITY_EVEN;
2552 }
2553 if (p_priv->old_cflag != p_priv->cflag) {
2554 p_priv->old_cflag = p_priv->cflag;
2555 msg.setLcr = 0x01;
2556 }
2557
2558 if (p_priv->flow_control == flow_cts)
2559 msg.txFlowControl = TXFLOW_CTS;
2560 msg.setTxFlowControl = 0x01;
2561 msg.setRxFlowControl = 0x01;
2562
2563 msg.rxForwardingLength = 16;
2564 msg.rxForwardingTimeout = 16;
2565 msg.txAckSetting = 0;
2566 msg.xonChar = 17;
2567 msg.xoffChar = 19;
2568
2569 /* Opening port */
2570 if (reset_port == 1) {
2571 msg.portEnabled = 1;
2572 msg.rxFlush = 1;
2573 msg.txBreak = (p_priv->break_on);
2574 }
2575 /* Closing port */
2576 else if (reset_port == 2)
2577 msg.portEnabled = 0;
2578 /* Sending intermediate configs */
2579 else {
2580 msg.portEnabled = 1;
2581 msg.txBreak = (p_priv->break_on);
2582 }
2583
2584 /* Do handshaking outputs */
2585 msg.setRts = 0x01;
2586 msg.rts = p_priv->rts_state;
2587
2588 msg.setDtr = 0x01;
2589 msg.dtr = p_priv->dtr_state;
2590
2591 p_priv->resend_cont = 0;
2592 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2593
2594 /* send the data out the device on control endpoint */
2595 this_urb->transfer_buffer_length = sizeof(msg);
2596
2597 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2598 if (err != 0)
2599 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2600 return 0;
2601}
2602
2603static int keyspan_usa67_send_setup(struct usb_serial *serial,
2604 struct usb_serial_port *port,
2605 int reset_port)
2606{
2607 struct keyspan_usa67_portControlMessage msg;
2608 struct keyspan_serial_private *s_priv;
2609 struct keyspan_port_private *p_priv;
2610 const struct keyspan_device_details *d_details;
2611 struct urb *this_urb;
2612 int err, device_port;
2613
2614 s_priv = usb_get_serial_data(serial);
2615 p_priv = usb_get_serial_port_data(port);
2616 d_details = s_priv->device_details;
2617
2618 this_urb = s_priv->glocont_urb;
2619
2620 /* Work out which port within the device is being setup */
2621 device_port = port->port_number;
2622
2623 /* Make sure we have an urb then send the message */
2624 if (this_urb == NULL) {
2625 dev_dbg(&port->dev, "%s - oops no urb for port.\n", __func__);
2626 return -1;
2627 }
2628
2629 /* Save reset port val for resend.
2630 Don't overwrite resend for open/close condition. */
2631 if ((reset_port + 1) > p_priv->resend_cont)
2632 p_priv->resend_cont = reset_port + 1;
2633 if (this_urb->status == -EINPROGRESS) {
2634 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
2635 mdelay(5);
2636 return -1;
2637 }
2638
2639 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2640
2641 msg.port = device_port;
2642
2643 /* Only set baud rate if it's changed */
2644 if (p_priv->old_baud != p_priv->baud) {
2645 p_priv->old_baud = p_priv->baud;
2646 msg.setClocking = 0xff;
2647 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2648 &msg.baudHi, &msg.baudLo, &msg.prescaler,
2649 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2650 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
2651 __func__, p_priv->baud);
2652 msg.baudLo = 0;
2653 msg.baudHi = 125; /* Values for 9600 baud */
2654 msg.prescaler = 10;
2655 }
2656 msg.setPrescaler = 0xff;
2657 }
2658
2659 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2660 switch (p_priv->cflag & CSIZE) {
2661 case CS5:
2662 msg.lcr |= USA_DATABITS_5;
2663 break;
2664 case CS6:
2665 msg.lcr |= USA_DATABITS_6;
2666 break;
2667 case CS7:
2668 msg.lcr |= USA_DATABITS_7;
2669 break;
2670 case CS8:
2671 msg.lcr |= USA_DATABITS_8;
2672 break;
2673 }
2674 if (p_priv->cflag & PARENB) {
2675 /* note USA_PARITY_NONE == 0 */
2676 msg.lcr |= (p_priv->cflag & PARODD) ?
2677 USA_PARITY_ODD : USA_PARITY_EVEN;
2678 }
2679 msg.setLcr = 0xff;
2680
2681 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2682 msg.xonFlowControl = 0;
2683 msg.setFlowControl = 0xff;
2684 msg.forwardingLength = 16;
2685 msg.xonChar = 17;
2686 msg.xoffChar = 19;
2687
2688 if (reset_port == 1) {
2689 /* Opening port */
2690 msg._txOn = 1;
2691 msg._txOff = 0;
2692 msg.txFlush = 0;
2693 msg.txBreak = 0;
2694 msg.rxOn = 1;
2695 msg.rxOff = 0;
2696 msg.rxFlush = 1;
2697 msg.rxForward = 0;
2698 msg.returnStatus = 0;
2699 msg.resetDataToggle = 0xff;
2700 } else if (reset_port == 2) {
2701 /* Closing port */
2702 msg._txOn = 0;
2703 msg._txOff = 1;
2704 msg.txFlush = 0;
2705 msg.txBreak = 0;
2706 msg.rxOn = 0;
2707 msg.rxOff = 1;
2708 msg.rxFlush = 1;
2709 msg.rxForward = 0;
2710 msg.returnStatus = 0;
2711 msg.resetDataToggle = 0;
2712 } else {
2713 /* Sending intermediate configs */
2714 msg._txOn = (!p_priv->break_on);
2715 msg._txOff = 0;
2716 msg.txFlush = 0;
2717 msg.txBreak = (p_priv->break_on);
2718 msg.rxOn = 0;
2719 msg.rxOff = 0;
2720 msg.rxFlush = 0;
2721 msg.rxForward = 0;
2722 msg.returnStatus = 0;
2723 msg.resetDataToggle = 0x0;
2724 }
2725
2726 /* Do handshaking outputs */
2727 msg.setTxTriState_setRts = 0xff;
2728 msg.txTriState_rts = p_priv->rts_state;
2729
2730 msg.setHskoa_setDtr = 0xff;
2731 msg.hskoa_dtr = p_priv->dtr_state;
2732
2733 p_priv->resend_cont = 0;
2734
2735 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2736
2737 /* send the data out the device on control endpoint */
2738 this_urb->transfer_buffer_length = sizeof(msg);
2739
2740 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2741 if (err != 0)
2742 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2743 return 0;
2744}
2745
2746static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2747{
2748 struct usb_serial *serial = port->serial;
2749 struct keyspan_serial_private *s_priv;
2750 const struct keyspan_device_details *d_details;
2751
2752 s_priv = usb_get_serial_data(serial);
2753 d_details = s_priv->device_details;
2754
2755 switch (d_details->msg_format) {
2756 case msg_usa26:
2757 keyspan_usa26_send_setup(serial, port, reset_port);
2758 break;
2759 case msg_usa28:
2760 keyspan_usa28_send_setup(serial, port, reset_port);
2761 break;
2762 case msg_usa49:
2763 keyspan_usa49_send_setup(serial, port, reset_port);
2764 break;
2765 case msg_usa90:
2766 keyspan_usa90_send_setup(serial, port, reset_port);
2767 break;
2768 case msg_usa67:
2769 keyspan_usa67_send_setup(serial, port, reset_port);
2770 break;
2771 }
2772}
2773
2774
2775/* Gets called by the "real" driver (ie once firmware is loaded
2776 and renumeration has taken place. */
2777static int keyspan_startup(struct usb_serial *serial)
2778{
2779 int i, err;
2780 struct keyspan_serial_private *s_priv;
2781 const struct keyspan_device_details *d_details;
2782
2783 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2784 if (d_details->product_id ==
2785 le16_to_cpu(serial->dev->descriptor.idProduct))
2786 break;
2787 if (d_details == NULL) {
2788 dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
2789 __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
2790 return -ENODEV;
2791 }
2792
2793 /* Setup private data for serial driver */
2794 s_priv = kzalloc_obj(struct keyspan_serial_private);
2795 if (!s_priv)
2796 return -ENOMEM;
2797
2798 s_priv->instat_buf = kzalloc(INSTAT_BUFLEN, GFP_KERNEL);
2799 if (!s_priv->instat_buf)
2800 goto err_instat_buf;
2801
2802 s_priv->indat_buf = kzalloc(INDAT49W_BUFLEN, GFP_KERNEL);
2803 if (!s_priv->indat_buf)
2804 goto err_indat_buf;
2805
2806 s_priv->glocont_buf = kzalloc(GLOCONT_BUFLEN, GFP_KERNEL);
2807 if (!s_priv->glocont_buf)
2808 goto err_glocont_buf;
2809
2810 s_priv->ctrl_buf = kzalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
2811 if (!s_priv->ctrl_buf)
2812 goto err_ctrl_buf;
2813
2814 s_priv->device_details = d_details;
2815 usb_set_serial_data(serial, s_priv);
2816
2817 keyspan_setup_urbs(serial);
2818
2819 if (s_priv->instat_urb != NULL) {
2820 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2821 if (err != 0)
2822 dev_dbg(&serial->dev->dev, "%s - submit instat urb failed %d\n", __func__, err);
2823 }
2824 if (s_priv->indat_urb != NULL) {
2825 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2826 if (err != 0)
2827 dev_dbg(&serial->dev->dev, "%s - submit indat urb failed %d\n", __func__, err);
2828 }
2829
2830 return 0;
2831
2832err_ctrl_buf:
2833 kfree(s_priv->glocont_buf);
2834err_glocont_buf:
2835 kfree(s_priv->indat_buf);
2836err_indat_buf:
2837 kfree(s_priv->instat_buf);
2838err_instat_buf:
2839 kfree(s_priv);
2840
2841 return -ENOMEM;
2842}
2843
2844static void keyspan_disconnect(struct usb_serial *serial)
2845{
2846 struct keyspan_serial_private *s_priv;
2847
2848 s_priv = usb_get_serial_data(serial);
2849
2850 usb_kill_urb(s_priv->instat_urb);
2851 usb_kill_urb(s_priv->glocont_urb);
2852 usb_kill_urb(s_priv->indat_urb);
2853}
2854
2855static void keyspan_release(struct usb_serial *serial)
2856{
2857 struct keyspan_serial_private *s_priv;
2858
2859 s_priv = usb_get_serial_data(serial);
2860
2861 /* Make sure to unlink the URBs submitted in attach. */
2862 usb_kill_urb(s_priv->instat_urb);
2863 usb_kill_urb(s_priv->indat_urb);
2864
2865 usb_free_urb(s_priv->instat_urb);
2866 usb_free_urb(s_priv->indat_urb);
2867 usb_free_urb(s_priv->glocont_urb);
2868
2869 kfree(s_priv->ctrl_buf);
2870 kfree(s_priv->glocont_buf);
2871 kfree(s_priv->indat_buf);
2872 kfree(s_priv->instat_buf);
2873
2874 kfree(s_priv);
2875}
2876
2877static int keyspan_port_probe(struct usb_serial_port *port)
2878{
2879 struct usb_serial *serial = port->serial;
2880 struct keyspan_serial_private *s_priv;
2881 struct keyspan_port_private *p_priv;
2882 const struct keyspan_device_details *d_details;
2883 struct callbacks *cback;
2884 int endp;
2885 int port_num;
2886 int i;
2887
2888 s_priv = usb_get_serial_data(serial);
2889 d_details = s_priv->device_details;
2890
2891 p_priv = kzalloc_obj(*p_priv);
2892 if (!p_priv)
2893 return -ENOMEM;
2894
2895 for (i = 0; i < ARRAY_SIZE(p_priv->in_buffer); ++i) {
2896 p_priv->in_buffer[i] = kzalloc(IN_BUFLEN, GFP_KERNEL);
2897 if (!p_priv->in_buffer[i])
2898 goto err_free_in_buffer;
2899 }
2900
2901 for (i = 0; i < ARRAY_SIZE(p_priv->out_buffer); ++i) {
2902 p_priv->out_buffer[i] = kzalloc(OUT_BUFLEN, GFP_KERNEL);
2903 if (!p_priv->out_buffer[i])
2904 goto err_free_out_buffer;
2905 }
2906
2907 p_priv->inack_buffer = kzalloc(INACK_BUFLEN, GFP_KERNEL);
2908 if (!p_priv->inack_buffer)
2909 goto err_free_out_buffer;
2910
2911 p_priv->outcont_buffer = kzalloc(OUTCONT_BUFLEN, GFP_KERNEL);
2912 if (!p_priv->outcont_buffer)
2913 goto err_free_inack_buffer;
2914
2915 p_priv->device_details = d_details;
2916
2917 /* Setup values for the various callback routines */
2918 cback = &keyspan_callbacks[d_details->msg_format];
2919
2920 port_num = port->port_number;
2921
2922 /* Do indat endpoints first, once for each flip */
2923 endp = d_details->indat_endpoints[port_num];
2924 for (i = 0; i <= d_details->indat_endp_flip; ++i, ++endp) {
2925 p_priv->in_urbs[i] = keyspan_setup_urb(serial, endp,
2926 USB_DIR_IN, port,
2927 p_priv->in_buffer[i],
2928 IN_BUFLEN,
2929 cback->indat_callback);
2930 }
2931 /* outdat endpoints also have flip */
2932 endp = d_details->outdat_endpoints[port_num];
2933 for (i = 0; i <= d_details->outdat_endp_flip; ++i, ++endp) {
2934 p_priv->out_urbs[i] = keyspan_setup_urb(serial, endp,
2935 USB_DIR_OUT, port,
2936 p_priv->out_buffer[i],
2937 OUT_BUFLEN,
2938 cback->outdat_callback);
2939 }
2940 /* inack endpoint */
2941 p_priv->inack_urb = keyspan_setup_urb(serial,
2942 d_details->inack_endpoints[port_num],
2943 USB_DIR_IN, port,
2944 p_priv->inack_buffer,
2945 INACK_BUFLEN,
2946 cback->inack_callback);
2947 /* outcont endpoint */
2948 p_priv->outcont_urb = keyspan_setup_urb(serial,
2949 d_details->outcont_endpoints[port_num],
2950 USB_DIR_OUT, port,
2951 p_priv->outcont_buffer,
2952 OUTCONT_BUFLEN,
2953 cback->outcont_callback);
2954
2955 usb_set_serial_port_data(port, p_priv);
2956
2957 return 0;
2958
2959err_free_inack_buffer:
2960 kfree(p_priv->inack_buffer);
2961err_free_out_buffer:
2962 for (i = 0; i < ARRAY_SIZE(p_priv->out_buffer); ++i)
2963 kfree(p_priv->out_buffer[i]);
2964err_free_in_buffer:
2965 for (i = 0; i < ARRAY_SIZE(p_priv->in_buffer); ++i)
2966 kfree(p_priv->in_buffer[i]);
2967 kfree(p_priv);
2968
2969 return -ENOMEM;
2970}
2971
2972static void keyspan_port_remove(struct usb_serial_port *port)
2973{
2974 struct keyspan_port_private *p_priv;
2975 int i;
2976
2977 p_priv = usb_get_serial_port_data(port);
2978
2979 usb_kill_urb(p_priv->inack_urb);
2980 usb_kill_urb(p_priv->outcont_urb);
2981 for (i = 0; i < 2; i++) {
2982 usb_kill_urb(p_priv->in_urbs[i]);
2983 usb_kill_urb(p_priv->out_urbs[i]);
2984 }
2985
2986 usb_free_urb(p_priv->inack_urb);
2987 usb_free_urb(p_priv->outcont_urb);
2988 for (i = 0; i < 2; i++) {
2989 usb_free_urb(p_priv->in_urbs[i]);
2990 usb_free_urb(p_priv->out_urbs[i]);
2991 }
2992
2993 kfree(p_priv->outcont_buffer);
2994 kfree(p_priv->inack_buffer);
2995 for (i = 0; i < ARRAY_SIZE(p_priv->out_buffer); ++i)
2996 kfree(p_priv->out_buffer[i]);
2997 for (i = 0; i < ARRAY_SIZE(p_priv->in_buffer); ++i)
2998 kfree(p_priv->in_buffer[i]);
2999
3000 kfree(p_priv);
3001}
3002
3003/* Structs for the devices, pre and post renumeration. */
3004static struct usb_serial_driver keyspan_pre_device = {
3005 .driver = {
3006 .name = "keyspan_no_firm",
3007 },
3008 .description = "Keyspan - (without firmware)",
3009 .id_table = keyspan_pre_ids,
3010 .num_ports = 1,
3011 .attach = keyspan_fake_startup,
3012};
3013
3014static struct usb_serial_driver keyspan_1port_device = {
3015 .driver = {
3016 .name = "keyspan_1",
3017 },
3018 .description = "Keyspan 1 port adapter",
3019 .id_table = keyspan_1port_ids,
3020 .num_ports = 1,
3021 .open = keyspan_open,
3022 .close = keyspan_close,
3023 .dtr_rts = keyspan_dtr_rts,
3024 .write = keyspan_write,
3025 .write_room = keyspan_write_room,
3026 .set_termios = keyspan_set_termios,
3027 .break_ctl = keyspan_break_ctl,
3028 .tiocmget = keyspan_tiocmget,
3029 .tiocmset = keyspan_tiocmset,
3030 .attach = keyspan_startup,
3031 .disconnect = keyspan_disconnect,
3032 .release = keyspan_release,
3033 .port_probe = keyspan_port_probe,
3034 .port_remove = keyspan_port_remove,
3035};
3036
3037static struct usb_serial_driver keyspan_2port_device = {
3038 .driver = {
3039 .name = "keyspan_2",
3040 },
3041 .description = "Keyspan 2 port adapter",
3042 .id_table = keyspan_2port_ids,
3043 .num_ports = 2,
3044 .open = keyspan_open,
3045 .close = keyspan_close,
3046 .dtr_rts = keyspan_dtr_rts,
3047 .write = keyspan_write,
3048 .write_room = keyspan_write_room,
3049 .set_termios = keyspan_set_termios,
3050 .break_ctl = keyspan_break_ctl,
3051 .tiocmget = keyspan_tiocmget,
3052 .tiocmset = keyspan_tiocmset,
3053 .attach = keyspan_startup,
3054 .disconnect = keyspan_disconnect,
3055 .release = keyspan_release,
3056 .port_probe = keyspan_port_probe,
3057 .port_remove = keyspan_port_remove,
3058};
3059
3060static struct usb_serial_driver keyspan_4port_device = {
3061 .driver = {
3062 .name = "keyspan_4",
3063 },
3064 .description = "Keyspan 4 port adapter",
3065 .id_table = keyspan_4port_ids,
3066 .num_ports = 4,
3067 .open = keyspan_open,
3068 .close = keyspan_close,
3069 .dtr_rts = keyspan_dtr_rts,
3070 .write = keyspan_write,
3071 .write_room = keyspan_write_room,
3072 .set_termios = keyspan_set_termios,
3073 .break_ctl = keyspan_break_ctl,
3074 .tiocmget = keyspan_tiocmget,
3075 .tiocmset = keyspan_tiocmset,
3076 .attach = keyspan_startup,
3077 .disconnect = keyspan_disconnect,
3078 .release = keyspan_release,
3079 .port_probe = keyspan_port_probe,
3080 .port_remove = keyspan_port_remove,
3081};
3082
3083static struct usb_serial_driver * const serial_drivers[] = {
3084 &keyspan_pre_device, &keyspan_1port_device,
3085 &keyspan_2port_device, &keyspan_4port_device, NULL
3086};
3087
3088module_usb_serial_driver(serial_drivers, keyspan_ids_combined);
3089
3090MODULE_AUTHOR(DRIVER_AUTHOR);
3091MODULE_DESCRIPTION(DRIVER_DESC);
3092MODULE_LICENSE("GPL");
3093
3094MODULE_FIRMWARE("keyspan/usa28.fw");
3095MODULE_FIRMWARE("keyspan/usa28x.fw");
3096MODULE_FIRMWARE("keyspan/usa28xa.fw");
3097MODULE_FIRMWARE("keyspan/usa28xb.fw");
3098MODULE_FIRMWARE("keyspan/usa19.fw");
3099MODULE_FIRMWARE("keyspan/usa19qi.fw");
3100MODULE_FIRMWARE("keyspan/mpr.fw");
3101MODULE_FIRMWARE("keyspan/usa19qw.fw");
3102MODULE_FIRMWARE("keyspan/usa18x.fw");
3103MODULE_FIRMWARE("keyspan/usa19w.fw");
3104MODULE_FIRMWARE("keyspan/usa49w.fw");
3105MODULE_FIRMWARE("keyspan/usa49wlc.fw");