Commit 6aad04f21374633bd8cecf25024553d1e11a9522

Authored by Jiri Slaby
Committed by Greg Kroah-Hartman
1 parent 6982a39842

TTY: add tty_port_tty_wakeup helper

It allows for cleaning up on a considerable amount of places. They did
port_get, wakeup, kref_put. Now the only thing needed is to call
tty_port_tty_wakeup which does exactly that.

One exception is ifx6x60 where tty_wakeup was open-coded. We now call
tty_wakeup properly there.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Showing 22 changed files with 51 additions and 176 deletions Inline Diff

arch/um/drivers/line.c
1 /* 1 /*
2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL 3 * Licensed under the GPL
4 */ 4 */
5 5
6 #include <linux/irqreturn.h> 6 #include <linux/irqreturn.h>
7 #include <linux/kd.h> 7 #include <linux/kd.h>
8 #include <linux/sched.h> 8 #include <linux/sched.h>
9 #include <linux/slab.h> 9 #include <linux/slab.h>
10 #include "chan.h" 10 #include "chan.h"
11 #include <irq_kern.h> 11 #include <irq_kern.h>
12 #include <irq_user.h> 12 #include <irq_user.h>
13 #include <kern_util.h> 13 #include <kern_util.h>
14 #include <os.h> 14 #include <os.h>
15 15
16 #define LINE_BUFSIZE 4096 16 #define LINE_BUFSIZE 4096
17 17
18 static irqreturn_t line_interrupt(int irq, void *data) 18 static irqreturn_t line_interrupt(int irq, void *data)
19 { 19 {
20 struct chan *chan = data; 20 struct chan *chan = data;
21 struct line *line = chan->line; 21 struct line *line = chan->line;
22 22
23 if (line) 23 if (line)
24 chan_interrupt(line, irq); 24 chan_interrupt(line, irq);
25 25
26 return IRQ_HANDLED; 26 return IRQ_HANDLED;
27 } 27 }
28 28
29 /* 29 /*
30 * Returns the free space inside the ring buffer of this line. 30 * Returns the free space inside the ring buffer of this line.
31 * 31 *
32 * Should be called while holding line->lock (this does not modify data). 32 * Should be called while holding line->lock (this does not modify data).
33 */ 33 */
34 static int write_room(struct line *line) 34 static int write_room(struct line *line)
35 { 35 {
36 int n; 36 int n;
37 37
38 if (line->buffer == NULL) 38 if (line->buffer == NULL)
39 return LINE_BUFSIZE - 1; 39 return LINE_BUFSIZE - 1;
40 40
41 /* This is for the case where the buffer is wrapped! */ 41 /* This is for the case where the buffer is wrapped! */
42 n = line->head - line->tail; 42 n = line->head - line->tail;
43 43
44 if (n <= 0) 44 if (n <= 0)
45 n += LINE_BUFSIZE; /* The other case */ 45 n += LINE_BUFSIZE; /* The other case */
46 return n - 1; 46 return n - 1;
47 } 47 }
48 48
49 int line_write_room(struct tty_struct *tty) 49 int line_write_room(struct tty_struct *tty)
50 { 50 {
51 struct line *line = tty->driver_data; 51 struct line *line = tty->driver_data;
52 unsigned long flags; 52 unsigned long flags;
53 int room; 53 int room;
54 54
55 spin_lock_irqsave(&line->lock, flags); 55 spin_lock_irqsave(&line->lock, flags);
56 room = write_room(line); 56 room = write_room(line);
57 spin_unlock_irqrestore(&line->lock, flags); 57 spin_unlock_irqrestore(&line->lock, flags);
58 58
59 return room; 59 return room;
60 } 60 }
61 61
62 int line_chars_in_buffer(struct tty_struct *tty) 62 int line_chars_in_buffer(struct tty_struct *tty)
63 { 63 {
64 struct line *line = tty->driver_data; 64 struct line *line = tty->driver_data;
65 unsigned long flags; 65 unsigned long flags;
66 int ret; 66 int ret;
67 67
68 spin_lock_irqsave(&line->lock, flags); 68 spin_lock_irqsave(&line->lock, flags);
69 /* write_room subtracts 1 for the needed NULL, so we readd it.*/ 69 /* write_room subtracts 1 for the needed NULL, so we readd it.*/
70 ret = LINE_BUFSIZE - (write_room(line) + 1); 70 ret = LINE_BUFSIZE - (write_room(line) + 1);
71 spin_unlock_irqrestore(&line->lock, flags); 71 spin_unlock_irqrestore(&line->lock, flags);
72 72
73 return ret; 73 return ret;
74 } 74 }
75 75
76 /* 76 /*
77 * This copies the content of buf into the circular buffer associated with 77 * This copies the content of buf into the circular buffer associated with
78 * this line. 78 * this line.
79 * The return value is the number of characters actually copied, i.e. the ones 79 * The return value is the number of characters actually copied, i.e. the ones
80 * for which there was space: this function is not supposed to ever flush out 80 * for which there was space: this function is not supposed to ever flush out
81 * the circular buffer. 81 * the circular buffer.
82 * 82 *
83 * Must be called while holding line->lock! 83 * Must be called while holding line->lock!
84 */ 84 */
85 static int buffer_data(struct line *line, const char *buf, int len) 85 static int buffer_data(struct line *line, const char *buf, int len)
86 { 86 {
87 int end, room; 87 int end, room;
88 88
89 if (line->buffer == NULL) { 89 if (line->buffer == NULL) {
90 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC); 90 line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
91 if (line->buffer == NULL) { 91 if (line->buffer == NULL) {
92 printk(KERN_ERR "buffer_data - atomic allocation " 92 printk(KERN_ERR "buffer_data - atomic allocation "
93 "failed\n"); 93 "failed\n");
94 return 0; 94 return 0;
95 } 95 }
96 line->head = line->buffer; 96 line->head = line->buffer;
97 line->tail = line->buffer; 97 line->tail = line->buffer;
98 } 98 }
99 99
100 room = write_room(line); 100 room = write_room(line);
101 len = (len > room) ? room : len; 101 len = (len > room) ? room : len;
102 102
103 end = line->buffer + LINE_BUFSIZE - line->tail; 103 end = line->buffer + LINE_BUFSIZE - line->tail;
104 104
105 if (len < end) { 105 if (len < end) {
106 memcpy(line->tail, buf, len); 106 memcpy(line->tail, buf, len);
107 line->tail += len; 107 line->tail += len;
108 } 108 }
109 else { 109 else {
110 /* The circular buffer is wrapping */ 110 /* The circular buffer is wrapping */
111 memcpy(line->tail, buf, end); 111 memcpy(line->tail, buf, end);
112 buf += end; 112 buf += end;
113 memcpy(line->buffer, buf, len - end); 113 memcpy(line->buffer, buf, len - end);
114 line->tail = line->buffer + len - end; 114 line->tail = line->buffer + len - end;
115 } 115 }
116 116
117 return len; 117 return len;
118 } 118 }
119 119
120 /* 120 /*
121 * Flushes the ring buffer to the output channels. That is, write_chan is 121 * Flushes the ring buffer to the output channels. That is, write_chan is
122 * called, passing it line->head as buffer, and an appropriate count. 122 * called, passing it line->head as buffer, and an appropriate count.
123 * 123 *
124 * On exit, returns 1 when the buffer is empty, 124 * On exit, returns 1 when the buffer is empty,
125 * 0 when the buffer is not empty on exit, 125 * 0 when the buffer is not empty on exit,
126 * and -errno when an error occurred. 126 * and -errno when an error occurred.
127 * 127 *
128 * Must be called while holding line->lock!*/ 128 * Must be called while holding line->lock!*/
129 static int flush_buffer(struct line *line) 129 static int flush_buffer(struct line *line)
130 { 130 {
131 int n, count; 131 int n, count;
132 132
133 if ((line->buffer == NULL) || (line->head == line->tail)) 133 if ((line->buffer == NULL) || (line->head == line->tail))
134 return 1; 134 return 1;
135 135
136 if (line->tail < line->head) { 136 if (line->tail < line->head) {
137 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */ 137 /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
138 count = line->buffer + LINE_BUFSIZE - line->head; 138 count = line->buffer + LINE_BUFSIZE - line->head;
139 139
140 n = write_chan(line->chan_out, line->head, count, 140 n = write_chan(line->chan_out, line->head, count,
141 line->driver->write_irq); 141 line->driver->write_irq);
142 if (n < 0) 142 if (n < 0)
143 return n; 143 return n;
144 if (n == count) { 144 if (n == count) {
145 /* 145 /*
146 * We have flushed from ->head to buffer end, now we 146 * We have flushed from ->head to buffer end, now we
147 * must flush only from the beginning to ->tail. 147 * must flush only from the beginning to ->tail.
148 */ 148 */
149 line->head = line->buffer; 149 line->head = line->buffer;
150 } else { 150 } else {
151 line->head += n; 151 line->head += n;
152 return 0; 152 return 0;
153 } 153 }
154 } 154 }
155 155
156 count = line->tail - line->head; 156 count = line->tail - line->head;
157 n = write_chan(line->chan_out, line->head, count, 157 n = write_chan(line->chan_out, line->head, count,
158 line->driver->write_irq); 158 line->driver->write_irq);
159 159
160 if (n < 0) 160 if (n < 0)
161 return n; 161 return n;
162 162
163 line->head += n; 163 line->head += n;
164 return line->head == line->tail; 164 return line->head == line->tail;
165 } 165 }
166 166
167 void line_flush_buffer(struct tty_struct *tty) 167 void line_flush_buffer(struct tty_struct *tty)
168 { 168 {
169 struct line *line = tty->driver_data; 169 struct line *line = tty->driver_data;
170 unsigned long flags; 170 unsigned long flags;
171 171
172 spin_lock_irqsave(&line->lock, flags); 172 spin_lock_irqsave(&line->lock, flags);
173 flush_buffer(line); 173 flush_buffer(line);
174 spin_unlock_irqrestore(&line->lock, flags); 174 spin_unlock_irqrestore(&line->lock, flags);
175 } 175 }
176 176
177 /* 177 /*
178 * We map both ->flush_chars and ->put_char (which go in pair) onto 178 * We map both ->flush_chars and ->put_char (which go in pair) onto
179 * ->flush_buffer and ->write. Hope it's not that bad. 179 * ->flush_buffer and ->write. Hope it's not that bad.
180 */ 180 */
181 void line_flush_chars(struct tty_struct *tty) 181 void line_flush_chars(struct tty_struct *tty)
182 { 182 {
183 line_flush_buffer(tty); 183 line_flush_buffer(tty);
184 } 184 }
185 185
186 int line_put_char(struct tty_struct *tty, unsigned char ch) 186 int line_put_char(struct tty_struct *tty, unsigned char ch)
187 { 187 {
188 return line_write(tty, &ch, sizeof(ch)); 188 return line_write(tty, &ch, sizeof(ch));
189 } 189 }
190 190
191 int line_write(struct tty_struct *tty, const unsigned char *buf, int len) 191 int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
192 { 192 {
193 struct line *line = tty->driver_data; 193 struct line *line = tty->driver_data;
194 unsigned long flags; 194 unsigned long flags;
195 int n, ret = 0; 195 int n, ret = 0;
196 196
197 spin_lock_irqsave(&line->lock, flags); 197 spin_lock_irqsave(&line->lock, flags);
198 if (line->head != line->tail) 198 if (line->head != line->tail)
199 ret = buffer_data(line, buf, len); 199 ret = buffer_data(line, buf, len);
200 else { 200 else {
201 n = write_chan(line->chan_out, buf, len, 201 n = write_chan(line->chan_out, buf, len,
202 line->driver->write_irq); 202 line->driver->write_irq);
203 if (n < 0) { 203 if (n < 0) {
204 ret = n; 204 ret = n;
205 goto out_up; 205 goto out_up;
206 } 206 }
207 207
208 len -= n; 208 len -= n;
209 ret += n; 209 ret += n;
210 if (len > 0) 210 if (len > 0)
211 ret += buffer_data(line, buf + n, len); 211 ret += buffer_data(line, buf + n, len);
212 } 212 }
213 out_up: 213 out_up:
214 spin_unlock_irqrestore(&line->lock, flags); 214 spin_unlock_irqrestore(&line->lock, flags);
215 return ret; 215 return ret;
216 } 216 }
217 217
218 void line_set_termios(struct tty_struct *tty, struct ktermios * old) 218 void line_set_termios(struct tty_struct *tty, struct ktermios * old)
219 { 219 {
220 /* nothing */ 220 /* nothing */
221 } 221 }
222 222
223 void line_throttle(struct tty_struct *tty) 223 void line_throttle(struct tty_struct *tty)
224 { 224 {
225 struct line *line = tty->driver_data; 225 struct line *line = tty->driver_data;
226 226
227 deactivate_chan(line->chan_in, line->driver->read_irq); 227 deactivate_chan(line->chan_in, line->driver->read_irq);
228 line->throttled = 1; 228 line->throttled = 1;
229 } 229 }
230 230
231 void line_unthrottle(struct tty_struct *tty) 231 void line_unthrottle(struct tty_struct *tty)
232 { 232 {
233 struct line *line = tty->driver_data; 233 struct line *line = tty->driver_data;
234 234
235 line->throttled = 0; 235 line->throttled = 0;
236 chan_interrupt(line, line->driver->read_irq); 236 chan_interrupt(line, line->driver->read_irq);
237 237
238 /* 238 /*
239 * Maybe there is enough stuff pending that calling the interrupt 239 * Maybe there is enough stuff pending that calling the interrupt
240 * throttles us again. In this case, line->throttled will be 1 240 * throttles us again. In this case, line->throttled will be 1
241 * again and we shouldn't turn the interrupt back on. 241 * again and we shouldn't turn the interrupt back on.
242 */ 242 */
243 if (!line->throttled) 243 if (!line->throttled)
244 reactivate_chan(line->chan_in, line->driver->read_irq); 244 reactivate_chan(line->chan_in, line->driver->read_irq);
245 } 245 }
246 246
247 static irqreturn_t line_write_interrupt(int irq, void *data) 247 static irqreturn_t line_write_interrupt(int irq, void *data)
248 { 248 {
249 struct chan *chan = data; 249 struct chan *chan = data;
250 struct line *line = chan->line; 250 struct line *line = chan->line;
251 struct tty_struct *tty;
252 int err; 251 int err;
253 252
254 /* 253 /*
255 * Interrupts are disabled here because genirq keep irqs disabled when 254 * Interrupts are disabled here because genirq keep irqs disabled when
256 * calling the action handler. 255 * calling the action handler.
257 */ 256 */
258 257
259 spin_lock(&line->lock); 258 spin_lock(&line->lock);
260 err = flush_buffer(line); 259 err = flush_buffer(line);
261 if (err == 0) { 260 if (err == 0) {
262 spin_unlock(&line->lock); 261 spin_unlock(&line->lock);
263 return IRQ_NONE; 262 return IRQ_NONE;
264 } else if (err < 0) { 263 } else if (err < 0) {
265 line->head = line->buffer; 264 line->head = line->buffer;
266 line->tail = line->buffer; 265 line->tail = line->buffer;
267 } 266 }
268 spin_unlock(&line->lock); 267 spin_unlock(&line->lock);
269 268
270 tty = tty_port_tty_get(&line->port); 269 tty_port_tty_wakeup(&line->port);
271 if (tty == NULL)
272 return IRQ_NONE;
273
274 tty_wakeup(tty);
275 tty_kref_put(tty);
276 270
277 return IRQ_HANDLED; 271 return IRQ_HANDLED;
278 } 272 }
279 273
280 int line_setup_irq(int fd, int input, int output, struct line *line, void *data) 274 int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
281 { 275 {
282 const struct line_driver *driver = line->driver; 276 const struct line_driver *driver = line->driver;
283 int err = 0; 277 int err = 0;
284 278
285 if (input) 279 if (input)
286 err = um_request_irq(driver->read_irq, fd, IRQ_READ, 280 err = um_request_irq(driver->read_irq, fd, IRQ_READ,
287 line_interrupt, IRQF_SHARED, 281 line_interrupt, IRQF_SHARED,
288 driver->read_irq_name, data); 282 driver->read_irq_name, data);
289 if (err) 283 if (err)
290 return err; 284 return err;
291 if (output) 285 if (output)
292 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE, 286 err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
293 line_write_interrupt, IRQF_SHARED, 287 line_write_interrupt, IRQF_SHARED,
294 driver->write_irq_name, data); 288 driver->write_irq_name, data);
295 return err; 289 return err;
296 } 290 }
297 291
298 static int line_activate(struct tty_port *port, struct tty_struct *tty) 292 static int line_activate(struct tty_port *port, struct tty_struct *tty)
299 { 293 {
300 int ret; 294 int ret;
301 struct line *line = tty->driver_data; 295 struct line *line = tty->driver_data;
302 296
303 ret = enable_chan(line); 297 ret = enable_chan(line);
304 if (ret) 298 if (ret)
305 return ret; 299 return ret;
306 300
307 if (!line->sigio) { 301 if (!line->sigio) {
308 chan_enable_winch(line->chan_out, tty); 302 chan_enable_winch(line->chan_out, tty);
309 line->sigio = 1; 303 line->sigio = 1;
310 } 304 }
311 305
312 chan_window_size(line, &tty->winsize.ws_row, 306 chan_window_size(line, &tty->winsize.ws_row,
313 &tty->winsize.ws_col); 307 &tty->winsize.ws_col);
314 308
315 return 0; 309 return 0;
316 } 310 }
317 311
318 static const struct tty_port_operations line_port_ops = { 312 static const struct tty_port_operations line_port_ops = {
319 .activate = line_activate, 313 .activate = line_activate,
320 }; 314 };
321 315
322 int line_open(struct tty_struct *tty, struct file *filp) 316 int line_open(struct tty_struct *tty, struct file *filp)
323 { 317 {
324 struct line *line = tty->driver_data; 318 struct line *line = tty->driver_data;
325 319
326 return tty_port_open(&line->port, tty, filp); 320 return tty_port_open(&line->port, tty, filp);
327 } 321 }
328 322
329 int line_install(struct tty_driver *driver, struct tty_struct *tty, 323 int line_install(struct tty_driver *driver, struct tty_struct *tty,
330 struct line *line) 324 struct line *line)
331 { 325 {
332 int ret; 326 int ret;
333 327
334 ret = tty_standard_install(driver, tty); 328 ret = tty_standard_install(driver, tty);
335 if (ret) 329 if (ret)
336 return ret; 330 return ret;
337 331
338 tty->driver_data = line; 332 tty->driver_data = line;
339 333
340 return 0; 334 return 0;
341 } 335 }
342 336
343 static void unregister_winch(struct tty_struct *tty); 337 static void unregister_winch(struct tty_struct *tty);
344 338
345 void line_cleanup(struct tty_struct *tty) 339 void line_cleanup(struct tty_struct *tty)
346 { 340 {
347 struct line *line = tty->driver_data; 341 struct line *line = tty->driver_data;
348 342
349 if (line->sigio) { 343 if (line->sigio) {
350 unregister_winch(tty); 344 unregister_winch(tty);
351 line->sigio = 0; 345 line->sigio = 0;
352 } 346 }
353 } 347 }
354 348
355 void line_close(struct tty_struct *tty, struct file * filp) 349 void line_close(struct tty_struct *tty, struct file * filp)
356 { 350 {
357 struct line *line = tty->driver_data; 351 struct line *line = tty->driver_data;
358 352
359 tty_port_close(&line->port, tty, filp); 353 tty_port_close(&line->port, tty, filp);
360 } 354 }
361 355
362 void line_hangup(struct tty_struct *tty) 356 void line_hangup(struct tty_struct *tty)
363 { 357 {
364 struct line *line = tty->driver_data; 358 struct line *line = tty->driver_data;
365 359
366 tty_port_hangup(&line->port); 360 tty_port_hangup(&line->port);
367 } 361 }
368 362
369 void close_lines(struct line *lines, int nlines) 363 void close_lines(struct line *lines, int nlines)
370 { 364 {
371 int i; 365 int i;
372 366
373 for(i = 0; i < nlines; i++) 367 for(i = 0; i < nlines; i++)
374 close_chan(&lines[i]); 368 close_chan(&lines[i]);
375 } 369 }
376 370
377 int setup_one_line(struct line *lines, int n, char *init, 371 int setup_one_line(struct line *lines, int n, char *init,
378 const struct chan_opts *opts, char **error_out) 372 const struct chan_opts *opts, char **error_out)
379 { 373 {
380 struct line *line = &lines[n]; 374 struct line *line = &lines[n];
381 struct tty_driver *driver = line->driver->driver; 375 struct tty_driver *driver = line->driver->driver;
382 int err = -EINVAL; 376 int err = -EINVAL;
383 377
384 if (line->port.count) { 378 if (line->port.count) {
385 *error_out = "Device is already open"; 379 *error_out = "Device is already open";
386 goto out; 380 goto out;
387 } 381 }
388 382
389 if (!strcmp(init, "none")) { 383 if (!strcmp(init, "none")) {
390 if (line->valid) { 384 if (line->valid) {
391 line->valid = 0; 385 line->valid = 0;
392 kfree(line->init_str); 386 kfree(line->init_str);
393 tty_unregister_device(driver, n); 387 tty_unregister_device(driver, n);
394 parse_chan_pair(NULL, line, n, opts, error_out); 388 parse_chan_pair(NULL, line, n, opts, error_out);
395 err = 0; 389 err = 0;
396 } 390 }
397 } else { 391 } else {
398 char *new = kstrdup(init, GFP_KERNEL); 392 char *new = kstrdup(init, GFP_KERNEL);
399 if (!new) { 393 if (!new) {
400 *error_out = "Failed to allocate memory"; 394 *error_out = "Failed to allocate memory";
401 return -ENOMEM; 395 return -ENOMEM;
402 } 396 }
403 if (line->valid) { 397 if (line->valid) {
404 tty_unregister_device(driver, n); 398 tty_unregister_device(driver, n);
405 kfree(line->init_str); 399 kfree(line->init_str);
406 } 400 }
407 line->init_str = new; 401 line->init_str = new;
408 line->valid = 1; 402 line->valid = 1;
409 err = parse_chan_pair(new, line, n, opts, error_out); 403 err = parse_chan_pair(new, line, n, opts, error_out);
410 if (!err) { 404 if (!err) {
411 struct device *d = tty_port_register_device(&line->port, 405 struct device *d = tty_port_register_device(&line->port,
412 driver, n, NULL); 406 driver, n, NULL);
413 if (IS_ERR(d)) { 407 if (IS_ERR(d)) {
414 *error_out = "Failed to register device"; 408 *error_out = "Failed to register device";
415 err = PTR_ERR(d); 409 err = PTR_ERR(d);
416 parse_chan_pair(NULL, line, n, opts, error_out); 410 parse_chan_pair(NULL, line, n, opts, error_out);
417 } 411 }
418 } 412 }
419 if (err) { 413 if (err) {
420 line->init_str = NULL; 414 line->init_str = NULL;
421 line->valid = 0; 415 line->valid = 0;
422 kfree(new); 416 kfree(new);
423 } 417 }
424 } 418 }
425 out: 419 out:
426 return err; 420 return err;
427 } 421 }
428 422
429 /* 423 /*
430 * Common setup code for both startup command line and mconsole initialization. 424 * Common setup code for both startup command line and mconsole initialization.
431 * @lines contains the array (of size @num) to modify; 425 * @lines contains the array (of size @num) to modify;
432 * @init is the setup string; 426 * @init is the setup string;
433 * @error_out is an error string in the case of failure; 427 * @error_out is an error string in the case of failure;
434 */ 428 */
435 429
436 int line_setup(char **conf, unsigned int num, char **def, 430 int line_setup(char **conf, unsigned int num, char **def,
437 char *init, char *name) 431 char *init, char *name)
438 { 432 {
439 char *error; 433 char *error;
440 434
441 if (*init == '=') { 435 if (*init == '=') {
442 /* 436 /*
443 * We said con=/ssl= instead of con#=, so we are configuring all 437 * We said con=/ssl= instead of con#=, so we are configuring all
444 * consoles at once. 438 * consoles at once.
445 */ 439 */
446 *def = init + 1; 440 *def = init + 1;
447 } else { 441 } else {
448 char *end; 442 char *end;
449 unsigned n = simple_strtoul(init, &end, 0); 443 unsigned n = simple_strtoul(init, &end, 0);
450 444
451 if (*end != '=') { 445 if (*end != '=') {
452 error = "Couldn't parse device number"; 446 error = "Couldn't parse device number";
453 goto out; 447 goto out;
454 } 448 }
455 if (n >= num) { 449 if (n >= num) {
456 error = "Device number out of range"; 450 error = "Device number out of range";
457 goto out; 451 goto out;
458 } 452 }
459 conf[n] = end + 1; 453 conf[n] = end + 1;
460 } 454 }
461 return 0; 455 return 0;
462 456
463 out: 457 out:
464 printk(KERN_ERR "Failed to set up %s with " 458 printk(KERN_ERR "Failed to set up %s with "
465 "configuration string \"%s\" : %s\n", name, init, error); 459 "configuration string \"%s\" : %s\n", name, init, error);
466 return -EINVAL; 460 return -EINVAL;
467 } 461 }
468 462
469 int line_config(struct line *lines, unsigned int num, char *str, 463 int line_config(struct line *lines, unsigned int num, char *str,
470 const struct chan_opts *opts, char **error_out) 464 const struct chan_opts *opts, char **error_out)
471 { 465 {
472 char *end; 466 char *end;
473 int n; 467 int n;
474 468
475 if (*str == '=') { 469 if (*str == '=') {
476 *error_out = "Can't configure all devices from mconsole"; 470 *error_out = "Can't configure all devices from mconsole";
477 return -EINVAL; 471 return -EINVAL;
478 } 472 }
479 473
480 n = simple_strtoul(str, &end, 0); 474 n = simple_strtoul(str, &end, 0);
481 if (*end++ != '=') { 475 if (*end++ != '=') {
482 *error_out = "Couldn't parse device number"; 476 *error_out = "Couldn't parse device number";
483 return -EINVAL; 477 return -EINVAL;
484 } 478 }
485 if (n >= num) { 479 if (n >= num) {
486 *error_out = "Device number out of range"; 480 *error_out = "Device number out of range";
487 return -EINVAL; 481 return -EINVAL;
488 } 482 }
489 483
490 return setup_one_line(lines, n, end, opts, error_out); 484 return setup_one_line(lines, n, end, opts, error_out);
491 } 485 }
492 486
493 int line_get_config(char *name, struct line *lines, unsigned int num, char *str, 487 int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
494 int size, char **error_out) 488 int size, char **error_out)
495 { 489 {
496 struct line *line; 490 struct line *line;
497 char *end; 491 char *end;
498 int dev, n = 0; 492 int dev, n = 0;
499 493
500 dev = simple_strtoul(name, &end, 0); 494 dev = simple_strtoul(name, &end, 0);
501 if ((*end != '\0') || (end == name)) { 495 if ((*end != '\0') || (end == name)) {
502 *error_out = "line_get_config failed to parse device number"; 496 *error_out = "line_get_config failed to parse device number";
503 return 0; 497 return 0;
504 } 498 }
505 499
506 if ((dev < 0) || (dev >= num)) { 500 if ((dev < 0) || (dev >= num)) {
507 *error_out = "device number out of range"; 501 *error_out = "device number out of range";
508 return 0; 502 return 0;
509 } 503 }
510 504
511 line = &lines[dev]; 505 line = &lines[dev];
512 506
513 if (!line->valid) 507 if (!line->valid)
514 CONFIG_CHUNK(str, size, n, "none", 1); 508 CONFIG_CHUNK(str, size, n, "none", 1);
515 else { 509 else {
516 struct tty_struct *tty = tty_port_tty_get(&line->port); 510 struct tty_struct *tty = tty_port_tty_get(&line->port);
517 if (tty == NULL) { 511 if (tty == NULL) {
518 CONFIG_CHUNK(str, size, n, line->init_str, 1); 512 CONFIG_CHUNK(str, size, n, line->init_str, 1);
519 } else { 513 } else {
520 n = chan_config_string(line, str, size, error_out); 514 n = chan_config_string(line, str, size, error_out);
521 tty_kref_put(tty); 515 tty_kref_put(tty);
522 } 516 }
523 } 517 }
524 518
525 return n; 519 return n;
526 } 520 }
527 521
528 int line_id(char **str, int *start_out, int *end_out) 522 int line_id(char **str, int *start_out, int *end_out)
529 { 523 {
530 char *end; 524 char *end;
531 int n; 525 int n;
532 526
533 n = simple_strtoul(*str, &end, 0); 527 n = simple_strtoul(*str, &end, 0);
534 if ((*end != '\0') || (end == *str)) 528 if ((*end != '\0') || (end == *str))
535 return -1; 529 return -1;
536 530
537 *str = end; 531 *str = end;
538 *start_out = n; 532 *start_out = n;
539 *end_out = n; 533 *end_out = n;
540 return n; 534 return n;
541 } 535 }
542 536
543 int line_remove(struct line *lines, unsigned int num, int n, char **error_out) 537 int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
544 { 538 {
545 if (n >= num) { 539 if (n >= num) {
546 *error_out = "Device number out of range"; 540 *error_out = "Device number out of range";
547 return -EINVAL; 541 return -EINVAL;
548 } 542 }
549 return setup_one_line(lines, n, "none", NULL, error_out); 543 return setup_one_line(lines, n, "none", NULL, error_out);
550 } 544 }
551 545
552 int register_lines(struct line_driver *line_driver, 546 int register_lines(struct line_driver *line_driver,
553 const struct tty_operations *ops, 547 const struct tty_operations *ops,
554 struct line *lines, int nlines) 548 struct line *lines, int nlines)
555 { 549 {
556 struct tty_driver *driver = alloc_tty_driver(nlines); 550 struct tty_driver *driver = alloc_tty_driver(nlines);
557 int err; 551 int err;
558 int i; 552 int i;
559 553
560 if (!driver) 554 if (!driver)
561 return -ENOMEM; 555 return -ENOMEM;
562 556
563 driver->driver_name = line_driver->name; 557 driver->driver_name = line_driver->name;
564 driver->name = line_driver->device_name; 558 driver->name = line_driver->device_name;
565 driver->major = line_driver->major; 559 driver->major = line_driver->major;
566 driver->minor_start = line_driver->minor_start; 560 driver->minor_start = line_driver->minor_start;
567 driver->type = line_driver->type; 561 driver->type = line_driver->type;
568 driver->subtype = line_driver->subtype; 562 driver->subtype = line_driver->subtype;
569 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 563 driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
570 driver->init_termios = tty_std_termios; 564 driver->init_termios = tty_std_termios;
571 565
572 for (i = 0; i < nlines; i++) { 566 for (i = 0; i < nlines; i++) {
573 tty_port_init(&lines[i].port); 567 tty_port_init(&lines[i].port);
574 lines[i].port.ops = &line_port_ops; 568 lines[i].port.ops = &line_port_ops;
575 spin_lock_init(&lines[i].lock); 569 spin_lock_init(&lines[i].lock);
576 lines[i].driver = line_driver; 570 lines[i].driver = line_driver;
577 INIT_LIST_HEAD(&lines[i].chan_list); 571 INIT_LIST_HEAD(&lines[i].chan_list);
578 } 572 }
579 tty_set_operations(driver, ops); 573 tty_set_operations(driver, ops);
580 574
581 err = tty_register_driver(driver); 575 err = tty_register_driver(driver);
582 if (err) { 576 if (err) {
583 printk(KERN_ERR "register_lines : can't register %s driver\n", 577 printk(KERN_ERR "register_lines : can't register %s driver\n",
584 line_driver->name); 578 line_driver->name);
585 put_tty_driver(driver); 579 put_tty_driver(driver);
586 for (i = 0; i < nlines; i++) 580 for (i = 0; i < nlines; i++)
587 tty_port_destroy(&lines[i].port); 581 tty_port_destroy(&lines[i].port);
588 return err; 582 return err;
589 } 583 }
590 584
591 line_driver->driver = driver; 585 line_driver->driver = driver;
592 mconsole_register_dev(&line_driver->mc); 586 mconsole_register_dev(&line_driver->mc);
593 return 0; 587 return 0;
594 } 588 }
595 589
596 static DEFINE_SPINLOCK(winch_handler_lock); 590 static DEFINE_SPINLOCK(winch_handler_lock);
597 static LIST_HEAD(winch_handlers); 591 static LIST_HEAD(winch_handlers);
598 592
599 struct winch { 593 struct winch {
600 struct list_head list; 594 struct list_head list;
601 int fd; 595 int fd;
602 int tty_fd; 596 int tty_fd;
603 int pid; 597 int pid;
604 struct tty_struct *tty; 598 struct tty_struct *tty;
605 unsigned long stack; 599 unsigned long stack;
606 struct work_struct work; 600 struct work_struct work;
607 }; 601 };
608 602
609 static void __free_winch(struct work_struct *work) 603 static void __free_winch(struct work_struct *work)
610 { 604 {
611 struct winch *winch = container_of(work, struct winch, work); 605 struct winch *winch = container_of(work, struct winch, work);
612 um_free_irq(WINCH_IRQ, winch); 606 um_free_irq(WINCH_IRQ, winch);
613 607
614 if (winch->pid != -1) 608 if (winch->pid != -1)
615 os_kill_process(winch->pid, 1); 609 os_kill_process(winch->pid, 1);
616 if (winch->stack != 0) 610 if (winch->stack != 0)
617 free_stack(winch->stack, 0); 611 free_stack(winch->stack, 0);
618 kfree(winch); 612 kfree(winch);
619 } 613 }
620 614
621 static void free_winch(struct winch *winch) 615 static void free_winch(struct winch *winch)
622 { 616 {
623 int fd = winch->fd; 617 int fd = winch->fd;
624 winch->fd = -1; 618 winch->fd = -1;
625 if (fd != -1) 619 if (fd != -1)
626 os_close_file(fd); 620 os_close_file(fd);
627 list_del(&winch->list); 621 list_del(&winch->list);
628 __free_winch(&winch->work); 622 __free_winch(&winch->work);
629 } 623 }
630 624
631 static irqreturn_t winch_interrupt(int irq, void *data) 625 static irqreturn_t winch_interrupt(int irq, void *data)
632 { 626 {
633 struct winch *winch = data; 627 struct winch *winch = data;
634 struct tty_struct *tty; 628 struct tty_struct *tty;
635 struct line *line; 629 struct line *line;
636 int fd = winch->fd; 630 int fd = winch->fd;
637 int err; 631 int err;
638 char c; 632 char c;
639 633
640 if (fd != -1) { 634 if (fd != -1) {
641 err = generic_read(fd, &c, NULL); 635 err = generic_read(fd, &c, NULL);
642 if (err < 0) { 636 if (err < 0) {
643 if (err != -EAGAIN) { 637 if (err != -EAGAIN) {
644 winch->fd = -1; 638 winch->fd = -1;
645 list_del(&winch->list); 639 list_del(&winch->list);
646 os_close_file(fd); 640 os_close_file(fd);
647 printk(KERN_ERR "winch_interrupt : " 641 printk(KERN_ERR "winch_interrupt : "
648 "read failed, errno = %d\n", -err); 642 "read failed, errno = %d\n", -err);
649 printk(KERN_ERR "fd %d is losing SIGWINCH " 643 printk(KERN_ERR "fd %d is losing SIGWINCH "
650 "support\n", winch->tty_fd); 644 "support\n", winch->tty_fd);
651 INIT_WORK(&winch->work, __free_winch); 645 INIT_WORK(&winch->work, __free_winch);
652 schedule_work(&winch->work); 646 schedule_work(&winch->work);
653 return IRQ_HANDLED; 647 return IRQ_HANDLED;
654 } 648 }
655 goto out; 649 goto out;
656 } 650 }
657 } 651 }
658 tty = winch->tty; 652 tty = winch->tty;
659 if (tty != NULL) { 653 if (tty != NULL) {
660 line = tty->driver_data; 654 line = tty->driver_data;
661 if (line != NULL) { 655 if (line != NULL) {
662 chan_window_size(line, &tty->winsize.ws_row, 656 chan_window_size(line, &tty->winsize.ws_row,
663 &tty->winsize.ws_col); 657 &tty->winsize.ws_col);
664 kill_pgrp(tty->pgrp, SIGWINCH, 1); 658 kill_pgrp(tty->pgrp, SIGWINCH, 1);
665 } 659 }
666 } 660 }
667 out: 661 out:
668 if (winch->fd != -1) 662 if (winch->fd != -1)
669 reactivate_fd(winch->fd, WINCH_IRQ); 663 reactivate_fd(winch->fd, WINCH_IRQ);
670 return IRQ_HANDLED; 664 return IRQ_HANDLED;
671 } 665 }
672 666
673 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty, 667 void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
674 unsigned long stack) 668 unsigned long stack)
675 { 669 {
676 struct winch *winch; 670 struct winch *winch;
677 671
678 winch = kmalloc(sizeof(*winch), GFP_KERNEL); 672 winch = kmalloc(sizeof(*winch), GFP_KERNEL);
679 if (winch == NULL) { 673 if (winch == NULL) {
680 printk(KERN_ERR "register_winch_irq - kmalloc failed\n"); 674 printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
681 goto cleanup; 675 goto cleanup;
682 } 676 }
683 677
684 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list), 678 *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
685 .fd = fd, 679 .fd = fd,
686 .tty_fd = tty_fd, 680 .tty_fd = tty_fd,
687 .pid = pid, 681 .pid = pid,
688 .tty = tty, 682 .tty = tty,
689 .stack = stack }); 683 .stack = stack });
690 684
691 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt, 685 if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
692 IRQF_SHARED, "winch", winch) < 0) { 686 IRQF_SHARED, "winch", winch) < 0) {
693 printk(KERN_ERR "register_winch_irq - failed to register " 687 printk(KERN_ERR "register_winch_irq - failed to register "
694 "IRQ\n"); 688 "IRQ\n");
695 goto out_free; 689 goto out_free;
696 } 690 }
697 691
698 spin_lock(&winch_handler_lock); 692 spin_lock(&winch_handler_lock);
699 list_add(&winch->list, &winch_handlers); 693 list_add(&winch->list, &winch_handlers);
700 spin_unlock(&winch_handler_lock); 694 spin_unlock(&winch_handler_lock);
701 695
702 return; 696 return;
703 697
704 out_free: 698 out_free:
705 kfree(winch); 699 kfree(winch);
706 cleanup: 700 cleanup:
707 os_kill_process(pid, 1); 701 os_kill_process(pid, 1);
708 os_close_file(fd); 702 os_close_file(fd);
709 if (stack != 0) 703 if (stack != 0)
710 free_stack(stack, 0); 704 free_stack(stack, 0);
711 } 705 }
712 706
713 static void unregister_winch(struct tty_struct *tty) 707 static void unregister_winch(struct tty_struct *tty)
714 { 708 {
715 struct list_head *ele, *next; 709 struct list_head *ele, *next;
716 struct winch *winch; 710 struct winch *winch;
717 711
718 spin_lock(&winch_handler_lock); 712 spin_lock(&winch_handler_lock);
719 713
720 list_for_each_safe(ele, next, &winch_handlers) { 714 list_for_each_safe(ele, next, &winch_handlers) {
721 winch = list_entry(ele, struct winch, list); 715 winch = list_entry(ele, struct winch, list);
722 if (winch->tty == tty) { 716 if (winch->tty == tty) {
723 free_winch(winch); 717 free_winch(winch);
724 break; 718 break;
725 } 719 }
726 } 720 }
727 spin_unlock(&winch_handler_lock); 721 spin_unlock(&winch_handler_lock);
728 } 722 }
729 723
730 static void winch_cleanup(void) 724 static void winch_cleanup(void)
731 { 725 {
732 struct list_head *ele, *next; 726 struct list_head *ele, *next;
733 struct winch *winch; 727 struct winch *winch;
734 728
735 spin_lock(&winch_handler_lock); 729 spin_lock(&winch_handler_lock);
736 730
737 list_for_each_safe(ele, next, &winch_handlers) { 731 list_for_each_safe(ele, next, &winch_handlers) {
738 winch = list_entry(ele, struct winch, list); 732 winch = list_entry(ele, struct winch, list);
739 free_winch(winch); 733 free_winch(winch);
740 } 734 }
741 735
742 spin_unlock(&winch_handler_lock); 736 spin_unlock(&winch_handler_lock);
743 } 737 }
744 __uml_exitcall(winch_cleanup); 738 __uml_exitcall(winch_cleanup);
745 739
746 char *add_xterm_umid(char *base) 740 char *add_xterm_umid(char *base)
747 { 741 {
748 char *umid, *title; 742 char *umid, *title;
749 int len; 743 int len;
750 744
751 umid = get_umid(); 745 umid = get_umid();
752 if (*umid == '\0') 746 if (*umid == '\0')
753 return base; 747 return base;
754 748
755 len = strlen(base) + strlen(" ()") + strlen(umid) + 1; 749 len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
756 title = kmalloc(len, GFP_KERNEL); 750 title = kmalloc(len, GFP_KERNEL);
757 if (title == NULL) { 751 if (title == NULL) {
758 printk(KERN_ERR "Failed to allocate buffer for xterm title\n"); 752 printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
759 return base; 753 return base;
760 } 754 }
761 755
762 snprintf(title, len, "%s (%s)", base, umid); 756 snprintf(title, len, "%s (%s)", base, umid);
763 return title; 757 return title;
764 } 758 }
765 759
drivers/isdn/capi/capi.c
1 /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $ 1 /* $Id: capi.c,v 1.1.2.7 2004/04/28 09:48:59 armin Exp $
2 * 2 *
3 * CAPI 2.0 Interface for Linux 3 * CAPI 2.0 Interface for Linux
4 * 4 *
5 * Copyright 1996 by Carsten Paeth <calle@calle.de> 5 * Copyright 1996 by Carsten Paeth <calle@calle.de>
6 * 6 *
7 * This software may be used and distributed according to the terms 7 * This software may be used and distributed according to the terms
8 * of the GNU General Public License, incorporated herein by reference. 8 * of the GNU General Public License, incorporated herein by reference.
9 * 9 *
10 */ 10 */
11 11
12 #include <linux/module.h> 12 #include <linux/module.h>
13 #include <linux/errno.h> 13 #include <linux/errno.h>
14 #include <linux/kernel.h> 14 #include <linux/kernel.h>
15 #include <linux/major.h> 15 #include <linux/major.h>
16 #include <linux/sched.h> 16 #include <linux/sched.h>
17 #include <linux/slab.h> 17 #include <linux/slab.h>
18 #include <linux/fcntl.h> 18 #include <linux/fcntl.h>
19 #include <linux/fs.h> 19 #include <linux/fs.h>
20 #include <linux/signal.h> 20 #include <linux/signal.h>
21 #include <linux/mutex.h> 21 #include <linux/mutex.h>
22 #include <linux/mm.h> 22 #include <linux/mm.h>
23 #include <linux/timer.h> 23 #include <linux/timer.h>
24 #include <linux/wait.h> 24 #include <linux/wait.h>
25 #include <linux/tty.h> 25 #include <linux/tty.h>
26 #include <linux/netdevice.h> 26 #include <linux/netdevice.h>
27 #include <linux/ppp_defs.h> 27 #include <linux/ppp_defs.h>
28 #include <linux/ppp-ioctl.h> 28 #include <linux/ppp-ioctl.h>
29 #include <linux/skbuff.h> 29 #include <linux/skbuff.h>
30 #include <linux/proc_fs.h> 30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h> 31 #include <linux/seq_file.h>
32 #include <linux/poll.h> 32 #include <linux/poll.h>
33 #include <linux/capi.h> 33 #include <linux/capi.h>
34 #include <linux/kernelcapi.h> 34 #include <linux/kernelcapi.h>
35 #include <linux/init.h> 35 #include <linux/init.h>
36 #include <linux/device.h> 36 #include <linux/device.h>
37 #include <linux/moduleparam.h> 37 #include <linux/moduleparam.h>
38 #include <linux/isdn/capiutil.h> 38 #include <linux/isdn/capiutil.h>
39 #include <linux/isdn/capicmd.h> 39 #include <linux/isdn/capicmd.h>
40 40
41 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface"); 41 MODULE_DESCRIPTION("CAPI4Linux: Userspace /dev/capi20 interface");
42 MODULE_AUTHOR("Carsten Paeth"); 42 MODULE_AUTHOR("Carsten Paeth");
43 MODULE_LICENSE("GPL"); 43 MODULE_LICENSE("GPL");
44 44
45 /* -------- driver information -------------------------------------- */ 45 /* -------- driver information -------------------------------------- */
46 46
47 static DEFINE_MUTEX(capi_mutex); 47 static DEFINE_MUTEX(capi_mutex);
48 static struct class *capi_class; 48 static struct class *capi_class;
49 static int capi_major = 68; /* allocated */ 49 static int capi_major = 68; /* allocated */
50 50
51 module_param_named(major, capi_major, uint, 0); 51 module_param_named(major, capi_major, uint, 0);
52 52
53 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE 53 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
54 #define CAPINC_NR_PORTS 32 54 #define CAPINC_NR_PORTS 32
55 #define CAPINC_MAX_PORTS 256 55 #define CAPINC_MAX_PORTS 256
56 56
57 static int capi_ttyminors = CAPINC_NR_PORTS; 57 static int capi_ttyminors = CAPINC_NR_PORTS;
58 58
59 module_param_named(ttyminors, capi_ttyminors, uint, 0); 59 module_param_named(ttyminors, capi_ttyminors, uint, 0);
60 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ 60 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
61 61
62 /* -------- defines ------------------------------------------------- */ 62 /* -------- defines ------------------------------------------------- */
63 63
64 #define CAPINC_MAX_RECVQUEUE 10 64 #define CAPINC_MAX_RECVQUEUE 10
65 #define CAPINC_MAX_SENDQUEUE 10 65 #define CAPINC_MAX_SENDQUEUE 10
66 #define CAPI_MAX_BLKSIZE 2048 66 #define CAPI_MAX_BLKSIZE 2048
67 67
68 /* -------- data structures ----------------------------------------- */ 68 /* -------- data structures ----------------------------------------- */
69 69
70 struct capidev; 70 struct capidev;
71 struct capincci; 71 struct capincci;
72 struct capiminor; 72 struct capiminor;
73 73
74 struct ackqueue_entry { 74 struct ackqueue_entry {
75 struct list_head list; 75 struct list_head list;
76 u16 datahandle; 76 u16 datahandle;
77 }; 77 };
78 78
79 struct capiminor { 79 struct capiminor {
80 unsigned int minor; 80 unsigned int minor;
81 81
82 struct capi20_appl *ap; 82 struct capi20_appl *ap;
83 u32 ncci; 83 u32 ncci;
84 atomic_t datahandle; 84 atomic_t datahandle;
85 atomic_t msgid; 85 atomic_t msgid;
86 86
87 struct tty_port port; 87 struct tty_port port;
88 int ttyinstop; 88 int ttyinstop;
89 int ttyoutstop; 89 int ttyoutstop;
90 90
91 struct sk_buff_head inqueue; 91 struct sk_buff_head inqueue;
92 92
93 struct sk_buff_head outqueue; 93 struct sk_buff_head outqueue;
94 int outbytes; 94 int outbytes;
95 struct sk_buff *outskb; 95 struct sk_buff *outskb;
96 spinlock_t outlock; 96 spinlock_t outlock;
97 97
98 /* transmit path */ 98 /* transmit path */
99 struct list_head ackqueue; 99 struct list_head ackqueue;
100 int nack; 100 int nack;
101 spinlock_t ackqlock; 101 spinlock_t ackqlock;
102 }; 102 };
103 103
104 struct capincci { 104 struct capincci {
105 struct list_head list; 105 struct list_head list;
106 u32 ncci; 106 u32 ncci;
107 struct capidev *cdev; 107 struct capidev *cdev;
108 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE 108 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
109 struct capiminor *minorp; 109 struct capiminor *minorp;
110 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ 110 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
111 }; 111 };
112 112
113 struct capidev { 113 struct capidev {
114 struct list_head list; 114 struct list_head list;
115 struct capi20_appl ap; 115 struct capi20_appl ap;
116 u16 errcode; 116 u16 errcode;
117 unsigned userflags; 117 unsigned userflags;
118 118
119 struct sk_buff_head recvqueue; 119 struct sk_buff_head recvqueue;
120 wait_queue_head_t recvwait; 120 wait_queue_head_t recvwait;
121 121
122 struct list_head nccis; 122 struct list_head nccis;
123 123
124 struct mutex lock; 124 struct mutex lock;
125 }; 125 };
126 126
127 /* -------- global variables ---------------------------------------- */ 127 /* -------- global variables ---------------------------------------- */
128 128
129 static DEFINE_MUTEX(capidev_list_lock); 129 static DEFINE_MUTEX(capidev_list_lock);
130 static LIST_HEAD(capidev_list); 130 static LIST_HEAD(capidev_list);
131 131
132 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE 132 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
133 133
134 static DEFINE_SPINLOCK(capiminors_lock); 134 static DEFINE_SPINLOCK(capiminors_lock);
135 static struct capiminor **capiminors; 135 static struct capiminor **capiminors;
136 136
137 static struct tty_driver *capinc_tty_driver; 137 static struct tty_driver *capinc_tty_driver;
138 138
139 /* -------- datahandles --------------------------------------------- */ 139 /* -------- datahandles --------------------------------------------- */
140 140
141 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle) 141 static int capiminor_add_ack(struct capiminor *mp, u16 datahandle)
142 { 142 {
143 struct ackqueue_entry *n; 143 struct ackqueue_entry *n;
144 144
145 n = kmalloc(sizeof(*n), GFP_ATOMIC); 145 n = kmalloc(sizeof(*n), GFP_ATOMIC);
146 if (unlikely(!n)) { 146 if (unlikely(!n)) {
147 printk(KERN_ERR "capi: alloc datahandle failed\n"); 147 printk(KERN_ERR "capi: alloc datahandle failed\n");
148 return -1; 148 return -1;
149 } 149 }
150 n->datahandle = datahandle; 150 n->datahandle = datahandle;
151 INIT_LIST_HEAD(&n->list); 151 INIT_LIST_HEAD(&n->list);
152 spin_lock_bh(&mp->ackqlock); 152 spin_lock_bh(&mp->ackqlock);
153 list_add_tail(&n->list, &mp->ackqueue); 153 list_add_tail(&n->list, &mp->ackqueue);
154 mp->nack++; 154 mp->nack++;
155 spin_unlock_bh(&mp->ackqlock); 155 spin_unlock_bh(&mp->ackqlock);
156 return 0; 156 return 0;
157 } 157 }
158 158
159 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle) 159 static int capiminor_del_ack(struct capiminor *mp, u16 datahandle)
160 { 160 {
161 struct ackqueue_entry *p, *tmp; 161 struct ackqueue_entry *p, *tmp;
162 162
163 spin_lock_bh(&mp->ackqlock); 163 spin_lock_bh(&mp->ackqlock);
164 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) { 164 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
165 if (p->datahandle == datahandle) { 165 if (p->datahandle == datahandle) {
166 list_del(&p->list); 166 list_del(&p->list);
167 mp->nack--; 167 mp->nack--;
168 spin_unlock_bh(&mp->ackqlock); 168 spin_unlock_bh(&mp->ackqlock);
169 kfree(p); 169 kfree(p);
170 return 0; 170 return 0;
171 } 171 }
172 } 172 }
173 spin_unlock_bh(&mp->ackqlock); 173 spin_unlock_bh(&mp->ackqlock);
174 return -1; 174 return -1;
175 } 175 }
176 176
177 static void capiminor_del_all_ack(struct capiminor *mp) 177 static void capiminor_del_all_ack(struct capiminor *mp)
178 { 178 {
179 struct ackqueue_entry *p, *tmp; 179 struct ackqueue_entry *p, *tmp;
180 180
181 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) { 181 list_for_each_entry_safe(p, tmp, &mp->ackqueue, list) {
182 list_del(&p->list); 182 list_del(&p->list);
183 kfree(p); 183 kfree(p);
184 mp->nack--; 184 mp->nack--;
185 } 185 }
186 } 186 }
187 187
188 188
189 /* -------- struct capiminor ---------------------------------------- */ 189 /* -------- struct capiminor ---------------------------------------- */
190 190
191 static void capiminor_destroy(struct tty_port *port) 191 static void capiminor_destroy(struct tty_port *port)
192 { 192 {
193 struct capiminor *mp = container_of(port, struct capiminor, port); 193 struct capiminor *mp = container_of(port, struct capiminor, port);
194 194
195 kfree_skb(mp->outskb); 195 kfree_skb(mp->outskb);
196 skb_queue_purge(&mp->inqueue); 196 skb_queue_purge(&mp->inqueue);
197 skb_queue_purge(&mp->outqueue); 197 skb_queue_purge(&mp->outqueue);
198 capiminor_del_all_ack(mp); 198 capiminor_del_all_ack(mp);
199 kfree(mp); 199 kfree(mp);
200 } 200 }
201 201
202 static const struct tty_port_operations capiminor_port_ops = { 202 static const struct tty_port_operations capiminor_port_ops = {
203 .destruct = capiminor_destroy, 203 .destruct = capiminor_destroy,
204 }; 204 };
205 205
206 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci) 206 static struct capiminor *capiminor_alloc(struct capi20_appl *ap, u32 ncci)
207 { 207 {
208 struct capiminor *mp; 208 struct capiminor *mp;
209 struct device *dev; 209 struct device *dev;
210 unsigned int minor; 210 unsigned int minor;
211 211
212 mp = kzalloc(sizeof(*mp), GFP_KERNEL); 212 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
213 if (!mp) { 213 if (!mp) {
214 printk(KERN_ERR "capi: can't alloc capiminor\n"); 214 printk(KERN_ERR "capi: can't alloc capiminor\n");
215 return NULL; 215 return NULL;
216 } 216 }
217 217
218 mp->ap = ap; 218 mp->ap = ap;
219 mp->ncci = ncci; 219 mp->ncci = ncci;
220 INIT_LIST_HEAD(&mp->ackqueue); 220 INIT_LIST_HEAD(&mp->ackqueue);
221 spin_lock_init(&mp->ackqlock); 221 spin_lock_init(&mp->ackqlock);
222 222
223 skb_queue_head_init(&mp->inqueue); 223 skb_queue_head_init(&mp->inqueue);
224 skb_queue_head_init(&mp->outqueue); 224 skb_queue_head_init(&mp->outqueue);
225 spin_lock_init(&mp->outlock); 225 spin_lock_init(&mp->outlock);
226 226
227 tty_port_init(&mp->port); 227 tty_port_init(&mp->port);
228 mp->port.ops = &capiminor_port_ops; 228 mp->port.ops = &capiminor_port_ops;
229 229
230 /* Allocate the least unused minor number. */ 230 /* Allocate the least unused minor number. */
231 spin_lock(&capiminors_lock); 231 spin_lock(&capiminors_lock);
232 for (minor = 0; minor < capi_ttyminors; minor++) 232 for (minor = 0; minor < capi_ttyminors; minor++)
233 if (!capiminors[minor]) { 233 if (!capiminors[minor]) {
234 capiminors[minor] = mp; 234 capiminors[minor] = mp;
235 break; 235 break;
236 } 236 }
237 spin_unlock(&capiminors_lock); 237 spin_unlock(&capiminors_lock);
238 238
239 if (minor == capi_ttyminors) { 239 if (minor == capi_ttyminors) {
240 printk(KERN_NOTICE "capi: out of minors\n"); 240 printk(KERN_NOTICE "capi: out of minors\n");
241 goto err_out1; 241 goto err_out1;
242 } 242 }
243 243
244 mp->minor = minor; 244 mp->minor = minor;
245 245
246 dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor, 246 dev = tty_port_register_device(&mp->port, capinc_tty_driver, minor,
247 NULL); 247 NULL);
248 if (IS_ERR(dev)) 248 if (IS_ERR(dev))
249 goto err_out2; 249 goto err_out2;
250 250
251 return mp; 251 return mp;
252 252
253 err_out2: 253 err_out2:
254 spin_lock(&capiminors_lock); 254 spin_lock(&capiminors_lock);
255 capiminors[minor] = NULL; 255 capiminors[minor] = NULL;
256 spin_unlock(&capiminors_lock); 256 spin_unlock(&capiminors_lock);
257 257
258 err_out1: 258 err_out1:
259 tty_port_put(&mp->port); 259 tty_port_put(&mp->port);
260 return NULL; 260 return NULL;
261 } 261 }
262 262
263 static struct capiminor *capiminor_get(unsigned int minor) 263 static struct capiminor *capiminor_get(unsigned int minor)
264 { 264 {
265 struct capiminor *mp; 265 struct capiminor *mp;
266 266
267 spin_lock(&capiminors_lock); 267 spin_lock(&capiminors_lock);
268 mp = capiminors[minor]; 268 mp = capiminors[minor];
269 if (mp) 269 if (mp)
270 tty_port_get(&mp->port); 270 tty_port_get(&mp->port);
271 spin_unlock(&capiminors_lock); 271 spin_unlock(&capiminors_lock);
272 272
273 return mp; 273 return mp;
274 } 274 }
275 275
276 static inline void capiminor_put(struct capiminor *mp) 276 static inline void capiminor_put(struct capiminor *mp)
277 { 277 {
278 tty_port_put(&mp->port); 278 tty_port_put(&mp->port);
279 } 279 }
280 280
281 static void capiminor_free(struct capiminor *mp) 281 static void capiminor_free(struct capiminor *mp)
282 { 282 {
283 tty_unregister_device(capinc_tty_driver, mp->minor); 283 tty_unregister_device(capinc_tty_driver, mp->minor);
284 284
285 spin_lock(&capiminors_lock); 285 spin_lock(&capiminors_lock);
286 capiminors[mp->minor] = NULL; 286 capiminors[mp->minor] = NULL;
287 spin_unlock(&capiminors_lock); 287 spin_unlock(&capiminors_lock);
288 288
289 capiminor_put(mp); 289 capiminor_put(mp);
290 } 290 }
291 291
292 /* -------- struct capincci ----------------------------------------- */ 292 /* -------- struct capincci ----------------------------------------- */
293 293
294 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np) 294 static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
295 { 295 {
296 if (cdev->userflags & CAPIFLAG_HIGHJACKING) 296 if (cdev->userflags & CAPIFLAG_HIGHJACKING)
297 np->minorp = capiminor_alloc(&cdev->ap, np->ncci); 297 np->minorp = capiminor_alloc(&cdev->ap, np->ncci);
298 } 298 }
299 299
300 static void capincci_free_minor(struct capincci *np) 300 static void capincci_free_minor(struct capincci *np)
301 { 301 {
302 struct capiminor *mp = np->minorp; 302 struct capiminor *mp = np->minorp;
303 struct tty_struct *tty; 303 struct tty_struct *tty;
304 304
305 if (mp) { 305 if (mp) {
306 tty = tty_port_tty_get(&mp->port); 306 tty = tty_port_tty_get(&mp->port);
307 if (tty) { 307 if (tty) {
308 tty_vhangup(tty); 308 tty_vhangup(tty);
309 tty_kref_put(tty); 309 tty_kref_put(tty);
310 } 310 }
311 311
312 capiminor_free(mp); 312 capiminor_free(mp);
313 } 313 }
314 } 314 }
315 315
316 static inline unsigned int capincci_minor_opencount(struct capincci *np) 316 static inline unsigned int capincci_minor_opencount(struct capincci *np)
317 { 317 {
318 struct capiminor *mp = np->minorp; 318 struct capiminor *mp = np->minorp;
319 unsigned int count = 0; 319 unsigned int count = 0;
320 struct tty_struct *tty; 320 struct tty_struct *tty;
321 321
322 if (mp) { 322 if (mp) {
323 tty = tty_port_tty_get(&mp->port); 323 tty = tty_port_tty_get(&mp->port);
324 if (tty) { 324 if (tty) {
325 count = tty->count; 325 count = tty->count;
326 tty_kref_put(tty); 326 tty_kref_put(tty);
327 } 327 }
328 } 328 }
329 return count; 329 return count;
330 } 330 }
331 331
332 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ 332 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
333 333
334 static inline void 334 static inline void
335 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { } 335 capincci_alloc_minor(struct capidev *cdev, struct capincci *np) { }
336 static inline void capincci_free_minor(struct capincci *np) { } 336 static inline void capincci_free_minor(struct capincci *np) { }
337 337
338 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ 338 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
339 339
340 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci) 340 static struct capincci *capincci_alloc(struct capidev *cdev, u32 ncci)
341 { 341 {
342 struct capincci *np; 342 struct capincci *np;
343 343
344 np = kzalloc(sizeof(*np), GFP_KERNEL); 344 np = kzalloc(sizeof(*np), GFP_KERNEL);
345 if (!np) 345 if (!np)
346 return NULL; 346 return NULL;
347 np->ncci = ncci; 347 np->ncci = ncci;
348 np->cdev = cdev; 348 np->cdev = cdev;
349 349
350 capincci_alloc_minor(cdev, np); 350 capincci_alloc_minor(cdev, np);
351 351
352 list_add_tail(&np->list, &cdev->nccis); 352 list_add_tail(&np->list, &cdev->nccis);
353 353
354 return np; 354 return np;
355 } 355 }
356 356
357 static void capincci_free(struct capidev *cdev, u32 ncci) 357 static void capincci_free(struct capidev *cdev, u32 ncci)
358 { 358 {
359 struct capincci *np, *tmp; 359 struct capincci *np, *tmp;
360 360
361 list_for_each_entry_safe(np, tmp, &cdev->nccis, list) 361 list_for_each_entry_safe(np, tmp, &cdev->nccis, list)
362 if (ncci == 0xffffffff || np->ncci == ncci) { 362 if (ncci == 0xffffffff || np->ncci == ncci) {
363 capincci_free_minor(np); 363 capincci_free_minor(np);
364 list_del(&np->list); 364 list_del(&np->list);
365 kfree(np); 365 kfree(np);
366 } 366 }
367 } 367 }
368 368
369 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE 369 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
370 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci) 370 static struct capincci *capincci_find(struct capidev *cdev, u32 ncci)
371 { 371 {
372 struct capincci *np; 372 struct capincci *np;
373 373
374 list_for_each_entry(np, &cdev->nccis, list) 374 list_for_each_entry(np, &cdev->nccis, list)
375 if (np->ncci == ncci) 375 if (np->ncci == ncci)
376 return np; 376 return np;
377 return NULL; 377 return NULL;
378 } 378 }
379 379
380 /* -------- handle data queue --------------------------------------- */ 380 /* -------- handle data queue --------------------------------------- */
381 381
382 static struct sk_buff * 382 static struct sk_buff *
383 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb) 383 gen_data_b3_resp_for(struct capiminor *mp, struct sk_buff *skb)
384 { 384 {
385 struct sk_buff *nskb; 385 struct sk_buff *nskb;
386 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL); 386 nskb = alloc_skb(CAPI_DATA_B3_RESP_LEN, GFP_KERNEL);
387 if (nskb) { 387 if (nskb) {
388 u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2); 388 u16 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
389 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN); 389 unsigned char *s = skb_put(nskb, CAPI_DATA_B3_RESP_LEN);
390 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN); 390 capimsg_setu16(s, 0, CAPI_DATA_B3_RESP_LEN);
391 capimsg_setu16(s, 2, mp->ap->applid); 391 capimsg_setu16(s, 2, mp->ap->applid);
392 capimsg_setu8 (s, 4, CAPI_DATA_B3); 392 capimsg_setu8 (s, 4, CAPI_DATA_B3);
393 capimsg_setu8 (s, 5, CAPI_RESP); 393 capimsg_setu8 (s, 5, CAPI_RESP);
394 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid)); 394 capimsg_setu16(s, 6, atomic_inc_return(&mp->msgid));
395 capimsg_setu32(s, 8, mp->ncci); 395 capimsg_setu32(s, 8, mp->ncci);
396 capimsg_setu16(s, 12, datahandle); 396 capimsg_setu16(s, 12, datahandle);
397 } 397 }
398 return nskb; 398 return nskb;
399 } 399 }
400 400
401 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb) 401 static int handle_recv_skb(struct capiminor *mp, struct sk_buff *skb)
402 { 402 {
403 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data); 403 unsigned int datalen = skb->len - CAPIMSG_LEN(skb->data);
404 struct tty_struct *tty; 404 struct tty_struct *tty;
405 struct sk_buff *nskb; 405 struct sk_buff *nskb;
406 u16 errcode, datahandle; 406 u16 errcode, datahandle;
407 struct tty_ldisc *ld; 407 struct tty_ldisc *ld;
408 int ret = -1; 408 int ret = -1;
409 409
410 tty = tty_port_tty_get(&mp->port); 410 tty = tty_port_tty_get(&mp->port);
411 if (!tty) { 411 if (!tty) {
412 pr_debug("capi: currently no receiver\n"); 412 pr_debug("capi: currently no receiver\n");
413 return -1; 413 return -1;
414 } 414 }
415 415
416 ld = tty_ldisc_ref(tty); 416 ld = tty_ldisc_ref(tty);
417 if (!ld) { 417 if (!ld) {
418 /* fatal error, do not requeue */ 418 /* fatal error, do not requeue */
419 ret = 0; 419 ret = 0;
420 kfree_skb(skb); 420 kfree_skb(skb);
421 goto deref_tty; 421 goto deref_tty;
422 } 422 }
423 423
424 if (ld->ops->receive_buf == NULL) { 424 if (ld->ops->receive_buf == NULL) {
425 pr_debug("capi: ldisc has no receive_buf function\n"); 425 pr_debug("capi: ldisc has no receive_buf function\n");
426 /* fatal error, do not requeue */ 426 /* fatal error, do not requeue */
427 goto free_skb; 427 goto free_skb;
428 } 428 }
429 if (mp->ttyinstop) { 429 if (mp->ttyinstop) {
430 pr_debug("capi: recv tty throttled\n"); 430 pr_debug("capi: recv tty throttled\n");
431 goto deref_ldisc; 431 goto deref_ldisc;
432 } 432 }
433 433
434 if (tty->receive_room < datalen) { 434 if (tty->receive_room < datalen) {
435 pr_debug("capi: no room in tty\n"); 435 pr_debug("capi: no room in tty\n");
436 goto deref_ldisc; 436 goto deref_ldisc;
437 } 437 }
438 438
439 nskb = gen_data_b3_resp_for(mp, skb); 439 nskb = gen_data_b3_resp_for(mp, skb);
440 if (!nskb) { 440 if (!nskb) {
441 printk(KERN_ERR "capi: gen_data_b3_resp failed\n"); 441 printk(KERN_ERR "capi: gen_data_b3_resp failed\n");
442 goto deref_ldisc; 442 goto deref_ldisc;
443 } 443 }
444 444
445 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4); 445 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
446 446
447 errcode = capi20_put_message(mp->ap, nskb); 447 errcode = capi20_put_message(mp->ap, nskb);
448 448
449 if (errcode == CAPI_NOERROR) { 449 if (errcode == CAPI_NOERROR) {
450 skb_pull(skb, CAPIMSG_LEN(skb->data)); 450 skb_pull(skb, CAPIMSG_LEN(skb->data));
451 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n", 451 pr_debug("capi: DATA_B3_RESP %u len=%d => ldisc\n",
452 datahandle, skb->len); 452 datahandle, skb->len);
453 ld->ops->receive_buf(tty, skb->data, NULL, skb->len); 453 ld->ops->receive_buf(tty, skb->data, NULL, skb->len);
454 } else { 454 } else {
455 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n", 455 printk(KERN_ERR "capi: send DATA_B3_RESP failed=%x\n",
456 errcode); 456 errcode);
457 kfree_skb(nskb); 457 kfree_skb(nskb);
458 458
459 if (errcode == CAPI_SENDQUEUEFULL) 459 if (errcode == CAPI_SENDQUEUEFULL)
460 goto deref_ldisc; 460 goto deref_ldisc;
461 } 461 }
462 462
463 free_skb: 463 free_skb:
464 ret = 0; 464 ret = 0;
465 kfree_skb(skb); 465 kfree_skb(skb);
466 466
467 deref_ldisc: 467 deref_ldisc:
468 tty_ldisc_deref(ld); 468 tty_ldisc_deref(ld);
469 469
470 deref_tty: 470 deref_tty:
471 tty_kref_put(tty); 471 tty_kref_put(tty);
472 return ret; 472 return ret;
473 } 473 }
474 474
475 static void handle_minor_recv(struct capiminor *mp) 475 static void handle_minor_recv(struct capiminor *mp)
476 { 476 {
477 struct sk_buff *skb; 477 struct sk_buff *skb;
478 478
479 while ((skb = skb_dequeue(&mp->inqueue)) != NULL) 479 while ((skb = skb_dequeue(&mp->inqueue)) != NULL)
480 if (handle_recv_skb(mp, skb) < 0) { 480 if (handle_recv_skb(mp, skb) < 0) {
481 skb_queue_head(&mp->inqueue, skb); 481 skb_queue_head(&mp->inqueue, skb);
482 return; 482 return;
483 } 483 }
484 } 484 }
485 485
486 static void handle_minor_send(struct capiminor *mp) 486 static void handle_minor_send(struct capiminor *mp)
487 { 487 {
488 struct tty_struct *tty; 488 struct tty_struct *tty;
489 struct sk_buff *skb; 489 struct sk_buff *skb;
490 u16 len; 490 u16 len;
491 u16 errcode; 491 u16 errcode;
492 u16 datahandle; 492 u16 datahandle;
493 493
494 tty = tty_port_tty_get(&mp->port); 494 tty = tty_port_tty_get(&mp->port);
495 if (!tty) 495 if (!tty)
496 return; 496 return;
497 497
498 if (mp->ttyoutstop) { 498 if (mp->ttyoutstop) {
499 pr_debug("capi: send: tty stopped\n"); 499 pr_debug("capi: send: tty stopped\n");
500 tty_kref_put(tty); 500 tty_kref_put(tty);
501 return; 501 return;
502 } 502 }
503 503
504 while (1) { 504 while (1) {
505 spin_lock_bh(&mp->outlock); 505 spin_lock_bh(&mp->outlock);
506 skb = __skb_dequeue(&mp->outqueue); 506 skb = __skb_dequeue(&mp->outqueue);
507 if (!skb) { 507 if (!skb) {
508 spin_unlock_bh(&mp->outlock); 508 spin_unlock_bh(&mp->outlock);
509 break; 509 break;
510 } 510 }
511 len = (u16)skb->len; 511 len = (u16)skb->len;
512 mp->outbytes -= len; 512 mp->outbytes -= len;
513 spin_unlock_bh(&mp->outlock); 513 spin_unlock_bh(&mp->outlock);
514 514
515 datahandle = atomic_inc_return(&mp->datahandle); 515 datahandle = atomic_inc_return(&mp->datahandle);
516 skb_push(skb, CAPI_DATA_B3_REQ_LEN); 516 skb_push(skb, CAPI_DATA_B3_REQ_LEN);
517 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN); 517 memset(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
518 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN); 518 capimsg_setu16(skb->data, 0, CAPI_DATA_B3_REQ_LEN);
519 capimsg_setu16(skb->data, 2, mp->ap->applid); 519 capimsg_setu16(skb->data, 2, mp->ap->applid);
520 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3); 520 capimsg_setu8 (skb->data, 4, CAPI_DATA_B3);
521 capimsg_setu8 (skb->data, 5, CAPI_REQ); 521 capimsg_setu8 (skb->data, 5, CAPI_REQ);
522 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid)); 522 capimsg_setu16(skb->data, 6, atomic_inc_return(&mp->msgid));
523 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */ 523 capimsg_setu32(skb->data, 8, mp->ncci); /* NCCI */
524 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */ 524 capimsg_setu32(skb->data, 12, (u32)(long)skb->data);/* Data32 */
525 capimsg_setu16(skb->data, 16, len); /* Data length */ 525 capimsg_setu16(skb->data, 16, len); /* Data length */
526 capimsg_setu16(skb->data, 18, datahandle); 526 capimsg_setu16(skb->data, 18, datahandle);
527 capimsg_setu16(skb->data, 20, 0); /* Flags */ 527 capimsg_setu16(skb->data, 20, 0); /* Flags */
528 528
529 if (capiminor_add_ack(mp, datahandle) < 0) { 529 if (capiminor_add_ack(mp, datahandle) < 0) {
530 skb_pull(skb, CAPI_DATA_B3_REQ_LEN); 530 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
531 531
532 spin_lock_bh(&mp->outlock); 532 spin_lock_bh(&mp->outlock);
533 __skb_queue_head(&mp->outqueue, skb); 533 __skb_queue_head(&mp->outqueue, skb);
534 mp->outbytes += len; 534 mp->outbytes += len;
535 spin_unlock_bh(&mp->outlock); 535 spin_unlock_bh(&mp->outlock);
536 536
537 break; 537 break;
538 } 538 }
539 errcode = capi20_put_message(mp->ap, skb); 539 errcode = capi20_put_message(mp->ap, skb);
540 if (errcode == CAPI_NOERROR) { 540 if (errcode == CAPI_NOERROR) {
541 pr_debug("capi: DATA_B3_REQ %u len=%u\n", 541 pr_debug("capi: DATA_B3_REQ %u len=%u\n",
542 datahandle, len); 542 datahandle, len);
543 continue; 543 continue;
544 } 544 }
545 capiminor_del_ack(mp, datahandle); 545 capiminor_del_ack(mp, datahandle);
546 546
547 if (errcode == CAPI_SENDQUEUEFULL) { 547 if (errcode == CAPI_SENDQUEUEFULL) {
548 skb_pull(skb, CAPI_DATA_B3_REQ_LEN); 548 skb_pull(skb, CAPI_DATA_B3_REQ_LEN);
549 549
550 spin_lock_bh(&mp->outlock); 550 spin_lock_bh(&mp->outlock);
551 __skb_queue_head(&mp->outqueue, skb); 551 __skb_queue_head(&mp->outqueue, skb);
552 mp->outbytes += len; 552 mp->outbytes += len;
553 spin_unlock_bh(&mp->outlock); 553 spin_unlock_bh(&mp->outlock);
554 554
555 break; 555 break;
556 } 556 }
557 557
558 /* ups, drop packet */ 558 /* ups, drop packet */
559 printk(KERN_ERR "capi: put_message = %x\n", errcode); 559 printk(KERN_ERR "capi: put_message = %x\n", errcode);
560 kfree_skb(skb); 560 kfree_skb(skb);
561 } 561 }
562 tty_kref_put(tty); 562 tty_kref_put(tty);
563 } 563 }
564 564
565 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ 565 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
566 /* -------- function called by lower level -------------------------- */ 566 /* -------- function called by lower level -------------------------- */
567 567
568 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb) 568 static void capi_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
569 { 569 {
570 struct capidev *cdev = ap->private; 570 struct capidev *cdev = ap->private;
571 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE 571 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
572 struct tty_struct *tty;
573 struct capiminor *mp; 572 struct capiminor *mp;
574 u16 datahandle; 573 u16 datahandle;
575 struct capincci *np; 574 struct capincci *np;
576 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ 575 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
577 576
578 mutex_lock(&cdev->lock); 577 mutex_lock(&cdev->lock);
579 578
580 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) { 579 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_CONF) {
581 u16 info = CAPIMSG_U16(skb->data, 12); // Info field 580 u16 info = CAPIMSG_U16(skb->data, 12); // Info field
582 if ((info & 0xff00) == 0) 581 if ((info & 0xff00) == 0)
583 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data)); 582 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
584 } 583 }
585 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND) 584 if (CAPIMSG_CMD(skb->data) == CAPI_CONNECT_B3_IND)
586 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data)); 585 capincci_alloc(cdev, CAPIMSG_NCCI(skb->data));
587 586
588 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) { 587 if (CAPIMSG_COMMAND(skb->data) != CAPI_DATA_B3) {
589 skb_queue_tail(&cdev->recvqueue, skb); 588 skb_queue_tail(&cdev->recvqueue, skb);
590 wake_up_interruptible(&cdev->recvwait); 589 wake_up_interruptible(&cdev->recvwait);
591 goto unlock_out; 590 goto unlock_out;
592 } 591 }
593 592
594 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE 593 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
595 skb_queue_tail(&cdev->recvqueue, skb); 594 skb_queue_tail(&cdev->recvqueue, skb);
596 wake_up_interruptible(&cdev->recvwait); 595 wake_up_interruptible(&cdev->recvwait);
597 596
598 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */ 597 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
599 598
600 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data)); 599 np = capincci_find(cdev, CAPIMSG_CONTROL(skb->data));
601 if (!np) { 600 if (!np) {
602 printk(KERN_ERR "BUG: capi_signal: ncci not found\n"); 601 printk(KERN_ERR "BUG: capi_signal: ncci not found\n");
603 skb_queue_tail(&cdev->recvqueue, skb); 602 skb_queue_tail(&cdev->recvqueue, skb);
604 wake_up_interruptible(&cdev->recvwait); 603 wake_up_interruptible(&cdev->recvwait);
605 goto unlock_out; 604 goto unlock_out;
606 } 605 }
607 606
608 mp = np->minorp; 607 mp = np->minorp;
609 if (!mp) { 608 if (!mp) {
610 skb_queue_tail(&cdev->recvqueue, skb); 609 skb_queue_tail(&cdev->recvqueue, skb);
611 wake_up_interruptible(&cdev->recvwait); 610 wake_up_interruptible(&cdev->recvwait);
612 goto unlock_out; 611 goto unlock_out;
613 } 612 }
614 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) { 613 if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_IND) {
615 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2); 614 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 4 + 2);
616 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n", 615 pr_debug("capi_signal: DATA_B3_IND %u len=%d\n",
617 datahandle, skb->len-CAPIMSG_LEN(skb->data)); 616 datahandle, skb->len-CAPIMSG_LEN(skb->data));
618 skb_queue_tail(&mp->inqueue, skb); 617 skb_queue_tail(&mp->inqueue, skb);
619 618
620 handle_minor_recv(mp); 619 handle_minor_recv(mp);
621 620
622 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) { 621 } else if (CAPIMSG_SUBCOMMAND(skb->data) == CAPI_CONF) {
623 622
624 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4); 623 datahandle = CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4);
625 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n", 624 pr_debug("capi_signal: DATA_B3_CONF %u 0x%x\n",
626 datahandle, 625 datahandle,
627 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2)); 626 CAPIMSG_U16(skb->data, CAPIMSG_BASELEN + 4 + 2));
628 kfree_skb(skb); 627 kfree_skb(skb);
629 capiminor_del_ack(mp, datahandle); 628 capiminor_del_ack(mp, datahandle);
630 tty = tty_port_tty_get(&mp->port); 629 tty_port_tty_wakeup(&mp->port);
631 if (tty) {
632 tty_wakeup(tty);
633 tty_kref_put(tty);
634 }
635 handle_minor_send(mp); 630 handle_minor_send(mp);
636 631
637 } else { 632 } else {
638 /* ups, let capi application handle it :-) */ 633 /* ups, let capi application handle it :-) */
639 skb_queue_tail(&cdev->recvqueue, skb); 634 skb_queue_tail(&cdev->recvqueue, skb);
640 wake_up_interruptible(&cdev->recvwait); 635 wake_up_interruptible(&cdev->recvwait);
641 } 636 }
642 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ 637 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
643 638
644 unlock_out: 639 unlock_out:
645 mutex_unlock(&cdev->lock); 640 mutex_unlock(&cdev->lock);
646 } 641 }
647 642
648 /* -------- file_operations for capidev ----------------------------- */ 643 /* -------- file_operations for capidev ----------------------------- */
649 644
650 static ssize_t 645 static ssize_t
651 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 646 capi_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
652 { 647 {
653 struct capidev *cdev = file->private_data; 648 struct capidev *cdev = file->private_data;
654 struct sk_buff *skb; 649 struct sk_buff *skb;
655 size_t copied; 650 size_t copied;
656 int err; 651 int err;
657 652
658 if (!cdev->ap.applid) 653 if (!cdev->ap.applid)
659 return -ENODEV; 654 return -ENODEV;
660 655
661 skb = skb_dequeue(&cdev->recvqueue); 656 skb = skb_dequeue(&cdev->recvqueue);
662 if (!skb) { 657 if (!skb) {
663 if (file->f_flags & O_NONBLOCK) 658 if (file->f_flags & O_NONBLOCK)
664 return -EAGAIN; 659 return -EAGAIN;
665 err = wait_event_interruptible(cdev->recvwait, 660 err = wait_event_interruptible(cdev->recvwait,
666 (skb = skb_dequeue(&cdev->recvqueue))); 661 (skb = skb_dequeue(&cdev->recvqueue)));
667 if (err) 662 if (err)
668 return err; 663 return err;
669 } 664 }
670 if (skb->len > count) { 665 if (skb->len > count) {
671 skb_queue_head(&cdev->recvqueue, skb); 666 skb_queue_head(&cdev->recvqueue, skb);
672 return -EMSGSIZE; 667 return -EMSGSIZE;
673 } 668 }
674 if (copy_to_user(buf, skb->data, skb->len)) { 669 if (copy_to_user(buf, skb->data, skb->len)) {
675 skb_queue_head(&cdev->recvqueue, skb); 670 skb_queue_head(&cdev->recvqueue, skb);
676 return -EFAULT; 671 return -EFAULT;
677 } 672 }
678 copied = skb->len; 673 copied = skb->len;
679 674
680 kfree_skb(skb); 675 kfree_skb(skb);
681 676
682 return copied; 677 return copied;
683 } 678 }
684 679
685 static ssize_t 680 static ssize_t
686 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) 681 capi_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
687 { 682 {
688 struct capidev *cdev = file->private_data; 683 struct capidev *cdev = file->private_data;
689 struct sk_buff *skb; 684 struct sk_buff *skb;
690 u16 mlen; 685 u16 mlen;
691 686
692 if (!cdev->ap.applid) 687 if (!cdev->ap.applid)
693 return -ENODEV; 688 return -ENODEV;
694 689
695 skb = alloc_skb(count, GFP_USER); 690 skb = alloc_skb(count, GFP_USER);
696 if (!skb) 691 if (!skb)
697 return -ENOMEM; 692 return -ENOMEM;
698 693
699 if (copy_from_user(skb_put(skb, count), buf, count)) { 694 if (copy_from_user(skb_put(skb, count), buf, count)) {
700 kfree_skb(skb); 695 kfree_skb(skb);
701 return -EFAULT; 696 return -EFAULT;
702 } 697 }
703 mlen = CAPIMSG_LEN(skb->data); 698 mlen = CAPIMSG_LEN(skb->data);
704 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) { 699 if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_REQ) {
705 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) { 700 if ((size_t)(mlen + CAPIMSG_DATALEN(skb->data)) != count) {
706 kfree_skb(skb); 701 kfree_skb(skb);
707 return -EINVAL; 702 return -EINVAL;
708 } 703 }
709 } else { 704 } else {
710 if (mlen != count) { 705 if (mlen != count) {
711 kfree_skb(skb); 706 kfree_skb(skb);
712 return -EINVAL; 707 return -EINVAL;
713 } 708 }
714 } 709 }
715 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid); 710 CAPIMSG_SETAPPID(skb->data, cdev->ap.applid);
716 711
717 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) { 712 if (CAPIMSG_CMD(skb->data) == CAPI_DISCONNECT_B3_RESP) {
718 mutex_lock(&cdev->lock); 713 mutex_lock(&cdev->lock);
719 capincci_free(cdev, CAPIMSG_NCCI(skb->data)); 714 capincci_free(cdev, CAPIMSG_NCCI(skb->data));
720 mutex_unlock(&cdev->lock); 715 mutex_unlock(&cdev->lock);
721 } 716 }
722 717
723 cdev->errcode = capi20_put_message(&cdev->ap, skb); 718 cdev->errcode = capi20_put_message(&cdev->ap, skb);
724 719
725 if (cdev->errcode) { 720 if (cdev->errcode) {
726 kfree_skb(skb); 721 kfree_skb(skb);
727 return -EIO; 722 return -EIO;
728 } 723 }
729 return count; 724 return count;
730 } 725 }
731 726
732 static unsigned int 727 static unsigned int
733 capi_poll(struct file *file, poll_table *wait) 728 capi_poll(struct file *file, poll_table *wait)
734 { 729 {
735 struct capidev *cdev = file->private_data; 730 struct capidev *cdev = file->private_data;
736 unsigned int mask = 0; 731 unsigned int mask = 0;
737 732
738 if (!cdev->ap.applid) 733 if (!cdev->ap.applid)
739 return POLLERR; 734 return POLLERR;
740 735
741 poll_wait(file, &(cdev->recvwait), wait); 736 poll_wait(file, &(cdev->recvwait), wait);
742 mask = POLLOUT | POLLWRNORM; 737 mask = POLLOUT | POLLWRNORM;
743 if (!skb_queue_empty(&cdev->recvqueue)) 738 if (!skb_queue_empty(&cdev->recvqueue))
744 mask |= POLLIN | POLLRDNORM; 739 mask |= POLLIN | POLLRDNORM;
745 return mask; 740 return mask;
746 } 741 }
747 742
748 static int 743 static int
749 capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 744 capi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
750 { 745 {
751 struct capidev *cdev = file->private_data; 746 struct capidev *cdev = file->private_data;
752 capi_ioctl_struct data; 747 capi_ioctl_struct data;
753 int retval = -EINVAL; 748 int retval = -EINVAL;
754 void __user *argp = (void __user *)arg; 749 void __user *argp = (void __user *)arg;
755 750
756 switch (cmd) { 751 switch (cmd) {
757 case CAPI_REGISTER: 752 case CAPI_REGISTER:
758 mutex_lock(&cdev->lock); 753 mutex_lock(&cdev->lock);
759 754
760 if (cdev->ap.applid) { 755 if (cdev->ap.applid) {
761 retval = -EEXIST; 756 retval = -EEXIST;
762 goto register_out; 757 goto register_out;
763 } 758 }
764 if (copy_from_user(&cdev->ap.rparam, argp, 759 if (copy_from_user(&cdev->ap.rparam, argp,
765 sizeof(struct capi_register_params))) { 760 sizeof(struct capi_register_params))) {
766 retval = -EFAULT; 761 retval = -EFAULT;
767 goto register_out; 762 goto register_out;
768 } 763 }
769 cdev->ap.private = cdev; 764 cdev->ap.private = cdev;
770 cdev->ap.recv_message = capi_recv_message; 765 cdev->ap.recv_message = capi_recv_message;
771 cdev->errcode = capi20_register(&cdev->ap); 766 cdev->errcode = capi20_register(&cdev->ap);
772 retval = (int)cdev->ap.applid; 767 retval = (int)cdev->ap.applid;
773 if (cdev->errcode) { 768 if (cdev->errcode) {
774 cdev->ap.applid = 0; 769 cdev->ap.applid = 0;
775 retval = -EIO; 770 retval = -EIO;
776 } 771 }
777 772
778 register_out: 773 register_out:
779 mutex_unlock(&cdev->lock); 774 mutex_unlock(&cdev->lock);
780 return retval; 775 return retval;
781 776
782 case CAPI_GET_VERSION: 777 case CAPI_GET_VERSION:
783 if (copy_from_user(&data.contr, argp, 778 if (copy_from_user(&data.contr, argp,
784 sizeof(data.contr))) 779 sizeof(data.contr)))
785 return -EFAULT; 780 return -EFAULT;
786 cdev->errcode = capi20_get_version(data.contr, &data.version); 781 cdev->errcode = capi20_get_version(data.contr, &data.version);
787 if (cdev->errcode) 782 if (cdev->errcode)
788 return -EIO; 783 return -EIO;
789 if (copy_to_user(argp, &data.version, 784 if (copy_to_user(argp, &data.version,
790 sizeof(data.version))) 785 sizeof(data.version)))
791 return -EFAULT; 786 return -EFAULT;
792 return 0; 787 return 0;
793 788
794 case CAPI_GET_SERIAL: 789 case CAPI_GET_SERIAL:
795 if (copy_from_user(&data.contr, argp, 790 if (copy_from_user(&data.contr, argp,
796 sizeof(data.contr))) 791 sizeof(data.contr)))
797 return -EFAULT; 792 return -EFAULT;
798 cdev->errcode = capi20_get_serial(data.contr, data.serial); 793 cdev->errcode = capi20_get_serial(data.contr, data.serial);
799 if (cdev->errcode) 794 if (cdev->errcode)
800 return -EIO; 795 return -EIO;
801 if (copy_to_user(argp, data.serial, 796 if (copy_to_user(argp, data.serial,
802 sizeof(data.serial))) 797 sizeof(data.serial)))
803 return -EFAULT; 798 return -EFAULT;
804 return 0; 799 return 0;
805 800
806 case CAPI_GET_PROFILE: 801 case CAPI_GET_PROFILE:
807 if (copy_from_user(&data.contr, argp, 802 if (copy_from_user(&data.contr, argp,
808 sizeof(data.contr))) 803 sizeof(data.contr)))
809 return -EFAULT; 804 return -EFAULT;
810 805
811 if (data.contr == 0) { 806 if (data.contr == 0) {
812 cdev->errcode = capi20_get_profile(data.contr, &data.profile); 807 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
813 if (cdev->errcode) 808 if (cdev->errcode)
814 return -EIO; 809 return -EIO;
815 810
816 retval = copy_to_user(argp, 811 retval = copy_to_user(argp,
817 &data.profile.ncontroller, 812 &data.profile.ncontroller,
818 sizeof(data.profile.ncontroller)); 813 sizeof(data.profile.ncontroller));
819 814
820 } else { 815 } else {
821 cdev->errcode = capi20_get_profile(data.contr, &data.profile); 816 cdev->errcode = capi20_get_profile(data.contr, &data.profile);
822 if (cdev->errcode) 817 if (cdev->errcode)
823 return -EIO; 818 return -EIO;
824 819
825 retval = copy_to_user(argp, &data.profile, 820 retval = copy_to_user(argp, &data.profile,
826 sizeof(data.profile)); 821 sizeof(data.profile));
827 } 822 }
828 if (retval) 823 if (retval)
829 return -EFAULT; 824 return -EFAULT;
830 return 0; 825 return 0;
831 826
832 case CAPI_GET_MANUFACTURER: 827 case CAPI_GET_MANUFACTURER:
833 if (copy_from_user(&data.contr, argp, 828 if (copy_from_user(&data.contr, argp,
834 sizeof(data.contr))) 829 sizeof(data.contr)))
835 return -EFAULT; 830 return -EFAULT;
836 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer); 831 cdev->errcode = capi20_get_manufacturer(data.contr, data.manufacturer);
837 if (cdev->errcode) 832 if (cdev->errcode)
838 return -EIO; 833 return -EIO;
839 834
840 if (copy_to_user(argp, data.manufacturer, 835 if (copy_to_user(argp, data.manufacturer,
841 sizeof(data.manufacturer))) 836 sizeof(data.manufacturer)))
842 return -EFAULT; 837 return -EFAULT;
843 838
844 return 0; 839 return 0;
845 840
846 case CAPI_GET_ERRCODE: 841 case CAPI_GET_ERRCODE:
847 data.errcode = cdev->errcode; 842 data.errcode = cdev->errcode;
848 cdev->errcode = CAPI_NOERROR; 843 cdev->errcode = CAPI_NOERROR;
849 if (arg) { 844 if (arg) {
850 if (copy_to_user(argp, &data.errcode, 845 if (copy_to_user(argp, &data.errcode,
851 sizeof(data.errcode))) 846 sizeof(data.errcode)))
852 return -EFAULT; 847 return -EFAULT;
853 } 848 }
854 return data.errcode; 849 return data.errcode;
855 850
856 case CAPI_INSTALLED: 851 case CAPI_INSTALLED:
857 if (capi20_isinstalled() == CAPI_NOERROR) 852 if (capi20_isinstalled() == CAPI_NOERROR)
858 return 0; 853 return 0;
859 return -ENXIO; 854 return -ENXIO;
860 855
861 case CAPI_MANUFACTURER_CMD: { 856 case CAPI_MANUFACTURER_CMD: {
862 struct capi_manufacturer_cmd mcmd; 857 struct capi_manufacturer_cmd mcmd;
863 if (!capable(CAP_SYS_ADMIN)) 858 if (!capable(CAP_SYS_ADMIN))
864 return -EPERM; 859 return -EPERM;
865 if (copy_from_user(&mcmd, argp, sizeof(mcmd))) 860 if (copy_from_user(&mcmd, argp, sizeof(mcmd)))
866 return -EFAULT; 861 return -EFAULT;
867 return capi20_manufacturer(mcmd.cmd, mcmd.data); 862 return capi20_manufacturer(mcmd.cmd, mcmd.data);
868 } 863 }
869 case CAPI_SET_FLAGS: 864 case CAPI_SET_FLAGS:
870 case CAPI_CLR_FLAGS: { 865 case CAPI_CLR_FLAGS: {
871 unsigned userflags; 866 unsigned userflags;
872 867
873 if (copy_from_user(&userflags, argp, sizeof(userflags))) 868 if (copy_from_user(&userflags, argp, sizeof(userflags)))
874 return -EFAULT; 869 return -EFAULT;
875 870
876 mutex_lock(&cdev->lock); 871 mutex_lock(&cdev->lock);
877 if (cmd == CAPI_SET_FLAGS) 872 if (cmd == CAPI_SET_FLAGS)
878 cdev->userflags |= userflags; 873 cdev->userflags |= userflags;
879 else 874 else
880 cdev->userflags &= ~userflags; 875 cdev->userflags &= ~userflags;
881 mutex_unlock(&cdev->lock); 876 mutex_unlock(&cdev->lock);
882 return 0; 877 return 0;
883 } 878 }
884 case CAPI_GET_FLAGS: 879 case CAPI_GET_FLAGS:
885 if (copy_to_user(argp, &cdev->userflags, 880 if (copy_to_user(argp, &cdev->userflags,
886 sizeof(cdev->userflags))) 881 sizeof(cdev->userflags)))
887 return -EFAULT; 882 return -EFAULT;
888 return 0; 883 return 0;
889 884
890 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE 885 #ifndef CONFIG_ISDN_CAPI_MIDDLEWARE
891 case CAPI_NCCI_OPENCOUNT: 886 case CAPI_NCCI_OPENCOUNT:
892 return 0; 887 return 0;
893 888
894 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */ 889 #else /* CONFIG_ISDN_CAPI_MIDDLEWARE */
895 case CAPI_NCCI_OPENCOUNT: { 890 case CAPI_NCCI_OPENCOUNT: {
896 struct capincci *nccip; 891 struct capincci *nccip;
897 unsigned ncci; 892 unsigned ncci;
898 int count = 0; 893 int count = 0;
899 894
900 if (copy_from_user(&ncci, argp, sizeof(ncci))) 895 if (copy_from_user(&ncci, argp, sizeof(ncci)))
901 return -EFAULT; 896 return -EFAULT;
902 897
903 mutex_lock(&cdev->lock); 898 mutex_lock(&cdev->lock);
904 nccip = capincci_find(cdev, (u32)ncci); 899 nccip = capincci_find(cdev, (u32)ncci);
905 if (nccip) 900 if (nccip)
906 count = capincci_minor_opencount(nccip); 901 count = capincci_minor_opencount(nccip);
907 mutex_unlock(&cdev->lock); 902 mutex_unlock(&cdev->lock);
908 return count; 903 return count;
909 } 904 }
910 905
911 case CAPI_NCCI_GETUNIT: { 906 case CAPI_NCCI_GETUNIT: {
912 struct capincci *nccip; 907 struct capincci *nccip;
913 struct capiminor *mp; 908 struct capiminor *mp;
914 unsigned ncci; 909 unsigned ncci;
915 int unit = -ESRCH; 910 int unit = -ESRCH;
916 911
917 if (copy_from_user(&ncci, argp, sizeof(ncci))) 912 if (copy_from_user(&ncci, argp, sizeof(ncci)))
918 return -EFAULT; 913 return -EFAULT;
919 914
920 mutex_lock(&cdev->lock); 915 mutex_lock(&cdev->lock);
921 nccip = capincci_find(cdev, (u32)ncci); 916 nccip = capincci_find(cdev, (u32)ncci);
922 if (nccip) { 917 if (nccip) {
923 mp = nccip->minorp; 918 mp = nccip->minorp;
924 if (mp) 919 if (mp)
925 unit = mp->minor; 920 unit = mp->minor;
926 } 921 }
927 mutex_unlock(&cdev->lock); 922 mutex_unlock(&cdev->lock);
928 return unit; 923 return unit;
929 } 924 }
930 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */ 925 #endif /* CONFIG_ISDN_CAPI_MIDDLEWARE */
931 926
932 default: 927 default:
933 return -EINVAL; 928 return -EINVAL;
934 } 929 }
935 } 930 }
936 931
937 static long 932 static long
938 capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 933 capi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
939 { 934 {
940 int ret; 935 int ret;
941 936
942 mutex_lock(&capi_mutex); 937 mutex_lock(&capi_mutex);
943 ret = capi_ioctl(file, cmd, arg); 938 ret = capi_ioctl(file, cmd, arg);
944 mutex_unlock(&capi_mutex); 939 mutex_unlock(&capi_mutex);
945 940
946 return ret; 941 return ret;
947 } 942 }
948 943
949 static int capi_open(struct inode *inode, struct file *file) 944 static int capi_open(struct inode *inode, struct file *file)
950 { 945 {
951 struct capidev *cdev; 946 struct capidev *cdev;
952 947
953 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 948 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
954 if (!cdev) 949 if (!cdev)
955 return -ENOMEM; 950 return -ENOMEM;
956 951
957 mutex_init(&cdev->lock); 952 mutex_init(&cdev->lock);
958 skb_queue_head_init(&cdev->recvqueue); 953 skb_queue_head_init(&cdev->recvqueue);
959 init_waitqueue_head(&cdev->recvwait); 954 init_waitqueue_head(&cdev->recvwait);
960 INIT_LIST_HEAD(&cdev->nccis); 955 INIT_LIST_HEAD(&cdev->nccis);
961 file->private_data = cdev; 956 file->private_data = cdev;
962 957
963 mutex_lock(&capidev_list_lock); 958 mutex_lock(&capidev_list_lock);
964 list_add_tail(&cdev->list, &capidev_list); 959 list_add_tail(&cdev->list, &capidev_list);
965 mutex_unlock(&capidev_list_lock); 960 mutex_unlock(&capidev_list_lock);
966 961
967 return nonseekable_open(inode, file); 962 return nonseekable_open(inode, file);
968 } 963 }
969 964
970 static int capi_release(struct inode *inode, struct file *file) 965 static int capi_release(struct inode *inode, struct file *file)
971 { 966 {
972 struct capidev *cdev = file->private_data; 967 struct capidev *cdev = file->private_data;
973 968
974 mutex_lock(&capidev_list_lock); 969 mutex_lock(&capidev_list_lock);
975 list_del(&cdev->list); 970 list_del(&cdev->list);
976 mutex_unlock(&capidev_list_lock); 971 mutex_unlock(&capidev_list_lock);
977 972
978 if (cdev->ap.applid) 973 if (cdev->ap.applid)
979 capi20_release(&cdev->ap); 974 capi20_release(&cdev->ap);
980 skb_queue_purge(&cdev->recvqueue); 975 skb_queue_purge(&cdev->recvqueue);
981 capincci_free(cdev, 0xffffffff); 976 capincci_free(cdev, 0xffffffff);
982 977
983 kfree(cdev); 978 kfree(cdev);
984 return 0; 979 return 0;
985 } 980 }
986 981
987 static const struct file_operations capi_fops = 982 static const struct file_operations capi_fops =
988 { 983 {
989 .owner = THIS_MODULE, 984 .owner = THIS_MODULE,
990 .llseek = no_llseek, 985 .llseek = no_llseek,
991 .read = capi_read, 986 .read = capi_read,
992 .write = capi_write, 987 .write = capi_write,
993 .poll = capi_poll, 988 .poll = capi_poll,
994 .unlocked_ioctl = capi_unlocked_ioctl, 989 .unlocked_ioctl = capi_unlocked_ioctl,
995 .open = capi_open, 990 .open = capi_open,
996 .release = capi_release, 991 .release = capi_release,
997 }; 992 };
998 993
999 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE 994 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1000 /* -------- tty_operations for capincci ----------------------------- */ 995 /* -------- tty_operations for capincci ----------------------------- */
1001 996
1002 static int 997 static int
1003 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty) 998 capinc_tty_install(struct tty_driver *driver, struct tty_struct *tty)
1004 { 999 {
1005 struct capiminor *mp = capiminor_get(tty->index); 1000 struct capiminor *mp = capiminor_get(tty->index);
1006 int ret = tty_standard_install(driver, tty); 1001 int ret = tty_standard_install(driver, tty);
1007 1002
1008 if (ret == 0) 1003 if (ret == 0)
1009 tty->driver_data = mp; 1004 tty->driver_data = mp;
1010 else 1005 else
1011 capiminor_put(mp); 1006 capiminor_put(mp);
1012 return ret; 1007 return ret;
1013 } 1008 }
1014 1009
1015 static void capinc_tty_cleanup(struct tty_struct *tty) 1010 static void capinc_tty_cleanup(struct tty_struct *tty)
1016 { 1011 {
1017 struct capiminor *mp = tty->driver_data; 1012 struct capiminor *mp = tty->driver_data;
1018 tty->driver_data = NULL; 1013 tty->driver_data = NULL;
1019 capiminor_put(mp); 1014 capiminor_put(mp);
1020 } 1015 }
1021 1016
1022 static int capinc_tty_open(struct tty_struct *tty, struct file *filp) 1017 static int capinc_tty_open(struct tty_struct *tty, struct file *filp)
1023 { 1018 {
1024 struct capiminor *mp = tty->driver_data; 1019 struct capiminor *mp = tty->driver_data;
1025 int err; 1020 int err;
1026 1021
1027 err = tty_port_open(&mp->port, tty, filp); 1022 err = tty_port_open(&mp->port, tty, filp);
1028 if (err) 1023 if (err)
1029 return err; 1024 return err;
1030 1025
1031 handle_minor_recv(mp); 1026 handle_minor_recv(mp);
1032 return 0; 1027 return 0;
1033 } 1028 }
1034 1029
1035 static void capinc_tty_close(struct tty_struct *tty, struct file *filp) 1030 static void capinc_tty_close(struct tty_struct *tty, struct file *filp)
1036 { 1031 {
1037 struct capiminor *mp = tty->driver_data; 1032 struct capiminor *mp = tty->driver_data;
1038 1033
1039 tty_port_close(&mp->port, tty, filp); 1034 tty_port_close(&mp->port, tty, filp);
1040 } 1035 }
1041 1036
1042 static int capinc_tty_write(struct tty_struct *tty, 1037 static int capinc_tty_write(struct tty_struct *tty,
1043 const unsigned char *buf, int count) 1038 const unsigned char *buf, int count)
1044 { 1039 {
1045 struct capiminor *mp = tty->driver_data; 1040 struct capiminor *mp = tty->driver_data;
1046 struct sk_buff *skb; 1041 struct sk_buff *skb;
1047 1042
1048 pr_debug("capinc_tty_write(count=%d)\n", count); 1043 pr_debug("capinc_tty_write(count=%d)\n", count);
1049 1044
1050 spin_lock_bh(&mp->outlock); 1045 spin_lock_bh(&mp->outlock);
1051 skb = mp->outskb; 1046 skb = mp->outskb;
1052 if (skb) { 1047 if (skb) {
1053 mp->outskb = NULL; 1048 mp->outskb = NULL;
1054 __skb_queue_tail(&mp->outqueue, skb); 1049 __skb_queue_tail(&mp->outqueue, skb);
1055 mp->outbytes += skb->len; 1050 mp->outbytes += skb->len;
1056 } 1051 }
1057 1052
1058 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC); 1053 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + count, GFP_ATOMIC);
1059 if (!skb) { 1054 if (!skb) {
1060 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n"); 1055 printk(KERN_ERR "capinc_tty_write: alloc_skb failed\n");
1061 spin_unlock_bh(&mp->outlock); 1056 spin_unlock_bh(&mp->outlock);
1062 return -ENOMEM; 1057 return -ENOMEM;
1063 } 1058 }
1064 1059
1065 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN); 1060 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1066 memcpy(skb_put(skb, count), buf, count); 1061 memcpy(skb_put(skb, count), buf, count);
1067 1062
1068 __skb_queue_tail(&mp->outqueue, skb); 1063 __skb_queue_tail(&mp->outqueue, skb);
1069 mp->outbytes += skb->len; 1064 mp->outbytes += skb->len;
1070 spin_unlock_bh(&mp->outlock); 1065 spin_unlock_bh(&mp->outlock);
1071 1066
1072 handle_minor_send(mp); 1067 handle_minor_send(mp);
1073 1068
1074 return count; 1069 return count;
1075 } 1070 }
1076 1071
1077 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch) 1072 static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
1078 { 1073 {
1079 struct capiminor *mp = tty->driver_data; 1074 struct capiminor *mp = tty->driver_data;
1080 bool invoke_send = false; 1075 bool invoke_send = false;
1081 struct sk_buff *skb; 1076 struct sk_buff *skb;
1082 int ret = 1; 1077 int ret = 1;
1083 1078
1084 pr_debug("capinc_put_char(%u)\n", ch); 1079 pr_debug("capinc_put_char(%u)\n", ch);
1085 1080
1086 spin_lock_bh(&mp->outlock); 1081 spin_lock_bh(&mp->outlock);
1087 skb = mp->outskb; 1082 skb = mp->outskb;
1088 if (skb) { 1083 if (skb) {
1089 if (skb_tailroom(skb) > 0) { 1084 if (skb_tailroom(skb) > 0) {
1090 *(skb_put(skb, 1)) = ch; 1085 *(skb_put(skb, 1)) = ch;
1091 goto unlock_out; 1086 goto unlock_out;
1092 } 1087 }
1093 mp->outskb = NULL; 1088 mp->outskb = NULL;
1094 __skb_queue_tail(&mp->outqueue, skb); 1089 __skb_queue_tail(&mp->outqueue, skb);
1095 mp->outbytes += skb->len; 1090 mp->outbytes += skb->len;
1096 invoke_send = true; 1091 invoke_send = true;
1097 } 1092 }
1098 1093
1099 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC); 1094 skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
1100 if (skb) { 1095 if (skb) {
1101 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN); 1096 skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
1102 *(skb_put(skb, 1)) = ch; 1097 *(skb_put(skb, 1)) = ch;
1103 mp->outskb = skb; 1098 mp->outskb = skb;
1104 } else { 1099 } else {
1105 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch); 1100 printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
1106 ret = 0; 1101 ret = 0;
1107 } 1102 }
1108 1103
1109 unlock_out: 1104 unlock_out:
1110 spin_unlock_bh(&mp->outlock); 1105 spin_unlock_bh(&mp->outlock);
1111 1106
1112 if (invoke_send) 1107 if (invoke_send)
1113 handle_minor_send(mp); 1108 handle_minor_send(mp);
1114 1109
1115 return ret; 1110 return ret;
1116 } 1111 }
1117 1112
1118 static void capinc_tty_flush_chars(struct tty_struct *tty) 1113 static void capinc_tty_flush_chars(struct tty_struct *tty)
1119 { 1114 {
1120 struct capiminor *mp = tty->driver_data; 1115 struct capiminor *mp = tty->driver_data;
1121 struct sk_buff *skb; 1116 struct sk_buff *skb;
1122 1117
1123 pr_debug("capinc_tty_flush_chars\n"); 1118 pr_debug("capinc_tty_flush_chars\n");
1124 1119
1125 spin_lock_bh(&mp->outlock); 1120 spin_lock_bh(&mp->outlock);
1126 skb = mp->outskb; 1121 skb = mp->outskb;
1127 if (skb) { 1122 if (skb) {
1128 mp->outskb = NULL; 1123 mp->outskb = NULL;
1129 __skb_queue_tail(&mp->outqueue, skb); 1124 __skb_queue_tail(&mp->outqueue, skb);
1130 mp->outbytes += skb->len; 1125 mp->outbytes += skb->len;
1131 spin_unlock_bh(&mp->outlock); 1126 spin_unlock_bh(&mp->outlock);
1132 1127
1133 handle_minor_send(mp); 1128 handle_minor_send(mp);
1134 } else 1129 } else
1135 spin_unlock_bh(&mp->outlock); 1130 spin_unlock_bh(&mp->outlock);
1136 1131
1137 handle_minor_recv(mp); 1132 handle_minor_recv(mp);
1138 } 1133 }
1139 1134
1140 static int capinc_tty_write_room(struct tty_struct *tty) 1135 static int capinc_tty_write_room(struct tty_struct *tty)
1141 { 1136 {
1142 struct capiminor *mp = tty->driver_data; 1137 struct capiminor *mp = tty->driver_data;
1143 int room; 1138 int room;
1144 1139
1145 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue); 1140 room = CAPINC_MAX_SENDQUEUE-skb_queue_len(&mp->outqueue);
1146 room *= CAPI_MAX_BLKSIZE; 1141 room *= CAPI_MAX_BLKSIZE;
1147 pr_debug("capinc_tty_write_room = %d\n", room); 1142 pr_debug("capinc_tty_write_room = %d\n", room);
1148 return room; 1143 return room;
1149 } 1144 }
1150 1145
1151 static int capinc_tty_chars_in_buffer(struct tty_struct *tty) 1146 static int capinc_tty_chars_in_buffer(struct tty_struct *tty)
1152 { 1147 {
1153 struct capiminor *mp = tty->driver_data; 1148 struct capiminor *mp = tty->driver_data;
1154 1149
1155 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n", 1150 pr_debug("capinc_tty_chars_in_buffer = %d nack=%d sq=%d rq=%d\n",
1156 mp->outbytes, mp->nack, 1151 mp->outbytes, mp->nack,
1157 skb_queue_len(&mp->outqueue), 1152 skb_queue_len(&mp->outqueue),
1158 skb_queue_len(&mp->inqueue)); 1153 skb_queue_len(&mp->inqueue));
1159 return mp->outbytes; 1154 return mp->outbytes;
1160 } 1155 }
1161 1156
1162 static int capinc_tty_ioctl(struct tty_struct *tty, 1157 static int capinc_tty_ioctl(struct tty_struct *tty,
1163 unsigned int cmd, unsigned long arg) 1158 unsigned int cmd, unsigned long arg)
1164 { 1159 {
1165 return -ENOIOCTLCMD; 1160 return -ENOIOCTLCMD;
1166 } 1161 }
1167 1162
1168 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old) 1163 static void capinc_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1169 { 1164 {
1170 pr_debug("capinc_tty_set_termios\n"); 1165 pr_debug("capinc_tty_set_termios\n");
1171 } 1166 }
1172 1167
1173 static void capinc_tty_throttle(struct tty_struct *tty) 1168 static void capinc_tty_throttle(struct tty_struct *tty)
1174 { 1169 {
1175 struct capiminor *mp = tty->driver_data; 1170 struct capiminor *mp = tty->driver_data;
1176 pr_debug("capinc_tty_throttle\n"); 1171 pr_debug("capinc_tty_throttle\n");
1177 mp->ttyinstop = 1; 1172 mp->ttyinstop = 1;
1178 } 1173 }
1179 1174
1180 static void capinc_tty_unthrottle(struct tty_struct *tty) 1175 static void capinc_tty_unthrottle(struct tty_struct *tty)
1181 { 1176 {
1182 struct capiminor *mp = tty->driver_data; 1177 struct capiminor *mp = tty->driver_data;
1183 1178
1184 pr_debug("capinc_tty_unthrottle\n"); 1179 pr_debug("capinc_tty_unthrottle\n");
1185 mp->ttyinstop = 0; 1180 mp->ttyinstop = 0;
1186 handle_minor_recv(mp); 1181 handle_minor_recv(mp);
1187 } 1182 }
1188 1183
1189 static void capinc_tty_stop(struct tty_struct *tty) 1184 static void capinc_tty_stop(struct tty_struct *tty)
1190 { 1185 {
1191 struct capiminor *mp = tty->driver_data; 1186 struct capiminor *mp = tty->driver_data;
1192 1187
1193 pr_debug("capinc_tty_stop\n"); 1188 pr_debug("capinc_tty_stop\n");
1194 mp->ttyoutstop = 1; 1189 mp->ttyoutstop = 1;
1195 } 1190 }
1196 1191
1197 static void capinc_tty_start(struct tty_struct *tty) 1192 static void capinc_tty_start(struct tty_struct *tty)
1198 { 1193 {
1199 struct capiminor *mp = tty->driver_data; 1194 struct capiminor *mp = tty->driver_data;
1200 1195
1201 pr_debug("capinc_tty_start\n"); 1196 pr_debug("capinc_tty_start\n");
1202 mp->ttyoutstop = 0; 1197 mp->ttyoutstop = 0;
1203 handle_minor_send(mp); 1198 handle_minor_send(mp);
1204 } 1199 }
1205 1200
1206 static void capinc_tty_hangup(struct tty_struct *tty) 1201 static void capinc_tty_hangup(struct tty_struct *tty)
1207 { 1202 {
1208 struct capiminor *mp = tty->driver_data; 1203 struct capiminor *mp = tty->driver_data;
1209 1204
1210 pr_debug("capinc_tty_hangup\n"); 1205 pr_debug("capinc_tty_hangup\n");
1211 tty_port_hangup(&mp->port); 1206 tty_port_hangup(&mp->port);
1212 } 1207 }
1213 1208
1214 static int capinc_tty_break_ctl(struct tty_struct *tty, int state) 1209 static int capinc_tty_break_ctl(struct tty_struct *tty, int state)
1215 { 1210 {
1216 pr_debug("capinc_tty_break_ctl(%d)\n", state); 1211 pr_debug("capinc_tty_break_ctl(%d)\n", state);
1217 return 0; 1212 return 0;
1218 } 1213 }
1219 1214
1220 static void capinc_tty_flush_buffer(struct tty_struct *tty) 1215 static void capinc_tty_flush_buffer(struct tty_struct *tty)
1221 { 1216 {
1222 pr_debug("capinc_tty_flush_buffer\n"); 1217 pr_debug("capinc_tty_flush_buffer\n");
1223 } 1218 }
1224 1219
1225 static void capinc_tty_set_ldisc(struct tty_struct *tty) 1220 static void capinc_tty_set_ldisc(struct tty_struct *tty)
1226 { 1221 {
1227 pr_debug("capinc_tty_set_ldisc\n"); 1222 pr_debug("capinc_tty_set_ldisc\n");
1228 } 1223 }
1229 1224
1230 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch) 1225 static void capinc_tty_send_xchar(struct tty_struct *tty, char ch)
1231 { 1226 {
1232 pr_debug("capinc_tty_send_xchar(%d)\n", ch); 1227 pr_debug("capinc_tty_send_xchar(%d)\n", ch);
1233 } 1228 }
1234 1229
1235 static const struct tty_operations capinc_ops = { 1230 static const struct tty_operations capinc_ops = {
1236 .open = capinc_tty_open, 1231 .open = capinc_tty_open,
1237 .close = capinc_tty_close, 1232 .close = capinc_tty_close,
1238 .write = capinc_tty_write, 1233 .write = capinc_tty_write,
1239 .put_char = capinc_tty_put_char, 1234 .put_char = capinc_tty_put_char,
1240 .flush_chars = capinc_tty_flush_chars, 1235 .flush_chars = capinc_tty_flush_chars,
1241 .write_room = capinc_tty_write_room, 1236 .write_room = capinc_tty_write_room,
1242 .chars_in_buffer = capinc_tty_chars_in_buffer, 1237 .chars_in_buffer = capinc_tty_chars_in_buffer,
1243 .ioctl = capinc_tty_ioctl, 1238 .ioctl = capinc_tty_ioctl,
1244 .set_termios = capinc_tty_set_termios, 1239 .set_termios = capinc_tty_set_termios,
1245 .throttle = capinc_tty_throttle, 1240 .throttle = capinc_tty_throttle,
1246 .unthrottle = capinc_tty_unthrottle, 1241 .unthrottle = capinc_tty_unthrottle,
1247 .stop = capinc_tty_stop, 1242 .stop = capinc_tty_stop,
1248 .start = capinc_tty_start, 1243 .start = capinc_tty_start,
1249 .hangup = capinc_tty_hangup, 1244 .hangup = capinc_tty_hangup,
1250 .break_ctl = capinc_tty_break_ctl, 1245 .break_ctl = capinc_tty_break_ctl,
1251 .flush_buffer = capinc_tty_flush_buffer, 1246 .flush_buffer = capinc_tty_flush_buffer,
1252 .set_ldisc = capinc_tty_set_ldisc, 1247 .set_ldisc = capinc_tty_set_ldisc,
1253 .send_xchar = capinc_tty_send_xchar, 1248 .send_xchar = capinc_tty_send_xchar,
1254 .install = capinc_tty_install, 1249 .install = capinc_tty_install,
1255 .cleanup = capinc_tty_cleanup, 1250 .cleanup = capinc_tty_cleanup,
1256 }; 1251 };
1257 1252
1258 static int __init capinc_tty_init(void) 1253 static int __init capinc_tty_init(void)
1259 { 1254 {
1260 struct tty_driver *drv; 1255 struct tty_driver *drv;
1261 int err; 1256 int err;
1262 1257
1263 if (capi_ttyminors > CAPINC_MAX_PORTS) 1258 if (capi_ttyminors > CAPINC_MAX_PORTS)
1264 capi_ttyminors = CAPINC_MAX_PORTS; 1259 capi_ttyminors = CAPINC_MAX_PORTS;
1265 if (capi_ttyminors <= 0) 1260 if (capi_ttyminors <= 0)
1266 capi_ttyminors = CAPINC_NR_PORTS; 1261 capi_ttyminors = CAPINC_NR_PORTS;
1267 1262
1268 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors, 1263 capiminors = kzalloc(sizeof(struct capi_minor *) * capi_ttyminors,
1269 GFP_KERNEL); 1264 GFP_KERNEL);
1270 if (!capiminors) 1265 if (!capiminors)
1271 return -ENOMEM; 1266 return -ENOMEM;
1272 1267
1273 drv = alloc_tty_driver(capi_ttyminors); 1268 drv = alloc_tty_driver(capi_ttyminors);
1274 if (!drv) { 1269 if (!drv) {
1275 kfree(capiminors); 1270 kfree(capiminors);
1276 return -ENOMEM; 1271 return -ENOMEM;
1277 } 1272 }
1278 drv->driver_name = "capi_nc"; 1273 drv->driver_name = "capi_nc";
1279 drv->name = "capi"; 1274 drv->name = "capi";
1280 drv->major = 0; 1275 drv->major = 0;
1281 drv->minor_start = 0; 1276 drv->minor_start = 0;
1282 drv->type = TTY_DRIVER_TYPE_SERIAL; 1277 drv->type = TTY_DRIVER_TYPE_SERIAL;
1283 drv->subtype = SERIAL_TYPE_NORMAL; 1278 drv->subtype = SERIAL_TYPE_NORMAL;
1284 drv->init_termios = tty_std_termios; 1279 drv->init_termios = tty_std_termios;
1285 drv->init_termios.c_iflag = ICRNL; 1280 drv->init_termios.c_iflag = ICRNL;
1286 drv->init_termios.c_oflag = OPOST | ONLCR; 1281 drv->init_termios.c_oflag = OPOST | ONLCR;
1287 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; 1282 drv->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1288 drv->init_termios.c_lflag = 0; 1283 drv->init_termios.c_lflag = 0;
1289 drv->flags = 1284 drv->flags =
1290 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS | 1285 TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS |
1291 TTY_DRIVER_DYNAMIC_DEV; 1286 TTY_DRIVER_DYNAMIC_DEV;
1292 tty_set_operations(drv, &capinc_ops); 1287 tty_set_operations(drv, &capinc_ops);
1293 1288
1294 err = tty_register_driver(drv); 1289 err = tty_register_driver(drv);
1295 if (err) { 1290 if (err) {
1296 put_tty_driver(drv); 1291 put_tty_driver(drv);
1297 kfree(capiminors); 1292 kfree(capiminors);
1298 printk(KERN_ERR "Couldn't register capi_nc driver\n"); 1293 printk(KERN_ERR "Couldn't register capi_nc driver\n");
1299 return err; 1294 return err;
1300 } 1295 }
1301 capinc_tty_driver = drv; 1296 capinc_tty_driver = drv;
1302 return 0; 1297 return 0;
1303 } 1298 }
1304 1299
1305 static void __exit capinc_tty_exit(void) 1300 static void __exit capinc_tty_exit(void)
1306 { 1301 {
1307 tty_unregister_driver(capinc_tty_driver); 1302 tty_unregister_driver(capinc_tty_driver);
1308 put_tty_driver(capinc_tty_driver); 1303 put_tty_driver(capinc_tty_driver);
1309 kfree(capiminors); 1304 kfree(capiminors);
1310 } 1305 }
1311 1306
1312 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ 1307 #else /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1313 1308
1314 static inline int capinc_tty_init(void) 1309 static inline int capinc_tty_init(void)
1315 { 1310 {
1316 return 0; 1311 return 0;
1317 } 1312 }
1318 1313
1319 static inline void capinc_tty_exit(void) { } 1314 static inline void capinc_tty_exit(void) { }
1320 1315
1321 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */ 1316 #endif /* !CONFIG_ISDN_CAPI_MIDDLEWARE */
1322 1317
1323 /* -------- /proc functions ----------------------------------------- */ 1318 /* -------- /proc functions ----------------------------------------- */
1324 1319
1325 /* 1320 /*
1326 * /proc/capi/capi20: 1321 * /proc/capi/capi20:
1327 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt 1322 * minor applid nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
1328 */ 1323 */
1329 static int capi20_proc_show(struct seq_file *m, void *v) 1324 static int capi20_proc_show(struct seq_file *m, void *v)
1330 { 1325 {
1331 struct capidev *cdev; 1326 struct capidev *cdev;
1332 struct list_head *l; 1327 struct list_head *l;
1333 1328
1334 mutex_lock(&capidev_list_lock); 1329 mutex_lock(&capidev_list_lock);
1335 list_for_each(l, &capidev_list) { 1330 list_for_each(l, &capidev_list) {
1336 cdev = list_entry(l, struct capidev, list); 1331 cdev = list_entry(l, struct capidev, list);
1337 seq_printf(m, "0 %d %lu %lu %lu %lu\n", 1332 seq_printf(m, "0 %d %lu %lu %lu %lu\n",
1338 cdev->ap.applid, 1333 cdev->ap.applid,
1339 cdev->ap.nrecvctlpkt, 1334 cdev->ap.nrecvctlpkt,
1340 cdev->ap.nrecvdatapkt, 1335 cdev->ap.nrecvdatapkt,
1341 cdev->ap.nsentctlpkt, 1336 cdev->ap.nsentctlpkt,
1342 cdev->ap.nsentdatapkt); 1337 cdev->ap.nsentdatapkt);
1343 } 1338 }
1344 mutex_unlock(&capidev_list_lock); 1339 mutex_unlock(&capidev_list_lock);
1345 return 0; 1340 return 0;
1346 } 1341 }
1347 1342
1348 static int capi20_proc_open(struct inode *inode, struct file *file) 1343 static int capi20_proc_open(struct inode *inode, struct file *file)
1349 { 1344 {
1350 return single_open(file, capi20_proc_show, NULL); 1345 return single_open(file, capi20_proc_show, NULL);
1351 } 1346 }
1352 1347
1353 static const struct file_operations capi20_proc_fops = { 1348 static const struct file_operations capi20_proc_fops = {
1354 .owner = THIS_MODULE, 1349 .owner = THIS_MODULE,
1355 .open = capi20_proc_open, 1350 .open = capi20_proc_open,
1356 .read = seq_read, 1351 .read = seq_read,
1357 .llseek = seq_lseek, 1352 .llseek = seq_lseek,
1358 .release = single_release, 1353 .release = single_release,
1359 }; 1354 };
1360 1355
1361 /* 1356 /*
1362 * /proc/capi/capi20ncci: 1357 * /proc/capi/capi20ncci:
1363 * applid ncci 1358 * applid ncci
1364 */ 1359 */
1365 static int capi20ncci_proc_show(struct seq_file *m, void *v) 1360 static int capi20ncci_proc_show(struct seq_file *m, void *v)
1366 { 1361 {
1367 struct capidev *cdev; 1362 struct capidev *cdev;
1368 struct capincci *np; 1363 struct capincci *np;
1369 1364
1370 mutex_lock(&capidev_list_lock); 1365 mutex_lock(&capidev_list_lock);
1371 list_for_each_entry(cdev, &capidev_list, list) { 1366 list_for_each_entry(cdev, &capidev_list, list) {
1372 mutex_lock(&cdev->lock); 1367 mutex_lock(&cdev->lock);
1373 list_for_each_entry(np, &cdev->nccis, list) 1368 list_for_each_entry(np, &cdev->nccis, list)
1374 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci); 1369 seq_printf(m, "%d 0x%x\n", cdev->ap.applid, np->ncci);
1375 mutex_unlock(&cdev->lock); 1370 mutex_unlock(&cdev->lock);
1376 } 1371 }
1377 mutex_unlock(&capidev_list_lock); 1372 mutex_unlock(&capidev_list_lock);
1378 return 0; 1373 return 0;
1379 } 1374 }
1380 1375
1381 static int capi20ncci_proc_open(struct inode *inode, struct file *file) 1376 static int capi20ncci_proc_open(struct inode *inode, struct file *file)
1382 { 1377 {
1383 return single_open(file, capi20ncci_proc_show, NULL); 1378 return single_open(file, capi20ncci_proc_show, NULL);
1384 } 1379 }
1385 1380
1386 static const struct file_operations capi20ncci_proc_fops = { 1381 static const struct file_operations capi20ncci_proc_fops = {
1387 .owner = THIS_MODULE, 1382 .owner = THIS_MODULE,
1388 .open = capi20ncci_proc_open, 1383 .open = capi20ncci_proc_open,
1389 .read = seq_read, 1384 .read = seq_read,
1390 .llseek = seq_lseek, 1385 .llseek = seq_lseek,
1391 .release = single_release, 1386 .release = single_release,
1392 }; 1387 };
1393 1388
1394 static void __init proc_init(void) 1389 static void __init proc_init(void)
1395 { 1390 {
1396 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops); 1391 proc_create("capi/capi20", 0, NULL, &capi20_proc_fops);
1397 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops); 1392 proc_create("capi/capi20ncci", 0, NULL, &capi20ncci_proc_fops);
1398 } 1393 }
1399 1394
1400 static void __exit proc_exit(void) 1395 static void __exit proc_exit(void)
1401 { 1396 {
1402 remove_proc_entry("capi/capi20", NULL); 1397 remove_proc_entry("capi/capi20", NULL);
1403 remove_proc_entry("capi/capi20ncci", NULL); 1398 remove_proc_entry("capi/capi20ncci", NULL);
1404 } 1399 }
1405 1400
1406 /* -------- init function and module interface ---------------------- */ 1401 /* -------- init function and module interface ---------------------- */
1407 1402
1408 1403
1409 static int __init capi_init(void) 1404 static int __init capi_init(void)
1410 { 1405 {
1411 const char *compileinfo; 1406 const char *compileinfo;
1412 int major_ret; 1407 int major_ret;
1413 1408
1414 major_ret = register_chrdev(capi_major, "capi20", &capi_fops); 1409 major_ret = register_chrdev(capi_major, "capi20", &capi_fops);
1415 if (major_ret < 0) { 1410 if (major_ret < 0) {
1416 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major); 1411 printk(KERN_ERR "capi20: unable to get major %d\n", capi_major);
1417 return major_ret; 1412 return major_ret;
1418 } 1413 }
1419 capi_class = class_create(THIS_MODULE, "capi"); 1414 capi_class = class_create(THIS_MODULE, "capi");
1420 if (IS_ERR(capi_class)) { 1415 if (IS_ERR(capi_class)) {
1421 unregister_chrdev(capi_major, "capi20"); 1416 unregister_chrdev(capi_major, "capi20");
1422 return PTR_ERR(capi_class); 1417 return PTR_ERR(capi_class);
1423 } 1418 }
1424 1419
1425 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi"); 1420 device_create(capi_class, NULL, MKDEV(capi_major, 0), NULL, "capi");
1426 1421
1427 if (capinc_tty_init() < 0) { 1422 if (capinc_tty_init() < 0) {
1428 device_destroy(capi_class, MKDEV(capi_major, 0)); 1423 device_destroy(capi_class, MKDEV(capi_major, 0));
1429 class_destroy(capi_class); 1424 class_destroy(capi_class);
1430 unregister_chrdev(capi_major, "capi20"); 1425 unregister_chrdev(capi_major, "capi20");
1431 return -ENOMEM; 1426 return -ENOMEM;
1432 } 1427 }
1433 1428
1434 proc_init(); 1429 proc_init();
1435 1430
1436 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE 1431 #ifdef CONFIG_ISDN_CAPI_MIDDLEWARE
1437 compileinfo = " (middleware)"; 1432 compileinfo = " (middleware)";
1438 #else 1433 #else
1439 compileinfo = " (no middleware)"; 1434 compileinfo = " (no middleware)";
1440 #endif 1435 #endif
1441 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n", 1436 printk(KERN_NOTICE "CAPI 2.0 started up with major %d%s\n",
1442 capi_major, compileinfo); 1437 capi_major, compileinfo);
1443 1438
1444 return 0; 1439 return 0;
1445 } 1440 }
1446 1441
1447 static void __exit capi_exit(void) 1442 static void __exit capi_exit(void)
1448 { 1443 {
1449 proc_exit(); 1444 proc_exit();
1450 1445
1451 device_destroy(capi_class, MKDEV(capi_major, 0)); 1446 device_destroy(capi_class, MKDEV(capi_major, 0));
1452 class_destroy(capi_class); 1447 class_destroy(capi_class);
1453 unregister_chrdev(capi_major, "capi20"); 1448 unregister_chrdev(capi_major, "capi20");
1454 1449
1455 capinc_tty_exit(); 1450 capinc_tty_exit();
1456 } 1451 }
1457 1452
1458 module_init(capi_init); 1453 module_init(capi_init);
1459 module_exit(capi_exit); 1454 module_exit(capi_exit);
1460 1455
drivers/isdn/gigaset/interface.c
1 /* 1 /*
2 * interface to user space for the gigaset driver 2 * interface to user space for the gigaset driver
3 * 3 *
4 * Copyright (c) 2004 by Hansjoerg Lipp <hjlipp@web.de> 4 * Copyright (c) 2004 by Hansjoerg Lipp <hjlipp@web.de>
5 * 5 *
6 * ===================================================================== 6 * =====================================================================
7 * This program is free software; you can redistribute it and/or 7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as 8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of 9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version. 10 * the License, or (at your option) any later version.
11 * ===================================================================== 11 * =====================================================================
12 */ 12 */
13 13
14 #include "gigaset.h" 14 #include "gigaset.h"
15 #include <linux/gigaset_dev.h> 15 #include <linux/gigaset_dev.h>
16 #include <linux/tty_flip.h> 16 #include <linux/tty_flip.h>
17 #include <linux/module.h> 17 #include <linux/module.h>
18 18
19 /*** our ioctls ***/ 19 /*** our ioctls ***/
20 20
21 static int if_lock(struct cardstate *cs, int *arg) 21 static int if_lock(struct cardstate *cs, int *arg)
22 { 22 {
23 int cmd = *arg; 23 int cmd = *arg;
24 24
25 gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd); 25 gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);
26 26
27 if (cmd > 1) 27 if (cmd > 1)
28 return -EINVAL; 28 return -EINVAL;
29 29
30 if (cmd < 0) { 30 if (cmd < 0) {
31 *arg = cs->mstate == MS_LOCKED; 31 *arg = cs->mstate == MS_LOCKED;
32 return 0; 32 return 0;
33 } 33 }
34 34
35 if (!cmd && cs->mstate == MS_LOCKED && cs->connected) { 35 if (!cmd && cs->mstate == MS_LOCKED && cs->connected) {
36 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS); 36 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS);
37 cs->ops->baud_rate(cs, B115200); 37 cs->ops->baud_rate(cs, B115200);
38 cs->ops->set_line_ctrl(cs, CS8); 38 cs->ops->set_line_ctrl(cs, CS8);
39 cs->control_state = TIOCM_DTR | TIOCM_RTS; 39 cs->control_state = TIOCM_DTR | TIOCM_RTS;
40 } 40 }
41 41
42 cs->waiting = 1; 42 cs->waiting = 1;
43 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK, 43 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
44 NULL, cmd, NULL)) { 44 NULL, cmd, NULL)) {
45 cs->waiting = 0; 45 cs->waiting = 0;
46 return -ENOMEM; 46 return -ENOMEM;
47 } 47 }
48 gigaset_schedule_event(cs); 48 gigaset_schedule_event(cs);
49 49
50 wait_event(cs->waitqueue, !cs->waiting); 50 wait_event(cs->waitqueue, !cs->waiting);
51 51
52 if (cs->cmd_result >= 0) { 52 if (cs->cmd_result >= 0) {
53 *arg = cs->cmd_result; 53 *arg = cs->cmd_result;
54 return 0; 54 return 0;
55 } 55 }
56 56
57 return cs->cmd_result; 57 return cs->cmd_result;
58 } 58 }
59 59
60 static int if_version(struct cardstate *cs, unsigned arg[4]) 60 static int if_version(struct cardstate *cs, unsigned arg[4])
61 { 61 {
62 static const unsigned version[4] = GIG_VERSION; 62 static const unsigned version[4] = GIG_VERSION;
63 static const unsigned compat[4] = GIG_COMPAT; 63 static const unsigned compat[4] = GIG_COMPAT;
64 unsigned cmd = arg[0]; 64 unsigned cmd = arg[0];
65 65
66 gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd); 66 gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
67 67
68 switch (cmd) { 68 switch (cmd) {
69 case GIGVER_DRIVER: 69 case GIGVER_DRIVER:
70 memcpy(arg, version, sizeof version); 70 memcpy(arg, version, sizeof version);
71 return 0; 71 return 0;
72 case GIGVER_COMPAT: 72 case GIGVER_COMPAT:
73 memcpy(arg, compat, sizeof compat); 73 memcpy(arg, compat, sizeof compat);
74 return 0; 74 return 0;
75 case GIGVER_FWBASE: 75 case GIGVER_FWBASE:
76 cs->waiting = 1; 76 cs->waiting = 1;
77 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER, 77 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
78 NULL, 0, arg)) { 78 NULL, 0, arg)) {
79 cs->waiting = 0; 79 cs->waiting = 0;
80 return -ENOMEM; 80 return -ENOMEM;
81 } 81 }
82 gigaset_schedule_event(cs); 82 gigaset_schedule_event(cs);
83 83
84 wait_event(cs->waitqueue, !cs->waiting); 84 wait_event(cs->waitqueue, !cs->waiting);
85 85
86 if (cs->cmd_result >= 0) 86 if (cs->cmd_result >= 0)
87 return 0; 87 return 0;
88 88
89 return cs->cmd_result; 89 return cs->cmd_result;
90 default: 90 default:
91 return -EINVAL; 91 return -EINVAL;
92 } 92 }
93 } 93 }
94 94
95 static int if_config(struct cardstate *cs, int *arg) 95 static int if_config(struct cardstate *cs, int *arg)
96 { 96 {
97 gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg); 97 gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
98 98
99 if (*arg != 1) 99 if (*arg != 1)
100 return -EINVAL; 100 return -EINVAL;
101 101
102 if (cs->mstate != MS_LOCKED) 102 if (cs->mstate != MS_LOCKED)
103 return -EBUSY; 103 return -EBUSY;
104 104
105 if (!cs->connected) { 105 if (!cs->connected) {
106 pr_err("%s: not connected\n", __func__); 106 pr_err("%s: not connected\n", __func__);
107 return -ENODEV; 107 return -ENODEV;
108 } 108 }
109 109
110 *arg = 0; 110 *arg = 0;
111 return gigaset_enterconfigmode(cs); 111 return gigaset_enterconfigmode(cs);
112 } 112 }
113 113
114 /*** the terminal driver ***/ 114 /*** the terminal driver ***/
115 115
116 static int if_open(struct tty_struct *tty, struct file *filp) 116 static int if_open(struct tty_struct *tty, struct file *filp)
117 { 117 {
118 struct cardstate *cs; 118 struct cardstate *cs;
119 119
120 gig_dbg(DEBUG_IF, "%d+%d: %s()", 120 gig_dbg(DEBUG_IF, "%d+%d: %s()",
121 tty->driver->minor_start, tty->index, __func__); 121 tty->driver->minor_start, tty->index, __func__);
122 122
123 cs = gigaset_get_cs_by_tty(tty); 123 cs = gigaset_get_cs_by_tty(tty);
124 if (!cs || !try_module_get(cs->driver->owner)) 124 if (!cs || !try_module_get(cs->driver->owner))
125 return -ENODEV; 125 return -ENODEV;
126 126
127 if (mutex_lock_interruptible(&cs->mutex)) { 127 if (mutex_lock_interruptible(&cs->mutex)) {
128 module_put(cs->driver->owner); 128 module_put(cs->driver->owner);
129 return -ERESTARTSYS; 129 return -ERESTARTSYS;
130 } 130 }
131 tty->driver_data = cs; 131 tty->driver_data = cs;
132 132
133 ++cs->port.count; 133 ++cs->port.count;
134 134
135 if (cs->port.count == 1) { 135 if (cs->port.count == 1) {
136 tty_port_tty_set(&cs->port, tty); 136 tty_port_tty_set(&cs->port, tty);
137 cs->port.low_latency = 1; 137 cs->port.low_latency = 1;
138 } 138 }
139 139
140 mutex_unlock(&cs->mutex); 140 mutex_unlock(&cs->mutex);
141 return 0; 141 return 0;
142 } 142 }
143 143
144 static void if_close(struct tty_struct *tty, struct file *filp) 144 static void if_close(struct tty_struct *tty, struct file *filp)
145 { 145 {
146 struct cardstate *cs = tty->driver_data; 146 struct cardstate *cs = tty->driver_data;
147 147
148 if (!cs) { /* happens if we didn't find cs in open */ 148 if (!cs) { /* happens if we didn't find cs in open */
149 gig_dbg(DEBUG_IF, "%s: no cardstate", __func__); 149 gig_dbg(DEBUG_IF, "%s: no cardstate", __func__);
150 return; 150 return;
151 } 151 }
152 152
153 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); 153 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
154 154
155 mutex_lock(&cs->mutex); 155 mutex_lock(&cs->mutex);
156 156
157 if (!cs->connected) 157 if (!cs->connected)
158 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ 158 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
159 else if (!cs->port.count) 159 else if (!cs->port.count)
160 dev_warn(cs->dev, "%s: device not opened\n", __func__); 160 dev_warn(cs->dev, "%s: device not opened\n", __func__);
161 else if (!--cs->port.count) 161 else if (!--cs->port.count)
162 tty_port_tty_set(&cs->port, NULL); 162 tty_port_tty_set(&cs->port, NULL);
163 163
164 mutex_unlock(&cs->mutex); 164 mutex_unlock(&cs->mutex);
165 165
166 module_put(cs->driver->owner); 166 module_put(cs->driver->owner);
167 } 167 }
168 168
169 static int if_ioctl(struct tty_struct *tty, 169 static int if_ioctl(struct tty_struct *tty,
170 unsigned int cmd, unsigned long arg) 170 unsigned int cmd, unsigned long arg)
171 { 171 {
172 struct cardstate *cs = tty->driver_data; 172 struct cardstate *cs = tty->driver_data;
173 int retval = -ENODEV; 173 int retval = -ENODEV;
174 int int_arg; 174 int int_arg;
175 unsigned char buf[6]; 175 unsigned char buf[6];
176 unsigned version[4]; 176 unsigned version[4];
177 177
178 gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd); 178 gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
179 179
180 if (mutex_lock_interruptible(&cs->mutex)) 180 if (mutex_lock_interruptible(&cs->mutex))
181 return -ERESTARTSYS; 181 return -ERESTARTSYS;
182 182
183 if (!cs->connected) { 183 if (!cs->connected) {
184 gig_dbg(DEBUG_IF, "not connected"); 184 gig_dbg(DEBUG_IF, "not connected");
185 retval = -ENODEV; 185 retval = -ENODEV;
186 } else { 186 } else {
187 retval = 0; 187 retval = 0;
188 switch (cmd) { 188 switch (cmd) {
189 case GIGASET_REDIR: 189 case GIGASET_REDIR:
190 retval = get_user(int_arg, (int __user *) arg); 190 retval = get_user(int_arg, (int __user *) arg);
191 if (retval >= 0) 191 if (retval >= 0)
192 retval = if_lock(cs, &int_arg); 192 retval = if_lock(cs, &int_arg);
193 if (retval >= 0) 193 if (retval >= 0)
194 retval = put_user(int_arg, (int __user *) arg); 194 retval = put_user(int_arg, (int __user *) arg);
195 break; 195 break;
196 case GIGASET_CONFIG: 196 case GIGASET_CONFIG:
197 retval = get_user(int_arg, (int __user *) arg); 197 retval = get_user(int_arg, (int __user *) arg);
198 if (retval >= 0) 198 if (retval >= 0)
199 retval = if_config(cs, &int_arg); 199 retval = if_config(cs, &int_arg);
200 if (retval >= 0) 200 if (retval >= 0)
201 retval = put_user(int_arg, (int __user *) arg); 201 retval = put_user(int_arg, (int __user *) arg);
202 break; 202 break;
203 case GIGASET_BRKCHARS: 203 case GIGASET_BRKCHARS:
204 retval = copy_from_user(&buf, 204 retval = copy_from_user(&buf,
205 (const unsigned char __user *) arg, 6) 205 (const unsigned char __user *) arg, 6)
206 ? -EFAULT : 0; 206 ? -EFAULT : 0;
207 if (retval >= 0) { 207 if (retval >= 0) {
208 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS", 208 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
209 6, (const unsigned char *) arg); 209 6, (const unsigned char *) arg);
210 retval = cs->ops->brkchars(cs, buf); 210 retval = cs->ops->brkchars(cs, buf);
211 } 211 }
212 break; 212 break;
213 case GIGASET_VERSION: 213 case GIGASET_VERSION:
214 retval = copy_from_user(version, 214 retval = copy_from_user(version,
215 (unsigned __user *) arg, sizeof version) 215 (unsigned __user *) arg, sizeof version)
216 ? -EFAULT : 0; 216 ? -EFAULT : 0;
217 if (retval >= 0) 217 if (retval >= 0)
218 retval = if_version(cs, version); 218 retval = if_version(cs, version);
219 if (retval >= 0) 219 if (retval >= 0)
220 retval = copy_to_user((unsigned __user *) arg, 220 retval = copy_to_user((unsigned __user *) arg,
221 version, sizeof version) 221 version, sizeof version)
222 ? -EFAULT : 0; 222 ? -EFAULT : 0;
223 break; 223 break;
224 default: 224 default:
225 gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x", 225 gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x",
226 __func__, cmd); 226 __func__, cmd);
227 retval = -ENOIOCTLCMD; 227 retval = -ENOIOCTLCMD;
228 } 228 }
229 } 229 }
230 230
231 mutex_unlock(&cs->mutex); 231 mutex_unlock(&cs->mutex);
232 232
233 return retval; 233 return retval;
234 } 234 }
235 235
236 static int if_tiocmget(struct tty_struct *tty) 236 static int if_tiocmget(struct tty_struct *tty)
237 { 237 {
238 struct cardstate *cs = tty->driver_data; 238 struct cardstate *cs = tty->driver_data;
239 int retval; 239 int retval;
240 240
241 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); 241 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
242 242
243 if (mutex_lock_interruptible(&cs->mutex)) 243 if (mutex_lock_interruptible(&cs->mutex))
244 return -ERESTARTSYS; 244 return -ERESTARTSYS;
245 245
246 retval = cs->control_state & (TIOCM_RTS | TIOCM_DTR); 246 retval = cs->control_state & (TIOCM_RTS | TIOCM_DTR);
247 247
248 mutex_unlock(&cs->mutex); 248 mutex_unlock(&cs->mutex);
249 249
250 return retval; 250 return retval;
251 } 251 }
252 252
253 static int if_tiocmset(struct tty_struct *tty, 253 static int if_tiocmset(struct tty_struct *tty,
254 unsigned int set, unsigned int clear) 254 unsigned int set, unsigned int clear)
255 { 255 {
256 struct cardstate *cs = tty->driver_data; 256 struct cardstate *cs = tty->driver_data;
257 int retval; 257 int retval;
258 unsigned mc; 258 unsigned mc;
259 259
260 gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)", 260 gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
261 cs->minor_index, __func__, set, clear); 261 cs->minor_index, __func__, set, clear);
262 262
263 if (mutex_lock_interruptible(&cs->mutex)) 263 if (mutex_lock_interruptible(&cs->mutex))
264 return -ERESTARTSYS; 264 return -ERESTARTSYS;
265 265
266 if (!cs->connected) { 266 if (!cs->connected) {
267 gig_dbg(DEBUG_IF, "not connected"); 267 gig_dbg(DEBUG_IF, "not connected");
268 retval = -ENODEV; 268 retval = -ENODEV;
269 } else { 269 } else {
270 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS | TIOCM_DTR); 270 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS | TIOCM_DTR);
271 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc); 271 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
272 cs->control_state = mc; 272 cs->control_state = mc;
273 } 273 }
274 274
275 mutex_unlock(&cs->mutex); 275 mutex_unlock(&cs->mutex);
276 276
277 return retval; 277 return retval;
278 } 278 }
279 279
280 static int if_write(struct tty_struct *tty, const unsigned char *buf, int count) 280 static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
281 { 281 {
282 struct cardstate *cs = tty->driver_data; 282 struct cardstate *cs = tty->driver_data;
283 struct cmdbuf_t *cb; 283 struct cmdbuf_t *cb;
284 int retval; 284 int retval;
285 285
286 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); 286 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
287 287
288 if (mutex_lock_interruptible(&cs->mutex)) 288 if (mutex_lock_interruptible(&cs->mutex))
289 return -ERESTARTSYS; 289 return -ERESTARTSYS;
290 290
291 if (!cs->connected) { 291 if (!cs->connected) {
292 gig_dbg(DEBUG_IF, "not connected"); 292 gig_dbg(DEBUG_IF, "not connected");
293 retval = -ENODEV; 293 retval = -ENODEV;
294 goto done; 294 goto done;
295 } 295 }
296 if (cs->mstate != MS_LOCKED) { 296 if (cs->mstate != MS_LOCKED) {
297 dev_warn(cs->dev, "can't write to unlocked device\n"); 297 dev_warn(cs->dev, "can't write to unlocked device\n");
298 retval = -EBUSY; 298 retval = -EBUSY;
299 goto done; 299 goto done;
300 } 300 }
301 if (count <= 0) { 301 if (count <= 0) {
302 /* nothing to do */ 302 /* nothing to do */
303 retval = 0; 303 retval = 0;
304 goto done; 304 goto done;
305 } 305 }
306 306
307 cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL); 307 cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);
308 if (!cb) { 308 if (!cb) {
309 dev_err(cs->dev, "%s: out of memory\n", __func__); 309 dev_err(cs->dev, "%s: out of memory\n", __func__);
310 retval = -ENOMEM; 310 retval = -ENOMEM;
311 goto done; 311 goto done;
312 } 312 }
313 313
314 memcpy(cb->buf, buf, count); 314 memcpy(cb->buf, buf, count);
315 cb->len = count; 315 cb->len = count;
316 cb->offset = 0; 316 cb->offset = 0;
317 cb->next = NULL; 317 cb->next = NULL;
318 cb->wake_tasklet = &cs->if_wake_tasklet; 318 cb->wake_tasklet = &cs->if_wake_tasklet;
319 retval = cs->ops->write_cmd(cs, cb); 319 retval = cs->ops->write_cmd(cs, cb);
320 done: 320 done:
321 mutex_unlock(&cs->mutex); 321 mutex_unlock(&cs->mutex);
322 return retval; 322 return retval;
323 } 323 }
324 324
325 static int if_write_room(struct tty_struct *tty) 325 static int if_write_room(struct tty_struct *tty)
326 { 326 {
327 struct cardstate *cs = tty->driver_data; 327 struct cardstate *cs = tty->driver_data;
328 int retval; 328 int retval;
329 329
330 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); 330 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
331 331
332 if (mutex_lock_interruptible(&cs->mutex)) 332 if (mutex_lock_interruptible(&cs->mutex))
333 return -ERESTARTSYS; 333 return -ERESTARTSYS;
334 334
335 if (!cs->connected) { 335 if (!cs->connected) {
336 gig_dbg(DEBUG_IF, "not connected"); 336 gig_dbg(DEBUG_IF, "not connected");
337 retval = -ENODEV; 337 retval = -ENODEV;
338 } else if (cs->mstate != MS_LOCKED) { 338 } else if (cs->mstate != MS_LOCKED) {
339 dev_warn(cs->dev, "can't write to unlocked device\n"); 339 dev_warn(cs->dev, "can't write to unlocked device\n");
340 retval = -EBUSY; 340 retval = -EBUSY;
341 } else 341 } else
342 retval = cs->ops->write_room(cs); 342 retval = cs->ops->write_room(cs);
343 343
344 mutex_unlock(&cs->mutex); 344 mutex_unlock(&cs->mutex);
345 345
346 return retval; 346 return retval;
347 } 347 }
348 348
349 static int if_chars_in_buffer(struct tty_struct *tty) 349 static int if_chars_in_buffer(struct tty_struct *tty)
350 { 350 {
351 struct cardstate *cs = tty->driver_data; 351 struct cardstate *cs = tty->driver_data;
352 int retval = 0; 352 int retval = 0;
353 353
354 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); 354 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
355 355
356 mutex_lock(&cs->mutex); 356 mutex_lock(&cs->mutex);
357 357
358 if (!cs->connected) 358 if (!cs->connected)
359 gig_dbg(DEBUG_IF, "not connected"); 359 gig_dbg(DEBUG_IF, "not connected");
360 else if (cs->mstate != MS_LOCKED) 360 else if (cs->mstate != MS_LOCKED)
361 dev_warn(cs->dev, "can't write to unlocked device\n"); 361 dev_warn(cs->dev, "can't write to unlocked device\n");
362 else 362 else
363 retval = cs->ops->chars_in_buffer(cs); 363 retval = cs->ops->chars_in_buffer(cs);
364 364
365 mutex_unlock(&cs->mutex); 365 mutex_unlock(&cs->mutex);
366 366
367 return retval; 367 return retval;
368 } 368 }
369 369
370 static void if_throttle(struct tty_struct *tty) 370 static void if_throttle(struct tty_struct *tty)
371 { 371 {
372 struct cardstate *cs = tty->driver_data; 372 struct cardstate *cs = tty->driver_data;
373 373
374 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); 374 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
375 375
376 mutex_lock(&cs->mutex); 376 mutex_lock(&cs->mutex);
377 377
378 if (!cs->connected) 378 if (!cs->connected)
379 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ 379 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
380 else 380 else
381 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__); 381 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
382 382
383 mutex_unlock(&cs->mutex); 383 mutex_unlock(&cs->mutex);
384 } 384 }
385 385
386 static void if_unthrottle(struct tty_struct *tty) 386 static void if_unthrottle(struct tty_struct *tty)
387 { 387 {
388 struct cardstate *cs = tty->driver_data; 388 struct cardstate *cs = tty->driver_data;
389 389
390 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); 390 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
391 391
392 mutex_lock(&cs->mutex); 392 mutex_lock(&cs->mutex);
393 393
394 if (!cs->connected) 394 if (!cs->connected)
395 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ 395 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
396 else 396 else
397 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__); 397 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
398 398
399 mutex_unlock(&cs->mutex); 399 mutex_unlock(&cs->mutex);
400 } 400 }
401 401
402 static void if_set_termios(struct tty_struct *tty, struct ktermios *old) 402 static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
403 { 403 {
404 struct cardstate *cs = tty->driver_data; 404 struct cardstate *cs = tty->driver_data;
405 unsigned int iflag; 405 unsigned int iflag;
406 unsigned int cflag; 406 unsigned int cflag;
407 unsigned int old_cflag; 407 unsigned int old_cflag;
408 unsigned int control_state, new_state; 408 unsigned int control_state, new_state;
409 409
410 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); 410 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
411 411
412 mutex_lock(&cs->mutex); 412 mutex_lock(&cs->mutex);
413 413
414 if (!cs->connected) { 414 if (!cs->connected) {
415 gig_dbg(DEBUG_IF, "not connected"); 415 gig_dbg(DEBUG_IF, "not connected");
416 goto out; 416 goto out;
417 } 417 }
418 418
419 iflag = tty->termios.c_iflag; 419 iflag = tty->termios.c_iflag;
420 cflag = tty->termios.c_cflag; 420 cflag = tty->termios.c_cflag;
421 old_cflag = old ? old->c_cflag : cflag; 421 old_cflag = old ? old->c_cflag : cflag;
422 gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x", 422 gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
423 cs->minor_index, iflag, cflag, old_cflag); 423 cs->minor_index, iflag, cflag, old_cflag);
424 424
425 /* get a local copy of the current port settings */ 425 /* get a local copy of the current port settings */
426 control_state = cs->control_state; 426 control_state = cs->control_state;
427 427
428 /* 428 /*
429 * Update baud rate. 429 * Update baud rate.
430 * Do not attempt to cache old rates and skip settings, 430 * Do not attempt to cache old rates and skip settings,
431 * disconnects screw such tricks up completely. 431 * disconnects screw such tricks up completely.
432 * Premature optimization is the root of all evil. 432 * Premature optimization is the root of all evil.
433 */ 433 */
434 434
435 /* reassert DTR and (maybe) RTS on transition from B0 */ 435 /* reassert DTR and (maybe) RTS on transition from B0 */
436 if ((old_cflag & CBAUD) == B0) { 436 if ((old_cflag & CBAUD) == B0) {
437 new_state = control_state | TIOCM_DTR; 437 new_state = control_state | TIOCM_DTR;
438 /* don't set RTS if using hardware flow control */ 438 /* don't set RTS if using hardware flow control */
439 if (!(old_cflag & CRTSCTS)) 439 if (!(old_cflag & CRTSCTS))
440 new_state |= TIOCM_RTS; 440 new_state |= TIOCM_RTS;
441 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s", 441 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
442 cs->minor_index, 442 cs->minor_index,
443 (new_state & TIOCM_RTS) ? " only" : "/RTS"); 443 (new_state & TIOCM_RTS) ? " only" : "/RTS");
444 cs->ops->set_modem_ctrl(cs, control_state, new_state); 444 cs->ops->set_modem_ctrl(cs, control_state, new_state);
445 control_state = new_state; 445 control_state = new_state;
446 } 446 }
447 447
448 cs->ops->baud_rate(cs, cflag & CBAUD); 448 cs->ops->baud_rate(cs, cflag & CBAUD);
449 449
450 if ((cflag & CBAUD) == B0) { 450 if ((cflag & CBAUD) == B0) {
451 /* Drop RTS and DTR */ 451 /* Drop RTS and DTR */
452 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index); 452 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
453 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS); 453 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
454 cs->ops->set_modem_ctrl(cs, control_state, new_state); 454 cs->ops->set_modem_ctrl(cs, control_state, new_state);
455 control_state = new_state; 455 control_state = new_state;
456 } 456 }
457 457
458 /* 458 /*
459 * Update line control register (LCR) 459 * Update line control register (LCR)
460 */ 460 */
461 461
462 cs->ops->set_line_ctrl(cs, cflag); 462 cs->ops->set_line_ctrl(cs, cflag);
463 463
464 /* save off the modified port settings */ 464 /* save off the modified port settings */
465 cs->control_state = control_state; 465 cs->control_state = control_state;
466 466
467 out: 467 out:
468 mutex_unlock(&cs->mutex); 468 mutex_unlock(&cs->mutex);
469 } 469 }
470 470
471 static const struct tty_operations if_ops = { 471 static const struct tty_operations if_ops = {
472 .open = if_open, 472 .open = if_open,
473 .close = if_close, 473 .close = if_close,
474 .ioctl = if_ioctl, 474 .ioctl = if_ioctl,
475 .write = if_write, 475 .write = if_write,
476 .write_room = if_write_room, 476 .write_room = if_write_room,
477 .chars_in_buffer = if_chars_in_buffer, 477 .chars_in_buffer = if_chars_in_buffer,
478 .set_termios = if_set_termios, 478 .set_termios = if_set_termios,
479 .throttle = if_throttle, 479 .throttle = if_throttle,
480 .unthrottle = if_unthrottle, 480 .unthrottle = if_unthrottle,
481 .tiocmget = if_tiocmget, 481 .tiocmget = if_tiocmget,
482 .tiocmset = if_tiocmset, 482 .tiocmset = if_tiocmset,
483 }; 483 };
484 484
485 485
486 /* wakeup tasklet for the write operation */ 486 /* wakeup tasklet for the write operation */
487 static void if_wake(unsigned long data) 487 static void if_wake(unsigned long data)
488 { 488 {
489 struct cardstate *cs = (struct cardstate *)data; 489 struct cardstate *cs = (struct cardstate *)data;
490 struct tty_struct *tty = tty_port_tty_get(&cs->port);
491 490
492 if (tty) { 491 tty_port_tty_wakeup(&cs->port);
493 tty_wakeup(tty);
494 tty_kref_put(tty);
495 }
496 } 492 }
497 493
498 /*** interface to common ***/ 494 /*** interface to common ***/
499 495
500 void gigaset_if_init(struct cardstate *cs) 496 void gigaset_if_init(struct cardstate *cs)
501 { 497 {
502 struct gigaset_driver *drv; 498 struct gigaset_driver *drv;
503 499
504 drv = cs->driver; 500 drv = cs->driver;
505 if (!drv->have_tty) 501 if (!drv->have_tty)
506 return; 502 return;
507 503
508 tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs); 504 tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
509 505
510 mutex_lock(&cs->mutex); 506 mutex_lock(&cs->mutex);
511 cs->tty_dev = tty_port_register_device(&cs->port, drv->tty, 507 cs->tty_dev = tty_port_register_device(&cs->port, drv->tty,
512 cs->minor_index, NULL); 508 cs->minor_index, NULL);
513 509
514 if (!IS_ERR(cs->tty_dev)) 510 if (!IS_ERR(cs->tty_dev))
515 dev_set_drvdata(cs->tty_dev, cs); 511 dev_set_drvdata(cs->tty_dev, cs);
516 else { 512 else {
517 pr_warning("could not register device to the tty subsystem\n"); 513 pr_warning("could not register device to the tty subsystem\n");
518 cs->tty_dev = NULL; 514 cs->tty_dev = NULL;
519 } 515 }
520 mutex_unlock(&cs->mutex); 516 mutex_unlock(&cs->mutex);
521 } 517 }
522 518
523 void gigaset_if_free(struct cardstate *cs) 519 void gigaset_if_free(struct cardstate *cs)
524 { 520 {
525 struct gigaset_driver *drv; 521 struct gigaset_driver *drv;
526 522
527 drv = cs->driver; 523 drv = cs->driver;
528 if (!drv->have_tty) 524 if (!drv->have_tty)
529 return; 525 return;
530 526
531 tasklet_disable(&cs->if_wake_tasklet); 527 tasklet_disable(&cs->if_wake_tasklet);
532 tasklet_kill(&cs->if_wake_tasklet); 528 tasklet_kill(&cs->if_wake_tasklet);
533 cs->tty_dev = NULL; 529 cs->tty_dev = NULL;
534 tty_unregister_device(drv->tty, cs->minor_index); 530 tty_unregister_device(drv->tty, cs->minor_index);
535 } 531 }
536 532
537 /** 533 /**
538 * gigaset_if_receive() - pass a received block of data to the tty device 534 * gigaset_if_receive() - pass a received block of data to the tty device
539 * @cs: device descriptor structure. 535 * @cs: device descriptor structure.
540 * @buffer: received data. 536 * @buffer: received data.
541 * @len: number of bytes received. 537 * @len: number of bytes received.
542 * 538 *
543 * Called by asyncdata/isocdata if a block of data received from the 539 * Called by asyncdata/isocdata if a block of data received from the
544 * device must be sent to userspace through the ttyG* device. 540 * device must be sent to userspace through the ttyG* device.
545 */ 541 */
546 void gigaset_if_receive(struct cardstate *cs, 542 void gigaset_if_receive(struct cardstate *cs,
547 unsigned char *buffer, size_t len) 543 unsigned char *buffer, size_t len)
548 { 544 {
549 tty_insert_flip_string(&cs->port, buffer, len); 545 tty_insert_flip_string(&cs->port, buffer, len);
550 tty_flip_buffer_push(&cs->port); 546 tty_flip_buffer_push(&cs->port);
551 } 547 }
552 EXPORT_SYMBOL_GPL(gigaset_if_receive); 548 EXPORT_SYMBOL_GPL(gigaset_if_receive);
553 549
554 /* gigaset_if_initdriver 550 /* gigaset_if_initdriver
555 * Initialize tty interface. 551 * Initialize tty interface.
556 * parameters: 552 * parameters:
557 * drv Driver 553 * drv Driver
558 * procname Name of the driver (e.g. for /proc/tty/drivers) 554 * procname Name of the driver (e.g. for /proc/tty/drivers)
559 * devname Name of the device files (prefix without minor number) 555 * devname Name of the device files (prefix without minor number)
560 */ 556 */
561 void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname, 557 void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
562 const char *devname) 558 const char *devname)
563 { 559 {
564 int ret; 560 int ret;
565 struct tty_driver *tty; 561 struct tty_driver *tty;
566 562
567 drv->have_tty = 0; 563 drv->have_tty = 0;
568 564
569 drv->tty = tty = alloc_tty_driver(drv->minors); 565 drv->tty = tty = alloc_tty_driver(drv->minors);
570 if (tty == NULL) 566 if (tty == NULL)
571 goto enomem; 567 goto enomem;
572 568
573 tty->type = TTY_DRIVER_TYPE_SERIAL; 569 tty->type = TTY_DRIVER_TYPE_SERIAL;
574 tty->subtype = SERIAL_TYPE_NORMAL; 570 tty->subtype = SERIAL_TYPE_NORMAL;
575 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 571 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
576 572
577 tty->driver_name = procname; 573 tty->driver_name = procname;
578 tty->name = devname; 574 tty->name = devname;
579 tty->minor_start = drv->minor; 575 tty->minor_start = drv->minor;
580 576
581 tty->init_termios = tty_std_termios; 577 tty->init_termios = tty_std_termios;
582 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; 578 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
583 tty_set_operations(tty, &if_ops); 579 tty_set_operations(tty, &if_ops);
584 580
585 ret = tty_register_driver(tty); 581 ret = tty_register_driver(tty);
586 if (ret < 0) { 582 if (ret < 0) {
587 pr_err("error %d registering tty driver\n", ret); 583 pr_err("error %d registering tty driver\n", ret);
588 goto error; 584 goto error;
589 } 585 }
590 gig_dbg(DEBUG_IF, "tty driver initialized"); 586 gig_dbg(DEBUG_IF, "tty driver initialized");
591 drv->have_tty = 1; 587 drv->have_tty = 1;
592 return; 588 return;
593 589
594 enomem: 590 enomem:
595 pr_err("out of memory\n"); 591 pr_err("out of memory\n");
596 error: 592 error:
597 if (drv->tty) 593 if (drv->tty)
598 put_tty_driver(drv->tty); 594 put_tty_driver(drv->tty);
599 } 595 }
600 596
601 void gigaset_if_freedriver(struct gigaset_driver *drv) 597 void gigaset_if_freedriver(struct gigaset_driver *drv)
602 { 598 {
603 if (!drv->have_tty) 599 if (!drv->have_tty)
604 return; 600 return;
605 601
606 drv->have_tty = 0; 602 drv->have_tty = 0;
607 tty_unregister_driver(drv->tty); 603 tty_unregister_driver(drv->tty);
608 put_tty_driver(drv->tty); 604 put_tty_driver(drv->tty);
609 } 605 }
610 606
drivers/net/usb/hso.c
1 /****************************************************************************** 1 /******************************************************************************
2 * 2 *
3 * Driver for Option High Speed Mobile Devices. 3 * Driver for Option High Speed Mobile Devices.
4 * 4 *
5 * Copyright (C) 2008 Option International 5 * Copyright (C) 2008 Option International
6 * Filip Aben <f.aben@option.com> 6 * Filip Aben <f.aben@option.com>
7 * Denis Joseph Barrow <d.barow@option.com> 7 * Denis Joseph Barrow <d.barow@option.com>
8 * Jan Dumon <j.dumon@option.com> 8 * Jan Dumon <j.dumon@option.com>
9 * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd) 9 * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
10 * <ajb@spheresystems.co.uk> 10 * <ajb@spheresystems.co.uk>
11 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de> 11 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
12 * Copyright (C) 2008 Novell, Inc. 12 * Copyright (C) 2008 Novell, Inc.
13 * 13 *
14 * This program is free software; you can redistribute it and/or modify 14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as 15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation. 16 * published by the Free Software Foundation.
17 * 17 *
18 * This program is distributed in the hope that it will be useful, 18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details. 21 * GNU General Public License for more details.
22 * 22 *
23 * You should have received a copy of the GNU General Public License 23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software 24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 * USA 26 * USA
27 * 27 *
28 * 28 *
29 *****************************************************************************/ 29 *****************************************************************************/
30 30
31 /****************************************************************************** 31 /******************************************************************************
32 * 32 *
33 * Description of the device: 33 * Description of the device:
34 * 34 *
35 * Interface 0: Contains the IP network interface on the bulk end points. 35 * Interface 0: Contains the IP network interface on the bulk end points.
36 * The multiplexed serial ports are using the interrupt and 36 * The multiplexed serial ports are using the interrupt and
37 * control endpoints. 37 * control endpoints.
38 * Interrupt contains a bitmap telling which multiplexed 38 * Interrupt contains a bitmap telling which multiplexed
39 * serialport needs servicing. 39 * serialport needs servicing.
40 * 40 *
41 * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the 41 * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
42 * port is opened, as this have a huge impact on the network port 42 * port is opened, as this have a huge impact on the network port
43 * throughput. 43 * throughput.
44 * 44 *
45 * Interface 2: Standard modem interface - circuit switched interface, this 45 * Interface 2: Standard modem interface - circuit switched interface, this
46 * can be used to make a standard ppp connection however it 46 * can be used to make a standard ppp connection however it
47 * should not be used in conjunction with the IP network interface 47 * should not be used in conjunction with the IP network interface
48 * enabled for USB performance reasons i.e. if using this set 48 * enabled for USB performance reasons i.e. if using this set
49 * ideally disable_net=1. 49 * ideally disable_net=1.
50 * 50 *
51 *****************************************************************************/ 51 *****************************************************************************/
52 52
53 #include <linux/sched.h> 53 #include <linux/sched.h>
54 #include <linux/slab.h> 54 #include <linux/slab.h>
55 #include <linux/init.h> 55 #include <linux/init.h>
56 #include <linux/delay.h> 56 #include <linux/delay.h>
57 #include <linux/netdevice.h> 57 #include <linux/netdevice.h>
58 #include <linux/module.h> 58 #include <linux/module.h>
59 #include <linux/ethtool.h> 59 #include <linux/ethtool.h>
60 #include <linux/usb.h> 60 #include <linux/usb.h>
61 #include <linux/timer.h> 61 #include <linux/timer.h>
62 #include <linux/tty.h> 62 #include <linux/tty.h>
63 #include <linux/tty_driver.h> 63 #include <linux/tty_driver.h>
64 #include <linux/tty_flip.h> 64 #include <linux/tty_flip.h>
65 #include <linux/kmod.h> 65 #include <linux/kmod.h>
66 #include <linux/rfkill.h> 66 #include <linux/rfkill.h>
67 #include <linux/ip.h> 67 #include <linux/ip.h>
68 #include <linux/uaccess.h> 68 #include <linux/uaccess.h>
69 #include <linux/usb/cdc.h> 69 #include <linux/usb/cdc.h>
70 #include <net/arp.h> 70 #include <net/arp.h>
71 #include <asm/byteorder.h> 71 #include <asm/byteorder.h>
72 #include <linux/serial_core.h> 72 #include <linux/serial_core.h>
73 #include <linux/serial.h> 73 #include <linux/serial.h>
74 74
75 75
76 #define MOD_AUTHOR "Option Wireless" 76 #define MOD_AUTHOR "Option Wireless"
77 #define MOD_DESCRIPTION "USB High Speed Option driver" 77 #define MOD_DESCRIPTION "USB High Speed Option driver"
78 #define MOD_LICENSE "GPL" 78 #define MOD_LICENSE "GPL"
79 79
80 #define HSO_MAX_NET_DEVICES 10 80 #define HSO_MAX_NET_DEVICES 10
81 #define HSO__MAX_MTU 2048 81 #define HSO__MAX_MTU 2048
82 #define DEFAULT_MTU 1500 82 #define DEFAULT_MTU 1500
83 #define DEFAULT_MRU 1500 83 #define DEFAULT_MRU 1500
84 84
85 #define CTRL_URB_RX_SIZE 1024 85 #define CTRL_URB_RX_SIZE 1024
86 #define CTRL_URB_TX_SIZE 64 86 #define CTRL_URB_TX_SIZE 64
87 87
88 #define BULK_URB_RX_SIZE 4096 88 #define BULK_URB_RX_SIZE 4096
89 #define BULK_URB_TX_SIZE 8192 89 #define BULK_URB_TX_SIZE 8192
90 90
91 #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU 91 #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
92 #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU 92 #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
93 #define MUX_BULK_RX_BUF_COUNT 4 93 #define MUX_BULK_RX_BUF_COUNT 4
94 #define USB_TYPE_OPTION_VENDOR 0x20 94 #define USB_TYPE_OPTION_VENDOR 0x20
95 95
96 /* These definitions are used with the struct hso_net flags element */ 96 /* These definitions are used with the struct hso_net flags element */
97 /* - use *_bit operations on it. (bit indices not values.) */ 97 /* - use *_bit operations on it. (bit indices not values.) */
98 #define HSO_NET_RUNNING 0 98 #define HSO_NET_RUNNING 0
99 99
100 #define HSO_NET_TX_TIMEOUT (HZ*10) 100 #define HSO_NET_TX_TIMEOUT (HZ*10)
101 101
102 #define HSO_SERIAL_MAGIC 0x48534f31 102 #define HSO_SERIAL_MAGIC 0x48534f31
103 103
104 /* Number of ttys to handle */ 104 /* Number of ttys to handle */
105 #define HSO_SERIAL_TTY_MINORS 256 105 #define HSO_SERIAL_TTY_MINORS 256
106 106
107 #define MAX_RX_URBS 2 107 #define MAX_RX_URBS 2
108 108
109 /*****************************************************************************/ 109 /*****************************************************************************/
110 /* Debugging functions */ 110 /* Debugging functions */
111 /*****************************************************************************/ 111 /*****************************************************************************/
112 #define D__(lvl_, fmt, arg...) \ 112 #define D__(lvl_, fmt, arg...) \
113 do { \ 113 do { \
114 printk(lvl_ "[%d:%s]: " fmt "\n", \ 114 printk(lvl_ "[%d:%s]: " fmt "\n", \
115 __LINE__, __func__, ## arg); \ 115 __LINE__, __func__, ## arg); \
116 } while (0) 116 } while (0)
117 117
118 #define D_(lvl, args...) \ 118 #define D_(lvl, args...) \
119 do { \ 119 do { \
120 if (lvl & debug) \ 120 if (lvl & debug) \
121 D__(KERN_INFO, args); \ 121 D__(KERN_INFO, args); \
122 } while (0) 122 } while (0)
123 123
124 #define D1(args...) D_(0x01, ##args) 124 #define D1(args...) D_(0x01, ##args)
125 #define D2(args...) D_(0x02, ##args) 125 #define D2(args...) D_(0x02, ##args)
126 #define D3(args...) D_(0x04, ##args) 126 #define D3(args...) D_(0x04, ##args)
127 #define D4(args...) D_(0x08, ##args) 127 #define D4(args...) D_(0x08, ##args)
128 #define D5(args...) D_(0x10, ##args) 128 #define D5(args...) D_(0x10, ##args)
129 129
130 /*****************************************************************************/ 130 /*****************************************************************************/
131 /* Enumerators */ 131 /* Enumerators */
132 /*****************************************************************************/ 132 /*****************************************************************************/
133 enum pkt_parse_state { 133 enum pkt_parse_state {
134 WAIT_IP, 134 WAIT_IP,
135 WAIT_DATA, 135 WAIT_DATA,
136 WAIT_SYNC 136 WAIT_SYNC
137 }; 137 };
138 138
139 /*****************************************************************************/ 139 /*****************************************************************************/
140 /* Structs */ 140 /* Structs */
141 /*****************************************************************************/ 141 /*****************************************************************************/
142 142
143 struct hso_shared_int { 143 struct hso_shared_int {
144 struct usb_endpoint_descriptor *intr_endp; 144 struct usb_endpoint_descriptor *intr_endp;
145 void *shared_intr_buf; 145 void *shared_intr_buf;
146 struct urb *shared_intr_urb; 146 struct urb *shared_intr_urb;
147 struct usb_device *usb; 147 struct usb_device *usb;
148 int use_count; 148 int use_count;
149 int ref_count; 149 int ref_count;
150 struct mutex shared_int_lock; 150 struct mutex shared_int_lock;
151 }; 151 };
152 152
153 struct hso_net { 153 struct hso_net {
154 struct hso_device *parent; 154 struct hso_device *parent;
155 struct net_device *net; 155 struct net_device *net;
156 struct rfkill *rfkill; 156 struct rfkill *rfkill;
157 157
158 struct usb_endpoint_descriptor *in_endp; 158 struct usb_endpoint_descriptor *in_endp;
159 struct usb_endpoint_descriptor *out_endp; 159 struct usb_endpoint_descriptor *out_endp;
160 160
161 struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT]; 161 struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
162 struct urb *mux_bulk_tx_urb; 162 struct urb *mux_bulk_tx_urb;
163 void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT]; 163 void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
164 void *mux_bulk_tx_buf; 164 void *mux_bulk_tx_buf;
165 165
166 struct sk_buff *skb_rx_buf; 166 struct sk_buff *skb_rx_buf;
167 struct sk_buff *skb_tx_buf; 167 struct sk_buff *skb_tx_buf;
168 168
169 enum pkt_parse_state rx_parse_state; 169 enum pkt_parse_state rx_parse_state;
170 spinlock_t net_lock; 170 spinlock_t net_lock;
171 171
172 unsigned short rx_buf_size; 172 unsigned short rx_buf_size;
173 unsigned short rx_buf_missing; 173 unsigned short rx_buf_missing;
174 struct iphdr rx_ip_hdr; 174 struct iphdr rx_ip_hdr;
175 175
176 unsigned long flags; 176 unsigned long flags;
177 }; 177 };
178 178
179 enum rx_ctrl_state{ 179 enum rx_ctrl_state{
180 RX_IDLE, 180 RX_IDLE,
181 RX_SENT, 181 RX_SENT,
182 RX_PENDING 182 RX_PENDING
183 }; 183 };
184 184
185 #define BM_REQUEST_TYPE (0xa1) 185 #define BM_REQUEST_TYPE (0xa1)
186 #define B_NOTIFICATION (0x20) 186 #define B_NOTIFICATION (0x20)
187 #define W_VALUE (0x0) 187 #define W_VALUE (0x0)
188 #define W_INDEX (0x2) 188 #define W_INDEX (0x2)
189 #define W_LENGTH (0x2) 189 #define W_LENGTH (0x2)
190 190
191 #define B_OVERRUN (0x1<<6) 191 #define B_OVERRUN (0x1<<6)
192 #define B_PARITY (0x1<<5) 192 #define B_PARITY (0x1<<5)
193 #define B_FRAMING (0x1<<4) 193 #define B_FRAMING (0x1<<4)
194 #define B_RING_SIGNAL (0x1<<3) 194 #define B_RING_SIGNAL (0x1<<3)
195 #define B_BREAK (0x1<<2) 195 #define B_BREAK (0x1<<2)
196 #define B_TX_CARRIER (0x1<<1) 196 #define B_TX_CARRIER (0x1<<1)
197 #define B_RX_CARRIER (0x1<<0) 197 #define B_RX_CARRIER (0x1<<0)
198 198
199 struct hso_serial_state_notification { 199 struct hso_serial_state_notification {
200 u8 bmRequestType; 200 u8 bmRequestType;
201 u8 bNotification; 201 u8 bNotification;
202 u16 wValue; 202 u16 wValue;
203 u16 wIndex; 203 u16 wIndex;
204 u16 wLength; 204 u16 wLength;
205 u16 UART_state_bitmap; 205 u16 UART_state_bitmap;
206 } __packed; 206 } __packed;
207 207
208 struct hso_tiocmget { 208 struct hso_tiocmget {
209 struct mutex mutex; 209 struct mutex mutex;
210 wait_queue_head_t waitq; 210 wait_queue_head_t waitq;
211 int intr_completed; 211 int intr_completed;
212 struct usb_endpoint_descriptor *endp; 212 struct usb_endpoint_descriptor *endp;
213 struct urb *urb; 213 struct urb *urb;
214 struct hso_serial_state_notification serial_state_notification; 214 struct hso_serial_state_notification serial_state_notification;
215 u16 prev_UART_state_bitmap; 215 u16 prev_UART_state_bitmap;
216 struct uart_icount icount; 216 struct uart_icount icount;
217 }; 217 };
218 218
219 219
220 struct hso_serial { 220 struct hso_serial {
221 struct hso_device *parent; 221 struct hso_device *parent;
222 int magic; 222 int magic;
223 u8 minor; 223 u8 minor;
224 224
225 struct hso_shared_int *shared_int; 225 struct hso_shared_int *shared_int;
226 226
227 /* rx/tx urb could be either a bulk urb or a control urb depending 227 /* rx/tx urb could be either a bulk urb or a control urb depending
228 on which serial port it is used on. */ 228 on which serial port it is used on. */
229 struct urb *rx_urb[MAX_RX_URBS]; 229 struct urb *rx_urb[MAX_RX_URBS];
230 u8 num_rx_urbs; 230 u8 num_rx_urbs;
231 u8 *rx_data[MAX_RX_URBS]; 231 u8 *rx_data[MAX_RX_URBS];
232 u16 rx_data_length; /* should contain allocated length */ 232 u16 rx_data_length; /* should contain allocated length */
233 233
234 struct urb *tx_urb; 234 struct urb *tx_urb;
235 u8 *tx_data; 235 u8 *tx_data;
236 u8 *tx_buffer; 236 u8 *tx_buffer;
237 u16 tx_data_length; /* should contain allocated length */ 237 u16 tx_data_length; /* should contain allocated length */
238 u16 tx_data_count; 238 u16 tx_data_count;
239 u16 tx_buffer_count; 239 u16 tx_buffer_count;
240 struct usb_ctrlrequest ctrl_req_tx; 240 struct usb_ctrlrequest ctrl_req_tx;
241 struct usb_ctrlrequest ctrl_req_rx; 241 struct usb_ctrlrequest ctrl_req_rx;
242 242
243 struct usb_endpoint_descriptor *in_endp; 243 struct usb_endpoint_descriptor *in_endp;
244 struct usb_endpoint_descriptor *out_endp; 244 struct usb_endpoint_descriptor *out_endp;
245 245
246 enum rx_ctrl_state rx_state; 246 enum rx_ctrl_state rx_state;
247 u8 rts_state; 247 u8 rts_state;
248 u8 dtr_state; 248 u8 dtr_state;
249 unsigned tx_urb_used:1; 249 unsigned tx_urb_used:1;
250 250
251 struct tty_port port; 251 struct tty_port port;
252 /* from usb_serial_port */ 252 /* from usb_serial_port */
253 spinlock_t serial_lock; 253 spinlock_t serial_lock;
254 254
255 int (*write_data) (struct hso_serial *serial); 255 int (*write_data) (struct hso_serial *serial);
256 struct hso_tiocmget *tiocmget; 256 struct hso_tiocmget *tiocmget;
257 /* Hacks required to get flow control 257 /* Hacks required to get flow control
258 * working on the serial receive buffers 258 * working on the serial receive buffers
259 * so as not to drop characters on the floor. 259 * so as not to drop characters on the floor.
260 */ 260 */
261 int curr_rx_urb_idx; 261 int curr_rx_urb_idx;
262 u16 curr_rx_urb_offset; 262 u16 curr_rx_urb_offset;
263 u8 rx_urb_filled[MAX_RX_URBS]; 263 u8 rx_urb_filled[MAX_RX_URBS];
264 struct tasklet_struct unthrottle_tasklet; 264 struct tasklet_struct unthrottle_tasklet;
265 struct work_struct retry_unthrottle_workqueue; 265 struct work_struct retry_unthrottle_workqueue;
266 }; 266 };
267 267
268 struct hso_device { 268 struct hso_device {
269 union { 269 union {
270 struct hso_serial *dev_serial; 270 struct hso_serial *dev_serial;
271 struct hso_net *dev_net; 271 struct hso_net *dev_net;
272 } port_data; 272 } port_data;
273 273
274 u32 port_spec; 274 u32 port_spec;
275 275
276 u8 is_active; 276 u8 is_active;
277 u8 usb_gone; 277 u8 usb_gone;
278 struct work_struct async_get_intf; 278 struct work_struct async_get_intf;
279 struct work_struct async_put_intf; 279 struct work_struct async_put_intf;
280 struct work_struct reset_device; 280 struct work_struct reset_device;
281 281
282 struct usb_device *usb; 282 struct usb_device *usb;
283 struct usb_interface *interface; 283 struct usb_interface *interface;
284 284
285 struct device *dev; 285 struct device *dev;
286 struct kref ref; 286 struct kref ref;
287 struct mutex mutex; 287 struct mutex mutex;
288 }; 288 };
289 289
290 /* Type of interface */ 290 /* Type of interface */
291 #define HSO_INTF_MASK 0xFF00 291 #define HSO_INTF_MASK 0xFF00
292 #define HSO_INTF_MUX 0x0100 292 #define HSO_INTF_MUX 0x0100
293 #define HSO_INTF_BULK 0x0200 293 #define HSO_INTF_BULK 0x0200
294 294
295 /* Type of port */ 295 /* Type of port */
296 #define HSO_PORT_MASK 0xFF 296 #define HSO_PORT_MASK 0xFF
297 #define HSO_PORT_NO_PORT 0x0 297 #define HSO_PORT_NO_PORT 0x0
298 #define HSO_PORT_CONTROL 0x1 298 #define HSO_PORT_CONTROL 0x1
299 #define HSO_PORT_APP 0x2 299 #define HSO_PORT_APP 0x2
300 #define HSO_PORT_GPS 0x3 300 #define HSO_PORT_GPS 0x3
301 #define HSO_PORT_PCSC 0x4 301 #define HSO_PORT_PCSC 0x4
302 #define HSO_PORT_APP2 0x5 302 #define HSO_PORT_APP2 0x5
303 #define HSO_PORT_GPS_CONTROL 0x6 303 #define HSO_PORT_GPS_CONTROL 0x6
304 #define HSO_PORT_MSD 0x7 304 #define HSO_PORT_MSD 0x7
305 #define HSO_PORT_VOICE 0x8 305 #define HSO_PORT_VOICE 0x8
306 #define HSO_PORT_DIAG2 0x9 306 #define HSO_PORT_DIAG2 0x9
307 #define HSO_PORT_DIAG 0x10 307 #define HSO_PORT_DIAG 0x10
308 #define HSO_PORT_MODEM 0x11 308 #define HSO_PORT_MODEM 0x11
309 #define HSO_PORT_NETWORK 0x12 309 #define HSO_PORT_NETWORK 0x12
310 310
311 /* Additional device info */ 311 /* Additional device info */
312 #define HSO_INFO_MASK 0xFF000000 312 #define HSO_INFO_MASK 0xFF000000
313 #define HSO_INFO_CRC_BUG 0x01000000 313 #define HSO_INFO_CRC_BUG 0x01000000
314 314
315 /*****************************************************************************/ 315 /*****************************************************************************/
316 /* Prototypes */ 316 /* Prototypes */
317 /*****************************************************************************/ 317 /*****************************************************************************/
318 /* Serial driver functions */ 318 /* Serial driver functions */
319 static int hso_serial_tiocmset(struct tty_struct *tty, 319 static int hso_serial_tiocmset(struct tty_struct *tty,
320 unsigned int set, unsigned int clear); 320 unsigned int set, unsigned int clear);
321 static void ctrl_callback(struct urb *urb); 321 static void ctrl_callback(struct urb *urb);
322 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial); 322 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
323 static void hso_kick_transmit(struct hso_serial *serial); 323 static void hso_kick_transmit(struct hso_serial *serial);
324 /* Helper functions */ 324 /* Helper functions */
325 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int, 325 static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
326 struct usb_device *usb, gfp_t gfp); 326 struct usb_device *usb, gfp_t gfp);
327 static void handle_usb_error(int status, const char *function, 327 static void handle_usb_error(int status, const char *function,
328 struct hso_device *hso_dev); 328 struct hso_device *hso_dev);
329 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf, 329 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
330 int type, int dir); 330 int type, int dir);
331 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports); 331 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
332 static void hso_free_interface(struct usb_interface *intf); 332 static void hso_free_interface(struct usb_interface *intf);
333 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags); 333 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
334 static int hso_stop_serial_device(struct hso_device *hso_dev); 334 static int hso_stop_serial_device(struct hso_device *hso_dev);
335 static int hso_start_net_device(struct hso_device *hso_dev); 335 static int hso_start_net_device(struct hso_device *hso_dev);
336 static void hso_free_shared_int(struct hso_shared_int *shared_int); 336 static void hso_free_shared_int(struct hso_shared_int *shared_int);
337 static int hso_stop_net_device(struct hso_device *hso_dev); 337 static int hso_stop_net_device(struct hso_device *hso_dev);
338 static void hso_serial_ref_free(struct kref *ref); 338 static void hso_serial_ref_free(struct kref *ref);
339 static void hso_std_serial_read_bulk_callback(struct urb *urb); 339 static void hso_std_serial_read_bulk_callback(struct urb *urb);
340 static int hso_mux_serial_read(struct hso_serial *serial); 340 static int hso_mux_serial_read(struct hso_serial *serial);
341 static void async_get_intf(struct work_struct *data); 341 static void async_get_intf(struct work_struct *data);
342 static void async_put_intf(struct work_struct *data); 342 static void async_put_intf(struct work_struct *data);
343 static int hso_put_activity(struct hso_device *hso_dev); 343 static int hso_put_activity(struct hso_device *hso_dev);
344 static int hso_get_activity(struct hso_device *hso_dev); 344 static int hso_get_activity(struct hso_device *hso_dev);
345 static void tiocmget_intr_callback(struct urb *urb); 345 static void tiocmget_intr_callback(struct urb *urb);
346 static void reset_device(struct work_struct *data); 346 static void reset_device(struct work_struct *data);
347 /*****************************************************************************/ 347 /*****************************************************************************/
348 /* Helping functions */ 348 /* Helping functions */
349 /*****************************************************************************/ 349 /*****************************************************************************/
350 350
351 /* #define DEBUG */ 351 /* #define DEBUG */
352 352
353 static inline struct hso_net *dev2net(struct hso_device *hso_dev) 353 static inline struct hso_net *dev2net(struct hso_device *hso_dev)
354 { 354 {
355 return hso_dev->port_data.dev_net; 355 return hso_dev->port_data.dev_net;
356 } 356 }
357 357
358 static inline struct hso_serial *dev2ser(struct hso_device *hso_dev) 358 static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
359 { 359 {
360 return hso_dev->port_data.dev_serial; 360 return hso_dev->port_data.dev_serial;
361 } 361 }
362 362
363 /* Debugging functions */ 363 /* Debugging functions */
364 #ifdef DEBUG 364 #ifdef DEBUG
365 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf, 365 static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
366 unsigned int len) 366 unsigned int len)
367 { 367 {
368 static char name[255]; 368 static char name[255];
369 369
370 sprintf(name, "hso[%d:%s]", line_count, func_name); 370 sprintf(name, "hso[%d:%s]", line_count, func_name);
371 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len); 371 print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
372 } 372 }
373 373
374 #define DUMP(buf_, len_) \ 374 #define DUMP(buf_, len_) \
375 dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_) 375 dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
376 376
377 #define DUMP1(buf_, len_) \ 377 #define DUMP1(buf_, len_) \
378 do { \ 378 do { \
379 if (0x01 & debug) \ 379 if (0x01 & debug) \
380 DUMP(buf_, len_); \ 380 DUMP(buf_, len_); \
381 } while (0) 381 } while (0)
382 #else 382 #else
383 #define DUMP(buf_, len_) 383 #define DUMP(buf_, len_)
384 #define DUMP1(buf_, len_) 384 #define DUMP1(buf_, len_)
385 #endif 385 #endif
386 386
387 /* module parameters */ 387 /* module parameters */
388 static int debug; 388 static int debug;
389 static int tty_major; 389 static int tty_major;
390 static int disable_net; 390 static int disable_net;
391 391
392 /* driver info */ 392 /* driver info */
393 static const char driver_name[] = "hso"; 393 static const char driver_name[] = "hso";
394 static const char tty_filename[] = "ttyHS"; 394 static const char tty_filename[] = "ttyHS";
395 static const char *version = __FILE__ ": " MOD_AUTHOR; 395 static const char *version = __FILE__ ": " MOD_AUTHOR;
396 /* the usb driver itself (registered in hso_init) */ 396 /* the usb driver itself (registered in hso_init) */
397 static struct usb_driver hso_driver; 397 static struct usb_driver hso_driver;
398 /* serial structures */ 398 /* serial structures */
399 static struct tty_driver *tty_drv; 399 static struct tty_driver *tty_drv;
400 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS]; 400 static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
401 static struct hso_device *network_table[HSO_MAX_NET_DEVICES]; 401 static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
402 static spinlock_t serial_table_lock; 402 static spinlock_t serial_table_lock;
403 403
404 static const s32 default_port_spec[] = { 404 static const s32 default_port_spec[] = {
405 HSO_INTF_MUX | HSO_PORT_NETWORK, 405 HSO_INTF_MUX | HSO_PORT_NETWORK,
406 HSO_INTF_BULK | HSO_PORT_DIAG, 406 HSO_INTF_BULK | HSO_PORT_DIAG,
407 HSO_INTF_BULK | HSO_PORT_MODEM, 407 HSO_INTF_BULK | HSO_PORT_MODEM,
408 0 408 0
409 }; 409 };
410 410
411 static const s32 icon321_port_spec[] = { 411 static const s32 icon321_port_spec[] = {
412 HSO_INTF_MUX | HSO_PORT_NETWORK, 412 HSO_INTF_MUX | HSO_PORT_NETWORK,
413 HSO_INTF_BULK | HSO_PORT_DIAG2, 413 HSO_INTF_BULK | HSO_PORT_DIAG2,
414 HSO_INTF_BULK | HSO_PORT_MODEM, 414 HSO_INTF_BULK | HSO_PORT_MODEM,
415 HSO_INTF_BULK | HSO_PORT_DIAG, 415 HSO_INTF_BULK | HSO_PORT_DIAG,
416 0 416 0
417 }; 417 };
418 418
419 #define default_port_device(vendor, product) \ 419 #define default_port_device(vendor, product) \
420 USB_DEVICE(vendor, product), \ 420 USB_DEVICE(vendor, product), \
421 .driver_info = (kernel_ulong_t)default_port_spec 421 .driver_info = (kernel_ulong_t)default_port_spec
422 422
423 #define icon321_port_device(vendor, product) \ 423 #define icon321_port_device(vendor, product) \
424 USB_DEVICE(vendor, product), \ 424 USB_DEVICE(vendor, product), \
425 .driver_info = (kernel_ulong_t)icon321_port_spec 425 .driver_info = (kernel_ulong_t)icon321_port_spec
426 426
427 /* list of devices we support */ 427 /* list of devices we support */
428 static const struct usb_device_id hso_ids[] = { 428 static const struct usb_device_id hso_ids[] = {
429 {default_port_device(0x0af0, 0x6711)}, 429 {default_port_device(0x0af0, 0x6711)},
430 {default_port_device(0x0af0, 0x6731)}, 430 {default_port_device(0x0af0, 0x6731)},
431 {default_port_device(0x0af0, 0x6751)}, 431 {default_port_device(0x0af0, 0x6751)},
432 {default_port_device(0x0af0, 0x6771)}, 432 {default_port_device(0x0af0, 0x6771)},
433 {default_port_device(0x0af0, 0x6791)}, 433 {default_port_device(0x0af0, 0x6791)},
434 {default_port_device(0x0af0, 0x6811)}, 434 {default_port_device(0x0af0, 0x6811)},
435 {default_port_device(0x0af0, 0x6911)}, 435 {default_port_device(0x0af0, 0x6911)},
436 {default_port_device(0x0af0, 0x6951)}, 436 {default_port_device(0x0af0, 0x6951)},
437 {default_port_device(0x0af0, 0x6971)}, 437 {default_port_device(0x0af0, 0x6971)},
438 {default_port_device(0x0af0, 0x7011)}, 438 {default_port_device(0x0af0, 0x7011)},
439 {default_port_device(0x0af0, 0x7031)}, 439 {default_port_device(0x0af0, 0x7031)},
440 {default_port_device(0x0af0, 0x7051)}, 440 {default_port_device(0x0af0, 0x7051)},
441 {default_port_device(0x0af0, 0x7071)}, 441 {default_port_device(0x0af0, 0x7071)},
442 {default_port_device(0x0af0, 0x7111)}, 442 {default_port_device(0x0af0, 0x7111)},
443 {default_port_device(0x0af0, 0x7211)}, 443 {default_port_device(0x0af0, 0x7211)},
444 {default_port_device(0x0af0, 0x7251)}, 444 {default_port_device(0x0af0, 0x7251)},
445 {default_port_device(0x0af0, 0x7271)}, 445 {default_port_device(0x0af0, 0x7271)},
446 {default_port_device(0x0af0, 0x7311)}, 446 {default_port_device(0x0af0, 0x7311)},
447 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */ 447 {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
448 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */ 448 {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
449 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */ 449 {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
450 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */ 450 {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
451 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */ 451 {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
452 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */ 452 {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
453 {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */ 453 {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
454 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */ 454 {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
455 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */ 455 {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
456 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */ 456 {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
457 {USB_DEVICE(0x0af0, 0x7701)}, 457 {USB_DEVICE(0x0af0, 0x7701)},
458 {USB_DEVICE(0x0af0, 0x7706)}, 458 {USB_DEVICE(0x0af0, 0x7706)},
459 {USB_DEVICE(0x0af0, 0x7801)}, 459 {USB_DEVICE(0x0af0, 0x7801)},
460 {USB_DEVICE(0x0af0, 0x7901)}, 460 {USB_DEVICE(0x0af0, 0x7901)},
461 {USB_DEVICE(0x0af0, 0x7A01)}, 461 {USB_DEVICE(0x0af0, 0x7A01)},
462 {USB_DEVICE(0x0af0, 0x7A05)}, 462 {USB_DEVICE(0x0af0, 0x7A05)},
463 {USB_DEVICE(0x0af0, 0x8200)}, 463 {USB_DEVICE(0x0af0, 0x8200)},
464 {USB_DEVICE(0x0af0, 0x8201)}, 464 {USB_DEVICE(0x0af0, 0x8201)},
465 {USB_DEVICE(0x0af0, 0x8300)}, 465 {USB_DEVICE(0x0af0, 0x8300)},
466 {USB_DEVICE(0x0af0, 0x8302)}, 466 {USB_DEVICE(0x0af0, 0x8302)},
467 {USB_DEVICE(0x0af0, 0x8304)}, 467 {USB_DEVICE(0x0af0, 0x8304)},
468 {USB_DEVICE(0x0af0, 0x8400)}, 468 {USB_DEVICE(0x0af0, 0x8400)},
469 {USB_DEVICE(0x0af0, 0x8600)}, 469 {USB_DEVICE(0x0af0, 0x8600)},
470 {USB_DEVICE(0x0af0, 0x8800)}, 470 {USB_DEVICE(0x0af0, 0x8800)},
471 {USB_DEVICE(0x0af0, 0x8900)}, 471 {USB_DEVICE(0x0af0, 0x8900)},
472 {USB_DEVICE(0x0af0, 0x9000)}, 472 {USB_DEVICE(0x0af0, 0x9000)},
473 {USB_DEVICE(0x0af0, 0xd035)}, 473 {USB_DEVICE(0x0af0, 0xd035)},
474 {USB_DEVICE(0x0af0, 0xd055)}, 474 {USB_DEVICE(0x0af0, 0xd055)},
475 {USB_DEVICE(0x0af0, 0xd155)}, 475 {USB_DEVICE(0x0af0, 0xd155)},
476 {USB_DEVICE(0x0af0, 0xd255)}, 476 {USB_DEVICE(0x0af0, 0xd255)},
477 {USB_DEVICE(0x0af0, 0xd057)}, 477 {USB_DEVICE(0x0af0, 0xd057)},
478 {USB_DEVICE(0x0af0, 0xd157)}, 478 {USB_DEVICE(0x0af0, 0xd157)},
479 {USB_DEVICE(0x0af0, 0xd257)}, 479 {USB_DEVICE(0x0af0, 0xd257)},
480 {USB_DEVICE(0x0af0, 0xd357)}, 480 {USB_DEVICE(0x0af0, 0xd357)},
481 {USB_DEVICE(0x0af0, 0xd058)}, 481 {USB_DEVICE(0x0af0, 0xd058)},
482 {USB_DEVICE(0x0af0, 0xc100)}, 482 {USB_DEVICE(0x0af0, 0xc100)},
483 {} 483 {}
484 }; 484 };
485 MODULE_DEVICE_TABLE(usb, hso_ids); 485 MODULE_DEVICE_TABLE(usb, hso_ids);
486 486
487 /* Sysfs attribute */ 487 /* Sysfs attribute */
488 static ssize_t hso_sysfs_show_porttype(struct device *dev, 488 static ssize_t hso_sysfs_show_porttype(struct device *dev,
489 struct device_attribute *attr, 489 struct device_attribute *attr,
490 char *buf) 490 char *buf)
491 { 491 {
492 struct hso_device *hso_dev = dev_get_drvdata(dev); 492 struct hso_device *hso_dev = dev_get_drvdata(dev);
493 char *port_name; 493 char *port_name;
494 494
495 if (!hso_dev) 495 if (!hso_dev)
496 return 0; 496 return 0;
497 497
498 switch (hso_dev->port_spec & HSO_PORT_MASK) { 498 switch (hso_dev->port_spec & HSO_PORT_MASK) {
499 case HSO_PORT_CONTROL: 499 case HSO_PORT_CONTROL:
500 port_name = "Control"; 500 port_name = "Control";
501 break; 501 break;
502 case HSO_PORT_APP: 502 case HSO_PORT_APP:
503 port_name = "Application"; 503 port_name = "Application";
504 break; 504 break;
505 case HSO_PORT_APP2: 505 case HSO_PORT_APP2:
506 port_name = "Application2"; 506 port_name = "Application2";
507 break; 507 break;
508 case HSO_PORT_GPS: 508 case HSO_PORT_GPS:
509 port_name = "GPS"; 509 port_name = "GPS";
510 break; 510 break;
511 case HSO_PORT_GPS_CONTROL: 511 case HSO_PORT_GPS_CONTROL:
512 port_name = "GPS Control"; 512 port_name = "GPS Control";
513 break; 513 break;
514 case HSO_PORT_PCSC: 514 case HSO_PORT_PCSC:
515 port_name = "PCSC"; 515 port_name = "PCSC";
516 break; 516 break;
517 case HSO_PORT_DIAG: 517 case HSO_PORT_DIAG:
518 port_name = "Diagnostic"; 518 port_name = "Diagnostic";
519 break; 519 break;
520 case HSO_PORT_DIAG2: 520 case HSO_PORT_DIAG2:
521 port_name = "Diagnostic2"; 521 port_name = "Diagnostic2";
522 break; 522 break;
523 case HSO_PORT_MODEM: 523 case HSO_PORT_MODEM:
524 port_name = "Modem"; 524 port_name = "Modem";
525 break; 525 break;
526 case HSO_PORT_NETWORK: 526 case HSO_PORT_NETWORK:
527 port_name = "Network"; 527 port_name = "Network";
528 break; 528 break;
529 default: 529 default:
530 port_name = "Unknown"; 530 port_name = "Unknown";
531 break; 531 break;
532 } 532 }
533 533
534 return sprintf(buf, "%s\n", port_name); 534 return sprintf(buf, "%s\n", port_name);
535 } 535 }
536 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL); 536 static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
537 537
538 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb) 538 static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
539 { 539 {
540 int idx; 540 int idx;
541 541
542 for (idx = 0; idx < serial->num_rx_urbs; idx++) 542 for (idx = 0; idx < serial->num_rx_urbs; idx++)
543 if (serial->rx_urb[idx] == urb) 543 if (serial->rx_urb[idx] == urb)
544 return idx; 544 return idx;
545 dev_err(serial->parent->dev, "hso_urb_to_index failed\n"); 545 dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
546 return -1; 546 return -1;
547 } 547 }
548 548
549 /* converts mux value to a port spec value */ 549 /* converts mux value to a port spec value */
550 static u32 hso_mux_to_port(int mux) 550 static u32 hso_mux_to_port(int mux)
551 { 551 {
552 u32 result; 552 u32 result;
553 553
554 switch (mux) { 554 switch (mux) {
555 case 0x1: 555 case 0x1:
556 result = HSO_PORT_CONTROL; 556 result = HSO_PORT_CONTROL;
557 break; 557 break;
558 case 0x2: 558 case 0x2:
559 result = HSO_PORT_APP; 559 result = HSO_PORT_APP;
560 break; 560 break;
561 case 0x4: 561 case 0x4:
562 result = HSO_PORT_PCSC; 562 result = HSO_PORT_PCSC;
563 break; 563 break;
564 case 0x8: 564 case 0x8:
565 result = HSO_PORT_GPS; 565 result = HSO_PORT_GPS;
566 break; 566 break;
567 case 0x10: 567 case 0x10:
568 result = HSO_PORT_APP2; 568 result = HSO_PORT_APP2;
569 break; 569 break;
570 default: 570 default:
571 result = HSO_PORT_NO_PORT; 571 result = HSO_PORT_NO_PORT;
572 } 572 }
573 return result; 573 return result;
574 } 574 }
575 575
576 /* converts port spec value to a mux value */ 576 /* converts port spec value to a mux value */
577 static u32 hso_port_to_mux(int port) 577 static u32 hso_port_to_mux(int port)
578 { 578 {
579 u32 result; 579 u32 result;
580 580
581 switch (port & HSO_PORT_MASK) { 581 switch (port & HSO_PORT_MASK) {
582 case HSO_PORT_CONTROL: 582 case HSO_PORT_CONTROL:
583 result = 0x0; 583 result = 0x0;
584 break; 584 break;
585 case HSO_PORT_APP: 585 case HSO_PORT_APP:
586 result = 0x1; 586 result = 0x1;
587 break; 587 break;
588 case HSO_PORT_PCSC: 588 case HSO_PORT_PCSC:
589 result = 0x2; 589 result = 0x2;
590 break; 590 break;
591 case HSO_PORT_GPS: 591 case HSO_PORT_GPS:
592 result = 0x3; 592 result = 0x3;
593 break; 593 break;
594 case HSO_PORT_APP2: 594 case HSO_PORT_APP2:
595 result = 0x4; 595 result = 0x4;
596 break; 596 break;
597 default: 597 default:
598 result = 0x0; 598 result = 0x0;
599 } 599 }
600 return result; 600 return result;
601 } 601 }
602 602
603 static struct hso_serial *get_serial_by_shared_int_and_type( 603 static struct hso_serial *get_serial_by_shared_int_and_type(
604 struct hso_shared_int *shared_int, 604 struct hso_shared_int *shared_int,
605 int mux) 605 int mux)
606 { 606 {
607 int i, port; 607 int i, port;
608 608
609 port = hso_mux_to_port(mux); 609 port = hso_mux_to_port(mux);
610 610
611 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) { 611 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
612 if (serial_table[i] && 612 if (serial_table[i] &&
613 (dev2ser(serial_table[i])->shared_int == shared_int) && 613 (dev2ser(serial_table[i])->shared_int == shared_int) &&
614 ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) { 614 ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
615 return dev2ser(serial_table[i]); 615 return dev2ser(serial_table[i]);
616 } 616 }
617 } 617 }
618 618
619 return NULL; 619 return NULL;
620 } 620 }
621 621
622 static struct hso_serial *get_serial_by_index(unsigned index) 622 static struct hso_serial *get_serial_by_index(unsigned index)
623 { 623 {
624 struct hso_serial *serial = NULL; 624 struct hso_serial *serial = NULL;
625 unsigned long flags; 625 unsigned long flags;
626 626
627 spin_lock_irqsave(&serial_table_lock, flags); 627 spin_lock_irqsave(&serial_table_lock, flags);
628 if (serial_table[index]) 628 if (serial_table[index])
629 serial = dev2ser(serial_table[index]); 629 serial = dev2ser(serial_table[index]);
630 spin_unlock_irqrestore(&serial_table_lock, flags); 630 spin_unlock_irqrestore(&serial_table_lock, flags);
631 631
632 return serial; 632 return serial;
633 } 633 }
634 634
635 static int get_free_serial_index(void) 635 static int get_free_serial_index(void)
636 { 636 {
637 int index; 637 int index;
638 unsigned long flags; 638 unsigned long flags;
639 639
640 spin_lock_irqsave(&serial_table_lock, flags); 640 spin_lock_irqsave(&serial_table_lock, flags);
641 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) { 641 for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
642 if (serial_table[index] == NULL) { 642 if (serial_table[index] == NULL) {
643 spin_unlock_irqrestore(&serial_table_lock, flags); 643 spin_unlock_irqrestore(&serial_table_lock, flags);
644 return index; 644 return index;
645 } 645 }
646 } 646 }
647 spin_unlock_irqrestore(&serial_table_lock, flags); 647 spin_unlock_irqrestore(&serial_table_lock, flags);
648 648
649 printk(KERN_ERR "%s: no free serial devices in table\n", __func__); 649 printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
650 return -1; 650 return -1;
651 } 651 }
652 652
653 static void set_serial_by_index(unsigned index, struct hso_serial *serial) 653 static void set_serial_by_index(unsigned index, struct hso_serial *serial)
654 { 654 {
655 unsigned long flags; 655 unsigned long flags;
656 656
657 spin_lock_irqsave(&serial_table_lock, flags); 657 spin_lock_irqsave(&serial_table_lock, flags);
658 if (serial) 658 if (serial)
659 serial_table[index] = serial->parent; 659 serial_table[index] = serial->parent;
660 else 660 else
661 serial_table[index] = NULL; 661 serial_table[index] = NULL;
662 spin_unlock_irqrestore(&serial_table_lock, flags); 662 spin_unlock_irqrestore(&serial_table_lock, flags);
663 } 663 }
664 664
665 static void handle_usb_error(int status, const char *function, 665 static void handle_usb_error(int status, const char *function,
666 struct hso_device *hso_dev) 666 struct hso_device *hso_dev)
667 { 667 {
668 char *explanation; 668 char *explanation;
669 669
670 switch (status) { 670 switch (status) {
671 case -ENODEV: 671 case -ENODEV:
672 explanation = "no device"; 672 explanation = "no device";
673 break; 673 break;
674 case -ENOENT: 674 case -ENOENT:
675 explanation = "endpoint not enabled"; 675 explanation = "endpoint not enabled";
676 break; 676 break;
677 case -EPIPE: 677 case -EPIPE:
678 explanation = "endpoint stalled"; 678 explanation = "endpoint stalled";
679 break; 679 break;
680 case -ENOSPC: 680 case -ENOSPC:
681 explanation = "not enough bandwidth"; 681 explanation = "not enough bandwidth";
682 break; 682 break;
683 case -ESHUTDOWN: 683 case -ESHUTDOWN:
684 explanation = "device disabled"; 684 explanation = "device disabled";
685 break; 685 break;
686 case -EHOSTUNREACH: 686 case -EHOSTUNREACH:
687 explanation = "device suspended"; 687 explanation = "device suspended";
688 break; 688 break;
689 case -EINVAL: 689 case -EINVAL:
690 case -EAGAIN: 690 case -EAGAIN:
691 case -EFBIG: 691 case -EFBIG:
692 case -EMSGSIZE: 692 case -EMSGSIZE:
693 explanation = "internal error"; 693 explanation = "internal error";
694 break; 694 break;
695 case -EILSEQ: 695 case -EILSEQ:
696 case -EPROTO: 696 case -EPROTO:
697 case -ETIME: 697 case -ETIME:
698 case -ETIMEDOUT: 698 case -ETIMEDOUT:
699 explanation = "protocol error"; 699 explanation = "protocol error";
700 if (hso_dev) 700 if (hso_dev)
701 schedule_work(&hso_dev->reset_device); 701 schedule_work(&hso_dev->reset_device);
702 break; 702 break;
703 default: 703 default:
704 explanation = "unknown status"; 704 explanation = "unknown status";
705 break; 705 break;
706 } 706 }
707 707
708 /* log a meaningful explanation of an USB status */ 708 /* log a meaningful explanation of an USB status */
709 D1("%s: received USB status - %s (%d)", function, explanation, status); 709 D1("%s: received USB status - %s (%d)", function, explanation, status);
710 } 710 }
711 711
712 /* Network interface functions */ 712 /* Network interface functions */
713 713
714 /* called when net interface is brought up by ifconfig */ 714 /* called when net interface is brought up by ifconfig */
715 static int hso_net_open(struct net_device *net) 715 static int hso_net_open(struct net_device *net)
716 { 716 {
717 struct hso_net *odev = netdev_priv(net); 717 struct hso_net *odev = netdev_priv(net);
718 unsigned long flags = 0; 718 unsigned long flags = 0;
719 719
720 if (!odev) { 720 if (!odev) {
721 dev_err(&net->dev, "No net device !\n"); 721 dev_err(&net->dev, "No net device !\n");
722 return -ENODEV; 722 return -ENODEV;
723 } 723 }
724 724
725 odev->skb_tx_buf = NULL; 725 odev->skb_tx_buf = NULL;
726 726
727 /* setup environment */ 727 /* setup environment */
728 spin_lock_irqsave(&odev->net_lock, flags); 728 spin_lock_irqsave(&odev->net_lock, flags);
729 odev->rx_parse_state = WAIT_IP; 729 odev->rx_parse_state = WAIT_IP;
730 odev->rx_buf_size = 0; 730 odev->rx_buf_size = 0;
731 odev->rx_buf_missing = sizeof(struct iphdr); 731 odev->rx_buf_missing = sizeof(struct iphdr);
732 spin_unlock_irqrestore(&odev->net_lock, flags); 732 spin_unlock_irqrestore(&odev->net_lock, flags);
733 733
734 /* We are up and running. */ 734 /* We are up and running. */
735 set_bit(HSO_NET_RUNNING, &odev->flags); 735 set_bit(HSO_NET_RUNNING, &odev->flags);
736 hso_start_net_device(odev->parent); 736 hso_start_net_device(odev->parent);
737 737
738 /* Tell the kernel we are ready to start receiving from it */ 738 /* Tell the kernel we are ready to start receiving from it */
739 netif_start_queue(net); 739 netif_start_queue(net);
740 740
741 return 0; 741 return 0;
742 } 742 }
743 743
744 /* called when interface is brought down by ifconfig */ 744 /* called when interface is brought down by ifconfig */
745 static int hso_net_close(struct net_device *net) 745 static int hso_net_close(struct net_device *net)
746 { 746 {
747 struct hso_net *odev = netdev_priv(net); 747 struct hso_net *odev = netdev_priv(net);
748 748
749 /* we don't need the queue anymore */ 749 /* we don't need the queue anymore */
750 netif_stop_queue(net); 750 netif_stop_queue(net);
751 /* no longer running */ 751 /* no longer running */
752 clear_bit(HSO_NET_RUNNING, &odev->flags); 752 clear_bit(HSO_NET_RUNNING, &odev->flags);
753 753
754 hso_stop_net_device(odev->parent); 754 hso_stop_net_device(odev->parent);
755 755
756 /* done */ 756 /* done */
757 return 0; 757 return 0;
758 } 758 }
759 759
760 /* USB tells is xmit done, we should start the netqueue again */ 760 /* USB tells is xmit done, we should start the netqueue again */
761 static void write_bulk_callback(struct urb *urb) 761 static void write_bulk_callback(struct urb *urb)
762 { 762 {
763 struct hso_net *odev = urb->context; 763 struct hso_net *odev = urb->context;
764 int status = urb->status; 764 int status = urb->status;
765 765
766 /* Sanity check */ 766 /* Sanity check */
767 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) { 767 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
768 dev_err(&urb->dev->dev, "%s: device not running\n", __func__); 768 dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
769 return; 769 return;
770 } 770 }
771 771
772 /* Do we still have a valid kernel network device? */ 772 /* Do we still have a valid kernel network device? */
773 if (!netif_device_present(odev->net)) { 773 if (!netif_device_present(odev->net)) {
774 dev_err(&urb->dev->dev, "%s: net device not present\n", 774 dev_err(&urb->dev->dev, "%s: net device not present\n",
775 __func__); 775 __func__);
776 return; 776 return;
777 } 777 }
778 778
779 /* log status, but don't act on it, we don't need to resubmit anything 779 /* log status, but don't act on it, we don't need to resubmit anything
780 * anyhow */ 780 * anyhow */
781 if (status) 781 if (status)
782 handle_usb_error(status, __func__, odev->parent); 782 handle_usb_error(status, __func__, odev->parent);
783 783
784 hso_put_activity(odev->parent); 784 hso_put_activity(odev->parent);
785 785
786 /* Tell the network interface we are ready for another frame */ 786 /* Tell the network interface we are ready for another frame */
787 netif_wake_queue(odev->net); 787 netif_wake_queue(odev->net);
788 } 788 }
789 789
790 /* called by kernel when we need to transmit a packet */ 790 /* called by kernel when we need to transmit a packet */
791 static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb, 791 static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
792 struct net_device *net) 792 struct net_device *net)
793 { 793 {
794 struct hso_net *odev = netdev_priv(net); 794 struct hso_net *odev = netdev_priv(net);
795 int result; 795 int result;
796 796
797 /* Tell the kernel, "No more frames 'til we are done with this one." */ 797 /* Tell the kernel, "No more frames 'til we are done with this one." */
798 netif_stop_queue(net); 798 netif_stop_queue(net);
799 if (hso_get_activity(odev->parent) == -EAGAIN) { 799 if (hso_get_activity(odev->parent) == -EAGAIN) {
800 odev->skb_tx_buf = skb; 800 odev->skb_tx_buf = skb;
801 return NETDEV_TX_OK; 801 return NETDEV_TX_OK;
802 } 802 }
803 803
804 /* log if asked */ 804 /* log if asked */
805 DUMP1(skb->data, skb->len); 805 DUMP1(skb->data, skb->len);
806 /* Copy it from kernel memory to OUR memory */ 806 /* Copy it from kernel memory to OUR memory */
807 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len); 807 memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
808 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE); 808 D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
809 809
810 /* Fill in the URB for shipping it out. */ 810 /* Fill in the URB for shipping it out. */
811 usb_fill_bulk_urb(odev->mux_bulk_tx_urb, 811 usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
812 odev->parent->usb, 812 odev->parent->usb,
813 usb_sndbulkpipe(odev->parent->usb, 813 usb_sndbulkpipe(odev->parent->usb,
814 odev->out_endp-> 814 odev->out_endp->
815 bEndpointAddress & 0x7F), 815 bEndpointAddress & 0x7F),
816 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback, 816 odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
817 odev); 817 odev);
818 818
819 /* Deal with the Zero Length packet problem, I hope */ 819 /* Deal with the Zero Length packet problem, I hope */
820 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET; 820 odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
821 821
822 /* Send the URB on its merry way. */ 822 /* Send the URB on its merry way. */
823 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC); 823 result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
824 if (result) { 824 if (result) {
825 dev_warn(&odev->parent->interface->dev, 825 dev_warn(&odev->parent->interface->dev,
826 "failed mux_bulk_tx_urb %d\n", result); 826 "failed mux_bulk_tx_urb %d\n", result);
827 net->stats.tx_errors++; 827 net->stats.tx_errors++;
828 netif_start_queue(net); 828 netif_start_queue(net);
829 } else { 829 } else {
830 net->stats.tx_packets++; 830 net->stats.tx_packets++;
831 net->stats.tx_bytes += skb->len; 831 net->stats.tx_bytes += skb->len;
832 } 832 }
833 dev_kfree_skb(skb); 833 dev_kfree_skb(skb);
834 /* we're done */ 834 /* we're done */
835 return NETDEV_TX_OK; 835 return NETDEV_TX_OK;
836 } 836 }
837 837
838 static const struct ethtool_ops ops = { 838 static const struct ethtool_ops ops = {
839 .get_link = ethtool_op_get_link 839 .get_link = ethtool_op_get_link
840 }; 840 };
841 841
842 /* called when a packet did not ack after watchdogtimeout */ 842 /* called when a packet did not ack after watchdogtimeout */
843 static void hso_net_tx_timeout(struct net_device *net) 843 static void hso_net_tx_timeout(struct net_device *net)
844 { 844 {
845 struct hso_net *odev = netdev_priv(net); 845 struct hso_net *odev = netdev_priv(net);
846 846
847 if (!odev) 847 if (!odev)
848 return; 848 return;
849 849
850 /* Tell syslog we are hosed. */ 850 /* Tell syslog we are hosed. */
851 dev_warn(&net->dev, "Tx timed out.\n"); 851 dev_warn(&net->dev, "Tx timed out.\n");
852 852
853 /* Tear the waiting frame off the list */ 853 /* Tear the waiting frame off the list */
854 if (odev->mux_bulk_tx_urb && 854 if (odev->mux_bulk_tx_urb &&
855 (odev->mux_bulk_tx_urb->status == -EINPROGRESS)) 855 (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
856 usb_unlink_urb(odev->mux_bulk_tx_urb); 856 usb_unlink_urb(odev->mux_bulk_tx_urb);
857 857
858 /* Update statistics */ 858 /* Update statistics */
859 net->stats.tx_errors++; 859 net->stats.tx_errors++;
860 } 860 }
861 861
862 /* make a real packet from the received USB buffer */ 862 /* make a real packet from the received USB buffer */
863 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt, 863 static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
864 unsigned int count, unsigned char is_eop) 864 unsigned int count, unsigned char is_eop)
865 { 865 {
866 unsigned short temp_bytes; 866 unsigned short temp_bytes;
867 unsigned short buffer_offset = 0; 867 unsigned short buffer_offset = 0;
868 unsigned short frame_len; 868 unsigned short frame_len;
869 unsigned char *tmp_rx_buf; 869 unsigned char *tmp_rx_buf;
870 870
871 /* log if needed */ 871 /* log if needed */
872 D1("Rx %d bytes", count); 872 D1("Rx %d bytes", count);
873 DUMP(ip_pkt, min(128, (int)count)); 873 DUMP(ip_pkt, min(128, (int)count));
874 874
875 while (count) { 875 while (count) {
876 switch (odev->rx_parse_state) { 876 switch (odev->rx_parse_state) {
877 case WAIT_IP: 877 case WAIT_IP:
878 /* waiting for IP header. */ 878 /* waiting for IP header. */
879 /* wanted bytes - size of ip header */ 879 /* wanted bytes - size of ip header */
880 temp_bytes = 880 temp_bytes =
881 (count < 881 (count <
882 odev->rx_buf_missing) ? count : odev-> 882 odev->rx_buf_missing) ? count : odev->
883 rx_buf_missing; 883 rx_buf_missing;
884 884
885 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) + 885 memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
886 odev->rx_buf_size, ip_pkt + buffer_offset, 886 odev->rx_buf_size, ip_pkt + buffer_offset,
887 temp_bytes); 887 temp_bytes);
888 888
889 odev->rx_buf_size += temp_bytes; 889 odev->rx_buf_size += temp_bytes;
890 buffer_offset += temp_bytes; 890 buffer_offset += temp_bytes;
891 odev->rx_buf_missing -= temp_bytes; 891 odev->rx_buf_missing -= temp_bytes;
892 count -= temp_bytes; 892 count -= temp_bytes;
893 893
894 if (!odev->rx_buf_missing) { 894 if (!odev->rx_buf_missing) {
895 /* header is complete allocate an sk_buffer and 895 /* header is complete allocate an sk_buffer and
896 * continue to WAIT_DATA */ 896 * continue to WAIT_DATA */
897 frame_len = ntohs(odev->rx_ip_hdr.tot_len); 897 frame_len = ntohs(odev->rx_ip_hdr.tot_len);
898 898
899 if ((frame_len > DEFAULT_MRU) || 899 if ((frame_len > DEFAULT_MRU) ||
900 (frame_len < sizeof(struct iphdr))) { 900 (frame_len < sizeof(struct iphdr))) {
901 dev_err(&odev->net->dev, 901 dev_err(&odev->net->dev,
902 "Invalid frame (%d) length\n", 902 "Invalid frame (%d) length\n",
903 frame_len); 903 frame_len);
904 odev->rx_parse_state = WAIT_SYNC; 904 odev->rx_parse_state = WAIT_SYNC;
905 continue; 905 continue;
906 } 906 }
907 /* Allocate an sk_buff */ 907 /* Allocate an sk_buff */
908 odev->skb_rx_buf = netdev_alloc_skb(odev->net, 908 odev->skb_rx_buf = netdev_alloc_skb(odev->net,
909 frame_len); 909 frame_len);
910 if (!odev->skb_rx_buf) { 910 if (!odev->skb_rx_buf) {
911 /* We got no receive buffer. */ 911 /* We got no receive buffer. */
912 D1("could not allocate memory"); 912 D1("could not allocate memory");
913 odev->rx_parse_state = WAIT_SYNC; 913 odev->rx_parse_state = WAIT_SYNC;
914 return; 914 return;
915 } 915 }
916 916
917 /* Copy what we got so far. make room for iphdr 917 /* Copy what we got so far. make room for iphdr
918 * after tail. */ 918 * after tail. */
919 tmp_rx_buf = 919 tmp_rx_buf =
920 skb_put(odev->skb_rx_buf, 920 skb_put(odev->skb_rx_buf,
921 sizeof(struct iphdr)); 921 sizeof(struct iphdr));
922 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr), 922 memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
923 sizeof(struct iphdr)); 923 sizeof(struct iphdr));
924 924
925 /* ETH_HLEN */ 925 /* ETH_HLEN */
926 odev->rx_buf_size = sizeof(struct iphdr); 926 odev->rx_buf_size = sizeof(struct iphdr);
927 927
928 /* Filip actually use .tot_len */ 928 /* Filip actually use .tot_len */
929 odev->rx_buf_missing = 929 odev->rx_buf_missing =
930 frame_len - sizeof(struct iphdr); 930 frame_len - sizeof(struct iphdr);
931 odev->rx_parse_state = WAIT_DATA; 931 odev->rx_parse_state = WAIT_DATA;
932 } 932 }
933 break; 933 break;
934 934
935 case WAIT_DATA: 935 case WAIT_DATA:
936 temp_bytes = (count < odev->rx_buf_missing) 936 temp_bytes = (count < odev->rx_buf_missing)
937 ? count : odev->rx_buf_missing; 937 ? count : odev->rx_buf_missing;
938 938
939 /* Copy the rest of the bytes that are left in the 939 /* Copy the rest of the bytes that are left in the
940 * buffer into the waiting sk_buf. */ 940 * buffer into the waiting sk_buf. */
941 /* Make room for temp_bytes after tail. */ 941 /* Make room for temp_bytes after tail. */
942 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes); 942 tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
943 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes); 943 memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
944 944
945 odev->rx_buf_missing -= temp_bytes; 945 odev->rx_buf_missing -= temp_bytes;
946 count -= temp_bytes; 946 count -= temp_bytes;
947 buffer_offset += temp_bytes; 947 buffer_offset += temp_bytes;
948 odev->rx_buf_size += temp_bytes; 948 odev->rx_buf_size += temp_bytes;
949 if (!odev->rx_buf_missing) { 949 if (!odev->rx_buf_missing) {
950 /* Packet is complete. Inject into stack. */ 950 /* Packet is complete. Inject into stack. */
951 /* We have IP packet here */ 951 /* We have IP packet here */
952 odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP); 952 odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
953 skb_reset_mac_header(odev->skb_rx_buf); 953 skb_reset_mac_header(odev->skb_rx_buf);
954 954
955 /* Ship it off to the kernel */ 955 /* Ship it off to the kernel */
956 netif_rx(odev->skb_rx_buf); 956 netif_rx(odev->skb_rx_buf);
957 /* No longer our buffer. */ 957 /* No longer our buffer. */
958 odev->skb_rx_buf = NULL; 958 odev->skb_rx_buf = NULL;
959 959
960 /* update out statistics */ 960 /* update out statistics */
961 odev->net->stats.rx_packets++; 961 odev->net->stats.rx_packets++;
962 962
963 odev->net->stats.rx_bytes += odev->rx_buf_size; 963 odev->net->stats.rx_bytes += odev->rx_buf_size;
964 964
965 odev->rx_buf_size = 0; 965 odev->rx_buf_size = 0;
966 odev->rx_buf_missing = sizeof(struct iphdr); 966 odev->rx_buf_missing = sizeof(struct iphdr);
967 odev->rx_parse_state = WAIT_IP; 967 odev->rx_parse_state = WAIT_IP;
968 } 968 }
969 break; 969 break;
970 970
971 case WAIT_SYNC: 971 case WAIT_SYNC:
972 D1(" W_S"); 972 D1(" W_S");
973 count = 0; 973 count = 0;
974 break; 974 break;
975 default: 975 default:
976 D1(" "); 976 D1(" ");
977 count--; 977 count--;
978 break; 978 break;
979 } 979 }
980 } 980 }
981 981
982 /* Recovery mechanism for WAIT_SYNC state. */ 982 /* Recovery mechanism for WAIT_SYNC state. */
983 if (is_eop) { 983 if (is_eop) {
984 if (odev->rx_parse_state == WAIT_SYNC) { 984 if (odev->rx_parse_state == WAIT_SYNC) {
985 odev->rx_parse_state = WAIT_IP; 985 odev->rx_parse_state = WAIT_IP;
986 odev->rx_buf_size = 0; 986 odev->rx_buf_size = 0;
987 odev->rx_buf_missing = sizeof(struct iphdr); 987 odev->rx_buf_missing = sizeof(struct iphdr);
988 } 988 }
989 } 989 }
990 } 990 }
991 991
992 static void fix_crc_bug(struct urb *urb, __le16 max_packet_size) 992 static void fix_crc_bug(struct urb *urb, __le16 max_packet_size)
993 { 993 {
994 static const u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF }; 994 static const u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
995 u32 rest = urb->actual_length % le16_to_cpu(max_packet_size); 995 u32 rest = urb->actual_length % le16_to_cpu(max_packet_size);
996 996
997 if (((rest == 5) || (rest == 6)) && 997 if (((rest == 5) || (rest == 6)) &&
998 !memcmp(((u8 *)urb->transfer_buffer) + urb->actual_length - 4, 998 !memcmp(((u8 *)urb->transfer_buffer) + urb->actual_length - 4,
999 crc_check, 4)) { 999 crc_check, 4)) {
1000 urb->actual_length -= 4; 1000 urb->actual_length -= 4;
1001 } 1001 }
1002 } 1002 }
1003 1003
1004 /* Moving data from usb to kernel (in interrupt state) */ 1004 /* Moving data from usb to kernel (in interrupt state) */
1005 static void read_bulk_callback(struct urb *urb) 1005 static void read_bulk_callback(struct urb *urb)
1006 { 1006 {
1007 struct hso_net *odev = urb->context; 1007 struct hso_net *odev = urb->context;
1008 struct net_device *net; 1008 struct net_device *net;
1009 int result; 1009 int result;
1010 int status = urb->status; 1010 int status = urb->status;
1011 1011
1012 /* is al ok? (Filip: Who's Al ?) */ 1012 /* is al ok? (Filip: Who's Al ?) */
1013 if (status) { 1013 if (status) {
1014 handle_usb_error(status, __func__, odev->parent); 1014 handle_usb_error(status, __func__, odev->parent);
1015 return; 1015 return;
1016 } 1016 }
1017 1017
1018 /* Sanity check */ 1018 /* Sanity check */
1019 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) { 1019 if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
1020 D1("BULK IN callback but driver is not active!"); 1020 D1("BULK IN callback but driver is not active!");
1021 return; 1021 return;
1022 } 1022 }
1023 usb_mark_last_busy(urb->dev); 1023 usb_mark_last_busy(urb->dev);
1024 1024
1025 net = odev->net; 1025 net = odev->net;
1026 1026
1027 if (!netif_device_present(net)) { 1027 if (!netif_device_present(net)) {
1028 /* Somebody killed our network interface... */ 1028 /* Somebody killed our network interface... */
1029 return; 1029 return;
1030 } 1030 }
1031 1031
1032 if (odev->parent->port_spec & HSO_INFO_CRC_BUG) 1032 if (odev->parent->port_spec & HSO_INFO_CRC_BUG)
1033 fix_crc_bug(urb, odev->in_endp->wMaxPacketSize); 1033 fix_crc_bug(urb, odev->in_endp->wMaxPacketSize);
1034 1034
1035 /* do we even have a packet? */ 1035 /* do we even have a packet? */
1036 if (urb->actual_length) { 1036 if (urb->actual_length) {
1037 /* Handle the IP stream, add header and push it onto network 1037 /* Handle the IP stream, add header and push it onto network
1038 * stack if the packet is complete. */ 1038 * stack if the packet is complete. */
1039 spin_lock(&odev->net_lock); 1039 spin_lock(&odev->net_lock);
1040 packetizeRx(odev, urb->transfer_buffer, urb->actual_length, 1040 packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
1041 (urb->transfer_buffer_length > 1041 (urb->transfer_buffer_length >
1042 urb->actual_length) ? 1 : 0); 1042 urb->actual_length) ? 1 : 0);
1043 spin_unlock(&odev->net_lock); 1043 spin_unlock(&odev->net_lock);
1044 } 1044 }
1045 1045
1046 /* We are done with this URB, resubmit it. Prep the USB to wait for 1046 /* We are done with this URB, resubmit it. Prep the USB to wait for
1047 * another frame. Reuse same as received. */ 1047 * another frame. Reuse same as received. */
1048 usb_fill_bulk_urb(urb, 1048 usb_fill_bulk_urb(urb,
1049 odev->parent->usb, 1049 odev->parent->usb,
1050 usb_rcvbulkpipe(odev->parent->usb, 1050 usb_rcvbulkpipe(odev->parent->usb,
1051 odev->in_endp-> 1051 odev->in_endp->
1052 bEndpointAddress & 0x7F), 1052 bEndpointAddress & 0x7F),
1053 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE, 1053 urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
1054 read_bulk_callback, odev); 1054 read_bulk_callback, odev);
1055 1055
1056 /* Give this to the USB subsystem so it can tell us when more data 1056 /* Give this to the USB subsystem so it can tell us when more data
1057 * arrives. */ 1057 * arrives. */
1058 result = usb_submit_urb(urb, GFP_ATOMIC); 1058 result = usb_submit_urb(urb, GFP_ATOMIC);
1059 if (result) 1059 if (result)
1060 dev_warn(&odev->parent->interface->dev, 1060 dev_warn(&odev->parent->interface->dev,
1061 "%s failed submit mux_bulk_rx_urb %d\n", __func__, 1061 "%s failed submit mux_bulk_rx_urb %d\n", __func__,
1062 result); 1062 result);
1063 } 1063 }
1064 1064
1065 /* Serial driver functions */ 1065 /* Serial driver functions */
1066 1066
1067 static void hso_init_termios(struct ktermios *termios) 1067 static void hso_init_termios(struct ktermios *termios)
1068 { 1068 {
1069 /* 1069 /*
1070 * The default requirements for this device are: 1070 * The default requirements for this device are:
1071 */ 1071 */
1072 termios->c_iflag &= 1072 termios->c_iflag &=
1073 ~(IGNBRK /* disable ignore break */ 1073 ~(IGNBRK /* disable ignore break */
1074 | BRKINT /* disable break causes interrupt */ 1074 | BRKINT /* disable break causes interrupt */
1075 | PARMRK /* disable mark parity errors */ 1075 | PARMRK /* disable mark parity errors */
1076 | ISTRIP /* disable clear high bit of input characters */ 1076 | ISTRIP /* disable clear high bit of input characters */
1077 | INLCR /* disable translate NL to CR */ 1077 | INLCR /* disable translate NL to CR */
1078 | IGNCR /* disable ignore CR */ 1078 | IGNCR /* disable ignore CR */
1079 | ICRNL /* disable translate CR to NL */ 1079 | ICRNL /* disable translate CR to NL */
1080 | IXON); /* disable enable XON/XOFF flow control */ 1080 | IXON); /* disable enable XON/XOFF flow control */
1081 1081
1082 /* disable postprocess output characters */ 1082 /* disable postprocess output characters */
1083 termios->c_oflag &= ~OPOST; 1083 termios->c_oflag &= ~OPOST;
1084 1084
1085 termios->c_lflag &= 1085 termios->c_lflag &=
1086 ~(ECHO /* disable echo input characters */ 1086 ~(ECHO /* disable echo input characters */
1087 | ECHONL /* disable echo new line */ 1087 | ECHONL /* disable echo new line */
1088 | ICANON /* disable erase, kill, werase, and rprnt 1088 | ICANON /* disable erase, kill, werase, and rprnt
1089 special characters */ 1089 special characters */
1090 | ISIG /* disable interrupt, quit, and suspend special 1090 | ISIG /* disable interrupt, quit, and suspend special
1091 characters */ 1091 characters */
1092 | IEXTEN); /* disable non-POSIX special characters */ 1092 | IEXTEN); /* disable non-POSIX special characters */
1093 1093
1094 termios->c_cflag &= 1094 termios->c_cflag &=
1095 ~(CSIZE /* no size */ 1095 ~(CSIZE /* no size */
1096 | PARENB /* disable parity bit */ 1096 | PARENB /* disable parity bit */
1097 | CBAUD /* clear current baud rate */ 1097 | CBAUD /* clear current baud rate */
1098 | CBAUDEX); /* clear current buad rate */ 1098 | CBAUDEX); /* clear current buad rate */
1099 1099
1100 termios->c_cflag |= CS8; /* character size 8 bits */ 1100 termios->c_cflag |= CS8; /* character size 8 bits */
1101 1101
1102 /* baud rate 115200 */ 1102 /* baud rate 115200 */
1103 tty_termios_encode_baud_rate(termios, 115200, 115200); 1103 tty_termios_encode_baud_rate(termios, 115200, 115200);
1104 } 1104 }
1105 1105
1106 static void _hso_serial_set_termios(struct tty_struct *tty, 1106 static void _hso_serial_set_termios(struct tty_struct *tty,
1107 struct ktermios *old) 1107 struct ktermios *old)
1108 { 1108 {
1109 struct hso_serial *serial = tty->driver_data; 1109 struct hso_serial *serial = tty->driver_data;
1110 1110
1111 if (!serial) { 1111 if (!serial) {
1112 printk(KERN_ERR "%s: no tty structures", __func__); 1112 printk(KERN_ERR "%s: no tty structures", __func__);
1113 return; 1113 return;
1114 } 1114 }
1115 1115
1116 D4("port %d", serial->minor); 1116 D4("port %d", serial->minor);
1117 1117
1118 /* 1118 /*
1119 * Fix up unsupported bits 1119 * Fix up unsupported bits
1120 */ 1120 */
1121 tty->termios.c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */ 1121 tty->termios.c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
1122 1122
1123 tty->termios.c_cflag &= 1123 tty->termios.c_cflag &=
1124 ~(CSIZE /* no size */ 1124 ~(CSIZE /* no size */
1125 | PARENB /* disable parity bit */ 1125 | PARENB /* disable parity bit */
1126 | CBAUD /* clear current baud rate */ 1126 | CBAUD /* clear current baud rate */
1127 | CBAUDEX); /* clear current buad rate */ 1127 | CBAUDEX); /* clear current buad rate */
1128 1128
1129 tty->termios.c_cflag |= CS8; /* character size 8 bits */ 1129 tty->termios.c_cflag |= CS8; /* character size 8 bits */
1130 1130
1131 /* baud rate 115200 */ 1131 /* baud rate 115200 */
1132 tty_encode_baud_rate(tty, 115200, 115200); 1132 tty_encode_baud_rate(tty, 115200, 115200);
1133 } 1133 }
1134 1134
1135 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb) 1135 static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
1136 { 1136 {
1137 int result; 1137 int result;
1138 /* We are done with this URB, resubmit it. Prep the USB to wait for 1138 /* We are done with this URB, resubmit it. Prep the USB to wait for
1139 * another frame */ 1139 * another frame */
1140 usb_fill_bulk_urb(urb, serial->parent->usb, 1140 usb_fill_bulk_urb(urb, serial->parent->usb,
1141 usb_rcvbulkpipe(serial->parent->usb, 1141 usb_rcvbulkpipe(serial->parent->usb,
1142 serial->in_endp-> 1142 serial->in_endp->
1143 bEndpointAddress & 0x7F), 1143 bEndpointAddress & 0x7F),
1144 urb->transfer_buffer, serial->rx_data_length, 1144 urb->transfer_buffer, serial->rx_data_length,
1145 hso_std_serial_read_bulk_callback, serial); 1145 hso_std_serial_read_bulk_callback, serial);
1146 /* Give this to the USB subsystem so it can tell us when more data 1146 /* Give this to the USB subsystem so it can tell us when more data
1147 * arrives. */ 1147 * arrives. */
1148 result = usb_submit_urb(urb, GFP_ATOMIC); 1148 result = usb_submit_urb(urb, GFP_ATOMIC);
1149 if (result) { 1149 if (result) {
1150 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n", 1150 dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
1151 __func__, result); 1151 __func__, result);
1152 } 1152 }
1153 } 1153 }
1154 1154
1155 1155
1156 1156
1157 1157
1158 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial) 1158 static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
1159 { 1159 {
1160 int count; 1160 int count;
1161 struct urb *curr_urb; 1161 struct urb *curr_urb;
1162 1162
1163 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) { 1163 while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
1164 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx]; 1164 curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
1165 count = put_rxbuf_data(curr_urb, serial); 1165 count = put_rxbuf_data(curr_urb, serial);
1166 if (count == -1) 1166 if (count == -1)
1167 return; 1167 return;
1168 if (count == 0) { 1168 if (count == 0) {
1169 serial->curr_rx_urb_idx++; 1169 serial->curr_rx_urb_idx++;
1170 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs) 1170 if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
1171 serial->curr_rx_urb_idx = 0; 1171 serial->curr_rx_urb_idx = 0;
1172 hso_resubmit_rx_bulk_urb(serial, curr_urb); 1172 hso_resubmit_rx_bulk_urb(serial, curr_urb);
1173 } 1173 }
1174 } 1174 }
1175 } 1175 }
1176 1176
1177 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial) 1177 static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
1178 { 1178 {
1179 int count = 0; 1179 int count = 0;
1180 struct urb *urb; 1180 struct urb *urb;
1181 1181
1182 urb = serial->rx_urb[0]; 1182 urb = serial->rx_urb[0];
1183 if (serial->port.count > 0) { 1183 if (serial->port.count > 0) {
1184 count = put_rxbuf_data(urb, serial); 1184 count = put_rxbuf_data(urb, serial);
1185 if (count == -1) 1185 if (count == -1)
1186 return; 1186 return;
1187 } 1187 }
1188 /* Re issue a read as long as we receive data. */ 1188 /* Re issue a read as long as we receive data. */
1189 1189
1190 if (count == 0 && ((urb->actual_length != 0) || 1190 if (count == 0 && ((urb->actual_length != 0) ||
1191 (serial->rx_state == RX_PENDING))) { 1191 (serial->rx_state == RX_PENDING))) {
1192 serial->rx_state = RX_SENT; 1192 serial->rx_state = RX_SENT;
1193 hso_mux_serial_read(serial); 1193 hso_mux_serial_read(serial);
1194 } else 1194 } else
1195 serial->rx_state = RX_IDLE; 1195 serial->rx_state = RX_IDLE;
1196 } 1196 }
1197 1197
1198 1198
1199 /* read callback for Diag and CS port */ 1199 /* read callback for Diag and CS port */
1200 static void hso_std_serial_read_bulk_callback(struct urb *urb) 1200 static void hso_std_serial_read_bulk_callback(struct urb *urb)
1201 { 1201 {
1202 struct hso_serial *serial = urb->context; 1202 struct hso_serial *serial = urb->context;
1203 int status = urb->status; 1203 int status = urb->status;
1204 1204
1205 /* sanity check */ 1205 /* sanity check */
1206 if (!serial) { 1206 if (!serial) {
1207 D1("serial == NULL"); 1207 D1("serial == NULL");
1208 return; 1208 return;
1209 } else if (status) { 1209 } else if (status) {
1210 handle_usb_error(status, __func__, serial->parent); 1210 handle_usb_error(status, __func__, serial->parent);
1211 return; 1211 return;
1212 } 1212 }
1213 1213
1214 D4("\n--- Got serial_read_bulk callback %02x ---", status); 1214 D4("\n--- Got serial_read_bulk callback %02x ---", status);
1215 D1("Actual length = %d\n", urb->actual_length); 1215 D1("Actual length = %d\n", urb->actual_length);
1216 DUMP1(urb->transfer_buffer, urb->actual_length); 1216 DUMP1(urb->transfer_buffer, urb->actual_length);
1217 1217
1218 /* Anyone listening? */ 1218 /* Anyone listening? */
1219 if (serial->port.count == 0) 1219 if (serial->port.count == 0)
1220 return; 1220 return;
1221 1221
1222 if (status == 0) { 1222 if (status == 0) {
1223 if (serial->parent->port_spec & HSO_INFO_CRC_BUG) 1223 if (serial->parent->port_spec & HSO_INFO_CRC_BUG)
1224 fix_crc_bug(urb, serial->in_endp->wMaxPacketSize); 1224 fix_crc_bug(urb, serial->in_endp->wMaxPacketSize);
1225 /* Valid data, handle RX data */ 1225 /* Valid data, handle RX data */
1226 spin_lock(&serial->serial_lock); 1226 spin_lock(&serial->serial_lock);
1227 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1; 1227 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
1228 put_rxbuf_data_and_resubmit_bulk_urb(serial); 1228 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1229 spin_unlock(&serial->serial_lock); 1229 spin_unlock(&serial->serial_lock);
1230 } else if (status == -ENOENT || status == -ECONNRESET) { 1230 } else if (status == -ENOENT || status == -ECONNRESET) {
1231 /* Unlinked - check for throttled port. */ 1231 /* Unlinked - check for throttled port. */
1232 D2("Port %d, successfully unlinked urb", serial->minor); 1232 D2("Port %d, successfully unlinked urb", serial->minor);
1233 spin_lock(&serial->serial_lock); 1233 spin_lock(&serial->serial_lock);
1234 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0; 1234 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
1235 hso_resubmit_rx_bulk_urb(serial, urb); 1235 hso_resubmit_rx_bulk_urb(serial, urb);
1236 spin_unlock(&serial->serial_lock); 1236 spin_unlock(&serial->serial_lock);
1237 } else { 1237 } else {
1238 D2("Port %d, status = %d for read urb", serial->minor, status); 1238 D2("Port %d, status = %d for read urb", serial->minor, status);
1239 return; 1239 return;
1240 } 1240 }
1241 } 1241 }
1242 1242
1243 /* 1243 /*
1244 * This needs to be a tasklet otherwise we will 1244 * This needs to be a tasklet otherwise we will
1245 * end up recursively calling this function. 1245 * end up recursively calling this function.
1246 */ 1246 */
1247 static void hso_unthrottle_tasklet(struct hso_serial *serial) 1247 static void hso_unthrottle_tasklet(struct hso_serial *serial)
1248 { 1248 {
1249 unsigned long flags; 1249 unsigned long flags;
1250 1250
1251 spin_lock_irqsave(&serial->serial_lock, flags); 1251 spin_lock_irqsave(&serial->serial_lock, flags);
1252 if ((serial->parent->port_spec & HSO_INTF_MUX)) 1252 if ((serial->parent->port_spec & HSO_INTF_MUX))
1253 put_rxbuf_data_and_resubmit_ctrl_urb(serial); 1253 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
1254 else 1254 else
1255 put_rxbuf_data_and_resubmit_bulk_urb(serial); 1255 put_rxbuf_data_and_resubmit_bulk_urb(serial);
1256 spin_unlock_irqrestore(&serial->serial_lock, flags); 1256 spin_unlock_irqrestore(&serial->serial_lock, flags);
1257 } 1257 }
1258 1258
1259 static void hso_unthrottle(struct tty_struct *tty) 1259 static void hso_unthrottle(struct tty_struct *tty)
1260 { 1260 {
1261 struct hso_serial *serial = tty->driver_data; 1261 struct hso_serial *serial = tty->driver_data;
1262 1262
1263 tasklet_hi_schedule(&serial->unthrottle_tasklet); 1263 tasklet_hi_schedule(&serial->unthrottle_tasklet);
1264 } 1264 }
1265 1265
1266 static void hso_unthrottle_workfunc(struct work_struct *work) 1266 static void hso_unthrottle_workfunc(struct work_struct *work)
1267 { 1267 {
1268 struct hso_serial *serial = 1268 struct hso_serial *serial =
1269 container_of(work, struct hso_serial, 1269 container_of(work, struct hso_serial,
1270 retry_unthrottle_workqueue); 1270 retry_unthrottle_workqueue);
1271 hso_unthrottle_tasklet(serial); 1271 hso_unthrottle_tasklet(serial);
1272 } 1272 }
1273 1273
1274 /* open the requested serial port */ 1274 /* open the requested serial port */
1275 static int hso_serial_open(struct tty_struct *tty, struct file *filp) 1275 static int hso_serial_open(struct tty_struct *tty, struct file *filp)
1276 { 1276 {
1277 struct hso_serial *serial = get_serial_by_index(tty->index); 1277 struct hso_serial *serial = get_serial_by_index(tty->index);
1278 int result; 1278 int result;
1279 1279
1280 /* sanity check */ 1280 /* sanity check */
1281 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) { 1281 if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
1282 WARN_ON(1); 1282 WARN_ON(1);
1283 tty->driver_data = NULL; 1283 tty->driver_data = NULL;
1284 D1("Failed to open port"); 1284 D1("Failed to open port");
1285 return -ENODEV; 1285 return -ENODEV;
1286 } 1286 }
1287 1287
1288 mutex_lock(&serial->parent->mutex); 1288 mutex_lock(&serial->parent->mutex);
1289 result = usb_autopm_get_interface(serial->parent->interface); 1289 result = usb_autopm_get_interface(serial->parent->interface);
1290 if (result < 0) 1290 if (result < 0)
1291 goto err_out; 1291 goto err_out;
1292 1292
1293 D1("Opening %d", serial->minor); 1293 D1("Opening %d", serial->minor);
1294 kref_get(&serial->parent->ref); 1294 kref_get(&serial->parent->ref);
1295 1295
1296 /* setup */ 1296 /* setup */
1297 tty->driver_data = serial; 1297 tty->driver_data = serial;
1298 tty_port_tty_set(&serial->port, tty); 1298 tty_port_tty_set(&serial->port, tty);
1299 1299
1300 /* check for port already opened, if not set the termios */ 1300 /* check for port already opened, if not set the termios */
1301 serial->port.count++; 1301 serial->port.count++;
1302 if (serial->port.count == 1) { 1302 if (serial->port.count == 1) {
1303 serial->rx_state = RX_IDLE; 1303 serial->rx_state = RX_IDLE;
1304 /* Force default termio settings */ 1304 /* Force default termio settings */
1305 _hso_serial_set_termios(tty, NULL); 1305 _hso_serial_set_termios(tty, NULL);
1306 tasklet_init(&serial->unthrottle_tasklet, 1306 tasklet_init(&serial->unthrottle_tasklet,
1307 (void (*)(unsigned long))hso_unthrottle_tasklet, 1307 (void (*)(unsigned long))hso_unthrottle_tasklet,
1308 (unsigned long)serial); 1308 (unsigned long)serial);
1309 INIT_WORK(&serial->retry_unthrottle_workqueue, 1309 INIT_WORK(&serial->retry_unthrottle_workqueue,
1310 hso_unthrottle_workfunc); 1310 hso_unthrottle_workfunc);
1311 result = hso_start_serial_device(serial->parent, GFP_KERNEL); 1311 result = hso_start_serial_device(serial->parent, GFP_KERNEL);
1312 if (result) { 1312 if (result) {
1313 hso_stop_serial_device(serial->parent); 1313 hso_stop_serial_device(serial->parent);
1314 serial->port.count--; 1314 serial->port.count--;
1315 kref_put(&serial->parent->ref, hso_serial_ref_free); 1315 kref_put(&serial->parent->ref, hso_serial_ref_free);
1316 } 1316 }
1317 } else { 1317 } else {
1318 D1("Port was already open"); 1318 D1("Port was already open");
1319 } 1319 }
1320 1320
1321 usb_autopm_put_interface(serial->parent->interface); 1321 usb_autopm_put_interface(serial->parent->interface);
1322 1322
1323 /* done */ 1323 /* done */
1324 if (result) 1324 if (result)
1325 hso_serial_tiocmset(tty, TIOCM_RTS | TIOCM_DTR, 0); 1325 hso_serial_tiocmset(tty, TIOCM_RTS | TIOCM_DTR, 0);
1326 err_out: 1326 err_out:
1327 mutex_unlock(&serial->parent->mutex); 1327 mutex_unlock(&serial->parent->mutex);
1328 return result; 1328 return result;
1329 } 1329 }
1330 1330
1331 /* close the requested serial port */ 1331 /* close the requested serial port */
1332 static void hso_serial_close(struct tty_struct *tty, struct file *filp) 1332 static void hso_serial_close(struct tty_struct *tty, struct file *filp)
1333 { 1333 {
1334 struct hso_serial *serial = tty->driver_data; 1334 struct hso_serial *serial = tty->driver_data;
1335 u8 usb_gone; 1335 u8 usb_gone;
1336 1336
1337 D1("Closing serial port"); 1337 D1("Closing serial port");
1338 1338
1339 /* Open failed, no close cleanup required */ 1339 /* Open failed, no close cleanup required */
1340 if (serial == NULL) 1340 if (serial == NULL)
1341 return; 1341 return;
1342 1342
1343 mutex_lock(&serial->parent->mutex); 1343 mutex_lock(&serial->parent->mutex);
1344 usb_gone = serial->parent->usb_gone; 1344 usb_gone = serial->parent->usb_gone;
1345 1345
1346 if (!usb_gone) 1346 if (!usb_gone)
1347 usb_autopm_get_interface(serial->parent->interface); 1347 usb_autopm_get_interface(serial->parent->interface);
1348 1348
1349 /* reset the rts and dtr */ 1349 /* reset the rts and dtr */
1350 /* do the actual close */ 1350 /* do the actual close */
1351 serial->port.count--; 1351 serial->port.count--;
1352 1352
1353 if (serial->port.count <= 0) { 1353 if (serial->port.count <= 0) {
1354 serial->port.count = 0; 1354 serial->port.count = 0;
1355 tty_port_tty_set(&serial->port, NULL); 1355 tty_port_tty_set(&serial->port, NULL);
1356 if (!usb_gone) 1356 if (!usb_gone)
1357 hso_stop_serial_device(serial->parent); 1357 hso_stop_serial_device(serial->parent);
1358 tasklet_kill(&serial->unthrottle_tasklet); 1358 tasklet_kill(&serial->unthrottle_tasklet);
1359 cancel_work_sync(&serial->retry_unthrottle_workqueue); 1359 cancel_work_sync(&serial->retry_unthrottle_workqueue);
1360 } 1360 }
1361 1361
1362 if (!usb_gone) 1362 if (!usb_gone)
1363 usb_autopm_put_interface(serial->parent->interface); 1363 usb_autopm_put_interface(serial->parent->interface);
1364 1364
1365 mutex_unlock(&serial->parent->mutex); 1365 mutex_unlock(&serial->parent->mutex);
1366 1366
1367 kref_put(&serial->parent->ref, hso_serial_ref_free); 1367 kref_put(&serial->parent->ref, hso_serial_ref_free);
1368 } 1368 }
1369 1369
1370 /* close the requested serial port */ 1370 /* close the requested serial port */
1371 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf, 1371 static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
1372 int count) 1372 int count)
1373 { 1373 {
1374 struct hso_serial *serial = tty->driver_data; 1374 struct hso_serial *serial = tty->driver_data;
1375 int space, tx_bytes; 1375 int space, tx_bytes;
1376 unsigned long flags; 1376 unsigned long flags;
1377 1377
1378 /* sanity check */ 1378 /* sanity check */
1379 if (serial == NULL) { 1379 if (serial == NULL) {
1380 printk(KERN_ERR "%s: serial is NULL\n", __func__); 1380 printk(KERN_ERR "%s: serial is NULL\n", __func__);
1381 return -ENODEV; 1381 return -ENODEV;
1382 } 1382 }
1383 1383
1384 spin_lock_irqsave(&serial->serial_lock, flags); 1384 spin_lock_irqsave(&serial->serial_lock, flags);
1385 1385
1386 space = serial->tx_data_length - serial->tx_buffer_count; 1386 space = serial->tx_data_length - serial->tx_buffer_count;
1387 tx_bytes = (count < space) ? count : space; 1387 tx_bytes = (count < space) ? count : space;
1388 1388
1389 if (!tx_bytes) 1389 if (!tx_bytes)
1390 goto out; 1390 goto out;
1391 1391
1392 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes); 1392 memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
1393 serial->tx_buffer_count += tx_bytes; 1393 serial->tx_buffer_count += tx_bytes;
1394 1394
1395 out: 1395 out:
1396 spin_unlock_irqrestore(&serial->serial_lock, flags); 1396 spin_unlock_irqrestore(&serial->serial_lock, flags);
1397 1397
1398 hso_kick_transmit(serial); 1398 hso_kick_transmit(serial);
1399 /* done */ 1399 /* done */
1400 return tx_bytes; 1400 return tx_bytes;
1401 } 1401 }
1402 1402
1403 /* how much room is there for writing */ 1403 /* how much room is there for writing */
1404 static int hso_serial_write_room(struct tty_struct *tty) 1404 static int hso_serial_write_room(struct tty_struct *tty)
1405 { 1405 {
1406 struct hso_serial *serial = tty->driver_data; 1406 struct hso_serial *serial = tty->driver_data;
1407 int room; 1407 int room;
1408 unsigned long flags; 1408 unsigned long flags;
1409 1409
1410 spin_lock_irqsave(&serial->serial_lock, flags); 1410 spin_lock_irqsave(&serial->serial_lock, flags);
1411 room = serial->tx_data_length - serial->tx_buffer_count; 1411 room = serial->tx_data_length - serial->tx_buffer_count;
1412 spin_unlock_irqrestore(&serial->serial_lock, flags); 1412 spin_unlock_irqrestore(&serial->serial_lock, flags);
1413 1413
1414 /* return free room */ 1414 /* return free room */
1415 return room; 1415 return room;
1416 } 1416 }
1417 1417
1418 /* setup the term */ 1418 /* setup the term */
1419 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old) 1419 static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
1420 { 1420 {
1421 struct hso_serial *serial = tty->driver_data; 1421 struct hso_serial *serial = tty->driver_data;
1422 unsigned long flags; 1422 unsigned long flags;
1423 1423
1424 if (old) 1424 if (old)
1425 D5("Termios called with: cflags new[%d] - old[%d]", 1425 D5("Termios called with: cflags new[%d] - old[%d]",
1426 tty->termios.c_cflag, old->c_cflag); 1426 tty->termios.c_cflag, old->c_cflag);
1427 1427
1428 /* the actual setup */ 1428 /* the actual setup */
1429 spin_lock_irqsave(&serial->serial_lock, flags); 1429 spin_lock_irqsave(&serial->serial_lock, flags);
1430 if (serial->port.count) 1430 if (serial->port.count)
1431 _hso_serial_set_termios(tty, old); 1431 _hso_serial_set_termios(tty, old);
1432 else 1432 else
1433 tty->termios = *old; 1433 tty->termios = *old;
1434 spin_unlock_irqrestore(&serial->serial_lock, flags); 1434 spin_unlock_irqrestore(&serial->serial_lock, flags);
1435 1435
1436 /* done */ 1436 /* done */
1437 } 1437 }
1438 1438
1439 /* how many characters in the buffer */ 1439 /* how many characters in the buffer */
1440 static int hso_serial_chars_in_buffer(struct tty_struct *tty) 1440 static int hso_serial_chars_in_buffer(struct tty_struct *tty)
1441 { 1441 {
1442 struct hso_serial *serial = tty->driver_data; 1442 struct hso_serial *serial = tty->driver_data;
1443 int chars; 1443 int chars;
1444 unsigned long flags; 1444 unsigned long flags;
1445 1445
1446 /* sanity check */ 1446 /* sanity check */
1447 if (serial == NULL) 1447 if (serial == NULL)
1448 return 0; 1448 return 0;
1449 1449
1450 spin_lock_irqsave(&serial->serial_lock, flags); 1450 spin_lock_irqsave(&serial->serial_lock, flags);
1451 chars = serial->tx_buffer_count; 1451 chars = serial->tx_buffer_count;
1452 spin_unlock_irqrestore(&serial->serial_lock, flags); 1452 spin_unlock_irqrestore(&serial->serial_lock, flags);
1453 1453
1454 return chars; 1454 return chars;
1455 } 1455 }
1456 static int tiocmget_submit_urb(struct hso_serial *serial, 1456 static int tiocmget_submit_urb(struct hso_serial *serial,
1457 struct hso_tiocmget *tiocmget, 1457 struct hso_tiocmget *tiocmget,
1458 struct usb_device *usb) 1458 struct usb_device *usb)
1459 { 1459 {
1460 int result; 1460 int result;
1461 1461
1462 if (serial->parent->usb_gone) 1462 if (serial->parent->usb_gone)
1463 return -ENODEV; 1463 return -ENODEV;
1464 usb_fill_int_urb(tiocmget->urb, usb, 1464 usb_fill_int_urb(tiocmget->urb, usb,
1465 usb_rcvintpipe(usb, 1465 usb_rcvintpipe(usb,
1466 tiocmget->endp-> 1466 tiocmget->endp->
1467 bEndpointAddress & 0x7F), 1467 bEndpointAddress & 0x7F),
1468 &tiocmget->serial_state_notification, 1468 &tiocmget->serial_state_notification,
1469 sizeof(struct hso_serial_state_notification), 1469 sizeof(struct hso_serial_state_notification),
1470 tiocmget_intr_callback, serial, 1470 tiocmget_intr_callback, serial,
1471 tiocmget->endp->bInterval); 1471 tiocmget->endp->bInterval);
1472 result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC); 1472 result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
1473 if (result) { 1473 if (result) {
1474 dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__, 1474 dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
1475 result); 1475 result);
1476 } 1476 }
1477 return result; 1477 return result;
1478 1478
1479 } 1479 }
1480 1480
1481 static void tiocmget_intr_callback(struct urb *urb) 1481 static void tiocmget_intr_callback(struct urb *urb)
1482 { 1482 {
1483 struct hso_serial *serial = urb->context; 1483 struct hso_serial *serial = urb->context;
1484 struct hso_tiocmget *tiocmget; 1484 struct hso_tiocmget *tiocmget;
1485 int status = urb->status; 1485 int status = urb->status;
1486 u16 UART_state_bitmap, prev_UART_state_bitmap; 1486 u16 UART_state_bitmap, prev_UART_state_bitmap;
1487 struct uart_icount *icount; 1487 struct uart_icount *icount;
1488 struct hso_serial_state_notification *serial_state_notification; 1488 struct hso_serial_state_notification *serial_state_notification;
1489 struct usb_device *usb; 1489 struct usb_device *usb;
1490 1490
1491 /* Sanity checks */ 1491 /* Sanity checks */
1492 if (!serial) 1492 if (!serial)
1493 return; 1493 return;
1494 if (status) { 1494 if (status) {
1495 handle_usb_error(status, __func__, serial->parent); 1495 handle_usb_error(status, __func__, serial->parent);
1496 return; 1496 return;
1497 } 1497 }
1498 tiocmget = serial->tiocmget; 1498 tiocmget = serial->tiocmget;
1499 if (!tiocmget) 1499 if (!tiocmget)
1500 return; 1500 return;
1501 usb = serial->parent->usb; 1501 usb = serial->parent->usb;
1502 serial_state_notification = &tiocmget->serial_state_notification; 1502 serial_state_notification = &tiocmget->serial_state_notification;
1503 if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE || 1503 if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
1504 serial_state_notification->bNotification != B_NOTIFICATION || 1504 serial_state_notification->bNotification != B_NOTIFICATION ||
1505 le16_to_cpu(serial_state_notification->wValue) != W_VALUE || 1505 le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
1506 le16_to_cpu(serial_state_notification->wIndex) != W_INDEX || 1506 le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
1507 le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) { 1507 le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
1508 dev_warn(&usb->dev, 1508 dev_warn(&usb->dev,
1509 "hso received invalid serial state notification\n"); 1509 "hso received invalid serial state notification\n");
1510 DUMP(serial_state_notification, 1510 DUMP(serial_state_notification,
1511 sizeof(struct hso_serial_state_notification)); 1511 sizeof(struct hso_serial_state_notification));
1512 } else { 1512 } else {
1513 1513
1514 UART_state_bitmap = le16_to_cpu(serial_state_notification-> 1514 UART_state_bitmap = le16_to_cpu(serial_state_notification->
1515 UART_state_bitmap); 1515 UART_state_bitmap);
1516 prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap; 1516 prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
1517 icount = &tiocmget->icount; 1517 icount = &tiocmget->icount;
1518 spin_lock(&serial->serial_lock); 1518 spin_lock(&serial->serial_lock);
1519 if ((UART_state_bitmap & B_OVERRUN) != 1519 if ((UART_state_bitmap & B_OVERRUN) !=
1520 (prev_UART_state_bitmap & B_OVERRUN)) 1520 (prev_UART_state_bitmap & B_OVERRUN))
1521 icount->parity++; 1521 icount->parity++;
1522 if ((UART_state_bitmap & B_PARITY) != 1522 if ((UART_state_bitmap & B_PARITY) !=
1523 (prev_UART_state_bitmap & B_PARITY)) 1523 (prev_UART_state_bitmap & B_PARITY))
1524 icount->parity++; 1524 icount->parity++;
1525 if ((UART_state_bitmap & B_FRAMING) != 1525 if ((UART_state_bitmap & B_FRAMING) !=
1526 (prev_UART_state_bitmap & B_FRAMING)) 1526 (prev_UART_state_bitmap & B_FRAMING))
1527 icount->frame++; 1527 icount->frame++;
1528 if ((UART_state_bitmap & B_RING_SIGNAL) && 1528 if ((UART_state_bitmap & B_RING_SIGNAL) &&
1529 !(prev_UART_state_bitmap & B_RING_SIGNAL)) 1529 !(prev_UART_state_bitmap & B_RING_SIGNAL))
1530 icount->rng++; 1530 icount->rng++;
1531 if ((UART_state_bitmap & B_BREAK) != 1531 if ((UART_state_bitmap & B_BREAK) !=
1532 (prev_UART_state_bitmap & B_BREAK)) 1532 (prev_UART_state_bitmap & B_BREAK))
1533 icount->brk++; 1533 icount->brk++;
1534 if ((UART_state_bitmap & B_TX_CARRIER) != 1534 if ((UART_state_bitmap & B_TX_CARRIER) !=
1535 (prev_UART_state_bitmap & B_TX_CARRIER)) 1535 (prev_UART_state_bitmap & B_TX_CARRIER))
1536 icount->dsr++; 1536 icount->dsr++;
1537 if ((UART_state_bitmap & B_RX_CARRIER) != 1537 if ((UART_state_bitmap & B_RX_CARRIER) !=
1538 (prev_UART_state_bitmap & B_RX_CARRIER)) 1538 (prev_UART_state_bitmap & B_RX_CARRIER))
1539 icount->dcd++; 1539 icount->dcd++;
1540 tiocmget->prev_UART_state_bitmap = UART_state_bitmap; 1540 tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
1541 spin_unlock(&serial->serial_lock); 1541 spin_unlock(&serial->serial_lock);
1542 tiocmget->intr_completed = 1; 1542 tiocmget->intr_completed = 1;
1543 wake_up_interruptible(&tiocmget->waitq); 1543 wake_up_interruptible(&tiocmget->waitq);
1544 } 1544 }
1545 memset(serial_state_notification, 0, 1545 memset(serial_state_notification, 0,
1546 sizeof(struct hso_serial_state_notification)); 1546 sizeof(struct hso_serial_state_notification));
1547 tiocmget_submit_urb(serial, 1547 tiocmget_submit_urb(serial,
1548 tiocmget, 1548 tiocmget,
1549 serial->parent->usb); 1549 serial->parent->usb);
1550 } 1550 }
1551 1551
1552 /* 1552 /*
1553 * next few functions largely stolen from drivers/serial/serial_core.c 1553 * next few functions largely stolen from drivers/serial/serial_core.c
1554 */ 1554 */
1555 /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change 1555 /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1556 * - mask passed in arg for lines of interest 1556 * - mask passed in arg for lines of interest
1557 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking) 1557 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1558 * Caller should use TIOCGICOUNT to see which one it was 1558 * Caller should use TIOCGICOUNT to see which one it was
1559 */ 1559 */
1560 static int 1560 static int
1561 hso_wait_modem_status(struct hso_serial *serial, unsigned long arg) 1561 hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
1562 { 1562 {
1563 DECLARE_WAITQUEUE(wait, current); 1563 DECLARE_WAITQUEUE(wait, current);
1564 struct uart_icount cprev, cnow; 1564 struct uart_icount cprev, cnow;
1565 struct hso_tiocmget *tiocmget; 1565 struct hso_tiocmget *tiocmget;
1566 int ret; 1566 int ret;
1567 1567
1568 tiocmget = serial->tiocmget; 1568 tiocmget = serial->tiocmget;
1569 if (!tiocmget) 1569 if (!tiocmget)
1570 return -ENOENT; 1570 return -ENOENT;
1571 /* 1571 /*
1572 * note the counters on entry 1572 * note the counters on entry
1573 */ 1573 */
1574 spin_lock_irq(&serial->serial_lock); 1574 spin_lock_irq(&serial->serial_lock);
1575 memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount)); 1575 memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
1576 spin_unlock_irq(&serial->serial_lock); 1576 spin_unlock_irq(&serial->serial_lock);
1577 add_wait_queue(&tiocmget->waitq, &wait); 1577 add_wait_queue(&tiocmget->waitq, &wait);
1578 for (;;) { 1578 for (;;) {
1579 spin_lock_irq(&serial->serial_lock); 1579 spin_lock_irq(&serial->serial_lock);
1580 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount)); 1580 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1581 spin_unlock_irq(&serial->serial_lock); 1581 spin_unlock_irq(&serial->serial_lock);
1582 set_current_state(TASK_INTERRUPTIBLE); 1582 set_current_state(TASK_INTERRUPTIBLE);
1583 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 1583 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1584 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 1584 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1585 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) { 1585 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
1586 ret = 0; 1586 ret = 0;
1587 break; 1587 break;
1588 } 1588 }
1589 schedule(); 1589 schedule();
1590 /* see if a signal did it */ 1590 /* see if a signal did it */
1591 if (signal_pending(current)) { 1591 if (signal_pending(current)) {
1592 ret = -ERESTARTSYS; 1592 ret = -ERESTARTSYS;
1593 break; 1593 break;
1594 } 1594 }
1595 cprev = cnow; 1595 cprev = cnow;
1596 } 1596 }
1597 current->state = TASK_RUNNING; 1597 current->state = TASK_RUNNING;
1598 remove_wait_queue(&tiocmget->waitq, &wait); 1598 remove_wait_queue(&tiocmget->waitq, &wait);
1599 1599
1600 return ret; 1600 return ret;
1601 } 1601 }
1602 1602
1603 /* 1603 /*
1604 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS) 1604 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1605 * Return: write counters to the user passed counter struct 1605 * Return: write counters to the user passed counter struct
1606 * NB: both 1->0 and 0->1 transitions are counted except for 1606 * NB: both 1->0 and 0->1 transitions are counted except for
1607 * RI where only 0->1 is counted. 1607 * RI where only 0->1 is counted.
1608 */ 1608 */
1609 static int hso_get_count(struct tty_struct *tty, 1609 static int hso_get_count(struct tty_struct *tty,
1610 struct serial_icounter_struct *icount) 1610 struct serial_icounter_struct *icount)
1611 { 1611 {
1612 struct uart_icount cnow; 1612 struct uart_icount cnow;
1613 struct hso_serial *serial = tty->driver_data; 1613 struct hso_serial *serial = tty->driver_data;
1614 struct hso_tiocmget *tiocmget = serial->tiocmget; 1614 struct hso_tiocmget *tiocmget = serial->tiocmget;
1615 1615
1616 memset(icount, 0, sizeof(struct serial_icounter_struct)); 1616 memset(icount, 0, sizeof(struct serial_icounter_struct));
1617 1617
1618 if (!tiocmget) 1618 if (!tiocmget)
1619 return -ENOENT; 1619 return -ENOENT;
1620 spin_lock_irq(&serial->serial_lock); 1620 spin_lock_irq(&serial->serial_lock);
1621 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount)); 1621 memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
1622 spin_unlock_irq(&serial->serial_lock); 1622 spin_unlock_irq(&serial->serial_lock);
1623 1623
1624 icount->cts = cnow.cts; 1624 icount->cts = cnow.cts;
1625 icount->dsr = cnow.dsr; 1625 icount->dsr = cnow.dsr;
1626 icount->rng = cnow.rng; 1626 icount->rng = cnow.rng;
1627 icount->dcd = cnow.dcd; 1627 icount->dcd = cnow.dcd;
1628 icount->rx = cnow.rx; 1628 icount->rx = cnow.rx;
1629 icount->tx = cnow.tx; 1629 icount->tx = cnow.tx;
1630 icount->frame = cnow.frame; 1630 icount->frame = cnow.frame;
1631 icount->overrun = cnow.overrun; 1631 icount->overrun = cnow.overrun;
1632 icount->parity = cnow.parity; 1632 icount->parity = cnow.parity;
1633 icount->brk = cnow.brk; 1633 icount->brk = cnow.brk;
1634 icount->buf_overrun = cnow.buf_overrun; 1634 icount->buf_overrun = cnow.buf_overrun;
1635 1635
1636 return 0; 1636 return 0;
1637 } 1637 }
1638 1638
1639 1639
1640 static int hso_serial_tiocmget(struct tty_struct *tty) 1640 static int hso_serial_tiocmget(struct tty_struct *tty)
1641 { 1641 {
1642 int retval; 1642 int retval;
1643 struct hso_serial *serial = tty->driver_data; 1643 struct hso_serial *serial = tty->driver_data;
1644 struct hso_tiocmget *tiocmget; 1644 struct hso_tiocmget *tiocmget;
1645 u16 UART_state_bitmap; 1645 u16 UART_state_bitmap;
1646 1646
1647 /* sanity check */ 1647 /* sanity check */
1648 if (!serial) { 1648 if (!serial) {
1649 D1("no tty structures"); 1649 D1("no tty structures");
1650 return -EINVAL; 1650 return -EINVAL;
1651 } 1651 }
1652 spin_lock_irq(&serial->serial_lock); 1652 spin_lock_irq(&serial->serial_lock);
1653 retval = ((serial->rts_state) ? TIOCM_RTS : 0) | 1653 retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
1654 ((serial->dtr_state) ? TIOCM_DTR : 0); 1654 ((serial->dtr_state) ? TIOCM_DTR : 0);
1655 tiocmget = serial->tiocmget; 1655 tiocmget = serial->tiocmget;
1656 if (tiocmget) { 1656 if (tiocmget) {
1657 1657
1658 UART_state_bitmap = le16_to_cpu( 1658 UART_state_bitmap = le16_to_cpu(
1659 tiocmget->prev_UART_state_bitmap); 1659 tiocmget->prev_UART_state_bitmap);
1660 if (UART_state_bitmap & B_RING_SIGNAL) 1660 if (UART_state_bitmap & B_RING_SIGNAL)
1661 retval |= TIOCM_RNG; 1661 retval |= TIOCM_RNG;
1662 if (UART_state_bitmap & B_RX_CARRIER) 1662 if (UART_state_bitmap & B_RX_CARRIER)
1663 retval |= TIOCM_CD; 1663 retval |= TIOCM_CD;
1664 if (UART_state_bitmap & B_TX_CARRIER) 1664 if (UART_state_bitmap & B_TX_CARRIER)
1665 retval |= TIOCM_DSR; 1665 retval |= TIOCM_DSR;
1666 } 1666 }
1667 spin_unlock_irq(&serial->serial_lock); 1667 spin_unlock_irq(&serial->serial_lock);
1668 return retval; 1668 return retval;
1669 } 1669 }
1670 1670
1671 static int hso_serial_tiocmset(struct tty_struct *tty, 1671 static int hso_serial_tiocmset(struct tty_struct *tty,
1672 unsigned int set, unsigned int clear) 1672 unsigned int set, unsigned int clear)
1673 { 1673 {
1674 int val = 0; 1674 int val = 0;
1675 unsigned long flags; 1675 unsigned long flags;
1676 int if_num; 1676 int if_num;
1677 struct hso_serial *serial = tty->driver_data; 1677 struct hso_serial *serial = tty->driver_data;
1678 1678
1679 /* sanity check */ 1679 /* sanity check */
1680 if (!serial) { 1680 if (!serial) {
1681 D1("no tty structures"); 1681 D1("no tty structures");
1682 return -EINVAL; 1682 return -EINVAL;
1683 } 1683 }
1684 1684
1685 if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM) 1685 if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
1686 return -EINVAL; 1686 return -EINVAL;
1687 1687
1688 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber; 1688 if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
1689 1689
1690 spin_lock_irqsave(&serial->serial_lock, flags); 1690 spin_lock_irqsave(&serial->serial_lock, flags);
1691 if (set & TIOCM_RTS) 1691 if (set & TIOCM_RTS)
1692 serial->rts_state = 1; 1692 serial->rts_state = 1;
1693 if (set & TIOCM_DTR) 1693 if (set & TIOCM_DTR)
1694 serial->dtr_state = 1; 1694 serial->dtr_state = 1;
1695 1695
1696 if (clear & TIOCM_RTS) 1696 if (clear & TIOCM_RTS)
1697 serial->rts_state = 0; 1697 serial->rts_state = 0;
1698 if (clear & TIOCM_DTR) 1698 if (clear & TIOCM_DTR)
1699 serial->dtr_state = 0; 1699 serial->dtr_state = 0;
1700 1700
1701 if (serial->dtr_state) 1701 if (serial->dtr_state)
1702 val |= 0x01; 1702 val |= 0x01;
1703 if (serial->rts_state) 1703 if (serial->rts_state)
1704 val |= 0x02; 1704 val |= 0x02;
1705 1705
1706 spin_unlock_irqrestore(&serial->serial_lock, flags); 1706 spin_unlock_irqrestore(&serial->serial_lock, flags);
1707 1707
1708 return usb_control_msg(serial->parent->usb, 1708 return usb_control_msg(serial->parent->usb,
1709 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22, 1709 usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
1710 0x21, val, if_num, NULL, 0, 1710 0x21, val, if_num, NULL, 0,
1711 USB_CTRL_SET_TIMEOUT); 1711 USB_CTRL_SET_TIMEOUT);
1712 } 1712 }
1713 1713
1714 static int hso_serial_ioctl(struct tty_struct *tty, 1714 static int hso_serial_ioctl(struct tty_struct *tty,
1715 unsigned int cmd, unsigned long arg) 1715 unsigned int cmd, unsigned long arg)
1716 { 1716 {
1717 struct hso_serial *serial = tty->driver_data; 1717 struct hso_serial *serial = tty->driver_data;
1718 int ret = 0; 1718 int ret = 0;
1719 D4("IOCTL cmd: %d, arg: %ld", cmd, arg); 1719 D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
1720 1720
1721 if (!serial) 1721 if (!serial)
1722 return -ENODEV; 1722 return -ENODEV;
1723 switch (cmd) { 1723 switch (cmd) {
1724 case TIOCMIWAIT: 1724 case TIOCMIWAIT:
1725 ret = hso_wait_modem_status(serial, arg); 1725 ret = hso_wait_modem_status(serial, arg);
1726 break; 1726 break;
1727 default: 1727 default:
1728 ret = -ENOIOCTLCMD; 1728 ret = -ENOIOCTLCMD;
1729 break; 1729 break;
1730 } 1730 }
1731 return ret; 1731 return ret;
1732 } 1732 }
1733 1733
1734 1734
1735 /* starts a transmit */ 1735 /* starts a transmit */
1736 static void hso_kick_transmit(struct hso_serial *serial) 1736 static void hso_kick_transmit(struct hso_serial *serial)
1737 { 1737 {
1738 u8 *temp; 1738 u8 *temp;
1739 unsigned long flags; 1739 unsigned long flags;
1740 int res; 1740 int res;
1741 1741
1742 spin_lock_irqsave(&serial->serial_lock, flags); 1742 spin_lock_irqsave(&serial->serial_lock, flags);
1743 if (!serial->tx_buffer_count) 1743 if (!serial->tx_buffer_count)
1744 goto out; 1744 goto out;
1745 1745
1746 if (serial->tx_urb_used) 1746 if (serial->tx_urb_used)
1747 goto out; 1747 goto out;
1748 1748
1749 /* Wakeup USB interface if necessary */ 1749 /* Wakeup USB interface if necessary */
1750 if (hso_get_activity(serial->parent) == -EAGAIN) 1750 if (hso_get_activity(serial->parent) == -EAGAIN)
1751 goto out; 1751 goto out;
1752 1752
1753 /* Switch pointers around to avoid memcpy */ 1753 /* Switch pointers around to avoid memcpy */
1754 temp = serial->tx_buffer; 1754 temp = serial->tx_buffer;
1755 serial->tx_buffer = serial->tx_data; 1755 serial->tx_buffer = serial->tx_data;
1756 serial->tx_data = temp; 1756 serial->tx_data = temp;
1757 serial->tx_data_count = serial->tx_buffer_count; 1757 serial->tx_data_count = serial->tx_buffer_count;
1758 serial->tx_buffer_count = 0; 1758 serial->tx_buffer_count = 0;
1759 1759
1760 /* If temp is set, it means we switched buffers */ 1760 /* If temp is set, it means we switched buffers */
1761 if (temp && serial->write_data) { 1761 if (temp && serial->write_data) {
1762 res = serial->write_data(serial); 1762 res = serial->write_data(serial);
1763 if (res >= 0) 1763 if (res >= 0)
1764 serial->tx_urb_used = 1; 1764 serial->tx_urb_used = 1;
1765 } 1765 }
1766 out: 1766 out:
1767 spin_unlock_irqrestore(&serial->serial_lock, flags); 1767 spin_unlock_irqrestore(&serial->serial_lock, flags);
1768 } 1768 }
1769 1769
1770 /* make a request (for reading and writing data to muxed serial port) */ 1770 /* make a request (for reading and writing data to muxed serial port) */
1771 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port, 1771 static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
1772 struct urb *ctrl_urb, 1772 struct urb *ctrl_urb,
1773 struct usb_ctrlrequest *ctrl_req, 1773 struct usb_ctrlrequest *ctrl_req,
1774 u8 *ctrl_urb_data, u32 size) 1774 u8 *ctrl_urb_data, u32 size)
1775 { 1775 {
1776 int result; 1776 int result;
1777 int pipe; 1777 int pipe;
1778 1778
1779 /* Sanity check */ 1779 /* Sanity check */
1780 if (!serial || !ctrl_urb || !ctrl_req) { 1780 if (!serial || !ctrl_urb || !ctrl_req) {
1781 printk(KERN_ERR "%s: Wrong arguments\n", __func__); 1781 printk(KERN_ERR "%s: Wrong arguments\n", __func__);
1782 return -EINVAL; 1782 return -EINVAL;
1783 } 1783 }
1784 1784
1785 /* initialize */ 1785 /* initialize */
1786 ctrl_req->wValue = 0; 1786 ctrl_req->wValue = 0;
1787 ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port)); 1787 ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
1788 ctrl_req->wLength = cpu_to_le16(size); 1788 ctrl_req->wLength = cpu_to_le16(size);
1789 1789
1790 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) { 1790 if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
1791 /* Reading command */ 1791 /* Reading command */
1792 ctrl_req->bRequestType = USB_DIR_IN | 1792 ctrl_req->bRequestType = USB_DIR_IN |
1793 USB_TYPE_OPTION_VENDOR | 1793 USB_TYPE_OPTION_VENDOR |
1794 USB_RECIP_INTERFACE; 1794 USB_RECIP_INTERFACE;
1795 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE; 1795 ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
1796 pipe = usb_rcvctrlpipe(serial->parent->usb, 0); 1796 pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
1797 } else { 1797 } else {
1798 /* Writing command */ 1798 /* Writing command */
1799 ctrl_req->bRequestType = USB_DIR_OUT | 1799 ctrl_req->bRequestType = USB_DIR_OUT |
1800 USB_TYPE_OPTION_VENDOR | 1800 USB_TYPE_OPTION_VENDOR |
1801 USB_RECIP_INTERFACE; 1801 USB_RECIP_INTERFACE;
1802 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND; 1802 ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
1803 pipe = usb_sndctrlpipe(serial->parent->usb, 0); 1803 pipe = usb_sndctrlpipe(serial->parent->usb, 0);
1804 } 1804 }
1805 /* syslog */ 1805 /* syslog */
1806 D2("%s command (%02x) len: %d, port: %d", 1806 D2("%s command (%02x) len: %d, port: %d",
1807 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write", 1807 type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
1808 ctrl_req->bRequestType, ctrl_req->wLength, port); 1808 ctrl_req->bRequestType, ctrl_req->wLength, port);
1809 1809
1810 /* Load ctrl urb */ 1810 /* Load ctrl urb */
1811 ctrl_urb->transfer_flags = 0; 1811 ctrl_urb->transfer_flags = 0;
1812 usb_fill_control_urb(ctrl_urb, 1812 usb_fill_control_urb(ctrl_urb,
1813 serial->parent->usb, 1813 serial->parent->usb,
1814 pipe, 1814 pipe,
1815 (u8 *) ctrl_req, 1815 (u8 *) ctrl_req,
1816 ctrl_urb_data, size, ctrl_callback, serial); 1816 ctrl_urb_data, size, ctrl_callback, serial);
1817 /* Send it on merry way */ 1817 /* Send it on merry way */
1818 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC); 1818 result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
1819 if (result) { 1819 if (result) {
1820 dev_err(&ctrl_urb->dev->dev, 1820 dev_err(&ctrl_urb->dev->dev,
1821 "%s failed submit ctrl_urb %d type %d\n", __func__, 1821 "%s failed submit ctrl_urb %d type %d\n", __func__,
1822 result, type); 1822 result, type);
1823 return result; 1823 return result;
1824 } 1824 }
1825 1825
1826 /* done */ 1826 /* done */
1827 return size; 1827 return size;
1828 } 1828 }
1829 1829
1830 /* called by intr_callback when read occurs */ 1830 /* called by intr_callback when read occurs */
1831 static int hso_mux_serial_read(struct hso_serial *serial) 1831 static int hso_mux_serial_read(struct hso_serial *serial)
1832 { 1832 {
1833 if (!serial) 1833 if (!serial)
1834 return -EINVAL; 1834 return -EINVAL;
1835 1835
1836 /* clean data */ 1836 /* clean data */
1837 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE); 1837 memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
1838 /* make the request */ 1838 /* make the request */
1839 1839
1840 if (serial->num_rx_urbs != 1) { 1840 if (serial->num_rx_urbs != 1) {
1841 dev_err(&serial->parent->interface->dev, 1841 dev_err(&serial->parent->interface->dev,
1842 "ERROR: mux'd reads with multiple buffers " 1842 "ERROR: mux'd reads with multiple buffers "
1843 "not possible\n"); 1843 "not possible\n");
1844 return 0; 1844 return 0;
1845 } 1845 }
1846 return mux_device_request(serial, 1846 return mux_device_request(serial,
1847 USB_CDC_GET_ENCAPSULATED_RESPONSE, 1847 USB_CDC_GET_ENCAPSULATED_RESPONSE,
1848 serial->parent->port_spec & HSO_PORT_MASK, 1848 serial->parent->port_spec & HSO_PORT_MASK,
1849 serial->rx_urb[0], 1849 serial->rx_urb[0],
1850 &serial->ctrl_req_rx, 1850 &serial->ctrl_req_rx,
1851 serial->rx_data[0], serial->rx_data_length); 1851 serial->rx_data[0], serial->rx_data_length);
1852 } 1852 }
1853 1853
1854 /* used for muxed serial port callback (muxed serial read) */ 1854 /* used for muxed serial port callback (muxed serial read) */
1855 static void intr_callback(struct urb *urb) 1855 static void intr_callback(struct urb *urb)
1856 { 1856 {
1857 struct hso_shared_int *shared_int = urb->context; 1857 struct hso_shared_int *shared_int = urb->context;
1858 struct hso_serial *serial; 1858 struct hso_serial *serial;
1859 unsigned char *port_req; 1859 unsigned char *port_req;
1860 int status = urb->status; 1860 int status = urb->status;
1861 int i; 1861 int i;
1862 1862
1863 usb_mark_last_busy(urb->dev); 1863 usb_mark_last_busy(urb->dev);
1864 1864
1865 /* sanity check */ 1865 /* sanity check */
1866 if (!shared_int) 1866 if (!shared_int)
1867 return; 1867 return;
1868 1868
1869 /* status check */ 1869 /* status check */
1870 if (status) { 1870 if (status) {
1871 handle_usb_error(status, __func__, NULL); 1871 handle_usb_error(status, __func__, NULL);
1872 return; 1872 return;
1873 } 1873 }
1874 D4("\n--- Got intr callback 0x%02X ---", status); 1874 D4("\n--- Got intr callback 0x%02X ---", status);
1875 1875
1876 /* what request? */ 1876 /* what request? */
1877 port_req = urb->transfer_buffer; 1877 port_req = urb->transfer_buffer;
1878 D4(" port_req = 0x%.2X\n", *port_req); 1878 D4(" port_req = 0x%.2X\n", *port_req);
1879 /* loop over all muxed ports to find the one sending this */ 1879 /* loop over all muxed ports to find the one sending this */
1880 for (i = 0; i < 8; i++) { 1880 for (i = 0; i < 8; i++) {
1881 /* max 8 channels on MUX */ 1881 /* max 8 channels on MUX */
1882 if (*port_req & (1 << i)) { 1882 if (*port_req & (1 << i)) {
1883 serial = get_serial_by_shared_int_and_type(shared_int, 1883 serial = get_serial_by_shared_int_and_type(shared_int,
1884 (1 << i)); 1884 (1 << i));
1885 if (serial != NULL) { 1885 if (serial != NULL) {
1886 D1("Pending read interrupt on port %d\n", i); 1886 D1("Pending read interrupt on port %d\n", i);
1887 spin_lock(&serial->serial_lock); 1887 spin_lock(&serial->serial_lock);
1888 if (serial->rx_state == RX_IDLE && 1888 if (serial->rx_state == RX_IDLE &&
1889 serial->port.count > 0) { 1889 serial->port.count > 0) {
1890 /* Setup and send a ctrl req read on 1890 /* Setup and send a ctrl req read on
1891 * port i */ 1891 * port i */
1892 if (!serial->rx_urb_filled[0]) { 1892 if (!serial->rx_urb_filled[0]) {
1893 serial->rx_state = RX_SENT; 1893 serial->rx_state = RX_SENT;
1894 hso_mux_serial_read(serial); 1894 hso_mux_serial_read(serial);
1895 } else 1895 } else
1896 serial->rx_state = RX_PENDING; 1896 serial->rx_state = RX_PENDING;
1897 } else { 1897 } else {
1898 D1("Already a read pending on " 1898 D1("Already a read pending on "
1899 "port %d or port not open\n", i); 1899 "port %d or port not open\n", i);
1900 } 1900 }
1901 spin_unlock(&serial->serial_lock); 1901 spin_unlock(&serial->serial_lock);
1902 } 1902 }
1903 } 1903 }
1904 } 1904 }
1905 /* Resubmit interrupt urb */ 1905 /* Resubmit interrupt urb */
1906 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC); 1906 hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
1907 } 1907 }
1908 1908
1909 /* called for writing to muxed serial port */ 1909 /* called for writing to muxed serial port */
1910 static int hso_mux_serial_write_data(struct hso_serial *serial) 1910 static int hso_mux_serial_write_data(struct hso_serial *serial)
1911 { 1911 {
1912 if (NULL == serial) 1912 if (NULL == serial)
1913 return -EINVAL; 1913 return -EINVAL;
1914 1914
1915 return mux_device_request(serial, 1915 return mux_device_request(serial,
1916 USB_CDC_SEND_ENCAPSULATED_COMMAND, 1916 USB_CDC_SEND_ENCAPSULATED_COMMAND,
1917 serial->parent->port_spec & HSO_PORT_MASK, 1917 serial->parent->port_spec & HSO_PORT_MASK,
1918 serial->tx_urb, 1918 serial->tx_urb,
1919 &serial->ctrl_req_tx, 1919 &serial->ctrl_req_tx,
1920 serial->tx_data, serial->tx_data_count); 1920 serial->tx_data, serial->tx_data_count);
1921 } 1921 }
1922 1922
1923 /* write callback for Diag and CS port */ 1923 /* write callback for Diag and CS port */
1924 static void hso_std_serial_write_bulk_callback(struct urb *urb) 1924 static void hso_std_serial_write_bulk_callback(struct urb *urb)
1925 { 1925 {
1926 struct hso_serial *serial = urb->context; 1926 struct hso_serial *serial = urb->context;
1927 int status = urb->status; 1927 int status = urb->status;
1928 struct tty_struct *tty;
1929 1928
1930 /* sanity check */ 1929 /* sanity check */
1931 if (!serial) { 1930 if (!serial) {
1932 D1("serial == NULL"); 1931 D1("serial == NULL");
1933 return; 1932 return;
1934 } 1933 }
1935 1934
1936 spin_lock(&serial->serial_lock); 1935 spin_lock(&serial->serial_lock);
1937 serial->tx_urb_used = 0; 1936 serial->tx_urb_used = 0;
1938 spin_unlock(&serial->serial_lock); 1937 spin_unlock(&serial->serial_lock);
1939 if (status) { 1938 if (status) {
1940 handle_usb_error(status, __func__, serial->parent); 1939 handle_usb_error(status, __func__, serial->parent);
1941 return; 1940 return;
1942 } 1941 }
1943 hso_put_activity(serial->parent); 1942 hso_put_activity(serial->parent);
1944 tty = tty_port_tty_get(&serial->port); 1943 tty_port_tty_wakeup(&serial->port);
1945 if (tty) {
1946 tty_wakeup(tty);
1947 tty_kref_put(tty);
1948 }
1949 hso_kick_transmit(serial); 1944 hso_kick_transmit(serial);
1950 1945
1951 D1(" "); 1946 D1(" ");
1952 } 1947 }
1953 1948
1954 /* called for writing diag or CS serial port */ 1949 /* called for writing diag or CS serial port */
1955 static int hso_std_serial_write_data(struct hso_serial *serial) 1950 static int hso_std_serial_write_data(struct hso_serial *serial)
1956 { 1951 {
1957 int count = serial->tx_data_count; 1952 int count = serial->tx_data_count;
1958 int result; 1953 int result;
1959 1954
1960 usb_fill_bulk_urb(serial->tx_urb, 1955 usb_fill_bulk_urb(serial->tx_urb,
1961 serial->parent->usb, 1956 serial->parent->usb,
1962 usb_sndbulkpipe(serial->parent->usb, 1957 usb_sndbulkpipe(serial->parent->usb,
1963 serial->out_endp-> 1958 serial->out_endp->
1964 bEndpointAddress & 0x7F), 1959 bEndpointAddress & 0x7F),
1965 serial->tx_data, serial->tx_data_count, 1960 serial->tx_data, serial->tx_data_count,
1966 hso_std_serial_write_bulk_callback, serial); 1961 hso_std_serial_write_bulk_callback, serial);
1967 1962
1968 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC); 1963 result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
1969 if (result) { 1964 if (result) {
1970 dev_warn(&serial->parent->usb->dev, 1965 dev_warn(&serial->parent->usb->dev,
1971 "Failed to submit urb - res %d\n", result); 1966 "Failed to submit urb - res %d\n", result);
1972 return result; 1967 return result;
1973 } 1968 }
1974 1969
1975 return count; 1970 return count;
1976 } 1971 }
1977 1972
1978 /* callback after read or write on muxed serial port */ 1973 /* callback after read or write on muxed serial port */
1979 static void ctrl_callback(struct urb *urb) 1974 static void ctrl_callback(struct urb *urb)
1980 { 1975 {
1981 struct hso_serial *serial = urb->context; 1976 struct hso_serial *serial = urb->context;
1982 struct usb_ctrlrequest *req; 1977 struct usb_ctrlrequest *req;
1983 int status = urb->status; 1978 int status = urb->status;
1984 1979
1985 /* sanity check */ 1980 /* sanity check */
1986 if (!serial) 1981 if (!serial)
1987 return; 1982 return;
1988 1983
1989 spin_lock(&serial->serial_lock); 1984 spin_lock(&serial->serial_lock);
1990 serial->tx_urb_used = 0; 1985 serial->tx_urb_used = 0;
1991 spin_unlock(&serial->serial_lock); 1986 spin_unlock(&serial->serial_lock);
1992 if (status) { 1987 if (status) {
1993 handle_usb_error(status, __func__, serial->parent); 1988 handle_usb_error(status, __func__, serial->parent);
1994 return; 1989 return;
1995 } 1990 }
1996 1991
1997 /* what request? */ 1992 /* what request? */
1998 req = (struct usb_ctrlrequest *)(urb->setup_packet); 1993 req = (struct usb_ctrlrequest *)(urb->setup_packet);
1999 D4("\n--- Got muxed ctrl callback 0x%02X ---", status); 1994 D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
2000 D4("Actual length of urb = %d\n", urb->actual_length); 1995 D4("Actual length of urb = %d\n", urb->actual_length);
2001 DUMP1(urb->transfer_buffer, urb->actual_length); 1996 DUMP1(urb->transfer_buffer, urb->actual_length);
2002 1997
2003 if (req->bRequestType == 1998 if (req->bRequestType ==
2004 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) { 1999 (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
2005 /* response to a read command */ 2000 /* response to a read command */
2006 serial->rx_urb_filled[0] = 1; 2001 serial->rx_urb_filled[0] = 1;
2007 spin_lock(&serial->serial_lock); 2002 spin_lock(&serial->serial_lock);
2008 put_rxbuf_data_and_resubmit_ctrl_urb(serial); 2003 put_rxbuf_data_and_resubmit_ctrl_urb(serial);
2009 spin_unlock(&serial->serial_lock); 2004 spin_unlock(&serial->serial_lock);
2010 } else { 2005 } else {
2011 struct tty_struct *tty = tty_port_tty_get(&serial->port);
2012 hso_put_activity(serial->parent); 2006 hso_put_activity(serial->parent);
2013 if (tty) { 2007 tty_port_tty_wakeup(&serial->port);
2014 tty_wakeup(tty);
2015 tty_kref_put(tty);
2016 }
2017 /* response to a write command */ 2008 /* response to a write command */
2018 hso_kick_transmit(serial); 2009 hso_kick_transmit(serial);
2019 } 2010 }
2020 } 2011 }
2021 2012
2022 /* handle RX data for serial port */ 2013 /* handle RX data for serial port */
2023 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial) 2014 static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
2024 { 2015 {
2025 struct tty_struct *tty; 2016 struct tty_struct *tty;
2026 int write_length_remaining = 0; 2017 int write_length_remaining = 0;
2027 int curr_write_len; 2018 int curr_write_len;
2028 2019
2029 /* Sanity check */ 2020 /* Sanity check */
2030 if (urb == NULL || serial == NULL) { 2021 if (urb == NULL || serial == NULL) {
2031 D1("serial = NULL"); 2022 D1("serial = NULL");
2032 return -2; 2023 return -2;
2033 } 2024 }
2034 2025
2035 tty = tty_port_tty_get(&serial->port); 2026 tty = tty_port_tty_get(&serial->port);
2036 2027
2037 /* Push data to tty */ 2028 /* Push data to tty */
2038 write_length_remaining = urb->actual_length - 2029 write_length_remaining = urb->actual_length -
2039 serial->curr_rx_urb_offset; 2030 serial->curr_rx_urb_offset;
2040 D1("data to push to tty"); 2031 D1("data to push to tty");
2041 while (write_length_remaining) { 2032 while (write_length_remaining) {
2042 if (tty && test_bit(TTY_THROTTLED, &tty->flags)) { 2033 if (tty && test_bit(TTY_THROTTLED, &tty->flags)) {
2043 tty_kref_put(tty); 2034 tty_kref_put(tty);
2044 return -1; 2035 return -1;
2045 } 2036 }
2046 curr_write_len = tty_insert_flip_string(&serial->port, 2037 curr_write_len = tty_insert_flip_string(&serial->port,
2047 urb->transfer_buffer + serial->curr_rx_urb_offset, 2038 urb->transfer_buffer + serial->curr_rx_urb_offset,
2048 write_length_remaining); 2039 write_length_remaining);
2049 serial->curr_rx_urb_offset += curr_write_len; 2040 serial->curr_rx_urb_offset += curr_write_len;
2050 write_length_remaining -= curr_write_len; 2041 write_length_remaining -= curr_write_len;
2051 tty_flip_buffer_push(&serial->port); 2042 tty_flip_buffer_push(&serial->port);
2052 } 2043 }
2053 tty_kref_put(tty); 2044 tty_kref_put(tty);
2054 2045
2055 if (write_length_remaining == 0) { 2046 if (write_length_remaining == 0) {
2056 serial->curr_rx_urb_offset = 0; 2047 serial->curr_rx_urb_offset = 0;
2057 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0; 2048 serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
2058 } 2049 }
2059 return write_length_remaining; 2050 return write_length_remaining;
2060 } 2051 }
2061 2052
2062 2053
2063 /* Base driver functions */ 2054 /* Base driver functions */
2064 2055
2065 static void hso_log_port(struct hso_device *hso_dev) 2056 static void hso_log_port(struct hso_device *hso_dev)
2066 { 2057 {
2067 char *port_type; 2058 char *port_type;
2068 char port_dev[20]; 2059 char port_dev[20];
2069 2060
2070 switch (hso_dev->port_spec & HSO_PORT_MASK) { 2061 switch (hso_dev->port_spec & HSO_PORT_MASK) {
2071 case HSO_PORT_CONTROL: 2062 case HSO_PORT_CONTROL:
2072 port_type = "Control"; 2063 port_type = "Control";
2073 break; 2064 break;
2074 case HSO_PORT_APP: 2065 case HSO_PORT_APP:
2075 port_type = "Application"; 2066 port_type = "Application";
2076 break; 2067 break;
2077 case HSO_PORT_GPS: 2068 case HSO_PORT_GPS:
2078 port_type = "GPS"; 2069 port_type = "GPS";
2079 break; 2070 break;
2080 case HSO_PORT_GPS_CONTROL: 2071 case HSO_PORT_GPS_CONTROL:
2081 port_type = "GPS control"; 2072 port_type = "GPS control";
2082 break; 2073 break;
2083 case HSO_PORT_APP2: 2074 case HSO_PORT_APP2:
2084 port_type = "Application2"; 2075 port_type = "Application2";
2085 break; 2076 break;
2086 case HSO_PORT_PCSC: 2077 case HSO_PORT_PCSC:
2087 port_type = "PCSC"; 2078 port_type = "PCSC";
2088 break; 2079 break;
2089 case HSO_PORT_DIAG: 2080 case HSO_PORT_DIAG:
2090 port_type = "Diagnostic"; 2081 port_type = "Diagnostic";
2091 break; 2082 break;
2092 case HSO_PORT_DIAG2: 2083 case HSO_PORT_DIAG2:
2093 port_type = "Diagnostic2"; 2084 port_type = "Diagnostic2";
2094 break; 2085 break;
2095 case HSO_PORT_MODEM: 2086 case HSO_PORT_MODEM:
2096 port_type = "Modem"; 2087 port_type = "Modem";
2097 break; 2088 break;
2098 case HSO_PORT_NETWORK: 2089 case HSO_PORT_NETWORK:
2099 port_type = "Network"; 2090 port_type = "Network";
2100 break; 2091 break;
2101 default: 2092 default:
2102 port_type = "Unknown"; 2093 port_type = "Unknown";
2103 break; 2094 break;
2104 } 2095 }
2105 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) { 2096 if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2106 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name); 2097 sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
2107 } else 2098 } else
2108 sprintf(port_dev, "/dev/%s%d", tty_filename, 2099 sprintf(port_dev, "/dev/%s%d", tty_filename,
2109 dev2ser(hso_dev)->minor); 2100 dev2ser(hso_dev)->minor);
2110 2101
2111 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n", 2102 dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
2112 port_type, port_dev); 2103 port_type, port_dev);
2113 } 2104 }
2114 2105
2115 static int hso_start_net_device(struct hso_device *hso_dev) 2106 static int hso_start_net_device(struct hso_device *hso_dev)
2116 { 2107 {
2117 int i, result = 0; 2108 int i, result = 0;
2118 struct hso_net *hso_net = dev2net(hso_dev); 2109 struct hso_net *hso_net = dev2net(hso_dev);
2119 2110
2120 if (!hso_net) 2111 if (!hso_net)
2121 return -ENODEV; 2112 return -ENODEV;
2122 2113
2123 /* send URBs for all read buffers */ 2114 /* send URBs for all read buffers */
2124 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) { 2115 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2125 2116
2126 /* Prep a receive URB */ 2117 /* Prep a receive URB */
2127 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i], 2118 usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
2128 hso_dev->usb, 2119 hso_dev->usb,
2129 usb_rcvbulkpipe(hso_dev->usb, 2120 usb_rcvbulkpipe(hso_dev->usb,
2130 hso_net->in_endp-> 2121 hso_net->in_endp->
2131 bEndpointAddress & 0x7F), 2122 bEndpointAddress & 0x7F),
2132 hso_net->mux_bulk_rx_buf_pool[i], 2123 hso_net->mux_bulk_rx_buf_pool[i],
2133 MUX_BULK_RX_BUF_SIZE, read_bulk_callback, 2124 MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
2134 hso_net); 2125 hso_net);
2135 2126
2136 /* Put it out there so the device can send us stuff */ 2127 /* Put it out there so the device can send us stuff */
2137 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i], 2128 result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
2138 GFP_NOIO); 2129 GFP_NOIO);
2139 if (result) 2130 if (result)
2140 dev_warn(&hso_dev->usb->dev, 2131 dev_warn(&hso_dev->usb->dev,
2141 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__, 2132 "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
2142 i, result); 2133 i, result);
2143 } 2134 }
2144 2135
2145 return result; 2136 return result;
2146 } 2137 }
2147 2138
2148 static int hso_stop_net_device(struct hso_device *hso_dev) 2139 static int hso_stop_net_device(struct hso_device *hso_dev)
2149 { 2140 {
2150 int i; 2141 int i;
2151 struct hso_net *hso_net = dev2net(hso_dev); 2142 struct hso_net *hso_net = dev2net(hso_dev);
2152 2143
2153 if (!hso_net) 2144 if (!hso_net)
2154 return -ENODEV; 2145 return -ENODEV;
2155 2146
2156 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) { 2147 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2157 if (hso_net->mux_bulk_rx_urb_pool[i]) 2148 if (hso_net->mux_bulk_rx_urb_pool[i])
2158 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]); 2149 usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2159 2150
2160 } 2151 }
2161 if (hso_net->mux_bulk_tx_urb) 2152 if (hso_net->mux_bulk_tx_urb)
2162 usb_kill_urb(hso_net->mux_bulk_tx_urb); 2153 usb_kill_urb(hso_net->mux_bulk_tx_urb);
2163 2154
2164 return 0; 2155 return 0;
2165 } 2156 }
2166 2157
2167 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags) 2158 static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
2168 { 2159 {
2169 int i, result = 0; 2160 int i, result = 0;
2170 struct hso_serial *serial = dev2ser(hso_dev); 2161 struct hso_serial *serial = dev2ser(hso_dev);
2171 2162
2172 if (!serial) 2163 if (!serial)
2173 return -ENODEV; 2164 return -ENODEV;
2174 2165
2175 /* If it is not the MUX port fill in and submit a bulk urb (already 2166 /* If it is not the MUX port fill in and submit a bulk urb (already
2176 * allocated in hso_serial_start) */ 2167 * allocated in hso_serial_start) */
2177 if (!(serial->parent->port_spec & HSO_INTF_MUX)) { 2168 if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
2178 for (i = 0; i < serial->num_rx_urbs; i++) { 2169 for (i = 0; i < serial->num_rx_urbs; i++) {
2179 usb_fill_bulk_urb(serial->rx_urb[i], 2170 usb_fill_bulk_urb(serial->rx_urb[i],
2180 serial->parent->usb, 2171 serial->parent->usb,
2181 usb_rcvbulkpipe(serial->parent->usb, 2172 usb_rcvbulkpipe(serial->parent->usb,
2182 serial->in_endp-> 2173 serial->in_endp->
2183 bEndpointAddress & 2174 bEndpointAddress &
2184 0x7F), 2175 0x7F),
2185 serial->rx_data[i], 2176 serial->rx_data[i],
2186 serial->rx_data_length, 2177 serial->rx_data_length,
2187 hso_std_serial_read_bulk_callback, 2178 hso_std_serial_read_bulk_callback,
2188 serial); 2179 serial);
2189 result = usb_submit_urb(serial->rx_urb[i], flags); 2180 result = usb_submit_urb(serial->rx_urb[i], flags);
2190 if (result) { 2181 if (result) {
2191 dev_warn(&serial->parent->usb->dev, 2182 dev_warn(&serial->parent->usb->dev,
2192 "Failed to submit urb - res %d\n", 2183 "Failed to submit urb - res %d\n",
2193 result); 2184 result);
2194 break; 2185 break;
2195 } 2186 }
2196 } 2187 }
2197 } else { 2188 } else {
2198 mutex_lock(&serial->shared_int->shared_int_lock); 2189 mutex_lock(&serial->shared_int->shared_int_lock);
2199 if (!serial->shared_int->use_count) { 2190 if (!serial->shared_int->use_count) {
2200 result = 2191 result =
2201 hso_mux_submit_intr_urb(serial->shared_int, 2192 hso_mux_submit_intr_urb(serial->shared_int,
2202 hso_dev->usb, flags); 2193 hso_dev->usb, flags);
2203 } 2194 }
2204 serial->shared_int->use_count++; 2195 serial->shared_int->use_count++;
2205 mutex_unlock(&serial->shared_int->shared_int_lock); 2196 mutex_unlock(&serial->shared_int->shared_int_lock);
2206 } 2197 }
2207 if (serial->tiocmget) 2198 if (serial->tiocmget)
2208 tiocmget_submit_urb(serial, 2199 tiocmget_submit_urb(serial,
2209 serial->tiocmget, 2200 serial->tiocmget,
2210 serial->parent->usb); 2201 serial->parent->usb);
2211 return result; 2202 return result;
2212 } 2203 }
2213 2204
2214 static int hso_stop_serial_device(struct hso_device *hso_dev) 2205 static int hso_stop_serial_device(struct hso_device *hso_dev)
2215 { 2206 {
2216 int i; 2207 int i;
2217 struct hso_serial *serial = dev2ser(hso_dev); 2208 struct hso_serial *serial = dev2ser(hso_dev);
2218 struct hso_tiocmget *tiocmget; 2209 struct hso_tiocmget *tiocmget;
2219 2210
2220 if (!serial) 2211 if (!serial)
2221 return -ENODEV; 2212 return -ENODEV;
2222 2213
2223 for (i = 0; i < serial->num_rx_urbs; i++) { 2214 for (i = 0; i < serial->num_rx_urbs; i++) {
2224 if (serial->rx_urb[i]) { 2215 if (serial->rx_urb[i]) {
2225 usb_kill_urb(serial->rx_urb[i]); 2216 usb_kill_urb(serial->rx_urb[i]);
2226 serial->rx_urb_filled[i] = 0; 2217 serial->rx_urb_filled[i] = 0;
2227 } 2218 }
2228 } 2219 }
2229 serial->curr_rx_urb_idx = 0; 2220 serial->curr_rx_urb_idx = 0;
2230 serial->curr_rx_urb_offset = 0; 2221 serial->curr_rx_urb_offset = 0;
2231 2222
2232 if (serial->tx_urb) 2223 if (serial->tx_urb)
2233 usb_kill_urb(serial->tx_urb); 2224 usb_kill_urb(serial->tx_urb);
2234 2225
2235 if (serial->shared_int) { 2226 if (serial->shared_int) {
2236 mutex_lock(&serial->shared_int->shared_int_lock); 2227 mutex_lock(&serial->shared_int->shared_int_lock);
2237 if (serial->shared_int->use_count && 2228 if (serial->shared_int->use_count &&
2238 (--serial->shared_int->use_count == 0)) { 2229 (--serial->shared_int->use_count == 0)) {
2239 struct urb *urb; 2230 struct urb *urb;
2240 2231
2241 urb = serial->shared_int->shared_intr_urb; 2232 urb = serial->shared_int->shared_intr_urb;
2242 if (urb) 2233 if (urb)
2243 usb_kill_urb(urb); 2234 usb_kill_urb(urb);
2244 } 2235 }
2245 mutex_unlock(&serial->shared_int->shared_int_lock); 2236 mutex_unlock(&serial->shared_int->shared_int_lock);
2246 } 2237 }
2247 tiocmget = serial->tiocmget; 2238 tiocmget = serial->tiocmget;
2248 if (tiocmget) { 2239 if (tiocmget) {
2249 wake_up_interruptible(&tiocmget->waitq); 2240 wake_up_interruptible(&tiocmget->waitq);
2250 usb_kill_urb(tiocmget->urb); 2241 usb_kill_urb(tiocmget->urb);
2251 } 2242 }
2252 2243
2253 return 0; 2244 return 0;
2254 } 2245 }
2255 2246
2256 static void hso_serial_common_free(struct hso_serial *serial) 2247 static void hso_serial_common_free(struct hso_serial *serial)
2257 { 2248 {
2258 int i; 2249 int i;
2259 2250
2260 if (serial->parent->dev) 2251 if (serial->parent->dev)
2261 device_remove_file(serial->parent->dev, &dev_attr_hsotype); 2252 device_remove_file(serial->parent->dev, &dev_attr_hsotype);
2262 2253
2263 tty_unregister_device(tty_drv, serial->minor); 2254 tty_unregister_device(tty_drv, serial->minor);
2264 2255
2265 for (i = 0; i < serial->num_rx_urbs; i++) { 2256 for (i = 0; i < serial->num_rx_urbs; i++) {
2266 /* unlink and free RX URB */ 2257 /* unlink and free RX URB */
2267 usb_free_urb(serial->rx_urb[i]); 2258 usb_free_urb(serial->rx_urb[i]);
2268 /* free the RX buffer */ 2259 /* free the RX buffer */
2269 kfree(serial->rx_data[i]); 2260 kfree(serial->rx_data[i]);
2270 } 2261 }
2271 2262
2272 /* unlink and free TX URB */ 2263 /* unlink and free TX URB */
2273 usb_free_urb(serial->tx_urb); 2264 usb_free_urb(serial->tx_urb);
2274 kfree(serial->tx_data); 2265 kfree(serial->tx_data);
2275 tty_port_destroy(&serial->port); 2266 tty_port_destroy(&serial->port);
2276 } 2267 }
2277 2268
2278 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs, 2269 static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
2279 int rx_size, int tx_size) 2270 int rx_size, int tx_size)
2280 { 2271 {
2281 struct device *dev; 2272 struct device *dev;
2282 int minor; 2273 int minor;
2283 int i; 2274 int i;
2284 2275
2285 tty_port_init(&serial->port); 2276 tty_port_init(&serial->port);
2286 2277
2287 minor = get_free_serial_index(); 2278 minor = get_free_serial_index();
2288 if (minor < 0) 2279 if (minor < 0)
2289 goto exit; 2280 goto exit;
2290 2281
2291 /* register our minor number */ 2282 /* register our minor number */
2292 serial->parent->dev = tty_port_register_device(&serial->port, tty_drv, 2283 serial->parent->dev = tty_port_register_device(&serial->port, tty_drv,
2293 minor, &serial->parent->interface->dev); 2284 minor, &serial->parent->interface->dev);
2294 dev = serial->parent->dev; 2285 dev = serial->parent->dev;
2295 dev_set_drvdata(dev, serial->parent); 2286 dev_set_drvdata(dev, serial->parent);
2296 i = device_create_file(dev, &dev_attr_hsotype); 2287 i = device_create_file(dev, &dev_attr_hsotype);
2297 2288
2298 /* fill in specific data for later use */ 2289 /* fill in specific data for later use */
2299 serial->minor = minor; 2290 serial->minor = minor;
2300 serial->magic = HSO_SERIAL_MAGIC; 2291 serial->magic = HSO_SERIAL_MAGIC;
2301 spin_lock_init(&serial->serial_lock); 2292 spin_lock_init(&serial->serial_lock);
2302 serial->num_rx_urbs = num_urbs; 2293 serial->num_rx_urbs = num_urbs;
2303 2294
2304 /* RX, allocate urb and initialize */ 2295 /* RX, allocate urb and initialize */
2305 2296
2306 /* prepare our RX buffer */ 2297 /* prepare our RX buffer */
2307 serial->rx_data_length = rx_size; 2298 serial->rx_data_length = rx_size;
2308 for (i = 0; i < serial->num_rx_urbs; i++) { 2299 for (i = 0; i < serial->num_rx_urbs; i++) {
2309 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL); 2300 serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
2310 if (!serial->rx_urb[i]) { 2301 if (!serial->rx_urb[i]) {
2311 dev_err(dev, "Could not allocate urb?\n"); 2302 dev_err(dev, "Could not allocate urb?\n");
2312 goto exit; 2303 goto exit;
2313 } 2304 }
2314 serial->rx_urb[i]->transfer_buffer = NULL; 2305 serial->rx_urb[i]->transfer_buffer = NULL;
2315 serial->rx_urb[i]->transfer_buffer_length = 0; 2306 serial->rx_urb[i]->transfer_buffer_length = 0;
2316 serial->rx_data[i] = kzalloc(serial->rx_data_length, 2307 serial->rx_data[i] = kzalloc(serial->rx_data_length,
2317 GFP_KERNEL); 2308 GFP_KERNEL);
2318 if (!serial->rx_data[i]) 2309 if (!serial->rx_data[i])
2319 goto exit; 2310 goto exit;
2320 } 2311 }
2321 2312
2322 /* TX, allocate urb and initialize */ 2313 /* TX, allocate urb and initialize */
2323 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL); 2314 serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2324 if (!serial->tx_urb) { 2315 if (!serial->tx_urb) {
2325 dev_err(dev, "Could not allocate urb?\n"); 2316 dev_err(dev, "Could not allocate urb?\n");
2326 goto exit; 2317 goto exit;
2327 } 2318 }
2328 serial->tx_urb->transfer_buffer = NULL; 2319 serial->tx_urb->transfer_buffer = NULL;
2329 serial->tx_urb->transfer_buffer_length = 0; 2320 serial->tx_urb->transfer_buffer_length = 0;
2330 /* prepare our TX buffer */ 2321 /* prepare our TX buffer */
2331 serial->tx_data_count = 0; 2322 serial->tx_data_count = 0;
2332 serial->tx_buffer_count = 0; 2323 serial->tx_buffer_count = 0;
2333 serial->tx_data_length = tx_size; 2324 serial->tx_data_length = tx_size;
2334 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL); 2325 serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
2335 if (!serial->tx_data) 2326 if (!serial->tx_data)
2336 goto exit; 2327 goto exit;
2337 2328
2338 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL); 2329 serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
2339 if (!serial->tx_buffer) 2330 if (!serial->tx_buffer)
2340 goto exit; 2331 goto exit;
2341 2332
2342 return 0; 2333 return 0;
2343 exit: 2334 exit:
2344 hso_serial_common_free(serial); 2335 hso_serial_common_free(serial);
2345 return -1; 2336 return -1;
2346 } 2337 }
2347 2338
2348 /* Creates a general hso device */ 2339 /* Creates a general hso device */
2349 static struct hso_device *hso_create_device(struct usb_interface *intf, 2340 static struct hso_device *hso_create_device(struct usb_interface *intf,
2350 int port_spec) 2341 int port_spec)
2351 { 2342 {
2352 struct hso_device *hso_dev; 2343 struct hso_device *hso_dev;
2353 2344
2354 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC); 2345 hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
2355 if (!hso_dev) 2346 if (!hso_dev)
2356 return NULL; 2347 return NULL;
2357 2348
2358 hso_dev->port_spec = port_spec; 2349 hso_dev->port_spec = port_spec;
2359 hso_dev->usb = interface_to_usbdev(intf); 2350 hso_dev->usb = interface_to_usbdev(intf);
2360 hso_dev->interface = intf; 2351 hso_dev->interface = intf;
2361 kref_init(&hso_dev->ref); 2352 kref_init(&hso_dev->ref);
2362 mutex_init(&hso_dev->mutex); 2353 mutex_init(&hso_dev->mutex);
2363 2354
2364 INIT_WORK(&hso_dev->async_get_intf, async_get_intf); 2355 INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
2365 INIT_WORK(&hso_dev->async_put_intf, async_put_intf); 2356 INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
2366 INIT_WORK(&hso_dev->reset_device, reset_device); 2357 INIT_WORK(&hso_dev->reset_device, reset_device);
2367 2358
2368 return hso_dev; 2359 return hso_dev;
2369 } 2360 }
2370 2361
2371 /* Removes a network device in the network device table */ 2362 /* Removes a network device in the network device table */
2372 static int remove_net_device(struct hso_device *hso_dev) 2363 static int remove_net_device(struct hso_device *hso_dev)
2373 { 2364 {
2374 int i; 2365 int i;
2375 2366
2376 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) { 2367 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2377 if (network_table[i] == hso_dev) { 2368 if (network_table[i] == hso_dev) {
2378 network_table[i] = NULL; 2369 network_table[i] = NULL;
2379 break; 2370 break;
2380 } 2371 }
2381 } 2372 }
2382 if (i == HSO_MAX_NET_DEVICES) 2373 if (i == HSO_MAX_NET_DEVICES)
2383 return -1; 2374 return -1;
2384 return 0; 2375 return 0;
2385 } 2376 }
2386 2377
2387 /* Frees our network device */ 2378 /* Frees our network device */
2388 static void hso_free_net_device(struct hso_device *hso_dev) 2379 static void hso_free_net_device(struct hso_device *hso_dev)
2389 { 2380 {
2390 int i; 2381 int i;
2391 struct hso_net *hso_net = dev2net(hso_dev); 2382 struct hso_net *hso_net = dev2net(hso_dev);
2392 2383
2393 if (!hso_net) 2384 if (!hso_net)
2394 return; 2385 return;
2395 2386
2396 remove_net_device(hso_net->parent); 2387 remove_net_device(hso_net->parent);
2397 2388
2398 if (hso_net->net) 2389 if (hso_net->net)
2399 unregister_netdev(hso_net->net); 2390 unregister_netdev(hso_net->net);
2400 2391
2401 /* start freeing */ 2392 /* start freeing */
2402 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) { 2393 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2403 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]); 2394 usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
2404 kfree(hso_net->mux_bulk_rx_buf_pool[i]); 2395 kfree(hso_net->mux_bulk_rx_buf_pool[i]);
2405 hso_net->mux_bulk_rx_buf_pool[i] = NULL; 2396 hso_net->mux_bulk_rx_buf_pool[i] = NULL;
2406 } 2397 }
2407 usb_free_urb(hso_net->mux_bulk_tx_urb); 2398 usb_free_urb(hso_net->mux_bulk_tx_urb);
2408 kfree(hso_net->mux_bulk_tx_buf); 2399 kfree(hso_net->mux_bulk_tx_buf);
2409 hso_net->mux_bulk_tx_buf = NULL; 2400 hso_net->mux_bulk_tx_buf = NULL;
2410 2401
2411 if (hso_net->net) 2402 if (hso_net->net)
2412 free_netdev(hso_net->net); 2403 free_netdev(hso_net->net);
2413 2404
2414 kfree(hso_dev); 2405 kfree(hso_dev);
2415 } 2406 }
2416 2407
2417 static const struct net_device_ops hso_netdev_ops = { 2408 static const struct net_device_ops hso_netdev_ops = {
2418 .ndo_open = hso_net_open, 2409 .ndo_open = hso_net_open,
2419 .ndo_stop = hso_net_close, 2410 .ndo_stop = hso_net_close,
2420 .ndo_start_xmit = hso_net_start_xmit, 2411 .ndo_start_xmit = hso_net_start_xmit,
2421 .ndo_tx_timeout = hso_net_tx_timeout, 2412 .ndo_tx_timeout = hso_net_tx_timeout,
2422 }; 2413 };
2423 2414
2424 /* initialize the network interface */ 2415 /* initialize the network interface */
2425 static void hso_net_init(struct net_device *net) 2416 static void hso_net_init(struct net_device *net)
2426 { 2417 {
2427 struct hso_net *hso_net = netdev_priv(net); 2418 struct hso_net *hso_net = netdev_priv(net);
2428 2419
2429 D1("sizeof hso_net is %d", (int)sizeof(*hso_net)); 2420 D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
2430 2421
2431 /* fill in the other fields */ 2422 /* fill in the other fields */
2432 net->netdev_ops = &hso_netdev_ops; 2423 net->netdev_ops = &hso_netdev_ops;
2433 net->watchdog_timeo = HSO_NET_TX_TIMEOUT; 2424 net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
2434 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 2425 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2435 net->type = ARPHRD_NONE; 2426 net->type = ARPHRD_NONE;
2436 net->mtu = DEFAULT_MTU - 14; 2427 net->mtu = DEFAULT_MTU - 14;
2437 net->tx_queue_len = 10; 2428 net->tx_queue_len = 10;
2438 SET_ETHTOOL_OPS(net, &ops); 2429 SET_ETHTOOL_OPS(net, &ops);
2439 2430
2440 /* and initialize the semaphore */ 2431 /* and initialize the semaphore */
2441 spin_lock_init(&hso_net->net_lock); 2432 spin_lock_init(&hso_net->net_lock);
2442 } 2433 }
2443 2434
2444 /* Adds a network device in the network device table */ 2435 /* Adds a network device in the network device table */
2445 static int add_net_device(struct hso_device *hso_dev) 2436 static int add_net_device(struct hso_device *hso_dev)
2446 { 2437 {
2447 int i; 2438 int i;
2448 2439
2449 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) { 2440 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
2450 if (network_table[i] == NULL) { 2441 if (network_table[i] == NULL) {
2451 network_table[i] = hso_dev; 2442 network_table[i] = hso_dev;
2452 break; 2443 break;
2453 } 2444 }
2454 } 2445 }
2455 if (i == HSO_MAX_NET_DEVICES) 2446 if (i == HSO_MAX_NET_DEVICES)
2456 return -1; 2447 return -1;
2457 return 0; 2448 return 0;
2458 } 2449 }
2459 2450
2460 static int hso_rfkill_set_block(void *data, bool blocked) 2451 static int hso_rfkill_set_block(void *data, bool blocked)
2461 { 2452 {
2462 struct hso_device *hso_dev = data; 2453 struct hso_device *hso_dev = data;
2463 int enabled = !blocked; 2454 int enabled = !blocked;
2464 int rv; 2455 int rv;
2465 2456
2466 mutex_lock(&hso_dev->mutex); 2457 mutex_lock(&hso_dev->mutex);
2467 if (hso_dev->usb_gone) 2458 if (hso_dev->usb_gone)
2468 rv = 0; 2459 rv = 0;
2469 else 2460 else
2470 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0), 2461 rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
2471 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0, 2462 enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
2472 USB_CTRL_SET_TIMEOUT); 2463 USB_CTRL_SET_TIMEOUT);
2473 mutex_unlock(&hso_dev->mutex); 2464 mutex_unlock(&hso_dev->mutex);
2474 return rv; 2465 return rv;
2475 } 2466 }
2476 2467
2477 static const struct rfkill_ops hso_rfkill_ops = { 2468 static const struct rfkill_ops hso_rfkill_ops = {
2478 .set_block = hso_rfkill_set_block, 2469 .set_block = hso_rfkill_set_block,
2479 }; 2470 };
2480 2471
2481 /* Creates and sets up everything for rfkill */ 2472 /* Creates and sets up everything for rfkill */
2482 static void hso_create_rfkill(struct hso_device *hso_dev, 2473 static void hso_create_rfkill(struct hso_device *hso_dev,
2483 struct usb_interface *interface) 2474 struct usb_interface *interface)
2484 { 2475 {
2485 struct hso_net *hso_net = dev2net(hso_dev); 2476 struct hso_net *hso_net = dev2net(hso_dev);
2486 struct device *dev = &hso_net->net->dev; 2477 struct device *dev = &hso_net->net->dev;
2487 char *rfkn; 2478 char *rfkn;
2488 2479
2489 rfkn = kzalloc(20, GFP_KERNEL); 2480 rfkn = kzalloc(20, GFP_KERNEL);
2490 if (!rfkn) 2481 if (!rfkn)
2491 dev_err(dev, "%s - Out of memory\n", __func__); 2482 dev_err(dev, "%s - Out of memory\n", __func__);
2492 2483
2493 snprintf(rfkn, 20, "hso-%d", 2484 snprintf(rfkn, 20, "hso-%d",
2494 interface->altsetting->desc.bInterfaceNumber); 2485 interface->altsetting->desc.bInterfaceNumber);
2495 2486
2496 hso_net->rfkill = rfkill_alloc(rfkn, 2487 hso_net->rfkill = rfkill_alloc(rfkn,
2497 &interface_to_usbdev(interface)->dev, 2488 &interface_to_usbdev(interface)->dev,
2498 RFKILL_TYPE_WWAN, 2489 RFKILL_TYPE_WWAN,
2499 &hso_rfkill_ops, hso_dev); 2490 &hso_rfkill_ops, hso_dev);
2500 if (!hso_net->rfkill) { 2491 if (!hso_net->rfkill) {
2501 dev_err(dev, "%s - Out of memory\n", __func__); 2492 dev_err(dev, "%s - Out of memory\n", __func__);
2502 kfree(rfkn); 2493 kfree(rfkn);
2503 return; 2494 return;
2504 } 2495 }
2505 if (rfkill_register(hso_net->rfkill) < 0) { 2496 if (rfkill_register(hso_net->rfkill) < 0) {
2506 rfkill_destroy(hso_net->rfkill); 2497 rfkill_destroy(hso_net->rfkill);
2507 kfree(rfkn); 2498 kfree(rfkn);
2508 hso_net->rfkill = NULL; 2499 hso_net->rfkill = NULL;
2509 dev_err(dev, "%s - Failed to register rfkill\n", __func__); 2500 dev_err(dev, "%s - Failed to register rfkill\n", __func__);
2510 return; 2501 return;
2511 } 2502 }
2512 } 2503 }
2513 2504
2514 static struct device_type hso_type = { 2505 static struct device_type hso_type = {
2515 .name = "wwan", 2506 .name = "wwan",
2516 }; 2507 };
2517 2508
2518 /* Creates our network device */ 2509 /* Creates our network device */
2519 static struct hso_device *hso_create_net_device(struct usb_interface *interface, 2510 static struct hso_device *hso_create_net_device(struct usb_interface *interface,
2520 int port_spec) 2511 int port_spec)
2521 { 2512 {
2522 int result, i; 2513 int result, i;
2523 struct net_device *net; 2514 struct net_device *net;
2524 struct hso_net *hso_net; 2515 struct hso_net *hso_net;
2525 struct hso_device *hso_dev; 2516 struct hso_device *hso_dev;
2526 2517
2527 hso_dev = hso_create_device(interface, port_spec); 2518 hso_dev = hso_create_device(interface, port_spec);
2528 if (!hso_dev) 2519 if (!hso_dev)
2529 return NULL; 2520 return NULL;
2530 2521
2531 /* allocate our network device, then we can put in our private data */ 2522 /* allocate our network device, then we can put in our private data */
2532 /* call hso_net_init to do the basic initialization */ 2523 /* call hso_net_init to do the basic initialization */
2533 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init); 2524 net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
2534 if (!net) { 2525 if (!net) {
2535 dev_err(&interface->dev, "Unable to create ethernet device\n"); 2526 dev_err(&interface->dev, "Unable to create ethernet device\n");
2536 goto exit; 2527 goto exit;
2537 } 2528 }
2538 2529
2539 hso_net = netdev_priv(net); 2530 hso_net = netdev_priv(net);
2540 2531
2541 hso_dev->port_data.dev_net = hso_net; 2532 hso_dev->port_data.dev_net = hso_net;
2542 hso_net->net = net; 2533 hso_net->net = net;
2543 hso_net->parent = hso_dev; 2534 hso_net->parent = hso_dev;
2544 2535
2545 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, 2536 hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2546 USB_DIR_IN); 2537 USB_DIR_IN);
2547 if (!hso_net->in_endp) { 2538 if (!hso_net->in_endp) {
2548 dev_err(&interface->dev, "Can't find BULK IN endpoint\n"); 2539 dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
2549 goto exit; 2540 goto exit;
2550 } 2541 }
2551 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, 2542 hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2552 USB_DIR_OUT); 2543 USB_DIR_OUT);
2553 if (!hso_net->out_endp) { 2544 if (!hso_net->out_endp) {
2554 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n"); 2545 dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
2555 goto exit; 2546 goto exit;
2556 } 2547 }
2557 SET_NETDEV_DEV(net, &interface->dev); 2548 SET_NETDEV_DEV(net, &interface->dev);
2558 SET_NETDEV_DEVTYPE(net, &hso_type); 2549 SET_NETDEV_DEVTYPE(net, &hso_type);
2559 2550
2560 /* registering our net device */ 2551 /* registering our net device */
2561 result = register_netdev(net); 2552 result = register_netdev(net);
2562 if (result) { 2553 if (result) {
2563 dev_err(&interface->dev, "Failed to register device\n"); 2554 dev_err(&interface->dev, "Failed to register device\n");
2564 goto exit; 2555 goto exit;
2565 } 2556 }
2566 2557
2567 /* start allocating */ 2558 /* start allocating */
2568 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) { 2559 for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
2569 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL); 2560 hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
2570 if (!hso_net->mux_bulk_rx_urb_pool[i]) { 2561 if (!hso_net->mux_bulk_rx_urb_pool[i]) {
2571 dev_err(&interface->dev, "Could not allocate rx urb\n"); 2562 dev_err(&interface->dev, "Could not allocate rx urb\n");
2572 goto exit; 2563 goto exit;
2573 } 2564 }
2574 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE, 2565 hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
2575 GFP_KERNEL); 2566 GFP_KERNEL);
2576 if (!hso_net->mux_bulk_rx_buf_pool[i]) 2567 if (!hso_net->mux_bulk_rx_buf_pool[i])
2577 goto exit; 2568 goto exit;
2578 } 2569 }
2579 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL); 2570 hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2580 if (!hso_net->mux_bulk_tx_urb) { 2571 if (!hso_net->mux_bulk_tx_urb) {
2581 dev_err(&interface->dev, "Could not allocate tx urb\n"); 2572 dev_err(&interface->dev, "Could not allocate tx urb\n");
2582 goto exit; 2573 goto exit;
2583 } 2574 }
2584 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL); 2575 hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
2585 if (!hso_net->mux_bulk_tx_buf) 2576 if (!hso_net->mux_bulk_tx_buf)
2586 goto exit; 2577 goto exit;
2587 2578
2588 add_net_device(hso_dev); 2579 add_net_device(hso_dev);
2589 2580
2590 hso_log_port(hso_dev); 2581 hso_log_port(hso_dev);
2591 2582
2592 hso_create_rfkill(hso_dev, interface); 2583 hso_create_rfkill(hso_dev, interface);
2593 2584
2594 return hso_dev; 2585 return hso_dev;
2595 exit: 2586 exit:
2596 hso_free_net_device(hso_dev); 2587 hso_free_net_device(hso_dev);
2597 return NULL; 2588 return NULL;
2598 } 2589 }
2599 2590
2600 static void hso_free_tiomget(struct hso_serial *serial) 2591 static void hso_free_tiomget(struct hso_serial *serial)
2601 { 2592 {
2602 struct hso_tiocmget *tiocmget; 2593 struct hso_tiocmget *tiocmget;
2603 if (!serial) 2594 if (!serial)
2604 return; 2595 return;
2605 tiocmget = serial->tiocmget; 2596 tiocmget = serial->tiocmget;
2606 if (tiocmget) { 2597 if (tiocmget) {
2607 usb_free_urb(tiocmget->urb); 2598 usb_free_urb(tiocmget->urb);
2608 tiocmget->urb = NULL; 2599 tiocmget->urb = NULL;
2609 serial->tiocmget = NULL; 2600 serial->tiocmget = NULL;
2610 kfree(tiocmget); 2601 kfree(tiocmget);
2611 } 2602 }
2612 } 2603 }
2613 2604
2614 /* Frees an AT channel ( goes for both mux and non-mux ) */ 2605 /* Frees an AT channel ( goes for both mux and non-mux ) */
2615 static void hso_free_serial_device(struct hso_device *hso_dev) 2606 static void hso_free_serial_device(struct hso_device *hso_dev)
2616 { 2607 {
2617 struct hso_serial *serial = dev2ser(hso_dev); 2608 struct hso_serial *serial = dev2ser(hso_dev);
2618 2609
2619 if (!serial) 2610 if (!serial)
2620 return; 2611 return;
2621 set_serial_by_index(serial->minor, NULL); 2612 set_serial_by_index(serial->minor, NULL);
2622 2613
2623 hso_serial_common_free(serial); 2614 hso_serial_common_free(serial);
2624 2615
2625 if (serial->shared_int) { 2616 if (serial->shared_int) {
2626 mutex_lock(&serial->shared_int->shared_int_lock); 2617 mutex_lock(&serial->shared_int->shared_int_lock);
2627 if (--serial->shared_int->ref_count == 0) 2618 if (--serial->shared_int->ref_count == 0)
2628 hso_free_shared_int(serial->shared_int); 2619 hso_free_shared_int(serial->shared_int);
2629 else 2620 else
2630 mutex_unlock(&serial->shared_int->shared_int_lock); 2621 mutex_unlock(&serial->shared_int->shared_int_lock);
2631 } 2622 }
2632 hso_free_tiomget(serial); 2623 hso_free_tiomget(serial);
2633 kfree(serial); 2624 kfree(serial);
2634 kfree(hso_dev); 2625 kfree(hso_dev);
2635 } 2626 }
2636 2627
2637 /* Creates a bulk AT channel */ 2628 /* Creates a bulk AT channel */
2638 static struct hso_device *hso_create_bulk_serial_device( 2629 static struct hso_device *hso_create_bulk_serial_device(
2639 struct usb_interface *interface, int port) 2630 struct usb_interface *interface, int port)
2640 { 2631 {
2641 struct hso_device *hso_dev; 2632 struct hso_device *hso_dev;
2642 struct hso_serial *serial; 2633 struct hso_serial *serial;
2643 int num_urbs; 2634 int num_urbs;
2644 struct hso_tiocmget *tiocmget; 2635 struct hso_tiocmget *tiocmget;
2645 2636
2646 hso_dev = hso_create_device(interface, port); 2637 hso_dev = hso_create_device(interface, port);
2647 if (!hso_dev) 2638 if (!hso_dev)
2648 return NULL; 2639 return NULL;
2649 2640
2650 serial = kzalloc(sizeof(*serial), GFP_KERNEL); 2641 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2651 if (!serial) 2642 if (!serial)
2652 goto exit; 2643 goto exit;
2653 2644
2654 serial->parent = hso_dev; 2645 serial->parent = hso_dev;
2655 hso_dev->port_data.dev_serial = serial; 2646 hso_dev->port_data.dev_serial = serial;
2656 2647
2657 if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) { 2648 if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
2658 num_urbs = 2; 2649 num_urbs = 2;
2659 serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget), 2650 serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
2660 GFP_KERNEL); 2651 GFP_KERNEL);
2661 /* it isn't going to break our heart if serial->tiocmget 2652 /* it isn't going to break our heart if serial->tiocmget
2662 * allocation fails don't bother checking this. 2653 * allocation fails don't bother checking this.
2663 */ 2654 */
2664 if (serial->tiocmget) { 2655 if (serial->tiocmget) {
2665 tiocmget = serial->tiocmget; 2656 tiocmget = serial->tiocmget;
2666 tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL); 2657 tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
2667 if (tiocmget->urb) { 2658 if (tiocmget->urb) {
2668 mutex_init(&tiocmget->mutex); 2659 mutex_init(&tiocmget->mutex);
2669 init_waitqueue_head(&tiocmget->waitq); 2660 init_waitqueue_head(&tiocmget->waitq);
2670 tiocmget->endp = hso_get_ep( 2661 tiocmget->endp = hso_get_ep(
2671 interface, 2662 interface,
2672 USB_ENDPOINT_XFER_INT, 2663 USB_ENDPOINT_XFER_INT,
2673 USB_DIR_IN); 2664 USB_DIR_IN);
2674 } else 2665 } else
2675 hso_free_tiomget(serial); 2666 hso_free_tiomget(serial);
2676 } 2667 }
2677 } 2668 }
2678 else 2669 else
2679 num_urbs = 1; 2670 num_urbs = 1;
2680 2671
2681 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE, 2672 if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
2682 BULK_URB_TX_SIZE)) 2673 BULK_URB_TX_SIZE))
2683 goto exit; 2674 goto exit;
2684 2675
2685 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, 2676 serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
2686 USB_DIR_IN); 2677 USB_DIR_IN);
2687 if (!serial->in_endp) { 2678 if (!serial->in_endp) {
2688 dev_err(&interface->dev, "Failed to find BULK IN ep\n"); 2679 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2689 goto exit2; 2680 goto exit2;
2690 } 2681 }
2691 2682
2692 if (! 2683 if (!
2693 (serial->out_endp = 2684 (serial->out_endp =
2694 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) { 2685 hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
2695 dev_err(&interface->dev, "Failed to find BULK IN ep\n"); 2686 dev_err(&interface->dev, "Failed to find BULK IN ep\n");
2696 goto exit2; 2687 goto exit2;
2697 } 2688 }
2698 2689
2699 serial->write_data = hso_std_serial_write_data; 2690 serial->write_data = hso_std_serial_write_data;
2700 2691
2701 /* and record this serial */ 2692 /* and record this serial */
2702 set_serial_by_index(serial->minor, serial); 2693 set_serial_by_index(serial->minor, serial);
2703 2694
2704 /* setup the proc dirs and files if needed */ 2695 /* setup the proc dirs and files if needed */
2705 hso_log_port(hso_dev); 2696 hso_log_port(hso_dev);
2706 2697
2707 /* done, return it */ 2698 /* done, return it */
2708 return hso_dev; 2699 return hso_dev;
2709 2700
2710 exit2: 2701 exit2:
2711 hso_serial_common_free(serial); 2702 hso_serial_common_free(serial);
2712 exit: 2703 exit:
2713 hso_free_tiomget(serial); 2704 hso_free_tiomget(serial);
2714 kfree(serial); 2705 kfree(serial);
2715 kfree(hso_dev); 2706 kfree(hso_dev);
2716 return NULL; 2707 return NULL;
2717 } 2708 }
2718 2709
2719 /* Creates a multiplexed AT channel */ 2710 /* Creates a multiplexed AT channel */
2720 static 2711 static
2721 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface, 2712 struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
2722 int port, 2713 int port,
2723 struct hso_shared_int *mux) 2714 struct hso_shared_int *mux)
2724 { 2715 {
2725 struct hso_device *hso_dev; 2716 struct hso_device *hso_dev;
2726 struct hso_serial *serial; 2717 struct hso_serial *serial;
2727 int port_spec; 2718 int port_spec;
2728 2719
2729 port_spec = HSO_INTF_MUX; 2720 port_spec = HSO_INTF_MUX;
2730 port_spec &= ~HSO_PORT_MASK; 2721 port_spec &= ~HSO_PORT_MASK;
2731 2722
2732 port_spec |= hso_mux_to_port(port); 2723 port_spec |= hso_mux_to_port(port);
2733 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT) 2724 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
2734 return NULL; 2725 return NULL;
2735 2726
2736 hso_dev = hso_create_device(interface, port_spec); 2727 hso_dev = hso_create_device(interface, port_spec);
2737 if (!hso_dev) 2728 if (!hso_dev)
2738 return NULL; 2729 return NULL;
2739 2730
2740 serial = kzalloc(sizeof(*serial), GFP_KERNEL); 2731 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2741 if (!serial) 2732 if (!serial)
2742 goto exit; 2733 goto exit;
2743 2734
2744 hso_dev->port_data.dev_serial = serial; 2735 hso_dev->port_data.dev_serial = serial;
2745 serial->parent = hso_dev; 2736 serial->parent = hso_dev;
2746 2737
2747 if (hso_serial_common_create 2738 if (hso_serial_common_create
2748 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE)) 2739 (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
2749 goto exit; 2740 goto exit;
2750 2741
2751 serial->tx_data_length--; 2742 serial->tx_data_length--;
2752 serial->write_data = hso_mux_serial_write_data; 2743 serial->write_data = hso_mux_serial_write_data;
2753 2744
2754 serial->shared_int = mux; 2745 serial->shared_int = mux;
2755 mutex_lock(&serial->shared_int->shared_int_lock); 2746 mutex_lock(&serial->shared_int->shared_int_lock);
2756 serial->shared_int->ref_count++; 2747 serial->shared_int->ref_count++;
2757 mutex_unlock(&serial->shared_int->shared_int_lock); 2748 mutex_unlock(&serial->shared_int->shared_int_lock);
2758 2749
2759 /* and record this serial */ 2750 /* and record this serial */
2760 set_serial_by_index(serial->minor, serial); 2751 set_serial_by_index(serial->minor, serial);
2761 2752
2762 /* setup the proc dirs and files if needed */ 2753 /* setup the proc dirs and files if needed */
2763 hso_log_port(hso_dev); 2754 hso_log_port(hso_dev);
2764 2755
2765 /* done, return it */ 2756 /* done, return it */
2766 return hso_dev; 2757 return hso_dev;
2767 2758
2768 exit: 2759 exit:
2769 if (serial) { 2760 if (serial) {
2770 tty_unregister_device(tty_drv, serial->minor); 2761 tty_unregister_device(tty_drv, serial->minor);
2771 kfree(serial); 2762 kfree(serial);
2772 } 2763 }
2773 if (hso_dev) 2764 if (hso_dev)
2774 kfree(hso_dev); 2765 kfree(hso_dev);
2775 return NULL; 2766 return NULL;
2776 2767
2777 } 2768 }
2778 2769
2779 static void hso_free_shared_int(struct hso_shared_int *mux) 2770 static void hso_free_shared_int(struct hso_shared_int *mux)
2780 { 2771 {
2781 usb_free_urb(mux->shared_intr_urb); 2772 usb_free_urb(mux->shared_intr_urb);
2782 kfree(mux->shared_intr_buf); 2773 kfree(mux->shared_intr_buf);
2783 mutex_unlock(&mux->shared_int_lock); 2774 mutex_unlock(&mux->shared_int_lock);
2784 kfree(mux); 2775 kfree(mux);
2785 } 2776 }
2786 2777
2787 static 2778 static
2788 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface) 2779 struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
2789 { 2780 {
2790 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL); 2781 struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2791 2782
2792 if (!mux) 2783 if (!mux)
2793 return NULL; 2784 return NULL;
2794 2785
2795 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT, 2786 mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
2796 USB_DIR_IN); 2787 USB_DIR_IN);
2797 if (!mux->intr_endp) { 2788 if (!mux->intr_endp) {
2798 dev_err(&interface->dev, "Can't find INT IN endpoint\n"); 2789 dev_err(&interface->dev, "Can't find INT IN endpoint\n");
2799 goto exit; 2790 goto exit;
2800 } 2791 }
2801 2792
2802 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL); 2793 mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2803 if (!mux->shared_intr_urb) { 2794 if (!mux->shared_intr_urb) {
2804 dev_err(&interface->dev, "Could not allocate intr urb?\n"); 2795 dev_err(&interface->dev, "Could not allocate intr urb?\n");
2805 goto exit; 2796 goto exit;
2806 } 2797 }
2807 mux->shared_intr_buf = 2798 mux->shared_intr_buf =
2808 kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize), 2799 kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
2809 GFP_KERNEL); 2800 GFP_KERNEL);
2810 if (!mux->shared_intr_buf) 2801 if (!mux->shared_intr_buf)
2811 goto exit; 2802 goto exit;
2812 2803
2813 mutex_init(&mux->shared_int_lock); 2804 mutex_init(&mux->shared_int_lock);
2814 2805
2815 return mux; 2806 return mux;
2816 2807
2817 exit: 2808 exit:
2818 kfree(mux->shared_intr_buf); 2809 kfree(mux->shared_intr_buf);
2819 usb_free_urb(mux->shared_intr_urb); 2810 usb_free_urb(mux->shared_intr_urb);
2820 kfree(mux); 2811 kfree(mux);
2821 return NULL; 2812 return NULL;
2822 } 2813 }
2823 2814
2824 /* Gets the port spec for a certain interface */ 2815 /* Gets the port spec for a certain interface */
2825 static int hso_get_config_data(struct usb_interface *interface) 2816 static int hso_get_config_data(struct usb_interface *interface)
2826 { 2817 {
2827 struct usb_device *usbdev = interface_to_usbdev(interface); 2818 struct usb_device *usbdev = interface_to_usbdev(interface);
2828 u8 config_data[17]; 2819 u8 config_data[17];
2829 u32 if_num = interface->altsetting->desc.bInterfaceNumber; 2820 u32 if_num = interface->altsetting->desc.bInterfaceNumber;
2830 s32 result; 2821 s32 result;
2831 2822
2832 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0), 2823 if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
2833 0x86, 0xC0, 0, 0, config_data, 17, 2824 0x86, 0xC0, 0, 0, config_data, 17,
2834 USB_CTRL_SET_TIMEOUT) != 0x11) { 2825 USB_CTRL_SET_TIMEOUT) != 0x11) {
2835 return -EIO; 2826 return -EIO;
2836 } 2827 }
2837 2828
2838 switch (config_data[if_num]) { 2829 switch (config_data[if_num]) {
2839 case 0x0: 2830 case 0x0:
2840 result = 0; 2831 result = 0;
2841 break; 2832 break;
2842 case 0x1: 2833 case 0x1:
2843 result = HSO_PORT_DIAG; 2834 result = HSO_PORT_DIAG;
2844 break; 2835 break;
2845 case 0x2: 2836 case 0x2:
2846 result = HSO_PORT_GPS; 2837 result = HSO_PORT_GPS;
2847 break; 2838 break;
2848 case 0x3: 2839 case 0x3:
2849 result = HSO_PORT_GPS_CONTROL; 2840 result = HSO_PORT_GPS_CONTROL;
2850 break; 2841 break;
2851 case 0x4: 2842 case 0x4:
2852 result = HSO_PORT_APP; 2843 result = HSO_PORT_APP;
2853 break; 2844 break;
2854 case 0x5: 2845 case 0x5:
2855 result = HSO_PORT_APP2; 2846 result = HSO_PORT_APP2;
2856 break; 2847 break;
2857 case 0x6: 2848 case 0x6:
2858 result = HSO_PORT_CONTROL; 2849 result = HSO_PORT_CONTROL;
2859 break; 2850 break;
2860 case 0x7: 2851 case 0x7:
2861 result = HSO_PORT_NETWORK; 2852 result = HSO_PORT_NETWORK;
2862 break; 2853 break;
2863 case 0x8: 2854 case 0x8:
2864 result = HSO_PORT_MODEM; 2855 result = HSO_PORT_MODEM;
2865 break; 2856 break;
2866 case 0x9: 2857 case 0x9:
2867 result = HSO_PORT_MSD; 2858 result = HSO_PORT_MSD;
2868 break; 2859 break;
2869 case 0xa: 2860 case 0xa:
2870 result = HSO_PORT_PCSC; 2861 result = HSO_PORT_PCSC;
2871 break; 2862 break;
2872 case 0xb: 2863 case 0xb:
2873 result = HSO_PORT_VOICE; 2864 result = HSO_PORT_VOICE;
2874 break; 2865 break;
2875 default: 2866 default:
2876 result = 0; 2867 result = 0;
2877 } 2868 }
2878 2869
2879 if (result) 2870 if (result)
2880 result |= HSO_INTF_BULK; 2871 result |= HSO_INTF_BULK;
2881 2872
2882 if (config_data[16] & 0x1) 2873 if (config_data[16] & 0x1)
2883 result |= HSO_INFO_CRC_BUG; 2874 result |= HSO_INFO_CRC_BUG;
2884 2875
2885 return result; 2876 return result;
2886 } 2877 }
2887 2878
2888 /* called once for each interface upon device insertion */ 2879 /* called once for each interface upon device insertion */
2889 static int hso_probe(struct usb_interface *interface, 2880 static int hso_probe(struct usb_interface *interface,
2890 const struct usb_device_id *id) 2881 const struct usb_device_id *id)
2891 { 2882 {
2892 int mux, i, if_num, port_spec; 2883 int mux, i, if_num, port_spec;
2893 unsigned char port_mask; 2884 unsigned char port_mask;
2894 struct hso_device *hso_dev = NULL; 2885 struct hso_device *hso_dev = NULL;
2895 struct hso_shared_int *shared_int; 2886 struct hso_shared_int *shared_int;
2896 struct hso_device *tmp_dev = NULL; 2887 struct hso_device *tmp_dev = NULL;
2897 2888
2898 if_num = interface->altsetting->desc.bInterfaceNumber; 2889 if_num = interface->altsetting->desc.bInterfaceNumber;
2899 2890
2900 /* Get the interface/port specification from either driver_info or from 2891 /* Get the interface/port specification from either driver_info or from
2901 * the device itself */ 2892 * the device itself */
2902 if (id->driver_info) 2893 if (id->driver_info)
2903 port_spec = ((u32 *)(id->driver_info))[if_num]; 2894 port_spec = ((u32 *)(id->driver_info))[if_num];
2904 else 2895 else
2905 port_spec = hso_get_config_data(interface); 2896 port_spec = hso_get_config_data(interface);
2906 2897
2907 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) { 2898 if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
2908 dev_err(&interface->dev, "Not our interface\n"); 2899 dev_err(&interface->dev, "Not our interface\n");
2909 return -ENODEV; 2900 return -ENODEV;
2910 } 2901 }
2911 /* Check if we need to switch to alt interfaces prior to port 2902 /* Check if we need to switch to alt interfaces prior to port
2912 * configuration */ 2903 * configuration */
2913 if (interface->num_altsetting > 1) 2904 if (interface->num_altsetting > 1)
2914 usb_set_interface(interface_to_usbdev(interface), if_num, 1); 2905 usb_set_interface(interface_to_usbdev(interface), if_num, 1);
2915 interface->needs_remote_wakeup = 1; 2906 interface->needs_remote_wakeup = 1;
2916 2907
2917 /* Allocate new hso device(s) */ 2908 /* Allocate new hso device(s) */
2918 switch (port_spec & HSO_INTF_MASK) { 2909 switch (port_spec & HSO_INTF_MASK) {
2919 case HSO_INTF_MUX: 2910 case HSO_INTF_MUX:
2920 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) { 2911 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2921 /* Create the network device */ 2912 /* Create the network device */
2922 if (!disable_net) { 2913 if (!disable_net) {
2923 hso_dev = hso_create_net_device(interface, 2914 hso_dev = hso_create_net_device(interface,
2924 port_spec); 2915 port_spec);
2925 if (!hso_dev) 2916 if (!hso_dev)
2926 goto exit; 2917 goto exit;
2927 tmp_dev = hso_dev; 2918 tmp_dev = hso_dev;
2928 } 2919 }
2929 } 2920 }
2930 2921
2931 if (hso_get_mux_ports(interface, &port_mask)) 2922 if (hso_get_mux_ports(interface, &port_mask))
2932 /* TODO: de-allocate everything */ 2923 /* TODO: de-allocate everything */
2933 goto exit; 2924 goto exit;
2934 2925
2935 shared_int = hso_create_shared_int(interface); 2926 shared_int = hso_create_shared_int(interface);
2936 if (!shared_int) 2927 if (!shared_int)
2937 goto exit; 2928 goto exit;
2938 2929
2939 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) { 2930 for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
2940 if (port_mask & i) { 2931 if (port_mask & i) {
2941 hso_dev = hso_create_mux_serial_device( 2932 hso_dev = hso_create_mux_serial_device(
2942 interface, i, shared_int); 2933 interface, i, shared_int);
2943 if (!hso_dev) 2934 if (!hso_dev)
2944 goto exit; 2935 goto exit;
2945 } 2936 }
2946 } 2937 }
2947 2938
2948 if (tmp_dev) 2939 if (tmp_dev)
2949 hso_dev = tmp_dev; 2940 hso_dev = tmp_dev;
2950 break; 2941 break;
2951 2942
2952 case HSO_INTF_BULK: 2943 case HSO_INTF_BULK:
2953 /* It's a regular bulk interface */ 2944 /* It's a regular bulk interface */
2954 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) { 2945 if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
2955 if (!disable_net) 2946 if (!disable_net)
2956 hso_dev = 2947 hso_dev =
2957 hso_create_net_device(interface, port_spec); 2948 hso_create_net_device(interface, port_spec);
2958 } else { 2949 } else {
2959 hso_dev = 2950 hso_dev =
2960 hso_create_bulk_serial_device(interface, port_spec); 2951 hso_create_bulk_serial_device(interface, port_spec);
2961 } 2952 }
2962 if (!hso_dev) 2953 if (!hso_dev)
2963 goto exit; 2954 goto exit;
2964 break; 2955 break;
2965 default: 2956 default:
2966 goto exit; 2957 goto exit;
2967 } 2958 }
2968 2959
2969 /* save our data pointer in this device */ 2960 /* save our data pointer in this device */
2970 usb_set_intfdata(interface, hso_dev); 2961 usb_set_intfdata(interface, hso_dev);
2971 2962
2972 /* done */ 2963 /* done */
2973 return 0; 2964 return 0;
2974 exit: 2965 exit:
2975 hso_free_interface(interface); 2966 hso_free_interface(interface);
2976 return -ENODEV; 2967 return -ENODEV;
2977 } 2968 }
2978 2969
2979 /* device removed, cleaning up */ 2970 /* device removed, cleaning up */
2980 static void hso_disconnect(struct usb_interface *interface) 2971 static void hso_disconnect(struct usb_interface *interface)
2981 { 2972 {
2982 hso_free_interface(interface); 2973 hso_free_interface(interface);
2983 2974
2984 /* remove reference of our private data */ 2975 /* remove reference of our private data */
2985 usb_set_intfdata(interface, NULL); 2976 usb_set_intfdata(interface, NULL);
2986 } 2977 }
2987 2978
2988 static void async_get_intf(struct work_struct *data) 2979 static void async_get_intf(struct work_struct *data)
2989 { 2980 {
2990 struct hso_device *hso_dev = 2981 struct hso_device *hso_dev =
2991 container_of(data, struct hso_device, async_get_intf); 2982 container_of(data, struct hso_device, async_get_intf);
2992 usb_autopm_get_interface(hso_dev->interface); 2983 usb_autopm_get_interface(hso_dev->interface);
2993 } 2984 }
2994 2985
2995 static void async_put_intf(struct work_struct *data) 2986 static void async_put_intf(struct work_struct *data)
2996 { 2987 {
2997 struct hso_device *hso_dev = 2988 struct hso_device *hso_dev =
2998 container_of(data, struct hso_device, async_put_intf); 2989 container_of(data, struct hso_device, async_put_intf);
2999 usb_autopm_put_interface(hso_dev->interface); 2990 usb_autopm_put_interface(hso_dev->interface);
3000 } 2991 }
3001 2992
3002 static int hso_get_activity(struct hso_device *hso_dev) 2993 static int hso_get_activity(struct hso_device *hso_dev)
3003 { 2994 {
3004 if (hso_dev->usb->state == USB_STATE_SUSPENDED) { 2995 if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
3005 if (!hso_dev->is_active) { 2996 if (!hso_dev->is_active) {
3006 hso_dev->is_active = 1; 2997 hso_dev->is_active = 1;
3007 schedule_work(&hso_dev->async_get_intf); 2998 schedule_work(&hso_dev->async_get_intf);
3008 } 2999 }
3009 } 3000 }
3010 3001
3011 if (hso_dev->usb->state != USB_STATE_CONFIGURED) 3002 if (hso_dev->usb->state != USB_STATE_CONFIGURED)
3012 return -EAGAIN; 3003 return -EAGAIN;
3013 3004
3014 usb_mark_last_busy(hso_dev->usb); 3005 usb_mark_last_busy(hso_dev->usb);
3015 3006
3016 return 0; 3007 return 0;
3017 } 3008 }
3018 3009
3019 static int hso_put_activity(struct hso_device *hso_dev) 3010 static int hso_put_activity(struct hso_device *hso_dev)
3020 { 3011 {
3021 if (hso_dev->usb->state != USB_STATE_SUSPENDED) { 3012 if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
3022 if (hso_dev->is_active) { 3013 if (hso_dev->is_active) {
3023 hso_dev->is_active = 0; 3014 hso_dev->is_active = 0;
3024 schedule_work(&hso_dev->async_put_intf); 3015 schedule_work(&hso_dev->async_put_intf);
3025 return -EAGAIN; 3016 return -EAGAIN;
3026 } 3017 }
3027 } 3018 }
3028 hso_dev->is_active = 0; 3019 hso_dev->is_active = 0;
3029 return 0; 3020 return 0;
3030 } 3021 }
3031 3022
3032 /* called by kernel when we need to suspend device */ 3023 /* called by kernel when we need to suspend device */
3033 static int hso_suspend(struct usb_interface *iface, pm_message_t message) 3024 static int hso_suspend(struct usb_interface *iface, pm_message_t message)
3034 { 3025 {
3035 int i, result; 3026 int i, result;
3036 3027
3037 /* Stop all serial ports */ 3028 /* Stop all serial ports */
3038 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) { 3029 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3039 if (serial_table[i] && (serial_table[i]->interface == iface)) { 3030 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3040 result = hso_stop_serial_device(serial_table[i]); 3031 result = hso_stop_serial_device(serial_table[i]);
3041 if (result) 3032 if (result)
3042 goto out; 3033 goto out;
3043 } 3034 }
3044 } 3035 }
3045 3036
3046 /* Stop all network ports */ 3037 /* Stop all network ports */
3047 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) { 3038 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3048 if (network_table[i] && 3039 if (network_table[i] &&
3049 (network_table[i]->interface == iface)) { 3040 (network_table[i]->interface == iface)) {
3050 result = hso_stop_net_device(network_table[i]); 3041 result = hso_stop_net_device(network_table[i]);
3051 if (result) 3042 if (result)
3052 goto out; 3043 goto out;
3053 } 3044 }
3054 } 3045 }
3055 3046
3056 out: 3047 out:
3057 return 0; 3048 return 0;
3058 } 3049 }
3059 3050
3060 /* called by kernel when we need to resume device */ 3051 /* called by kernel when we need to resume device */
3061 static int hso_resume(struct usb_interface *iface) 3052 static int hso_resume(struct usb_interface *iface)
3062 { 3053 {
3063 int i, result = 0; 3054 int i, result = 0;
3064 struct hso_net *hso_net; 3055 struct hso_net *hso_net;
3065 3056
3066 /* Start all serial ports */ 3057 /* Start all serial ports */
3067 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) { 3058 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3068 if (serial_table[i] && (serial_table[i]->interface == iface)) { 3059 if (serial_table[i] && (serial_table[i]->interface == iface)) {
3069 if (dev2ser(serial_table[i])->port.count) { 3060 if (dev2ser(serial_table[i])->port.count) {
3070 result = 3061 result =
3071 hso_start_serial_device(serial_table[i], GFP_NOIO); 3062 hso_start_serial_device(serial_table[i], GFP_NOIO);
3072 hso_kick_transmit(dev2ser(serial_table[i])); 3063 hso_kick_transmit(dev2ser(serial_table[i]));
3073 if (result) 3064 if (result)
3074 goto out; 3065 goto out;
3075 } 3066 }
3076 } 3067 }
3077 } 3068 }
3078 3069
3079 /* Start all network ports */ 3070 /* Start all network ports */
3080 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) { 3071 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3081 if (network_table[i] && 3072 if (network_table[i] &&
3082 (network_table[i]->interface == iface)) { 3073 (network_table[i]->interface == iface)) {
3083 hso_net = dev2net(network_table[i]); 3074 hso_net = dev2net(network_table[i]);
3084 if (hso_net->flags & IFF_UP) { 3075 if (hso_net->flags & IFF_UP) {
3085 /* First transmit any lingering data, 3076 /* First transmit any lingering data,
3086 then restart the device. */ 3077 then restart the device. */
3087 if (hso_net->skb_tx_buf) { 3078 if (hso_net->skb_tx_buf) {
3088 dev_dbg(&iface->dev, 3079 dev_dbg(&iface->dev,
3089 "Transmitting" 3080 "Transmitting"
3090 " lingering data\n"); 3081 " lingering data\n");
3091 hso_net_start_xmit(hso_net->skb_tx_buf, 3082 hso_net_start_xmit(hso_net->skb_tx_buf,
3092 hso_net->net); 3083 hso_net->net);
3093 hso_net->skb_tx_buf = NULL; 3084 hso_net->skb_tx_buf = NULL;
3094 } 3085 }
3095 result = hso_start_net_device(network_table[i]); 3086 result = hso_start_net_device(network_table[i]);
3096 if (result) 3087 if (result)
3097 goto out; 3088 goto out;
3098 } 3089 }
3099 } 3090 }
3100 } 3091 }
3101 3092
3102 out: 3093 out:
3103 return result; 3094 return result;
3104 } 3095 }
3105 3096
3106 static void reset_device(struct work_struct *data) 3097 static void reset_device(struct work_struct *data)
3107 { 3098 {
3108 struct hso_device *hso_dev = 3099 struct hso_device *hso_dev =
3109 container_of(data, struct hso_device, reset_device); 3100 container_of(data, struct hso_device, reset_device);
3110 struct usb_device *usb = hso_dev->usb; 3101 struct usb_device *usb = hso_dev->usb;
3111 int result; 3102 int result;
3112 3103
3113 if (hso_dev->usb_gone) { 3104 if (hso_dev->usb_gone) {
3114 D1("No reset during disconnect\n"); 3105 D1("No reset during disconnect\n");
3115 } else { 3106 } else {
3116 result = usb_lock_device_for_reset(usb, hso_dev->interface); 3107 result = usb_lock_device_for_reset(usb, hso_dev->interface);
3117 if (result < 0) 3108 if (result < 0)
3118 D1("unable to lock device for reset: %d\n", result); 3109 D1("unable to lock device for reset: %d\n", result);
3119 else { 3110 else {
3120 usb_reset_device(usb); 3111 usb_reset_device(usb);
3121 usb_unlock_device(usb); 3112 usb_unlock_device(usb);
3122 } 3113 }
3123 } 3114 }
3124 } 3115 }
3125 3116
3126 static void hso_serial_ref_free(struct kref *ref) 3117 static void hso_serial_ref_free(struct kref *ref)
3127 { 3118 {
3128 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref); 3119 struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
3129 3120
3130 hso_free_serial_device(hso_dev); 3121 hso_free_serial_device(hso_dev);
3131 } 3122 }
3132 3123
3133 static void hso_free_interface(struct usb_interface *interface) 3124 static void hso_free_interface(struct usb_interface *interface)
3134 { 3125 {
3135 struct hso_serial *hso_dev; 3126 struct hso_serial *hso_dev;
3136 struct tty_struct *tty; 3127 struct tty_struct *tty;
3137 int i; 3128 int i;
3138 3129
3139 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) { 3130 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
3140 if (serial_table[i] && 3131 if (serial_table[i] &&
3141 (serial_table[i]->interface == interface)) { 3132 (serial_table[i]->interface == interface)) {
3142 hso_dev = dev2ser(serial_table[i]); 3133 hso_dev = dev2ser(serial_table[i]);
3143 tty = tty_port_tty_get(&hso_dev->port); 3134 tty = tty_port_tty_get(&hso_dev->port);
3144 if (tty) { 3135 if (tty) {
3145 tty_hangup(tty); 3136 tty_hangup(tty);
3146 tty_kref_put(tty); 3137 tty_kref_put(tty);
3147 } 3138 }
3148 mutex_lock(&hso_dev->parent->mutex); 3139 mutex_lock(&hso_dev->parent->mutex);
3149 hso_dev->parent->usb_gone = 1; 3140 hso_dev->parent->usb_gone = 1;
3150 mutex_unlock(&hso_dev->parent->mutex); 3141 mutex_unlock(&hso_dev->parent->mutex);
3151 kref_put(&serial_table[i]->ref, hso_serial_ref_free); 3142 kref_put(&serial_table[i]->ref, hso_serial_ref_free);
3152 } 3143 }
3153 } 3144 }
3154 3145
3155 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) { 3146 for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
3156 if (network_table[i] && 3147 if (network_table[i] &&
3157 (network_table[i]->interface == interface)) { 3148 (network_table[i]->interface == interface)) {
3158 struct rfkill *rfk = dev2net(network_table[i])->rfkill; 3149 struct rfkill *rfk = dev2net(network_table[i])->rfkill;
3159 /* hso_stop_net_device doesn't stop the net queue since 3150 /* hso_stop_net_device doesn't stop the net queue since
3160 * traffic needs to start it again when suspended */ 3151 * traffic needs to start it again when suspended */
3161 netif_stop_queue(dev2net(network_table[i])->net); 3152 netif_stop_queue(dev2net(network_table[i])->net);
3162 hso_stop_net_device(network_table[i]); 3153 hso_stop_net_device(network_table[i]);
3163 cancel_work_sync(&network_table[i]->async_put_intf); 3154 cancel_work_sync(&network_table[i]->async_put_intf);
3164 cancel_work_sync(&network_table[i]->async_get_intf); 3155 cancel_work_sync(&network_table[i]->async_get_intf);
3165 if (rfk) { 3156 if (rfk) {
3166 rfkill_unregister(rfk); 3157 rfkill_unregister(rfk);
3167 rfkill_destroy(rfk); 3158 rfkill_destroy(rfk);
3168 } 3159 }
3169 hso_free_net_device(network_table[i]); 3160 hso_free_net_device(network_table[i]);
3170 } 3161 }
3171 } 3162 }
3172 } 3163 }
3173 3164
3174 /* Helper functions */ 3165 /* Helper functions */
3175 3166
3176 /* Get the endpoint ! */ 3167 /* Get the endpoint ! */
3177 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf, 3168 static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
3178 int type, int dir) 3169 int type, int dir)
3179 { 3170 {
3180 int i; 3171 int i;
3181 struct usb_host_interface *iface = intf->cur_altsetting; 3172 struct usb_host_interface *iface = intf->cur_altsetting;
3182 struct usb_endpoint_descriptor *endp; 3173 struct usb_endpoint_descriptor *endp;
3183 3174
3184 for (i = 0; i < iface->desc.bNumEndpoints; i++) { 3175 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3185 endp = &iface->endpoint[i].desc; 3176 endp = &iface->endpoint[i].desc;
3186 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) && 3177 if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
3187 (usb_endpoint_type(endp) == type)) 3178 (usb_endpoint_type(endp) == type))
3188 return endp; 3179 return endp;
3189 } 3180 }
3190 3181
3191 return NULL; 3182 return NULL;
3192 } 3183 }
3193 3184
3194 /* Get the byte that describes which ports are enabled */ 3185 /* Get the byte that describes which ports are enabled */
3195 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports) 3186 static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
3196 { 3187 {
3197 int i; 3188 int i;
3198 struct usb_host_interface *iface = intf->cur_altsetting; 3189 struct usb_host_interface *iface = intf->cur_altsetting;
3199 3190
3200 if (iface->extralen == 3) { 3191 if (iface->extralen == 3) {
3201 *ports = iface->extra[2]; 3192 *ports = iface->extra[2];
3202 return 0; 3193 return 0;
3203 } 3194 }
3204 3195
3205 for (i = 0; i < iface->desc.bNumEndpoints; i++) { 3196 for (i = 0; i < iface->desc.bNumEndpoints; i++) {
3206 if (iface->endpoint[i].extralen == 3) { 3197 if (iface->endpoint[i].extralen == 3) {
3207 *ports = iface->endpoint[i].extra[2]; 3198 *ports = iface->endpoint[i].extra[2];
3208 return 0; 3199 return 0;
3209 } 3200 }
3210 } 3201 }
3211 3202
3212 return -1; 3203 return -1;
3213 } 3204 }
3214 3205
3215 /* interrupt urb needs to be submitted, used for serial read of muxed port */ 3206 /* interrupt urb needs to be submitted, used for serial read of muxed port */
3216 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int, 3207 static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
3217 struct usb_device *usb, gfp_t gfp) 3208 struct usb_device *usb, gfp_t gfp)
3218 { 3209 {
3219 int result; 3210 int result;
3220 3211
3221 usb_fill_int_urb(shared_int->shared_intr_urb, usb, 3212 usb_fill_int_urb(shared_int->shared_intr_urb, usb,
3222 usb_rcvintpipe(usb, 3213 usb_rcvintpipe(usb,
3223 shared_int->intr_endp->bEndpointAddress & 0x7F), 3214 shared_int->intr_endp->bEndpointAddress & 0x7F),
3224 shared_int->shared_intr_buf, 3215 shared_int->shared_intr_buf,
3225 1, 3216 1,
3226 intr_callback, shared_int, 3217 intr_callback, shared_int,
3227 shared_int->intr_endp->bInterval); 3218 shared_int->intr_endp->bInterval);
3228 3219
3229 result = usb_submit_urb(shared_int->shared_intr_urb, gfp); 3220 result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
3230 if (result) 3221 if (result)
3231 dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__, 3222 dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
3232 result); 3223 result);
3233 3224
3234 return result; 3225 return result;
3235 } 3226 }
3236 3227
3237 /* operations setup of the serial interface */ 3228 /* operations setup of the serial interface */
3238 static const struct tty_operations hso_serial_ops = { 3229 static const struct tty_operations hso_serial_ops = {
3239 .open = hso_serial_open, 3230 .open = hso_serial_open,
3240 .close = hso_serial_close, 3231 .close = hso_serial_close,
3241 .write = hso_serial_write, 3232 .write = hso_serial_write,
3242 .write_room = hso_serial_write_room, 3233 .write_room = hso_serial_write_room,
3243 .ioctl = hso_serial_ioctl, 3234 .ioctl = hso_serial_ioctl,
3244 .set_termios = hso_serial_set_termios, 3235 .set_termios = hso_serial_set_termios,
3245 .chars_in_buffer = hso_serial_chars_in_buffer, 3236 .chars_in_buffer = hso_serial_chars_in_buffer,
3246 .tiocmget = hso_serial_tiocmget, 3237 .tiocmget = hso_serial_tiocmget,
3247 .tiocmset = hso_serial_tiocmset, 3238 .tiocmset = hso_serial_tiocmset,
3248 .get_icount = hso_get_count, 3239 .get_icount = hso_get_count,
3249 .unthrottle = hso_unthrottle 3240 .unthrottle = hso_unthrottle
3250 }; 3241 };
3251 3242
3252 static struct usb_driver hso_driver = { 3243 static struct usb_driver hso_driver = {
3253 .name = driver_name, 3244 .name = driver_name,
3254 .probe = hso_probe, 3245 .probe = hso_probe,
3255 .disconnect = hso_disconnect, 3246 .disconnect = hso_disconnect,
3256 .id_table = hso_ids, 3247 .id_table = hso_ids,
3257 .suspend = hso_suspend, 3248 .suspend = hso_suspend,
3258 .resume = hso_resume, 3249 .resume = hso_resume,
3259 .reset_resume = hso_resume, 3250 .reset_resume = hso_resume,
3260 .supports_autosuspend = 1, 3251 .supports_autosuspend = 1,
3261 .disable_hub_initiated_lpm = 1, 3252 .disable_hub_initiated_lpm = 1,
3262 }; 3253 };
3263 3254
3264 static int __init hso_init(void) 3255 static int __init hso_init(void)
3265 { 3256 {
3266 int i; 3257 int i;
3267 int result; 3258 int result;
3268 3259
3269 /* put it in the log */ 3260 /* put it in the log */
3270 printk(KERN_INFO "hso: %s\n", version); 3261 printk(KERN_INFO "hso: %s\n", version);
3271 3262
3272 /* Initialise the serial table semaphore and table */ 3263 /* Initialise the serial table semaphore and table */
3273 spin_lock_init(&serial_table_lock); 3264 spin_lock_init(&serial_table_lock);
3274 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) 3265 for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
3275 serial_table[i] = NULL; 3266 serial_table[i] = NULL;
3276 3267
3277 /* allocate our driver using the proper amount of supported minors */ 3268 /* allocate our driver using the proper amount of supported minors */
3278 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS); 3269 tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
3279 if (!tty_drv) 3270 if (!tty_drv)
3280 return -ENOMEM; 3271 return -ENOMEM;
3281 3272
3282 /* fill in all needed values */ 3273 /* fill in all needed values */
3283 tty_drv->driver_name = driver_name; 3274 tty_drv->driver_name = driver_name;
3284 tty_drv->name = tty_filename; 3275 tty_drv->name = tty_filename;
3285 3276
3286 /* if major number is provided as parameter, use that one */ 3277 /* if major number is provided as parameter, use that one */
3287 if (tty_major) 3278 if (tty_major)
3288 tty_drv->major = tty_major; 3279 tty_drv->major = tty_major;
3289 3280
3290 tty_drv->minor_start = 0; 3281 tty_drv->minor_start = 0;
3291 tty_drv->type = TTY_DRIVER_TYPE_SERIAL; 3282 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
3292 tty_drv->subtype = SERIAL_TYPE_NORMAL; 3283 tty_drv->subtype = SERIAL_TYPE_NORMAL;
3293 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 3284 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3294 tty_drv->init_termios = tty_std_termios; 3285 tty_drv->init_termios = tty_std_termios;
3295 hso_init_termios(&tty_drv->init_termios); 3286 hso_init_termios(&tty_drv->init_termios);
3296 tty_set_operations(tty_drv, &hso_serial_ops); 3287 tty_set_operations(tty_drv, &hso_serial_ops);
3297 3288
3298 /* register the tty driver */ 3289 /* register the tty driver */
3299 result = tty_register_driver(tty_drv); 3290 result = tty_register_driver(tty_drv);
3300 if (result) { 3291 if (result) {
3301 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n", 3292 printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
3302 __func__, result); 3293 __func__, result);
3303 goto err_free_tty; 3294 goto err_free_tty;
3304 } 3295 }
3305 3296
3306 /* register this module as an usb driver */ 3297 /* register this module as an usb driver */
3307 result = usb_register(&hso_driver); 3298 result = usb_register(&hso_driver);
3308 if (result) { 3299 if (result) {
3309 printk(KERN_ERR "Could not register hso driver? error: %d\n", 3300 printk(KERN_ERR "Could not register hso driver? error: %d\n",
3310 result); 3301 result);
3311 goto err_unreg_tty; 3302 goto err_unreg_tty;
3312 } 3303 }
3313 3304
3314 /* done */ 3305 /* done */
3315 return 0; 3306 return 0;
3316 err_unreg_tty: 3307 err_unreg_tty:
3317 tty_unregister_driver(tty_drv); 3308 tty_unregister_driver(tty_drv);
3318 err_free_tty: 3309 err_free_tty:
3319 put_tty_driver(tty_drv); 3310 put_tty_driver(tty_drv);
3320 return result; 3311 return result;
3321 } 3312 }
3322 3313
3323 static void __exit hso_exit(void) 3314 static void __exit hso_exit(void)
3324 { 3315 {
3325 printk(KERN_INFO "hso: unloaded\n"); 3316 printk(KERN_INFO "hso: unloaded\n");
3326 3317
3327 tty_unregister_driver(tty_drv); 3318 tty_unregister_driver(tty_drv);
3328 put_tty_driver(tty_drv); 3319 put_tty_driver(tty_drv);
3329 /* deregister the usb driver */ 3320 /* deregister the usb driver */
3330 usb_deregister(&hso_driver); 3321 usb_deregister(&hso_driver);
3331 } 3322 }
3332 3323
3333 /* Module definitions */ 3324 /* Module definitions */
3334 module_init(hso_init); 3325 module_init(hso_init);
3335 module_exit(hso_exit); 3326 module_exit(hso_exit);
3336 3327
3337 MODULE_AUTHOR(MOD_AUTHOR); 3328 MODULE_AUTHOR(MOD_AUTHOR);
3338 MODULE_DESCRIPTION(MOD_DESCRIPTION); 3329 MODULE_DESCRIPTION(MOD_DESCRIPTION);
3339 MODULE_LICENSE(MOD_LICENSE); 3330 MODULE_LICENSE(MOD_LICENSE);
3340 3331
3341 /* change the debug level (eg: insmod hso.ko debug=0x04) */ 3332 /* change the debug level (eg: insmod hso.ko debug=0x04) */
3342 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]"); 3333 MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
3343 module_param(debug, int, S_IRUGO | S_IWUSR); 3334 module_param(debug, int, S_IRUGO | S_IWUSR);
3344 3335
3345 /* set the major tty number (eg: insmod hso.ko tty_major=245) */ 3336 /* set the major tty number (eg: insmod hso.ko tty_major=245) */
3346 MODULE_PARM_DESC(tty_major, "Set the major tty number"); 3337 MODULE_PARM_DESC(tty_major, "Set the major tty number");
3347 module_param(tty_major, int, S_IRUGO | S_IWUSR); 3338 module_param(tty_major, int, S_IRUGO | S_IWUSR);
3348 3339
3349 /* disable network interface (eg: insmod hso.ko disable_net=1) */ 3340 /* disable network interface (eg: insmod hso.ko disable_net=1) */
3350 MODULE_PARM_DESC(disable_net, "Disable the network interface"); 3341 MODULE_PARM_DESC(disable_net, "Disable the network interface");
3351 module_param(disable_net, int, S_IRUGO | S_IWUSR); 3342 module_param(disable_net, int, S_IRUGO | S_IWUSR);
3352 3343
drivers/s390/char/sclp_tty.c
1 /* 1 /*
2 * SCLP line mode terminal driver. 2 * SCLP line mode terminal driver.
3 * 3 *
4 * S390 version 4 * S390 version
5 * Copyright IBM Corp. 1999 5 * Copyright IBM Corp. 1999
6 * Author(s): Martin Peschke <mpeschke@de.ibm.com> 6 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
7 * Martin Schwidefsky <schwidefsky@de.ibm.com> 7 * Martin Schwidefsky <schwidefsky@de.ibm.com>
8 */ 8 */
9 9
10 #include <linux/module.h> 10 #include <linux/module.h>
11 #include <linux/kmod.h> 11 #include <linux/kmod.h>
12 #include <linux/tty.h> 12 #include <linux/tty.h>
13 #include <linux/tty_driver.h> 13 #include <linux/tty_driver.h>
14 #include <linux/tty_flip.h> 14 #include <linux/tty_flip.h>
15 #include <linux/err.h> 15 #include <linux/err.h>
16 #include <linux/init.h> 16 #include <linux/init.h>
17 #include <linux/interrupt.h> 17 #include <linux/interrupt.h>
18 #include <linux/gfp.h> 18 #include <linux/gfp.h>
19 #include <asm/uaccess.h> 19 #include <asm/uaccess.h>
20 20
21 #include "ctrlchar.h" 21 #include "ctrlchar.h"
22 #include "sclp.h" 22 #include "sclp.h"
23 #include "sclp_rw.h" 23 #include "sclp_rw.h"
24 #include "sclp_tty.h" 24 #include "sclp_tty.h"
25 25
26 /* 26 /*
27 * size of a buffer that collects single characters coming in 27 * size of a buffer that collects single characters coming in
28 * via sclp_tty_put_char() 28 * via sclp_tty_put_char()
29 */ 29 */
30 #define SCLP_TTY_BUF_SIZE 512 30 #define SCLP_TTY_BUF_SIZE 512
31 31
32 /* 32 /*
33 * There is exactly one SCLP terminal, so we can keep things simple 33 * There is exactly one SCLP terminal, so we can keep things simple
34 * and allocate all variables statically. 34 * and allocate all variables statically.
35 */ 35 */
36 36
37 /* Lock to guard over changes to global variables. */ 37 /* Lock to guard over changes to global variables. */
38 static spinlock_t sclp_tty_lock; 38 static spinlock_t sclp_tty_lock;
39 /* List of free pages that can be used for console output buffering. */ 39 /* List of free pages that can be used for console output buffering. */
40 static struct list_head sclp_tty_pages; 40 static struct list_head sclp_tty_pages;
41 /* List of full struct sclp_buffer structures ready for output. */ 41 /* List of full struct sclp_buffer structures ready for output. */
42 static struct list_head sclp_tty_outqueue; 42 static struct list_head sclp_tty_outqueue;
43 /* Counter how many buffers are emitted. */ 43 /* Counter how many buffers are emitted. */
44 static int sclp_tty_buffer_count; 44 static int sclp_tty_buffer_count;
45 /* Pointer to current console buffer. */ 45 /* Pointer to current console buffer. */
46 static struct sclp_buffer *sclp_ttybuf; 46 static struct sclp_buffer *sclp_ttybuf;
47 /* Timer for delayed output of console messages. */ 47 /* Timer for delayed output of console messages. */
48 static struct timer_list sclp_tty_timer; 48 static struct timer_list sclp_tty_timer;
49 49
50 static struct tty_port sclp_port; 50 static struct tty_port sclp_port;
51 static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE]; 51 static unsigned char sclp_tty_chars[SCLP_TTY_BUF_SIZE];
52 static unsigned short int sclp_tty_chars_count; 52 static unsigned short int sclp_tty_chars_count;
53 53
54 struct tty_driver *sclp_tty_driver; 54 struct tty_driver *sclp_tty_driver;
55 55
56 static int sclp_tty_tolower; 56 static int sclp_tty_tolower;
57 static int sclp_tty_columns = 80; 57 static int sclp_tty_columns = 80;
58 58
59 #define SPACES_PER_TAB 8 59 #define SPACES_PER_TAB 8
60 #define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */ 60 #define CASE_DELIMITER 0x6c /* to separate upper and lower case (% in EBCDIC) */
61 61
62 /* This routine is called whenever we try to open a SCLP terminal. */ 62 /* This routine is called whenever we try to open a SCLP terminal. */
63 static int 63 static int
64 sclp_tty_open(struct tty_struct *tty, struct file *filp) 64 sclp_tty_open(struct tty_struct *tty, struct file *filp)
65 { 65 {
66 tty_port_tty_set(&sclp_port, tty); 66 tty_port_tty_set(&sclp_port, tty);
67 tty->driver_data = NULL; 67 tty->driver_data = NULL;
68 sclp_port.low_latency = 0; 68 sclp_port.low_latency = 0;
69 return 0; 69 return 0;
70 } 70 }
71 71
72 /* This routine is called when the SCLP terminal is closed. */ 72 /* This routine is called when the SCLP terminal is closed. */
73 static void 73 static void
74 sclp_tty_close(struct tty_struct *tty, struct file *filp) 74 sclp_tty_close(struct tty_struct *tty, struct file *filp)
75 { 75 {
76 if (tty->count > 1) 76 if (tty->count > 1)
77 return; 77 return;
78 tty_port_tty_set(&sclp_port, NULL); 78 tty_port_tty_set(&sclp_port, NULL);
79 } 79 }
80 80
81 /* 81 /*
82 * This routine returns the numbers of characters the tty driver 82 * This routine returns the numbers of characters the tty driver
83 * will accept for queuing to be written. This number is subject 83 * will accept for queuing to be written. This number is subject
84 * to change as output buffers get emptied, or if the output flow 84 * to change as output buffers get emptied, or if the output flow
85 * control is acted. This is not an exact number because not every 85 * control is acted. This is not an exact number because not every
86 * character needs the same space in the sccb. The worst case is 86 * character needs the same space in the sccb. The worst case is
87 * a string of newlines. Every newlines creates a new mto which 87 * a string of newlines. Every newlines creates a new mto which
88 * needs 8 bytes. 88 * needs 8 bytes.
89 */ 89 */
90 static int 90 static int
91 sclp_tty_write_room (struct tty_struct *tty) 91 sclp_tty_write_room (struct tty_struct *tty)
92 { 92 {
93 unsigned long flags; 93 unsigned long flags;
94 struct list_head *l; 94 struct list_head *l;
95 int count; 95 int count;
96 96
97 spin_lock_irqsave(&sclp_tty_lock, flags); 97 spin_lock_irqsave(&sclp_tty_lock, flags);
98 count = 0; 98 count = 0;
99 if (sclp_ttybuf != NULL) 99 if (sclp_ttybuf != NULL)
100 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto); 100 count = sclp_buffer_space(sclp_ttybuf) / sizeof(struct mto);
101 list_for_each(l, &sclp_tty_pages) 101 list_for_each(l, &sclp_tty_pages)
102 count += NR_EMPTY_MTO_PER_SCCB; 102 count += NR_EMPTY_MTO_PER_SCCB;
103 spin_unlock_irqrestore(&sclp_tty_lock, flags); 103 spin_unlock_irqrestore(&sclp_tty_lock, flags);
104 return count; 104 return count;
105 } 105 }
106 106
107 static void 107 static void
108 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc) 108 sclp_ttybuf_callback(struct sclp_buffer *buffer, int rc)
109 { 109 {
110 struct tty_struct *tty;
111 unsigned long flags; 110 unsigned long flags;
112 void *page; 111 void *page;
113 112
114 do { 113 do {
115 page = sclp_unmake_buffer(buffer); 114 page = sclp_unmake_buffer(buffer);
116 spin_lock_irqsave(&sclp_tty_lock, flags); 115 spin_lock_irqsave(&sclp_tty_lock, flags);
117 /* Remove buffer from outqueue */ 116 /* Remove buffer from outqueue */
118 list_del(&buffer->list); 117 list_del(&buffer->list);
119 sclp_tty_buffer_count--; 118 sclp_tty_buffer_count--;
120 list_add_tail((struct list_head *) page, &sclp_tty_pages); 119 list_add_tail((struct list_head *) page, &sclp_tty_pages);
121 /* Check if there is a pending buffer on the out queue. */ 120 /* Check if there is a pending buffer on the out queue. */
122 buffer = NULL; 121 buffer = NULL;
123 if (!list_empty(&sclp_tty_outqueue)) 122 if (!list_empty(&sclp_tty_outqueue))
124 buffer = list_entry(sclp_tty_outqueue.next, 123 buffer = list_entry(sclp_tty_outqueue.next,
125 struct sclp_buffer, list); 124 struct sclp_buffer, list);
126 spin_unlock_irqrestore(&sclp_tty_lock, flags); 125 spin_unlock_irqrestore(&sclp_tty_lock, flags);
127 } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback)); 126 } while (buffer && sclp_emit_buffer(buffer, sclp_ttybuf_callback));
128 /* check if the tty needs a wake up call */ 127
129 tty = tty_port_tty_get(&sclp_port); 128 tty_port_tty_wakeup(&sclp_port);
130 if (tty != NULL) {
131 tty_wakeup(tty);
132 tty_kref_put(tty);
133 }
134 } 129 }
135 130
136 static inline void 131 static inline void
137 __sclp_ttybuf_emit(struct sclp_buffer *buffer) 132 __sclp_ttybuf_emit(struct sclp_buffer *buffer)
138 { 133 {
139 unsigned long flags; 134 unsigned long flags;
140 int count; 135 int count;
141 int rc; 136 int rc;
142 137
143 spin_lock_irqsave(&sclp_tty_lock, flags); 138 spin_lock_irqsave(&sclp_tty_lock, flags);
144 list_add_tail(&buffer->list, &sclp_tty_outqueue); 139 list_add_tail(&buffer->list, &sclp_tty_outqueue);
145 count = sclp_tty_buffer_count++; 140 count = sclp_tty_buffer_count++;
146 spin_unlock_irqrestore(&sclp_tty_lock, flags); 141 spin_unlock_irqrestore(&sclp_tty_lock, flags);
147 if (count) 142 if (count)
148 return; 143 return;
149 rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback); 144 rc = sclp_emit_buffer(buffer, sclp_ttybuf_callback);
150 if (rc) 145 if (rc)
151 sclp_ttybuf_callback(buffer, rc); 146 sclp_ttybuf_callback(buffer, rc);
152 } 147 }
153 148
154 /* 149 /*
155 * When this routine is called from the timer then we flush the 150 * When this routine is called from the timer then we flush the
156 * temporary write buffer. 151 * temporary write buffer.
157 */ 152 */
158 static void 153 static void
159 sclp_tty_timeout(unsigned long data) 154 sclp_tty_timeout(unsigned long data)
160 { 155 {
161 unsigned long flags; 156 unsigned long flags;
162 struct sclp_buffer *buf; 157 struct sclp_buffer *buf;
163 158
164 spin_lock_irqsave(&sclp_tty_lock, flags); 159 spin_lock_irqsave(&sclp_tty_lock, flags);
165 buf = sclp_ttybuf; 160 buf = sclp_ttybuf;
166 sclp_ttybuf = NULL; 161 sclp_ttybuf = NULL;
167 spin_unlock_irqrestore(&sclp_tty_lock, flags); 162 spin_unlock_irqrestore(&sclp_tty_lock, flags);
168 163
169 if (buf != NULL) { 164 if (buf != NULL) {
170 __sclp_ttybuf_emit(buf); 165 __sclp_ttybuf_emit(buf);
171 } 166 }
172 } 167 }
173 168
174 /* 169 /*
175 * Write a string to the sclp tty. 170 * Write a string to the sclp tty.
176 */ 171 */
177 static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail) 172 static int sclp_tty_write_string(const unsigned char *str, int count, int may_fail)
178 { 173 {
179 unsigned long flags; 174 unsigned long flags;
180 void *page; 175 void *page;
181 int written; 176 int written;
182 int overall_written; 177 int overall_written;
183 struct sclp_buffer *buf; 178 struct sclp_buffer *buf;
184 179
185 if (count <= 0) 180 if (count <= 0)
186 return 0; 181 return 0;
187 overall_written = 0; 182 overall_written = 0;
188 spin_lock_irqsave(&sclp_tty_lock, flags); 183 spin_lock_irqsave(&sclp_tty_lock, flags);
189 do { 184 do {
190 /* Create a sclp output buffer if none exists yet */ 185 /* Create a sclp output buffer if none exists yet */
191 if (sclp_ttybuf == NULL) { 186 if (sclp_ttybuf == NULL) {
192 while (list_empty(&sclp_tty_pages)) { 187 while (list_empty(&sclp_tty_pages)) {
193 spin_unlock_irqrestore(&sclp_tty_lock, flags); 188 spin_unlock_irqrestore(&sclp_tty_lock, flags);
194 if (may_fail) 189 if (may_fail)
195 goto out; 190 goto out;
196 else 191 else
197 sclp_sync_wait(); 192 sclp_sync_wait();
198 spin_lock_irqsave(&sclp_tty_lock, flags); 193 spin_lock_irqsave(&sclp_tty_lock, flags);
199 } 194 }
200 page = sclp_tty_pages.next; 195 page = sclp_tty_pages.next;
201 list_del((struct list_head *) page); 196 list_del((struct list_head *) page);
202 sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns, 197 sclp_ttybuf = sclp_make_buffer(page, sclp_tty_columns,
203 SPACES_PER_TAB); 198 SPACES_PER_TAB);
204 } 199 }
205 /* try to write the string to the current output buffer */ 200 /* try to write the string to the current output buffer */
206 written = sclp_write(sclp_ttybuf, str, count); 201 written = sclp_write(sclp_ttybuf, str, count);
207 overall_written += written; 202 overall_written += written;
208 if (written == count) 203 if (written == count)
209 break; 204 break;
210 /* 205 /*
211 * Not all characters could be written to the current 206 * Not all characters could be written to the current
212 * output buffer. Emit the buffer, create a new buffer 207 * output buffer. Emit the buffer, create a new buffer
213 * and then output the rest of the string. 208 * and then output the rest of the string.
214 */ 209 */
215 buf = sclp_ttybuf; 210 buf = sclp_ttybuf;
216 sclp_ttybuf = NULL; 211 sclp_ttybuf = NULL;
217 spin_unlock_irqrestore(&sclp_tty_lock, flags); 212 spin_unlock_irqrestore(&sclp_tty_lock, flags);
218 __sclp_ttybuf_emit(buf); 213 __sclp_ttybuf_emit(buf);
219 spin_lock_irqsave(&sclp_tty_lock, flags); 214 spin_lock_irqsave(&sclp_tty_lock, flags);
220 str += written; 215 str += written;
221 count -= written; 216 count -= written;
222 } while (count > 0); 217 } while (count > 0);
223 /* Setup timer to output current console buffer after 1/10 second */ 218 /* Setup timer to output current console buffer after 1/10 second */
224 if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) && 219 if (sclp_ttybuf && sclp_chars_in_buffer(sclp_ttybuf) &&
225 !timer_pending(&sclp_tty_timer)) { 220 !timer_pending(&sclp_tty_timer)) {
226 init_timer(&sclp_tty_timer); 221 init_timer(&sclp_tty_timer);
227 sclp_tty_timer.function = sclp_tty_timeout; 222 sclp_tty_timer.function = sclp_tty_timeout;
228 sclp_tty_timer.data = 0UL; 223 sclp_tty_timer.data = 0UL;
229 sclp_tty_timer.expires = jiffies + HZ/10; 224 sclp_tty_timer.expires = jiffies + HZ/10;
230 add_timer(&sclp_tty_timer); 225 add_timer(&sclp_tty_timer);
231 } 226 }
232 spin_unlock_irqrestore(&sclp_tty_lock, flags); 227 spin_unlock_irqrestore(&sclp_tty_lock, flags);
233 out: 228 out:
234 return overall_written; 229 return overall_written;
235 } 230 }
236 231
237 /* 232 /*
238 * This routine is called by the kernel to write a series of characters to the 233 * This routine is called by the kernel to write a series of characters to the
239 * tty device. The characters may come from user space or kernel space. This 234 * tty device. The characters may come from user space or kernel space. This
240 * routine will return the number of characters actually accepted for writing. 235 * routine will return the number of characters actually accepted for writing.
241 */ 236 */
242 static int 237 static int
243 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) 238 sclp_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
244 { 239 {
245 if (sclp_tty_chars_count > 0) { 240 if (sclp_tty_chars_count > 0) {
246 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); 241 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
247 sclp_tty_chars_count = 0; 242 sclp_tty_chars_count = 0;
248 } 243 }
249 return sclp_tty_write_string(buf, count, 1); 244 return sclp_tty_write_string(buf, count, 1);
250 } 245 }
251 246
252 /* 247 /*
253 * This routine is called by the kernel to write a single character to the tty 248 * This routine is called by the kernel to write a single character to the tty
254 * device. If the kernel uses this routine, it must call the flush_chars() 249 * device. If the kernel uses this routine, it must call the flush_chars()
255 * routine (if defined) when it is done stuffing characters into the driver. 250 * routine (if defined) when it is done stuffing characters into the driver.
256 * 251 *
257 * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver. 252 * Characters provided to sclp_tty_put_char() are buffered by the SCLP driver.
258 * If the given character is a '\n' the contents of the SCLP write buffer 253 * If the given character is a '\n' the contents of the SCLP write buffer
259 * - including previous characters from sclp_tty_put_char() and strings from 254 * - including previous characters from sclp_tty_put_char() and strings from
260 * sclp_write() without final '\n' - will be written. 255 * sclp_write() without final '\n' - will be written.
261 */ 256 */
262 static int 257 static int
263 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch) 258 sclp_tty_put_char(struct tty_struct *tty, unsigned char ch)
264 { 259 {
265 sclp_tty_chars[sclp_tty_chars_count++] = ch; 260 sclp_tty_chars[sclp_tty_chars_count++] = ch;
266 if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) { 261 if (ch == '\n' || sclp_tty_chars_count >= SCLP_TTY_BUF_SIZE) {
267 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); 262 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
268 sclp_tty_chars_count = 0; 263 sclp_tty_chars_count = 0;
269 } 264 }
270 return 1; 265 return 1;
271 } 266 }
272 267
273 /* 268 /*
274 * This routine is called by the kernel after it has written a series of 269 * This routine is called by the kernel after it has written a series of
275 * characters to the tty device using put_char(). 270 * characters to the tty device using put_char().
276 */ 271 */
277 static void 272 static void
278 sclp_tty_flush_chars(struct tty_struct *tty) 273 sclp_tty_flush_chars(struct tty_struct *tty)
279 { 274 {
280 if (sclp_tty_chars_count > 0) { 275 if (sclp_tty_chars_count > 0) {
281 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); 276 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
282 sclp_tty_chars_count = 0; 277 sclp_tty_chars_count = 0;
283 } 278 }
284 } 279 }
285 280
286 /* 281 /*
287 * This routine returns the number of characters in the write buffer of the 282 * This routine returns the number of characters in the write buffer of the
288 * SCLP driver. The provided number includes all characters that are stored 283 * SCLP driver. The provided number includes all characters that are stored
289 * in the SCCB (will be written next time the SCLP is not busy) as well as 284 * in the SCCB (will be written next time the SCLP is not busy) as well as
290 * characters in the write buffer (will not be written as long as there is a 285 * characters in the write buffer (will not be written as long as there is a
291 * final line feed missing). 286 * final line feed missing).
292 */ 287 */
293 static int 288 static int
294 sclp_tty_chars_in_buffer(struct tty_struct *tty) 289 sclp_tty_chars_in_buffer(struct tty_struct *tty)
295 { 290 {
296 unsigned long flags; 291 unsigned long flags;
297 struct list_head *l; 292 struct list_head *l;
298 struct sclp_buffer *t; 293 struct sclp_buffer *t;
299 int count; 294 int count;
300 295
301 spin_lock_irqsave(&sclp_tty_lock, flags); 296 spin_lock_irqsave(&sclp_tty_lock, flags);
302 count = 0; 297 count = 0;
303 if (sclp_ttybuf != NULL) 298 if (sclp_ttybuf != NULL)
304 count = sclp_chars_in_buffer(sclp_ttybuf); 299 count = sclp_chars_in_buffer(sclp_ttybuf);
305 list_for_each(l, &sclp_tty_outqueue) { 300 list_for_each(l, &sclp_tty_outqueue) {
306 t = list_entry(l, struct sclp_buffer, list); 301 t = list_entry(l, struct sclp_buffer, list);
307 count += sclp_chars_in_buffer(t); 302 count += sclp_chars_in_buffer(t);
308 } 303 }
309 spin_unlock_irqrestore(&sclp_tty_lock, flags); 304 spin_unlock_irqrestore(&sclp_tty_lock, flags);
310 return count; 305 return count;
311 } 306 }
312 307
313 /* 308 /*
314 * removes all content from buffers of low level driver 309 * removes all content from buffers of low level driver
315 */ 310 */
316 static void 311 static void
317 sclp_tty_flush_buffer(struct tty_struct *tty) 312 sclp_tty_flush_buffer(struct tty_struct *tty)
318 { 313 {
319 if (sclp_tty_chars_count > 0) { 314 if (sclp_tty_chars_count > 0) {
320 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0); 315 sclp_tty_write_string(sclp_tty_chars, sclp_tty_chars_count, 0);
321 sclp_tty_chars_count = 0; 316 sclp_tty_chars_count = 0;
322 } 317 }
323 } 318 }
324 319
325 /* 320 /*
326 * push input to tty 321 * push input to tty
327 */ 322 */
328 static void 323 static void
329 sclp_tty_input(unsigned char* buf, unsigned int count) 324 sclp_tty_input(unsigned char* buf, unsigned int count)
330 { 325 {
331 struct tty_struct *tty = tty_port_tty_get(&sclp_port); 326 struct tty_struct *tty = tty_port_tty_get(&sclp_port);
332 unsigned int cchar; 327 unsigned int cchar;
333 328
334 /* 329 /*
335 * If this tty driver is currently closed 330 * If this tty driver is currently closed
336 * then throw the received input away. 331 * then throw the received input away.
337 */ 332 */
338 if (tty == NULL) 333 if (tty == NULL)
339 return; 334 return;
340 cchar = ctrlchar_handle(buf, count, tty); 335 cchar = ctrlchar_handle(buf, count, tty);
341 switch (cchar & CTRLCHAR_MASK) { 336 switch (cchar & CTRLCHAR_MASK) {
342 case CTRLCHAR_SYSRQ: 337 case CTRLCHAR_SYSRQ:
343 break; 338 break;
344 case CTRLCHAR_CTRL: 339 case CTRLCHAR_CTRL:
345 tty_insert_flip_char(&sclp_port, cchar, TTY_NORMAL); 340 tty_insert_flip_char(&sclp_port, cchar, TTY_NORMAL);
346 tty_flip_buffer_push(&sclp_port); 341 tty_flip_buffer_push(&sclp_port);
347 break; 342 break;
348 case CTRLCHAR_NONE: 343 case CTRLCHAR_NONE:
349 /* send (normal) input to line discipline */ 344 /* send (normal) input to line discipline */
350 if (count < 2 || 345 if (count < 2 ||
351 (strncmp((const char *) buf + count - 2, "^n", 2) && 346 (strncmp((const char *) buf + count - 2, "^n", 2) &&
352 strncmp((const char *) buf + count - 2, "\252n", 2))) { 347 strncmp((const char *) buf + count - 2, "\252n", 2))) {
353 /* add the auto \n */ 348 /* add the auto \n */
354 tty_insert_flip_string(&sclp_port, buf, count); 349 tty_insert_flip_string(&sclp_port, buf, count);
355 tty_insert_flip_char(&sclp_port, '\n', TTY_NORMAL); 350 tty_insert_flip_char(&sclp_port, '\n', TTY_NORMAL);
356 } else 351 } else
357 tty_insert_flip_string(&sclp_port, buf, count - 2); 352 tty_insert_flip_string(&sclp_port, buf, count - 2);
358 tty_flip_buffer_push(&sclp_port); 353 tty_flip_buffer_push(&sclp_port);
359 break; 354 break;
360 } 355 }
361 tty_kref_put(tty); 356 tty_kref_put(tty);
362 } 357 }
363 358
364 /* 359 /*
365 * get a EBCDIC string in upper/lower case, 360 * get a EBCDIC string in upper/lower case,
366 * find out characters in lower/upper case separated by a special character, 361 * find out characters in lower/upper case separated by a special character,
367 * modifiy original string, 362 * modifiy original string,
368 * returns length of resulting string 363 * returns length of resulting string
369 */ 364 */
370 static int sclp_switch_cases(unsigned char *buf, int count) 365 static int sclp_switch_cases(unsigned char *buf, int count)
371 { 366 {
372 unsigned char *ip, *op; 367 unsigned char *ip, *op;
373 int toggle; 368 int toggle;
374 369
375 /* initially changing case is off */ 370 /* initially changing case is off */
376 toggle = 0; 371 toggle = 0;
377 ip = op = buf; 372 ip = op = buf;
378 while (count-- > 0) { 373 while (count-- > 0) {
379 /* compare with special character */ 374 /* compare with special character */
380 if (*ip == CASE_DELIMITER) { 375 if (*ip == CASE_DELIMITER) {
381 /* followed by another special character? */ 376 /* followed by another special character? */
382 if (count && ip[1] == CASE_DELIMITER) { 377 if (count && ip[1] == CASE_DELIMITER) {
383 /* 378 /*
384 * ... then put a single copy of the special 379 * ... then put a single copy of the special
385 * character to the output string 380 * character to the output string
386 */ 381 */
387 *op++ = *ip++; 382 *op++ = *ip++;
388 count--; 383 count--;
389 } else 384 } else
390 /* 385 /*
391 * ... special character follower by a normal 386 * ... special character follower by a normal
392 * character toggles the case change behaviour 387 * character toggles the case change behaviour
393 */ 388 */
394 toggle = ~toggle; 389 toggle = ~toggle;
395 /* skip special character */ 390 /* skip special character */
396 ip++; 391 ip++;
397 } else 392 } else
398 /* not the special character */ 393 /* not the special character */
399 if (toggle) 394 if (toggle)
400 /* but case switching is on */ 395 /* but case switching is on */
401 if (sclp_tty_tolower) 396 if (sclp_tty_tolower)
402 /* switch to uppercase */ 397 /* switch to uppercase */
403 *op++ = _ebc_toupper[(int) *ip++]; 398 *op++ = _ebc_toupper[(int) *ip++];
404 else 399 else
405 /* switch to lowercase */ 400 /* switch to lowercase */
406 *op++ = _ebc_tolower[(int) *ip++]; 401 *op++ = _ebc_tolower[(int) *ip++];
407 else 402 else
408 /* no case switching, copy the character */ 403 /* no case switching, copy the character */
409 *op++ = *ip++; 404 *op++ = *ip++;
410 } 405 }
411 /* return length of reformatted string. */ 406 /* return length of reformatted string. */
412 return op - buf; 407 return op - buf;
413 } 408 }
414 409
415 static void sclp_get_input(struct gds_subvector *sv) 410 static void sclp_get_input(struct gds_subvector *sv)
416 { 411 {
417 unsigned char *str; 412 unsigned char *str;
418 int count; 413 int count;
419 414
420 str = (unsigned char *) (sv + 1); 415 str = (unsigned char *) (sv + 1);
421 count = sv->length - sizeof(*sv); 416 count = sv->length - sizeof(*sv);
422 if (sclp_tty_tolower) 417 if (sclp_tty_tolower)
423 EBC_TOLOWER(str, count); 418 EBC_TOLOWER(str, count);
424 count = sclp_switch_cases(str, count); 419 count = sclp_switch_cases(str, count);
425 /* convert EBCDIC to ASCII (modify original input in SCCB) */ 420 /* convert EBCDIC to ASCII (modify original input in SCCB) */
426 sclp_ebcasc_str(str, count); 421 sclp_ebcasc_str(str, count);
427 422
428 /* transfer input to high level driver */ 423 /* transfer input to high level driver */
429 sclp_tty_input(str, count); 424 sclp_tty_input(str, count);
430 } 425 }
431 426
432 static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv) 427 static inline void sclp_eval_selfdeftextmsg(struct gds_subvector *sv)
433 { 428 {
434 void *end; 429 void *end;
435 430
436 end = (void *) sv + sv->length; 431 end = (void *) sv + sv->length;
437 for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length) 432 for (sv = sv + 1; (void *) sv < end; sv = (void *) sv + sv->length)
438 if (sv->key == 0x30) 433 if (sv->key == 0x30)
439 sclp_get_input(sv); 434 sclp_get_input(sv);
440 } 435 }
441 436
442 static inline void sclp_eval_textcmd(struct gds_vector *v) 437 static inline void sclp_eval_textcmd(struct gds_vector *v)
443 { 438 {
444 struct gds_subvector *sv; 439 struct gds_subvector *sv;
445 void *end; 440 void *end;
446 441
447 end = (void *) v + v->length; 442 end = (void *) v + v->length;
448 for (sv = (struct gds_subvector *) (v + 1); 443 for (sv = (struct gds_subvector *) (v + 1);
449 (void *) sv < end; sv = (void *) sv + sv->length) 444 (void *) sv < end; sv = (void *) sv + sv->length)
450 if (sv->key == GDS_KEY_SELFDEFTEXTMSG) 445 if (sv->key == GDS_KEY_SELFDEFTEXTMSG)
451 sclp_eval_selfdeftextmsg(sv); 446 sclp_eval_selfdeftextmsg(sv);
452 447
453 } 448 }
454 449
455 static inline void sclp_eval_cpmsu(struct gds_vector *v) 450 static inline void sclp_eval_cpmsu(struct gds_vector *v)
456 { 451 {
457 void *end; 452 void *end;
458 453
459 end = (void *) v + v->length; 454 end = (void *) v + v->length;
460 for (v = v + 1; (void *) v < end; v = (void *) v + v->length) 455 for (v = v + 1; (void *) v < end; v = (void *) v + v->length)
461 if (v->gds_id == GDS_ID_TEXTCMD) 456 if (v->gds_id == GDS_ID_TEXTCMD)
462 sclp_eval_textcmd(v); 457 sclp_eval_textcmd(v);
463 } 458 }
464 459
465 460
466 static inline void sclp_eval_mdsmu(struct gds_vector *v) 461 static inline void sclp_eval_mdsmu(struct gds_vector *v)
467 { 462 {
468 v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU); 463 v = sclp_find_gds_vector(v + 1, (void *) v + v->length, GDS_ID_CPMSU);
469 if (v) 464 if (v)
470 sclp_eval_cpmsu(v); 465 sclp_eval_cpmsu(v);
471 } 466 }
472 467
473 static void sclp_tty_receiver(struct evbuf_header *evbuf) 468 static void sclp_tty_receiver(struct evbuf_header *evbuf)
474 { 469 {
475 struct gds_vector *v; 470 struct gds_vector *v;
476 471
477 v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length, 472 v = sclp_find_gds_vector(evbuf + 1, (void *) evbuf + evbuf->length,
478 GDS_ID_MDSMU); 473 GDS_ID_MDSMU);
479 if (v) 474 if (v)
480 sclp_eval_mdsmu(v); 475 sclp_eval_mdsmu(v);
481 } 476 }
482 477
483 static void 478 static void
484 sclp_tty_state_change(struct sclp_register *reg) 479 sclp_tty_state_change(struct sclp_register *reg)
485 { 480 {
486 } 481 }
487 482
488 static struct sclp_register sclp_input_event = 483 static struct sclp_register sclp_input_event =
489 { 484 {
490 .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK, 485 .receive_mask = EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK,
491 .state_change_fn = sclp_tty_state_change, 486 .state_change_fn = sclp_tty_state_change,
492 .receiver_fn = sclp_tty_receiver 487 .receiver_fn = sclp_tty_receiver
493 }; 488 };
494 489
495 static const struct tty_operations sclp_ops = { 490 static const struct tty_operations sclp_ops = {
496 .open = sclp_tty_open, 491 .open = sclp_tty_open,
497 .close = sclp_tty_close, 492 .close = sclp_tty_close,
498 .write = sclp_tty_write, 493 .write = sclp_tty_write,
499 .put_char = sclp_tty_put_char, 494 .put_char = sclp_tty_put_char,
500 .flush_chars = sclp_tty_flush_chars, 495 .flush_chars = sclp_tty_flush_chars,
501 .write_room = sclp_tty_write_room, 496 .write_room = sclp_tty_write_room,
502 .chars_in_buffer = sclp_tty_chars_in_buffer, 497 .chars_in_buffer = sclp_tty_chars_in_buffer,
503 .flush_buffer = sclp_tty_flush_buffer, 498 .flush_buffer = sclp_tty_flush_buffer,
504 }; 499 };
505 500
506 static int __init 501 static int __init
507 sclp_tty_init(void) 502 sclp_tty_init(void)
508 { 503 {
509 struct tty_driver *driver; 504 struct tty_driver *driver;
510 void *page; 505 void *page;
511 int i; 506 int i;
512 int rc; 507 int rc;
513 508
514 if (!CONSOLE_IS_SCLP) 509 if (!CONSOLE_IS_SCLP)
515 return 0; 510 return 0;
516 driver = alloc_tty_driver(1); 511 driver = alloc_tty_driver(1);
517 if (!driver) 512 if (!driver)
518 return -ENOMEM; 513 return -ENOMEM;
519 514
520 rc = sclp_rw_init(); 515 rc = sclp_rw_init();
521 if (rc) { 516 if (rc) {
522 put_tty_driver(driver); 517 put_tty_driver(driver);
523 return rc; 518 return rc;
524 } 519 }
525 /* Allocate pages for output buffering */ 520 /* Allocate pages for output buffering */
526 INIT_LIST_HEAD(&sclp_tty_pages); 521 INIT_LIST_HEAD(&sclp_tty_pages);
527 for (i = 0; i < MAX_KMEM_PAGES; i++) { 522 for (i = 0; i < MAX_KMEM_PAGES; i++) {
528 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 523 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
529 if (page == NULL) { 524 if (page == NULL) {
530 put_tty_driver(driver); 525 put_tty_driver(driver);
531 return -ENOMEM; 526 return -ENOMEM;
532 } 527 }
533 list_add_tail((struct list_head *) page, &sclp_tty_pages); 528 list_add_tail((struct list_head *) page, &sclp_tty_pages);
534 } 529 }
535 INIT_LIST_HEAD(&sclp_tty_outqueue); 530 INIT_LIST_HEAD(&sclp_tty_outqueue);
536 spin_lock_init(&sclp_tty_lock); 531 spin_lock_init(&sclp_tty_lock);
537 init_timer(&sclp_tty_timer); 532 init_timer(&sclp_tty_timer);
538 sclp_ttybuf = NULL; 533 sclp_ttybuf = NULL;
539 sclp_tty_buffer_count = 0; 534 sclp_tty_buffer_count = 0;
540 if (MACHINE_IS_VM) { 535 if (MACHINE_IS_VM) {
541 /* 536 /*
542 * save 4 characters for the CPU number 537 * save 4 characters for the CPU number
543 * written at start of each line by VM/CP 538 * written at start of each line by VM/CP
544 */ 539 */
545 sclp_tty_columns = 76; 540 sclp_tty_columns = 76;
546 /* case input lines to lowercase */ 541 /* case input lines to lowercase */
547 sclp_tty_tolower = 1; 542 sclp_tty_tolower = 1;
548 } 543 }
549 sclp_tty_chars_count = 0; 544 sclp_tty_chars_count = 0;
550 545
551 rc = sclp_register(&sclp_input_event); 546 rc = sclp_register(&sclp_input_event);
552 if (rc) { 547 if (rc) {
553 put_tty_driver(driver); 548 put_tty_driver(driver);
554 return rc; 549 return rc;
555 } 550 }
556 551
557 tty_port_init(&sclp_port); 552 tty_port_init(&sclp_port);
558 553
559 driver->driver_name = "sclp_line"; 554 driver->driver_name = "sclp_line";
560 driver->name = "sclp_line"; 555 driver->name = "sclp_line";
561 driver->major = TTY_MAJOR; 556 driver->major = TTY_MAJOR;
562 driver->minor_start = 64; 557 driver->minor_start = 64;
563 driver->type = TTY_DRIVER_TYPE_SYSTEM; 558 driver->type = TTY_DRIVER_TYPE_SYSTEM;
564 driver->subtype = SYSTEM_TYPE_TTY; 559 driver->subtype = SYSTEM_TYPE_TTY;
565 driver->init_termios = tty_std_termios; 560 driver->init_termios = tty_std_termios;
566 driver->init_termios.c_iflag = IGNBRK | IGNPAR; 561 driver->init_termios.c_iflag = IGNBRK | IGNPAR;
567 driver->init_termios.c_oflag = ONLCR | XTABS; 562 driver->init_termios.c_oflag = ONLCR | XTABS;
568 driver->init_termios.c_lflag = ISIG | ECHO; 563 driver->init_termios.c_lflag = ISIG | ECHO;
569 driver->flags = TTY_DRIVER_REAL_RAW; 564 driver->flags = TTY_DRIVER_REAL_RAW;
570 tty_set_operations(driver, &sclp_ops); 565 tty_set_operations(driver, &sclp_ops);
571 tty_port_link_device(&sclp_port, driver, 0); 566 tty_port_link_device(&sclp_port, driver, 0);
572 rc = tty_register_driver(driver); 567 rc = tty_register_driver(driver);
573 if (rc) { 568 if (rc) {
574 put_tty_driver(driver); 569 put_tty_driver(driver);
575 tty_port_destroy(&sclp_port); 570 tty_port_destroy(&sclp_port);
576 return rc; 571 return rc;
577 } 572 }
578 sclp_tty_driver = driver; 573 sclp_tty_driver = driver;
579 return 0; 574 return 0;
580 } 575 }
581 module_init(sclp_tty_init); 576 module_init(sclp_tty_init);
582 577
drivers/s390/char/sclp_vt220.c
1 /* 1 /*
2 * SCLP VT220 terminal driver. 2 * SCLP VT220 terminal driver.
3 * 3 *
4 * Copyright IBM Corp. 2003, 2009 4 * Copyright IBM Corp. 2003, 2009
5 * 5 *
6 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com> 6 * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
7 */ 7 */
8 8
9 #include <linux/module.h> 9 #include <linux/module.h>
10 #include <linux/spinlock.h> 10 #include <linux/spinlock.h>
11 #include <linux/list.h> 11 #include <linux/list.h>
12 #include <linux/wait.h> 12 #include <linux/wait.h>
13 #include <linux/timer.h> 13 #include <linux/timer.h>
14 #include <linux/kernel.h> 14 #include <linux/kernel.h>
15 #include <linux/tty.h> 15 #include <linux/tty.h>
16 #include <linux/tty_driver.h> 16 #include <linux/tty_driver.h>
17 #include <linux/tty_flip.h> 17 #include <linux/tty_flip.h>
18 #include <linux/errno.h> 18 #include <linux/errno.h>
19 #include <linux/mm.h> 19 #include <linux/mm.h>
20 #include <linux/major.h> 20 #include <linux/major.h>
21 #include <linux/console.h> 21 #include <linux/console.h>
22 #include <linux/kdev_t.h> 22 #include <linux/kdev_t.h>
23 #include <linux/interrupt.h> 23 #include <linux/interrupt.h>
24 #include <linux/init.h> 24 #include <linux/init.h>
25 #include <linux/reboot.h> 25 #include <linux/reboot.h>
26 #include <linux/slab.h> 26 #include <linux/slab.h>
27 27
28 #include <asm/uaccess.h> 28 #include <asm/uaccess.h>
29 #include "sclp.h" 29 #include "sclp.h"
30 30
31 #define SCLP_VT220_MAJOR TTY_MAJOR 31 #define SCLP_VT220_MAJOR TTY_MAJOR
32 #define SCLP_VT220_MINOR 65 32 #define SCLP_VT220_MINOR 65
33 #define SCLP_VT220_DRIVER_NAME "sclp_vt220" 33 #define SCLP_VT220_DRIVER_NAME "sclp_vt220"
34 #define SCLP_VT220_DEVICE_NAME "ttysclp" 34 #define SCLP_VT220_DEVICE_NAME "ttysclp"
35 #define SCLP_VT220_CONSOLE_NAME "ttyS" 35 #define SCLP_VT220_CONSOLE_NAME "ttyS"
36 #define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */ 36 #define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */
37 37
38 /* Representation of a single write request */ 38 /* Representation of a single write request */
39 struct sclp_vt220_request { 39 struct sclp_vt220_request {
40 struct list_head list; 40 struct list_head list;
41 struct sclp_req sclp_req; 41 struct sclp_req sclp_req;
42 int retry_count; 42 int retry_count;
43 }; 43 };
44 44
45 /* VT220 SCCB */ 45 /* VT220 SCCB */
46 struct sclp_vt220_sccb { 46 struct sclp_vt220_sccb {
47 struct sccb_header header; 47 struct sccb_header header;
48 struct evbuf_header evbuf; 48 struct evbuf_header evbuf;
49 }; 49 };
50 50
51 #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \ 51 #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
52 sizeof(struct sclp_vt220_request) - \ 52 sizeof(struct sclp_vt220_request) - \
53 sizeof(struct sclp_vt220_sccb)) 53 sizeof(struct sclp_vt220_sccb))
54 54
55 /* Structures and data needed to register tty driver */ 55 /* Structures and data needed to register tty driver */
56 static struct tty_driver *sclp_vt220_driver; 56 static struct tty_driver *sclp_vt220_driver;
57 57
58 static struct tty_port sclp_vt220_port; 58 static struct tty_port sclp_vt220_port;
59 59
60 /* Lock to protect internal data from concurrent access */ 60 /* Lock to protect internal data from concurrent access */
61 static spinlock_t sclp_vt220_lock; 61 static spinlock_t sclp_vt220_lock;
62 62
63 /* List of empty pages to be used as write request buffers */ 63 /* List of empty pages to be used as write request buffers */
64 static struct list_head sclp_vt220_empty; 64 static struct list_head sclp_vt220_empty;
65 65
66 /* List of pending requests */ 66 /* List of pending requests */
67 static struct list_head sclp_vt220_outqueue; 67 static struct list_head sclp_vt220_outqueue;
68 68
69 /* Suspend mode flag */ 69 /* Suspend mode flag */
70 static int sclp_vt220_suspended; 70 static int sclp_vt220_suspended;
71 71
72 /* Flag that output queue is currently running */ 72 /* Flag that output queue is currently running */
73 static int sclp_vt220_queue_running; 73 static int sclp_vt220_queue_running;
74 74
75 /* Timer used for delaying write requests to merge subsequent messages into 75 /* Timer used for delaying write requests to merge subsequent messages into
76 * a single buffer */ 76 * a single buffer */
77 static struct timer_list sclp_vt220_timer; 77 static struct timer_list sclp_vt220_timer;
78 78
79 /* Pointer to current request buffer which has been partially filled but not 79 /* Pointer to current request buffer which has been partially filled but not
80 * yet sent */ 80 * yet sent */
81 static struct sclp_vt220_request *sclp_vt220_current_request; 81 static struct sclp_vt220_request *sclp_vt220_current_request;
82 82
83 /* Number of characters in current request buffer */ 83 /* Number of characters in current request buffer */
84 static int sclp_vt220_buffered_chars; 84 static int sclp_vt220_buffered_chars;
85 85
86 /* Counter controlling core driver initialization. */ 86 /* Counter controlling core driver initialization. */
87 static int __initdata sclp_vt220_init_count; 87 static int __initdata sclp_vt220_init_count;
88 88
89 /* Flag indicating that sclp_vt220_current_request should really 89 /* Flag indicating that sclp_vt220_current_request should really
90 * have been already queued but wasn't because the SCLP was processing 90 * have been already queued but wasn't because the SCLP was processing
91 * another buffer */ 91 * another buffer */
92 static int sclp_vt220_flush_later; 92 static int sclp_vt220_flush_later;
93 93
94 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf); 94 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
95 static void sclp_vt220_pm_event_fn(struct sclp_register *reg, 95 static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
96 enum sclp_pm_event sclp_pm_event); 96 enum sclp_pm_event sclp_pm_event);
97 static int __sclp_vt220_emit(struct sclp_vt220_request *request); 97 static int __sclp_vt220_emit(struct sclp_vt220_request *request);
98 static void sclp_vt220_emit_current(void); 98 static void sclp_vt220_emit_current(void);
99 99
100 /* Registration structure for our interest in SCLP event buffers */ 100 /* Registration structure for our interest in SCLP event buffers */
101 static struct sclp_register sclp_vt220_register = { 101 static struct sclp_register sclp_vt220_register = {
102 .send_mask = EVTYP_VT220MSG_MASK, 102 .send_mask = EVTYP_VT220MSG_MASK,
103 .receive_mask = EVTYP_VT220MSG_MASK, 103 .receive_mask = EVTYP_VT220MSG_MASK,
104 .state_change_fn = NULL, 104 .state_change_fn = NULL,
105 .receiver_fn = sclp_vt220_receiver_fn, 105 .receiver_fn = sclp_vt220_receiver_fn,
106 .pm_event_fn = sclp_vt220_pm_event_fn, 106 .pm_event_fn = sclp_vt220_pm_event_fn,
107 }; 107 };
108 108
109 109
110 /* 110 /*
111 * Put provided request buffer back into queue and check emit pending 111 * Put provided request buffer back into queue and check emit pending
112 * buffers if necessary. 112 * buffers if necessary.
113 */ 113 */
114 static void 114 static void
115 sclp_vt220_process_queue(struct sclp_vt220_request *request) 115 sclp_vt220_process_queue(struct sclp_vt220_request *request)
116 { 116 {
117 struct tty_struct *tty;
118 unsigned long flags; 117 unsigned long flags;
119 void *page; 118 void *page;
120 119
121 do { 120 do {
122 /* Put buffer back to list of empty buffers */ 121 /* Put buffer back to list of empty buffers */
123 page = request->sclp_req.sccb; 122 page = request->sclp_req.sccb;
124 spin_lock_irqsave(&sclp_vt220_lock, flags); 123 spin_lock_irqsave(&sclp_vt220_lock, flags);
125 /* Move request from outqueue to empty queue */ 124 /* Move request from outqueue to empty queue */
126 list_del(&request->list); 125 list_del(&request->list);
127 list_add_tail((struct list_head *) page, &sclp_vt220_empty); 126 list_add_tail((struct list_head *) page, &sclp_vt220_empty);
128 /* Check if there is a pending buffer on the out queue. */ 127 /* Check if there is a pending buffer on the out queue. */
129 request = NULL; 128 request = NULL;
130 if (!list_empty(&sclp_vt220_outqueue)) 129 if (!list_empty(&sclp_vt220_outqueue))
131 request = list_entry(sclp_vt220_outqueue.next, 130 request = list_entry(sclp_vt220_outqueue.next,
132 struct sclp_vt220_request, list); 131 struct sclp_vt220_request, list);
133 if (!request || sclp_vt220_suspended) { 132 if (!request || sclp_vt220_suspended) {
134 sclp_vt220_queue_running = 0; 133 sclp_vt220_queue_running = 0;
135 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 134 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
136 break; 135 break;
137 } 136 }
138 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 137 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
139 } while (__sclp_vt220_emit(request)); 138 } while (__sclp_vt220_emit(request));
140 if (request == NULL && sclp_vt220_flush_later) 139 if (request == NULL && sclp_vt220_flush_later)
141 sclp_vt220_emit_current(); 140 sclp_vt220_emit_current();
142 /* Check if the tty needs a wake up call */ 141 tty_port_tty_wakeup(&sclp_vt220_port);
143 tty = tty_port_tty_get(&sclp_vt220_port);
144 if (tty) {
145 tty_wakeup(tty);
146 tty_kref_put(tty);
147 }
148 } 142 }
149 143
150 #define SCLP_BUFFER_MAX_RETRY 1 144 #define SCLP_BUFFER_MAX_RETRY 1
151 145
152 /* 146 /*
153 * Callback through which the result of a write request is reported by the 147 * Callback through which the result of a write request is reported by the
154 * SCLP. 148 * SCLP.
155 */ 149 */
156 static void 150 static void
157 sclp_vt220_callback(struct sclp_req *request, void *data) 151 sclp_vt220_callback(struct sclp_req *request, void *data)
158 { 152 {
159 struct sclp_vt220_request *vt220_request; 153 struct sclp_vt220_request *vt220_request;
160 struct sclp_vt220_sccb *sccb; 154 struct sclp_vt220_sccb *sccb;
161 155
162 vt220_request = (struct sclp_vt220_request *) data; 156 vt220_request = (struct sclp_vt220_request *) data;
163 if (request->status == SCLP_REQ_FAILED) { 157 if (request->status == SCLP_REQ_FAILED) {
164 sclp_vt220_process_queue(vt220_request); 158 sclp_vt220_process_queue(vt220_request);
165 return; 159 return;
166 } 160 }
167 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb; 161 sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
168 162
169 /* Check SCLP response code and choose suitable action */ 163 /* Check SCLP response code and choose suitable action */
170 switch (sccb->header.response_code) { 164 switch (sccb->header.response_code) {
171 case 0x0020 : 165 case 0x0020 :
172 break; 166 break;
173 167
174 case 0x05f0: /* Target resource in improper state */ 168 case 0x05f0: /* Target resource in improper state */
175 break; 169 break;
176 170
177 case 0x0340: /* Contained SCLP equipment check */ 171 case 0x0340: /* Contained SCLP equipment check */
178 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY) 172 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
179 break; 173 break;
180 /* Remove processed buffers and requeue rest */ 174 /* Remove processed buffers and requeue rest */
181 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) { 175 if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
182 /* Not all buffers were processed */ 176 /* Not all buffers were processed */
183 sccb->header.response_code = 0x0000; 177 sccb->header.response_code = 0x0000;
184 vt220_request->sclp_req.status = SCLP_REQ_FILLED; 178 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
185 if (sclp_add_request(request) == 0) 179 if (sclp_add_request(request) == 0)
186 return; 180 return;
187 } 181 }
188 break; 182 break;
189 183
190 case 0x0040: /* SCLP equipment check */ 184 case 0x0040: /* SCLP equipment check */
191 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY) 185 if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
192 break; 186 break;
193 sccb->header.response_code = 0x0000; 187 sccb->header.response_code = 0x0000;
194 vt220_request->sclp_req.status = SCLP_REQ_FILLED; 188 vt220_request->sclp_req.status = SCLP_REQ_FILLED;
195 if (sclp_add_request(request) == 0) 189 if (sclp_add_request(request) == 0)
196 return; 190 return;
197 break; 191 break;
198 192
199 default: 193 default:
200 break; 194 break;
201 } 195 }
202 sclp_vt220_process_queue(vt220_request); 196 sclp_vt220_process_queue(vt220_request);
203 } 197 }
204 198
205 /* 199 /*
206 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero 200 * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
207 * otherwise. 201 * otherwise.
208 */ 202 */
209 static int 203 static int
210 __sclp_vt220_emit(struct sclp_vt220_request *request) 204 __sclp_vt220_emit(struct sclp_vt220_request *request)
211 { 205 {
212 if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) { 206 if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
213 request->sclp_req.status = SCLP_REQ_FAILED; 207 request->sclp_req.status = SCLP_REQ_FAILED;
214 return -EIO; 208 return -EIO;
215 } 209 }
216 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA; 210 request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
217 request->sclp_req.status = SCLP_REQ_FILLED; 211 request->sclp_req.status = SCLP_REQ_FILLED;
218 request->sclp_req.callback = sclp_vt220_callback; 212 request->sclp_req.callback = sclp_vt220_callback;
219 request->sclp_req.callback_data = (void *) request; 213 request->sclp_req.callback_data = (void *) request;
220 214
221 return sclp_add_request(&request->sclp_req); 215 return sclp_add_request(&request->sclp_req);
222 } 216 }
223 217
224 /* 218 /*
225 * Queue and emit current request. 219 * Queue and emit current request.
226 */ 220 */
227 static void 221 static void
228 sclp_vt220_emit_current(void) 222 sclp_vt220_emit_current(void)
229 { 223 {
230 unsigned long flags; 224 unsigned long flags;
231 struct sclp_vt220_request *request; 225 struct sclp_vt220_request *request;
232 struct sclp_vt220_sccb *sccb; 226 struct sclp_vt220_sccb *sccb;
233 227
234 spin_lock_irqsave(&sclp_vt220_lock, flags); 228 spin_lock_irqsave(&sclp_vt220_lock, flags);
235 if (sclp_vt220_current_request) { 229 if (sclp_vt220_current_request) {
236 sccb = (struct sclp_vt220_sccb *) 230 sccb = (struct sclp_vt220_sccb *)
237 sclp_vt220_current_request->sclp_req.sccb; 231 sclp_vt220_current_request->sclp_req.sccb;
238 /* Only emit buffers with content */ 232 /* Only emit buffers with content */
239 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) { 233 if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
240 list_add_tail(&sclp_vt220_current_request->list, 234 list_add_tail(&sclp_vt220_current_request->list,
241 &sclp_vt220_outqueue); 235 &sclp_vt220_outqueue);
242 sclp_vt220_current_request = NULL; 236 sclp_vt220_current_request = NULL;
243 if (timer_pending(&sclp_vt220_timer)) 237 if (timer_pending(&sclp_vt220_timer))
244 del_timer(&sclp_vt220_timer); 238 del_timer(&sclp_vt220_timer);
245 } 239 }
246 sclp_vt220_flush_later = 0; 240 sclp_vt220_flush_later = 0;
247 } 241 }
248 if (sclp_vt220_queue_running || sclp_vt220_suspended) 242 if (sclp_vt220_queue_running || sclp_vt220_suspended)
249 goto out_unlock; 243 goto out_unlock;
250 if (list_empty(&sclp_vt220_outqueue)) 244 if (list_empty(&sclp_vt220_outqueue))
251 goto out_unlock; 245 goto out_unlock;
252 request = list_first_entry(&sclp_vt220_outqueue, 246 request = list_first_entry(&sclp_vt220_outqueue,
253 struct sclp_vt220_request, list); 247 struct sclp_vt220_request, list);
254 sclp_vt220_queue_running = 1; 248 sclp_vt220_queue_running = 1;
255 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 249 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
256 250
257 if (__sclp_vt220_emit(request)) 251 if (__sclp_vt220_emit(request))
258 sclp_vt220_process_queue(request); 252 sclp_vt220_process_queue(request);
259 return; 253 return;
260 out_unlock: 254 out_unlock:
261 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 255 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
262 } 256 }
263 257
264 #define SCLP_NORMAL_WRITE 0x00 258 #define SCLP_NORMAL_WRITE 0x00
265 259
266 /* 260 /*
267 * Helper function to initialize a page with the sclp request structure. 261 * Helper function to initialize a page with the sclp request structure.
268 */ 262 */
269 static struct sclp_vt220_request * 263 static struct sclp_vt220_request *
270 sclp_vt220_initialize_page(void *page) 264 sclp_vt220_initialize_page(void *page)
271 { 265 {
272 struct sclp_vt220_request *request; 266 struct sclp_vt220_request *request;
273 struct sclp_vt220_sccb *sccb; 267 struct sclp_vt220_sccb *sccb;
274 268
275 /* Place request structure at end of page */ 269 /* Place request structure at end of page */
276 request = ((struct sclp_vt220_request *) 270 request = ((struct sclp_vt220_request *)
277 ((addr_t) page + PAGE_SIZE)) - 1; 271 ((addr_t) page + PAGE_SIZE)) - 1;
278 request->retry_count = 0; 272 request->retry_count = 0;
279 request->sclp_req.sccb = page; 273 request->sclp_req.sccb = page;
280 /* SCCB goes at start of page */ 274 /* SCCB goes at start of page */
281 sccb = (struct sclp_vt220_sccb *) page; 275 sccb = (struct sclp_vt220_sccb *) page;
282 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb)); 276 memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
283 sccb->header.length = sizeof(struct sclp_vt220_sccb); 277 sccb->header.length = sizeof(struct sclp_vt220_sccb);
284 sccb->header.function_code = SCLP_NORMAL_WRITE; 278 sccb->header.function_code = SCLP_NORMAL_WRITE;
285 sccb->header.response_code = 0x0000; 279 sccb->header.response_code = 0x0000;
286 sccb->evbuf.type = EVTYP_VT220MSG; 280 sccb->evbuf.type = EVTYP_VT220MSG;
287 sccb->evbuf.length = sizeof(struct evbuf_header); 281 sccb->evbuf.length = sizeof(struct evbuf_header);
288 282
289 return request; 283 return request;
290 } 284 }
291 285
292 static inline unsigned int 286 static inline unsigned int
293 sclp_vt220_space_left(struct sclp_vt220_request *request) 287 sclp_vt220_space_left(struct sclp_vt220_request *request)
294 { 288 {
295 struct sclp_vt220_sccb *sccb; 289 struct sclp_vt220_sccb *sccb;
296 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb; 290 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
297 return PAGE_SIZE - sizeof(struct sclp_vt220_request) - 291 return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
298 sccb->header.length; 292 sccb->header.length;
299 } 293 }
300 294
301 static inline unsigned int 295 static inline unsigned int
302 sclp_vt220_chars_stored(struct sclp_vt220_request *request) 296 sclp_vt220_chars_stored(struct sclp_vt220_request *request)
303 { 297 {
304 struct sclp_vt220_sccb *sccb; 298 struct sclp_vt220_sccb *sccb;
305 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb; 299 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
306 return sccb->evbuf.length - sizeof(struct evbuf_header); 300 return sccb->evbuf.length - sizeof(struct evbuf_header);
307 } 301 }
308 302
309 /* 303 /*
310 * Add msg to buffer associated with request. Return the number of characters 304 * Add msg to buffer associated with request. Return the number of characters
311 * added. 305 * added.
312 */ 306 */
313 static int 307 static int
314 sclp_vt220_add_msg(struct sclp_vt220_request *request, 308 sclp_vt220_add_msg(struct sclp_vt220_request *request,
315 const unsigned char *msg, int count, int convertlf) 309 const unsigned char *msg, int count, int convertlf)
316 { 310 {
317 struct sclp_vt220_sccb *sccb; 311 struct sclp_vt220_sccb *sccb;
318 void *buffer; 312 void *buffer;
319 unsigned char c; 313 unsigned char c;
320 int from; 314 int from;
321 int to; 315 int to;
322 316
323 if (count > sclp_vt220_space_left(request)) 317 if (count > sclp_vt220_space_left(request))
324 count = sclp_vt220_space_left(request); 318 count = sclp_vt220_space_left(request);
325 if (count <= 0) 319 if (count <= 0)
326 return 0; 320 return 0;
327 321
328 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb; 322 sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
329 buffer = (void *) ((addr_t) sccb + sccb->header.length); 323 buffer = (void *) ((addr_t) sccb + sccb->header.length);
330 324
331 if (convertlf) { 325 if (convertlf) {
332 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/ 326 /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
333 for (from=0, to=0; 327 for (from=0, to=0;
334 (from < count) && (to < sclp_vt220_space_left(request)); 328 (from < count) && (to < sclp_vt220_space_left(request));
335 from++) { 329 from++) {
336 /* Retrieve character */ 330 /* Retrieve character */
337 c = msg[from]; 331 c = msg[from];
338 /* Perform conversion */ 332 /* Perform conversion */
339 if (c == 0x0a) { 333 if (c == 0x0a) {
340 if (to + 1 < sclp_vt220_space_left(request)) { 334 if (to + 1 < sclp_vt220_space_left(request)) {
341 ((unsigned char *) buffer)[to++] = c; 335 ((unsigned char *) buffer)[to++] = c;
342 ((unsigned char *) buffer)[to++] = 0x0d; 336 ((unsigned char *) buffer)[to++] = 0x0d;
343 } else 337 } else
344 break; 338 break;
345 339
346 } else 340 } else
347 ((unsigned char *) buffer)[to++] = c; 341 ((unsigned char *) buffer)[to++] = c;
348 } 342 }
349 sccb->header.length += to; 343 sccb->header.length += to;
350 sccb->evbuf.length += to; 344 sccb->evbuf.length += to;
351 return from; 345 return from;
352 } else { 346 } else {
353 memcpy(buffer, (const void *) msg, count); 347 memcpy(buffer, (const void *) msg, count);
354 sccb->header.length += count; 348 sccb->header.length += count;
355 sccb->evbuf.length += count; 349 sccb->evbuf.length += count;
356 return count; 350 return count;
357 } 351 }
358 } 352 }
359 353
360 /* 354 /*
361 * Emit buffer after having waited long enough for more data to arrive. 355 * Emit buffer after having waited long enough for more data to arrive.
362 */ 356 */
363 static void 357 static void
364 sclp_vt220_timeout(unsigned long data) 358 sclp_vt220_timeout(unsigned long data)
365 { 359 {
366 sclp_vt220_emit_current(); 360 sclp_vt220_emit_current();
367 } 361 }
368 362
369 #define BUFFER_MAX_DELAY HZ/20 363 #define BUFFER_MAX_DELAY HZ/20
370 364
371 /* 365 /*
372 * Internal implementation of the write function. Write COUNT bytes of data 366 * Internal implementation of the write function. Write COUNT bytes of data
373 * from memory at BUF 367 * from memory at BUF
374 * to the SCLP interface. In case that the data does not fit into the current 368 * to the SCLP interface. In case that the data does not fit into the current
375 * write buffer, emit the current one and allocate a new one. If there are no 369 * write buffer, emit the current one and allocate a new one. If there are no
376 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE 370 * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
377 * is non-zero, the buffer will be scheduled for emitting after a timeout - 371 * is non-zero, the buffer will be scheduled for emitting after a timeout -
378 * otherwise the user has to explicitly call the flush function. 372 * otherwise the user has to explicitly call the flush function.
379 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message 373 * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
380 * buffer should be converted to 0x0a 0x0d. After completion, return the number 374 * buffer should be converted to 0x0a 0x0d. After completion, return the number
381 * of bytes written. 375 * of bytes written.
382 */ 376 */
383 static int 377 static int
384 __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule, 378 __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
385 int convertlf, int may_fail) 379 int convertlf, int may_fail)
386 { 380 {
387 unsigned long flags; 381 unsigned long flags;
388 void *page; 382 void *page;
389 int written; 383 int written;
390 int overall_written; 384 int overall_written;
391 385
392 if (count <= 0) 386 if (count <= 0)
393 return 0; 387 return 0;
394 overall_written = 0; 388 overall_written = 0;
395 spin_lock_irqsave(&sclp_vt220_lock, flags); 389 spin_lock_irqsave(&sclp_vt220_lock, flags);
396 do { 390 do {
397 /* Create an sclp output buffer if none exists yet */ 391 /* Create an sclp output buffer if none exists yet */
398 if (sclp_vt220_current_request == NULL) { 392 if (sclp_vt220_current_request == NULL) {
399 while (list_empty(&sclp_vt220_empty)) { 393 while (list_empty(&sclp_vt220_empty)) {
400 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 394 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
401 if (may_fail || sclp_vt220_suspended) 395 if (may_fail || sclp_vt220_suspended)
402 goto out; 396 goto out;
403 else 397 else
404 sclp_sync_wait(); 398 sclp_sync_wait();
405 spin_lock_irqsave(&sclp_vt220_lock, flags); 399 spin_lock_irqsave(&sclp_vt220_lock, flags);
406 } 400 }
407 page = (void *) sclp_vt220_empty.next; 401 page = (void *) sclp_vt220_empty.next;
408 list_del((struct list_head *) page); 402 list_del((struct list_head *) page);
409 sclp_vt220_current_request = 403 sclp_vt220_current_request =
410 sclp_vt220_initialize_page(page); 404 sclp_vt220_initialize_page(page);
411 } 405 }
412 /* Try to write the string to the current request buffer */ 406 /* Try to write the string to the current request buffer */
413 written = sclp_vt220_add_msg(sclp_vt220_current_request, 407 written = sclp_vt220_add_msg(sclp_vt220_current_request,
414 buf, count, convertlf); 408 buf, count, convertlf);
415 overall_written += written; 409 overall_written += written;
416 if (written == count) 410 if (written == count)
417 break; 411 break;
418 /* 412 /*
419 * Not all characters could be written to the current 413 * Not all characters could be written to the current
420 * output buffer. Emit the buffer, create a new buffer 414 * output buffer. Emit the buffer, create a new buffer
421 * and then output the rest of the string. 415 * and then output the rest of the string.
422 */ 416 */
423 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 417 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
424 sclp_vt220_emit_current(); 418 sclp_vt220_emit_current();
425 spin_lock_irqsave(&sclp_vt220_lock, flags); 419 spin_lock_irqsave(&sclp_vt220_lock, flags);
426 buf += written; 420 buf += written;
427 count -= written; 421 count -= written;
428 } while (count > 0); 422 } while (count > 0);
429 /* Setup timer to output current console buffer after some time */ 423 /* Setup timer to output current console buffer after some time */
430 if (sclp_vt220_current_request != NULL && 424 if (sclp_vt220_current_request != NULL &&
431 !timer_pending(&sclp_vt220_timer) && do_schedule) { 425 !timer_pending(&sclp_vt220_timer) && do_schedule) {
432 sclp_vt220_timer.function = sclp_vt220_timeout; 426 sclp_vt220_timer.function = sclp_vt220_timeout;
433 sclp_vt220_timer.data = 0UL; 427 sclp_vt220_timer.data = 0UL;
434 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY; 428 sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
435 add_timer(&sclp_vt220_timer); 429 add_timer(&sclp_vt220_timer);
436 } 430 }
437 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 431 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
438 out: 432 out:
439 return overall_written; 433 return overall_written;
440 } 434 }
441 435
442 /* 436 /*
443 * This routine is called by the kernel to write a series of 437 * This routine is called by the kernel to write a series of
444 * characters to the tty device. The characters may come from 438 * characters to the tty device. The characters may come from
445 * user space or kernel space. This routine will return the 439 * user space or kernel space. This routine will return the
446 * number of characters actually accepted for writing. 440 * number of characters actually accepted for writing.
447 */ 441 */
448 static int 442 static int
449 sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count) 443 sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
450 { 444 {
451 return __sclp_vt220_write(buf, count, 1, 0, 1); 445 return __sclp_vt220_write(buf, count, 1, 0, 1);
452 } 446 }
453 447
454 #define SCLP_VT220_SESSION_ENDED 0x01 448 #define SCLP_VT220_SESSION_ENDED 0x01
455 #define SCLP_VT220_SESSION_STARTED 0x80 449 #define SCLP_VT220_SESSION_STARTED 0x80
456 #define SCLP_VT220_SESSION_DATA 0x00 450 #define SCLP_VT220_SESSION_DATA 0x00
457 451
458 /* 452 /*
459 * Called by the SCLP to report incoming event buffers. 453 * Called by the SCLP to report incoming event buffers.
460 */ 454 */
461 static void 455 static void
462 sclp_vt220_receiver_fn(struct evbuf_header *evbuf) 456 sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
463 { 457 {
464 char *buffer; 458 char *buffer;
465 unsigned int count; 459 unsigned int count;
466 460
467 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header)); 461 buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
468 count = evbuf->length - sizeof(struct evbuf_header); 462 count = evbuf->length - sizeof(struct evbuf_header);
469 463
470 switch (*buffer) { 464 switch (*buffer) {
471 case SCLP_VT220_SESSION_ENDED: 465 case SCLP_VT220_SESSION_ENDED:
472 case SCLP_VT220_SESSION_STARTED: 466 case SCLP_VT220_SESSION_STARTED:
473 break; 467 break;
474 case SCLP_VT220_SESSION_DATA: 468 case SCLP_VT220_SESSION_DATA:
475 /* Send input to line discipline */ 469 /* Send input to line discipline */
476 buffer++; 470 buffer++;
477 count--; 471 count--;
478 tty_insert_flip_string(&sclp_vt220_port, buffer, count); 472 tty_insert_flip_string(&sclp_vt220_port, buffer, count);
479 tty_flip_buffer_push(&sclp_vt220_port); 473 tty_flip_buffer_push(&sclp_vt220_port);
480 break; 474 break;
481 } 475 }
482 } 476 }
483 477
484 /* 478 /*
485 * This routine is called when a particular tty device is opened. 479 * This routine is called when a particular tty device is opened.
486 */ 480 */
487 static int 481 static int
488 sclp_vt220_open(struct tty_struct *tty, struct file *filp) 482 sclp_vt220_open(struct tty_struct *tty, struct file *filp)
489 { 483 {
490 if (tty->count == 1) { 484 if (tty->count == 1) {
491 tty_port_tty_set(&sclp_vt220_port, tty); 485 tty_port_tty_set(&sclp_vt220_port, tty);
492 sclp_vt220_port.low_latency = 0; 486 sclp_vt220_port.low_latency = 0;
493 if (!tty->winsize.ws_row && !tty->winsize.ws_col) { 487 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
494 tty->winsize.ws_row = 24; 488 tty->winsize.ws_row = 24;
495 tty->winsize.ws_col = 80; 489 tty->winsize.ws_col = 80;
496 } 490 }
497 } 491 }
498 return 0; 492 return 0;
499 } 493 }
500 494
501 /* 495 /*
502 * This routine is called when a particular tty device is closed. 496 * This routine is called when a particular tty device is closed.
503 */ 497 */
504 static void 498 static void
505 sclp_vt220_close(struct tty_struct *tty, struct file *filp) 499 sclp_vt220_close(struct tty_struct *tty, struct file *filp)
506 { 500 {
507 if (tty->count == 1) 501 if (tty->count == 1)
508 tty_port_tty_set(&sclp_vt220_port, NULL); 502 tty_port_tty_set(&sclp_vt220_port, NULL);
509 } 503 }
510 504
511 /* 505 /*
512 * This routine is called by the kernel to write a single 506 * This routine is called by the kernel to write a single
513 * character to the tty device. If the kernel uses this routine, 507 * character to the tty device. If the kernel uses this routine,
514 * it must call the flush_chars() routine (if defined) when it is 508 * it must call the flush_chars() routine (if defined) when it is
515 * done stuffing characters into the driver. 509 * done stuffing characters into the driver.
516 */ 510 */
517 static int 511 static int
518 sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch) 512 sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
519 { 513 {
520 return __sclp_vt220_write(&ch, 1, 0, 0, 1); 514 return __sclp_vt220_write(&ch, 1, 0, 0, 1);
521 } 515 }
522 516
523 /* 517 /*
524 * This routine is called by the kernel after it has written a 518 * This routine is called by the kernel after it has written a
525 * series of characters to the tty device using put_char(). 519 * series of characters to the tty device using put_char().
526 */ 520 */
527 static void 521 static void
528 sclp_vt220_flush_chars(struct tty_struct *tty) 522 sclp_vt220_flush_chars(struct tty_struct *tty)
529 { 523 {
530 if (!sclp_vt220_queue_running) 524 if (!sclp_vt220_queue_running)
531 sclp_vt220_emit_current(); 525 sclp_vt220_emit_current();
532 else 526 else
533 sclp_vt220_flush_later = 1; 527 sclp_vt220_flush_later = 1;
534 } 528 }
535 529
536 /* 530 /*
537 * This routine returns the numbers of characters the tty driver 531 * This routine returns the numbers of characters the tty driver
538 * will accept for queuing to be written. This number is subject 532 * will accept for queuing to be written. This number is subject
539 * to change as output buffers get emptied, or if the output flow 533 * to change as output buffers get emptied, or if the output flow
540 * control is acted. 534 * control is acted.
541 */ 535 */
542 static int 536 static int
543 sclp_vt220_write_room(struct tty_struct *tty) 537 sclp_vt220_write_room(struct tty_struct *tty)
544 { 538 {
545 unsigned long flags; 539 unsigned long flags;
546 struct list_head *l; 540 struct list_head *l;
547 int count; 541 int count;
548 542
549 spin_lock_irqsave(&sclp_vt220_lock, flags); 543 spin_lock_irqsave(&sclp_vt220_lock, flags);
550 count = 0; 544 count = 0;
551 if (sclp_vt220_current_request != NULL) 545 if (sclp_vt220_current_request != NULL)
552 count = sclp_vt220_space_left(sclp_vt220_current_request); 546 count = sclp_vt220_space_left(sclp_vt220_current_request);
553 list_for_each(l, &sclp_vt220_empty) 547 list_for_each(l, &sclp_vt220_empty)
554 count += SCLP_VT220_MAX_CHARS_PER_BUFFER; 548 count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
555 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 549 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
556 return count; 550 return count;
557 } 551 }
558 552
559 /* 553 /*
560 * Return number of buffered chars. 554 * Return number of buffered chars.
561 */ 555 */
562 static int 556 static int
563 sclp_vt220_chars_in_buffer(struct tty_struct *tty) 557 sclp_vt220_chars_in_buffer(struct tty_struct *tty)
564 { 558 {
565 unsigned long flags; 559 unsigned long flags;
566 struct list_head *l; 560 struct list_head *l;
567 struct sclp_vt220_request *r; 561 struct sclp_vt220_request *r;
568 int count; 562 int count;
569 563
570 spin_lock_irqsave(&sclp_vt220_lock, flags); 564 spin_lock_irqsave(&sclp_vt220_lock, flags);
571 count = 0; 565 count = 0;
572 if (sclp_vt220_current_request != NULL) 566 if (sclp_vt220_current_request != NULL)
573 count = sclp_vt220_chars_stored(sclp_vt220_current_request); 567 count = sclp_vt220_chars_stored(sclp_vt220_current_request);
574 list_for_each(l, &sclp_vt220_outqueue) { 568 list_for_each(l, &sclp_vt220_outqueue) {
575 r = list_entry(l, struct sclp_vt220_request, list); 569 r = list_entry(l, struct sclp_vt220_request, list);
576 count += sclp_vt220_chars_stored(r); 570 count += sclp_vt220_chars_stored(r);
577 } 571 }
578 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 572 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
579 return count; 573 return count;
580 } 574 }
581 575
582 /* 576 /*
583 * Pass on all buffers to the hardware. Return only when there are no more 577 * Pass on all buffers to the hardware. Return only when there are no more
584 * buffers pending. 578 * buffers pending.
585 */ 579 */
586 static void 580 static void
587 sclp_vt220_flush_buffer(struct tty_struct *tty) 581 sclp_vt220_flush_buffer(struct tty_struct *tty)
588 { 582 {
589 sclp_vt220_emit_current(); 583 sclp_vt220_emit_current();
590 } 584 }
591 585
592 /* Release allocated pages. */ 586 /* Release allocated pages. */
593 static void __init __sclp_vt220_free_pages(void) 587 static void __init __sclp_vt220_free_pages(void)
594 { 588 {
595 struct list_head *page, *p; 589 struct list_head *page, *p;
596 590
597 list_for_each_safe(page, p, &sclp_vt220_empty) { 591 list_for_each_safe(page, p, &sclp_vt220_empty) {
598 list_del(page); 592 list_del(page);
599 free_page((unsigned long) page); 593 free_page((unsigned long) page);
600 } 594 }
601 } 595 }
602 596
603 /* Release memory and unregister from sclp core. Controlled by init counting - 597 /* Release memory and unregister from sclp core. Controlled by init counting -
604 * only the last invoker will actually perform these actions. */ 598 * only the last invoker will actually perform these actions. */
605 static void __init __sclp_vt220_cleanup(void) 599 static void __init __sclp_vt220_cleanup(void)
606 { 600 {
607 sclp_vt220_init_count--; 601 sclp_vt220_init_count--;
608 if (sclp_vt220_init_count != 0) 602 if (sclp_vt220_init_count != 0)
609 return; 603 return;
610 sclp_unregister(&sclp_vt220_register); 604 sclp_unregister(&sclp_vt220_register);
611 __sclp_vt220_free_pages(); 605 __sclp_vt220_free_pages();
612 tty_port_destroy(&sclp_vt220_port); 606 tty_port_destroy(&sclp_vt220_port);
613 } 607 }
614 608
615 /* Allocate buffer pages and register with sclp core. Controlled by init 609 /* Allocate buffer pages and register with sclp core. Controlled by init
616 * counting - only the first invoker will actually perform these actions. */ 610 * counting - only the first invoker will actually perform these actions. */
617 static int __init __sclp_vt220_init(int num_pages) 611 static int __init __sclp_vt220_init(int num_pages)
618 { 612 {
619 void *page; 613 void *page;
620 int i; 614 int i;
621 int rc; 615 int rc;
622 616
623 sclp_vt220_init_count++; 617 sclp_vt220_init_count++;
624 if (sclp_vt220_init_count != 1) 618 if (sclp_vt220_init_count != 1)
625 return 0; 619 return 0;
626 spin_lock_init(&sclp_vt220_lock); 620 spin_lock_init(&sclp_vt220_lock);
627 INIT_LIST_HEAD(&sclp_vt220_empty); 621 INIT_LIST_HEAD(&sclp_vt220_empty);
628 INIT_LIST_HEAD(&sclp_vt220_outqueue); 622 INIT_LIST_HEAD(&sclp_vt220_outqueue);
629 init_timer(&sclp_vt220_timer); 623 init_timer(&sclp_vt220_timer);
630 tty_port_init(&sclp_vt220_port); 624 tty_port_init(&sclp_vt220_port);
631 sclp_vt220_current_request = NULL; 625 sclp_vt220_current_request = NULL;
632 sclp_vt220_buffered_chars = 0; 626 sclp_vt220_buffered_chars = 0;
633 sclp_vt220_flush_later = 0; 627 sclp_vt220_flush_later = 0;
634 628
635 /* Allocate pages for output buffering */ 629 /* Allocate pages for output buffering */
636 rc = -ENOMEM; 630 rc = -ENOMEM;
637 for (i = 0; i < num_pages; i++) { 631 for (i = 0; i < num_pages; i++) {
638 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA); 632 page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
639 if (!page) 633 if (!page)
640 goto out; 634 goto out;
641 list_add_tail(page, &sclp_vt220_empty); 635 list_add_tail(page, &sclp_vt220_empty);
642 } 636 }
643 rc = sclp_register(&sclp_vt220_register); 637 rc = sclp_register(&sclp_vt220_register);
644 out: 638 out:
645 if (rc) { 639 if (rc) {
646 __sclp_vt220_free_pages(); 640 __sclp_vt220_free_pages();
647 sclp_vt220_init_count--; 641 sclp_vt220_init_count--;
648 tty_port_destroy(&sclp_vt220_port); 642 tty_port_destroy(&sclp_vt220_port);
649 } 643 }
650 return rc; 644 return rc;
651 } 645 }
652 646
653 static const struct tty_operations sclp_vt220_ops = { 647 static const struct tty_operations sclp_vt220_ops = {
654 .open = sclp_vt220_open, 648 .open = sclp_vt220_open,
655 .close = sclp_vt220_close, 649 .close = sclp_vt220_close,
656 .write = sclp_vt220_write, 650 .write = sclp_vt220_write,
657 .put_char = sclp_vt220_put_char, 651 .put_char = sclp_vt220_put_char,
658 .flush_chars = sclp_vt220_flush_chars, 652 .flush_chars = sclp_vt220_flush_chars,
659 .write_room = sclp_vt220_write_room, 653 .write_room = sclp_vt220_write_room,
660 .chars_in_buffer = sclp_vt220_chars_in_buffer, 654 .chars_in_buffer = sclp_vt220_chars_in_buffer,
661 .flush_buffer = sclp_vt220_flush_buffer, 655 .flush_buffer = sclp_vt220_flush_buffer,
662 }; 656 };
663 657
664 /* 658 /*
665 * Register driver with SCLP and Linux and initialize internal tty structures. 659 * Register driver with SCLP and Linux and initialize internal tty structures.
666 */ 660 */
667 static int __init sclp_vt220_tty_init(void) 661 static int __init sclp_vt220_tty_init(void)
668 { 662 {
669 struct tty_driver *driver; 663 struct tty_driver *driver;
670 int rc; 664 int rc;
671 665
672 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve 666 /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
673 * symmetry between VM and LPAR systems regarding ttyS1. */ 667 * symmetry between VM and LPAR systems regarding ttyS1. */
674 driver = alloc_tty_driver(1); 668 driver = alloc_tty_driver(1);
675 if (!driver) 669 if (!driver)
676 return -ENOMEM; 670 return -ENOMEM;
677 rc = __sclp_vt220_init(MAX_KMEM_PAGES); 671 rc = __sclp_vt220_init(MAX_KMEM_PAGES);
678 if (rc) 672 if (rc)
679 goto out_driver; 673 goto out_driver;
680 674
681 driver->driver_name = SCLP_VT220_DRIVER_NAME; 675 driver->driver_name = SCLP_VT220_DRIVER_NAME;
682 driver->name = SCLP_VT220_DEVICE_NAME; 676 driver->name = SCLP_VT220_DEVICE_NAME;
683 driver->major = SCLP_VT220_MAJOR; 677 driver->major = SCLP_VT220_MAJOR;
684 driver->minor_start = SCLP_VT220_MINOR; 678 driver->minor_start = SCLP_VT220_MINOR;
685 driver->type = TTY_DRIVER_TYPE_SYSTEM; 679 driver->type = TTY_DRIVER_TYPE_SYSTEM;
686 driver->subtype = SYSTEM_TYPE_TTY; 680 driver->subtype = SYSTEM_TYPE_TTY;
687 driver->init_termios = tty_std_termios; 681 driver->init_termios = tty_std_termios;
688 driver->flags = TTY_DRIVER_REAL_RAW; 682 driver->flags = TTY_DRIVER_REAL_RAW;
689 tty_set_operations(driver, &sclp_vt220_ops); 683 tty_set_operations(driver, &sclp_vt220_ops);
690 tty_port_link_device(&sclp_vt220_port, driver, 0); 684 tty_port_link_device(&sclp_vt220_port, driver, 0);
691 685
692 rc = tty_register_driver(driver); 686 rc = tty_register_driver(driver);
693 if (rc) 687 if (rc)
694 goto out_init; 688 goto out_init;
695 sclp_vt220_driver = driver; 689 sclp_vt220_driver = driver;
696 return 0; 690 return 0;
697 691
698 out_init: 692 out_init:
699 __sclp_vt220_cleanup(); 693 __sclp_vt220_cleanup();
700 out_driver: 694 out_driver:
701 put_tty_driver(driver); 695 put_tty_driver(driver);
702 return rc; 696 return rc;
703 } 697 }
704 __initcall(sclp_vt220_tty_init); 698 __initcall(sclp_vt220_tty_init);
705 699
706 static void __sclp_vt220_flush_buffer(void) 700 static void __sclp_vt220_flush_buffer(void)
707 { 701 {
708 unsigned long flags; 702 unsigned long flags;
709 703
710 sclp_vt220_emit_current(); 704 sclp_vt220_emit_current();
711 spin_lock_irqsave(&sclp_vt220_lock, flags); 705 spin_lock_irqsave(&sclp_vt220_lock, flags);
712 if (timer_pending(&sclp_vt220_timer)) 706 if (timer_pending(&sclp_vt220_timer))
713 del_timer(&sclp_vt220_timer); 707 del_timer(&sclp_vt220_timer);
714 while (sclp_vt220_queue_running) { 708 while (sclp_vt220_queue_running) {
715 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 709 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
716 sclp_sync_wait(); 710 sclp_sync_wait();
717 spin_lock_irqsave(&sclp_vt220_lock, flags); 711 spin_lock_irqsave(&sclp_vt220_lock, flags);
718 } 712 }
719 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 713 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
720 } 714 }
721 715
722 /* 716 /*
723 * Resume console: If there are cached messages, emit them. 717 * Resume console: If there are cached messages, emit them.
724 */ 718 */
725 static void sclp_vt220_resume(void) 719 static void sclp_vt220_resume(void)
726 { 720 {
727 unsigned long flags; 721 unsigned long flags;
728 722
729 spin_lock_irqsave(&sclp_vt220_lock, flags); 723 spin_lock_irqsave(&sclp_vt220_lock, flags);
730 sclp_vt220_suspended = 0; 724 sclp_vt220_suspended = 0;
731 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 725 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
732 sclp_vt220_emit_current(); 726 sclp_vt220_emit_current();
733 } 727 }
734 728
735 /* 729 /*
736 * Suspend console: Set suspend flag and flush console 730 * Suspend console: Set suspend flag and flush console
737 */ 731 */
738 static void sclp_vt220_suspend(void) 732 static void sclp_vt220_suspend(void)
739 { 733 {
740 unsigned long flags; 734 unsigned long flags;
741 735
742 spin_lock_irqsave(&sclp_vt220_lock, flags); 736 spin_lock_irqsave(&sclp_vt220_lock, flags);
743 sclp_vt220_suspended = 1; 737 sclp_vt220_suspended = 1;
744 spin_unlock_irqrestore(&sclp_vt220_lock, flags); 738 spin_unlock_irqrestore(&sclp_vt220_lock, flags);
745 __sclp_vt220_flush_buffer(); 739 __sclp_vt220_flush_buffer();
746 } 740 }
747 741
748 static void sclp_vt220_pm_event_fn(struct sclp_register *reg, 742 static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
749 enum sclp_pm_event sclp_pm_event) 743 enum sclp_pm_event sclp_pm_event)
750 { 744 {
751 switch (sclp_pm_event) { 745 switch (sclp_pm_event) {
752 case SCLP_PM_EVENT_FREEZE: 746 case SCLP_PM_EVENT_FREEZE:
753 sclp_vt220_suspend(); 747 sclp_vt220_suspend();
754 break; 748 break;
755 case SCLP_PM_EVENT_RESTORE: 749 case SCLP_PM_EVENT_RESTORE:
756 case SCLP_PM_EVENT_THAW: 750 case SCLP_PM_EVENT_THAW:
757 sclp_vt220_resume(); 751 sclp_vt220_resume();
758 break; 752 break;
759 } 753 }
760 } 754 }
761 755
762 #ifdef CONFIG_SCLP_VT220_CONSOLE 756 #ifdef CONFIG_SCLP_VT220_CONSOLE
763 757
764 static void 758 static void
765 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count) 759 sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
766 { 760 {
767 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0); 761 __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
768 } 762 }
769 763
770 static struct tty_driver * 764 static struct tty_driver *
771 sclp_vt220_con_device(struct console *c, int *index) 765 sclp_vt220_con_device(struct console *c, int *index)
772 { 766 {
773 *index = 0; 767 *index = 0;
774 return sclp_vt220_driver; 768 return sclp_vt220_driver;
775 } 769 }
776 770
777 static int 771 static int
778 sclp_vt220_notify(struct notifier_block *self, 772 sclp_vt220_notify(struct notifier_block *self,
779 unsigned long event, void *data) 773 unsigned long event, void *data)
780 { 774 {
781 __sclp_vt220_flush_buffer(); 775 __sclp_vt220_flush_buffer();
782 return NOTIFY_OK; 776 return NOTIFY_OK;
783 } 777 }
784 778
785 static struct notifier_block on_panic_nb = { 779 static struct notifier_block on_panic_nb = {
786 .notifier_call = sclp_vt220_notify, 780 .notifier_call = sclp_vt220_notify,
787 .priority = 1, 781 .priority = 1,
788 }; 782 };
789 783
790 static struct notifier_block on_reboot_nb = { 784 static struct notifier_block on_reboot_nb = {
791 .notifier_call = sclp_vt220_notify, 785 .notifier_call = sclp_vt220_notify,
792 .priority = 1, 786 .priority = 1,
793 }; 787 };
794 788
795 /* Structure needed to register with printk */ 789 /* Structure needed to register with printk */
796 static struct console sclp_vt220_console = 790 static struct console sclp_vt220_console =
797 { 791 {
798 .name = SCLP_VT220_CONSOLE_NAME, 792 .name = SCLP_VT220_CONSOLE_NAME,
799 .write = sclp_vt220_con_write, 793 .write = sclp_vt220_con_write,
800 .device = sclp_vt220_con_device, 794 .device = sclp_vt220_con_device,
801 .flags = CON_PRINTBUFFER, 795 .flags = CON_PRINTBUFFER,
802 .index = SCLP_VT220_CONSOLE_INDEX 796 .index = SCLP_VT220_CONSOLE_INDEX
803 }; 797 };
804 798
805 static int __init 799 static int __init
806 sclp_vt220_con_init(void) 800 sclp_vt220_con_init(void)
807 { 801 {
808 int rc; 802 int rc;
809 803
810 if (!CONSOLE_IS_SCLP) 804 if (!CONSOLE_IS_SCLP)
811 return 0; 805 return 0;
812 rc = __sclp_vt220_init(MAX_CONSOLE_PAGES); 806 rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
813 if (rc) 807 if (rc)
814 return rc; 808 return rc;
815 /* Attach linux console */ 809 /* Attach linux console */
816 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb); 810 atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
817 register_reboot_notifier(&on_reboot_nb); 811 register_reboot_notifier(&on_reboot_nb);
818 register_console(&sclp_vt220_console); 812 register_console(&sclp_vt220_console);
819 return 0; 813 return 0;
820 } 814 }
821 815
822 console_initcall(sclp_vt220_con_init); 816 console_initcall(sclp_vt220_con_init);
823 #endif /* CONFIG_SCLP_VT220_CONSOLE */ 817 #endif /* CONFIG_SCLP_VT220_CONSOLE */
824 818
825 819
drivers/staging/fwserial/fwserial.c
1 /* 1 /*
2 * FireWire Serial driver 2 * FireWire Serial driver
3 * 3 *
4 * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com> 4 * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com>
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 * 10 *
11 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation, 17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */ 19 */
20 20
21 #include <linux/sched.h> 21 #include <linux/sched.h>
22 #include <linux/slab.h> 22 #include <linux/slab.h>
23 #include <linux/device.h> 23 #include <linux/device.h>
24 #include <linux/mod_devicetable.h> 24 #include <linux/mod_devicetable.h>
25 #include <linux/rculist.h> 25 #include <linux/rculist.h>
26 #include <linux/workqueue.h> 26 #include <linux/workqueue.h>
27 #include <linux/ratelimit.h> 27 #include <linux/ratelimit.h>
28 #include <linux/bug.h> 28 #include <linux/bug.h>
29 #include <linux/uaccess.h> 29 #include <linux/uaccess.h>
30 30
31 #include "fwserial.h" 31 #include "fwserial.h"
32 32
33 #define be32_to_u64(hi, lo) ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo)) 33 #define be32_to_u64(hi, lo) ((u64)be32_to_cpu(hi) << 32 | be32_to_cpu(lo))
34 34
35 #define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */ 35 #define LINUX_VENDOR_ID 0xd00d1eU /* same id used in card root directory */
36 #define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */ 36 #define FWSERIAL_VERSION 0x00e81cU /* must be unique within LINUX_VENDOR_ID */
37 37
38 /* configurable options */ 38 /* configurable options */
39 static int num_ttys = 4; /* # of std ttys to create per fw_card */ 39 static int num_ttys = 4; /* # of std ttys to create per fw_card */
40 /* - doubles as loopback port index */ 40 /* - doubles as loopback port index */
41 static bool auto_connect = true; /* try to VIRT_CABLE to every peer */ 41 static bool auto_connect = true; /* try to VIRT_CABLE to every peer */
42 static bool create_loop_dev = true; /* create a loopback device for each card */ 42 static bool create_loop_dev = true; /* create a loopback device for each card */
43 43
44 module_param_named(ttys, num_ttys, int, S_IRUGO | S_IWUSR); 44 module_param_named(ttys, num_ttys, int, S_IRUGO | S_IWUSR);
45 module_param_named(auto, auto_connect, bool, S_IRUGO | S_IWUSR); 45 module_param_named(auto, auto_connect, bool, S_IRUGO | S_IWUSR);
46 module_param_named(loop, create_loop_dev, bool, S_IRUGO | S_IWUSR); 46 module_param_named(loop, create_loop_dev, bool, S_IRUGO | S_IWUSR);
47 47
48 /* 48 /*
49 * Threshold below which the tty is woken for writing 49 * Threshold below which the tty is woken for writing
50 * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because 50 * - should be equal to WAKEUP_CHARS in drivers/tty/n_tty.c because
51 * even if the writer is woken, n_tty_poll() won't set POLLOUT until 51 * even if the writer is woken, n_tty_poll() won't set POLLOUT until
52 * our fifo is below this level 52 * our fifo is below this level
53 */ 53 */
54 #define WAKEUP_CHARS 256 54 #define WAKEUP_CHARS 256
55 55
56 /** 56 /**
57 * fwserial_list: list of every fw_serial created for each fw_card 57 * fwserial_list: list of every fw_serial created for each fw_card
58 * See discussion in fwserial_probe. 58 * See discussion in fwserial_probe.
59 */ 59 */
60 static LIST_HEAD(fwserial_list); 60 static LIST_HEAD(fwserial_list);
61 static DEFINE_MUTEX(fwserial_list_mutex); 61 static DEFINE_MUTEX(fwserial_list_mutex);
62 62
63 /** 63 /**
64 * port_table: array of tty ports allocated to each fw_card 64 * port_table: array of tty ports allocated to each fw_card
65 * 65 *
66 * tty ports are allocated during probe when an fw_serial is first 66 * tty ports are allocated during probe when an fw_serial is first
67 * created for a given fw_card. Ports are allocated in a contiguous block, 67 * created for a given fw_card. Ports are allocated in a contiguous block,
68 * each block consisting of 'num_ports' ports. 68 * each block consisting of 'num_ports' ports.
69 */ 69 */
70 static struct fwtty_port *port_table[MAX_TOTAL_PORTS]; 70 static struct fwtty_port *port_table[MAX_TOTAL_PORTS];
71 static DEFINE_MUTEX(port_table_lock); 71 static DEFINE_MUTEX(port_table_lock);
72 static bool port_table_corrupt; 72 static bool port_table_corrupt;
73 #define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS 73 #define FWTTY_INVALID_INDEX MAX_TOTAL_PORTS
74 74
75 #define loop_idx(port) (((port)->index) / num_ports) 75 #define loop_idx(port) (((port)->index) / num_ports)
76 #define table_idx(loop) ((loop) * num_ports + num_ttys) 76 #define table_idx(loop) ((loop) * num_ports + num_ttys)
77 77
78 /* total # of tty ports created per fw_card */ 78 /* total # of tty ports created per fw_card */
79 static int num_ports; 79 static int num_ports;
80 80
81 /* slab used as pool for struct fwtty_transactions */ 81 /* slab used as pool for struct fwtty_transactions */
82 static struct kmem_cache *fwtty_txn_cache; 82 static struct kmem_cache *fwtty_txn_cache;
83 83
84 struct tty_driver *fwtty_driver; 84 struct tty_driver *fwtty_driver;
85 static struct tty_driver *fwloop_driver; 85 static struct tty_driver *fwloop_driver;
86 86
87 static struct dentry *fwserial_debugfs; 87 static struct dentry *fwserial_debugfs;
88 88
89 struct fwtty_transaction; 89 struct fwtty_transaction;
90 typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode, 90 typedef void (*fwtty_transaction_cb)(struct fw_card *card, int rcode,
91 void *data, size_t length, 91 void *data, size_t length,
92 struct fwtty_transaction *txn); 92 struct fwtty_transaction *txn);
93 93
94 struct fwtty_transaction { 94 struct fwtty_transaction {
95 struct fw_transaction fw_txn; 95 struct fw_transaction fw_txn;
96 fwtty_transaction_cb callback; 96 fwtty_transaction_cb callback;
97 struct fwtty_port *port; 97 struct fwtty_port *port;
98 union { 98 union {
99 struct dma_pending dma_pended; 99 struct dma_pending dma_pended;
100 }; 100 };
101 }; 101 };
102 102
103 #define to_device(a, b) (a->b) 103 #define to_device(a, b) (a->b)
104 #define fwtty_err(p, s, v...) dev_err(to_device(p, device), s, ##v) 104 #define fwtty_err(p, s, v...) dev_err(to_device(p, device), s, ##v)
105 #define fwtty_info(p, s, v...) dev_info(to_device(p, device), s, ##v) 105 #define fwtty_info(p, s, v...) dev_info(to_device(p, device), s, ##v)
106 #define fwtty_notice(p, s, v...) dev_notice(to_device(p, device), s, ##v) 106 #define fwtty_notice(p, s, v...) dev_notice(to_device(p, device), s, ##v)
107 #define fwtty_dbg(p, s, v...) \ 107 #define fwtty_dbg(p, s, v...) \
108 dev_dbg(to_device(p, device), "%s: " s, __func__, ##v) 108 dev_dbg(to_device(p, device), "%s: " s, __func__, ##v)
109 #define fwtty_err_ratelimited(p, s, v...) \ 109 #define fwtty_err_ratelimited(p, s, v...) \
110 dev_err_ratelimited(to_device(p, device), s, ##v) 110 dev_err_ratelimited(to_device(p, device), s, ##v)
111 111
112 #ifdef DEBUG 112 #ifdef DEBUG
113 static inline void debug_short_write(struct fwtty_port *port, int c, int n) 113 static inline void debug_short_write(struct fwtty_port *port, int c, int n)
114 { 114 {
115 int avail; 115 int avail;
116 116
117 if (n < c) { 117 if (n < c) {
118 spin_lock_bh(&port->lock); 118 spin_lock_bh(&port->lock);
119 avail = dma_fifo_avail(&port->tx_fifo); 119 avail = dma_fifo_avail(&port->tx_fifo);
120 spin_unlock_bh(&port->lock); 120 spin_unlock_bh(&port->lock);
121 fwtty_dbg(port, "short write: avail:%d req:%d wrote:%d", 121 fwtty_dbg(port, "short write: avail:%d req:%d wrote:%d",
122 avail, c, n); 122 avail, c, n);
123 } 123 }
124 } 124 }
125 #else 125 #else
126 #define debug_short_write(port, c, n) 126 #define debug_short_write(port, c, n)
127 #endif 127 #endif
128 128
129 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card, 129 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
130 int generation, int id); 130 int generation, int id);
131 131
132 #ifdef FWTTY_PROFILING 132 #ifdef FWTTY_PROFILING
133 133
134 static void profile_fifo_avail(struct fwtty_port *port, unsigned *stat) 134 static void profile_fifo_avail(struct fwtty_port *port, unsigned *stat)
135 { 135 {
136 spin_lock_bh(&port->lock); 136 spin_lock_bh(&port->lock);
137 profile_size_distrib(stat, dma_fifo_avail(&port->tx_fifo)); 137 profile_size_distrib(stat, dma_fifo_avail(&port->tx_fifo));
138 spin_unlock_bh(&port->lock); 138 spin_unlock_bh(&port->lock);
139 } 139 }
140 140
141 static void dump_profile(struct seq_file *m, struct stats *stats) 141 static void dump_profile(struct seq_file *m, struct stats *stats)
142 { 142 {
143 /* for each stat, print sum of 0 to 2^k, then individually */ 143 /* for each stat, print sum of 0 to 2^k, then individually */
144 int k = 4; 144 int k = 4;
145 unsigned sum; 145 unsigned sum;
146 int j; 146 int j;
147 char t[10]; 147 char t[10];
148 148
149 snprintf(t, 10, "< %d", 1 << k); 149 snprintf(t, 10, "< %d", 1 << k);
150 seq_printf(m, "\n%14s %6s", " ", t); 150 seq_printf(m, "\n%14s %6s", " ", t);
151 for (j = k + 1; j < DISTRIBUTION_MAX_INDEX; ++j) 151 for (j = k + 1; j < DISTRIBUTION_MAX_INDEX; ++j)
152 seq_printf(m, "%6d", 1 << j); 152 seq_printf(m, "%6d", 1 << j);
153 153
154 ++k; 154 ++k;
155 for (j = 0, sum = 0; j <= k; ++j) 155 for (j = 0, sum = 0; j <= k; ++j)
156 sum += stats->reads[j]; 156 sum += stats->reads[j];
157 seq_printf(m, "\n%14s: %6d", "reads", sum); 157 seq_printf(m, "\n%14s: %6d", "reads", sum);
158 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j) 158 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
159 seq_printf(m, "%6d", stats->reads[j]); 159 seq_printf(m, "%6d", stats->reads[j]);
160 160
161 for (j = 0, sum = 0; j <= k; ++j) 161 for (j = 0, sum = 0; j <= k; ++j)
162 sum += stats->writes[j]; 162 sum += stats->writes[j];
163 seq_printf(m, "\n%14s: %6d", "writes", sum); 163 seq_printf(m, "\n%14s: %6d", "writes", sum);
164 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j) 164 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
165 seq_printf(m, "%6d", stats->writes[j]); 165 seq_printf(m, "%6d", stats->writes[j]);
166 166
167 for (j = 0, sum = 0; j <= k; ++j) 167 for (j = 0, sum = 0; j <= k; ++j)
168 sum += stats->txns[j]; 168 sum += stats->txns[j];
169 seq_printf(m, "\n%14s: %6d", "txns", sum); 169 seq_printf(m, "\n%14s: %6d", "txns", sum);
170 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j) 170 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
171 seq_printf(m, "%6d", stats->txns[j]); 171 seq_printf(m, "%6d", stats->txns[j]);
172 172
173 for (j = 0, sum = 0; j <= k; ++j) 173 for (j = 0, sum = 0; j <= k; ++j)
174 sum += stats->unthrottle[j]; 174 sum += stats->unthrottle[j];
175 seq_printf(m, "\n%14s: %6d", "avail @ unthr", sum); 175 seq_printf(m, "\n%14s: %6d", "avail @ unthr", sum);
176 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j) 176 for (j = k + 1; j <= DISTRIBUTION_MAX_INDEX; ++j)
177 seq_printf(m, "%6d", stats->unthrottle[j]); 177 seq_printf(m, "%6d", stats->unthrottle[j]);
178 } 178 }
179 179
180 #else 180 #else
181 #define profile_fifo_avail(port, stat) 181 #define profile_fifo_avail(port, stat)
182 #define dump_profile(m, stats) 182 #define dump_profile(m, stats)
183 #endif 183 #endif
184 184
185 /* 185 /*
186 * Returns the max receive packet size for the given node 186 * Returns the max receive packet size for the given node
187 * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant 187 * Devices which are OHCI v1.0/ v1.1/ v1.2-draft or RFC 2734 compliant
188 * are required by specification to support max_rec of 8 (512 bytes) or more. 188 * are required by specification to support max_rec of 8 (512 bytes) or more.
189 */ 189 */
190 static inline int device_max_receive(struct fw_device *fw_device) 190 static inline int device_max_receive(struct fw_device *fw_device)
191 { 191 {
192 /* see IEEE 1394-2008 table 8-8 */ 192 /* see IEEE 1394-2008 table 8-8 */
193 return min(2 << fw_device->max_rec, 4096); 193 return min(2 << fw_device->max_rec, 4096);
194 } 194 }
195 195
196 static void fwtty_log_tx_error(struct fwtty_port *port, int rcode) 196 static void fwtty_log_tx_error(struct fwtty_port *port, int rcode)
197 { 197 {
198 switch (rcode) { 198 switch (rcode) {
199 case RCODE_SEND_ERROR: 199 case RCODE_SEND_ERROR:
200 fwtty_err_ratelimited(port, "card busy"); 200 fwtty_err_ratelimited(port, "card busy");
201 break; 201 break;
202 case RCODE_ADDRESS_ERROR: 202 case RCODE_ADDRESS_ERROR:
203 fwtty_err_ratelimited(port, "bad unit addr or write length"); 203 fwtty_err_ratelimited(port, "bad unit addr or write length");
204 break; 204 break;
205 case RCODE_DATA_ERROR: 205 case RCODE_DATA_ERROR:
206 fwtty_err_ratelimited(port, "failed rx"); 206 fwtty_err_ratelimited(port, "failed rx");
207 break; 207 break;
208 case RCODE_NO_ACK: 208 case RCODE_NO_ACK:
209 fwtty_err_ratelimited(port, "missing ack"); 209 fwtty_err_ratelimited(port, "missing ack");
210 break; 210 break;
211 case RCODE_BUSY: 211 case RCODE_BUSY:
212 fwtty_err_ratelimited(port, "remote busy"); 212 fwtty_err_ratelimited(port, "remote busy");
213 break; 213 break;
214 default: 214 default:
215 fwtty_err_ratelimited(port, "failed tx: %d", rcode); 215 fwtty_err_ratelimited(port, "failed tx: %d", rcode);
216 } 216 }
217 } 217 }
218 218
219 static void fwtty_txn_constructor(void *this) 219 static void fwtty_txn_constructor(void *this)
220 { 220 {
221 struct fwtty_transaction *txn = this; 221 struct fwtty_transaction *txn = this;
222 222
223 init_timer(&txn->fw_txn.split_timeout_timer); 223 init_timer(&txn->fw_txn.split_timeout_timer);
224 } 224 }
225 225
226 static void fwtty_common_callback(struct fw_card *card, int rcode, 226 static void fwtty_common_callback(struct fw_card *card, int rcode,
227 void *payload, size_t len, void *cb_data) 227 void *payload, size_t len, void *cb_data)
228 { 228 {
229 struct fwtty_transaction *txn = cb_data; 229 struct fwtty_transaction *txn = cb_data;
230 struct fwtty_port *port = txn->port; 230 struct fwtty_port *port = txn->port;
231 231
232 if (port && rcode != RCODE_COMPLETE) 232 if (port && rcode != RCODE_COMPLETE)
233 fwtty_log_tx_error(port, rcode); 233 fwtty_log_tx_error(port, rcode);
234 if (txn->callback) 234 if (txn->callback)
235 txn->callback(card, rcode, payload, len, txn); 235 txn->callback(card, rcode, payload, len, txn);
236 kmem_cache_free(fwtty_txn_cache, txn); 236 kmem_cache_free(fwtty_txn_cache, txn);
237 } 237 }
238 238
239 static int fwtty_send_data_async(struct fwtty_peer *peer, int tcode, 239 static int fwtty_send_data_async(struct fwtty_peer *peer, int tcode,
240 unsigned long long addr, void *payload, 240 unsigned long long addr, void *payload,
241 size_t len, fwtty_transaction_cb callback, 241 size_t len, fwtty_transaction_cb callback,
242 struct fwtty_port *port) 242 struct fwtty_port *port)
243 { 243 {
244 struct fwtty_transaction *txn; 244 struct fwtty_transaction *txn;
245 int generation; 245 int generation;
246 246
247 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC); 247 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
248 if (!txn) 248 if (!txn)
249 return -ENOMEM; 249 return -ENOMEM;
250 250
251 txn->callback = callback; 251 txn->callback = callback;
252 txn->port = port; 252 txn->port = port;
253 253
254 generation = peer->generation; 254 generation = peer->generation;
255 smp_rmb(); 255 smp_rmb();
256 fw_send_request(peer->serial->card, &txn->fw_txn, tcode, 256 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
257 peer->node_id, generation, peer->speed, addr, payload, 257 peer->node_id, generation, peer->speed, addr, payload,
258 len, fwtty_common_callback, txn); 258 len, fwtty_common_callback, txn);
259 return 0; 259 return 0;
260 } 260 }
261 261
262 static void fwtty_send_txn_async(struct fwtty_peer *peer, 262 static void fwtty_send_txn_async(struct fwtty_peer *peer,
263 struct fwtty_transaction *txn, int tcode, 263 struct fwtty_transaction *txn, int tcode,
264 unsigned long long addr, void *payload, 264 unsigned long long addr, void *payload,
265 size_t len, fwtty_transaction_cb callback, 265 size_t len, fwtty_transaction_cb callback,
266 struct fwtty_port *port) 266 struct fwtty_port *port)
267 { 267 {
268 int generation; 268 int generation;
269 269
270 txn->callback = callback; 270 txn->callback = callback;
271 txn->port = port; 271 txn->port = port;
272 272
273 generation = peer->generation; 273 generation = peer->generation;
274 smp_rmb(); 274 smp_rmb();
275 fw_send_request(peer->serial->card, &txn->fw_txn, tcode, 275 fw_send_request(peer->serial->card, &txn->fw_txn, tcode,
276 peer->node_id, generation, peer->speed, addr, payload, 276 peer->node_id, generation, peer->speed, addr, payload,
277 len, fwtty_common_callback, txn); 277 len, fwtty_common_callback, txn);
278 } 278 }
279 279
280 280
281 static void __fwtty_restart_tx(struct fwtty_port *port) 281 static void __fwtty_restart_tx(struct fwtty_port *port)
282 { 282 {
283 int len, avail; 283 int len, avail;
284 284
285 len = dma_fifo_out_level(&port->tx_fifo); 285 len = dma_fifo_out_level(&port->tx_fifo);
286 if (len) 286 if (len)
287 schedule_delayed_work(&port->drain, 0); 287 schedule_delayed_work(&port->drain, 0);
288 avail = dma_fifo_avail(&port->tx_fifo); 288 avail = dma_fifo_avail(&port->tx_fifo);
289 289
290 fwtty_dbg(port, "fifo len: %d avail: %d", len, avail); 290 fwtty_dbg(port, "fifo len: %d avail: %d", len, avail);
291 } 291 }
292 292
293 static void fwtty_restart_tx(struct fwtty_port *port) 293 static void fwtty_restart_tx(struct fwtty_port *port)
294 { 294 {
295 spin_lock_bh(&port->lock); 295 spin_lock_bh(&port->lock);
296 __fwtty_restart_tx(port); 296 __fwtty_restart_tx(port);
297 spin_unlock_bh(&port->lock); 297 spin_unlock_bh(&port->lock);
298 } 298 }
299 299
300 /** 300 /**
301 * fwtty_update_port_status - decodes & dispatches line status changes 301 * fwtty_update_port_status - decodes & dispatches line status changes
302 * 302 *
303 * Note: in loopback, the port->lock is being held. Only use functions that 303 * Note: in loopback, the port->lock is being held. Only use functions that
304 * don't attempt to reclaim the port->lock. 304 * don't attempt to reclaim the port->lock.
305 */ 305 */
306 static void fwtty_update_port_status(struct fwtty_port *port, unsigned status) 306 static void fwtty_update_port_status(struct fwtty_port *port, unsigned status)
307 { 307 {
308 unsigned delta; 308 unsigned delta;
309 struct tty_struct *tty; 309 struct tty_struct *tty;
310 310
311 /* simulated LSR/MSR status from remote */ 311 /* simulated LSR/MSR status from remote */
312 status &= ~MCTRL_MASK; 312 status &= ~MCTRL_MASK;
313 delta = (port->mstatus ^ status) & ~MCTRL_MASK; 313 delta = (port->mstatus ^ status) & ~MCTRL_MASK;
314 delta &= ~(status & TIOCM_RNG); 314 delta &= ~(status & TIOCM_RNG);
315 port->mstatus = status; 315 port->mstatus = status;
316 316
317 if (delta & TIOCM_RNG) 317 if (delta & TIOCM_RNG)
318 ++port->icount.rng; 318 ++port->icount.rng;
319 if (delta & TIOCM_DSR) 319 if (delta & TIOCM_DSR)
320 ++port->icount.dsr; 320 ++port->icount.dsr;
321 if (delta & TIOCM_CAR) 321 if (delta & TIOCM_CAR)
322 ++port->icount.dcd; 322 ++port->icount.dcd;
323 if (delta & TIOCM_CTS) 323 if (delta & TIOCM_CTS)
324 ++port->icount.cts; 324 ++port->icount.cts;
325 325
326 fwtty_dbg(port, "status: %x delta: %x", status, delta); 326 fwtty_dbg(port, "status: %x delta: %x", status, delta);
327 327
328 if (delta & TIOCM_CAR) { 328 if (delta & TIOCM_CAR) {
329 tty = tty_port_tty_get(&port->port); 329 tty = tty_port_tty_get(&port->port);
330 if (tty && !C_CLOCAL(tty)) { 330 if (tty && !C_CLOCAL(tty)) {
331 if (status & TIOCM_CAR) 331 if (status & TIOCM_CAR)
332 wake_up_interruptible(&port->port.open_wait); 332 wake_up_interruptible(&port->port.open_wait);
333 else 333 else
334 schedule_work(&port->hangup); 334 schedule_work(&port->hangup);
335 } 335 }
336 tty_kref_put(tty); 336 tty_kref_put(tty);
337 } 337 }
338 338
339 if (delta & TIOCM_CTS) { 339 if (delta & TIOCM_CTS) {
340 tty = tty_port_tty_get(&port->port); 340 tty = tty_port_tty_get(&port->port);
341 if (tty && C_CRTSCTS(tty)) { 341 if (tty && C_CRTSCTS(tty)) {
342 if (tty->hw_stopped) { 342 if (tty->hw_stopped) {
343 if (status & TIOCM_CTS) { 343 if (status & TIOCM_CTS) {
344 tty->hw_stopped = 0; 344 tty->hw_stopped = 0;
345 if (port->loopback) 345 if (port->loopback)
346 __fwtty_restart_tx(port); 346 __fwtty_restart_tx(port);
347 else 347 else
348 fwtty_restart_tx(port); 348 fwtty_restart_tx(port);
349 } 349 }
350 } else { 350 } else {
351 if (~status & TIOCM_CTS) 351 if (~status & TIOCM_CTS)
352 tty->hw_stopped = 1; 352 tty->hw_stopped = 1;
353 } 353 }
354 } 354 }
355 tty_kref_put(tty); 355 tty_kref_put(tty);
356 356
357 } else if (delta & OOB_TX_THROTTLE) { 357 } else if (delta & OOB_TX_THROTTLE) {
358 tty = tty_port_tty_get(&port->port); 358 tty = tty_port_tty_get(&port->port);
359 if (tty) { 359 if (tty) {
360 if (tty->hw_stopped) { 360 if (tty->hw_stopped) {
361 if (~status & OOB_TX_THROTTLE) { 361 if (~status & OOB_TX_THROTTLE) {
362 tty->hw_stopped = 0; 362 tty->hw_stopped = 0;
363 if (port->loopback) 363 if (port->loopback)
364 __fwtty_restart_tx(port); 364 __fwtty_restart_tx(port);
365 else 365 else
366 fwtty_restart_tx(port); 366 fwtty_restart_tx(port);
367 } 367 }
368 } else { 368 } else {
369 if (status & OOB_TX_THROTTLE) 369 if (status & OOB_TX_THROTTLE)
370 tty->hw_stopped = 1; 370 tty->hw_stopped = 1;
371 } 371 }
372 } 372 }
373 tty_kref_put(tty); 373 tty_kref_put(tty);
374 } 374 }
375 375
376 if (delta & (UART_LSR_BI << 24)) { 376 if (delta & (UART_LSR_BI << 24)) {
377 if (status & (UART_LSR_BI << 24)) { 377 if (status & (UART_LSR_BI << 24)) {
378 port->break_last = jiffies; 378 port->break_last = jiffies;
379 schedule_delayed_work(&port->emit_breaks, 0); 379 schedule_delayed_work(&port->emit_breaks, 0);
380 } else { 380 } else {
381 /* run emit_breaks one last time (if pending) */ 381 /* run emit_breaks one last time (if pending) */
382 mod_delayed_work(system_wq, &port->emit_breaks, 0); 382 mod_delayed_work(system_wq, &port->emit_breaks, 0);
383 } 383 }
384 } 384 }
385 385
386 if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG)) 386 if (delta & (TIOCM_DSR | TIOCM_CAR | TIOCM_CTS | TIOCM_RNG))
387 wake_up_interruptible(&port->port.delta_msr_wait); 387 wake_up_interruptible(&port->port.delta_msr_wait);
388 } 388 }
389 389
390 /** 390 /**
391 * __fwtty_port_line_status - generate 'line status' for indicated port 391 * __fwtty_port_line_status - generate 'line status' for indicated port
392 * 392 *
393 * This function returns a remote 'MSR' state based on the local 'MCR' state, 393 * This function returns a remote 'MSR' state based on the local 'MCR' state,
394 * as if a null modem cable was attached. The actual status is a mangling 394 * as if a null modem cable was attached. The actual status is a mangling
395 * of TIOCM_* bits suitable for sending to a peer's status_addr. 395 * of TIOCM_* bits suitable for sending to a peer's status_addr.
396 * 396 *
397 * Note: caller must be holding port lock 397 * Note: caller must be holding port lock
398 */ 398 */
399 static unsigned __fwtty_port_line_status(struct fwtty_port *port) 399 static unsigned __fwtty_port_line_status(struct fwtty_port *port)
400 { 400 {
401 unsigned status = 0; 401 unsigned status = 0;
402 402
403 /* TODO: add module param to tie RNG to DTR as well */ 403 /* TODO: add module param to tie RNG to DTR as well */
404 404
405 if (port->mctrl & TIOCM_DTR) 405 if (port->mctrl & TIOCM_DTR)
406 status |= TIOCM_DSR | TIOCM_CAR; 406 status |= TIOCM_DSR | TIOCM_CAR;
407 if (port->mctrl & TIOCM_RTS) 407 if (port->mctrl & TIOCM_RTS)
408 status |= TIOCM_CTS; 408 status |= TIOCM_CTS;
409 if (port->mctrl & OOB_RX_THROTTLE) 409 if (port->mctrl & OOB_RX_THROTTLE)
410 status |= OOB_TX_THROTTLE; 410 status |= OOB_TX_THROTTLE;
411 /* emulate BRK as add'l line status */ 411 /* emulate BRK as add'l line status */
412 if (port->break_ctl) 412 if (port->break_ctl)
413 status |= UART_LSR_BI << 24; 413 status |= UART_LSR_BI << 24;
414 414
415 return status; 415 return status;
416 } 416 }
417 417
418 /** 418 /**
419 * __fwtty_write_port_status - send the port line status to peer 419 * __fwtty_write_port_status - send the port line status to peer
420 * 420 *
421 * Note: caller must be holding the port lock. 421 * Note: caller must be holding the port lock.
422 */ 422 */
423 static int __fwtty_write_port_status(struct fwtty_port *port) 423 static int __fwtty_write_port_status(struct fwtty_port *port)
424 { 424 {
425 struct fwtty_peer *peer; 425 struct fwtty_peer *peer;
426 int err = -ENOENT; 426 int err = -ENOENT;
427 unsigned status = __fwtty_port_line_status(port); 427 unsigned status = __fwtty_port_line_status(port);
428 428
429 rcu_read_lock(); 429 rcu_read_lock();
430 peer = rcu_dereference(port->peer); 430 peer = rcu_dereference(port->peer);
431 if (peer) { 431 if (peer) {
432 err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST, 432 err = fwtty_send_data_async(peer, TCODE_WRITE_QUADLET_REQUEST,
433 peer->status_addr, &status, 433 peer->status_addr, &status,
434 sizeof(status), NULL, port); 434 sizeof(status), NULL, port);
435 } 435 }
436 rcu_read_unlock(); 436 rcu_read_unlock();
437 437
438 return err; 438 return err;
439 } 439 }
440 440
441 /** 441 /**
442 * fwtty_write_port_status - same as above but locked by port lock 442 * fwtty_write_port_status - same as above but locked by port lock
443 */ 443 */
444 static int fwtty_write_port_status(struct fwtty_port *port) 444 static int fwtty_write_port_status(struct fwtty_port *port)
445 { 445 {
446 int err; 446 int err;
447 447
448 spin_lock_bh(&port->lock); 448 spin_lock_bh(&port->lock);
449 err = __fwtty_write_port_status(port); 449 err = __fwtty_write_port_status(port);
450 spin_unlock_bh(&port->lock); 450 spin_unlock_bh(&port->lock);
451 return err; 451 return err;
452 } 452 }
453 453
454 static void __fwtty_throttle(struct fwtty_port *port, struct tty_struct *tty) 454 static void __fwtty_throttle(struct fwtty_port *port, struct tty_struct *tty)
455 { 455 {
456 unsigned old; 456 unsigned old;
457 457
458 old = port->mctrl; 458 old = port->mctrl;
459 port->mctrl |= OOB_RX_THROTTLE; 459 port->mctrl |= OOB_RX_THROTTLE;
460 if (C_CRTSCTS(tty)) 460 if (C_CRTSCTS(tty))
461 port->mctrl &= ~TIOCM_RTS; 461 port->mctrl &= ~TIOCM_RTS;
462 if (~old & OOB_RX_THROTTLE) 462 if (~old & OOB_RX_THROTTLE)
463 __fwtty_write_port_status(port); 463 __fwtty_write_port_status(port);
464 } 464 }
465 465
466 /** 466 /**
467 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup 467 * fwtty_do_hangup - wait for ldisc to deliver all pending rx; only then hangup
468 * 468 *
469 * When the remote has finished tx, and all in-flight rx has been received and 469 * When the remote has finished tx, and all in-flight rx has been received and
470 * and pushed to the flip buffer, the remote may close its device. This will 470 * and pushed to the flip buffer, the remote may close its device. This will
471 * drop DTR on the remote which will drop carrier here. Typically, the tty is 471 * drop DTR on the remote which will drop carrier here. Typically, the tty is
472 * hung up when carrier is dropped or lost. 472 * hung up when carrier is dropped or lost.
473 * 473 *
474 * However, there is a race between the hang up and the line discipline 474 * However, there is a race between the hang up and the line discipline
475 * delivering its data to the reader. A hangup will cause the ldisc to flush 475 * delivering its data to the reader. A hangup will cause the ldisc to flush
476 * (ie., clear) the read buffer and flip buffer. Because of firewire's 476 * (ie., clear) the read buffer and flip buffer. Because of firewire's
477 * relatively high throughput, the ldisc frequently lags well behind the driver, 477 * relatively high throughput, the ldisc frequently lags well behind the driver,
478 * resulting in lost data (which has already been received and written to 478 * resulting in lost data (which has already been received and written to
479 * the flip buffer) when the remote closes its end. 479 * the flip buffer) when the remote closes its end.
480 * 480 *
481 * Unfortunately, since the flip buffer offers no direct method for determining 481 * Unfortunately, since the flip buffer offers no direct method for determining
482 * if it holds data, ensuring the ldisc has delivered all data is problematic. 482 * if it holds data, ensuring the ldisc has delivered all data is problematic.
483 */ 483 */
484 484
485 /* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */ 485 /* FIXME: drop this workaround when __tty_hangup waits for ldisc completion */
486 static void fwtty_do_hangup(struct work_struct *work) 486 static void fwtty_do_hangup(struct work_struct *work)
487 { 487 {
488 struct fwtty_port *port = to_port(work, hangup); 488 struct fwtty_port *port = to_port(work, hangup);
489 struct tty_struct *tty; 489 struct tty_struct *tty;
490 490
491 schedule_timeout_uninterruptible(msecs_to_jiffies(50)); 491 schedule_timeout_uninterruptible(msecs_to_jiffies(50));
492 492
493 tty = tty_port_tty_get(&port->port); 493 tty = tty_port_tty_get(&port->port);
494 if (tty) 494 if (tty)
495 tty_vhangup(tty); 495 tty_vhangup(tty);
496 tty_kref_put(tty); 496 tty_kref_put(tty);
497 } 497 }
498 498
499 499
500 static void fwtty_emit_breaks(struct work_struct *work) 500 static void fwtty_emit_breaks(struct work_struct *work)
501 { 501 {
502 struct fwtty_port *port = to_port(to_delayed_work(work), emit_breaks); 502 struct fwtty_port *port = to_port(to_delayed_work(work), emit_breaks);
503 static const char buf[16]; 503 static const char buf[16];
504 unsigned long now = jiffies; 504 unsigned long now = jiffies;
505 unsigned long elapsed = now - port->break_last; 505 unsigned long elapsed = now - port->break_last;
506 int n, t, c, brk = 0; 506 int n, t, c, brk = 0;
507 507
508 /* generate breaks at the line rate (but at least 1) */ 508 /* generate breaks at the line rate (but at least 1) */
509 n = (elapsed * port->cps) / HZ + 1; 509 n = (elapsed * port->cps) / HZ + 1;
510 port->break_last = now; 510 port->break_last = now;
511 511
512 fwtty_dbg(port, "sending %d brks", n); 512 fwtty_dbg(port, "sending %d brks", n);
513 513
514 while (n) { 514 while (n) {
515 t = min(n, 16); 515 t = min(n, 16);
516 c = tty_insert_flip_string_fixed_flag(&port->port, buf, 516 c = tty_insert_flip_string_fixed_flag(&port->port, buf,
517 TTY_BREAK, t); 517 TTY_BREAK, t);
518 n -= c; 518 n -= c;
519 brk += c; 519 brk += c;
520 if (c < t) 520 if (c < t)
521 break; 521 break;
522 } 522 }
523 tty_flip_buffer_push(&port->port); 523 tty_flip_buffer_push(&port->port);
524 524
525 if (port->mstatus & (UART_LSR_BI << 24)) 525 if (port->mstatus & (UART_LSR_BI << 24))
526 schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS); 526 schedule_delayed_work(&port->emit_breaks, FREQ_BREAKS);
527 port->icount.brk += brk; 527 port->icount.brk += brk;
528 } 528 }
529 529
530 static void fwtty_pushrx(struct work_struct *work) 530 static void fwtty_pushrx(struct work_struct *work)
531 { 531 {
532 struct fwtty_port *port = to_port(work, push); 532 struct fwtty_port *port = to_port(work, push);
533 struct tty_struct *tty; 533 struct tty_struct *tty;
534 struct buffered_rx *buf, *next; 534 struct buffered_rx *buf, *next;
535 int n, c = 0; 535 int n, c = 0;
536 536
537 spin_lock_bh(&port->lock); 537 spin_lock_bh(&port->lock);
538 list_for_each_entry_safe(buf, next, &port->buf_list, list) { 538 list_for_each_entry_safe(buf, next, &port->buf_list, list) {
539 n = tty_insert_flip_string_fixed_flag(&port->port, buf->data, 539 n = tty_insert_flip_string_fixed_flag(&port->port, buf->data,
540 TTY_NORMAL, buf->n); 540 TTY_NORMAL, buf->n);
541 c += n; 541 c += n;
542 port->buffered -= n; 542 port->buffered -= n;
543 if (n < buf->n) { 543 if (n < buf->n) {
544 if (n > 0) { 544 if (n > 0) {
545 memmove(buf->data, buf->data + n, buf->n - n); 545 memmove(buf->data, buf->data + n, buf->n - n);
546 buf->n -= n; 546 buf->n -= n;
547 } 547 }
548 tty = tty_port_tty_get(&port->port); 548 tty = tty_port_tty_get(&port->port);
549 if (tty) { 549 if (tty) {
550 __fwtty_throttle(port, tty); 550 __fwtty_throttle(port, tty);
551 tty_kref_put(tty); 551 tty_kref_put(tty);
552 } 552 }
553 break; 553 break;
554 } else { 554 } else {
555 list_del(&buf->list); 555 list_del(&buf->list);
556 kfree(buf); 556 kfree(buf);
557 } 557 }
558 } 558 }
559 if (c > 0) 559 if (c > 0)
560 tty_flip_buffer_push(&port->port); 560 tty_flip_buffer_push(&port->port);
561 561
562 if (list_empty(&port->buf_list)) 562 if (list_empty(&port->buf_list))
563 clear_bit(BUFFERING_RX, &port->flags); 563 clear_bit(BUFFERING_RX, &port->flags);
564 spin_unlock_bh(&port->lock); 564 spin_unlock_bh(&port->lock);
565 } 565 }
566 566
567 static int fwtty_buffer_rx(struct fwtty_port *port, unsigned char *d, size_t n) 567 static int fwtty_buffer_rx(struct fwtty_port *port, unsigned char *d, size_t n)
568 { 568 {
569 struct buffered_rx *buf; 569 struct buffered_rx *buf;
570 size_t size = (n + sizeof(struct buffered_rx) + 0xFF) & ~0xFF; 570 size_t size = (n + sizeof(struct buffered_rx) + 0xFF) & ~0xFF;
571 571
572 if (port->buffered + n > HIGH_WATERMARK) { 572 if (port->buffered + n > HIGH_WATERMARK) {
573 fwtty_err_ratelimited(port, "overflowed rx buffer: buffered: %d new: %zu wtrmk: %d", 573 fwtty_err_ratelimited(port, "overflowed rx buffer: buffered: %d new: %zu wtrmk: %d",
574 port->buffered, n, HIGH_WATERMARK); 574 port->buffered, n, HIGH_WATERMARK);
575 return 0; 575 return 0;
576 } 576 }
577 buf = kmalloc(size, GFP_ATOMIC); 577 buf = kmalloc(size, GFP_ATOMIC);
578 if (!buf) 578 if (!buf)
579 return 0; 579 return 0;
580 INIT_LIST_HEAD(&buf->list); 580 INIT_LIST_HEAD(&buf->list);
581 buf->n = n; 581 buf->n = n;
582 memcpy(buf->data, d, n); 582 memcpy(buf->data, d, n);
583 583
584 spin_lock_bh(&port->lock); 584 spin_lock_bh(&port->lock);
585 list_add_tail(&buf->list, &port->buf_list); 585 list_add_tail(&buf->list, &port->buf_list);
586 port->buffered += n; 586 port->buffered += n;
587 if (port->buffered > port->stats.watermark) 587 if (port->buffered > port->stats.watermark)
588 port->stats.watermark = port->buffered; 588 port->stats.watermark = port->buffered;
589 set_bit(BUFFERING_RX, &port->flags); 589 set_bit(BUFFERING_RX, &port->flags);
590 spin_unlock_bh(&port->lock); 590 spin_unlock_bh(&port->lock);
591 591
592 return n; 592 return n;
593 } 593 }
594 594
595 static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len) 595 static int fwtty_rx(struct fwtty_port *port, unsigned char *data, size_t len)
596 { 596 {
597 struct tty_struct *tty; 597 struct tty_struct *tty;
598 int c, n = len; 598 int c, n = len;
599 unsigned lsr; 599 unsigned lsr;
600 int err = 0; 600 int err = 0;
601 601
602 fwtty_dbg(port, "%d", n); 602 fwtty_dbg(port, "%d", n);
603 profile_size_distrib(port->stats.reads, n); 603 profile_size_distrib(port->stats.reads, n);
604 604
605 if (port->write_only) { 605 if (port->write_only) {
606 n = 0; 606 n = 0;
607 goto out; 607 goto out;
608 } 608 }
609 609
610 /* disregard break status; breaks are generated by emit_breaks work */ 610 /* disregard break status; breaks are generated by emit_breaks work */
611 lsr = (port->mstatus >> 24) & ~UART_LSR_BI; 611 lsr = (port->mstatus >> 24) & ~UART_LSR_BI;
612 612
613 if (port->overrun) 613 if (port->overrun)
614 lsr |= UART_LSR_OE; 614 lsr |= UART_LSR_OE;
615 615
616 if (lsr & UART_LSR_OE) 616 if (lsr & UART_LSR_OE)
617 ++port->icount.overrun; 617 ++port->icount.overrun;
618 618
619 lsr &= port->status_mask; 619 lsr &= port->status_mask;
620 if (lsr & ~port->ignore_mask & UART_LSR_OE) { 620 if (lsr & ~port->ignore_mask & UART_LSR_OE) {
621 if (!tty_insert_flip_char(&port->port, 0, TTY_OVERRUN)) { 621 if (!tty_insert_flip_char(&port->port, 0, TTY_OVERRUN)) {
622 err = -EIO; 622 err = -EIO;
623 goto out; 623 goto out;
624 } 624 }
625 } 625 }
626 port->overrun = false; 626 port->overrun = false;
627 627
628 if (lsr & port->ignore_mask & ~UART_LSR_OE) { 628 if (lsr & port->ignore_mask & ~UART_LSR_OE) {
629 /* TODO: don't drop SAK and Magic SysRq here */ 629 /* TODO: don't drop SAK and Magic SysRq here */
630 n = 0; 630 n = 0;
631 goto out; 631 goto out;
632 } 632 }
633 633
634 if (!test_bit(BUFFERING_RX, &port->flags)) { 634 if (!test_bit(BUFFERING_RX, &port->flags)) {
635 c = tty_insert_flip_string_fixed_flag(&port->port, data, 635 c = tty_insert_flip_string_fixed_flag(&port->port, data,
636 TTY_NORMAL, n); 636 TTY_NORMAL, n);
637 if (c > 0) 637 if (c > 0)
638 tty_flip_buffer_push(&port->port); 638 tty_flip_buffer_push(&port->port);
639 n -= c; 639 n -= c;
640 640
641 if (n) { 641 if (n) {
642 /* start buffering and throttling */ 642 /* start buffering and throttling */
643 n -= fwtty_buffer_rx(port, &data[c], n); 643 n -= fwtty_buffer_rx(port, &data[c], n);
644 644
645 tty = tty_port_tty_get(&port->port); 645 tty = tty_port_tty_get(&port->port);
646 if (tty) { 646 if (tty) {
647 spin_lock_bh(&port->lock); 647 spin_lock_bh(&port->lock);
648 __fwtty_throttle(port, tty); 648 __fwtty_throttle(port, tty);
649 spin_unlock_bh(&port->lock); 649 spin_unlock_bh(&port->lock);
650 tty_kref_put(tty); 650 tty_kref_put(tty);
651 } 651 }
652 } 652 }
653 } else 653 } else
654 n -= fwtty_buffer_rx(port, data, n); 654 n -= fwtty_buffer_rx(port, data, n);
655 655
656 if (n) { 656 if (n) {
657 port->overrun = true; 657 port->overrun = true;
658 err = -EIO; 658 err = -EIO;
659 } 659 }
660 660
661 out: 661 out:
662 port->icount.rx += len; 662 port->icount.rx += len;
663 port->stats.lost += n; 663 port->stats.lost += n;
664 return err; 664 return err;
665 } 665 }
666 666
667 /** 667 /**
668 * fwtty_port_handler - bus address handler for port reads/writes 668 * fwtty_port_handler - bus address handler for port reads/writes
669 * @parameters: fw_address_callback_t as specified by firewire core interface 669 * @parameters: fw_address_callback_t as specified by firewire core interface
670 * 670 *
671 * This handler is responsible for handling inbound read/write dma from remotes. 671 * This handler is responsible for handling inbound read/write dma from remotes.
672 */ 672 */
673 static void fwtty_port_handler(struct fw_card *card, 673 static void fwtty_port_handler(struct fw_card *card,
674 struct fw_request *request, 674 struct fw_request *request,
675 int tcode, int destination, int source, 675 int tcode, int destination, int source,
676 int generation, 676 int generation,
677 unsigned long long addr, 677 unsigned long long addr,
678 void *data, size_t len, 678 void *data, size_t len,
679 void *callback_data) 679 void *callback_data)
680 { 680 {
681 struct fwtty_port *port = callback_data; 681 struct fwtty_port *port = callback_data;
682 struct fwtty_peer *peer; 682 struct fwtty_peer *peer;
683 int err; 683 int err;
684 int rcode; 684 int rcode;
685 685
686 /* Only accept rx from the peer virtual-cabled to this port */ 686 /* Only accept rx from the peer virtual-cabled to this port */
687 rcu_read_lock(); 687 rcu_read_lock();
688 peer = __fwserial_peer_by_node_id(card, generation, source); 688 peer = __fwserial_peer_by_node_id(card, generation, source);
689 rcu_read_unlock(); 689 rcu_read_unlock();
690 if (!peer || peer != rcu_access_pointer(port->peer)) { 690 if (!peer || peer != rcu_access_pointer(port->peer)) {
691 rcode = RCODE_ADDRESS_ERROR; 691 rcode = RCODE_ADDRESS_ERROR;
692 fwtty_err_ratelimited(port, "ignoring unauthenticated data"); 692 fwtty_err_ratelimited(port, "ignoring unauthenticated data");
693 goto respond; 693 goto respond;
694 } 694 }
695 695
696 switch (tcode) { 696 switch (tcode) {
697 case TCODE_WRITE_QUADLET_REQUEST: 697 case TCODE_WRITE_QUADLET_REQUEST:
698 if (addr != port->rx_handler.offset || len != 4) 698 if (addr != port->rx_handler.offset || len != 4)
699 rcode = RCODE_ADDRESS_ERROR; 699 rcode = RCODE_ADDRESS_ERROR;
700 else { 700 else {
701 fwtty_update_port_status(port, *(unsigned *)data); 701 fwtty_update_port_status(port, *(unsigned *)data);
702 rcode = RCODE_COMPLETE; 702 rcode = RCODE_COMPLETE;
703 } 703 }
704 break; 704 break;
705 705
706 case TCODE_WRITE_BLOCK_REQUEST: 706 case TCODE_WRITE_BLOCK_REQUEST:
707 if (addr != port->rx_handler.offset + 4 || 707 if (addr != port->rx_handler.offset + 4 ||
708 len > port->rx_handler.length - 4) { 708 len > port->rx_handler.length - 4) {
709 rcode = RCODE_ADDRESS_ERROR; 709 rcode = RCODE_ADDRESS_ERROR;
710 } else { 710 } else {
711 err = fwtty_rx(port, data, len); 711 err = fwtty_rx(port, data, len);
712 switch (err) { 712 switch (err) {
713 case 0: 713 case 0:
714 rcode = RCODE_COMPLETE; 714 rcode = RCODE_COMPLETE;
715 break; 715 break;
716 case -EIO: 716 case -EIO:
717 rcode = RCODE_DATA_ERROR; 717 rcode = RCODE_DATA_ERROR;
718 break; 718 break;
719 default: 719 default:
720 rcode = RCODE_CONFLICT_ERROR; 720 rcode = RCODE_CONFLICT_ERROR;
721 break; 721 break;
722 } 722 }
723 } 723 }
724 break; 724 break;
725 725
726 default: 726 default:
727 rcode = RCODE_TYPE_ERROR; 727 rcode = RCODE_TYPE_ERROR;
728 } 728 }
729 729
730 respond: 730 respond:
731 fw_send_response(card, request, rcode); 731 fw_send_response(card, request, rcode);
732 } 732 }
733 733
734 /** 734 /**
735 * fwtty_tx_complete - callback for tx dma 735 * fwtty_tx_complete - callback for tx dma
736 * @data: ignored, has no meaning for write txns 736 * @data: ignored, has no meaning for write txns
737 * @length: ignored, has no meaning for write txns 737 * @length: ignored, has no meaning for write txns
738 * 738 *
739 * The writer must be woken here if the fifo has been emptied because it 739 * The writer must be woken here if the fifo has been emptied because it
740 * may have slept if chars_in_buffer was != 0 740 * may have slept if chars_in_buffer was != 0
741 */ 741 */
742 static void fwtty_tx_complete(struct fw_card *card, int rcode, 742 static void fwtty_tx_complete(struct fw_card *card, int rcode,
743 void *data, size_t length, 743 void *data, size_t length,
744 struct fwtty_transaction *txn) 744 struct fwtty_transaction *txn)
745 { 745 {
746 struct fwtty_port *port = txn->port; 746 struct fwtty_port *port = txn->port;
747 struct tty_struct *tty;
748 int len; 747 int len;
749 748
750 fwtty_dbg(port, "rcode: %d", rcode); 749 fwtty_dbg(port, "rcode: %d", rcode);
751 750
752 switch (rcode) { 751 switch (rcode) {
753 case RCODE_COMPLETE: 752 case RCODE_COMPLETE:
754 spin_lock_bh(&port->lock); 753 spin_lock_bh(&port->lock);
755 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended); 754 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
756 len = dma_fifo_level(&port->tx_fifo); 755 len = dma_fifo_level(&port->tx_fifo);
757 spin_unlock_bh(&port->lock); 756 spin_unlock_bh(&port->lock);
758 757
759 port->icount.tx += txn->dma_pended.len; 758 port->icount.tx += txn->dma_pended.len;
760 break; 759 break;
761 760
762 default: 761 default:
763 /* TODO: implement retries */ 762 /* TODO: implement retries */
764 spin_lock_bh(&port->lock); 763 spin_lock_bh(&port->lock);
765 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended); 764 dma_fifo_out_complete(&port->tx_fifo, &txn->dma_pended);
766 len = dma_fifo_level(&port->tx_fifo); 765 len = dma_fifo_level(&port->tx_fifo);
767 spin_unlock_bh(&port->lock); 766 spin_unlock_bh(&port->lock);
768 767
769 port->stats.dropped += txn->dma_pended.len; 768 port->stats.dropped += txn->dma_pended.len;
770 } 769 }
771 770
772 if (len < WAKEUP_CHARS) { 771 if (len < WAKEUP_CHARS)
773 tty = tty_port_tty_get(&port->port); 772 tty_port_tty_wakeup(&port->port);
774 if (tty) {
775 tty_wakeup(tty);
776 tty_kref_put(tty);
777 }
778 }
779 } 773 }
780 774
781 static int fwtty_tx(struct fwtty_port *port, bool drain) 775 static int fwtty_tx(struct fwtty_port *port, bool drain)
782 { 776 {
783 struct fwtty_peer *peer; 777 struct fwtty_peer *peer;
784 struct fwtty_transaction *txn; 778 struct fwtty_transaction *txn;
785 struct tty_struct *tty; 779 struct tty_struct *tty;
786 int n, len; 780 int n, len;
787 781
788 tty = tty_port_tty_get(&port->port); 782 tty = tty_port_tty_get(&port->port);
789 if (!tty) 783 if (!tty)
790 return -ENOENT; 784 return -ENOENT;
791 785
792 rcu_read_lock(); 786 rcu_read_lock();
793 peer = rcu_dereference(port->peer); 787 peer = rcu_dereference(port->peer);
794 if (!peer) { 788 if (!peer) {
795 n = -EIO; 789 n = -EIO;
796 goto out; 790 goto out;
797 } 791 }
798 792
799 if (test_and_set_bit(IN_TX, &port->flags)) { 793 if (test_and_set_bit(IN_TX, &port->flags)) {
800 n = -EALREADY; 794 n = -EALREADY;
801 goto out; 795 goto out;
802 } 796 }
803 797
804 /* try to write as many dma transactions out as possible */ 798 /* try to write as many dma transactions out as possible */
805 n = -EAGAIN; 799 n = -EAGAIN;
806 while (!tty->stopped && !tty->hw_stopped && 800 while (!tty->stopped && !tty->hw_stopped &&
807 !test_bit(STOP_TX, &port->flags)) { 801 !test_bit(STOP_TX, &port->flags)) {
808 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC); 802 txn = kmem_cache_alloc(fwtty_txn_cache, GFP_ATOMIC);
809 if (!txn) { 803 if (!txn) {
810 n = -ENOMEM; 804 n = -ENOMEM;
811 break; 805 break;
812 } 806 }
813 807
814 spin_lock_bh(&port->lock); 808 spin_lock_bh(&port->lock);
815 n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended); 809 n = dma_fifo_out_pend(&port->tx_fifo, &txn->dma_pended);
816 spin_unlock_bh(&port->lock); 810 spin_unlock_bh(&port->lock);
817 811
818 fwtty_dbg(port, "out: %u rem: %d", txn->dma_pended.len, n); 812 fwtty_dbg(port, "out: %u rem: %d", txn->dma_pended.len, n);
819 813
820 if (n < 0) { 814 if (n < 0) {
821 kmem_cache_free(fwtty_txn_cache, txn); 815 kmem_cache_free(fwtty_txn_cache, txn);
822 if (n == -EAGAIN) 816 if (n == -EAGAIN)
823 ++port->stats.tx_stall; 817 ++port->stats.tx_stall;
824 else if (n == -ENODATA) 818 else if (n == -ENODATA)
825 profile_size_distrib(port->stats.txns, 0); 819 profile_size_distrib(port->stats.txns, 0);
826 else { 820 else {
827 ++port->stats.fifo_errs; 821 ++port->stats.fifo_errs;
828 fwtty_err_ratelimited(port, "fifo err: %d", n); 822 fwtty_err_ratelimited(port, "fifo err: %d", n);
829 } 823 }
830 break; 824 break;
831 } 825 }
832 826
833 profile_size_distrib(port->stats.txns, txn->dma_pended.len); 827 profile_size_distrib(port->stats.txns, txn->dma_pended.len);
834 828
835 fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST, 829 fwtty_send_txn_async(peer, txn, TCODE_WRITE_BLOCK_REQUEST,
836 peer->fifo_addr, txn->dma_pended.data, 830 peer->fifo_addr, txn->dma_pended.data,
837 txn->dma_pended.len, fwtty_tx_complete, 831 txn->dma_pended.len, fwtty_tx_complete,
838 port); 832 port);
839 ++port->stats.sent; 833 ++port->stats.sent;
840 834
841 /* 835 /*
842 * Stop tx if the 'last view' of the fifo is empty or if 836 * Stop tx if the 'last view' of the fifo is empty or if
843 * this is the writer and there's not enough data to bother 837 * this is the writer and there's not enough data to bother
844 */ 838 */
845 if (n == 0 || (!drain && n < WRITER_MINIMUM)) 839 if (n == 0 || (!drain && n < WRITER_MINIMUM))
846 break; 840 break;
847 } 841 }
848 842
849 if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) { 843 if (n >= 0 || n == -EAGAIN || n == -ENOMEM || n == -ENODATA) {
850 spin_lock_bh(&port->lock); 844 spin_lock_bh(&port->lock);
851 len = dma_fifo_out_level(&port->tx_fifo); 845 len = dma_fifo_out_level(&port->tx_fifo);
852 if (len) { 846 if (len) {
853 unsigned long delay = (n == -ENOMEM) ? HZ : 1; 847 unsigned long delay = (n == -ENOMEM) ? HZ : 1;
854 schedule_delayed_work(&port->drain, delay); 848 schedule_delayed_work(&port->drain, delay);
855 } 849 }
856 len = dma_fifo_level(&port->tx_fifo); 850 len = dma_fifo_level(&port->tx_fifo);
857 spin_unlock_bh(&port->lock); 851 spin_unlock_bh(&port->lock);
858 852
859 /* wakeup the writer */ 853 /* wakeup the writer */
860 if (drain && len < WAKEUP_CHARS) 854 if (drain && len < WAKEUP_CHARS)
861 tty_wakeup(tty); 855 tty_wakeup(tty);
862 } 856 }
863 857
864 clear_bit(IN_TX, &port->flags); 858 clear_bit(IN_TX, &port->flags);
865 wake_up_interruptible(&port->wait_tx); 859 wake_up_interruptible(&port->wait_tx);
866 860
867 out: 861 out:
868 rcu_read_unlock(); 862 rcu_read_unlock();
869 tty_kref_put(tty); 863 tty_kref_put(tty);
870 return n; 864 return n;
871 } 865 }
872 866
873 static void fwtty_drain_tx(struct work_struct *work) 867 static void fwtty_drain_tx(struct work_struct *work)
874 { 868 {
875 struct fwtty_port *port = to_port(to_delayed_work(work), drain); 869 struct fwtty_port *port = to_port(to_delayed_work(work), drain);
876 870
877 fwtty_tx(port, true); 871 fwtty_tx(port, true);
878 } 872 }
879 873
880 static void fwtty_write_xchar(struct fwtty_port *port, char ch) 874 static void fwtty_write_xchar(struct fwtty_port *port, char ch)
881 { 875 {
882 struct fwtty_peer *peer; 876 struct fwtty_peer *peer;
883 877
884 ++port->stats.xchars; 878 ++port->stats.xchars;
885 879
886 fwtty_dbg(port, "%02x", ch); 880 fwtty_dbg(port, "%02x", ch);
887 881
888 rcu_read_lock(); 882 rcu_read_lock();
889 peer = rcu_dereference(port->peer); 883 peer = rcu_dereference(port->peer);
890 if (peer) { 884 if (peer) {
891 fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST, 885 fwtty_send_data_async(peer, TCODE_WRITE_BLOCK_REQUEST,
892 peer->fifo_addr, &ch, sizeof(ch), 886 peer->fifo_addr, &ch, sizeof(ch),
893 NULL, port); 887 NULL, port);
894 } 888 }
895 rcu_read_unlock(); 889 rcu_read_unlock();
896 } 890 }
897 891
898 struct fwtty_port *fwtty_port_get(unsigned index) 892 struct fwtty_port *fwtty_port_get(unsigned index)
899 { 893 {
900 struct fwtty_port *port; 894 struct fwtty_port *port;
901 895
902 if (index >= MAX_TOTAL_PORTS) 896 if (index >= MAX_TOTAL_PORTS)
903 return NULL; 897 return NULL;
904 898
905 mutex_lock(&port_table_lock); 899 mutex_lock(&port_table_lock);
906 port = port_table[index]; 900 port = port_table[index];
907 if (port) 901 if (port)
908 kref_get(&port->serial->kref); 902 kref_get(&port->serial->kref);
909 mutex_unlock(&port_table_lock); 903 mutex_unlock(&port_table_lock);
910 return port; 904 return port;
911 } 905 }
912 EXPORT_SYMBOL(fwtty_port_get); 906 EXPORT_SYMBOL(fwtty_port_get);
913 907
914 static int fwtty_ports_add(struct fw_serial *serial) 908 static int fwtty_ports_add(struct fw_serial *serial)
915 { 909 {
916 int err = -EBUSY; 910 int err = -EBUSY;
917 int i, j; 911 int i, j;
918 912
919 if (port_table_corrupt) 913 if (port_table_corrupt)
920 return err; 914 return err;
921 915
922 mutex_lock(&port_table_lock); 916 mutex_lock(&port_table_lock);
923 for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) { 917 for (i = 0; i + num_ports <= MAX_TOTAL_PORTS; i += num_ports) {
924 if (!port_table[i]) { 918 if (!port_table[i]) {
925 for (j = 0; j < num_ports; ++i, ++j) { 919 for (j = 0; j < num_ports; ++i, ++j) {
926 serial->ports[j]->index = i; 920 serial->ports[j]->index = i;
927 port_table[i] = serial->ports[j]; 921 port_table[i] = serial->ports[j];
928 } 922 }
929 err = 0; 923 err = 0;
930 break; 924 break;
931 } 925 }
932 } 926 }
933 mutex_unlock(&port_table_lock); 927 mutex_unlock(&port_table_lock);
934 return err; 928 return err;
935 } 929 }
936 930
937 static void fwserial_destroy(struct kref *kref) 931 static void fwserial_destroy(struct kref *kref)
938 { 932 {
939 struct fw_serial *serial = to_serial(kref, kref); 933 struct fw_serial *serial = to_serial(kref, kref);
940 struct fwtty_port **ports = serial->ports; 934 struct fwtty_port **ports = serial->ports;
941 int j, i = ports[0]->index; 935 int j, i = ports[0]->index;
942 936
943 synchronize_rcu(); 937 synchronize_rcu();
944 938
945 mutex_lock(&port_table_lock); 939 mutex_lock(&port_table_lock);
946 for (j = 0; j < num_ports; ++i, ++j) { 940 for (j = 0; j < num_ports; ++i, ++j) {
947 port_table_corrupt |= port_table[i] != ports[j]; 941 port_table_corrupt |= port_table[i] != ports[j];
948 WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p", 942 WARN_ONCE(port_table_corrupt, "port_table[%d]: %p != ports[%d]: %p",
949 i, port_table[i], j, ports[j]); 943 i, port_table[i], j, ports[j]);
950 944
951 port_table[i] = NULL; 945 port_table[i] = NULL;
952 } 946 }
953 mutex_unlock(&port_table_lock); 947 mutex_unlock(&port_table_lock);
954 948
955 for (j = 0; j < num_ports; ++j) { 949 for (j = 0; j < num_ports; ++j) {
956 fw_core_remove_address_handler(&ports[j]->rx_handler); 950 fw_core_remove_address_handler(&ports[j]->rx_handler);
957 tty_port_destroy(&ports[j]->port); 951 tty_port_destroy(&ports[j]->port);
958 kfree(ports[j]); 952 kfree(ports[j]);
959 } 953 }
960 kfree(serial); 954 kfree(serial);
961 } 955 }
962 956
963 void fwtty_port_put(struct fwtty_port *port) 957 void fwtty_port_put(struct fwtty_port *port)
964 { 958 {
965 kref_put(&port->serial->kref, fwserial_destroy); 959 kref_put(&port->serial->kref, fwserial_destroy);
966 } 960 }
967 EXPORT_SYMBOL(fwtty_port_put); 961 EXPORT_SYMBOL(fwtty_port_put);
968 962
969 static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on) 963 static void fwtty_port_dtr_rts(struct tty_port *tty_port, int on)
970 { 964 {
971 struct fwtty_port *port = to_port(tty_port, port); 965 struct fwtty_port *port = to_port(tty_port, port);
972 966
973 fwtty_dbg(port, "on/off: %d", on); 967 fwtty_dbg(port, "on/off: %d", on);
974 968
975 spin_lock_bh(&port->lock); 969 spin_lock_bh(&port->lock);
976 /* Don't change carrier state if this is a console */ 970 /* Don't change carrier state if this is a console */
977 if (!port->port.console) { 971 if (!port->port.console) {
978 if (on) 972 if (on)
979 port->mctrl |= TIOCM_DTR | TIOCM_RTS; 973 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
980 else 974 else
981 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS); 975 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
982 } 976 }
983 977
984 __fwtty_write_port_status(port); 978 __fwtty_write_port_status(port);
985 spin_unlock_bh(&port->lock); 979 spin_unlock_bh(&port->lock);
986 } 980 }
987 981
988 /** 982 /**
989 * fwtty_port_carrier_raised: required tty_port operation 983 * fwtty_port_carrier_raised: required tty_port operation
990 * 984 *
991 * This port operation is polled after a tty has been opened and is waiting for 985 * This port operation is polled after a tty has been opened and is waiting for
992 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready(). 986 * carrier detect -- see drivers/tty/tty_port:tty_port_block_til_ready().
993 */ 987 */
994 static int fwtty_port_carrier_raised(struct tty_port *tty_port) 988 static int fwtty_port_carrier_raised(struct tty_port *tty_port)
995 { 989 {
996 struct fwtty_port *port = to_port(tty_port, port); 990 struct fwtty_port *port = to_port(tty_port, port);
997 int rc; 991 int rc;
998 992
999 rc = (port->mstatus & TIOCM_CAR); 993 rc = (port->mstatus & TIOCM_CAR);
1000 994
1001 fwtty_dbg(port, "%d", rc); 995 fwtty_dbg(port, "%d", rc);
1002 996
1003 return rc; 997 return rc;
1004 } 998 }
1005 999
1006 static unsigned set_termios(struct fwtty_port *port, struct tty_struct *tty) 1000 static unsigned set_termios(struct fwtty_port *port, struct tty_struct *tty)
1007 { 1001 {
1008 unsigned baud, frame; 1002 unsigned baud, frame;
1009 1003
1010 baud = tty_termios_baud_rate(&tty->termios); 1004 baud = tty_termios_baud_rate(&tty->termios);
1011 tty_termios_encode_baud_rate(&tty->termios, baud, baud); 1005 tty_termios_encode_baud_rate(&tty->termios, baud, baud);
1012 1006
1013 /* compute bit count of 2 frames */ 1007 /* compute bit count of 2 frames */
1014 frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0); 1008 frame = 12 + ((C_CSTOPB(tty)) ? 4 : 2) + ((C_PARENB(tty)) ? 2 : 0);
1015 1009
1016 switch (C_CSIZE(tty)) { 1010 switch (C_CSIZE(tty)) {
1017 case CS5: 1011 case CS5:
1018 frame -= (C_CSTOPB(tty)) ? 1 : 0; 1012 frame -= (C_CSTOPB(tty)) ? 1 : 0;
1019 break; 1013 break;
1020 case CS6: 1014 case CS6:
1021 frame += 2; 1015 frame += 2;
1022 break; 1016 break;
1023 case CS7: 1017 case CS7:
1024 frame += 4; 1018 frame += 4;
1025 break; 1019 break;
1026 case CS8: 1020 case CS8:
1027 frame += 6; 1021 frame += 6;
1028 break; 1022 break;
1029 } 1023 }
1030 1024
1031 port->cps = (baud << 1) / frame; 1025 port->cps = (baud << 1) / frame;
1032 1026
1033 port->status_mask = UART_LSR_OE; 1027 port->status_mask = UART_LSR_OE;
1034 if (_I_FLAG(tty, BRKINT | PARMRK)) 1028 if (_I_FLAG(tty, BRKINT | PARMRK))
1035 port->status_mask |= UART_LSR_BI; 1029 port->status_mask |= UART_LSR_BI;
1036 1030
1037 port->ignore_mask = 0; 1031 port->ignore_mask = 0;
1038 if (I_IGNBRK(tty)) { 1032 if (I_IGNBRK(tty)) {
1039 port->ignore_mask |= UART_LSR_BI; 1033 port->ignore_mask |= UART_LSR_BI;
1040 if (I_IGNPAR(tty)) 1034 if (I_IGNPAR(tty))
1041 port->ignore_mask |= UART_LSR_OE; 1035 port->ignore_mask |= UART_LSR_OE;
1042 } 1036 }
1043 1037
1044 port->write_only = !C_CREAD(tty); 1038 port->write_only = !C_CREAD(tty);
1045 1039
1046 /* turn off echo and newline xlat if loopback */ 1040 /* turn off echo and newline xlat if loopback */
1047 if (port->loopback) { 1041 if (port->loopback) {
1048 tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE | 1042 tty->termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHOKE |
1049 ECHONL | ECHOPRT | ECHOCTL); 1043 ECHONL | ECHOPRT | ECHOCTL);
1050 tty->termios.c_oflag &= ~ONLCR; 1044 tty->termios.c_oflag &= ~ONLCR;
1051 } 1045 }
1052 1046
1053 return baud; 1047 return baud;
1054 } 1048 }
1055 1049
1056 static int fwtty_port_activate(struct tty_port *tty_port, 1050 static int fwtty_port_activate(struct tty_port *tty_port,
1057 struct tty_struct *tty) 1051 struct tty_struct *tty)
1058 { 1052 {
1059 struct fwtty_port *port = to_port(tty_port, port); 1053 struct fwtty_port *port = to_port(tty_port, port);
1060 unsigned baud; 1054 unsigned baud;
1061 int err; 1055 int err;
1062 1056
1063 set_bit(TTY_IO_ERROR, &tty->flags); 1057 set_bit(TTY_IO_ERROR, &tty->flags);
1064 1058
1065 err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN, 1059 err = dma_fifo_alloc(&port->tx_fifo, FWTTY_PORT_TXFIFO_LEN,
1066 cache_line_size(), 1060 cache_line_size(),
1067 port->max_payload, 1061 port->max_payload,
1068 FWTTY_PORT_MAX_PEND_DMA, 1062 FWTTY_PORT_MAX_PEND_DMA,
1069 GFP_KERNEL); 1063 GFP_KERNEL);
1070 if (err) 1064 if (err)
1071 return err; 1065 return err;
1072 1066
1073 spin_lock_bh(&port->lock); 1067 spin_lock_bh(&port->lock);
1074 1068
1075 baud = set_termios(port, tty); 1069 baud = set_termios(port, tty);
1076 1070
1077 /* if console, don't change carrier state */ 1071 /* if console, don't change carrier state */
1078 if (!port->port.console) { 1072 if (!port->port.console) {
1079 port->mctrl = 0; 1073 port->mctrl = 0;
1080 if (baud != 0) 1074 if (baud != 0)
1081 port->mctrl = TIOCM_DTR | TIOCM_RTS; 1075 port->mctrl = TIOCM_DTR | TIOCM_RTS;
1082 } 1076 }
1083 1077
1084 if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) 1078 if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS)
1085 tty->hw_stopped = 1; 1079 tty->hw_stopped = 1;
1086 1080
1087 __fwtty_write_port_status(port); 1081 __fwtty_write_port_status(port);
1088 spin_unlock_bh(&port->lock); 1082 spin_unlock_bh(&port->lock);
1089 1083
1090 clear_bit(TTY_IO_ERROR, &tty->flags); 1084 clear_bit(TTY_IO_ERROR, &tty->flags);
1091 1085
1092 return 0; 1086 return 0;
1093 } 1087 }
1094 1088
1095 /** 1089 /**
1096 * fwtty_port_shutdown 1090 * fwtty_port_shutdown
1097 * 1091 *
1098 * Note: the tty port core ensures this is not the console and 1092 * Note: the tty port core ensures this is not the console and
1099 * manages TTY_IO_ERROR properly 1093 * manages TTY_IO_ERROR properly
1100 */ 1094 */
1101 static void fwtty_port_shutdown(struct tty_port *tty_port) 1095 static void fwtty_port_shutdown(struct tty_port *tty_port)
1102 { 1096 {
1103 struct fwtty_port *port = to_port(tty_port, port); 1097 struct fwtty_port *port = to_port(tty_port, port);
1104 struct buffered_rx *buf, *next; 1098 struct buffered_rx *buf, *next;
1105 1099
1106 /* TODO: cancel outstanding transactions */ 1100 /* TODO: cancel outstanding transactions */
1107 1101
1108 cancel_delayed_work_sync(&port->emit_breaks); 1102 cancel_delayed_work_sync(&port->emit_breaks);
1109 cancel_delayed_work_sync(&port->drain); 1103 cancel_delayed_work_sync(&port->drain);
1110 cancel_work_sync(&port->push); 1104 cancel_work_sync(&port->push);
1111 1105
1112 spin_lock_bh(&port->lock); 1106 spin_lock_bh(&port->lock);
1113 list_for_each_entry_safe(buf, next, &port->buf_list, list) { 1107 list_for_each_entry_safe(buf, next, &port->buf_list, list) {
1114 list_del(&buf->list); 1108 list_del(&buf->list);
1115 kfree(buf); 1109 kfree(buf);
1116 } 1110 }
1117 port->buffered = 0; 1111 port->buffered = 0;
1118 port->flags = 0; 1112 port->flags = 0;
1119 port->break_ctl = 0; 1113 port->break_ctl = 0;
1120 port->overrun = 0; 1114 port->overrun = 0;
1121 __fwtty_write_port_status(port); 1115 __fwtty_write_port_status(port);
1122 dma_fifo_free(&port->tx_fifo); 1116 dma_fifo_free(&port->tx_fifo);
1123 spin_unlock_bh(&port->lock); 1117 spin_unlock_bh(&port->lock);
1124 } 1118 }
1125 1119
1126 static int fwtty_open(struct tty_struct *tty, struct file *fp) 1120 static int fwtty_open(struct tty_struct *tty, struct file *fp)
1127 { 1121 {
1128 struct fwtty_port *port = tty->driver_data; 1122 struct fwtty_port *port = tty->driver_data;
1129 1123
1130 return tty_port_open(&port->port, tty, fp); 1124 return tty_port_open(&port->port, tty, fp);
1131 } 1125 }
1132 1126
1133 static void fwtty_close(struct tty_struct *tty, struct file *fp) 1127 static void fwtty_close(struct tty_struct *tty, struct file *fp)
1134 { 1128 {
1135 struct fwtty_port *port = tty->driver_data; 1129 struct fwtty_port *port = tty->driver_data;
1136 1130
1137 tty_port_close(&port->port, tty, fp); 1131 tty_port_close(&port->port, tty, fp);
1138 } 1132 }
1139 1133
1140 static void fwtty_hangup(struct tty_struct *tty) 1134 static void fwtty_hangup(struct tty_struct *tty)
1141 { 1135 {
1142 struct fwtty_port *port = tty->driver_data; 1136 struct fwtty_port *port = tty->driver_data;
1143 1137
1144 tty_port_hangup(&port->port); 1138 tty_port_hangup(&port->port);
1145 } 1139 }
1146 1140
1147 static void fwtty_cleanup(struct tty_struct *tty) 1141 static void fwtty_cleanup(struct tty_struct *tty)
1148 { 1142 {
1149 struct fwtty_port *port = tty->driver_data; 1143 struct fwtty_port *port = tty->driver_data;
1150 1144
1151 tty->driver_data = NULL; 1145 tty->driver_data = NULL;
1152 fwtty_port_put(port); 1146 fwtty_port_put(port);
1153 } 1147 }
1154 1148
1155 static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty) 1149 static int fwtty_install(struct tty_driver *driver, struct tty_struct *tty)
1156 { 1150 {
1157 struct fwtty_port *port = fwtty_port_get(tty->index); 1151 struct fwtty_port *port = fwtty_port_get(tty->index);
1158 int err; 1152 int err;
1159 1153
1160 err = tty_standard_install(driver, tty); 1154 err = tty_standard_install(driver, tty);
1161 if (!err) 1155 if (!err)
1162 tty->driver_data = port; 1156 tty->driver_data = port;
1163 else 1157 else
1164 fwtty_port_put(port); 1158 fwtty_port_put(port);
1165 return err; 1159 return err;
1166 } 1160 }
1167 1161
1168 static int fwloop_install(struct tty_driver *driver, struct tty_struct *tty) 1162 static int fwloop_install(struct tty_driver *driver, struct tty_struct *tty)
1169 { 1163 {
1170 struct fwtty_port *port = fwtty_port_get(table_idx(tty->index)); 1164 struct fwtty_port *port = fwtty_port_get(table_idx(tty->index));
1171 int err; 1165 int err;
1172 1166
1173 err = tty_standard_install(driver, tty); 1167 err = tty_standard_install(driver, tty);
1174 if (!err) 1168 if (!err)
1175 tty->driver_data = port; 1169 tty->driver_data = port;
1176 else 1170 else
1177 fwtty_port_put(port); 1171 fwtty_port_put(port);
1178 return err; 1172 return err;
1179 } 1173 }
1180 1174
1181 static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c) 1175 static int fwtty_write(struct tty_struct *tty, const unsigned char *buf, int c)
1182 { 1176 {
1183 struct fwtty_port *port = tty->driver_data; 1177 struct fwtty_port *port = tty->driver_data;
1184 int n, len; 1178 int n, len;
1185 1179
1186 fwtty_dbg(port, "%d", c); 1180 fwtty_dbg(port, "%d", c);
1187 profile_size_distrib(port->stats.writes, c); 1181 profile_size_distrib(port->stats.writes, c);
1188 1182
1189 spin_lock_bh(&port->lock); 1183 spin_lock_bh(&port->lock);
1190 n = dma_fifo_in(&port->tx_fifo, buf, c); 1184 n = dma_fifo_in(&port->tx_fifo, buf, c);
1191 len = dma_fifo_out_level(&port->tx_fifo); 1185 len = dma_fifo_out_level(&port->tx_fifo);
1192 if (len < DRAIN_THRESHOLD) 1186 if (len < DRAIN_THRESHOLD)
1193 schedule_delayed_work(&port->drain, 1); 1187 schedule_delayed_work(&port->drain, 1);
1194 spin_unlock_bh(&port->lock); 1188 spin_unlock_bh(&port->lock);
1195 1189
1196 if (len >= DRAIN_THRESHOLD) 1190 if (len >= DRAIN_THRESHOLD)
1197 fwtty_tx(port, false); 1191 fwtty_tx(port, false);
1198 1192
1199 debug_short_write(port, c, n); 1193 debug_short_write(port, c, n);
1200 1194
1201 return (n < 0) ? 0 : n; 1195 return (n < 0) ? 0 : n;
1202 } 1196 }
1203 1197
1204 static int fwtty_write_room(struct tty_struct *tty) 1198 static int fwtty_write_room(struct tty_struct *tty)
1205 { 1199 {
1206 struct fwtty_port *port = tty->driver_data; 1200 struct fwtty_port *port = tty->driver_data;
1207 int n; 1201 int n;
1208 1202
1209 spin_lock_bh(&port->lock); 1203 spin_lock_bh(&port->lock);
1210 n = dma_fifo_avail(&port->tx_fifo); 1204 n = dma_fifo_avail(&port->tx_fifo);
1211 spin_unlock_bh(&port->lock); 1205 spin_unlock_bh(&port->lock);
1212 1206
1213 fwtty_dbg(port, "%d", n); 1207 fwtty_dbg(port, "%d", n);
1214 1208
1215 return n; 1209 return n;
1216 } 1210 }
1217 1211
1218 static int fwtty_chars_in_buffer(struct tty_struct *tty) 1212 static int fwtty_chars_in_buffer(struct tty_struct *tty)
1219 { 1213 {
1220 struct fwtty_port *port = tty->driver_data; 1214 struct fwtty_port *port = tty->driver_data;
1221 int n; 1215 int n;
1222 1216
1223 spin_lock_bh(&port->lock); 1217 spin_lock_bh(&port->lock);
1224 n = dma_fifo_level(&port->tx_fifo); 1218 n = dma_fifo_level(&port->tx_fifo);
1225 spin_unlock_bh(&port->lock); 1219 spin_unlock_bh(&port->lock);
1226 1220
1227 fwtty_dbg(port, "%d", n); 1221 fwtty_dbg(port, "%d", n);
1228 1222
1229 return n; 1223 return n;
1230 } 1224 }
1231 1225
1232 static void fwtty_send_xchar(struct tty_struct *tty, char ch) 1226 static void fwtty_send_xchar(struct tty_struct *tty, char ch)
1233 { 1227 {
1234 struct fwtty_port *port = tty->driver_data; 1228 struct fwtty_port *port = tty->driver_data;
1235 1229
1236 fwtty_dbg(port, "%02x", ch); 1230 fwtty_dbg(port, "%02x", ch);
1237 1231
1238 fwtty_write_xchar(port, ch); 1232 fwtty_write_xchar(port, ch);
1239 } 1233 }
1240 1234
1241 static void fwtty_throttle(struct tty_struct *tty) 1235 static void fwtty_throttle(struct tty_struct *tty)
1242 { 1236 {
1243 struct fwtty_port *port = tty->driver_data; 1237 struct fwtty_port *port = tty->driver_data;
1244 1238
1245 /* 1239 /*
1246 * Ignore throttling (but not unthrottling). 1240 * Ignore throttling (but not unthrottling).
1247 * It only makes sense to throttle when data will no longer be 1241 * It only makes sense to throttle when data will no longer be
1248 * accepted by the tty flip buffer. For example, it is 1242 * accepted by the tty flip buffer. For example, it is
1249 * possible for received data to overflow the tty buffer long 1243 * possible for received data to overflow the tty buffer long
1250 * before the line discipline ever has a chance to throttle the driver. 1244 * before the line discipline ever has a chance to throttle the driver.
1251 * Additionally, the driver may have already completed the I/O 1245 * Additionally, the driver may have already completed the I/O
1252 * but the tty buffer is still emptying, so the line discipline is 1246 * but the tty buffer is still emptying, so the line discipline is
1253 * throttling and unthrottling nothing. 1247 * throttling and unthrottling nothing.
1254 */ 1248 */
1255 1249
1256 ++port->stats.throttled; 1250 ++port->stats.throttled;
1257 } 1251 }
1258 1252
1259 static void fwtty_unthrottle(struct tty_struct *tty) 1253 static void fwtty_unthrottle(struct tty_struct *tty)
1260 { 1254 {
1261 struct fwtty_port *port = tty->driver_data; 1255 struct fwtty_port *port = tty->driver_data;
1262 1256
1263 fwtty_dbg(port, "CRTSCTS: %d", (C_CRTSCTS(tty) != 0)); 1257 fwtty_dbg(port, "CRTSCTS: %d", (C_CRTSCTS(tty) != 0));
1264 1258
1265 profile_fifo_avail(port, port->stats.unthrottle); 1259 profile_fifo_avail(port, port->stats.unthrottle);
1266 1260
1267 schedule_work(&port->push); 1261 schedule_work(&port->push);
1268 1262
1269 spin_lock_bh(&port->lock); 1263 spin_lock_bh(&port->lock);
1270 port->mctrl &= ~OOB_RX_THROTTLE; 1264 port->mctrl &= ~OOB_RX_THROTTLE;
1271 if (C_CRTSCTS(tty)) 1265 if (C_CRTSCTS(tty))
1272 port->mctrl |= TIOCM_RTS; 1266 port->mctrl |= TIOCM_RTS;
1273 __fwtty_write_port_status(port); 1267 __fwtty_write_port_status(port);
1274 spin_unlock_bh(&port->lock); 1268 spin_unlock_bh(&port->lock);
1275 } 1269 }
1276 1270
1277 static int check_msr_delta(struct fwtty_port *port, unsigned long mask, 1271 static int check_msr_delta(struct fwtty_port *port, unsigned long mask,
1278 struct async_icount *prev) 1272 struct async_icount *prev)
1279 { 1273 {
1280 struct async_icount now; 1274 struct async_icount now;
1281 int delta; 1275 int delta;
1282 1276
1283 now = port->icount; 1277 now = port->icount;
1284 1278
1285 delta = ((mask & TIOCM_RNG && prev->rng != now.rng) || 1279 delta = ((mask & TIOCM_RNG && prev->rng != now.rng) ||
1286 (mask & TIOCM_DSR && prev->dsr != now.dsr) || 1280 (mask & TIOCM_DSR && prev->dsr != now.dsr) ||
1287 (mask & TIOCM_CAR && prev->dcd != now.dcd) || 1281 (mask & TIOCM_CAR && prev->dcd != now.dcd) ||
1288 (mask & TIOCM_CTS && prev->cts != now.cts)); 1282 (mask & TIOCM_CTS && prev->cts != now.cts));
1289 1283
1290 *prev = now; 1284 *prev = now;
1291 1285
1292 return delta; 1286 return delta;
1293 } 1287 }
1294 1288
1295 static int wait_msr_change(struct fwtty_port *port, unsigned long mask) 1289 static int wait_msr_change(struct fwtty_port *port, unsigned long mask)
1296 { 1290 {
1297 struct async_icount prev; 1291 struct async_icount prev;
1298 1292
1299 prev = port->icount; 1293 prev = port->icount;
1300 1294
1301 return wait_event_interruptible(port->port.delta_msr_wait, 1295 return wait_event_interruptible(port->port.delta_msr_wait,
1302 check_msr_delta(port, mask, &prev)); 1296 check_msr_delta(port, mask, &prev));
1303 } 1297 }
1304 1298
1305 static int get_serial_info(struct fwtty_port *port, 1299 static int get_serial_info(struct fwtty_port *port,
1306 struct serial_struct __user *info) 1300 struct serial_struct __user *info)
1307 { 1301 {
1308 struct serial_struct tmp; 1302 struct serial_struct tmp;
1309 1303
1310 memset(&tmp, 0, sizeof(tmp)); 1304 memset(&tmp, 0, sizeof(tmp));
1311 1305
1312 tmp.type = PORT_UNKNOWN; 1306 tmp.type = PORT_UNKNOWN;
1313 tmp.line = port->port.tty->index; 1307 tmp.line = port->port.tty->index;
1314 tmp.flags = port->port.flags; 1308 tmp.flags = port->port.flags;
1315 tmp.xmit_fifo_size = FWTTY_PORT_TXFIFO_LEN; 1309 tmp.xmit_fifo_size = FWTTY_PORT_TXFIFO_LEN;
1316 tmp.baud_base = 400000000; 1310 tmp.baud_base = 400000000;
1317 tmp.close_delay = port->port.close_delay; 1311 tmp.close_delay = port->port.close_delay;
1318 1312
1319 return (copy_to_user(info, &tmp, sizeof(*info))) ? -EFAULT : 0; 1313 return (copy_to_user(info, &tmp, sizeof(*info))) ? -EFAULT : 0;
1320 } 1314 }
1321 1315
1322 static int set_serial_info(struct fwtty_port *port, 1316 static int set_serial_info(struct fwtty_port *port,
1323 struct serial_struct __user *info) 1317 struct serial_struct __user *info)
1324 { 1318 {
1325 struct serial_struct tmp; 1319 struct serial_struct tmp;
1326 1320
1327 if (copy_from_user(&tmp, info, sizeof(tmp))) 1321 if (copy_from_user(&tmp, info, sizeof(tmp)))
1328 return -EFAULT; 1322 return -EFAULT;
1329 1323
1330 if (tmp.irq != 0 || tmp.port != 0 || tmp.custom_divisor != 0 || 1324 if (tmp.irq != 0 || tmp.port != 0 || tmp.custom_divisor != 0 ||
1331 tmp.baud_base != 400000000) 1325 tmp.baud_base != 400000000)
1332 return -EPERM; 1326 return -EPERM;
1333 1327
1334 if (!capable(CAP_SYS_ADMIN)) { 1328 if (!capable(CAP_SYS_ADMIN)) {
1335 if (((tmp.flags & ~ASYNC_USR_MASK) != 1329 if (((tmp.flags & ~ASYNC_USR_MASK) !=
1336 (port->port.flags & ~ASYNC_USR_MASK))) 1330 (port->port.flags & ~ASYNC_USR_MASK)))
1337 return -EPERM; 1331 return -EPERM;
1338 } else 1332 } else
1339 port->port.close_delay = tmp.close_delay * HZ / 100; 1333 port->port.close_delay = tmp.close_delay * HZ / 100;
1340 1334
1341 return 0; 1335 return 0;
1342 } 1336 }
1343 1337
1344 static int fwtty_ioctl(struct tty_struct *tty, unsigned cmd, 1338 static int fwtty_ioctl(struct tty_struct *tty, unsigned cmd,
1345 unsigned long arg) 1339 unsigned long arg)
1346 { 1340 {
1347 struct fwtty_port *port = tty->driver_data; 1341 struct fwtty_port *port = tty->driver_data;
1348 int err; 1342 int err;
1349 1343
1350 switch (cmd) { 1344 switch (cmd) {
1351 case TIOCGSERIAL: 1345 case TIOCGSERIAL:
1352 mutex_lock(&port->port.mutex); 1346 mutex_lock(&port->port.mutex);
1353 err = get_serial_info(port, (void __user *)arg); 1347 err = get_serial_info(port, (void __user *)arg);
1354 mutex_unlock(&port->port.mutex); 1348 mutex_unlock(&port->port.mutex);
1355 break; 1349 break;
1356 1350
1357 case TIOCSSERIAL: 1351 case TIOCSSERIAL:
1358 mutex_lock(&port->port.mutex); 1352 mutex_lock(&port->port.mutex);
1359 err = set_serial_info(port, (void __user *)arg); 1353 err = set_serial_info(port, (void __user *)arg);
1360 mutex_unlock(&port->port.mutex); 1354 mutex_unlock(&port->port.mutex);
1361 break; 1355 break;
1362 1356
1363 case TIOCMIWAIT: 1357 case TIOCMIWAIT:
1364 err = wait_msr_change(port, arg); 1358 err = wait_msr_change(port, arg);
1365 break; 1359 break;
1366 1360
1367 default: 1361 default:
1368 err = -ENOIOCTLCMD; 1362 err = -ENOIOCTLCMD;
1369 } 1363 }
1370 1364
1371 return err; 1365 return err;
1372 } 1366 }
1373 1367
1374 static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old) 1368 static void fwtty_set_termios(struct tty_struct *tty, struct ktermios *old)
1375 { 1369 {
1376 struct fwtty_port *port = tty->driver_data; 1370 struct fwtty_port *port = tty->driver_data;
1377 unsigned baud; 1371 unsigned baud;
1378 1372
1379 spin_lock_bh(&port->lock); 1373 spin_lock_bh(&port->lock);
1380 baud = set_termios(port, tty); 1374 baud = set_termios(port, tty);
1381 1375
1382 if ((baud == 0) && (old->c_cflag & CBAUD)) 1376 if ((baud == 0) && (old->c_cflag & CBAUD))
1383 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS); 1377 port->mctrl &= ~(TIOCM_DTR | TIOCM_RTS);
1384 else if ((baud != 0) && !(old->c_cflag & CBAUD)) { 1378 else if ((baud != 0) && !(old->c_cflag & CBAUD)) {
1385 if (C_CRTSCTS(tty) || !test_bit(TTY_THROTTLED, &tty->flags)) 1379 if (C_CRTSCTS(tty) || !test_bit(TTY_THROTTLED, &tty->flags))
1386 port->mctrl |= TIOCM_DTR | TIOCM_RTS; 1380 port->mctrl |= TIOCM_DTR | TIOCM_RTS;
1387 else 1381 else
1388 port->mctrl |= TIOCM_DTR; 1382 port->mctrl |= TIOCM_DTR;
1389 } 1383 }
1390 __fwtty_write_port_status(port); 1384 __fwtty_write_port_status(port);
1391 spin_unlock_bh(&port->lock); 1385 spin_unlock_bh(&port->lock);
1392 1386
1393 if (old->c_cflag & CRTSCTS) { 1387 if (old->c_cflag & CRTSCTS) {
1394 if (!C_CRTSCTS(tty)) { 1388 if (!C_CRTSCTS(tty)) {
1395 tty->hw_stopped = 0; 1389 tty->hw_stopped = 0;
1396 fwtty_restart_tx(port); 1390 fwtty_restart_tx(port);
1397 } 1391 }
1398 } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) { 1392 } else if (C_CRTSCTS(tty) && ~port->mstatus & TIOCM_CTS) {
1399 tty->hw_stopped = 1; 1393 tty->hw_stopped = 1;
1400 } 1394 }
1401 } 1395 }
1402 1396
1403 /** 1397 /**
1404 * fwtty_break_ctl - start/stop sending breaks 1398 * fwtty_break_ctl - start/stop sending breaks
1405 * 1399 *
1406 * Signals the remote to start or stop generating simulated breaks. 1400 * Signals the remote to start or stop generating simulated breaks.
1407 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx 1401 * First, stop dequeueing from the fifo and wait for writer/drain to leave tx
1408 * before signalling the break line status. This guarantees any pending rx will 1402 * before signalling the break line status. This guarantees any pending rx will
1409 * be queued to the line discipline before break is simulated on the remote. 1403 * be queued to the line discipline before break is simulated on the remote.
1410 * Conversely, turning off break_ctl requires signalling the line status change, 1404 * Conversely, turning off break_ctl requires signalling the line status change,
1411 * then enabling tx. 1405 * then enabling tx.
1412 */ 1406 */
1413 static int fwtty_break_ctl(struct tty_struct *tty, int state) 1407 static int fwtty_break_ctl(struct tty_struct *tty, int state)
1414 { 1408 {
1415 struct fwtty_port *port = tty->driver_data; 1409 struct fwtty_port *port = tty->driver_data;
1416 long ret; 1410 long ret;
1417 1411
1418 fwtty_dbg(port, "%d", state); 1412 fwtty_dbg(port, "%d", state);
1419 1413
1420 if (state == -1) { 1414 if (state == -1) {
1421 set_bit(STOP_TX, &port->flags); 1415 set_bit(STOP_TX, &port->flags);
1422 ret = wait_event_interruptible_timeout(port->wait_tx, 1416 ret = wait_event_interruptible_timeout(port->wait_tx,
1423 !test_bit(IN_TX, &port->flags), 1417 !test_bit(IN_TX, &port->flags),
1424 10); 1418 10);
1425 if (ret == 0 || ret == -ERESTARTSYS) { 1419 if (ret == 0 || ret == -ERESTARTSYS) {
1426 clear_bit(STOP_TX, &port->flags); 1420 clear_bit(STOP_TX, &port->flags);
1427 fwtty_restart_tx(port); 1421 fwtty_restart_tx(port);
1428 return -EINTR; 1422 return -EINTR;
1429 } 1423 }
1430 } 1424 }
1431 1425
1432 spin_lock_bh(&port->lock); 1426 spin_lock_bh(&port->lock);
1433 port->break_ctl = (state == -1); 1427 port->break_ctl = (state == -1);
1434 __fwtty_write_port_status(port); 1428 __fwtty_write_port_status(port);
1435 spin_unlock_bh(&port->lock); 1429 spin_unlock_bh(&port->lock);
1436 1430
1437 if (state == 0) { 1431 if (state == 0) {
1438 spin_lock_bh(&port->lock); 1432 spin_lock_bh(&port->lock);
1439 dma_fifo_reset(&port->tx_fifo); 1433 dma_fifo_reset(&port->tx_fifo);
1440 clear_bit(STOP_TX, &port->flags); 1434 clear_bit(STOP_TX, &port->flags);
1441 spin_unlock_bh(&port->lock); 1435 spin_unlock_bh(&port->lock);
1442 } 1436 }
1443 return 0; 1437 return 0;
1444 } 1438 }
1445 1439
1446 static int fwtty_tiocmget(struct tty_struct *tty) 1440 static int fwtty_tiocmget(struct tty_struct *tty)
1447 { 1441 {
1448 struct fwtty_port *port = tty->driver_data; 1442 struct fwtty_port *port = tty->driver_data;
1449 unsigned tiocm; 1443 unsigned tiocm;
1450 1444
1451 spin_lock_bh(&port->lock); 1445 spin_lock_bh(&port->lock);
1452 tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK); 1446 tiocm = (port->mctrl & MCTRL_MASK) | (port->mstatus & ~MCTRL_MASK);
1453 spin_unlock_bh(&port->lock); 1447 spin_unlock_bh(&port->lock);
1454 1448
1455 fwtty_dbg(port, "%x", tiocm); 1449 fwtty_dbg(port, "%x", tiocm);
1456 1450
1457 return tiocm; 1451 return tiocm;
1458 } 1452 }
1459 1453
1460 static int fwtty_tiocmset(struct tty_struct *tty, unsigned set, unsigned clear) 1454 static int fwtty_tiocmset(struct tty_struct *tty, unsigned set, unsigned clear)
1461 { 1455 {
1462 struct fwtty_port *port = tty->driver_data; 1456 struct fwtty_port *port = tty->driver_data;
1463 1457
1464 fwtty_dbg(port, "set: %x clear: %x", set, clear); 1458 fwtty_dbg(port, "set: %x clear: %x", set, clear);
1465 1459
1466 /* TODO: simulate loopback if TIOCM_LOOP set */ 1460 /* TODO: simulate loopback if TIOCM_LOOP set */
1467 1461
1468 spin_lock_bh(&port->lock); 1462 spin_lock_bh(&port->lock);
1469 port->mctrl &= ~(clear & MCTRL_MASK & 0xffff); 1463 port->mctrl &= ~(clear & MCTRL_MASK & 0xffff);
1470 port->mctrl |= set & MCTRL_MASK & 0xffff; 1464 port->mctrl |= set & MCTRL_MASK & 0xffff;
1471 __fwtty_write_port_status(port); 1465 __fwtty_write_port_status(port);
1472 spin_unlock_bh(&port->lock); 1466 spin_unlock_bh(&port->lock);
1473 return 0; 1467 return 0;
1474 } 1468 }
1475 1469
1476 static int fwtty_get_icount(struct tty_struct *tty, 1470 static int fwtty_get_icount(struct tty_struct *tty,
1477 struct serial_icounter_struct *icount) 1471 struct serial_icounter_struct *icount)
1478 { 1472 {
1479 struct fwtty_port *port = tty->driver_data; 1473 struct fwtty_port *port = tty->driver_data;
1480 struct stats stats; 1474 struct stats stats;
1481 1475
1482 memcpy(&stats, &port->stats, sizeof(stats)); 1476 memcpy(&stats, &port->stats, sizeof(stats));
1483 if (port->port.console) 1477 if (port->port.console)
1484 (*port->fwcon_ops->stats)(&stats, port->con_data); 1478 (*port->fwcon_ops->stats)(&stats, port->con_data);
1485 1479
1486 icount->cts = port->icount.cts; 1480 icount->cts = port->icount.cts;
1487 icount->dsr = port->icount.dsr; 1481 icount->dsr = port->icount.dsr;
1488 icount->rng = port->icount.rng; 1482 icount->rng = port->icount.rng;
1489 icount->dcd = port->icount.dcd; 1483 icount->dcd = port->icount.dcd;
1490 icount->rx = port->icount.rx; 1484 icount->rx = port->icount.rx;
1491 icount->tx = port->icount.tx + stats.xchars; 1485 icount->tx = port->icount.tx + stats.xchars;
1492 icount->frame = port->icount.frame; 1486 icount->frame = port->icount.frame;
1493 icount->overrun = port->icount.overrun; 1487 icount->overrun = port->icount.overrun;
1494 icount->parity = port->icount.parity; 1488 icount->parity = port->icount.parity;
1495 icount->brk = port->icount.brk; 1489 icount->brk = port->icount.brk;
1496 icount->buf_overrun = port->icount.overrun; 1490 icount->buf_overrun = port->icount.overrun;
1497 return 0; 1491 return 0;
1498 } 1492 }
1499 1493
1500 static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port) 1494 static void fwtty_proc_show_port(struct seq_file *m, struct fwtty_port *port)
1501 { 1495 {
1502 struct stats stats; 1496 struct stats stats;
1503 1497
1504 memcpy(&stats, &port->stats, sizeof(stats)); 1498 memcpy(&stats, &port->stats, sizeof(stats));
1505 if (port->port.console) 1499 if (port->port.console)
1506 (*port->fwcon_ops->stats)(&stats, port->con_data); 1500 (*port->fwcon_ops->stats)(&stats, port->con_data);
1507 1501
1508 seq_printf(m, " addr:%012llx tx:%d rx:%d", port->rx_handler.offset, 1502 seq_printf(m, " addr:%012llx tx:%d rx:%d", port->rx_handler.offset,
1509 port->icount.tx + stats.xchars, port->icount.rx); 1503 port->icount.tx + stats.xchars, port->icount.rx);
1510 seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts, 1504 seq_printf(m, " cts:%d dsr:%d rng:%d dcd:%d", port->icount.cts,
1511 port->icount.dsr, port->icount.rng, port->icount.dcd); 1505 port->icount.dsr, port->icount.rng, port->icount.dcd);
1512 seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame, 1506 seq_printf(m, " fe:%d oe:%d pe:%d brk:%d", port->icount.frame,
1513 port->icount.overrun, port->icount.parity, port->icount.brk); 1507 port->icount.overrun, port->icount.parity, port->icount.brk);
1514 } 1508 }
1515 1509
1516 static void fwtty_debugfs_show_port(struct seq_file *m, struct fwtty_port *port) 1510 static void fwtty_debugfs_show_port(struct seq_file *m, struct fwtty_port *port)
1517 { 1511 {
1518 struct stats stats; 1512 struct stats stats;
1519 1513
1520 memcpy(&stats, &port->stats, sizeof(stats)); 1514 memcpy(&stats, &port->stats, sizeof(stats));
1521 if (port->port.console) 1515 if (port->port.console)
1522 (*port->fwcon_ops->stats)(&stats, port->con_data); 1516 (*port->fwcon_ops->stats)(&stats, port->con_data);
1523 1517
1524 seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped, 1518 seq_printf(m, " dr:%d st:%d err:%d lost:%d", stats.dropped,
1525 stats.tx_stall, stats.fifo_errs, stats.lost); 1519 stats.tx_stall, stats.fifo_errs, stats.lost);
1526 seq_printf(m, " pkts:%d thr:%d wtrmk:%d", stats.sent, stats.throttled, 1520 seq_printf(m, " pkts:%d thr:%d wtrmk:%d", stats.sent, stats.throttled,
1527 stats.watermark); 1521 stats.watermark);
1528 1522
1529 if (port->port.console) { 1523 if (port->port.console) {
1530 seq_printf(m, "\n "); 1524 seq_printf(m, "\n ");
1531 (*port->fwcon_ops->proc_show)(m, port->con_data); 1525 (*port->fwcon_ops->proc_show)(m, port->con_data);
1532 } 1526 }
1533 1527
1534 dump_profile(m, &port->stats); 1528 dump_profile(m, &port->stats);
1535 } 1529 }
1536 1530
1537 static void fwtty_debugfs_show_peer(struct seq_file *m, struct fwtty_peer *peer) 1531 static void fwtty_debugfs_show_peer(struct seq_file *m, struct fwtty_peer *peer)
1538 { 1532 {
1539 int generation = peer->generation; 1533 int generation = peer->generation;
1540 1534
1541 smp_rmb(); 1535 smp_rmb();
1542 seq_printf(m, " %s:", dev_name(&peer->unit->device)); 1536 seq_printf(m, " %s:", dev_name(&peer->unit->device));
1543 seq_printf(m, " node:%04x gen:%d", peer->node_id, generation); 1537 seq_printf(m, " node:%04x gen:%d", peer->node_id, generation);
1544 seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed, 1538 seq_printf(m, " sp:%d max:%d guid:%016llx", peer->speed,
1545 peer->max_payload, (unsigned long long) peer->guid); 1539 peer->max_payload, (unsigned long long) peer->guid);
1546 seq_printf(m, " mgmt:%012llx", (unsigned long long) peer->mgmt_addr); 1540 seq_printf(m, " mgmt:%012llx", (unsigned long long) peer->mgmt_addr);
1547 seq_printf(m, " addr:%012llx", (unsigned long long) peer->status_addr); 1541 seq_printf(m, " addr:%012llx", (unsigned long long) peer->status_addr);
1548 seq_putc(m, '\n'); 1542 seq_putc(m, '\n');
1549 } 1543 }
1550 1544
1551 static int fwtty_proc_show(struct seq_file *m, void *v) 1545 static int fwtty_proc_show(struct seq_file *m, void *v)
1552 { 1546 {
1553 struct fwtty_port *port; 1547 struct fwtty_port *port;
1554 int i; 1548 int i;
1555 1549
1556 seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n"); 1550 seq_puts(m, "fwserinfo: 1.0 driver: 1.0\n");
1557 for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) { 1551 for (i = 0; i < MAX_TOTAL_PORTS && (port = fwtty_port_get(i)); ++i) {
1558 seq_printf(m, "%2d:", i); 1552 seq_printf(m, "%2d:", i);
1559 if (capable(CAP_SYS_ADMIN)) 1553 if (capable(CAP_SYS_ADMIN))
1560 fwtty_proc_show_port(m, port); 1554 fwtty_proc_show_port(m, port);
1561 fwtty_port_put(port); 1555 fwtty_port_put(port);
1562 seq_printf(m, "\n"); 1556 seq_printf(m, "\n");
1563 } 1557 }
1564 return 0; 1558 return 0;
1565 } 1559 }
1566 1560
1567 static int fwtty_debugfs_stats_show(struct seq_file *m, void *v) 1561 static int fwtty_debugfs_stats_show(struct seq_file *m, void *v)
1568 { 1562 {
1569 struct fw_serial *serial = m->private; 1563 struct fw_serial *serial = m->private;
1570 struct fwtty_port *port; 1564 struct fwtty_port *port;
1571 int i; 1565 int i;
1572 1566
1573 for (i = 0; i < num_ports; ++i) { 1567 for (i = 0; i < num_ports; ++i) {
1574 port = fwtty_port_get(serial->ports[i]->index); 1568 port = fwtty_port_get(serial->ports[i]->index);
1575 if (port) { 1569 if (port) {
1576 seq_printf(m, "%2d:", port->index); 1570 seq_printf(m, "%2d:", port->index);
1577 fwtty_proc_show_port(m, port); 1571 fwtty_proc_show_port(m, port);
1578 fwtty_debugfs_show_port(m, port); 1572 fwtty_debugfs_show_port(m, port);
1579 fwtty_port_put(port); 1573 fwtty_port_put(port);
1580 seq_printf(m, "\n"); 1574 seq_printf(m, "\n");
1581 } 1575 }
1582 } 1576 }
1583 return 0; 1577 return 0;
1584 } 1578 }
1585 1579
1586 static int fwtty_debugfs_peers_show(struct seq_file *m, void *v) 1580 static int fwtty_debugfs_peers_show(struct seq_file *m, void *v)
1587 { 1581 {
1588 struct fw_serial *serial = m->private; 1582 struct fw_serial *serial = m->private;
1589 struct fwtty_peer *peer; 1583 struct fwtty_peer *peer;
1590 1584
1591 rcu_read_lock(); 1585 rcu_read_lock();
1592 seq_printf(m, "card: %s guid: %016llx\n", 1586 seq_printf(m, "card: %s guid: %016llx\n",
1593 dev_name(serial->card->device), 1587 dev_name(serial->card->device),
1594 (unsigned long long) serial->card->guid); 1588 (unsigned long long) serial->card->guid);
1595 list_for_each_entry_rcu(peer, &serial->peer_list, list) 1589 list_for_each_entry_rcu(peer, &serial->peer_list, list)
1596 fwtty_debugfs_show_peer(m, peer); 1590 fwtty_debugfs_show_peer(m, peer);
1597 rcu_read_unlock(); 1591 rcu_read_unlock();
1598 return 0; 1592 return 0;
1599 } 1593 }
1600 1594
1601 static int fwtty_proc_open(struct inode *inode, struct file *fp) 1595 static int fwtty_proc_open(struct inode *inode, struct file *fp)
1602 { 1596 {
1603 return single_open(fp, fwtty_proc_show, NULL); 1597 return single_open(fp, fwtty_proc_show, NULL);
1604 } 1598 }
1605 1599
1606 static int fwtty_stats_open(struct inode *inode, struct file *fp) 1600 static int fwtty_stats_open(struct inode *inode, struct file *fp)
1607 { 1601 {
1608 return single_open(fp, fwtty_debugfs_stats_show, inode->i_private); 1602 return single_open(fp, fwtty_debugfs_stats_show, inode->i_private);
1609 } 1603 }
1610 1604
1611 static int fwtty_peers_open(struct inode *inode, struct file *fp) 1605 static int fwtty_peers_open(struct inode *inode, struct file *fp)
1612 { 1606 {
1613 return single_open(fp, fwtty_debugfs_peers_show, inode->i_private); 1607 return single_open(fp, fwtty_debugfs_peers_show, inode->i_private);
1614 } 1608 }
1615 1609
1616 static const struct file_operations fwtty_stats_fops = { 1610 static const struct file_operations fwtty_stats_fops = {
1617 .owner = THIS_MODULE, 1611 .owner = THIS_MODULE,
1618 .open = fwtty_stats_open, 1612 .open = fwtty_stats_open,
1619 .read = seq_read, 1613 .read = seq_read,
1620 .llseek = seq_lseek, 1614 .llseek = seq_lseek,
1621 .release = single_release, 1615 .release = single_release,
1622 }; 1616 };
1623 1617
1624 static const struct file_operations fwtty_peers_fops = { 1618 static const struct file_operations fwtty_peers_fops = {
1625 .owner = THIS_MODULE, 1619 .owner = THIS_MODULE,
1626 .open = fwtty_peers_open, 1620 .open = fwtty_peers_open,
1627 .read = seq_read, 1621 .read = seq_read,
1628 .llseek = seq_lseek, 1622 .llseek = seq_lseek,
1629 .release = single_release, 1623 .release = single_release,
1630 }; 1624 };
1631 1625
1632 static const struct file_operations fwtty_proc_fops = { 1626 static const struct file_operations fwtty_proc_fops = {
1633 .owner = THIS_MODULE, 1627 .owner = THIS_MODULE,
1634 .open = fwtty_proc_open, 1628 .open = fwtty_proc_open,
1635 .read = seq_read, 1629 .read = seq_read,
1636 .llseek = seq_lseek, 1630 .llseek = seq_lseek,
1637 .release = single_release, 1631 .release = single_release,
1638 }; 1632 };
1639 1633
1640 static const struct tty_port_operations fwtty_port_ops = { 1634 static const struct tty_port_operations fwtty_port_ops = {
1641 .dtr_rts = fwtty_port_dtr_rts, 1635 .dtr_rts = fwtty_port_dtr_rts,
1642 .carrier_raised = fwtty_port_carrier_raised, 1636 .carrier_raised = fwtty_port_carrier_raised,
1643 .shutdown = fwtty_port_shutdown, 1637 .shutdown = fwtty_port_shutdown,
1644 .activate = fwtty_port_activate, 1638 .activate = fwtty_port_activate,
1645 }; 1639 };
1646 1640
1647 static const struct tty_operations fwtty_ops = { 1641 static const struct tty_operations fwtty_ops = {
1648 .open = fwtty_open, 1642 .open = fwtty_open,
1649 .close = fwtty_close, 1643 .close = fwtty_close,
1650 .hangup = fwtty_hangup, 1644 .hangup = fwtty_hangup,
1651 .cleanup = fwtty_cleanup, 1645 .cleanup = fwtty_cleanup,
1652 .install = fwtty_install, 1646 .install = fwtty_install,
1653 .write = fwtty_write, 1647 .write = fwtty_write,
1654 .write_room = fwtty_write_room, 1648 .write_room = fwtty_write_room,
1655 .chars_in_buffer = fwtty_chars_in_buffer, 1649 .chars_in_buffer = fwtty_chars_in_buffer,
1656 .send_xchar = fwtty_send_xchar, 1650 .send_xchar = fwtty_send_xchar,
1657 .throttle = fwtty_throttle, 1651 .throttle = fwtty_throttle,
1658 .unthrottle = fwtty_unthrottle, 1652 .unthrottle = fwtty_unthrottle,
1659 .ioctl = fwtty_ioctl, 1653 .ioctl = fwtty_ioctl,
1660 .set_termios = fwtty_set_termios, 1654 .set_termios = fwtty_set_termios,
1661 .break_ctl = fwtty_break_ctl, 1655 .break_ctl = fwtty_break_ctl,
1662 .tiocmget = fwtty_tiocmget, 1656 .tiocmget = fwtty_tiocmget,
1663 .tiocmset = fwtty_tiocmset, 1657 .tiocmset = fwtty_tiocmset,
1664 .get_icount = fwtty_get_icount, 1658 .get_icount = fwtty_get_icount,
1665 .proc_fops = &fwtty_proc_fops, 1659 .proc_fops = &fwtty_proc_fops,
1666 }; 1660 };
1667 1661
1668 static const struct tty_operations fwloop_ops = { 1662 static const struct tty_operations fwloop_ops = {
1669 .open = fwtty_open, 1663 .open = fwtty_open,
1670 .close = fwtty_close, 1664 .close = fwtty_close,
1671 .hangup = fwtty_hangup, 1665 .hangup = fwtty_hangup,
1672 .cleanup = fwtty_cleanup, 1666 .cleanup = fwtty_cleanup,
1673 .install = fwloop_install, 1667 .install = fwloop_install,
1674 .write = fwtty_write, 1668 .write = fwtty_write,
1675 .write_room = fwtty_write_room, 1669 .write_room = fwtty_write_room,
1676 .chars_in_buffer = fwtty_chars_in_buffer, 1670 .chars_in_buffer = fwtty_chars_in_buffer,
1677 .send_xchar = fwtty_send_xchar, 1671 .send_xchar = fwtty_send_xchar,
1678 .throttle = fwtty_throttle, 1672 .throttle = fwtty_throttle,
1679 .unthrottle = fwtty_unthrottle, 1673 .unthrottle = fwtty_unthrottle,
1680 .ioctl = fwtty_ioctl, 1674 .ioctl = fwtty_ioctl,
1681 .set_termios = fwtty_set_termios, 1675 .set_termios = fwtty_set_termios,
1682 .break_ctl = fwtty_break_ctl, 1676 .break_ctl = fwtty_break_ctl,
1683 .tiocmget = fwtty_tiocmget, 1677 .tiocmget = fwtty_tiocmget,
1684 .tiocmset = fwtty_tiocmset, 1678 .tiocmset = fwtty_tiocmset,
1685 .get_icount = fwtty_get_icount, 1679 .get_icount = fwtty_get_icount,
1686 }; 1680 };
1687 1681
1688 static inline int mgmt_pkt_expected_len(__be16 code) 1682 static inline int mgmt_pkt_expected_len(__be16 code)
1689 { 1683 {
1690 static const struct fwserial_mgmt_pkt pkt; 1684 static const struct fwserial_mgmt_pkt pkt;
1691 1685
1692 switch (be16_to_cpu(code)) { 1686 switch (be16_to_cpu(code)) {
1693 case FWSC_VIRT_CABLE_PLUG: 1687 case FWSC_VIRT_CABLE_PLUG:
1694 return sizeof(pkt.hdr) + sizeof(pkt.plug_req); 1688 return sizeof(pkt.hdr) + sizeof(pkt.plug_req);
1695 1689
1696 case FWSC_VIRT_CABLE_PLUG_RSP: /* | FWSC_RSP_OK */ 1690 case FWSC_VIRT_CABLE_PLUG_RSP: /* | FWSC_RSP_OK */
1697 return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp); 1691 return sizeof(pkt.hdr) + sizeof(pkt.plug_rsp);
1698 1692
1699 1693
1700 case FWSC_VIRT_CABLE_UNPLUG: 1694 case FWSC_VIRT_CABLE_UNPLUG:
1701 case FWSC_VIRT_CABLE_UNPLUG_RSP: 1695 case FWSC_VIRT_CABLE_UNPLUG_RSP:
1702 case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK: 1696 case FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK:
1703 case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK: 1697 case FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK:
1704 return sizeof(pkt.hdr); 1698 return sizeof(pkt.hdr);
1705 1699
1706 default: 1700 default:
1707 return -1; 1701 return -1;
1708 } 1702 }
1709 } 1703 }
1710 1704
1711 static inline void fill_plug_params(struct virt_plug_params *params, 1705 static inline void fill_plug_params(struct virt_plug_params *params,
1712 struct fwtty_port *port) 1706 struct fwtty_port *port)
1713 { 1707 {
1714 u64 status_addr = port->rx_handler.offset; 1708 u64 status_addr = port->rx_handler.offset;
1715 u64 fifo_addr = port->rx_handler.offset + 4; 1709 u64 fifo_addr = port->rx_handler.offset + 4;
1716 size_t fifo_len = port->rx_handler.length - 4; 1710 size_t fifo_len = port->rx_handler.length - 4;
1717 1711
1718 params->status_hi = cpu_to_be32(status_addr >> 32); 1712 params->status_hi = cpu_to_be32(status_addr >> 32);
1719 params->status_lo = cpu_to_be32(status_addr); 1713 params->status_lo = cpu_to_be32(status_addr);
1720 params->fifo_hi = cpu_to_be32(fifo_addr >> 32); 1714 params->fifo_hi = cpu_to_be32(fifo_addr >> 32);
1721 params->fifo_lo = cpu_to_be32(fifo_addr); 1715 params->fifo_lo = cpu_to_be32(fifo_addr);
1722 params->fifo_len = cpu_to_be32(fifo_len); 1716 params->fifo_len = cpu_to_be32(fifo_len);
1723 } 1717 }
1724 1718
1725 static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt, 1719 static inline void fill_plug_req(struct fwserial_mgmt_pkt *pkt,
1726 struct fwtty_port *port) 1720 struct fwtty_port *port)
1727 { 1721 {
1728 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG); 1722 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG);
1729 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code)); 1723 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1730 fill_plug_params(&pkt->plug_req, port); 1724 fill_plug_params(&pkt->plug_req, port);
1731 } 1725 }
1732 1726
1733 static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt, 1727 static inline void fill_plug_rsp_ok(struct fwserial_mgmt_pkt *pkt,
1734 struct fwtty_port *port) 1728 struct fwtty_port *port)
1735 { 1729 {
1736 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP); 1730 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP);
1737 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code)); 1731 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1738 fill_plug_params(&pkt->plug_rsp, port); 1732 fill_plug_params(&pkt->plug_rsp, port);
1739 } 1733 }
1740 1734
1741 static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt) 1735 static inline void fill_plug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1742 { 1736 {
1743 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK); 1737 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_PLUG_RSP | FWSC_RSP_NACK);
1744 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code)); 1738 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1745 } 1739 }
1746 1740
1747 static inline void fill_unplug_req(struct fwserial_mgmt_pkt *pkt) 1741 static inline void fill_unplug_req(struct fwserial_mgmt_pkt *pkt)
1748 { 1742 {
1749 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG); 1743 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG);
1750 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code)); 1744 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1751 } 1745 }
1752 1746
1753 static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt) 1747 static inline void fill_unplug_rsp_nack(struct fwserial_mgmt_pkt *pkt)
1754 { 1748 {
1755 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK); 1749 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP | FWSC_RSP_NACK);
1756 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code)); 1750 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1757 } 1751 }
1758 1752
1759 static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt) 1753 static inline void fill_unplug_rsp_ok(struct fwserial_mgmt_pkt *pkt)
1760 { 1754 {
1761 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP); 1755 pkt->hdr.code = cpu_to_be16(FWSC_VIRT_CABLE_UNPLUG_RSP);
1762 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code)); 1756 pkt->hdr.len = cpu_to_be16(mgmt_pkt_expected_len(pkt->hdr.code));
1763 } 1757 }
1764 1758
1765 static void fwserial_virt_plug_complete(struct fwtty_peer *peer, 1759 static void fwserial_virt_plug_complete(struct fwtty_peer *peer,
1766 struct virt_plug_params *params) 1760 struct virt_plug_params *params)
1767 { 1761 {
1768 struct fwtty_port *port = peer->port; 1762 struct fwtty_port *port = peer->port;
1769 1763
1770 peer->status_addr = be32_to_u64(params->status_hi, params->status_lo); 1764 peer->status_addr = be32_to_u64(params->status_hi, params->status_lo);
1771 peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo); 1765 peer->fifo_addr = be32_to_u64(params->fifo_hi, params->fifo_lo);
1772 peer->fifo_len = be32_to_cpu(params->fifo_len); 1766 peer->fifo_len = be32_to_cpu(params->fifo_len);
1773 peer_set_state(peer, FWPS_ATTACHED); 1767 peer_set_state(peer, FWPS_ATTACHED);
1774 1768
1775 /* reconfigure tx_fifo optimally for this peer */ 1769 /* reconfigure tx_fifo optimally for this peer */
1776 spin_lock_bh(&port->lock); 1770 spin_lock_bh(&port->lock);
1777 port->max_payload = min(peer->max_payload, peer->fifo_len); 1771 port->max_payload = min(peer->max_payload, peer->fifo_len);
1778 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload); 1772 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1779 spin_unlock_bh(&peer->port->lock); 1773 spin_unlock_bh(&peer->port->lock);
1780 1774
1781 if (port->port.console && port->fwcon_ops->notify != NULL) 1775 if (port->port.console && port->fwcon_ops->notify != NULL)
1782 (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data); 1776 (*port->fwcon_ops->notify)(FWCON_NOTIFY_ATTACH, port->con_data);
1783 1777
1784 fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s", 1778 fwtty_info(&peer->unit, "peer (guid:%016llx) connected on %s",
1785 (unsigned long long)peer->guid, dev_name(port->device)); 1779 (unsigned long long)peer->guid, dev_name(port->device));
1786 } 1780 }
1787 1781
1788 static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer, 1782 static inline int fwserial_send_mgmt_sync(struct fwtty_peer *peer,
1789 struct fwserial_mgmt_pkt *pkt) 1783 struct fwserial_mgmt_pkt *pkt)
1790 { 1784 {
1791 int generation; 1785 int generation;
1792 int rcode, tries = 5; 1786 int rcode, tries = 5;
1793 1787
1794 do { 1788 do {
1795 generation = peer->generation; 1789 generation = peer->generation;
1796 smp_rmb(); 1790 smp_rmb();
1797 1791
1798 rcode = fw_run_transaction(peer->serial->card, 1792 rcode = fw_run_transaction(peer->serial->card,
1799 TCODE_WRITE_BLOCK_REQUEST, 1793 TCODE_WRITE_BLOCK_REQUEST,
1800 peer->node_id, 1794 peer->node_id,
1801 generation, peer->speed, 1795 generation, peer->speed,
1802 peer->mgmt_addr, 1796 peer->mgmt_addr,
1803 pkt, be16_to_cpu(pkt->hdr.len)); 1797 pkt, be16_to_cpu(pkt->hdr.len));
1804 if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR || 1798 if (rcode == RCODE_BUSY || rcode == RCODE_SEND_ERROR ||
1805 rcode == RCODE_GENERATION) { 1799 rcode == RCODE_GENERATION) {
1806 fwtty_dbg(&peer->unit, "mgmt write error: %d", rcode); 1800 fwtty_dbg(&peer->unit, "mgmt write error: %d", rcode);
1807 continue; 1801 continue;
1808 } else 1802 } else
1809 break; 1803 break;
1810 } while (--tries > 0); 1804 } while (--tries > 0);
1811 return rcode; 1805 return rcode;
1812 } 1806 }
1813 1807
1814 /** 1808 /**
1815 * fwserial_claim_port - attempt to claim port @ index for peer 1809 * fwserial_claim_port - attempt to claim port @ index for peer
1816 * 1810 *
1817 * Returns ptr to claimed port or error code (as ERR_PTR()) 1811 * Returns ptr to claimed port or error code (as ERR_PTR())
1818 * Can sleep - must be called from process context 1812 * Can sleep - must be called from process context
1819 */ 1813 */
1820 static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer, 1814 static struct fwtty_port *fwserial_claim_port(struct fwtty_peer *peer,
1821 int index) 1815 int index)
1822 { 1816 {
1823 struct fwtty_port *port; 1817 struct fwtty_port *port;
1824 1818
1825 if (index < 0 || index >= num_ports) 1819 if (index < 0 || index >= num_ports)
1826 return ERR_PTR(-EINVAL); 1820 return ERR_PTR(-EINVAL);
1827 1821
1828 /* must guarantee that previous port releases have completed */ 1822 /* must guarantee that previous port releases have completed */
1829 synchronize_rcu(); 1823 synchronize_rcu();
1830 1824
1831 port = peer->serial->ports[index]; 1825 port = peer->serial->ports[index];
1832 spin_lock_bh(&port->lock); 1826 spin_lock_bh(&port->lock);
1833 if (!rcu_access_pointer(port->peer)) 1827 if (!rcu_access_pointer(port->peer))
1834 rcu_assign_pointer(port->peer, peer); 1828 rcu_assign_pointer(port->peer, peer);
1835 else 1829 else
1836 port = ERR_PTR(-EBUSY); 1830 port = ERR_PTR(-EBUSY);
1837 spin_unlock_bh(&port->lock); 1831 spin_unlock_bh(&port->lock);
1838 1832
1839 return port; 1833 return port;
1840 } 1834 }
1841 1835
1842 /** 1836 /**
1843 * fwserial_find_port - find avail port and claim for peer 1837 * fwserial_find_port - find avail port and claim for peer
1844 * 1838 *
1845 * Returns ptr to claimed port or NULL if none avail 1839 * Returns ptr to claimed port or NULL if none avail
1846 * Can sleep - must be called from process context 1840 * Can sleep - must be called from process context
1847 */ 1841 */
1848 static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer) 1842 static struct fwtty_port *fwserial_find_port(struct fwtty_peer *peer)
1849 { 1843 {
1850 struct fwtty_port **ports = peer->serial->ports; 1844 struct fwtty_port **ports = peer->serial->ports;
1851 int i; 1845 int i;
1852 1846
1853 /* must guarantee that previous port releases have completed */ 1847 /* must guarantee that previous port releases have completed */
1854 synchronize_rcu(); 1848 synchronize_rcu();
1855 1849
1856 /* TODO: implement optional GUID-to-specific port # matching */ 1850 /* TODO: implement optional GUID-to-specific port # matching */
1857 1851
1858 /* find an unattached port (but not the loopback port, if present) */ 1852 /* find an unattached port (but not the loopback port, if present) */
1859 for (i = 0; i < num_ttys; ++i) { 1853 for (i = 0; i < num_ttys; ++i) {
1860 spin_lock_bh(&ports[i]->lock); 1854 spin_lock_bh(&ports[i]->lock);
1861 if (!ports[i]->peer) { 1855 if (!ports[i]->peer) {
1862 /* claim port */ 1856 /* claim port */
1863 rcu_assign_pointer(ports[i]->peer, peer); 1857 rcu_assign_pointer(ports[i]->peer, peer);
1864 spin_unlock_bh(&ports[i]->lock); 1858 spin_unlock_bh(&ports[i]->lock);
1865 return ports[i]; 1859 return ports[i];
1866 } 1860 }
1867 spin_unlock_bh(&ports[i]->lock); 1861 spin_unlock_bh(&ports[i]->lock);
1868 } 1862 }
1869 return NULL; 1863 return NULL;
1870 } 1864 }
1871 1865
1872 static void fwserial_release_port(struct fwtty_port *port, bool reset) 1866 static void fwserial_release_port(struct fwtty_port *port, bool reset)
1873 { 1867 {
1874 /* drop carrier (and all other line status) */ 1868 /* drop carrier (and all other line status) */
1875 if (reset) 1869 if (reset)
1876 fwtty_update_port_status(port, 0); 1870 fwtty_update_port_status(port, 0);
1877 1871
1878 spin_lock_bh(&port->lock); 1872 spin_lock_bh(&port->lock);
1879 1873
1880 /* reset dma fifo max transmission size back to S100 */ 1874 /* reset dma fifo max transmission size back to S100 */
1881 port->max_payload = link_speed_to_max_payload(SCODE_100); 1875 port->max_payload = link_speed_to_max_payload(SCODE_100);
1882 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload); 1876 dma_fifo_change_tx_limit(&port->tx_fifo, port->max_payload);
1883 1877
1884 rcu_assign_pointer(port->peer, NULL); 1878 rcu_assign_pointer(port->peer, NULL);
1885 spin_unlock_bh(&port->lock); 1879 spin_unlock_bh(&port->lock);
1886 1880
1887 if (port->port.console && port->fwcon_ops->notify != NULL) 1881 if (port->port.console && port->fwcon_ops->notify != NULL)
1888 (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data); 1882 (*port->fwcon_ops->notify)(FWCON_NOTIFY_DETACH, port->con_data);
1889 } 1883 }
1890 1884
1891 static void fwserial_plug_timeout(unsigned long data) 1885 static void fwserial_plug_timeout(unsigned long data)
1892 { 1886 {
1893 struct fwtty_peer *peer = (struct fwtty_peer *) data; 1887 struct fwtty_peer *peer = (struct fwtty_peer *) data;
1894 struct fwtty_port *port; 1888 struct fwtty_port *port;
1895 1889
1896 spin_lock_bh(&peer->lock); 1890 spin_lock_bh(&peer->lock);
1897 if (peer->state != FWPS_PLUG_PENDING) { 1891 if (peer->state != FWPS_PLUG_PENDING) {
1898 spin_unlock_bh(&peer->lock); 1892 spin_unlock_bh(&peer->lock);
1899 return; 1893 return;
1900 } 1894 }
1901 1895
1902 port = peer_revert_state(peer); 1896 port = peer_revert_state(peer);
1903 spin_unlock_bh(&peer->lock); 1897 spin_unlock_bh(&peer->lock);
1904 1898
1905 if (port) 1899 if (port)
1906 fwserial_release_port(port, false); 1900 fwserial_release_port(port, false);
1907 } 1901 }
1908 1902
1909 /** 1903 /**
1910 * fwserial_connect_peer - initiate virtual cable with peer 1904 * fwserial_connect_peer - initiate virtual cable with peer
1911 * 1905 *
1912 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent, 1906 * Returns 0 if VIRT_CABLE_PLUG request was successfully sent,
1913 * otherwise error code. Must be called from process context. 1907 * otherwise error code. Must be called from process context.
1914 */ 1908 */
1915 static int fwserial_connect_peer(struct fwtty_peer *peer) 1909 static int fwserial_connect_peer(struct fwtty_peer *peer)
1916 { 1910 {
1917 struct fwtty_port *port; 1911 struct fwtty_port *port;
1918 struct fwserial_mgmt_pkt *pkt; 1912 struct fwserial_mgmt_pkt *pkt;
1919 int err, rcode; 1913 int err, rcode;
1920 1914
1921 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL); 1915 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
1922 if (!pkt) 1916 if (!pkt)
1923 return -ENOMEM; 1917 return -ENOMEM;
1924 1918
1925 port = fwserial_find_port(peer); 1919 port = fwserial_find_port(peer);
1926 if (!port) { 1920 if (!port) {
1927 fwtty_err(&peer->unit, "avail ports in use"); 1921 fwtty_err(&peer->unit, "avail ports in use");
1928 err = -EBUSY; 1922 err = -EBUSY;
1929 goto free_pkt; 1923 goto free_pkt;
1930 } 1924 }
1931 1925
1932 spin_lock_bh(&peer->lock); 1926 spin_lock_bh(&peer->lock);
1933 1927
1934 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */ 1928 /* only initiate VIRT_CABLE_PLUG if peer is currently not attached */
1935 if (peer->state != FWPS_NOT_ATTACHED) { 1929 if (peer->state != FWPS_NOT_ATTACHED) {
1936 err = -EBUSY; 1930 err = -EBUSY;
1937 goto release_port; 1931 goto release_port;
1938 } 1932 }
1939 1933
1940 peer->port = port; 1934 peer->port = port;
1941 peer_set_state(peer, FWPS_PLUG_PENDING); 1935 peer_set_state(peer, FWPS_PLUG_PENDING);
1942 1936
1943 fill_plug_req(pkt, peer->port); 1937 fill_plug_req(pkt, peer->port);
1944 1938
1945 setup_timer(&peer->timer, fwserial_plug_timeout, (unsigned long)peer); 1939 setup_timer(&peer->timer, fwserial_plug_timeout, (unsigned long)peer);
1946 mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT); 1940 mod_timer(&peer->timer, jiffies + VIRT_CABLE_PLUG_TIMEOUT);
1947 spin_unlock_bh(&peer->lock); 1941 spin_unlock_bh(&peer->lock);
1948 1942
1949 rcode = fwserial_send_mgmt_sync(peer, pkt); 1943 rcode = fwserial_send_mgmt_sync(peer, pkt);
1950 1944
1951 spin_lock_bh(&peer->lock); 1945 spin_lock_bh(&peer->lock);
1952 if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) { 1946 if (peer->state == FWPS_PLUG_PENDING && rcode != RCODE_COMPLETE) {
1953 if (rcode == RCODE_CONFLICT_ERROR) 1947 if (rcode == RCODE_CONFLICT_ERROR)
1954 err = -EAGAIN; 1948 err = -EAGAIN;
1955 else 1949 else
1956 err = -EIO; 1950 err = -EIO;
1957 goto cancel_timer; 1951 goto cancel_timer;
1958 } 1952 }
1959 spin_unlock_bh(&peer->lock); 1953 spin_unlock_bh(&peer->lock);
1960 1954
1961 kfree(pkt); 1955 kfree(pkt);
1962 return 0; 1956 return 0;
1963 1957
1964 cancel_timer: 1958 cancel_timer:
1965 del_timer(&peer->timer); 1959 del_timer(&peer->timer);
1966 peer_revert_state(peer); 1960 peer_revert_state(peer);
1967 release_port: 1961 release_port:
1968 spin_unlock_bh(&peer->lock); 1962 spin_unlock_bh(&peer->lock);
1969 fwserial_release_port(port, false); 1963 fwserial_release_port(port, false);
1970 free_pkt: 1964 free_pkt:
1971 kfree(pkt); 1965 kfree(pkt);
1972 return err; 1966 return err;
1973 } 1967 }
1974 1968
1975 /** 1969 /**
1976 * fwserial_close_port - 1970 * fwserial_close_port -
1977 * HUP the tty (if the tty exists) and unregister the tty device. 1971 * HUP the tty (if the tty exists) and unregister the tty device.
1978 * Only used by the unit driver upon unit removal to disconnect and 1972 * Only used by the unit driver upon unit removal to disconnect and
1979 * cleanup all attached ports 1973 * cleanup all attached ports
1980 * 1974 *
1981 * The port reference is put by fwtty_cleanup (if a reference was 1975 * The port reference is put by fwtty_cleanup (if a reference was
1982 * ever taken). 1976 * ever taken).
1983 */ 1977 */
1984 static void fwserial_close_port(struct tty_driver *driver, 1978 static void fwserial_close_port(struct tty_driver *driver,
1985 struct fwtty_port *port) 1979 struct fwtty_port *port)
1986 { 1980 {
1987 struct tty_struct *tty; 1981 struct tty_struct *tty;
1988 1982
1989 mutex_lock(&port->port.mutex); 1983 mutex_lock(&port->port.mutex);
1990 tty = tty_port_tty_get(&port->port); 1984 tty = tty_port_tty_get(&port->port);
1991 if (tty) { 1985 if (tty) {
1992 tty_vhangup(tty); 1986 tty_vhangup(tty);
1993 tty_kref_put(tty); 1987 tty_kref_put(tty);
1994 } 1988 }
1995 mutex_unlock(&port->port.mutex); 1989 mutex_unlock(&port->port.mutex);
1996 1990
1997 if (driver == fwloop_driver) 1991 if (driver == fwloop_driver)
1998 tty_unregister_device(driver, loop_idx(port)); 1992 tty_unregister_device(driver, loop_idx(port));
1999 else 1993 else
2000 tty_unregister_device(driver, port->index); 1994 tty_unregister_device(driver, port->index);
2001 } 1995 }
2002 1996
2003 /** 1997 /**
2004 * fwserial_lookup - finds first fw_serial associated with card 1998 * fwserial_lookup - finds first fw_serial associated with card
2005 * @card: fw_card to match 1999 * @card: fw_card to match
2006 * 2000 *
2007 * NB: caller must be holding fwserial_list_mutex 2001 * NB: caller must be holding fwserial_list_mutex
2008 */ 2002 */
2009 static struct fw_serial *fwserial_lookup(struct fw_card *card) 2003 static struct fw_serial *fwserial_lookup(struct fw_card *card)
2010 { 2004 {
2011 struct fw_serial *serial; 2005 struct fw_serial *serial;
2012 2006
2013 list_for_each_entry(serial, &fwserial_list, list) { 2007 list_for_each_entry(serial, &fwserial_list, list) {
2014 if (card == serial->card) 2008 if (card == serial->card)
2015 return serial; 2009 return serial;
2016 } 2010 }
2017 2011
2018 return NULL; 2012 return NULL;
2019 } 2013 }
2020 2014
2021 /** 2015 /**
2022 * __fwserial_lookup_rcu - finds first fw_serial associated with card 2016 * __fwserial_lookup_rcu - finds first fw_serial associated with card
2023 * @card: fw_card to match 2017 * @card: fw_card to match
2024 * 2018 *
2025 * NB: caller must be inside rcu_read_lock() section 2019 * NB: caller must be inside rcu_read_lock() section
2026 */ 2020 */
2027 static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card) 2021 static struct fw_serial *__fwserial_lookup_rcu(struct fw_card *card)
2028 { 2022 {
2029 struct fw_serial *serial; 2023 struct fw_serial *serial;
2030 2024
2031 list_for_each_entry_rcu(serial, &fwserial_list, list) { 2025 list_for_each_entry_rcu(serial, &fwserial_list, list) {
2032 if (card == serial->card) 2026 if (card == serial->card)
2033 return serial; 2027 return serial;
2034 } 2028 }
2035 2029
2036 return NULL; 2030 return NULL;
2037 } 2031 }
2038 2032
2039 /** 2033 /**
2040 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id 2034 * __fwserial_peer_by_node_id - finds a peer matching the given generation + id
2041 * 2035 *
2042 * If a matching peer could not be found for the specified generation/node id, 2036 * If a matching peer could not be found for the specified generation/node id,
2043 * this could be because: 2037 * this could be because:
2044 * a) the generation has changed and one of the nodes hasn't updated yet 2038 * a) the generation has changed and one of the nodes hasn't updated yet
2045 * b) the remote node has created its remote unit device before this 2039 * b) the remote node has created its remote unit device before this
2046 * local node has created its corresponding remote unit device 2040 * local node has created its corresponding remote unit device
2047 * In either case, the remote node should retry 2041 * In either case, the remote node should retry
2048 * 2042 *
2049 * Note: caller must be in rcu_read_lock() section 2043 * Note: caller must be in rcu_read_lock() section
2050 */ 2044 */
2051 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card, 2045 static struct fwtty_peer *__fwserial_peer_by_node_id(struct fw_card *card,
2052 int generation, int id) 2046 int generation, int id)
2053 { 2047 {
2054 struct fw_serial *serial; 2048 struct fw_serial *serial;
2055 struct fwtty_peer *peer; 2049 struct fwtty_peer *peer;
2056 2050
2057 serial = __fwserial_lookup_rcu(card); 2051 serial = __fwserial_lookup_rcu(card);
2058 if (!serial) { 2052 if (!serial) {
2059 /* 2053 /*
2060 * Something is very wrong - there should be a matching 2054 * Something is very wrong - there should be a matching
2061 * fw_serial structure for every fw_card. Maybe the remote node 2055 * fw_serial structure for every fw_card. Maybe the remote node
2062 * has created its remote unit device before this driver has 2056 * has created its remote unit device before this driver has
2063 * been probed for any unit devices... 2057 * been probed for any unit devices...
2064 */ 2058 */
2065 fwtty_err(card, "unknown card (guid %016llx)", 2059 fwtty_err(card, "unknown card (guid %016llx)",
2066 (unsigned long long) card->guid); 2060 (unsigned long long) card->guid);
2067 return NULL; 2061 return NULL;
2068 } 2062 }
2069 2063
2070 list_for_each_entry_rcu(peer, &serial->peer_list, list) { 2064 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
2071 int g = peer->generation; 2065 int g = peer->generation;
2072 smp_rmb(); 2066 smp_rmb();
2073 if (generation == g && id == peer->node_id) 2067 if (generation == g && id == peer->node_id)
2074 return peer; 2068 return peer;
2075 } 2069 }
2076 2070
2077 return NULL; 2071 return NULL;
2078 } 2072 }
2079 2073
2080 #ifdef DEBUG 2074 #ifdef DEBUG
2081 static void __dump_peer_list(struct fw_card *card) 2075 static void __dump_peer_list(struct fw_card *card)
2082 { 2076 {
2083 struct fw_serial *serial; 2077 struct fw_serial *serial;
2084 struct fwtty_peer *peer; 2078 struct fwtty_peer *peer;
2085 2079
2086 serial = __fwserial_lookup_rcu(card); 2080 serial = __fwserial_lookup_rcu(card);
2087 if (!serial) 2081 if (!serial)
2088 return; 2082 return;
2089 2083
2090 list_for_each_entry_rcu(peer, &serial->peer_list, list) { 2084 list_for_each_entry_rcu(peer, &serial->peer_list, list) {
2091 int g = peer->generation; 2085 int g = peer->generation;
2092 smp_rmb(); 2086 smp_rmb();
2093 fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n", g, 2087 fwtty_dbg(card, "peer(%d:%x) guid: %016llx\n", g,
2094 peer->node_id, (unsigned long long) peer->guid); 2088 peer->node_id, (unsigned long long) peer->guid);
2095 } 2089 }
2096 } 2090 }
2097 #else 2091 #else
2098 #define __dump_peer_list(s) 2092 #define __dump_peer_list(s)
2099 #endif 2093 #endif
2100 2094
2101 static void fwserial_auto_connect(struct work_struct *work) 2095 static void fwserial_auto_connect(struct work_struct *work)
2102 { 2096 {
2103 struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect); 2097 struct fwtty_peer *peer = to_peer(to_delayed_work(work), connect);
2104 int err; 2098 int err;
2105 2099
2106 err = fwserial_connect_peer(peer); 2100 err = fwserial_connect_peer(peer);
2107 if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES) 2101 if (err == -EAGAIN && ++peer->connect_retries < MAX_CONNECT_RETRIES)
2108 schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY); 2102 schedule_delayed_work(&peer->connect, CONNECT_RETRY_DELAY);
2109 } 2103 }
2110 2104
2111 /** 2105 /**
2112 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer' 2106 * fwserial_add_peer - add a newly probed 'serial' unit device as a 'peer'
2113 * @serial: aggregate representing the specific fw_card to add the peer to 2107 * @serial: aggregate representing the specific fw_card to add the peer to
2114 * @unit: 'peer' to create and add to peer_list of serial 2108 * @unit: 'peer' to create and add to peer_list of serial
2115 * 2109 *
2116 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of 2110 * Adds a 'peer' (ie, a local or remote 'serial' unit device) to the list of
2117 * peers for a specific fw_card. Optionally, auto-attach this peer to an 2111 * peers for a specific fw_card. Optionally, auto-attach this peer to an
2118 * available tty port. This function is called either directly or indirectly 2112 * available tty port. This function is called either directly or indirectly
2119 * as a result of a 'serial' unit device being created & probed. 2113 * as a result of a 'serial' unit device being created & probed.
2120 * 2114 *
2121 * Note: this function is serialized with fwserial_remove_peer() by the 2115 * Note: this function is serialized with fwserial_remove_peer() by the
2122 * fwserial_list_mutex held in fwserial_probe(). 2116 * fwserial_list_mutex held in fwserial_probe().
2123 * 2117 *
2124 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained 2118 * A 1:1 correspondence between an fw_unit and an fwtty_peer is maintained
2125 * via the dev_set_drvdata() for the device of the fw_unit. 2119 * via the dev_set_drvdata() for the device of the fw_unit.
2126 */ 2120 */
2127 static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit) 2121 static int fwserial_add_peer(struct fw_serial *serial, struct fw_unit *unit)
2128 { 2122 {
2129 struct device *dev = &unit->device; 2123 struct device *dev = &unit->device;
2130 struct fw_device *parent = fw_parent_device(unit); 2124 struct fw_device *parent = fw_parent_device(unit);
2131 struct fwtty_peer *peer; 2125 struct fwtty_peer *peer;
2132 struct fw_csr_iterator ci; 2126 struct fw_csr_iterator ci;
2133 int key, val; 2127 int key, val;
2134 int generation; 2128 int generation;
2135 2129
2136 peer = kzalloc(sizeof(*peer), GFP_KERNEL); 2130 peer = kzalloc(sizeof(*peer), GFP_KERNEL);
2137 if (!peer) 2131 if (!peer)
2138 return -ENOMEM; 2132 return -ENOMEM;
2139 2133
2140 peer_set_state(peer, FWPS_NOT_ATTACHED); 2134 peer_set_state(peer, FWPS_NOT_ATTACHED);
2141 2135
2142 dev_set_drvdata(dev, peer); 2136 dev_set_drvdata(dev, peer);
2143 peer->unit = unit; 2137 peer->unit = unit;
2144 peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4]; 2138 peer->guid = (u64)parent->config_rom[3] << 32 | parent->config_rom[4];
2145 peer->speed = parent->max_speed; 2139 peer->speed = parent->max_speed;
2146 peer->max_payload = min(device_max_receive(parent), 2140 peer->max_payload = min(device_max_receive(parent),
2147 link_speed_to_max_payload(peer->speed)); 2141 link_speed_to_max_payload(peer->speed));
2148 2142
2149 generation = parent->generation; 2143 generation = parent->generation;
2150 smp_rmb(); 2144 smp_rmb();
2151 peer->node_id = parent->node_id; 2145 peer->node_id = parent->node_id;
2152 smp_wmb(); 2146 smp_wmb();
2153 peer->generation = generation; 2147 peer->generation = generation;
2154 2148
2155 /* retrieve the mgmt bus addr from the unit directory */ 2149 /* retrieve the mgmt bus addr from the unit directory */
2156 fw_csr_iterator_init(&ci, unit->directory); 2150 fw_csr_iterator_init(&ci, unit->directory);
2157 while (fw_csr_iterator_next(&ci, &key, &val)) { 2151 while (fw_csr_iterator_next(&ci, &key, &val)) {
2158 if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) { 2152 if (key == (CSR_OFFSET | CSR_DEPENDENT_INFO)) {
2159 peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val; 2153 peer->mgmt_addr = CSR_REGISTER_BASE + 4 * val;
2160 break; 2154 break;
2161 } 2155 }
2162 } 2156 }
2163 if (peer->mgmt_addr == 0ULL) { 2157 if (peer->mgmt_addr == 0ULL) {
2164 /* 2158 /*
2165 * No mgmt address effectively disables VIRT_CABLE_PLUG - 2159 * No mgmt address effectively disables VIRT_CABLE_PLUG -
2166 * this peer will not be able to attach to a remote 2160 * this peer will not be able to attach to a remote
2167 */ 2161 */
2168 peer_set_state(peer, FWPS_NO_MGMT_ADDR); 2162 peer_set_state(peer, FWPS_NO_MGMT_ADDR);
2169 } 2163 }
2170 2164
2171 spin_lock_init(&peer->lock); 2165 spin_lock_init(&peer->lock);
2172 peer->port = NULL; 2166 peer->port = NULL;
2173 2167
2174 init_timer(&peer->timer); 2168 init_timer(&peer->timer);
2175 INIT_WORK(&peer->work, NULL); 2169 INIT_WORK(&peer->work, NULL);
2176 INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect); 2170 INIT_DELAYED_WORK(&peer->connect, fwserial_auto_connect);
2177 2171
2178 /* associate peer with specific fw_card */ 2172 /* associate peer with specific fw_card */
2179 peer->serial = serial; 2173 peer->serial = serial;
2180 list_add_rcu(&peer->list, &serial->peer_list); 2174 list_add_rcu(&peer->list, &serial->peer_list);
2181 2175
2182 fwtty_info(&peer->unit, "peer added (guid:%016llx)", 2176 fwtty_info(&peer->unit, "peer added (guid:%016llx)",
2183 (unsigned long long)peer->guid); 2177 (unsigned long long)peer->guid);
2184 2178
2185 /* identify the local unit & virt cable to loopback port */ 2179 /* identify the local unit & virt cable to loopback port */
2186 if (parent->is_local) { 2180 if (parent->is_local) {
2187 serial->self = peer; 2181 serial->self = peer;
2188 if (create_loop_dev) { 2182 if (create_loop_dev) {
2189 struct fwtty_port *port; 2183 struct fwtty_port *port;
2190 port = fwserial_claim_port(peer, num_ttys); 2184 port = fwserial_claim_port(peer, num_ttys);
2191 if (!IS_ERR(port)) { 2185 if (!IS_ERR(port)) {
2192 struct virt_plug_params params; 2186 struct virt_plug_params params;
2193 2187
2194 spin_lock_bh(&peer->lock); 2188 spin_lock_bh(&peer->lock);
2195 peer->port = port; 2189 peer->port = port;
2196 fill_plug_params(&params, port); 2190 fill_plug_params(&params, port);
2197 fwserial_virt_plug_complete(peer, &params); 2191 fwserial_virt_plug_complete(peer, &params);
2198 spin_unlock_bh(&peer->lock); 2192 spin_unlock_bh(&peer->lock);
2199 2193
2200 fwtty_write_port_status(port); 2194 fwtty_write_port_status(port);
2201 } 2195 }
2202 } 2196 }
2203 2197
2204 } else if (auto_connect) { 2198 } else if (auto_connect) {
2205 /* auto-attach to remote units only (if policy allows) */ 2199 /* auto-attach to remote units only (if policy allows) */
2206 schedule_delayed_work(&peer->connect, 1); 2200 schedule_delayed_work(&peer->connect, 1);
2207 } 2201 }
2208 2202
2209 return 0; 2203 return 0;
2210 } 2204 }
2211 2205
2212 /** 2206 /**
2213 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer' 2207 * fwserial_remove_peer - remove a 'serial' unit device as a 'peer'
2214 * 2208 *
2215 * Remove a 'peer' from its list of peers. This function is only 2209 * Remove a 'peer' from its list of peers. This function is only
2216 * called by fwserial_remove() on bus removal of the unit device. 2210 * called by fwserial_remove() on bus removal of the unit device.
2217 * 2211 *
2218 * Note: this function is serialized with fwserial_add_peer() by the 2212 * Note: this function is serialized with fwserial_add_peer() by the
2219 * fwserial_list_mutex held in fwserial_remove(). 2213 * fwserial_list_mutex held in fwserial_remove().
2220 */ 2214 */
2221 static void fwserial_remove_peer(struct fwtty_peer *peer) 2215 static void fwserial_remove_peer(struct fwtty_peer *peer)
2222 { 2216 {
2223 struct fwtty_port *port; 2217 struct fwtty_port *port;
2224 2218
2225 spin_lock_bh(&peer->lock); 2219 spin_lock_bh(&peer->lock);
2226 peer_set_state(peer, FWPS_GONE); 2220 peer_set_state(peer, FWPS_GONE);
2227 spin_unlock_bh(&peer->lock); 2221 spin_unlock_bh(&peer->lock);
2228 2222
2229 cancel_delayed_work_sync(&peer->connect); 2223 cancel_delayed_work_sync(&peer->connect);
2230 cancel_work_sync(&peer->work); 2224 cancel_work_sync(&peer->work);
2231 2225
2232 spin_lock_bh(&peer->lock); 2226 spin_lock_bh(&peer->lock);
2233 /* if this unit is the local unit, clear link */ 2227 /* if this unit is the local unit, clear link */
2234 if (peer == peer->serial->self) 2228 if (peer == peer->serial->self)
2235 peer->serial->self = NULL; 2229 peer->serial->self = NULL;
2236 2230
2237 /* cancel the request timeout timer (if running) */ 2231 /* cancel the request timeout timer (if running) */
2238 del_timer(&peer->timer); 2232 del_timer(&peer->timer);
2239 2233
2240 port = peer->port; 2234 port = peer->port;
2241 peer->port = NULL; 2235 peer->port = NULL;
2242 2236
2243 list_del_rcu(&peer->list); 2237 list_del_rcu(&peer->list);
2244 2238
2245 fwtty_info(&peer->unit, "peer removed (guid:%016llx)", 2239 fwtty_info(&peer->unit, "peer removed (guid:%016llx)",
2246 (unsigned long long)peer->guid); 2240 (unsigned long long)peer->guid);
2247 2241
2248 spin_unlock_bh(&peer->lock); 2242 spin_unlock_bh(&peer->lock);
2249 2243
2250 if (port) 2244 if (port)
2251 fwserial_release_port(port, true); 2245 fwserial_release_port(port, true);
2252 2246
2253 synchronize_rcu(); 2247 synchronize_rcu();
2254 kfree(peer); 2248 kfree(peer);
2255 } 2249 }
2256 2250
2257 /** 2251 /**
2258 * fwserial_create - init everything to create TTYs for a specific fw_card 2252 * fwserial_create - init everything to create TTYs for a specific fw_card
2259 * @unit: fw_unit for first 'serial' unit device probed for this fw_card 2253 * @unit: fw_unit for first 'serial' unit device probed for this fw_card
2260 * 2254 *
2261 * This function inits the aggregate structure (an fw_serial instance) 2255 * This function inits the aggregate structure (an fw_serial instance)
2262 * used to manage the TTY ports registered by a specific fw_card. Also, the 2256 * used to manage the TTY ports registered by a specific fw_card. Also, the
2263 * unit device is added as the first 'peer'. 2257 * unit device is added as the first 'peer'.
2264 * 2258 *
2265 * This unit device may represent a local unit device (as specified by the 2259 * This unit device may represent a local unit device (as specified by the
2266 * config ROM unit directory) or it may represent a remote unit device 2260 * config ROM unit directory) or it may represent a remote unit device
2267 * (as specified by the reading of the remote node's config ROM). 2261 * (as specified by the reading of the remote node's config ROM).
2268 * 2262 *
2269 * Returns 0 to indicate "ownership" of the unit device, or a negative errno 2263 * Returns 0 to indicate "ownership" of the unit device, or a negative errno
2270 * value to indicate which error. 2264 * value to indicate which error.
2271 */ 2265 */
2272 static int fwserial_create(struct fw_unit *unit) 2266 static int fwserial_create(struct fw_unit *unit)
2273 { 2267 {
2274 struct fw_device *parent = fw_parent_device(unit); 2268 struct fw_device *parent = fw_parent_device(unit);
2275 struct fw_card *card = parent->card; 2269 struct fw_card *card = parent->card;
2276 struct fw_serial *serial; 2270 struct fw_serial *serial;
2277 struct fwtty_port *port; 2271 struct fwtty_port *port;
2278 struct device *tty_dev; 2272 struct device *tty_dev;
2279 int i, j; 2273 int i, j;
2280 int err; 2274 int err;
2281 2275
2282 serial = kzalloc(sizeof(*serial), GFP_KERNEL); 2276 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
2283 if (!serial) 2277 if (!serial)
2284 return -ENOMEM; 2278 return -ENOMEM;
2285 2279
2286 kref_init(&serial->kref); 2280 kref_init(&serial->kref);
2287 serial->card = card; 2281 serial->card = card;
2288 INIT_LIST_HEAD(&serial->peer_list); 2282 INIT_LIST_HEAD(&serial->peer_list);
2289 2283
2290 for (i = 0; i < num_ports; ++i) { 2284 for (i = 0; i < num_ports; ++i) {
2291 port = kzalloc(sizeof(*port), GFP_KERNEL); 2285 port = kzalloc(sizeof(*port), GFP_KERNEL);
2292 if (!port) { 2286 if (!port) {
2293 err = -ENOMEM; 2287 err = -ENOMEM;
2294 goto free_ports; 2288 goto free_ports;
2295 } 2289 }
2296 tty_port_init(&port->port); 2290 tty_port_init(&port->port);
2297 port->index = FWTTY_INVALID_INDEX; 2291 port->index = FWTTY_INVALID_INDEX;
2298 port->port.ops = &fwtty_port_ops; 2292 port->port.ops = &fwtty_port_ops;
2299 port->serial = serial; 2293 port->serial = serial;
2300 2294
2301 spin_lock_init(&port->lock); 2295 spin_lock_init(&port->lock);
2302 INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx); 2296 INIT_DELAYED_WORK(&port->drain, fwtty_drain_tx);
2303 INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks); 2297 INIT_DELAYED_WORK(&port->emit_breaks, fwtty_emit_breaks);
2304 INIT_WORK(&port->hangup, fwtty_do_hangup); 2298 INIT_WORK(&port->hangup, fwtty_do_hangup);
2305 INIT_WORK(&port->push, fwtty_pushrx); 2299 INIT_WORK(&port->push, fwtty_pushrx);
2306 INIT_LIST_HEAD(&port->buf_list); 2300 INIT_LIST_HEAD(&port->buf_list);
2307 init_waitqueue_head(&port->wait_tx); 2301 init_waitqueue_head(&port->wait_tx);
2308 port->max_payload = link_speed_to_max_payload(SCODE_100); 2302 port->max_payload = link_speed_to_max_payload(SCODE_100);
2309 dma_fifo_init(&port->tx_fifo); 2303 dma_fifo_init(&port->tx_fifo);
2310 2304
2311 rcu_assign_pointer(port->peer, NULL); 2305 rcu_assign_pointer(port->peer, NULL);
2312 serial->ports[i] = port; 2306 serial->ports[i] = port;
2313 2307
2314 /* get unique bus addr region for port's status & recv fifo */ 2308 /* get unique bus addr region for port's status & recv fifo */
2315 port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4; 2309 port->rx_handler.length = FWTTY_PORT_RXFIFO_LEN + 4;
2316 port->rx_handler.address_callback = fwtty_port_handler; 2310 port->rx_handler.address_callback = fwtty_port_handler;
2317 port->rx_handler.callback_data = port; 2311 port->rx_handler.callback_data = port;
2318 /* 2312 /*
2319 * XXX: use custom memory region above cpu physical memory addrs 2313 * XXX: use custom memory region above cpu physical memory addrs
2320 * this will ease porting to 64-bit firewire adapters 2314 * this will ease porting to 64-bit firewire adapters
2321 */ 2315 */
2322 err = fw_core_add_address_handler(&port->rx_handler, 2316 err = fw_core_add_address_handler(&port->rx_handler,
2323 &fw_high_memory_region); 2317 &fw_high_memory_region);
2324 if (err) { 2318 if (err) {
2325 kfree(port); 2319 kfree(port);
2326 goto free_ports; 2320 goto free_ports;
2327 } 2321 }
2328 } 2322 }
2329 /* preserve i for error cleanup */ 2323 /* preserve i for error cleanup */
2330 2324
2331 err = fwtty_ports_add(serial); 2325 err = fwtty_ports_add(serial);
2332 if (err) { 2326 if (err) {
2333 fwtty_err(&unit, "no space in port table"); 2327 fwtty_err(&unit, "no space in port table");
2334 goto free_ports; 2328 goto free_ports;
2335 } 2329 }
2336 2330
2337 for (j = 0; j < num_ttys; ++j) { 2331 for (j = 0; j < num_ttys; ++j) {
2338 tty_dev = tty_port_register_device(&serial->ports[j]->port, 2332 tty_dev = tty_port_register_device(&serial->ports[j]->port,
2339 fwtty_driver, 2333 fwtty_driver,
2340 serial->ports[j]->index, 2334 serial->ports[j]->index,
2341 card->device); 2335 card->device);
2342 if (IS_ERR(tty_dev)) { 2336 if (IS_ERR(tty_dev)) {
2343 err = PTR_ERR(tty_dev); 2337 err = PTR_ERR(tty_dev);
2344 fwtty_err(&unit, "register tty device error (%d)", err); 2338 fwtty_err(&unit, "register tty device error (%d)", err);
2345 goto unregister_ttys; 2339 goto unregister_ttys;
2346 } 2340 }
2347 2341
2348 serial->ports[j]->device = tty_dev; 2342 serial->ports[j]->device = tty_dev;
2349 } 2343 }
2350 /* preserve j for error cleanup */ 2344 /* preserve j for error cleanup */
2351 2345
2352 if (create_loop_dev) { 2346 if (create_loop_dev) {
2353 struct device *loop_dev; 2347 struct device *loop_dev;
2354 2348
2355 loop_dev = tty_port_register_device(&serial->ports[j]->port, 2349 loop_dev = tty_port_register_device(&serial->ports[j]->port,
2356 fwloop_driver, 2350 fwloop_driver,
2357 loop_idx(serial->ports[j]), 2351 loop_idx(serial->ports[j]),
2358 card->device); 2352 card->device);
2359 if (IS_ERR(loop_dev)) { 2353 if (IS_ERR(loop_dev)) {
2360 err = PTR_ERR(loop_dev); 2354 err = PTR_ERR(loop_dev);
2361 fwtty_err(&unit, "create loop device failed (%d)", err); 2355 fwtty_err(&unit, "create loop device failed (%d)", err);
2362 goto unregister_ttys; 2356 goto unregister_ttys;
2363 } 2357 }
2364 serial->ports[j]->device = loop_dev; 2358 serial->ports[j]->device = loop_dev;
2365 serial->ports[j]->loopback = true; 2359 serial->ports[j]->loopback = true;
2366 } 2360 }
2367 2361
2368 if (!IS_ERR_OR_NULL(fwserial_debugfs)) { 2362 if (!IS_ERR_OR_NULL(fwserial_debugfs)) {
2369 serial->debugfs = debugfs_create_dir(dev_name(&unit->device), 2363 serial->debugfs = debugfs_create_dir(dev_name(&unit->device),
2370 fwserial_debugfs); 2364 fwserial_debugfs);
2371 if (!IS_ERR_OR_NULL(serial->debugfs)) { 2365 if (!IS_ERR_OR_NULL(serial->debugfs)) {
2372 debugfs_create_file("peers", 0444, serial->debugfs, 2366 debugfs_create_file("peers", 0444, serial->debugfs,
2373 serial, &fwtty_peers_fops); 2367 serial, &fwtty_peers_fops);
2374 debugfs_create_file("stats", 0444, serial->debugfs, 2368 debugfs_create_file("stats", 0444, serial->debugfs,
2375 serial, &fwtty_stats_fops); 2369 serial, &fwtty_stats_fops);
2376 } 2370 }
2377 } 2371 }
2378 2372
2379 list_add_rcu(&serial->list, &fwserial_list); 2373 list_add_rcu(&serial->list, &fwserial_list);
2380 2374
2381 fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)", 2375 fwtty_notice(&unit, "TTY over FireWire on device %s (guid %016llx)",
2382 dev_name(card->device), (unsigned long long) card->guid); 2376 dev_name(card->device), (unsigned long long) card->guid);
2383 2377
2384 err = fwserial_add_peer(serial, unit); 2378 err = fwserial_add_peer(serial, unit);
2385 if (!err) 2379 if (!err)
2386 return 0; 2380 return 0;
2387 2381
2388 fwtty_err(&unit, "unable to add peer unit device (%d)", err); 2382 fwtty_err(&unit, "unable to add peer unit device (%d)", err);
2389 2383
2390 /* fall-through to error processing */ 2384 /* fall-through to error processing */
2391 debugfs_remove_recursive(serial->debugfs); 2385 debugfs_remove_recursive(serial->debugfs);
2392 2386
2393 list_del_rcu(&serial->list); 2387 list_del_rcu(&serial->list);
2394 if (create_loop_dev) 2388 if (create_loop_dev)
2395 tty_unregister_device(fwloop_driver, loop_idx(serial->ports[j])); 2389 tty_unregister_device(fwloop_driver, loop_idx(serial->ports[j]));
2396 unregister_ttys: 2390 unregister_ttys:
2397 for (--j; j >= 0; --j) 2391 for (--j; j >= 0; --j)
2398 tty_unregister_device(fwtty_driver, serial->ports[j]->index); 2392 tty_unregister_device(fwtty_driver, serial->ports[j]->index);
2399 kref_put(&serial->kref, fwserial_destroy); 2393 kref_put(&serial->kref, fwserial_destroy);
2400 return err; 2394 return err;
2401 2395
2402 free_ports: 2396 free_ports:
2403 for (--i; i >= 0; --i) { 2397 for (--i; i >= 0; --i) {
2404 tty_port_destroy(&serial->ports[i]->port); 2398 tty_port_destroy(&serial->ports[i]->port);
2405 kfree(serial->ports[i]); 2399 kfree(serial->ports[i]);
2406 } 2400 }
2407 kfree(serial); 2401 kfree(serial);
2408 return err; 2402 return err;
2409 } 2403 }
2410 2404
2411 /** 2405 /**
2412 * fwserial_probe: bus probe function for firewire 'serial' unit devices 2406 * fwserial_probe: bus probe function for firewire 'serial' unit devices
2413 * 2407 *
2414 * A 'serial' unit device is created and probed as a result of: 2408 * A 'serial' unit device is created and probed as a result of:
2415 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated 2409 * - declaring a ieee1394 bus id table for 'devices' matching a fabricated
2416 * 'serial' unit specifier id 2410 * 'serial' unit specifier id
2417 * - adding a unit directory to the config ROM(s) for a 'serial' unit 2411 * - adding a unit directory to the config ROM(s) for a 'serial' unit
2418 * 2412 *
2419 * The firewire core registers unit devices by enumerating unit directories 2413 * The firewire core registers unit devices by enumerating unit directories
2420 * of a node's config ROM after reading the config ROM when a new node is 2414 * of a node's config ROM after reading the config ROM when a new node is
2421 * added to the bus topology after a bus reset. 2415 * added to the bus topology after a bus reset.
2422 * 2416 *
2423 * The practical implications of this are: 2417 * The practical implications of this are:
2424 * - this probe is called for both local and remote nodes that have a 'serial' 2418 * - this probe is called for both local and remote nodes that have a 'serial'
2425 * unit directory in their config ROM (that matches the specifiers in 2419 * unit directory in their config ROM (that matches the specifiers in
2426 * fwserial_id_table). 2420 * fwserial_id_table).
2427 * - no specific order is enforced for local vs. remote unit devices 2421 * - no specific order is enforced for local vs. remote unit devices
2428 * 2422 *
2429 * This unit driver copes with the lack of specific order in the same way the 2423 * This unit driver copes with the lack of specific order in the same way the
2430 * firewire net driver does -- each probe, for either a local or remote unit 2424 * firewire net driver does -- each probe, for either a local or remote unit
2431 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the 2425 * device, is treated as a 'peer' (has a struct fwtty_peer instance) and the
2432 * first peer created for a given fw_card (tracked by the global fwserial_list) 2426 * first peer created for a given fw_card (tracked by the global fwserial_list)
2433 * creates the underlying TTYs (aggregated in a fw_serial instance). 2427 * creates the underlying TTYs (aggregated in a fw_serial instance).
2434 * 2428 *
2435 * NB: an early attempt to differentiate local & remote unit devices by creating 2429 * NB: an early attempt to differentiate local & remote unit devices by creating
2436 * peers only for remote units and fw_serial instances (with their 2430 * peers only for remote units and fw_serial instances (with their
2437 * associated TTY devices) only for local units was discarded. Managing 2431 * associated TTY devices) only for local units was discarded. Managing
2438 * the peer lifetimes on device removal proved too complicated. 2432 * the peer lifetimes on device removal proved too complicated.
2439 * 2433 *
2440 * fwserial_probe/fwserial_remove are effectively serialized by the 2434 * fwserial_probe/fwserial_remove are effectively serialized by the
2441 * fwserial_list_mutex. This is necessary because the addition of the first peer 2435 * fwserial_list_mutex. This is necessary because the addition of the first peer
2442 * for a given fw_card will trigger the creation of the fw_serial for that 2436 * for a given fw_card will trigger the creation of the fw_serial for that
2443 * fw_card, which must not simultaneously contend with the removal of the 2437 * fw_card, which must not simultaneously contend with the removal of the
2444 * last peer for a given fw_card triggering the destruction of the same 2438 * last peer for a given fw_card triggering the destruction of the same
2445 * fw_serial for the same fw_card. 2439 * fw_serial for the same fw_card.
2446 */ 2440 */
2447 static int fwserial_probe(struct device *dev) 2441 static int fwserial_probe(struct device *dev)
2448 { 2442 {
2449 struct fw_unit *unit = fw_unit(dev); 2443 struct fw_unit *unit = fw_unit(dev);
2450 struct fw_serial *serial; 2444 struct fw_serial *serial;
2451 int err; 2445 int err;
2452 2446
2453 mutex_lock(&fwserial_list_mutex); 2447 mutex_lock(&fwserial_list_mutex);
2454 serial = fwserial_lookup(fw_parent_device(unit)->card); 2448 serial = fwserial_lookup(fw_parent_device(unit)->card);
2455 if (!serial) 2449 if (!serial)
2456 err = fwserial_create(unit); 2450 err = fwserial_create(unit);
2457 else 2451 else
2458 err = fwserial_add_peer(serial, unit); 2452 err = fwserial_add_peer(serial, unit);
2459 mutex_unlock(&fwserial_list_mutex); 2453 mutex_unlock(&fwserial_list_mutex);
2460 return err; 2454 return err;
2461 } 2455 }
2462 2456
2463 /** 2457 /**
2464 * fwserial_remove: bus removal function for firewire 'serial' unit devices 2458 * fwserial_remove: bus removal function for firewire 'serial' unit devices
2465 * 2459 *
2466 * The corresponding 'peer' for this unit device is removed from the list of 2460 * The corresponding 'peer' for this unit device is removed from the list of
2467 * peers for the associated fw_serial (which has a 1:1 correspondence with a 2461 * peers for the associated fw_serial (which has a 1:1 correspondence with a
2468 * specific fw_card). If this is the last peer being removed, then trigger 2462 * specific fw_card). If this is the last peer being removed, then trigger
2469 * the destruction of the underlying TTYs. 2463 * the destruction of the underlying TTYs.
2470 */ 2464 */
2471 static int fwserial_remove(struct device *dev) 2465 static int fwserial_remove(struct device *dev)
2472 { 2466 {
2473 struct fwtty_peer *peer = dev_get_drvdata(dev); 2467 struct fwtty_peer *peer = dev_get_drvdata(dev);
2474 struct fw_serial *serial = peer->serial; 2468 struct fw_serial *serial = peer->serial;
2475 int i; 2469 int i;
2476 2470
2477 mutex_lock(&fwserial_list_mutex); 2471 mutex_lock(&fwserial_list_mutex);
2478 fwserial_remove_peer(peer); 2472 fwserial_remove_peer(peer);
2479 2473
2480 if (list_empty(&serial->peer_list)) { 2474 if (list_empty(&serial->peer_list)) {
2481 /* unlink from the fwserial_list here */ 2475 /* unlink from the fwserial_list here */
2482 list_del_rcu(&serial->list); 2476 list_del_rcu(&serial->list);
2483 2477
2484 debugfs_remove_recursive(serial->debugfs); 2478 debugfs_remove_recursive(serial->debugfs);
2485 2479
2486 for (i = 0; i < num_ttys; ++i) 2480 for (i = 0; i < num_ttys; ++i)
2487 fwserial_close_port(fwtty_driver, serial->ports[i]); 2481 fwserial_close_port(fwtty_driver, serial->ports[i]);
2488 if (create_loop_dev) 2482 if (create_loop_dev)
2489 fwserial_close_port(fwloop_driver, serial->ports[i]); 2483 fwserial_close_port(fwloop_driver, serial->ports[i]);
2490 kref_put(&serial->kref, fwserial_destroy); 2484 kref_put(&serial->kref, fwserial_destroy);
2491 } 2485 }
2492 mutex_unlock(&fwserial_list_mutex); 2486 mutex_unlock(&fwserial_list_mutex);
2493 2487
2494 return 0; 2488 return 0;
2495 } 2489 }
2496 2490
2497 /** 2491 /**
2498 * fwserial_update: bus update function for 'firewire' serial unit devices 2492 * fwserial_update: bus update function for 'firewire' serial unit devices
2499 * 2493 *
2500 * Updates the new node_id and bus generation for this peer. Note that locking 2494 * Updates the new node_id and bus generation for this peer. Note that locking
2501 * is unnecessary; but careful memory barrier usage is important to enforce the 2495 * is unnecessary; but careful memory barrier usage is important to enforce the
2502 * load and store order of generation & node_id. 2496 * load and store order of generation & node_id.
2503 * 2497 *
2504 * The fw-core orders the write of node_id before generation in the parent 2498 * The fw-core orders the write of node_id before generation in the parent
2505 * fw_device to ensure that a stale node_id cannot be used with a current 2499 * fw_device to ensure that a stale node_id cannot be used with a current
2506 * bus generation. So the generation value must be read before the node_id. 2500 * bus generation. So the generation value must be read before the node_id.
2507 * 2501 *
2508 * In turn, this orders the write of node_id before generation in the peer to 2502 * In turn, this orders the write of node_id before generation in the peer to
2509 * also ensure a stale node_id cannot be used with a current bus generation. 2503 * also ensure a stale node_id cannot be used with a current bus generation.
2510 */ 2504 */
2511 static void fwserial_update(struct fw_unit *unit) 2505 static void fwserial_update(struct fw_unit *unit)
2512 { 2506 {
2513 struct fw_device *parent = fw_parent_device(unit); 2507 struct fw_device *parent = fw_parent_device(unit);
2514 struct fwtty_peer *peer = dev_get_drvdata(&unit->device); 2508 struct fwtty_peer *peer = dev_get_drvdata(&unit->device);
2515 int generation; 2509 int generation;
2516 2510
2517 generation = parent->generation; 2511 generation = parent->generation;
2518 smp_rmb(); 2512 smp_rmb();
2519 peer->node_id = parent->node_id; 2513 peer->node_id = parent->node_id;
2520 smp_wmb(); 2514 smp_wmb();
2521 peer->generation = generation; 2515 peer->generation = generation;
2522 } 2516 }
2523 2517
2524 static const struct ieee1394_device_id fwserial_id_table[] = { 2518 static const struct ieee1394_device_id fwserial_id_table[] = {
2525 { 2519 {
2526 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | 2520 .match_flags = IEEE1394_MATCH_SPECIFIER_ID |
2527 IEEE1394_MATCH_VERSION, 2521 IEEE1394_MATCH_VERSION,
2528 .specifier_id = LINUX_VENDOR_ID, 2522 .specifier_id = LINUX_VENDOR_ID,
2529 .version = FWSERIAL_VERSION, 2523 .version = FWSERIAL_VERSION,
2530 }, 2524 },
2531 { } 2525 { }
2532 }; 2526 };
2533 2527
2534 static struct fw_driver fwserial_driver = { 2528 static struct fw_driver fwserial_driver = {
2535 .driver = { 2529 .driver = {
2536 .owner = THIS_MODULE, 2530 .owner = THIS_MODULE,
2537 .name = KBUILD_MODNAME, 2531 .name = KBUILD_MODNAME,
2538 .bus = &fw_bus_type, 2532 .bus = &fw_bus_type,
2539 .probe = fwserial_probe, 2533 .probe = fwserial_probe,
2540 .remove = fwserial_remove, 2534 .remove = fwserial_remove,
2541 }, 2535 },
2542 .update = fwserial_update, 2536 .update = fwserial_update,
2543 .id_table = fwserial_id_table, 2537 .id_table = fwserial_id_table,
2544 }; 2538 };
2545 2539
2546 #define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id)) 2540 #define FW_UNIT_SPECIFIER(id) ((CSR_SPECIFIER_ID << 24) | (id))
2547 #define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver)) 2541 #define FW_UNIT_VERSION(ver) ((CSR_VERSION << 24) | (ver))
2548 #define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \ 2542 #define FW_UNIT_ADDRESS(ofs) (((CSR_OFFSET | CSR_DEPENDENT_INFO) << 24) \
2549 | (((ofs) - CSR_REGISTER_BASE) >> 2)) 2543 | (((ofs) - CSR_REGISTER_BASE) >> 2))
2550 /* XXX: config ROM definitons could be improved with semi-automated offset 2544 /* XXX: config ROM definitons could be improved with semi-automated offset
2551 * and length calculation 2545 * and length calculation
2552 */ 2546 */
2553 #define FW_ROM_LEN(quads) ((quads) << 16) 2547 #define FW_ROM_LEN(quads) ((quads) << 16)
2554 #define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs)) 2548 #define FW_ROM_DESCRIPTOR(ofs) (((CSR_LEAF | CSR_DESCRIPTOR) << 24) | (ofs))
2555 2549
2556 struct fwserial_unit_directory_data { 2550 struct fwserial_unit_directory_data {
2557 u32 len_crc; 2551 u32 len_crc;
2558 u32 unit_specifier; 2552 u32 unit_specifier;
2559 u32 unit_sw_version; 2553 u32 unit_sw_version;
2560 u32 unit_addr_offset; 2554 u32 unit_addr_offset;
2561 u32 desc1_ofs; 2555 u32 desc1_ofs;
2562 u32 desc1_len_crc; 2556 u32 desc1_len_crc;
2563 u32 desc1_data[5]; 2557 u32 desc1_data[5];
2564 } __packed; 2558 } __packed;
2565 2559
2566 static struct fwserial_unit_directory_data fwserial_unit_directory_data = { 2560 static struct fwserial_unit_directory_data fwserial_unit_directory_data = {
2567 .len_crc = FW_ROM_LEN(4), 2561 .len_crc = FW_ROM_LEN(4),
2568 .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID), 2562 .unit_specifier = FW_UNIT_SPECIFIER(LINUX_VENDOR_ID),
2569 .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION), 2563 .unit_sw_version = FW_UNIT_VERSION(FWSERIAL_VERSION),
2570 .desc1_ofs = FW_ROM_DESCRIPTOR(1), 2564 .desc1_ofs = FW_ROM_DESCRIPTOR(1),
2571 .desc1_len_crc = FW_ROM_LEN(5), 2565 .desc1_len_crc = FW_ROM_LEN(5),
2572 .desc1_data = { 2566 .desc1_data = {
2573 0x00000000, /* type = text */ 2567 0x00000000, /* type = text */
2574 0x00000000, /* enc = ASCII, lang EN */ 2568 0x00000000, /* enc = ASCII, lang EN */
2575 0x4c696e75, /* 'Linux TTY' */ 2569 0x4c696e75, /* 'Linux TTY' */
2576 0x78205454, 2570 0x78205454,
2577 0x59000000, 2571 0x59000000,
2578 }, 2572 },
2579 }; 2573 };
2580 2574
2581 static struct fw_descriptor fwserial_unit_directory = { 2575 static struct fw_descriptor fwserial_unit_directory = {
2582 .length = sizeof(fwserial_unit_directory_data) / sizeof(u32), 2576 .length = sizeof(fwserial_unit_directory_data) / sizeof(u32),
2583 .key = (CSR_DIRECTORY | CSR_UNIT) << 24, 2577 .key = (CSR_DIRECTORY | CSR_UNIT) << 24,
2584 .data = (u32 *)&fwserial_unit_directory_data, 2578 .data = (u32 *)&fwserial_unit_directory_data,
2585 }; 2579 };
2586 2580
2587 /* 2581 /*
2588 * The management address is in the unit space region but above other known 2582 * The management address is in the unit space region but above other known
2589 * address users (to keep wild writes from causing havoc) 2583 * address users (to keep wild writes from causing havoc)
2590 */ 2584 */
2591 static const struct fw_address_region fwserial_mgmt_addr_region = { 2585 static const struct fw_address_region fwserial_mgmt_addr_region = {
2592 .start = CSR_REGISTER_BASE + 0x1e0000ULL, 2586 .start = CSR_REGISTER_BASE + 0x1e0000ULL,
2593 .end = 0x1000000000000ULL, 2587 .end = 0x1000000000000ULL,
2594 }; 2588 };
2595 2589
2596 static struct fw_address_handler fwserial_mgmt_addr_handler; 2590 static struct fw_address_handler fwserial_mgmt_addr_handler;
2597 2591
2598 /** 2592 /**
2599 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work 2593 * fwserial_handle_plug_req - handle VIRT_CABLE_PLUG request work
2600 * @work: ptr to peer->work 2594 * @work: ptr to peer->work
2601 * 2595 *
2602 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer. 2596 * Attempts to complete the VIRT_CABLE_PLUG handshake sequence for this peer.
2603 * 2597 *
2604 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was 2598 * This checks for a collided request-- ie, that a VIRT_CABLE_PLUG request was
2605 * already sent to this peer. If so, the collision is resolved by comparing 2599 * already sent to this peer. If so, the collision is resolved by comparing
2606 * guid values; the loser sends the plug response. 2600 * guid values; the loser sends the plug response.
2607 * 2601 *
2608 * Note: if an error prevents a response, don't do anything -- the 2602 * Note: if an error prevents a response, don't do anything -- the
2609 * remote will timeout its request. 2603 * remote will timeout its request.
2610 */ 2604 */
2611 static void fwserial_handle_plug_req(struct work_struct *work) 2605 static void fwserial_handle_plug_req(struct work_struct *work)
2612 { 2606 {
2613 struct fwtty_peer *peer = to_peer(work, work); 2607 struct fwtty_peer *peer = to_peer(work, work);
2614 struct virt_plug_params *plug_req = &peer->work_params.plug_req; 2608 struct virt_plug_params *plug_req = &peer->work_params.plug_req;
2615 struct fwtty_port *port; 2609 struct fwtty_port *port;
2616 struct fwserial_mgmt_pkt *pkt; 2610 struct fwserial_mgmt_pkt *pkt;
2617 int rcode; 2611 int rcode;
2618 2612
2619 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL); 2613 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2620 if (!pkt) 2614 if (!pkt)
2621 return; 2615 return;
2622 2616
2623 port = fwserial_find_port(peer); 2617 port = fwserial_find_port(peer);
2624 2618
2625 spin_lock_bh(&peer->lock); 2619 spin_lock_bh(&peer->lock);
2626 2620
2627 switch (peer->state) { 2621 switch (peer->state) {
2628 case FWPS_NOT_ATTACHED: 2622 case FWPS_NOT_ATTACHED:
2629 if (!port) { 2623 if (!port) {
2630 fwtty_err(&peer->unit, "no more ports avail"); 2624 fwtty_err(&peer->unit, "no more ports avail");
2631 fill_plug_rsp_nack(pkt); 2625 fill_plug_rsp_nack(pkt);
2632 } else { 2626 } else {
2633 peer->port = port; 2627 peer->port = port;
2634 fill_plug_rsp_ok(pkt, peer->port); 2628 fill_plug_rsp_ok(pkt, peer->port);
2635 peer_set_state(peer, FWPS_PLUG_RESPONDING); 2629 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2636 /* don't release claimed port */ 2630 /* don't release claimed port */
2637 port = NULL; 2631 port = NULL;
2638 } 2632 }
2639 break; 2633 break;
2640 2634
2641 case FWPS_PLUG_PENDING: 2635 case FWPS_PLUG_PENDING:
2642 if (peer->serial->card->guid > peer->guid) 2636 if (peer->serial->card->guid > peer->guid)
2643 goto cleanup; 2637 goto cleanup;
2644 2638
2645 /* We lost - hijack the already-claimed port and send ok */ 2639 /* We lost - hijack the already-claimed port and send ok */
2646 del_timer(&peer->timer); 2640 del_timer(&peer->timer);
2647 fill_plug_rsp_ok(pkt, peer->port); 2641 fill_plug_rsp_ok(pkt, peer->port);
2648 peer_set_state(peer, FWPS_PLUG_RESPONDING); 2642 peer_set_state(peer, FWPS_PLUG_RESPONDING);
2649 break; 2643 break;
2650 2644
2651 default: 2645 default:
2652 fill_plug_rsp_nack(pkt); 2646 fill_plug_rsp_nack(pkt);
2653 } 2647 }
2654 2648
2655 spin_unlock_bh(&peer->lock); 2649 spin_unlock_bh(&peer->lock);
2656 if (port) 2650 if (port)
2657 fwserial_release_port(port, false); 2651 fwserial_release_port(port, false);
2658 2652
2659 rcode = fwserial_send_mgmt_sync(peer, pkt); 2653 rcode = fwserial_send_mgmt_sync(peer, pkt);
2660 2654
2661 spin_lock_bh(&peer->lock); 2655 spin_lock_bh(&peer->lock);
2662 if (peer->state == FWPS_PLUG_RESPONDING) { 2656 if (peer->state == FWPS_PLUG_RESPONDING) {
2663 if (rcode == RCODE_COMPLETE) { 2657 if (rcode == RCODE_COMPLETE) {
2664 struct fwtty_port *tmp = peer->port; 2658 struct fwtty_port *tmp = peer->port;
2665 2659
2666 fwserial_virt_plug_complete(peer, plug_req); 2660 fwserial_virt_plug_complete(peer, plug_req);
2667 spin_unlock_bh(&peer->lock); 2661 spin_unlock_bh(&peer->lock);
2668 2662
2669 fwtty_write_port_status(tmp); 2663 fwtty_write_port_status(tmp);
2670 spin_lock_bh(&peer->lock); 2664 spin_lock_bh(&peer->lock);
2671 } else { 2665 } else {
2672 fwtty_err(&peer->unit, "PLUG_RSP error (%d)", rcode); 2666 fwtty_err(&peer->unit, "PLUG_RSP error (%d)", rcode);
2673 port = peer_revert_state(peer); 2667 port = peer_revert_state(peer);
2674 } 2668 }
2675 } 2669 }
2676 cleanup: 2670 cleanup:
2677 spin_unlock_bh(&peer->lock); 2671 spin_unlock_bh(&peer->lock);
2678 if (port) 2672 if (port)
2679 fwserial_release_port(port, false); 2673 fwserial_release_port(port, false);
2680 kfree(pkt); 2674 kfree(pkt);
2681 return; 2675 return;
2682 } 2676 }
2683 2677
2684 static void fwserial_handle_unplug_req(struct work_struct *work) 2678 static void fwserial_handle_unplug_req(struct work_struct *work)
2685 { 2679 {
2686 struct fwtty_peer *peer = to_peer(work, work); 2680 struct fwtty_peer *peer = to_peer(work, work);
2687 struct fwtty_port *port = NULL; 2681 struct fwtty_port *port = NULL;
2688 struct fwserial_mgmt_pkt *pkt; 2682 struct fwserial_mgmt_pkt *pkt;
2689 int rcode; 2683 int rcode;
2690 2684
2691 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL); 2685 pkt = kmalloc(sizeof(*pkt), GFP_KERNEL);
2692 if (!pkt) 2686 if (!pkt)
2693 return; 2687 return;
2694 2688
2695 spin_lock_bh(&peer->lock); 2689 spin_lock_bh(&peer->lock);
2696 2690
2697 switch (peer->state) { 2691 switch (peer->state) {
2698 case FWPS_ATTACHED: 2692 case FWPS_ATTACHED:
2699 fill_unplug_rsp_ok(pkt); 2693 fill_unplug_rsp_ok(pkt);
2700 peer_set_state(peer, FWPS_UNPLUG_RESPONDING); 2694 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2701 break; 2695 break;
2702 2696
2703 case FWPS_UNPLUG_PENDING: 2697 case FWPS_UNPLUG_PENDING:
2704 if (peer->serial->card->guid > peer->guid) 2698 if (peer->serial->card->guid > peer->guid)
2705 goto cleanup; 2699 goto cleanup;
2706 2700
2707 /* We lost - send unplug rsp */ 2701 /* We lost - send unplug rsp */
2708 del_timer(&peer->timer); 2702 del_timer(&peer->timer);
2709 fill_unplug_rsp_ok(pkt); 2703 fill_unplug_rsp_ok(pkt);
2710 peer_set_state(peer, FWPS_UNPLUG_RESPONDING); 2704 peer_set_state(peer, FWPS_UNPLUG_RESPONDING);
2711 break; 2705 break;
2712 2706
2713 default: 2707 default:
2714 fill_unplug_rsp_nack(pkt); 2708 fill_unplug_rsp_nack(pkt);
2715 } 2709 }
2716 2710
2717 spin_unlock_bh(&peer->lock); 2711 spin_unlock_bh(&peer->lock);
2718 2712
2719 rcode = fwserial_send_mgmt_sync(peer, pkt); 2713 rcode = fwserial_send_mgmt_sync(peer, pkt);
2720 2714
2721 spin_lock_bh(&peer->lock); 2715 spin_lock_bh(&peer->lock);
2722 if (peer->state == FWPS_UNPLUG_RESPONDING) { 2716 if (peer->state == FWPS_UNPLUG_RESPONDING) {
2723 if (rcode != RCODE_COMPLETE) 2717 if (rcode != RCODE_COMPLETE)
2724 fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)", rcode); 2718 fwtty_err(&peer->unit, "UNPLUG_RSP error (%d)", rcode);
2725 port = peer_revert_state(peer); 2719 port = peer_revert_state(peer);
2726 } 2720 }
2727 cleanup: 2721 cleanup:
2728 spin_unlock_bh(&peer->lock); 2722 spin_unlock_bh(&peer->lock);
2729 if (port) 2723 if (port)
2730 fwserial_release_port(port, true); 2724 fwserial_release_port(port, true);
2731 kfree(pkt); 2725 kfree(pkt);
2732 return; 2726 return;
2733 } 2727 }
2734 2728
2735 static int fwserial_parse_mgmt_write(struct fwtty_peer *peer, 2729 static int fwserial_parse_mgmt_write(struct fwtty_peer *peer,
2736 struct fwserial_mgmt_pkt *pkt, 2730 struct fwserial_mgmt_pkt *pkt,
2737 unsigned long long addr, 2731 unsigned long long addr,
2738 size_t len) 2732 size_t len)
2739 { 2733 {
2740 struct fwtty_port *port = NULL; 2734 struct fwtty_port *port = NULL;
2741 bool reset = false; 2735 bool reset = false;
2742 int rcode; 2736 int rcode;
2743 2737
2744 if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr)) 2738 if (addr != fwserial_mgmt_addr_handler.offset || len < sizeof(pkt->hdr))
2745 return RCODE_ADDRESS_ERROR; 2739 return RCODE_ADDRESS_ERROR;
2746 2740
2747 if (len != be16_to_cpu(pkt->hdr.len) || 2741 if (len != be16_to_cpu(pkt->hdr.len) ||
2748 len != mgmt_pkt_expected_len(pkt->hdr.code)) 2742 len != mgmt_pkt_expected_len(pkt->hdr.code))
2749 return RCODE_DATA_ERROR; 2743 return RCODE_DATA_ERROR;
2750 2744
2751 spin_lock_bh(&peer->lock); 2745 spin_lock_bh(&peer->lock);
2752 if (peer->state == FWPS_GONE) { 2746 if (peer->state == FWPS_GONE) {
2753 /* 2747 /*
2754 * This should never happen - it would mean that the 2748 * This should never happen - it would mean that the
2755 * remote unit that just wrote this transaction was 2749 * remote unit that just wrote this transaction was
2756 * already removed from the bus -- and the removal was 2750 * already removed from the bus -- and the removal was
2757 * processed before we rec'd this transaction 2751 * processed before we rec'd this transaction
2758 */ 2752 */
2759 fwtty_err(&peer->unit, "peer already removed"); 2753 fwtty_err(&peer->unit, "peer already removed");
2760 spin_unlock_bh(&peer->lock); 2754 spin_unlock_bh(&peer->lock);
2761 return RCODE_ADDRESS_ERROR; 2755 return RCODE_ADDRESS_ERROR;
2762 } 2756 }
2763 2757
2764 rcode = RCODE_COMPLETE; 2758 rcode = RCODE_COMPLETE;
2765 2759
2766 fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04hx", pkt->hdr.code); 2760 fwtty_dbg(&peer->unit, "mgmt: hdr.code: %04hx", pkt->hdr.code);
2767 2761
2768 switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) { 2762 switch (be16_to_cpu(pkt->hdr.code) & FWSC_CODE_MASK) {
2769 case FWSC_VIRT_CABLE_PLUG: 2763 case FWSC_VIRT_CABLE_PLUG:
2770 if (work_pending(&peer->work)) { 2764 if (work_pending(&peer->work)) {
2771 fwtty_err(&peer->unit, "plug req: busy"); 2765 fwtty_err(&peer->unit, "plug req: busy");
2772 rcode = RCODE_CONFLICT_ERROR; 2766 rcode = RCODE_CONFLICT_ERROR;
2773 2767
2774 } else { 2768 } else {
2775 peer->work_params.plug_req = pkt->plug_req; 2769 peer->work_params.plug_req = pkt->plug_req;
2776 PREPARE_WORK(&peer->work, fwserial_handle_plug_req); 2770 PREPARE_WORK(&peer->work, fwserial_handle_plug_req);
2777 queue_work(system_unbound_wq, &peer->work); 2771 queue_work(system_unbound_wq, &peer->work);
2778 } 2772 }
2779 break; 2773 break;
2780 2774
2781 case FWSC_VIRT_CABLE_PLUG_RSP: 2775 case FWSC_VIRT_CABLE_PLUG_RSP:
2782 if (peer->state != FWPS_PLUG_PENDING) { 2776 if (peer->state != FWPS_PLUG_PENDING) {
2783 rcode = RCODE_CONFLICT_ERROR; 2777 rcode = RCODE_CONFLICT_ERROR;
2784 2778
2785 } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) { 2779 } else if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) {
2786 fwtty_notice(&peer->unit, "NACK plug rsp"); 2780 fwtty_notice(&peer->unit, "NACK plug rsp");
2787 port = peer_revert_state(peer); 2781 port = peer_revert_state(peer);
2788 2782
2789 } else { 2783 } else {
2790 struct fwtty_port *tmp = peer->port; 2784 struct fwtty_port *tmp = peer->port;
2791 2785
2792 fwserial_virt_plug_complete(peer, &pkt->plug_rsp); 2786 fwserial_virt_plug_complete(peer, &pkt->plug_rsp);
2793 spin_unlock_bh(&peer->lock); 2787 spin_unlock_bh(&peer->lock);
2794 2788
2795 fwtty_write_port_status(tmp); 2789 fwtty_write_port_status(tmp);
2796 spin_lock_bh(&peer->lock); 2790 spin_lock_bh(&peer->lock);
2797 } 2791 }
2798 break; 2792 break;
2799 2793
2800 case FWSC_VIRT_CABLE_UNPLUG: 2794 case FWSC_VIRT_CABLE_UNPLUG:
2801 if (work_pending(&peer->work)) { 2795 if (work_pending(&peer->work)) {
2802 fwtty_err(&peer->unit, "unplug req: busy"); 2796 fwtty_err(&peer->unit, "unplug req: busy");
2803 rcode = RCODE_CONFLICT_ERROR; 2797 rcode = RCODE_CONFLICT_ERROR;
2804 } else { 2798 } else {
2805 PREPARE_WORK(&peer->work, fwserial_handle_unplug_req); 2799 PREPARE_WORK(&peer->work, fwserial_handle_unplug_req);
2806 queue_work(system_unbound_wq, &peer->work); 2800 queue_work(system_unbound_wq, &peer->work);
2807 } 2801 }
2808 break; 2802 break;
2809 2803
2810 case FWSC_VIRT_CABLE_UNPLUG_RSP: 2804 case FWSC_VIRT_CABLE_UNPLUG_RSP:
2811 if (peer->state != FWPS_UNPLUG_PENDING) 2805 if (peer->state != FWPS_UNPLUG_PENDING)
2812 rcode = RCODE_CONFLICT_ERROR; 2806 rcode = RCODE_CONFLICT_ERROR;
2813 else { 2807 else {
2814 if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK) 2808 if (be16_to_cpu(pkt->hdr.code) & FWSC_RSP_NACK)
2815 fwtty_notice(&peer->unit, "NACK unplug?"); 2809 fwtty_notice(&peer->unit, "NACK unplug?");
2816 port = peer_revert_state(peer); 2810 port = peer_revert_state(peer);
2817 reset = true; 2811 reset = true;
2818 } 2812 }
2819 break; 2813 break;
2820 2814
2821 default: 2815 default:
2822 fwtty_err(&peer->unit, "unknown mgmt code %d", 2816 fwtty_err(&peer->unit, "unknown mgmt code %d",
2823 be16_to_cpu(pkt->hdr.code)); 2817 be16_to_cpu(pkt->hdr.code));
2824 rcode = RCODE_DATA_ERROR; 2818 rcode = RCODE_DATA_ERROR;
2825 } 2819 }
2826 spin_unlock_bh(&peer->lock); 2820 spin_unlock_bh(&peer->lock);
2827 2821
2828 if (port) 2822 if (port)
2829 fwserial_release_port(port, reset); 2823 fwserial_release_port(port, reset);
2830 2824
2831 return rcode; 2825 return rcode;
2832 } 2826 }
2833 2827
2834 /** 2828 /**
2835 * fwserial_mgmt_handler: bus address handler for mgmt requests 2829 * fwserial_mgmt_handler: bus address handler for mgmt requests
2836 * @parameters: fw_address_callback_t as specified by firewire core interface 2830 * @parameters: fw_address_callback_t as specified by firewire core interface
2837 * 2831 *
2838 * This handler is responsible for handling virtual cable requests from remotes 2832 * This handler is responsible for handling virtual cable requests from remotes
2839 * for all cards. 2833 * for all cards.
2840 */ 2834 */
2841 static void fwserial_mgmt_handler(struct fw_card *card, 2835 static void fwserial_mgmt_handler(struct fw_card *card,
2842 struct fw_request *request, 2836 struct fw_request *request,
2843 int tcode, int destination, int source, 2837 int tcode, int destination, int source,
2844 int generation, 2838 int generation,
2845 unsigned long long addr, 2839 unsigned long long addr,
2846 void *data, size_t len, 2840 void *data, size_t len,
2847 void *callback_data) 2841 void *callback_data)
2848 { 2842 {
2849 struct fwserial_mgmt_pkt *pkt = data; 2843 struct fwserial_mgmt_pkt *pkt = data;
2850 struct fwtty_peer *peer; 2844 struct fwtty_peer *peer;
2851 int rcode; 2845 int rcode;
2852 2846
2853 rcu_read_lock(); 2847 rcu_read_lock();
2854 peer = __fwserial_peer_by_node_id(card, generation, source); 2848 peer = __fwserial_peer_by_node_id(card, generation, source);
2855 if (!peer) { 2849 if (!peer) {
2856 fwtty_dbg(card, "peer(%d:%x) not found", generation, source); 2850 fwtty_dbg(card, "peer(%d:%x) not found", generation, source);
2857 __dump_peer_list(card); 2851 __dump_peer_list(card);
2858 rcode = RCODE_CONFLICT_ERROR; 2852 rcode = RCODE_CONFLICT_ERROR;
2859 2853
2860 } else { 2854 } else {
2861 switch (tcode) { 2855 switch (tcode) {
2862 case TCODE_WRITE_BLOCK_REQUEST: 2856 case TCODE_WRITE_BLOCK_REQUEST:
2863 rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len); 2857 rcode = fwserial_parse_mgmt_write(peer, pkt, addr, len);
2864 break; 2858 break;
2865 2859
2866 default: 2860 default:
2867 rcode = RCODE_TYPE_ERROR; 2861 rcode = RCODE_TYPE_ERROR;
2868 } 2862 }
2869 } 2863 }
2870 2864
2871 rcu_read_unlock(); 2865 rcu_read_unlock();
2872 fw_send_response(card, request, rcode); 2866 fw_send_response(card, request, rcode);
2873 } 2867 }
2874 2868
2875 static int __init fwserial_init(void) 2869 static int __init fwserial_init(void)
2876 { 2870 {
2877 int err, num_loops = !!(create_loop_dev); 2871 int err, num_loops = !!(create_loop_dev);
2878 2872
2879 /* XXX: placeholder for a "firewire" debugfs node */ 2873 /* XXX: placeholder for a "firewire" debugfs node */
2880 fwserial_debugfs = debugfs_create_dir(KBUILD_MODNAME, NULL); 2874 fwserial_debugfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
2881 2875
2882 /* num_ttys/num_ports must not be set above the static alloc avail */ 2876 /* num_ttys/num_ports must not be set above the static alloc avail */
2883 if (num_ttys + num_loops > MAX_CARD_PORTS) 2877 if (num_ttys + num_loops > MAX_CARD_PORTS)
2884 num_ttys = MAX_CARD_PORTS - num_loops; 2878 num_ttys = MAX_CARD_PORTS - num_loops;
2885 num_ports = num_ttys + num_loops; 2879 num_ports = num_ttys + num_loops;
2886 2880
2887 fwtty_driver = tty_alloc_driver(MAX_TOTAL_PORTS, TTY_DRIVER_REAL_RAW 2881 fwtty_driver = tty_alloc_driver(MAX_TOTAL_PORTS, TTY_DRIVER_REAL_RAW
2888 | TTY_DRIVER_DYNAMIC_DEV); 2882 | TTY_DRIVER_DYNAMIC_DEV);
2889 if (IS_ERR(fwtty_driver)) { 2883 if (IS_ERR(fwtty_driver)) {
2890 err = PTR_ERR(fwtty_driver); 2884 err = PTR_ERR(fwtty_driver);
2891 return err; 2885 return err;
2892 } 2886 }
2893 2887
2894 fwtty_driver->driver_name = KBUILD_MODNAME; 2888 fwtty_driver->driver_name = KBUILD_MODNAME;
2895 fwtty_driver->name = tty_dev_name; 2889 fwtty_driver->name = tty_dev_name;
2896 fwtty_driver->major = 0; 2890 fwtty_driver->major = 0;
2897 fwtty_driver->minor_start = 0; 2891 fwtty_driver->minor_start = 0;
2898 fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL; 2892 fwtty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2899 fwtty_driver->subtype = SERIAL_TYPE_NORMAL; 2893 fwtty_driver->subtype = SERIAL_TYPE_NORMAL;
2900 fwtty_driver->init_termios = tty_std_termios; 2894 fwtty_driver->init_termios = tty_std_termios;
2901 fwtty_driver->init_termios.c_cflag |= CLOCAL; 2895 fwtty_driver->init_termios.c_cflag |= CLOCAL;
2902 tty_set_operations(fwtty_driver, &fwtty_ops); 2896 tty_set_operations(fwtty_driver, &fwtty_ops);
2903 2897
2904 err = tty_register_driver(fwtty_driver); 2898 err = tty_register_driver(fwtty_driver);
2905 if (err) { 2899 if (err) {
2906 driver_err("register tty driver failed (%d)", err); 2900 driver_err("register tty driver failed (%d)", err);
2907 goto put_tty; 2901 goto put_tty;
2908 } 2902 }
2909 2903
2910 if (create_loop_dev) { 2904 if (create_loop_dev) {
2911 fwloop_driver = tty_alloc_driver(MAX_TOTAL_PORTS / num_ports, 2905 fwloop_driver = tty_alloc_driver(MAX_TOTAL_PORTS / num_ports,
2912 TTY_DRIVER_REAL_RAW 2906 TTY_DRIVER_REAL_RAW
2913 | TTY_DRIVER_DYNAMIC_DEV); 2907 | TTY_DRIVER_DYNAMIC_DEV);
2914 if (IS_ERR(fwloop_driver)) { 2908 if (IS_ERR(fwloop_driver)) {
2915 err = PTR_ERR(fwloop_driver); 2909 err = PTR_ERR(fwloop_driver);
2916 goto unregister_driver; 2910 goto unregister_driver;
2917 } 2911 }
2918 2912
2919 fwloop_driver->driver_name = KBUILD_MODNAME "_loop"; 2913 fwloop_driver->driver_name = KBUILD_MODNAME "_loop";
2920 fwloop_driver->name = loop_dev_name; 2914 fwloop_driver->name = loop_dev_name;
2921 fwloop_driver->major = 0; 2915 fwloop_driver->major = 0;
2922 fwloop_driver->minor_start = 0; 2916 fwloop_driver->minor_start = 0;
2923 fwloop_driver->type = TTY_DRIVER_TYPE_SERIAL; 2917 fwloop_driver->type = TTY_DRIVER_TYPE_SERIAL;
2924 fwloop_driver->subtype = SERIAL_TYPE_NORMAL; 2918 fwloop_driver->subtype = SERIAL_TYPE_NORMAL;
2925 fwloop_driver->init_termios = tty_std_termios; 2919 fwloop_driver->init_termios = tty_std_termios;
2926 fwloop_driver->init_termios.c_cflag |= CLOCAL; 2920 fwloop_driver->init_termios.c_cflag |= CLOCAL;
2927 tty_set_operations(fwloop_driver, &fwloop_ops); 2921 tty_set_operations(fwloop_driver, &fwloop_ops);
2928 2922
2929 err = tty_register_driver(fwloop_driver); 2923 err = tty_register_driver(fwloop_driver);
2930 if (err) { 2924 if (err) {
2931 driver_err("register loop driver failed (%d)", err); 2925 driver_err("register loop driver failed (%d)", err);
2932 goto put_loop; 2926 goto put_loop;
2933 } 2927 }
2934 } 2928 }
2935 2929
2936 fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache", 2930 fwtty_txn_cache = kmem_cache_create("fwtty_txn_cache",
2937 sizeof(struct fwtty_transaction), 2931 sizeof(struct fwtty_transaction),
2938 0, 0, fwtty_txn_constructor); 2932 0, 0, fwtty_txn_constructor);
2939 if (!fwtty_txn_cache) { 2933 if (!fwtty_txn_cache) {
2940 err = -ENOMEM; 2934 err = -ENOMEM;
2941 goto unregister_loop; 2935 goto unregister_loop;
2942 } 2936 }
2943 2937
2944 /* 2938 /*
2945 * Ideally, this address handler would be registered per local node 2939 * Ideally, this address handler would be registered per local node
2946 * (rather than the same handler for all local nodes). However, 2940 * (rather than the same handler for all local nodes). However,
2947 * since the firewire core requires the config rom descriptor *before* 2941 * since the firewire core requires the config rom descriptor *before*
2948 * the local unit device(s) are created, a single management handler 2942 * the local unit device(s) are created, a single management handler
2949 * must suffice for all local serial units. 2943 * must suffice for all local serial units.
2950 */ 2944 */
2951 fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt); 2945 fwserial_mgmt_addr_handler.length = sizeof(struct fwserial_mgmt_pkt);
2952 fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler; 2946 fwserial_mgmt_addr_handler.address_callback = fwserial_mgmt_handler;
2953 2947
2954 err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler, 2948 err = fw_core_add_address_handler(&fwserial_mgmt_addr_handler,
2955 &fwserial_mgmt_addr_region); 2949 &fwserial_mgmt_addr_region);
2956 if (err) { 2950 if (err) {
2957 driver_err("add management handler failed (%d)", err); 2951 driver_err("add management handler failed (%d)", err);
2958 goto destroy_cache; 2952 goto destroy_cache;
2959 } 2953 }
2960 2954
2961 fwserial_unit_directory_data.unit_addr_offset = 2955 fwserial_unit_directory_data.unit_addr_offset =
2962 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset); 2956 FW_UNIT_ADDRESS(fwserial_mgmt_addr_handler.offset);
2963 err = fw_core_add_descriptor(&fwserial_unit_directory); 2957 err = fw_core_add_descriptor(&fwserial_unit_directory);
2964 if (err) { 2958 if (err) {
2965 driver_err("add unit descriptor failed (%d)", err); 2959 driver_err("add unit descriptor failed (%d)", err);
2966 goto remove_handler; 2960 goto remove_handler;
2967 } 2961 }
2968 2962
2969 err = driver_register(&fwserial_driver.driver); 2963 err = driver_register(&fwserial_driver.driver);
2970 if (err) { 2964 if (err) {
2971 driver_err("register fwserial driver failed (%d)", err); 2965 driver_err("register fwserial driver failed (%d)", err);
2972 goto remove_descriptor; 2966 goto remove_descriptor;
2973 } 2967 }
2974 2968
2975 return 0; 2969 return 0;
2976 2970
2977 remove_descriptor: 2971 remove_descriptor:
2978 fw_core_remove_descriptor(&fwserial_unit_directory); 2972 fw_core_remove_descriptor(&fwserial_unit_directory);
2979 remove_handler: 2973 remove_handler:
2980 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler); 2974 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
2981 destroy_cache: 2975 destroy_cache:
2982 kmem_cache_destroy(fwtty_txn_cache); 2976 kmem_cache_destroy(fwtty_txn_cache);
2983 unregister_loop: 2977 unregister_loop:
2984 if (create_loop_dev) 2978 if (create_loop_dev)
2985 tty_unregister_driver(fwloop_driver); 2979 tty_unregister_driver(fwloop_driver);
2986 put_loop: 2980 put_loop:
2987 if (create_loop_dev) 2981 if (create_loop_dev)
2988 put_tty_driver(fwloop_driver); 2982 put_tty_driver(fwloop_driver);
2989 unregister_driver: 2983 unregister_driver:
2990 tty_unregister_driver(fwtty_driver); 2984 tty_unregister_driver(fwtty_driver);
2991 put_tty: 2985 put_tty:
2992 put_tty_driver(fwtty_driver); 2986 put_tty_driver(fwtty_driver);
2993 debugfs_remove_recursive(fwserial_debugfs); 2987 debugfs_remove_recursive(fwserial_debugfs);
2994 return err; 2988 return err;
2995 } 2989 }
2996 2990
2997 static void __exit fwserial_exit(void) 2991 static void __exit fwserial_exit(void)
2998 { 2992 {
2999 driver_unregister(&fwserial_driver.driver); 2993 driver_unregister(&fwserial_driver.driver);
3000 fw_core_remove_descriptor(&fwserial_unit_directory); 2994 fw_core_remove_descriptor(&fwserial_unit_directory);
3001 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler); 2995 fw_core_remove_address_handler(&fwserial_mgmt_addr_handler);
3002 kmem_cache_destroy(fwtty_txn_cache); 2996 kmem_cache_destroy(fwtty_txn_cache);
3003 if (create_loop_dev) { 2997 if (create_loop_dev) {
3004 tty_unregister_driver(fwloop_driver); 2998 tty_unregister_driver(fwloop_driver);
3005 put_tty_driver(fwloop_driver); 2999 put_tty_driver(fwloop_driver);
3006 } 3000 }
3007 tty_unregister_driver(fwtty_driver); 3001 tty_unregister_driver(fwtty_driver);
3008 put_tty_driver(fwtty_driver); 3002 put_tty_driver(fwtty_driver);
3009 debugfs_remove_recursive(fwserial_debugfs); 3003 debugfs_remove_recursive(fwserial_debugfs);
3010 } 3004 }
3011 3005
3012 module_init(fwserial_init); 3006 module_init(fwserial_init);
3013 module_exit(fwserial_exit); 3007 module_exit(fwserial_exit);
3014 3008
3015 MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)"); 3009 MODULE_AUTHOR("Peter Hurley (peter@hurleysoftware.com)");
3016 MODULE_DESCRIPTION("FireWire Serial TTY Driver"); 3010 MODULE_DESCRIPTION("FireWire Serial TTY Driver");
3017 MODULE_LICENSE("GPL"); 3011 MODULE_LICENSE("GPL");
3018 MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table); 3012 MODULE_DEVICE_TABLE(ieee1394, fwserial_id_table);
3019 MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node"); 3013 MODULE_PARM_DESC(ttys, "Number of ttys to create for each local firewire node");
3020 MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered"); 3014 MODULE_PARM_DESC(auto, "Auto-connect a tty to each firewire node discovered");
3021 MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys"); 3015 MODULE_PARM_DESC(loop, "Create a loopback device, fwloop<n>, with ttys");
3022 3016
drivers/staging/serqt_usb2/serqt_usb2.c
1 /* 1 /*
2 * This code was developed for the Quatech USB line for linux, it used 2 * This code was developed for the Quatech USB line for linux, it used
3 * much of the code developed by Greg Kroah-Hartman for USB serial devices 3 * much of the code developed by Greg Kroah-Hartman for USB serial devices
4 * 4 *
5 */ 5 */
6 6
7 #include <linux/errno.h> 7 #include <linux/errno.h>
8 #include <linux/init.h> 8 #include <linux/init.h>
9 #include <linux/slab.h> 9 #include <linux/slab.h>
10 #include <linux/tty.h> 10 #include <linux/tty.h>
11 #include <linux/tty_driver.h> 11 #include <linux/tty_driver.h>
12 #include <linux/tty_flip.h> 12 #include <linux/tty_flip.h>
13 #include <linux/module.h> 13 #include <linux/module.h>
14 #include <linux/serial.h> 14 #include <linux/serial.h>
15 #include <linux/usb.h> 15 #include <linux/usb.h>
16 #include <linux/usb/serial.h> 16 #include <linux/usb/serial.h>
17 #include <linux/uaccess.h> 17 #include <linux/uaccess.h>
18 18
19 /* Version Information */ 19 /* Version Information */
20 #define DRIVER_VERSION "v2.14" 20 #define DRIVER_VERSION "v2.14"
21 #define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc" 21 #define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc"
22 #define DRIVER_DESC "Quatech USB to Serial Driver" 22 #define DRIVER_DESC "Quatech USB to Serial Driver"
23 23
24 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */ 24 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
25 #define QUATECH_SSU200 0xC030 /* SSU200 */ 25 #define QUATECH_SSU200 0xC030 /* SSU200 */
26 #define QUATECH_DSU100 0xC040 /* DSU100 */ 26 #define QUATECH_DSU100 0xC040 /* DSU100 */
27 #define QUATECH_DSU200 0xC050 /* DSU200 */ 27 #define QUATECH_DSU200 0xC050 /* DSU200 */
28 #define QUATECH_QSU100 0xC060 /* QSU100 */ 28 #define QUATECH_QSU100 0xC060 /* QSU100 */
29 #define QUATECH_QSU200 0xC070 /* QSU200 */ 29 #define QUATECH_QSU200 0xC070 /* QSU200 */
30 #define QUATECH_ESU100A 0xC080 /* ESU100A */ 30 #define QUATECH_ESU100A 0xC080 /* ESU100A */
31 #define QUATECH_ESU100B 0xC081 /* ESU100B */ 31 #define QUATECH_ESU100B 0xC081 /* ESU100B */
32 #define QUATECH_ESU200A 0xC0A0 /* ESU200A */ 32 #define QUATECH_ESU200A 0xC0A0 /* ESU200A */
33 #define QUATECH_ESU200B 0xC0A1 /* ESU200B */ 33 #define QUATECH_ESU200B 0xC0A1 /* ESU200B */
34 #define QUATECH_HSU100A 0xC090 /* HSU100A */ 34 #define QUATECH_HSU100A 0xC090 /* HSU100A */
35 #define QUATECH_HSU100B 0xC091 /* HSU100B */ 35 #define QUATECH_HSU100B 0xC091 /* HSU100B */
36 #define QUATECH_HSU100C 0xC092 /* HSU100C */ 36 #define QUATECH_HSU100C 0xC092 /* HSU100C */
37 #define QUATECH_HSU100D 0xC093 /* HSU100D */ 37 #define QUATECH_HSU100D 0xC093 /* HSU100D */
38 #define QUATECH_HSU200A 0xC0B0 /* HSU200A */ 38 #define QUATECH_HSU200A 0xC0B0 /* HSU200A */
39 #define QUATECH_HSU200B 0xC0B1 /* HSU200B */ 39 #define QUATECH_HSU200B 0xC0B1 /* HSU200B */
40 #define QUATECH_HSU200C 0xC0B2 /* HSU200C */ 40 #define QUATECH_HSU200C 0xC0B2 /* HSU200C */
41 #define QUATECH_HSU200D 0xC0B3 /* HSU200D */ 41 #define QUATECH_HSU200D 0xC0B3 /* HSU200D */
42 42
43 #define QT_SET_GET_DEVICE 0xc2 43 #define QT_SET_GET_DEVICE 0xc2
44 #define QT_OPEN_CLOSE_CHANNEL 0xca 44 #define QT_OPEN_CLOSE_CHANNEL 0xca
45 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc 45 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
46 #define QT_SET_ATF 0xcd 46 #define QT_SET_ATF 0xcd
47 #define QT_GET_SET_REGISTER 0xc0 47 #define QT_GET_SET_REGISTER 0xc0
48 #define QT_GET_SET_UART 0xc1 48 #define QT_GET_SET_UART 0xc1
49 #define QT_HW_FLOW_CONTROL_MASK 0xc5 49 #define QT_HW_FLOW_CONTROL_MASK 0xc5
50 #define QT_SW_FLOW_CONTROL_MASK 0xc6 50 #define QT_SW_FLOW_CONTROL_MASK 0xc6
51 #define QT_SW_FLOW_CONTROL_DISABLE 0xc7 51 #define QT_SW_FLOW_CONTROL_DISABLE 0xc7
52 #define QT_BREAK_CONTROL 0xc8 52 #define QT_BREAK_CONTROL 0xc8
53 53
54 #define USBD_TRANSFER_DIRECTION_IN 0xc0 54 #define USBD_TRANSFER_DIRECTION_IN 0xc0
55 #define USBD_TRANSFER_DIRECTION_OUT 0x40 55 #define USBD_TRANSFER_DIRECTION_OUT 0x40
56 56
57 #define MAX_BAUD_RATE 460800 57 #define MAX_BAUD_RATE 460800
58 #define MAX_BAUD_REMAINDER 4608 58 #define MAX_BAUD_REMAINDER 4608
59 59
60 #define DIV_LATCH_LS 0x00 60 #define DIV_LATCH_LS 0x00
61 #define XMT_HOLD_REGISTER 0x00 61 #define XMT_HOLD_REGISTER 0x00
62 #define XVR_BUFFER_REGISTER 0x00 62 #define XVR_BUFFER_REGISTER 0x00
63 #define DIV_LATCH_MS 0x01 63 #define DIV_LATCH_MS 0x01
64 #define FIFO_CONTROL_REGISTER 0x02 64 #define FIFO_CONTROL_REGISTER 0x02
65 #define LINE_CONTROL_REGISTER 0x03 65 #define LINE_CONTROL_REGISTER 0x03
66 #define MODEM_CONTROL_REGISTER 0x04 66 #define MODEM_CONTROL_REGISTER 0x04
67 #define LINE_STATUS_REGISTER 0x05 67 #define LINE_STATUS_REGISTER 0x05
68 #define MODEM_STATUS_REGISTER 0x06 68 #define MODEM_STATUS_REGISTER 0x06
69 69
70 #define SERIAL_MCR_DTR 0x01 70 #define SERIAL_MCR_DTR 0x01
71 #define SERIAL_MCR_RTS 0x02 71 #define SERIAL_MCR_RTS 0x02
72 #define SERIAL_MCR_LOOP 0x10 72 #define SERIAL_MCR_LOOP 0x10
73 73
74 #define SERIAL_MSR_CTS 0x10 74 #define SERIAL_MSR_CTS 0x10
75 #define SERIAL_MSR_CD 0x80 75 #define SERIAL_MSR_CD 0x80
76 #define SERIAL_MSR_RI 0x40 76 #define SERIAL_MSR_RI 0x40
77 #define SERIAL_MSR_DSR 0x20 77 #define SERIAL_MSR_DSR 0x20
78 #define SERIAL_MSR_MASK 0xf0 78 #define SERIAL_MSR_MASK 0xf0
79 79
80 #define SERIAL_8_DATA 0x03 80 #define SERIAL_8_DATA 0x03
81 #define SERIAL_7_DATA 0x02 81 #define SERIAL_7_DATA 0x02
82 #define SERIAL_6_DATA 0x01 82 #define SERIAL_6_DATA 0x01
83 #define SERIAL_5_DATA 0x00 83 #define SERIAL_5_DATA 0x00
84 84
85 #define SERIAL_ODD_PARITY 0X08 85 #define SERIAL_ODD_PARITY 0X08
86 #define SERIAL_EVEN_PARITY 0X18 86 #define SERIAL_EVEN_PARITY 0X18
87 #define SERIAL_TWO_STOPB 0x04 87 #define SERIAL_TWO_STOPB 0x04
88 #define SERIAL_ONE_STOPB 0x00 88 #define SERIAL_ONE_STOPB 0x00
89 89
90 #define DEFAULT_DIVISOR 0x30 /* gives 9600 baud rate */ 90 #define DEFAULT_DIVISOR 0x30 /* gives 9600 baud rate */
91 #define DEFAULT_LCR SERIAL_8_DATA /* 8, none , 1 */ 91 #define DEFAULT_LCR SERIAL_8_DATA /* 8, none , 1 */
92 92
93 #define FULLPWRBIT 0x00000080 93 #define FULLPWRBIT 0x00000080
94 #define NEXT_BOARD_POWER_BIT 0x00000004 94 #define NEXT_BOARD_POWER_BIT 0x00000004
95 95
96 #define SERIAL_LSR_OE 0x02 96 #define SERIAL_LSR_OE 0x02
97 #define SERIAL_LSR_PE 0x04 97 #define SERIAL_LSR_PE 0x04
98 #define SERIAL_LSR_FE 0x08 98 #define SERIAL_LSR_FE 0x08
99 #define SERIAL_LSR_BI 0x10 99 #define SERIAL_LSR_BI 0x10
100 100
101 #define SERIAL_MSR_CTS 0x10 101 #define SERIAL_MSR_CTS 0x10
102 #define SERIAL_MSR_CD 0x80 102 #define SERIAL_MSR_CD 0x80
103 #define SERIAL_MSR_RI 0x40 103 #define SERIAL_MSR_RI 0x40
104 #define SERIAL_MSR_DSR 0x20 104 #define SERIAL_MSR_DSR 0x20
105 #define SERIAL_MSR_MASK 0xf0 105 #define SERIAL_MSR_MASK 0xf0
106 106
107 #define PREFUFF_LEVEL_CONSERVATIVE 128 107 #define PREFUFF_LEVEL_CONSERVATIVE 128
108 #define ATC_DISABLED 0x0 108 #define ATC_DISABLED 0x0
109 109
110 #define RR_BITS 0x03 /* for clearing clock bits */ 110 #define RR_BITS 0x03 /* for clearing clock bits */
111 #define DUPMODE_BITS 0xc0 111 #define DUPMODE_BITS 0xc0
112 #define CLKS_X4 0x02 112 #define CLKS_X4 0x02
113 113
114 #define LOOPMODE_BITS 0x41 /* LOOP1 = b6, LOOP0 = b0 (PORT B) */ 114 #define LOOPMODE_BITS 0x41 /* LOOP1 = b6, LOOP0 = b0 (PORT B) */
115 #define ALL_LOOPBACK 0x01 115 #define ALL_LOOPBACK 0x01
116 #define MODEM_CTRL 0x40 116 #define MODEM_CTRL 0x40
117 #define RS232_MODE 0x00 117 #define RS232_MODE 0x00
118 118
119 static const struct usb_device_id id_table[] = { 119 static const struct usb_device_id id_table[] = {
120 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU200)}, 120 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU200)},
121 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU100)}, 121 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU100)},
122 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU200)}, 122 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU200)},
123 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU100)}, 123 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU100)},
124 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU200)}, 124 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU200)},
125 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100A)}, 125 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100A)},
126 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100B)}, 126 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU100B)},
127 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200A)}, 127 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200A)},
128 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200B)}, 128 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU200B)},
129 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100A)}, 129 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100A)},
130 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100B)}, 130 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100B)},
131 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100C)}, 131 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100C)},
132 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100D)}, 132 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU100D)},
133 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200A)}, 133 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200A)},
134 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200B)}, 134 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200B)},
135 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200C)}, 135 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200C)},
136 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200D)}, 136 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_HSU200D)},
137 {} /* Terminating entry */ 137 {} /* Terminating entry */
138 }; 138 };
139 MODULE_DEVICE_TABLE(usb, id_table); 139 MODULE_DEVICE_TABLE(usb, id_table);
140 140
141 struct qt_get_device_data { 141 struct qt_get_device_data {
142 __u8 porta; 142 __u8 porta;
143 __u8 portb; 143 __u8 portb;
144 __u8 portc; 144 __u8 portc;
145 }; 145 };
146 146
147 struct qt_open_channel_data { 147 struct qt_open_channel_data {
148 __u8 line_status; 148 __u8 line_status;
149 __u8 modem_status; 149 __u8 modem_status;
150 }; 150 };
151 151
152 struct quatech_port { 152 struct quatech_port {
153 int port_num; /* number of the port */ 153 int port_num; /* number of the port */
154 struct urb *write_urb; /* write URB for this port */ 154 struct urb *write_urb; /* write URB for this port */
155 struct urb *read_urb; /* read URB for this port */ 155 struct urb *read_urb; /* read URB for this port */
156 struct urb *int_urb; 156 struct urb *int_urb;
157 157
158 __u8 shadowLCR; /* last LCR value received */ 158 __u8 shadowLCR; /* last LCR value received */
159 __u8 shadowMCR; /* last MCR value received */ 159 __u8 shadowMCR; /* last MCR value received */
160 __u8 shadowMSR; /* last MSR value received */ 160 __u8 shadowMSR; /* last MSR value received */
161 __u8 shadowLSR; /* last LSR value received */ 161 __u8 shadowLSR; /* last LSR value received */
162 char open_ports; 162 char open_ports;
163 163
164 /* Used for TIOCMIWAIT */ 164 /* Used for TIOCMIWAIT */
165 wait_queue_head_t msr_wait; 165 wait_queue_head_t msr_wait;
166 char prev_status, diff_status; 166 char prev_status, diff_status;
167 167
168 wait_queue_head_t wait; 168 wait_queue_head_t wait;
169 169
170 struct async_icount icount; 170 struct async_icount icount;
171 171
172 struct usb_serial_port *port; /* owner of this object */ 172 struct usb_serial_port *port; /* owner of this object */
173 struct qt_get_device_data DeviceData; 173 struct qt_get_device_data DeviceData;
174 struct mutex lock; 174 struct mutex lock;
175 bool read_urb_busy; 175 bool read_urb_busy;
176 int RxHolding; 176 int RxHolding;
177 int ReadBulkStopped; 177 int ReadBulkStopped;
178 char closePending; 178 char closePending;
179 }; 179 };
180 180
181 static int port_paranoia_check(struct usb_serial_port *port, 181 static int port_paranoia_check(struct usb_serial_port *port,
182 const char *function) 182 const char *function)
183 { 183 {
184 if (!port) { 184 if (!port) {
185 pr_debug("%s - port == NULL", function); 185 pr_debug("%s - port == NULL", function);
186 return -1; 186 return -1;
187 } 187 }
188 if (!port->serial) { 188 if (!port->serial) {
189 pr_debug("%s - port->serial == NULL\n", function); 189 pr_debug("%s - port->serial == NULL\n", function);
190 return -1; 190 return -1;
191 } 191 }
192 192
193 return 0; 193 return 0;
194 } 194 }
195 195
196 static int serial_paranoia_check(struct usb_serial *serial, 196 static int serial_paranoia_check(struct usb_serial *serial,
197 const char *function) 197 const char *function)
198 { 198 {
199 if (!serial) { 199 if (!serial) {
200 pr_debug("%s - serial == NULL\n", function); 200 pr_debug("%s - serial == NULL\n", function);
201 return -1; 201 return -1;
202 } 202 }
203 203
204 if (!serial->type) { 204 if (!serial->type) {
205 pr_debug("%s - serial->type == NULL!", function); 205 pr_debug("%s - serial->type == NULL!", function);
206 return -1; 206 return -1;
207 } 207 }
208 208
209 return 0; 209 return 0;
210 } 210 }
211 211
212 static inline struct quatech_port *qt_get_port_private(struct usb_serial_port 212 static inline struct quatech_port *qt_get_port_private(struct usb_serial_port
213 *port) 213 *port)
214 { 214 {
215 return (struct quatech_port *)usb_get_serial_port_data(port); 215 return (struct quatech_port *)usb_get_serial_port_data(port);
216 } 216 }
217 217
218 static inline void qt_set_port_private(struct usb_serial_port *port, 218 static inline void qt_set_port_private(struct usb_serial_port *port,
219 struct quatech_port *data) 219 struct quatech_port *data)
220 { 220 {
221 usb_set_serial_port_data(port, (void *)data); 221 usb_set_serial_port_data(port, (void *)data);
222 } 222 }
223 223
224 static struct usb_serial *get_usb_serial(struct usb_serial_port *port, 224 static struct usb_serial *get_usb_serial(struct usb_serial_port *port,
225 const char *function) 225 const char *function)
226 { 226 {
227 /* if no port was specified, or it fails a paranoia check */ 227 /* if no port was specified, or it fails a paranoia check */
228 if (!port || 228 if (!port ||
229 port_paranoia_check(port, function) || 229 port_paranoia_check(port, function) ||
230 serial_paranoia_check(port->serial, function)) { 230 serial_paranoia_check(port->serial, function)) {
231 /* 231 /*
232 * then say that we dont have a valid usb_serial thing, 232 * then say that we dont have a valid usb_serial thing,
233 * which will end up genrating -ENODEV return values 233 * which will end up genrating -ENODEV return values
234 */ 234 */
235 return NULL; 235 return NULL;
236 } 236 }
237 237
238 return port->serial; 238 return port->serial;
239 } 239 }
240 240
241 static void ProcessLineStatus(struct quatech_port *qt_port, 241 static void ProcessLineStatus(struct quatech_port *qt_port,
242 unsigned char line_status) 242 unsigned char line_status)
243 { 243 {
244 244
245 qt_port->shadowLSR = 245 qt_port->shadowLSR =
246 line_status & (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE | 246 line_status & (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE |
247 SERIAL_LSR_BI); 247 SERIAL_LSR_BI);
248 } 248 }
249 249
250 static void ProcessModemStatus(struct quatech_port *qt_port, 250 static void ProcessModemStatus(struct quatech_port *qt_port,
251 unsigned char modem_status) 251 unsigned char modem_status)
252 { 252 {
253 253
254 qt_port->shadowMSR = modem_status; 254 qt_port->shadowMSR = modem_status;
255 wake_up_interruptible(&qt_port->wait); 255 wake_up_interruptible(&qt_port->wait);
256 } 256 }
257 257
258 static void ProcessRxChar(struct usb_serial_port *port, unsigned char data) 258 static void ProcessRxChar(struct usb_serial_port *port, unsigned char data)
259 { 259 {
260 struct urb *urb = port->read_urb; 260 struct urb *urb = port->read_urb;
261 if (urb->actual_length) 261 if (urb->actual_length)
262 tty_insert_flip_char(&port->port, data, TTY_NORMAL); 262 tty_insert_flip_char(&port->port, data, TTY_NORMAL);
263 } 263 }
264 264
265 static void qt_write_bulk_callback(struct urb *urb) 265 static void qt_write_bulk_callback(struct urb *urb)
266 { 266 {
267 struct tty_struct *tty;
268 int status; 267 int status;
269 struct quatech_port *quatech_port; 268 struct quatech_port *quatech_port;
270 269
271 status = urb->status; 270 status = urb->status;
272 271
273 if (status) { 272 if (status) {
274 dev_dbg(&urb->dev->dev, 273 dev_dbg(&urb->dev->dev,
275 "nonzero write bulk status received:%d\n", status); 274 "nonzero write bulk status received:%d\n", status);
276 return; 275 return;
277 } 276 }
278 277
279 quatech_port = urb->context; 278 quatech_port = urb->context;
280 279
281 tty = tty_port_tty_get(&quatech_port->port->port); 280 tty_port_tty_wakeup(&quatech_port->port->port);
282
283 if (tty)
284 tty_wakeup(tty);
285 tty_kref_put(tty);
286 } 281 }
287 282
288 static void qt_interrupt_callback(struct urb *urb) 283 static void qt_interrupt_callback(struct urb *urb)
289 { 284 {
290 /* FIXME */ 285 /* FIXME */
291 } 286 }
292 287
293 static void qt_status_change_check(struct urb *urb, 288 static void qt_status_change_check(struct urb *urb,
294 struct quatech_port *qt_port, 289 struct quatech_port *qt_port,
295 struct usb_serial_port *port) 290 struct usb_serial_port *port)
296 { 291 {
297 int flag, i; 292 int flag, i;
298 unsigned char *data = urb->transfer_buffer; 293 unsigned char *data = urb->transfer_buffer;
299 unsigned int RxCount = urb->actual_length; 294 unsigned int RxCount = urb->actual_length;
300 295
301 for (i = 0; i < RxCount; ++i) { 296 for (i = 0; i < RxCount; ++i) {
302 /* Look ahead code here */ 297 /* Look ahead code here */
303 if ((i <= (RxCount - 3)) && (data[i] == 0x1b) 298 if ((i <= (RxCount - 3)) && (data[i] == 0x1b)
304 && (data[i + 1] == 0x1b)) { 299 && (data[i + 1] == 0x1b)) {
305 flag = 0; 300 flag = 0;
306 switch (data[i + 2]) { 301 switch (data[i + 2]) {
307 case 0x00: 302 case 0x00:
308 if (i > (RxCount - 4)) { 303 if (i > (RxCount - 4)) {
309 dev_dbg(&port->dev, 304 dev_dbg(&port->dev,
310 "Illegal escape seuences in received data\n"); 305 "Illegal escape seuences in received data\n");
311 break; 306 break;
312 } 307 }
313 308
314 ProcessLineStatus(qt_port, data[i + 3]); 309 ProcessLineStatus(qt_port, data[i + 3]);
315 310
316 i += 3; 311 i += 3;
317 flag = 1; 312 flag = 1;
318 break; 313 break;
319 314
320 case 0x01: 315 case 0x01:
321 if (i > (RxCount - 4)) { 316 if (i > (RxCount - 4)) {
322 dev_dbg(&port->dev, 317 dev_dbg(&port->dev,
323 "Illegal escape seuences in received data\n"); 318 "Illegal escape seuences in received data\n");
324 break; 319 break;
325 } 320 }
326 321
327 ProcessModemStatus(qt_port, data[i + 3]); 322 ProcessModemStatus(qt_port, data[i + 3]);
328 323
329 i += 3; 324 i += 3;
330 flag = 1; 325 flag = 1;
331 break; 326 break;
332 327
333 case 0xff: 328 case 0xff:
334 dev_dbg(&port->dev, "No status sequence.\n"); 329 dev_dbg(&port->dev, "No status sequence.\n");
335 330
336 ProcessRxChar(port, data[i]); 331 ProcessRxChar(port, data[i]);
337 ProcessRxChar(port, data[i + 1]); 332 ProcessRxChar(port, data[i + 1]);
338 333
339 i += 2; 334 i += 2;
340 break; 335 break;
341 } 336 }
342 if (flag == 1) 337 if (flag == 1)
343 continue; 338 continue;
344 } 339 }
345 340
346 if (urb->actual_length) 341 if (urb->actual_length)
347 tty_insert_flip_char(&port->port, data[i], TTY_NORMAL); 342 tty_insert_flip_char(&port->port, data[i], TTY_NORMAL);
348 343
349 } 344 }
350 tty_flip_buffer_push(&port->port); 345 tty_flip_buffer_push(&port->port);
351 } 346 }
352 347
353 static void qt_read_bulk_callback(struct urb *urb) 348 static void qt_read_bulk_callback(struct urb *urb)
354 { 349 {
355 350
356 struct usb_serial_port *port = urb->context; 351 struct usb_serial_port *port = urb->context;
357 struct usb_serial *serial = get_usb_serial(port, __func__); 352 struct usb_serial *serial = get_usb_serial(port, __func__);
358 struct quatech_port *qt_port = qt_get_port_private(port); 353 struct quatech_port *qt_port = qt_get_port_private(port);
359 int result; 354 int result;
360 355
361 if (urb->status) { 356 if (urb->status) {
362 qt_port->ReadBulkStopped = 1; 357 qt_port->ReadBulkStopped = 1;
363 dev_dbg(&urb->dev->dev, 358 dev_dbg(&urb->dev->dev,
364 "%s - nonzero write bulk status received: %d\n", 359 "%s - nonzero write bulk status received: %d\n",
365 __func__, urb->status); 360 __func__, urb->status);
366 return; 361 return;
367 } 362 }
368 363
369 dev_dbg(&port->dev, 364 dev_dbg(&port->dev,
370 "%s - port->RxHolding = %d\n", __func__, qt_port->RxHolding); 365 "%s - port->RxHolding = %d\n", __func__, qt_port->RxHolding);
371 366
372 if (port_paranoia_check(port, __func__) != 0) { 367 if (port_paranoia_check(port, __func__) != 0) {
373 qt_port->ReadBulkStopped = 1; 368 qt_port->ReadBulkStopped = 1;
374 return; 369 return;
375 } 370 }
376 371
377 if (!serial) 372 if (!serial)
378 return; 373 return;
379 374
380 if (qt_port->closePending == 1) { 375 if (qt_port->closePending == 1) {
381 /* Were closing , stop reading */ 376 /* Were closing , stop reading */
382 dev_dbg(&port->dev, 377 dev_dbg(&port->dev,
383 "%s - (qt_port->closepending == 1\n", __func__); 378 "%s - (qt_port->closepending == 1\n", __func__);
384 qt_port->ReadBulkStopped = 1; 379 qt_port->ReadBulkStopped = 1;
385 return; 380 return;
386 } 381 }
387 382
388 /* 383 /*
389 * RxHolding is asserted by throttle, if we assert it, we're not 384 * RxHolding is asserted by throttle, if we assert it, we're not
390 * receiving any more characters and let the box handle the flow 385 * receiving any more characters and let the box handle the flow
391 * control 386 * control
392 */ 387 */
393 if (qt_port->RxHolding == 1) { 388 if (qt_port->RxHolding == 1) {
394 qt_port->ReadBulkStopped = 1; 389 qt_port->ReadBulkStopped = 1;
395 return; 390 return;
396 } 391 }
397 392
398 if (urb->status) { 393 if (urb->status) {
399 qt_port->ReadBulkStopped = 1; 394 qt_port->ReadBulkStopped = 1;
400 395
401 dev_dbg(&port->dev, 396 dev_dbg(&port->dev,
402 "%s - nonzero read bulk status received: %d\n", 397 "%s - nonzero read bulk status received: %d\n",
403 __func__, urb->status); 398 __func__, urb->status);
404 return; 399 return;
405 } 400 }
406 401
407 if (urb->actual_length) 402 if (urb->actual_length)
408 qt_status_change_check(urb, qt_port, port); 403 qt_status_change_check(urb, qt_port, port);
409 404
410 /* Continue trying to always read */ 405 /* Continue trying to always read */
411 usb_fill_bulk_urb(port->read_urb, serial->dev, 406 usb_fill_bulk_urb(port->read_urb, serial->dev,
412 usb_rcvbulkpipe(serial->dev, 407 usb_rcvbulkpipe(serial->dev,
413 port->bulk_in_endpointAddress), 408 port->bulk_in_endpointAddress),
414 port->read_urb->transfer_buffer, 409 port->read_urb->transfer_buffer,
415 port->read_urb->transfer_buffer_length, 410 port->read_urb->transfer_buffer_length,
416 qt_read_bulk_callback, port); 411 qt_read_bulk_callback, port);
417 result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 412 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
418 if (result) 413 if (result)
419 dev_dbg(&port->dev, 414 dev_dbg(&port->dev,
420 "%s - failed resubmitting read urb, error %d", 415 "%s - failed resubmitting read urb, error %d",
421 __func__, result); 416 __func__, result);
422 else { 417 else {
423 if (urb->actual_length) { 418 if (urb->actual_length) {
424 tty_flip_buffer_push(&port->port); 419 tty_flip_buffer_push(&port->port);
425 tty_schedule_flip(&port->port); 420 tty_schedule_flip(&port->port);
426 } 421 }
427 } 422 }
428 423
429 schedule_work(&port->work); 424 schedule_work(&port->work);
430 } 425 }
431 426
432 /* 427 /*
433 * qt_get_device 428 * qt_get_device
434 * Issue a GET_DEVICE vendor-specific request on the default control pipe If 429 * Issue a GET_DEVICE vendor-specific request on the default control pipe If
435 * successful, fills in the qt_get_device_data structure pointed to by 430 * successful, fills in the qt_get_device_data structure pointed to by
436 * device_data, otherwise return a negative error number of the problem. 431 * device_data, otherwise return a negative error number of the problem.
437 */ 432 */
438 433
439 static int qt_get_device(struct usb_serial *serial, 434 static int qt_get_device(struct usb_serial *serial,
440 struct qt_get_device_data *device_data) 435 struct qt_get_device_data *device_data)
441 { 436 {
442 int result; 437 int result;
443 unsigned char *transfer_buffer; 438 unsigned char *transfer_buffer;
444 439
445 transfer_buffer = 440 transfer_buffer =
446 kmalloc(sizeof(struct qt_get_device_data), GFP_KERNEL); 441 kmalloc(sizeof(struct qt_get_device_data), GFP_KERNEL);
447 if (!transfer_buffer) 442 if (!transfer_buffer)
448 return -ENOMEM; 443 return -ENOMEM;
449 444
450 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 445 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
451 QT_SET_GET_DEVICE, 0xc0, 0, 0, 446 QT_SET_GET_DEVICE, 0xc0, 0, 0,
452 transfer_buffer, 447 transfer_buffer,
453 sizeof(struct qt_get_device_data), 300); 448 sizeof(struct qt_get_device_data), 300);
454 if (result > 0) 449 if (result > 0)
455 memcpy(device_data, transfer_buffer, 450 memcpy(device_data, transfer_buffer,
456 sizeof(struct qt_get_device_data)); 451 sizeof(struct qt_get_device_data));
457 kfree(transfer_buffer); 452 kfree(transfer_buffer);
458 453
459 return result; 454 return result;
460 } 455 }
461 456
462 /**************************************************************************** 457 /****************************************************************************
463 * BoxSetPrebufferLevel 458 * BoxSetPrebufferLevel
464 TELLS BOX WHEN TO ASSERT FLOW CONTROL 459 TELLS BOX WHEN TO ASSERT FLOW CONTROL
465 ****************************************************************************/ 460 ****************************************************************************/
466 static int BoxSetPrebufferLevel(struct usb_serial *serial) 461 static int BoxSetPrebufferLevel(struct usb_serial *serial)
467 { 462 {
468 int result; 463 int result;
469 __u16 buffer_length; 464 __u16 buffer_length;
470 465
471 buffer_length = PREFUFF_LEVEL_CONSERVATIVE; 466 buffer_length = PREFUFF_LEVEL_CONSERVATIVE;
472 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 467 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
473 QT_GET_SET_PREBUF_TRIG_LVL, 0x40, 468 QT_GET_SET_PREBUF_TRIG_LVL, 0x40,
474 buffer_length, 0, NULL, 0, 300); 469 buffer_length, 0, NULL, 0, 300);
475 return result; 470 return result;
476 } 471 }
477 472
478 /**************************************************************************** 473 /****************************************************************************
479 * BoxSetATC 474 * BoxSetATC
480 TELLS BOX WHEN TO ASSERT automatic transmitter control 475 TELLS BOX WHEN TO ASSERT automatic transmitter control
481 ****************************************************************************/ 476 ****************************************************************************/
482 static int BoxSetATC(struct usb_serial *serial, __u16 n_Mode) 477 static int BoxSetATC(struct usb_serial *serial, __u16 n_Mode)
483 { 478 {
484 int result; 479 int result;
485 __u16 buffer_length; 480 __u16 buffer_length;
486 481
487 buffer_length = PREFUFF_LEVEL_CONSERVATIVE; 482 buffer_length = PREFUFF_LEVEL_CONSERVATIVE;
488 483
489 result = 484 result =
490 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 485 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
491 QT_SET_ATF, 0x40, n_Mode, 0, NULL, 0, 300); 486 QT_SET_ATF, 0x40, n_Mode, 0, NULL, 0, 300);
492 487
493 return result; 488 return result;
494 } 489 }
495 490
496 /** 491 /**
497 * qt_set_device 492 * qt_set_device
498 * Issue a SET_DEVICE vendor-specific request on the default control pipe If 493 * Issue a SET_DEVICE vendor-specific request on the default control pipe If
499 * successful returns the number of bytes written, otherwise it returns a 494 * successful returns the number of bytes written, otherwise it returns a
500 * negative error number of the problem. 495 * negative error number of the problem.
501 */ 496 */
502 static int qt_set_device(struct usb_serial *serial, 497 static int qt_set_device(struct usb_serial *serial,
503 struct qt_get_device_data *device_data) 498 struct qt_get_device_data *device_data)
504 { 499 {
505 int result; 500 int result;
506 __u16 length; 501 __u16 length;
507 __u16 PortSettings; 502 __u16 PortSettings;
508 503
509 PortSettings = ((__u16) (device_data->portb)); 504 PortSettings = ((__u16) (device_data->portb));
510 PortSettings = (PortSettings << 8); 505 PortSettings = (PortSettings << 8);
511 PortSettings += ((__u16) (device_data->porta)); 506 PortSettings += ((__u16) (device_data->porta));
512 507
513 length = sizeof(struct qt_get_device_data); 508 length = sizeof(struct qt_get_device_data);
514 509
515 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 510 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
516 QT_SET_GET_DEVICE, 0x40, PortSettings, 511 QT_SET_GET_DEVICE, 0x40, PortSettings,
517 0, NULL, 0, 300); 512 0, NULL, 0, 300);
518 return result; 513 return result;
519 } 514 }
520 515
521 static int qt_open_channel(struct usb_serial *serial, __u16 Uart_Number, 516 static int qt_open_channel(struct usb_serial *serial, __u16 Uart_Number,
522 struct qt_open_channel_data *pDeviceData) 517 struct qt_open_channel_data *pDeviceData)
523 { 518 {
524 int result; 519 int result;
525 520
526 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 521 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
527 QT_OPEN_CLOSE_CHANNEL, 522 QT_OPEN_CLOSE_CHANNEL,
528 USBD_TRANSFER_DIRECTION_IN, 1, Uart_Number, 523 USBD_TRANSFER_DIRECTION_IN, 1, Uart_Number,
529 pDeviceData, 524 pDeviceData,
530 sizeof(struct qt_open_channel_data), 300); 525 sizeof(struct qt_open_channel_data), 300);
531 526
532 return result; 527 return result;
533 528
534 } 529 }
535 530
536 static int qt_close_channel(struct usb_serial *serial, __u16 Uart_Number) 531 static int qt_close_channel(struct usb_serial *serial, __u16 Uart_Number)
537 { 532 {
538 int result; 533 int result;
539 534
540 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 535 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
541 QT_OPEN_CLOSE_CHANNEL, 536 QT_OPEN_CLOSE_CHANNEL,
542 USBD_TRANSFER_DIRECTION_OUT, 0, Uart_Number, 537 USBD_TRANSFER_DIRECTION_OUT, 0, Uart_Number,
543 NULL, 0, 300); 538 NULL, 0, 300);
544 539
545 return result; 540 return result;
546 541
547 } 542 }
548 543
549 /**************************************************************************** 544 /****************************************************************************
550 * BoxGetRegister 545 * BoxGetRegister
551 * issuse a GET_REGISTER vendor-spcific request on the default control pipe 546 * issuse a GET_REGISTER vendor-spcific request on the default control pipe
552 * If successful, fills in the pValue with the register value asked for 547 * If successful, fills in the pValue with the register value asked for
553 ****************************************************************************/ 548 ****************************************************************************/
554 static int BoxGetRegister(struct usb_serial *serial, unsigned short Uart_Number, 549 static int BoxGetRegister(struct usb_serial *serial, unsigned short Uart_Number,
555 unsigned short Register_Num, __u8 *pValue) 550 unsigned short Register_Num, __u8 *pValue)
556 { 551 {
557 int result; 552 int result;
558 __u16 current_length; 553 __u16 current_length;
559 554
560 current_length = sizeof(struct qt_get_device_data); 555 current_length = sizeof(struct qt_get_device_data);
561 556
562 result = 557 result =
563 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 558 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
564 QT_GET_SET_REGISTER, 0xC0, Register_Num, 559 QT_GET_SET_REGISTER, 0xC0, Register_Num,
565 Uart_Number, (void *)pValue, sizeof(*pValue), 300); 560 Uart_Number, (void *)pValue, sizeof(*pValue), 300);
566 561
567 return result; 562 return result;
568 } 563 }
569 564
570 /**************************************************************************** 565 /****************************************************************************
571 * BoxSetRegister 566 * BoxSetRegister
572 * issuse a GET_REGISTER vendor-spcific request on the default control pipe 567 * issuse a GET_REGISTER vendor-spcific request on the default control pipe
573 * If successful, fills in the pValue with the register value asked for 568 * If successful, fills in the pValue with the register value asked for
574 ****************************************************************************/ 569 ****************************************************************************/
575 static int BoxSetRegister(struct usb_serial *serial, unsigned short Uart_Number, 570 static int BoxSetRegister(struct usb_serial *serial, unsigned short Uart_Number,
576 unsigned short Register_Num, unsigned short Value) 571 unsigned short Register_Num, unsigned short Value)
577 { 572 {
578 int result; 573 int result;
579 unsigned short RegAndByte; 574 unsigned short RegAndByte;
580 575
581 RegAndByte = Value; 576 RegAndByte = Value;
582 RegAndByte = RegAndByte << 8; 577 RegAndByte = RegAndByte << 8;
583 RegAndByte = RegAndByte + Register_Num; 578 RegAndByte = RegAndByte + Register_Num;
584 579
585 /* 580 /*
586 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 581 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
587 QT_GET_SET_REGISTER, 0xC0, Register_Num, 582 QT_GET_SET_REGISTER, 0xC0, Register_Num,
588 Uart_Number, NULL, 0, 300); 583 Uart_Number, NULL, 0, 300);
589 */ 584 */
590 585
591 result = 586 result =
592 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 587 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
593 QT_GET_SET_REGISTER, 0x40, RegAndByte, Uart_Number, 588 QT_GET_SET_REGISTER, 0x40, RegAndByte, Uart_Number,
594 NULL, 0, 300); 589 NULL, 0, 300);
595 590
596 return result; 591 return result;
597 } 592 }
598 593
599 /* 594 /*
600 * qt_setuart 595 * qt_setuart
601 * issues a SET_UART vendor-specific request on the default control pipe 596 * issues a SET_UART vendor-specific request on the default control pipe
602 * If successful sets baud rate divisor and LCR value 597 * If successful sets baud rate divisor and LCR value
603 */ 598 */
604 static int qt_setuart(struct usb_serial *serial, unsigned short Uart_Number, 599 static int qt_setuart(struct usb_serial *serial, unsigned short Uart_Number,
605 unsigned short default_divisor, unsigned char default_LCR) 600 unsigned short default_divisor, unsigned char default_LCR)
606 { 601 {
607 int result; 602 int result;
608 unsigned short UartNumandLCR; 603 unsigned short UartNumandLCR;
609 604
610 UartNumandLCR = (default_LCR << 8) + Uart_Number; 605 UartNumandLCR = (default_LCR << 8) + Uart_Number;
611 606
612 result = 607 result =
613 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 608 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
614 QT_GET_SET_UART, 0x40, default_divisor, 609 QT_GET_SET_UART, 0x40, default_divisor,
615 UartNumandLCR, NULL, 0, 300); 610 UartNumandLCR, NULL, 0, 300);
616 611
617 return result; 612 return result;
618 } 613 }
619 614
620 static int BoxSetHW_FlowCtrl(struct usb_serial *serial, unsigned int index, 615 static int BoxSetHW_FlowCtrl(struct usb_serial *serial, unsigned int index,
621 int bSet) 616 int bSet)
622 { 617 {
623 __u8 mcr = 0; 618 __u8 mcr = 0;
624 __u8 msr = 0, MOUT_Value = 0; 619 __u8 msr = 0, MOUT_Value = 0;
625 unsigned int status; 620 unsigned int status;
626 621
627 if (bSet == 1) { 622 if (bSet == 1) {
628 /* flow control, box will clear RTS line to prevent remote */ 623 /* flow control, box will clear RTS line to prevent remote */
629 mcr = SERIAL_MCR_RTS; 624 mcr = SERIAL_MCR_RTS;
630 } /* device from xmitting more chars */ 625 } /* device from xmitting more chars */
631 else { 626 else {
632 /* no flow control to remote device */ 627 /* no flow control to remote device */
633 mcr = 0; 628 mcr = 0;
634 629
635 } 630 }
636 MOUT_Value = mcr << 8; 631 MOUT_Value = mcr << 8;
637 632
638 if (bSet == 1) { 633 if (bSet == 1) {
639 /* flow control, box will inhibit xmit data if CTS line is 634 /* flow control, box will inhibit xmit data if CTS line is
640 * asserted */ 635 * asserted */
641 msr = SERIAL_MSR_CTS; 636 msr = SERIAL_MSR_CTS;
642 } else { 637 } else {
643 /* Box will not inhimbe xmit data due to CTS line */ 638 /* Box will not inhimbe xmit data due to CTS line */
644 msr = 0; 639 msr = 0;
645 } 640 }
646 MOUT_Value |= msr; 641 MOUT_Value |= msr;
647 642
648 status = 643 status =
649 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 644 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
650 QT_HW_FLOW_CONTROL_MASK, 0x40, MOUT_Value, 645 QT_HW_FLOW_CONTROL_MASK, 0x40, MOUT_Value,
651 index, NULL, 0, 300); 646 index, NULL, 0, 300);
652 return status; 647 return status;
653 648
654 } 649 }
655 650
656 static int BoxSetSW_FlowCtrl(struct usb_serial *serial, __u16 index, 651 static int BoxSetSW_FlowCtrl(struct usb_serial *serial, __u16 index,
657 unsigned char stop_char, unsigned char start_char) 652 unsigned char stop_char, unsigned char start_char)
658 { 653 {
659 __u16 nSWflowout; 654 __u16 nSWflowout;
660 int result; 655 int result;
661 656
662 nSWflowout = start_char << 8; 657 nSWflowout = start_char << 8;
663 nSWflowout = (unsigned short)stop_char; 658 nSWflowout = (unsigned short)stop_char;
664 659
665 result = 660 result =
666 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 661 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
667 QT_SW_FLOW_CONTROL_MASK, 0x40, nSWflowout, 662 QT_SW_FLOW_CONTROL_MASK, 0x40, nSWflowout,
668 index, NULL, 0, 300); 663 index, NULL, 0, 300);
669 return result; 664 return result;
670 665
671 } 666 }
672 667
673 static int BoxDisable_SW_FlowCtrl(struct usb_serial *serial, __u16 index) 668 static int BoxDisable_SW_FlowCtrl(struct usb_serial *serial, __u16 index)
674 { 669 {
675 int result; 670 int result;
676 671
677 result = 672 result =
678 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 673 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
679 QT_SW_FLOW_CONTROL_DISABLE, 0x40, 0, index, 674 QT_SW_FLOW_CONTROL_DISABLE, 0x40, 0, index,
680 NULL, 0, 300); 675 NULL, 0, 300);
681 return result; 676 return result;
682 677
683 } 678 }
684 679
685 static int qt_startup(struct usb_serial *serial) 680 static int qt_startup(struct usb_serial *serial)
686 { 681 {
687 struct device *dev = &serial->dev->dev; 682 struct device *dev = &serial->dev->dev;
688 struct usb_serial_port *port; 683 struct usb_serial_port *port;
689 struct quatech_port *qt_port; 684 struct quatech_port *qt_port;
690 struct qt_get_device_data DeviceData; 685 struct qt_get_device_data DeviceData;
691 int i; 686 int i;
692 int status; 687 int status;
693 688
694 /* Now setup per port private data */ 689 /* Now setup per port private data */
695 for (i = 0; i < serial->num_ports; i++) { 690 for (i = 0; i < serial->num_ports; i++) {
696 port = serial->port[i]; 691 port = serial->port[i];
697 qt_port = kzalloc(sizeof(*qt_port), GFP_KERNEL); 692 qt_port = kzalloc(sizeof(*qt_port), GFP_KERNEL);
698 if (!qt_port) { 693 if (!qt_port) {
699 for (--i; i >= 0; i--) { 694 for (--i; i >= 0; i--) {
700 port = serial->port[i]; 695 port = serial->port[i];
701 kfree(usb_get_serial_port_data(port)); 696 kfree(usb_get_serial_port_data(port));
702 usb_set_serial_port_data(port, NULL); 697 usb_set_serial_port_data(port, NULL);
703 } 698 }
704 return -ENOMEM; 699 return -ENOMEM;
705 } 700 }
706 mutex_init(&qt_port->lock); 701 mutex_init(&qt_port->lock);
707 702
708 usb_set_serial_port_data(port, qt_port); 703 usb_set_serial_port_data(port, qt_port);
709 704
710 } 705 }
711 706
712 status = qt_get_device(serial, &DeviceData); 707 status = qt_get_device(serial, &DeviceData);
713 if (status < 0) 708 if (status < 0)
714 goto startup_error; 709 goto startup_error;
715 710
716 dev_dbg(dev, "DeviceData.portb = 0x%x\n", DeviceData.portb); 711 dev_dbg(dev, "DeviceData.portb = 0x%x\n", DeviceData.portb);
717 712
718 DeviceData.portb &= ~FULLPWRBIT; 713 DeviceData.portb &= ~FULLPWRBIT;
719 dev_dbg(dev, "Changing DeviceData.portb to 0x%x\n", DeviceData.portb); 714 dev_dbg(dev, "Changing DeviceData.portb to 0x%x\n", DeviceData.portb);
720 715
721 status = qt_set_device(serial, &DeviceData); 716 status = qt_set_device(serial, &DeviceData);
722 if (status < 0) { 717 if (status < 0) {
723 dev_dbg(dev, "qt_set_device failed\n"); 718 dev_dbg(dev, "qt_set_device failed\n");
724 goto startup_error; 719 goto startup_error;
725 } 720 }
726 721
727 status = qt_get_device(serial, &DeviceData); 722 status = qt_get_device(serial, &DeviceData);
728 if (status < 0) { 723 if (status < 0) {
729 dev_dbg(dev, "qt_get_device failed\n"); 724 dev_dbg(dev, "qt_get_device failed\n");
730 goto startup_error; 725 goto startup_error;
731 } 726 }
732 727
733 switch (serial->dev->descriptor.idProduct) { 728 switch (serial->dev->descriptor.idProduct) {
734 case QUATECH_DSU100: 729 case QUATECH_DSU100:
735 case QUATECH_QSU100: 730 case QUATECH_QSU100:
736 case QUATECH_ESU100A: 731 case QUATECH_ESU100A:
737 case QUATECH_ESU100B: 732 case QUATECH_ESU100B:
738 case QUATECH_HSU100A: 733 case QUATECH_HSU100A:
739 case QUATECH_HSU100B: 734 case QUATECH_HSU100B:
740 case QUATECH_HSU100C: 735 case QUATECH_HSU100C:
741 case QUATECH_HSU100D: 736 case QUATECH_HSU100D:
742 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS); 737 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
743 DeviceData.porta |= CLKS_X4; 738 DeviceData.porta |= CLKS_X4;
744 DeviceData.portb &= ~(LOOPMODE_BITS); 739 DeviceData.portb &= ~(LOOPMODE_BITS);
745 DeviceData.portb |= RS232_MODE; 740 DeviceData.portb |= RS232_MODE;
746 break; 741 break;
747 742
748 case QUATECH_SSU200: 743 case QUATECH_SSU200:
749 case QUATECH_DSU200: 744 case QUATECH_DSU200:
750 case QUATECH_QSU200: 745 case QUATECH_QSU200:
751 case QUATECH_ESU200A: 746 case QUATECH_ESU200A:
752 case QUATECH_ESU200B: 747 case QUATECH_ESU200B:
753 case QUATECH_HSU200A: 748 case QUATECH_HSU200A:
754 case QUATECH_HSU200B: 749 case QUATECH_HSU200B:
755 case QUATECH_HSU200C: 750 case QUATECH_HSU200C:
756 case QUATECH_HSU200D: 751 case QUATECH_HSU200D:
757 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS); 752 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
758 DeviceData.porta |= CLKS_X4; 753 DeviceData.porta |= CLKS_X4;
759 DeviceData.portb &= ~(LOOPMODE_BITS); 754 DeviceData.portb &= ~(LOOPMODE_BITS);
760 DeviceData.portb |= ALL_LOOPBACK; 755 DeviceData.portb |= ALL_LOOPBACK;
761 break; 756 break;
762 default: 757 default:
763 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS); 758 DeviceData.porta &= ~(RR_BITS | DUPMODE_BITS);
764 DeviceData.porta |= CLKS_X4; 759 DeviceData.porta |= CLKS_X4;
765 DeviceData.portb &= ~(LOOPMODE_BITS); 760 DeviceData.portb &= ~(LOOPMODE_BITS);
766 DeviceData.portb |= RS232_MODE; 761 DeviceData.portb |= RS232_MODE;
767 break; 762 break;
768 763
769 } 764 }
770 765
771 status = BoxSetPrebufferLevel(serial); /* sets to default value */ 766 status = BoxSetPrebufferLevel(serial); /* sets to default value */
772 if (status < 0) { 767 if (status < 0) {
773 dev_dbg(dev, "BoxSetPrebufferLevel failed\n"); 768 dev_dbg(dev, "BoxSetPrebufferLevel failed\n");
774 goto startup_error; 769 goto startup_error;
775 } 770 }
776 771
777 status = BoxSetATC(serial, ATC_DISABLED); 772 status = BoxSetATC(serial, ATC_DISABLED);
778 if (status < 0) { 773 if (status < 0) {
779 dev_dbg(dev, "BoxSetATC failed\n"); 774 dev_dbg(dev, "BoxSetATC failed\n");
780 goto startup_error; 775 goto startup_error;
781 } 776 }
782 777
783 dev_dbg(dev, "DeviceData.portb = 0x%x\n", DeviceData.portb); 778 dev_dbg(dev, "DeviceData.portb = 0x%x\n", DeviceData.portb);
784 779
785 DeviceData.portb |= NEXT_BOARD_POWER_BIT; 780 DeviceData.portb |= NEXT_BOARD_POWER_BIT;
786 dev_dbg(dev, "Changing DeviceData.portb to 0x%x\n", DeviceData.portb); 781 dev_dbg(dev, "Changing DeviceData.portb to 0x%x\n", DeviceData.portb);
787 782
788 status = qt_set_device(serial, &DeviceData); 783 status = qt_set_device(serial, &DeviceData);
789 if (status < 0) { 784 if (status < 0) {
790 dev_dbg(dev, "qt_set_device failed\n"); 785 dev_dbg(dev, "qt_set_device failed\n");
791 goto startup_error; 786 goto startup_error;
792 } 787 }
793 788
794 return 0; 789 return 0;
795 790
796 startup_error: 791 startup_error:
797 for (i = 0; i < serial->num_ports; i++) { 792 for (i = 0; i < serial->num_ports; i++) {
798 port = serial->port[i]; 793 port = serial->port[i];
799 qt_port = qt_get_port_private(port); 794 qt_port = qt_get_port_private(port);
800 kfree(qt_port); 795 kfree(qt_port);
801 usb_set_serial_port_data(port, NULL); 796 usb_set_serial_port_data(port, NULL);
802 } 797 }
803 798
804 return -EIO; 799 return -EIO;
805 } 800 }
806 801
807 static void qt_release(struct usb_serial *serial) 802 static void qt_release(struct usb_serial *serial)
808 { 803 {
809 struct usb_serial_port *port; 804 struct usb_serial_port *port;
810 struct quatech_port *qt_port; 805 struct quatech_port *qt_port;
811 int i; 806 int i;
812 807
813 for (i = 0; i < serial->num_ports; i++) { 808 for (i = 0; i < serial->num_ports; i++) {
814 port = serial->port[i]; 809 port = serial->port[i];
815 if (!port) 810 if (!port)
816 continue; 811 continue;
817 812
818 qt_port = usb_get_serial_port_data(port); 813 qt_port = usb_get_serial_port_data(port);
819 kfree(qt_port); 814 kfree(qt_port);
820 usb_set_serial_port_data(port, NULL); 815 usb_set_serial_port_data(port, NULL);
821 } 816 }
822 817
823 } 818 }
824 819
825 static void qt_submit_urb_from_open(struct usb_serial *serial, 820 static void qt_submit_urb_from_open(struct usb_serial *serial,
826 struct usb_serial_port *port) 821 struct usb_serial_port *port)
827 { 822 {
828 int result; 823 int result;
829 struct usb_serial_port *port0 = serial->port[0]; 824 struct usb_serial_port *port0 = serial->port[0];
830 825
831 /* set up interrupt urb */ 826 /* set up interrupt urb */
832 usb_fill_int_urb(port0->interrupt_in_urb, 827 usb_fill_int_urb(port0->interrupt_in_urb,
833 serial->dev, 828 serial->dev,
834 usb_rcvintpipe(serial->dev, 829 usb_rcvintpipe(serial->dev,
835 port0->interrupt_in_endpointAddress), 830 port0->interrupt_in_endpointAddress),
836 port0->interrupt_in_buffer, 831 port0->interrupt_in_buffer,
837 port0->interrupt_in_urb->transfer_buffer_length, 832 port0->interrupt_in_urb->transfer_buffer_length,
838 qt_interrupt_callback, serial, 833 qt_interrupt_callback, serial,
839 port0->interrupt_in_urb->interval); 834 port0->interrupt_in_urb->interval);
840 835
841 result = usb_submit_urb(port0->interrupt_in_urb, 836 result = usb_submit_urb(port0->interrupt_in_urb,
842 GFP_KERNEL); 837 GFP_KERNEL);
843 if (result) { 838 if (result) {
844 dev_err(&port->dev, 839 dev_err(&port->dev,
845 "%s - Error %d submitting interrupt urb\n", 840 "%s - Error %d submitting interrupt urb\n",
846 __func__, result); 841 __func__, result);
847 } 842 }
848 } 843 }
849 844
850 static int qt_open(struct tty_struct *tty, 845 static int qt_open(struct tty_struct *tty,
851 struct usb_serial_port *port) 846 struct usb_serial_port *port)
852 { 847 {
853 struct usb_serial *serial; 848 struct usb_serial *serial;
854 struct quatech_port *quatech_port; 849 struct quatech_port *quatech_port;
855 struct quatech_port *port0; 850 struct quatech_port *port0;
856 struct qt_open_channel_data ChannelData; 851 struct qt_open_channel_data ChannelData;
857 852
858 int result; 853 int result;
859 854
860 if (port_paranoia_check(port, __func__)) 855 if (port_paranoia_check(port, __func__))
861 return -ENODEV; 856 return -ENODEV;
862 857
863 serial = port->serial; 858 serial = port->serial;
864 859
865 if (serial_paranoia_check(serial, __func__)) 860 if (serial_paranoia_check(serial, __func__))
866 return -ENODEV; 861 return -ENODEV;
867 862
868 quatech_port = qt_get_port_private(port); 863 quatech_port = qt_get_port_private(port);
869 port0 = qt_get_port_private(serial->port[0]); 864 port0 = qt_get_port_private(serial->port[0]);
870 865
871 if (quatech_port == NULL || port0 == NULL) 866 if (quatech_port == NULL || port0 == NULL)
872 return -ENODEV; 867 return -ENODEV;
873 868
874 usb_clear_halt(serial->dev, port->write_urb->pipe); 869 usb_clear_halt(serial->dev, port->write_urb->pipe);
875 usb_clear_halt(serial->dev, port->read_urb->pipe); 870 usb_clear_halt(serial->dev, port->read_urb->pipe);
876 port0->open_ports++; 871 port0->open_ports++;
877 872
878 result = qt_get_device(serial, &port0->DeviceData); 873 result = qt_get_device(serial, &port0->DeviceData);
879 874
880 /* Port specific setups */ 875 /* Port specific setups */
881 result = qt_open_channel(serial, port->number, &ChannelData); 876 result = qt_open_channel(serial, port->number, &ChannelData);
882 if (result < 0) { 877 if (result < 0) {
883 dev_dbg(&port->dev, "qt_open_channel failed\n"); 878 dev_dbg(&port->dev, "qt_open_channel failed\n");
884 return result; 879 return result;
885 } 880 }
886 dev_dbg(&port->dev, "qt_open_channel completed.\n"); 881 dev_dbg(&port->dev, "qt_open_channel completed.\n");
887 882
888 /* FIXME: are these needed? Does it even do anything useful? */ 883 /* FIXME: are these needed? Does it even do anything useful? */
889 quatech_port->shadowLSR = ChannelData.line_status & 884 quatech_port->shadowLSR = ChannelData.line_status &
890 (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE | SERIAL_LSR_BI); 885 (SERIAL_LSR_OE | SERIAL_LSR_PE | SERIAL_LSR_FE | SERIAL_LSR_BI);
891 886
892 quatech_port->shadowMSR = ChannelData.modem_status & 887 quatech_port->shadowMSR = ChannelData.modem_status &
893 (SERIAL_MSR_CTS | SERIAL_MSR_DSR | SERIAL_MSR_RI | SERIAL_MSR_CD); 888 (SERIAL_MSR_CTS | SERIAL_MSR_DSR | SERIAL_MSR_RI | SERIAL_MSR_CD);
894 889
895 /* Set Baud rate to default and turn off (default)flow control here */ 890 /* Set Baud rate to default and turn off (default)flow control here */
896 result = qt_setuart(serial, port->number, DEFAULT_DIVISOR, DEFAULT_LCR); 891 result = qt_setuart(serial, port->number, DEFAULT_DIVISOR, DEFAULT_LCR);
897 if (result < 0) { 892 if (result < 0) {
898 dev_dbg(&port->dev, "qt_setuart failed\n"); 893 dev_dbg(&port->dev, "qt_setuart failed\n");
899 return result; 894 return result;
900 } 895 }
901 dev_dbg(&port->dev, "qt_setuart completed.\n"); 896 dev_dbg(&port->dev, "qt_setuart completed.\n");
902 897
903 /* 898 /*
904 * Put this here to make it responsive to stty and defaults set by 899 * Put this here to make it responsive to stty and defaults set by
905 * the tty layer 900 * the tty layer
906 */ 901 */
907 902
908 /* Check to see if we've set up our endpoint info yet */ 903 /* Check to see if we've set up our endpoint info yet */
909 if (port0->open_ports == 1) { 904 if (port0->open_ports == 1) {
910 if (serial->port[0]->interrupt_in_buffer == NULL) 905 if (serial->port[0]->interrupt_in_buffer == NULL)
911 qt_submit_urb_from_open(serial, port); 906 qt_submit_urb_from_open(serial, port);
912 } 907 }
913 908
914 dev_dbg(&port->dev, "port number is %d\n", port->number); 909 dev_dbg(&port->dev, "port number is %d\n", port->number);
915 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor); 910 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
916 dev_dbg(&port->dev, 911 dev_dbg(&port->dev,
917 "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress); 912 "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
918 dev_dbg(&port->dev, 913 dev_dbg(&port->dev,
919 "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress); 914 "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
920 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", 915 dev_dbg(&port->dev, "Interrupt endpoint is %d\n",
921 port->interrupt_in_endpointAddress); 916 port->interrupt_in_endpointAddress);
922 dev_dbg(&port->dev, "port's number in the device is %d\n", 917 dev_dbg(&port->dev, "port's number in the device is %d\n",
923 quatech_port->port_num); 918 quatech_port->port_num);
924 quatech_port->read_urb = port->read_urb; 919 quatech_port->read_urb = port->read_urb;
925 920
926 /* set up our bulk in urb */ 921 /* set up our bulk in urb */
927 922
928 usb_fill_bulk_urb(quatech_port->read_urb, 923 usb_fill_bulk_urb(quatech_port->read_urb,
929 serial->dev, 924 serial->dev,
930 usb_rcvbulkpipe(serial->dev, 925 usb_rcvbulkpipe(serial->dev,
931 port->bulk_in_endpointAddress), 926 port->bulk_in_endpointAddress),
932 port->bulk_in_buffer, 927 port->bulk_in_buffer,
933 quatech_port->read_urb->transfer_buffer_length, 928 quatech_port->read_urb->transfer_buffer_length,
934 qt_read_bulk_callback, quatech_port); 929 qt_read_bulk_callback, quatech_port);
935 930
936 dev_dbg(&port->dev, "qt_open: bulkin endpoint is %d\n", 931 dev_dbg(&port->dev, "qt_open: bulkin endpoint is %d\n",
937 port->bulk_in_endpointAddress); 932 port->bulk_in_endpointAddress);
938 quatech_port->read_urb_busy = true; 933 quatech_port->read_urb_busy = true;
939 result = usb_submit_urb(quatech_port->read_urb, GFP_KERNEL); 934 result = usb_submit_urb(quatech_port->read_urb, GFP_KERNEL);
940 if (result) { 935 if (result) {
941 dev_err(&port->dev, 936 dev_err(&port->dev,
942 "%s - Error %d submitting control urb\n", 937 "%s - Error %d submitting control urb\n",
943 __func__, result); 938 __func__, result);
944 quatech_port->read_urb_busy = false; 939 quatech_port->read_urb_busy = false;
945 } 940 }
946 941
947 /* initialize our wait queues */ 942 /* initialize our wait queues */
948 init_waitqueue_head(&quatech_port->wait); 943 init_waitqueue_head(&quatech_port->wait);
949 init_waitqueue_head(&quatech_port->msr_wait); 944 init_waitqueue_head(&quatech_port->msr_wait);
950 945
951 /* initialize our icount structure */ 946 /* initialize our icount structure */
952 memset(&(quatech_port->icount), 0x00, sizeof(quatech_port->icount)); 947 memset(&(quatech_port->icount), 0x00, sizeof(quatech_port->icount));
953 948
954 return 0; 949 return 0;
955 950
956 } 951 }
957 952
958 static int qt_chars_in_buffer(struct tty_struct *tty) 953 static int qt_chars_in_buffer(struct tty_struct *tty)
959 { 954 {
960 struct usb_serial_port *port = tty->driver_data; 955 struct usb_serial_port *port = tty->driver_data;
961 struct usb_serial *serial; 956 struct usb_serial *serial;
962 int chars = 0; 957 int chars = 0;
963 958
964 serial = get_usb_serial(port, __func__); 959 serial = get_usb_serial(port, __func__);
965 960
966 if (serial->num_bulk_out) { 961 if (serial->num_bulk_out) {
967 if (port->write_urb->status == -EINPROGRESS) 962 if (port->write_urb->status == -EINPROGRESS)
968 chars = port->write_urb->transfer_buffer_length; 963 chars = port->write_urb->transfer_buffer_length;
969 } 964 }
970 965
971 return chars; 966 return chars;
972 } 967 }
973 968
974 static void qt_block_until_empty(struct tty_struct *tty, 969 static void qt_block_until_empty(struct tty_struct *tty,
975 struct quatech_port *qt_port) 970 struct quatech_port *qt_port)
976 { 971 {
977 int timeout = HZ / 10; 972 int timeout = HZ / 10;
978 int wait = 30; 973 int wait = 30;
979 int count; 974 int count;
980 975
981 while (1) { 976 while (1) {
982 977
983 count = qt_chars_in_buffer(tty); 978 count = qt_chars_in_buffer(tty);
984 979
985 if (count <= 0) 980 if (count <= 0)
986 return; 981 return;
987 982
988 interruptible_sleep_on_timeout(&qt_port->wait, timeout); 983 interruptible_sleep_on_timeout(&qt_port->wait, timeout);
989 984
990 wait--; 985 wait--;
991 if (wait == 0) { 986 if (wait == 0) {
992 dev_dbg(&qt_port->port->dev, "%s - TIMEOUT", __func__); 987 dev_dbg(&qt_port->port->dev, "%s - TIMEOUT", __func__);
993 return; 988 return;
994 } else { 989 } else {
995 wait = 30; 990 wait = 30;
996 } 991 }
997 } 992 }
998 } 993 }
999 994
1000 static void qt_close(struct usb_serial_port *port) 995 static void qt_close(struct usb_serial_port *port)
1001 { 996 {
1002 struct usb_serial *serial = port->serial; 997 struct usb_serial *serial = port->serial;
1003 struct quatech_port *qt_port; 998 struct quatech_port *qt_port;
1004 struct quatech_port *port0; 999 struct quatech_port *port0;
1005 struct tty_struct *tty; 1000 struct tty_struct *tty;
1006 int status; 1001 int status;
1007 unsigned int index; 1002 unsigned int index;
1008 status = 0; 1003 status = 0;
1009 1004
1010 tty = tty_port_tty_get(&port->port); 1005 tty = tty_port_tty_get(&port->port);
1011 index = tty->index - serial->minor; 1006 index = tty->index - serial->minor;
1012 1007
1013 qt_port = qt_get_port_private(port); 1008 qt_port = qt_get_port_private(port);
1014 port0 = qt_get_port_private(serial->port[0]); 1009 port0 = qt_get_port_private(serial->port[0]);
1015 1010
1016 /* shutdown any bulk reads that might be going on */ 1011 /* shutdown any bulk reads that might be going on */
1017 if (serial->num_bulk_out) 1012 if (serial->num_bulk_out)
1018 usb_unlink_urb(port->write_urb); 1013 usb_unlink_urb(port->write_urb);
1019 if (serial->num_bulk_in) 1014 if (serial->num_bulk_in)
1020 usb_unlink_urb(port->read_urb); 1015 usb_unlink_urb(port->read_urb);
1021 1016
1022 /* wait up to for transmitter to empty */ 1017 /* wait up to for transmitter to empty */
1023 if (serial->dev) 1018 if (serial->dev)
1024 qt_block_until_empty(tty, qt_port); 1019 qt_block_until_empty(tty, qt_port);
1025 tty_kref_put(tty); 1020 tty_kref_put(tty);
1026 1021
1027 /* Close uart channel */ 1022 /* Close uart channel */
1028 status = qt_close_channel(serial, index); 1023 status = qt_close_channel(serial, index);
1029 if (status < 0) 1024 if (status < 0)
1030 dev_dbg(&port->dev, 1025 dev_dbg(&port->dev,
1031 "%s - port %d qt_close_channel failed.\n", 1026 "%s - port %d qt_close_channel failed.\n",
1032 __func__, port->number); 1027 __func__, port->number);
1033 1028
1034 port0->open_ports--; 1029 port0->open_ports--;
1035 1030
1036 dev_dbg(&port->dev, "qt_num_open_ports in close%d:in port%d\n", 1031 dev_dbg(&port->dev, "qt_num_open_ports in close%d:in port%d\n",
1037 port0->open_ports, port->number); 1032 port0->open_ports, port->number);
1038 1033
1039 if (port0->open_ports == 0) { 1034 if (port0->open_ports == 0) {
1040 if (serial->port[0]->interrupt_in_urb) { 1035 if (serial->port[0]->interrupt_in_urb) {
1041 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n"); 1036 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1042 usb_kill_urb(serial->port[0]->interrupt_in_urb); 1037 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1043 } 1038 }
1044 1039
1045 } 1040 }
1046 1041
1047 if (qt_port->write_urb) { 1042 if (qt_port->write_urb) {
1048 /* if this urb had a transfer buffer already (old tx) free it */ 1043 /* if this urb had a transfer buffer already (old tx) free it */
1049 kfree(qt_port->write_urb->transfer_buffer); 1044 kfree(qt_port->write_urb->transfer_buffer);
1050 usb_free_urb(qt_port->write_urb); 1045 usb_free_urb(qt_port->write_urb);
1051 } 1046 }
1052 1047
1053 } 1048 }
1054 1049
1055 static int qt_write(struct tty_struct *tty, struct usb_serial_port *port, 1050 static int qt_write(struct tty_struct *tty, struct usb_serial_port *port,
1056 const unsigned char *buf, int count) 1051 const unsigned char *buf, int count)
1057 { 1052 {
1058 int result; 1053 int result;
1059 struct usb_serial *serial = get_usb_serial(port, __func__); 1054 struct usb_serial *serial = get_usb_serial(port, __func__);
1060 1055
1061 if (serial == NULL) 1056 if (serial == NULL)
1062 return -ENODEV; 1057 return -ENODEV;
1063 1058
1064 if (count == 0) { 1059 if (count == 0) {
1065 dev_dbg(&port->dev, 1060 dev_dbg(&port->dev,
1066 "%s - write request of 0 bytes\n", __func__); 1061 "%s - write request of 0 bytes\n", __func__);
1067 return 0; 1062 return 0;
1068 } 1063 }
1069 1064
1070 /* only do something if we have a bulk out endpoint */ 1065 /* only do something if we have a bulk out endpoint */
1071 if (serial->num_bulk_out) { 1066 if (serial->num_bulk_out) {
1072 if (port->write_urb->status == -EINPROGRESS) { 1067 if (port->write_urb->status == -EINPROGRESS) {
1073 dev_dbg(&port->dev, "%s - already writing\n", __func__); 1068 dev_dbg(&port->dev, "%s - already writing\n", __func__);
1074 return 0; 1069 return 0;
1075 } 1070 }
1076 1071
1077 count = 1072 count =
1078 (count > port->bulk_out_size) ? port->bulk_out_size : count; 1073 (count > port->bulk_out_size) ? port->bulk_out_size : count;
1079 memcpy(port->write_urb->transfer_buffer, buf, count); 1074 memcpy(port->write_urb->transfer_buffer, buf, count);
1080 1075
1081 /* set up our urb */ 1076 /* set up our urb */
1082 1077
1083 usb_fill_bulk_urb(port->write_urb, serial->dev, 1078 usb_fill_bulk_urb(port->write_urb, serial->dev,
1084 usb_sndbulkpipe(serial->dev, 1079 usb_sndbulkpipe(serial->dev,
1085 port-> 1080 port->
1086 bulk_out_endpointAddress), 1081 bulk_out_endpointAddress),
1087 port->write_urb->transfer_buffer, count, 1082 port->write_urb->transfer_buffer, count,
1088 qt_write_bulk_callback, port); 1083 qt_write_bulk_callback, port);
1089 1084
1090 /* send the data out the bulk port */ 1085 /* send the data out the bulk port */
1091 result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 1086 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1092 if (result) 1087 if (result)
1093 dev_dbg(&port->dev, 1088 dev_dbg(&port->dev,
1094 "%s - failed submitting write urb, error %d\n", 1089 "%s - failed submitting write urb, error %d\n",
1095 __func__, result); 1090 __func__, result);
1096 else 1091 else
1097 result = count; 1092 result = count;
1098 1093
1099 return result; 1094 return result;
1100 } 1095 }
1101 1096
1102 /* no bulk out, so return 0 bytes written */ 1097 /* no bulk out, so return 0 bytes written */
1103 return 0; 1098 return 0;
1104 } 1099 }
1105 1100
1106 static int qt_write_room(struct tty_struct *tty) 1101 static int qt_write_room(struct tty_struct *tty)
1107 { 1102 {
1108 struct usb_serial_port *port = tty->driver_data; 1103 struct usb_serial_port *port = tty->driver_data;
1109 struct usb_serial *serial; 1104 struct usb_serial *serial;
1110 struct quatech_port *qt_port; 1105 struct quatech_port *qt_port;
1111 1106
1112 int retval = -EINVAL; 1107 int retval = -EINVAL;
1113 1108
1114 if (port_paranoia_check(port, __func__)) 1109 if (port_paranoia_check(port, __func__))
1115 return -1; 1110 return -1;
1116 1111
1117 serial = get_usb_serial(port, __func__); 1112 serial = get_usb_serial(port, __func__);
1118 1113
1119 if (!serial) 1114 if (!serial)
1120 return -ENODEV; 1115 return -ENODEV;
1121 1116
1122 qt_port = qt_get_port_private(port); 1117 qt_port = qt_get_port_private(port);
1123 1118
1124 mutex_lock(&qt_port->lock); 1119 mutex_lock(&qt_port->lock);
1125 1120
1126 if (serial->num_bulk_out) { 1121 if (serial->num_bulk_out) {
1127 if (port->write_urb->status != -EINPROGRESS) 1122 if (port->write_urb->status != -EINPROGRESS)
1128 retval = port->bulk_out_size; 1123 retval = port->bulk_out_size;
1129 } 1124 }
1130 1125
1131 mutex_unlock(&qt_port->lock); 1126 mutex_unlock(&qt_port->lock);
1132 return retval; 1127 return retval;
1133 1128
1134 } 1129 }
1135 1130
1136 static int qt_ioctl(struct tty_struct *tty, 1131 static int qt_ioctl(struct tty_struct *tty,
1137 unsigned int cmd, unsigned long arg) 1132 unsigned int cmd, unsigned long arg)
1138 { 1133 {
1139 struct usb_serial_port *port = tty->driver_data; 1134 struct usb_serial_port *port = tty->driver_data;
1140 struct quatech_port *qt_port = qt_get_port_private(port); 1135 struct quatech_port *qt_port = qt_get_port_private(port);
1141 struct usb_serial *serial = get_usb_serial(port, __func__); 1136 struct usb_serial *serial = get_usb_serial(port, __func__);
1142 unsigned int index; 1137 unsigned int index;
1143 1138
1144 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd); 1139 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
1145 1140
1146 index = tty->index - serial->minor; 1141 index = tty->index - serial->minor;
1147 1142
1148 if (cmd == TIOCMIWAIT) { 1143 if (cmd == TIOCMIWAIT) {
1149 while (qt_port != NULL) { 1144 while (qt_port != NULL) {
1150 interruptible_sleep_on(&qt_port->msr_wait); 1145 interruptible_sleep_on(&qt_port->msr_wait);
1151 if (signal_pending(current)) 1146 if (signal_pending(current))
1152 return -ERESTARTSYS; 1147 return -ERESTARTSYS;
1153 else { 1148 else {
1154 char diff = qt_port->diff_status; 1149 char diff = qt_port->diff_status;
1155 1150
1156 if (diff == 0) 1151 if (diff == 0)
1157 return -EIO; /* no change => error */ 1152 return -EIO; /* no change => error */
1158 1153
1159 /* Consume all events */ 1154 /* Consume all events */
1160 qt_port->diff_status = 0; 1155 qt_port->diff_status = 0;
1161 1156
1162 if (((arg & TIOCM_RNG) 1157 if (((arg & TIOCM_RNG)
1163 && (diff & SERIAL_MSR_RI)) 1158 && (diff & SERIAL_MSR_RI))
1164 || ((arg & TIOCM_DSR) 1159 || ((arg & TIOCM_DSR)
1165 && (diff & SERIAL_MSR_DSR)) 1160 && (diff & SERIAL_MSR_DSR))
1166 || ((arg & TIOCM_CD) 1161 || ((arg & TIOCM_CD)
1167 && (diff & SERIAL_MSR_CD)) 1162 && (diff & SERIAL_MSR_CD))
1168 || ((arg & TIOCM_CTS) 1163 || ((arg & TIOCM_CTS)
1169 && (diff & SERIAL_MSR_CTS))) { 1164 && (diff & SERIAL_MSR_CTS))) {
1170 return 0; 1165 return 0;
1171 } 1166 }
1172 } 1167 }
1173 } 1168 }
1174 return 0; 1169 return 0;
1175 } 1170 }
1176 1171
1177 dev_dbg(&port->dev, "%s -No ioctl for that one. port = %d\n", 1172 dev_dbg(&port->dev, "%s -No ioctl for that one. port = %d\n",
1178 __func__, port->number); 1173 __func__, port->number);
1179 return -ENOIOCTLCMD; 1174 return -ENOIOCTLCMD;
1180 } 1175 }
1181 1176
1182 static void qt_set_termios(struct tty_struct *tty, 1177 static void qt_set_termios(struct tty_struct *tty,
1183 struct usb_serial_port *port, 1178 struct usb_serial_port *port,
1184 struct ktermios *old_termios) 1179 struct ktermios *old_termios)
1185 { 1180 {
1186 struct ktermios *termios = &tty->termios; 1181 struct ktermios *termios = &tty->termios;
1187 unsigned char new_LCR = 0; 1182 unsigned char new_LCR = 0;
1188 unsigned int cflag = termios->c_cflag; 1183 unsigned int cflag = termios->c_cflag;
1189 unsigned int index; 1184 unsigned int index;
1190 int baud, divisor, remainder; 1185 int baud, divisor, remainder;
1191 int status; 1186 int status;
1192 1187
1193 index = tty->index - port->serial->minor; 1188 index = tty->index - port->serial->minor;
1194 1189
1195 switch (cflag & CSIZE) { 1190 switch (cflag & CSIZE) {
1196 case CS5: 1191 case CS5:
1197 new_LCR |= SERIAL_5_DATA; 1192 new_LCR |= SERIAL_5_DATA;
1198 break; 1193 break;
1199 case CS6: 1194 case CS6:
1200 new_LCR |= SERIAL_6_DATA; 1195 new_LCR |= SERIAL_6_DATA;
1201 break; 1196 break;
1202 case CS7: 1197 case CS7:
1203 new_LCR |= SERIAL_7_DATA; 1198 new_LCR |= SERIAL_7_DATA;
1204 break; 1199 break;
1205 default: 1200 default:
1206 termios->c_cflag &= ~CSIZE; 1201 termios->c_cflag &= ~CSIZE;
1207 termios->c_cflag |= CS8; 1202 termios->c_cflag |= CS8;
1208 case CS8: 1203 case CS8:
1209 new_LCR |= SERIAL_8_DATA; 1204 new_LCR |= SERIAL_8_DATA;
1210 break; 1205 break;
1211 } 1206 }
1212 1207
1213 /* Parity stuff */ 1208 /* Parity stuff */
1214 if (cflag & PARENB) { 1209 if (cflag & PARENB) {
1215 if (cflag & PARODD) 1210 if (cflag & PARODD)
1216 new_LCR |= SERIAL_ODD_PARITY; 1211 new_LCR |= SERIAL_ODD_PARITY;
1217 else 1212 else
1218 new_LCR |= SERIAL_EVEN_PARITY; 1213 new_LCR |= SERIAL_EVEN_PARITY;
1219 } 1214 }
1220 if (cflag & CSTOPB) 1215 if (cflag & CSTOPB)
1221 new_LCR |= SERIAL_TWO_STOPB; 1216 new_LCR |= SERIAL_TWO_STOPB;
1222 else 1217 else
1223 new_LCR |= SERIAL_ONE_STOPB; 1218 new_LCR |= SERIAL_ONE_STOPB;
1224 1219
1225 dev_dbg(&port->dev, "%s - 4\n", __func__); 1220 dev_dbg(&port->dev, "%s - 4\n", __func__);
1226 1221
1227 /* Thats the LCR stuff, go ahead and set it */ 1222 /* Thats the LCR stuff, go ahead and set it */
1228 baud = tty_get_baud_rate(tty); 1223 baud = tty_get_baud_rate(tty);
1229 if (!baud) 1224 if (!baud)
1230 /* pick a default, any default... */ 1225 /* pick a default, any default... */
1231 baud = 9600; 1226 baud = 9600;
1232 1227
1233 dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud); 1228 dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
1234 1229
1235 divisor = MAX_BAUD_RATE / baud; 1230 divisor = MAX_BAUD_RATE / baud;
1236 remainder = MAX_BAUD_RATE % baud; 1231 remainder = MAX_BAUD_RATE % baud;
1237 /* Round to nearest divisor */ 1232 /* Round to nearest divisor */
1238 if (((remainder * 2) >= baud) && (baud != 110)) 1233 if (((remainder * 2) >= baud) && (baud != 110))
1239 divisor++; 1234 divisor++;
1240 1235
1241 /* 1236 /*
1242 * Set Baud rate to default and turn off (default)flow control here 1237 * Set Baud rate to default and turn off (default)flow control here
1243 */ 1238 */
1244 status = 1239 status =
1245 qt_setuart(port->serial, index, (unsigned short)divisor, new_LCR); 1240 qt_setuart(port->serial, index, (unsigned short)divisor, new_LCR);
1246 if (status < 0) { 1241 if (status < 0) {
1247 dev_dbg(&port->dev, "qt_setuart failed\n"); 1242 dev_dbg(&port->dev, "qt_setuart failed\n");
1248 return; 1243 return;
1249 } 1244 }
1250 1245
1251 /* Now determine flow control */ 1246 /* Now determine flow control */
1252 if (cflag & CRTSCTS) { 1247 if (cflag & CRTSCTS) {
1253 dev_dbg(&port->dev, "%s - Enabling HW flow control port %d\n", 1248 dev_dbg(&port->dev, "%s - Enabling HW flow control port %d\n",
1254 __func__, port->number); 1249 __func__, port->number);
1255 1250
1256 /* Enable RTS/CTS flow control */ 1251 /* Enable RTS/CTS flow control */
1257 status = BoxSetHW_FlowCtrl(port->serial, index, 1); 1252 status = BoxSetHW_FlowCtrl(port->serial, index, 1);
1258 1253
1259 if (status < 0) { 1254 if (status < 0) {
1260 dev_dbg(&port->dev, "BoxSetHW_FlowCtrl failed\n"); 1255 dev_dbg(&port->dev, "BoxSetHW_FlowCtrl failed\n");
1261 return; 1256 return;
1262 } 1257 }
1263 } else { 1258 } else {
1264 /* Disable RTS/CTS flow control */ 1259 /* Disable RTS/CTS flow control */
1265 dev_dbg(&port->dev, 1260 dev_dbg(&port->dev,
1266 "%s - disabling HW flow control port %d\n", 1261 "%s - disabling HW flow control port %d\n",
1267 __func__, port->number); 1262 __func__, port->number);
1268 1263
1269 status = BoxSetHW_FlowCtrl(port->serial, index, 0); 1264 status = BoxSetHW_FlowCtrl(port->serial, index, 0);
1270 if (status < 0) { 1265 if (status < 0) {
1271 dev_dbg(&port->dev, "BoxSetHW_FlowCtrl failed\n"); 1266 dev_dbg(&port->dev, "BoxSetHW_FlowCtrl failed\n");
1272 return; 1267 return;
1273 } 1268 }
1274 1269
1275 } 1270 }
1276 1271
1277 /* if we are implementing XON/XOFF, set the start and stop character in 1272 /* if we are implementing XON/XOFF, set the start and stop character in
1278 * the device */ 1273 * the device */
1279 if (I_IXOFF(tty) || I_IXON(tty)) { 1274 if (I_IXOFF(tty) || I_IXON(tty)) {
1280 unsigned char stop_char = STOP_CHAR(tty); 1275 unsigned char stop_char = STOP_CHAR(tty);
1281 unsigned char start_char = START_CHAR(tty); 1276 unsigned char start_char = START_CHAR(tty);
1282 status = 1277 status =
1283 BoxSetSW_FlowCtrl(port->serial, index, stop_char, 1278 BoxSetSW_FlowCtrl(port->serial, index, stop_char,
1284 start_char); 1279 start_char);
1285 if (status < 0) 1280 if (status < 0)
1286 dev_dbg(&port->dev, 1281 dev_dbg(&port->dev,
1287 "BoxSetSW_FlowCtrl (enabled) failed\n"); 1282 "BoxSetSW_FlowCtrl (enabled) failed\n");
1288 1283
1289 } else { 1284 } else {
1290 /* disable SW flow control */ 1285 /* disable SW flow control */
1291 status = BoxDisable_SW_FlowCtrl(port->serial, index); 1286 status = BoxDisable_SW_FlowCtrl(port->serial, index);
1292 if (status < 0) 1287 if (status < 0)
1293 dev_dbg(&port->dev, 1288 dev_dbg(&port->dev,
1294 "BoxSetSW_FlowCtrl (diabling) failed\n"); 1289 "BoxSetSW_FlowCtrl (diabling) failed\n");
1295 1290
1296 } 1291 }
1297 termios->c_cflag &= ~CMSPAR; 1292 termios->c_cflag &= ~CMSPAR;
1298 /* FIXME: 1293 /* FIXME:
1299 Error cases should be returning the actual bits changed only 1294 Error cases should be returning the actual bits changed only
1300 */ 1295 */
1301 } 1296 }
1302 1297
1303 static void qt_break(struct tty_struct *tty, int break_state) 1298 static void qt_break(struct tty_struct *tty, int break_state)
1304 { 1299 {
1305 struct usb_serial_port *port = tty->driver_data; 1300 struct usb_serial_port *port = tty->driver_data;
1306 struct usb_serial *serial = get_usb_serial(port, __func__); 1301 struct usb_serial *serial = get_usb_serial(port, __func__);
1307 struct quatech_port *qt_port; 1302 struct quatech_port *qt_port;
1308 u16 index, onoff; 1303 u16 index, onoff;
1309 unsigned int result; 1304 unsigned int result;
1310 1305
1311 index = tty->index - serial->minor; 1306 index = tty->index - serial->minor;
1312 1307
1313 qt_port = qt_get_port_private(port); 1308 qt_port = qt_get_port_private(port);
1314 1309
1315 if (break_state == -1) 1310 if (break_state == -1)
1316 onoff = 1; 1311 onoff = 1;
1317 else 1312 else
1318 onoff = 0; 1313 onoff = 0;
1319 1314
1320 mutex_lock(&qt_port->lock); 1315 mutex_lock(&qt_port->lock);
1321 1316
1322 result = 1317 result =
1323 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 1318 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1324 QT_BREAK_CONTROL, 0x40, onoff, index, NULL, 0, 300); 1319 QT_BREAK_CONTROL, 0x40, onoff, index, NULL, 0, 300);
1325 1320
1326 mutex_unlock(&qt_port->lock); 1321 mutex_unlock(&qt_port->lock);
1327 } 1322 }
1328 1323
1329 static inline int qt_real_tiocmget(struct tty_struct *tty, 1324 static inline int qt_real_tiocmget(struct tty_struct *tty,
1330 struct usb_serial_port *port, 1325 struct usb_serial_port *port,
1331 struct usb_serial *serial) 1326 struct usb_serial *serial)
1332 { 1327 {
1333 1328
1334 u8 mcr; 1329 u8 mcr;
1335 u8 msr; 1330 u8 msr;
1336 unsigned int result = 0; 1331 unsigned int result = 0;
1337 int status; 1332 int status;
1338 unsigned int index; 1333 unsigned int index;
1339 1334
1340 index = tty->index - serial->minor; 1335 index = tty->index - serial->minor;
1341 status = 1336 status =
1342 BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr); 1337 BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1343 if (status >= 0) { 1338 if (status >= 0) {
1344 status = 1339 status =
1345 BoxGetRegister(port->serial, index, 1340 BoxGetRegister(port->serial, index,
1346 MODEM_STATUS_REGISTER, &msr); 1341 MODEM_STATUS_REGISTER, &msr);
1347 1342
1348 } 1343 }
1349 1344
1350 if (status >= 0) { 1345 if (status >= 0) {
1351 result = ((mcr & SERIAL_MCR_DTR) ? TIOCM_DTR : 0) 1346 result = ((mcr & SERIAL_MCR_DTR) ? TIOCM_DTR : 0)
1352 /* DTR IS SET */ 1347 /* DTR IS SET */
1353 | ((mcr & SERIAL_MCR_RTS) ? TIOCM_RTS : 0) 1348 | ((mcr & SERIAL_MCR_RTS) ? TIOCM_RTS : 0)
1354 /* RTS IS SET */ 1349 /* RTS IS SET */
1355 | ((msr & SERIAL_MSR_CTS) ? TIOCM_CTS : 0) 1350 | ((msr & SERIAL_MSR_CTS) ? TIOCM_CTS : 0)
1356 /* CTS is set */ 1351 /* CTS is set */
1357 | ((msr & SERIAL_MSR_CD) ? TIOCM_CAR : 0) 1352 | ((msr & SERIAL_MSR_CD) ? TIOCM_CAR : 0)
1358 /* Carrier detect is set */ 1353 /* Carrier detect is set */
1359 | ((msr & SERIAL_MSR_RI) ? TIOCM_RI : 0) 1354 | ((msr & SERIAL_MSR_RI) ? TIOCM_RI : 0)
1360 /* Ring indicator set */ 1355 /* Ring indicator set */
1361 | ((msr & SERIAL_MSR_DSR) ? TIOCM_DSR : 0); 1356 | ((msr & SERIAL_MSR_DSR) ? TIOCM_DSR : 0);
1362 /* DSR is set */ 1357 /* DSR is set */
1363 return result; 1358 return result;
1364 1359
1365 } else 1360 } else
1366 return -ESPIPE; 1361 return -ESPIPE;
1367 } 1362 }
1368 1363
1369 static inline int qt_real_tiocmset(struct tty_struct *tty, 1364 static inline int qt_real_tiocmset(struct tty_struct *tty,
1370 struct usb_serial_port *port, 1365 struct usb_serial_port *port,
1371 struct usb_serial *serial, 1366 struct usb_serial *serial,
1372 unsigned int value) 1367 unsigned int value)
1373 { 1368 {
1374 1369
1375 u8 mcr; 1370 u8 mcr;
1376 int status; 1371 int status;
1377 unsigned int index; 1372 unsigned int index;
1378 1373
1379 index = tty->index - serial->minor; 1374 index = tty->index - serial->minor;
1380 status = 1375 status =
1381 BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr); 1376 BoxGetRegister(port->serial, index, MODEM_CONTROL_REGISTER, &mcr);
1382 if (status < 0) 1377 if (status < 0)
1383 return -ESPIPE; 1378 return -ESPIPE;
1384 1379
1385 /* 1380 /*
1386 * Turn off the RTS and DTR and loopback and then only turn on what was 1381 * Turn off the RTS and DTR and loopback and then only turn on what was
1387 * asked for 1382 * asked for
1388 */ 1383 */
1389 mcr &= ~(SERIAL_MCR_RTS | SERIAL_MCR_DTR | SERIAL_MCR_LOOP); 1384 mcr &= ~(SERIAL_MCR_RTS | SERIAL_MCR_DTR | SERIAL_MCR_LOOP);
1390 if (value & TIOCM_RTS) 1385 if (value & TIOCM_RTS)
1391 mcr |= SERIAL_MCR_RTS; 1386 mcr |= SERIAL_MCR_RTS;
1392 if (value & TIOCM_DTR) 1387 if (value & TIOCM_DTR)
1393 mcr |= SERIAL_MCR_DTR; 1388 mcr |= SERIAL_MCR_DTR;
1394 if (value & TIOCM_LOOP) 1389 if (value & TIOCM_LOOP)
1395 mcr |= SERIAL_MCR_LOOP; 1390 mcr |= SERIAL_MCR_LOOP;
1396 1391
1397 status = 1392 status =
1398 BoxSetRegister(port->serial, index, MODEM_CONTROL_REGISTER, mcr); 1393 BoxSetRegister(port->serial, index, MODEM_CONTROL_REGISTER, mcr);
1399 if (status < 0) 1394 if (status < 0)
1400 return -ESPIPE; 1395 return -ESPIPE;
1401 else 1396 else
1402 return 0; 1397 return 0;
1403 } 1398 }
1404 1399
1405 static int qt_tiocmget(struct tty_struct *tty) 1400 static int qt_tiocmget(struct tty_struct *tty)
1406 { 1401 {
1407 struct usb_serial_port *port = tty->driver_data; 1402 struct usb_serial_port *port = tty->driver_data;
1408 struct usb_serial *serial = get_usb_serial(port, __func__); 1403 struct usb_serial *serial = get_usb_serial(port, __func__);
1409 struct quatech_port *qt_port = qt_get_port_private(port); 1404 struct quatech_port *qt_port = qt_get_port_private(port);
1410 int retval; 1405 int retval;
1411 1406
1412 if (!serial) 1407 if (!serial)
1413 return -ENODEV; 1408 return -ENODEV;
1414 1409
1415 mutex_lock(&qt_port->lock); 1410 mutex_lock(&qt_port->lock);
1416 retval = qt_real_tiocmget(tty, port, serial); 1411 retval = qt_real_tiocmget(tty, port, serial);
1417 mutex_unlock(&qt_port->lock); 1412 mutex_unlock(&qt_port->lock);
1418 return retval; 1413 return retval;
1419 } 1414 }
1420 1415
1421 static int qt_tiocmset(struct tty_struct *tty, 1416 static int qt_tiocmset(struct tty_struct *tty,
1422 unsigned int set, unsigned int clear) 1417 unsigned int set, unsigned int clear)
1423 { 1418 {
1424 1419
1425 struct usb_serial_port *port = tty->driver_data; 1420 struct usb_serial_port *port = tty->driver_data;
1426 struct usb_serial *serial = get_usb_serial(port, __func__); 1421 struct usb_serial *serial = get_usb_serial(port, __func__);
1427 struct quatech_port *qt_port = qt_get_port_private(port); 1422 struct quatech_port *qt_port = qt_get_port_private(port);
1428 int retval; 1423 int retval;
1429 1424
1430 if (!serial) 1425 if (!serial)
1431 return -ENODEV; 1426 return -ENODEV;
1432 1427
1433 mutex_lock(&qt_port->lock); 1428 mutex_lock(&qt_port->lock);
1434 retval = qt_real_tiocmset(tty, port, serial, set); 1429 retval = qt_real_tiocmset(tty, port, serial, set);
1435 mutex_unlock(&qt_port->lock); 1430 mutex_unlock(&qt_port->lock);
1436 return retval; 1431 return retval;
1437 } 1432 }
1438 1433
1439 static void qt_throttle(struct tty_struct *tty) 1434 static void qt_throttle(struct tty_struct *tty)
1440 { 1435 {
1441 struct usb_serial_port *port = tty->driver_data; 1436 struct usb_serial_port *port = tty->driver_data;
1442 struct usb_serial *serial = get_usb_serial(port, __func__); 1437 struct usb_serial *serial = get_usb_serial(port, __func__);
1443 struct quatech_port *qt_port; 1438 struct quatech_port *qt_port;
1444 1439
1445 if (!serial) 1440 if (!serial)
1446 return; 1441 return;
1447 1442
1448 qt_port = qt_get_port_private(port); 1443 qt_port = qt_get_port_private(port);
1449 1444
1450 mutex_lock(&qt_port->lock); 1445 mutex_lock(&qt_port->lock);
1451 1446
1452 /* pass on to the driver specific version of this function */ 1447 /* pass on to the driver specific version of this function */
1453 qt_port->RxHolding = 1; 1448 qt_port->RxHolding = 1;
1454 1449
1455 mutex_unlock(&qt_port->lock); 1450 mutex_unlock(&qt_port->lock);
1456 } 1451 }
1457 1452
1458 static void qt_submit_urb_from_unthrottle(struct usb_serial_port *port, 1453 static void qt_submit_urb_from_unthrottle(struct usb_serial_port *port,
1459 struct usb_serial *serial) 1454 struct usb_serial *serial)
1460 { 1455 {
1461 int result; 1456 int result;
1462 1457
1463 /* Start reading from the device */ 1458 /* Start reading from the device */
1464 usb_fill_bulk_urb(port->read_urb, serial->dev, 1459 usb_fill_bulk_urb(port->read_urb, serial->dev,
1465 usb_rcvbulkpipe(serial->dev, 1460 usb_rcvbulkpipe(serial->dev,
1466 port->bulk_in_endpointAddress), 1461 port->bulk_in_endpointAddress),
1467 port->read_urb->transfer_buffer, 1462 port->read_urb->transfer_buffer,
1468 port->read_urb->transfer_buffer_length, 1463 port->read_urb->transfer_buffer_length,
1469 qt_read_bulk_callback, port); 1464 qt_read_bulk_callback, port);
1470 1465
1471 result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 1466 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1472 1467
1473 if (result) 1468 if (result)
1474 dev_err(&port->dev, 1469 dev_err(&port->dev,
1475 "%s - failed restarting read urb, error %d\n", 1470 "%s - failed restarting read urb, error %d\n",
1476 __func__, result); 1471 __func__, result);
1477 } 1472 }
1478 1473
1479 static void qt_unthrottle(struct tty_struct *tty) 1474 static void qt_unthrottle(struct tty_struct *tty)
1480 { 1475 {
1481 struct usb_serial_port *port = tty->driver_data; 1476 struct usb_serial_port *port = tty->driver_data;
1482 struct usb_serial *serial = get_usb_serial(port, __func__); 1477 struct usb_serial *serial = get_usb_serial(port, __func__);
1483 struct quatech_port *qt_port; 1478 struct quatech_port *qt_port;
1484 1479
1485 if (!serial) 1480 if (!serial)
1486 return; 1481 return;
1487 1482
1488 qt_port = qt_get_port_private(port); 1483 qt_port = qt_get_port_private(port);
1489 1484
1490 mutex_lock(&qt_port->lock); 1485 mutex_lock(&qt_port->lock);
1491 1486
1492 if (qt_port->RxHolding == 1) { 1487 if (qt_port->RxHolding == 1) {
1493 dev_dbg(&port->dev, "%s -qt_port->RxHolding == 1\n", __func__); 1488 dev_dbg(&port->dev, "%s -qt_port->RxHolding == 1\n", __func__);
1494 1489
1495 qt_port->RxHolding = 0; 1490 qt_port->RxHolding = 0;
1496 dev_dbg(&port->dev, "%s - qt_port->RxHolding = 0\n", __func__); 1491 dev_dbg(&port->dev, "%s - qt_port->RxHolding = 0\n", __func__);
1497 1492
1498 /* if we have a bulk endpoint, start it up */ 1493 /* if we have a bulk endpoint, start it up */
1499 if ((serial->num_bulk_in) && (qt_port->ReadBulkStopped == 1)) 1494 if ((serial->num_bulk_in) && (qt_port->ReadBulkStopped == 1))
1500 qt_submit_urb_from_unthrottle(port, serial); 1495 qt_submit_urb_from_unthrottle(port, serial);
1501 } 1496 }
1502 mutex_unlock(&qt_port->lock); 1497 mutex_unlock(&qt_port->lock);
1503 } 1498 }
1504 1499
1505 static int qt_calc_num_ports(struct usb_serial *serial) 1500 static int qt_calc_num_ports(struct usb_serial *serial)
1506 { 1501 {
1507 int num_ports; 1502 int num_ports;
1508 1503
1509 num_ports = 1504 num_ports =
1510 (serial->interface->cur_altsetting->desc.bNumEndpoints - 1) / 2; 1505 (serial->interface->cur_altsetting->desc.bNumEndpoints - 1) / 2;
1511 1506
1512 return num_ports; 1507 return num_ports;
1513 } 1508 }
1514 1509
1515 static struct usb_serial_driver quatech_device = { 1510 static struct usb_serial_driver quatech_device = {
1516 .driver = { 1511 .driver = {
1517 .owner = THIS_MODULE, 1512 .owner = THIS_MODULE,
1518 .name = "serqt", 1513 .name = "serqt",
1519 }, 1514 },
1520 .description = DRIVER_DESC, 1515 .description = DRIVER_DESC,
1521 .id_table = id_table, 1516 .id_table = id_table,
1522 .num_ports = 8, 1517 .num_ports = 8,
1523 .open = qt_open, 1518 .open = qt_open,
1524 .close = qt_close, 1519 .close = qt_close,
1525 .write = qt_write, 1520 .write = qt_write,
1526 .write_room = qt_write_room, 1521 .write_room = qt_write_room,
1527 .chars_in_buffer = qt_chars_in_buffer, 1522 .chars_in_buffer = qt_chars_in_buffer,
1528 .throttle = qt_throttle, 1523 .throttle = qt_throttle,
1529 .unthrottle = qt_unthrottle, 1524 .unthrottle = qt_unthrottle,
1530 .calc_num_ports = qt_calc_num_ports, 1525 .calc_num_ports = qt_calc_num_ports,
1531 .ioctl = qt_ioctl, 1526 .ioctl = qt_ioctl,
1532 .set_termios = qt_set_termios, 1527 .set_termios = qt_set_termios,
1533 .break_ctl = qt_break, 1528 .break_ctl = qt_break,
1534 .tiocmget = qt_tiocmget, 1529 .tiocmget = qt_tiocmget,
1535 .tiocmset = qt_tiocmset, 1530 .tiocmset = qt_tiocmset,
1536 .attach = qt_startup, 1531 .attach = qt_startup,
1537 .release = qt_release, 1532 .release = qt_release,
1538 }; 1533 };
1539 1534
1540 static struct usb_serial_driver * const serial_drivers[] = { 1535 static struct usb_serial_driver * const serial_drivers[] = {
1541 &quatech_device, NULL 1536 &quatech_device, NULL
1542 }; 1537 };
1543 1538
1544 module_usb_serial_driver(serial_drivers, id_table); 1539 module_usb_serial_driver(serial_drivers, id_table);
1545 1540
1546 MODULE_AUTHOR(DRIVER_AUTHOR); 1541 MODULE_AUTHOR(DRIVER_AUTHOR);
1547 MODULE_DESCRIPTION(DRIVER_DESC); 1542 MODULE_DESCRIPTION(DRIVER_DESC);
1548 MODULE_LICENSE("GPL"); 1543 MODULE_LICENSE("GPL");
1549 1544
drivers/tty/ehv_bytechan.c
1 /* ePAPR hypervisor byte channel device driver 1 /* ePAPR hypervisor byte channel device driver
2 * 2 *
3 * Copyright 2009-2011 Freescale Semiconductor, Inc. 3 * Copyright 2009-2011 Freescale Semiconductor, Inc.
4 * 4 *
5 * Author: Timur Tabi <timur@freescale.com> 5 * Author: Timur Tabi <timur@freescale.com>
6 * 6 *
7 * This file is licensed under the terms of the GNU General Public License 7 * This file is licensed under the terms of the GNU General Public License
8 * version 2. This program is licensed "as is" without any warranty of any 8 * version 2. This program is licensed "as is" without any warranty of any
9 * kind, whether express or implied. 9 * kind, whether express or implied.
10 * 10 *
11 * This driver support three distinct interfaces, all of which are related to 11 * This driver support three distinct interfaces, all of which are related to
12 * ePAPR hypervisor byte channels. 12 * ePAPR hypervisor byte channels.
13 * 13 *
14 * 1) An early-console (udbg) driver. This provides early console output 14 * 1) An early-console (udbg) driver. This provides early console output
15 * through a byte channel. The byte channel handle must be specified in a 15 * through a byte channel. The byte channel handle must be specified in a
16 * Kconfig option. 16 * Kconfig option.
17 * 17 *
18 * 2) A normal console driver. Output is sent to the byte channel designated 18 * 2) A normal console driver. Output is sent to the byte channel designated
19 * for stdout in the device tree. The console driver is for handling kernel 19 * for stdout in the device tree. The console driver is for handling kernel
20 * printk calls. 20 * printk calls.
21 * 21 *
22 * 3) A tty driver, which is used to handle user-space input and output. The 22 * 3) A tty driver, which is used to handle user-space input and output. The
23 * byte channel used for the console is designated as the default tty. 23 * byte channel used for the console is designated as the default tty.
24 */ 24 */
25 25
26 #include <linux/module.h> 26 #include <linux/module.h>
27 #include <linux/init.h> 27 #include <linux/init.h>
28 #include <linux/slab.h> 28 #include <linux/slab.h>
29 #include <linux/err.h> 29 #include <linux/err.h>
30 #include <linux/interrupt.h> 30 #include <linux/interrupt.h>
31 #include <linux/fs.h> 31 #include <linux/fs.h>
32 #include <linux/poll.h> 32 #include <linux/poll.h>
33 #include <asm/epapr_hcalls.h> 33 #include <asm/epapr_hcalls.h>
34 #include <linux/of.h> 34 #include <linux/of.h>
35 #include <linux/platform_device.h> 35 #include <linux/platform_device.h>
36 #include <linux/cdev.h> 36 #include <linux/cdev.h>
37 #include <linux/console.h> 37 #include <linux/console.h>
38 #include <linux/tty.h> 38 #include <linux/tty.h>
39 #include <linux/tty_flip.h> 39 #include <linux/tty_flip.h>
40 #include <linux/circ_buf.h> 40 #include <linux/circ_buf.h>
41 #include <asm/udbg.h> 41 #include <asm/udbg.h>
42 42
43 /* The size of the transmit circular buffer. This must be a power of two. */ 43 /* The size of the transmit circular buffer. This must be a power of two. */
44 #define BUF_SIZE 2048 44 #define BUF_SIZE 2048
45 45
46 /* Per-byte channel private data */ 46 /* Per-byte channel private data */
47 struct ehv_bc_data { 47 struct ehv_bc_data {
48 struct device *dev; 48 struct device *dev;
49 struct tty_port port; 49 struct tty_port port;
50 uint32_t handle; 50 uint32_t handle;
51 unsigned int rx_irq; 51 unsigned int rx_irq;
52 unsigned int tx_irq; 52 unsigned int tx_irq;
53 53
54 spinlock_t lock; /* lock for transmit buffer */ 54 spinlock_t lock; /* lock for transmit buffer */
55 unsigned char buf[BUF_SIZE]; /* transmit circular buffer */ 55 unsigned char buf[BUF_SIZE]; /* transmit circular buffer */
56 unsigned int head; /* circular buffer head */ 56 unsigned int head; /* circular buffer head */
57 unsigned int tail; /* circular buffer tail */ 57 unsigned int tail; /* circular buffer tail */
58 58
59 int tx_irq_enabled; /* true == TX interrupt is enabled */ 59 int tx_irq_enabled; /* true == TX interrupt is enabled */
60 }; 60 };
61 61
62 /* Array of byte channel objects */ 62 /* Array of byte channel objects */
63 static struct ehv_bc_data *bcs; 63 static struct ehv_bc_data *bcs;
64 64
65 /* Byte channel handle for stdout (and stdin), taken from device tree */ 65 /* Byte channel handle for stdout (and stdin), taken from device tree */
66 static unsigned int stdout_bc; 66 static unsigned int stdout_bc;
67 67
68 /* Virtual IRQ for the byte channel handle for stdin, taken from device tree */ 68 /* Virtual IRQ for the byte channel handle for stdin, taken from device tree */
69 static unsigned int stdout_irq; 69 static unsigned int stdout_irq;
70 70
71 /**************************** SUPPORT FUNCTIONS ****************************/ 71 /**************************** SUPPORT FUNCTIONS ****************************/
72 72
73 /* 73 /*
74 * Enable the transmit interrupt 74 * Enable the transmit interrupt
75 * 75 *
76 * Unlike a serial device, byte channels have no mechanism for disabling their 76 * Unlike a serial device, byte channels have no mechanism for disabling their
77 * own receive or transmit interrupts. To emulate that feature, we toggle 77 * own receive or transmit interrupts. To emulate that feature, we toggle
78 * the IRQ in the kernel. 78 * the IRQ in the kernel.
79 * 79 *
80 * We cannot just blindly call enable_irq() or disable_irq(), because these 80 * We cannot just blindly call enable_irq() or disable_irq(), because these
81 * calls are reference counted. This means that we cannot call enable_irq() 81 * calls are reference counted. This means that we cannot call enable_irq()
82 * if interrupts are already enabled. This can happen in two situations: 82 * if interrupts are already enabled. This can happen in two situations:
83 * 83 *
84 * 1. The tty layer makes two back-to-back calls to ehv_bc_tty_write() 84 * 1. The tty layer makes two back-to-back calls to ehv_bc_tty_write()
85 * 2. A transmit interrupt occurs while executing ehv_bc_tx_dequeue() 85 * 2. A transmit interrupt occurs while executing ehv_bc_tx_dequeue()
86 * 86 *
87 * To work around this, we keep a flag to tell us if the IRQ is enabled or not. 87 * To work around this, we keep a flag to tell us if the IRQ is enabled or not.
88 */ 88 */
89 static void enable_tx_interrupt(struct ehv_bc_data *bc) 89 static void enable_tx_interrupt(struct ehv_bc_data *bc)
90 { 90 {
91 if (!bc->tx_irq_enabled) { 91 if (!bc->tx_irq_enabled) {
92 enable_irq(bc->tx_irq); 92 enable_irq(bc->tx_irq);
93 bc->tx_irq_enabled = 1; 93 bc->tx_irq_enabled = 1;
94 } 94 }
95 } 95 }
96 96
97 static void disable_tx_interrupt(struct ehv_bc_data *bc) 97 static void disable_tx_interrupt(struct ehv_bc_data *bc)
98 { 98 {
99 if (bc->tx_irq_enabled) { 99 if (bc->tx_irq_enabled) {
100 disable_irq_nosync(bc->tx_irq); 100 disable_irq_nosync(bc->tx_irq);
101 bc->tx_irq_enabled = 0; 101 bc->tx_irq_enabled = 0;
102 } 102 }
103 } 103 }
104 104
105 /* 105 /*
106 * find the byte channel handle to use for the console 106 * find the byte channel handle to use for the console
107 * 107 *
108 * The byte channel to be used for the console is specified via a "stdout" 108 * The byte channel to be used for the console is specified via a "stdout"
109 * property in the /chosen node. 109 * property in the /chosen node.
110 * 110 *
111 * For compatible with legacy device trees, we also look for a "stdout" alias. 111 * For compatible with legacy device trees, we also look for a "stdout" alias.
112 */ 112 */
113 static int find_console_handle(void) 113 static int find_console_handle(void)
114 { 114 {
115 struct device_node *np, *np2; 115 struct device_node *np, *np2;
116 const char *sprop = NULL; 116 const char *sprop = NULL;
117 const uint32_t *iprop; 117 const uint32_t *iprop;
118 118
119 np = of_find_node_by_path("/chosen"); 119 np = of_find_node_by_path("/chosen");
120 if (np) 120 if (np)
121 sprop = of_get_property(np, "stdout-path", NULL); 121 sprop = of_get_property(np, "stdout-path", NULL);
122 122
123 if (!np || !sprop) { 123 if (!np || !sprop) {
124 of_node_put(np); 124 of_node_put(np);
125 np = of_find_node_by_name(NULL, "aliases"); 125 np = of_find_node_by_name(NULL, "aliases");
126 if (np) 126 if (np)
127 sprop = of_get_property(np, "stdout", NULL); 127 sprop = of_get_property(np, "stdout", NULL);
128 } 128 }
129 129
130 if (!sprop) { 130 if (!sprop) {
131 of_node_put(np); 131 of_node_put(np);
132 return 0; 132 return 0;
133 } 133 }
134 134
135 /* We don't care what the aliased node is actually called. We only 135 /* We don't care what the aliased node is actually called. We only
136 * care if it's compatible with "epapr,hv-byte-channel", because that 136 * care if it's compatible with "epapr,hv-byte-channel", because that
137 * indicates that it's a byte channel node. We use a temporary 137 * indicates that it's a byte channel node. We use a temporary
138 * variable, 'np2', because we can't release 'np' until we're done with 138 * variable, 'np2', because we can't release 'np' until we're done with
139 * 'sprop'. 139 * 'sprop'.
140 */ 140 */
141 np2 = of_find_node_by_path(sprop); 141 np2 = of_find_node_by_path(sprop);
142 of_node_put(np); 142 of_node_put(np);
143 np = np2; 143 np = np2;
144 if (!np) { 144 if (!np) {
145 pr_warning("ehv-bc: stdout node '%s' does not exist\n", sprop); 145 pr_warning("ehv-bc: stdout node '%s' does not exist\n", sprop);
146 return 0; 146 return 0;
147 } 147 }
148 148
149 /* Is it a byte channel? */ 149 /* Is it a byte channel? */
150 if (!of_device_is_compatible(np, "epapr,hv-byte-channel")) { 150 if (!of_device_is_compatible(np, "epapr,hv-byte-channel")) {
151 of_node_put(np); 151 of_node_put(np);
152 return 0; 152 return 0;
153 } 153 }
154 154
155 stdout_irq = irq_of_parse_and_map(np, 0); 155 stdout_irq = irq_of_parse_and_map(np, 0);
156 if (stdout_irq == NO_IRQ) { 156 if (stdout_irq == NO_IRQ) {
157 pr_err("ehv-bc: no 'interrupts' property in %s node\n", sprop); 157 pr_err("ehv-bc: no 'interrupts' property in %s node\n", sprop);
158 of_node_put(np); 158 of_node_put(np);
159 return 0; 159 return 0;
160 } 160 }
161 161
162 /* 162 /*
163 * The 'hv-handle' property contains the handle for this byte channel. 163 * The 'hv-handle' property contains the handle for this byte channel.
164 */ 164 */
165 iprop = of_get_property(np, "hv-handle", NULL); 165 iprop = of_get_property(np, "hv-handle", NULL);
166 if (!iprop) { 166 if (!iprop) {
167 pr_err("ehv-bc: no 'hv-handle' property in %s node\n", 167 pr_err("ehv-bc: no 'hv-handle' property in %s node\n",
168 np->name); 168 np->name);
169 of_node_put(np); 169 of_node_put(np);
170 return 0; 170 return 0;
171 } 171 }
172 stdout_bc = be32_to_cpu(*iprop); 172 stdout_bc = be32_to_cpu(*iprop);
173 173
174 of_node_put(np); 174 of_node_put(np);
175 return 1; 175 return 1;
176 } 176 }
177 177
178 /*************************** EARLY CONSOLE DRIVER ***************************/ 178 /*************************** EARLY CONSOLE DRIVER ***************************/
179 179
180 #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC 180 #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
181 181
182 /* 182 /*
183 * send a byte to a byte channel, wait if necessary 183 * send a byte to a byte channel, wait if necessary
184 * 184 *
185 * This function sends a byte to a byte channel, and it waits and 185 * This function sends a byte to a byte channel, and it waits and
186 * retries if the byte channel is full. It returns if the character 186 * retries if the byte channel is full. It returns if the character
187 * has been sent, or if some error has occurred. 187 * has been sent, or if some error has occurred.
188 * 188 *
189 */ 189 */
190 static void byte_channel_spin_send(const char data) 190 static void byte_channel_spin_send(const char data)
191 { 191 {
192 int ret, count; 192 int ret, count;
193 193
194 do { 194 do {
195 count = 1; 195 count = 1;
196 ret = ev_byte_channel_send(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE, 196 ret = ev_byte_channel_send(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
197 &count, &data); 197 &count, &data);
198 } while (ret == EV_EAGAIN); 198 } while (ret == EV_EAGAIN);
199 } 199 }
200 200
201 /* 201 /*
202 * The udbg subsystem calls this function to display a single character. 202 * The udbg subsystem calls this function to display a single character.
203 * We convert CR to a CR/LF. 203 * We convert CR to a CR/LF.
204 */ 204 */
205 static void ehv_bc_udbg_putc(char c) 205 static void ehv_bc_udbg_putc(char c)
206 { 206 {
207 if (c == '\n') 207 if (c == '\n')
208 byte_channel_spin_send('\r'); 208 byte_channel_spin_send('\r');
209 209
210 byte_channel_spin_send(c); 210 byte_channel_spin_send(c);
211 } 211 }
212 212
213 /* 213 /*
214 * early console initialization 214 * early console initialization
215 * 215 *
216 * PowerPC kernels support an early printk console, also known as udbg. 216 * PowerPC kernels support an early printk console, also known as udbg.
217 * This function must be called via the ppc_md.init_early function pointer. 217 * This function must be called via the ppc_md.init_early function pointer.
218 * At this point, the device tree has been unflattened, so we can obtain the 218 * At this point, the device tree has been unflattened, so we can obtain the
219 * byte channel handle for stdout. 219 * byte channel handle for stdout.
220 * 220 *
221 * We only support displaying of characters (putc). We do not support 221 * We only support displaying of characters (putc). We do not support
222 * keyboard input. 222 * keyboard input.
223 */ 223 */
224 void __init udbg_init_ehv_bc(void) 224 void __init udbg_init_ehv_bc(void)
225 { 225 {
226 unsigned int rx_count, tx_count; 226 unsigned int rx_count, tx_count;
227 unsigned int ret; 227 unsigned int ret;
228 228
229 /* Verify the byte channel handle */ 229 /* Verify the byte channel handle */
230 ret = ev_byte_channel_poll(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE, 230 ret = ev_byte_channel_poll(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
231 &rx_count, &tx_count); 231 &rx_count, &tx_count);
232 if (ret) 232 if (ret)
233 return; 233 return;
234 234
235 udbg_putc = ehv_bc_udbg_putc; 235 udbg_putc = ehv_bc_udbg_putc;
236 register_early_udbg_console(); 236 register_early_udbg_console();
237 237
238 udbg_printf("ehv-bc: early console using byte channel handle %u\n", 238 udbg_printf("ehv-bc: early console using byte channel handle %u\n",
239 CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE); 239 CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
240 } 240 }
241 241
242 #endif 242 #endif
243 243
244 /****************************** CONSOLE DRIVER ******************************/ 244 /****************************** CONSOLE DRIVER ******************************/
245 245
246 static struct tty_driver *ehv_bc_driver; 246 static struct tty_driver *ehv_bc_driver;
247 247
248 /* 248 /*
249 * Byte channel console sending worker function. 249 * Byte channel console sending worker function.
250 * 250 *
251 * For consoles, if the output buffer is full, we should just spin until it 251 * For consoles, if the output buffer is full, we should just spin until it
252 * clears. 252 * clears.
253 */ 253 */
254 static int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s, 254 static int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s,
255 unsigned int count) 255 unsigned int count)
256 { 256 {
257 unsigned int len; 257 unsigned int len;
258 int ret = 0; 258 int ret = 0;
259 259
260 while (count) { 260 while (count) {
261 len = min_t(unsigned int, count, EV_BYTE_CHANNEL_MAX_BYTES); 261 len = min_t(unsigned int, count, EV_BYTE_CHANNEL_MAX_BYTES);
262 do { 262 do {
263 ret = ev_byte_channel_send(handle, &len, s); 263 ret = ev_byte_channel_send(handle, &len, s);
264 } while (ret == EV_EAGAIN); 264 } while (ret == EV_EAGAIN);
265 count -= len; 265 count -= len;
266 s += len; 266 s += len;
267 } 267 }
268 268
269 return ret; 269 return ret;
270 } 270 }
271 271
272 /* 272 /*
273 * write a string to the console 273 * write a string to the console
274 * 274 *
275 * This function gets called to write a string from the kernel, typically from 275 * This function gets called to write a string from the kernel, typically from
276 * a printk(). This function spins until all data is written. 276 * a printk(). This function spins until all data is written.
277 * 277 *
278 * We copy the data to a temporary buffer because we need to insert a \r in 278 * We copy the data to a temporary buffer because we need to insert a \r in
279 * front of every \n. It's more efficient to copy the data to the buffer than 279 * front of every \n. It's more efficient to copy the data to the buffer than
280 * it is to make multiple hcalls for each character or each newline. 280 * it is to make multiple hcalls for each character or each newline.
281 */ 281 */
282 static void ehv_bc_console_write(struct console *co, const char *s, 282 static void ehv_bc_console_write(struct console *co, const char *s,
283 unsigned int count) 283 unsigned int count)
284 { 284 {
285 char s2[EV_BYTE_CHANNEL_MAX_BYTES]; 285 char s2[EV_BYTE_CHANNEL_MAX_BYTES];
286 unsigned int i, j = 0; 286 unsigned int i, j = 0;
287 char c; 287 char c;
288 288
289 for (i = 0; i < count; i++) { 289 for (i = 0; i < count; i++) {
290 c = *s++; 290 c = *s++;
291 291
292 if (c == '\n') 292 if (c == '\n')
293 s2[j++] = '\r'; 293 s2[j++] = '\r';
294 294
295 s2[j++] = c; 295 s2[j++] = c;
296 if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) { 296 if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) {
297 if (ehv_bc_console_byte_channel_send(stdout_bc, s2, j)) 297 if (ehv_bc_console_byte_channel_send(stdout_bc, s2, j))
298 return; 298 return;
299 j = 0; 299 j = 0;
300 } 300 }
301 } 301 }
302 302
303 if (j) 303 if (j)
304 ehv_bc_console_byte_channel_send(stdout_bc, s2, j); 304 ehv_bc_console_byte_channel_send(stdout_bc, s2, j);
305 } 305 }
306 306
307 /* 307 /*
308 * When /dev/console is opened, the kernel iterates the console list looking 308 * When /dev/console is opened, the kernel iterates the console list looking
309 * for one with ->device and then calls that method. On success, it expects 309 * for one with ->device and then calls that method. On success, it expects
310 * the passed-in int* to contain the minor number to use. 310 * the passed-in int* to contain the minor number to use.
311 */ 311 */
312 static struct tty_driver *ehv_bc_console_device(struct console *co, int *index) 312 static struct tty_driver *ehv_bc_console_device(struct console *co, int *index)
313 { 313 {
314 *index = co->index; 314 *index = co->index;
315 315
316 return ehv_bc_driver; 316 return ehv_bc_driver;
317 } 317 }
318 318
319 static struct console ehv_bc_console = { 319 static struct console ehv_bc_console = {
320 .name = "ttyEHV", 320 .name = "ttyEHV",
321 .write = ehv_bc_console_write, 321 .write = ehv_bc_console_write,
322 .device = ehv_bc_console_device, 322 .device = ehv_bc_console_device,
323 .flags = CON_PRINTBUFFER | CON_ENABLED, 323 .flags = CON_PRINTBUFFER | CON_ENABLED,
324 }; 324 };
325 325
326 /* 326 /*
327 * Console initialization 327 * Console initialization
328 * 328 *
329 * This is the first function that is called after the device tree is 329 * This is the first function that is called after the device tree is
330 * available, so here is where we determine the byte channel handle and IRQ for 330 * available, so here is where we determine the byte channel handle and IRQ for
331 * stdout/stdin, even though that information is used by the tty and character 331 * stdout/stdin, even though that information is used by the tty and character
332 * drivers. 332 * drivers.
333 */ 333 */
334 static int __init ehv_bc_console_init(void) 334 static int __init ehv_bc_console_init(void)
335 { 335 {
336 if (!find_console_handle()) { 336 if (!find_console_handle()) {
337 pr_debug("ehv-bc: stdout is not a byte channel\n"); 337 pr_debug("ehv-bc: stdout is not a byte channel\n");
338 return -ENODEV; 338 return -ENODEV;
339 } 339 }
340 340
341 #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC 341 #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
342 /* Print a friendly warning if the user chose the wrong byte channel 342 /* Print a friendly warning if the user chose the wrong byte channel
343 * handle for udbg. 343 * handle for udbg.
344 */ 344 */
345 if (stdout_bc != CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE) 345 if (stdout_bc != CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE)
346 pr_warning("ehv-bc: udbg handle %u is not the stdout handle\n", 346 pr_warning("ehv-bc: udbg handle %u is not the stdout handle\n",
347 CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE); 347 CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
348 #endif 348 #endif
349 349
350 /* add_preferred_console() must be called before register_console(), 350 /* add_preferred_console() must be called before register_console(),
351 otherwise it won't work. However, we don't want to enumerate all the 351 otherwise it won't work. However, we don't want to enumerate all the
352 byte channels here, either, since we only care about one. */ 352 byte channels here, either, since we only care about one. */
353 353
354 add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL); 354 add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL);
355 register_console(&ehv_bc_console); 355 register_console(&ehv_bc_console);
356 356
357 pr_info("ehv-bc: registered console driver for byte channel %u\n", 357 pr_info("ehv-bc: registered console driver for byte channel %u\n",
358 stdout_bc); 358 stdout_bc);
359 359
360 return 0; 360 return 0;
361 } 361 }
362 console_initcall(ehv_bc_console_init); 362 console_initcall(ehv_bc_console_init);
363 363
364 /******************************** TTY DRIVER ********************************/ 364 /******************************** TTY DRIVER ********************************/
365 365
366 /* 366 /*
367 * byte channel receive interupt handler 367 * byte channel receive interupt handler
368 * 368 *
369 * This ISR is called whenever data is available on a byte channel. 369 * This ISR is called whenever data is available on a byte channel.
370 */ 370 */
371 static irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data) 371 static irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data)
372 { 372 {
373 struct ehv_bc_data *bc = data; 373 struct ehv_bc_data *bc = data;
374 unsigned int rx_count, tx_count, len; 374 unsigned int rx_count, tx_count, len;
375 int count; 375 int count;
376 char buffer[EV_BYTE_CHANNEL_MAX_BYTES]; 376 char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
377 int ret; 377 int ret;
378 378
379 /* Find out how much data needs to be read, and then ask the TTY layer 379 /* Find out how much data needs to be read, and then ask the TTY layer
380 * if it can handle that much. We want to ensure that every byte we 380 * if it can handle that much. We want to ensure that every byte we
381 * read from the byte channel will be accepted by the TTY layer. 381 * read from the byte channel will be accepted by the TTY layer.
382 */ 382 */
383 ev_byte_channel_poll(bc->handle, &rx_count, &tx_count); 383 ev_byte_channel_poll(bc->handle, &rx_count, &tx_count);
384 count = tty_buffer_request_room(&bc->port, rx_count); 384 count = tty_buffer_request_room(&bc->port, rx_count);
385 385
386 /* 'count' is the maximum amount of data the TTY layer can accept at 386 /* 'count' is the maximum amount of data the TTY layer can accept at
387 * this time. However, during testing, I was never able to get 'count' 387 * this time. However, during testing, I was never able to get 'count'
388 * to be less than 'rx_count'. I'm not sure whether I'm calling it 388 * to be less than 'rx_count'. I'm not sure whether I'm calling it
389 * correctly. 389 * correctly.
390 */ 390 */
391 391
392 while (count > 0) { 392 while (count > 0) {
393 len = min_t(unsigned int, count, sizeof(buffer)); 393 len = min_t(unsigned int, count, sizeof(buffer));
394 394
395 /* Read some data from the byte channel. This function will 395 /* Read some data from the byte channel. This function will
396 * never return more than EV_BYTE_CHANNEL_MAX_BYTES bytes. 396 * never return more than EV_BYTE_CHANNEL_MAX_BYTES bytes.
397 */ 397 */
398 ev_byte_channel_receive(bc->handle, &len, buffer); 398 ev_byte_channel_receive(bc->handle, &len, buffer);
399 399
400 /* 'len' is now the amount of data that's been received. 'len' 400 /* 'len' is now the amount of data that's been received. 'len'
401 * can't be zero, and most likely it's equal to one. 401 * can't be zero, and most likely it's equal to one.
402 */ 402 */
403 403
404 /* Pass the received data to the tty layer. */ 404 /* Pass the received data to the tty layer. */
405 ret = tty_insert_flip_string(&bc->port, buffer, len); 405 ret = tty_insert_flip_string(&bc->port, buffer, len);
406 406
407 /* 'ret' is the number of bytes that the TTY layer accepted. 407 /* 'ret' is the number of bytes that the TTY layer accepted.
408 * If it's not equal to 'len', then it means the buffer is 408 * If it's not equal to 'len', then it means the buffer is
409 * full, which should never happen. If it does happen, we can 409 * full, which should never happen. If it does happen, we can
410 * exit gracefully, but we drop the last 'len - ret' characters 410 * exit gracefully, but we drop the last 'len - ret' characters
411 * that we read from the byte channel. 411 * that we read from the byte channel.
412 */ 412 */
413 if (ret != len) 413 if (ret != len)
414 break; 414 break;
415 415
416 count -= len; 416 count -= len;
417 } 417 }
418 418
419 /* Tell the tty layer that we're done. */ 419 /* Tell the tty layer that we're done. */
420 tty_flip_buffer_push(&bc->port); 420 tty_flip_buffer_push(&bc->port);
421 421
422 return IRQ_HANDLED; 422 return IRQ_HANDLED;
423 } 423 }
424 424
425 /* 425 /*
426 * dequeue the transmit buffer to the hypervisor 426 * dequeue the transmit buffer to the hypervisor
427 * 427 *
428 * This function, which can be called in interrupt context, dequeues as much 428 * This function, which can be called in interrupt context, dequeues as much
429 * data as possible from the transmit buffer to the byte channel. 429 * data as possible from the transmit buffer to the byte channel.
430 */ 430 */
431 static void ehv_bc_tx_dequeue(struct ehv_bc_data *bc) 431 static void ehv_bc_tx_dequeue(struct ehv_bc_data *bc)
432 { 432 {
433 unsigned int count; 433 unsigned int count;
434 unsigned int len, ret; 434 unsigned int len, ret;
435 unsigned long flags; 435 unsigned long flags;
436 436
437 do { 437 do {
438 spin_lock_irqsave(&bc->lock, flags); 438 spin_lock_irqsave(&bc->lock, flags);
439 len = min_t(unsigned int, 439 len = min_t(unsigned int,
440 CIRC_CNT_TO_END(bc->head, bc->tail, BUF_SIZE), 440 CIRC_CNT_TO_END(bc->head, bc->tail, BUF_SIZE),
441 EV_BYTE_CHANNEL_MAX_BYTES); 441 EV_BYTE_CHANNEL_MAX_BYTES);
442 442
443 ret = ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail); 443 ret = ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail);
444 444
445 /* 'len' is valid only if the return code is 0 or EV_EAGAIN */ 445 /* 'len' is valid only if the return code is 0 or EV_EAGAIN */
446 if (!ret || (ret == EV_EAGAIN)) 446 if (!ret || (ret == EV_EAGAIN))
447 bc->tail = (bc->tail + len) & (BUF_SIZE - 1); 447 bc->tail = (bc->tail + len) & (BUF_SIZE - 1);
448 448
449 count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE); 449 count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE);
450 spin_unlock_irqrestore(&bc->lock, flags); 450 spin_unlock_irqrestore(&bc->lock, flags);
451 } while (count && !ret); 451 } while (count && !ret);
452 452
453 spin_lock_irqsave(&bc->lock, flags); 453 spin_lock_irqsave(&bc->lock, flags);
454 if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE)) 454 if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE))
455 /* 455 /*
456 * If we haven't emptied the buffer, then enable the TX IRQ. 456 * If we haven't emptied the buffer, then enable the TX IRQ.
457 * We'll get an interrupt when there's more room in the 457 * We'll get an interrupt when there's more room in the
458 * hypervisor's output buffer. 458 * hypervisor's output buffer.
459 */ 459 */
460 enable_tx_interrupt(bc); 460 enable_tx_interrupt(bc);
461 else 461 else
462 disable_tx_interrupt(bc); 462 disable_tx_interrupt(bc);
463 spin_unlock_irqrestore(&bc->lock, flags); 463 spin_unlock_irqrestore(&bc->lock, flags);
464 } 464 }
465 465
466 /* 466 /*
467 * byte channel transmit interupt handler 467 * byte channel transmit interupt handler
468 * 468 *
469 * This ISR is called whenever space becomes available for transmitting 469 * This ISR is called whenever space becomes available for transmitting
470 * characters on a byte channel. 470 * characters on a byte channel.
471 */ 471 */
472 static irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data) 472 static irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data)
473 { 473 {
474 struct ehv_bc_data *bc = data; 474 struct ehv_bc_data *bc = data;
475 struct tty_struct *ttys = tty_port_tty_get(&bc->port);
476 475
477 ehv_bc_tx_dequeue(bc); 476 ehv_bc_tx_dequeue(bc);
478 if (ttys) { 477 tty_port_tty_wakeup(&bc->port);
479 tty_wakeup(ttys);
480 tty_kref_put(ttys);
481 }
482 478
483 return IRQ_HANDLED; 479 return IRQ_HANDLED;
484 } 480 }
485 481
486 /* 482 /*
487 * This function is called when the tty layer has data for us send. We store 483 * This function is called when the tty layer has data for us send. We store
488 * the data first in a circular buffer, and then dequeue as much of that data 484 * the data first in a circular buffer, and then dequeue as much of that data
489 * as possible. 485 * as possible.
490 * 486 *
491 * We don't need to worry about whether there is enough room in the buffer for 487 * We don't need to worry about whether there is enough room in the buffer for
492 * all the data. The purpose of ehv_bc_tty_write_room() is to tell the tty 488 * all the data. The purpose of ehv_bc_tty_write_room() is to tell the tty
493 * layer how much data it can safely send to us. We guarantee that 489 * layer how much data it can safely send to us. We guarantee that
494 * ehv_bc_tty_write_room() will never lie, so the tty layer will never send us 490 * ehv_bc_tty_write_room() will never lie, so the tty layer will never send us
495 * too much data. 491 * too much data.
496 */ 492 */
497 static int ehv_bc_tty_write(struct tty_struct *ttys, const unsigned char *s, 493 static int ehv_bc_tty_write(struct tty_struct *ttys, const unsigned char *s,
498 int count) 494 int count)
499 { 495 {
500 struct ehv_bc_data *bc = ttys->driver_data; 496 struct ehv_bc_data *bc = ttys->driver_data;
501 unsigned long flags; 497 unsigned long flags;
502 unsigned int len; 498 unsigned int len;
503 unsigned int written = 0; 499 unsigned int written = 0;
504 500
505 while (1) { 501 while (1) {
506 spin_lock_irqsave(&bc->lock, flags); 502 spin_lock_irqsave(&bc->lock, flags);
507 len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE); 503 len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE);
508 if (count < len) 504 if (count < len)
509 len = count; 505 len = count;
510 if (len) { 506 if (len) {
511 memcpy(bc->buf + bc->head, s, len); 507 memcpy(bc->buf + bc->head, s, len);
512 bc->head = (bc->head + len) & (BUF_SIZE - 1); 508 bc->head = (bc->head + len) & (BUF_SIZE - 1);
513 } 509 }
514 spin_unlock_irqrestore(&bc->lock, flags); 510 spin_unlock_irqrestore(&bc->lock, flags);
515 if (!len) 511 if (!len)
516 break; 512 break;
517 513
518 s += len; 514 s += len;
519 count -= len; 515 count -= len;
520 written += len; 516 written += len;
521 } 517 }
522 518
523 ehv_bc_tx_dequeue(bc); 519 ehv_bc_tx_dequeue(bc);
524 520
525 return written; 521 return written;
526 } 522 }
527 523
528 /* 524 /*
529 * This function can be called multiple times for a given tty_struct, which is 525 * This function can be called multiple times for a given tty_struct, which is
530 * why we initialize bc->ttys in ehv_bc_tty_port_activate() instead. 526 * why we initialize bc->ttys in ehv_bc_tty_port_activate() instead.
531 * 527 *
532 * The tty layer will still call this function even if the device was not 528 * The tty layer will still call this function even if the device was not
533 * registered (i.e. tty_register_device() was not called). This happens 529 * registered (i.e. tty_register_device() was not called). This happens
534 * because tty_register_device() is optional and some legacy drivers don't 530 * because tty_register_device() is optional and some legacy drivers don't
535 * use it. So we need to check for that. 531 * use it. So we need to check for that.
536 */ 532 */
537 static int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp) 533 static int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp)
538 { 534 {
539 struct ehv_bc_data *bc = &bcs[ttys->index]; 535 struct ehv_bc_data *bc = &bcs[ttys->index];
540 536
541 if (!bc->dev) 537 if (!bc->dev)
542 return -ENODEV; 538 return -ENODEV;
543 539
544 return tty_port_open(&bc->port, ttys, filp); 540 return tty_port_open(&bc->port, ttys, filp);
545 } 541 }
546 542
547 /* 543 /*
548 * Amazingly, if ehv_bc_tty_open() returns an error code, the tty layer will 544 * Amazingly, if ehv_bc_tty_open() returns an error code, the tty layer will
549 * still call this function to close the tty device. So we can't assume that 545 * still call this function to close the tty device. So we can't assume that
550 * the tty port has been initialized. 546 * the tty port has been initialized.
551 */ 547 */
552 static void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp) 548 static void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp)
553 { 549 {
554 struct ehv_bc_data *bc = &bcs[ttys->index]; 550 struct ehv_bc_data *bc = &bcs[ttys->index];
555 551
556 if (bc->dev) 552 if (bc->dev)
557 tty_port_close(&bc->port, ttys, filp); 553 tty_port_close(&bc->port, ttys, filp);
558 } 554 }
559 555
560 /* 556 /*
561 * Return the amount of space in the output buffer 557 * Return the amount of space in the output buffer
562 * 558 *
563 * This is actually a contract between the driver and the tty layer outlining 559 * This is actually a contract between the driver and the tty layer outlining
564 * how much write room the driver can guarantee will be sent OR BUFFERED. This 560 * how much write room the driver can guarantee will be sent OR BUFFERED. This
565 * driver MUST honor the return value. 561 * driver MUST honor the return value.
566 */ 562 */
567 static int ehv_bc_tty_write_room(struct tty_struct *ttys) 563 static int ehv_bc_tty_write_room(struct tty_struct *ttys)
568 { 564 {
569 struct ehv_bc_data *bc = ttys->driver_data; 565 struct ehv_bc_data *bc = ttys->driver_data;
570 unsigned long flags; 566 unsigned long flags;
571 int count; 567 int count;
572 568
573 spin_lock_irqsave(&bc->lock, flags); 569 spin_lock_irqsave(&bc->lock, flags);
574 count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE); 570 count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE);
575 spin_unlock_irqrestore(&bc->lock, flags); 571 spin_unlock_irqrestore(&bc->lock, flags);
576 572
577 return count; 573 return count;
578 } 574 }
579 575
580 /* 576 /*
581 * Stop sending data to the tty layer 577 * Stop sending data to the tty layer
582 * 578 *
583 * This function is called when the tty layer's input buffers are getting full, 579 * This function is called when the tty layer's input buffers are getting full,
584 * so the driver should stop sending it data. The easiest way to do this is to 580 * so the driver should stop sending it data. The easiest way to do this is to
585 * disable the RX IRQ, which will prevent ehv_bc_tty_rx_isr() from being 581 * disable the RX IRQ, which will prevent ehv_bc_tty_rx_isr() from being
586 * called. 582 * called.
587 * 583 *
588 * The hypervisor will continue to queue up any incoming data. If there is any 584 * The hypervisor will continue to queue up any incoming data. If there is any
589 * data in the queue when the RX interrupt is enabled, we'll immediately get an 585 * data in the queue when the RX interrupt is enabled, we'll immediately get an
590 * RX interrupt. 586 * RX interrupt.
591 */ 587 */
592 static void ehv_bc_tty_throttle(struct tty_struct *ttys) 588 static void ehv_bc_tty_throttle(struct tty_struct *ttys)
593 { 589 {
594 struct ehv_bc_data *bc = ttys->driver_data; 590 struct ehv_bc_data *bc = ttys->driver_data;
595 591
596 disable_irq(bc->rx_irq); 592 disable_irq(bc->rx_irq);
597 } 593 }
598 594
599 /* 595 /*
600 * Resume sending data to the tty layer 596 * Resume sending data to the tty layer
601 * 597 *
602 * This function is called after previously calling ehv_bc_tty_throttle(). The 598 * This function is called after previously calling ehv_bc_tty_throttle(). The
603 * tty layer's input buffers now have more room, so the driver can resume 599 * tty layer's input buffers now have more room, so the driver can resume
604 * sending it data. 600 * sending it data.
605 */ 601 */
606 static void ehv_bc_tty_unthrottle(struct tty_struct *ttys) 602 static void ehv_bc_tty_unthrottle(struct tty_struct *ttys)
607 { 603 {
608 struct ehv_bc_data *bc = ttys->driver_data; 604 struct ehv_bc_data *bc = ttys->driver_data;
609 605
610 /* If there is any data in the queue when the RX interrupt is enabled, 606 /* If there is any data in the queue when the RX interrupt is enabled,
611 * we'll immediately get an RX interrupt. 607 * we'll immediately get an RX interrupt.
612 */ 608 */
613 enable_irq(bc->rx_irq); 609 enable_irq(bc->rx_irq);
614 } 610 }
615 611
616 static void ehv_bc_tty_hangup(struct tty_struct *ttys) 612 static void ehv_bc_tty_hangup(struct tty_struct *ttys)
617 { 613 {
618 struct ehv_bc_data *bc = ttys->driver_data; 614 struct ehv_bc_data *bc = ttys->driver_data;
619 615
620 ehv_bc_tx_dequeue(bc); 616 ehv_bc_tx_dequeue(bc);
621 tty_port_hangup(&bc->port); 617 tty_port_hangup(&bc->port);
622 } 618 }
623 619
624 /* 620 /*
625 * TTY driver operations 621 * TTY driver operations
626 * 622 *
627 * If we could ask the hypervisor how much data is still in the TX buffer, or 623 * If we could ask the hypervisor how much data is still in the TX buffer, or
628 * at least how big the TX buffers are, then we could implement the 624 * at least how big the TX buffers are, then we could implement the
629 * .wait_until_sent and .chars_in_buffer functions. 625 * .wait_until_sent and .chars_in_buffer functions.
630 */ 626 */
631 static const struct tty_operations ehv_bc_ops = { 627 static const struct tty_operations ehv_bc_ops = {
632 .open = ehv_bc_tty_open, 628 .open = ehv_bc_tty_open,
633 .close = ehv_bc_tty_close, 629 .close = ehv_bc_tty_close,
634 .write = ehv_bc_tty_write, 630 .write = ehv_bc_tty_write,
635 .write_room = ehv_bc_tty_write_room, 631 .write_room = ehv_bc_tty_write_room,
636 .throttle = ehv_bc_tty_throttle, 632 .throttle = ehv_bc_tty_throttle,
637 .unthrottle = ehv_bc_tty_unthrottle, 633 .unthrottle = ehv_bc_tty_unthrottle,
638 .hangup = ehv_bc_tty_hangup, 634 .hangup = ehv_bc_tty_hangup,
639 }; 635 };
640 636
641 /* 637 /*
642 * initialize the TTY port 638 * initialize the TTY port
643 * 639 *
644 * This function will only be called once, no matter how many times 640 * This function will only be called once, no matter how many times
645 * ehv_bc_tty_open() is called. That's why we register the ISR here, and also 641 * ehv_bc_tty_open() is called. That's why we register the ISR here, and also
646 * why we initialize tty_struct-related variables here. 642 * why we initialize tty_struct-related variables here.
647 */ 643 */
648 static int ehv_bc_tty_port_activate(struct tty_port *port, 644 static int ehv_bc_tty_port_activate(struct tty_port *port,
649 struct tty_struct *ttys) 645 struct tty_struct *ttys)
650 { 646 {
651 struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port); 647 struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
652 int ret; 648 int ret;
653 649
654 ttys->driver_data = bc; 650 ttys->driver_data = bc;
655 651
656 ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc); 652 ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc);
657 if (ret < 0) { 653 if (ret < 0) {
658 dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n", 654 dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n",
659 bc->rx_irq, ret); 655 bc->rx_irq, ret);
660 return ret; 656 return ret;
661 } 657 }
662 658
663 /* request_irq also enables the IRQ */ 659 /* request_irq also enables the IRQ */
664 bc->tx_irq_enabled = 1; 660 bc->tx_irq_enabled = 1;
665 661
666 ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc); 662 ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc);
667 if (ret < 0) { 663 if (ret < 0) {
668 dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n", 664 dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n",
669 bc->tx_irq, ret); 665 bc->tx_irq, ret);
670 free_irq(bc->rx_irq, bc); 666 free_irq(bc->rx_irq, bc);
671 return ret; 667 return ret;
672 } 668 }
673 669
674 /* The TX IRQ is enabled only when we can't write all the data to the 670 /* The TX IRQ is enabled only when we can't write all the data to the
675 * byte channel at once, so by default it's disabled. 671 * byte channel at once, so by default it's disabled.
676 */ 672 */
677 disable_tx_interrupt(bc); 673 disable_tx_interrupt(bc);
678 674
679 return 0; 675 return 0;
680 } 676 }
681 677
682 static void ehv_bc_tty_port_shutdown(struct tty_port *port) 678 static void ehv_bc_tty_port_shutdown(struct tty_port *port)
683 { 679 {
684 struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port); 680 struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
685 681
686 free_irq(bc->tx_irq, bc); 682 free_irq(bc->tx_irq, bc);
687 free_irq(bc->rx_irq, bc); 683 free_irq(bc->rx_irq, bc);
688 } 684 }
689 685
690 static const struct tty_port_operations ehv_bc_tty_port_ops = { 686 static const struct tty_port_operations ehv_bc_tty_port_ops = {
691 .activate = ehv_bc_tty_port_activate, 687 .activate = ehv_bc_tty_port_activate,
692 .shutdown = ehv_bc_tty_port_shutdown, 688 .shutdown = ehv_bc_tty_port_shutdown,
693 }; 689 };
694 690
695 static int ehv_bc_tty_probe(struct platform_device *pdev) 691 static int ehv_bc_tty_probe(struct platform_device *pdev)
696 { 692 {
697 struct device_node *np = pdev->dev.of_node; 693 struct device_node *np = pdev->dev.of_node;
698 struct ehv_bc_data *bc; 694 struct ehv_bc_data *bc;
699 const uint32_t *iprop; 695 const uint32_t *iprop;
700 unsigned int handle; 696 unsigned int handle;
701 int ret; 697 int ret;
702 static unsigned int index = 1; 698 static unsigned int index = 1;
703 unsigned int i; 699 unsigned int i;
704 700
705 iprop = of_get_property(np, "hv-handle", NULL); 701 iprop = of_get_property(np, "hv-handle", NULL);
706 if (!iprop) { 702 if (!iprop) {
707 dev_err(&pdev->dev, "no 'hv-handle' property in %s node\n", 703 dev_err(&pdev->dev, "no 'hv-handle' property in %s node\n",
708 np->name); 704 np->name);
709 return -ENODEV; 705 return -ENODEV;
710 } 706 }
711 707
712 /* We already told the console layer that the index for the console 708 /* We already told the console layer that the index for the console
713 * device is zero, so we need to make sure that we use that index when 709 * device is zero, so we need to make sure that we use that index when
714 * we probe the console byte channel node. 710 * we probe the console byte channel node.
715 */ 711 */
716 handle = be32_to_cpu(*iprop); 712 handle = be32_to_cpu(*iprop);
717 i = (handle == stdout_bc) ? 0 : index++; 713 i = (handle == stdout_bc) ? 0 : index++;
718 bc = &bcs[i]; 714 bc = &bcs[i];
719 715
720 bc->handle = handle; 716 bc->handle = handle;
721 bc->head = 0; 717 bc->head = 0;
722 bc->tail = 0; 718 bc->tail = 0;
723 spin_lock_init(&bc->lock); 719 spin_lock_init(&bc->lock);
724 720
725 bc->rx_irq = irq_of_parse_and_map(np, 0); 721 bc->rx_irq = irq_of_parse_and_map(np, 0);
726 bc->tx_irq = irq_of_parse_and_map(np, 1); 722 bc->tx_irq = irq_of_parse_and_map(np, 1);
727 if ((bc->rx_irq == NO_IRQ) || (bc->tx_irq == NO_IRQ)) { 723 if ((bc->rx_irq == NO_IRQ) || (bc->tx_irq == NO_IRQ)) {
728 dev_err(&pdev->dev, "no 'interrupts' property in %s node\n", 724 dev_err(&pdev->dev, "no 'interrupts' property in %s node\n",
729 np->name); 725 np->name);
730 ret = -ENODEV; 726 ret = -ENODEV;
731 goto error; 727 goto error;
732 } 728 }
733 729
734 tty_port_init(&bc->port); 730 tty_port_init(&bc->port);
735 bc->port.ops = &ehv_bc_tty_port_ops; 731 bc->port.ops = &ehv_bc_tty_port_ops;
736 732
737 bc->dev = tty_port_register_device(&bc->port, ehv_bc_driver, i, 733 bc->dev = tty_port_register_device(&bc->port, ehv_bc_driver, i,
738 &pdev->dev); 734 &pdev->dev);
739 if (IS_ERR(bc->dev)) { 735 if (IS_ERR(bc->dev)) {
740 ret = PTR_ERR(bc->dev); 736 ret = PTR_ERR(bc->dev);
741 dev_err(&pdev->dev, "could not register tty (ret=%i)\n", ret); 737 dev_err(&pdev->dev, "could not register tty (ret=%i)\n", ret);
742 goto error; 738 goto error;
743 } 739 }
744 740
745 dev_set_drvdata(&pdev->dev, bc); 741 dev_set_drvdata(&pdev->dev, bc);
746 742
747 dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n", 743 dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n",
748 ehv_bc_driver->name, i, bc->handle); 744 ehv_bc_driver->name, i, bc->handle);
749 745
750 return 0; 746 return 0;
751 747
752 error: 748 error:
753 tty_port_destroy(&bc->port); 749 tty_port_destroy(&bc->port);
754 irq_dispose_mapping(bc->tx_irq); 750 irq_dispose_mapping(bc->tx_irq);
755 irq_dispose_mapping(bc->rx_irq); 751 irq_dispose_mapping(bc->rx_irq);
756 752
757 memset(bc, 0, sizeof(struct ehv_bc_data)); 753 memset(bc, 0, sizeof(struct ehv_bc_data));
758 return ret; 754 return ret;
759 } 755 }
760 756
761 static int ehv_bc_tty_remove(struct platform_device *pdev) 757 static int ehv_bc_tty_remove(struct platform_device *pdev)
762 { 758 {
763 struct ehv_bc_data *bc = dev_get_drvdata(&pdev->dev); 759 struct ehv_bc_data *bc = dev_get_drvdata(&pdev->dev);
764 760
765 tty_unregister_device(ehv_bc_driver, bc - bcs); 761 tty_unregister_device(ehv_bc_driver, bc - bcs);
766 762
767 tty_port_destroy(&bc->port); 763 tty_port_destroy(&bc->port);
768 irq_dispose_mapping(bc->tx_irq); 764 irq_dispose_mapping(bc->tx_irq);
769 irq_dispose_mapping(bc->rx_irq); 765 irq_dispose_mapping(bc->rx_irq);
770 766
771 return 0; 767 return 0;
772 } 768 }
773 769
774 static const struct of_device_id ehv_bc_tty_of_ids[] = { 770 static const struct of_device_id ehv_bc_tty_of_ids[] = {
775 { .compatible = "epapr,hv-byte-channel" }, 771 { .compatible = "epapr,hv-byte-channel" },
776 {} 772 {}
777 }; 773 };
778 774
779 static struct platform_driver ehv_bc_tty_driver = { 775 static struct platform_driver ehv_bc_tty_driver = {
780 .driver = { 776 .driver = {
781 .owner = THIS_MODULE, 777 .owner = THIS_MODULE,
782 .name = "ehv-bc", 778 .name = "ehv-bc",
783 .of_match_table = ehv_bc_tty_of_ids, 779 .of_match_table = ehv_bc_tty_of_ids,
784 }, 780 },
785 .probe = ehv_bc_tty_probe, 781 .probe = ehv_bc_tty_probe,
786 .remove = ehv_bc_tty_remove, 782 .remove = ehv_bc_tty_remove,
787 }; 783 };
788 784
789 /** 785 /**
790 * ehv_bc_init - ePAPR hypervisor byte channel driver initialization 786 * ehv_bc_init - ePAPR hypervisor byte channel driver initialization
791 * 787 *
792 * This function is called when this module is loaded. 788 * This function is called when this module is loaded.
793 */ 789 */
794 static int __init ehv_bc_init(void) 790 static int __init ehv_bc_init(void)
795 { 791 {
796 struct device_node *np; 792 struct device_node *np;
797 unsigned int count = 0; /* Number of elements in bcs[] */ 793 unsigned int count = 0; /* Number of elements in bcs[] */
798 int ret; 794 int ret;
799 795
800 pr_info("ePAPR hypervisor byte channel driver\n"); 796 pr_info("ePAPR hypervisor byte channel driver\n");
801 797
802 /* Count the number of byte channels */ 798 /* Count the number of byte channels */
803 for_each_compatible_node(np, NULL, "epapr,hv-byte-channel") 799 for_each_compatible_node(np, NULL, "epapr,hv-byte-channel")
804 count++; 800 count++;
805 801
806 if (!count) 802 if (!count)
807 return -ENODEV; 803 return -ENODEV;
808 804
809 /* The array index of an element in bcs[] is the same as the tty index 805 /* The array index of an element in bcs[] is the same as the tty index
810 * for that element. If you know the address of an element in the 806 * for that element. If you know the address of an element in the
811 * array, then you can use pointer math (e.g. "bc - bcs") to get its 807 * array, then you can use pointer math (e.g. "bc - bcs") to get its
812 * tty index. 808 * tty index.
813 */ 809 */
814 bcs = kzalloc(count * sizeof(struct ehv_bc_data), GFP_KERNEL); 810 bcs = kzalloc(count * sizeof(struct ehv_bc_data), GFP_KERNEL);
815 if (!bcs) 811 if (!bcs)
816 return -ENOMEM; 812 return -ENOMEM;
817 813
818 ehv_bc_driver = alloc_tty_driver(count); 814 ehv_bc_driver = alloc_tty_driver(count);
819 if (!ehv_bc_driver) { 815 if (!ehv_bc_driver) {
820 ret = -ENOMEM; 816 ret = -ENOMEM;
821 goto error; 817 goto error;
822 } 818 }
823 819
824 ehv_bc_driver->driver_name = "ehv-bc"; 820 ehv_bc_driver->driver_name = "ehv-bc";
825 ehv_bc_driver->name = ehv_bc_console.name; 821 ehv_bc_driver->name = ehv_bc_console.name;
826 ehv_bc_driver->type = TTY_DRIVER_TYPE_CONSOLE; 822 ehv_bc_driver->type = TTY_DRIVER_TYPE_CONSOLE;
827 ehv_bc_driver->subtype = SYSTEM_TYPE_CONSOLE; 823 ehv_bc_driver->subtype = SYSTEM_TYPE_CONSOLE;
828 ehv_bc_driver->init_termios = tty_std_termios; 824 ehv_bc_driver->init_termios = tty_std_termios;
829 ehv_bc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 825 ehv_bc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
830 tty_set_operations(ehv_bc_driver, &ehv_bc_ops); 826 tty_set_operations(ehv_bc_driver, &ehv_bc_ops);
831 827
832 ret = tty_register_driver(ehv_bc_driver); 828 ret = tty_register_driver(ehv_bc_driver);
833 if (ret) { 829 if (ret) {
834 pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret); 830 pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret);
835 goto error; 831 goto error;
836 } 832 }
837 833
838 ret = platform_driver_register(&ehv_bc_tty_driver); 834 ret = platform_driver_register(&ehv_bc_tty_driver);
839 if (ret) { 835 if (ret) {
840 pr_err("ehv-bc: could not register platform driver (ret=%i)\n", 836 pr_err("ehv-bc: could not register platform driver (ret=%i)\n",
841 ret); 837 ret);
842 goto error; 838 goto error;
843 } 839 }
844 840
845 return 0; 841 return 0;
846 842
847 error: 843 error:
848 if (ehv_bc_driver) { 844 if (ehv_bc_driver) {
849 tty_unregister_driver(ehv_bc_driver); 845 tty_unregister_driver(ehv_bc_driver);
850 put_tty_driver(ehv_bc_driver); 846 put_tty_driver(ehv_bc_driver);
851 } 847 }
852 848
853 kfree(bcs); 849 kfree(bcs);
854 850
855 return ret; 851 return ret;
856 } 852 }
857 853
858 854
859 /** 855 /**
860 * ehv_bc_exit - ePAPR hypervisor byte channel driver termination 856 * ehv_bc_exit - ePAPR hypervisor byte channel driver termination
861 * 857 *
862 * This function is called when this driver is unloaded. 858 * This function is called when this driver is unloaded.
863 */ 859 */
864 static void __exit ehv_bc_exit(void) 860 static void __exit ehv_bc_exit(void)
865 { 861 {
866 tty_unregister_driver(ehv_bc_driver); 862 tty_unregister_driver(ehv_bc_driver);
867 put_tty_driver(ehv_bc_driver); 863 put_tty_driver(ehv_bc_driver);
868 kfree(bcs); 864 kfree(bcs);
869 } 865 }
870 866
871 module_init(ehv_bc_init); 867 module_init(ehv_bc_init);
872 module_exit(ehv_bc_exit); 868 module_exit(ehv_bc_exit);
873 869
874 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); 870 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
875 MODULE_DESCRIPTION("ePAPR hypervisor byte channel driver"); 871 MODULE_DESCRIPTION("ePAPR hypervisor byte channel driver");
876 MODULE_LICENSE("GPL v2"); 872 MODULE_LICENSE("GPL v2");
877 873
drivers/tty/hvc/hvsi.c
1 /* 1 /*
2 * Copyright (C) 2004 Hollis Blanchard <hollisb@us.ibm.com>, IBM 2 * Copyright (C) 2004 Hollis Blanchard <hollisb@us.ibm.com>, IBM
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or 6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version. 7 * (at your option) any later version.
8 * 8 *
9 * This program is distributed in the hope that it will be useful, 9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software 15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */ 17 */
18 18
19 /* Host Virtual Serial Interface (HVSI) is a protocol between the hosted OS 19 /* Host Virtual Serial Interface (HVSI) is a protocol between the hosted OS
20 * and the service processor on IBM pSeries servers. On these servers, there 20 * and the service processor on IBM pSeries servers. On these servers, there
21 * are no serial ports under the OS's control, and sometimes there is no other 21 * are no serial ports under the OS's control, and sometimes there is no other
22 * console available either. However, the service processor has two standard 22 * console available either. However, the service processor has two standard
23 * serial ports, so this over-complicated protocol allows the OS to control 23 * serial ports, so this over-complicated protocol allows the OS to control
24 * those ports by proxy. 24 * those ports by proxy.
25 * 25 *
26 * Besides data, the procotol supports the reading/writing of the serial 26 * Besides data, the procotol supports the reading/writing of the serial
27 * port's DTR line, and the reading of the CD line. This is to allow the OS to 27 * port's DTR line, and the reading of the CD line. This is to allow the OS to
28 * control a modem attached to the service processor's serial port. Note that 28 * control a modem attached to the service processor's serial port. Note that
29 * the OS cannot change the speed of the port through this protocol. 29 * the OS cannot change the speed of the port through this protocol.
30 */ 30 */
31 31
32 #undef DEBUG 32 #undef DEBUG
33 33
34 #include <linux/console.h> 34 #include <linux/console.h>
35 #include <linux/ctype.h> 35 #include <linux/ctype.h>
36 #include <linux/delay.h> 36 #include <linux/delay.h>
37 #include <linux/init.h> 37 #include <linux/init.h>
38 #include <linux/interrupt.h> 38 #include <linux/interrupt.h>
39 #include <linux/module.h> 39 #include <linux/module.h>
40 #include <linux/major.h> 40 #include <linux/major.h>
41 #include <linux/kernel.h> 41 #include <linux/kernel.h>
42 #include <linux/spinlock.h> 42 #include <linux/spinlock.h>
43 #include <linux/sysrq.h> 43 #include <linux/sysrq.h>
44 #include <linux/tty.h> 44 #include <linux/tty.h>
45 #include <linux/tty_flip.h> 45 #include <linux/tty_flip.h>
46 #include <asm/hvcall.h> 46 #include <asm/hvcall.h>
47 #include <asm/hvconsole.h> 47 #include <asm/hvconsole.h>
48 #include <asm/prom.h> 48 #include <asm/prom.h>
49 #include <asm/uaccess.h> 49 #include <asm/uaccess.h>
50 #include <asm/vio.h> 50 #include <asm/vio.h>
51 #include <asm/param.h> 51 #include <asm/param.h>
52 #include <asm/hvsi.h> 52 #include <asm/hvsi.h>
53 53
54 #define HVSI_MAJOR 229 54 #define HVSI_MAJOR 229
55 #define HVSI_MINOR 128 55 #define HVSI_MINOR 128
56 #define MAX_NR_HVSI_CONSOLES 4 56 #define MAX_NR_HVSI_CONSOLES 4
57 57
58 #define HVSI_TIMEOUT (5*HZ) 58 #define HVSI_TIMEOUT (5*HZ)
59 #define HVSI_VERSION 1 59 #define HVSI_VERSION 1
60 #define HVSI_MAX_PACKET 256 60 #define HVSI_MAX_PACKET 256
61 #define HVSI_MAX_READ 16 61 #define HVSI_MAX_READ 16
62 #define HVSI_MAX_OUTGOING_DATA 12 62 #define HVSI_MAX_OUTGOING_DATA 12
63 #define N_OUTBUF 12 63 #define N_OUTBUF 12
64 64
65 /* 65 /*
66 * we pass data via two 8-byte registers, so we would like our char arrays 66 * we pass data via two 8-byte registers, so we would like our char arrays
67 * properly aligned for those loads. 67 * properly aligned for those loads.
68 */ 68 */
69 #define __ALIGNED__ __attribute__((__aligned__(sizeof(long)))) 69 #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
70 70
71 struct hvsi_struct { 71 struct hvsi_struct {
72 struct tty_port port; 72 struct tty_port port;
73 struct delayed_work writer; 73 struct delayed_work writer;
74 struct work_struct handshaker; 74 struct work_struct handshaker;
75 wait_queue_head_t emptyq; /* woken when outbuf is emptied */ 75 wait_queue_head_t emptyq; /* woken when outbuf is emptied */
76 wait_queue_head_t stateq; /* woken when HVSI state changes */ 76 wait_queue_head_t stateq; /* woken when HVSI state changes */
77 spinlock_t lock; 77 spinlock_t lock;
78 int index; 78 int index;
79 uint8_t throttle_buf[128]; 79 uint8_t throttle_buf[128];
80 uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */ 80 uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */
81 /* inbuf is for packet reassembly. leave a little room for leftovers. */ 81 /* inbuf is for packet reassembly. leave a little room for leftovers. */
82 uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ]; 82 uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ];
83 uint8_t *inbuf_end; 83 uint8_t *inbuf_end;
84 int n_throttle; 84 int n_throttle;
85 int n_outbuf; 85 int n_outbuf;
86 uint32_t vtermno; 86 uint32_t vtermno;
87 uint32_t virq; 87 uint32_t virq;
88 atomic_t seqno; /* HVSI packet sequence number */ 88 atomic_t seqno; /* HVSI packet sequence number */
89 uint16_t mctrl; 89 uint16_t mctrl;
90 uint8_t state; /* HVSI protocol state */ 90 uint8_t state; /* HVSI protocol state */
91 uint8_t flags; 91 uint8_t flags;
92 #ifdef CONFIG_MAGIC_SYSRQ 92 #ifdef CONFIG_MAGIC_SYSRQ
93 uint8_t sysrq; 93 uint8_t sysrq;
94 #endif /* CONFIG_MAGIC_SYSRQ */ 94 #endif /* CONFIG_MAGIC_SYSRQ */
95 }; 95 };
96 static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES]; 96 static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES];
97 97
98 static struct tty_driver *hvsi_driver; 98 static struct tty_driver *hvsi_driver;
99 static int hvsi_count; 99 static int hvsi_count;
100 static int (*hvsi_wait)(struct hvsi_struct *hp, int state); 100 static int (*hvsi_wait)(struct hvsi_struct *hp, int state);
101 101
102 enum HVSI_PROTOCOL_STATE { 102 enum HVSI_PROTOCOL_STATE {
103 HVSI_CLOSED, 103 HVSI_CLOSED,
104 HVSI_WAIT_FOR_VER_RESPONSE, 104 HVSI_WAIT_FOR_VER_RESPONSE,
105 HVSI_WAIT_FOR_VER_QUERY, 105 HVSI_WAIT_FOR_VER_QUERY,
106 HVSI_OPEN, 106 HVSI_OPEN,
107 HVSI_WAIT_FOR_MCTRL_RESPONSE, 107 HVSI_WAIT_FOR_MCTRL_RESPONSE,
108 HVSI_FSP_DIED, 108 HVSI_FSP_DIED,
109 }; 109 };
110 #define HVSI_CONSOLE 0x1 110 #define HVSI_CONSOLE 0x1
111 111
112 static inline int is_console(struct hvsi_struct *hp) 112 static inline int is_console(struct hvsi_struct *hp)
113 { 113 {
114 return hp->flags & HVSI_CONSOLE; 114 return hp->flags & HVSI_CONSOLE;
115 } 115 }
116 116
117 static inline int is_open(struct hvsi_struct *hp) 117 static inline int is_open(struct hvsi_struct *hp)
118 { 118 {
119 /* if we're waiting for an mctrl then we're already open */ 119 /* if we're waiting for an mctrl then we're already open */
120 return (hp->state == HVSI_OPEN) 120 return (hp->state == HVSI_OPEN)
121 || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE); 121 || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
122 } 122 }
123 123
124 static inline void print_state(struct hvsi_struct *hp) 124 static inline void print_state(struct hvsi_struct *hp)
125 { 125 {
126 #ifdef DEBUG 126 #ifdef DEBUG
127 static const char *state_names[] = { 127 static const char *state_names[] = {
128 "HVSI_CLOSED", 128 "HVSI_CLOSED",
129 "HVSI_WAIT_FOR_VER_RESPONSE", 129 "HVSI_WAIT_FOR_VER_RESPONSE",
130 "HVSI_WAIT_FOR_VER_QUERY", 130 "HVSI_WAIT_FOR_VER_QUERY",
131 "HVSI_OPEN", 131 "HVSI_OPEN",
132 "HVSI_WAIT_FOR_MCTRL_RESPONSE", 132 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
133 "HVSI_FSP_DIED", 133 "HVSI_FSP_DIED",
134 }; 134 };
135 const char *name = (hp->state < ARRAY_SIZE(state_names)) 135 const char *name = (hp->state < ARRAY_SIZE(state_names))
136 ? state_names[hp->state] : "UNKNOWN"; 136 ? state_names[hp->state] : "UNKNOWN";
137 137
138 pr_debug("hvsi%i: state = %s\n", hp->index, name); 138 pr_debug("hvsi%i: state = %s\n", hp->index, name);
139 #endif /* DEBUG */ 139 #endif /* DEBUG */
140 } 140 }
141 141
142 static inline void __set_state(struct hvsi_struct *hp, int state) 142 static inline void __set_state(struct hvsi_struct *hp, int state)
143 { 143 {
144 hp->state = state; 144 hp->state = state;
145 print_state(hp); 145 print_state(hp);
146 wake_up_all(&hp->stateq); 146 wake_up_all(&hp->stateq);
147 } 147 }
148 148
149 static inline void set_state(struct hvsi_struct *hp, int state) 149 static inline void set_state(struct hvsi_struct *hp, int state)
150 { 150 {
151 unsigned long flags; 151 unsigned long flags;
152 152
153 spin_lock_irqsave(&hp->lock, flags); 153 spin_lock_irqsave(&hp->lock, flags);
154 __set_state(hp, state); 154 __set_state(hp, state);
155 spin_unlock_irqrestore(&hp->lock, flags); 155 spin_unlock_irqrestore(&hp->lock, flags);
156 } 156 }
157 157
158 static inline int len_packet(const uint8_t *packet) 158 static inline int len_packet(const uint8_t *packet)
159 { 159 {
160 return (int)((struct hvsi_header *)packet)->len; 160 return (int)((struct hvsi_header *)packet)->len;
161 } 161 }
162 162
163 static inline int is_header(const uint8_t *packet) 163 static inline int is_header(const uint8_t *packet)
164 { 164 {
165 struct hvsi_header *header = (struct hvsi_header *)packet; 165 struct hvsi_header *header = (struct hvsi_header *)packet;
166 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER; 166 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
167 } 167 }
168 168
169 static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet) 169 static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
170 { 170 {
171 if (hp->inbuf_end < packet + sizeof(struct hvsi_header)) 171 if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
172 return 0; /* don't even have the packet header */ 172 return 0; /* don't even have the packet header */
173 173
174 if (hp->inbuf_end < (packet + len_packet(packet))) 174 if (hp->inbuf_end < (packet + len_packet(packet)))
175 return 0; /* don't have the rest of the packet */ 175 return 0; /* don't have the rest of the packet */
176 176
177 return 1; 177 return 1;
178 } 178 }
179 179
180 /* shift remaining bytes in packetbuf down */ 180 /* shift remaining bytes in packetbuf down */
181 static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to) 181 static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
182 { 182 {
183 int remaining = (int)(hp->inbuf_end - read_to); 183 int remaining = (int)(hp->inbuf_end - read_to);
184 184
185 pr_debug("%s: %i chars remain\n", __func__, remaining); 185 pr_debug("%s: %i chars remain\n", __func__, remaining);
186 186
187 if (read_to != hp->inbuf) 187 if (read_to != hp->inbuf)
188 memmove(hp->inbuf, read_to, remaining); 188 memmove(hp->inbuf, read_to, remaining);
189 189
190 hp->inbuf_end = hp->inbuf + remaining; 190 hp->inbuf_end = hp->inbuf + remaining;
191 } 191 }
192 192
193 #ifdef DEBUG 193 #ifdef DEBUG
194 #define dbg_dump_packet(packet) dump_packet(packet) 194 #define dbg_dump_packet(packet) dump_packet(packet)
195 #define dbg_dump_hex(data, len) dump_hex(data, len) 195 #define dbg_dump_hex(data, len) dump_hex(data, len)
196 #else 196 #else
197 #define dbg_dump_packet(packet) do { } while (0) 197 #define dbg_dump_packet(packet) do { } while (0)
198 #define dbg_dump_hex(data, len) do { } while (0) 198 #define dbg_dump_hex(data, len) do { } while (0)
199 #endif 199 #endif
200 200
201 static void dump_hex(const uint8_t *data, int len) 201 static void dump_hex(const uint8_t *data, int len)
202 { 202 {
203 int i; 203 int i;
204 204
205 printk(" "); 205 printk(" ");
206 for (i=0; i < len; i++) 206 for (i=0; i < len; i++)
207 printk("%.2x", data[i]); 207 printk("%.2x", data[i]);
208 208
209 printk("\n "); 209 printk("\n ");
210 for (i=0; i < len; i++) { 210 for (i=0; i < len; i++) {
211 if (isprint(data[i])) 211 if (isprint(data[i]))
212 printk("%c", data[i]); 212 printk("%c", data[i]);
213 else 213 else
214 printk("."); 214 printk(".");
215 } 215 }
216 printk("\n"); 216 printk("\n");
217 } 217 }
218 218
219 static void dump_packet(uint8_t *packet) 219 static void dump_packet(uint8_t *packet)
220 { 220 {
221 struct hvsi_header *header = (struct hvsi_header *)packet; 221 struct hvsi_header *header = (struct hvsi_header *)packet;
222 222
223 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len, 223 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
224 header->seqno); 224 header->seqno);
225 225
226 dump_hex(packet, header->len); 226 dump_hex(packet, header->len);
227 } 227 }
228 228
229 static int hvsi_read(struct hvsi_struct *hp, char *buf, int count) 229 static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
230 { 230 {
231 unsigned long got; 231 unsigned long got;
232 232
233 got = hvc_get_chars(hp->vtermno, buf, count); 233 got = hvc_get_chars(hp->vtermno, buf, count);
234 234
235 return got; 235 return got;
236 } 236 }
237 237
238 static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet, 238 static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
239 struct tty_struct *tty, struct hvsi_struct **to_handshake) 239 struct tty_struct *tty, struct hvsi_struct **to_handshake)
240 { 240 {
241 struct hvsi_control *header = (struct hvsi_control *)packet; 241 struct hvsi_control *header = (struct hvsi_control *)packet;
242 242
243 switch (header->verb) { 243 switch (header->verb) {
244 case VSV_MODEM_CTL_UPDATE: 244 case VSV_MODEM_CTL_UPDATE:
245 if ((header->word & HVSI_TSCD) == 0) { 245 if ((header->word & HVSI_TSCD) == 0) {
246 /* CD went away; no more connection */ 246 /* CD went away; no more connection */
247 pr_debug("hvsi%i: CD dropped\n", hp->index); 247 pr_debug("hvsi%i: CD dropped\n", hp->index);
248 hp->mctrl &= TIOCM_CD; 248 hp->mctrl &= TIOCM_CD;
249 if (tty && !C_CLOCAL(tty)) 249 if (tty && !C_CLOCAL(tty))
250 tty_hangup(tty); 250 tty_hangup(tty);
251 } 251 }
252 break; 252 break;
253 case VSV_CLOSE_PROTOCOL: 253 case VSV_CLOSE_PROTOCOL:
254 pr_debug("hvsi%i: service processor came back\n", hp->index); 254 pr_debug("hvsi%i: service processor came back\n", hp->index);
255 if (hp->state != HVSI_CLOSED) { 255 if (hp->state != HVSI_CLOSED) {
256 *to_handshake = hp; 256 *to_handshake = hp;
257 } 257 }
258 break; 258 break;
259 default: 259 default:
260 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ", 260 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
261 hp->index); 261 hp->index);
262 dump_packet(packet); 262 dump_packet(packet);
263 break; 263 break;
264 } 264 }
265 } 265 }
266 266
267 static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet) 267 static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
268 { 268 {
269 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet; 269 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
270 270
271 switch (hp->state) { 271 switch (hp->state) {
272 case HVSI_WAIT_FOR_VER_RESPONSE: 272 case HVSI_WAIT_FOR_VER_RESPONSE:
273 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY); 273 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
274 break; 274 break;
275 case HVSI_WAIT_FOR_MCTRL_RESPONSE: 275 case HVSI_WAIT_FOR_MCTRL_RESPONSE:
276 hp->mctrl = 0; 276 hp->mctrl = 0;
277 if (resp->u.mctrl_word & HVSI_TSDTR) 277 if (resp->u.mctrl_word & HVSI_TSDTR)
278 hp->mctrl |= TIOCM_DTR; 278 hp->mctrl |= TIOCM_DTR;
279 if (resp->u.mctrl_word & HVSI_TSCD) 279 if (resp->u.mctrl_word & HVSI_TSCD)
280 hp->mctrl |= TIOCM_CD; 280 hp->mctrl |= TIOCM_CD;
281 __set_state(hp, HVSI_OPEN); 281 __set_state(hp, HVSI_OPEN);
282 break; 282 break;
283 default: 283 default:
284 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index); 284 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
285 dump_packet(packet); 285 dump_packet(packet);
286 break; 286 break;
287 } 287 }
288 } 288 }
289 289
290 /* respond to service processor's version query */ 290 /* respond to service processor's version query */
291 static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno) 291 static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
292 { 292 {
293 struct hvsi_query_response packet __ALIGNED__; 293 struct hvsi_query_response packet __ALIGNED__;
294 int wrote; 294 int wrote;
295 295
296 packet.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER; 296 packet.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER;
297 packet.hdr.len = sizeof(struct hvsi_query_response); 297 packet.hdr.len = sizeof(struct hvsi_query_response);
298 packet.hdr.seqno = atomic_inc_return(&hp->seqno); 298 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
299 packet.verb = VSV_SEND_VERSION_NUMBER; 299 packet.verb = VSV_SEND_VERSION_NUMBER;
300 packet.u.version = HVSI_VERSION; 300 packet.u.version = HVSI_VERSION;
301 packet.query_seqno = query_seqno+1; 301 packet.query_seqno = query_seqno+1;
302 302
303 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len); 303 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
304 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len); 304 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
305 305
306 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len); 306 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
307 if (wrote != packet.hdr.len) { 307 if (wrote != packet.hdr.len) {
308 printk(KERN_ERR "hvsi%i: couldn't send query response!\n", 308 printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
309 hp->index); 309 hp->index);
310 return -EIO; 310 return -EIO;
311 } 311 }
312 312
313 return 0; 313 return 0;
314 } 314 }
315 315
316 static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet) 316 static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
317 { 317 {
318 struct hvsi_query *query = (struct hvsi_query *)packet; 318 struct hvsi_query *query = (struct hvsi_query *)packet;
319 319
320 switch (hp->state) { 320 switch (hp->state) {
321 case HVSI_WAIT_FOR_VER_QUERY: 321 case HVSI_WAIT_FOR_VER_QUERY:
322 hvsi_version_respond(hp, query->hdr.seqno); 322 hvsi_version_respond(hp, query->hdr.seqno);
323 __set_state(hp, HVSI_OPEN); 323 __set_state(hp, HVSI_OPEN);
324 break; 324 break;
325 default: 325 default:
326 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index); 326 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
327 dump_packet(packet); 327 dump_packet(packet);
328 break; 328 break;
329 } 329 }
330 } 330 }
331 331
332 static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len) 332 static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
333 { 333 {
334 int i; 334 int i;
335 335
336 for (i=0; i < len; i++) { 336 for (i=0; i < len; i++) {
337 char c = buf[i]; 337 char c = buf[i];
338 #ifdef CONFIG_MAGIC_SYSRQ 338 #ifdef CONFIG_MAGIC_SYSRQ
339 if (c == '\0') { 339 if (c == '\0') {
340 hp->sysrq = 1; 340 hp->sysrq = 1;
341 continue; 341 continue;
342 } else if (hp->sysrq) { 342 } else if (hp->sysrq) {
343 handle_sysrq(c); 343 handle_sysrq(c);
344 hp->sysrq = 0; 344 hp->sysrq = 0;
345 continue; 345 continue;
346 } 346 }
347 #endif /* CONFIG_MAGIC_SYSRQ */ 347 #endif /* CONFIG_MAGIC_SYSRQ */
348 tty_insert_flip_char(&hp->port, c, 0); 348 tty_insert_flip_char(&hp->port, c, 0);
349 } 349 }
350 } 350 }
351 351
352 /* 352 /*
353 * We could get 252 bytes of data at once here. But the tty layer only 353 * We could get 252 bytes of data at once here. But the tty layer only
354 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow 354 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
355 * it. Accordingly we won't send more than 128 bytes at a time to the flip 355 * it. Accordingly we won't send more than 128 bytes at a time to the flip
356 * buffer, which will give the tty buffer a chance to throttle us. Should the 356 * buffer, which will give the tty buffer a chance to throttle us. Should the
357 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be 357 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
358 * revisited. 358 * revisited.
359 */ 359 */
360 #define TTY_THRESHOLD_THROTTLE 128 360 #define TTY_THRESHOLD_THROTTLE 128
361 static bool hvsi_recv_data(struct hvsi_struct *hp, const uint8_t *packet) 361 static bool hvsi_recv_data(struct hvsi_struct *hp, const uint8_t *packet)
362 { 362 {
363 const struct hvsi_header *header = (const struct hvsi_header *)packet; 363 const struct hvsi_header *header = (const struct hvsi_header *)packet;
364 const uint8_t *data = packet + sizeof(struct hvsi_header); 364 const uint8_t *data = packet + sizeof(struct hvsi_header);
365 int datalen = header->len - sizeof(struct hvsi_header); 365 int datalen = header->len - sizeof(struct hvsi_header);
366 int overflow = datalen - TTY_THRESHOLD_THROTTLE; 366 int overflow = datalen - TTY_THRESHOLD_THROTTLE;
367 367
368 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data); 368 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
369 369
370 if (datalen == 0) 370 if (datalen == 0)
371 return false; 371 return false;
372 372
373 if (overflow > 0) { 373 if (overflow > 0) {
374 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__); 374 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
375 datalen = TTY_THRESHOLD_THROTTLE; 375 datalen = TTY_THRESHOLD_THROTTLE;
376 } 376 }
377 377
378 hvsi_insert_chars(hp, data, datalen); 378 hvsi_insert_chars(hp, data, datalen);
379 379
380 if (overflow > 0) { 380 if (overflow > 0) {
381 /* 381 /*
382 * we still have more data to deliver, so we need to save off the 382 * we still have more data to deliver, so we need to save off the
383 * overflow and send it later 383 * overflow and send it later
384 */ 384 */
385 pr_debug("%s: deferring overflow\n", __func__); 385 pr_debug("%s: deferring overflow\n", __func__);
386 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow); 386 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
387 hp->n_throttle = overflow; 387 hp->n_throttle = overflow;
388 } 388 }
389 389
390 return true; 390 return true;
391 } 391 }
392 392
393 /* 393 /*
394 * Returns true/false indicating data successfully read from hypervisor. 394 * Returns true/false indicating data successfully read from hypervisor.
395 * Used both to get packets for tty connections and to advance the state 395 * Used both to get packets for tty connections and to advance the state
396 * machine during console handshaking (in which case tty = NULL and we ignore 396 * machine during console handshaking (in which case tty = NULL and we ignore
397 * incoming data). 397 * incoming data).
398 */ 398 */
399 static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct *tty, 399 static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct *tty,
400 struct hvsi_struct **handshake) 400 struct hvsi_struct **handshake)
401 { 401 {
402 uint8_t *packet = hp->inbuf; 402 uint8_t *packet = hp->inbuf;
403 int chunklen; 403 int chunklen;
404 bool flip = false; 404 bool flip = false;
405 405
406 *handshake = NULL; 406 *handshake = NULL;
407 407
408 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ); 408 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
409 if (chunklen == 0) { 409 if (chunklen == 0) {
410 pr_debug("%s: 0-length read\n", __func__); 410 pr_debug("%s: 0-length read\n", __func__);
411 return 0; 411 return 0;
412 } 412 }
413 413
414 pr_debug("%s: got %i bytes\n", __func__, chunklen); 414 pr_debug("%s: got %i bytes\n", __func__, chunklen);
415 dbg_dump_hex(hp->inbuf_end, chunklen); 415 dbg_dump_hex(hp->inbuf_end, chunklen);
416 416
417 hp->inbuf_end += chunklen; 417 hp->inbuf_end += chunklen;
418 418
419 /* handle all completed packets */ 419 /* handle all completed packets */
420 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) { 420 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
421 struct hvsi_header *header = (struct hvsi_header *)packet; 421 struct hvsi_header *header = (struct hvsi_header *)packet;
422 422
423 if (!is_header(packet)) { 423 if (!is_header(packet)) {
424 printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index); 424 printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
425 /* skip bytes until we find a header or run out of data */ 425 /* skip bytes until we find a header or run out of data */
426 while ((packet < hp->inbuf_end) && (!is_header(packet))) 426 while ((packet < hp->inbuf_end) && (!is_header(packet)))
427 packet++; 427 packet++;
428 continue; 428 continue;
429 } 429 }
430 430
431 pr_debug("%s: handling %i-byte packet\n", __func__, 431 pr_debug("%s: handling %i-byte packet\n", __func__,
432 len_packet(packet)); 432 len_packet(packet));
433 dbg_dump_packet(packet); 433 dbg_dump_packet(packet);
434 434
435 switch (header->type) { 435 switch (header->type) {
436 case VS_DATA_PACKET_HEADER: 436 case VS_DATA_PACKET_HEADER:
437 if (!is_open(hp)) 437 if (!is_open(hp))
438 break; 438 break;
439 flip = hvsi_recv_data(hp, packet); 439 flip = hvsi_recv_data(hp, packet);
440 break; 440 break;
441 case VS_CONTROL_PACKET_HEADER: 441 case VS_CONTROL_PACKET_HEADER:
442 hvsi_recv_control(hp, packet, tty, handshake); 442 hvsi_recv_control(hp, packet, tty, handshake);
443 break; 443 break;
444 case VS_QUERY_RESPONSE_PACKET_HEADER: 444 case VS_QUERY_RESPONSE_PACKET_HEADER:
445 hvsi_recv_response(hp, packet); 445 hvsi_recv_response(hp, packet);
446 break; 446 break;
447 case VS_QUERY_PACKET_HEADER: 447 case VS_QUERY_PACKET_HEADER:
448 hvsi_recv_query(hp, packet); 448 hvsi_recv_query(hp, packet);
449 break; 449 break;
450 default: 450 default:
451 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n", 451 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
452 hp->index, header->type); 452 hp->index, header->type);
453 dump_packet(packet); 453 dump_packet(packet);
454 break; 454 break;
455 } 455 }
456 456
457 packet += len_packet(packet); 457 packet += len_packet(packet);
458 458
459 if (*handshake) { 459 if (*handshake) {
460 pr_debug("%s: handshake\n", __func__); 460 pr_debug("%s: handshake\n", __func__);
461 break; 461 break;
462 } 462 }
463 } 463 }
464 464
465 compact_inbuf(hp, packet); 465 compact_inbuf(hp, packet);
466 466
467 if (flip) 467 if (flip)
468 tty_flip_buffer_push(&hp->port); 468 tty_flip_buffer_push(&hp->port);
469 469
470 return 1; 470 return 1;
471 } 471 }
472 472
473 static void hvsi_send_overflow(struct hvsi_struct *hp) 473 static void hvsi_send_overflow(struct hvsi_struct *hp)
474 { 474 {
475 pr_debug("%s: delivering %i bytes overflow\n", __func__, 475 pr_debug("%s: delivering %i bytes overflow\n", __func__,
476 hp->n_throttle); 476 hp->n_throttle);
477 477
478 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle); 478 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
479 hp->n_throttle = 0; 479 hp->n_throttle = 0;
480 } 480 }
481 481
482 /* 482 /*
483 * must get all pending data because we only get an irq on empty->non-empty 483 * must get all pending data because we only get an irq on empty->non-empty
484 * transition 484 * transition
485 */ 485 */
486 static irqreturn_t hvsi_interrupt(int irq, void *arg) 486 static irqreturn_t hvsi_interrupt(int irq, void *arg)
487 { 487 {
488 struct hvsi_struct *hp = (struct hvsi_struct *)arg; 488 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
489 struct hvsi_struct *handshake; 489 struct hvsi_struct *handshake;
490 struct tty_struct *tty; 490 struct tty_struct *tty;
491 unsigned long flags; 491 unsigned long flags;
492 int again = 1; 492 int again = 1;
493 493
494 pr_debug("%s\n", __func__); 494 pr_debug("%s\n", __func__);
495 495
496 tty = tty_port_tty_get(&hp->port); 496 tty = tty_port_tty_get(&hp->port);
497 497
498 while (again) { 498 while (again) {
499 spin_lock_irqsave(&hp->lock, flags); 499 spin_lock_irqsave(&hp->lock, flags);
500 again = hvsi_load_chunk(hp, tty, &handshake); 500 again = hvsi_load_chunk(hp, tty, &handshake);
501 spin_unlock_irqrestore(&hp->lock, flags); 501 spin_unlock_irqrestore(&hp->lock, flags);
502 502
503 if (handshake) { 503 if (handshake) {
504 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index); 504 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
505 schedule_work(&handshake->handshaker); 505 schedule_work(&handshake->handshaker);
506 } 506 }
507 } 507 }
508 508
509 spin_lock_irqsave(&hp->lock, flags); 509 spin_lock_irqsave(&hp->lock, flags);
510 if (tty && hp->n_throttle && !test_bit(TTY_THROTTLED, &tty->flags)) { 510 if (tty && hp->n_throttle && !test_bit(TTY_THROTTLED, &tty->flags)) {
511 /* we weren't hung up and we weren't throttled, so we can 511 /* we weren't hung up and we weren't throttled, so we can
512 * deliver the rest now */ 512 * deliver the rest now */
513 hvsi_send_overflow(hp); 513 hvsi_send_overflow(hp);
514 tty_flip_buffer_push(&hp->port); 514 tty_flip_buffer_push(&hp->port);
515 } 515 }
516 spin_unlock_irqrestore(&hp->lock, flags); 516 spin_unlock_irqrestore(&hp->lock, flags);
517 517
518 tty_kref_put(tty); 518 tty_kref_put(tty);
519 519
520 return IRQ_HANDLED; 520 return IRQ_HANDLED;
521 } 521 }
522 522
523 /* for boot console, before the irq handler is running */ 523 /* for boot console, before the irq handler is running */
524 static int __init poll_for_state(struct hvsi_struct *hp, int state) 524 static int __init poll_for_state(struct hvsi_struct *hp, int state)
525 { 525 {
526 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT; 526 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
527 527
528 for (;;) { 528 for (;;) {
529 hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */ 529 hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
530 530
531 if (hp->state == state) 531 if (hp->state == state)
532 return 0; 532 return 0;
533 533
534 mdelay(5); 534 mdelay(5);
535 if (time_after(jiffies, end_jiffies)) 535 if (time_after(jiffies, end_jiffies))
536 return -EIO; 536 return -EIO;
537 } 537 }
538 } 538 }
539 539
540 /* wait for irq handler to change our state */ 540 /* wait for irq handler to change our state */
541 static int wait_for_state(struct hvsi_struct *hp, int state) 541 static int wait_for_state(struct hvsi_struct *hp, int state)
542 { 542 {
543 int ret = 0; 543 int ret = 0;
544 544
545 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT)) 545 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
546 ret = -EIO; 546 ret = -EIO;
547 547
548 return ret; 548 return ret;
549 } 549 }
550 550
551 static int hvsi_query(struct hvsi_struct *hp, uint16_t verb) 551 static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
552 { 552 {
553 struct hvsi_query packet __ALIGNED__; 553 struct hvsi_query packet __ALIGNED__;
554 int wrote; 554 int wrote;
555 555
556 packet.hdr.type = VS_QUERY_PACKET_HEADER; 556 packet.hdr.type = VS_QUERY_PACKET_HEADER;
557 packet.hdr.len = sizeof(struct hvsi_query); 557 packet.hdr.len = sizeof(struct hvsi_query);
558 packet.hdr.seqno = atomic_inc_return(&hp->seqno); 558 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
559 packet.verb = verb; 559 packet.verb = verb;
560 560
561 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len); 561 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
562 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len); 562 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
563 563
564 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len); 564 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
565 if (wrote != packet.hdr.len) { 565 if (wrote != packet.hdr.len) {
566 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index, 566 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
567 wrote); 567 wrote);
568 return -EIO; 568 return -EIO;
569 } 569 }
570 570
571 return 0; 571 return 0;
572 } 572 }
573 573
574 static int hvsi_get_mctrl(struct hvsi_struct *hp) 574 static int hvsi_get_mctrl(struct hvsi_struct *hp)
575 { 575 {
576 int ret; 576 int ret;
577 577
578 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE); 578 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
579 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS); 579 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
580 580
581 ret = hvsi_wait(hp, HVSI_OPEN); 581 ret = hvsi_wait(hp, HVSI_OPEN);
582 if (ret < 0) { 582 if (ret < 0) {
583 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index); 583 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
584 set_state(hp, HVSI_OPEN); 584 set_state(hp, HVSI_OPEN);
585 return ret; 585 return ret;
586 } 586 }
587 587
588 pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl); 588 pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
589 589
590 return 0; 590 return 0;
591 } 591 }
592 592
593 /* note that we can only set DTR */ 593 /* note that we can only set DTR */
594 static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl) 594 static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
595 { 595 {
596 struct hvsi_control packet __ALIGNED__; 596 struct hvsi_control packet __ALIGNED__;
597 int wrote; 597 int wrote;
598 598
599 packet.hdr.type = VS_CONTROL_PACKET_HEADER, 599 packet.hdr.type = VS_CONTROL_PACKET_HEADER,
600 packet.hdr.seqno = atomic_inc_return(&hp->seqno); 600 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
601 packet.hdr.len = sizeof(struct hvsi_control); 601 packet.hdr.len = sizeof(struct hvsi_control);
602 packet.verb = VSV_SET_MODEM_CTL; 602 packet.verb = VSV_SET_MODEM_CTL;
603 packet.mask = HVSI_TSDTR; 603 packet.mask = HVSI_TSDTR;
604 604
605 if (mctrl & TIOCM_DTR) 605 if (mctrl & TIOCM_DTR)
606 packet.word = HVSI_TSDTR; 606 packet.word = HVSI_TSDTR;
607 607
608 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len); 608 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
609 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len); 609 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
610 610
611 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len); 611 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
612 if (wrote != packet.hdr.len) { 612 if (wrote != packet.hdr.len) {
613 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index); 613 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
614 return -EIO; 614 return -EIO;
615 } 615 }
616 616
617 return 0; 617 return 0;
618 } 618 }
619 619
620 static void hvsi_drain_input(struct hvsi_struct *hp) 620 static void hvsi_drain_input(struct hvsi_struct *hp)
621 { 621 {
622 uint8_t buf[HVSI_MAX_READ] __ALIGNED__; 622 uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
623 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT; 623 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
624 624
625 while (time_before(end_jiffies, jiffies)) 625 while (time_before(end_jiffies, jiffies))
626 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ)) 626 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
627 break; 627 break;
628 } 628 }
629 629
630 static int hvsi_handshake(struct hvsi_struct *hp) 630 static int hvsi_handshake(struct hvsi_struct *hp)
631 { 631 {
632 int ret; 632 int ret;
633 633
634 /* 634 /*
635 * We could have a CLOSE or other data waiting for us before we even try 635 * We could have a CLOSE or other data waiting for us before we even try
636 * to open; try to throw it all away so we don't get confused. (CLOSE 636 * to open; try to throw it all away so we don't get confused. (CLOSE
637 * is the first message sent up the pipe when the FSP comes online. We 637 * is the first message sent up the pipe when the FSP comes online. We
638 * need to distinguish between "it came up a while ago and we're the first 638 * need to distinguish between "it came up a while ago and we're the first
639 * user" and "it was just reset before it saw our handshake packet".) 639 * user" and "it was just reset before it saw our handshake packet".)
640 */ 640 */
641 hvsi_drain_input(hp); 641 hvsi_drain_input(hp);
642 642
643 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE); 643 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
644 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER); 644 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
645 if (ret < 0) { 645 if (ret < 0) {
646 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index); 646 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
647 return ret; 647 return ret;
648 } 648 }
649 649
650 ret = hvsi_wait(hp, HVSI_OPEN); 650 ret = hvsi_wait(hp, HVSI_OPEN);
651 if (ret < 0) 651 if (ret < 0)
652 return ret; 652 return ret;
653 653
654 return 0; 654 return 0;
655 } 655 }
656 656
657 static void hvsi_handshaker(struct work_struct *work) 657 static void hvsi_handshaker(struct work_struct *work)
658 { 658 {
659 struct hvsi_struct *hp = 659 struct hvsi_struct *hp =
660 container_of(work, struct hvsi_struct, handshaker); 660 container_of(work, struct hvsi_struct, handshaker);
661 661
662 if (hvsi_handshake(hp) >= 0) 662 if (hvsi_handshake(hp) >= 0)
663 return; 663 return;
664 664
665 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index); 665 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
666 if (is_console(hp)) { 666 if (is_console(hp)) {
667 /* 667 /*
668 * ttys will re-attempt the handshake via hvsi_open, but 668 * ttys will re-attempt the handshake via hvsi_open, but
669 * the console will not. 669 * the console will not.
670 */ 670 */
671 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index); 671 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
672 } 672 }
673 } 673 }
674 674
675 static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count) 675 static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
676 { 676 {
677 struct hvsi_data packet __ALIGNED__; 677 struct hvsi_data packet __ALIGNED__;
678 int ret; 678 int ret;
679 679
680 BUG_ON(count > HVSI_MAX_OUTGOING_DATA); 680 BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
681 681
682 packet.hdr.type = VS_DATA_PACKET_HEADER; 682 packet.hdr.type = VS_DATA_PACKET_HEADER;
683 packet.hdr.seqno = atomic_inc_return(&hp->seqno); 683 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
684 packet.hdr.len = count + sizeof(struct hvsi_header); 684 packet.hdr.len = count + sizeof(struct hvsi_header);
685 memcpy(&packet.data, buf, count); 685 memcpy(&packet.data, buf, count);
686 686
687 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len); 687 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
688 if (ret == packet.hdr.len) { 688 if (ret == packet.hdr.len) {
689 /* return the number of chars written, not the packet length */ 689 /* return the number of chars written, not the packet length */
690 return count; 690 return count;
691 } 691 }
692 return ret; /* return any errors */ 692 return ret; /* return any errors */
693 } 693 }
694 694
695 static void hvsi_close_protocol(struct hvsi_struct *hp) 695 static void hvsi_close_protocol(struct hvsi_struct *hp)
696 { 696 {
697 struct hvsi_control packet __ALIGNED__; 697 struct hvsi_control packet __ALIGNED__;
698 698
699 packet.hdr.type = VS_CONTROL_PACKET_HEADER; 699 packet.hdr.type = VS_CONTROL_PACKET_HEADER;
700 packet.hdr.seqno = atomic_inc_return(&hp->seqno); 700 packet.hdr.seqno = atomic_inc_return(&hp->seqno);
701 packet.hdr.len = 6; 701 packet.hdr.len = 6;
702 packet.verb = VSV_CLOSE_PROTOCOL; 702 packet.verb = VSV_CLOSE_PROTOCOL;
703 703
704 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len); 704 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
705 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len); 705 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
706 706
707 hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len); 707 hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
708 } 708 }
709 709
710 static int hvsi_open(struct tty_struct *tty, struct file *filp) 710 static int hvsi_open(struct tty_struct *tty, struct file *filp)
711 { 711 {
712 struct hvsi_struct *hp; 712 struct hvsi_struct *hp;
713 unsigned long flags; 713 unsigned long flags;
714 int ret; 714 int ret;
715 715
716 pr_debug("%s\n", __func__); 716 pr_debug("%s\n", __func__);
717 717
718 hp = &hvsi_ports[tty->index]; 718 hp = &hvsi_ports[tty->index];
719 719
720 tty->driver_data = hp; 720 tty->driver_data = hp;
721 721
722 mb(); 722 mb();
723 if (hp->state == HVSI_FSP_DIED) 723 if (hp->state == HVSI_FSP_DIED)
724 return -EIO; 724 return -EIO;
725 725
726 tty_port_tty_set(&hp->port, tty); 726 tty_port_tty_set(&hp->port, tty);
727 spin_lock_irqsave(&hp->lock, flags); 727 spin_lock_irqsave(&hp->lock, flags);
728 hp->port.count++; 728 hp->port.count++;
729 atomic_set(&hp->seqno, 0); 729 atomic_set(&hp->seqno, 0);
730 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE); 730 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
731 spin_unlock_irqrestore(&hp->lock, flags); 731 spin_unlock_irqrestore(&hp->lock, flags);
732 732
733 if (is_console(hp)) 733 if (is_console(hp))
734 return 0; /* this has already been handshaked as the console */ 734 return 0; /* this has already been handshaked as the console */
735 735
736 ret = hvsi_handshake(hp); 736 ret = hvsi_handshake(hp);
737 if (ret < 0) { 737 if (ret < 0) {
738 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name); 738 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
739 return ret; 739 return ret;
740 } 740 }
741 741
742 ret = hvsi_get_mctrl(hp); 742 ret = hvsi_get_mctrl(hp);
743 if (ret < 0) { 743 if (ret < 0) {
744 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name); 744 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
745 return ret; 745 return ret;
746 } 746 }
747 747
748 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR); 748 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
749 if (ret < 0) { 749 if (ret < 0) {
750 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name); 750 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
751 return ret; 751 return ret;
752 } 752 }
753 753
754 return 0; 754 return 0;
755 } 755 }
756 756
757 /* wait for hvsi_write_worker to empty hp->outbuf */ 757 /* wait for hvsi_write_worker to empty hp->outbuf */
758 static void hvsi_flush_output(struct hvsi_struct *hp) 758 static void hvsi_flush_output(struct hvsi_struct *hp)
759 { 759 {
760 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT); 760 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
761 761
762 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */ 762 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
763 cancel_delayed_work_sync(&hp->writer); 763 cancel_delayed_work_sync(&hp->writer);
764 flush_work(&hp->handshaker); 764 flush_work(&hp->handshaker);
765 765
766 /* 766 /*
767 * it's also possible that our timeout expired and hvsi_write_worker 767 * it's also possible that our timeout expired and hvsi_write_worker
768 * didn't manage to push outbuf. poof. 768 * didn't manage to push outbuf. poof.
769 */ 769 */
770 hp->n_outbuf = 0; 770 hp->n_outbuf = 0;
771 } 771 }
772 772
773 static void hvsi_close(struct tty_struct *tty, struct file *filp) 773 static void hvsi_close(struct tty_struct *tty, struct file *filp)
774 { 774 {
775 struct hvsi_struct *hp = tty->driver_data; 775 struct hvsi_struct *hp = tty->driver_data;
776 unsigned long flags; 776 unsigned long flags;
777 777
778 pr_debug("%s\n", __func__); 778 pr_debug("%s\n", __func__);
779 779
780 if (tty_hung_up_p(filp)) 780 if (tty_hung_up_p(filp))
781 return; 781 return;
782 782
783 spin_lock_irqsave(&hp->lock, flags); 783 spin_lock_irqsave(&hp->lock, flags);
784 784
785 if (--hp->port.count == 0) { 785 if (--hp->port.count == 0) {
786 tty_port_tty_set(&hp->port, NULL); 786 tty_port_tty_set(&hp->port, NULL);
787 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */ 787 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
788 788
789 /* only close down connection if it is not the console */ 789 /* only close down connection if it is not the console */
790 if (!is_console(hp)) { 790 if (!is_console(hp)) {
791 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */ 791 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
792 __set_state(hp, HVSI_CLOSED); 792 __set_state(hp, HVSI_CLOSED);
793 /* 793 /*
794 * any data delivered to the tty layer after this will be 794 * any data delivered to the tty layer after this will be
795 * discarded (except for XON/XOFF) 795 * discarded (except for XON/XOFF)
796 */ 796 */
797 tty->closing = 1; 797 tty->closing = 1;
798 798
799 spin_unlock_irqrestore(&hp->lock, flags); 799 spin_unlock_irqrestore(&hp->lock, flags);
800 800
801 /* let any existing irq handlers finish. no more will start. */ 801 /* let any existing irq handlers finish. no more will start. */
802 synchronize_irq(hp->virq); 802 synchronize_irq(hp->virq);
803 803
804 /* hvsi_write_worker will re-schedule until outbuf is empty. */ 804 /* hvsi_write_worker will re-schedule until outbuf is empty. */
805 hvsi_flush_output(hp); 805 hvsi_flush_output(hp);
806 806
807 /* tell FSP to stop sending data */ 807 /* tell FSP to stop sending data */
808 hvsi_close_protocol(hp); 808 hvsi_close_protocol(hp);
809 809
810 /* 810 /*
811 * drain anything FSP is still in the middle of sending, and let 811 * drain anything FSP is still in the middle of sending, and let
812 * hvsi_handshake drain the rest on the next open. 812 * hvsi_handshake drain the rest on the next open.
813 */ 813 */
814 hvsi_drain_input(hp); 814 hvsi_drain_input(hp);
815 815
816 spin_lock_irqsave(&hp->lock, flags); 816 spin_lock_irqsave(&hp->lock, flags);
817 } 817 }
818 } else if (hp->port.count < 0) 818 } else if (hp->port.count < 0)
819 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n", 819 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
820 hp - hvsi_ports, hp->port.count); 820 hp - hvsi_ports, hp->port.count);
821 821
822 spin_unlock_irqrestore(&hp->lock, flags); 822 spin_unlock_irqrestore(&hp->lock, flags);
823 } 823 }
824 824
825 static void hvsi_hangup(struct tty_struct *tty) 825 static void hvsi_hangup(struct tty_struct *tty)
826 { 826 {
827 struct hvsi_struct *hp = tty->driver_data; 827 struct hvsi_struct *hp = tty->driver_data;
828 unsigned long flags; 828 unsigned long flags;
829 829
830 pr_debug("%s\n", __func__); 830 pr_debug("%s\n", __func__);
831 831
832 tty_port_tty_set(&hp->port, NULL); 832 tty_port_tty_set(&hp->port, NULL);
833 833
834 spin_lock_irqsave(&hp->lock, flags); 834 spin_lock_irqsave(&hp->lock, flags);
835 hp->port.count = 0; 835 hp->port.count = 0;
836 hp->n_outbuf = 0; 836 hp->n_outbuf = 0;
837 spin_unlock_irqrestore(&hp->lock, flags); 837 spin_unlock_irqrestore(&hp->lock, flags);
838 } 838 }
839 839
840 /* called with hp->lock held */ 840 /* called with hp->lock held */
841 static void hvsi_push(struct hvsi_struct *hp) 841 static void hvsi_push(struct hvsi_struct *hp)
842 { 842 {
843 int n; 843 int n;
844 844
845 if (hp->n_outbuf <= 0) 845 if (hp->n_outbuf <= 0)
846 return; 846 return;
847 847
848 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf); 848 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
849 if (n > 0) { 849 if (n > 0) {
850 /* success */ 850 /* success */
851 pr_debug("%s: wrote %i chars\n", __func__, n); 851 pr_debug("%s: wrote %i chars\n", __func__, n);
852 hp->n_outbuf = 0; 852 hp->n_outbuf = 0;
853 } else if (n == -EIO) { 853 } else if (n == -EIO) {
854 __set_state(hp, HVSI_FSP_DIED); 854 __set_state(hp, HVSI_FSP_DIED);
855 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index); 855 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
856 } 856 }
857 } 857 }
858 858
859 /* hvsi_write_worker will keep rescheduling itself until outbuf is empty */ 859 /* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
860 static void hvsi_write_worker(struct work_struct *work) 860 static void hvsi_write_worker(struct work_struct *work)
861 { 861 {
862 struct hvsi_struct *hp = 862 struct hvsi_struct *hp =
863 container_of(work, struct hvsi_struct, writer.work); 863 container_of(work, struct hvsi_struct, writer.work);
864 struct tty_struct *tty;
865 unsigned long flags; 864 unsigned long flags;
866 #ifdef DEBUG 865 #ifdef DEBUG
867 static long start_j = 0; 866 static long start_j = 0;
868 867
869 if (start_j == 0) 868 if (start_j == 0)
870 start_j = jiffies; 869 start_j = jiffies;
871 #endif /* DEBUG */ 870 #endif /* DEBUG */
872 871
873 spin_lock_irqsave(&hp->lock, flags); 872 spin_lock_irqsave(&hp->lock, flags);
874 873
875 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf); 874 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
876 875
877 if (!is_open(hp)) { 876 if (!is_open(hp)) {
878 /* 877 /*
879 * We could have a non-open connection if the service processor died 878 * We could have a non-open connection if the service processor died
880 * while we were busily scheduling ourselves. In that case, it could 879 * while we were busily scheduling ourselves. In that case, it could
881 * be minutes before the service processor comes back, so only try 880 * be minutes before the service processor comes back, so only try
882 * again once a second. 881 * again once a second.
883 */ 882 */
884 schedule_delayed_work(&hp->writer, HZ); 883 schedule_delayed_work(&hp->writer, HZ);
885 goto out; 884 goto out;
886 } 885 }
887 886
888 hvsi_push(hp); 887 hvsi_push(hp);
889 if (hp->n_outbuf > 0) 888 if (hp->n_outbuf > 0)
890 schedule_delayed_work(&hp->writer, 10); 889 schedule_delayed_work(&hp->writer, 10);
891 else { 890 else {
892 #ifdef DEBUG 891 #ifdef DEBUG
893 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__, 892 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
894 jiffies - start_j); 893 jiffies - start_j);
895 start_j = 0; 894 start_j = 0;
896 #endif /* DEBUG */ 895 #endif /* DEBUG */
897 wake_up_all(&hp->emptyq); 896 wake_up_all(&hp->emptyq);
898 tty = tty_port_tty_get(&hp->port); 897 tty_port_tty_wakeup(&hp->port);
899 if (tty) {
900 tty_wakeup(tty);
901 tty_kref_put(tty);
902 }
903 } 898 }
904 899
905 out: 900 out:
906 spin_unlock_irqrestore(&hp->lock, flags); 901 spin_unlock_irqrestore(&hp->lock, flags);
907 } 902 }
908 903
909 static int hvsi_write_room(struct tty_struct *tty) 904 static int hvsi_write_room(struct tty_struct *tty)
910 { 905 {
911 struct hvsi_struct *hp = tty->driver_data; 906 struct hvsi_struct *hp = tty->driver_data;
912 907
913 return N_OUTBUF - hp->n_outbuf; 908 return N_OUTBUF - hp->n_outbuf;
914 } 909 }
915 910
916 static int hvsi_chars_in_buffer(struct tty_struct *tty) 911 static int hvsi_chars_in_buffer(struct tty_struct *tty)
917 { 912 {
918 struct hvsi_struct *hp = tty->driver_data; 913 struct hvsi_struct *hp = tty->driver_data;
919 914
920 return hp->n_outbuf; 915 return hp->n_outbuf;
921 } 916 }
922 917
923 static int hvsi_write(struct tty_struct *tty, 918 static int hvsi_write(struct tty_struct *tty,
924 const unsigned char *buf, int count) 919 const unsigned char *buf, int count)
925 { 920 {
926 struct hvsi_struct *hp = tty->driver_data; 921 struct hvsi_struct *hp = tty->driver_data;
927 const char *source = buf; 922 const char *source = buf;
928 unsigned long flags; 923 unsigned long flags;
929 int total = 0; 924 int total = 0;
930 int origcount = count; 925 int origcount = count;
931 926
932 spin_lock_irqsave(&hp->lock, flags); 927 spin_lock_irqsave(&hp->lock, flags);
933 928
934 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf); 929 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
935 930
936 if (!is_open(hp)) { 931 if (!is_open(hp)) {
937 /* we're either closing or not yet open; don't accept data */ 932 /* we're either closing or not yet open; don't accept data */
938 pr_debug("%s: not open\n", __func__); 933 pr_debug("%s: not open\n", __func__);
939 goto out; 934 goto out;
940 } 935 }
941 936
942 /* 937 /*
943 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf 938 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
944 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls 939 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
945 * will see there is no room in outbuf and return. 940 * will see there is no room in outbuf and return.
946 */ 941 */
947 while ((count > 0) && (hvsi_write_room(tty) > 0)) { 942 while ((count > 0) && (hvsi_write_room(tty) > 0)) {
948 int chunksize = min(count, hvsi_write_room(tty)); 943 int chunksize = min(count, hvsi_write_room(tty));
949 944
950 BUG_ON(hp->n_outbuf < 0); 945 BUG_ON(hp->n_outbuf < 0);
951 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize); 946 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
952 hp->n_outbuf += chunksize; 947 hp->n_outbuf += chunksize;
953 948
954 total += chunksize; 949 total += chunksize;
955 source += chunksize; 950 source += chunksize;
956 count -= chunksize; 951 count -= chunksize;
957 hvsi_push(hp); 952 hvsi_push(hp);
958 } 953 }
959 954
960 if (hp->n_outbuf > 0) { 955 if (hp->n_outbuf > 0) {
961 /* 956 /*
962 * we weren't able to write it all to the hypervisor. 957 * we weren't able to write it all to the hypervisor.
963 * schedule another push attempt. 958 * schedule another push attempt.
964 */ 959 */
965 schedule_delayed_work(&hp->writer, 10); 960 schedule_delayed_work(&hp->writer, 10);
966 } 961 }
967 962
968 out: 963 out:
969 spin_unlock_irqrestore(&hp->lock, flags); 964 spin_unlock_irqrestore(&hp->lock, flags);
970 965
971 if (total != origcount) 966 if (total != origcount)
972 pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount, 967 pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
973 total); 968 total);
974 969
975 return total; 970 return total;
976 } 971 }
977 972
978 /* 973 /*
979 * I have never seen throttle or unthrottle called, so this little throttle 974 * I have never seen throttle or unthrottle called, so this little throttle
980 * buffering scheme may or may not work. 975 * buffering scheme may or may not work.
981 */ 976 */
982 static void hvsi_throttle(struct tty_struct *tty) 977 static void hvsi_throttle(struct tty_struct *tty)
983 { 978 {
984 struct hvsi_struct *hp = tty->driver_data; 979 struct hvsi_struct *hp = tty->driver_data;
985 980
986 pr_debug("%s\n", __func__); 981 pr_debug("%s\n", __func__);
987 982
988 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); 983 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
989 } 984 }
990 985
991 static void hvsi_unthrottle(struct tty_struct *tty) 986 static void hvsi_unthrottle(struct tty_struct *tty)
992 { 987 {
993 struct hvsi_struct *hp = tty->driver_data; 988 struct hvsi_struct *hp = tty->driver_data;
994 unsigned long flags; 989 unsigned long flags;
995 990
996 pr_debug("%s\n", __func__); 991 pr_debug("%s\n", __func__);
997 992
998 spin_lock_irqsave(&hp->lock, flags); 993 spin_lock_irqsave(&hp->lock, flags);
999 if (hp->n_throttle) { 994 if (hp->n_throttle) {
1000 hvsi_send_overflow(hp); 995 hvsi_send_overflow(hp);
1001 tty_flip_buffer_push(&hp->port); 996 tty_flip_buffer_push(&hp->port);
1002 } 997 }
1003 spin_unlock_irqrestore(&hp->lock, flags); 998 spin_unlock_irqrestore(&hp->lock, flags);
1004 999
1005 1000
1006 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE); 1001 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
1007 } 1002 }
1008 1003
1009 static int hvsi_tiocmget(struct tty_struct *tty) 1004 static int hvsi_tiocmget(struct tty_struct *tty)
1010 { 1005 {
1011 struct hvsi_struct *hp = tty->driver_data; 1006 struct hvsi_struct *hp = tty->driver_data;
1012 1007
1013 hvsi_get_mctrl(hp); 1008 hvsi_get_mctrl(hp);
1014 return hp->mctrl; 1009 return hp->mctrl;
1015 } 1010 }
1016 1011
1017 static int hvsi_tiocmset(struct tty_struct *tty, 1012 static int hvsi_tiocmset(struct tty_struct *tty,
1018 unsigned int set, unsigned int clear) 1013 unsigned int set, unsigned int clear)
1019 { 1014 {
1020 struct hvsi_struct *hp = tty->driver_data; 1015 struct hvsi_struct *hp = tty->driver_data;
1021 unsigned long flags; 1016 unsigned long flags;
1022 uint16_t new_mctrl; 1017 uint16_t new_mctrl;
1023 1018
1024 /* we can only alter DTR */ 1019 /* we can only alter DTR */
1025 clear &= TIOCM_DTR; 1020 clear &= TIOCM_DTR;
1026 set &= TIOCM_DTR; 1021 set &= TIOCM_DTR;
1027 1022
1028 spin_lock_irqsave(&hp->lock, flags); 1023 spin_lock_irqsave(&hp->lock, flags);
1029 1024
1030 new_mctrl = (hp->mctrl & ~clear) | set; 1025 new_mctrl = (hp->mctrl & ~clear) | set;
1031 1026
1032 if (hp->mctrl != new_mctrl) { 1027 if (hp->mctrl != new_mctrl) {
1033 hvsi_set_mctrl(hp, new_mctrl); 1028 hvsi_set_mctrl(hp, new_mctrl);
1034 hp->mctrl = new_mctrl; 1029 hp->mctrl = new_mctrl;
1035 } 1030 }
1036 spin_unlock_irqrestore(&hp->lock, flags); 1031 spin_unlock_irqrestore(&hp->lock, flags);
1037 1032
1038 return 0; 1033 return 0;
1039 } 1034 }
1040 1035
1041 1036
1042 static const struct tty_operations hvsi_ops = { 1037 static const struct tty_operations hvsi_ops = {
1043 .open = hvsi_open, 1038 .open = hvsi_open,
1044 .close = hvsi_close, 1039 .close = hvsi_close,
1045 .write = hvsi_write, 1040 .write = hvsi_write,
1046 .hangup = hvsi_hangup, 1041 .hangup = hvsi_hangup,
1047 .write_room = hvsi_write_room, 1042 .write_room = hvsi_write_room,
1048 .chars_in_buffer = hvsi_chars_in_buffer, 1043 .chars_in_buffer = hvsi_chars_in_buffer,
1049 .throttle = hvsi_throttle, 1044 .throttle = hvsi_throttle,
1050 .unthrottle = hvsi_unthrottle, 1045 .unthrottle = hvsi_unthrottle,
1051 .tiocmget = hvsi_tiocmget, 1046 .tiocmget = hvsi_tiocmget,
1052 .tiocmset = hvsi_tiocmset, 1047 .tiocmset = hvsi_tiocmset,
1053 }; 1048 };
1054 1049
1055 static int __init hvsi_init(void) 1050 static int __init hvsi_init(void)
1056 { 1051 {
1057 int i; 1052 int i;
1058 1053
1059 hvsi_driver = alloc_tty_driver(hvsi_count); 1054 hvsi_driver = alloc_tty_driver(hvsi_count);
1060 if (!hvsi_driver) 1055 if (!hvsi_driver)
1061 return -ENOMEM; 1056 return -ENOMEM;
1062 1057
1063 hvsi_driver->driver_name = "hvsi"; 1058 hvsi_driver->driver_name = "hvsi";
1064 hvsi_driver->name = "hvsi"; 1059 hvsi_driver->name = "hvsi";
1065 hvsi_driver->major = HVSI_MAJOR; 1060 hvsi_driver->major = HVSI_MAJOR;
1066 hvsi_driver->minor_start = HVSI_MINOR; 1061 hvsi_driver->minor_start = HVSI_MINOR;
1067 hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM; 1062 hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1068 hvsi_driver->init_termios = tty_std_termios; 1063 hvsi_driver->init_termios = tty_std_termios;
1069 hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL; 1064 hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1070 hvsi_driver->init_termios.c_ispeed = 9600; 1065 hvsi_driver->init_termios.c_ispeed = 9600;
1071 hvsi_driver->init_termios.c_ospeed = 9600; 1066 hvsi_driver->init_termios.c_ospeed = 9600;
1072 hvsi_driver->flags = TTY_DRIVER_REAL_RAW; 1067 hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
1073 tty_set_operations(hvsi_driver, &hvsi_ops); 1068 tty_set_operations(hvsi_driver, &hvsi_ops);
1074 1069
1075 for (i=0; i < hvsi_count; i++) { 1070 for (i=0; i < hvsi_count; i++) {
1076 struct hvsi_struct *hp = &hvsi_ports[i]; 1071 struct hvsi_struct *hp = &hvsi_ports[i];
1077 int ret = 1; 1072 int ret = 1;
1078 1073
1079 tty_port_link_device(&hp->port, hvsi_driver, i); 1074 tty_port_link_device(&hp->port, hvsi_driver, i);
1080 1075
1081 ret = request_irq(hp->virq, hvsi_interrupt, 0, "hvsi", hp); 1076 ret = request_irq(hp->virq, hvsi_interrupt, 0, "hvsi", hp);
1082 if (ret) 1077 if (ret)
1083 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n", 1078 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1084 hp->virq, ret); 1079 hp->virq, ret);
1085 } 1080 }
1086 hvsi_wait = wait_for_state; /* irqs active now */ 1081 hvsi_wait = wait_for_state; /* irqs active now */
1087 1082
1088 if (tty_register_driver(hvsi_driver)) 1083 if (tty_register_driver(hvsi_driver))
1089 panic("Couldn't register hvsi console driver\n"); 1084 panic("Couldn't register hvsi console driver\n");
1090 1085
1091 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count); 1086 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
1092 1087
1093 return 0; 1088 return 0;
1094 } 1089 }
1095 device_initcall(hvsi_init); 1090 device_initcall(hvsi_init);
1096 1091
1097 /***** console (not tty) code: *****/ 1092 /***** console (not tty) code: *****/
1098 1093
1099 static void hvsi_console_print(struct console *console, const char *buf, 1094 static void hvsi_console_print(struct console *console, const char *buf,
1100 unsigned int count) 1095 unsigned int count)
1101 { 1096 {
1102 struct hvsi_struct *hp = &hvsi_ports[console->index]; 1097 struct hvsi_struct *hp = &hvsi_ports[console->index];
1103 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__; 1098 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1104 unsigned int i = 0, n = 0; 1099 unsigned int i = 0, n = 0;
1105 int ret, donecr = 0; 1100 int ret, donecr = 0;
1106 1101
1107 mb(); 1102 mb();
1108 if (!is_open(hp)) 1103 if (!is_open(hp))
1109 return; 1104 return;
1110 1105
1111 /* 1106 /*
1112 * ugh, we have to translate LF -> CRLF ourselves, in place. 1107 * ugh, we have to translate LF -> CRLF ourselves, in place.
1113 * copied from hvc_console.c: 1108 * copied from hvc_console.c:
1114 */ 1109 */
1115 while (count > 0 || i > 0) { 1110 while (count > 0 || i > 0) {
1116 if (count > 0 && i < sizeof(c)) { 1111 if (count > 0 && i < sizeof(c)) {
1117 if (buf[n] == '\n' && !donecr) { 1112 if (buf[n] == '\n' && !donecr) {
1118 c[i++] = '\r'; 1113 c[i++] = '\r';
1119 donecr = 1; 1114 donecr = 1;
1120 } else { 1115 } else {
1121 c[i++] = buf[n++]; 1116 c[i++] = buf[n++];
1122 donecr = 0; 1117 donecr = 0;
1123 --count; 1118 --count;
1124 } 1119 }
1125 } else { 1120 } else {
1126 ret = hvsi_put_chars(hp, c, i); 1121 ret = hvsi_put_chars(hp, c, i);
1127 if (ret < 0) 1122 if (ret < 0)
1128 i = 0; 1123 i = 0;
1129 i -= ret; 1124 i -= ret;
1130 } 1125 }
1131 } 1126 }
1132 } 1127 }
1133 1128
1134 static struct tty_driver *hvsi_console_device(struct console *console, 1129 static struct tty_driver *hvsi_console_device(struct console *console,
1135 int *index) 1130 int *index)
1136 { 1131 {
1137 *index = console->index; 1132 *index = console->index;
1138 return hvsi_driver; 1133 return hvsi_driver;
1139 } 1134 }
1140 1135
1141 static int __init hvsi_console_setup(struct console *console, char *options) 1136 static int __init hvsi_console_setup(struct console *console, char *options)
1142 { 1137 {
1143 struct hvsi_struct *hp; 1138 struct hvsi_struct *hp;
1144 int ret; 1139 int ret;
1145 1140
1146 if (console->index < 0 || console->index >= hvsi_count) 1141 if (console->index < 0 || console->index >= hvsi_count)
1147 return -1; 1142 return -1;
1148 hp = &hvsi_ports[console->index]; 1143 hp = &hvsi_ports[console->index];
1149 1144
1150 /* give the FSP a chance to change the baud rate when we re-open */ 1145 /* give the FSP a chance to change the baud rate when we re-open */
1151 hvsi_close_protocol(hp); 1146 hvsi_close_protocol(hp);
1152 1147
1153 ret = hvsi_handshake(hp); 1148 ret = hvsi_handshake(hp);
1154 if (ret < 0) 1149 if (ret < 0)
1155 return ret; 1150 return ret;
1156 1151
1157 ret = hvsi_get_mctrl(hp); 1152 ret = hvsi_get_mctrl(hp);
1158 if (ret < 0) 1153 if (ret < 0)
1159 return ret; 1154 return ret;
1160 1155
1161 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR); 1156 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1162 if (ret < 0) 1157 if (ret < 0)
1163 return ret; 1158 return ret;
1164 1159
1165 hp->flags |= HVSI_CONSOLE; 1160 hp->flags |= HVSI_CONSOLE;
1166 1161
1167 return 0; 1162 return 0;
1168 } 1163 }
1169 1164
1170 static struct console hvsi_console = { 1165 static struct console hvsi_console = {
1171 .name = "hvsi", 1166 .name = "hvsi",
1172 .write = hvsi_console_print, 1167 .write = hvsi_console_print,
1173 .device = hvsi_console_device, 1168 .device = hvsi_console_device,
1174 .setup = hvsi_console_setup, 1169 .setup = hvsi_console_setup,
1175 .flags = CON_PRINTBUFFER, 1170 .flags = CON_PRINTBUFFER,
1176 .index = -1, 1171 .index = -1,
1177 }; 1172 };
1178 1173
1179 static int __init hvsi_console_init(void) 1174 static int __init hvsi_console_init(void)
1180 { 1175 {
1181 struct device_node *vty; 1176 struct device_node *vty;
1182 1177
1183 hvsi_wait = poll_for_state; /* no irqs yet; must poll */ 1178 hvsi_wait = poll_for_state; /* no irqs yet; must poll */
1184 1179
1185 /* search device tree for vty nodes */ 1180 /* search device tree for vty nodes */
1186 for_each_compatible_node(vty, "serial", "hvterm-protocol") { 1181 for_each_compatible_node(vty, "serial", "hvterm-protocol") {
1187 struct hvsi_struct *hp; 1182 struct hvsi_struct *hp;
1188 const uint32_t *vtermno, *irq; 1183 const uint32_t *vtermno, *irq;
1189 1184
1190 vtermno = of_get_property(vty, "reg", NULL); 1185 vtermno = of_get_property(vty, "reg", NULL);
1191 irq = of_get_property(vty, "interrupts", NULL); 1186 irq = of_get_property(vty, "interrupts", NULL);
1192 if (!vtermno || !irq) 1187 if (!vtermno || !irq)
1193 continue; 1188 continue;
1194 1189
1195 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) { 1190 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1196 of_node_put(vty); 1191 of_node_put(vty);
1197 break; 1192 break;
1198 } 1193 }
1199 1194
1200 hp = &hvsi_ports[hvsi_count]; 1195 hp = &hvsi_ports[hvsi_count];
1201 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker); 1196 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1202 INIT_WORK(&hp->handshaker, hvsi_handshaker); 1197 INIT_WORK(&hp->handshaker, hvsi_handshaker);
1203 init_waitqueue_head(&hp->emptyq); 1198 init_waitqueue_head(&hp->emptyq);
1204 init_waitqueue_head(&hp->stateq); 1199 init_waitqueue_head(&hp->stateq);
1205 spin_lock_init(&hp->lock); 1200 spin_lock_init(&hp->lock);
1206 tty_port_init(&hp->port); 1201 tty_port_init(&hp->port);
1207 hp->index = hvsi_count; 1202 hp->index = hvsi_count;
1208 hp->inbuf_end = hp->inbuf; 1203 hp->inbuf_end = hp->inbuf;
1209 hp->state = HVSI_CLOSED; 1204 hp->state = HVSI_CLOSED;
1210 hp->vtermno = *vtermno; 1205 hp->vtermno = *vtermno;
1211 hp->virq = irq_create_mapping(NULL, irq[0]); 1206 hp->virq = irq_create_mapping(NULL, irq[0]);
1212 if (hp->virq == 0) { 1207 if (hp->virq == 0) {
1213 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n", 1208 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
1214 __func__, irq[0]); 1209 __func__, irq[0]);
1215 tty_port_destroy(&hp->port); 1210 tty_port_destroy(&hp->port);
1216 continue; 1211 continue;
1217 } 1212 }
1218 1213
1219 hvsi_count++; 1214 hvsi_count++;
1220 } 1215 }
1221 1216
1222 if (hvsi_count) 1217 if (hvsi_count)
1223 register_console(&hvsi_console); 1218 register_console(&hvsi_console);
1224 return 0; 1219 return 0;
1225 } 1220 }
1226 console_initcall(hvsi_console_init); 1221 console_initcall(hvsi_console_init);
1227 1222
drivers/tty/nozomi.c
1 /* 1 /*
2 * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter 2 * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
3 * 3 *
4 * Written by: Ulf Jakobsson, 4 * Written by: Ulf Jakobsson,
5 * Jan ร…kerfeldt, 5 * Jan ร…kerfeldt,
6 * Stefan Thomasson, 6 * Stefan Thomasson,
7 * 7 *
8 * Maintained by: Paul Hardwick (p.hardwick@option.com) 8 * Maintained by: Paul Hardwick (p.hardwick@option.com)
9 * 9 *
10 * Patches: 10 * Patches:
11 * Locking code changes for Vodafone by Sphere Systems Ltd, 11 * Locking code changes for Vodafone by Sphere Systems Ltd,
12 * Andrew Bird (ajb@spheresystems.co.uk ) 12 * Andrew Bird (ajb@spheresystems.co.uk )
13 * & Phil Sanderson 13 * & Phil Sanderson
14 * 14 *
15 * Source has been ported from an implementation made by Filip Aben @ Option 15 * Source has been ported from an implementation made by Filip Aben @ Option
16 * 16 *
17 * -------------------------------------------------------------------------- 17 * --------------------------------------------------------------------------
18 * 18 *
19 * Copyright (c) 2005,2006 Option Wireless Sweden AB 19 * Copyright (c) 2005,2006 Option Wireless Sweden AB
20 * Copyright (c) 2006 Sphere Systems Ltd 20 * Copyright (c) 2006 Sphere Systems Ltd
21 * Copyright (c) 2006 Option Wireless n/v 21 * Copyright (c) 2006 Option Wireless n/v
22 * All rights Reserved. 22 * All rights Reserved.
23 * 23 *
24 * This program is free software; you can redistribute it and/or modify 24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by 25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or 26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version. 27 * (at your option) any later version.
28 * 28 *
29 * This program is distributed in the hope that it will be useful, 29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of 30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details. 32 * GNU General Public License for more details.
33 * 33 *
34 * You should have received a copy of the GNU General Public License 34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software 35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 * 37 *
38 * -------------------------------------------------------------------------- 38 * --------------------------------------------------------------------------
39 */ 39 */
40 40
41 /* Enable this to have a lot of debug printouts */ 41 /* Enable this to have a lot of debug printouts */
42 #define DEBUG 42 #define DEBUG
43 43
44 #include <linux/kernel.h> 44 #include <linux/kernel.h>
45 #include <linux/module.h> 45 #include <linux/module.h>
46 #include <linux/pci.h> 46 #include <linux/pci.h>
47 #include <linux/ioport.h> 47 #include <linux/ioport.h>
48 #include <linux/tty.h> 48 #include <linux/tty.h>
49 #include <linux/tty_driver.h> 49 #include <linux/tty_driver.h>
50 #include <linux/tty_flip.h> 50 #include <linux/tty_flip.h>
51 #include <linux/sched.h> 51 #include <linux/sched.h>
52 #include <linux/serial.h> 52 #include <linux/serial.h>
53 #include <linux/interrupt.h> 53 #include <linux/interrupt.h>
54 #include <linux/kmod.h> 54 #include <linux/kmod.h>
55 #include <linux/init.h> 55 #include <linux/init.h>
56 #include <linux/kfifo.h> 56 #include <linux/kfifo.h>
57 #include <linux/uaccess.h> 57 #include <linux/uaccess.h>
58 #include <linux/slab.h> 58 #include <linux/slab.h>
59 #include <asm/byteorder.h> 59 #include <asm/byteorder.h>
60 60
61 #include <linux/delay.h> 61 #include <linux/delay.h>
62 62
63 63
64 #define VERSION_STRING DRIVER_DESC " 2.1d" 64 #define VERSION_STRING DRIVER_DESC " 2.1d"
65 65
66 /* Macros definitions */ 66 /* Macros definitions */
67 67
68 /* Default debug printout level */ 68 /* Default debug printout level */
69 #define NOZOMI_DEBUG_LEVEL 0x00 69 #define NOZOMI_DEBUG_LEVEL 0x00
70 70
71 #define P_BUF_SIZE 128 71 #define P_BUF_SIZE 128
72 #define NFO(_err_flag_, args...) \ 72 #define NFO(_err_flag_, args...) \
73 do { \ 73 do { \
74 char tmp[P_BUF_SIZE]; \ 74 char tmp[P_BUF_SIZE]; \
75 snprintf(tmp, sizeof(tmp), ##args); \ 75 snprintf(tmp, sizeof(tmp), ##args); \
76 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \ 76 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
77 __func__, tmp); \ 77 __func__, tmp); \
78 } while (0) 78 } while (0)
79 79
80 #define DBG1(args...) D_(0x01, ##args) 80 #define DBG1(args...) D_(0x01, ##args)
81 #define DBG2(args...) D_(0x02, ##args) 81 #define DBG2(args...) D_(0x02, ##args)
82 #define DBG3(args...) D_(0x04, ##args) 82 #define DBG3(args...) D_(0x04, ##args)
83 #define DBG4(args...) D_(0x08, ##args) 83 #define DBG4(args...) D_(0x08, ##args)
84 #define DBG5(args...) D_(0x10, ##args) 84 #define DBG5(args...) D_(0x10, ##args)
85 #define DBG6(args...) D_(0x20, ##args) 85 #define DBG6(args...) D_(0x20, ##args)
86 #define DBG7(args...) D_(0x40, ##args) 86 #define DBG7(args...) D_(0x40, ##args)
87 #define DBG8(args...) D_(0x80, ##args) 87 #define DBG8(args...) D_(0x80, ##args)
88 88
89 #ifdef DEBUG 89 #ifdef DEBUG
90 /* Do we need this settable at runtime? */ 90 /* Do we need this settable at runtime? */
91 static int debug = NOZOMI_DEBUG_LEVEL; 91 static int debug = NOZOMI_DEBUG_LEVEL;
92 92
93 #define D(lvl, args...) do \ 93 #define D(lvl, args...) do \
94 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \ 94 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
95 while (0) 95 while (0)
96 #define D_(lvl, args...) D(lvl, ##args) 96 #define D_(lvl, args...) D(lvl, ##args)
97 97
98 /* These printouts are always printed */ 98 /* These printouts are always printed */
99 99
100 #else 100 #else
101 static int debug; 101 static int debug;
102 #define D_(lvl, args...) 102 #define D_(lvl, args...)
103 #endif 103 #endif
104 104
105 /* TODO: rewrite to optimize macros... */ 105 /* TODO: rewrite to optimize macros... */
106 106
107 #define TMP_BUF_MAX 256 107 #define TMP_BUF_MAX 256
108 108
109 #define DUMP(buf__,len__) \ 109 #define DUMP(buf__,len__) \
110 do { \ 110 do { \
111 char tbuf[TMP_BUF_MAX] = {0};\ 111 char tbuf[TMP_BUF_MAX] = {0};\
112 if (len__ > 1) {\ 112 if (len__ > 1) {\
113 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\ 113 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
114 if (tbuf[len__-2] == '\r') {\ 114 if (tbuf[len__-2] == '\r') {\
115 tbuf[len__-2] = 'r';\ 115 tbuf[len__-2] = 'r';\
116 } \ 116 } \
117 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\ 117 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
118 } else {\ 118 } else {\
119 DBG1("SENDING: '%s' (%d)", tbuf, len__);\ 119 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
120 } \ 120 } \
121 } while (0) 121 } while (0)
122 122
123 /* Defines */ 123 /* Defines */
124 #define NOZOMI_NAME "nozomi" 124 #define NOZOMI_NAME "nozomi"
125 #define NOZOMI_NAME_TTY "nozomi_tty" 125 #define NOZOMI_NAME_TTY "nozomi_tty"
126 #define DRIVER_DESC "Nozomi driver" 126 #define DRIVER_DESC "Nozomi driver"
127 127
128 #define NTTY_TTY_MAXMINORS 256 128 #define NTTY_TTY_MAXMINORS 256
129 #define NTTY_FIFO_BUFFER_SIZE 8192 129 #define NTTY_FIFO_BUFFER_SIZE 8192
130 130
131 /* Must be power of 2 */ 131 /* Must be power of 2 */
132 #define FIFO_BUFFER_SIZE_UL 8192 132 #define FIFO_BUFFER_SIZE_UL 8192
133 133
134 /* Size of tmp send buffer to card */ 134 /* Size of tmp send buffer to card */
135 #define SEND_BUF_MAX 1024 135 #define SEND_BUF_MAX 1024
136 #define RECEIVE_BUF_MAX 4 136 #define RECEIVE_BUF_MAX 4
137 137
138 138
139 #define R_IIR 0x0000 /* Interrupt Identity Register */ 139 #define R_IIR 0x0000 /* Interrupt Identity Register */
140 #define R_FCR 0x0000 /* Flow Control Register */ 140 #define R_FCR 0x0000 /* Flow Control Register */
141 #define R_IER 0x0004 /* Interrupt Enable Register */ 141 #define R_IER 0x0004 /* Interrupt Enable Register */
142 142
143 #define CONFIG_MAGIC 0xEFEFFEFE 143 #define CONFIG_MAGIC 0xEFEFFEFE
144 #define TOGGLE_VALID 0x0000 144 #define TOGGLE_VALID 0x0000
145 145
146 /* Definition of interrupt tokens */ 146 /* Definition of interrupt tokens */
147 #define MDM_DL1 0x0001 147 #define MDM_DL1 0x0001
148 #define MDM_UL1 0x0002 148 #define MDM_UL1 0x0002
149 #define MDM_DL2 0x0004 149 #define MDM_DL2 0x0004
150 #define MDM_UL2 0x0008 150 #define MDM_UL2 0x0008
151 #define DIAG_DL1 0x0010 151 #define DIAG_DL1 0x0010
152 #define DIAG_DL2 0x0020 152 #define DIAG_DL2 0x0020
153 #define DIAG_UL 0x0040 153 #define DIAG_UL 0x0040
154 #define APP1_DL 0x0080 154 #define APP1_DL 0x0080
155 #define APP1_UL 0x0100 155 #define APP1_UL 0x0100
156 #define APP2_DL 0x0200 156 #define APP2_DL 0x0200
157 #define APP2_UL 0x0400 157 #define APP2_UL 0x0400
158 #define CTRL_DL 0x0800 158 #define CTRL_DL 0x0800
159 #define CTRL_UL 0x1000 159 #define CTRL_UL 0x1000
160 #define RESET 0x8000 160 #define RESET 0x8000
161 161
162 #define MDM_DL (MDM_DL1 | MDM_DL2) 162 #define MDM_DL (MDM_DL1 | MDM_DL2)
163 #define MDM_UL (MDM_UL1 | MDM_UL2) 163 #define MDM_UL (MDM_UL1 | MDM_UL2)
164 #define DIAG_DL (DIAG_DL1 | DIAG_DL2) 164 #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
165 165
166 /* modem signal definition */ 166 /* modem signal definition */
167 #define CTRL_DSR 0x0001 167 #define CTRL_DSR 0x0001
168 #define CTRL_DCD 0x0002 168 #define CTRL_DCD 0x0002
169 #define CTRL_RI 0x0004 169 #define CTRL_RI 0x0004
170 #define CTRL_CTS 0x0008 170 #define CTRL_CTS 0x0008
171 171
172 #define CTRL_DTR 0x0001 172 #define CTRL_DTR 0x0001
173 #define CTRL_RTS 0x0002 173 #define CTRL_RTS 0x0002
174 174
175 #define MAX_PORT 4 175 #define MAX_PORT 4
176 #define NOZOMI_MAX_PORTS 5 176 #define NOZOMI_MAX_PORTS 5
177 #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT) 177 #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
178 178
179 /* Type definitions */ 179 /* Type definitions */
180 180
181 /* 181 /*
182 * There are two types of nozomi cards, 182 * There are two types of nozomi cards,
183 * one with 2048 memory and with 8192 memory 183 * one with 2048 memory and with 8192 memory
184 */ 184 */
185 enum card_type { 185 enum card_type {
186 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */ 186 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
187 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */ 187 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
188 }; 188 };
189 189
190 /* Initialization states a card can be in */ 190 /* Initialization states a card can be in */
191 enum card_state { 191 enum card_state {
192 NOZOMI_STATE_UKNOWN = 0, 192 NOZOMI_STATE_UKNOWN = 0,
193 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */ 193 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
194 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */ 194 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
195 NOZOMI_STATE_READY = 3, /* flowcontrols received */ 195 NOZOMI_STATE_READY = 3, /* flowcontrols received */
196 }; 196 };
197 197
198 /* Two different toggle channels exist */ 198 /* Two different toggle channels exist */
199 enum channel_type { 199 enum channel_type {
200 CH_A = 0, 200 CH_A = 0,
201 CH_B = 1, 201 CH_B = 1,
202 }; 202 };
203 203
204 /* Port definition for the card regarding flow control */ 204 /* Port definition for the card regarding flow control */
205 enum ctrl_port_type { 205 enum ctrl_port_type {
206 CTRL_CMD = 0, 206 CTRL_CMD = 0,
207 CTRL_MDM = 1, 207 CTRL_MDM = 1,
208 CTRL_DIAG = 2, 208 CTRL_DIAG = 2,
209 CTRL_APP1 = 3, 209 CTRL_APP1 = 3,
210 CTRL_APP2 = 4, 210 CTRL_APP2 = 4,
211 CTRL_ERROR = -1, 211 CTRL_ERROR = -1,
212 }; 212 };
213 213
214 /* Ports that the nozomi has */ 214 /* Ports that the nozomi has */
215 enum port_type { 215 enum port_type {
216 PORT_MDM = 0, 216 PORT_MDM = 0,
217 PORT_DIAG = 1, 217 PORT_DIAG = 1,
218 PORT_APP1 = 2, 218 PORT_APP1 = 2,
219 PORT_APP2 = 3, 219 PORT_APP2 = 3,
220 PORT_CTRL = 4, 220 PORT_CTRL = 4,
221 PORT_ERROR = -1, 221 PORT_ERROR = -1,
222 }; 222 };
223 223
224 #ifdef __BIG_ENDIAN 224 #ifdef __BIG_ENDIAN
225 /* Big endian */ 225 /* Big endian */
226 226
227 struct toggles { 227 struct toggles {
228 unsigned int enabled:5; /* 228 unsigned int enabled:5; /*
229 * Toggle fields are valid if enabled is 0, 229 * Toggle fields are valid if enabled is 0,
230 * else A-channels must always be used. 230 * else A-channels must always be used.
231 */ 231 */
232 unsigned int diag_dl:1; 232 unsigned int diag_dl:1;
233 unsigned int mdm_dl:1; 233 unsigned int mdm_dl:1;
234 unsigned int mdm_ul:1; 234 unsigned int mdm_ul:1;
235 } __attribute__ ((packed)); 235 } __attribute__ ((packed));
236 236
237 /* Configuration table to read at startup of card */ 237 /* Configuration table to read at startup of card */
238 /* Is for now only needed during initialization phase */ 238 /* Is for now only needed during initialization phase */
239 struct config_table { 239 struct config_table {
240 u32 signature; 240 u32 signature;
241 u16 product_information; 241 u16 product_information;
242 u16 version; 242 u16 version;
243 u8 pad3[3]; 243 u8 pad3[3];
244 struct toggles toggle; 244 struct toggles toggle;
245 u8 pad1[4]; 245 u8 pad1[4];
246 u16 dl_mdm_len1; /* 246 u16 dl_mdm_len1; /*
247 * If this is 64, it can hold 247 * If this is 64, it can hold
248 * 60 bytes + 4 that is length field 248 * 60 bytes + 4 that is length field
249 */ 249 */
250 u16 dl_start; 250 u16 dl_start;
251 251
252 u16 dl_diag_len1; 252 u16 dl_diag_len1;
253 u16 dl_mdm_len2; /* 253 u16 dl_mdm_len2; /*
254 * If this is 64, it can hold 254 * If this is 64, it can hold
255 * 60 bytes + 4 that is length field 255 * 60 bytes + 4 that is length field
256 */ 256 */
257 u16 dl_app1_len; 257 u16 dl_app1_len;
258 258
259 u16 dl_diag_len2; 259 u16 dl_diag_len2;
260 u16 dl_ctrl_len; 260 u16 dl_ctrl_len;
261 u16 dl_app2_len; 261 u16 dl_app2_len;
262 u8 pad2[16]; 262 u8 pad2[16];
263 u16 ul_mdm_len1; 263 u16 ul_mdm_len1;
264 u16 ul_start; 264 u16 ul_start;
265 u16 ul_diag_len; 265 u16 ul_diag_len;
266 u16 ul_mdm_len2; 266 u16 ul_mdm_len2;
267 u16 ul_app1_len; 267 u16 ul_app1_len;
268 u16 ul_app2_len; 268 u16 ul_app2_len;
269 u16 ul_ctrl_len; 269 u16 ul_ctrl_len;
270 } __attribute__ ((packed)); 270 } __attribute__ ((packed));
271 271
272 /* This stores all control downlink flags */ 272 /* This stores all control downlink flags */
273 struct ctrl_dl { 273 struct ctrl_dl {
274 u8 port; 274 u8 port;
275 unsigned int reserved:4; 275 unsigned int reserved:4;
276 unsigned int CTS:1; 276 unsigned int CTS:1;
277 unsigned int RI:1; 277 unsigned int RI:1;
278 unsigned int DCD:1; 278 unsigned int DCD:1;
279 unsigned int DSR:1; 279 unsigned int DSR:1;
280 } __attribute__ ((packed)); 280 } __attribute__ ((packed));
281 281
282 /* This stores all control uplink flags */ 282 /* This stores all control uplink flags */
283 struct ctrl_ul { 283 struct ctrl_ul {
284 u8 port; 284 u8 port;
285 unsigned int reserved:6; 285 unsigned int reserved:6;
286 unsigned int RTS:1; 286 unsigned int RTS:1;
287 unsigned int DTR:1; 287 unsigned int DTR:1;
288 } __attribute__ ((packed)); 288 } __attribute__ ((packed));
289 289
290 #else 290 #else
291 /* Little endian */ 291 /* Little endian */
292 292
293 /* This represents the toggle information */ 293 /* This represents the toggle information */
294 struct toggles { 294 struct toggles {
295 unsigned int mdm_ul:1; 295 unsigned int mdm_ul:1;
296 unsigned int mdm_dl:1; 296 unsigned int mdm_dl:1;
297 unsigned int diag_dl:1; 297 unsigned int diag_dl:1;
298 unsigned int enabled:5; /* 298 unsigned int enabled:5; /*
299 * Toggle fields are valid if enabled is 0, 299 * Toggle fields are valid if enabled is 0,
300 * else A-channels must always be used. 300 * else A-channels must always be used.
301 */ 301 */
302 } __attribute__ ((packed)); 302 } __attribute__ ((packed));
303 303
304 /* Configuration table to read at startup of card */ 304 /* Configuration table to read at startup of card */
305 struct config_table { 305 struct config_table {
306 u32 signature; 306 u32 signature;
307 u16 version; 307 u16 version;
308 u16 product_information; 308 u16 product_information;
309 struct toggles toggle; 309 struct toggles toggle;
310 u8 pad1[7]; 310 u8 pad1[7];
311 u16 dl_start; 311 u16 dl_start;
312 u16 dl_mdm_len1; /* 312 u16 dl_mdm_len1; /*
313 * If this is 64, it can hold 313 * If this is 64, it can hold
314 * 60 bytes + 4 that is length field 314 * 60 bytes + 4 that is length field
315 */ 315 */
316 u16 dl_mdm_len2; 316 u16 dl_mdm_len2;
317 u16 dl_diag_len1; 317 u16 dl_diag_len1;
318 u16 dl_diag_len2; 318 u16 dl_diag_len2;
319 u16 dl_app1_len; 319 u16 dl_app1_len;
320 u16 dl_app2_len; 320 u16 dl_app2_len;
321 u16 dl_ctrl_len; 321 u16 dl_ctrl_len;
322 u8 pad2[16]; 322 u8 pad2[16];
323 u16 ul_start; 323 u16 ul_start;
324 u16 ul_mdm_len2; 324 u16 ul_mdm_len2;
325 u16 ul_mdm_len1; 325 u16 ul_mdm_len1;
326 u16 ul_diag_len; 326 u16 ul_diag_len;
327 u16 ul_app1_len; 327 u16 ul_app1_len;
328 u16 ul_app2_len; 328 u16 ul_app2_len;
329 u16 ul_ctrl_len; 329 u16 ul_ctrl_len;
330 } __attribute__ ((packed)); 330 } __attribute__ ((packed));
331 331
332 /* This stores all control downlink flags */ 332 /* This stores all control downlink flags */
333 struct ctrl_dl { 333 struct ctrl_dl {
334 unsigned int DSR:1; 334 unsigned int DSR:1;
335 unsigned int DCD:1; 335 unsigned int DCD:1;
336 unsigned int RI:1; 336 unsigned int RI:1;
337 unsigned int CTS:1; 337 unsigned int CTS:1;
338 unsigned int reserverd:4; 338 unsigned int reserverd:4;
339 u8 port; 339 u8 port;
340 } __attribute__ ((packed)); 340 } __attribute__ ((packed));
341 341
342 /* This stores all control uplink flags */ 342 /* This stores all control uplink flags */
343 struct ctrl_ul { 343 struct ctrl_ul {
344 unsigned int DTR:1; 344 unsigned int DTR:1;
345 unsigned int RTS:1; 345 unsigned int RTS:1;
346 unsigned int reserved:6; 346 unsigned int reserved:6;
347 u8 port; 347 u8 port;
348 } __attribute__ ((packed)); 348 } __attribute__ ((packed));
349 #endif 349 #endif
350 350
351 /* This holds all information that is needed regarding a port */ 351 /* This holds all information that is needed regarding a port */
352 struct port { 352 struct port {
353 struct tty_port port; 353 struct tty_port port;
354 u8 update_flow_control; 354 u8 update_flow_control;
355 struct ctrl_ul ctrl_ul; 355 struct ctrl_ul ctrl_ul;
356 struct ctrl_dl ctrl_dl; 356 struct ctrl_dl ctrl_dl;
357 struct kfifo fifo_ul; 357 struct kfifo fifo_ul;
358 void __iomem *dl_addr[2]; 358 void __iomem *dl_addr[2];
359 u32 dl_size[2]; 359 u32 dl_size[2];
360 u8 toggle_dl; 360 u8 toggle_dl;
361 void __iomem *ul_addr[2]; 361 void __iomem *ul_addr[2];
362 u32 ul_size[2]; 362 u32 ul_size[2];
363 u8 toggle_ul; 363 u8 toggle_ul;
364 u16 token_dl; 364 u16 token_dl;
365 365
366 wait_queue_head_t tty_wait; 366 wait_queue_head_t tty_wait;
367 struct async_icount tty_icount; 367 struct async_icount tty_icount;
368 368
369 struct nozomi *dc; 369 struct nozomi *dc;
370 }; 370 };
371 371
372 /* Private data one for each card in the system */ 372 /* Private data one for each card in the system */
373 struct nozomi { 373 struct nozomi {
374 void __iomem *base_addr; 374 void __iomem *base_addr;
375 unsigned long flip; 375 unsigned long flip;
376 376
377 /* Pointers to registers */ 377 /* Pointers to registers */
378 void __iomem *reg_iir; 378 void __iomem *reg_iir;
379 void __iomem *reg_fcr; 379 void __iomem *reg_fcr;
380 void __iomem *reg_ier; 380 void __iomem *reg_ier;
381 381
382 u16 last_ier; 382 u16 last_ier;
383 enum card_type card_type; 383 enum card_type card_type;
384 struct config_table config_table; /* Configuration table */ 384 struct config_table config_table; /* Configuration table */
385 struct pci_dev *pdev; 385 struct pci_dev *pdev;
386 struct port port[NOZOMI_MAX_PORTS]; 386 struct port port[NOZOMI_MAX_PORTS];
387 u8 *send_buf; 387 u8 *send_buf;
388 388
389 spinlock_t spin_mutex; /* secures access to registers and tty */ 389 spinlock_t spin_mutex; /* secures access to registers and tty */
390 390
391 unsigned int index_start; 391 unsigned int index_start;
392 enum card_state state; 392 enum card_state state;
393 u32 open_ttys; 393 u32 open_ttys;
394 }; 394 };
395 395
396 /* This is a data packet that is read or written to/from card */ 396 /* This is a data packet that is read or written to/from card */
397 struct buffer { 397 struct buffer {
398 u32 size; /* size is the length of the data buffer */ 398 u32 size; /* size is the length of the data buffer */
399 u8 *data; 399 u8 *data;
400 } __attribute__ ((packed)); 400 } __attribute__ ((packed));
401 401
402 /* Global variables */ 402 /* Global variables */
403 static const struct pci_device_id nozomi_pci_tbl[] = { 403 static const struct pci_device_id nozomi_pci_tbl[] = {
404 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */ 404 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
405 {}, 405 {},
406 }; 406 };
407 407
408 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl); 408 MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
409 409
410 static struct nozomi *ndevs[NOZOMI_MAX_CARDS]; 410 static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
411 static struct tty_driver *ntty_driver; 411 static struct tty_driver *ntty_driver;
412 412
413 static const struct tty_port_operations noz_tty_port_ops; 413 static const struct tty_port_operations noz_tty_port_ops;
414 414
415 /* 415 /*
416 * find card by tty_index 416 * find card by tty_index
417 */ 417 */
418 static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty) 418 static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
419 { 419 {
420 return tty ? ndevs[tty->index / MAX_PORT] : NULL; 420 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
421 } 421 }
422 422
423 static inline struct port *get_port_by_tty(const struct tty_struct *tty) 423 static inline struct port *get_port_by_tty(const struct tty_struct *tty)
424 { 424 {
425 struct nozomi *ndev = get_dc_by_tty(tty); 425 struct nozomi *ndev = get_dc_by_tty(tty);
426 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL; 426 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
427 } 427 }
428 428
429 /* 429 /*
430 * TODO: 430 * TODO:
431 * -Optimize 431 * -Optimize
432 * -Rewrite cleaner 432 * -Rewrite cleaner
433 */ 433 */
434 434
435 static void read_mem32(u32 *buf, const void __iomem *mem_addr_start, 435 static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
436 u32 size_bytes) 436 u32 size_bytes)
437 { 437 {
438 u32 i = 0; 438 u32 i = 0;
439 const u32 __iomem *ptr = mem_addr_start; 439 const u32 __iomem *ptr = mem_addr_start;
440 u16 *buf16; 440 u16 *buf16;
441 441
442 if (unlikely(!ptr || !buf)) 442 if (unlikely(!ptr || !buf))
443 goto out; 443 goto out;
444 444
445 /* shortcut for extremely often used cases */ 445 /* shortcut for extremely often used cases */
446 switch (size_bytes) { 446 switch (size_bytes) {
447 case 2: /* 2 bytes */ 447 case 2: /* 2 bytes */
448 buf16 = (u16 *) buf; 448 buf16 = (u16 *) buf;
449 *buf16 = __le16_to_cpu(readw(ptr)); 449 *buf16 = __le16_to_cpu(readw(ptr));
450 goto out; 450 goto out;
451 break; 451 break;
452 case 4: /* 4 bytes */ 452 case 4: /* 4 bytes */
453 *(buf) = __le32_to_cpu(readl(ptr)); 453 *(buf) = __le32_to_cpu(readl(ptr));
454 goto out; 454 goto out;
455 break; 455 break;
456 } 456 }
457 457
458 while (i < size_bytes) { 458 while (i < size_bytes) {
459 if (size_bytes - i == 2) { 459 if (size_bytes - i == 2) {
460 /* Handle 2 bytes in the end */ 460 /* Handle 2 bytes in the end */
461 buf16 = (u16 *) buf; 461 buf16 = (u16 *) buf;
462 *(buf16) = __le16_to_cpu(readw(ptr)); 462 *(buf16) = __le16_to_cpu(readw(ptr));
463 i += 2; 463 i += 2;
464 } else { 464 } else {
465 /* Read 4 bytes */ 465 /* Read 4 bytes */
466 *(buf) = __le32_to_cpu(readl(ptr)); 466 *(buf) = __le32_to_cpu(readl(ptr));
467 i += 4; 467 i += 4;
468 } 468 }
469 buf++; 469 buf++;
470 ptr++; 470 ptr++;
471 } 471 }
472 out: 472 out:
473 return; 473 return;
474 } 474 }
475 475
476 /* 476 /*
477 * TODO: 477 * TODO:
478 * -Optimize 478 * -Optimize
479 * -Rewrite cleaner 479 * -Rewrite cleaner
480 */ 480 */
481 static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf, 481 static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
482 u32 size_bytes) 482 u32 size_bytes)
483 { 483 {
484 u32 i = 0; 484 u32 i = 0;
485 u32 __iomem *ptr = mem_addr_start; 485 u32 __iomem *ptr = mem_addr_start;
486 const u16 *buf16; 486 const u16 *buf16;
487 487
488 if (unlikely(!ptr || !buf)) 488 if (unlikely(!ptr || !buf))
489 return 0; 489 return 0;
490 490
491 /* shortcut for extremely often used cases */ 491 /* shortcut for extremely often used cases */
492 switch (size_bytes) { 492 switch (size_bytes) {
493 case 2: /* 2 bytes */ 493 case 2: /* 2 bytes */
494 buf16 = (const u16 *)buf; 494 buf16 = (const u16 *)buf;
495 writew(__cpu_to_le16(*buf16), ptr); 495 writew(__cpu_to_le16(*buf16), ptr);
496 return 2; 496 return 2;
497 break; 497 break;
498 case 1: /* 498 case 1: /*
499 * also needs to write 4 bytes in this case 499 * also needs to write 4 bytes in this case
500 * so falling through.. 500 * so falling through..
501 */ 501 */
502 case 4: /* 4 bytes */ 502 case 4: /* 4 bytes */
503 writel(__cpu_to_le32(*buf), ptr); 503 writel(__cpu_to_le32(*buf), ptr);
504 return 4; 504 return 4;
505 break; 505 break;
506 } 506 }
507 507
508 while (i < size_bytes) { 508 while (i < size_bytes) {
509 if (size_bytes - i == 2) { 509 if (size_bytes - i == 2) {
510 /* 2 bytes */ 510 /* 2 bytes */
511 buf16 = (const u16 *)buf; 511 buf16 = (const u16 *)buf;
512 writew(__cpu_to_le16(*buf16), ptr); 512 writew(__cpu_to_le16(*buf16), ptr);
513 i += 2; 513 i += 2;
514 } else { 514 } else {
515 /* 4 bytes */ 515 /* 4 bytes */
516 writel(__cpu_to_le32(*buf), ptr); 516 writel(__cpu_to_le32(*buf), ptr);
517 i += 4; 517 i += 4;
518 } 518 }
519 buf++; 519 buf++;
520 ptr++; 520 ptr++;
521 } 521 }
522 return i; 522 return i;
523 } 523 }
524 524
525 /* Setup pointers to different channels and also setup buffer sizes. */ 525 /* Setup pointers to different channels and also setup buffer sizes. */
526 static void setup_memory(struct nozomi *dc) 526 static void setup_memory(struct nozomi *dc)
527 { 527 {
528 void __iomem *offset = dc->base_addr + dc->config_table.dl_start; 528 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
529 /* The length reported is including the length field of 4 bytes, 529 /* The length reported is including the length field of 4 bytes,
530 * hence subtract with 4. 530 * hence subtract with 4.
531 */ 531 */
532 const u16 buff_offset = 4; 532 const u16 buff_offset = 4;
533 533
534 /* Modem port dl configuration */ 534 /* Modem port dl configuration */
535 dc->port[PORT_MDM].dl_addr[CH_A] = offset; 535 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
536 dc->port[PORT_MDM].dl_addr[CH_B] = 536 dc->port[PORT_MDM].dl_addr[CH_B] =
537 (offset += dc->config_table.dl_mdm_len1); 537 (offset += dc->config_table.dl_mdm_len1);
538 dc->port[PORT_MDM].dl_size[CH_A] = 538 dc->port[PORT_MDM].dl_size[CH_A] =
539 dc->config_table.dl_mdm_len1 - buff_offset; 539 dc->config_table.dl_mdm_len1 - buff_offset;
540 dc->port[PORT_MDM].dl_size[CH_B] = 540 dc->port[PORT_MDM].dl_size[CH_B] =
541 dc->config_table.dl_mdm_len2 - buff_offset; 541 dc->config_table.dl_mdm_len2 - buff_offset;
542 542
543 /* Diag port dl configuration */ 543 /* Diag port dl configuration */
544 dc->port[PORT_DIAG].dl_addr[CH_A] = 544 dc->port[PORT_DIAG].dl_addr[CH_A] =
545 (offset += dc->config_table.dl_mdm_len2); 545 (offset += dc->config_table.dl_mdm_len2);
546 dc->port[PORT_DIAG].dl_size[CH_A] = 546 dc->port[PORT_DIAG].dl_size[CH_A] =
547 dc->config_table.dl_diag_len1 - buff_offset; 547 dc->config_table.dl_diag_len1 - buff_offset;
548 dc->port[PORT_DIAG].dl_addr[CH_B] = 548 dc->port[PORT_DIAG].dl_addr[CH_B] =
549 (offset += dc->config_table.dl_diag_len1); 549 (offset += dc->config_table.dl_diag_len1);
550 dc->port[PORT_DIAG].dl_size[CH_B] = 550 dc->port[PORT_DIAG].dl_size[CH_B] =
551 dc->config_table.dl_diag_len2 - buff_offset; 551 dc->config_table.dl_diag_len2 - buff_offset;
552 552
553 /* App1 port dl configuration */ 553 /* App1 port dl configuration */
554 dc->port[PORT_APP1].dl_addr[CH_A] = 554 dc->port[PORT_APP1].dl_addr[CH_A] =
555 (offset += dc->config_table.dl_diag_len2); 555 (offset += dc->config_table.dl_diag_len2);
556 dc->port[PORT_APP1].dl_size[CH_A] = 556 dc->port[PORT_APP1].dl_size[CH_A] =
557 dc->config_table.dl_app1_len - buff_offset; 557 dc->config_table.dl_app1_len - buff_offset;
558 558
559 /* App2 port dl configuration */ 559 /* App2 port dl configuration */
560 dc->port[PORT_APP2].dl_addr[CH_A] = 560 dc->port[PORT_APP2].dl_addr[CH_A] =
561 (offset += dc->config_table.dl_app1_len); 561 (offset += dc->config_table.dl_app1_len);
562 dc->port[PORT_APP2].dl_size[CH_A] = 562 dc->port[PORT_APP2].dl_size[CH_A] =
563 dc->config_table.dl_app2_len - buff_offset; 563 dc->config_table.dl_app2_len - buff_offset;
564 564
565 /* Ctrl dl configuration */ 565 /* Ctrl dl configuration */
566 dc->port[PORT_CTRL].dl_addr[CH_A] = 566 dc->port[PORT_CTRL].dl_addr[CH_A] =
567 (offset += dc->config_table.dl_app2_len); 567 (offset += dc->config_table.dl_app2_len);
568 dc->port[PORT_CTRL].dl_size[CH_A] = 568 dc->port[PORT_CTRL].dl_size[CH_A] =
569 dc->config_table.dl_ctrl_len - buff_offset; 569 dc->config_table.dl_ctrl_len - buff_offset;
570 570
571 offset = dc->base_addr + dc->config_table.ul_start; 571 offset = dc->base_addr + dc->config_table.ul_start;
572 572
573 /* Modem Port ul configuration */ 573 /* Modem Port ul configuration */
574 dc->port[PORT_MDM].ul_addr[CH_A] = offset; 574 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
575 dc->port[PORT_MDM].ul_size[CH_A] = 575 dc->port[PORT_MDM].ul_size[CH_A] =
576 dc->config_table.ul_mdm_len1 - buff_offset; 576 dc->config_table.ul_mdm_len1 - buff_offset;
577 dc->port[PORT_MDM].ul_addr[CH_B] = 577 dc->port[PORT_MDM].ul_addr[CH_B] =
578 (offset += dc->config_table.ul_mdm_len1); 578 (offset += dc->config_table.ul_mdm_len1);
579 dc->port[PORT_MDM].ul_size[CH_B] = 579 dc->port[PORT_MDM].ul_size[CH_B] =
580 dc->config_table.ul_mdm_len2 - buff_offset; 580 dc->config_table.ul_mdm_len2 - buff_offset;
581 581
582 /* Diag port ul configuration */ 582 /* Diag port ul configuration */
583 dc->port[PORT_DIAG].ul_addr[CH_A] = 583 dc->port[PORT_DIAG].ul_addr[CH_A] =
584 (offset += dc->config_table.ul_mdm_len2); 584 (offset += dc->config_table.ul_mdm_len2);
585 dc->port[PORT_DIAG].ul_size[CH_A] = 585 dc->port[PORT_DIAG].ul_size[CH_A] =
586 dc->config_table.ul_diag_len - buff_offset; 586 dc->config_table.ul_diag_len - buff_offset;
587 587
588 /* App1 port ul configuration */ 588 /* App1 port ul configuration */
589 dc->port[PORT_APP1].ul_addr[CH_A] = 589 dc->port[PORT_APP1].ul_addr[CH_A] =
590 (offset += dc->config_table.ul_diag_len); 590 (offset += dc->config_table.ul_diag_len);
591 dc->port[PORT_APP1].ul_size[CH_A] = 591 dc->port[PORT_APP1].ul_size[CH_A] =
592 dc->config_table.ul_app1_len - buff_offset; 592 dc->config_table.ul_app1_len - buff_offset;
593 593
594 /* App2 port ul configuration */ 594 /* App2 port ul configuration */
595 dc->port[PORT_APP2].ul_addr[CH_A] = 595 dc->port[PORT_APP2].ul_addr[CH_A] =
596 (offset += dc->config_table.ul_app1_len); 596 (offset += dc->config_table.ul_app1_len);
597 dc->port[PORT_APP2].ul_size[CH_A] = 597 dc->port[PORT_APP2].ul_size[CH_A] =
598 dc->config_table.ul_app2_len - buff_offset; 598 dc->config_table.ul_app2_len - buff_offset;
599 599
600 /* Ctrl ul configuration */ 600 /* Ctrl ul configuration */
601 dc->port[PORT_CTRL].ul_addr[CH_A] = 601 dc->port[PORT_CTRL].ul_addr[CH_A] =
602 (offset += dc->config_table.ul_app2_len); 602 (offset += dc->config_table.ul_app2_len);
603 dc->port[PORT_CTRL].ul_size[CH_A] = 603 dc->port[PORT_CTRL].ul_size[CH_A] =
604 dc->config_table.ul_ctrl_len - buff_offset; 604 dc->config_table.ul_ctrl_len - buff_offset;
605 } 605 }
606 606
607 /* Dump config table under initalization phase */ 607 /* Dump config table under initalization phase */
608 #ifdef DEBUG 608 #ifdef DEBUG
609 static void dump_table(const struct nozomi *dc) 609 static void dump_table(const struct nozomi *dc)
610 { 610 {
611 DBG3("signature: 0x%08X", dc->config_table.signature); 611 DBG3("signature: 0x%08X", dc->config_table.signature);
612 DBG3("version: 0x%04X", dc->config_table.version); 612 DBG3("version: 0x%04X", dc->config_table.version);
613 DBG3("product_information: 0x%04X", \ 613 DBG3("product_information: 0x%04X", \
614 dc->config_table.product_information); 614 dc->config_table.product_information);
615 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled); 615 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
616 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul); 616 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
617 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl); 617 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
618 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl); 618 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
619 619
620 DBG3("dl_start: 0x%04X", dc->config_table.dl_start); 620 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
621 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1, 621 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
622 dc->config_table.dl_mdm_len1); 622 dc->config_table.dl_mdm_len1);
623 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2, 623 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
624 dc->config_table.dl_mdm_len2); 624 dc->config_table.dl_mdm_len2);
625 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1, 625 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
626 dc->config_table.dl_diag_len1); 626 dc->config_table.dl_diag_len1);
627 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2, 627 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
628 dc->config_table.dl_diag_len2); 628 dc->config_table.dl_diag_len2);
629 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len, 629 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
630 dc->config_table.dl_app1_len); 630 dc->config_table.dl_app1_len);
631 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len, 631 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
632 dc->config_table.dl_app2_len); 632 dc->config_table.dl_app2_len);
633 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len, 633 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
634 dc->config_table.dl_ctrl_len); 634 dc->config_table.dl_ctrl_len);
635 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start, 635 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
636 dc->config_table.ul_start); 636 dc->config_table.ul_start);
637 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1, 637 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
638 dc->config_table.ul_mdm_len1); 638 dc->config_table.ul_mdm_len1);
639 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2, 639 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
640 dc->config_table.ul_mdm_len2); 640 dc->config_table.ul_mdm_len2);
641 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len, 641 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
642 dc->config_table.ul_diag_len); 642 dc->config_table.ul_diag_len);
643 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len, 643 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
644 dc->config_table.ul_app1_len); 644 dc->config_table.ul_app1_len);
645 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len, 645 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
646 dc->config_table.ul_app2_len); 646 dc->config_table.ul_app2_len);
647 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len, 647 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
648 dc->config_table.ul_ctrl_len); 648 dc->config_table.ul_ctrl_len);
649 } 649 }
650 #else 650 #else
651 static inline void dump_table(const struct nozomi *dc) { } 651 static inline void dump_table(const struct nozomi *dc) { }
652 #endif 652 #endif
653 653
654 /* 654 /*
655 * Read configuration table from card under intalization phase 655 * Read configuration table from card under intalization phase
656 * Returns 1 if ok, else 0 656 * Returns 1 if ok, else 0
657 */ 657 */
658 static int nozomi_read_config_table(struct nozomi *dc) 658 static int nozomi_read_config_table(struct nozomi *dc)
659 { 659 {
660 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0, 660 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
661 sizeof(struct config_table)); 661 sizeof(struct config_table));
662 662
663 if (dc->config_table.signature != CONFIG_MAGIC) { 663 if (dc->config_table.signature != CONFIG_MAGIC) {
664 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n", 664 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
665 dc->config_table.signature, CONFIG_MAGIC); 665 dc->config_table.signature, CONFIG_MAGIC);
666 return 0; 666 return 0;
667 } 667 }
668 668
669 if ((dc->config_table.version == 0) 669 if ((dc->config_table.version == 0)
670 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) { 670 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
671 int i; 671 int i;
672 DBG1("Second phase, configuring card"); 672 DBG1("Second phase, configuring card");
673 673
674 setup_memory(dc); 674 setup_memory(dc);
675 675
676 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul; 676 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
677 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl; 677 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
678 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl; 678 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
679 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d", 679 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
680 dc->port[PORT_MDM].toggle_ul, 680 dc->port[PORT_MDM].toggle_ul,
681 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl); 681 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
682 682
683 dump_table(dc); 683 dump_table(dc);
684 684
685 for (i = PORT_MDM; i < MAX_PORT; i++) { 685 for (i = PORT_MDM; i < MAX_PORT; i++) {
686 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl)); 686 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
687 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul)); 687 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
688 } 688 }
689 689
690 /* Enable control channel */ 690 /* Enable control channel */
691 dc->last_ier = dc->last_ier | CTRL_DL; 691 dc->last_ier = dc->last_ier | CTRL_DL;
692 writew(dc->last_ier, dc->reg_ier); 692 writew(dc->last_ier, dc->reg_ier);
693 693
694 dc->state = NOZOMI_STATE_ALLOCATED; 694 dc->state = NOZOMI_STATE_ALLOCATED;
695 dev_info(&dc->pdev->dev, "Initialization OK!\n"); 695 dev_info(&dc->pdev->dev, "Initialization OK!\n");
696 return 1; 696 return 1;
697 } 697 }
698 698
699 if ((dc->config_table.version > 0) 699 if ((dc->config_table.version > 0)
700 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) { 700 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
701 u32 offset = 0; 701 u32 offset = 0;
702 DBG1("First phase: pushing upload buffers, clearing download"); 702 DBG1("First phase: pushing upload buffers, clearing download");
703 703
704 dev_info(&dc->pdev->dev, "Version of card: %d\n", 704 dev_info(&dc->pdev->dev, "Version of card: %d\n",
705 dc->config_table.version); 705 dc->config_table.version);
706 706
707 /* Here we should disable all I/O over F32. */ 707 /* Here we should disable all I/O over F32. */
708 setup_memory(dc); 708 setup_memory(dc);
709 709
710 /* 710 /*
711 * We should send ALL channel pair tokens back along 711 * We should send ALL channel pair tokens back along
712 * with reset token 712 * with reset token
713 */ 713 */
714 714
715 /* push upload modem buffers */ 715 /* push upload modem buffers */
716 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A], 716 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
717 (u32 *) &offset, 4); 717 (u32 *) &offset, 4);
718 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B], 718 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
719 (u32 *) &offset, 4); 719 (u32 *) &offset, 4);
720 720
721 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr); 721 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
722 722
723 DBG1("First phase done"); 723 DBG1("First phase done");
724 } 724 }
725 725
726 return 1; 726 return 1;
727 } 727 }
728 728
729 /* Enable uplink interrupts */ 729 /* Enable uplink interrupts */
730 static void enable_transmit_ul(enum port_type port, struct nozomi *dc) 730 static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
731 { 731 {
732 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL}; 732 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
733 733
734 if (port < NOZOMI_MAX_PORTS) { 734 if (port < NOZOMI_MAX_PORTS) {
735 dc->last_ier |= mask[port]; 735 dc->last_ier |= mask[port];
736 writew(dc->last_ier, dc->reg_ier); 736 writew(dc->last_ier, dc->reg_ier);
737 } else { 737 } else {
738 dev_err(&dc->pdev->dev, "Called with wrong port?\n"); 738 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
739 } 739 }
740 } 740 }
741 741
742 /* Disable uplink interrupts */ 742 /* Disable uplink interrupts */
743 static void disable_transmit_ul(enum port_type port, struct nozomi *dc) 743 static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
744 { 744 {
745 static const u16 mask[] = 745 static const u16 mask[] =
746 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL}; 746 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
747 747
748 if (port < NOZOMI_MAX_PORTS) { 748 if (port < NOZOMI_MAX_PORTS) {
749 dc->last_ier &= mask[port]; 749 dc->last_ier &= mask[port];
750 writew(dc->last_ier, dc->reg_ier); 750 writew(dc->last_ier, dc->reg_ier);
751 } else { 751 } else {
752 dev_err(&dc->pdev->dev, "Called with wrong port?\n"); 752 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
753 } 753 }
754 } 754 }
755 755
756 /* Enable downlink interrupts */ 756 /* Enable downlink interrupts */
757 static void enable_transmit_dl(enum port_type port, struct nozomi *dc) 757 static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
758 { 758 {
759 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL}; 759 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
760 760
761 if (port < NOZOMI_MAX_PORTS) { 761 if (port < NOZOMI_MAX_PORTS) {
762 dc->last_ier |= mask[port]; 762 dc->last_ier |= mask[port];
763 writew(dc->last_ier, dc->reg_ier); 763 writew(dc->last_ier, dc->reg_ier);
764 } else { 764 } else {
765 dev_err(&dc->pdev->dev, "Called with wrong port?\n"); 765 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
766 } 766 }
767 } 767 }
768 768
769 /* Disable downlink interrupts */ 769 /* Disable downlink interrupts */
770 static void disable_transmit_dl(enum port_type port, struct nozomi *dc) 770 static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
771 { 771 {
772 static const u16 mask[] = 772 static const u16 mask[] =
773 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL}; 773 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
774 774
775 if (port < NOZOMI_MAX_PORTS) { 775 if (port < NOZOMI_MAX_PORTS) {
776 dc->last_ier &= mask[port]; 776 dc->last_ier &= mask[port];
777 writew(dc->last_ier, dc->reg_ier); 777 writew(dc->last_ier, dc->reg_ier);
778 } else { 778 } else {
779 dev_err(&dc->pdev->dev, "Called with wrong port?\n"); 779 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
780 } 780 }
781 } 781 }
782 782
783 /* 783 /*
784 * Return 1 - send buffer to card and ack. 784 * Return 1 - send buffer to card and ack.
785 * Return 0 - don't ack, don't send buffer to card. 785 * Return 0 - don't ack, don't send buffer to card.
786 */ 786 */
787 static int send_data(enum port_type index, struct nozomi *dc) 787 static int send_data(enum port_type index, struct nozomi *dc)
788 { 788 {
789 u32 size = 0; 789 u32 size = 0;
790 struct port *port = &dc->port[index]; 790 struct port *port = &dc->port[index];
791 const u8 toggle = port->toggle_ul; 791 const u8 toggle = port->toggle_ul;
792 void __iomem *addr = port->ul_addr[toggle]; 792 void __iomem *addr = port->ul_addr[toggle];
793 const u32 ul_size = port->ul_size[toggle]; 793 const u32 ul_size = port->ul_size[toggle];
794 struct tty_struct *tty = tty_port_tty_get(&port->port);
795 794
796 /* Get data from tty and place in buf for now */ 795 /* Get data from tty and place in buf for now */
797 size = kfifo_out(&port->fifo_ul, dc->send_buf, 796 size = kfifo_out(&port->fifo_ul, dc->send_buf,
798 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX); 797 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
799 798
800 if (size == 0) { 799 if (size == 0) {
801 DBG4("No more data to send, disable link:"); 800 DBG4("No more data to send, disable link:");
802 tty_kref_put(tty);
803 return 0; 801 return 0;
804 } 802 }
805 803
806 /* DUMP(buf, size); */ 804 /* DUMP(buf, size); */
807 805
808 /* Write length + data */ 806 /* Write length + data */
809 write_mem32(addr, (u32 *) &size, 4); 807 write_mem32(addr, (u32 *) &size, 4);
810 write_mem32(addr + 4, (u32 *) dc->send_buf, size); 808 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
811 809
812 if (tty) 810 tty_port_tty_wakeup(&port->port);
813 tty_wakeup(tty);
814 811
815 tty_kref_put(tty);
816 return 1; 812 return 1;
817 } 813 }
818 814
819 /* If all data has been read, return 1, else 0 */ 815 /* If all data has been read, return 1, else 0 */
820 static int receive_data(enum port_type index, struct nozomi *dc) 816 static int receive_data(enum port_type index, struct nozomi *dc)
821 { 817 {
822 u8 buf[RECEIVE_BUF_MAX] = { 0 }; 818 u8 buf[RECEIVE_BUF_MAX] = { 0 };
823 int size; 819 int size;
824 u32 offset = 4; 820 u32 offset = 4;
825 struct port *port = &dc->port[index]; 821 struct port *port = &dc->port[index];
826 void __iomem *addr = port->dl_addr[port->toggle_dl]; 822 void __iomem *addr = port->dl_addr[port->toggle_dl];
827 struct tty_struct *tty = tty_port_tty_get(&port->port); 823 struct tty_struct *tty = tty_port_tty_get(&port->port);
828 int i, ret; 824 int i, ret;
829 825
830 read_mem32((u32 *) &size, addr, 4); 826 read_mem32((u32 *) &size, addr, 4);
831 /* DBG1( "%d bytes port: %d", size, index); */ 827 /* DBG1( "%d bytes port: %d", size, index); */
832 828
833 if (tty && test_bit(TTY_THROTTLED, &tty->flags)) { 829 if (tty && test_bit(TTY_THROTTLED, &tty->flags)) {
834 DBG1("No room in tty, don't read data, don't ack interrupt, " 830 DBG1("No room in tty, don't read data, don't ack interrupt, "
835 "disable interrupt"); 831 "disable interrupt");
836 832
837 /* disable interrupt in downlink... */ 833 /* disable interrupt in downlink... */
838 disable_transmit_dl(index, dc); 834 disable_transmit_dl(index, dc);
839 ret = 0; 835 ret = 0;
840 goto put; 836 goto put;
841 } 837 }
842 838
843 if (unlikely(size == 0)) { 839 if (unlikely(size == 0)) {
844 dev_err(&dc->pdev->dev, "size == 0?\n"); 840 dev_err(&dc->pdev->dev, "size == 0?\n");
845 ret = 1; 841 ret = 1;
846 goto put; 842 goto put;
847 } 843 }
848 844
849 while (size > 0) { 845 while (size > 0) {
850 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX); 846 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
851 847
852 if (size == 1) { 848 if (size == 1) {
853 tty_insert_flip_char(&port->port, buf[0], TTY_NORMAL); 849 tty_insert_flip_char(&port->port, buf[0], TTY_NORMAL);
854 size = 0; 850 size = 0;
855 } else if (size < RECEIVE_BUF_MAX) { 851 } else if (size < RECEIVE_BUF_MAX) {
856 size -= tty_insert_flip_string(&port->port, 852 size -= tty_insert_flip_string(&port->port,
857 (char *)buf, size); 853 (char *)buf, size);
858 } else { 854 } else {
859 i = tty_insert_flip_string(&port->port, 855 i = tty_insert_flip_string(&port->port,
860 (char *)buf, RECEIVE_BUF_MAX); 856 (char *)buf, RECEIVE_BUF_MAX);
861 size -= i; 857 size -= i;
862 offset += i; 858 offset += i;
863 } 859 }
864 } 860 }
865 861
866 set_bit(index, &dc->flip); 862 set_bit(index, &dc->flip);
867 ret = 1; 863 ret = 1;
868 put: 864 put:
869 tty_kref_put(tty); 865 tty_kref_put(tty);
870 return ret; 866 return ret;
871 } 867 }
872 868
873 /* Debug for interrupts */ 869 /* Debug for interrupts */
874 #ifdef DEBUG 870 #ifdef DEBUG
875 static char *interrupt2str(u16 interrupt) 871 static char *interrupt2str(u16 interrupt)
876 { 872 {
877 static char buf[TMP_BUF_MAX]; 873 static char buf[TMP_BUF_MAX];
878 char *p = buf; 874 char *p = buf;
879 875
880 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL; 876 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
881 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 877 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
882 "MDM_DL2 ") : NULL; 878 "MDM_DL2 ") : NULL;
883 879
884 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 880 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
885 "MDM_UL1 ") : NULL; 881 "MDM_UL1 ") : NULL;
886 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 882 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
887 "MDM_UL2 ") : NULL; 883 "MDM_UL2 ") : NULL;
888 884
889 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 885 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
890 "DIAG_DL1 ") : NULL; 886 "DIAG_DL1 ") : NULL;
891 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 887 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
892 "DIAG_DL2 ") : NULL; 888 "DIAG_DL2 ") : NULL;
893 889
894 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 890 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
895 "DIAG_UL ") : NULL; 891 "DIAG_UL ") : NULL;
896 892
897 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 893 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
898 "APP1_DL ") : NULL; 894 "APP1_DL ") : NULL;
899 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 895 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
900 "APP2_DL ") : NULL; 896 "APP2_DL ") : NULL;
901 897
902 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 898 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
903 "APP1_UL ") : NULL; 899 "APP1_UL ") : NULL;
904 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 900 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
905 "APP2_UL ") : NULL; 901 "APP2_UL ") : NULL;
906 902
907 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 903 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
908 "CTRL_DL ") : NULL; 904 "CTRL_DL ") : NULL;
909 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 905 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
910 "CTRL_UL ") : NULL; 906 "CTRL_UL ") : NULL;
911 907
912 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf), 908 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
913 "RESET ") : NULL; 909 "RESET ") : NULL;
914 910
915 return buf; 911 return buf;
916 } 912 }
917 #endif 913 #endif
918 914
919 /* 915 /*
920 * Receive flow control 916 * Receive flow control
921 * Return 1 - If ok, else 0 917 * Return 1 - If ok, else 0
922 */ 918 */
923 static int receive_flow_control(struct nozomi *dc) 919 static int receive_flow_control(struct nozomi *dc)
924 { 920 {
925 enum port_type port = PORT_MDM; 921 enum port_type port = PORT_MDM;
926 struct ctrl_dl ctrl_dl; 922 struct ctrl_dl ctrl_dl;
927 struct ctrl_dl old_ctrl; 923 struct ctrl_dl old_ctrl;
928 u16 enable_ier = 0; 924 u16 enable_ier = 0;
929 925
930 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2); 926 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
931 927
932 switch (ctrl_dl.port) { 928 switch (ctrl_dl.port) {
933 case CTRL_CMD: 929 case CTRL_CMD:
934 DBG1("The Base Band sends this value as a response to a " 930 DBG1("The Base Band sends this value as a response to a "
935 "request for IMSI detach sent over the control " 931 "request for IMSI detach sent over the control "
936 "channel uplink (see section 7.6.1)."); 932 "channel uplink (see section 7.6.1).");
937 break; 933 break;
938 case CTRL_MDM: 934 case CTRL_MDM:
939 port = PORT_MDM; 935 port = PORT_MDM;
940 enable_ier = MDM_DL; 936 enable_ier = MDM_DL;
941 break; 937 break;
942 case CTRL_DIAG: 938 case CTRL_DIAG:
943 port = PORT_DIAG; 939 port = PORT_DIAG;
944 enable_ier = DIAG_DL; 940 enable_ier = DIAG_DL;
945 break; 941 break;
946 case CTRL_APP1: 942 case CTRL_APP1:
947 port = PORT_APP1; 943 port = PORT_APP1;
948 enable_ier = APP1_DL; 944 enable_ier = APP1_DL;
949 break; 945 break;
950 case CTRL_APP2: 946 case CTRL_APP2:
951 port = PORT_APP2; 947 port = PORT_APP2;
952 enable_ier = APP2_DL; 948 enable_ier = APP2_DL;
953 if (dc->state == NOZOMI_STATE_ALLOCATED) { 949 if (dc->state == NOZOMI_STATE_ALLOCATED) {
954 /* 950 /*
955 * After card initialization the flow control 951 * After card initialization the flow control
956 * received for APP2 is always the last 952 * received for APP2 is always the last
957 */ 953 */
958 dc->state = NOZOMI_STATE_READY; 954 dc->state = NOZOMI_STATE_READY;
959 dev_info(&dc->pdev->dev, "Device READY!\n"); 955 dev_info(&dc->pdev->dev, "Device READY!\n");
960 } 956 }
961 break; 957 break;
962 default: 958 default:
963 dev_err(&dc->pdev->dev, 959 dev_err(&dc->pdev->dev,
964 "ERROR: flow control received for non-existing port\n"); 960 "ERROR: flow control received for non-existing port\n");
965 return 0; 961 return 0;
966 }; 962 };
967 963
968 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl), 964 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
969 *((u16 *)&ctrl_dl)); 965 *((u16 *)&ctrl_dl));
970 966
971 old_ctrl = dc->port[port].ctrl_dl; 967 old_ctrl = dc->port[port].ctrl_dl;
972 dc->port[port].ctrl_dl = ctrl_dl; 968 dc->port[port].ctrl_dl = ctrl_dl;
973 969
974 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) { 970 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
975 DBG1("Disable interrupt (0x%04X) on port: %d", 971 DBG1("Disable interrupt (0x%04X) on port: %d",
976 enable_ier, port); 972 enable_ier, port);
977 disable_transmit_ul(port, dc); 973 disable_transmit_ul(port, dc);
978 974
979 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) { 975 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
980 976
981 if (kfifo_len(&dc->port[port].fifo_ul)) { 977 if (kfifo_len(&dc->port[port].fifo_ul)) {
982 DBG1("Enable interrupt (0x%04X) on port: %d", 978 DBG1("Enable interrupt (0x%04X) on port: %d",
983 enable_ier, port); 979 enable_ier, port);
984 DBG1("Data in buffer [%d], enable transmit! ", 980 DBG1("Data in buffer [%d], enable transmit! ",
985 kfifo_len(&dc->port[port].fifo_ul)); 981 kfifo_len(&dc->port[port].fifo_ul));
986 enable_transmit_ul(port, dc); 982 enable_transmit_ul(port, dc);
987 } else { 983 } else {
988 DBG1("No data in buffer..."); 984 DBG1("No data in buffer...");
989 } 985 }
990 } 986 }
991 987
992 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) { 988 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
993 DBG1(" No change in mctrl"); 989 DBG1(" No change in mctrl");
994 return 1; 990 return 1;
995 } 991 }
996 /* Update statistics */ 992 /* Update statistics */
997 if (old_ctrl.CTS != ctrl_dl.CTS) 993 if (old_ctrl.CTS != ctrl_dl.CTS)
998 dc->port[port].tty_icount.cts++; 994 dc->port[port].tty_icount.cts++;
999 if (old_ctrl.DSR != ctrl_dl.DSR) 995 if (old_ctrl.DSR != ctrl_dl.DSR)
1000 dc->port[port].tty_icount.dsr++; 996 dc->port[port].tty_icount.dsr++;
1001 if (old_ctrl.RI != ctrl_dl.RI) 997 if (old_ctrl.RI != ctrl_dl.RI)
1002 dc->port[port].tty_icount.rng++; 998 dc->port[port].tty_icount.rng++;
1003 if (old_ctrl.DCD != ctrl_dl.DCD) 999 if (old_ctrl.DCD != ctrl_dl.DCD)
1004 dc->port[port].tty_icount.dcd++; 1000 dc->port[port].tty_icount.dcd++;
1005 1001
1006 wake_up_interruptible(&dc->port[port].tty_wait); 1002 wake_up_interruptible(&dc->port[port].tty_wait);
1007 1003
1008 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)", 1004 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1009 port, 1005 port,
1010 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts, 1006 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1011 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr); 1007 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1012 1008
1013 return 1; 1009 return 1;
1014 } 1010 }
1015 1011
1016 static enum ctrl_port_type port2ctrl(enum port_type port, 1012 static enum ctrl_port_type port2ctrl(enum port_type port,
1017 const struct nozomi *dc) 1013 const struct nozomi *dc)
1018 { 1014 {
1019 switch (port) { 1015 switch (port) {
1020 case PORT_MDM: 1016 case PORT_MDM:
1021 return CTRL_MDM; 1017 return CTRL_MDM;
1022 case PORT_DIAG: 1018 case PORT_DIAG:
1023 return CTRL_DIAG; 1019 return CTRL_DIAG;
1024 case PORT_APP1: 1020 case PORT_APP1:
1025 return CTRL_APP1; 1021 return CTRL_APP1;
1026 case PORT_APP2: 1022 case PORT_APP2:
1027 return CTRL_APP2; 1023 return CTRL_APP2;
1028 default: 1024 default:
1029 dev_err(&dc->pdev->dev, 1025 dev_err(&dc->pdev->dev,
1030 "ERROR: send flow control " \ 1026 "ERROR: send flow control " \
1031 "received for non-existing port\n"); 1027 "received for non-existing port\n");
1032 }; 1028 };
1033 return CTRL_ERROR; 1029 return CTRL_ERROR;
1034 } 1030 }
1035 1031
1036 /* 1032 /*
1037 * Send flow control, can only update one channel at a time 1033 * Send flow control, can only update one channel at a time
1038 * Return 0 - If we have updated all flow control 1034 * Return 0 - If we have updated all flow control
1039 * Return 1 - If we need to update more flow control, ack current enable more 1035 * Return 1 - If we need to update more flow control, ack current enable more
1040 */ 1036 */
1041 static int send_flow_control(struct nozomi *dc) 1037 static int send_flow_control(struct nozomi *dc)
1042 { 1038 {
1043 u32 i, more_flow_control_to_be_updated = 0; 1039 u32 i, more_flow_control_to_be_updated = 0;
1044 u16 *ctrl; 1040 u16 *ctrl;
1045 1041
1046 for (i = PORT_MDM; i < MAX_PORT; i++) { 1042 for (i = PORT_MDM; i < MAX_PORT; i++) {
1047 if (dc->port[i].update_flow_control) { 1043 if (dc->port[i].update_flow_control) {
1048 if (more_flow_control_to_be_updated) { 1044 if (more_flow_control_to_be_updated) {
1049 /* We have more flow control to be updated */ 1045 /* We have more flow control to be updated */
1050 return 1; 1046 return 1;
1051 } 1047 }
1052 dc->port[i].ctrl_ul.port = port2ctrl(i, dc); 1048 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1053 ctrl = (u16 *)&dc->port[i].ctrl_ul; 1049 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1054 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \ 1050 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1055 (u32 *) ctrl, 2); 1051 (u32 *) ctrl, 2);
1056 dc->port[i].update_flow_control = 0; 1052 dc->port[i].update_flow_control = 0;
1057 more_flow_control_to_be_updated = 1; 1053 more_flow_control_to_be_updated = 1;
1058 } 1054 }
1059 } 1055 }
1060 return 0; 1056 return 0;
1061 } 1057 }
1062 1058
1063 /* 1059 /*
1064 * Handle downlink data, ports that are handled are modem and diagnostics 1060 * Handle downlink data, ports that are handled are modem and diagnostics
1065 * Return 1 - ok 1061 * Return 1 - ok
1066 * Return 0 - toggle fields are out of sync 1062 * Return 0 - toggle fields are out of sync
1067 */ 1063 */
1068 static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle, 1064 static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1069 u16 read_iir, u16 mask1, u16 mask2) 1065 u16 read_iir, u16 mask1, u16 mask2)
1070 { 1066 {
1071 if (*toggle == 0 && read_iir & mask1) { 1067 if (*toggle == 0 && read_iir & mask1) {
1072 if (receive_data(port, dc)) { 1068 if (receive_data(port, dc)) {
1073 writew(mask1, dc->reg_fcr); 1069 writew(mask1, dc->reg_fcr);
1074 *toggle = !(*toggle); 1070 *toggle = !(*toggle);
1075 } 1071 }
1076 1072
1077 if (read_iir & mask2) { 1073 if (read_iir & mask2) {
1078 if (receive_data(port, dc)) { 1074 if (receive_data(port, dc)) {
1079 writew(mask2, dc->reg_fcr); 1075 writew(mask2, dc->reg_fcr);
1080 *toggle = !(*toggle); 1076 *toggle = !(*toggle);
1081 } 1077 }
1082 } 1078 }
1083 } else if (*toggle == 1 && read_iir & mask2) { 1079 } else if (*toggle == 1 && read_iir & mask2) {
1084 if (receive_data(port, dc)) { 1080 if (receive_data(port, dc)) {
1085 writew(mask2, dc->reg_fcr); 1081 writew(mask2, dc->reg_fcr);
1086 *toggle = !(*toggle); 1082 *toggle = !(*toggle);
1087 } 1083 }
1088 1084
1089 if (read_iir & mask1) { 1085 if (read_iir & mask1) {
1090 if (receive_data(port, dc)) { 1086 if (receive_data(port, dc)) {
1091 writew(mask1, dc->reg_fcr); 1087 writew(mask1, dc->reg_fcr);
1092 *toggle = !(*toggle); 1088 *toggle = !(*toggle);
1093 } 1089 }
1094 } 1090 }
1095 } else { 1091 } else {
1096 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n", 1092 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1097 *toggle); 1093 *toggle);
1098 return 0; 1094 return 0;
1099 } 1095 }
1100 return 1; 1096 return 1;
1101 } 1097 }
1102 1098
1103 /* 1099 /*
1104 * Handle uplink data, this is currently for the modem port 1100 * Handle uplink data, this is currently for the modem port
1105 * Return 1 - ok 1101 * Return 1 - ok
1106 * Return 0 - toggle field are out of sync 1102 * Return 0 - toggle field are out of sync
1107 */ 1103 */
1108 static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir) 1104 static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1109 { 1105 {
1110 u8 *toggle = &(dc->port[port].toggle_ul); 1106 u8 *toggle = &(dc->port[port].toggle_ul);
1111 1107
1112 if (*toggle == 0 && read_iir & MDM_UL1) { 1108 if (*toggle == 0 && read_iir & MDM_UL1) {
1113 dc->last_ier &= ~MDM_UL; 1109 dc->last_ier &= ~MDM_UL;
1114 writew(dc->last_ier, dc->reg_ier); 1110 writew(dc->last_ier, dc->reg_ier);
1115 if (send_data(port, dc)) { 1111 if (send_data(port, dc)) {
1116 writew(MDM_UL1, dc->reg_fcr); 1112 writew(MDM_UL1, dc->reg_fcr);
1117 dc->last_ier = dc->last_ier | MDM_UL; 1113 dc->last_ier = dc->last_ier | MDM_UL;
1118 writew(dc->last_ier, dc->reg_ier); 1114 writew(dc->last_ier, dc->reg_ier);
1119 *toggle = !*toggle; 1115 *toggle = !*toggle;
1120 } 1116 }
1121 1117
1122 if (read_iir & MDM_UL2) { 1118 if (read_iir & MDM_UL2) {
1123 dc->last_ier &= ~MDM_UL; 1119 dc->last_ier &= ~MDM_UL;
1124 writew(dc->last_ier, dc->reg_ier); 1120 writew(dc->last_ier, dc->reg_ier);
1125 if (send_data(port, dc)) { 1121 if (send_data(port, dc)) {
1126 writew(MDM_UL2, dc->reg_fcr); 1122 writew(MDM_UL2, dc->reg_fcr);
1127 dc->last_ier = dc->last_ier | MDM_UL; 1123 dc->last_ier = dc->last_ier | MDM_UL;
1128 writew(dc->last_ier, dc->reg_ier); 1124 writew(dc->last_ier, dc->reg_ier);
1129 *toggle = !*toggle; 1125 *toggle = !*toggle;
1130 } 1126 }
1131 } 1127 }
1132 1128
1133 } else if (*toggle == 1 && read_iir & MDM_UL2) { 1129 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1134 dc->last_ier &= ~MDM_UL; 1130 dc->last_ier &= ~MDM_UL;
1135 writew(dc->last_ier, dc->reg_ier); 1131 writew(dc->last_ier, dc->reg_ier);
1136 if (send_data(port, dc)) { 1132 if (send_data(port, dc)) {
1137 writew(MDM_UL2, dc->reg_fcr); 1133 writew(MDM_UL2, dc->reg_fcr);
1138 dc->last_ier = dc->last_ier | MDM_UL; 1134 dc->last_ier = dc->last_ier | MDM_UL;
1139 writew(dc->last_ier, dc->reg_ier); 1135 writew(dc->last_ier, dc->reg_ier);
1140 *toggle = !*toggle; 1136 *toggle = !*toggle;
1141 } 1137 }
1142 1138
1143 if (read_iir & MDM_UL1) { 1139 if (read_iir & MDM_UL1) {
1144 dc->last_ier &= ~MDM_UL; 1140 dc->last_ier &= ~MDM_UL;
1145 writew(dc->last_ier, dc->reg_ier); 1141 writew(dc->last_ier, dc->reg_ier);
1146 if (send_data(port, dc)) { 1142 if (send_data(port, dc)) {
1147 writew(MDM_UL1, dc->reg_fcr); 1143 writew(MDM_UL1, dc->reg_fcr);
1148 dc->last_ier = dc->last_ier | MDM_UL; 1144 dc->last_ier = dc->last_ier | MDM_UL;
1149 writew(dc->last_ier, dc->reg_ier); 1145 writew(dc->last_ier, dc->reg_ier);
1150 *toggle = !*toggle; 1146 *toggle = !*toggle;
1151 } 1147 }
1152 } 1148 }
1153 } else { 1149 } else {
1154 writew(read_iir & MDM_UL, dc->reg_fcr); 1150 writew(read_iir & MDM_UL, dc->reg_fcr);
1155 dev_err(&dc->pdev->dev, "port out of sync!\n"); 1151 dev_err(&dc->pdev->dev, "port out of sync!\n");
1156 return 0; 1152 return 0;
1157 } 1153 }
1158 return 1; 1154 return 1;
1159 } 1155 }
1160 1156
1161 static irqreturn_t interrupt_handler(int irq, void *dev_id) 1157 static irqreturn_t interrupt_handler(int irq, void *dev_id)
1162 { 1158 {
1163 struct nozomi *dc = dev_id; 1159 struct nozomi *dc = dev_id;
1164 unsigned int a; 1160 unsigned int a;
1165 u16 read_iir; 1161 u16 read_iir;
1166 1162
1167 if (!dc) 1163 if (!dc)
1168 return IRQ_NONE; 1164 return IRQ_NONE;
1169 1165
1170 spin_lock(&dc->spin_mutex); 1166 spin_lock(&dc->spin_mutex);
1171 read_iir = readw(dc->reg_iir); 1167 read_iir = readw(dc->reg_iir);
1172 1168
1173 /* Card removed */ 1169 /* Card removed */
1174 if (read_iir == (u16)-1) 1170 if (read_iir == (u16)-1)
1175 goto none; 1171 goto none;
1176 /* 1172 /*
1177 * Just handle interrupt enabled in IER 1173 * Just handle interrupt enabled in IER
1178 * (by masking with dc->last_ier) 1174 * (by masking with dc->last_ier)
1179 */ 1175 */
1180 read_iir &= dc->last_ier; 1176 read_iir &= dc->last_ier;
1181 1177
1182 if (read_iir == 0) 1178 if (read_iir == 0)
1183 goto none; 1179 goto none;
1184 1180
1185 1181
1186 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir, 1182 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1187 dc->last_ier); 1183 dc->last_ier);
1188 1184
1189 if (read_iir & RESET) { 1185 if (read_iir & RESET) {
1190 if (unlikely(!nozomi_read_config_table(dc))) { 1186 if (unlikely(!nozomi_read_config_table(dc))) {
1191 dc->last_ier = 0x0; 1187 dc->last_ier = 0x0;
1192 writew(dc->last_ier, dc->reg_ier); 1188 writew(dc->last_ier, dc->reg_ier);
1193 dev_err(&dc->pdev->dev, "Could not read status from " 1189 dev_err(&dc->pdev->dev, "Could not read status from "
1194 "card, we should disable interface\n"); 1190 "card, we should disable interface\n");
1195 } else { 1191 } else {
1196 writew(RESET, dc->reg_fcr); 1192 writew(RESET, dc->reg_fcr);
1197 } 1193 }
1198 /* No more useful info if this was the reset interrupt. */ 1194 /* No more useful info if this was the reset interrupt. */
1199 goto exit_handler; 1195 goto exit_handler;
1200 } 1196 }
1201 if (read_iir & CTRL_UL) { 1197 if (read_iir & CTRL_UL) {
1202 DBG1("CTRL_UL"); 1198 DBG1("CTRL_UL");
1203 dc->last_ier &= ~CTRL_UL; 1199 dc->last_ier &= ~CTRL_UL;
1204 writew(dc->last_ier, dc->reg_ier); 1200 writew(dc->last_ier, dc->reg_ier);
1205 if (send_flow_control(dc)) { 1201 if (send_flow_control(dc)) {
1206 writew(CTRL_UL, dc->reg_fcr); 1202 writew(CTRL_UL, dc->reg_fcr);
1207 dc->last_ier = dc->last_ier | CTRL_UL; 1203 dc->last_ier = dc->last_ier | CTRL_UL;
1208 writew(dc->last_ier, dc->reg_ier); 1204 writew(dc->last_ier, dc->reg_ier);
1209 } 1205 }
1210 } 1206 }
1211 if (read_iir & CTRL_DL) { 1207 if (read_iir & CTRL_DL) {
1212 receive_flow_control(dc); 1208 receive_flow_control(dc);
1213 writew(CTRL_DL, dc->reg_fcr); 1209 writew(CTRL_DL, dc->reg_fcr);
1214 } 1210 }
1215 if (read_iir & MDM_DL) { 1211 if (read_iir & MDM_DL) {
1216 if (!handle_data_dl(dc, PORT_MDM, 1212 if (!handle_data_dl(dc, PORT_MDM,
1217 &(dc->port[PORT_MDM].toggle_dl), read_iir, 1213 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1218 MDM_DL1, MDM_DL2)) { 1214 MDM_DL1, MDM_DL2)) {
1219 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n"); 1215 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1220 goto exit_handler; 1216 goto exit_handler;
1221 } 1217 }
1222 } 1218 }
1223 if (read_iir & MDM_UL) { 1219 if (read_iir & MDM_UL) {
1224 if (!handle_data_ul(dc, PORT_MDM, read_iir)) { 1220 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1225 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n"); 1221 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1226 goto exit_handler; 1222 goto exit_handler;
1227 } 1223 }
1228 } 1224 }
1229 if (read_iir & DIAG_DL) { 1225 if (read_iir & DIAG_DL) {
1230 if (!handle_data_dl(dc, PORT_DIAG, 1226 if (!handle_data_dl(dc, PORT_DIAG,
1231 &(dc->port[PORT_DIAG].toggle_dl), read_iir, 1227 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1232 DIAG_DL1, DIAG_DL2)) { 1228 DIAG_DL1, DIAG_DL2)) {
1233 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n"); 1229 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1234 goto exit_handler; 1230 goto exit_handler;
1235 } 1231 }
1236 } 1232 }
1237 if (read_iir & DIAG_UL) { 1233 if (read_iir & DIAG_UL) {
1238 dc->last_ier &= ~DIAG_UL; 1234 dc->last_ier &= ~DIAG_UL;
1239 writew(dc->last_ier, dc->reg_ier); 1235 writew(dc->last_ier, dc->reg_ier);
1240 if (send_data(PORT_DIAG, dc)) { 1236 if (send_data(PORT_DIAG, dc)) {
1241 writew(DIAG_UL, dc->reg_fcr); 1237 writew(DIAG_UL, dc->reg_fcr);
1242 dc->last_ier = dc->last_ier | DIAG_UL; 1238 dc->last_ier = dc->last_ier | DIAG_UL;
1243 writew(dc->last_ier, dc->reg_ier); 1239 writew(dc->last_ier, dc->reg_ier);
1244 } 1240 }
1245 } 1241 }
1246 if (read_iir & APP1_DL) { 1242 if (read_iir & APP1_DL) {
1247 if (receive_data(PORT_APP1, dc)) 1243 if (receive_data(PORT_APP1, dc))
1248 writew(APP1_DL, dc->reg_fcr); 1244 writew(APP1_DL, dc->reg_fcr);
1249 } 1245 }
1250 if (read_iir & APP1_UL) { 1246 if (read_iir & APP1_UL) {
1251 dc->last_ier &= ~APP1_UL; 1247 dc->last_ier &= ~APP1_UL;
1252 writew(dc->last_ier, dc->reg_ier); 1248 writew(dc->last_ier, dc->reg_ier);
1253 if (send_data(PORT_APP1, dc)) { 1249 if (send_data(PORT_APP1, dc)) {
1254 writew(APP1_UL, dc->reg_fcr); 1250 writew(APP1_UL, dc->reg_fcr);
1255 dc->last_ier = dc->last_ier | APP1_UL; 1251 dc->last_ier = dc->last_ier | APP1_UL;
1256 writew(dc->last_ier, dc->reg_ier); 1252 writew(dc->last_ier, dc->reg_ier);
1257 } 1253 }
1258 } 1254 }
1259 if (read_iir & APP2_DL) { 1255 if (read_iir & APP2_DL) {
1260 if (receive_data(PORT_APP2, dc)) 1256 if (receive_data(PORT_APP2, dc))
1261 writew(APP2_DL, dc->reg_fcr); 1257 writew(APP2_DL, dc->reg_fcr);
1262 } 1258 }
1263 if (read_iir & APP2_UL) { 1259 if (read_iir & APP2_UL) {
1264 dc->last_ier &= ~APP2_UL; 1260 dc->last_ier &= ~APP2_UL;
1265 writew(dc->last_ier, dc->reg_ier); 1261 writew(dc->last_ier, dc->reg_ier);
1266 if (send_data(PORT_APP2, dc)) { 1262 if (send_data(PORT_APP2, dc)) {
1267 writew(APP2_UL, dc->reg_fcr); 1263 writew(APP2_UL, dc->reg_fcr);
1268 dc->last_ier = dc->last_ier | APP2_UL; 1264 dc->last_ier = dc->last_ier | APP2_UL;
1269 writew(dc->last_ier, dc->reg_ier); 1265 writew(dc->last_ier, dc->reg_ier);
1270 } 1266 }
1271 } 1267 }
1272 1268
1273 exit_handler: 1269 exit_handler:
1274 spin_unlock(&dc->spin_mutex); 1270 spin_unlock(&dc->spin_mutex);
1275 1271
1276 for (a = 0; a < NOZOMI_MAX_PORTS; a++) 1272 for (a = 0; a < NOZOMI_MAX_PORTS; a++)
1277 if (test_and_clear_bit(a, &dc->flip)) 1273 if (test_and_clear_bit(a, &dc->flip))
1278 tty_flip_buffer_push(&dc->port[a].port); 1274 tty_flip_buffer_push(&dc->port[a].port);
1279 1275
1280 return IRQ_HANDLED; 1276 return IRQ_HANDLED;
1281 none: 1277 none:
1282 spin_unlock(&dc->spin_mutex); 1278 spin_unlock(&dc->spin_mutex);
1283 return IRQ_NONE; 1279 return IRQ_NONE;
1284 } 1280 }
1285 1281
1286 static void nozomi_get_card_type(struct nozomi *dc) 1282 static void nozomi_get_card_type(struct nozomi *dc)
1287 { 1283 {
1288 int i; 1284 int i;
1289 u32 size = 0; 1285 u32 size = 0;
1290 1286
1291 for (i = 0; i < 6; i++) 1287 for (i = 0; i < 6; i++)
1292 size += pci_resource_len(dc->pdev, i); 1288 size += pci_resource_len(dc->pdev, i);
1293 1289
1294 /* Assume card type F32_8 if no match */ 1290 /* Assume card type F32_8 if no match */
1295 dc->card_type = size == 2048 ? F32_2 : F32_8; 1291 dc->card_type = size == 2048 ? F32_2 : F32_8;
1296 1292
1297 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type); 1293 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1298 } 1294 }
1299 1295
1300 static void nozomi_setup_private_data(struct nozomi *dc) 1296 static void nozomi_setup_private_data(struct nozomi *dc)
1301 { 1297 {
1302 void __iomem *offset = dc->base_addr + dc->card_type / 2; 1298 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1303 unsigned int i; 1299 unsigned int i;
1304 1300
1305 dc->reg_fcr = (void __iomem *)(offset + R_FCR); 1301 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1306 dc->reg_iir = (void __iomem *)(offset + R_IIR); 1302 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1307 dc->reg_ier = (void __iomem *)(offset + R_IER); 1303 dc->reg_ier = (void __iomem *)(offset + R_IER);
1308 dc->last_ier = 0; 1304 dc->last_ier = 0;
1309 dc->flip = 0; 1305 dc->flip = 0;
1310 1306
1311 dc->port[PORT_MDM].token_dl = MDM_DL; 1307 dc->port[PORT_MDM].token_dl = MDM_DL;
1312 dc->port[PORT_DIAG].token_dl = DIAG_DL; 1308 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1313 dc->port[PORT_APP1].token_dl = APP1_DL; 1309 dc->port[PORT_APP1].token_dl = APP1_DL;
1314 dc->port[PORT_APP2].token_dl = APP2_DL; 1310 dc->port[PORT_APP2].token_dl = APP2_DL;
1315 1311
1316 for (i = 0; i < MAX_PORT; i++) 1312 for (i = 0; i < MAX_PORT; i++)
1317 init_waitqueue_head(&dc->port[i].tty_wait); 1313 init_waitqueue_head(&dc->port[i].tty_wait);
1318 } 1314 }
1319 1315
1320 static ssize_t card_type_show(struct device *dev, struct device_attribute *attr, 1316 static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1321 char *buf) 1317 char *buf)
1322 { 1318 {
1323 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev)); 1319 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1324 1320
1325 return sprintf(buf, "%d\n", dc->card_type); 1321 return sprintf(buf, "%d\n", dc->card_type);
1326 } 1322 }
1327 static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL); 1323 static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
1328 1324
1329 static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr, 1325 static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1330 char *buf) 1326 char *buf)
1331 { 1327 {
1332 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev)); 1328 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1333 1329
1334 return sprintf(buf, "%u\n", dc->open_ttys); 1330 return sprintf(buf, "%u\n", dc->open_ttys);
1335 } 1331 }
1336 static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL); 1332 static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
1337 1333
1338 static void make_sysfs_files(struct nozomi *dc) 1334 static void make_sysfs_files(struct nozomi *dc)
1339 { 1335 {
1340 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type)) 1336 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1341 dev_err(&dc->pdev->dev, 1337 dev_err(&dc->pdev->dev,
1342 "Could not create sysfs file for card_type\n"); 1338 "Could not create sysfs file for card_type\n");
1343 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys)) 1339 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1344 dev_err(&dc->pdev->dev, 1340 dev_err(&dc->pdev->dev,
1345 "Could not create sysfs file for open_ttys\n"); 1341 "Could not create sysfs file for open_ttys\n");
1346 } 1342 }
1347 1343
1348 static void remove_sysfs_files(struct nozomi *dc) 1344 static void remove_sysfs_files(struct nozomi *dc)
1349 { 1345 {
1350 device_remove_file(&dc->pdev->dev, &dev_attr_card_type); 1346 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1351 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys); 1347 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1352 } 1348 }
1353 1349
1354 /* Allocate memory for one device */ 1350 /* Allocate memory for one device */
1355 static int nozomi_card_init(struct pci_dev *pdev, 1351 static int nozomi_card_init(struct pci_dev *pdev,
1356 const struct pci_device_id *ent) 1352 const struct pci_device_id *ent)
1357 { 1353 {
1358 resource_size_t start; 1354 resource_size_t start;
1359 int ret; 1355 int ret;
1360 struct nozomi *dc = NULL; 1356 struct nozomi *dc = NULL;
1361 int ndev_idx; 1357 int ndev_idx;
1362 int i; 1358 int i;
1363 1359
1364 dev_dbg(&pdev->dev, "Init, new card found\n"); 1360 dev_dbg(&pdev->dev, "Init, new card found\n");
1365 1361
1366 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++) 1362 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1367 if (!ndevs[ndev_idx]) 1363 if (!ndevs[ndev_idx])
1368 break; 1364 break;
1369 1365
1370 if (ndev_idx >= ARRAY_SIZE(ndevs)) { 1366 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1371 dev_err(&pdev->dev, "no free tty range for this card left\n"); 1367 dev_err(&pdev->dev, "no free tty range for this card left\n");
1372 ret = -EIO; 1368 ret = -EIO;
1373 goto err; 1369 goto err;
1374 } 1370 }
1375 1371
1376 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL); 1372 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1377 if (unlikely(!dc)) { 1373 if (unlikely(!dc)) {
1378 dev_err(&pdev->dev, "Could not allocate memory\n"); 1374 dev_err(&pdev->dev, "Could not allocate memory\n");
1379 ret = -ENOMEM; 1375 ret = -ENOMEM;
1380 goto err_free; 1376 goto err_free;
1381 } 1377 }
1382 1378
1383 dc->pdev = pdev; 1379 dc->pdev = pdev;
1384 1380
1385 ret = pci_enable_device(dc->pdev); 1381 ret = pci_enable_device(dc->pdev);
1386 if (ret) { 1382 if (ret) {
1387 dev_err(&pdev->dev, "Failed to enable PCI Device\n"); 1383 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1388 goto err_free; 1384 goto err_free;
1389 } 1385 }
1390 1386
1391 ret = pci_request_regions(dc->pdev, NOZOMI_NAME); 1387 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1392 if (ret) { 1388 if (ret) {
1393 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n", 1389 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1394 (int) /* nozomi_private.io_addr */ 0); 1390 (int) /* nozomi_private.io_addr */ 0);
1395 goto err_disable_device; 1391 goto err_disable_device;
1396 } 1392 }
1397 1393
1398 start = pci_resource_start(dc->pdev, 0); 1394 start = pci_resource_start(dc->pdev, 0);
1399 if (start == 0) { 1395 if (start == 0) {
1400 dev_err(&pdev->dev, "No I/O address for card detected\n"); 1396 dev_err(&pdev->dev, "No I/O address for card detected\n");
1401 ret = -ENODEV; 1397 ret = -ENODEV;
1402 goto err_rel_regs; 1398 goto err_rel_regs;
1403 } 1399 }
1404 1400
1405 /* Find out what card type it is */ 1401 /* Find out what card type it is */
1406 nozomi_get_card_type(dc); 1402 nozomi_get_card_type(dc);
1407 1403
1408 dc->base_addr = ioremap_nocache(start, dc->card_type); 1404 dc->base_addr = ioremap_nocache(start, dc->card_type);
1409 if (!dc->base_addr) { 1405 if (!dc->base_addr) {
1410 dev_err(&pdev->dev, "Unable to map card MMIO\n"); 1406 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1411 ret = -ENODEV; 1407 ret = -ENODEV;
1412 goto err_rel_regs; 1408 goto err_rel_regs;
1413 } 1409 }
1414 1410
1415 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL); 1411 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1416 if (!dc->send_buf) { 1412 if (!dc->send_buf) {
1417 dev_err(&pdev->dev, "Could not allocate send buffer?\n"); 1413 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1418 ret = -ENOMEM; 1414 ret = -ENOMEM;
1419 goto err_free_sbuf; 1415 goto err_free_sbuf;
1420 } 1416 }
1421 1417
1422 for (i = PORT_MDM; i < MAX_PORT; i++) { 1418 for (i = PORT_MDM; i < MAX_PORT; i++) {
1423 if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL, 1419 if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL,
1424 GFP_KERNEL)) { 1420 GFP_KERNEL)) {
1425 dev_err(&pdev->dev, 1421 dev_err(&pdev->dev,
1426 "Could not allocate kfifo buffer\n"); 1422 "Could not allocate kfifo buffer\n");
1427 ret = -ENOMEM; 1423 ret = -ENOMEM;
1428 goto err_free_kfifo; 1424 goto err_free_kfifo;
1429 } 1425 }
1430 } 1426 }
1431 1427
1432 spin_lock_init(&dc->spin_mutex); 1428 spin_lock_init(&dc->spin_mutex);
1433 1429
1434 nozomi_setup_private_data(dc); 1430 nozomi_setup_private_data(dc);
1435 1431
1436 /* Disable all interrupts */ 1432 /* Disable all interrupts */
1437 dc->last_ier = 0; 1433 dc->last_ier = 0;
1438 writew(dc->last_ier, dc->reg_ier); 1434 writew(dc->last_ier, dc->reg_ier);
1439 1435
1440 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED, 1436 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1441 NOZOMI_NAME, dc); 1437 NOZOMI_NAME, dc);
1442 if (unlikely(ret)) { 1438 if (unlikely(ret)) {
1443 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq); 1439 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1444 goto err_free_kfifo; 1440 goto err_free_kfifo;
1445 } 1441 }
1446 1442
1447 DBG1("base_addr: %p", dc->base_addr); 1443 DBG1("base_addr: %p", dc->base_addr);
1448 1444
1449 make_sysfs_files(dc); 1445 make_sysfs_files(dc);
1450 1446
1451 dc->index_start = ndev_idx * MAX_PORT; 1447 dc->index_start = ndev_idx * MAX_PORT;
1452 ndevs[ndev_idx] = dc; 1448 ndevs[ndev_idx] = dc;
1453 1449
1454 pci_set_drvdata(pdev, dc); 1450 pci_set_drvdata(pdev, dc);
1455 1451
1456 /* Enable RESET interrupt */ 1452 /* Enable RESET interrupt */
1457 dc->last_ier = RESET; 1453 dc->last_ier = RESET;
1458 iowrite16(dc->last_ier, dc->reg_ier); 1454 iowrite16(dc->last_ier, dc->reg_ier);
1459 1455
1460 dc->state = NOZOMI_STATE_ENABLED; 1456 dc->state = NOZOMI_STATE_ENABLED;
1461 1457
1462 for (i = 0; i < MAX_PORT; i++) { 1458 for (i = 0; i < MAX_PORT; i++) {
1463 struct device *tty_dev; 1459 struct device *tty_dev;
1464 struct port *port = &dc->port[i]; 1460 struct port *port = &dc->port[i];
1465 port->dc = dc; 1461 port->dc = dc;
1466 tty_port_init(&port->port); 1462 tty_port_init(&port->port);
1467 port->port.ops = &noz_tty_port_ops; 1463 port->port.ops = &noz_tty_port_ops;
1468 tty_dev = tty_port_register_device(&port->port, ntty_driver, 1464 tty_dev = tty_port_register_device(&port->port, ntty_driver,
1469 dc->index_start + i, &pdev->dev); 1465 dc->index_start + i, &pdev->dev);
1470 1466
1471 if (IS_ERR(tty_dev)) { 1467 if (IS_ERR(tty_dev)) {
1472 ret = PTR_ERR(tty_dev); 1468 ret = PTR_ERR(tty_dev);
1473 dev_err(&pdev->dev, "Could not allocate tty?\n"); 1469 dev_err(&pdev->dev, "Could not allocate tty?\n");
1474 tty_port_destroy(&port->port); 1470 tty_port_destroy(&port->port);
1475 goto err_free_tty; 1471 goto err_free_tty;
1476 } 1472 }
1477 } 1473 }
1478 1474
1479 return 0; 1475 return 0;
1480 1476
1481 err_free_tty: 1477 err_free_tty:
1482 for (i = 0; i < MAX_PORT; ++i) { 1478 for (i = 0; i < MAX_PORT; ++i) {
1483 tty_unregister_device(ntty_driver, dc->index_start + i); 1479 tty_unregister_device(ntty_driver, dc->index_start + i);
1484 tty_port_destroy(&dc->port[i].port); 1480 tty_port_destroy(&dc->port[i].port);
1485 } 1481 }
1486 err_free_kfifo: 1482 err_free_kfifo:
1487 for (i = 0; i < MAX_PORT; i++) 1483 for (i = 0; i < MAX_PORT; i++)
1488 kfifo_free(&dc->port[i].fifo_ul); 1484 kfifo_free(&dc->port[i].fifo_ul);
1489 err_free_sbuf: 1485 err_free_sbuf:
1490 kfree(dc->send_buf); 1486 kfree(dc->send_buf);
1491 iounmap(dc->base_addr); 1487 iounmap(dc->base_addr);
1492 err_rel_regs: 1488 err_rel_regs:
1493 pci_release_regions(pdev); 1489 pci_release_regions(pdev);
1494 err_disable_device: 1490 err_disable_device:
1495 pci_disable_device(pdev); 1491 pci_disable_device(pdev);
1496 err_free: 1492 err_free:
1497 kfree(dc); 1493 kfree(dc);
1498 err: 1494 err:
1499 return ret; 1495 return ret;
1500 } 1496 }
1501 1497
1502 static void tty_exit(struct nozomi *dc) 1498 static void tty_exit(struct nozomi *dc)
1503 { 1499 {
1504 unsigned int i; 1500 unsigned int i;
1505 1501
1506 DBG1(" "); 1502 DBG1(" ");
1507 1503
1508 for (i = 0; i < MAX_PORT; ++i) { 1504 for (i = 0; i < MAX_PORT; ++i) {
1509 struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port); 1505 struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
1510 if (tty && list_empty(&tty->hangup_work.entry)) 1506 if (tty && list_empty(&tty->hangup_work.entry))
1511 tty_hangup(tty); 1507 tty_hangup(tty);
1512 tty_kref_put(tty); 1508 tty_kref_put(tty);
1513 } 1509 }
1514 /* Racy below - surely should wait for scheduled work to be done or 1510 /* Racy below - surely should wait for scheduled work to be done or
1515 complete off a hangup method ? */ 1511 complete off a hangup method ? */
1516 while (dc->open_ttys) 1512 while (dc->open_ttys)
1517 msleep(1); 1513 msleep(1);
1518 for (i = 0; i < MAX_PORT; ++i) { 1514 for (i = 0; i < MAX_PORT; ++i) {
1519 tty_unregister_device(ntty_driver, dc->index_start + i); 1515 tty_unregister_device(ntty_driver, dc->index_start + i);
1520 tty_port_destroy(&dc->port[i].port); 1516 tty_port_destroy(&dc->port[i].port);
1521 } 1517 }
1522 } 1518 }
1523 1519
1524 /* Deallocate memory for one device */ 1520 /* Deallocate memory for one device */
1525 static void nozomi_card_exit(struct pci_dev *pdev) 1521 static void nozomi_card_exit(struct pci_dev *pdev)
1526 { 1522 {
1527 int i; 1523 int i;
1528 struct ctrl_ul ctrl; 1524 struct ctrl_ul ctrl;
1529 struct nozomi *dc = pci_get_drvdata(pdev); 1525 struct nozomi *dc = pci_get_drvdata(pdev);
1530 1526
1531 /* Disable all interrupts */ 1527 /* Disable all interrupts */
1532 dc->last_ier = 0; 1528 dc->last_ier = 0;
1533 writew(dc->last_ier, dc->reg_ier); 1529 writew(dc->last_ier, dc->reg_ier);
1534 1530
1535 tty_exit(dc); 1531 tty_exit(dc);
1536 1532
1537 /* Send 0x0001, command card to resend the reset token. */ 1533 /* Send 0x0001, command card to resend the reset token. */
1538 /* This is to get the reset when the module is reloaded. */ 1534 /* This is to get the reset when the module is reloaded. */
1539 ctrl.port = 0x00; 1535 ctrl.port = 0x00;
1540 ctrl.reserved = 0; 1536 ctrl.reserved = 0;
1541 ctrl.RTS = 0; 1537 ctrl.RTS = 0;
1542 ctrl.DTR = 1; 1538 ctrl.DTR = 1;
1543 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl)); 1539 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1544 1540
1545 /* Setup dc->reg addresses to we can use defines here */ 1541 /* Setup dc->reg addresses to we can use defines here */
1546 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2); 1542 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1547 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */ 1543 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1548 1544
1549 remove_sysfs_files(dc); 1545 remove_sysfs_files(dc);
1550 1546
1551 free_irq(pdev->irq, dc); 1547 free_irq(pdev->irq, dc);
1552 1548
1553 for (i = 0; i < MAX_PORT; i++) 1549 for (i = 0; i < MAX_PORT; i++)
1554 kfifo_free(&dc->port[i].fifo_ul); 1550 kfifo_free(&dc->port[i].fifo_ul);
1555 1551
1556 kfree(dc->send_buf); 1552 kfree(dc->send_buf);
1557 1553
1558 iounmap(dc->base_addr); 1554 iounmap(dc->base_addr);
1559 1555
1560 pci_release_regions(pdev); 1556 pci_release_regions(pdev);
1561 1557
1562 pci_disable_device(pdev); 1558 pci_disable_device(pdev);
1563 1559
1564 ndevs[dc->index_start / MAX_PORT] = NULL; 1560 ndevs[dc->index_start / MAX_PORT] = NULL;
1565 1561
1566 kfree(dc); 1562 kfree(dc);
1567 } 1563 }
1568 1564
1569 static void set_rts(const struct tty_struct *tty, int rts) 1565 static void set_rts(const struct tty_struct *tty, int rts)
1570 { 1566 {
1571 struct port *port = get_port_by_tty(tty); 1567 struct port *port = get_port_by_tty(tty);
1572 1568
1573 port->ctrl_ul.RTS = rts; 1569 port->ctrl_ul.RTS = rts;
1574 port->update_flow_control = 1; 1570 port->update_flow_control = 1;
1575 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty)); 1571 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1576 } 1572 }
1577 1573
1578 static void set_dtr(const struct tty_struct *tty, int dtr) 1574 static void set_dtr(const struct tty_struct *tty, int dtr)
1579 { 1575 {
1580 struct port *port = get_port_by_tty(tty); 1576 struct port *port = get_port_by_tty(tty);
1581 1577
1582 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr); 1578 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1583 1579
1584 port->ctrl_ul.DTR = dtr; 1580 port->ctrl_ul.DTR = dtr;
1585 port->update_flow_control = 1; 1581 port->update_flow_control = 1;
1586 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty)); 1582 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1587 } 1583 }
1588 1584
1589 /* 1585 /*
1590 * ---------------------------------------------------------------------------- 1586 * ----------------------------------------------------------------------------
1591 * TTY code 1587 * TTY code
1592 * ---------------------------------------------------------------------------- 1588 * ----------------------------------------------------------------------------
1593 */ 1589 */
1594 1590
1595 static int ntty_install(struct tty_driver *driver, struct tty_struct *tty) 1591 static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
1596 { 1592 {
1597 struct port *port = get_port_by_tty(tty); 1593 struct port *port = get_port_by_tty(tty);
1598 struct nozomi *dc = get_dc_by_tty(tty); 1594 struct nozomi *dc = get_dc_by_tty(tty);
1599 int ret; 1595 int ret;
1600 if (!port || !dc || dc->state != NOZOMI_STATE_READY) 1596 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
1601 return -ENODEV; 1597 return -ENODEV;
1602 ret = tty_standard_install(driver, tty); 1598 ret = tty_standard_install(driver, tty);
1603 if (ret == 0) 1599 if (ret == 0)
1604 tty->driver_data = port; 1600 tty->driver_data = port;
1605 return ret; 1601 return ret;
1606 } 1602 }
1607 1603
1608 static void ntty_cleanup(struct tty_struct *tty) 1604 static void ntty_cleanup(struct tty_struct *tty)
1609 { 1605 {
1610 tty->driver_data = NULL; 1606 tty->driver_data = NULL;
1611 } 1607 }
1612 1608
1613 static int ntty_activate(struct tty_port *tport, struct tty_struct *tty) 1609 static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
1614 { 1610 {
1615 struct port *port = container_of(tport, struct port, port); 1611 struct port *port = container_of(tport, struct port, port);
1616 struct nozomi *dc = port->dc; 1612 struct nozomi *dc = port->dc;
1617 unsigned long flags; 1613 unsigned long flags;
1618 1614
1619 DBG1("open: %d", port->token_dl); 1615 DBG1("open: %d", port->token_dl);
1620 spin_lock_irqsave(&dc->spin_mutex, flags); 1616 spin_lock_irqsave(&dc->spin_mutex, flags);
1621 dc->last_ier = dc->last_ier | port->token_dl; 1617 dc->last_ier = dc->last_ier | port->token_dl;
1622 writew(dc->last_ier, dc->reg_ier); 1618 writew(dc->last_ier, dc->reg_ier);
1623 dc->open_ttys++; 1619 dc->open_ttys++;
1624 spin_unlock_irqrestore(&dc->spin_mutex, flags); 1620 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1625 printk("noz: activated %d: %p\n", tty->index, tport); 1621 printk("noz: activated %d: %p\n", tty->index, tport);
1626 return 0; 1622 return 0;
1627 } 1623 }
1628 1624
1629 static int ntty_open(struct tty_struct *tty, struct file *filp) 1625 static int ntty_open(struct tty_struct *tty, struct file *filp)
1630 { 1626 {
1631 struct port *port = tty->driver_data; 1627 struct port *port = tty->driver_data;
1632 return tty_port_open(&port->port, tty, filp); 1628 return tty_port_open(&port->port, tty, filp);
1633 } 1629 }
1634 1630
1635 static void ntty_shutdown(struct tty_port *tport) 1631 static void ntty_shutdown(struct tty_port *tport)
1636 { 1632 {
1637 struct port *port = container_of(tport, struct port, port); 1633 struct port *port = container_of(tport, struct port, port);
1638 struct nozomi *dc = port->dc; 1634 struct nozomi *dc = port->dc;
1639 unsigned long flags; 1635 unsigned long flags;
1640 1636
1641 DBG1("close: %d", port->token_dl); 1637 DBG1("close: %d", port->token_dl);
1642 spin_lock_irqsave(&dc->spin_mutex, flags); 1638 spin_lock_irqsave(&dc->spin_mutex, flags);
1643 dc->last_ier &= ~(port->token_dl); 1639 dc->last_ier &= ~(port->token_dl);
1644 writew(dc->last_ier, dc->reg_ier); 1640 writew(dc->last_ier, dc->reg_ier);
1645 dc->open_ttys--; 1641 dc->open_ttys--;
1646 spin_unlock_irqrestore(&dc->spin_mutex, flags); 1642 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1647 printk("noz: shutdown %p\n", tport); 1643 printk("noz: shutdown %p\n", tport);
1648 } 1644 }
1649 1645
1650 static void ntty_close(struct tty_struct *tty, struct file *filp) 1646 static void ntty_close(struct tty_struct *tty, struct file *filp)
1651 { 1647 {
1652 struct port *port = tty->driver_data; 1648 struct port *port = tty->driver_data;
1653 if (port) 1649 if (port)
1654 tty_port_close(&port->port, tty, filp); 1650 tty_port_close(&port->port, tty, filp);
1655 } 1651 }
1656 1652
1657 static void ntty_hangup(struct tty_struct *tty) 1653 static void ntty_hangup(struct tty_struct *tty)
1658 { 1654 {
1659 struct port *port = tty->driver_data; 1655 struct port *port = tty->driver_data;
1660 tty_port_hangup(&port->port); 1656 tty_port_hangup(&port->port);
1661 } 1657 }
1662 1658
1663 /* 1659 /*
1664 * called when the userspace process writes to the tty (/dev/noz*). 1660 * called when the userspace process writes to the tty (/dev/noz*).
1665 * Data is inserted into a fifo, which is then read and transferred to the modem. 1661 * Data is inserted into a fifo, which is then read and transferred to the modem.
1666 */ 1662 */
1667 static int ntty_write(struct tty_struct *tty, const unsigned char *buffer, 1663 static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1668 int count) 1664 int count)
1669 { 1665 {
1670 int rval = -EINVAL; 1666 int rval = -EINVAL;
1671 struct nozomi *dc = get_dc_by_tty(tty); 1667 struct nozomi *dc = get_dc_by_tty(tty);
1672 struct port *port = tty->driver_data; 1668 struct port *port = tty->driver_data;
1673 unsigned long flags; 1669 unsigned long flags;
1674 1670
1675 /* DBG1( "WRITEx: %d, index = %d", count, index); */ 1671 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1676 1672
1677 if (!dc || !port) 1673 if (!dc || !port)
1678 return -ENODEV; 1674 return -ENODEV;
1679 1675
1680 rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count); 1676 rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
1681 1677
1682 spin_lock_irqsave(&dc->spin_mutex, flags); 1678 spin_lock_irqsave(&dc->spin_mutex, flags);
1683 /* CTS is only valid on the modem channel */ 1679 /* CTS is only valid on the modem channel */
1684 if (port == &(dc->port[PORT_MDM])) { 1680 if (port == &(dc->port[PORT_MDM])) {
1685 if (port->ctrl_dl.CTS) { 1681 if (port->ctrl_dl.CTS) {
1686 DBG4("Enable interrupt"); 1682 DBG4("Enable interrupt");
1687 enable_transmit_ul(tty->index % MAX_PORT, dc); 1683 enable_transmit_ul(tty->index % MAX_PORT, dc);
1688 } else { 1684 } else {
1689 dev_err(&dc->pdev->dev, 1685 dev_err(&dc->pdev->dev,
1690 "CTS not active on modem port?\n"); 1686 "CTS not active on modem port?\n");
1691 } 1687 }
1692 } else { 1688 } else {
1693 enable_transmit_ul(tty->index % MAX_PORT, dc); 1689 enable_transmit_ul(tty->index % MAX_PORT, dc);
1694 } 1690 }
1695 spin_unlock_irqrestore(&dc->spin_mutex, flags); 1691 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1696 1692
1697 return rval; 1693 return rval;
1698 } 1694 }
1699 1695
1700 /* 1696 /*
1701 * Calculate how much is left in device 1697 * Calculate how much is left in device
1702 * This method is called by the upper tty layer. 1698 * This method is called by the upper tty layer.
1703 * #according to sources N_TTY.c it expects a value >= 0 and 1699 * #according to sources N_TTY.c it expects a value >= 0 and
1704 * does not check for negative values. 1700 * does not check for negative values.
1705 * 1701 *
1706 * If the port is unplugged report lots of room and let the bits 1702 * If the port is unplugged report lots of room and let the bits
1707 * dribble away so we don't block anything. 1703 * dribble away so we don't block anything.
1708 */ 1704 */
1709 static int ntty_write_room(struct tty_struct *tty) 1705 static int ntty_write_room(struct tty_struct *tty)
1710 { 1706 {
1711 struct port *port = tty->driver_data; 1707 struct port *port = tty->driver_data;
1712 int room = 4096; 1708 int room = 4096;
1713 const struct nozomi *dc = get_dc_by_tty(tty); 1709 const struct nozomi *dc = get_dc_by_tty(tty);
1714 1710
1715 if (dc) 1711 if (dc)
1716 room = kfifo_avail(&port->fifo_ul); 1712 room = kfifo_avail(&port->fifo_ul);
1717 1713
1718 return room; 1714 return room;
1719 } 1715 }
1720 1716
1721 /* Gets io control parameters */ 1717 /* Gets io control parameters */
1722 static int ntty_tiocmget(struct tty_struct *tty) 1718 static int ntty_tiocmget(struct tty_struct *tty)
1723 { 1719 {
1724 const struct port *port = tty->driver_data; 1720 const struct port *port = tty->driver_data;
1725 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl; 1721 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1726 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul; 1722 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
1727 1723
1728 /* Note: these could change under us but it is not clear this 1724 /* Note: these could change under us but it is not clear this
1729 matters if so */ 1725 matters if so */
1730 return (ctrl_ul->RTS ? TIOCM_RTS : 0) | 1726 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1731 (ctrl_ul->DTR ? TIOCM_DTR : 0) | 1727 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1732 (ctrl_dl->DCD ? TIOCM_CAR : 0) | 1728 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1733 (ctrl_dl->RI ? TIOCM_RNG : 0) | 1729 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1734 (ctrl_dl->DSR ? TIOCM_DSR : 0) | 1730 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1735 (ctrl_dl->CTS ? TIOCM_CTS : 0); 1731 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1736 } 1732 }
1737 1733
1738 /* Sets io controls parameters */ 1734 /* Sets io controls parameters */
1739 static int ntty_tiocmset(struct tty_struct *tty, 1735 static int ntty_tiocmset(struct tty_struct *tty,
1740 unsigned int set, unsigned int clear) 1736 unsigned int set, unsigned int clear)
1741 { 1737 {
1742 struct nozomi *dc = get_dc_by_tty(tty); 1738 struct nozomi *dc = get_dc_by_tty(tty);
1743 unsigned long flags; 1739 unsigned long flags;
1744 1740
1745 spin_lock_irqsave(&dc->spin_mutex, flags); 1741 spin_lock_irqsave(&dc->spin_mutex, flags);
1746 if (set & TIOCM_RTS) 1742 if (set & TIOCM_RTS)
1747 set_rts(tty, 1); 1743 set_rts(tty, 1);
1748 else if (clear & TIOCM_RTS) 1744 else if (clear & TIOCM_RTS)
1749 set_rts(tty, 0); 1745 set_rts(tty, 0);
1750 1746
1751 if (set & TIOCM_DTR) 1747 if (set & TIOCM_DTR)
1752 set_dtr(tty, 1); 1748 set_dtr(tty, 1);
1753 else if (clear & TIOCM_DTR) 1749 else if (clear & TIOCM_DTR)
1754 set_dtr(tty, 0); 1750 set_dtr(tty, 0);
1755 spin_unlock_irqrestore(&dc->spin_mutex, flags); 1751 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1756 1752
1757 return 0; 1753 return 0;
1758 } 1754 }
1759 1755
1760 static int ntty_cflags_changed(struct port *port, unsigned long flags, 1756 static int ntty_cflags_changed(struct port *port, unsigned long flags,
1761 struct async_icount *cprev) 1757 struct async_icount *cprev)
1762 { 1758 {
1763 const struct async_icount cnow = port->tty_icount; 1759 const struct async_icount cnow = port->tty_icount;
1764 int ret; 1760 int ret;
1765 1761
1766 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) || 1762 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1767 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) || 1763 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1768 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) || 1764 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1769 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts)); 1765 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1770 1766
1771 *cprev = cnow; 1767 *cprev = cnow;
1772 1768
1773 return ret; 1769 return ret;
1774 } 1770 }
1775 1771
1776 static int ntty_tiocgicount(struct tty_struct *tty, 1772 static int ntty_tiocgicount(struct tty_struct *tty,
1777 struct serial_icounter_struct *icount) 1773 struct serial_icounter_struct *icount)
1778 { 1774 {
1779 struct port *port = tty->driver_data; 1775 struct port *port = tty->driver_data;
1780 const struct async_icount cnow = port->tty_icount; 1776 const struct async_icount cnow = port->tty_icount;
1781 1777
1782 icount->cts = cnow.cts; 1778 icount->cts = cnow.cts;
1783 icount->dsr = cnow.dsr; 1779 icount->dsr = cnow.dsr;
1784 icount->rng = cnow.rng; 1780 icount->rng = cnow.rng;
1785 icount->dcd = cnow.dcd; 1781 icount->dcd = cnow.dcd;
1786 icount->rx = cnow.rx; 1782 icount->rx = cnow.rx;
1787 icount->tx = cnow.tx; 1783 icount->tx = cnow.tx;
1788 icount->frame = cnow.frame; 1784 icount->frame = cnow.frame;
1789 icount->overrun = cnow.overrun; 1785 icount->overrun = cnow.overrun;
1790 icount->parity = cnow.parity; 1786 icount->parity = cnow.parity;
1791 icount->brk = cnow.brk; 1787 icount->brk = cnow.brk;
1792 icount->buf_overrun = cnow.buf_overrun; 1788 icount->buf_overrun = cnow.buf_overrun;
1793 return 0; 1789 return 0;
1794 } 1790 }
1795 1791
1796 static int ntty_ioctl(struct tty_struct *tty, 1792 static int ntty_ioctl(struct tty_struct *tty,
1797 unsigned int cmd, unsigned long arg) 1793 unsigned int cmd, unsigned long arg)
1798 { 1794 {
1799 struct port *port = tty->driver_data; 1795 struct port *port = tty->driver_data;
1800 int rval = -ENOIOCTLCMD; 1796 int rval = -ENOIOCTLCMD;
1801 1797
1802 DBG1("******** IOCTL, cmd: %d", cmd); 1798 DBG1("******** IOCTL, cmd: %d", cmd);
1803 1799
1804 switch (cmd) { 1800 switch (cmd) {
1805 case TIOCMIWAIT: { 1801 case TIOCMIWAIT: {
1806 struct async_icount cprev = port->tty_icount; 1802 struct async_icount cprev = port->tty_icount;
1807 1803
1808 rval = wait_event_interruptible(port->tty_wait, 1804 rval = wait_event_interruptible(port->tty_wait,
1809 ntty_cflags_changed(port, arg, &cprev)); 1805 ntty_cflags_changed(port, arg, &cprev));
1810 break; 1806 break;
1811 } 1807 }
1812 default: 1808 default:
1813 DBG1("ERR: 0x%08X, %d", cmd, cmd); 1809 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1814 break; 1810 break;
1815 }; 1811 };
1816 1812
1817 return rval; 1813 return rval;
1818 } 1814 }
1819 1815
1820 /* 1816 /*
1821 * Called by the upper tty layer when tty buffers are ready 1817 * Called by the upper tty layer when tty buffers are ready
1822 * to receive data again after a call to throttle. 1818 * to receive data again after a call to throttle.
1823 */ 1819 */
1824 static void ntty_unthrottle(struct tty_struct *tty) 1820 static void ntty_unthrottle(struct tty_struct *tty)
1825 { 1821 {
1826 struct nozomi *dc = get_dc_by_tty(tty); 1822 struct nozomi *dc = get_dc_by_tty(tty);
1827 unsigned long flags; 1823 unsigned long flags;
1828 1824
1829 DBG1("UNTHROTTLE"); 1825 DBG1("UNTHROTTLE");
1830 spin_lock_irqsave(&dc->spin_mutex, flags); 1826 spin_lock_irqsave(&dc->spin_mutex, flags);
1831 enable_transmit_dl(tty->index % MAX_PORT, dc); 1827 enable_transmit_dl(tty->index % MAX_PORT, dc);
1832 set_rts(tty, 1); 1828 set_rts(tty, 1);
1833 1829
1834 spin_unlock_irqrestore(&dc->spin_mutex, flags); 1830 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1835 } 1831 }
1836 1832
1837 /* 1833 /*
1838 * Called by the upper tty layer when the tty buffers are almost full. 1834 * Called by the upper tty layer when the tty buffers are almost full.
1839 * The driver should stop send more data. 1835 * The driver should stop send more data.
1840 */ 1836 */
1841 static void ntty_throttle(struct tty_struct *tty) 1837 static void ntty_throttle(struct tty_struct *tty)
1842 { 1838 {
1843 struct nozomi *dc = get_dc_by_tty(tty); 1839 struct nozomi *dc = get_dc_by_tty(tty);
1844 unsigned long flags; 1840 unsigned long flags;
1845 1841
1846 DBG1("THROTTLE"); 1842 DBG1("THROTTLE");
1847 spin_lock_irqsave(&dc->spin_mutex, flags); 1843 spin_lock_irqsave(&dc->spin_mutex, flags);
1848 set_rts(tty, 0); 1844 set_rts(tty, 0);
1849 spin_unlock_irqrestore(&dc->spin_mutex, flags); 1845 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1850 } 1846 }
1851 1847
1852 /* Returns number of chars in buffer, called by tty layer */ 1848 /* Returns number of chars in buffer, called by tty layer */
1853 static s32 ntty_chars_in_buffer(struct tty_struct *tty) 1849 static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1854 { 1850 {
1855 struct port *port = tty->driver_data; 1851 struct port *port = tty->driver_data;
1856 struct nozomi *dc = get_dc_by_tty(tty); 1852 struct nozomi *dc = get_dc_by_tty(tty);
1857 s32 rval = 0; 1853 s32 rval = 0;
1858 1854
1859 if (unlikely(!dc || !port)) { 1855 if (unlikely(!dc || !port)) {
1860 goto exit_in_buffer; 1856 goto exit_in_buffer;
1861 } 1857 }
1862 1858
1863 rval = kfifo_len(&port->fifo_ul); 1859 rval = kfifo_len(&port->fifo_ul);
1864 1860
1865 exit_in_buffer: 1861 exit_in_buffer:
1866 return rval; 1862 return rval;
1867 } 1863 }
1868 1864
1869 static const struct tty_port_operations noz_tty_port_ops = { 1865 static const struct tty_port_operations noz_tty_port_ops = {
1870 .activate = ntty_activate, 1866 .activate = ntty_activate,
1871 .shutdown = ntty_shutdown, 1867 .shutdown = ntty_shutdown,
1872 }; 1868 };
1873 1869
1874 static const struct tty_operations tty_ops = { 1870 static const struct tty_operations tty_ops = {
1875 .ioctl = ntty_ioctl, 1871 .ioctl = ntty_ioctl,
1876 .open = ntty_open, 1872 .open = ntty_open,
1877 .close = ntty_close, 1873 .close = ntty_close,
1878 .hangup = ntty_hangup, 1874 .hangup = ntty_hangup,
1879 .write = ntty_write, 1875 .write = ntty_write,
1880 .write_room = ntty_write_room, 1876 .write_room = ntty_write_room,
1881 .unthrottle = ntty_unthrottle, 1877 .unthrottle = ntty_unthrottle,
1882 .throttle = ntty_throttle, 1878 .throttle = ntty_throttle,
1883 .chars_in_buffer = ntty_chars_in_buffer, 1879 .chars_in_buffer = ntty_chars_in_buffer,
1884 .tiocmget = ntty_tiocmget, 1880 .tiocmget = ntty_tiocmget,
1885 .tiocmset = ntty_tiocmset, 1881 .tiocmset = ntty_tiocmset,
1886 .get_icount = ntty_tiocgicount, 1882 .get_icount = ntty_tiocgicount,
1887 .install = ntty_install, 1883 .install = ntty_install,
1888 .cleanup = ntty_cleanup, 1884 .cleanup = ntty_cleanup,
1889 }; 1885 };
1890 1886
1891 /* Module initialization */ 1887 /* Module initialization */
1892 static struct pci_driver nozomi_driver = { 1888 static struct pci_driver nozomi_driver = {
1893 .name = NOZOMI_NAME, 1889 .name = NOZOMI_NAME,
1894 .id_table = nozomi_pci_tbl, 1890 .id_table = nozomi_pci_tbl,
1895 .probe = nozomi_card_init, 1891 .probe = nozomi_card_init,
1896 .remove = nozomi_card_exit, 1892 .remove = nozomi_card_exit,
1897 }; 1893 };
1898 1894
1899 static __init int nozomi_init(void) 1895 static __init int nozomi_init(void)
1900 { 1896 {
1901 int ret; 1897 int ret;
1902 1898
1903 printk(KERN_INFO "Initializing %s\n", VERSION_STRING); 1899 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1904 1900
1905 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS); 1901 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1906 if (!ntty_driver) 1902 if (!ntty_driver)
1907 return -ENOMEM; 1903 return -ENOMEM;
1908 1904
1909 ntty_driver->driver_name = NOZOMI_NAME_TTY; 1905 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1910 ntty_driver->name = "noz"; 1906 ntty_driver->name = "noz";
1911 ntty_driver->major = 0; 1907 ntty_driver->major = 0;
1912 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL; 1908 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1913 ntty_driver->subtype = SERIAL_TYPE_NORMAL; 1909 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1914 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 1910 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1915 ntty_driver->init_termios = tty_std_termios; 1911 ntty_driver->init_termios = tty_std_termios;
1916 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \ 1912 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1917 HUPCL | CLOCAL; 1913 HUPCL | CLOCAL;
1918 ntty_driver->init_termios.c_ispeed = 115200; 1914 ntty_driver->init_termios.c_ispeed = 115200;
1919 ntty_driver->init_termios.c_ospeed = 115200; 1915 ntty_driver->init_termios.c_ospeed = 115200;
1920 tty_set_operations(ntty_driver, &tty_ops); 1916 tty_set_operations(ntty_driver, &tty_ops);
1921 1917
1922 ret = tty_register_driver(ntty_driver); 1918 ret = tty_register_driver(ntty_driver);
1923 if (ret) { 1919 if (ret) {
1924 printk(KERN_ERR "Nozomi: failed to register ntty driver\n"); 1920 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1925 goto free_tty; 1921 goto free_tty;
1926 } 1922 }
1927 1923
1928 ret = pci_register_driver(&nozomi_driver); 1924 ret = pci_register_driver(&nozomi_driver);
1929 if (ret) { 1925 if (ret) {
1930 printk(KERN_ERR "Nozomi: can't register pci driver\n"); 1926 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1931 goto unr_tty; 1927 goto unr_tty;
1932 } 1928 }
1933 1929
1934 return 0; 1930 return 0;
1935 unr_tty: 1931 unr_tty:
1936 tty_unregister_driver(ntty_driver); 1932 tty_unregister_driver(ntty_driver);
1937 free_tty: 1933 free_tty:
1938 put_tty_driver(ntty_driver); 1934 put_tty_driver(ntty_driver);
1939 return ret; 1935 return ret;
1940 } 1936 }
1941 1937
1942 static __exit void nozomi_exit(void) 1938 static __exit void nozomi_exit(void)
1943 { 1939 {
1944 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC); 1940 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1945 pci_unregister_driver(&nozomi_driver); 1941 pci_unregister_driver(&nozomi_driver);
1946 tty_unregister_driver(ntty_driver); 1942 tty_unregister_driver(ntty_driver);
1947 put_tty_driver(ntty_driver); 1943 put_tty_driver(ntty_driver);
1948 } 1944 }
1949 1945
1950 module_init(nozomi_init); 1946 module_init(nozomi_init);
1951 module_exit(nozomi_exit); 1947 module_exit(nozomi_exit);
1952 1948
1953 module_param(debug, int, S_IRUGO | S_IWUSR); 1949 module_param(debug, int, S_IRUGO | S_IWUSR);
1954 1950
1955 MODULE_LICENSE("Dual BSD/GPL"); 1951 MODULE_LICENSE("Dual BSD/GPL");
1956 MODULE_DESCRIPTION(DRIVER_DESC); 1952 MODULE_DESCRIPTION(DRIVER_DESC);
1957 1953
drivers/tty/serial/ifx6x60.c
1 /**************************************************************************** 1 /****************************************************************************
2 * 2 *
3 * Driver for the IFX 6x60 spi modem. 3 * Driver for the IFX 6x60 spi modem.
4 * 4 *
5 * Copyright (C) 2008 Option International 5 * Copyright (C) 2008 Option International
6 * Copyright (C) 2008 Filip Aben <f.aben@option.com> 6 * Copyright (C) 2008 Filip Aben <f.aben@option.com>
7 * Denis Joseph Barrow <d.barow@option.com> 7 * Denis Joseph Barrow <d.barow@option.com>
8 * Jan Dumon <j.dumon@option.com> 8 * Jan Dumon <j.dumon@option.com>
9 * 9 *
10 * Copyright (C) 2009, 2010 Intel Corp 10 * Copyright (C) 2009, 2010 Intel Corp
11 * Russ Gorby <russ.gorby@intel.com> 11 * Russ Gorby <russ.gorby@intel.com>
12 * 12 *
13 * This program is free software; you can redistribute it and/or modify 13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as 14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation. 15 * published by the Free Software Foundation.
16 * 16 *
17 * This program is distributed in the hope that it will be useful, 17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details. 20 * GNU General Public License for more details.
21 * 21 *
22 * You should have received a copy of the GNU General Public License 22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software 23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 * USA 25 * USA
26 * 26 *
27 * Driver modified by Intel from Option gtm501l_spi.c 27 * Driver modified by Intel from Option gtm501l_spi.c
28 * 28 *
29 * Notes 29 * Notes
30 * o The driver currently assumes a single device only. If you need to 30 * o The driver currently assumes a single device only. If you need to
31 * change this then look for saved_ifx_dev and add a device lookup 31 * change this then look for saved_ifx_dev and add a device lookup
32 * o The driver is intended to be big-endian safe but has never been 32 * o The driver is intended to be big-endian safe but has never been
33 * tested that way (no suitable hardware). There are a couple of FIXME 33 * tested that way (no suitable hardware). There are a couple of FIXME
34 * notes by areas that may need addressing 34 * notes by areas that may need addressing
35 * o Some of the GPIO naming/setup assumptions may need revisiting if 35 * o Some of the GPIO naming/setup assumptions may need revisiting if
36 * you need to use this driver for another platform. 36 * you need to use this driver for another platform.
37 * 37 *
38 *****************************************************************************/ 38 *****************************************************************************/
39 #include <linux/dma-mapping.h> 39 #include <linux/dma-mapping.h>
40 #include <linux/module.h> 40 #include <linux/module.h>
41 #include <linux/termios.h> 41 #include <linux/termios.h>
42 #include <linux/tty.h> 42 #include <linux/tty.h>
43 #include <linux/device.h> 43 #include <linux/device.h>
44 #include <linux/spi/spi.h> 44 #include <linux/spi/spi.h>
45 #include <linux/kfifo.h> 45 #include <linux/kfifo.h>
46 #include <linux/tty_flip.h> 46 #include <linux/tty_flip.h>
47 #include <linux/timer.h> 47 #include <linux/timer.h>
48 #include <linux/serial.h> 48 #include <linux/serial.h>
49 #include <linux/interrupt.h> 49 #include <linux/interrupt.h>
50 #include <linux/irq.h> 50 #include <linux/irq.h>
51 #include <linux/rfkill.h> 51 #include <linux/rfkill.h>
52 #include <linux/fs.h> 52 #include <linux/fs.h>
53 #include <linux/ip.h> 53 #include <linux/ip.h>
54 #include <linux/dmapool.h> 54 #include <linux/dmapool.h>
55 #include <linux/gpio.h> 55 #include <linux/gpio.h>
56 #include <linux/sched.h> 56 #include <linux/sched.h>
57 #include <linux/time.h> 57 #include <linux/time.h>
58 #include <linux/wait.h> 58 #include <linux/wait.h>
59 #include <linux/pm.h> 59 #include <linux/pm.h>
60 #include <linux/pm_runtime.h> 60 #include <linux/pm_runtime.h>
61 #include <linux/spi/ifx_modem.h> 61 #include <linux/spi/ifx_modem.h>
62 #include <linux/delay.h> 62 #include <linux/delay.h>
63 #include <linux/reboot.h> 63 #include <linux/reboot.h>
64 64
65 #include "ifx6x60.h" 65 #include "ifx6x60.h"
66 66
67 #define IFX_SPI_MORE_MASK 0x10 67 #define IFX_SPI_MORE_MASK 0x10
68 #define IFX_SPI_MORE_BIT 4 /* bit position in u8 */ 68 #define IFX_SPI_MORE_BIT 4 /* bit position in u8 */
69 #define IFX_SPI_CTS_BIT 6 /* bit position in u8 */ 69 #define IFX_SPI_CTS_BIT 6 /* bit position in u8 */
70 #define IFX_SPI_MODE SPI_MODE_1 70 #define IFX_SPI_MODE SPI_MODE_1
71 #define IFX_SPI_TTY_ID 0 71 #define IFX_SPI_TTY_ID 0
72 #define IFX_SPI_TIMEOUT_SEC 2 72 #define IFX_SPI_TIMEOUT_SEC 2
73 #define IFX_SPI_HEADER_0 (-1) 73 #define IFX_SPI_HEADER_0 (-1)
74 #define IFX_SPI_HEADER_F (-2) 74 #define IFX_SPI_HEADER_F (-2)
75 75
76 #define PO_POST_DELAY 200 76 #define PO_POST_DELAY 200
77 #define IFX_MDM_RST_PMU 4 77 #define IFX_MDM_RST_PMU 4
78 78
79 /* forward reference */ 79 /* forward reference */
80 static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev); 80 static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev);
81 static int ifx_modem_reboot_callback(struct notifier_block *nfb, 81 static int ifx_modem_reboot_callback(struct notifier_block *nfb,
82 unsigned long event, void *data); 82 unsigned long event, void *data);
83 static int ifx_modem_power_off(struct ifx_spi_device *ifx_dev); 83 static int ifx_modem_power_off(struct ifx_spi_device *ifx_dev);
84 84
85 /* local variables */ 85 /* local variables */
86 static int spi_bpw = 16; /* 8, 16 or 32 bit word length */ 86 static int spi_bpw = 16; /* 8, 16 or 32 bit word length */
87 static struct tty_driver *tty_drv; 87 static struct tty_driver *tty_drv;
88 static struct ifx_spi_device *saved_ifx_dev; 88 static struct ifx_spi_device *saved_ifx_dev;
89 static struct lock_class_key ifx_spi_key; 89 static struct lock_class_key ifx_spi_key;
90 90
91 static struct notifier_block ifx_modem_reboot_notifier_block = { 91 static struct notifier_block ifx_modem_reboot_notifier_block = {
92 .notifier_call = ifx_modem_reboot_callback, 92 .notifier_call = ifx_modem_reboot_callback,
93 }; 93 };
94 94
95 static int ifx_modem_power_off(struct ifx_spi_device *ifx_dev) 95 static int ifx_modem_power_off(struct ifx_spi_device *ifx_dev)
96 { 96 {
97 gpio_set_value(IFX_MDM_RST_PMU, 1); 97 gpio_set_value(IFX_MDM_RST_PMU, 1);
98 msleep(PO_POST_DELAY); 98 msleep(PO_POST_DELAY);
99 99
100 return 0; 100 return 0;
101 } 101 }
102 102
103 static int ifx_modem_reboot_callback(struct notifier_block *nfb, 103 static int ifx_modem_reboot_callback(struct notifier_block *nfb,
104 unsigned long event, void *data) 104 unsigned long event, void *data)
105 { 105 {
106 if (saved_ifx_dev) 106 if (saved_ifx_dev)
107 ifx_modem_power_off(saved_ifx_dev); 107 ifx_modem_power_off(saved_ifx_dev);
108 else 108 else
109 pr_warn("no ifx modem active;\n"); 109 pr_warn("no ifx modem active;\n");
110 110
111 return NOTIFY_OK; 111 return NOTIFY_OK;
112 } 112 }
113 113
114 /* GPIO/GPE settings */ 114 /* GPIO/GPE settings */
115 115
116 /** 116 /**
117 * mrdy_set_high - set MRDY GPIO 117 * mrdy_set_high - set MRDY GPIO
118 * @ifx: device we are controlling 118 * @ifx: device we are controlling
119 * 119 *
120 */ 120 */
121 static inline void mrdy_set_high(struct ifx_spi_device *ifx) 121 static inline void mrdy_set_high(struct ifx_spi_device *ifx)
122 { 122 {
123 gpio_set_value(ifx->gpio.mrdy, 1); 123 gpio_set_value(ifx->gpio.mrdy, 1);
124 } 124 }
125 125
126 /** 126 /**
127 * mrdy_set_low - clear MRDY GPIO 127 * mrdy_set_low - clear MRDY GPIO
128 * @ifx: device we are controlling 128 * @ifx: device we are controlling
129 * 129 *
130 */ 130 */
131 static inline void mrdy_set_low(struct ifx_spi_device *ifx) 131 static inline void mrdy_set_low(struct ifx_spi_device *ifx)
132 { 132 {
133 gpio_set_value(ifx->gpio.mrdy, 0); 133 gpio_set_value(ifx->gpio.mrdy, 0);
134 } 134 }
135 135
136 /** 136 /**
137 * ifx_spi_power_state_set 137 * ifx_spi_power_state_set
138 * @ifx_dev: our SPI device 138 * @ifx_dev: our SPI device
139 * @val: bits to set 139 * @val: bits to set
140 * 140 *
141 * Set bit in power status and signal power system if status becomes non-0 141 * Set bit in power status and signal power system if status becomes non-0
142 */ 142 */
143 static void 143 static void
144 ifx_spi_power_state_set(struct ifx_spi_device *ifx_dev, unsigned char val) 144 ifx_spi_power_state_set(struct ifx_spi_device *ifx_dev, unsigned char val)
145 { 145 {
146 unsigned long flags; 146 unsigned long flags;
147 147
148 spin_lock_irqsave(&ifx_dev->power_lock, flags); 148 spin_lock_irqsave(&ifx_dev->power_lock, flags);
149 149
150 /* 150 /*
151 * if power status is already non-0, just update, else 151 * if power status is already non-0, just update, else
152 * tell power system 152 * tell power system
153 */ 153 */
154 if (!ifx_dev->power_status) 154 if (!ifx_dev->power_status)
155 pm_runtime_get(&ifx_dev->spi_dev->dev); 155 pm_runtime_get(&ifx_dev->spi_dev->dev);
156 ifx_dev->power_status |= val; 156 ifx_dev->power_status |= val;
157 157
158 spin_unlock_irqrestore(&ifx_dev->power_lock, flags); 158 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
159 } 159 }
160 160
161 /** 161 /**
162 * ifx_spi_power_state_clear - clear power bit 162 * ifx_spi_power_state_clear - clear power bit
163 * @ifx_dev: our SPI device 163 * @ifx_dev: our SPI device
164 * @val: bits to clear 164 * @val: bits to clear
165 * 165 *
166 * clear bit in power status and signal power system if status becomes 0 166 * clear bit in power status and signal power system if status becomes 0
167 */ 167 */
168 static void 168 static void
169 ifx_spi_power_state_clear(struct ifx_spi_device *ifx_dev, unsigned char val) 169 ifx_spi_power_state_clear(struct ifx_spi_device *ifx_dev, unsigned char val)
170 { 170 {
171 unsigned long flags; 171 unsigned long flags;
172 172
173 spin_lock_irqsave(&ifx_dev->power_lock, flags); 173 spin_lock_irqsave(&ifx_dev->power_lock, flags);
174 174
175 if (ifx_dev->power_status) { 175 if (ifx_dev->power_status) {
176 ifx_dev->power_status &= ~val; 176 ifx_dev->power_status &= ~val;
177 if (!ifx_dev->power_status) 177 if (!ifx_dev->power_status)
178 pm_runtime_put(&ifx_dev->spi_dev->dev); 178 pm_runtime_put(&ifx_dev->spi_dev->dev);
179 } 179 }
180 180
181 spin_unlock_irqrestore(&ifx_dev->power_lock, flags); 181 spin_unlock_irqrestore(&ifx_dev->power_lock, flags);
182 } 182 }
183 183
184 /** 184 /**
185 * swap_buf_8 185 * swap_buf_8
186 * @buf: our buffer 186 * @buf: our buffer
187 * @len : number of bytes (not words) in the buffer 187 * @len : number of bytes (not words) in the buffer
188 * @end: end of buffer 188 * @end: end of buffer
189 * 189 *
190 * Swap the contents of a buffer into big endian format 190 * Swap the contents of a buffer into big endian format
191 */ 191 */
192 static inline void swap_buf_8(unsigned char *buf, int len, void *end) 192 static inline void swap_buf_8(unsigned char *buf, int len, void *end)
193 { 193 {
194 /* don't swap buffer if SPI word width is 8 bits */ 194 /* don't swap buffer if SPI word width is 8 bits */
195 return; 195 return;
196 } 196 }
197 197
198 /** 198 /**
199 * swap_buf_16 199 * swap_buf_16
200 * @buf: our buffer 200 * @buf: our buffer
201 * @len : number of bytes (not words) in the buffer 201 * @len : number of bytes (not words) in the buffer
202 * @end: end of buffer 202 * @end: end of buffer
203 * 203 *
204 * Swap the contents of a buffer into big endian format 204 * Swap the contents of a buffer into big endian format
205 */ 205 */
206 static inline void swap_buf_16(unsigned char *buf, int len, void *end) 206 static inline void swap_buf_16(unsigned char *buf, int len, void *end)
207 { 207 {
208 int n; 208 int n;
209 209
210 u16 *buf_16 = (u16 *)buf; 210 u16 *buf_16 = (u16 *)buf;
211 len = ((len + 1) >> 1); 211 len = ((len + 1) >> 1);
212 if ((void *)&buf_16[len] > end) { 212 if ((void *)&buf_16[len] > end) {
213 pr_err("swap_buf_16: swap exceeds boundary (%p > %p)!", 213 pr_err("swap_buf_16: swap exceeds boundary (%p > %p)!",
214 &buf_16[len], end); 214 &buf_16[len], end);
215 return; 215 return;
216 } 216 }
217 for (n = 0; n < len; n++) { 217 for (n = 0; n < len; n++) {
218 *buf_16 = cpu_to_be16(*buf_16); 218 *buf_16 = cpu_to_be16(*buf_16);
219 buf_16++; 219 buf_16++;
220 } 220 }
221 } 221 }
222 222
223 /** 223 /**
224 * swap_buf_32 224 * swap_buf_32
225 * @buf: our buffer 225 * @buf: our buffer
226 * @len : number of bytes (not words) in the buffer 226 * @len : number of bytes (not words) in the buffer
227 * @end: end of buffer 227 * @end: end of buffer
228 * 228 *
229 * Swap the contents of a buffer into big endian format 229 * Swap the contents of a buffer into big endian format
230 */ 230 */
231 static inline void swap_buf_32(unsigned char *buf, int len, void *end) 231 static inline void swap_buf_32(unsigned char *buf, int len, void *end)
232 { 232 {
233 int n; 233 int n;
234 234
235 u32 *buf_32 = (u32 *)buf; 235 u32 *buf_32 = (u32 *)buf;
236 len = (len + 3) >> 2; 236 len = (len + 3) >> 2;
237 237
238 if ((void *)&buf_32[len] > end) { 238 if ((void *)&buf_32[len] > end) {
239 pr_err("swap_buf_32: swap exceeds boundary (%p > %p)!\n", 239 pr_err("swap_buf_32: swap exceeds boundary (%p > %p)!\n",
240 &buf_32[len], end); 240 &buf_32[len], end);
241 return; 241 return;
242 } 242 }
243 for (n = 0; n < len; n++) { 243 for (n = 0; n < len; n++) {
244 *buf_32 = cpu_to_be32(*buf_32); 244 *buf_32 = cpu_to_be32(*buf_32);
245 buf_32++; 245 buf_32++;
246 } 246 }
247 } 247 }
248 248
249 /** 249 /**
250 * mrdy_assert - assert MRDY line 250 * mrdy_assert - assert MRDY line
251 * @ifx_dev: our SPI device 251 * @ifx_dev: our SPI device
252 * 252 *
253 * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low 253 * Assert mrdy and set timer to wait for SRDY interrupt, if SRDY is low
254 * now. 254 * now.
255 * 255 *
256 * FIXME: Can SRDY even go high as we are running this code ? 256 * FIXME: Can SRDY even go high as we are running this code ?
257 */ 257 */
258 static void mrdy_assert(struct ifx_spi_device *ifx_dev) 258 static void mrdy_assert(struct ifx_spi_device *ifx_dev)
259 { 259 {
260 int val = gpio_get_value(ifx_dev->gpio.srdy); 260 int val = gpio_get_value(ifx_dev->gpio.srdy);
261 if (!val) { 261 if (!val) {
262 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING, 262 if (!test_and_set_bit(IFX_SPI_STATE_TIMER_PENDING,
263 &ifx_dev->flags)) { 263 &ifx_dev->flags)) {
264 mod_timer(&ifx_dev->spi_timer,jiffies + IFX_SPI_TIMEOUT_SEC*HZ); 264 mod_timer(&ifx_dev->spi_timer,jiffies + IFX_SPI_TIMEOUT_SEC*HZ);
265 265
266 } 266 }
267 } 267 }
268 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_DATA_PENDING); 268 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_DATA_PENDING);
269 mrdy_set_high(ifx_dev); 269 mrdy_set_high(ifx_dev);
270 } 270 }
271 271
272 /** 272 /**
273 * ifx_spi_hangup - hang up an IFX device 273 * ifx_spi_hangup - hang up an IFX device
274 * @ifx_dev: our SPI device 274 * @ifx_dev: our SPI device
275 * 275 *
276 * Hang up the tty attached to the IFX device if one is currently 276 * Hang up the tty attached to the IFX device if one is currently
277 * open. If not take no action 277 * open. If not take no action
278 */ 278 */
279 static void ifx_spi_ttyhangup(struct ifx_spi_device *ifx_dev) 279 static void ifx_spi_ttyhangup(struct ifx_spi_device *ifx_dev)
280 { 280 {
281 struct tty_port *pport = &ifx_dev->tty_port; 281 struct tty_port *pport = &ifx_dev->tty_port;
282 struct tty_struct *tty = tty_port_tty_get(pport); 282 struct tty_struct *tty = tty_port_tty_get(pport);
283 if (tty) { 283 if (tty) {
284 tty_hangup(tty); 284 tty_hangup(tty);
285 tty_kref_put(tty); 285 tty_kref_put(tty);
286 } 286 }
287 } 287 }
288 288
289 /** 289 /**
290 * ifx_spi_timeout - SPI timeout 290 * ifx_spi_timeout - SPI timeout
291 * @arg: our SPI device 291 * @arg: our SPI device
292 * 292 *
293 * The SPI has timed out: hang up the tty. Users will then see a hangup 293 * The SPI has timed out: hang up the tty. Users will then see a hangup
294 * and error events. 294 * and error events.
295 */ 295 */
296 static void ifx_spi_timeout(unsigned long arg) 296 static void ifx_spi_timeout(unsigned long arg)
297 { 297 {
298 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *)arg; 298 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *)arg;
299 299
300 dev_warn(&ifx_dev->spi_dev->dev, "*** SPI Timeout ***"); 300 dev_warn(&ifx_dev->spi_dev->dev, "*** SPI Timeout ***");
301 ifx_spi_ttyhangup(ifx_dev); 301 ifx_spi_ttyhangup(ifx_dev);
302 mrdy_set_low(ifx_dev); 302 mrdy_set_low(ifx_dev);
303 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags); 303 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
304 } 304 }
305 305
306 /* char/tty operations */ 306 /* char/tty operations */
307 307
308 /** 308 /**
309 * ifx_spi_tiocmget - get modem lines 309 * ifx_spi_tiocmget - get modem lines
310 * @tty: our tty device 310 * @tty: our tty device
311 * @filp: file handle issuing the request 311 * @filp: file handle issuing the request
312 * 312 *
313 * Map the signal state into Linux modem flags and report the value 313 * Map the signal state into Linux modem flags and report the value
314 * in Linux terms 314 * in Linux terms
315 */ 315 */
316 static int ifx_spi_tiocmget(struct tty_struct *tty) 316 static int ifx_spi_tiocmget(struct tty_struct *tty)
317 { 317 {
318 unsigned int value; 318 unsigned int value;
319 struct ifx_spi_device *ifx_dev = tty->driver_data; 319 struct ifx_spi_device *ifx_dev = tty->driver_data;
320 320
321 value = 321 value =
322 (test_bit(IFX_SPI_RTS, &ifx_dev->signal_state) ? TIOCM_RTS : 0) | 322 (test_bit(IFX_SPI_RTS, &ifx_dev->signal_state) ? TIOCM_RTS : 0) |
323 (test_bit(IFX_SPI_DTR, &ifx_dev->signal_state) ? TIOCM_DTR : 0) | 323 (test_bit(IFX_SPI_DTR, &ifx_dev->signal_state) ? TIOCM_DTR : 0) |
324 (test_bit(IFX_SPI_CTS, &ifx_dev->signal_state) ? TIOCM_CTS : 0) | 324 (test_bit(IFX_SPI_CTS, &ifx_dev->signal_state) ? TIOCM_CTS : 0) |
325 (test_bit(IFX_SPI_DSR, &ifx_dev->signal_state) ? TIOCM_DSR : 0) | 325 (test_bit(IFX_SPI_DSR, &ifx_dev->signal_state) ? TIOCM_DSR : 0) |
326 (test_bit(IFX_SPI_DCD, &ifx_dev->signal_state) ? TIOCM_CAR : 0) | 326 (test_bit(IFX_SPI_DCD, &ifx_dev->signal_state) ? TIOCM_CAR : 0) |
327 (test_bit(IFX_SPI_RI, &ifx_dev->signal_state) ? TIOCM_RNG : 0); 327 (test_bit(IFX_SPI_RI, &ifx_dev->signal_state) ? TIOCM_RNG : 0);
328 return value; 328 return value;
329 } 329 }
330 330
331 /** 331 /**
332 * ifx_spi_tiocmset - set modem bits 332 * ifx_spi_tiocmset - set modem bits
333 * @tty: the tty structure 333 * @tty: the tty structure
334 * @set: bits to set 334 * @set: bits to set
335 * @clear: bits to clear 335 * @clear: bits to clear
336 * 336 *
337 * The IFX6x60 only supports DTR and RTS. Set them accordingly 337 * The IFX6x60 only supports DTR and RTS. Set them accordingly
338 * and flag that an update to the modem is needed. 338 * and flag that an update to the modem is needed.
339 * 339 *
340 * FIXME: do we need to kick the tranfers when we do this ? 340 * FIXME: do we need to kick the tranfers when we do this ?
341 */ 341 */
342 static int ifx_spi_tiocmset(struct tty_struct *tty, 342 static int ifx_spi_tiocmset(struct tty_struct *tty,
343 unsigned int set, unsigned int clear) 343 unsigned int set, unsigned int clear)
344 { 344 {
345 struct ifx_spi_device *ifx_dev = tty->driver_data; 345 struct ifx_spi_device *ifx_dev = tty->driver_data;
346 346
347 if (set & TIOCM_RTS) 347 if (set & TIOCM_RTS)
348 set_bit(IFX_SPI_RTS, &ifx_dev->signal_state); 348 set_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
349 if (set & TIOCM_DTR) 349 if (set & TIOCM_DTR)
350 set_bit(IFX_SPI_DTR, &ifx_dev->signal_state); 350 set_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
351 if (clear & TIOCM_RTS) 351 if (clear & TIOCM_RTS)
352 clear_bit(IFX_SPI_RTS, &ifx_dev->signal_state); 352 clear_bit(IFX_SPI_RTS, &ifx_dev->signal_state);
353 if (clear & TIOCM_DTR) 353 if (clear & TIOCM_DTR)
354 clear_bit(IFX_SPI_DTR, &ifx_dev->signal_state); 354 clear_bit(IFX_SPI_DTR, &ifx_dev->signal_state);
355 355
356 set_bit(IFX_SPI_UPDATE, &ifx_dev->signal_state); 356 set_bit(IFX_SPI_UPDATE, &ifx_dev->signal_state);
357 return 0; 357 return 0;
358 } 358 }
359 359
360 /** 360 /**
361 * ifx_spi_open - called on tty open 361 * ifx_spi_open - called on tty open
362 * @tty: our tty device 362 * @tty: our tty device
363 * @filp: file handle being associated with the tty 363 * @filp: file handle being associated with the tty
364 * 364 *
365 * Open the tty interface. We let the tty_port layer do all the work 365 * Open the tty interface. We let the tty_port layer do all the work
366 * for us. 366 * for us.
367 * 367 *
368 * FIXME: Remove single device assumption and saved_ifx_dev 368 * FIXME: Remove single device assumption and saved_ifx_dev
369 */ 369 */
370 static int ifx_spi_open(struct tty_struct *tty, struct file *filp) 370 static int ifx_spi_open(struct tty_struct *tty, struct file *filp)
371 { 371 {
372 return tty_port_open(&saved_ifx_dev->tty_port, tty, filp); 372 return tty_port_open(&saved_ifx_dev->tty_port, tty, filp);
373 } 373 }
374 374
375 /** 375 /**
376 * ifx_spi_close - called when our tty closes 376 * ifx_spi_close - called when our tty closes
377 * @tty: the tty being closed 377 * @tty: the tty being closed
378 * @filp: the file handle being closed 378 * @filp: the file handle being closed
379 * 379 *
380 * Perform the close of the tty. We use the tty_port layer to do all 380 * Perform the close of the tty. We use the tty_port layer to do all
381 * our hard work. 381 * our hard work.
382 */ 382 */
383 static void ifx_spi_close(struct tty_struct *tty, struct file *filp) 383 static void ifx_spi_close(struct tty_struct *tty, struct file *filp)
384 { 384 {
385 struct ifx_spi_device *ifx_dev = tty->driver_data; 385 struct ifx_spi_device *ifx_dev = tty->driver_data;
386 tty_port_close(&ifx_dev->tty_port, tty, filp); 386 tty_port_close(&ifx_dev->tty_port, tty, filp);
387 /* FIXME: should we do an ifx_spi_reset here ? */ 387 /* FIXME: should we do an ifx_spi_reset here ? */
388 } 388 }
389 389
390 /** 390 /**
391 * ifx_decode_spi_header - decode received header 391 * ifx_decode_spi_header - decode received header
392 * @buffer: the received data 392 * @buffer: the received data
393 * @length: decoded length 393 * @length: decoded length
394 * @more: decoded more flag 394 * @more: decoded more flag
395 * @received_cts: status of cts we received 395 * @received_cts: status of cts we received
396 * 396 *
397 * Note how received_cts is handled -- if header is all F it is left 397 * Note how received_cts is handled -- if header is all F it is left
398 * the same as it was, if header is all 0 it is set to 0 otherwise it is 398 * the same as it was, if header is all 0 it is set to 0 otherwise it is
399 * taken from the incoming header. 399 * taken from the incoming header.
400 * 400 *
401 * FIXME: endianness 401 * FIXME: endianness
402 */ 402 */
403 static int ifx_spi_decode_spi_header(unsigned char *buffer, int *length, 403 static int ifx_spi_decode_spi_header(unsigned char *buffer, int *length,
404 unsigned char *more, unsigned char *received_cts) 404 unsigned char *more, unsigned char *received_cts)
405 { 405 {
406 u16 h1; 406 u16 h1;
407 u16 h2; 407 u16 h2;
408 u16 *in_buffer = (u16 *)buffer; 408 u16 *in_buffer = (u16 *)buffer;
409 409
410 h1 = *in_buffer; 410 h1 = *in_buffer;
411 h2 = *(in_buffer+1); 411 h2 = *(in_buffer+1);
412 412
413 if (h1 == 0 && h2 == 0) { 413 if (h1 == 0 && h2 == 0) {
414 *received_cts = 0; 414 *received_cts = 0;
415 return IFX_SPI_HEADER_0; 415 return IFX_SPI_HEADER_0;
416 } else if (h1 == 0xffff && h2 == 0xffff) { 416 } else if (h1 == 0xffff && h2 == 0xffff) {
417 /* spi_slave_cts remains as it was */ 417 /* spi_slave_cts remains as it was */
418 return IFX_SPI_HEADER_F; 418 return IFX_SPI_HEADER_F;
419 } 419 }
420 420
421 *length = h1 & 0xfff; /* upper bits of byte are flags */ 421 *length = h1 & 0xfff; /* upper bits of byte are flags */
422 *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1; 422 *more = (buffer[1] >> IFX_SPI_MORE_BIT) & 1;
423 *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1; 423 *received_cts = (buffer[3] >> IFX_SPI_CTS_BIT) & 1;
424 return 0; 424 return 0;
425 } 425 }
426 426
427 /** 427 /**
428 * ifx_setup_spi_header - set header fields 428 * ifx_setup_spi_header - set header fields
429 * @txbuffer: pointer to start of SPI buffer 429 * @txbuffer: pointer to start of SPI buffer
430 * @tx_count: bytes 430 * @tx_count: bytes
431 * @more: indicate if more to follow 431 * @more: indicate if more to follow
432 * 432 *
433 * Format up an SPI header for a transfer 433 * Format up an SPI header for a transfer
434 * 434 *
435 * FIXME: endianness? 435 * FIXME: endianness?
436 */ 436 */
437 static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count, 437 static void ifx_spi_setup_spi_header(unsigned char *txbuffer, int tx_count,
438 unsigned char more) 438 unsigned char more)
439 { 439 {
440 *(u16 *)(txbuffer) = tx_count; 440 *(u16 *)(txbuffer) = tx_count;
441 *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE; 441 *(u16 *)(txbuffer+2) = IFX_SPI_PAYLOAD_SIZE;
442 txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK; 442 txbuffer[1] |= (more << IFX_SPI_MORE_BIT) & IFX_SPI_MORE_MASK;
443 } 443 }
444 444
445 /** 445 /**
446 * ifx_spi_wakeup_serial - SPI space made
447 * @port_data: our SPI device
448 *
449 * We have emptied the FIFO enough that we want to get more data
450 * queued into it. Poke the line discipline via tty_wakeup so that
451 * it will feed us more bits
452 */
453 static void ifx_spi_wakeup_serial(struct ifx_spi_device *ifx_dev)
454 {
455 struct tty_struct *tty;
456
457 tty = tty_port_tty_get(&ifx_dev->tty_port);
458 if (!tty)
459 return;
460 tty_wakeup(tty);
461 tty_kref_put(tty);
462 }
463
464 /**
465 * ifx_spi_prepare_tx_buffer - prepare transmit frame 446 * ifx_spi_prepare_tx_buffer - prepare transmit frame
466 * @ifx_dev: our SPI device 447 * @ifx_dev: our SPI device
467 * 448 *
468 * The transmit buffr needs a header and various other bits of 449 * The transmit buffr needs a header and various other bits of
469 * information followed by as much data as we can pull from the FIFO 450 * information followed by as much data as we can pull from the FIFO
470 * and transfer. This function formats up a suitable buffer in the 451 * and transfer. This function formats up a suitable buffer in the
471 * ifx_dev->tx_buffer 452 * ifx_dev->tx_buffer
472 * 453 *
473 * FIXME: performance - should we wake the tty when the queue is half 454 * FIXME: performance - should we wake the tty when the queue is half
474 * empty ? 455 * empty ?
475 */ 456 */
476 static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev) 457 static int ifx_spi_prepare_tx_buffer(struct ifx_spi_device *ifx_dev)
477 { 458 {
478 int temp_count; 459 int temp_count;
479 int queue_length; 460 int queue_length;
480 int tx_count; 461 int tx_count;
481 unsigned char *tx_buffer; 462 unsigned char *tx_buffer;
482 463
483 tx_buffer = ifx_dev->tx_buffer; 464 tx_buffer = ifx_dev->tx_buffer;
484 465
485 /* make room for required SPI header */ 466 /* make room for required SPI header */
486 tx_buffer += IFX_SPI_HEADER_OVERHEAD; 467 tx_buffer += IFX_SPI_HEADER_OVERHEAD;
487 tx_count = IFX_SPI_HEADER_OVERHEAD; 468 tx_count = IFX_SPI_HEADER_OVERHEAD;
488 469
489 /* clear to signal no more data if this turns out to be the 470 /* clear to signal no more data if this turns out to be the
490 * last buffer sent in a sequence */ 471 * last buffer sent in a sequence */
491 ifx_dev->spi_more = 0; 472 ifx_dev->spi_more = 0;
492 473
493 /* if modem cts is set, just send empty buffer */ 474 /* if modem cts is set, just send empty buffer */
494 if (!ifx_dev->spi_slave_cts) { 475 if (!ifx_dev->spi_slave_cts) {
495 /* see if there's tx data */ 476 /* see if there's tx data */
496 queue_length = kfifo_len(&ifx_dev->tx_fifo); 477 queue_length = kfifo_len(&ifx_dev->tx_fifo);
497 if (queue_length != 0) { 478 if (queue_length != 0) {
498 /* data to mux -- see if there's room for it */ 479 /* data to mux -- see if there's room for it */
499 temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE); 480 temp_count = min(queue_length, IFX_SPI_PAYLOAD_SIZE);
500 temp_count = kfifo_out_locked(&ifx_dev->tx_fifo, 481 temp_count = kfifo_out_locked(&ifx_dev->tx_fifo,
501 tx_buffer, temp_count, 482 tx_buffer, temp_count,
502 &ifx_dev->fifo_lock); 483 &ifx_dev->fifo_lock);
503 484
504 /* update buffer pointer and data count in message */ 485 /* update buffer pointer and data count in message */
505 tx_buffer += temp_count; 486 tx_buffer += temp_count;
506 tx_count += temp_count; 487 tx_count += temp_count;
507 if (temp_count == queue_length) 488 if (temp_count == queue_length)
508 /* poke port to get more data */ 489 /* poke port to get more data */
509 ifx_spi_wakeup_serial(ifx_dev); 490 tty_port_tty_wakeup(&ifx_dev->tty_port);
510 else /* more data in port, use next SPI message */ 491 else /* more data in port, use next SPI message */
511 ifx_dev->spi_more = 1; 492 ifx_dev->spi_more = 1;
512 } 493 }
513 } 494 }
514 /* have data and info for header -- set up SPI header in buffer */ 495 /* have data and info for header -- set up SPI header in buffer */
515 /* spi header needs payload size, not entire buffer size */ 496 /* spi header needs payload size, not entire buffer size */
516 ifx_spi_setup_spi_header(ifx_dev->tx_buffer, 497 ifx_spi_setup_spi_header(ifx_dev->tx_buffer,
517 tx_count-IFX_SPI_HEADER_OVERHEAD, 498 tx_count-IFX_SPI_HEADER_OVERHEAD,
518 ifx_dev->spi_more); 499 ifx_dev->spi_more);
519 /* swap actual data in the buffer */ 500 /* swap actual data in the buffer */
520 ifx_dev->swap_buf((ifx_dev->tx_buffer), tx_count, 501 ifx_dev->swap_buf((ifx_dev->tx_buffer), tx_count,
521 &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]); 502 &ifx_dev->tx_buffer[IFX_SPI_TRANSFER_SIZE]);
522 return tx_count; 503 return tx_count;
523 } 504 }
524 505
525 /** 506 /**
526 * ifx_spi_write - line discipline write 507 * ifx_spi_write - line discipline write
527 * @tty: our tty device 508 * @tty: our tty device
528 * @buf: pointer to buffer to write (kernel space) 509 * @buf: pointer to buffer to write (kernel space)
529 * @count: size of buffer 510 * @count: size of buffer
530 * 511 *
531 * Write the characters we have been given into the FIFO. If the device 512 * Write the characters we have been given into the FIFO. If the device
532 * is not active then activate it, when the SRDY line is asserted back 513 * is not active then activate it, when the SRDY line is asserted back
533 * this will commence I/O 514 * this will commence I/O
534 */ 515 */
535 static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf, 516 static int ifx_spi_write(struct tty_struct *tty, const unsigned char *buf,
536 int count) 517 int count)
537 { 518 {
538 struct ifx_spi_device *ifx_dev = tty->driver_data; 519 struct ifx_spi_device *ifx_dev = tty->driver_data;
539 unsigned char *tmp_buf = (unsigned char *)buf; 520 unsigned char *tmp_buf = (unsigned char *)buf;
540 unsigned long flags; 521 unsigned long flags;
541 bool is_fifo_empty; 522 bool is_fifo_empty;
542 int tx_count; 523 int tx_count;
543 524
544 spin_lock_irqsave(&ifx_dev->fifo_lock, flags); 525 spin_lock_irqsave(&ifx_dev->fifo_lock, flags);
545 is_fifo_empty = kfifo_is_empty(&ifx_dev->tx_fifo); 526 is_fifo_empty = kfifo_is_empty(&ifx_dev->tx_fifo);
546 tx_count = kfifo_in(&ifx_dev->tx_fifo, tmp_buf, count); 527 tx_count = kfifo_in(&ifx_dev->tx_fifo, tmp_buf, count);
547 spin_unlock_irqrestore(&ifx_dev->fifo_lock, flags); 528 spin_unlock_irqrestore(&ifx_dev->fifo_lock, flags);
548 if (is_fifo_empty) 529 if (is_fifo_empty)
549 mrdy_assert(ifx_dev); 530 mrdy_assert(ifx_dev);
550 531
551 return tx_count; 532 return tx_count;
552 } 533 }
553 534
554 /** 535 /**
555 * ifx_spi_chars_in_buffer - line discipline helper 536 * ifx_spi_chars_in_buffer - line discipline helper
556 * @tty: our tty device 537 * @tty: our tty device
557 * 538 *
558 * Report how much data we can accept before we drop bytes. As we use 539 * Report how much data we can accept before we drop bytes. As we use
559 * a simple FIFO this is nice and easy. 540 * a simple FIFO this is nice and easy.
560 */ 541 */
561 static int ifx_spi_write_room(struct tty_struct *tty) 542 static int ifx_spi_write_room(struct tty_struct *tty)
562 { 543 {
563 struct ifx_spi_device *ifx_dev = tty->driver_data; 544 struct ifx_spi_device *ifx_dev = tty->driver_data;
564 return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo); 545 return IFX_SPI_FIFO_SIZE - kfifo_len(&ifx_dev->tx_fifo);
565 } 546 }
566 547
567 /** 548 /**
568 * ifx_spi_chars_in_buffer - line discipline helper 549 * ifx_spi_chars_in_buffer - line discipline helper
569 * @tty: our tty device 550 * @tty: our tty device
570 * 551 *
571 * Report how many characters we have buffered. In our case this is the 552 * Report how many characters we have buffered. In our case this is the
572 * number of bytes sitting in our transmit FIFO. 553 * number of bytes sitting in our transmit FIFO.
573 */ 554 */
574 static int ifx_spi_chars_in_buffer(struct tty_struct *tty) 555 static int ifx_spi_chars_in_buffer(struct tty_struct *tty)
575 { 556 {
576 struct ifx_spi_device *ifx_dev = tty->driver_data; 557 struct ifx_spi_device *ifx_dev = tty->driver_data;
577 return kfifo_len(&ifx_dev->tx_fifo); 558 return kfifo_len(&ifx_dev->tx_fifo);
578 } 559 }
579 560
580 /** 561 /**
581 * ifx_port_hangup 562 * ifx_port_hangup
582 * @port: our tty port 563 * @port: our tty port
583 * 564 *
584 * tty port hang up. Called when tty_hangup processing is invoked either 565 * tty port hang up. Called when tty_hangup processing is invoked either
585 * by loss of carrier, or by software (eg vhangup). Serialized against 566 * by loss of carrier, or by software (eg vhangup). Serialized against
586 * activate/shutdown by the tty layer. 567 * activate/shutdown by the tty layer.
587 */ 568 */
588 static void ifx_spi_hangup(struct tty_struct *tty) 569 static void ifx_spi_hangup(struct tty_struct *tty)
589 { 570 {
590 struct ifx_spi_device *ifx_dev = tty->driver_data; 571 struct ifx_spi_device *ifx_dev = tty->driver_data;
591 tty_port_hangup(&ifx_dev->tty_port); 572 tty_port_hangup(&ifx_dev->tty_port);
592 } 573 }
593 574
594 /** 575 /**
595 * ifx_port_activate 576 * ifx_port_activate
596 * @port: our tty port 577 * @port: our tty port
597 * 578 *
598 * tty port activate method - called for first open. Serialized 579 * tty port activate method - called for first open. Serialized
599 * with hangup and shutdown by the tty layer. 580 * with hangup and shutdown by the tty layer.
600 */ 581 */
601 static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty) 582 static int ifx_port_activate(struct tty_port *port, struct tty_struct *tty)
602 { 583 {
603 struct ifx_spi_device *ifx_dev = 584 struct ifx_spi_device *ifx_dev =
604 container_of(port, struct ifx_spi_device, tty_port); 585 container_of(port, struct ifx_spi_device, tty_port);
605 586
606 /* clear any old data; can't do this in 'close' */ 587 /* clear any old data; can't do this in 'close' */
607 kfifo_reset(&ifx_dev->tx_fifo); 588 kfifo_reset(&ifx_dev->tx_fifo);
608 589
609 /* clear any flag which may be set in port shutdown procedure */ 590 /* clear any flag which may be set in port shutdown procedure */
610 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags); 591 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
611 clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags); 592 clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
612 593
613 /* put port data into this tty */ 594 /* put port data into this tty */
614 tty->driver_data = ifx_dev; 595 tty->driver_data = ifx_dev;
615 596
616 /* allows flip string push from int context */ 597 /* allows flip string push from int context */
617 port->low_latency = 1; 598 port->low_latency = 1;
618 599
619 /* set flag to allows data transfer */ 600 /* set flag to allows data transfer */
620 set_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags); 601 set_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
621 602
622 return 0; 603 return 0;
623 } 604 }
624 605
625 /** 606 /**
626 * ifx_port_shutdown 607 * ifx_port_shutdown
627 * @port: our tty port 608 * @port: our tty port
628 * 609 *
629 * tty port shutdown method - called for last port close. Serialized 610 * tty port shutdown method - called for last port close. Serialized
630 * with hangup and activate by the tty layer. 611 * with hangup and activate by the tty layer.
631 */ 612 */
632 static void ifx_port_shutdown(struct tty_port *port) 613 static void ifx_port_shutdown(struct tty_port *port)
633 { 614 {
634 struct ifx_spi_device *ifx_dev = 615 struct ifx_spi_device *ifx_dev =
635 container_of(port, struct ifx_spi_device, tty_port); 616 container_of(port, struct ifx_spi_device, tty_port);
636 617
637 clear_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags); 618 clear_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags);
638 mrdy_set_low(ifx_dev); 619 mrdy_set_low(ifx_dev);
639 del_timer(&ifx_dev->spi_timer); 620 del_timer(&ifx_dev->spi_timer);
640 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags); 621 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
641 tasklet_kill(&ifx_dev->io_work_tasklet); 622 tasklet_kill(&ifx_dev->io_work_tasklet);
642 } 623 }
643 624
644 static const struct tty_port_operations ifx_tty_port_ops = { 625 static const struct tty_port_operations ifx_tty_port_ops = {
645 .activate = ifx_port_activate, 626 .activate = ifx_port_activate,
646 .shutdown = ifx_port_shutdown, 627 .shutdown = ifx_port_shutdown,
647 }; 628 };
648 629
649 static const struct tty_operations ifx_spi_serial_ops = { 630 static const struct tty_operations ifx_spi_serial_ops = {
650 .open = ifx_spi_open, 631 .open = ifx_spi_open,
651 .close = ifx_spi_close, 632 .close = ifx_spi_close,
652 .write = ifx_spi_write, 633 .write = ifx_spi_write,
653 .hangup = ifx_spi_hangup, 634 .hangup = ifx_spi_hangup,
654 .write_room = ifx_spi_write_room, 635 .write_room = ifx_spi_write_room,
655 .chars_in_buffer = ifx_spi_chars_in_buffer, 636 .chars_in_buffer = ifx_spi_chars_in_buffer,
656 .tiocmget = ifx_spi_tiocmget, 637 .tiocmget = ifx_spi_tiocmget,
657 .tiocmset = ifx_spi_tiocmset, 638 .tiocmset = ifx_spi_tiocmset,
658 }; 639 };
659 640
660 /** 641 /**
661 * ifx_spi_insert_fip_string - queue received data 642 * ifx_spi_insert_fip_string - queue received data
662 * @ifx_ser: our SPI device 643 * @ifx_ser: our SPI device
663 * @chars: buffer we have received 644 * @chars: buffer we have received
664 * @size: number of chars reeived 645 * @size: number of chars reeived
665 * 646 *
666 * Queue bytes to the tty assuming the tty side is currently open. If 647 * Queue bytes to the tty assuming the tty side is currently open. If
667 * not the discard the data. 648 * not the discard the data.
668 */ 649 */
669 static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev, 650 static void ifx_spi_insert_flip_string(struct ifx_spi_device *ifx_dev,
670 unsigned char *chars, size_t size) 651 unsigned char *chars, size_t size)
671 { 652 {
672 tty_insert_flip_string(&ifx_dev->tty_port, chars, size); 653 tty_insert_flip_string(&ifx_dev->tty_port, chars, size);
673 tty_flip_buffer_push(&ifx_dev->tty_port); 654 tty_flip_buffer_push(&ifx_dev->tty_port);
674 } 655 }
675 656
676 /** 657 /**
677 * ifx_spi_complete - SPI transfer completed 658 * ifx_spi_complete - SPI transfer completed
678 * @ctx: our SPI device 659 * @ctx: our SPI device
679 * 660 *
680 * An SPI transfer has completed. Process any received data and kick off 661 * An SPI transfer has completed. Process any received data and kick off
681 * any further transmits we can commence. 662 * any further transmits we can commence.
682 */ 663 */
683 static void ifx_spi_complete(void *ctx) 664 static void ifx_spi_complete(void *ctx)
684 { 665 {
685 struct ifx_spi_device *ifx_dev = ctx; 666 struct ifx_spi_device *ifx_dev = ctx;
686 struct tty_struct *tty;
687 struct tty_ldisc *ldisc = NULL;
688 int length; 667 int length;
689 int actual_length; 668 int actual_length;
690 unsigned char more; 669 unsigned char more;
691 unsigned char cts; 670 unsigned char cts;
692 int local_write_pending = 0; 671 int local_write_pending = 0;
693 int queue_length; 672 int queue_length;
694 int srdy; 673 int srdy;
695 int decode_result; 674 int decode_result;
696 675
697 mrdy_set_low(ifx_dev); 676 mrdy_set_low(ifx_dev);
698 677
699 if (!ifx_dev->spi_msg.status) { 678 if (!ifx_dev->spi_msg.status) {
700 /* check header validity, get comm flags */ 679 /* check header validity, get comm flags */
701 ifx_dev->swap_buf(ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD, 680 ifx_dev->swap_buf(ifx_dev->rx_buffer, IFX_SPI_HEADER_OVERHEAD,
702 &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]); 681 &ifx_dev->rx_buffer[IFX_SPI_HEADER_OVERHEAD]);
703 decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer, 682 decode_result = ifx_spi_decode_spi_header(ifx_dev->rx_buffer,
704 &length, &more, &cts); 683 &length, &more, &cts);
705 if (decode_result == IFX_SPI_HEADER_0) { 684 if (decode_result == IFX_SPI_HEADER_0) {
706 dev_dbg(&ifx_dev->spi_dev->dev, 685 dev_dbg(&ifx_dev->spi_dev->dev,
707 "ignore input: invalid header 0"); 686 "ignore input: invalid header 0");
708 ifx_dev->spi_slave_cts = 0; 687 ifx_dev->spi_slave_cts = 0;
709 goto complete_exit; 688 goto complete_exit;
710 } else if (decode_result == IFX_SPI_HEADER_F) { 689 } else if (decode_result == IFX_SPI_HEADER_F) {
711 dev_dbg(&ifx_dev->spi_dev->dev, 690 dev_dbg(&ifx_dev->spi_dev->dev,
712 "ignore input: invalid header F"); 691 "ignore input: invalid header F");
713 goto complete_exit; 692 goto complete_exit;
714 } 693 }
715 694
716 ifx_dev->spi_slave_cts = cts; 695 ifx_dev->spi_slave_cts = cts;
717 696
718 actual_length = min((unsigned int)length, 697 actual_length = min((unsigned int)length,
719 ifx_dev->spi_msg.actual_length); 698 ifx_dev->spi_msg.actual_length);
720 ifx_dev->swap_buf( 699 ifx_dev->swap_buf(
721 (ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD), 700 (ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD),
722 actual_length, 701 actual_length,
723 &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]); 702 &ifx_dev->rx_buffer[IFX_SPI_TRANSFER_SIZE]);
724 ifx_spi_insert_flip_string( 703 ifx_spi_insert_flip_string(
725 ifx_dev, 704 ifx_dev,
726 ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD, 705 ifx_dev->rx_buffer + IFX_SPI_HEADER_OVERHEAD,
727 (size_t)actual_length); 706 (size_t)actual_length);
728 } else { 707 } else {
729 dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d", 708 dev_dbg(&ifx_dev->spi_dev->dev, "SPI transfer error %d",
730 ifx_dev->spi_msg.status); 709 ifx_dev->spi_msg.status);
731 } 710 }
732 711
733 complete_exit: 712 complete_exit:
734 if (ifx_dev->write_pending) { 713 if (ifx_dev->write_pending) {
735 ifx_dev->write_pending = 0; 714 ifx_dev->write_pending = 0;
736 local_write_pending = 1; 715 local_write_pending = 1;
737 } 716 }
738 717
739 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags)); 718 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &(ifx_dev->flags));
740 719
741 queue_length = kfifo_len(&ifx_dev->tx_fifo); 720 queue_length = kfifo_len(&ifx_dev->tx_fifo);
742 srdy = gpio_get_value(ifx_dev->gpio.srdy); 721 srdy = gpio_get_value(ifx_dev->gpio.srdy);
743 if (!srdy) 722 if (!srdy)
744 ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY); 723 ifx_spi_power_state_clear(ifx_dev, IFX_SPI_POWER_SRDY);
745 724
746 /* schedule output if there is more to do */ 725 /* schedule output if there is more to do */
747 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags)) 726 if (test_and_clear_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags))
748 tasklet_schedule(&ifx_dev->io_work_tasklet); 727 tasklet_schedule(&ifx_dev->io_work_tasklet);
749 else { 728 else {
750 if (more || ifx_dev->spi_more || queue_length > 0 || 729 if (more || ifx_dev->spi_more || queue_length > 0 ||
751 local_write_pending) { 730 local_write_pending) {
752 if (ifx_dev->spi_slave_cts) { 731 if (ifx_dev->spi_slave_cts) {
753 if (more) 732 if (more)
754 mrdy_assert(ifx_dev); 733 mrdy_assert(ifx_dev);
755 } else 734 } else
756 mrdy_assert(ifx_dev); 735 mrdy_assert(ifx_dev);
757 } else { 736 } else {
758 /* 737 /*
759 * poke line discipline driver if any for more data 738 * poke line discipline driver if any for more data
760 * may or may not get more data to write 739 * may or may not get more data to write
761 * for now, say not busy 740 * for now, say not busy
762 */ 741 */
763 ifx_spi_power_state_clear(ifx_dev, 742 ifx_spi_power_state_clear(ifx_dev,
764 IFX_SPI_POWER_DATA_PENDING); 743 IFX_SPI_POWER_DATA_PENDING);
765 tty = tty_port_tty_get(&ifx_dev->tty_port); 744 tty_port_tty_wakeup(&ifx_dev->tty_port);
766 if (tty) {
767 ldisc = tty_ldisc_ref(tty);
768 if (ldisc) {
769 ldisc->ops->write_wakeup(tty);
770 tty_ldisc_deref(ldisc);
771 }
772 tty_kref_put(tty);
773 }
774 } 745 }
775 } 746 }
776 } 747 }
777 748
778 /** 749 /**
779 * ifx_spio_io - I/O tasklet 750 * ifx_spio_io - I/O tasklet
780 * @data: our SPI device 751 * @data: our SPI device
781 * 752 *
782 * Queue data for transmission if possible and then kick off the 753 * Queue data for transmission if possible and then kick off the
783 * transfer. 754 * transfer.
784 */ 755 */
785 static void ifx_spi_io(unsigned long data) 756 static void ifx_spi_io(unsigned long data)
786 { 757 {
787 int retval; 758 int retval;
788 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data; 759 struct ifx_spi_device *ifx_dev = (struct ifx_spi_device *) data;
789 760
790 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags) && 761 if (!test_and_set_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags) &&
791 test_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags)) { 762 test_bit(IFX_SPI_STATE_IO_AVAILABLE, &ifx_dev->flags)) {
792 if (ifx_dev->gpio.unack_srdy_int_nb > 0) 763 if (ifx_dev->gpio.unack_srdy_int_nb > 0)
793 ifx_dev->gpio.unack_srdy_int_nb--; 764 ifx_dev->gpio.unack_srdy_int_nb--;
794 765
795 ifx_spi_prepare_tx_buffer(ifx_dev); 766 ifx_spi_prepare_tx_buffer(ifx_dev);
796 767
797 spi_message_init(&ifx_dev->spi_msg); 768 spi_message_init(&ifx_dev->spi_msg);
798 INIT_LIST_HEAD(&ifx_dev->spi_msg.queue); 769 INIT_LIST_HEAD(&ifx_dev->spi_msg.queue);
799 770
800 ifx_dev->spi_msg.context = ifx_dev; 771 ifx_dev->spi_msg.context = ifx_dev;
801 ifx_dev->spi_msg.complete = ifx_spi_complete; 772 ifx_dev->spi_msg.complete = ifx_spi_complete;
802 773
803 /* set up our spi transfer */ 774 /* set up our spi transfer */
804 /* note len is BYTES, not transfers */ 775 /* note len is BYTES, not transfers */
805 ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE; 776 ifx_dev->spi_xfer.len = IFX_SPI_TRANSFER_SIZE;
806 ifx_dev->spi_xfer.cs_change = 0; 777 ifx_dev->spi_xfer.cs_change = 0;
807 ifx_dev->spi_xfer.speed_hz = ifx_dev->spi_dev->max_speed_hz; 778 ifx_dev->spi_xfer.speed_hz = ifx_dev->spi_dev->max_speed_hz;
808 /* ifx_dev->spi_xfer.speed_hz = 390625; */ 779 /* ifx_dev->spi_xfer.speed_hz = 390625; */
809 ifx_dev->spi_xfer.bits_per_word = 780 ifx_dev->spi_xfer.bits_per_word =
810 ifx_dev->spi_dev->bits_per_word; 781 ifx_dev->spi_dev->bits_per_word;
811 782
812 ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer; 783 ifx_dev->spi_xfer.tx_buf = ifx_dev->tx_buffer;
813 ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer; 784 ifx_dev->spi_xfer.rx_buf = ifx_dev->rx_buffer;
814 785
815 /* 786 /*
816 * setup dma pointers 787 * setup dma pointers
817 */ 788 */
818 if (ifx_dev->use_dma) { 789 if (ifx_dev->use_dma) {
819 ifx_dev->spi_msg.is_dma_mapped = 1; 790 ifx_dev->spi_msg.is_dma_mapped = 1;
820 ifx_dev->tx_dma = ifx_dev->tx_bus; 791 ifx_dev->tx_dma = ifx_dev->tx_bus;
821 ifx_dev->rx_dma = ifx_dev->rx_bus; 792 ifx_dev->rx_dma = ifx_dev->rx_bus;
822 ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma; 793 ifx_dev->spi_xfer.tx_dma = ifx_dev->tx_dma;
823 ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma; 794 ifx_dev->spi_xfer.rx_dma = ifx_dev->rx_dma;
824 } else { 795 } else {
825 ifx_dev->spi_msg.is_dma_mapped = 0; 796 ifx_dev->spi_msg.is_dma_mapped = 0;
826 ifx_dev->tx_dma = (dma_addr_t)0; 797 ifx_dev->tx_dma = (dma_addr_t)0;
827 ifx_dev->rx_dma = (dma_addr_t)0; 798 ifx_dev->rx_dma = (dma_addr_t)0;
828 ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0; 799 ifx_dev->spi_xfer.tx_dma = (dma_addr_t)0;
829 ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0; 800 ifx_dev->spi_xfer.rx_dma = (dma_addr_t)0;
830 } 801 }
831 802
832 spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg); 803 spi_message_add_tail(&ifx_dev->spi_xfer, &ifx_dev->spi_msg);
833 804
834 /* Assert MRDY. This may have already been done by the write 805 /* Assert MRDY. This may have already been done by the write
835 * routine. 806 * routine.
836 */ 807 */
837 mrdy_assert(ifx_dev); 808 mrdy_assert(ifx_dev);
838 809
839 retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg); 810 retval = spi_async(ifx_dev->spi_dev, &ifx_dev->spi_msg);
840 if (retval) { 811 if (retval) {
841 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, 812 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS,
842 &ifx_dev->flags); 813 &ifx_dev->flags);
843 tasklet_schedule(&ifx_dev->io_work_tasklet); 814 tasklet_schedule(&ifx_dev->io_work_tasklet);
844 return; 815 return;
845 } 816 }
846 } else 817 } else
847 ifx_dev->write_pending = 1; 818 ifx_dev->write_pending = 1;
848 } 819 }
849 820
850 /** 821 /**
851 * ifx_spi_free_port - free up the tty side 822 * ifx_spi_free_port - free up the tty side
852 * @ifx_dev: IFX device going away 823 * @ifx_dev: IFX device going away
853 * 824 *
854 * Unregister and free up a port when the device goes away 825 * Unregister and free up a port when the device goes away
855 */ 826 */
856 static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev) 827 static void ifx_spi_free_port(struct ifx_spi_device *ifx_dev)
857 { 828 {
858 if (ifx_dev->tty_dev) 829 if (ifx_dev->tty_dev)
859 tty_unregister_device(tty_drv, ifx_dev->minor); 830 tty_unregister_device(tty_drv, ifx_dev->minor);
860 tty_port_destroy(&ifx_dev->tty_port); 831 tty_port_destroy(&ifx_dev->tty_port);
861 kfifo_free(&ifx_dev->tx_fifo); 832 kfifo_free(&ifx_dev->tx_fifo);
862 } 833 }
863 834
864 /** 835 /**
865 * ifx_spi_create_port - create a new port 836 * ifx_spi_create_port - create a new port
866 * @ifx_dev: our spi device 837 * @ifx_dev: our spi device
867 * 838 *
868 * Allocate and initialise the tty port that goes with this interface 839 * Allocate and initialise the tty port that goes with this interface
869 * and add it to the tty layer so that it can be opened. 840 * and add it to the tty layer so that it can be opened.
870 */ 841 */
871 static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev) 842 static int ifx_spi_create_port(struct ifx_spi_device *ifx_dev)
872 { 843 {
873 int ret = 0; 844 int ret = 0;
874 struct tty_port *pport = &ifx_dev->tty_port; 845 struct tty_port *pport = &ifx_dev->tty_port;
875 846
876 spin_lock_init(&ifx_dev->fifo_lock); 847 spin_lock_init(&ifx_dev->fifo_lock);
877 lockdep_set_class_and_subclass(&ifx_dev->fifo_lock, 848 lockdep_set_class_and_subclass(&ifx_dev->fifo_lock,
878 &ifx_spi_key, 0); 849 &ifx_spi_key, 0);
879 850
880 if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) { 851 if (kfifo_alloc(&ifx_dev->tx_fifo, IFX_SPI_FIFO_SIZE, GFP_KERNEL)) {
881 ret = -ENOMEM; 852 ret = -ENOMEM;
882 goto error_ret; 853 goto error_ret;
883 } 854 }
884 855
885 tty_port_init(pport); 856 tty_port_init(pport);
886 pport->ops = &ifx_tty_port_ops; 857 pport->ops = &ifx_tty_port_ops;
887 ifx_dev->minor = IFX_SPI_TTY_ID; 858 ifx_dev->minor = IFX_SPI_TTY_ID;
888 ifx_dev->tty_dev = tty_port_register_device(pport, tty_drv, 859 ifx_dev->tty_dev = tty_port_register_device(pport, tty_drv,
889 ifx_dev->minor, &ifx_dev->spi_dev->dev); 860 ifx_dev->minor, &ifx_dev->spi_dev->dev);
890 if (IS_ERR(ifx_dev->tty_dev)) { 861 if (IS_ERR(ifx_dev->tty_dev)) {
891 dev_dbg(&ifx_dev->spi_dev->dev, 862 dev_dbg(&ifx_dev->spi_dev->dev,
892 "%s: registering tty device failed", __func__); 863 "%s: registering tty device failed", __func__);
893 ret = PTR_ERR(ifx_dev->tty_dev); 864 ret = PTR_ERR(ifx_dev->tty_dev);
894 goto error_port; 865 goto error_port;
895 } 866 }
896 return 0; 867 return 0;
897 868
898 error_port: 869 error_port:
899 tty_port_destroy(pport); 870 tty_port_destroy(pport);
900 error_ret: 871 error_ret:
901 ifx_spi_free_port(ifx_dev); 872 ifx_spi_free_port(ifx_dev);
902 return ret; 873 return ret;
903 } 874 }
904 875
905 /** 876 /**
906 * ifx_spi_handle_srdy - handle SRDY 877 * ifx_spi_handle_srdy - handle SRDY
907 * @ifx_dev: device asserting SRDY 878 * @ifx_dev: device asserting SRDY
908 * 879 *
909 * Check our device state and see what we need to kick off when SRDY 880 * Check our device state and see what we need to kick off when SRDY
910 * is asserted. This usually means killing the timer and firing off the 881 * is asserted. This usually means killing the timer and firing off the
911 * I/O processing. 882 * I/O processing.
912 */ 883 */
913 static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev) 884 static void ifx_spi_handle_srdy(struct ifx_spi_device *ifx_dev)
914 { 885 {
915 if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) { 886 if (test_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags)) {
916 del_timer(&ifx_dev->spi_timer); 887 del_timer(&ifx_dev->spi_timer);
917 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags); 888 clear_bit(IFX_SPI_STATE_TIMER_PENDING, &ifx_dev->flags);
918 } 889 }
919 890
920 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY); 891 ifx_spi_power_state_set(ifx_dev, IFX_SPI_POWER_SRDY);
921 892
922 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags)) 893 if (!test_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags))
923 tasklet_schedule(&ifx_dev->io_work_tasklet); 894 tasklet_schedule(&ifx_dev->io_work_tasklet);
924 else 895 else
925 set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags); 896 set_bit(IFX_SPI_STATE_IO_READY, &ifx_dev->flags);
926 } 897 }
927 898
928 /** 899 /**
929 * ifx_spi_srdy_interrupt - SRDY asserted 900 * ifx_spi_srdy_interrupt - SRDY asserted
930 * @irq: our IRQ number 901 * @irq: our IRQ number
931 * @dev: our ifx device 902 * @dev: our ifx device
932 * 903 *
933 * The modem asserted SRDY. Handle the srdy event 904 * The modem asserted SRDY. Handle the srdy event
934 */ 905 */
935 static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev) 906 static irqreturn_t ifx_spi_srdy_interrupt(int irq, void *dev)
936 { 907 {
937 struct ifx_spi_device *ifx_dev = dev; 908 struct ifx_spi_device *ifx_dev = dev;
938 ifx_dev->gpio.unack_srdy_int_nb++; 909 ifx_dev->gpio.unack_srdy_int_nb++;
939 ifx_spi_handle_srdy(ifx_dev); 910 ifx_spi_handle_srdy(ifx_dev);
940 return IRQ_HANDLED; 911 return IRQ_HANDLED;
941 } 912 }
942 913
943 /** 914 /**
944 * ifx_spi_reset_interrupt - Modem has changed reset state 915 * ifx_spi_reset_interrupt - Modem has changed reset state
945 * @irq: interrupt number 916 * @irq: interrupt number
946 * @dev: our device pointer 917 * @dev: our device pointer
947 * 918 *
948 * The modem has either entered or left reset state. Check the GPIO 919 * The modem has either entered or left reset state. Check the GPIO
949 * line to see which. 920 * line to see which.
950 * 921 *
951 * FIXME: review locking on MR_INPROGRESS versus 922 * FIXME: review locking on MR_INPROGRESS versus
952 * parallel unsolicited reset/solicited reset 923 * parallel unsolicited reset/solicited reset
953 */ 924 */
954 static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev) 925 static irqreturn_t ifx_spi_reset_interrupt(int irq, void *dev)
955 { 926 {
956 struct ifx_spi_device *ifx_dev = dev; 927 struct ifx_spi_device *ifx_dev = dev;
957 int val = gpio_get_value(ifx_dev->gpio.reset_out); 928 int val = gpio_get_value(ifx_dev->gpio.reset_out);
958 int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state); 929 int solreset = test_bit(MR_START, &ifx_dev->mdm_reset_state);
959 930
960 if (val == 0) { 931 if (val == 0) {
961 /* entered reset */ 932 /* entered reset */
962 set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state); 933 set_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
963 if (!solreset) { 934 if (!solreset) {
964 /* unsolicited reset */ 935 /* unsolicited reset */
965 ifx_spi_ttyhangup(ifx_dev); 936 ifx_spi_ttyhangup(ifx_dev);
966 } 937 }
967 } else { 938 } else {
968 /* exited reset */ 939 /* exited reset */
969 clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state); 940 clear_bit(MR_INPROGRESS, &ifx_dev->mdm_reset_state);
970 if (solreset) { 941 if (solreset) {
971 set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state); 942 set_bit(MR_COMPLETE, &ifx_dev->mdm_reset_state);
972 wake_up(&ifx_dev->mdm_reset_wait); 943 wake_up(&ifx_dev->mdm_reset_wait);
973 } 944 }
974 } 945 }
975 return IRQ_HANDLED; 946 return IRQ_HANDLED;
976 } 947 }
977 948
978 /** 949 /**
979 * ifx_spi_free_device - free device 950 * ifx_spi_free_device - free device
980 * @ifx_dev: device to free 951 * @ifx_dev: device to free
981 * 952 *
982 * Free the IFX device 953 * Free the IFX device
983 */ 954 */
984 static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev) 955 static void ifx_spi_free_device(struct ifx_spi_device *ifx_dev)
985 { 956 {
986 ifx_spi_free_port(ifx_dev); 957 ifx_spi_free_port(ifx_dev);
987 dma_free_coherent(&ifx_dev->spi_dev->dev, 958 dma_free_coherent(&ifx_dev->spi_dev->dev,
988 IFX_SPI_TRANSFER_SIZE, 959 IFX_SPI_TRANSFER_SIZE,
989 ifx_dev->tx_buffer, 960 ifx_dev->tx_buffer,
990 ifx_dev->tx_bus); 961 ifx_dev->tx_bus);
991 dma_free_coherent(&ifx_dev->spi_dev->dev, 962 dma_free_coherent(&ifx_dev->spi_dev->dev,
992 IFX_SPI_TRANSFER_SIZE, 963 IFX_SPI_TRANSFER_SIZE,
993 ifx_dev->rx_buffer, 964 ifx_dev->rx_buffer,
994 ifx_dev->rx_bus); 965 ifx_dev->rx_bus);
995 } 966 }
996 967
997 /** 968 /**
998 * ifx_spi_reset - reset modem 969 * ifx_spi_reset - reset modem
999 * @ifx_dev: modem to reset 970 * @ifx_dev: modem to reset
1000 * 971 *
1001 * Perform a reset on the modem 972 * Perform a reset on the modem
1002 */ 973 */
1003 static int ifx_spi_reset(struct ifx_spi_device *ifx_dev) 974 static int ifx_spi_reset(struct ifx_spi_device *ifx_dev)
1004 { 975 {
1005 int ret; 976 int ret;
1006 /* 977 /*
1007 * set up modem power, reset 978 * set up modem power, reset
1008 * 979 *
1009 * delays are required on some platforms for the modem 980 * delays are required on some platforms for the modem
1010 * to reset properly 981 * to reset properly
1011 */ 982 */
1012 set_bit(MR_START, &ifx_dev->mdm_reset_state); 983 set_bit(MR_START, &ifx_dev->mdm_reset_state);
1013 gpio_set_value(ifx_dev->gpio.po, 0); 984 gpio_set_value(ifx_dev->gpio.po, 0);
1014 gpio_set_value(ifx_dev->gpio.reset, 0); 985 gpio_set_value(ifx_dev->gpio.reset, 0);
1015 msleep(25); 986 msleep(25);
1016 gpio_set_value(ifx_dev->gpio.reset, 1); 987 gpio_set_value(ifx_dev->gpio.reset, 1);
1017 msleep(1); 988 msleep(1);
1018 gpio_set_value(ifx_dev->gpio.po, 1); 989 gpio_set_value(ifx_dev->gpio.po, 1);
1019 msleep(1); 990 msleep(1);
1020 gpio_set_value(ifx_dev->gpio.po, 0); 991 gpio_set_value(ifx_dev->gpio.po, 0);
1021 ret = wait_event_timeout(ifx_dev->mdm_reset_wait, 992 ret = wait_event_timeout(ifx_dev->mdm_reset_wait,
1022 test_bit(MR_COMPLETE, 993 test_bit(MR_COMPLETE,
1023 &ifx_dev->mdm_reset_state), 994 &ifx_dev->mdm_reset_state),
1024 IFX_RESET_TIMEOUT); 995 IFX_RESET_TIMEOUT);
1025 if (!ret) 996 if (!ret)
1026 dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)", 997 dev_warn(&ifx_dev->spi_dev->dev, "Modem reset timeout: (state:%lx)",
1027 ifx_dev->mdm_reset_state); 998 ifx_dev->mdm_reset_state);
1028 999
1029 ifx_dev->mdm_reset_state = 0; 1000 ifx_dev->mdm_reset_state = 0;
1030 return ret; 1001 return ret;
1031 } 1002 }
1032 1003
1033 /** 1004 /**
1034 * ifx_spi_spi_probe - probe callback 1005 * ifx_spi_spi_probe - probe callback
1035 * @spi: our possible matching SPI device 1006 * @spi: our possible matching SPI device
1036 * 1007 *
1037 * Probe for a 6x60 modem on SPI bus. Perform any needed device and 1008 * Probe for a 6x60 modem on SPI bus. Perform any needed device and
1038 * GPIO setup. 1009 * GPIO setup.
1039 * 1010 *
1040 * FIXME: 1011 * FIXME:
1041 * - Support for multiple devices 1012 * - Support for multiple devices
1042 * - Split out MID specific GPIO handling eventually 1013 * - Split out MID specific GPIO handling eventually
1043 */ 1014 */
1044 1015
1045 static int ifx_spi_spi_probe(struct spi_device *spi) 1016 static int ifx_spi_spi_probe(struct spi_device *spi)
1046 { 1017 {
1047 int ret; 1018 int ret;
1048 int srdy; 1019 int srdy;
1049 struct ifx_modem_platform_data *pl_data; 1020 struct ifx_modem_platform_data *pl_data;
1050 struct ifx_spi_device *ifx_dev; 1021 struct ifx_spi_device *ifx_dev;
1051 1022
1052 if (saved_ifx_dev) { 1023 if (saved_ifx_dev) {
1053 dev_dbg(&spi->dev, "ignoring subsequent detection"); 1024 dev_dbg(&spi->dev, "ignoring subsequent detection");
1054 return -ENODEV; 1025 return -ENODEV;
1055 } 1026 }
1056 1027
1057 pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data; 1028 pl_data = (struct ifx_modem_platform_data *)spi->dev.platform_data;
1058 if (!pl_data) { 1029 if (!pl_data) {
1059 dev_err(&spi->dev, "missing platform data!"); 1030 dev_err(&spi->dev, "missing platform data!");
1060 return -ENODEV; 1031 return -ENODEV;
1061 } 1032 }
1062 1033
1063 /* initialize structure to hold our device variables */ 1034 /* initialize structure to hold our device variables */
1064 ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL); 1035 ifx_dev = kzalloc(sizeof(struct ifx_spi_device), GFP_KERNEL);
1065 if (!ifx_dev) { 1036 if (!ifx_dev) {
1066 dev_err(&spi->dev, "spi device allocation failed"); 1037 dev_err(&spi->dev, "spi device allocation failed");
1067 return -ENOMEM; 1038 return -ENOMEM;
1068 } 1039 }
1069 saved_ifx_dev = ifx_dev; 1040 saved_ifx_dev = ifx_dev;
1070 ifx_dev->spi_dev = spi; 1041 ifx_dev->spi_dev = spi;
1071 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags); 1042 clear_bit(IFX_SPI_STATE_IO_IN_PROGRESS, &ifx_dev->flags);
1072 spin_lock_init(&ifx_dev->write_lock); 1043 spin_lock_init(&ifx_dev->write_lock);
1073 spin_lock_init(&ifx_dev->power_lock); 1044 spin_lock_init(&ifx_dev->power_lock);
1074 ifx_dev->power_status = 0; 1045 ifx_dev->power_status = 0;
1075 init_timer(&ifx_dev->spi_timer); 1046 init_timer(&ifx_dev->spi_timer);
1076 ifx_dev->spi_timer.function = ifx_spi_timeout; 1047 ifx_dev->spi_timer.function = ifx_spi_timeout;
1077 ifx_dev->spi_timer.data = (unsigned long)ifx_dev; 1048 ifx_dev->spi_timer.data = (unsigned long)ifx_dev;
1078 ifx_dev->modem = pl_data->modem_type; 1049 ifx_dev->modem = pl_data->modem_type;
1079 ifx_dev->use_dma = pl_data->use_dma; 1050 ifx_dev->use_dma = pl_data->use_dma;
1080 ifx_dev->max_hz = pl_data->max_hz; 1051 ifx_dev->max_hz = pl_data->max_hz;
1081 /* initialize spi mode, etc */ 1052 /* initialize spi mode, etc */
1082 spi->max_speed_hz = ifx_dev->max_hz; 1053 spi->max_speed_hz = ifx_dev->max_hz;
1083 spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode); 1054 spi->mode = IFX_SPI_MODE | (SPI_LOOP & spi->mode);
1084 spi->bits_per_word = spi_bpw; 1055 spi->bits_per_word = spi_bpw;
1085 ret = spi_setup(spi); 1056 ret = spi_setup(spi);
1086 if (ret) { 1057 if (ret) {
1087 dev_err(&spi->dev, "SPI setup wasn't successful %d", ret); 1058 dev_err(&spi->dev, "SPI setup wasn't successful %d", ret);
1088 return -ENODEV; 1059 return -ENODEV;
1089 } 1060 }
1090 1061
1091 /* init swap_buf function according to word width configuration */ 1062 /* init swap_buf function according to word width configuration */
1092 if (spi->bits_per_word == 32) 1063 if (spi->bits_per_word == 32)
1093 ifx_dev->swap_buf = swap_buf_32; 1064 ifx_dev->swap_buf = swap_buf_32;
1094 else if (spi->bits_per_word == 16) 1065 else if (spi->bits_per_word == 16)
1095 ifx_dev->swap_buf = swap_buf_16; 1066 ifx_dev->swap_buf = swap_buf_16;
1096 else 1067 else
1097 ifx_dev->swap_buf = swap_buf_8; 1068 ifx_dev->swap_buf = swap_buf_8;
1098 1069
1099 /* ensure SPI protocol flags are initialized to enable transfer */ 1070 /* ensure SPI protocol flags are initialized to enable transfer */
1100 ifx_dev->spi_more = 0; 1071 ifx_dev->spi_more = 0;
1101 ifx_dev->spi_slave_cts = 0; 1072 ifx_dev->spi_slave_cts = 0;
1102 1073
1103 /*initialize transfer and dma buffers */ 1074 /*initialize transfer and dma buffers */
1104 ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent, 1075 ifx_dev->tx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1105 IFX_SPI_TRANSFER_SIZE, 1076 IFX_SPI_TRANSFER_SIZE,
1106 &ifx_dev->tx_bus, 1077 &ifx_dev->tx_bus,
1107 GFP_KERNEL); 1078 GFP_KERNEL);
1108 if (!ifx_dev->tx_buffer) { 1079 if (!ifx_dev->tx_buffer) {
1109 dev_err(&spi->dev, "DMA-TX buffer allocation failed"); 1080 dev_err(&spi->dev, "DMA-TX buffer allocation failed");
1110 ret = -ENOMEM; 1081 ret = -ENOMEM;
1111 goto error_ret; 1082 goto error_ret;
1112 } 1083 }
1113 ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent, 1084 ifx_dev->rx_buffer = dma_alloc_coherent(ifx_dev->spi_dev->dev.parent,
1114 IFX_SPI_TRANSFER_SIZE, 1085 IFX_SPI_TRANSFER_SIZE,
1115 &ifx_dev->rx_bus, 1086 &ifx_dev->rx_bus,
1116 GFP_KERNEL); 1087 GFP_KERNEL);
1117 if (!ifx_dev->rx_buffer) { 1088 if (!ifx_dev->rx_buffer) {
1118 dev_err(&spi->dev, "DMA-RX buffer allocation failed"); 1089 dev_err(&spi->dev, "DMA-RX buffer allocation failed");
1119 ret = -ENOMEM; 1090 ret = -ENOMEM;
1120 goto error_ret; 1091 goto error_ret;
1121 } 1092 }
1122 1093
1123 /* initialize waitq for modem reset */ 1094 /* initialize waitq for modem reset */
1124 init_waitqueue_head(&ifx_dev->mdm_reset_wait); 1095 init_waitqueue_head(&ifx_dev->mdm_reset_wait);
1125 1096
1126 spi_set_drvdata(spi, ifx_dev); 1097 spi_set_drvdata(spi, ifx_dev);
1127 tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io, 1098 tasklet_init(&ifx_dev->io_work_tasklet, ifx_spi_io,
1128 (unsigned long)ifx_dev); 1099 (unsigned long)ifx_dev);
1129 1100
1130 set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags); 1101 set_bit(IFX_SPI_STATE_PRESENT, &ifx_dev->flags);
1131 1102
1132 /* create our tty port */ 1103 /* create our tty port */
1133 ret = ifx_spi_create_port(ifx_dev); 1104 ret = ifx_spi_create_port(ifx_dev);
1134 if (ret != 0) { 1105 if (ret != 0) {
1135 dev_err(&spi->dev, "create default tty port failed"); 1106 dev_err(&spi->dev, "create default tty port failed");
1136 goto error_ret; 1107 goto error_ret;
1137 } 1108 }
1138 1109
1139 ifx_dev->gpio.reset = pl_data->rst_pmu; 1110 ifx_dev->gpio.reset = pl_data->rst_pmu;
1140 ifx_dev->gpio.po = pl_data->pwr_on; 1111 ifx_dev->gpio.po = pl_data->pwr_on;
1141 ifx_dev->gpio.mrdy = pl_data->mrdy; 1112 ifx_dev->gpio.mrdy = pl_data->mrdy;
1142 ifx_dev->gpio.srdy = pl_data->srdy; 1113 ifx_dev->gpio.srdy = pl_data->srdy;
1143 ifx_dev->gpio.reset_out = pl_data->rst_out; 1114 ifx_dev->gpio.reset_out = pl_data->rst_out;
1144 1115
1145 dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d", 1116 dev_info(&spi->dev, "gpios %d, %d, %d, %d, %d",
1146 ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy, 1117 ifx_dev->gpio.reset, ifx_dev->gpio.po, ifx_dev->gpio.mrdy,
1147 ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out); 1118 ifx_dev->gpio.srdy, ifx_dev->gpio.reset_out);
1148 1119
1149 /* Configure gpios */ 1120 /* Configure gpios */
1150 ret = gpio_request(ifx_dev->gpio.reset, "ifxModem"); 1121 ret = gpio_request(ifx_dev->gpio.reset, "ifxModem");
1151 if (ret < 0) { 1122 if (ret < 0) {
1152 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)", 1123 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET)",
1153 ifx_dev->gpio.reset); 1124 ifx_dev->gpio.reset);
1154 goto error_ret; 1125 goto error_ret;
1155 } 1126 }
1156 ret += gpio_direction_output(ifx_dev->gpio.reset, 0); 1127 ret += gpio_direction_output(ifx_dev->gpio.reset, 0);
1157 ret += gpio_export(ifx_dev->gpio.reset, 1); 1128 ret += gpio_export(ifx_dev->gpio.reset, 1);
1158 if (ret) { 1129 if (ret) {
1159 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)", 1130 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET)",
1160 ifx_dev->gpio.reset); 1131 ifx_dev->gpio.reset);
1161 ret = -EBUSY; 1132 ret = -EBUSY;
1162 goto error_ret2; 1133 goto error_ret2;
1163 } 1134 }
1164 1135
1165 ret = gpio_request(ifx_dev->gpio.po, "ifxModem"); 1136 ret = gpio_request(ifx_dev->gpio.po, "ifxModem");
1166 ret += gpio_direction_output(ifx_dev->gpio.po, 0); 1137 ret += gpio_direction_output(ifx_dev->gpio.po, 0);
1167 ret += gpio_export(ifx_dev->gpio.po, 1); 1138 ret += gpio_export(ifx_dev->gpio.po, 1);
1168 if (ret) { 1139 if (ret) {
1169 dev_err(&spi->dev, "Unable to configure GPIO%d (ON)", 1140 dev_err(&spi->dev, "Unable to configure GPIO%d (ON)",
1170 ifx_dev->gpio.po); 1141 ifx_dev->gpio.po);
1171 ret = -EBUSY; 1142 ret = -EBUSY;
1172 goto error_ret3; 1143 goto error_ret3;
1173 } 1144 }
1174 1145
1175 ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem"); 1146 ret = gpio_request(ifx_dev->gpio.mrdy, "ifxModem");
1176 if (ret < 0) { 1147 if (ret < 0) {
1177 dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)", 1148 dev_err(&spi->dev, "Unable to allocate GPIO%d (MRDY)",
1178 ifx_dev->gpio.mrdy); 1149 ifx_dev->gpio.mrdy);
1179 goto error_ret3; 1150 goto error_ret3;
1180 } 1151 }
1181 ret += gpio_export(ifx_dev->gpio.mrdy, 1); 1152 ret += gpio_export(ifx_dev->gpio.mrdy, 1);
1182 ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0); 1153 ret += gpio_direction_output(ifx_dev->gpio.mrdy, 0);
1183 if (ret) { 1154 if (ret) {
1184 dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)", 1155 dev_err(&spi->dev, "Unable to configure GPIO%d (MRDY)",
1185 ifx_dev->gpio.mrdy); 1156 ifx_dev->gpio.mrdy);
1186 ret = -EBUSY; 1157 ret = -EBUSY;
1187 goto error_ret4; 1158 goto error_ret4;
1188 } 1159 }
1189 1160
1190 ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem"); 1161 ret = gpio_request(ifx_dev->gpio.srdy, "ifxModem");
1191 if (ret < 0) { 1162 if (ret < 0) {
1192 dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)", 1163 dev_err(&spi->dev, "Unable to allocate GPIO%d (SRDY)",
1193 ifx_dev->gpio.srdy); 1164 ifx_dev->gpio.srdy);
1194 ret = -EBUSY; 1165 ret = -EBUSY;
1195 goto error_ret4; 1166 goto error_ret4;
1196 } 1167 }
1197 ret += gpio_export(ifx_dev->gpio.srdy, 1); 1168 ret += gpio_export(ifx_dev->gpio.srdy, 1);
1198 ret += gpio_direction_input(ifx_dev->gpio.srdy); 1169 ret += gpio_direction_input(ifx_dev->gpio.srdy);
1199 if (ret) { 1170 if (ret) {
1200 dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)", 1171 dev_err(&spi->dev, "Unable to configure GPIO%d (SRDY)",
1201 ifx_dev->gpio.srdy); 1172 ifx_dev->gpio.srdy);
1202 ret = -EBUSY; 1173 ret = -EBUSY;
1203 goto error_ret5; 1174 goto error_ret5;
1204 } 1175 }
1205 1176
1206 ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem"); 1177 ret = gpio_request(ifx_dev->gpio.reset_out, "ifxModem");
1207 if (ret < 0) { 1178 if (ret < 0) {
1208 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)", 1179 dev_err(&spi->dev, "Unable to allocate GPIO%d (RESET_OUT)",
1209 ifx_dev->gpio.reset_out); 1180 ifx_dev->gpio.reset_out);
1210 goto error_ret5; 1181 goto error_ret5;
1211 } 1182 }
1212 ret += gpio_export(ifx_dev->gpio.reset_out, 1); 1183 ret += gpio_export(ifx_dev->gpio.reset_out, 1);
1213 ret += gpio_direction_input(ifx_dev->gpio.reset_out); 1184 ret += gpio_direction_input(ifx_dev->gpio.reset_out);
1214 if (ret) { 1185 if (ret) {
1215 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)", 1186 dev_err(&spi->dev, "Unable to configure GPIO%d (RESET_OUT)",
1216 ifx_dev->gpio.reset_out); 1187 ifx_dev->gpio.reset_out);
1217 ret = -EBUSY; 1188 ret = -EBUSY;
1218 goto error_ret6; 1189 goto error_ret6;
1219 } 1190 }
1220 1191
1221 ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out), 1192 ret = request_irq(gpio_to_irq(ifx_dev->gpio.reset_out),
1222 ifx_spi_reset_interrupt, 1193 ifx_spi_reset_interrupt,
1223 IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME, 1194 IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, DRVNAME,
1224 (void *)ifx_dev); 1195 (void *)ifx_dev);
1225 if (ret) { 1196 if (ret) {
1226 dev_err(&spi->dev, "Unable to get irq %x\n", 1197 dev_err(&spi->dev, "Unable to get irq %x\n",
1227 gpio_to_irq(ifx_dev->gpio.reset_out)); 1198 gpio_to_irq(ifx_dev->gpio.reset_out));
1228 goto error_ret6; 1199 goto error_ret6;
1229 } 1200 }
1230 1201
1231 ret = ifx_spi_reset(ifx_dev); 1202 ret = ifx_spi_reset(ifx_dev);
1232 1203
1233 ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy), 1204 ret = request_irq(gpio_to_irq(ifx_dev->gpio.srdy),
1234 ifx_spi_srdy_interrupt, 1205 ifx_spi_srdy_interrupt,
1235 IRQF_TRIGGER_RISING, DRVNAME, 1206 IRQF_TRIGGER_RISING, DRVNAME,
1236 (void *)ifx_dev); 1207 (void *)ifx_dev);
1237 if (ret) { 1208 if (ret) {
1238 dev_err(&spi->dev, "Unable to get irq %x", 1209 dev_err(&spi->dev, "Unable to get irq %x",
1239 gpio_to_irq(ifx_dev->gpio.srdy)); 1210 gpio_to_irq(ifx_dev->gpio.srdy));
1240 goto error_ret7; 1211 goto error_ret7;
1241 } 1212 }
1242 1213
1243 /* set pm runtime power state and register with power system */ 1214 /* set pm runtime power state and register with power system */
1244 pm_runtime_set_active(&spi->dev); 1215 pm_runtime_set_active(&spi->dev);
1245 pm_runtime_enable(&spi->dev); 1216 pm_runtime_enable(&spi->dev);
1246 1217
1247 /* handle case that modem is already signaling SRDY */ 1218 /* handle case that modem is already signaling SRDY */
1248 /* no outgoing tty open at this point, this just satisfies the 1219 /* no outgoing tty open at this point, this just satisfies the
1249 * modem's read and should reset communication properly 1220 * modem's read and should reset communication properly
1250 */ 1221 */
1251 srdy = gpio_get_value(ifx_dev->gpio.srdy); 1222 srdy = gpio_get_value(ifx_dev->gpio.srdy);
1252 1223
1253 if (srdy) { 1224 if (srdy) {
1254 mrdy_assert(ifx_dev); 1225 mrdy_assert(ifx_dev);
1255 ifx_spi_handle_srdy(ifx_dev); 1226 ifx_spi_handle_srdy(ifx_dev);
1256 } else 1227 } else
1257 mrdy_set_low(ifx_dev); 1228 mrdy_set_low(ifx_dev);
1258 return 0; 1229 return 0;
1259 1230
1260 error_ret7: 1231 error_ret7:
1261 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev); 1232 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1262 error_ret6: 1233 error_ret6:
1263 gpio_free(ifx_dev->gpio.srdy); 1234 gpio_free(ifx_dev->gpio.srdy);
1264 error_ret5: 1235 error_ret5:
1265 gpio_free(ifx_dev->gpio.mrdy); 1236 gpio_free(ifx_dev->gpio.mrdy);
1266 error_ret4: 1237 error_ret4:
1267 gpio_free(ifx_dev->gpio.reset); 1238 gpio_free(ifx_dev->gpio.reset);
1268 error_ret3: 1239 error_ret3:
1269 gpio_free(ifx_dev->gpio.po); 1240 gpio_free(ifx_dev->gpio.po);
1270 error_ret2: 1241 error_ret2:
1271 gpio_free(ifx_dev->gpio.reset_out); 1242 gpio_free(ifx_dev->gpio.reset_out);
1272 error_ret: 1243 error_ret:
1273 ifx_spi_free_device(ifx_dev); 1244 ifx_spi_free_device(ifx_dev);
1274 saved_ifx_dev = NULL; 1245 saved_ifx_dev = NULL;
1275 return ret; 1246 return ret;
1276 } 1247 }
1277 1248
1278 /** 1249 /**
1279 * ifx_spi_spi_remove - SPI device was removed 1250 * ifx_spi_spi_remove - SPI device was removed
1280 * @spi: SPI device 1251 * @spi: SPI device
1281 * 1252 *
1282 * FIXME: We should be shutting the device down here not in 1253 * FIXME: We should be shutting the device down here not in
1283 * the module unload path. 1254 * the module unload path.
1284 */ 1255 */
1285 1256
1286 static int ifx_spi_spi_remove(struct spi_device *spi) 1257 static int ifx_spi_spi_remove(struct spi_device *spi)
1287 { 1258 {
1288 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi); 1259 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1289 /* stop activity */ 1260 /* stop activity */
1290 tasklet_kill(&ifx_dev->io_work_tasklet); 1261 tasklet_kill(&ifx_dev->io_work_tasklet);
1291 /* free irq */ 1262 /* free irq */
1292 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev); 1263 free_irq(gpio_to_irq(ifx_dev->gpio.reset_out), (void *)ifx_dev);
1293 free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev); 1264 free_irq(gpio_to_irq(ifx_dev->gpio.srdy), (void *)ifx_dev);
1294 1265
1295 gpio_free(ifx_dev->gpio.srdy); 1266 gpio_free(ifx_dev->gpio.srdy);
1296 gpio_free(ifx_dev->gpio.mrdy); 1267 gpio_free(ifx_dev->gpio.mrdy);
1297 gpio_free(ifx_dev->gpio.reset); 1268 gpio_free(ifx_dev->gpio.reset);
1298 gpio_free(ifx_dev->gpio.po); 1269 gpio_free(ifx_dev->gpio.po);
1299 gpio_free(ifx_dev->gpio.reset_out); 1270 gpio_free(ifx_dev->gpio.reset_out);
1300 1271
1301 /* free allocations */ 1272 /* free allocations */
1302 ifx_spi_free_device(ifx_dev); 1273 ifx_spi_free_device(ifx_dev);
1303 1274
1304 saved_ifx_dev = NULL; 1275 saved_ifx_dev = NULL;
1305 return 0; 1276 return 0;
1306 } 1277 }
1307 1278
1308 /** 1279 /**
1309 * ifx_spi_spi_shutdown - called on SPI shutdown 1280 * ifx_spi_spi_shutdown - called on SPI shutdown
1310 * @spi: SPI device 1281 * @spi: SPI device
1311 * 1282 *
1312 * No action needs to be taken here 1283 * No action needs to be taken here
1313 */ 1284 */
1314 1285
1315 static void ifx_spi_spi_shutdown(struct spi_device *spi) 1286 static void ifx_spi_spi_shutdown(struct spi_device *spi)
1316 { 1287 {
1317 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi); 1288 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1318 1289
1319 ifx_modem_power_off(ifx_dev); 1290 ifx_modem_power_off(ifx_dev);
1320 } 1291 }
1321 1292
1322 /* 1293 /*
1323 * various suspends and resumes have nothing to do 1294 * various suspends and resumes have nothing to do
1324 * no hardware to save state for 1295 * no hardware to save state for
1325 */ 1296 */
1326 1297
1327 /** 1298 /**
1328 * ifx_spi_spi_suspend - suspend SPI on system suspend 1299 * ifx_spi_spi_suspend - suspend SPI on system suspend
1329 * @dev: device being suspended 1300 * @dev: device being suspended
1330 * 1301 *
1331 * Suspend the SPI side. No action needed on Intel MID platforms, may 1302 * Suspend the SPI side. No action needed on Intel MID platforms, may
1332 * need extending for other systems. 1303 * need extending for other systems.
1333 */ 1304 */
1334 static int ifx_spi_spi_suspend(struct spi_device *spi, pm_message_t msg) 1305 static int ifx_spi_spi_suspend(struct spi_device *spi, pm_message_t msg)
1335 { 1306 {
1336 return 0; 1307 return 0;
1337 } 1308 }
1338 1309
1339 /** 1310 /**
1340 * ifx_spi_spi_resume - resume SPI side on system resume 1311 * ifx_spi_spi_resume - resume SPI side on system resume
1341 * @dev: device being suspended 1312 * @dev: device being suspended
1342 * 1313 *
1343 * Suspend the SPI side. No action needed on Intel MID platforms, may 1314 * Suspend the SPI side. No action needed on Intel MID platforms, may
1344 * need extending for other systems. 1315 * need extending for other systems.
1345 */ 1316 */
1346 static int ifx_spi_spi_resume(struct spi_device *spi) 1317 static int ifx_spi_spi_resume(struct spi_device *spi)
1347 { 1318 {
1348 return 0; 1319 return 0;
1349 } 1320 }
1350 1321
1351 /** 1322 /**
1352 * ifx_spi_pm_suspend - suspend modem on system suspend 1323 * ifx_spi_pm_suspend - suspend modem on system suspend
1353 * @dev: device being suspended 1324 * @dev: device being suspended
1354 * 1325 *
1355 * Suspend the modem. No action needed on Intel MID platforms, may 1326 * Suspend the modem. No action needed on Intel MID platforms, may
1356 * need extending for other systems. 1327 * need extending for other systems.
1357 */ 1328 */
1358 static int ifx_spi_pm_suspend(struct device *dev) 1329 static int ifx_spi_pm_suspend(struct device *dev)
1359 { 1330 {
1360 return 0; 1331 return 0;
1361 } 1332 }
1362 1333
1363 /** 1334 /**
1364 * ifx_spi_pm_resume - resume modem on system resume 1335 * ifx_spi_pm_resume - resume modem on system resume
1365 * @dev: device being suspended 1336 * @dev: device being suspended
1366 * 1337 *
1367 * Allow the modem to resume. No action needed. 1338 * Allow the modem to resume. No action needed.
1368 * 1339 *
1369 * FIXME: do we need to reset anything here ? 1340 * FIXME: do we need to reset anything here ?
1370 */ 1341 */
1371 static int ifx_spi_pm_resume(struct device *dev) 1342 static int ifx_spi_pm_resume(struct device *dev)
1372 { 1343 {
1373 return 0; 1344 return 0;
1374 } 1345 }
1375 1346
1376 /** 1347 /**
1377 * ifx_spi_pm_runtime_resume - suspend modem 1348 * ifx_spi_pm_runtime_resume - suspend modem
1378 * @dev: device being suspended 1349 * @dev: device being suspended
1379 * 1350 *
1380 * Allow the modem to resume. No action needed. 1351 * Allow the modem to resume. No action needed.
1381 */ 1352 */
1382 static int ifx_spi_pm_runtime_resume(struct device *dev) 1353 static int ifx_spi_pm_runtime_resume(struct device *dev)
1383 { 1354 {
1384 return 0; 1355 return 0;
1385 } 1356 }
1386 1357
1387 /** 1358 /**
1388 * ifx_spi_pm_runtime_suspend - suspend modem 1359 * ifx_spi_pm_runtime_suspend - suspend modem
1389 * @dev: device being suspended 1360 * @dev: device being suspended
1390 * 1361 *
1391 * Allow the modem to suspend and thus suspend to continue up the 1362 * Allow the modem to suspend and thus suspend to continue up the
1392 * device tree. 1363 * device tree.
1393 */ 1364 */
1394 static int ifx_spi_pm_runtime_suspend(struct device *dev) 1365 static int ifx_spi_pm_runtime_suspend(struct device *dev)
1395 { 1366 {
1396 return 0; 1367 return 0;
1397 } 1368 }
1398 1369
1399 /** 1370 /**
1400 * ifx_spi_pm_runtime_idle - check if modem idle 1371 * ifx_spi_pm_runtime_idle - check if modem idle
1401 * @dev: our device 1372 * @dev: our device
1402 * 1373 *
1403 * Check conditions and queue runtime suspend if idle. 1374 * Check conditions and queue runtime suspend if idle.
1404 */ 1375 */
1405 static int ifx_spi_pm_runtime_idle(struct device *dev) 1376 static int ifx_spi_pm_runtime_idle(struct device *dev)
1406 { 1377 {
1407 struct spi_device *spi = to_spi_device(dev); 1378 struct spi_device *spi = to_spi_device(dev);
1408 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi); 1379 struct ifx_spi_device *ifx_dev = spi_get_drvdata(spi);
1409 1380
1410 if (!ifx_dev->power_status) 1381 if (!ifx_dev->power_status)
1411 pm_runtime_suspend(dev); 1382 pm_runtime_suspend(dev);
1412 1383
1413 return 0; 1384 return 0;
1414 } 1385 }
1415 1386
1416 static const struct dev_pm_ops ifx_spi_pm = { 1387 static const struct dev_pm_ops ifx_spi_pm = {
1417 .resume = ifx_spi_pm_resume, 1388 .resume = ifx_spi_pm_resume,
1418 .suspend = ifx_spi_pm_suspend, 1389 .suspend = ifx_spi_pm_suspend,
1419 .runtime_resume = ifx_spi_pm_runtime_resume, 1390 .runtime_resume = ifx_spi_pm_runtime_resume,
1420 .runtime_suspend = ifx_spi_pm_runtime_suspend, 1391 .runtime_suspend = ifx_spi_pm_runtime_suspend,
1421 .runtime_idle = ifx_spi_pm_runtime_idle 1392 .runtime_idle = ifx_spi_pm_runtime_idle
1422 }; 1393 };
1423 1394
1424 static const struct spi_device_id ifx_id_table[] = { 1395 static const struct spi_device_id ifx_id_table[] = {
1425 {"ifx6160", 0}, 1396 {"ifx6160", 0},
1426 {"ifx6260", 0}, 1397 {"ifx6260", 0},
1427 { } 1398 { }
1428 }; 1399 };
1429 MODULE_DEVICE_TABLE(spi, ifx_id_table); 1400 MODULE_DEVICE_TABLE(spi, ifx_id_table);
1430 1401
1431 /* spi operations */ 1402 /* spi operations */
1432 static struct spi_driver ifx_spi_driver = { 1403 static struct spi_driver ifx_spi_driver = {
1433 .driver = { 1404 .driver = {
1434 .name = DRVNAME, 1405 .name = DRVNAME,
1435 .pm = &ifx_spi_pm, 1406 .pm = &ifx_spi_pm,
1436 .owner = THIS_MODULE}, 1407 .owner = THIS_MODULE},
1437 .probe = ifx_spi_spi_probe, 1408 .probe = ifx_spi_spi_probe,
1438 .shutdown = ifx_spi_spi_shutdown, 1409 .shutdown = ifx_spi_spi_shutdown,
1439 .remove = ifx_spi_spi_remove, 1410 .remove = ifx_spi_spi_remove,
1440 .suspend = ifx_spi_spi_suspend, 1411 .suspend = ifx_spi_spi_suspend,
1441 .resume = ifx_spi_spi_resume, 1412 .resume = ifx_spi_spi_resume,
1442 .id_table = ifx_id_table 1413 .id_table = ifx_id_table
1443 }; 1414 };
1444 1415
1445 /** 1416 /**
1446 * ifx_spi_exit - module exit 1417 * ifx_spi_exit - module exit
1447 * 1418 *
1448 * Unload the module. 1419 * Unload the module.
1449 */ 1420 */
1450 1421
1451 static void __exit ifx_spi_exit(void) 1422 static void __exit ifx_spi_exit(void)
1452 { 1423 {
1453 /* unregister */ 1424 /* unregister */
1454 tty_unregister_driver(tty_drv); 1425 tty_unregister_driver(tty_drv);
1455 put_tty_driver(tty_drv); 1426 put_tty_driver(tty_drv);
1456 spi_unregister_driver((void *)&ifx_spi_driver); 1427 spi_unregister_driver((void *)&ifx_spi_driver);
1457 unregister_reboot_notifier(&ifx_modem_reboot_notifier_block); 1428 unregister_reboot_notifier(&ifx_modem_reboot_notifier_block);
1458 } 1429 }
1459 1430
1460 /** 1431 /**
1461 * ifx_spi_init - module entry point 1432 * ifx_spi_init - module entry point
1462 * 1433 *
1463 * Initialise the SPI and tty interfaces for the IFX SPI driver 1434 * Initialise the SPI and tty interfaces for the IFX SPI driver
1464 * We need to initialize upper-edge spi driver after the tty 1435 * We need to initialize upper-edge spi driver after the tty
1465 * driver because otherwise the spi probe will race 1436 * driver because otherwise the spi probe will race
1466 */ 1437 */
1467 1438
1468 static int __init ifx_spi_init(void) 1439 static int __init ifx_spi_init(void)
1469 { 1440 {
1470 int result; 1441 int result;
1471 1442
1472 tty_drv = alloc_tty_driver(1); 1443 tty_drv = alloc_tty_driver(1);
1473 if (!tty_drv) { 1444 if (!tty_drv) {
1474 pr_err("%s: alloc_tty_driver failed", DRVNAME); 1445 pr_err("%s: alloc_tty_driver failed", DRVNAME);
1475 return -ENOMEM; 1446 return -ENOMEM;
1476 } 1447 }
1477 1448
1478 tty_drv->driver_name = DRVNAME; 1449 tty_drv->driver_name = DRVNAME;
1479 tty_drv->name = TTYNAME; 1450 tty_drv->name = TTYNAME;
1480 tty_drv->minor_start = IFX_SPI_TTY_ID; 1451 tty_drv->minor_start = IFX_SPI_TTY_ID;
1481 tty_drv->type = TTY_DRIVER_TYPE_SERIAL; 1452 tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
1482 tty_drv->subtype = SERIAL_TYPE_NORMAL; 1453 tty_drv->subtype = SERIAL_TYPE_NORMAL;
1483 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 1454 tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1484 tty_drv->init_termios = tty_std_termios; 1455 tty_drv->init_termios = tty_std_termios;
1485 1456
1486 tty_set_operations(tty_drv, &ifx_spi_serial_ops); 1457 tty_set_operations(tty_drv, &ifx_spi_serial_ops);
1487 1458
1488 result = tty_register_driver(tty_drv); 1459 result = tty_register_driver(tty_drv);
1489 if (result) { 1460 if (result) {
1490 pr_err("%s: tty_register_driver failed(%d)", 1461 pr_err("%s: tty_register_driver failed(%d)",
1491 DRVNAME, result); 1462 DRVNAME, result);
1492 goto err_free_tty; 1463 goto err_free_tty;
1493 } 1464 }
1494 1465
1495 result = spi_register_driver((void *)&ifx_spi_driver); 1466 result = spi_register_driver((void *)&ifx_spi_driver);
1496 if (result) { 1467 if (result) {
1497 pr_err("%s: spi_register_driver failed(%d)", 1468 pr_err("%s: spi_register_driver failed(%d)",
1498 DRVNAME, result); 1469 DRVNAME, result);
1499 goto err_unreg_tty; 1470 goto err_unreg_tty;
1500 } 1471 }
1501 1472
1502 result = register_reboot_notifier(&ifx_modem_reboot_notifier_block); 1473 result = register_reboot_notifier(&ifx_modem_reboot_notifier_block);
1503 if (result) { 1474 if (result) {
1504 pr_err("%s: register ifx modem reboot notifier failed(%d)", 1475 pr_err("%s: register ifx modem reboot notifier failed(%d)",
1505 DRVNAME, result); 1476 DRVNAME, result);
1506 goto err_unreg_spi; 1477 goto err_unreg_spi;
1507 } 1478 }
1508 1479
1509 return 0; 1480 return 0;
1510 err_unreg_spi: 1481 err_unreg_spi:
1511 spi_unregister_driver((void *)&ifx_spi_driver); 1482 spi_unregister_driver((void *)&ifx_spi_driver);
1512 err_unreg_tty: 1483 err_unreg_tty:
1513 tty_unregister_driver(tty_drv); 1484 tty_unregister_driver(tty_drv);
1514 err_free_tty: 1485 err_free_tty:
1515 put_tty_driver(tty_drv); 1486 put_tty_driver(tty_drv);
1516 1487
1517 return result; 1488 return result;
1518 } 1489 }
1519 1490
1520 module_init(ifx_spi_init); 1491 module_init(ifx_spi_init);
1521 module_exit(ifx_spi_exit); 1492 module_exit(ifx_spi_exit);
1522 1493
1523 MODULE_AUTHOR("Intel"); 1494 MODULE_AUTHOR("Intel");
1524 MODULE_DESCRIPTION("IFX6x60 spi driver"); 1495 MODULE_DESCRIPTION("IFX6x60 spi driver");
1525 MODULE_LICENSE("GPL"); 1496 MODULE_LICENSE("GPL");
1526 MODULE_INFO(Version, "0.1-IFX6x60"); 1497 MODULE_INFO(Version, "0.1-IFX6x60");
1527 1498
drivers/tty/tty_port.c
1 /* 1 /*
2 * Tty port functions 2 * Tty port functions
3 */ 3 */
4 4
5 #include <linux/types.h> 5 #include <linux/types.h>
6 #include <linux/errno.h> 6 #include <linux/errno.h>
7 #include <linux/tty.h> 7 #include <linux/tty.h>
8 #include <linux/tty_driver.h> 8 #include <linux/tty_driver.h>
9 #include <linux/tty_flip.h> 9 #include <linux/tty_flip.h>
10 #include <linux/serial.h> 10 #include <linux/serial.h>
11 #include <linux/timer.h> 11 #include <linux/timer.h>
12 #include <linux/string.h> 12 #include <linux/string.h>
13 #include <linux/slab.h> 13 #include <linux/slab.h>
14 #include <linux/sched.h> 14 #include <linux/sched.h>
15 #include <linux/init.h> 15 #include <linux/init.h>
16 #include <linux/wait.h> 16 #include <linux/wait.h>
17 #include <linux/bitops.h> 17 #include <linux/bitops.h>
18 #include <linux/delay.h> 18 #include <linux/delay.h>
19 #include <linux/module.h> 19 #include <linux/module.h>
20 20
21 void tty_port_init(struct tty_port *port) 21 void tty_port_init(struct tty_port *port)
22 { 22 {
23 memset(port, 0, sizeof(*port)); 23 memset(port, 0, sizeof(*port));
24 tty_buffer_init(port); 24 tty_buffer_init(port);
25 init_waitqueue_head(&port->open_wait); 25 init_waitqueue_head(&port->open_wait);
26 init_waitqueue_head(&port->close_wait); 26 init_waitqueue_head(&port->close_wait);
27 init_waitqueue_head(&port->delta_msr_wait); 27 init_waitqueue_head(&port->delta_msr_wait);
28 mutex_init(&port->mutex); 28 mutex_init(&port->mutex);
29 mutex_init(&port->buf_mutex); 29 mutex_init(&port->buf_mutex);
30 spin_lock_init(&port->lock); 30 spin_lock_init(&port->lock);
31 port->close_delay = (50 * HZ) / 100; 31 port->close_delay = (50 * HZ) / 100;
32 port->closing_wait = (3000 * HZ) / 100; 32 port->closing_wait = (3000 * HZ) / 100;
33 kref_init(&port->kref); 33 kref_init(&port->kref);
34 } 34 }
35 EXPORT_SYMBOL(tty_port_init); 35 EXPORT_SYMBOL(tty_port_init);
36 36
37 /** 37 /**
38 * tty_port_link_device - link tty and tty_port 38 * tty_port_link_device - link tty and tty_port
39 * @port: tty_port of the device 39 * @port: tty_port of the device
40 * @driver: tty_driver for this device 40 * @driver: tty_driver for this device
41 * @index: index of the tty 41 * @index: index of the tty
42 * 42 *
43 * Provide the tty layer wit ha link from a tty (specified by @index) to a 43 * Provide the tty layer wit ha link from a tty (specified by @index) to a
44 * tty_port (@port). Use this only if neither tty_port_register_device nor 44 * tty_port (@port). Use this only if neither tty_port_register_device nor
45 * tty_port_install is used in the driver. If used, this has to be called before 45 * tty_port_install is used in the driver. If used, this has to be called before
46 * tty_register_driver. 46 * tty_register_driver.
47 */ 47 */
48 void tty_port_link_device(struct tty_port *port, 48 void tty_port_link_device(struct tty_port *port,
49 struct tty_driver *driver, unsigned index) 49 struct tty_driver *driver, unsigned index)
50 { 50 {
51 if (WARN_ON(index >= driver->num)) 51 if (WARN_ON(index >= driver->num))
52 return; 52 return;
53 driver->ports[index] = port; 53 driver->ports[index] = port;
54 } 54 }
55 EXPORT_SYMBOL_GPL(tty_port_link_device); 55 EXPORT_SYMBOL_GPL(tty_port_link_device);
56 56
57 /** 57 /**
58 * tty_port_register_device - register tty device 58 * tty_port_register_device - register tty device
59 * @port: tty_port of the device 59 * @port: tty_port of the device
60 * @driver: tty_driver for this device 60 * @driver: tty_driver for this device
61 * @index: index of the tty 61 * @index: index of the tty
62 * @device: parent if exists, otherwise NULL 62 * @device: parent if exists, otherwise NULL
63 * 63 *
64 * It is the same as tty_register_device except the provided @port is linked to 64 * It is the same as tty_register_device except the provided @port is linked to
65 * a concrete tty specified by @index. Use this or tty_port_install (or both). 65 * a concrete tty specified by @index. Use this or tty_port_install (or both).
66 * Call tty_port_link_device as a last resort. 66 * Call tty_port_link_device as a last resort.
67 */ 67 */
68 struct device *tty_port_register_device(struct tty_port *port, 68 struct device *tty_port_register_device(struct tty_port *port,
69 struct tty_driver *driver, unsigned index, 69 struct tty_driver *driver, unsigned index,
70 struct device *device) 70 struct device *device)
71 { 71 {
72 tty_port_link_device(port, driver, index); 72 tty_port_link_device(port, driver, index);
73 return tty_register_device(driver, index, device); 73 return tty_register_device(driver, index, device);
74 } 74 }
75 EXPORT_SYMBOL_GPL(tty_port_register_device); 75 EXPORT_SYMBOL_GPL(tty_port_register_device);
76 76
77 /** 77 /**
78 * tty_port_register_device_attr - register tty device 78 * tty_port_register_device_attr - register tty device
79 * @port: tty_port of the device 79 * @port: tty_port of the device
80 * @driver: tty_driver for this device 80 * @driver: tty_driver for this device
81 * @index: index of the tty 81 * @index: index of the tty
82 * @device: parent if exists, otherwise NULL 82 * @device: parent if exists, otherwise NULL
83 * @drvdata: Driver data to be set to device. 83 * @drvdata: Driver data to be set to device.
84 * @attr_grp: Attribute group to be set on device. 84 * @attr_grp: Attribute group to be set on device.
85 * 85 *
86 * It is the same as tty_register_device_attr except the provided @port is 86 * It is the same as tty_register_device_attr except the provided @port is
87 * linked to a concrete tty specified by @index. Use this or tty_port_install 87 * linked to a concrete tty specified by @index. Use this or tty_port_install
88 * (or both). Call tty_port_link_device as a last resort. 88 * (or both). Call tty_port_link_device as a last resort.
89 */ 89 */
90 struct device *tty_port_register_device_attr(struct tty_port *port, 90 struct device *tty_port_register_device_attr(struct tty_port *port,
91 struct tty_driver *driver, unsigned index, 91 struct tty_driver *driver, unsigned index,
92 struct device *device, void *drvdata, 92 struct device *device, void *drvdata,
93 const struct attribute_group **attr_grp) 93 const struct attribute_group **attr_grp)
94 { 94 {
95 tty_port_link_device(port, driver, index); 95 tty_port_link_device(port, driver, index);
96 return tty_register_device_attr(driver, index, device, drvdata, 96 return tty_register_device_attr(driver, index, device, drvdata,
97 attr_grp); 97 attr_grp);
98 } 98 }
99 EXPORT_SYMBOL_GPL(tty_port_register_device_attr); 99 EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
100 100
101 int tty_port_alloc_xmit_buf(struct tty_port *port) 101 int tty_port_alloc_xmit_buf(struct tty_port *port)
102 { 102 {
103 /* We may sleep in get_zeroed_page() */ 103 /* We may sleep in get_zeroed_page() */
104 mutex_lock(&port->buf_mutex); 104 mutex_lock(&port->buf_mutex);
105 if (port->xmit_buf == NULL) 105 if (port->xmit_buf == NULL)
106 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); 106 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
107 mutex_unlock(&port->buf_mutex); 107 mutex_unlock(&port->buf_mutex);
108 if (port->xmit_buf == NULL) 108 if (port->xmit_buf == NULL)
109 return -ENOMEM; 109 return -ENOMEM;
110 return 0; 110 return 0;
111 } 111 }
112 EXPORT_SYMBOL(tty_port_alloc_xmit_buf); 112 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
113 113
114 void tty_port_free_xmit_buf(struct tty_port *port) 114 void tty_port_free_xmit_buf(struct tty_port *port)
115 { 115 {
116 mutex_lock(&port->buf_mutex); 116 mutex_lock(&port->buf_mutex);
117 if (port->xmit_buf != NULL) { 117 if (port->xmit_buf != NULL) {
118 free_page((unsigned long)port->xmit_buf); 118 free_page((unsigned long)port->xmit_buf);
119 port->xmit_buf = NULL; 119 port->xmit_buf = NULL;
120 } 120 }
121 mutex_unlock(&port->buf_mutex); 121 mutex_unlock(&port->buf_mutex);
122 } 122 }
123 EXPORT_SYMBOL(tty_port_free_xmit_buf); 123 EXPORT_SYMBOL(tty_port_free_xmit_buf);
124 124
125 /** 125 /**
126 * tty_port_destroy -- destroy inited port 126 * tty_port_destroy -- destroy inited port
127 * @port: tty port to be doestroyed 127 * @port: tty port to be doestroyed
128 * 128 *
129 * When a port was initialized using tty_port_init, one has to destroy the 129 * When a port was initialized using tty_port_init, one has to destroy the
130 * port by this function. Either indirectly by using tty_port refcounting 130 * port by this function. Either indirectly by using tty_port refcounting
131 * (tty_port_put) or directly if refcounting is not used. 131 * (tty_port_put) or directly if refcounting is not used.
132 */ 132 */
133 void tty_port_destroy(struct tty_port *port) 133 void tty_port_destroy(struct tty_port *port)
134 { 134 {
135 tty_buffer_free_all(port); 135 tty_buffer_free_all(port);
136 } 136 }
137 EXPORT_SYMBOL(tty_port_destroy); 137 EXPORT_SYMBOL(tty_port_destroy);
138 138
139 static void tty_port_destructor(struct kref *kref) 139 static void tty_port_destructor(struct kref *kref)
140 { 140 {
141 struct tty_port *port = container_of(kref, struct tty_port, kref); 141 struct tty_port *port = container_of(kref, struct tty_port, kref);
142 if (port->xmit_buf) 142 if (port->xmit_buf)
143 free_page((unsigned long)port->xmit_buf); 143 free_page((unsigned long)port->xmit_buf);
144 tty_port_destroy(port); 144 tty_port_destroy(port);
145 if (port->ops && port->ops->destruct) 145 if (port->ops && port->ops->destruct)
146 port->ops->destruct(port); 146 port->ops->destruct(port);
147 else 147 else
148 kfree(port); 148 kfree(port);
149 } 149 }
150 150
151 void tty_port_put(struct tty_port *port) 151 void tty_port_put(struct tty_port *port)
152 { 152 {
153 if (port) 153 if (port)
154 kref_put(&port->kref, tty_port_destructor); 154 kref_put(&port->kref, tty_port_destructor);
155 } 155 }
156 EXPORT_SYMBOL(tty_port_put); 156 EXPORT_SYMBOL(tty_port_put);
157 157
158 /** 158 /**
159 * tty_port_tty_get - get a tty reference 159 * tty_port_tty_get - get a tty reference
160 * @port: tty port 160 * @port: tty port
161 * 161 *
162 * Return a refcount protected tty instance or NULL if the port is not 162 * Return a refcount protected tty instance or NULL if the port is not
163 * associated with a tty (eg due to close or hangup) 163 * associated with a tty (eg due to close or hangup)
164 */ 164 */
165 165
166 struct tty_struct *tty_port_tty_get(struct tty_port *port) 166 struct tty_struct *tty_port_tty_get(struct tty_port *port)
167 { 167 {
168 unsigned long flags; 168 unsigned long flags;
169 struct tty_struct *tty; 169 struct tty_struct *tty;
170 170
171 spin_lock_irqsave(&port->lock, flags); 171 spin_lock_irqsave(&port->lock, flags);
172 tty = tty_kref_get(port->tty); 172 tty = tty_kref_get(port->tty);
173 spin_unlock_irqrestore(&port->lock, flags); 173 spin_unlock_irqrestore(&port->lock, flags);
174 return tty; 174 return tty;
175 } 175 }
176 EXPORT_SYMBOL(tty_port_tty_get); 176 EXPORT_SYMBOL(tty_port_tty_get);
177 177
178 /** 178 /**
179 * tty_port_tty_set - set the tty of a port 179 * tty_port_tty_set - set the tty of a port
180 * @port: tty port 180 * @port: tty port
181 * @tty: the tty 181 * @tty: the tty
182 * 182 *
183 * Associate the port and tty pair. Manages any internal refcounts. 183 * Associate the port and tty pair. Manages any internal refcounts.
184 * Pass NULL to deassociate a port 184 * Pass NULL to deassociate a port
185 */ 185 */
186 186
187 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) 187 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
188 { 188 {
189 unsigned long flags; 189 unsigned long flags;
190 190
191 spin_lock_irqsave(&port->lock, flags); 191 spin_lock_irqsave(&port->lock, flags);
192 if (port->tty) 192 if (port->tty)
193 tty_kref_put(port->tty); 193 tty_kref_put(port->tty);
194 port->tty = tty_kref_get(tty); 194 port->tty = tty_kref_get(tty);
195 spin_unlock_irqrestore(&port->lock, flags); 195 spin_unlock_irqrestore(&port->lock, flags);
196 } 196 }
197 EXPORT_SYMBOL(tty_port_tty_set); 197 EXPORT_SYMBOL(tty_port_tty_set);
198 198
199 static void tty_port_shutdown(struct tty_port *port) 199 static void tty_port_shutdown(struct tty_port *port)
200 { 200 {
201 mutex_lock(&port->mutex); 201 mutex_lock(&port->mutex);
202 if (port->ops->shutdown && !port->console && 202 if (port->ops->shutdown && !port->console &&
203 test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) 203 test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
204 port->ops->shutdown(port); 204 port->ops->shutdown(port);
205 mutex_unlock(&port->mutex); 205 mutex_unlock(&port->mutex);
206 } 206 }
207 207
208 /** 208 /**
209 * tty_port_hangup - hangup helper 209 * tty_port_hangup - hangup helper
210 * @port: tty port 210 * @port: tty port
211 * 211 *
212 * Perform port level tty hangup flag and count changes. Drop the tty 212 * Perform port level tty hangup flag and count changes. Drop the tty
213 * reference. 213 * reference.
214 */ 214 */
215 215
216 void tty_port_hangup(struct tty_port *port) 216 void tty_port_hangup(struct tty_port *port)
217 { 217 {
218 unsigned long flags; 218 unsigned long flags;
219 219
220 spin_lock_irqsave(&port->lock, flags); 220 spin_lock_irqsave(&port->lock, flags);
221 port->count = 0; 221 port->count = 0;
222 port->flags &= ~ASYNC_NORMAL_ACTIVE; 222 port->flags &= ~ASYNC_NORMAL_ACTIVE;
223 if (port->tty) { 223 if (port->tty) {
224 set_bit(TTY_IO_ERROR, &port->tty->flags); 224 set_bit(TTY_IO_ERROR, &port->tty->flags);
225 tty_kref_put(port->tty); 225 tty_kref_put(port->tty);
226 } 226 }
227 port->tty = NULL; 227 port->tty = NULL;
228 spin_unlock_irqrestore(&port->lock, flags); 228 spin_unlock_irqrestore(&port->lock, flags);
229 wake_up_interruptible(&port->open_wait); 229 wake_up_interruptible(&port->open_wait);
230 wake_up_interruptible(&port->delta_msr_wait); 230 wake_up_interruptible(&port->delta_msr_wait);
231 tty_port_shutdown(port); 231 tty_port_shutdown(port);
232 } 232 }
233 EXPORT_SYMBOL(tty_port_hangup); 233 EXPORT_SYMBOL(tty_port_hangup);
234 234
235 /** 235 /**
236 * tty_port_tty_wakeup - helper to wake up a tty
237 *
238 * @port: tty port
239 */
240 void tty_port_tty_wakeup(struct tty_port *port)
241 {
242 struct tty_struct *tty = tty_port_tty_get(port);
243
244 if (tty) {
245 tty_wakeup(tty);
246 tty_kref_put(tty);
247 }
248 }
249 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
250
251 /**
236 * tty_port_carrier_raised - carrier raised check 252 * tty_port_carrier_raised - carrier raised check
237 * @port: tty port 253 * @port: tty port
238 * 254 *
239 * Wrapper for the carrier detect logic. For the moment this is used 255 * Wrapper for the carrier detect logic. For the moment this is used
240 * to hide some internal details. This will eventually become entirely 256 * to hide some internal details. This will eventually become entirely
241 * internal to the tty port. 257 * internal to the tty port.
242 */ 258 */
243 259
244 int tty_port_carrier_raised(struct tty_port *port) 260 int tty_port_carrier_raised(struct tty_port *port)
245 { 261 {
246 if (port->ops->carrier_raised == NULL) 262 if (port->ops->carrier_raised == NULL)
247 return 1; 263 return 1;
248 return port->ops->carrier_raised(port); 264 return port->ops->carrier_raised(port);
249 } 265 }
250 EXPORT_SYMBOL(tty_port_carrier_raised); 266 EXPORT_SYMBOL(tty_port_carrier_raised);
251 267
252 /** 268 /**
253 * tty_port_raise_dtr_rts - Raise DTR/RTS 269 * tty_port_raise_dtr_rts - Raise DTR/RTS
254 * @port: tty port 270 * @port: tty port
255 * 271 *
256 * Wrapper for the DTR/RTS raise logic. For the moment this is used 272 * Wrapper for the DTR/RTS raise logic. For the moment this is used
257 * to hide some internal details. This will eventually become entirely 273 * to hide some internal details. This will eventually become entirely
258 * internal to the tty port. 274 * internal to the tty port.
259 */ 275 */
260 276
261 void tty_port_raise_dtr_rts(struct tty_port *port) 277 void tty_port_raise_dtr_rts(struct tty_port *port)
262 { 278 {
263 if (port->ops->dtr_rts) 279 if (port->ops->dtr_rts)
264 port->ops->dtr_rts(port, 1); 280 port->ops->dtr_rts(port, 1);
265 } 281 }
266 EXPORT_SYMBOL(tty_port_raise_dtr_rts); 282 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
267 283
268 /** 284 /**
269 * tty_port_lower_dtr_rts - Lower DTR/RTS 285 * tty_port_lower_dtr_rts - Lower DTR/RTS
270 * @port: tty port 286 * @port: tty port
271 * 287 *
272 * Wrapper for the DTR/RTS raise logic. For the moment this is used 288 * Wrapper for the DTR/RTS raise logic. For the moment this is used
273 * to hide some internal details. This will eventually become entirely 289 * to hide some internal details. This will eventually become entirely
274 * internal to the tty port. 290 * internal to the tty port.
275 */ 291 */
276 292
277 void tty_port_lower_dtr_rts(struct tty_port *port) 293 void tty_port_lower_dtr_rts(struct tty_port *port)
278 { 294 {
279 if (port->ops->dtr_rts) 295 if (port->ops->dtr_rts)
280 port->ops->dtr_rts(port, 0); 296 port->ops->dtr_rts(port, 0);
281 } 297 }
282 EXPORT_SYMBOL(tty_port_lower_dtr_rts); 298 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
283 299
284 /** 300 /**
285 * tty_port_block_til_ready - Waiting logic for tty open 301 * tty_port_block_til_ready - Waiting logic for tty open
286 * @port: the tty port being opened 302 * @port: the tty port being opened
287 * @tty: the tty device being bound 303 * @tty: the tty device being bound
288 * @filp: the file pointer of the opener 304 * @filp: the file pointer of the opener
289 * 305 *
290 * Implement the core POSIX/SuS tty behaviour when opening a tty device. 306 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
291 * Handles: 307 * Handles:
292 * - hangup (both before and during) 308 * - hangup (both before and during)
293 * - non blocking open 309 * - non blocking open
294 * - rts/dtr/dcd 310 * - rts/dtr/dcd
295 * - signals 311 * - signals
296 * - port flags and counts 312 * - port flags and counts
297 * 313 *
298 * The passed tty_port must implement the carrier_raised method if it can 314 * The passed tty_port must implement the carrier_raised method if it can
299 * do carrier detect and the dtr_rts method if it supports software 315 * do carrier detect and the dtr_rts method if it supports software
300 * management of these lines. Note that the dtr/rts raise is done each 316 * management of these lines. Note that the dtr/rts raise is done each
301 * iteration as a hangup may have previously dropped them while we wait. 317 * iteration as a hangup may have previously dropped them while we wait.
302 */ 318 */
303 319
304 int tty_port_block_til_ready(struct tty_port *port, 320 int tty_port_block_til_ready(struct tty_port *port,
305 struct tty_struct *tty, struct file *filp) 321 struct tty_struct *tty, struct file *filp)
306 { 322 {
307 int do_clocal = 0, retval; 323 int do_clocal = 0, retval;
308 unsigned long flags; 324 unsigned long flags;
309 DEFINE_WAIT(wait); 325 DEFINE_WAIT(wait);
310 326
311 /* block if port is in the process of being closed */ 327 /* block if port is in the process of being closed */
312 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) { 328 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
313 wait_event_interruptible_tty(tty, port->close_wait, 329 wait_event_interruptible_tty(tty, port->close_wait,
314 !(port->flags & ASYNC_CLOSING)); 330 !(port->flags & ASYNC_CLOSING));
315 if (port->flags & ASYNC_HUP_NOTIFY) 331 if (port->flags & ASYNC_HUP_NOTIFY)
316 return -EAGAIN; 332 return -EAGAIN;
317 else 333 else
318 return -ERESTARTSYS; 334 return -ERESTARTSYS;
319 } 335 }
320 336
321 /* if non-blocking mode is set we can pass directly to open unless 337 /* if non-blocking mode is set we can pass directly to open unless
322 the port has just hung up or is in another error state */ 338 the port has just hung up or is in another error state */
323 if (tty->flags & (1 << TTY_IO_ERROR)) { 339 if (tty->flags & (1 << TTY_IO_ERROR)) {
324 port->flags |= ASYNC_NORMAL_ACTIVE; 340 port->flags |= ASYNC_NORMAL_ACTIVE;
325 return 0; 341 return 0;
326 } 342 }
327 if (filp->f_flags & O_NONBLOCK) { 343 if (filp->f_flags & O_NONBLOCK) {
328 /* Indicate we are open */ 344 /* Indicate we are open */
329 if (tty->termios.c_cflag & CBAUD) 345 if (tty->termios.c_cflag & CBAUD)
330 tty_port_raise_dtr_rts(port); 346 tty_port_raise_dtr_rts(port);
331 port->flags |= ASYNC_NORMAL_ACTIVE; 347 port->flags |= ASYNC_NORMAL_ACTIVE;
332 return 0; 348 return 0;
333 } 349 }
334 350
335 if (C_CLOCAL(tty)) 351 if (C_CLOCAL(tty))
336 do_clocal = 1; 352 do_clocal = 1;
337 353
338 /* Block waiting until we can proceed. We may need to wait for the 354 /* Block waiting until we can proceed. We may need to wait for the
339 carrier, but we must also wait for any close that is in progress 355 carrier, but we must also wait for any close that is in progress
340 before the next open may complete */ 356 before the next open may complete */
341 357
342 retval = 0; 358 retval = 0;
343 359
344 /* The port lock protects the port counts */ 360 /* The port lock protects the port counts */
345 spin_lock_irqsave(&port->lock, flags); 361 spin_lock_irqsave(&port->lock, flags);
346 if (!tty_hung_up_p(filp)) 362 if (!tty_hung_up_p(filp))
347 port->count--; 363 port->count--;
348 port->blocked_open++; 364 port->blocked_open++;
349 spin_unlock_irqrestore(&port->lock, flags); 365 spin_unlock_irqrestore(&port->lock, flags);
350 366
351 while (1) { 367 while (1) {
352 /* Indicate we are open */ 368 /* Indicate we are open */
353 if (tty->termios.c_cflag & CBAUD) 369 if (tty->termios.c_cflag & CBAUD)
354 tty_port_raise_dtr_rts(port); 370 tty_port_raise_dtr_rts(port);
355 371
356 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); 372 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
357 /* Check for a hangup or uninitialised port. 373 /* Check for a hangup or uninitialised port.
358 Return accordingly */ 374 Return accordingly */
359 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) { 375 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
360 if (port->flags & ASYNC_HUP_NOTIFY) 376 if (port->flags & ASYNC_HUP_NOTIFY)
361 retval = -EAGAIN; 377 retval = -EAGAIN;
362 else 378 else
363 retval = -ERESTARTSYS; 379 retval = -ERESTARTSYS;
364 break; 380 break;
365 } 381 }
366 /* 382 /*
367 * Probe the carrier. For devices with no carrier detect 383 * Probe the carrier. For devices with no carrier detect
368 * tty_port_carrier_raised will always return true. 384 * tty_port_carrier_raised will always return true.
369 * Never ask drivers if CLOCAL is set, this causes troubles 385 * Never ask drivers if CLOCAL is set, this causes troubles
370 * on some hardware. 386 * on some hardware.
371 */ 387 */
372 if (!(port->flags & ASYNC_CLOSING) && 388 if (!(port->flags & ASYNC_CLOSING) &&
373 (do_clocal || tty_port_carrier_raised(port))) 389 (do_clocal || tty_port_carrier_raised(port)))
374 break; 390 break;
375 if (signal_pending(current)) { 391 if (signal_pending(current)) {
376 retval = -ERESTARTSYS; 392 retval = -ERESTARTSYS;
377 break; 393 break;
378 } 394 }
379 tty_unlock(tty); 395 tty_unlock(tty);
380 schedule(); 396 schedule();
381 tty_lock(tty); 397 tty_lock(tty);
382 } 398 }
383 finish_wait(&port->open_wait, &wait); 399 finish_wait(&port->open_wait, &wait);
384 400
385 /* Update counts. A parallel hangup will have set count to zero and 401 /* Update counts. A parallel hangup will have set count to zero and
386 we must not mess that up further */ 402 we must not mess that up further */
387 spin_lock_irqsave(&port->lock, flags); 403 spin_lock_irqsave(&port->lock, flags);
388 if (!tty_hung_up_p(filp)) 404 if (!tty_hung_up_p(filp))
389 port->count++; 405 port->count++;
390 port->blocked_open--; 406 port->blocked_open--;
391 if (retval == 0) 407 if (retval == 0)
392 port->flags |= ASYNC_NORMAL_ACTIVE; 408 port->flags |= ASYNC_NORMAL_ACTIVE;
393 spin_unlock_irqrestore(&port->lock, flags); 409 spin_unlock_irqrestore(&port->lock, flags);
394 return retval; 410 return retval;
395 } 411 }
396 EXPORT_SYMBOL(tty_port_block_til_ready); 412 EXPORT_SYMBOL(tty_port_block_til_ready);
397 413
398 int tty_port_close_start(struct tty_port *port, 414 int tty_port_close_start(struct tty_port *port,
399 struct tty_struct *tty, struct file *filp) 415 struct tty_struct *tty, struct file *filp)
400 { 416 {
401 unsigned long flags; 417 unsigned long flags;
402 418
403 spin_lock_irqsave(&port->lock, flags); 419 spin_lock_irqsave(&port->lock, flags);
404 if (tty_hung_up_p(filp)) { 420 if (tty_hung_up_p(filp)) {
405 spin_unlock_irqrestore(&port->lock, flags); 421 spin_unlock_irqrestore(&port->lock, flags);
406 return 0; 422 return 0;
407 } 423 }
408 424
409 if (tty->count == 1 && port->count != 1) { 425 if (tty->count == 1 && port->count != 1) {
410 printk(KERN_WARNING 426 printk(KERN_WARNING
411 "tty_port_close_start: tty->count = 1 port count = %d.\n", 427 "tty_port_close_start: tty->count = 1 port count = %d.\n",
412 port->count); 428 port->count);
413 port->count = 1; 429 port->count = 1;
414 } 430 }
415 if (--port->count < 0) { 431 if (--port->count < 0) {
416 printk(KERN_WARNING "tty_port_close_start: count = %d\n", 432 printk(KERN_WARNING "tty_port_close_start: count = %d\n",
417 port->count); 433 port->count);
418 port->count = 0; 434 port->count = 0;
419 } 435 }
420 436
421 if (port->count) { 437 if (port->count) {
422 spin_unlock_irqrestore(&port->lock, flags); 438 spin_unlock_irqrestore(&port->lock, flags);
423 if (port->ops->drop) 439 if (port->ops->drop)
424 port->ops->drop(port); 440 port->ops->drop(port);
425 return 0; 441 return 0;
426 } 442 }
427 set_bit(ASYNCB_CLOSING, &port->flags); 443 set_bit(ASYNCB_CLOSING, &port->flags);
428 tty->closing = 1; 444 tty->closing = 1;
429 spin_unlock_irqrestore(&port->lock, flags); 445 spin_unlock_irqrestore(&port->lock, flags);
430 /* Don't block on a stalled port, just pull the chain */ 446 /* Don't block on a stalled port, just pull the chain */
431 if (tty->flow_stopped) 447 if (tty->flow_stopped)
432 tty_driver_flush_buffer(tty); 448 tty_driver_flush_buffer(tty);
433 if (test_bit(ASYNCB_INITIALIZED, &port->flags) && 449 if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
434 port->closing_wait != ASYNC_CLOSING_WAIT_NONE) 450 port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
435 tty_wait_until_sent_from_close(tty, port->closing_wait); 451 tty_wait_until_sent_from_close(tty, port->closing_wait);
436 if (port->drain_delay) { 452 if (port->drain_delay) {
437 unsigned int bps = tty_get_baud_rate(tty); 453 unsigned int bps = tty_get_baud_rate(tty);
438 long timeout; 454 long timeout;
439 455
440 if (bps > 1200) 456 if (bps > 1200)
441 timeout = max_t(long, 457 timeout = max_t(long,
442 (HZ * 10 * port->drain_delay) / bps, HZ / 10); 458 (HZ * 10 * port->drain_delay) / bps, HZ / 10);
443 else 459 else
444 timeout = 2 * HZ; 460 timeout = 2 * HZ;
445 schedule_timeout_interruptible(timeout); 461 schedule_timeout_interruptible(timeout);
446 } 462 }
447 /* Flush the ldisc buffering */ 463 /* Flush the ldisc buffering */
448 tty_ldisc_flush(tty); 464 tty_ldisc_flush(tty);
449 465
450 /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to 466 /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
451 hang up the line */ 467 hang up the line */
452 if (tty->termios.c_cflag & HUPCL) 468 if (tty->termios.c_cflag & HUPCL)
453 tty_port_lower_dtr_rts(port); 469 tty_port_lower_dtr_rts(port);
454 470
455 /* Don't call port->drop for the last reference. Callers will want 471 /* Don't call port->drop for the last reference. Callers will want
456 to drop the last active reference in ->shutdown() or the tty 472 to drop the last active reference in ->shutdown() or the tty
457 shutdown path */ 473 shutdown path */
458 return 1; 474 return 1;
459 } 475 }
460 EXPORT_SYMBOL(tty_port_close_start); 476 EXPORT_SYMBOL(tty_port_close_start);
461 477
462 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty) 478 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
463 { 479 {
464 unsigned long flags; 480 unsigned long flags;
465 481
466 spin_lock_irqsave(&port->lock, flags); 482 spin_lock_irqsave(&port->lock, flags);
467 tty->closing = 0; 483 tty->closing = 0;
468 484
469 if (port->blocked_open) { 485 if (port->blocked_open) {
470 spin_unlock_irqrestore(&port->lock, flags); 486 spin_unlock_irqrestore(&port->lock, flags);
471 if (port->close_delay) { 487 if (port->close_delay) {
472 msleep_interruptible( 488 msleep_interruptible(
473 jiffies_to_msecs(port->close_delay)); 489 jiffies_to_msecs(port->close_delay));
474 } 490 }
475 spin_lock_irqsave(&port->lock, flags); 491 spin_lock_irqsave(&port->lock, flags);
476 wake_up_interruptible(&port->open_wait); 492 wake_up_interruptible(&port->open_wait);
477 } 493 }
478 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING); 494 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
479 wake_up_interruptible(&port->close_wait); 495 wake_up_interruptible(&port->close_wait);
480 spin_unlock_irqrestore(&port->lock, flags); 496 spin_unlock_irqrestore(&port->lock, flags);
481 } 497 }
482 EXPORT_SYMBOL(tty_port_close_end); 498 EXPORT_SYMBOL(tty_port_close_end);
483 499
484 void tty_port_close(struct tty_port *port, struct tty_struct *tty, 500 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
485 struct file *filp) 501 struct file *filp)
486 { 502 {
487 if (tty_port_close_start(port, tty, filp) == 0) 503 if (tty_port_close_start(port, tty, filp) == 0)
488 return; 504 return;
489 tty_port_shutdown(port); 505 tty_port_shutdown(port);
490 set_bit(TTY_IO_ERROR, &tty->flags); 506 set_bit(TTY_IO_ERROR, &tty->flags);
491 tty_port_close_end(port, tty); 507 tty_port_close_end(port, tty);
492 tty_port_tty_set(port, NULL); 508 tty_port_tty_set(port, NULL);
493 } 509 }
494 EXPORT_SYMBOL(tty_port_close); 510 EXPORT_SYMBOL(tty_port_close);
495 511
496 /** 512 /**
497 * tty_port_install - generic tty->ops->install handler 513 * tty_port_install - generic tty->ops->install handler
498 * @port: tty_port of the device 514 * @port: tty_port of the device
499 * @driver: tty_driver for this device 515 * @driver: tty_driver for this device
500 * @tty: tty to be installed 516 * @tty: tty to be installed
501 * 517 *
502 * It is the same as tty_standard_install except the provided @port is linked 518 * It is the same as tty_standard_install except the provided @port is linked
503 * to a concrete tty specified by @tty. Use this or tty_port_register_device 519 * to a concrete tty specified by @tty. Use this or tty_port_register_device
504 * (or both). Call tty_port_link_device as a last resort. 520 * (or both). Call tty_port_link_device as a last resort.
505 */ 521 */
506 int tty_port_install(struct tty_port *port, struct tty_driver *driver, 522 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
507 struct tty_struct *tty) 523 struct tty_struct *tty)
508 { 524 {
509 tty->port = port; 525 tty->port = port;
510 return tty_standard_install(driver, tty); 526 return tty_standard_install(driver, tty);
511 } 527 }
512 EXPORT_SYMBOL_GPL(tty_port_install); 528 EXPORT_SYMBOL_GPL(tty_port_install);
513 529
514 int tty_port_open(struct tty_port *port, struct tty_struct *tty, 530 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
515 struct file *filp) 531 struct file *filp)
516 { 532 {
517 spin_lock_irq(&port->lock); 533 spin_lock_irq(&port->lock);
518 if (!tty_hung_up_p(filp)) 534 if (!tty_hung_up_p(filp))
519 ++port->count; 535 ++port->count;
520 spin_unlock_irq(&port->lock); 536 spin_unlock_irq(&port->lock);
521 tty_port_tty_set(port, tty); 537 tty_port_tty_set(port, tty);
522 538
523 /* 539 /*
524 * Do the device-specific open only if the hardware isn't 540 * Do the device-specific open only if the hardware isn't
525 * already initialized. Serialize open and shutdown using the 541 * already initialized. Serialize open and shutdown using the
526 * port mutex. 542 * port mutex.
527 */ 543 */
528 544
529 mutex_lock(&port->mutex); 545 mutex_lock(&port->mutex);
530 546
531 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) { 547 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
532 clear_bit(TTY_IO_ERROR, &tty->flags); 548 clear_bit(TTY_IO_ERROR, &tty->flags);
533 if (port->ops->activate) { 549 if (port->ops->activate) {
534 int retval = port->ops->activate(port, tty); 550 int retval = port->ops->activate(port, tty);
535 if (retval) { 551 if (retval) {
536 mutex_unlock(&port->mutex); 552 mutex_unlock(&port->mutex);
537 return retval; 553 return retval;
538 } 554 }
539 } 555 }
540 set_bit(ASYNCB_INITIALIZED, &port->flags); 556 set_bit(ASYNCB_INITIALIZED, &port->flags);
541 } 557 }
542 mutex_unlock(&port->mutex); 558 mutex_unlock(&port->mutex);
543 return tty_port_block_til_ready(port, tty, filp); 559 return tty_port_block_til_ready(port, tty, filp);
544 } 560 }
545 561
546 EXPORT_SYMBOL(tty_port_open); 562 EXPORT_SYMBOL(tty_port_open);
547 563
drivers/usb/class/cdc-acm.c
1 /* 1 /*
2 * cdc-acm.c 2 * cdc-acm.c
3 * 3 *
4 * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de> 4 * Copyright (c) 1999 Armin Fuerst <fuerst@in.tum.de>
5 * Copyright (c) 1999 Pavel Machek <pavel@ucw.cz> 5 * Copyright (c) 1999 Pavel Machek <pavel@ucw.cz>
6 * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com> 6 * Copyright (c) 1999 Johannes Erdfelt <johannes@erdfelt.com>
7 * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz> 7 * Copyright (c) 2000 Vojtech Pavlik <vojtech@suse.cz>
8 * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name> 8 * Copyright (c) 2004 Oliver Neukum <oliver@neukum.name>
9 * Copyright (c) 2005 David Kubicek <dave@awk.cz> 9 * Copyright (c) 2005 David Kubicek <dave@awk.cz>
10 * Copyright (c) 2011 Johan Hovold <jhovold@gmail.com> 10 * Copyright (c) 2011 Johan Hovold <jhovold@gmail.com>
11 * 11 *
12 * USB Abstract Control Model driver for USB modems and ISDN adapters 12 * USB Abstract Control Model driver for USB modems and ISDN adapters
13 * 13 *
14 * Sponsored by SuSE 14 * Sponsored by SuSE
15 * 15 *
16 * This program is free software; you can redistribute it and/or modify 16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by 17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or 18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version. 19 * (at your option) any later version.
20 * 20 *
21 * This program is distributed in the hope that it will be useful, 21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details. 24 * GNU General Public License for more details.
25 * 25 *
26 * You should have received a copy of the GNU General Public License 26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software 27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 */ 29 */
30 30
31 #undef DEBUG 31 #undef DEBUG
32 #undef VERBOSE_DEBUG 32 #undef VERBOSE_DEBUG
33 33
34 #include <linux/kernel.h> 34 #include <linux/kernel.h>
35 #include <linux/errno.h> 35 #include <linux/errno.h>
36 #include <linux/init.h> 36 #include <linux/init.h>
37 #include <linux/slab.h> 37 #include <linux/slab.h>
38 #include <linux/tty.h> 38 #include <linux/tty.h>
39 #include <linux/serial.h> 39 #include <linux/serial.h>
40 #include <linux/tty_driver.h> 40 #include <linux/tty_driver.h>
41 #include <linux/tty_flip.h> 41 #include <linux/tty_flip.h>
42 #include <linux/module.h> 42 #include <linux/module.h>
43 #include <linux/mutex.h> 43 #include <linux/mutex.h>
44 #include <linux/uaccess.h> 44 #include <linux/uaccess.h>
45 #include <linux/usb.h> 45 #include <linux/usb.h>
46 #include <linux/usb/cdc.h> 46 #include <linux/usb/cdc.h>
47 #include <asm/byteorder.h> 47 #include <asm/byteorder.h>
48 #include <asm/unaligned.h> 48 #include <asm/unaligned.h>
49 #include <linux/list.h> 49 #include <linux/list.h>
50 50
51 #include "cdc-acm.h" 51 #include "cdc-acm.h"
52 52
53 53
54 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold" 54 #define DRIVER_AUTHOR "Armin Fuerst, Pavel Machek, Johannes Erdfelt, Vojtech Pavlik, David Kubicek, Johan Hovold"
55 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters" 55 #define DRIVER_DESC "USB Abstract Control Model driver for USB modems and ISDN adapters"
56 56
57 static struct usb_driver acm_driver; 57 static struct usb_driver acm_driver;
58 static struct tty_driver *acm_tty_driver; 58 static struct tty_driver *acm_tty_driver;
59 static struct acm *acm_table[ACM_TTY_MINORS]; 59 static struct acm *acm_table[ACM_TTY_MINORS];
60 60
61 static DEFINE_MUTEX(acm_table_lock); 61 static DEFINE_MUTEX(acm_table_lock);
62 62
63 /* 63 /*
64 * acm_table accessors 64 * acm_table accessors
65 */ 65 */
66 66
67 /* 67 /*
68 * Look up an ACM structure by index. If found and not disconnected, increment 68 * Look up an ACM structure by index. If found and not disconnected, increment
69 * its refcount and return it with its mutex held. 69 * its refcount and return it with its mutex held.
70 */ 70 */
71 static struct acm *acm_get_by_index(unsigned index) 71 static struct acm *acm_get_by_index(unsigned index)
72 { 72 {
73 struct acm *acm; 73 struct acm *acm;
74 74
75 mutex_lock(&acm_table_lock); 75 mutex_lock(&acm_table_lock);
76 acm = acm_table[index]; 76 acm = acm_table[index];
77 if (acm) { 77 if (acm) {
78 mutex_lock(&acm->mutex); 78 mutex_lock(&acm->mutex);
79 if (acm->disconnected) { 79 if (acm->disconnected) {
80 mutex_unlock(&acm->mutex); 80 mutex_unlock(&acm->mutex);
81 acm = NULL; 81 acm = NULL;
82 } else { 82 } else {
83 tty_port_get(&acm->port); 83 tty_port_get(&acm->port);
84 mutex_unlock(&acm->mutex); 84 mutex_unlock(&acm->mutex);
85 } 85 }
86 } 86 }
87 mutex_unlock(&acm_table_lock); 87 mutex_unlock(&acm_table_lock);
88 return acm; 88 return acm;
89 } 89 }
90 90
91 /* 91 /*
92 * Try to find an available minor number and if found, associate it with 'acm'. 92 * Try to find an available minor number and if found, associate it with 'acm'.
93 */ 93 */
94 static int acm_alloc_minor(struct acm *acm) 94 static int acm_alloc_minor(struct acm *acm)
95 { 95 {
96 int minor; 96 int minor;
97 97
98 mutex_lock(&acm_table_lock); 98 mutex_lock(&acm_table_lock);
99 for (minor = 0; minor < ACM_TTY_MINORS; minor++) { 99 for (minor = 0; minor < ACM_TTY_MINORS; minor++) {
100 if (!acm_table[minor]) { 100 if (!acm_table[minor]) {
101 acm_table[minor] = acm; 101 acm_table[minor] = acm;
102 break; 102 break;
103 } 103 }
104 } 104 }
105 mutex_unlock(&acm_table_lock); 105 mutex_unlock(&acm_table_lock);
106 106
107 return minor; 107 return minor;
108 } 108 }
109 109
110 /* Release the minor number associated with 'acm'. */ 110 /* Release the minor number associated with 'acm'. */
111 static void acm_release_minor(struct acm *acm) 111 static void acm_release_minor(struct acm *acm)
112 { 112 {
113 mutex_lock(&acm_table_lock); 113 mutex_lock(&acm_table_lock);
114 acm_table[acm->minor] = NULL; 114 acm_table[acm->minor] = NULL;
115 mutex_unlock(&acm_table_lock); 115 mutex_unlock(&acm_table_lock);
116 } 116 }
117 117
118 /* 118 /*
119 * Functions for ACM control messages. 119 * Functions for ACM control messages.
120 */ 120 */
121 121
122 static int acm_ctrl_msg(struct acm *acm, int request, int value, 122 static int acm_ctrl_msg(struct acm *acm, int request, int value,
123 void *buf, int len) 123 void *buf, int len)
124 { 124 {
125 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0), 125 int retval = usb_control_msg(acm->dev, usb_sndctrlpipe(acm->dev, 0),
126 request, USB_RT_ACM, value, 126 request, USB_RT_ACM, value,
127 acm->control->altsetting[0].desc.bInterfaceNumber, 127 acm->control->altsetting[0].desc.bInterfaceNumber,
128 buf, len, 5000); 128 buf, len, 5000);
129 dev_dbg(&acm->control->dev, 129 dev_dbg(&acm->control->dev,
130 "%s - rq 0x%02x, val %#x, len %#x, result %d\n", 130 "%s - rq 0x%02x, val %#x, len %#x, result %d\n",
131 __func__, request, value, len, retval); 131 __func__, request, value, len, retval);
132 return retval < 0 ? retval : 0; 132 return retval < 0 ? retval : 0;
133 } 133 }
134 134
135 /* devices aren't required to support these requests. 135 /* devices aren't required to support these requests.
136 * the cdc acm descriptor tells whether they do... 136 * the cdc acm descriptor tells whether they do...
137 */ 137 */
138 #define acm_set_control(acm, control) \ 138 #define acm_set_control(acm, control) \
139 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0) 139 acm_ctrl_msg(acm, USB_CDC_REQ_SET_CONTROL_LINE_STATE, control, NULL, 0)
140 #define acm_set_line(acm, line) \ 140 #define acm_set_line(acm, line) \
141 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line)) 141 acm_ctrl_msg(acm, USB_CDC_REQ_SET_LINE_CODING, 0, line, sizeof *(line))
142 #define acm_send_break(acm, ms) \ 142 #define acm_send_break(acm, ms) \
143 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0) 143 acm_ctrl_msg(acm, USB_CDC_REQ_SEND_BREAK, ms, NULL, 0)
144 144
145 /* 145 /*
146 * Write buffer management. 146 * Write buffer management.
147 * All of these assume proper locks taken by the caller. 147 * All of these assume proper locks taken by the caller.
148 */ 148 */
149 149
150 static int acm_wb_alloc(struct acm *acm) 150 static int acm_wb_alloc(struct acm *acm)
151 { 151 {
152 int i, wbn; 152 int i, wbn;
153 struct acm_wb *wb; 153 struct acm_wb *wb;
154 154
155 wbn = 0; 155 wbn = 0;
156 i = 0; 156 i = 0;
157 for (;;) { 157 for (;;) {
158 wb = &acm->wb[wbn]; 158 wb = &acm->wb[wbn];
159 if (!wb->use) { 159 if (!wb->use) {
160 wb->use = 1; 160 wb->use = 1;
161 return wbn; 161 return wbn;
162 } 162 }
163 wbn = (wbn + 1) % ACM_NW; 163 wbn = (wbn + 1) % ACM_NW;
164 if (++i >= ACM_NW) 164 if (++i >= ACM_NW)
165 return -1; 165 return -1;
166 } 166 }
167 } 167 }
168 168
169 static int acm_wb_is_avail(struct acm *acm) 169 static int acm_wb_is_avail(struct acm *acm)
170 { 170 {
171 int i, n; 171 int i, n;
172 unsigned long flags; 172 unsigned long flags;
173 173
174 n = ACM_NW; 174 n = ACM_NW;
175 spin_lock_irqsave(&acm->write_lock, flags); 175 spin_lock_irqsave(&acm->write_lock, flags);
176 for (i = 0; i < ACM_NW; i++) 176 for (i = 0; i < ACM_NW; i++)
177 n -= acm->wb[i].use; 177 n -= acm->wb[i].use;
178 spin_unlock_irqrestore(&acm->write_lock, flags); 178 spin_unlock_irqrestore(&acm->write_lock, flags);
179 return n; 179 return n;
180 } 180 }
181 181
182 /* 182 /*
183 * Finish write. Caller must hold acm->write_lock 183 * Finish write. Caller must hold acm->write_lock
184 */ 184 */
185 static void acm_write_done(struct acm *acm, struct acm_wb *wb) 185 static void acm_write_done(struct acm *acm, struct acm_wb *wb)
186 { 186 {
187 wb->use = 0; 187 wb->use = 0;
188 acm->transmitting--; 188 acm->transmitting--;
189 usb_autopm_put_interface_async(acm->control); 189 usb_autopm_put_interface_async(acm->control);
190 } 190 }
191 191
192 /* 192 /*
193 * Poke write. 193 * Poke write.
194 * 194 *
195 * the caller is responsible for locking 195 * the caller is responsible for locking
196 */ 196 */
197 197
198 static int acm_start_wb(struct acm *acm, struct acm_wb *wb) 198 static int acm_start_wb(struct acm *acm, struct acm_wb *wb)
199 { 199 {
200 int rc; 200 int rc;
201 201
202 acm->transmitting++; 202 acm->transmitting++;
203 203
204 wb->urb->transfer_buffer = wb->buf; 204 wb->urb->transfer_buffer = wb->buf;
205 wb->urb->transfer_dma = wb->dmah; 205 wb->urb->transfer_dma = wb->dmah;
206 wb->urb->transfer_buffer_length = wb->len; 206 wb->urb->transfer_buffer_length = wb->len;
207 wb->urb->dev = acm->dev; 207 wb->urb->dev = acm->dev;
208 208
209 rc = usb_submit_urb(wb->urb, GFP_ATOMIC); 209 rc = usb_submit_urb(wb->urb, GFP_ATOMIC);
210 if (rc < 0) { 210 if (rc < 0) {
211 dev_err(&acm->data->dev, 211 dev_err(&acm->data->dev,
212 "%s - usb_submit_urb(write bulk) failed: %d\n", 212 "%s - usb_submit_urb(write bulk) failed: %d\n",
213 __func__, rc); 213 __func__, rc);
214 acm_write_done(acm, wb); 214 acm_write_done(acm, wb);
215 } 215 }
216 return rc; 216 return rc;
217 } 217 }
218 218
219 static int acm_write_start(struct acm *acm, int wbn) 219 static int acm_write_start(struct acm *acm, int wbn)
220 { 220 {
221 unsigned long flags; 221 unsigned long flags;
222 struct acm_wb *wb = &acm->wb[wbn]; 222 struct acm_wb *wb = &acm->wb[wbn];
223 int rc; 223 int rc;
224 224
225 spin_lock_irqsave(&acm->write_lock, flags); 225 spin_lock_irqsave(&acm->write_lock, flags);
226 if (!acm->dev) { 226 if (!acm->dev) {
227 wb->use = 0; 227 wb->use = 0;
228 spin_unlock_irqrestore(&acm->write_lock, flags); 228 spin_unlock_irqrestore(&acm->write_lock, flags);
229 return -ENODEV; 229 return -ENODEV;
230 } 230 }
231 231
232 dev_vdbg(&acm->data->dev, "%s - susp_count %d\n", __func__, 232 dev_vdbg(&acm->data->dev, "%s - susp_count %d\n", __func__,
233 acm->susp_count); 233 acm->susp_count);
234 usb_autopm_get_interface_async(acm->control); 234 usb_autopm_get_interface_async(acm->control);
235 if (acm->susp_count) { 235 if (acm->susp_count) {
236 if (!acm->delayed_wb) 236 if (!acm->delayed_wb)
237 acm->delayed_wb = wb; 237 acm->delayed_wb = wb;
238 else 238 else
239 usb_autopm_put_interface_async(acm->control); 239 usb_autopm_put_interface_async(acm->control);
240 spin_unlock_irqrestore(&acm->write_lock, flags); 240 spin_unlock_irqrestore(&acm->write_lock, flags);
241 return 0; /* A white lie */ 241 return 0; /* A white lie */
242 } 242 }
243 usb_mark_last_busy(acm->dev); 243 usb_mark_last_busy(acm->dev);
244 244
245 rc = acm_start_wb(acm, wb); 245 rc = acm_start_wb(acm, wb);
246 spin_unlock_irqrestore(&acm->write_lock, flags); 246 spin_unlock_irqrestore(&acm->write_lock, flags);
247 247
248 return rc; 248 return rc;
249 249
250 } 250 }
251 /* 251 /*
252 * attributes exported through sysfs 252 * attributes exported through sysfs
253 */ 253 */
254 static ssize_t show_caps 254 static ssize_t show_caps
255 (struct device *dev, struct device_attribute *attr, char *buf) 255 (struct device *dev, struct device_attribute *attr, char *buf)
256 { 256 {
257 struct usb_interface *intf = to_usb_interface(dev); 257 struct usb_interface *intf = to_usb_interface(dev);
258 struct acm *acm = usb_get_intfdata(intf); 258 struct acm *acm = usb_get_intfdata(intf);
259 259
260 return sprintf(buf, "%d", acm->ctrl_caps); 260 return sprintf(buf, "%d", acm->ctrl_caps);
261 } 261 }
262 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL); 262 static DEVICE_ATTR(bmCapabilities, S_IRUGO, show_caps, NULL);
263 263
264 static ssize_t show_country_codes 264 static ssize_t show_country_codes
265 (struct device *dev, struct device_attribute *attr, char *buf) 265 (struct device *dev, struct device_attribute *attr, char *buf)
266 { 266 {
267 struct usb_interface *intf = to_usb_interface(dev); 267 struct usb_interface *intf = to_usb_interface(dev);
268 struct acm *acm = usb_get_intfdata(intf); 268 struct acm *acm = usb_get_intfdata(intf);
269 269
270 memcpy(buf, acm->country_codes, acm->country_code_size); 270 memcpy(buf, acm->country_codes, acm->country_code_size);
271 return acm->country_code_size; 271 return acm->country_code_size;
272 } 272 }
273 273
274 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL); 274 static DEVICE_ATTR(wCountryCodes, S_IRUGO, show_country_codes, NULL);
275 275
276 static ssize_t show_country_rel_date 276 static ssize_t show_country_rel_date
277 (struct device *dev, struct device_attribute *attr, char *buf) 277 (struct device *dev, struct device_attribute *attr, char *buf)
278 { 278 {
279 struct usb_interface *intf = to_usb_interface(dev); 279 struct usb_interface *intf = to_usb_interface(dev);
280 struct acm *acm = usb_get_intfdata(intf); 280 struct acm *acm = usb_get_intfdata(intf);
281 281
282 return sprintf(buf, "%d", acm->country_rel_date); 282 return sprintf(buf, "%d", acm->country_rel_date);
283 } 283 }
284 284
285 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL); 285 static DEVICE_ATTR(iCountryCodeRelDate, S_IRUGO, show_country_rel_date, NULL);
286 /* 286 /*
287 * Interrupt handlers for various ACM device responses 287 * Interrupt handlers for various ACM device responses
288 */ 288 */
289 289
290 /* control interface reports status changes with "interrupt" transfers */ 290 /* control interface reports status changes with "interrupt" transfers */
291 static void acm_ctrl_irq(struct urb *urb) 291 static void acm_ctrl_irq(struct urb *urb)
292 { 292 {
293 struct acm *acm = urb->context; 293 struct acm *acm = urb->context;
294 struct usb_cdc_notification *dr = urb->transfer_buffer; 294 struct usb_cdc_notification *dr = urb->transfer_buffer;
295 struct tty_struct *tty; 295 struct tty_struct *tty;
296 unsigned char *data; 296 unsigned char *data;
297 int newctrl; 297 int newctrl;
298 int retval; 298 int retval;
299 int status = urb->status; 299 int status = urb->status;
300 300
301 switch (status) { 301 switch (status) {
302 case 0: 302 case 0:
303 /* success */ 303 /* success */
304 break; 304 break;
305 case -ECONNRESET: 305 case -ECONNRESET:
306 case -ENOENT: 306 case -ENOENT:
307 case -ESHUTDOWN: 307 case -ESHUTDOWN:
308 /* this urb is terminated, clean up */ 308 /* this urb is terminated, clean up */
309 dev_dbg(&acm->control->dev, 309 dev_dbg(&acm->control->dev,
310 "%s - urb shutting down with status: %d\n", 310 "%s - urb shutting down with status: %d\n",
311 __func__, status); 311 __func__, status);
312 return; 312 return;
313 default: 313 default:
314 dev_dbg(&acm->control->dev, 314 dev_dbg(&acm->control->dev,
315 "%s - nonzero urb status received: %d\n", 315 "%s - nonzero urb status received: %d\n",
316 __func__, status); 316 __func__, status);
317 goto exit; 317 goto exit;
318 } 318 }
319 319
320 usb_mark_last_busy(acm->dev); 320 usb_mark_last_busy(acm->dev);
321 321
322 data = (unsigned char *)(dr + 1); 322 data = (unsigned char *)(dr + 1);
323 switch (dr->bNotificationType) { 323 switch (dr->bNotificationType) {
324 case USB_CDC_NOTIFY_NETWORK_CONNECTION: 324 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
325 dev_dbg(&acm->control->dev, "%s - network connection: %d\n", 325 dev_dbg(&acm->control->dev, "%s - network connection: %d\n",
326 __func__, dr->wValue); 326 __func__, dr->wValue);
327 break; 327 break;
328 328
329 case USB_CDC_NOTIFY_SERIAL_STATE: 329 case USB_CDC_NOTIFY_SERIAL_STATE:
330 tty = tty_port_tty_get(&acm->port); 330 tty = tty_port_tty_get(&acm->port);
331 newctrl = get_unaligned_le16(data); 331 newctrl = get_unaligned_le16(data);
332 332
333 if (tty) { 333 if (tty) {
334 if (!acm->clocal && 334 if (!acm->clocal &&
335 (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) { 335 (acm->ctrlin & ~newctrl & ACM_CTRL_DCD)) {
336 dev_dbg(&acm->control->dev, 336 dev_dbg(&acm->control->dev,
337 "%s - calling hangup\n", __func__); 337 "%s - calling hangup\n", __func__);
338 tty_hangup(tty); 338 tty_hangup(tty);
339 } 339 }
340 tty_kref_put(tty); 340 tty_kref_put(tty);
341 } 341 }
342 342
343 acm->ctrlin = newctrl; 343 acm->ctrlin = newctrl;
344 344
345 dev_dbg(&acm->control->dev, 345 dev_dbg(&acm->control->dev,
346 "%s - input control lines: dcd%c dsr%c break%c " 346 "%s - input control lines: dcd%c dsr%c break%c "
347 "ring%c framing%c parity%c overrun%c\n", 347 "ring%c framing%c parity%c overrun%c\n",
348 __func__, 348 __func__,
349 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-', 349 acm->ctrlin & ACM_CTRL_DCD ? '+' : '-',
350 acm->ctrlin & ACM_CTRL_DSR ? '+' : '-', 350 acm->ctrlin & ACM_CTRL_DSR ? '+' : '-',
351 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-', 351 acm->ctrlin & ACM_CTRL_BRK ? '+' : '-',
352 acm->ctrlin & ACM_CTRL_RI ? '+' : '-', 352 acm->ctrlin & ACM_CTRL_RI ? '+' : '-',
353 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-', 353 acm->ctrlin & ACM_CTRL_FRAMING ? '+' : '-',
354 acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-', 354 acm->ctrlin & ACM_CTRL_PARITY ? '+' : '-',
355 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-'); 355 acm->ctrlin & ACM_CTRL_OVERRUN ? '+' : '-');
356 break; 356 break;
357 357
358 default: 358 default:
359 dev_dbg(&acm->control->dev, 359 dev_dbg(&acm->control->dev,
360 "%s - unknown notification %d received: index %d " 360 "%s - unknown notification %d received: index %d "
361 "len %d data0 %d data1 %d\n", 361 "len %d data0 %d data1 %d\n",
362 __func__, 362 __func__,
363 dr->bNotificationType, dr->wIndex, 363 dr->bNotificationType, dr->wIndex,
364 dr->wLength, data[0], data[1]); 364 dr->wLength, data[0], data[1]);
365 break; 365 break;
366 } 366 }
367 exit: 367 exit:
368 retval = usb_submit_urb(urb, GFP_ATOMIC); 368 retval = usb_submit_urb(urb, GFP_ATOMIC);
369 if (retval) 369 if (retval)
370 dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n", 370 dev_err(&acm->control->dev, "%s - usb_submit_urb failed: %d\n",
371 __func__, retval); 371 __func__, retval);
372 } 372 }
373 373
374 static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags) 374 static int acm_submit_read_urb(struct acm *acm, int index, gfp_t mem_flags)
375 { 375 {
376 int res; 376 int res;
377 377
378 if (!test_and_clear_bit(index, &acm->read_urbs_free)) 378 if (!test_and_clear_bit(index, &acm->read_urbs_free))
379 return 0; 379 return 0;
380 380
381 dev_vdbg(&acm->data->dev, "%s - urb %d\n", __func__, index); 381 dev_vdbg(&acm->data->dev, "%s - urb %d\n", __func__, index);
382 382
383 res = usb_submit_urb(acm->read_urbs[index], mem_flags); 383 res = usb_submit_urb(acm->read_urbs[index], mem_flags);
384 if (res) { 384 if (res) {
385 if (res != -EPERM) { 385 if (res != -EPERM) {
386 dev_err(&acm->data->dev, 386 dev_err(&acm->data->dev,
387 "%s - usb_submit_urb failed: %d\n", 387 "%s - usb_submit_urb failed: %d\n",
388 __func__, res); 388 __func__, res);
389 } 389 }
390 set_bit(index, &acm->read_urbs_free); 390 set_bit(index, &acm->read_urbs_free);
391 return res; 391 return res;
392 } 392 }
393 393
394 return 0; 394 return 0;
395 } 395 }
396 396
397 static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags) 397 static int acm_submit_read_urbs(struct acm *acm, gfp_t mem_flags)
398 { 398 {
399 int res; 399 int res;
400 int i; 400 int i;
401 401
402 for (i = 0; i < acm->rx_buflimit; ++i) { 402 for (i = 0; i < acm->rx_buflimit; ++i) {
403 res = acm_submit_read_urb(acm, i, mem_flags); 403 res = acm_submit_read_urb(acm, i, mem_flags);
404 if (res) 404 if (res)
405 return res; 405 return res;
406 } 406 }
407 407
408 return 0; 408 return 0;
409 } 409 }
410 410
411 static void acm_process_read_urb(struct acm *acm, struct urb *urb) 411 static void acm_process_read_urb(struct acm *acm, struct urb *urb)
412 { 412 {
413 if (!urb->actual_length) 413 if (!urb->actual_length)
414 return; 414 return;
415 415
416 tty_insert_flip_string(&acm->port, urb->transfer_buffer, 416 tty_insert_flip_string(&acm->port, urb->transfer_buffer,
417 urb->actual_length); 417 urb->actual_length);
418 tty_flip_buffer_push(&acm->port); 418 tty_flip_buffer_push(&acm->port);
419 } 419 }
420 420
421 static void acm_read_bulk_callback(struct urb *urb) 421 static void acm_read_bulk_callback(struct urb *urb)
422 { 422 {
423 struct acm_rb *rb = urb->context; 423 struct acm_rb *rb = urb->context;
424 struct acm *acm = rb->instance; 424 struct acm *acm = rb->instance;
425 unsigned long flags; 425 unsigned long flags;
426 426
427 dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__, 427 dev_vdbg(&acm->data->dev, "%s - urb %d, len %d\n", __func__,
428 rb->index, urb->actual_length); 428 rb->index, urb->actual_length);
429 set_bit(rb->index, &acm->read_urbs_free); 429 set_bit(rb->index, &acm->read_urbs_free);
430 430
431 if (!acm->dev) { 431 if (!acm->dev) {
432 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__); 432 dev_dbg(&acm->data->dev, "%s - disconnected\n", __func__);
433 return; 433 return;
434 } 434 }
435 usb_mark_last_busy(acm->dev); 435 usb_mark_last_busy(acm->dev);
436 436
437 if (urb->status) { 437 if (urb->status) {
438 dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n", 438 dev_dbg(&acm->data->dev, "%s - non-zero urb status: %d\n",
439 __func__, urb->status); 439 __func__, urb->status);
440 return; 440 return;
441 } 441 }
442 acm_process_read_urb(acm, urb); 442 acm_process_read_urb(acm, urb);
443 443
444 /* throttle device if requested by tty */ 444 /* throttle device if requested by tty */
445 spin_lock_irqsave(&acm->read_lock, flags); 445 spin_lock_irqsave(&acm->read_lock, flags);
446 acm->throttled = acm->throttle_req; 446 acm->throttled = acm->throttle_req;
447 if (!acm->throttled && !acm->susp_count) { 447 if (!acm->throttled && !acm->susp_count) {
448 spin_unlock_irqrestore(&acm->read_lock, flags); 448 spin_unlock_irqrestore(&acm->read_lock, flags);
449 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC); 449 acm_submit_read_urb(acm, rb->index, GFP_ATOMIC);
450 } else { 450 } else {
451 spin_unlock_irqrestore(&acm->read_lock, flags); 451 spin_unlock_irqrestore(&acm->read_lock, flags);
452 } 452 }
453 } 453 }
454 454
455 /* data interface wrote those outgoing bytes */ 455 /* data interface wrote those outgoing bytes */
456 static void acm_write_bulk(struct urb *urb) 456 static void acm_write_bulk(struct urb *urb)
457 { 457 {
458 struct acm_wb *wb = urb->context; 458 struct acm_wb *wb = urb->context;
459 struct acm *acm = wb->instance; 459 struct acm *acm = wb->instance;
460 unsigned long flags; 460 unsigned long flags;
461 461
462 if (urb->status || (urb->actual_length != urb->transfer_buffer_length)) 462 if (urb->status || (urb->actual_length != urb->transfer_buffer_length))
463 dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n", 463 dev_vdbg(&acm->data->dev, "%s - len %d/%d, status %d\n",
464 __func__, 464 __func__,
465 urb->actual_length, 465 urb->actual_length,
466 urb->transfer_buffer_length, 466 urb->transfer_buffer_length,
467 urb->status); 467 urb->status);
468 468
469 spin_lock_irqsave(&acm->write_lock, flags); 469 spin_lock_irqsave(&acm->write_lock, flags);
470 acm_write_done(acm, wb); 470 acm_write_done(acm, wb);
471 spin_unlock_irqrestore(&acm->write_lock, flags); 471 spin_unlock_irqrestore(&acm->write_lock, flags);
472 schedule_work(&acm->work); 472 schedule_work(&acm->work);
473 } 473 }
474 474
475 static void acm_softint(struct work_struct *work) 475 static void acm_softint(struct work_struct *work)
476 { 476 {
477 struct acm *acm = container_of(work, struct acm, work); 477 struct acm *acm = container_of(work, struct acm, work);
478 struct tty_struct *tty;
479 478
480 dev_vdbg(&acm->data->dev, "%s\n", __func__); 479 dev_vdbg(&acm->data->dev, "%s\n", __func__);
481 480
482 tty = tty_port_tty_get(&acm->port); 481 tty_port_tty_wakeup(&acm->port);
483 if (!tty)
484 return;
485 tty_wakeup(tty);
486 tty_kref_put(tty);
487 } 482 }
488 483
489 /* 484 /*
490 * TTY handlers 485 * TTY handlers
491 */ 486 */
492 487
493 static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty) 488 static int acm_tty_install(struct tty_driver *driver, struct tty_struct *tty)
494 { 489 {
495 struct acm *acm; 490 struct acm *acm;
496 int retval; 491 int retval;
497 492
498 dev_dbg(tty->dev, "%s\n", __func__); 493 dev_dbg(tty->dev, "%s\n", __func__);
499 494
500 acm = acm_get_by_index(tty->index); 495 acm = acm_get_by_index(tty->index);
501 if (!acm) 496 if (!acm)
502 return -ENODEV; 497 return -ENODEV;
503 498
504 retval = tty_standard_install(driver, tty); 499 retval = tty_standard_install(driver, tty);
505 if (retval) 500 if (retval)
506 goto error_init_termios; 501 goto error_init_termios;
507 502
508 tty->driver_data = acm; 503 tty->driver_data = acm;
509 504
510 return 0; 505 return 0;
511 506
512 error_init_termios: 507 error_init_termios:
513 tty_port_put(&acm->port); 508 tty_port_put(&acm->port);
514 return retval; 509 return retval;
515 } 510 }
516 511
517 static int acm_tty_open(struct tty_struct *tty, struct file *filp) 512 static int acm_tty_open(struct tty_struct *tty, struct file *filp)
518 { 513 {
519 struct acm *acm = tty->driver_data; 514 struct acm *acm = tty->driver_data;
520 515
521 dev_dbg(tty->dev, "%s\n", __func__); 516 dev_dbg(tty->dev, "%s\n", __func__);
522 517
523 return tty_port_open(&acm->port, tty, filp); 518 return tty_port_open(&acm->port, tty, filp);
524 } 519 }
525 520
526 static int acm_port_activate(struct tty_port *port, struct tty_struct *tty) 521 static int acm_port_activate(struct tty_port *port, struct tty_struct *tty)
527 { 522 {
528 struct acm *acm = container_of(port, struct acm, port); 523 struct acm *acm = container_of(port, struct acm, port);
529 int retval = -ENODEV; 524 int retval = -ENODEV;
530 525
531 dev_dbg(&acm->control->dev, "%s\n", __func__); 526 dev_dbg(&acm->control->dev, "%s\n", __func__);
532 527
533 mutex_lock(&acm->mutex); 528 mutex_lock(&acm->mutex);
534 if (acm->disconnected) 529 if (acm->disconnected)
535 goto disconnected; 530 goto disconnected;
536 531
537 retval = usb_autopm_get_interface(acm->control); 532 retval = usb_autopm_get_interface(acm->control);
538 if (retval) 533 if (retval)
539 goto error_get_interface; 534 goto error_get_interface;
540 535
541 /* 536 /*
542 * FIXME: Why do we need this? Allocating 64K of physically contiguous 537 * FIXME: Why do we need this? Allocating 64K of physically contiguous
543 * memory is really nasty... 538 * memory is really nasty...
544 */ 539 */
545 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags); 540 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags);
546 acm->control->needs_remote_wakeup = 1; 541 acm->control->needs_remote_wakeup = 1;
547 542
548 acm->ctrlurb->dev = acm->dev; 543 acm->ctrlurb->dev = acm->dev;
549 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) { 544 if (usb_submit_urb(acm->ctrlurb, GFP_KERNEL)) {
550 dev_err(&acm->control->dev, 545 dev_err(&acm->control->dev,
551 "%s - usb_submit_urb(ctrl irq) failed\n", __func__); 546 "%s - usb_submit_urb(ctrl irq) failed\n", __func__);
552 goto error_submit_urb; 547 goto error_submit_urb;
553 } 548 }
554 549
555 acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS; 550 acm->ctrlout = ACM_CTRL_DTR | ACM_CTRL_RTS;
556 if (acm_set_control(acm, acm->ctrlout) < 0 && 551 if (acm_set_control(acm, acm->ctrlout) < 0 &&
557 (acm->ctrl_caps & USB_CDC_CAP_LINE)) 552 (acm->ctrl_caps & USB_CDC_CAP_LINE))
558 goto error_set_control; 553 goto error_set_control;
559 554
560 usb_autopm_put_interface(acm->control); 555 usb_autopm_put_interface(acm->control);
561 556
562 /* 557 /*
563 * Unthrottle device in case the TTY was closed while throttled. 558 * Unthrottle device in case the TTY was closed while throttled.
564 */ 559 */
565 spin_lock_irq(&acm->read_lock); 560 spin_lock_irq(&acm->read_lock);
566 acm->throttled = 0; 561 acm->throttled = 0;
567 acm->throttle_req = 0; 562 acm->throttle_req = 0;
568 spin_unlock_irq(&acm->read_lock); 563 spin_unlock_irq(&acm->read_lock);
569 564
570 if (acm_submit_read_urbs(acm, GFP_KERNEL)) 565 if (acm_submit_read_urbs(acm, GFP_KERNEL))
571 goto error_submit_read_urbs; 566 goto error_submit_read_urbs;
572 567
573 mutex_unlock(&acm->mutex); 568 mutex_unlock(&acm->mutex);
574 569
575 return 0; 570 return 0;
576 571
577 error_submit_read_urbs: 572 error_submit_read_urbs:
578 acm->ctrlout = 0; 573 acm->ctrlout = 0;
579 acm_set_control(acm, acm->ctrlout); 574 acm_set_control(acm, acm->ctrlout);
580 error_set_control: 575 error_set_control:
581 usb_kill_urb(acm->ctrlurb); 576 usb_kill_urb(acm->ctrlurb);
582 error_submit_urb: 577 error_submit_urb:
583 usb_autopm_put_interface(acm->control); 578 usb_autopm_put_interface(acm->control);
584 error_get_interface: 579 error_get_interface:
585 disconnected: 580 disconnected:
586 mutex_unlock(&acm->mutex); 581 mutex_unlock(&acm->mutex);
587 return retval; 582 return retval;
588 } 583 }
589 584
590 static void acm_port_destruct(struct tty_port *port) 585 static void acm_port_destruct(struct tty_port *port)
591 { 586 {
592 struct acm *acm = container_of(port, struct acm, port); 587 struct acm *acm = container_of(port, struct acm, port);
593 588
594 dev_dbg(&acm->control->dev, "%s\n", __func__); 589 dev_dbg(&acm->control->dev, "%s\n", __func__);
595 590
596 tty_unregister_device(acm_tty_driver, acm->minor); 591 tty_unregister_device(acm_tty_driver, acm->minor);
597 acm_release_minor(acm); 592 acm_release_minor(acm);
598 usb_put_intf(acm->control); 593 usb_put_intf(acm->control);
599 kfree(acm->country_codes); 594 kfree(acm->country_codes);
600 kfree(acm); 595 kfree(acm);
601 } 596 }
602 597
603 static void acm_port_shutdown(struct tty_port *port) 598 static void acm_port_shutdown(struct tty_port *port)
604 { 599 {
605 struct acm *acm = container_of(port, struct acm, port); 600 struct acm *acm = container_of(port, struct acm, port);
606 int i; 601 int i;
607 602
608 dev_dbg(&acm->control->dev, "%s\n", __func__); 603 dev_dbg(&acm->control->dev, "%s\n", __func__);
609 604
610 mutex_lock(&acm->mutex); 605 mutex_lock(&acm->mutex);
611 if (!acm->disconnected) { 606 if (!acm->disconnected) {
612 usb_autopm_get_interface(acm->control); 607 usb_autopm_get_interface(acm->control);
613 acm_set_control(acm, acm->ctrlout = 0); 608 acm_set_control(acm, acm->ctrlout = 0);
614 usb_kill_urb(acm->ctrlurb); 609 usb_kill_urb(acm->ctrlurb);
615 for (i = 0; i < ACM_NW; i++) 610 for (i = 0; i < ACM_NW; i++)
616 usb_kill_urb(acm->wb[i].urb); 611 usb_kill_urb(acm->wb[i].urb);
617 for (i = 0; i < acm->rx_buflimit; i++) 612 for (i = 0; i < acm->rx_buflimit; i++)
618 usb_kill_urb(acm->read_urbs[i]); 613 usb_kill_urb(acm->read_urbs[i]);
619 acm->control->needs_remote_wakeup = 0; 614 acm->control->needs_remote_wakeup = 0;
620 usb_autopm_put_interface(acm->control); 615 usb_autopm_put_interface(acm->control);
621 } 616 }
622 mutex_unlock(&acm->mutex); 617 mutex_unlock(&acm->mutex);
623 } 618 }
624 619
625 static void acm_tty_cleanup(struct tty_struct *tty) 620 static void acm_tty_cleanup(struct tty_struct *tty)
626 { 621 {
627 struct acm *acm = tty->driver_data; 622 struct acm *acm = tty->driver_data;
628 dev_dbg(&acm->control->dev, "%s\n", __func__); 623 dev_dbg(&acm->control->dev, "%s\n", __func__);
629 tty_port_put(&acm->port); 624 tty_port_put(&acm->port);
630 } 625 }
631 626
632 static void acm_tty_hangup(struct tty_struct *tty) 627 static void acm_tty_hangup(struct tty_struct *tty)
633 { 628 {
634 struct acm *acm = tty->driver_data; 629 struct acm *acm = tty->driver_data;
635 dev_dbg(&acm->control->dev, "%s\n", __func__); 630 dev_dbg(&acm->control->dev, "%s\n", __func__);
636 tty_port_hangup(&acm->port); 631 tty_port_hangup(&acm->port);
637 } 632 }
638 633
639 static void acm_tty_close(struct tty_struct *tty, struct file *filp) 634 static void acm_tty_close(struct tty_struct *tty, struct file *filp)
640 { 635 {
641 struct acm *acm = tty->driver_data; 636 struct acm *acm = tty->driver_data;
642 dev_dbg(&acm->control->dev, "%s\n", __func__); 637 dev_dbg(&acm->control->dev, "%s\n", __func__);
643 tty_port_close(&acm->port, tty, filp); 638 tty_port_close(&acm->port, tty, filp);
644 } 639 }
645 640
646 static int acm_tty_write(struct tty_struct *tty, 641 static int acm_tty_write(struct tty_struct *tty,
647 const unsigned char *buf, int count) 642 const unsigned char *buf, int count)
648 { 643 {
649 struct acm *acm = tty->driver_data; 644 struct acm *acm = tty->driver_data;
650 int stat; 645 int stat;
651 unsigned long flags; 646 unsigned long flags;
652 int wbn; 647 int wbn;
653 struct acm_wb *wb; 648 struct acm_wb *wb;
654 649
655 if (!count) 650 if (!count)
656 return 0; 651 return 0;
657 652
658 dev_vdbg(&acm->data->dev, "%s - count %d\n", __func__, count); 653 dev_vdbg(&acm->data->dev, "%s - count %d\n", __func__, count);
659 654
660 spin_lock_irqsave(&acm->write_lock, flags); 655 spin_lock_irqsave(&acm->write_lock, flags);
661 wbn = acm_wb_alloc(acm); 656 wbn = acm_wb_alloc(acm);
662 if (wbn < 0) { 657 if (wbn < 0) {
663 spin_unlock_irqrestore(&acm->write_lock, flags); 658 spin_unlock_irqrestore(&acm->write_lock, flags);
664 return 0; 659 return 0;
665 } 660 }
666 wb = &acm->wb[wbn]; 661 wb = &acm->wb[wbn];
667 662
668 count = (count > acm->writesize) ? acm->writesize : count; 663 count = (count > acm->writesize) ? acm->writesize : count;
669 dev_vdbg(&acm->data->dev, "%s - write %d\n", __func__, count); 664 dev_vdbg(&acm->data->dev, "%s - write %d\n", __func__, count);
670 memcpy(wb->buf, buf, count); 665 memcpy(wb->buf, buf, count);
671 wb->len = count; 666 wb->len = count;
672 spin_unlock_irqrestore(&acm->write_lock, flags); 667 spin_unlock_irqrestore(&acm->write_lock, flags);
673 668
674 stat = acm_write_start(acm, wbn); 669 stat = acm_write_start(acm, wbn);
675 if (stat < 0) 670 if (stat < 0)
676 return stat; 671 return stat;
677 return count; 672 return count;
678 } 673 }
679 674
680 static int acm_tty_write_room(struct tty_struct *tty) 675 static int acm_tty_write_room(struct tty_struct *tty)
681 { 676 {
682 struct acm *acm = tty->driver_data; 677 struct acm *acm = tty->driver_data;
683 /* 678 /*
684 * Do not let the line discipline to know that we have a reserve, 679 * Do not let the line discipline to know that we have a reserve,
685 * or it might get too enthusiastic. 680 * or it might get too enthusiastic.
686 */ 681 */
687 return acm_wb_is_avail(acm) ? acm->writesize : 0; 682 return acm_wb_is_avail(acm) ? acm->writesize : 0;
688 } 683 }
689 684
690 static int acm_tty_chars_in_buffer(struct tty_struct *tty) 685 static int acm_tty_chars_in_buffer(struct tty_struct *tty)
691 { 686 {
692 struct acm *acm = tty->driver_data; 687 struct acm *acm = tty->driver_data;
693 /* 688 /*
694 * if the device was unplugged then any remaining characters fell out 689 * if the device was unplugged then any remaining characters fell out
695 * of the connector ;) 690 * of the connector ;)
696 */ 691 */
697 if (acm->disconnected) 692 if (acm->disconnected)
698 return 0; 693 return 0;
699 /* 694 /*
700 * This is inaccurate (overcounts), but it works. 695 * This is inaccurate (overcounts), but it works.
701 */ 696 */
702 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize; 697 return (ACM_NW - acm_wb_is_avail(acm)) * acm->writesize;
703 } 698 }
704 699
705 static void acm_tty_throttle(struct tty_struct *tty) 700 static void acm_tty_throttle(struct tty_struct *tty)
706 { 701 {
707 struct acm *acm = tty->driver_data; 702 struct acm *acm = tty->driver_data;
708 703
709 spin_lock_irq(&acm->read_lock); 704 spin_lock_irq(&acm->read_lock);
710 acm->throttle_req = 1; 705 acm->throttle_req = 1;
711 spin_unlock_irq(&acm->read_lock); 706 spin_unlock_irq(&acm->read_lock);
712 } 707 }
713 708
714 static void acm_tty_unthrottle(struct tty_struct *tty) 709 static void acm_tty_unthrottle(struct tty_struct *tty)
715 { 710 {
716 struct acm *acm = tty->driver_data; 711 struct acm *acm = tty->driver_data;
717 unsigned int was_throttled; 712 unsigned int was_throttled;
718 713
719 spin_lock_irq(&acm->read_lock); 714 spin_lock_irq(&acm->read_lock);
720 was_throttled = acm->throttled; 715 was_throttled = acm->throttled;
721 acm->throttled = 0; 716 acm->throttled = 0;
722 acm->throttle_req = 0; 717 acm->throttle_req = 0;
723 spin_unlock_irq(&acm->read_lock); 718 spin_unlock_irq(&acm->read_lock);
724 719
725 if (was_throttled) 720 if (was_throttled)
726 acm_submit_read_urbs(acm, GFP_KERNEL); 721 acm_submit_read_urbs(acm, GFP_KERNEL);
727 } 722 }
728 723
729 static int acm_tty_break_ctl(struct tty_struct *tty, int state) 724 static int acm_tty_break_ctl(struct tty_struct *tty, int state)
730 { 725 {
731 struct acm *acm = tty->driver_data; 726 struct acm *acm = tty->driver_data;
732 int retval; 727 int retval;
733 728
734 retval = acm_send_break(acm, state ? 0xffff : 0); 729 retval = acm_send_break(acm, state ? 0xffff : 0);
735 if (retval < 0) 730 if (retval < 0)
736 dev_dbg(&acm->control->dev, "%s - send break failed\n", 731 dev_dbg(&acm->control->dev, "%s - send break failed\n",
737 __func__); 732 __func__);
738 return retval; 733 return retval;
739 } 734 }
740 735
741 static int acm_tty_tiocmget(struct tty_struct *tty) 736 static int acm_tty_tiocmget(struct tty_struct *tty)
742 { 737 {
743 struct acm *acm = tty->driver_data; 738 struct acm *acm = tty->driver_data;
744 739
745 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) | 740 return (acm->ctrlout & ACM_CTRL_DTR ? TIOCM_DTR : 0) |
746 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) | 741 (acm->ctrlout & ACM_CTRL_RTS ? TIOCM_RTS : 0) |
747 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) | 742 (acm->ctrlin & ACM_CTRL_DSR ? TIOCM_DSR : 0) |
748 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) | 743 (acm->ctrlin & ACM_CTRL_RI ? TIOCM_RI : 0) |
749 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) | 744 (acm->ctrlin & ACM_CTRL_DCD ? TIOCM_CD : 0) |
750 TIOCM_CTS; 745 TIOCM_CTS;
751 } 746 }
752 747
753 static int acm_tty_tiocmset(struct tty_struct *tty, 748 static int acm_tty_tiocmset(struct tty_struct *tty,
754 unsigned int set, unsigned int clear) 749 unsigned int set, unsigned int clear)
755 { 750 {
756 struct acm *acm = tty->driver_data; 751 struct acm *acm = tty->driver_data;
757 unsigned int newctrl; 752 unsigned int newctrl;
758 753
759 newctrl = acm->ctrlout; 754 newctrl = acm->ctrlout;
760 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) | 755 set = (set & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
761 (set & TIOCM_RTS ? ACM_CTRL_RTS : 0); 756 (set & TIOCM_RTS ? ACM_CTRL_RTS : 0);
762 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) | 757 clear = (clear & TIOCM_DTR ? ACM_CTRL_DTR : 0) |
763 (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0); 758 (clear & TIOCM_RTS ? ACM_CTRL_RTS : 0);
764 759
765 newctrl = (newctrl & ~clear) | set; 760 newctrl = (newctrl & ~clear) | set;
766 761
767 if (acm->ctrlout == newctrl) 762 if (acm->ctrlout == newctrl)
768 return 0; 763 return 0;
769 return acm_set_control(acm, acm->ctrlout = newctrl); 764 return acm_set_control(acm, acm->ctrlout = newctrl);
770 } 765 }
771 766
772 static int get_serial_info(struct acm *acm, struct serial_struct __user *info) 767 static int get_serial_info(struct acm *acm, struct serial_struct __user *info)
773 { 768 {
774 struct serial_struct tmp; 769 struct serial_struct tmp;
775 770
776 if (!info) 771 if (!info)
777 return -EINVAL; 772 return -EINVAL;
778 773
779 memset(&tmp, 0, sizeof(tmp)); 774 memset(&tmp, 0, sizeof(tmp));
780 tmp.flags = ASYNC_LOW_LATENCY; 775 tmp.flags = ASYNC_LOW_LATENCY;
781 tmp.xmit_fifo_size = acm->writesize; 776 tmp.xmit_fifo_size = acm->writesize;
782 tmp.baud_base = le32_to_cpu(acm->line.dwDTERate); 777 tmp.baud_base = le32_to_cpu(acm->line.dwDTERate);
783 tmp.close_delay = acm->port.close_delay / 10; 778 tmp.close_delay = acm->port.close_delay / 10;
784 tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ? 779 tmp.closing_wait = acm->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
785 ASYNC_CLOSING_WAIT_NONE : 780 ASYNC_CLOSING_WAIT_NONE :
786 acm->port.closing_wait / 10; 781 acm->port.closing_wait / 10;
787 782
788 if (copy_to_user(info, &tmp, sizeof(tmp))) 783 if (copy_to_user(info, &tmp, sizeof(tmp)))
789 return -EFAULT; 784 return -EFAULT;
790 else 785 else
791 return 0; 786 return 0;
792 } 787 }
793 788
794 static int set_serial_info(struct acm *acm, 789 static int set_serial_info(struct acm *acm,
795 struct serial_struct __user *newinfo) 790 struct serial_struct __user *newinfo)
796 { 791 {
797 struct serial_struct new_serial; 792 struct serial_struct new_serial;
798 unsigned int closing_wait, close_delay; 793 unsigned int closing_wait, close_delay;
799 int retval = 0; 794 int retval = 0;
800 795
801 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) 796 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
802 return -EFAULT; 797 return -EFAULT;
803 798
804 close_delay = new_serial.close_delay * 10; 799 close_delay = new_serial.close_delay * 10;
805 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ? 800 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
806 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10; 801 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
807 802
808 mutex_lock(&acm->port.mutex); 803 mutex_lock(&acm->port.mutex);
809 804
810 if (!capable(CAP_SYS_ADMIN)) { 805 if (!capable(CAP_SYS_ADMIN)) {
811 if ((close_delay != acm->port.close_delay) || 806 if ((close_delay != acm->port.close_delay) ||
812 (closing_wait != acm->port.closing_wait)) 807 (closing_wait != acm->port.closing_wait))
813 retval = -EPERM; 808 retval = -EPERM;
814 else 809 else
815 retval = -EOPNOTSUPP; 810 retval = -EOPNOTSUPP;
816 } else { 811 } else {
817 acm->port.close_delay = close_delay; 812 acm->port.close_delay = close_delay;
818 acm->port.closing_wait = closing_wait; 813 acm->port.closing_wait = closing_wait;
819 } 814 }
820 815
821 mutex_unlock(&acm->port.mutex); 816 mutex_unlock(&acm->port.mutex);
822 return retval; 817 return retval;
823 } 818 }
824 819
825 static int acm_tty_ioctl(struct tty_struct *tty, 820 static int acm_tty_ioctl(struct tty_struct *tty,
826 unsigned int cmd, unsigned long arg) 821 unsigned int cmd, unsigned long arg)
827 { 822 {
828 struct acm *acm = tty->driver_data; 823 struct acm *acm = tty->driver_data;
829 int rv = -ENOIOCTLCMD; 824 int rv = -ENOIOCTLCMD;
830 825
831 switch (cmd) { 826 switch (cmd) {
832 case TIOCGSERIAL: /* gets serial port data */ 827 case TIOCGSERIAL: /* gets serial port data */
833 rv = get_serial_info(acm, (struct serial_struct __user *) arg); 828 rv = get_serial_info(acm, (struct serial_struct __user *) arg);
834 break; 829 break;
835 case TIOCSSERIAL: 830 case TIOCSSERIAL:
836 rv = set_serial_info(acm, (struct serial_struct __user *) arg); 831 rv = set_serial_info(acm, (struct serial_struct __user *) arg);
837 break; 832 break;
838 } 833 }
839 834
840 return rv; 835 return rv;
841 } 836 }
842 837
843 static const __u32 acm_tty_speed[] = { 838 static const __u32 acm_tty_speed[] = {
844 0, 50, 75, 110, 134, 150, 200, 300, 600, 839 0, 50, 75, 110, 134, 150, 200, 300, 600,
845 1200, 1800, 2400, 4800, 9600, 19200, 38400, 840 1200, 1800, 2400, 4800, 9600, 19200, 38400,
846 57600, 115200, 230400, 460800, 500000, 576000, 841 57600, 115200, 230400, 460800, 500000, 576000,
847 921600, 1000000, 1152000, 1500000, 2000000, 842 921600, 1000000, 1152000, 1500000, 2000000,
848 2500000, 3000000, 3500000, 4000000 843 2500000, 3000000, 3500000, 4000000
849 }; 844 };
850 845
851 static void acm_tty_set_termios(struct tty_struct *tty, 846 static void acm_tty_set_termios(struct tty_struct *tty,
852 struct ktermios *termios_old) 847 struct ktermios *termios_old)
853 { 848 {
854 struct acm *acm = tty->driver_data; 849 struct acm *acm = tty->driver_data;
855 struct ktermios *termios = &tty->termios; 850 struct ktermios *termios = &tty->termios;
856 struct usb_cdc_line_coding newline; 851 struct usb_cdc_line_coding newline;
857 int newctrl = acm->ctrlout; 852 int newctrl = acm->ctrlout;
858 853
859 newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty)); 854 newline.dwDTERate = cpu_to_le32(tty_get_baud_rate(tty));
860 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0; 855 newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 0;
861 newline.bParityType = termios->c_cflag & PARENB ? 856 newline.bParityType = termios->c_cflag & PARENB ?
862 (termios->c_cflag & PARODD ? 1 : 2) + 857 (termios->c_cflag & PARODD ? 1 : 2) +
863 (termios->c_cflag & CMSPAR ? 2 : 0) : 0; 858 (termios->c_cflag & CMSPAR ? 2 : 0) : 0;
864 switch (termios->c_cflag & CSIZE) { 859 switch (termios->c_cflag & CSIZE) {
865 case CS5: 860 case CS5:
866 newline.bDataBits = 5; 861 newline.bDataBits = 5;
867 break; 862 break;
868 case CS6: 863 case CS6:
869 newline.bDataBits = 6; 864 newline.bDataBits = 6;
870 break; 865 break;
871 case CS7: 866 case CS7:
872 newline.bDataBits = 7; 867 newline.bDataBits = 7;
873 break; 868 break;
874 case CS8: 869 case CS8:
875 default: 870 default:
876 newline.bDataBits = 8; 871 newline.bDataBits = 8;
877 break; 872 break;
878 } 873 }
879 /* FIXME: Needs to clear unsupported bits in the termios */ 874 /* FIXME: Needs to clear unsupported bits in the termios */
880 acm->clocal = ((termios->c_cflag & CLOCAL) != 0); 875 acm->clocal = ((termios->c_cflag & CLOCAL) != 0);
881 876
882 if (!newline.dwDTERate) { 877 if (!newline.dwDTERate) {
883 newline.dwDTERate = acm->line.dwDTERate; 878 newline.dwDTERate = acm->line.dwDTERate;
884 newctrl &= ~ACM_CTRL_DTR; 879 newctrl &= ~ACM_CTRL_DTR;
885 } else 880 } else
886 newctrl |= ACM_CTRL_DTR; 881 newctrl |= ACM_CTRL_DTR;
887 882
888 if (newctrl != acm->ctrlout) 883 if (newctrl != acm->ctrlout)
889 acm_set_control(acm, acm->ctrlout = newctrl); 884 acm_set_control(acm, acm->ctrlout = newctrl);
890 885
891 if (memcmp(&acm->line, &newline, sizeof newline)) { 886 if (memcmp(&acm->line, &newline, sizeof newline)) {
892 memcpy(&acm->line, &newline, sizeof newline); 887 memcpy(&acm->line, &newline, sizeof newline);
893 dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n", 888 dev_dbg(&acm->control->dev, "%s - set line: %d %d %d %d\n",
894 __func__, 889 __func__,
895 le32_to_cpu(newline.dwDTERate), 890 le32_to_cpu(newline.dwDTERate),
896 newline.bCharFormat, newline.bParityType, 891 newline.bCharFormat, newline.bParityType,
897 newline.bDataBits); 892 newline.bDataBits);
898 acm_set_line(acm, &acm->line); 893 acm_set_line(acm, &acm->line);
899 } 894 }
900 } 895 }
901 896
902 static const struct tty_port_operations acm_port_ops = { 897 static const struct tty_port_operations acm_port_ops = {
903 .shutdown = acm_port_shutdown, 898 .shutdown = acm_port_shutdown,
904 .activate = acm_port_activate, 899 .activate = acm_port_activate,
905 .destruct = acm_port_destruct, 900 .destruct = acm_port_destruct,
906 }; 901 };
907 902
908 /* 903 /*
909 * USB probe and disconnect routines. 904 * USB probe and disconnect routines.
910 */ 905 */
911 906
912 /* Little helpers: write/read buffers free */ 907 /* Little helpers: write/read buffers free */
913 static void acm_write_buffers_free(struct acm *acm) 908 static void acm_write_buffers_free(struct acm *acm)
914 { 909 {
915 int i; 910 int i;
916 struct acm_wb *wb; 911 struct acm_wb *wb;
917 struct usb_device *usb_dev = interface_to_usbdev(acm->control); 912 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
918 913
919 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) 914 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++)
920 usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah); 915 usb_free_coherent(usb_dev, acm->writesize, wb->buf, wb->dmah);
921 } 916 }
922 917
923 static void acm_read_buffers_free(struct acm *acm) 918 static void acm_read_buffers_free(struct acm *acm)
924 { 919 {
925 struct usb_device *usb_dev = interface_to_usbdev(acm->control); 920 struct usb_device *usb_dev = interface_to_usbdev(acm->control);
926 int i; 921 int i;
927 922
928 for (i = 0; i < acm->rx_buflimit; i++) 923 for (i = 0; i < acm->rx_buflimit; i++)
929 usb_free_coherent(usb_dev, acm->readsize, 924 usb_free_coherent(usb_dev, acm->readsize,
930 acm->read_buffers[i].base, acm->read_buffers[i].dma); 925 acm->read_buffers[i].base, acm->read_buffers[i].dma);
931 } 926 }
932 927
933 /* Little helper: write buffers allocate */ 928 /* Little helper: write buffers allocate */
934 static int acm_write_buffers_alloc(struct acm *acm) 929 static int acm_write_buffers_alloc(struct acm *acm)
935 { 930 {
936 int i; 931 int i;
937 struct acm_wb *wb; 932 struct acm_wb *wb;
938 933
939 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) { 934 for (wb = &acm->wb[0], i = 0; i < ACM_NW; i++, wb++) {
940 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL, 935 wb->buf = usb_alloc_coherent(acm->dev, acm->writesize, GFP_KERNEL,
941 &wb->dmah); 936 &wb->dmah);
942 if (!wb->buf) { 937 if (!wb->buf) {
943 while (i != 0) { 938 while (i != 0) {
944 --i; 939 --i;
945 --wb; 940 --wb;
946 usb_free_coherent(acm->dev, acm->writesize, 941 usb_free_coherent(acm->dev, acm->writesize,
947 wb->buf, wb->dmah); 942 wb->buf, wb->dmah);
948 } 943 }
949 return -ENOMEM; 944 return -ENOMEM;
950 } 945 }
951 } 946 }
952 return 0; 947 return 0;
953 } 948 }
954 949
955 static int acm_probe(struct usb_interface *intf, 950 static int acm_probe(struct usb_interface *intf,
956 const struct usb_device_id *id) 951 const struct usb_device_id *id)
957 { 952 {
958 struct usb_cdc_union_desc *union_header = NULL; 953 struct usb_cdc_union_desc *union_header = NULL;
959 struct usb_cdc_country_functional_desc *cfd = NULL; 954 struct usb_cdc_country_functional_desc *cfd = NULL;
960 unsigned char *buffer = intf->altsetting->extra; 955 unsigned char *buffer = intf->altsetting->extra;
961 int buflen = intf->altsetting->extralen; 956 int buflen = intf->altsetting->extralen;
962 struct usb_interface *control_interface; 957 struct usb_interface *control_interface;
963 struct usb_interface *data_interface; 958 struct usb_interface *data_interface;
964 struct usb_endpoint_descriptor *epctrl = NULL; 959 struct usb_endpoint_descriptor *epctrl = NULL;
965 struct usb_endpoint_descriptor *epread = NULL; 960 struct usb_endpoint_descriptor *epread = NULL;
966 struct usb_endpoint_descriptor *epwrite = NULL; 961 struct usb_endpoint_descriptor *epwrite = NULL;
967 struct usb_device *usb_dev = interface_to_usbdev(intf); 962 struct usb_device *usb_dev = interface_to_usbdev(intf);
968 struct acm *acm; 963 struct acm *acm;
969 int minor; 964 int minor;
970 int ctrlsize, readsize; 965 int ctrlsize, readsize;
971 u8 *buf; 966 u8 *buf;
972 u8 ac_management_function = 0; 967 u8 ac_management_function = 0;
973 u8 call_management_function = 0; 968 u8 call_management_function = 0;
974 int call_interface_num = -1; 969 int call_interface_num = -1;
975 int data_interface_num = -1; 970 int data_interface_num = -1;
976 unsigned long quirks; 971 unsigned long quirks;
977 int num_rx_buf; 972 int num_rx_buf;
978 int i; 973 int i;
979 int combined_interfaces = 0; 974 int combined_interfaces = 0;
980 975
981 /* normal quirks */ 976 /* normal quirks */
982 quirks = (unsigned long)id->driver_info; 977 quirks = (unsigned long)id->driver_info;
983 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR; 978 num_rx_buf = (quirks == SINGLE_RX_URB) ? 1 : ACM_NR;
984 979
985 /* handle quirks deadly to normal probing*/ 980 /* handle quirks deadly to normal probing*/
986 if (quirks == NO_UNION_NORMAL) { 981 if (quirks == NO_UNION_NORMAL) {
987 data_interface = usb_ifnum_to_if(usb_dev, 1); 982 data_interface = usb_ifnum_to_if(usb_dev, 1);
988 control_interface = usb_ifnum_to_if(usb_dev, 0); 983 control_interface = usb_ifnum_to_if(usb_dev, 0);
989 goto skip_normal_probe; 984 goto skip_normal_probe;
990 } 985 }
991 986
992 /* normal probing*/ 987 /* normal probing*/
993 if (!buffer) { 988 if (!buffer) {
994 dev_err(&intf->dev, "Weird descriptor references\n"); 989 dev_err(&intf->dev, "Weird descriptor references\n");
995 return -EINVAL; 990 return -EINVAL;
996 } 991 }
997 992
998 if (!buflen) { 993 if (!buflen) {
999 if (intf->cur_altsetting->endpoint && 994 if (intf->cur_altsetting->endpoint &&
1000 intf->cur_altsetting->endpoint->extralen && 995 intf->cur_altsetting->endpoint->extralen &&
1001 intf->cur_altsetting->endpoint->extra) { 996 intf->cur_altsetting->endpoint->extra) {
1002 dev_dbg(&intf->dev, 997 dev_dbg(&intf->dev,
1003 "Seeking extra descriptors on endpoint\n"); 998 "Seeking extra descriptors on endpoint\n");
1004 buflen = intf->cur_altsetting->endpoint->extralen; 999 buflen = intf->cur_altsetting->endpoint->extralen;
1005 buffer = intf->cur_altsetting->endpoint->extra; 1000 buffer = intf->cur_altsetting->endpoint->extra;
1006 } else { 1001 } else {
1007 dev_err(&intf->dev, 1002 dev_err(&intf->dev,
1008 "Zero length descriptor references\n"); 1003 "Zero length descriptor references\n");
1009 return -EINVAL; 1004 return -EINVAL;
1010 } 1005 }
1011 } 1006 }
1012 1007
1013 while (buflen > 0) { 1008 while (buflen > 0) {
1014 if (buffer[1] != USB_DT_CS_INTERFACE) { 1009 if (buffer[1] != USB_DT_CS_INTERFACE) {
1015 dev_err(&intf->dev, "skipping garbage\n"); 1010 dev_err(&intf->dev, "skipping garbage\n");
1016 goto next_desc; 1011 goto next_desc;
1017 } 1012 }
1018 1013
1019 switch (buffer[2]) { 1014 switch (buffer[2]) {
1020 case USB_CDC_UNION_TYPE: /* we've found it */ 1015 case USB_CDC_UNION_TYPE: /* we've found it */
1021 if (union_header) { 1016 if (union_header) {
1022 dev_err(&intf->dev, "More than one " 1017 dev_err(&intf->dev, "More than one "
1023 "union descriptor, skipping ...\n"); 1018 "union descriptor, skipping ...\n");
1024 goto next_desc; 1019 goto next_desc;
1025 } 1020 }
1026 union_header = (struct usb_cdc_union_desc *)buffer; 1021 union_header = (struct usb_cdc_union_desc *)buffer;
1027 break; 1022 break;
1028 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/ 1023 case USB_CDC_COUNTRY_TYPE: /* export through sysfs*/
1029 cfd = (struct usb_cdc_country_functional_desc *)buffer; 1024 cfd = (struct usb_cdc_country_functional_desc *)buffer;
1030 break; 1025 break;
1031 case USB_CDC_HEADER_TYPE: /* maybe check version */ 1026 case USB_CDC_HEADER_TYPE: /* maybe check version */
1032 break; /* for now we ignore it */ 1027 break; /* for now we ignore it */
1033 case USB_CDC_ACM_TYPE: 1028 case USB_CDC_ACM_TYPE:
1034 ac_management_function = buffer[3]; 1029 ac_management_function = buffer[3];
1035 break; 1030 break;
1036 case USB_CDC_CALL_MANAGEMENT_TYPE: 1031 case USB_CDC_CALL_MANAGEMENT_TYPE:
1037 call_management_function = buffer[3]; 1032 call_management_function = buffer[3];
1038 call_interface_num = buffer[4]; 1033 call_interface_num = buffer[4];
1039 if ((quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3) 1034 if ((quirks & NOT_A_MODEM) == 0 && (call_management_function & 3) != 3)
1040 dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n"); 1035 dev_err(&intf->dev, "This device cannot do calls on its own. It is not a modem.\n");
1041 break; 1036 break;
1042 default: 1037 default:
1043 /* there are LOTS more CDC descriptors that 1038 /* there are LOTS more CDC descriptors that
1044 * could legitimately be found here. 1039 * could legitimately be found here.
1045 */ 1040 */
1046 dev_dbg(&intf->dev, "Ignoring descriptor: " 1041 dev_dbg(&intf->dev, "Ignoring descriptor: "
1047 "type %02x, length %d\n", 1042 "type %02x, length %d\n",
1048 buffer[2], buffer[0]); 1043 buffer[2], buffer[0]);
1049 break; 1044 break;
1050 } 1045 }
1051 next_desc: 1046 next_desc:
1052 buflen -= buffer[0]; 1047 buflen -= buffer[0];
1053 buffer += buffer[0]; 1048 buffer += buffer[0];
1054 } 1049 }
1055 1050
1056 if (!union_header) { 1051 if (!union_header) {
1057 if (call_interface_num > 0) { 1052 if (call_interface_num > 0) {
1058 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n"); 1053 dev_dbg(&intf->dev, "No union descriptor, using call management descriptor\n");
1059 /* quirks for Droids MuIn LCD */ 1054 /* quirks for Droids MuIn LCD */
1060 if (quirks & NO_DATA_INTERFACE) 1055 if (quirks & NO_DATA_INTERFACE)
1061 data_interface = usb_ifnum_to_if(usb_dev, 0); 1056 data_interface = usb_ifnum_to_if(usb_dev, 0);
1062 else 1057 else
1063 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num)); 1058 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = call_interface_num));
1064 control_interface = intf; 1059 control_interface = intf;
1065 } else { 1060 } else {
1066 if (intf->cur_altsetting->desc.bNumEndpoints != 3) { 1061 if (intf->cur_altsetting->desc.bNumEndpoints != 3) {
1067 dev_dbg(&intf->dev,"No union descriptor, giving up\n"); 1062 dev_dbg(&intf->dev,"No union descriptor, giving up\n");
1068 return -ENODEV; 1063 return -ENODEV;
1069 } else { 1064 } else {
1070 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n"); 1065 dev_warn(&intf->dev,"No union descriptor, testing for castrated device\n");
1071 combined_interfaces = 1; 1066 combined_interfaces = 1;
1072 control_interface = data_interface = intf; 1067 control_interface = data_interface = intf;
1073 goto look_for_collapsed_interface; 1068 goto look_for_collapsed_interface;
1074 } 1069 }
1075 } 1070 }
1076 } else { 1071 } else {
1077 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0); 1072 control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0);
1078 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0)); 1073 data_interface = usb_ifnum_to_if(usb_dev, (data_interface_num = union_header->bSlaveInterface0));
1079 if (!control_interface || !data_interface) { 1074 if (!control_interface || !data_interface) {
1080 dev_dbg(&intf->dev, "no interfaces\n"); 1075 dev_dbg(&intf->dev, "no interfaces\n");
1081 return -ENODEV; 1076 return -ENODEV;
1082 } 1077 }
1083 } 1078 }
1084 1079
1085 if (data_interface_num != call_interface_num) 1080 if (data_interface_num != call_interface_num)
1086 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n"); 1081 dev_dbg(&intf->dev, "Separate call control interface. That is not fully supported.\n");
1087 1082
1088 if (control_interface == data_interface) { 1083 if (control_interface == data_interface) {
1089 /* some broken devices designed for windows work this way */ 1084 /* some broken devices designed for windows work this way */
1090 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n"); 1085 dev_warn(&intf->dev,"Control and data interfaces are not separated!\n");
1091 combined_interfaces = 1; 1086 combined_interfaces = 1;
1092 /* a popular other OS doesn't use it */ 1087 /* a popular other OS doesn't use it */
1093 quirks |= NO_CAP_LINE; 1088 quirks |= NO_CAP_LINE;
1094 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) { 1089 if (data_interface->cur_altsetting->desc.bNumEndpoints != 3) {
1095 dev_err(&intf->dev, "This needs exactly 3 endpoints\n"); 1090 dev_err(&intf->dev, "This needs exactly 3 endpoints\n");
1096 return -EINVAL; 1091 return -EINVAL;
1097 } 1092 }
1098 look_for_collapsed_interface: 1093 look_for_collapsed_interface:
1099 for (i = 0; i < 3; i++) { 1094 for (i = 0; i < 3; i++) {
1100 struct usb_endpoint_descriptor *ep; 1095 struct usb_endpoint_descriptor *ep;
1101 ep = &data_interface->cur_altsetting->endpoint[i].desc; 1096 ep = &data_interface->cur_altsetting->endpoint[i].desc;
1102 1097
1103 if (usb_endpoint_is_int_in(ep)) 1098 if (usb_endpoint_is_int_in(ep))
1104 epctrl = ep; 1099 epctrl = ep;
1105 else if (usb_endpoint_is_bulk_out(ep)) 1100 else if (usb_endpoint_is_bulk_out(ep))
1106 epwrite = ep; 1101 epwrite = ep;
1107 else if (usb_endpoint_is_bulk_in(ep)) 1102 else if (usb_endpoint_is_bulk_in(ep))
1108 epread = ep; 1103 epread = ep;
1109 else 1104 else
1110 return -EINVAL; 1105 return -EINVAL;
1111 } 1106 }
1112 if (!epctrl || !epread || !epwrite) 1107 if (!epctrl || !epread || !epwrite)
1113 return -ENODEV; 1108 return -ENODEV;
1114 else 1109 else
1115 goto made_compressed_probe; 1110 goto made_compressed_probe;
1116 } 1111 }
1117 1112
1118 skip_normal_probe: 1113 skip_normal_probe:
1119 1114
1120 /*workaround for switched interfaces */ 1115 /*workaround for switched interfaces */
1121 if (data_interface->cur_altsetting->desc.bInterfaceClass 1116 if (data_interface->cur_altsetting->desc.bInterfaceClass
1122 != CDC_DATA_INTERFACE_TYPE) { 1117 != CDC_DATA_INTERFACE_TYPE) {
1123 if (control_interface->cur_altsetting->desc.bInterfaceClass 1118 if (control_interface->cur_altsetting->desc.bInterfaceClass
1124 == CDC_DATA_INTERFACE_TYPE) { 1119 == CDC_DATA_INTERFACE_TYPE) {
1125 struct usb_interface *t; 1120 struct usb_interface *t;
1126 dev_dbg(&intf->dev, 1121 dev_dbg(&intf->dev,
1127 "Your device has switched interfaces.\n"); 1122 "Your device has switched interfaces.\n");
1128 t = control_interface; 1123 t = control_interface;
1129 control_interface = data_interface; 1124 control_interface = data_interface;
1130 data_interface = t; 1125 data_interface = t;
1131 } else { 1126 } else {
1132 return -EINVAL; 1127 return -EINVAL;
1133 } 1128 }
1134 } 1129 }
1135 1130
1136 /* Accept probe requests only for the control interface */ 1131 /* Accept probe requests only for the control interface */
1137 if (!combined_interfaces && intf != control_interface) 1132 if (!combined_interfaces && intf != control_interface)
1138 return -ENODEV; 1133 return -ENODEV;
1139 1134
1140 if (!combined_interfaces && usb_interface_claimed(data_interface)) { 1135 if (!combined_interfaces && usb_interface_claimed(data_interface)) {
1141 /* valid in this context */ 1136 /* valid in this context */
1142 dev_dbg(&intf->dev, "The data interface isn't available\n"); 1137 dev_dbg(&intf->dev, "The data interface isn't available\n");
1143 return -EBUSY; 1138 return -EBUSY;
1144 } 1139 }
1145 1140
1146 1141
1147 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 || 1142 if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 ||
1148 control_interface->cur_altsetting->desc.bNumEndpoints == 0) 1143 control_interface->cur_altsetting->desc.bNumEndpoints == 0)
1149 return -EINVAL; 1144 return -EINVAL;
1150 1145
1151 epctrl = &control_interface->cur_altsetting->endpoint[0].desc; 1146 epctrl = &control_interface->cur_altsetting->endpoint[0].desc;
1152 epread = &data_interface->cur_altsetting->endpoint[0].desc; 1147 epread = &data_interface->cur_altsetting->endpoint[0].desc;
1153 epwrite = &data_interface->cur_altsetting->endpoint[1].desc; 1148 epwrite = &data_interface->cur_altsetting->endpoint[1].desc;
1154 1149
1155 1150
1156 /* workaround for switched endpoints */ 1151 /* workaround for switched endpoints */
1157 if (!usb_endpoint_dir_in(epread)) { 1152 if (!usb_endpoint_dir_in(epread)) {
1158 /* descriptors are swapped */ 1153 /* descriptors are swapped */
1159 struct usb_endpoint_descriptor *t; 1154 struct usb_endpoint_descriptor *t;
1160 dev_dbg(&intf->dev, 1155 dev_dbg(&intf->dev,
1161 "The data interface has switched endpoints\n"); 1156 "The data interface has switched endpoints\n");
1162 t = epread; 1157 t = epread;
1163 epread = epwrite; 1158 epread = epwrite;
1164 epwrite = t; 1159 epwrite = t;
1165 } 1160 }
1166 made_compressed_probe: 1161 made_compressed_probe:
1167 dev_dbg(&intf->dev, "interfaces are valid\n"); 1162 dev_dbg(&intf->dev, "interfaces are valid\n");
1168 1163
1169 acm = kzalloc(sizeof(struct acm), GFP_KERNEL); 1164 acm = kzalloc(sizeof(struct acm), GFP_KERNEL);
1170 if (acm == NULL) { 1165 if (acm == NULL) {
1171 dev_err(&intf->dev, "out of memory (acm kzalloc)\n"); 1166 dev_err(&intf->dev, "out of memory (acm kzalloc)\n");
1172 goto alloc_fail; 1167 goto alloc_fail;
1173 } 1168 }
1174 1169
1175 minor = acm_alloc_minor(acm); 1170 minor = acm_alloc_minor(acm);
1176 if (minor == ACM_TTY_MINORS) { 1171 if (minor == ACM_TTY_MINORS) {
1177 dev_err(&intf->dev, "no more free acm devices\n"); 1172 dev_err(&intf->dev, "no more free acm devices\n");
1178 kfree(acm); 1173 kfree(acm);
1179 return -ENODEV; 1174 return -ENODEV;
1180 } 1175 }
1181 1176
1182 ctrlsize = usb_endpoint_maxp(epctrl); 1177 ctrlsize = usb_endpoint_maxp(epctrl);
1183 readsize = usb_endpoint_maxp(epread) * 1178 readsize = usb_endpoint_maxp(epread) *
1184 (quirks == SINGLE_RX_URB ? 1 : 2); 1179 (quirks == SINGLE_RX_URB ? 1 : 2);
1185 acm->combined_interfaces = combined_interfaces; 1180 acm->combined_interfaces = combined_interfaces;
1186 acm->writesize = usb_endpoint_maxp(epwrite) * 20; 1181 acm->writesize = usb_endpoint_maxp(epwrite) * 20;
1187 acm->control = control_interface; 1182 acm->control = control_interface;
1188 acm->data = data_interface; 1183 acm->data = data_interface;
1189 acm->minor = minor; 1184 acm->minor = minor;
1190 acm->dev = usb_dev; 1185 acm->dev = usb_dev;
1191 acm->ctrl_caps = ac_management_function; 1186 acm->ctrl_caps = ac_management_function;
1192 if (quirks & NO_CAP_LINE) 1187 if (quirks & NO_CAP_LINE)
1193 acm->ctrl_caps &= ~USB_CDC_CAP_LINE; 1188 acm->ctrl_caps &= ~USB_CDC_CAP_LINE;
1194 acm->ctrlsize = ctrlsize; 1189 acm->ctrlsize = ctrlsize;
1195 acm->readsize = readsize; 1190 acm->readsize = readsize;
1196 acm->rx_buflimit = num_rx_buf; 1191 acm->rx_buflimit = num_rx_buf;
1197 INIT_WORK(&acm->work, acm_softint); 1192 INIT_WORK(&acm->work, acm_softint);
1198 spin_lock_init(&acm->write_lock); 1193 spin_lock_init(&acm->write_lock);
1199 spin_lock_init(&acm->read_lock); 1194 spin_lock_init(&acm->read_lock);
1200 mutex_init(&acm->mutex); 1195 mutex_init(&acm->mutex);
1201 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress); 1196 acm->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress);
1202 acm->is_int_ep = usb_endpoint_xfer_int(epread); 1197 acm->is_int_ep = usb_endpoint_xfer_int(epread);
1203 if (acm->is_int_ep) 1198 if (acm->is_int_ep)
1204 acm->bInterval = epread->bInterval; 1199 acm->bInterval = epread->bInterval;
1205 tty_port_init(&acm->port); 1200 tty_port_init(&acm->port);
1206 acm->port.ops = &acm_port_ops; 1201 acm->port.ops = &acm_port_ops;
1207 1202
1208 buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma); 1203 buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &acm->ctrl_dma);
1209 if (!buf) { 1204 if (!buf) {
1210 dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n"); 1205 dev_err(&intf->dev, "out of memory (ctrl buffer alloc)\n");
1211 goto alloc_fail2; 1206 goto alloc_fail2;
1212 } 1207 }
1213 acm->ctrl_buffer = buf; 1208 acm->ctrl_buffer = buf;
1214 1209
1215 if (acm_write_buffers_alloc(acm) < 0) { 1210 if (acm_write_buffers_alloc(acm) < 0) {
1216 dev_err(&intf->dev, "out of memory (write buffer alloc)\n"); 1211 dev_err(&intf->dev, "out of memory (write buffer alloc)\n");
1217 goto alloc_fail4; 1212 goto alloc_fail4;
1218 } 1213 }
1219 1214
1220 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL); 1215 acm->ctrlurb = usb_alloc_urb(0, GFP_KERNEL);
1221 if (!acm->ctrlurb) { 1216 if (!acm->ctrlurb) {
1222 dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n"); 1217 dev_err(&intf->dev, "out of memory (ctrlurb kmalloc)\n");
1223 goto alloc_fail5; 1218 goto alloc_fail5;
1224 } 1219 }
1225 for (i = 0; i < num_rx_buf; i++) { 1220 for (i = 0; i < num_rx_buf; i++) {
1226 struct acm_rb *rb = &(acm->read_buffers[i]); 1221 struct acm_rb *rb = &(acm->read_buffers[i]);
1227 struct urb *urb; 1222 struct urb *urb;
1228 1223
1229 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL, 1224 rb->base = usb_alloc_coherent(acm->dev, readsize, GFP_KERNEL,
1230 &rb->dma); 1225 &rb->dma);
1231 if (!rb->base) { 1226 if (!rb->base) {
1232 dev_err(&intf->dev, "out of memory " 1227 dev_err(&intf->dev, "out of memory "
1233 "(read bufs usb_alloc_coherent)\n"); 1228 "(read bufs usb_alloc_coherent)\n");
1234 goto alloc_fail6; 1229 goto alloc_fail6;
1235 } 1230 }
1236 rb->index = i; 1231 rb->index = i;
1237 rb->instance = acm; 1232 rb->instance = acm;
1238 1233
1239 urb = usb_alloc_urb(0, GFP_KERNEL); 1234 urb = usb_alloc_urb(0, GFP_KERNEL);
1240 if (!urb) { 1235 if (!urb) {
1241 dev_err(&intf->dev, 1236 dev_err(&intf->dev,
1242 "out of memory (read urbs usb_alloc_urb)\n"); 1237 "out of memory (read urbs usb_alloc_urb)\n");
1243 goto alloc_fail6; 1238 goto alloc_fail6;
1244 } 1239 }
1245 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1240 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1246 urb->transfer_dma = rb->dma; 1241 urb->transfer_dma = rb->dma;
1247 if (acm->is_int_ep) { 1242 if (acm->is_int_ep) {
1248 usb_fill_int_urb(urb, acm->dev, 1243 usb_fill_int_urb(urb, acm->dev,
1249 acm->rx_endpoint, 1244 acm->rx_endpoint,
1250 rb->base, 1245 rb->base,
1251 acm->readsize, 1246 acm->readsize,
1252 acm_read_bulk_callback, rb, 1247 acm_read_bulk_callback, rb,
1253 acm->bInterval); 1248 acm->bInterval);
1254 } else { 1249 } else {
1255 usb_fill_bulk_urb(urb, acm->dev, 1250 usb_fill_bulk_urb(urb, acm->dev,
1256 acm->rx_endpoint, 1251 acm->rx_endpoint,
1257 rb->base, 1252 rb->base,
1258 acm->readsize, 1253 acm->readsize,
1259 acm_read_bulk_callback, rb); 1254 acm_read_bulk_callback, rb);
1260 } 1255 }
1261 1256
1262 acm->read_urbs[i] = urb; 1257 acm->read_urbs[i] = urb;
1263 __set_bit(i, &acm->read_urbs_free); 1258 __set_bit(i, &acm->read_urbs_free);
1264 } 1259 }
1265 for (i = 0; i < ACM_NW; i++) { 1260 for (i = 0; i < ACM_NW; i++) {
1266 struct acm_wb *snd = &(acm->wb[i]); 1261 struct acm_wb *snd = &(acm->wb[i]);
1267 1262
1268 snd->urb = usb_alloc_urb(0, GFP_KERNEL); 1263 snd->urb = usb_alloc_urb(0, GFP_KERNEL);
1269 if (snd->urb == NULL) { 1264 if (snd->urb == NULL) {
1270 dev_err(&intf->dev, 1265 dev_err(&intf->dev,
1271 "out of memory (write urbs usb_alloc_urb)\n"); 1266 "out of memory (write urbs usb_alloc_urb)\n");
1272 goto alloc_fail7; 1267 goto alloc_fail7;
1273 } 1268 }
1274 1269
1275 if (usb_endpoint_xfer_int(epwrite)) 1270 if (usb_endpoint_xfer_int(epwrite))
1276 usb_fill_int_urb(snd->urb, usb_dev, 1271 usb_fill_int_urb(snd->urb, usb_dev,
1277 usb_sndintpipe(usb_dev, epwrite->bEndpointAddress), 1272 usb_sndintpipe(usb_dev, epwrite->bEndpointAddress),
1278 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval); 1273 NULL, acm->writesize, acm_write_bulk, snd, epwrite->bInterval);
1279 else 1274 else
1280 usb_fill_bulk_urb(snd->urb, usb_dev, 1275 usb_fill_bulk_urb(snd->urb, usb_dev,
1281 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress), 1276 usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress),
1282 NULL, acm->writesize, acm_write_bulk, snd); 1277 NULL, acm->writesize, acm_write_bulk, snd);
1283 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1278 snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1284 snd->instance = acm; 1279 snd->instance = acm;
1285 } 1280 }
1286 1281
1287 usb_set_intfdata(intf, acm); 1282 usb_set_intfdata(intf, acm);
1288 1283
1289 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities); 1284 i = device_create_file(&intf->dev, &dev_attr_bmCapabilities);
1290 if (i < 0) 1285 if (i < 0)
1291 goto alloc_fail7; 1286 goto alloc_fail7;
1292 1287
1293 if (cfd) { /* export the country data */ 1288 if (cfd) { /* export the country data */
1294 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL); 1289 acm->country_codes = kmalloc(cfd->bLength - 4, GFP_KERNEL);
1295 if (!acm->country_codes) 1290 if (!acm->country_codes)
1296 goto skip_countries; 1291 goto skip_countries;
1297 acm->country_code_size = cfd->bLength - 4; 1292 acm->country_code_size = cfd->bLength - 4;
1298 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0, 1293 memcpy(acm->country_codes, (u8 *)&cfd->wCountyCode0,
1299 cfd->bLength - 4); 1294 cfd->bLength - 4);
1300 acm->country_rel_date = cfd->iCountryCodeRelDate; 1295 acm->country_rel_date = cfd->iCountryCodeRelDate;
1301 1296
1302 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes); 1297 i = device_create_file(&intf->dev, &dev_attr_wCountryCodes);
1303 if (i < 0) { 1298 if (i < 0) {
1304 kfree(acm->country_codes); 1299 kfree(acm->country_codes);
1305 acm->country_codes = NULL; 1300 acm->country_codes = NULL;
1306 acm->country_code_size = 0; 1301 acm->country_code_size = 0;
1307 goto skip_countries; 1302 goto skip_countries;
1308 } 1303 }
1309 1304
1310 i = device_create_file(&intf->dev, 1305 i = device_create_file(&intf->dev,
1311 &dev_attr_iCountryCodeRelDate); 1306 &dev_attr_iCountryCodeRelDate);
1312 if (i < 0) { 1307 if (i < 0) {
1313 device_remove_file(&intf->dev, &dev_attr_wCountryCodes); 1308 device_remove_file(&intf->dev, &dev_attr_wCountryCodes);
1314 kfree(acm->country_codes); 1309 kfree(acm->country_codes);
1315 acm->country_codes = NULL; 1310 acm->country_codes = NULL;
1316 acm->country_code_size = 0; 1311 acm->country_code_size = 0;
1317 goto skip_countries; 1312 goto skip_countries;
1318 } 1313 }
1319 } 1314 }
1320 1315
1321 skip_countries: 1316 skip_countries:
1322 usb_fill_int_urb(acm->ctrlurb, usb_dev, 1317 usb_fill_int_urb(acm->ctrlurb, usb_dev,
1323 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress), 1318 usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress),
1324 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm, 1319 acm->ctrl_buffer, ctrlsize, acm_ctrl_irq, acm,
1325 /* works around buggy devices */ 1320 /* works around buggy devices */
1326 epctrl->bInterval ? epctrl->bInterval : 0xff); 1321 epctrl->bInterval ? epctrl->bInterval : 0xff);
1327 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 1322 acm->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1328 acm->ctrlurb->transfer_dma = acm->ctrl_dma; 1323 acm->ctrlurb->transfer_dma = acm->ctrl_dma;
1329 1324
1330 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor); 1325 dev_info(&intf->dev, "ttyACM%d: USB ACM device\n", minor);
1331 1326
1332 acm_set_control(acm, acm->ctrlout); 1327 acm_set_control(acm, acm->ctrlout);
1333 1328
1334 acm->line.dwDTERate = cpu_to_le32(9600); 1329 acm->line.dwDTERate = cpu_to_le32(9600);
1335 acm->line.bDataBits = 8; 1330 acm->line.bDataBits = 8;
1336 acm_set_line(acm, &acm->line); 1331 acm_set_line(acm, &acm->line);
1337 1332
1338 usb_driver_claim_interface(&acm_driver, data_interface, acm); 1333 usb_driver_claim_interface(&acm_driver, data_interface, acm);
1339 usb_set_intfdata(data_interface, acm); 1334 usb_set_intfdata(data_interface, acm);
1340 1335
1341 usb_get_intf(control_interface); 1336 usb_get_intf(control_interface);
1342 tty_port_register_device(&acm->port, acm_tty_driver, minor, 1337 tty_port_register_device(&acm->port, acm_tty_driver, minor,
1343 &control_interface->dev); 1338 &control_interface->dev);
1344 1339
1345 return 0; 1340 return 0;
1346 alloc_fail7: 1341 alloc_fail7:
1347 for (i = 0; i < ACM_NW; i++) 1342 for (i = 0; i < ACM_NW; i++)
1348 usb_free_urb(acm->wb[i].urb); 1343 usb_free_urb(acm->wb[i].urb);
1349 alloc_fail6: 1344 alloc_fail6:
1350 for (i = 0; i < num_rx_buf; i++) 1345 for (i = 0; i < num_rx_buf; i++)
1351 usb_free_urb(acm->read_urbs[i]); 1346 usb_free_urb(acm->read_urbs[i]);
1352 acm_read_buffers_free(acm); 1347 acm_read_buffers_free(acm);
1353 usb_free_urb(acm->ctrlurb); 1348 usb_free_urb(acm->ctrlurb);
1354 alloc_fail5: 1349 alloc_fail5:
1355 acm_write_buffers_free(acm); 1350 acm_write_buffers_free(acm);
1356 alloc_fail4: 1351 alloc_fail4:
1357 usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); 1352 usb_free_coherent(usb_dev, ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1358 alloc_fail2: 1353 alloc_fail2:
1359 acm_release_minor(acm); 1354 acm_release_minor(acm);
1360 kfree(acm); 1355 kfree(acm);
1361 alloc_fail: 1356 alloc_fail:
1362 return -ENOMEM; 1357 return -ENOMEM;
1363 } 1358 }
1364 1359
1365 static void stop_data_traffic(struct acm *acm) 1360 static void stop_data_traffic(struct acm *acm)
1366 { 1361 {
1367 int i; 1362 int i;
1368 1363
1369 dev_dbg(&acm->control->dev, "%s\n", __func__); 1364 dev_dbg(&acm->control->dev, "%s\n", __func__);
1370 1365
1371 usb_kill_urb(acm->ctrlurb); 1366 usb_kill_urb(acm->ctrlurb);
1372 for (i = 0; i < ACM_NW; i++) 1367 for (i = 0; i < ACM_NW; i++)
1373 usb_kill_urb(acm->wb[i].urb); 1368 usb_kill_urb(acm->wb[i].urb);
1374 for (i = 0; i < acm->rx_buflimit; i++) 1369 for (i = 0; i < acm->rx_buflimit; i++)
1375 usb_kill_urb(acm->read_urbs[i]); 1370 usb_kill_urb(acm->read_urbs[i]);
1376 1371
1377 cancel_work_sync(&acm->work); 1372 cancel_work_sync(&acm->work);
1378 } 1373 }
1379 1374
1380 static void acm_disconnect(struct usb_interface *intf) 1375 static void acm_disconnect(struct usb_interface *intf)
1381 { 1376 {
1382 struct acm *acm = usb_get_intfdata(intf); 1377 struct acm *acm = usb_get_intfdata(intf);
1383 struct usb_device *usb_dev = interface_to_usbdev(intf); 1378 struct usb_device *usb_dev = interface_to_usbdev(intf);
1384 struct tty_struct *tty; 1379 struct tty_struct *tty;
1385 int i; 1380 int i;
1386 1381
1387 dev_dbg(&intf->dev, "%s\n", __func__); 1382 dev_dbg(&intf->dev, "%s\n", __func__);
1388 1383
1389 /* sibling interface is already cleaning up */ 1384 /* sibling interface is already cleaning up */
1390 if (!acm) 1385 if (!acm)
1391 return; 1386 return;
1392 1387
1393 mutex_lock(&acm->mutex); 1388 mutex_lock(&acm->mutex);
1394 acm->disconnected = true; 1389 acm->disconnected = true;
1395 if (acm->country_codes) { 1390 if (acm->country_codes) {
1396 device_remove_file(&acm->control->dev, 1391 device_remove_file(&acm->control->dev,
1397 &dev_attr_wCountryCodes); 1392 &dev_attr_wCountryCodes);
1398 device_remove_file(&acm->control->dev, 1393 device_remove_file(&acm->control->dev,
1399 &dev_attr_iCountryCodeRelDate); 1394 &dev_attr_iCountryCodeRelDate);
1400 } 1395 }
1401 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities); 1396 device_remove_file(&acm->control->dev, &dev_attr_bmCapabilities);
1402 usb_set_intfdata(acm->control, NULL); 1397 usb_set_intfdata(acm->control, NULL);
1403 usb_set_intfdata(acm->data, NULL); 1398 usb_set_intfdata(acm->data, NULL);
1404 mutex_unlock(&acm->mutex); 1399 mutex_unlock(&acm->mutex);
1405 1400
1406 tty = tty_port_tty_get(&acm->port); 1401 tty = tty_port_tty_get(&acm->port);
1407 if (tty) { 1402 if (tty) {
1408 tty_vhangup(tty); 1403 tty_vhangup(tty);
1409 tty_kref_put(tty); 1404 tty_kref_put(tty);
1410 } 1405 }
1411 1406
1412 stop_data_traffic(acm); 1407 stop_data_traffic(acm);
1413 1408
1414 usb_free_urb(acm->ctrlurb); 1409 usb_free_urb(acm->ctrlurb);
1415 for (i = 0; i < ACM_NW; i++) 1410 for (i = 0; i < ACM_NW; i++)
1416 usb_free_urb(acm->wb[i].urb); 1411 usb_free_urb(acm->wb[i].urb);
1417 for (i = 0; i < acm->rx_buflimit; i++) 1412 for (i = 0; i < acm->rx_buflimit; i++)
1418 usb_free_urb(acm->read_urbs[i]); 1413 usb_free_urb(acm->read_urbs[i]);
1419 acm_write_buffers_free(acm); 1414 acm_write_buffers_free(acm);
1420 usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma); 1415 usb_free_coherent(usb_dev, acm->ctrlsize, acm->ctrl_buffer, acm->ctrl_dma);
1421 acm_read_buffers_free(acm); 1416 acm_read_buffers_free(acm);
1422 1417
1423 if (!acm->combined_interfaces) 1418 if (!acm->combined_interfaces)
1424 usb_driver_release_interface(&acm_driver, intf == acm->control ? 1419 usb_driver_release_interface(&acm_driver, intf == acm->control ?
1425 acm->data : acm->control); 1420 acm->data : acm->control);
1426 1421
1427 tty_port_put(&acm->port); 1422 tty_port_put(&acm->port);
1428 } 1423 }
1429 1424
1430 #ifdef CONFIG_PM 1425 #ifdef CONFIG_PM
1431 static int acm_suspend(struct usb_interface *intf, pm_message_t message) 1426 static int acm_suspend(struct usb_interface *intf, pm_message_t message)
1432 { 1427 {
1433 struct acm *acm = usb_get_intfdata(intf); 1428 struct acm *acm = usb_get_intfdata(intf);
1434 int cnt; 1429 int cnt;
1435 1430
1436 if (PMSG_IS_AUTO(message)) { 1431 if (PMSG_IS_AUTO(message)) {
1437 int b; 1432 int b;
1438 1433
1439 spin_lock_irq(&acm->write_lock); 1434 spin_lock_irq(&acm->write_lock);
1440 b = acm->transmitting; 1435 b = acm->transmitting;
1441 spin_unlock_irq(&acm->write_lock); 1436 spin_unlock_irq(&acm->write_lock);
1442 if (b) 1437 if (b)
1443 return -EBUSY; 1438 return -EBUSY;
1444 } 1439 }
1445 1440
1446 spin_lock_irq(&acm->read_lock); 1441 spin_lock_irq(&acm->read_lock);
1447 spin_lock(&acm->write_lock); 1442 spin_lock(&acm->write_lock);
1448 cnt = acm->susp_count++; 1443 cnt = acm->susp_count++;
1449 spin_unlock(&acm->write_lock); 1444 spin_unlock(&acm->write_lock);
1450 spin_unlock_irq(&acm->read_lock); 1445 spin_unlock_irq(&acm->read_lock);
1451 1446
1452 if (cnt) 1447 if (cnt)
1453 return 0; 1448 return 0;
1454 1449
1455 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) 1450 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags))
1456 stop_data_traffic(acm); 1451 stop_data_traffic(acm);
1457 1452
1458 return 0; 1453 return 0;
1459 } 1454 }
1460 1455
1461 static int acm_resume(struct usb_interface *intf) 1456 static int acm_resume(struct usb_interface *intf)
1462 { 1457 {
1463 struct acm *acm = usb_get_intfdata(intf); 1458 struct acm *acm = usb_get_intfdata(intf);
1464 struct acm_wb *wb; 1459 struct acm_wb *wb;
1465 int rv = 0; 1460 int rv = 0;
1466 int cnt; 1461 int cnt;
1467 1462
1468 spin_lock_irq(&acm->read_lock); 1463 spin_lock_irq(&acm->read_lock);
1469 acm->susp_count -= 1; 1464 acm->susp_count -= 1;
1470 cnt = acm->susp_count; 1465 cnt = acm->susp_count;
1471 spin_unlock_irq(&acm->read_lock); 1466 spin_unlock_irq(&acm->read_lock);
1472 1467
1473 if (cnt) 1468 if (cnt)
1474 return 0; 1469 return 0;
1475 1470
1476 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) { 1471 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) {
1477 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO); 1472 rv = usb_submit_urb(acm->ctrlurb, GFP_NOIO);
1478 1473
1479 spin_lock_irq(&acm->write_lock); 1474 spin_lock_irq(&acm->write_lock);
1480 if (acm->delayed_wb) { 1475 if (acm->delayed_wb) {
1481 wb = acm->delayed_wb; 1476 wb = acm->delayed_wb;
1482 acm->delayed_wb = NULL; 1477 acm->delayed_wb = NULL;
1483 spin_unlock_irq(&acm->write_lock); 1478 spin_unlock_irq(&acm->write_lock);
1484 acm_start_wb(acm, wb); 1479 acm_start_wb(acm, wb);
1485 } else { 1480 } else {
1486 spin_unlock_irq(&acm->write_lock); 1481 spin_unlock_irq(&acm->write_lock);
1487 } 1482 }
1488 1483
1489 /* 1484 /*
1490 * delayed error checking because we must 1485 * delayed error checking because we must
1491 * do the write path at all cost 1486 * do the write path at all cost
1492 */ 1487 */
1493 if (rv < 0) 1488 if (rv < 0)
1494 goto err_out; 1489 goto err_out;
1495 1490
1496 rv = acm_submit_read_urbs(acm, GFP_NOIO); 1491 rv = acm_submit_read_urbs(acm, GFP_NOIO);
1497 } 1492 }
1498 1493
1499 err_out: 1494 err_out:
1500 return rv; 1495 return rv;
1501 } 1496 }
1502 1497
1503 static int acm_reset_resume(struct usb_interface *intf) 1498 static int acm_reset_resume(struct usb_interface *intf)
1504 { 1499 {
1505 struct acm *acm = usb_get_intfdata(intf); 1500 struct acm *acm = usb_get_intfdata(intf);
1506 struct tty_struct *tty; 1501 struct tty_struct *tty;
1507 1502
1508 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) { 1503 if (test_bit(ASYNCB_INITIALIZED, &acm->port.flags)) {
1509 tty = tty_port_tty_get(&acm->port); 1504 tty = tty_port_tty_get(&acm->port);
1510 if (tty) { 1505 if (tty) {
1511 tty_hangup(tty); 1506 tty_hangup(tty);
1512 tty_kref_put(tty); 1507 tty_kref_put(tty);
1513 } 1508 }
1514 } 1509 }
1515 1510
1516 return acm_resume(intf); 1511 return acm_resume(intf);
1517 } 1512 }
1518 1513
1519 #endif /* CONFIG_PM */ 1514 #endif /* CONFIG_PM */
1520 1515
1521 #define NOKIA_PCSUITE_ACM_INFO(x) \ 1516 #define NOKIA_PCSUITE_ACM_INFO(x) \
1522 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \ 1517 USB_DEVICE_AND_INTERFACE_INFO(0x0421, x, \
1523 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \ 1518 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1524 USB_CDC_ACM_PROTO_VENDOR) 1519 USB_CDC_ACM_PROTO_VENDOR)
1525 1520
1526 #define SAMSUNG_PCSUITE_ACM_INFO(x) \ 1521 #define SAMSUNG_PCSUITE_ACM_INFO(x) \
1527 USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \ 1522 USB_DEVICE_AND_INTERFACE_INFO(0x04e7, x, \
1528 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \ 1523 USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, \
1529 USB_CDC_ACM_PROTO_VENDOR) 1524 USB_CDC_ACM_PROTO_VENDOR)
1530 1525
1531 /* 1526 /*
1532 * USB driver structure. 1527 * USB driver structure.
1533 */ 1528 */
1534 1529
1535 static const struct usb_device_id acm_ids[] = { 1530 static const struct usb_device_id acm_ids[] = {
1536 /* quirky and broken devices */ 1531 /* quirky and broken devices */
1537 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */ 1532 { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1538 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1533 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1539 }, 1534 },
1540 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */ 1535 { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1541 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1536 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1542 }, 1537 },
1543 { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */ 1538 { USB_DEVICE(0x0e8d, 0x3329), /* MediaTek Inc GPS */
1544 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1539 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1545 }, 1540 },
1546 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */ 1541 { USB_DEVICE(0x0482, 0x0203), /* KYOCERA AH-K3001V */
1547 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1542 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1548 }, 1543 },
1549 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */ 1544 { USB_DEVICE(0x079b, 0x000f), /* BT On-Air USB MODEM */
1550 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1545 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1551 }, 1546 },
1552 { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */ 1547 { USB_DEVICE(0x0ace, 0x1602), /* ZyDAS 56K USB MODEM */
1553 .driver_info = SINGLE_RX_URB, 1548 .driver_info = SINGLE_RX_URB,
1554 }, 1549 },
1555 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */ 1550 { USB_DEVICE(0x0ace, 0x1608), /* ZyDAS 56K USB MODEM */
1556 .driver_info = SINGLE_RX_URB, /* firmware bug */ 1551 .driver_info = SINGLE_RX_URB, /* firmware bug */
1557 }, 1552 },
1558 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */ 1553 { USB_DEVICE(0x0ace, 0x1611), /* ZyDAS 56K USB MODEM - new version */
1559 .driver_info = SINGLE_RX_URB, /* firmware bug */ 1554 .driver_info = SINGLE_RX_URB, /* firmware bug */
1560 }, 1555 },
1561 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */ 1556 { USB_DEVICE(0x22b8, 0x7000), /* Motorola Q Phone */
1562 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1557 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1563 }, 1558 },
1564 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */ 1559 { USB_DEVICE(0x0803, 0x3095), /* Zoom Telephonics Model 3095F USB MODEM */
1565 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1560 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1566 }, 1561 },
1567 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */ 1562 { USB_DEVICE(0x0572, 0x1321), /* Conexant USB MODEM CX93010 */
1568 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1563 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1569 }, 1564 },
1570 { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */ 1565 { USB_DEVICE(0x0572, 0x1324), /* Conexant USB MODEM RD02-D400 */
1571 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1566 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1572 }, 1567 },
1573 { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */ 1568 { USB_DEVICE(0x0572, 0x1328), /* Shiro / Aztech USB MODEM UM-3100 */
1574 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */ 1569 .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1575 }, 1570 },
1576 { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */ 1571 { USB_DEVICE(0x22b8, 0x6425), /* Motorola MOTOMAGX phones */
1577 }, 1572 },
1578 /* Motorola H24 HSPA module: */ 1573 /* Motorola H24 HSPA module: */
1579 { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */ 1574 { USB_DEVICE(0x22b8, 0x2d91) }, /* modem */
1580 { USB_DEVICE(0x22b8, 0x2d92) }, /* modem + diagnostics */ 1575 { USB_DEVICE(0x22b8, 0x2d92) }, /* modem + diagnostics */
1581 { USB_DEVICE(0x22b8, 0x2d93) }, /* modem + AT port */ 1576 { USB_DEVICE(0x22b8, 0x2d93) }, /* modem + AT port */
1582 { USB_DEVICE(0x22b8, 0x2d95) }, /* modem + AT port + diagnostics */ 1577 { USB_DEVICE(0x22b8, 0x2d95) }, /* modem + AT port + diagnostics */
1583 { USB_DEVICE(0x22b8, 0x2d96) }, /* modem + NMEA */ 1578 { USB_DEVICE(0x22b8, 0x2d96) }, /* modem + NMEA */
1584 { USB_DEVICE(0x22b8, 0x2d97) }, /* modem + diagnostics + NMEA */ 1579 { USB_DEVICE(0x22b8, 0x2d97) }, /* modem + diagnostics + NMEA */
1585 { USB_DEVICE(0x22b8, 0x2d99) }, /* modem + AT port + NMEA */ 1580 { USB_DEVICE(0x22b8, 0x2d99) }, /* modem + AT port + NMEA */
1586 { USB_DEVICE(0x22b8, 0x2d9a) }, /* modem + AT port + diagnostics + NMEA */ 1581 { USB_DEVICE(0x22b8, 0x2d9a) }, /* modem + AT port + diagnostics + NMEA */
1587 1582
1588 { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */ 1583 { USB_DEVICE(0x0572, 0x1329), /* Hummingbird huc56s (Conexant) */
1589 .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on 1584 .driver_info = NO_UNION_NORMAL, /* union descriptor misplaced on
1590 data interface instead of 1585 data interface instead of
1591 communications interface. 1586 communications interface.
1592 Maybe we should define a new 1587 Maybe we should define a new
1593 quirk for this. */ 1588 quirk for this. */
1594 }, 1589 },
1595 { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */ 1590 { USB_DEVICE(0x0572, 0x1340), /* Conexant CX93010-2x UCMxx */
1596 .driver_info = NO_UNION_NORMAL, 1591 .driver_info = NO_UNION_NORMAL,
1597 }, 1592 },
1598 { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */ 1593 { USB_DEVICE(0x05f9, 0x4002), /* PSC Scanning, Magellan 800i */
1599 .driver_info = NO_UNION_NORMAL, 1594 .driver_info = NO_UNION_NORMAL,
1600 }, 1595 },
1601 { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */ 1596 { USB_DEVICE(0x1bbb, 0x0003), /* Alcatel OT-I650 */
1602 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */ 1597 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1603 }, 1598 },
1604 { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */ 1599 { USB_DEVICE(0x1576, 0x03b1), /* Maretron USB100 */
1605 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */ 1600 .driver_info = NO_UNION_NORMAL, /* reports zero length descriptor */
1606 }, 1601 },
1607 1602
1608 /* Nokia S60 phones expose two ACM channels. The first is 1603 /* Nokia S60 phones expose two ACM channels. The first is
1609 * a modem and is picked up by the standard AT-command 1604 * a modem and is picked up by the standard AT-command
1610 * information below. The second is 'vendor-specific' but 1605 * information below. The second is 'vendor-specific' but
1611 * is treated as a serial device at the S60 end, so we want 1606 * is treated as a serial device at the S60 end, so we want
1612 * to expose it on Linux too. */ 1607 * to expose it on Linux too. */
1613 { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */ 1608 { NOKIA_PCSUITE_ACM_INFO(0x042D), }, /* Nokia 3250 */
1614 { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */ 1609 { NOKIA_PCSUITE_ACM_INFO(0x04D8), }, /* Nokia 5500 Sport */
1615 { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */ 1610 { NOKIA_PCSUITE_ACM_INFO(0x04C9), }, /* Nokia E50 */
1616 { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */ 1611 { NOKIA_PCSUITE_ACM_INFO(0x0419), }, /* Nokia E60 */
1617 { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */ 1612 { NOKIA_PCSUITE_ACM_INFO(0x044D), }, /* Nokia E61 */
1618 { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */ 1613 { NOKIA_PCSUITE_ACM_INFO(0x0001), }, /* Nokia E61i */
1619 { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */ 1614 { NOKIA_PCSUITE_ACM_INFO(0x0475), }, /* Nokia E62 */
1620 { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */ 1615 { NOKIA_PCSUITE_ACM_INFO(0x0508), }, /* Nokia E65 */
1621 { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */ 1616 { NOKIA_PCSUITE_ACM_INFO(0x0418), }, /* Nokia E70 */
1622 { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */ 1617 { NOKIA_PCSUITE_ACM_INFO(0x0425), }, /* Nokia N71 */
1623 { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */ 1618 { NOKIA_PCSUITE_ACM_INFO(0x0486), }, /* Nokia N73 */
1624 { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */ 1619 { NOKIA_PCSUITE_ACM_INFO(0x04DF), }, /* Nokia N75 */
1625 { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */ 1620 { NOKIA_PCSUITE_ACM_INFO(0x000e), }, /* Nokia N77 */
1626 { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */ 1621 { NOKIA_PCSUITE_ACM_INFO(0x0445), }, /* Nokia N80 */
1627 { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */ 1622 { NOKIA_PCSUITE_ACM_INFO(0x042F), }, /* Nokia N91 & N91 8GB */
1628 { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */ 1623 { NOKIA_PCSUITE_ACM_INFO(0x048E), }, /* Nokia N92 */
1629 { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */ 1624 { NOKIA_PCSUITE_ACM_INFO(0x0420), }, /* Nokia N93 */
1630 { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */ 1625 { NOKIA_PCSUITE_ACM_INFO(0x04E6), }, /* Nokia N93i */
1631 { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */ 1626 { NOKIA_PCSUITE_ACM_INFO(0x04B2), }, /* Nokia 5700 XpressMusic */
1632 { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */ 1627 { NOKIA_PCSUITE_ACM_INFO(0x0134), }, /* Nokia 6110 Navigator (China) */
1633 { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */ 1628 { NOKIA_PCSUITE_ACM_INFO(0x046E), }, /* Nokia 6110 Navigator */
1634 { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */ 1629 { NOKIA_PCSUITE_ACM_INFO(0x002f), }, /* Nokia 6120 classic & */
1635 { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */ 1630 { NOKIA_PCSUITE_ACM_INFO(0x0088), }, /* Nokia 6121 classic */
1636 { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */ 1631 { NOKIA_PCSUITE_ACM_INFO(0x00fc), }, /* Nokia 6124 classic */
1637 { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */ 1632 { NOKIA_PCSUITE_ACM_INFO(0x0042), }, /* Nokia E51 */
1638 { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */ 1633 { NOKIA_PCSUITE_ACM_INFO(0x00b0), }, /* Nokia E66 */
1639 { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */ 1634 { NOKIA_PCSUITE_ACM_INFO(0x00ab), }, /* Nokia E71 */
1640 { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */ 1635 { NOKIA_PCSUITE_ACM_INFO(0x0481), }, /* Nokia N76 */
1641 { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */ 1636 { NOKIA_PCSUITE_ACM_INFO(0x0007), }, /* Nokia N81 & N81 8GB */
1642 { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */ 1637 { NOKIA_PCSUITE_ACM_INFO(0x0071), }, /* Nokia N82 */
1643 { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */ 1638 { NOKIA_PCSUITE_ACM_INFO(0x04F0), }, /* Nokia N95 & N95-3 NAM */
1644 { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */ 1639 { NOKIA_PCSUITE_ACM_INFO(0x0070), }, /* Nokia N95 8GB */
1645 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */ 1640 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1646 { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */ 1641 { NOKIA_PCSUITE_ACM_INFO(0x0099), }, /* Nokia 6210 Navigator, RM-367 */
1647 { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */ 1642 { NOKIA_PCSUITE_ACM_INFO(0x0128), }, /* Nokia 6210 Navigator, RM-419 */
1648 { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */ 1643 { NOKIA_PCSUITE_ACM_INFO(0x008f), }, /* Nokia 6220 Classic */
1649 { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */ 1644 { NOKIA_PCSUITE_ACM_INFO(0x00a0), }, /* Nokia 6650 */
1650 { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */ 1645 { NOKIA_PCSUITE_ACM_INFO(0x007b), }, /* Nokia N78 */
1651 { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */ 1646 { NOKIA_PCSUITE_ACM_INFO(0x0094), }, /* Nokia N85 */
1652 { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */ 1647 { NOKIA_PCSUITE_ACM_INFO(0x003a), }, /* Nokia N96 & N96-3 */
1653 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */ 1648 { NOKIA_PCSUITE_ACM_INFO(0x00e9), }, /* Nokia 5320 XpressMusic */
1654 { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */ 1649 { NOKIA_PCSUITE_ACM_INFO(0x0108), }, /* Nokia 5320 XpressMusic 2G */
1655 { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */ 1650 { NOKIA_PCSUITE_ACM_INFO(0x01f5), }, /* Nokia N97, RM-505 */
1656 { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */ 1651 { NOKIA_PCSUITE_ACM_INFO(0x02e3), }, /* Nokia 5230, RM-588 */
1657 { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */ 1652 { NOKIA_PCSUITE_ACM_INFO(0x0178), }, /* Nokia E63 */
1658 { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */ 1653 { NOKIA_PCSUITE_ACM_INFO(0x010e), }, /* Nokia E75 */
1659 { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */ 1654 { NOKIA_PCSUITE_ACM_INFO(0x02d9), }, /* Nokia 6760 Slide */
1660 { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */ 1655 { NOKIA_PCSUITE_ACM_INFO(0x01d0), }, /* Nokia E52 */
1661 { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */ 1656 { NOKIA_PCSUITE_ACM_INFO(0x0223), }, /* Nokia E72 */
1662 { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */ 1657 { NOKIA_PCSUITE_ACM_INFO(0x0275), }, /* Nokia X6 */
1663 { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */ 1658 { NOKIA_PCSUITE_ACM_INFO(0x026c), }, /* Nokia N97 Mini */
1664 { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */ 1659 { NOKIA_PCSUITE_ACM_INFO(0x0154), }, /* Nokia 5800 XpressMusic */
1665 { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */ 1660 { NOKIA_PCSUITE_ACM_INFO(0x04ce), }, /* Nokia E90 */
1666 { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */ 1661 { NOKIA_PCSUITE_ACM_INFO(0x01d4), }, /* Nokia E55 */
1667 { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */ 1662 { NOKIA_PCSUITE_ACM_INFO(0x0302), }, /* Nokia N8 */
1668 { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */ 1663 { NOKIA_PCSUITE_ACM_INFO(0x0335), }, /* Nokia E7 */
1669 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */ 1664 { NOKIA_PCSUITE_ACM_INFO(0x03cd), }, /* Nokia C7 */
1670 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */ 1665 { SAMSUNG_PCSUITE_ACM_INFO(0x6651), }, /* Samsung GTi8510 (INNOV8) */
1671 1666
1672 /* Support for Owen devices */ 1667 /* Support for Owen devices */
1673 { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */ 1668 { USB_DEVICE(0x03eb, 0x0030), }, /* Owen SI30 */
1674 1669
1675 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */ 1670 /* NOTE: non-Nokia COMM/ACM/0xff is likely MSFT RNDIS... NOT a modem! */
1676 1671
1677 /* Support Lego NXT using pbLua firmware */ 1672 /* Support Lego NXT using pbLua firmware */
1678 { USB_DEVICE(0x0694, 0xff00), 1673 { USB_DEVICE(0x0694, 0xff00),
1679 .driver_info = NOT_A_MODEM, 1674 .driver_info = NOT_A_MODEM,
1680 }, 1675 },
1681 1676
1682 /* Support for Droids MuIn LCD */ 1677 /* Support for Droids MuIn LCD */
1683 { USB_DEVICE(0x04d8, 0x000b), 1678 { USB_DEVICE(0x04d8, 0x000b),
1684 .driver_info = NO_DATA_INTERFACE, 1679 .driver_info = NO_DATA_INTERFACE,
1685 }, 1680 },
1686 1681
1687 /* control interfaces without any protocol set */ 1682 /* control interfaces without any protocol set */
1688 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1683 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1689 USB_CDC_PROTO_NONE) }, 1684 USB_CDC_PROTO_NONE) },
1690 1685
1691 /* control interfaces with various AT-command sets */ 1686 /* control interfaces with various AT-command sets */
1692 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1687 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1693 USB_CDC_ACM_PROTO_AT_V25TER) }, 1688 USB_CDC_ACM_PROTO_AT_V25TER) },
1694 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1689 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1695 USB_CDC_ACM_PROTO_AT_PCCA101) }, 1690 USB_CDC_ACM_PROTO_AT_PCCA101) },
1696 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1691 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1697 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) }, 1692 USB_CDC_ACM_PROTO_AT_PCCA101_WAKE) },
1698 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1693 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1699 USB_CDC_ACM_PROTO_AT_GSM) }, 1694 USB_CDC_ACM_PROTO_AT_GSM) },
1700 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1695 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1701 USB_CDC_ACM_PROTO_AT_3G) }, 1696 USB_CDC_ACM_PROTO_AT_3G) },
1702 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM, 1697 { USB_INTERFACE_INFO(USB_CLASS_COMM, USB_CDC_SUBCLASS_ACM,
1703 USB_CDC_ACM_PROTO_AT_CDMA) }, 1698 USB_CDC_ACM_PROTO_AT_CDMA) },
1704 1699
1705 { } 1700 { }
1706 }; 1701 };
1707 1702
1708 MODULE_DEVICE_TABLE(usb, acm_ids); 1703 MODULE_DEVICE_TABLE(usb, acm_ids);
1709 1704
1710 static struct usb_driver acm_driver = { 1705 static struct usb_driver acm_driver = {
1711 .name = "cdc_acm", 1706 .name = "cdc_acm",
1712 .probe = acm_probe, 1707 .probe = acm_probe,
1713 .disconnect = acm_disconnect, 1708 .disconnect = acm_disconnect,
1714 #ifdef CONFIG_PM 1709 #ifdef CONFIG_PM
1715 .suspend = acm_suspend, 1710 .suspend = acm_suspend,
1716 .resume = acm_resume, 1711 .resume = acm_resume,
1717 .reset_resume = acm_reset_resume, 1712 .reset_resume = acm_reset_resume,
1718 #endif 1713 #endif
1719 .id_table = acm_ids, 1714 .id_table = acm_ids,
1720 #ifdef CONFIG_PM 1715 #ifdef CONFIG_PM
1721 .supports_autosuspend = 1, 1716 .supports_autosuspend = 1,
1722 #endif 1717 #endif
1723 .disable_hub_initiated_lpm = 1, 1718 .disable_hub_initiated_lpm = 1,
1724 }; 1719 };
1725 1720
1726 /* 1721 /*
1727 * TTY driver structures. 1722 * TTY driver structures.
1728 */ 1723 */
1729 1724
1730 static const struct tty_operations acm_ops = { 1725 static const struct tty_operations acm_ops = {
1731 .install = acm_tty_install, 1726 .install = acm_tty_install,
1732 .open = acm_tty_open, 1727 .open = acm_tty_open,
1733 .close = acm_tty_close, 1728 .close = acm_tty_close,
1734 .cleanup = acm_tty_cleanup, 1729 .cleanup = acm_tty_cleanup,
1735 .hangup = acm_tty_hangup, 1730 .hangup = acm_tty_hangup,
1736 .write = acm_tty_write, 1731 .write = acm_tty_write,
1737 .write_room = acm_tty_write_room, 1732 .write_room = acm_tty_write_room,
1738 .ioctl = acm_tty_ioctl, 1733 .ioctl = acm_tty_ioctl,
1739 .throttle = acm_tty_throttle, 1734 .throttle = acm_tty_throttle,
1740 .unthrottle = acm_tty_unthrottle, 1735 .unthrottle = acm_tty_unthrottle,
1741 .chars_in_buffer = acm_tty_chars_in_buffer, 1736 .chars_in_buffer = acm_tty_chars_in_buffer,
1742 .break_ctl = acm_tty_break_ctl, 1737 .break_ctl = acm_tty_break_ctl,
1743 .set_termios = acm_tty_set_termios, 1738 .set_termios = acm_tty_set_termios,
1744 .tiocmget = acm_tty_tiocmget, 1739 .tiocmget = acm_tty_tiocmget,
1745 .tiocmset = acm_tty_tiocmset, 1740 .tiocmset = acm_tty_tiocmset,
1746 }; 1741 };
1747 1742
1748 /* 1743 /*
1749 * Init / exit. 1744 * Init / exit.
1750 */ 1745 */
1751 1746
1752 static int __init acm_init(void) 1747 static int __init acm_init(void)
1753 { 1748 {
1754 int retval; 1749 int retval;
1755 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS); 1750 acm_tty_driver = alloc_tty_driver(ACM_TTY_MINORS);
1756 if (!acm_tty_driver) 1751 if (!acm_tty_driver)
1757 return -ENOMEM; 1752 return -ENOMEM;
1758 acm_tty_driver->driver_name = "acm", 1753 acm_tty_driver->driver_name = "acm",
1759 acm_tty_driver->name = "ttyACM", 1754 acm_tty_driver->name = "ttyACM",
1760 acm_tty_driver->major = ACM_TTY_MAJOR, 1755 acm_tty_driver->major = ACM_TTY_MAJOR,
1761 acm_tty_driver->minor_start = 0, 1756 acm_tty_driver->minor_start = 0,
1762 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL, 1757 acm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
1763 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL, 1758 acm_tty_driver->subtype = SERIAL_TYPE_NORMAL,
1764 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 1759 acm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1765 acm_tty_driver->init_termios = tty_std_termios; 1760 acm_tty_driver->init_termios = tty_std_termios;
1766 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | 1761 acm_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD |
1767 HUPCL | CLOCAL; 1762 HUPCL | CLOCAL;
1768 tty_set_operations(acm_tty_driver, &acm_ops); 1763 tty_set_operations(acm_tty_driver, &acm_ops);
1769 1764
1770 retval = tty_register_driver(acm_tty_driver); 1765 retval = tty_register_driver(acm_tty_driver);
1771 if (retval) { 1766 if (retval) {
1772 put_tty_driver(acm_tty_driver); 1767 put_tty_driver(acm_tty_driver);
1773 return retval; 1768 return retval;
1774 } 1769 }
1775 1770
1776 retval = usb_register(&acm_driver); 1771 retval = usb_register(&acm_driver);
1777 if (retval) { 1772 if (retval) {
1778 tty_unregister_driver(acm_tty_driver); 1773 tty_unregister_driver(acm_tty_driver);
1779 put_tty_driver(acm_tty_driver); 1774 put_tty_driver(acm_tty_driver);
1780 return retval; 1775 return retval;
1781 } 1776 }
1782 1777
1783 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n"); 1778 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1784 1779
1785 return 0; 1780 return 0;
1786 } 1781 }
1787 1782
1788 static void __exit acm_exit(void) 1783 static void __exit acm_exit(void)
1789 { 1784 {
1790 usb_deregister(&acm_driver); 1785 usb_deregister(&acm_driver);
1791 tty_unregister_driver(acm_tty_driver); 1786 tty_unregister_driver(acm_tty_driver);
1792 put_tty_driver(acm_tty_driver); 1787 put_tty_driver(acm_tty_driver);
1793 } 1788 }
1794 1789
1795 module_init(acm_init); 1790 module_init(acm_init);
1796 module_exit(acm_exit); 1791 module_exit(acm_exit);
1797 1792
1798 MODULE_AUTHOR(DRIVER_AUTHOR); 1793 MODULE_AUTHOR(DRIVER_AUTHOR);
1799 MODULE_DESCRIPTION(DRIVER_DESC); 1794 MODULE_DESCRIPTION(DRIVER_DESC);
1800 MODULE_LICENSE("GPL"); 1795 MODULE_LICENSE("GPL");
1801 MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR); 1796 MODULE_ALIAS_CHARDEV_MAJOR(ACM_TTY_MAJOR);
1802 1797
drivers/usb/serial/digi_acceleport.c
1 /* 1 /*
2 * Digi AccelePort USB-4 and USB-2 Serial Converters 2 * Digi AccelePort USB-4 and USB-2 Serial Converters
3 * 3 *
4 * Copyright 2000 by Digi International 4 * Copyright 2000 by Digi International
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 * 10 *
11 * Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's 11 * Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's
12 * usb-serial driver. 12 * usb-serial driver.
13 * 13 *
14 * Peter Berger (pberger@brimson.com) 14 * Peter Berger (pberger@brimson.com)
15 * Al Borchers (borchers@steinerpoint.com) 15 * Al Borchers (borchers@steinerpoint.com)
16 */ 16 */
17 17
18 #include <linux/kernel.h> 18 #include <linux/kernel.h>
19 #include <linux/errno.h> 19 #include <linux/errno.h>
20 #include <linux/init.h> 20 #include <linux/init.h>
21 #include <linux/slab.h> 21 #include <linux/slab.h>
22 #include <linux/tty.h> 22 #include <linux/tty.h>
23 #include <linux/tty_driver.h> 23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h> 24 #include <linux/tty_flip.h>
25 #include <linux/module.h> 25 #include <linux/module.h>
26 #include <linux/spinlock.h> 26 #include <linux/spinlock.h>
27 #include <linux/workqueue.h> 27 #include <linux/workqueue.h>
28 #include <linux/uaccess.h> 28 #include <linux/uaccess.h>
29 #include <linux/usb.h> 29 #include <linux/usb.h>
30 #include <linux/wait.h> 30 #include <linux/wait.h>
31 #include <linux/usb/serial.h> 31 #include <linux/usb/serial.h>
32 32
33 /* Defines */ 33 /* Defines */
34 34
35 #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>" 35 #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
36 #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver" 36 #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
37 37
38 /* port output buffer length -- must be <= transfer buffer length - 2 */ 38 /* port output buffer length -- must be <= transfer buffer length - 2 */
39 /* so we can be sure to send the full buffer in one urb */ 39 /* so we can be sure to send the full buffer in one urb */
40 #define DIGI_OUT_BUF_SIZE 8 40 #define DIGI_OUT_BUF_SIZE 8
41 41
42 /* port input buffer length -- must be >= transfer buffer length - 3 */ 42 /* port input buffer length -- must be >= transfer buffer length - 3 */
43 /* so we can be sure to hold at least one full buffer from one urb */ 43 /* so we can be sure to hold at least one full buffer from one urb */
44 #define DIGI_IN_BUF_SIZE 64 44 #define DIGI_IN_BUF_SIZE 64
45 45
46 /* retry timeout while sleeping */ 46 /* retry timeout while sleeping */
47 #define DIGI_RETRY_TIMEOUT (HZ/10) 47 #define DIGI_RETRY_TIMEOUT (HZ/10)
48 48
49 /* timeout while waiting for tty output to drain in close */ 49 /* timeout while waiting for tty output to drain in close */
50 /* this delay is used twice in close, so the total delay could */ 50 /* this delay is used twice in close, so the total delay could */
51 /* be twice this value */ 51 /* be twice this value */
52 #define DIGI_CLOSE_TIMEOUT (5*HZ) 52 #define DIGI_CLOSE_TIMEOUT (5*HZ)
53 53
54 54
55 /* AccelePort USB Defines */ 55 /* AccelePort USB Defines */
56 56
57 /* ids */ 57 /* ids */
58 #define DIGI_VENDOR_ID 0x05c5 58 #define DIGI_VENDOR_ID 0x05c5
59 #define DIGI_2_ID 0x0002 /* USB-2 */ 59 #define DIGI_2_ID 0x0002 /* USB-2 */
60 #define DIGI_4_ID 0x0004 /* USB-4 */ 60 #define DIGI_4_ID 0x0004 /* USB-4 */
61 61
62 /* commands 62 /* commands
63 * "INB": can be used on the in-band endpoint 63 * "INB": can be used on the in-band endpoint
64 * "OOB": can be used on the out-of-band endpoint 64 * "OOB": can be used on the out-of-band endpoint
65 */ 65 */
66 #define DIGI_CMD_SET_BAUD_RATE 0 /* INB, OOB */ 66 #define DIGI_CMD_SET_BAUD_RATE 0 /* INB, OOB */
67 #define DIGI_CMD_SET_WORD_SIZE 1 /* INB, OOB */ 67 #define DIGI_CMD_SET_WORD_SIZE 1 /* INB, OOB */
68 #define DIGI_CMD_SET_PARITY 2 /* INB, OOB */ 68 #define DIGI_CMD_SET_PARITY 2 /* INB, OOB */
69 #define DIGI_CMD_SET_STOP_BITS 3 /* INB, OOB */ 69 #define DIGI_CMD_SET_STOP_BITS 3 /* INB, OOB */
70 #define DIGI_CMD_SET_INPUT_FLOW_CONTROL 4 /* INB, OOB */ 70 #define DIGI_CMD_SET_INPUT_FLOW_CONTROL 4 /* INB, OOB */
71 #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL 5 /* INB, OOB */ 71 #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL 5 /* INB, OOB */
72 #define DIGI_CMD_SET_DTR_SIGNAL 6 /* INB, OOB */ 72 #define DIGI_CMD_SET_DTR_SIGNAL 6 /* INB, OOB */
73 #define DIGI_CMD_SET_RTS_SIGNAL 7 /* INB, OOB */ 73 #define DIGI_CMD_SET_RTS_SIGNAL 7 /* INB, OOB */
74 #define DIGI_CMD_READ_INPUT_SIGNALS 8 /* OOB */ 74 #define DIGI_CMD_READ_INPUT_SIGNALS 8 /* OOB */
75 #define DIGI_CMD_IFLUSH_FIFO 9 /* OOB */ 75 #define DIGI_CMD_IFLUSH_FIFO 9 /* OOB */
76 #define DIGI_CMD_RECEIVE_ENABLE 10 /* INB, OOB */ 76 #define DIGI_CMD_RECEIVE_ENABLE 10 /* INB, OOB */
77 #define DIGI_CMD_BREAK_CONTROL 11 /* INB, OOB */ 77 #define DIGI_CMD_BREAK_CONTROL 11 /* INB, OOB */
78 #define DIGI_CMD_LOCAL_LOOPBACK 12 /* INB, OOB */ 78 #define DIGI_CMD_LOCAL_LOOPBACK 12 /* INB, OOB */
79 #define DIGI_CMD_TRANSMIT_IDLE 13 /* INB, OOB */ 79 #define DIGI_CMD_TRANSMIT_IDLE 13 /* INB, OOB */
80 #define DIGI_CMD_READ_UART_REGISTER 14 /* OOB */ 80 #define DIGI_CMD_READ_UART_REGISTER 14 /* OOB */
81 #define DIGI_CMD_WRITE_UART_REGISTER 15 /* INB, OOB */ 81 #define DIGI_CMD_WRITE_UART_REGISTER 15 /* INB, OOB */
82 #define DIGI_CMD_AND_UART_REGISTER 16 /* INB, OOB */ 82 #define DIGI_CMD_AND_UART_REGISTER 16 /* INB, OOB */
83 #define DIGI_CMD_OR_UART_REGISTER 17 /* INB, OOB */ 83 #define DIGI_CMD_OR_UART_REGISTER 17 /* INB, OOB */
84 #define DIGI_CMD_SEND_DATA 18 /* INB */ 84 #define DIGI_CMD_SEND_DATA 18 /* INB */
85 #define DIGI_CMD_RECEIVE_DATA 19 /* INB */ 85 #define DIGI_CMD_RECEIVE_DATA 19 /* INB */
86 #define DIGI_CMD_RECEIVE_DISABLE 20 /* INB */ 86 #define DIGI_CMD_RECEIVE_DISABLE 20 /* INB */
87 #define DIGI_CMD_GET_PORT_TYPE 21 /* OOB */ 87 #define DIGI_CMD_GET_PORT_TYPE 21 /* OOB */
88 88
89 /* baud rates */ 89 /* baud rates */
90 #define DIGI_BAUD_50 0 90 #define DIGI_BAUD_50 0
91 #define DIGI_BAUD_75 1 91 #define DIGI_BAUD_75 1
92 #define DIGI_BAUD_110 2 92 #define DIGI_BAUD_110 2
93 #define DIGI_BAUD_150 3 93 #define DIGI_BAUD_150 3
94 #define DIGI_BAUD_200 4 94 #define DIGI_BAUD_200 4
95 #define DIGI_BAUD_300 5 95 #define DIGI_BAUD_300 5
96 #define DIGI_BAUD_600 6 96 #define DIGI_BAUD_600 6
97 #define DIGI_BAUD_1200 7 97 #define DIGI_BAUD_1200 7
98 #define DIGI_BAUD_1800 8 98 #define DIGI_BAUD_1800 8
99 #define DIGI_BAUD_2400 9 99 #define DIGI_BAUD_2400 9
100 #define DIGI_BAUD_4800 10 100 #define DIGI_BAUD_4800 10
101 #define DIGI_BAUD_7200 11 101 #define DIGI_BAUD_7200 11
102 #define DIGI_BAUD_9600 12 102 #define DIGI_BAUD_9600 12
103 #define DIGI_BAUD_14400 13 103 #define DIGI_BAUD_14400 13
104 #define DIGI_BAUD_19200 14 104 #define DIGI_BAUD_19200 14
105 #define DIGI_BAUD_28800 15 105 #define DIGI_BAUD_28800 15
106 #define DIGI_BAUD_38400 16 106 #define DIGI_BAUD_38400 16
107 #define DIGI_BAUD_57600 17 107 #define DIGI_BAUD_57600 17
108 #define DIGI_BAUD_76800 18 108 #define DIGI_BAUD_76800 18
109 #define DIGI_BAUD_115200 19 109 #define DIGI_BAUD_115200 19
110 #define DIGI_BAUD_153600 20 110 #define DIGI_BAUD_153600 20
111 #define DIGI_BAUD_230400 21 111 #define DIGI_BAUD_230400 21
112 #define DIGI_BAUD_460800 22 112 #define DIGI_BAUD_460800 22
113 113
114 /* arguments */ 114 /* arguments */
115 #define DIGI_WORD_SIZE_5 0 115 #define DIGI_WORD_SIZE_5 0
116 #define DIGI_WORD_SIZE_6 1 116 #define DIGI_WORD_SIZE_6 1
117 #define DIGI_WORD_SIZE_7 2 117 #define DIGI_WORD_SIZE_7 2
118 #define DIGI_WORD_SIZE_8 3 118 #define DIGI_WORD_SIZE_8 3
119 119
120 #define DIGI_PARITY_NONE 0 120 #define DIGI_PARITY_NONE 0
121 #define DIGI_PARITY_ODD 1 121 #define DIGI_PARITY_ODD 1
122 #define DIGI_PARITY_EVEN 2 122 #define DIGI_PARITY_EVEN 2
123 #define DIGI_PARITY_MARK 3 123 #define DIGI_PARITY_MARK 3
124 #define DIGI_PARITY_SPACE 4 124 #define DIGI_PARITY_SPACE 4
125 125
126 #define DIGI_STOP_BITS_1 0 126 #define DIGI_STOP_BITS_1 0
127 #define DIGI_STOP_BITS_2 1 127 #define DIGI_STOP_BITS_2 1
128 128
129 #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF 1 129 #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF 1
130 #define DIGI_INPUT_FLOW_CONTROL_RTS 2 130 #define DIGI_INPUT_FLOW_CONTROL_RTS 2
131 #define DIGI_INPUT_FLOW_CONTROL_DTR 4 131 #define DIGI_INPUT_FLOW_CONTROL_DTR 4
132 132
133 #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF 1 133 #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF 1
134 #define DIGI_OUTPUT_FLOW_CONTROL_CTS 2 134 #define DIGI_OUTPUT_FLOW_CONTROL_CTS 2
135 #define DIGI_OUTPUT_FLOW_CONTROL_DSR 4 135 #define DIGI_OUTPUT_FLOW_CONTROL_DSR 4
136 136
137 #define DIGI_DTR_INACTIVE 0 137 #define DIGI_DTR_INACTIVE 0
138 #define DIGI_DTR_ACTIVE 1 138 #define DIGI_DTR_ACTIVE 1
139 #define DIGI_DTR_INPUT_FLOW_CONTROL 2 139 #define DIGI_DTR_INPUT_FLOW_CONTROL 2
140 140
141 #define DIGI_RTS_INACTIVE 0 141 #define DIGI_RTS_INACTIVE 0
142 #define DIGI_RTS_ACTIVE 1 142 #define DIGI_RTS_ACTIVE 1
143 #define DIGI_RTS_INPUT_FLOW_CONTROL 2 143 #define DIGI_RTS_INPUT_FLOW_CONTROL 2
144 #define DIGI_RTS_TOGGLE 3 144 #define DIGI_RTS_TOGGLE 3
145 145
146 #define DIGI_FLUSH_TX 1 146 #define DIGI_FLUSH_TX 1
147 #define DIGI_FLUSH_RX 2 147 #define DIGI_FLUSH_RX 2
148 #define DIGI_RESUME_TX 4 /* clears xoff condition */ 148 #define DIGI_RESUME_TX 4 /* clears xoff condition */
149 149
150 #define DIGI_TRANSMIT_NOT_IDLE 0 150 #define DIGI_TRANSMIT_NOT_IDLE 0
151 #define DIGI_TRANSMIT_IDLE 1 151 #define DIGI_TRANSMIT_IDLE 1
152 152
153 #define DIGI_DISABLE 0 153 #define DIGI_DISABLE 0
154 #define DIGI_ENABLE 1 154 #define DIGI_ENABLE 1
155 155
156 #define DIGI_DEASSERT 0 156 #define DIGI_DEASSERT 0
157 #define DIGI_ASSERT 1 157 #define DIGI_ASSERT 1
158 158
159 /* in band status codes */ 159 /* in band status codes */
160 #define DIGI_OVERRUN_ERROR 4 160 #define DIGI_OVERRUN_ERROR 4
161 #define DIGI_PARITY_ERROR 8 161 #define DIGI_PARITY_ERROR 8
162 #define DIGI_FRAMING_ERROR 16 162 #define DIGI_FRAMING_ERROR 16
163 #define DIGI_BREAK_ERROR 32 163 #define DIGI_BREAK_ERROR 32
164 164
165 /* out of band status */ 165 /* out of band status */
166 #define DIGI_NO_ERROR 0 166 #define DIGI_NO_ERROR 0
167 #define DIGI_BAD_FIRST_PARAMETER 1 167 #define DIGI_BAD_FIRST_PARAMETER 1
168 #define DIGI_BAD_SECOND_PARAMETER 2 168 #define DIGI_BAD_SECOND_PARAMETER 2
169 #define DIGI_INVALID_LINE 3 169 #define DIGI_INVALID_LINE 3
170 #define DIGI_INVALID_OPCODE 4 170 #define DIGI_INVALID_OPCODE 4
171 171
172 /* input signals */ 172 /* input signals */
173 #define DIGI_READ_INPUT_SIGNALS_SLOT 1 173 #define DIGI_READ_INPUT_SIGNALS_SLOT 1
174 #define DIGI_READ_INPUT_SIGNALS_ERR 2 174 #define DIGI_READ_INPUT_SIGNALS_ERR 2
175 #define DIGI_READ_INPUT_SIGNALS_BUSY 4 175 #define DIGI_READ_INPUT_SIGNALS_BUSY 4
176 #define DIGI_READ_INPUT_SIGNALS_PE 8 176 #define DIGI_READ_INPUT_SIGNALS_PE 8
177 #define DIGI_READ_INPUT_SIGNALS_CTS 16 177 #define DIGI_READ_INPUT_SIGNALS_CTS 16
178 #define DIGI_READ_INPUT_SIGNALS_DSR 32 178 #define DIGI_READ_INPUT_SIGNALS_DSR 32
179 #define DIGI_READ_INPUT_SIGNALS_RI 64 179 #define DIGI_READ_INPUT_SIGNALS_RI 64
180 #define DIGI_READ_INPUT_SIGNALS_DCD 128 180 #define DIGI_READ_INPUT_SIGNALS_DCD 128
181 181
182 182
183 /* Structures */ 183 /* Structures */
184 184
185 struct digi_serial { 185 struct digi_serial {
186 spinlock_t ds_serial_lock; 186 spinlock_t ds_serial_lock;
187 struct usb_serial_port *ds_oob_port; /* out-of-band port */ 187 struct usb_serial_port *ds_oob_port; /* out-of-band port */
188 int ds_oob_port_num; /* index of out-of-band port */ 188 int ds_oob_port_num; /* index of out-of-band port */
189 int ds_device_started; 189 int ds_device_started;
190 }; 190 };
191 191
192 struct digi_port { 192 struct digi_port {
193 spinlock_t dp_port_lock; 193 spinlock_t dp_port_lock;
194 int dp_port_num; 194 int dp_port_num;
195 int dp_out_buf_len; 195 int dp_out_buf_len;
196 unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE]; 196 unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
197 int dp_write_urb_in_use; 197 int dp_write_urb_in_use;
198 unsigned int dp_modem_signals; 198 unsigned int dp_modem_signals;
199 wait_queue_head_t dp_modem_change_wait; 199 wait_queue_head_t dp_modem_change_wait;
200 int dp_transmit_idle; 200 int dp_transmit_idle;
201 wait_queue_head_t dp_transmit_idle_wait; 201 wait_queue_head_t dp_transmit_idle_wait;
202 int dp_throttled; 202 int dp_throttled;
203 int dp_throttle_restart; 203 int dp_throttle_restart;
204 wait_queue_head_t dp_flush_wait; 204 wait_queue_head_t dp_flush_wait;
205 wait_queue_head_t dp_close_wait; /* wait queue for close */ 205 wait_queue_head_t dp_close_wait; /* wait queue for close */
206 struct work_struct dp_wakeup_work; 206 struct work_struct dp_wakeup_work;
207 struct usb_serial_port *dp_port; 207 struct usb_serial_port *dp_port;
208 }; 208 };
209 209
210 210
211 /* Local Function Declarations */ 211 /* Local Function Declarations */
212 212
213 static void digi_wakeup_write(struct usb_serial_port *port);
214 static void digi_wakeup_write_lock(struct work_struct *work); 213 static void digi_wakeup_write_lock(struct work_struct *work);
215 static int digi_write_oob_command(struct usb_serial_port *port, 214 static int digi_write_oob_command(struct usb_serial_port *port,
216 unsigned char *buf, int count, int interruptible); 215 unsigned char *buf, int count, int interruptible);
217 static int digi_write_inb_command(struct usb_serial_port *port, 216 static int digi_write_inb_command(struct usb_serial_port *port,
218 unsigned char *buf, int count, unsigned long timeout); 217 unsigned char *buf, int count, unsigned long timeout);
219 static int digi_set_modem_signals(struct usb_serial_port *port, 218 static int digi_set_modem_signals(struct usb_serial_port *port,
220 unsigned int modem_signals, int interruptible); 219 unsigned int modem_signals, int interruptible);
221 static int digi_transmit_idle(struct usb_serial_port *port, 220 static int digi_transmit_idle(struct usb_serial_port *port,
222 unsigned long timeout); 221 unsigned long timeout);
223 static void digi_rx_throttle(struct tty_struct *tty); 222 static void digi_rx_throttle(struct tty_struct *tty);
224 static void digi_rx_unthrottle(struct tty_struct *tty); 223 static void digi_rx_unthrottle(struct tty_struct *tty);
225 static void digi_set_termios(struct tty_struct *tty, 224 static void digi_set_termios(struct tty_struct *tty,
226 struct usb_serial_port *port, struct ktermios *old_termios); 225 struct usb_serial_port *port, struct ktermios *old_termios);
227 static void digi_break_ctl(struct tty_struct *tty, int break_state); 226 static void digi_break_ctl(struct tty_struct *tty, int break_state);
228 static int digi_tiocmget(struct tty_struct *tty); 227 static int digi_tiocmget(struct tty_struct *tty);
229 static int digi_tiocmset(struct tty_struct *tty, unsigned int set, 228 static int digi_tiocmset(struct tty_struct *tty, unsigned int set,
230 unsigned int clear); 229 unsigned int clear);
231 static int digi_write(struct tty_struct *tty, struct usb_serial_port *port, 230 static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
232 const unsigned char *buf, int count); 231 const unsigned char *buf, int count);
233 static void digi_write_bulk_callback(struct urb *urb); 232 static void digi_write_bulk_callback(struct urb *urb);
234 static int digi_write_room(struct tty_struct *tty); 233 static int digi_write_room(struct tty_struct *tty);
235 static int digi_chars_in_buffer(struct tty_struct *tty); 234 static int digi_chars_in_buffer(struct tty_struct *tty);
236 static int digi_open(struct tty_struct *tty, struct usb_serial_port *port); 235 static int digi_open(struct tty_struct *tty, struct usb_serial_port *port);
237 static void digi_close(struct usb_serial_port *port); 236 static void digi_close(struct usb_serial_port *port);
238 static void digi_dtr_rts(struct usb_serial_port *port, int on); 237 static void digi_dtr_rts(struct usb_serial_port *port, int on);
239 static int digi_startup_device(struct usb_serial *serial); 238 static int digi_startup_device(struct usb_serial *serial);
240 static int digi_startup(struct usb_serial *serial); 239 static int digi_startup(struct usb_serial *serial);
241 static void digi_disconnect(struct usb_serial *serial); 240 static void digi_disconnect(struct usb_serial *serial);
242 static void digi_release(struct usb_serial *serial); 241 static void digi_release(struct usb_serial *serial);
243 static int digi_port_probe(struct usb_serial_port *port); 242 static int digi_port_probe(struct usb_serial_port *port);
244 static int digi_port_remove(struct usb_serial_port *port); 243 static int digi_port_remove(struct usb_serial_port *port);
245 static void digi_read_bulk_callback(struct urb *urb); 244 static void digi_read_bulk_callback(struct urb *urb);
246 static int digi_read_inb_callback(struct urb *urb); 245 static int digi_read_inb_callback(struct urb *urb);
247 static int digi_read_oob_callback(struct urb *urb); 246 static int digi_read_oob_callback(struct urb *urb);
248 247
249 248
250 static const struct usb_device_id id_table_combined[] = { 249 static const struct usb_device_id id_table_combined[] = {
251 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) }, 250 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
252 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) }, 251 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
253 { } /* Terminating entry */ 252 { } /* Terminating entry */
254 }; 253 };
255 254
256 static const struct usb_device_id id_table_2[] = { 255 static const struct usb_device_id id_table_2[] = {
257 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) }, 256 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
258 { } /* Terminating entry */ 257 { } /* Terminating entry */
259 }; 258 };
260 259
261 static const struct usb_device_id id_table_4[] = { 260 static const struct usb_device_id id_table_4[] = {
262 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) }, 261 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
263 { } /* Terminating entry */ 262 { } /* Terminating entry */
264 }; 263 };
265 264
266 MODULE_DEVICE_TABLE(usb, id_table_combined); 265 MODULE_DEVICE_TABLE(usb, id_table_combined);
267 266
268 /* device info needed for the Digi serial converter */ 267 /* device info needed for the Digi serial converter */
269 268
270 static struct usb_serial_driver digi_acceleport_2_device = { 269 static struct usb_serial_driver digi_acceleport_2_device = {
271 .driver = { 270 .driver = {
272 .owner = THIS_MODULE, 271 .owner = THIS_MODULE,
273 .name = "digi_2", 272 .name = "digi_2",
274 }, 273 },
275 .description = "Digi 2 port USB adapter", 274 .description = "Digi 2 port USB adapter",
276 .id_table = id_table_2, 275 .id_table = id_table_2,
277 .num_ports = 3, 276 .num_ports = 3,
278 .open = digi_open, 277 .open = digi_open,
279 .close = digi_close, 278 .close = digi_close,
280 .dtr_rts = digi_dtr_rts, 279 .dtr_rts = digi_dtr_rts,
281 .write = digi_write, 280 .write = digi_write,
282 .write_room = digi_write_room, 281 .write_room = digi_write_room,
283 .write_bulk_callback = digi_write_bulk_callback, 282 .write_bulk_callback = digi_write_bulk_callback,
284 .read_bulk_callback = digi_read_bulk_callback, 283 .read_bulk_callback = digi_read_bulk_callback,
285 .chars_in_buffer = digi_chars_in_buffer, 284 .chars_in_buffer = digi_chars_in_buffer,
286 .throttle = digi_rx_throttle, 285 .throttle = digi_rx_throttle,
287 .unthrottle = digi_rx_unthrottle, 286 .unthrottle = digi_rx_unthrottle,
288 .set_termios = digi_set_termios, 287 .set_termios = digi_set_termios,
289 .break_ctl = digi_break_ctl, 288 .break_ctl = digi_break_ctl,
290 .tiocmget = digi_tiocmget, 289 .tiocmget = digi_tiocmget,
291 .tiocmset = digi_tiocmset, 290 .tiocmset = digi_tiocmset,
292 .attach = digi_startup, 291 .attach = digi_startup,
293 .disconnect = digi_disconnect, 292 .disconnect = digi_disconnect,
294 .release = digi_release, 293 .release = digi_release,
295 .port_probe = digi_port_probe, 294 .port_probe = digi_port_probe,
296 .port_remove = digi_port_remove, 295 .port_remove = digi_port_remove,
297 }; 296 };
298 297
299 static struct usb_serial_driver digi_acceleport_4_device = { 298 static struct usb_serial_driver digi_acceleport_4_device = {
300 .driver = { 299 .driver = {
301 .owner = THIS_MODULE, 300 .owner = THIS_MODULE,
302 .name = "digi_4", 301 .name = "digi_4",
303 }, 302 },
304 .description = "Digi 4 port USB adapter", 303 .description = "Digi 4 port USB adapter",
305 .id_table = id_table_4, 304 .id_table = id_table_4,
306 .num_ports = 4, 305 .num_ports = 4,
307 .open = digi_open, 306 .open = digi_open,
308 .close = digi_close, 307 .close = digi_close,
309 .write = digi_write, 308 .write = digi_write,
310 .write_room = digi_write_room, 309 .write_room = digi_write_room,
311 .write_bulk_callback = digi_write_bulk_callback, 310 .write_bulk_callback = digi_write_bulk_callback,
312 .read_bulk_callback = digi_read_bulk_callback, 311 .read_bulk_callback = digi_read_bulk_callback,
313 .chars_in_buffer = digi_chars_in_buffer, 312 .chars_in_buffer = digi_chars_in_buffer,
314 .throttle = digi_rx_throttle, 313 .throttle = digi_rx_throttle,
315 .unthrottle = digi_rx_unthrottle, 314 .unthrottle = digi_rx_unthrottle,
316 .set_termios = digi_set_termios, 315 .set_termios = digi_set_termios,
317 .break_ctl = digi_break_ctl, 316 .break_ctl = digi_break_ctl,
318 .tiocmget = digi_tiocmget, 317 .tiocmget = digi_tiocmget,
319 .tiocmset = digi_tiocmset, 318 .tiocmset = digi_tiocmset,
320 .attach = digi_startup, 319 .attach = digi_startup,
321 .disconnect = digi_disconnect, 320 .disconnect = digi_disconnect,
322 .release = digi_release, 321 .release = digi_release,
323 .port_probe = digi_port_probe, 322 .port_probe = digi_port_probe,
324 .port_remove = digi_port_remove, 323 .port_remove = digi_port_remove,
325 }; 324 };
326 325
327 static struct usb_serial_driver * const serial_drivers[] = { 326 static struct usb_serial_driver * const serial_drivers[] = {
328 &digi_acceleport_2_device, &digi_acceleport_4_device, NULL 327 &digi_acceleport_2_device, &digi_acceleport_4_device, NULL
329 }; 328 };
330 329
331 /* Functions */ 330 /* Functions */
332 331
333 /* 332 /*
334 * Cond Wait Interruptible Timeout Irqrestore 333 * Cond Wait Interruptible Timeout Irqrestore
335 * 334 *
336 * Do spin_unlock_irqrestore and interruptible_sleep_on_timeout 335 * Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
337 * so that wake ups are not lost if they occur between the unlock 336 * so that wake ups are not lost if they occur between the unlock
338 * and the sleep. In other words, spin_unlock_irqrestore and 337 * and the sleep. In other words, spin_unlock_irqrestore and
339 * interruptible_sleep_on_timeout are "atomic" with respect to 338 * interruptible_sleep_on_timeout are "atomic" with respect to
340 * wake ups. This is used to implement condition variables. 339 * wake ups. This is used to implement condition variables.
341 * 340 *
342 * interruptible_sleep_on_timeout is deprecated and has been replaced 341 * interruptible_sleep_on_timeout is deprecated and has been replaced
343 * with the equivalent code. 342 * with the equivalent code.
344 */ 343 */
345 344
346 static long cond_wait_interruptible_timeout_irqrestore( 345 static long cond_wait_interruptible_timeout_irqrestore(
347 wait_queue_head_t *q, long timeout, 346 wait_queue_head_t *q, long timeout,
348 spinlock_t *lock, unsigned long flags) 347 spinlock_t *lock, unsigned long flags)
349 __releases(lock) 348 __releases(lock)
350 { 349 {
351 DEFINE_WAIT(wait); 350 DEFINE_WAIT(wait);
352 351
353 prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE); 352 prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
354 spin_unlock_irqrestore(lock, flags); 353 spin_unlock_irqrestore(lock, flags);
355 timeout = schedule_timeout(timeout); 354 timeout = schedule_timeout(timeout);
356 finish_wait(q, &wait); 355 finish_wait(q, &wait);
357 356
358 return timeout; 357 return timeout;
359 } 358 }
360 359
361 360
362 /* 361 /*
363 * Digi Wakeup Write 362 * Digi Wakeup Write
364 * 363 *
365 * Wake up port, line discipline, and tty processes sleeping 364 * Wake up port, line discipline, and tty processes sleeping
366 * on writes. 365 * on writes.
367 */ 366 */
368 367
369 static void digi_wakeup_write_lock(struct work_struct *work) 368 static void digi_wakeup_write_lock(struct work_struct *work)
370 { 369 {
371 struct digi_port *priv = 370 struct digi_port *priv =
372 container_of(work, struct digi_port, dp_wakeup_work); 371 container_of(work, struct digi_port, dp_wakeup_work);
373 struct usb_serial_port *port = priv->dp_port; 372 struct usb_serial_port *port = priv->dp_port;
374 unsigned long flags; 373 unsigned long flags;
375 374
376 spin_lock_irqsave(&priv->dp_port_lock, flags); 375 spin_lock_irqsave(&priv->dp_port_lock, flags);
377 digi_wakeup_write(port); 376 tty_port_tty_wakeup(&port->port);
378 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 377 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
379 } 378 }
380 379
381 static void digi_wakeup_write(struct usb_serial_port *port)
382 {
383 struct tty_struct *tty = tty_port_tty_get(&port->port);
384 if (tty) {
385 tty_wakeup(tty);
386 tty_kref_put(tty);
387 }
388 }
389
390
391 /* 380 /*
392 * Digi Write OOB Command 381 * Digi Write OOB Command
393 * 382 *
394 * Write commands on the out of band port. Commands are 4 383 * Write commands on the out of band port. Commands are 4
395 * bytes each, multiple commands can be sent at once, and 384 * bytes each, multiple commands can be sent at once, and
396 * no command will be split across USB packets. Returns 0 385 * no command will be split across USB packets. Returns 0
397 * if successful, -EINTR if interrupted while sleeping and 386 * if successful, -EINTR if interrupted while sleeping and
398 * the interruptible flag is true, or a negative error 387 * the interruptible flag is true, or a negative error
399 * returned by usb_submit_urb. 388 * returned by usb_submit_urb.
400 */ 389 */
401 390
402 static int digi_write_oob_command(struct usb_serial_port *port, 391 static int digi_write_oob_command(struct usb_serial_port *port,
403 unsigned char *buf, int count, int interruptible) 392 unsigned char *buf, int count, int interruptible)
404 { 393 {
405 int ret = 0; 394 int ret = 0;
406 int len; 395 int len;
407 struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port; 396 struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
408 struct digi_port *oob_priv = usb_get_serial_port_data(oob_port); 397 struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
409 unsigned long flags = 0; 398 unsigned long flags = 0;
410 399
411 dev_dbg(&port->dev, 400 dev_dbg(&port->dev,
412 "digi_write_oob_command: TOP: port=%d, count=%d\n", 401 "digi_write_oob_command: TOP: port=%d, count=%d\n",
413 oob_priv->dp_port_num, count); 402 oob_priv->dp_port_num, count);
414 403
415 spin_lock_irqsave(&oob_priv->dp_port_lock, flags); 404 spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
416 while (count > 0) { 405 while (count > 0) {
417 while (oob_priv->dp_write_urb_in_use) { 406 while (oob_priv->dp_write_urb_in_use) {
418 cond_wait_interruptible_timeout_irqrestore( 407 cond_wait_interruptible_timeout_irqrestore(
419 &oob_port->write_wait, DIGI_RETRY_TIMEOUT, 408 &oob_port->write_wait, DIGI_RETRY_TIMEOUT,
420 &oob_priv->dp_port_lock, flags); 409 &oob_priv->dp_port_lock, flags);
421 if (interruptible && signal_pending(current)) 410 if (interruptible && signal_pending(current))
422 return -EINTR; 411 return -EINTR;
423 spin_lock_irqsave(&oob_priv->dp_port_lock, flags); 412 spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
424 } 413 }
425 414
426 /* len must be a multiple of 4, so commands are not split */ 415 /* len must be a multiple of 4, so commands are not split */
427 len = min(count, oob_port->bulk_out_size); 416 len = min(count, oob_port->bulk_out_size);
428 if (len > 4) 417 if (len > 4)
429 len &= ~3; 418 len &= ~3;
430 memcpy(oob_port->write_urb->transfer_buffer, buf, len); 419 memcpy(oob_port->write_urb->transfer_buffer, buf, len);
431 oob_port->write_urb->transfer_buffer_length = len; 420 oob_port->write_urb->transfer_buffer_length = len;
432 ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC); 421 ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
433 if (ret == 0) { 422 if (ret == 0) {
434 oob_priv->dp_write_urb_in_use = 1; 423 oob_priv->dp_write_urb_in_use = 1;
435 count -= len; 424 count -= len;
436 buf += len; 425 buf += len;
437 } 426 }
438 } 427 }
439 spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags); 428 spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
440 if (ret) 429 if (ret)
441 dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n", 430 dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
442 __func__, ret); 431 __func__, ret);
443 return ret; 432 return ret;
444 433
445 } 434 }
446 435
447 436
448 /* 437 /*
449 * Digi Write In Band Command 438 * Digi Write In Band Command
450 * 439 *
451 * Write commands on the given port. Commands are 4 440 * Write commands on the given port. Commands are 4
452 * bytes each, multiple commands can be sent at once, and 441 * bytes each, multiple commands can be sent at once, and
453 * no command will be split across USB packets. If timeout 442 * no command will be split across USB packets. If timeout
454 * is non-zero, write in band command will return after 443 * is non-zero, write in band command will return after
455 * waiting unsuccessfully for the URB status to clear for 444 * waiting unsuccessfully for the URB status to clear for
456 * timeout ticks. Returns 0 if successful, or a negative 445 * timeout ticks. Returns 0 if successful, or a negative
457 * error returned by digi_write. 446 * error returned by digi_write.
458 */ 447 */
459 448
460 static int digi_write_inb_command(struct usb_serial_port *port, 449 static int digi_write_inb_command(struct usb_serial_port *port,
461 unsigned char *buf, int count, unsigned long timeout) 450 unsigned char *buf, int count, unsigned long timeout)
462 { 451 {
463 int ret = 0; 452 int ret = 0;
464 int len; 453 int len;
465 struct digi_port *priv = usb_get_serial_port_data(port); 454 struct digi_port *priv = usb_get_serial_port_data(port);
466 unsigned char *data = port->write_urb->transfer_buffer; 455 unsigned char *data = port->write_urb->transfer_buffer;
467 unsigned long flags = 0; 456 unsigned long flags = 0;
468 457
469 dev_dbg(&port->dev, "digi_write_inb_command: TOP: port=%d, count=%d\n", 458 dev_dbg(&port->dev, "digi_write_inb_command: TOP: port=%d, count=%d\n",
470 priv->dp_port_num, count); 459 priv->dp_port_num, count);
471 460
472 if (timeout) 461 if (timeout)
473 timeout += jiffies; 462 timeout += jiffies;
474 else 463 else
475 timeout = ULONG_MAX; 464 timeout = ULONG_MAX;
476 465
477 spin_lock_irqsave(&priv->dp_port_lock, flags); 466 spin_lock_irqsave(&priv->dp_port_lock, flags);
478 while (count > 0 && ret == 0) { 467 while (count > 0 && ret == 0) {
479 while (priv->dp_write_urb_in_use && 468 while (priv->dp_write_urb_in_use &&
480 time_before(jiffies, timeout)) { 469 time_before(jiffies, timeout)) {
481 cond_wait_interruptible_timeout_irqrestore( 470 cond_wait_interruptible_timeout_irqrestore(
482 &port->write_wait, DIGI_RETRY_TIMEOUT, 471 &port->write_wait, DIGI_RETRY_TIMEOUT,
483 &priv->dp_port_lock, flags); 472 &priv->dp_port_lock, flags);
484 if (signal_pending(current)) 473 if (signal_pending(current))
485 return -EINTR; 474 return -EINTR;
486 spin_lock_irqsave(&priv->dp_port_lock, flags); 475 spin_lock_irqsave(&priv->dp_port_lock, flags);
487 } 476 }
488 477
489 /* len must be a multiple of 4 and small enough to */ 478 /* len must be a multiple of 4 and small enough to */
490 /* guarantee the write will send buffered data first, */ 479 /* guarantee the write will send buffered data first, */
491 /* so commands are in order with data and not split */ 480 /* so commands are in order with data and not split */
492 len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len); 481 len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
493 if (len > 4) 482 if (len > 4)
494 len &= ~3; 483 len &= ~3;
495 484
496 /* write any buffered data first */ 485 /* write any buffered data first */
497 if (priv->dp_out_buf_len > 0) { 486 if (priv->dp_out_buf_len > 0) {
498 data[0] = DIGI_CMD_SEND_DATA; 487 data[0] = DIGI_CMD_SEND_DATA;
499 data[1] = priv->dp_out_buf_len; 488 data[1] = priv->dp_out_buf_len;
500 memcpy(data + 2, priv->dp_out_buf, 489 memcpy(data + 2, priv->dp_out_buf,
501 priv->dp_out_buf_len); 490 priv->dp_out_buf_len);
502 memcpy(data + 2 + priv->dp_out_buf_len, buf, len); 491 memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
503 port->write_urb->transfer_buffer_length 492 port->write_urb->transfer_buffer_length
504 = priv->dp_out_buf_len + 2 + len; 493 = priv->dp_out_buf_len + 2 + len;
505 } else { 494 } else {
506 memcpy(data, buf, len); 495 memcpy(data, buf, len);
507 port->write_urb->transfer_buffer_length = len; 496 port->write_urb->transfer_buffer_length = len;
508 } 497 }
509 498
510 ret = usb_submit_urb(port->write_urb, GFP_ATOMIC); 499 ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
511 if (ret == 0) { 500 if (ret == 0) {
512 priv->dp_write_urb_in_use = 1; 501 priv->dp_write_urb_in_use = 1;
513 priv->dp_out_buf_len = 0; 502 priv->dp_out_buf_len = 0;
514 count -= len; 503 count -= len;
515 buf += len; 504 buf += len;
516 } 505 }
517 506
518 } 507 }
519 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 508 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
520 509
521 if (ret) 510 if (ret)
522 dev_err(&port->dev, 511 dev_err(&port->dev,
523 "%s: usb_submit_urb failed, ret=%d, port=%d\n", 512 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
524 __func__, ret, priv->dp_port_num); 513 __func__, ret, priv->dp_port_num);
525 return ret; 514 return ret;
526 } 515 }
527 516
528 517
529 /* 518 /*
530 * Digi Set Modem Signals 519 * Digi Set Modem Signals
531 * 520 *
532 * Sets or clears DTR and RTS on the port, according to the 521 * Sets or clears DTR and RTS on the port, according to the
533 * modem_signals argument. Use TIOCM_DTR and TIOCM_RTS flags 522 * modem_signals argument. Use TIOCM_DTR and TIOCM_RTS flags
534 * for the modem_signals argument. Returns 0 if successful, 523 * for the modem_signals argument. Returns 0 if successful,
535 * -EINTR if interrupted while sleeping, or a non-zero error 524 * -EINTR if interrupted while sleeping, or a non-zero error
536 * returned by usb_submit_urb. 525 * returned by usb_submit_urb.
537 */ 526 */
538 527
539 static int digi_set_modem_signals(struct usb_serial_port *port, 528 static int digi_set_modem_signals(struct usb_serial_port *port,
540 unsigned int modem_signals, int interruptible) 529 unsigned int modem_signals, int interruptible)
541 { 530 {
542 531
543 int ret; 532 int ret;
544 struct digi_port *port_priv = usb_get_serial_port_data(port); 533 struct digi_port *port_priv = usb_get_serial_port_data(port);
545 struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port; 534 struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
546 struct digi_port *oob_priv = usb_get_serial_port_data(oob_port); 535 struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
547 unsigned char *data = oob_port->write_urb->transfer_buffer; 536 unsigned char *data = oob_port->write_urb->transfer_buffer;
548 unsigned long flags = 0; 537 unsigned long flags = 0;
549 538
550 539
551 dev_dbg(&port->dev, 540 dev_dbg(&port->dev,
552 "digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n", 541 "digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n",
553 port_priv->dp_port_num, modem_signals); 542 port_priv->dp_port_num, modem_signals);
554 543
555 spin_lock_irqsave(&oob_priv->dp_port_lock, flags); 544 spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
556 spin_lock(&port_priv->dp_port_lock); 545 spin_lock(&port_priv->dp_port_lock);
557 546
558 while (oob_priv->dp_write_urb_in_use) { 547 while (oob_priv->dp_write_urb_in_use) {
559 spin_unlock(&port_priv->dp_port_lock); 548 spin_unlock(&port_priv->dp_port_lock);
560 cond_wait_interruptible_timeout_irqrestore( 549 cond_wait_interruptible_timeout_irqrestore(
561 &oob_port->write_wait, DIGI_RETRY_TIMEOUT, 550 &oob_port->write_wait, DIGI_RETRY_TIMEOUT,
562 &oob_priv->dp_port_lock, flags); 551 &oob_priv->dp_port_lock, flags);
563 if (interruptible && signal_pending(current)) 552 if (interruptible && signal_pending(current))
564 return -EINTR; 553 return -EINTR;
565 spin_lock_irqsave(&oob_priv->dp_port_lock, flags); 554 spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
566 spin_lock(&port_priv->dp_port_lock); 555 spin_lock(&port_priv->dp_port_lock);
567 } 556 }
568 data[0] = DIGI_CMD_SET_DTR_SIGNAL; 557 data[0] = DIGI_CMD_SET_DTR_SIGNAL;
569 data[1] = port_priv->dp_port_num; 558 data[1] = port_priv->dp_port_num;
570 data[2] = (modem_signals & TIOCM_DTR) ? 559 data[2] = (modem_signals & TIOCM_DTR) ?
571 DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE; 560 DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
572 data[3] = 0; 561 data[3] = 0;
573 data[4] = DIGI_CMD_SET_RTS_SIGNAL; 562 data[4] = DIGI_CMD_SET_RTS_SIGNAL;
574 data[5] = port_priv->dp_port_num; 563 data[5] = port_priv->dp_port_num;
575 data[6] = (modem_signals & TIOCM_RTS) ? 564 data[6] = (modem_signals & TIOCM_RTS) ?
576 DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE; 565 DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
577 data[7] = 0; 566 data[7] = 0;
578 567
579 oob_port->write_urb->transfer_buffer_length = 8; 568 oob_port->write_urb->transfer_buffer_length = 8;
580 569
581 ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC); 570 ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
582 if (ret == 0) { 571 if (ret == 0) {
583 oob_priv->dp_write_urb_in_use = 1; 572 oob_priv->dp_write_urb_in_use = 1;
584 port_priv->dp_modem_signals = 573 port_priv->dp_modem_signals =
585 (port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS)) 574 (port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS))
586 | (modem_signals&(TIOCM_DTR|TIOCM_RTS)); 575 | (modem_signals&(TIOCM_DTR|TIOCM_RTS));
587 } 576 }
588 spin_unlock(&port_priv->dp_port_lock); 577 spin_unlock(&port_priv->dp_port_lock);
589 spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags); 578 spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
590 if (ret) 579 if (ret)
591 dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n", 580 dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
592 __func__, ret); 581 __func__, ret);
593 return ret; 582 return ret;
594 } 583 }
595 584
596 /* 585 /*
597 * Digi Transmit Idle 586 * Digi Transmit Idle
598 * 587 *
599 * Digi transmit idle waits, up to timeout ticks, for the transmitter 588 * Digi transmit idle waits, up to timeout ticks, for the transmitter
600 * to go idle. It returns 0 if successful or a negative error. 589 * to go idle. It returns 0 if successful or a negative error.
601 * 590 *
602 * There are race conditions here if more than one process is calling 591 * There are race conditions here if more than one process is calling
603 * digi_transmit_idle on the same port at the same time. However, this 592 * digi_transmit_idle on the same port at the same time. However, this
604 * is only called from close, and only one process can be in close on a 593 * is only called from close, and only one process can be in close on a
605 * port at a time, so its ok. 594 * port at a time, so its ok.
606 */ 595 */
607 596
608 static int digi_transmit_idle(struct usb_serial_port *port, 597 static int digi_transmit_idle(struct usb_serial_port *port,
609 unsigned long timeout) 598 unsigned long timeout)
610 { 599 {
611 int ret; 600 int ret;
612 unsigned char buf[2]; 601 unsigned char buf[2];
613 struct digi_port *priv = usb_get_serial_port_data(port); 602 struct digi_port *priv = usb_get_serial_port_data(port);
614 unsigned long flags = 0; 603 unsigned long flags = 0;
615 604
616 spin_lock_irqsave(&priv->dp_port_lock, flags); 605 spin_lock_irqsave(&priv->dp_port_lock, flags);
617 priv->dp_transmit_idle = 0; 606 priv->dp_transmit_idle = 0;
618 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 607 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
619 608
620 buf[0] = DIGI_CMD_TRANSMIT_IDLE; 609 buf[0] = DIGI_CMD_TRANSMIT_IDLE;
621 buf[1] = 0; 610 buf[1] = 0;
622 611
623 timeout += jiffies; 612 timeout += jiffies;
624 613
625 ret = digi_write_inb_command(port, buf, 2, timeout - jiffies); 614 ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
626 if (ret != 0) 615 if (ret != 0)
627 return ret; 616 return ret;
628 617
629 spin_lock_irqsave(&priv->dp_port_lock, flags); 618 spin_lock_irqsave(&priv->dp_port_lock, flags);
630 619
631 while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) { 620 while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
632 cond_wait_interruptible_timeout_irqrestore( 621 cond_wait_interruptible_timeout_irqrestore(
633 &priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT, 622 &priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
634 &priv->dp_port_lock, flags); 623 &priv->dp_port_lock, flags);
635 if (signal_pending(current)) 624 if (signal_pending(current))
636 return -EINTR; 625 return -EINTR;
637 spin_lock_irqsave(&priv->dp_port_lock, flags); 626 spin_lock_irqsave(&priv->dp_port_lock, flags);
638 } 627 }
639 priv->dp_transmit_idle = 0; 628 priv->dp_transmit_idle = 0;
640 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 629 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
641 return 0; 630 return 0;
642 631
643 } 632 }
644 633
645 634
646 static void digi_rx_throttle(struct tty_struct *tty) 635 static void digi_rx_throttle(struct tty_struct *tty)
647 { 636 {
648 unsigned long flags; 637 unsigned long flags;
649 struct usb_serial_port *port = tty->driver_data; 638 struct usb_serial_port *port = tty->driver_data;
650 struct digi_port *priv = usb_get_serial_port_data(port); 639 struct digi_port *priv = usb_get_serial_port_data(port);
651 640
652 /* stop receiving characters by not resubmitting the read urb */ 641 /* stop receiving characters by not resubmitting the read urb */
653 spin_lock_irqsave(&priv->dp_port_lock, flags); 642 spin_lock_irqsave(&priv->dp_port_lock, flags);
654 priv->dp_throttled = 1; 643 priv->dp_throttled = 1;
655 priv->dp_throttle_restart = 0; 644 priv->dp_throttle_restart = 0;
656 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 645 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
657 } 646 }
658 647
659 648
660 static void digi_rx_unthrottle(struct tty_struct *tty) 649 static void digi_rx_unthrottle(struct tty_struct *tty)
661 { 650 {
662 int ret = 0; 651 int ret = 0;
663 unsigned long flags; 652 unsigned long flags;
664 struct usb_serial_port *port = tty->driver_data; 653 struct usb_serial_port *port = tty->driver_data;
665 struct digi_port *priv = usb_get_serial_port_data(port); 654 struct digi_port *priv = usb_get_serial_port_data(port);
666 655
667 spin_lock_irqsave(&priv->dp_port_lock, flags); 656 spin_lock_irqsave(&priv->dp_port_lock, flags);
668 657
669 /* restart read chain */ 658 /* restart read chain */
670 if (priv->dp_throttle_restart) 659 if (priv->dp_throttle_restart)
671 ret = usb_submit_urb(port->read_urb, GFP_ATOMIC); 660 ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
672 661
673 /* turn throttle off */ 662 /* turn throttle off */
674 priv->dp_throttled = 0; 663 priv->dp_throttled = 0;
675 priv->dp_throttle_restart = 0; 664 priv->dp_throttle_restart = 0;
676 665
677 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 666 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
678 667
679 if (ret) 668 if (ret)
680 dev_err(&port->dev, 669 dev_err(&port->dev,
681 "%s: usb_submit_urb failed, ret=%d, port=%d\n", 670 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
682 __func__, ret, priv->dp_port_num); 671 __func__, ret, priv->dp_port_num);
683 } 672 }
684 673
685 674
686 static void digi_set_termios(struct tty_struct *tty, 675 static void digi_set_termios(struct tty_struct *tty,
687 struct usb_serial_port *port, struct ktermios *old_termios) 676 struct usb_serial_port *port, struct ktermios *old_termios)
688 { 677 {
689 struct digi_port *priv = usb_get_serial_port_data(port); 678 struct digi_port *priv = usb_get_serial_port_data(port);
690 struct device *dev = &port->dev; 679 struct device *dev = &port->dev;
691 unsigned int iflag = tty->termios.c_iflag; 680 unsigned int iflag = tty->termios.c_iflag;
692 unsigned int cflag = tty->termios.c_cflag; 681 unsigned int cflag = tty->termios.c_cflag;
693 unsigned int old_iflag = old_termios->c_iflag; 682 unsigned int old_iflag = old_termios->c_iflag;
694 unsigned int old_cflag = old_termios->c_cflag; 683 unsigned int old_cflag = old_termios->c_cflag;
695 unsigned char buf[32]; 684 unsigned char buf[32];
696 unsigned int modem_signals; 685 unsigned int modem_signals;
697 int arg, ret; 686 int arg, ret;
698 int i = 0; 687 int i = 0;
699 speed_t baud; 688 speed_t baud;
700 689
701 dev_dbg(dev, 690 dev_dbg(dev,
702 "digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n", 691 "digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n",
703 priv->dp_port_num, iflag, old_iflag, cflag, old_cflag); 692 priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
704 693
705 /* set baud rate */ 694 /* set baud rate */
706 baud = tty_get_baud_rate(tty); 695 baud = tty_get_baud_rate(tty);
707 if (baud != tty_termios_baud_rate(old_termios)) { 696 if (baud != tty_termios_baud_rate(old_termios)) {
708 arg = -1; 697 arg = -1;
709 698
710 /* reassert DTR and (maybe) RTS on transition from B0 */ 699 /* reassert DTR and (maybe) RTS on transition from B0 */
711 if ((old_cflag&CBAUD) == B0) { 700 if ((old_cflag&CBAUD) == B0) {
712 /* don't set RTS if using hardware flow control */ 701 /* don't set RTS if using hardware flow control */
713 /* and throttling input */ 702 /* and throttling input */
714 modem_signals = TIOCM_DTR; 703 modem_signals = TIOCM_DTR;
715 if (!(tty->termios.c_cflag & CRTSCTS) || 704 if (!(tty->termios.c_cflag & CRTSCTS) ||
716 !test_bit(TTY_THROTTLED, &tty->flags)) 705 !test_bit(TTY_THROTTLED, &tty->flags))
717 modem_signals |= TIOCM_RTS; 706 modem_signals |= TIOCM_RTS;
718 digi_set_modem_signals(port, modem_signals, 1); 707 digi_set_modem_signals(port, modem_signals, 1);
719 } 708 }
720 switch (baud) { 709 switch (baud) {
721 /* drop DTR and RTS on transition to B0 */ 710 /* drop DTR and RTS on transition to B0 */
722 case 0: digi_set_modem_signals(port, 0, 1); break; 711 case 0: digi_set_modem_signals(port, 0, 1); break;
723 case 50: arg = DIGI_BAUD_50; break; 712 case 50: arg = DIGI_BAUD_50; break;
724 case 75: arg = DIGI_BAUD_75; break; 713 case 75: arg = DIGI_BAUD_75; break;
725 case 110: arg = DIGI_BAUD_110; break; 714 case 110: arg = DIGI_BAUD_110; break;
726 case 150: arg = DIGI_BAUD_150; break; 715 case 150: arg = DIGI_BAUD_150; break;
727 case 200: arg = DIGI_BAUD_200; break; 716 case 200: arg = DIGI_BAUD_200; break;
728 case 300: arg = DIGI_BAUD_300; break; 717 case 300: arg = DIGI_BAUD_300; break;
729 case 600: arg = DIGI_BAUD_600; break; 718 case 600: arg = DIGI_BAUD_600; break;
730 case 1200: arg = DIGI_BAUD_1200; break; 719 case 1200: arg = DIGI_BAUD_1200; break;
731 case 1800: arg = DIGI_BAUD_1800; break; 720 case 1800: arg = DIGI_BAUD_1800; break;
732 case 2400: arg = DIGI_BAUD_2400; break; 721 case 2400: arg = DIGI_BAUD_2400; break;
733 case 4800: arg = DIGI_BAUD_4800; break; 722 case 4800: arg = DIGI_BAUD_4800; break;
734 case 9600: arg = DIGI_BAUD_9600; break; 723 case 9600: arg = DIGI_BAUD_9600; break;
735 case 19200: arg = DIGI_BAUD_19200; break; 724 case 19200: arg = DIGI_BAUD_19200; break;
736 case 38400: arg = DIGI_BAUD_38400; break; 725 case 38400: arg = DIGI_BAUD_38400; break;
737 case 57600: arg = DIGI_BAUD_57600; break; 726 case 57600: arg = DIGI_BAUD_57600; break;
738 case 115200: arg = DIGI_BAUD_115200; break; 727 case 115200: arg = DIGI_BAUD_115200; break;
739 case 230400: arg = DIGI_BAUD_230400; break; 728 case 230400: arg = DIGI_BAUD_230400; break;
740 case 460800: arg = DIGI_BAUD_460800; break; 729 case 460800: arg = DIGI_BAUD_460800; break;
741 default: 730 default:
742 arg = DIGI_BAUD_9600; 731 arg = DIGI_BAUD_9600;
743 baud = 9600; 732 baud = 9600;
744 break; 733 break;
745 } 734 }
746 if (arg != -1) { 735 if (arg != -1) {
747 buf[i++] = DIGI_CMD_SET_BAUD_RATE; 736 buf[i++] = DIGI_CMD_SET_BAUD_RATE;
748 buf[i++] = priv->dp_port_num; 737 buf[i++] = priv->dp_port_num;
749 buf[i++] = arg; 738 buf[i++] = arg;
750 buf[i++] = 0; 739 buf[i++] = 0;
751 } 740 }
752 } 741 }
753 /* set parity */ 742 /* set parity */
754 tty->termios.c_cflag &= ~CMSPAR; 743 tty->termios.c_cflag &= ~CMSPAR;
755 744
756 if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) { 745 if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) {
757 if (cflag&PARENB) { 746 if (cflag&PARENB) {
758 if (cflag&PARODD) 747 if (cflag&PARODD)
759 arg = DIGI_PARITY_ODD; 748 arg = DIGI_PARITY_ODD;
760 else 749 else
761 arg = DIGI_PARITY_EVEN; 750 arg = DIGI_PARITY_EVEN;
762 } else { 751 } else {
763 arg = DIGI_PARITY_NONE; 752 arg = DIGI_PARITY_NONE;
764 } 753 }
765 buf[i++] = DIGI_CMD_SET_PARITY; 754 buf[i++] = DIGI_CMD_SET_PARITY;
766 buf[i++] = priv->dp_port_num; 755 buf[i++] = priv->dp_port_num;
767 buf[i++] = arg; 756 buf[i++] = arg;
768 buf[i++] = 0; 757 buf[i++] = 0;
769 } 758 }
770 /* set word size */ 759 /* set word size */
771 if ((cflag&CSIZE) != (old_cflag&CSIZE)) { 760 if ((cflag&CSIZE) != (old_cflag&CSIZE)) {
772 arg = -1; 761 arg = -1;
773 switch (cflag&CSIZE) { 762 switch (cflag&CSIZE) {
774 case CS5: arg = DIGI_WORD_SIZE_5; break; 763 case CS5: arg = DIGI_WORD_SIZE_5; break;
775 case CS6: arg = DIGI_WORD_SIZE_6; break; 764 case CS6: arg = DIGI_WORD_SIZE_6; break;
776 case CS7: arg = DIGI_WORD_SIZE_7; break; 765 case CS7: arg = DIGI_WORD_SIZE_7; break;
777 case CS8: arg = DIGI_WORD_SIZE_8; break; 766 case CS8: arg = DIGI_WORD_SIZE_8; break;
778 default: 767 default:
779 dev_dbg(dev, 768 dev_dbg(dev,
780 "digi_set_termios: can't handle word size %d\n", 769 "digi_set_termios: can't handle word size %d\n",
781 (cflag&CSIZE)); 770 (cflag&CSIZE));
782 break; 771 break;
783 } 772 }
784 773
785 if (arg != -1) { 774 if (arg != -1) {
786 buf[i++] = DIGI_CMD_SET_WORD_SIZE; 775 buf[i++] = DIGI_CMD_SET_WORD_SIZE;
787 buf[i++] = priv->dp_port_num; 776 buf[i++] = priv->dp_port_num;
788 buf[i++] = arg; 777 buf[i++] = arg;
789 buf[i++] = 0; 778 buf[i++] = 0;
790 } 779 }
791 780
792 } 781 }
793 782
794 /* set stop bits */ 783 /* set stop bits */
795 if ((cflag&CSTOPB) != (old_cflag&CSTOPB)) { 784 if ((cflag&CSTOPB) != (old_cflag&CSTOPB)) {
796 785
797 if ((cflag&CSTOPB)) 786 if ((cflag&CSTOPB))
798 arg = DIGI_STOP_BITS_2; 787 arg = DIGI_STOP_BITS_2;
799 else 788 else
800 arg = DIGI_STOP_BITS_1; 789 arg = DIGI_STOP_BITS_1;
801 790
802 buf[i++] = DIGI_CMD_SET_STOP_BITS; 791 buf[i++] = DIGI_CMD_SET_STOP_BITS;
803 buf[i++] = priv->dp_port_num; 792 buf[i++] = priv->dp_port_num;
804 buf[i++] = arg; 793 buf[i++] = arg;
805 buf[i++] = 0; 794 buf[i++] = 0;
806 795
807 } 796 }
808 797
809 /* set input flow control */ 798 /* set input flow control */
810 if ((iflag&IXOFF) != (old_iflag&IXOFF) 799 if ((iflag&IXOFF) != (old_iflag&IXOFF)
811 || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) { 800 || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) {
812 arg = 0; 801 arg = 0;
813 if (iflag&IXOFF) 802 if (iflag&IXOFF)
814 arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF; 803 arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
815 else 804 else
816 arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF; 805 arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
817 806
818 if (cflag&CRTSCTS) { 807 if (cflag&CRTSCTS) {
819 arg |= DIGI_INPUT_FLOW_CONTROL_RTS; 808 arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
820 809
821 /* On USB-4 it is necessary to assert RTS prior */ 810 /* On USB-4 it is necessary to assert RTS prior */
822 /* to selecting RTS input flow control. */ 811 /* to selecting RTS input flow control. */
823 buf[i++] = DIGI_CMD_SET_RTS_SIGNAL; 812 buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
824 buf[i++] = priv->dp_port_num; 813 buf[i++] = priv->dp_port_num;
825 buf[i++] = DIGI_RTS_ACTIVE; 814 buf[i++] = DIGI_RTS_ACTIVE;
826 buf[i++] = 0; 815 buf[i++] = 0;
827 816
828 } else { 817 } else {
829 arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS; 818 arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
830 } 819 }
831 buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL; 820 buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
832 buf[i++] = priv->dp_port_num; 821 buf[i++] = priv->dp_port_num;
833 buf[i++] = arg; 822 buf[i++] = arg;
834 buf[i++] = 0; 823 buf[i++] = 0;
835 } 824 }
836 825
837 /* set output flow control */ 826 /* set output flow control */
838 if ((iflag & IXON) != (old_iflag & IXON) 827 if ((iflag & IXON) != (old_iflag & IXON)
839 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) { 828 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
840 arg = 0; 829 arg = 0;
841 if (iflag & IXON) 830 if (iflag & IXON)
842 arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF; 831 arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
843 else 832 else
844 arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF; 833 arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
845 834
846 if (cflag & CRTSCTS) { 835 if (cflag & CRTSCTS) {
847 arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS; 836 arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
848 } else { 837 } else {
849 arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS; 838 arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
850 tty->hw_stopped = 0; 839 tty->hw_stopped = 0;
851 } 840 }
852 841
853 buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL; 842 buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
854 buf[i++] = priv->dp_port_num; 843 buf[i++] = priv->dp_port_num;
855 buf[i++] = arg; 844 buf[i++] = arg;
856 buf[i++] = 0; 845 buf[i++] = 0;
857 } 846 }
858 847
859 /* set receive enable/disable */ 848 /* set receive enable/disable */
860 if ((cflag & CREAD) != (old_cflag & CREAD)) { 849 if ((cflag & CREAD) != (old_cflag & CREAD)) {
861 if (cflag & CREAD) 850 if (cflag & CREAD)
862 arg = DIGI_ENABLE; 851 arg = DIGI_ENABLE;
863 else 852 else
864 arg = DIGI_DISABLE; 853 arg = DIGI_DISABLE;
865 854
866 buf[i++] = DIGI_CMD_RECEIVE_ENABLE; 855 buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
867 buf[i++] = priv->dp_port_num; 856 buf[i++] = priv->dp_port_num;
868 buf[i++] = arg; 857 buf[i++] = arg;
869 buf[i++] = 0; 858 buf[i++] = 0;
870 } 859 }
871 ret = digi_write_oob_command(port, buf, i, 1); 860 ret = digi_write_oob_command(port, buf, i, 1);
872 if (ret != 0) 861 if (ret != 0)
873 dev_dbg(dev, "digi_set_termios: write oob failed, ret=%d\n", ret); 862 dev_dbg(dev, "digi_set_termios: write oob failed, ret=%d\n", ret);
874 tty_encode_baud_rate(tty, baud, baud); 863 tty_encode_baud_rate(tty, baud, baud);
875 } 864 }
876 865
877 866
878 static void digi_break_ctl(struct tty_struct *tty, int break_state) 867 static void digi_break_ctl(struct tty_struct *tty, int break_state)
879 { 868 {
880 struct usb_serial_port *port = tty->driver_data; 869 struct usb_serial_port *port = tty->driver_data;
881 unsigned char buf[4]; 870 unsigned char buf[4];
882 871
883 buf[0] = DIGI_CMD_BREAK_CONTROL; 872 buf[0] = DIGI_CMD_BREAK_CONTROL;
884 buf[1] = 2; /* length */ 873 buf[1] = 2; /* length */
885 buf[2] = break_state ? 1 : 0; 874 buf[2] = break_state ? 1 : 0;
886 buf[3] = 0; /* pad */ 875 buf[3] = 0; /* pad */
887 digi_write_inb_command(port, buf, 4, 0); 876 digi_write_inb_command(port, buf, 4, 0);
888 } 877 }
889 878
890 879
891 static int digi_tiocmget(struct tty_struct *tty) 880 static int digi_tiocmget(struct tty_struct *tty)
892 { 881 {
893 struct usb_serial_port *port = tty->driver_data; 882 struct usb_serial_port *port = tty->driver_data;
894 struct digi_port *priv = usb_get_serial_port_data(port); 883 struct digi_port *priv = usb_get_serial_port_data(port);
895 unsigned int val; 884 unsigned int val;
896 unsigned long flags; 885 unsigned long flags;
897 886
898 spin_lock_irqsave(&priv->dp_port_lock, flags); 887 spin_lock_irqsave(&priv->dp_port_lock, flags);
899 val = priv->dp_modem_signals; 888 val = priv->dp_modem_signals;
900 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 889 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
901 return val; 890 return val;
902 } 891 }
903 892
904 893
905 static int digi_tiocmset(struct tty_struct *tty, 894 static int digi_tiocmset(struct tty_struct *tty,
906 unsigned int set, unsigned int clear) 895 unsigned int set, unsigned int clear)
907 { 896 {
908 struct usb_serial_port *port = tty->driver_data; 897 struct usb_serial_port *port = tty->driver_data;
909 struct digi_port *priv = usb_get_serial_port_data(port); 898 struct digi_port *priv = usb_get_serial_port_data(port);
910 unsigned int val; 899 unsigned int val;
911 unsigned long flags; 900 unsigned long flags;
912 901
913 spin_lock_irqsave(&priv->dp_port_lock, flags); 902 spin_lock_irqsave(&priv->dp_port_lock, flags);
914 val = (priv->dp_modem_signals & ~clear) | set; 903 val = (priv->dp_modem_signals & ~clear) | set;
915 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 904 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
916 return digi_set_modem_signals(port, val, 1); 905 return digi_set_modem_signals(port, val, 1);
917 } 906 }
918 907
919 908
920 static int digi_write(struct tty_struct *tty, struct usb_serial_port *port, 909 static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
921 const unsigned char *buf, int count) 910 const unsigned char *buf, int count)
922 { 911 {
923 912
924 int ret, data_len, new_len; 913 int ret, data_len, new_len;
925 struct digi_port *priv = usb_get_serial_port_data(port); 914 struct digi_port *priv = usb_get_serial_port_data(port);
926 unsigned char *data = port->write_urb->transfer_buffer; 915 unsigned char *data = port->write_urb->transfer_buffer;
927 unsigned long flags = 0; 916 unsigned long flags = 0;
928 917
929 dev_dbg(&port->dev, 918 dev_dbg(&port->dev,
930 "digi_write: TOP: port=%d, count=%d, in_interrupt=%ld\n", 919 "digi_write: TOP: port=%d, count=%d, in_interrupt=%ld\n",
931 priv->dp_port_num, count, in_interrupt()); 920 priv->dp_port_num, count, in_interrupt());
932 921
933 /* copy user data (which can sleep) before getting spin lock */ 922 /* copy user data (which can sleep) before getting spin lock */
934 count = min(count, port->bulk_out_size-2); 923 count = min(count, port->bulk_out_size-2);
935 count = min(64, count); 924 count = min(64, count);
936 925
937 /* be sure only one write proceeds at a time */ 926 /* be sure only one write proceeds at a time */
938 /* there are races on the port private buffer */ 927 /* there are races on the port private buffer */
939 spin_lock_irqsave(&priv->dp_port_lock, flags); 928 spin_lock_irqsave(&priv->dp_port_lock, flags);
940 929
941 /* wait for urb status clear to submit another urb */ 930 /* wait for urb status clear to submit another urb */
942 if (priv->dp_write_urb_in_use) { 931 if (priv->dp_write_urb_in_use) {
943 /* buffer data if count is 1 (probably put_char) if possible */ 932 /* buffer data if count is 1 (probably put_char) if possible */
944 if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) { 933 if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
945 priv->dp_out_buf[priv->dp_out_buf_len++] = *buf; 934 priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
946 new_len = 1; 935 new_len = 1;
947 } else { 936 } else {
948 new_len = 0; 937 new_len = 0;
949 } 938 }
950 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 939 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
951 return new_len; 940 return new_len;
952 } 941 }
953 942
954 /* allow space for any buffered data and for new data, up to */ 943 /* allow space for any buffered data and for new data, up to */
955 /* transfer buffer size - 2 (for command and length bytes) */ 944 /* transfer buffer size - 2 (for command and length bytes) */
956 new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len); 945 new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
957 data_len = new_len + priv->dp_out_buf_len; 946 data_len = new_len + priv->dp_out_buf_len;
958 947
959 if (data_len == 0) { 948 if (data_len == 0) {
960 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 949 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
961 return 0; 950 return 0;
962 } 951 }
963 952
964 port->write_urb->transfer_buffer_length = data_len+2; 953 port->write_urb->transfer_buffer_length = data_len+2;
965 954
966 *data++ = DIGI_CMD_SEND_DATA; 955 *data++ = DIGI_CMD_SEND_DATA;
967 *data++ = data_len; 956 *data++ = data_len;
968 957
969 /* copy in buffered data first */ 958 /* copy in buffered data first */
970 memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len); 959 memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
971 data += priv->dp_out_buf_len; 960 data += priv->dp_out_buf_len;
972 961
973 /* copy in new data */ 962 /* copy in new data */
974 memcpy(data, buf, new_len); 963 memcpy(data, buf, new_len);
975 964
976 ret = usb_submit_urb(port->write_urb, GFP_ATOMIC); 965 ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
977 if (ret == 0) { 966 if (ret == 0) {
978 priv->dp_write_urb_in_use = 1; 967 priv->dp_write_urb_in_use = 1;
979 ret = new_len; 968 ret = new_len;
980 priv->dp_out_buf_len = 0; 969 priv->dp_out_buf_len = 0;
981 } 970 }
982 971
983 /* return length of new data written, or error */ 972 /* return length of new data written, or error */
984 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 973 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
985 if (ret < 0) 974 if (ret < 0)
986 dev_err_console(port, 975 dev_err_console(port,
987 "%s: usb_submit_urb failed, ret=%d, port=%d\n", 976 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
988 __func__, ret, priv->dp_port_num); 977 __func__, ret, priv->dp_port_num);
989 dev_dbg(&port->dev, "digi_write: returning %d\n", ret); 978 dev_dbg(&port->dev, "digi_write: returning %d\n", ret);
990 return ret; 979 return ret;
991 980
992 } 981 }
993 982
994 static void digi_write_bulk_callback(struct urb *urb) 983 static void digi_write_bulk_callback(struct urb *urb)
995 { 984 {
996 985
997 struct usb_serial_port *port = urb->context; 986 struct usb_serial_port *port = urb->context;
998 struct usb_serial *serial; 987 struct usb_serial *serial;
999 struct digi_port *priv; 988 struct digi_port *priv;
1000 struct digi_serial *serial_priv; 989 struct digi_serial *serial_priv;
1001 int ret = 0; 990 int ret = 0;
1002 int status = urb->status; 991 int status = urb->status;
1003 992
1004 /* port and serial sanity check */ 993 /* port and serial sanity check */
1005 if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) { 994 if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
1006 pr_err("%s: port or port->private is NULL, status=%d\n", 995 pr_err("%s: port or port->private is NULL, status=%d\n",
1007 __func__, status); 996 __func__, status);
1008 return; 997 return;
1009 } 998 }
1010 serial = port->serial; 999 serial = port->serial;
1011 if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) { 1000 if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
1012 dev_err(&port->dev, 1001 dev_err(&port->dev,
1013 "%s: serial or serial->private is NULL, status=%d\n", 1002 "%s: serial or serial->private is NULL, status=%d\n",
1014 __func__, status); 1003 __func__, status);
1015 return; 1004 return;
1016 } 1005 }
1017 1006
1018 /* handle oob callback */ 1007 /* handle oob callback */
1019 if (priv->dp_port_num == serial_priv->ds_oob_port_num) { 1008 if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
1020 dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n"); 1009 dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n");
1021 spin_lock(&priv->dp_port_lock); 1010 spin_lock(&priv->dp_port_lock);
1022 priv->dp_write_urb_in_use = 0; 1011 priv->dp_write_urb_in_use = 0;
1023 wake_up_interruptible(&port->write_wait); 1012 wake_up_interruptible(&port->write_wait);
1024 spin_unlock(&priv->dp_port_lock); 1013 spin_unlock(&priv->dp_port_lock);
1025 return; 1014 return;
1026 } 1015 }
1027 1016
1028 /* try to send any buffered data on this port */ 1017 /* try to send any buffered data on this port */
1029 spin_lock(&priv->dp_port_lock); 1018 spin_lock(&priv->dp_port_lock);
1030 priv->dp_write_urb_in_use = 0; 1019 priv->dp_write_urb_in_use = 0;
1031 if (priv->dp_out_buf_len > 0) { 1020 if (priv->dp_out_buf_len > 0) {
1032 *((unsigned char *)(port->write_urb->transfer_buffer)) 1021 *((unsigned char *)(port->write_urb->transfer_buffer))
1033 = (unsigned char)DIGI_CMD_SEND_DATA; 1022 = (unsigned char)DIGI_CMD_SEND_DATA;
1034 *((unsigned char *)(port->write_urb->transfer_buffer) + 1) 1023 *((unsigned char *)(port->write_urb->transfer_buffer) + 1)
1035 = (unsigned char)priv->dp_out_buf_len; 1024 = (unsigned char)priv->dp_out_buf_len;
1036 port->write_urb->transfer_buffer_length = 1025 port->write_urb->transfer_buffer_length =
1037 priv->dp_out_buf_len + 2; 1026 priv->dp_out_buf_len + 2;
1038 memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf, 1027 memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
1039 priv->dp_out_buf_len); 1028 priv->dp_out_buf_len);
1040 ret = usb_submit_urb(port->write_urb, GFP_ATOMIC); 1029 ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1041 if (ret == 0) { 1030 if (ret == 0) {
1042 priv->dp_write_urb_in_use = 1; 1031 priv->dp_write_urb_in_use = 1;
1043 priv->dp_out_buf_len = 0; 1032 priv->dp_out_buf_len = 0;
1044 } 1033 }
1045 } 1034 }
1046 /* wake up processes sleeping on writes immediately */ 1035 /* wake up processes sleeping on writes immediately */
1047 digi_wakeup_write(port); 1036 tty_port_tty_wakeup(&port->port);
1048 /* also queue up a wakeup at scheduler time, in case we */ 1037 /* also queue up a wakeup at scheduler time, in case we */
1049 /* lost the race in write_chan(). */ 1038 /* lost the race in write_chan(). */
1050 schedule_work(&priv->dp_wakeup_work); 1039 schedule_work(&priv->dp_wakeup_work);
1051 1040
1052 spin_unlock(&priv->dp_port_lock); 1041 spin_unlock(&priv->dp_port_lock);
1053 if (ret && ret != -EPERM) 1042 if (ret && ret != -EPERM)
1054 dev_err_console(port, 1043 dev_err_console(port,
1055 "%s: usb_submit_urb failed, ret=%d, port=%d\n", 1044 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
1056 __func__, ret, priv->dp_port_num); 1045 __func__, ret, priv->dp_port_num);
1057 } 1046 }
1058 1047
1059 static int digi_write_room(struct tty_struct *tty) 1048 static int digi_write_room(struct tty_struct *tty)
1060 { 1049 {
1061 struct usb_serial_port *port = tty->driver_data; 1050 struct usb_serial_port *port = tty->driver_data;
1062 struct digi_port *priv = usb_get_serial_port_data(port); 1051 struct digi_port *priv = usb_get_serial_port_data(port);
1063 int room; 1052 int room;
1064 unsigned long flags = 0; 1053 unsigned long flags = 0;
1065 1054
1066 spin_lock_irqsave(&priv->dp_port_lock, flags); 1055 spin_lock_irqsave(&priv->dp_port_lock, flags);
1067 1056
1068 if (priv->dp_write_urb_in_use) 1057 if (priv->dp_write_urb_in_use)
1069 room = 0; 1058 room = 0;
1070 else 1059 else
1071 room = port->bulk_out_size - 2 - priv->dp_out_buf_len; 1060 room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
1072 1061
1073 spin_unlock_irqrestore(&priv->dp_port_lock, flags); 1062 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1074 dev_dbg(&port->dev, "digi_write_room: port=%d, room=%d\n", priv->dp_port_num, room); 1063 dev_dbg(&port->dev, "digi_write_room: port=%d, room=%d\n", priv->dp_port_num, room);
1075 return room; 1064 return room;
1076 1065
1077 } 1066 }
1078 1067
1079 static int digi_chars_in_buffer(struct tty_struct *tty) 1068 static int digi_chars_in_buffer(struct tty_struct *tty)
1080 { 1069 {
1081 struct usb_serial_port *port = tty->driver_data; 1070 struct usb_serial_port *port = tty->driver_data;
1082 struct digi_port *priv = usb_get_serial_port_data(port); 1071 struct digi_port *priv = usb_get_serial_port_data(port);
1083 1072
1084 if (priv->dp_write_urb_in_use) { 1073 if (priv->dp_write_urb_in_use) {
1085 dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n", 1074 dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
1086 priv->dp_port_num, port->bulk_out_size - 2); 1075 priv->dp_port_num, port->bulk_out_size - 2);
1087 /* return(port->bulk_out_size - 2); */ 1076 /* return(port->bulk_out_size - 2); */
1088 return 256; 1077 return 256;
1089 } else { 1078 } else {
1090 dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n", 1079 dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
1091 priv->dp_port_num, priv->dp_out_buf_len); 1080 priv->dp_port_num, priv->dp_out_buf_len);
1092 return priv->dp_out_buf_len; 1081 return priv->dp_out_buf_len;
1093 } 1082 }
1094 1083
1095 } 1084 }
1096 1085
1097 static void digi_dtr_rts(struct usb_serial_port *port, int on) 1086 static void digi_dtr_rts(struct usb_serial_port *port, int on)
1098 { 1087 {
1099 /* Adjust DTR and RTS */ 1088 /* Adjust DTR and RTS */
1100 digi_set_modem_signals(port, on * (TIOCM_DTR|TIOCM_RTS), 1); 1089 digi_set_modem_signals(port, on * (TIOCM_DTR|TIOCM_RTS), 1);
1101 } 1090 }
1102 1091
1103 static int digi_open(struct tty_struct *tty, struct usb_serial_port *port) 1092 static int digi_open(struct tty_struct *tty, struct usb_serial_port *port)
1104 { 1093 {
1105 int ret; 1094 int ret;
1106 unsigned char buf[32]; 1095 unsigned char buf[32];
1107 struct digi_port *priv = usb_get_serial_port_data(port); 1096 struct digi_port *priv = usb_get_serial_port_data(port);
1108 struct ktermios not_termios; 1097 struct ktermios not_termios;
1109 1098
1110 /* be sure the device is started up */ 1099 /* be sure the device is started up */
1111 if (digi_startup_device(port->serial) != 0) 1100 if (digi_startup_device(port->serial) != 0)
1112 return -ENXIO; 1101 return -ENXIO;
1113 1102
1114 /* read modem signals automatically whenever they change */ 1103 /* read modem signals automatically whenever they change */
1115 buf[0] = DIGI_CMD_READ_INPUT_SIGNALS; 1104 buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
1116 buf[1] = priv->dp_port_num; 1105 buf[1] = priv->dp_port_num;
1117 buf[2] = DIGI_ENABLE; 1106 buf[2] = DIGI_ENABLE;
1118 buf[3] = 0; 1107 buf[3] = 0;
1119 1108
1120 /* flush fifos */ 1109 /* flush fifos */
1121 buf[4] = DIGI_CMD_IFLUSH_FIFO; 1110 buf[4] = DIGI_CMD_IFLUSH_FIFO;
1122 buf[5] = priv->dp_port_num; 1111 buf[5] = priv->dp_port_num;
1123 buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX; 1112 buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1124 buf[7] = 0; 1113 buf[7] = 0;
1125 1114
1126 ret = digi_write_oob_command(port, buf, 8, 1); 1115 ret = digi_write_oob_command(port, buf, 8, 1);
1127 if (ret != 0) 1116 if (ret != 0)
1128 dev_dbg(&port->dev, "digi_open: write oob failed, ret=%d\n", ret); 1117 dev_dbg(&port->dev, "digi_open: write oob failed, ret=%d\n", ret);
1129 1118
1130 /* set termios settings */ 1119 /* set termios settings */
1131 if (tty) { 1120 if (tty) {
1132 not_termios.c_cflag = ~tty->termios.c_cflag; 1121 not_termios.c_cflag = ~tty->termios.c_cflag;
1133 not_termios.c_iflag = ~tty->termios.c_iflag; 1122 not_termios.c_iflag = ~tty->termios.c_iflag;
1134 digi_set_termios(tty, port, &not_termios); 1123 digi_set_termios(tty, port, &not_termios);
1135 } 1124 }
1136 return 0; 1125 return 0;
1137 } 1126 }
1138 1127
1139 1128
1140 static void digi_close(struct usb_serial_port *port) 1129 static void digi_close(struct usb_serial_port *port)
1141 { 1130 {
1142 DEFINE_WAIT(wait); 1131 DEFINE_WAIT(wait);
1143 int ret; 1132 int ret;
1144 unsigned char buf[32]; 1133 unsigned char buf[32];
1145 struct digi_port *priv = usb_get_serial_port_data(port); 1134 struct digi_port *priv = usb_get_serial_port_data(port);
1146 1135
1147 mutex_lock(&port->serial->disc_mutex); 1136 mutex_lock(&port->serial->disc_mutex);
1148 /* if disconnected, just clear flags */ 1137 /* if disconnected, just clear flags */
1149 if (port->serial->disconnected) 1138 if (port->serial->disconnected)
1150 goto exit; 1139 goto exit;
1151 1140
1152 if (port->serial->dev) { 1141 if (port->serial->dev) {
1153 /* FIXME: Transmit idle belongs in the wait_unti_sent path */ 1142 /* FIXME: Transmit idle belongs in the wait_unti_sent path */
1154 digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT); 1143 digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
1155 1144
1156 /* disable input flow control */ 1145 /* disable input flow control */
1157 buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL; 1146 buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
1158 buf[1] = priv->dp_port_num; 1147 buf[1] = priv->dp_port_num;
1159 buf[2] = DIGI_DISABLE; 1148 buf[2] = DIGI_DISABLE;
1160 buf[3] = 0; 1149 buf[3] = 0;
1161 1150
1162 /* disable output flow control */ 1151 /* disable output flow control */
1163 buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL; 1152 buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
1164 buf[5] = priv->dp_port_num; 1153 buf[5] = priv->dp_port_num;
1165 buf[6] = DIGI_DISABLE; 1154 buf[6] = DIGI_DISABLE;
1166 buf[7] = 0; 1155 buf[7] = 0;
1167 1156
1168 /* disable reading modem signals automatically */ 1157 /* disable reading modem signals automatically */
1169 buf[8] = DIGI_CMD_READ_INPUT_SIGNALS; 1158 buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
1170 buf[9] = priv->dp_port_num; 1159 buf[9] = priv->dp_port_num;
1171 buf[10] = DIGI_DISABLE; 1160 buf[10] = DIGI_DISABLE;
1172 buf[11] = 0; 1161 buf[11] = 0;
1173 1162
1174 /* disable receive */ 1163 /* disable receive */
1175 buf[12] = DIGI_CMD_RECEIVE_ENABLE; 1164 buf[12] = DIGI_CMD_RECEIVE_ENABLE;
1176 buf[13] = priv->dp_port_num; 1165 buf[13] = priv->dp_port_num;
1177 buf[14] = DIGI_DISABLE; 1166 buf[14] = DIGI_DISABLE;
1178 buf[15] = 0; 1167 buf[15] = 0;
1179 1168
1180 /* flush fifos */ 1169 /* flush fifos */
1181 buf[16] = DIGI_CMD_IFLUSH_FIFO; 1170 buf[16] = DIGI_CMD_IFLUSH_FIFO;
1182 buf[17] = priv->dp_port_num; 1171 buf[17] = priv->dp_port_num;
1183 buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX; 1172 buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1184 buf[19] = 0; 1173 buf[19] = 0;
1185 1174
1186 ret = digi_write_oob_command(port, buf, 20, 0); 1175 ret = digi_write_oob_command(port, buf, 20, 0);
1187 if (ret != 0) 1176 if (ret != 0)
1188 dev_dbg(&port->dev, "digi_close: write oob failed, ret=%d\n", ret); 1177 dev_dbg(&port->dev, "digi_close: write oob failed, ret=%d\n", ret);
1189 1178
1190 /* wait for final commands on oob port to complete */ 1179 /* wait for final commands on oob port to complete */
1191 prepare_to_wait(&priv->dp_flush_wait, &wait, 1180 prepare_to_wait(&priv->dp_flush_wait, &wait,
1192 TASK_INTERRUPTIBLE); 1181 TASK_INTERRUPTIBLE);
1193 schedule_timeout(DIGI_CLOSE_TIMEOUT); 1182 schedule_timeout(DIGI_CLOSE_TIMEOUT);
1194 finish_wait(&priv->dp_flush_wait, &wait); 1183 finish_wait(&priv->dp_flush_wait, &wait);
1195 1184
1196 /* shutdown any outstanding bulk writes */ 1185 /* shutdown any outstanding bulk writes */
1197 usb_kill_urb(port->write_urb); 1186 usb_kill_urb(port->write_urb);
1198 } 1187 }
1199 exit: 1188 exit:
1200 spin_lock_irq(&priv->dp_port_lock); 1189 spin_lock_irq(&priv->dp_port_lock);
1201 priv->dp_write_urb_in_use = 0; 1190 priv->dp_write_urb_in_use = 0;
1202 wake_up_interruptible(&priv->dp_close_wait); 1191 wake_up_interruptible(&priv->dp_close_wait);
1203 spin_unlock_irq(&priv->dp_port_lock); 1192 spin_unlock_irq(&priv->dp_port_lock);
1204 mutex_unlock(&port->serial->disc_mutex); 1193 mutex_unlock(&port->serial->disc_mutex);
1205 } 1194 }
1206 1195
1207 1196
1208 /* 1197 /*
1209 * Digi Startup Device 1198 * Digi Startup Device
1210 * 1199 *
1211 * Starts reads on all ports. Must be called AFTER startup, with 1200 * Starts reads on all ports. Must be called AFTER startup, with
1212 * urbs initialized. Returns 0 if successful, non-zero error otherwise. 1201 * urbs initialized. Returns 0 if successful, non-zero error otherwise.
1213 */ 1202 */
1214 1203
1215 static int digi_startup_device(struct usb_serial *serial) 1204 static int digi_startup_device(struct usb_serial *serial)
1216 { 1205 {
1217 int i, ret = 0; 1206 int i, ret = 0;
1218 struct digi_serial *serial_priv = usb_get_serial_data(serial); 1207 struct digi_serial *serial_priv = usb_get_serial_data(serial);
1219 struct usb_serial_port *port; 1208 struct usb_serial_port *port;
1220 1209
1221 /* be sure this happens exactly once */ 1210 /* be sure this happens exactly once */
1222 spin_lock(&serial_priv->ds_serial_lock); 1211 spin_lock(&serial_priv->ds_serial_lock);
1223 if (serial_priv->ds_device_started) { 1212 if (serial_priv->ds_device_started) {
1224 spin_unlock(&serial_priv->ds_serial_lock); 1213 spin_unlock(&serial_priv->ds_serial_lock);
1225 return 0; 1214 return 0;
1226 } 1215 }
1227 serial_priv->ds_device_started = 1; 1216 serial_priv->ds_device_started = 1;
1228 spin_unlock(&serial_priv->ds_serial_lock); 1217 spin_unlock(&serial_priv->ds_serial_lock);
1229 1218
1230 /* start reading from each bulk in endpoint for the device */ 1219 /* start reading from each bulk in endpoint for the device */
1231 /* set USB_DISABLE_SPD flag for write bulk urbs */ 1220 /* set USB_DISABLE_SPD flag for write bulk urbs */
1232 for (i = 0; i < serial->type->num_ports + 1; i++) { 1221 for (i = 0; i < serial->type->num_ports + 1; i++) {
1233 port = serial->port[i]; 1222 port = serial->port[i];
1234 ret = usb_submit_urb(port->read_urb, GFP_KERNEL); 1223 ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
1235 if (ret != 0) { 1224 if (ret != 0) {
1236 dev_err(&port->dev, 1225 dev_err(&port->dev,
1237 "%s: usb_submit_urb failed, ret=%d, port=%d\n", 1226 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
1238 __func__, ret, i); 1227 __func__, ret, i);
1239 break; 1228 break;
1240 } 1229 }
1241 } 1230 }
1242 return ret; 1231 return ret;
1243 } 1232 }
1244 1233
1245 static int digi_port_init(struct usb_serial_port *port, unsigned port_num) 1234 static int digi_port_init(struct usb_serial_port *port, unsigned port_num)
1246 { 1235 {
1247 struct digi_port *priv; 1236 struct digi_port *priv;
1248 1237
1249 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 1238 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1250 if (!priv) 1239 if (!priv)
1251 return -ENOMEM; 1240 return -ENOMEM;
1252 1241
1253 spin_lock_init(&priv->dp_port_lock); 1242 spin_lock_init(&priv->dp_port_lock);
1254 priv->dp_port_num = port_num; 1243 priv->dp_port_num = port_num;
1255 init_waitqueue_head(&priv->dp_modem_change_wait); 1244 init_waitqueue_head(&priv->dp_modem_change_wait);
1256 init_waitqueue_head(&priv->dp_transmit_idle_wait); 1245 init_waitqueue_head(&priv->dp_transmit_idle_wait);
1257 init_waitqueue_head(&priv->dp_flush_wait); 1246 init_waitqueue_head(&priv->dp_flush_wait);
1258 init_waitqueue_head(&priv->dp_close_wait); 1247 init_waitqueue_head(&priv->dp_close_wait);
1259 INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock); 1248 INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock);
1260 priv->dp_port = port; 1249 priv->dp_port = port;
1261 1250
1262 init_waitqueue_head(&port->write_wait); 1251 init_waitqueue_head(&port->write_wait);
1263 1252
1264 usb_set_serial_port_data(port, priv); 1253 usb_set_serial_port_data(port, priv);
1265 1254
1266 return 0; 1255 return 0;
1267 } 1256 }
1268 1257
1269 static int digi_startup(struct usb_serial *serial) 1258 static int digi_startup(struct usb_serial *serial)
1270 { 1259 {
1271 struct digi_serial *serial_priv; 1260 struct digi_serial *serial_priv;
1272 int ret; 1261 int ret;
1273 1262
1274 serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL); 1263 serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
1275 if (!serial_priv) 1264 if (!serial_priv)
1276 return -ENOMEM; 1265 return -ENOMEM;
1277 1266
1278 spin_lock_init(&serial_priv->ds_serial_lock); 1267 spin_lock_init(&serial_priv->ds_serial_lock);
1279 serial_priv->ds_oob_port_num = serial->type->num_ports; 1268 serial_priv->ds_oob_port_num = serial->type->num_ports;
1280 serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num]; 1269 serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
1281 1270
1282 ret = digi_port_init(serial_priv->ds_oob_port, 1271 ret = digi_port_init(serial_priv->ds_oob_port,
1283 serial_priv->ds_oob_port_num); 1272 serial_priv->ds_oob_port_num);
1284 if (ret) { 1273 if (ret) {
1285 kfree(serial_priv); 1274 kfree(serial_priv);
1286 return ret; 1275 return ret;
1287 } 1276 }
1288 1277
1289 usb_set_serial_data(serial, serial_priv); 1278 usb_set_serial_data(serial, serial_priv);
1290 1279
1291 return 0; 1280 return 0;
1292 } 1281 }
1293 1282
1294 1283
1295 static void digi_disconnect(struct usb_serial *serial) 1284 static void digi_disconnect(struct usb_serial *serial)
1296 { 1285 {
1297 int i; 1286 int i;
1298 1287
1299 /* stop reads and writes on all ports */ 1288 /* stop reads and writes on all ports */
1300 for (i = 0; i < serial->type->num_ports + 1; i++) { 1289 for (i = 0; i < serial->type->num_ports + 1; i++) {
1301 usb_kill_urb(serial->port[i]->read_urb); 1290 usb_kill_urb(serial->port[i]->read_urb);
1302 usb_kill_urb(serial->port[i]->write_urb); 1291 usb_kill_urb(serial->port[i]->write_urb);
1303 } 1292 }
1304 } 1293 }
1305 1294
1306 1295
1307 static void digi_release(struct usb_serial *serial) 1296 static void digi_release(struct usb_serial *serial)
1308 { 1297 {
1309 struct digi_serial *serial_priv; 1298 struct digi_serial *serial_priv;
1310 struct digi_port *priv; 1299 struct digi_port *priv;
1311 1300
1312 serial_priv = usb_get_serial_data(serial); 1301 serial_priv = usb_get_serial_data(serial);
1313 1302
1314 priv = usb_get_serial_port_data(serial_priv->ds_oob_port); 1303 priv = usb_get_serial_port_data(serial_priv->ds_oob_port);
1315 kfree(priv); 1304 kfree(priv);
1316 1305
1317 kfree(serial_priv); 1306 kfree(serial_priv);
1318 } 1307 }
1319 1308
1320 static int digi_port_probe(struct usb_serial_port *port) 1309 static int digi_port_probe(struct usb_serial_port *port)
1321 { 1310 {
1322 unsigned port_num; 1311 unsigned port_num;
1323 1312
1324 port_num = port->number - port->serial->minor; 1313 port_num = port->number - port->serial->minor;
1325 1314
1326 return digi_port_init(port, port_num); 1315 return digi_port_init(port, port_num);
1327 } 1316 }
1328 1317
1329 static int digi_port_remove(struct usb_serial_port *port) 1318 static int digi_port_remove(struct usb_serial_port *port)
1330 { 1319 {
1331 struct digi_port *priv; 1320 struct digi_port *priv;
1332 1321
1333 priv = usb_get_serial_port_data(port); 1322 priv = usb_get_serial_port_data(port);
1334 kfree(priv); 1323 kfree(priv);
1335 1324
1336 return 0; 1325 return 0;
1337 } 1326 }
1338 1327
1339 static void digi_read_bulk_callback(struct urb *urb) 1328 static void digi_read_bulk_callback(struct urb *urb)
1340 { 1329 {
1341 struct usb_serial_port *port = urb->context; 1330 struct usb_serial_port *port = urb->context;
1342 struct digi_port *priv; 1331 struct digi_port *priv;
1343 struct digi_serial *serial_priv; 1332 struct digi_serial *serial_priv;
1344 int ret; 1333 int ret;
1345 int status = urb->status; 1334 int status = urb->status;
1346 1335
1347 /* port sanity check, do not resubmit if port is not valid */ 1336 /* port sanity check, do not resubmit if port is not valid */
1348 if (port == NULL) 1337 if (port == NULL)
1349 return; 1338 return;
1350 priv = usb_get_serial_port_data(port); 1339 priv = usb_get_serial_port_data(port);
1351 if (priv == NULL) { 1340 if (priv == NULL) {
1352 dev_err(&port->dev, "%s: port->private is NULL, status=%d\n", 1341 dev_err(&port->dev, "%s: port->private is NULL, status=%d\n",
1353 __func__, status); 1342 __func__, status);
1354 return; 1343 return;
1355 } 1344 }
1356 if (port->serial == NULL || 1345 if (port->serial == NULL ||
1357 (serial_priv = usb_get_serial_data(port->serial)) == NULL) { 1346 (serial_priv = usb_get_serial_data(port->serial)) == NULL) {
1358 dev_err(&port->dev, "%s: serial is bad or serial->private " 1347 dev_err(&port->dev, "%s: serial is bad or serial->private "
1359 "is NULL, status=%d\n", __func__, status); 1348 "is NULL, status=%d\n", __func__, status);
1360 return; 1349 return;
1361 } 1350 }
1362 1351
1363 /* do not resubmit urb if it has any status error */ 1352 /* do not resubmit urb if it has any status error */
1364 if (status) { 1353 if (status) {
1365 dev_err(&port->dev, 1354 dev_err(&port->dev,
1366 "%s: nonzero read bulk status: status=%d, port=%d\n", 1355 "%s: nonzero read bulk status: status=%d, port=%d\n",
1367 __func__, status, priv->dp_port_num); 1356 __func__, status, priv->dp_port_num);
1368 return; 1357 return;
1369 } 1358 }
1370 1359
1371 /* handle oob or inb callback, do not resubmit if error */ 1360 /* handle oob or inb callback, do not resubmit if error */
1372 if (priv->dp_port_num == serial_priv->ds_oob_port_num) { 1361 if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
1373 if (digi_read_oob_callback(urb) != 0) 1362 if (digi_read_oob_callback(urb) != 0)
1374 return; 1363 return;
1375 } else { 1364 } else {
1376 if (digi_read_inb_callback(urb) != 0) 1365 if (digi_read_inb_callback(urb) != 0)
1377 return; 1366 return;
1378 } 1367 }
1379 1368
1380 /* continue read */ 1369 /* continue read */
1381 ret = usb_submit_urb(urb, GFP_ATOMIC); 1370 ret = usb_submit_urb(urb, GFP_ATOMIC);
1382 if (ret != 0 && ret != -EPERM) { 1371 if (ret != 0 && ret != -EPERM) {
1383 dev_err(&port->dev, 1372 dev_err(&port->dev,
1384 "%s: failed resubmitting urb, ret=%d, port=%d\n", 1373 "%s: failed resubmitting urb, ret=%d, port=%d\n",
1385 __func__, ret, priv->dp_port_num); 1374 __func__, ret, priv->dp_port_num);
1386 } 1375 }
1387 1376
1388 } 1377 }
1389 1378
1390 /* 1379 /*
1391 * Digi Read INB Callback 1380 * Digi Read INB Callback
1392 * 1381 *
1393 * Digi Read INB Callback handles reads on the in band ports, sending 1382 * Digi Read INB Callback handles reads on the in band ports, sending
1394 * the data on to the tty subsystem. When called we know port and 1383 * the data on to the tty subsystem. When called we know port and
1395 * port->private are not NULL and port->serial has been validated. 1384 * port->private are not NULL and port->serial has been validated.
1396 * It returns 0 if successful, 1 if successful but the port is 1385 * It returns 0 if successful, 1 if successful but the port is
1397 * throttled, and -1 if the sanity checks failed. 1386 * throttled, and -1 if the sanity checks failed.
1398 */ 1387 */
1399 1388
1400 static int digi_read_inb_callback(struct urb *urb) 1389 static int digi_read_inb_callback(struct urb *urb)
1401 { 1390 {
1402 struct usb_serial_port *port = urb->context; 1391 struct usb_serial_port *port = urb->context;
1403 struct digi_port *priv = usb_get_serial_port_data(port); 1392 struct digi_port *priv = usb_get_serial_port_data(port);
1404 int opcode = ((unsigned char *)urb->transfer_buffer)[0]; 1393 int opcode = ((unsigned char *)urb->transfer_buffer)[0];
1405 int len = ((unsigned char *)urb->transfer_buffer)[1]; 1394 int len = ((unsigned char *)urb->transfer_buffer)[1];
1406 int port_status = ((unsigned char *)urb->transfer_buffer)[2]; 1395 int port_status = ((unsigned char *)urb->transfer_buffer)[2];
1407 unsigned char *data = ((unsigned char *)urb->transfer_buffer) + 3; 1396 unsigned char *data = ((unsigned char *)urb->transfer_buffer) + 3;
1408 int flag, throttled; 1397 int flag, throttled;
1409 int status = urb->status; 1398 int status = urb->status;
1410 1399
1411 /* do not process callbacks on closed ports */ 1400 /* do not process callbacks on closed ports */
1412 /* but do continue the read chain */ 1401 /* but do continue the read chain */
1413 if (urb->status == -ENOENT) 1402 if (urb->status == -ENOENT)
1414 return 0; 1403 return 0;
1415 1404
1416 /* short/multiple packet check */ 1405 /* short/multiple packet check */
1417 if (urb->actual_length != len + 2) { 1406 if (urb->actual_length != len + 2) {
1418 dev_err(&port->dev, "%s: INCOMPLETE OR MULTIPLE PACKET, " 1407 dev_err(&port->dev, "%s: INCOMPLETE OR MULTIPLE PACKET, "
1419 "status=%d, port=%d, opcode=%d, len=%d, " 1408 "status=%d, port=%d, opcode=%d, len=%d, "
1420 "actual_length=%d, status=%d\n", __func__, status, 1409 "actual_length=%d, status=%d\n", __func__, status,
1421 priv->dp_port_num, opcode, len, urb->actual_length, 1410 priv->dp_port_num, opcode, len, urb->actual_length,
1422 port_status); 1411 port_status);
1423 return -1; 1412 return -1;
1424 } 1413 }
1425 1414
1426 spin_lock(&priv->dp_port_lock); 1415 spin_lock(&priv->dp_port_lock);
1427 1416
1428 /* check for throttle; if set, do not resubmit read urb */ 1417 /* check for throttle; if set, do not resubmit read urb */
1429 /* indicate the read chain needs to be restarted on unthrottle */ 1418 /* indicate the read chain needs to be restarted on unthrottle */
1430 throttled = priv->dp_throttled; 1419 throttled = priv->dp_throttled;
1431 if (throttled) 1420 if (throttled)
1432 priv->dp_throttle_restart = 1; 1421 priv->dp_throttle_restart = 1;
1433 1422
1434 /* receive data */ 1423 /* receive data */
1435 if (opcode == DIGI_CMD_RECEIVE_DATA) { 1424 if (opcode == DIGI_CMD_RECEIVE_DATA) {
1436 /* get flag from port_status */ 1425 /* get flag from port_status */
1437 flag = 0; 1426 flag = 0;
1438 1427
1439 /* overrun is special, not associated with a char */ 1428 /* overrun is special, not associated with a char */
1440 if (port_status & DIGI_OVERRUN_ERROR) 1429 if (port_status & DIGI_OVERRUN_ERROR)
1441 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN); 1430 tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
1442 1431
1443 /* break takes precedence over parity, */ 1432 /* break takes precedence over parity, */
1444 /* which takes precedence over framing errors */ 1433 /* which takes precedence over framing errors */
1445 if (port_status & DIGI_BREAK_ERROR) 1434 if (port_status & DIGI_BREAK_ERROR)
1446 flag = TTY_BREAK; 1435 flag = TTY_BREAK;
1447 else if (port_status & DIGI_PARITY_ERROR) 1436 else if (port_status & DIGI_PARITY_ERROR)
1448 flag = TTY_PARITY; 1437 flag = TTY_PARITY;
1449 else if (port_status & DIGI_FRAMING_ERROR) 1438 else if (port_status & DIGI_FRAMING_ERROR)
1450 flag = TTY_FRAME; 1439 flag = TTY_FRAME;
1451 1440
1452 /* data length is len-1 (one byte of len is port_status) */ 1441 /* data length is len-1 (one byte of len is port_status) */
1453 --len; 1442 --len;
1454 if (len > 0) { 1443 if (len > 0) {
1455 tty_insert_flip_string_fixed_flag(&port->port, data, 1444 tty_insert_flip_string_fixed_flag(&port->port, data,
1456 flag, len); 1445 flag, len);
1457 tty_flip_buffer_push(&port->port); 1446 tty_flip_buffer_push(&port->port);
1458 } 1447 }
1459 } 1448 }
1460 spin_unlock(&priv->dp_port_lock); 1449 spin_unlock(&priv->dp_port_lock);
1461 1450
1462 if (opcode == DIGI_CMD_RECEIVE_DISABLE) 1451 if (opcode == DIGI_CMD_RECEIVE_DISABLE)
1463 dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__); 1452 dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__);
1464 else if (opcode != DIGI_CMD_RECEIVE_DATA) 1453 else if (opcode != DIGI_CMD_RECEIVE_DATA)
1465 dev_dbg(&port->dev, "%s: unknown opcode: %d\n", __func__, opcode); 1454 dev_dbg(&port->dev, "%s: unknown opcode: %d\n", __func__, opcode);
1466 1455
1467 return throttled ? 1 : 0; 1456 return throttled ? 1 : 0;
1468 1457
1469 } 1458 }
1470 1459
1471 1460
1472 /* 1461 /*
1473 * Digi Read OOB Callback 1462 * Digi Read OOB Callback
1474 * 1463 *
1475 * Digi Read OOB Callback handles reads on the out of band port. 1464 * Digi Read OOB Callback handles reads on the out of band port.
1476 * When called we know port and port->private are not NULL and 1465 * When called we know port and port->private are not NULL and
1477 * the port->serial is valid. It returns 0 if successful, and 1466 * the port->serial is valid. It returns 0 if successful, and
1478 * -1 if the sanity checks failed. 1467 * -1 if the sanity checks failed.
1479 */ 1468 */
1480 1469
1481 static int digi_read_oob_callback(struct urb *urb) 1470 static int digi_read_oob_callback(struct urb *urb)
1482 { 1471 {
1483 1472
1484 struct usb_serial_port *port = urb->context; 1473 struct usb_serial_port *port = urb->context;
1485 struct usb_serial *serial = port->serial; 1474 struct usb_serial *serial = port->serial;
1486 struct tty_struct *tty; 1475 struct tty_struct *tty;
1487 struct digi_port *priv = usb_get_serial_port_data(port); 1476 struct digi_port *priv = usb_get_serial_port_data(port);
1488 int opcode, line, status, val; 1477 int opcode, line, status, val;
1489 int i; 1478 int i;
1490 unsigned int rts; 1479 unsigned int rts;
1491 1480
1492 /* handle each oob command */ 1481 /* handle each oob command */
1493 for (i = 0; i < urb->actual_length - 3;) { 1482 for (i = 0; i < urb->actual_length - 3;) {
1494 opcode = ((unsigned char *)urb->transfer_buffer)[i++]; 1483 opcode = ((unsigned char *)urb->transfer_buffer)[i++];
1495 line = ((unsigned char *)urb->transfer_buffer)[i++]; 1484 line = ((unsigned char *)urb->transfer_buffer)[i++];
1496 status = ((unsigned char *)urb->transfer_buffer)[i++]; 1485 status = ((unsigned char *)urb->transfer_buffer)[i++];
1497 val = ((unsigned char *)urb->transfer_buffer)[i++]; 1486 val = ((unsigned char *)urb->transfer_buffer)[i++];
1498 1487
1499 dev_dbg(&port->dev, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n", 1488 dev_dbg(&port->dev, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n",
1500 opcode, line, status, val); 1489 opcode, line, status, val);
1501 1490
1502 if (status != 0 || line >= serial->type->num_ports) 1491 if (status != 0 || line >= serial->type->num_ports)
1503 continue; 1492 continue;
1504 1493
1505 port = serial->port[line]; 1494 port = serial->port[line];
1506 1495
1507 priv = usb_get_serial_port_data(port); 1496 priv = usb_get_serial_port_data(port);
1508 if (priv == NULL) 1497 if (priv == NULL)
1509 return -1; 1498 return -1;
1510 1499
1511 tty = tty_port_tty_get(&port->port); 1500 tty = tty_port_tty_get(&port->port);
1512 1501
1513 rts = 0; 1502 rts = 0;
1514 if (tty) 1503 if (tty)
1515 rts = tty->termios.c_cflag & CRTSCTS; 1504 rts = tty->termios.c_cflag & CRTSCTS;
1516 1505
1517 if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) { 1506 if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
1518 spin_lock(&priv->dp_port_lock); 1507 spin_lock(&priv->dp_port_lock);
1519 /* convert from digi flags to termiox flags */ 1508 /* convert from digi flags to termiox flags */
1520 if (val & DIGI_READ_INPUT_SIGNALS_CTS) { 1509 if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
1521 priv->dp_modem_signals |= TIOCM_CTS; 1510 priv->dp_modem_signals |= TIOCM_CTS;
1522 /* port must be open to use tty struct */ 1511 /* port must be open to use tty struct */
1523 if (rts) { 1512 if (rts) {
1524 tty->hw_stopped = 0; 1513 tty->hw_stopped = 0;
1525 digi_wakeup_write(port); 1514 tty_port_tty_wakeup(&port->port);
1526 } 1515 }
1527 } else { 1516 } else {
1528 priv->dp_modem_signals &= ~TIOCM_CTS; 1517 priv->dp_modem_signals &= ~TIOCM_CTS;
1529 /* port must be open to use tty struct */ 1518 /* port must be open to use tty struct */
1530 if (rts) 1519 if (rts)
1531 tty->hw_stopped = 1; 1520 tty->hw_stopped = 1;
1532 } 1521 }
1533 if (val & DIGI_READ_INPUT_SIGNALS_DSR) 1522 if (val & DIGI_READ_INPUT_SIGNALS_DSR)
1534 priv->dp_modem_signals |= TIOCM_DSR; 1523 priv->dp_modem_signals |= TIOCM_DSR;
1535 else 1524 else
1536 priv->dp_modem_signals &= ~TIOCM_DSR; 1525 priv->dp_modem_signals &= ~TIOCM_DSR;
1537 if (val & DIGI_READ_INPUT_SIGNALS_RI) 1526 if (val & DIGI_READ_INPUT_SIGNALS_RI)
1538 priv->dp_modem_signals |= TIOCM_RI; 1527 priv->dp_modem_signals |= TIOCM_RI;
1539 else 1528 else
1540 priv->dp_modem_signals &= ~TIOCM_RI; 1529 priv->dp_modem_signals &= ~TIOCM_RI;
1541 if (val & DIGI_READ_INPUT_SIGNALS_DCD) 1530 if (val & DIGI_READ_INPUT_SIGNALS_DCD)
1542 priv->dp_modem_signals |= TIOCM_CD; 1531 priv->dp_modem_signals |= TIOCM_CD;
1543 else 1532 else
1544 priv->dp_modem_signals &= ~TIOCM_CD; 1533 priv->dp_modem_signals &= ~TIOCM_CD;
1545 1534
1546 wake_up_interruptible(&priv->dp_modem_change_wait); 1535 wake_up_interruptible(&priv->dp_modem_change_wait);
1547 spin_unlock(&priv->dp_port_lock); 1536 spin_unlock(&priv->dp_port_lock);
1548 } else if (opcode == DIGI_CMD_TRANSMIT_IDLE) { 1537 } else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
1549 spin_lock(&priv->dp_port_lock); 1538 spin_lock(&priv->dp_port_lock);
1550 priv->dp_transmit_idle = 1; 1539 priv->dp_transmit_idle = 1;
1551 wake_up_interruptible(&priv->dp_transmit_idle_wait); 1540 wake_up_interruptible(&priv->dp_transmit_idle_wait);
1552 spin_unlock(&priv->dp_port_lock); 1541 spin_unlock(&priv->dp_port_lock);
1553 } else if (opcode == DIGI_CMD_IFLUSH_FIFO) { 1542 } else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
1554 wake_up_interruptible(&priv->dp_flush_wait); 1543 wake_up_interruptible(&priv->dp_flush_wait);
1555 } 1544 }
1556 tty_kref_put(tty); 1545 tty_kref_put(tty);
1557 } 1546 }
1558 return 0; 1547 return 0;
1559 1548
1560 } 1549 }
1561 1550
1562 module_usb_serial_driver(serial_drivers, id_table_combined); 1551 module_usb_serial_driver(serial_drivers, id_table_combined);
1563 1552
1564 MODULE_AUTHOR(DRIVER_AUTHOR); 1553 MODULE_AUTHOR(DRIVER_AUTHOR);
1565 MODULE_DESCRIPTION(DRIVER_DESC); 1554 MODULE_DESCRIPTION(DRIVER_DESC);
1566 MODULE_LICENSE("GPL"); 1555 MODULE_LICENSE("GPL");
1567 1556
drivers/usb/serial/io_edgeport.c
1 /* 1 /*
2 * Edgeport USB Serial Converter driver 2 * Edgeport USB Serial Converter driver
3 * 3 *
4 * Copyright (C) 2000 Inside Out Networks, All rights reserved. 4 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> 5 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 * 11 *
12 * Supports the following devices: 12 * Supports the following devices:
13 * Edgeport/4 13 * Edgeport/4
14 * Edgeport/4t 14 * Edgeport/4t
15 * Edgeport/2 15 * Edgeport/2
16 * Edgeport/4i 16 * Edgeport/4i
17 * Edgeport/2i 17 * Edgeport/2i
18 * Edgeport/421 18 * Edgeport/421
19 * Edgeport/21 19 * Edgeport/21
20 * Rapidport/4 20 * Rapidport/4
21 * Edgeport/8 21 * Edgeport/8
22 * Edgeport/2D8 22 * Edgeport/2D8
23 * Edgeport/4D8 23 * Edgeport/4D8
24 * Edgeport/8i 24 * Edgeport/8i
25 * 25 *
26 * For questions or problems with this driver, contact Inside Out 26 * For questions or problems with this driver, contact Inside Out
27 * Networks technical support, or Peter Berger <pberger@brimson.com>, 27 * Networks technical support, or Peter Berger <pberger@brimson.com>,
28 * or Al Borchers <alborchers@steinerpoint.com>. 28 * or Al Borchers <alborchers@steinerpoint.com>.
29 * 29 *
30 */ 30 */
31 31
32 #include <linux/kernel.h> 32 #include <linux/kernel.h>
33 #include <linux/jiffies.h> 33 #include <linux/jiffies.h>
34 #include <linux/errno.h> 34 #include <linux/errno.h>
35 #include <linux/init.h> 35 #include <linux/init.h>
36 #include <linux/slab.h> 36 #include <linux/slab.h>
37 #include <linux/tty.h> 37 #include <linux/tty.h>
38 #include <linux/tty_driver.h> 38 #include <linux/tty_driver.h>
39 #include <linux/tty_flip.h> 39 #include <linux/tty_flip.h>
40 #include <linux/module.h> 40 #include <linux/module.h>
41 #include <linux/spinlock.h> 41 #include <linux/spinlock.h>
42 #include <linux/serial.h> 42 #include <linux/serial.h>
43 #include <linux/ioctl.h> 43 #include <linux/ioctl.h>
44 #include <linux/wait.h> 44 #include <linux/wait.h>
45 #include <linux/firmware.h> 45 #include <linux/firmware.h>
46 #include <linux/ihex.h> 46 #include <linux/ihex.h>
47 #include <linux/uaccess.h> 47 #include <linux/uaccess.h>
48 #include <linux/usb.h> 48 #include <linux/usb.h>
49 #include <linux/usb/serial.h> 49 #include <linux/usb/serial.h>
50 #include "io_edgeport.h" 50 #include "io_edgeport.h"
51 #include "io_ionsp.h" /* info for the iosp messages */ 51 #include "io_ionsp.h" /* info for the iosp messages */
52 #include "io_16654.h" /* 16654 UART defines */ 52 #include "io_16654.h" /* 16654 UART defines */
53 53
54 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli" 54 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
55 #define DRIVER_DESC "Edgeport USB Serial Driver" 55 #define DRIVER_DESC "Edgeport USB Serial Driver"
56 56
57 #define MAX_NAME_LEN 64 57 #define MAX_NAME_LEN 64
58 58
59 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */ 59 #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
60 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */ 60 #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
61 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */ 61 #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
62 62
63 /* receive port state */ 63 /* receive port state */
64 enum RXSTATE { 64 enum RXSTATE {
65 EXPECT_HDR1 = 0, /* Expect header byte 1 */ 65 EXPECT_HDR1 = 0, /* Expect header byte 1 */
66 EXPECT_HDR2 = 1, /* Expect header byte 2 */ 66 EXPECT_HDR2 = 1, /* Expect header byte 2 */
67 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */ 67 EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
68 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */ 68 EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
69 }; 69 };
70 70
71 71
72 /* Transmit Fifo 72 /* Transmit Fifo
73 * This Transmit queue is an extension of the edgeport Rx buffer. 73 * This Transmit queue is an extension of the edgeport Rx buffer.
74 * The maximum amount of data buffered in both the edgeport 74 * The maximum amount of data buffered in both the edgeport
75 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits. 75 * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
76 */ 76 */
77 struct TxFifo { 77 struct TxFifo {
78 unsigned int head; /* index to head pointer (write) */ 78 unsigned int head; /* index to head pointer (write) */
79 unsigned int tail; /* index to tail pointer (read) */ 79 unsigned int tail; /* index to tail pointer (read) */
80 unsigned int count; /* Bytes in queue */ 80 unsigned int count; /* Bytes in queue */
81 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */ 81 unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
82 unsigned char *fifo; /* allocated Buffer */ 82 unsigned char *fifo; /* allocated Buffer */
83 }; 83 };
84 84
85 /* This structure holds all of the local port information */ 85 /* This structure holds all of the local port information */
86 struct edgeport_port { 86 struct edgeport_port {
87 __u16 txCredits; /* our current credits for this port */ 87 __u16 txCredits; /* our current credits for this port */
88 __u16 maxTxCredits; /* the max size of the port */ 88 __u16 maxTxCredits; /* the max size of the port */
89 89
90 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */ 90 struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
91 struct urb *write_urb; /* write URB for this port */ 91 struct urb *write_urb; /* write URB for this port */
92 bool write_in_progress; /* 'true' while a write URB is outstanding */ 92 bool write_in_progress; /* 'true' while a write URB is outstanding */
93 spinlock_t ep_lock; 93 spinlock_t ep_lock;
94 94
95 __u8 shadowLCR; /* last LCR value received */ 95 __u8 shadowLCR; /* last LCR value received */
96 __u8 shadowMCR; /* last MCR value received */ 96 __u8 shadowMCR; /* last MCR value received */
97 __u8 shadowMSR; /* last MSR value received */ 97 __u8 shadowMSR; /* last MSR value received */
98 __u8 shadowLSR; /* last LSR value received */ 98 __u8 shadowLSR; /* last LSR value received */
99 __u8 shadowXonChar; /* last value set as XON char in Edgeport */ 99 __u8 shadowXonChar; /* last value set as XON char in Edgeport */
100 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */ 100 __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
101 __u8 validDataMask; 101 __u8 validDataMask;
102 __u32 baudRate; 102 __u32 baudRate;
103 103
104 bool open; 104 bool open;
105 bool openPending; 105 bool openPending;
106 bool commandPending; 106 bool commandPending;
107 bool closePending; 107 bool closePending;
108 bool chaseResponsePending; 108 bool chaseResponsePending;
109 109
110 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */ 110 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
111 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */ 111 wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
112 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */ 112 wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
113 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */ 113 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
114 114
115 struct async_icount icount; 115 struct async_icount icount;
116 struct usb_serial_port *port; /* loop back to the owner of this object */ 116 struct usb_serial_port *port; /* loop back to the owner of this object */
117 }; 117 };
118 118
119 119
120 /* This structure holds all of the individual device information */ 120 /* This structure holds all of the individual device information */
121 struct edgeport_serial { 121 struct edgeport_serial {
122 char name[MAX_NAME_LEN+2]; /* string name of this device */ 122 char name[MAX_NAME_LEN+2]; /* string name of this device */
123 123
124 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */ 124 struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
125 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */ 125 struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
126 struct edgeport_product_info product_info; /* Product Info */ 126 struct edgeport_product_info product_info; /* Product Info */
127 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */ 127 struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
128 int is_epic; /* flag if EPiC device or not */ 128 int is_epic; /* flag if EPiC device or not */
129 129
130 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */ 130 __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
131 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */ 131 unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
132 struct urb *interrupt_read_urb; /* our interrupt urb */ 132 struct urb *interrupt_read_urb; /* our interrupt urb */
133 133
134 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */ 134 __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
135 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */ 135 unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
136 struct urb *read_urb; /* our bulk read urb */ 136 struct urb *read_urb; /* our bulk read urb */
137 bool read_in_progress; 137 bool read_in_progress;
138 spinlock_t es_lock; 138 spinlock_t es_lock;
139 139
140 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */ 140 __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
141 141
142 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */ 142 __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
143 143
144 enum RXSTATE rxState; /* the current state of the bulk receive processor */ 144 enum RXSTATE rxState; /* the current state of the bulk receive processor */
145 __u8 rxHeader1; /* receive header byte 1 */ 145 __u8 rxHeader1; /* receive header byte 1 */
146 __u8 rxHeader2; /* receive header byte 2 */ 146 __u8 rxHeader2; /* receive header byte 2 */
147 __u8 rxHeader3; /* receive header byte 3 */ 147 __u8 rxHeader3; /* receive header byte 3 */
148 __u8 rxPort; /* the port that we are currently receiving data for */ 148 __u8 rxPort; /* the port that we are currently receiving data for */
149 __u8 rxStatusCode; /* the receive status code */ 149 __u8 rxStatusCode; /* the receive status code */
150 __u8 rxStatusParam; /* the receive status paramater */ 150 __u8 rxStatusParam; /* the receive status paramater */
151 __s16 rxBytesRemaining; /* the number of port bytes left to read */ 151 __s16 rxBytesRemaining; /* the number of port bytes left to read */
152 struct usb_serial *serial; /* loop back to the owner of this object */ 152 struct usb_serial *serial; /* loop back to the owner of this object */
153 }; 153 };
154 154
155 /* baud rate information */ 155 /* baud rate information */
156 struct divisor_table_entry { 156 struct divisor_table_entry {
157 __u32 BaudRate; 157 __u32 BaudRate;
158 __u16 Divisor; 158 __u16 Divisor;
159 }; 159 };
160 160
161 /* 161 /*
162 * Define table of divisors for Rev A EdgePort/4 hardware 162 * Define table of divisors for Rev A EdgePort/4 hardware
163 * These assume a 3.6864MHz crystal, the standard /16, and 163 * These assume a 3.6864MHz crystal, the standard /16, and
164 * MCR.7 = 0. 164 * MCR.7 = 0.
165 */ 165 */
166 166
167 static const struct divisor_table_entry divisor_table[] = { 167 static const struct divisor_table_entry divisor_table[] = {
168 { 50, 4608}, 168 { 50, 4608},
169 { 75, 3072}, 169 { 75, 3072},
170 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */ 170 { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
171 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */ 171 { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
172 { 150, 1536}, 172 { 150, 1536},
173 { 300, 768}, 173 { 300, 768},
174 { 600, 384}, 174 { 600, 384},
175 { 1200, 192}, 175 { 1200, 192},
176 { 1800, 128}, 176 { 1800, 128},
177 { 2400, 96}, 177 { 2400, 96},
178 { 4800, 48}, 178 { 4800, 48},
179 { 7200, 32}, 179 { 7200, 32},
180 { 9600, 24}, 180 { 9600, 24},
181 { 14400, 16}, 181 { 14400, 16},
182 { 19200, 12}, 182 { 19200, 12},
183 { 38400, 6}, 183 { 38400, 6},
184 { 57600, 4}, 184 { 57600, 4},
185 { 115200, 2}, 185 { 115200, 2},
186 { 230400, 1}, 186 { 230400, 1},
187 }; 187 };
188 188
189 /* Number of outstanding Command Write Urbs */ 189 /* Number of outstanding Command Write Urbs */
190 static atomic_t CmdUrbs = ATOMIC_INIT(0); 190 static atomic_t CmdUrbs = ATOMIC_INIT(0);
191 191
192 192
193 /* local function prototypes */ 193 /* local function prototypes */
194 194
195 /* function prototypes for all URB callbacks */ 195 /* function prototypes for all URB callbacks */
196 static void edge_interrupt_callback(struct urb *urb); 196 static void edge_interrupt_callback(struct urb *urb);
197 static void edge_bulk_in_callback(struct urb *urb); 197 static void edge_bulk_in_callback(struct urb *urb);
198 static void edge_bulk_out_data_callback(struct urb *urb); 198 static void edge_bulk_out_data_callback(struct urb *urb);
199 static void edge_bulk_out_cmd_callback(struct urb *urb); 199 static void edge_bulk_out_cmd_callback(struct urb *urb);
200 200
201 /* function prototypes for the usbserial callbacks */ 201 /* function prototypes for the usbserial callbacks */
202 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port); 202 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
203 static void edge_close(struct usb_serial_port *port); 203 static void edge_close(struct usb_serial_port *port);
204 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port, 204 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
205 const unsigned char *buf, int count); 205 const unsigned char *buf, int count);
206 static int edge_write_room(struct tty_struct *tty); 206 static int edge_write_room(struct tty_struct *tty);
207 static int edge_chars_in_buffer(struct tty_struct *tty); 207 static int edge_chars_in_buffer(struct tty_struct *tty);
208 static void edge_throttle(struct tty_struct *tty); 208 static void edge_throttle(struct tty_struct *tty);
209 static void edge_unthrottle(struct tty_struct *tty); 209 static void edge_unthrottle(struct tty_struct *tty);
210 static void edge_set_termios(struct tty_struct *tty, 210 static void edge_set_termios(struct tty_struct *tty,
211 struct usb_serial_port *port, 211 struct usb_serial_port *port,
212 struct ktermios *old_termios); 212 struct ktermios *old_termios);
213 static int edge_ioctl(struct tty_struct *tty, 213 static int edge_ioctl(struct tty_struct *tty,
214 unsigned int cmd, unsigned long arg); 214 unsigned int cmd, unsigned long arg);
215 static void edge_break(struct tty_struct *tty, int break_state); 215 static void edge_break(struct tty_struct *tty, int break_state);
216 static int edge_tiocmget(struct tty_struct *tty); 216 static int edge_tiocmget(struct tty_struct *tty);
217 static int edge_tiocmset(struct tty_struct *tty, 217 static int edge_tiocmset(struct tty_struct *tty,
218 unsigned int set, unsigned int clear); 218 unsigned int set, unsigned int clear);
219 static int edge_get_icount(struct tty_struct *tty, 219 static int edge_get_icount(struct tty_struct *tty,
220 struct serial_icounter_struct *icount); 220 struct serial_icounter_struct *icount);
221 static int edge_startup(struct usb_serial *serial); 221 static int edge_startup(struct usb_serial *serial);
222 static void edge_disconnect(struct usb_serial *serial); 222 static void edge_disconnect(struct usb_serial *serial);
223 static void edge_release(struct usb_serial *serial); 223 static void edge_release(struct usb_serial *serial);
224 static int edge_port_probe(struct usb_serial_port *port); 224 static int edge_port_probe(struct usb_serial_port *port);
225 static int edge_port_remove(struct usb_serial_port *port); 225 static int edge_port_remove(struct usb_serial_port *port);
226 226
227 #include "io_tables.h" /* all of the devices that this driver supports */ 227 #include "io_tables.h" /* all of the devices that this driver supports */
228 228
229 /* function prototypes for all of our local functions */ 229 /* function prototypes for all of our local functions */
230 230
231 static void process_rcvd_data(struct edgeport_serial *edge_serial, 231 static void process_rcvd_data(struct edgeport_serial *edge_serial,
232 unsigned char *buffer, __u16 bufferLength); 232 unsigned char *buffer, __u16 bufferLength);
233 static void process_rcvd_status(struct edgeport_serial *edge_serial, 233 static void process_rcvd_status(struct edgeport_serial *edge_serial,
234 __u8 byte2, __u8 byte3); 234 __u8 byte2, __u8 byte3);
235 static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data, 235 static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
236 int length); 236 int length);
237 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr); 237 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
238 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, 238 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
239 __u8 lsr, __u8 data); 239 __u8 lsr, __u8 data);
240 static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command, 240 static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
241 __u8 param); 241 __u8 param);
242 static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor); 242 static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
243 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port, 243 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
244 int baudRate); 244 int baudRate);
245 static void change_port_settings(struct tty_struct *tty, 245 static void change_port_settings(struct tty_struct *tty,
246 struct edgeport_port *edge_port, 246 struct edgeport_port *edge_port,
247 struct ktermios *old_termios); 247 struct ktermios *old_termios);
248 static int send_cmd_write_uart_register(struct edgeport_port *edge_port, 248 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
249 __u8 regNum, __u8 regValue); 249 __u8 regNum, __u8 regValue);
250 static int write_cmd_usb(struct edgeport_port *edge_port, 250 static int write_cmd_usb(struct edgeport_port *edge_port,
251 unsigned char *buffer, int writeLength); 251 unsigned char *buffer, int writeLength);
252 static void send_more_port_data(struct edgeport_serial *edge_serial, 252 static void send_more_port_data(struct edgeport_serial *edge_serial,
253 struct edgeport_port *edge_port); 253 struct edgeport_port *edge_port);
254 254
255 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr, 255 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
256 __u16 length, const __u8 *data); 256 __u16 length, const __u8 *data);
257 static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr, 257 static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
258 __u16 length, __u8 *data); 258 __u16 length, __u8 *data);
259 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr, 259 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
260 __u16 length, const __u8 *data); 260 __u16 length, const __u8 *data);
261 static void get_manufacturing_desc(struct edgeport_serial *edge_serial); 261 static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
262 static void get_boot_desc(struct edgeport_serial *edge_serial); 262 static void get_boot_desc(struct edgeport_serial *edge_serial);
263 static void load_application_firmware(struct edgeport_serial *edge_serial); 263 static void load_application_firmware(struct edgeport_serial *edge_serial);
264 264
265 static void unicode_to_ascii(char *string, int buflen, 265 static void unicode_to_ascii(char *string, int buflen,
266 __le16 *unicode, int unicode_size); 266 __le16 *unicode, int unicode_size);
267 267
268 268
269 /* ************************************************************************ */ 269 /* ************************************************************************ */
270 /* ************************************************************************ */ 270 /* ************************************************************************ */
271 /* ************************************************************************ */ 271 /* ************************************************************************ */
272 /* ************************************************************************ */ 272 /* ************************************************************************ */
273 273
274 /************************************************************************ 274 /************************************************************************
275 * * 275 * *
276 * update_edgeport_E2PROM() Compare current versions of * 276 * update_edgeport_E2PROM() Compare current versions of *
277 * Boot ROM and Manufacture * 277 * Boot ROM and Manufacture *
278 * Descriptors with versions * 278 * Descriptors with versions *
279 * embedded in this driver * 279 * embedded in this driver *
280 * * 280 * *
281 ************************************************************************/ 281 ************************************************************************/
282 static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial) 282 static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
283 { 283 {
284 struct device *dev = &edge_serial->serial->dev->dev; 284 struct device *dev = &edge_serial->serial->dev->dev;
285 __u32 BootCurVer; 285 __u32 BootCurVer;
286 __u32 BootNewVer; 286 __u32 BootNewVer;
287 __u8 BootMajorVersion; 287 __u8 BootMajorVersion;
288 __u8 BootMinorVersion; 288 __u8 BootMinorVersion;
289 __u16 BootBuildNumber; 289 __u16 BootBuildNumber;
290 __u32 Bootaddr; 290 __u32 Bootaddr;
291 const struct ihex_binrec *rec; 291 const struct ihex_binrec *rec;
292 const struct firmware *fw; 292 const struct firmware *fw;
293 const char *fw_name; 293 const char *fw_name;
294 int response; 294 int response;
295 295
296 switch (edge_serial->product_info.iDownloadFile) { 296 switch (edge_serial->product_info.iDownloadFile) {
297 case EDGE_DOWNLOAD_FILE_I930: 297 case EDGE_DOWNLOAD_FILE_I930:
298 fw_name = "edgeport/boot.fw"; 298 fw_name = "edgeport/boot.fw";
299 break; 299 break;
300 case EDGE_DOWNLOAD_FILE_80251: 300 case EDGE_DOWNLOAD_FILE_80251:
301 fw_name = "edgeport/boot2.fw"; 301 fw_name = "edgeport/boot2.fw";
302 break; 302 break;
303 default: 303 default:
304 return; 304 return;
305 } 305 }
306 306
307 response = request_ihex_firmware(&fw, fw_name, 307 response = request_ihex_firmware(&fw, fw_name,
308 &edge_serial->serial->dev->dev); 308 &edge_serial->serial->dev->dev);
309 if (response) { 309 if (response) {
310 dev_err(dev, "Failed to load image \"%s\" err %d\n", 310 dev_err(dev, "Failed to load image \"%s\" err %d\n",
311 fw_name, response); 311 fw_name, response);
312 return; 312 return;
313 } 313 }
314 314
315 rec = (const struct ihex_binrec *)fw->data; 315 rec = (const struct ihex_binrec *)fw->data;
316 BootMajorVersion = rec->data[0]; 316 BootMajorVersion = rec->data[0];
317 BootMinorVersion = rec->data[1]; 317 BootMinorVersion = rec->data[1];
318 BootBuildNumber = (rec->data[2] << 8) | rec->data[3]; 318 BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
319 319
320 /* Check Boot Image Version */ 320 /* Check Boot Image Version */
321 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) + 321 BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
322 (edge_serial->boot_descriptor.MinorVersion << 16) + 322 (edge_serial->boot_descriptor.MinorVersion << 16) +
323 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber); 323 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
324 324
325 BootNewVer = (BootMajorVersion << 24) + 325 BootNewVer = (BootMajorVersion << 24) +
326 (BootMinorVersion << 16) + 326 (BootMinorVersion << 16) +
327 BootBuildNumber; 327 BootBuildNumber;
328 328
329 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n", 329 dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
330 edge_serial->boot_descriptor.MajorVersion, 330 edge_serial->boot_descriptor.MajorVersion,
331 edge_serial->boot_descriptor.MinorVersion, 331 edge_serial->boot_descriptor.MinorVersion,
332 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber)); 332 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
333 333
334 334
335 if (BootNewVer > BootCurVer) { 335 if (BootNewVer > BootCurVer) {
336 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n", 336 dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
337 edge_serial->boot_descriptor.MajorVersion, 337 edge_serial->boot_descriptor.MajorVersion,
338 edge_serial->boot_descriptor.MinorVersion, 338 edge_serial->boot_descriptor.MinorVersion,
339 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber), 339 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
340 BootMajorVersion, BootMinorVersion, BootBuildNumber); 340 BootMajorVersion, BootMinorVersion, BootBuildNumber);
341 341
342 dev_dbg(dev, "Downloading new Boot Image\n"); 342 dev_dbg(dev, "Downloading new Boot Image\n");
343 343
344 for (rec = ihex_next_binrec(rec); rec; 344 for (rec = ihex_next_binrec(rec); rec;
345 rec = ihex_next_binrec(rec)) { 345 rec = ihex_next_binrec(rec)) {
346 Bootaddr = be32_to_cpu(rec->addr); 346 Bootaddr = be32_to_cpu(rec->addr);
347 response = rom_write(edge_serial->serial, 347 response = rom_write(edge_serial->serial,
348 Bootaddr >> 16, 348 Bootaddr >> 16,
349 Bootaddr & 0xFFFF, 349 Bootaddr & 0xFFFF,
350 be16_to_cpu(rec->len), 350 be16_to_cpu(rec->len),
351 &rec->data[0]); 351 &rec->data[0]);
352 if (response < 0) { 352 if (response < 0) {
353 dev_err(&edge_serial->serial->dev->dev, 353 dev_err(&edge_serial->serial->dev->dev,
354 "rom_write failed (%x, %x, %d)\n", 354 "rom_write failed (%x, %x, %d)\n",
355 Bootaddr >> 16, Bootaddr & 0xFFFF, 355 Bootaddr >> 16, Bootaddr & 0xFFFF,
356 be16_to_cpu(rec->len)); 356 be16_to_cpu(rec->len));
357 break; 357 break;
358 } 358 }
359 } 359 }
360 } else { 360 } else {
361 dev_dbg(dev, "Boot Image -- already up to date\n"); 361 dev_dbg(dev, "Boot Image -- already up to date\n");
362 } 362 }
363 release_firmware(fw); 363 release_firmware(fw);
364 } 364 }
365 365
366 #if 0 366 #if 0
367 /************************************************************************ 367 /************************************************************************
368 * 368 *
369 * Get string descriptor from device 369 * Get string descriptor from device
370 * 370 *
371 ************************************************************************/ 371 ************************************************************************/
372 static int get_string_desc(struct usb_device *dev, int Id, 372 static int get_string_desc(struct usb_device *dev, int Id,
373 struct usb_string_descriptor **pRetDesc) 373 struct usb_string_descriptor **pRetDesc)
374 { 374 {
375 struct usb_string_descriptor StringDesc; 375 struct usb_string_descriptor StringDesc;
376 struct usb_string_descriptor *pStringDesc; 376 struct usb_string_descriptor *pStringDesc;
377 377
378 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id); 378 dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
379 379
380 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc, 380 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
381 sizeof(StringDesc))) 381 sizeof(StringDesc)))
382 return 0; 382 return 0;
383 383
384 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL); 384 pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
385 if (!pStringDesc) 385 if (!pStringDesc)
386 return -1; 386 return -1;
387 387
388 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc, 388 if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
389 StringDesc.bLength)) { 389 StringDesc.bLength)) {
390 kfree(pStringDesc); 390 kfree(pStringDesc);
391 return -1; 391 return -1;
392 } 392 }
393 393
394 *pRetDesc = pStringDesc; 394 *pRetDesc = pStringDesc;
395 return 0; 395 return 0;
396 } 396 }
397 #endif 397 #endif
398 398
399 static void dump_product_info(struct edgeport_serial *edge_serial, 399 static void dump_product_info(struct edgeport_serial *edge_serial,
400 struct edgeport_product_info *product_info) 400 struct edgeport_product_info *product_info)
401 { 401 {
402 struct device *dev = &edge_serial->serial->dev->dev; 402 struct device *dev = &edge_serial->serial->dev->dev;
403 403
404 /* Dump Product Info structure */ 404 /* Dump Product Info structure */
405 dev_dbg(dev, "**Product Information:\n"); 405 dev_dbg(dev, "**Product Information:\n");
406 dev_dbg(dev, " ProductId %x\n", product_info->ProductId); 406 dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
407 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts); 407 dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
408 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer); 408 dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
409 dev_dbg(dev, " IsServer %d\n", product_info->IsServer); 409 dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
410 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232); 410 dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
411 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422); 411 dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
412 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485); 412 dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
413 dev_dbg(dev, " RomSize %d\n", product_info->RomSize); 413 dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
414 dev_dbg(dev, " RamSize %d\n", product_info->RamSize); 414 dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
415 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev); 415 dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
416 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev); 416 dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
417 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n", 417 dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
418 product_info->BootMajorVersion, 418 product_info->BootMajorVersion,
419 product_info->BootMinorVersion, 419 product_info->BootMinorVersion,
420 le16_to_cpu(product_info->BootBuildNumber)); 420 le16_to_cpu(product_info->BootBuildNumber));
421 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n", 421 dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
422 product_info->FirmwareMajorVersion, 422 product_info->FirmwareMajorVersion,
423 product_info->FirmwareMinorVersion, 423 product_info->FirmwareMinorVersion,
424 le16_to_cpu(product_info->FirmwareBuildNumber)); 424 le16_to_cpu(product_info->FirmwareBuildNumber));
425 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n", 425 dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
426 product_info->ManufactureDescDate[0], 426 product_info->ManufactureDescDate[0],
427 product_info->ManufactureDescDate[1], 427 product_info->ManufactureDescDate[1],
428 product_info->ManufactureDescDate[2]+1900); 428 product_info->ManufactureDescDate[2]+1900);
429 dev_dbg(dev, " iDownloadFile 0x%x\n", 429 dev_dbg(dev, " iDownloadFile 0x%x\n",
430 product_info->iDownloadFile); 430 product_info->iDownloadFile);
431 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer); 431 dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
432 } 432 }
433 433
434 static void get_product_info(struct edgeport_serial *edge_serial) 434 static void get_product_info(struct edgeport_serial *edge_serial)
435 { 435 {
436 struct edgeport_product_info *product_info = &edge_serial->product_info; 436 struct edgeport_product_info *product_info = &edge_serial->product_info;
437 437
438 memset(product_info, 0, sizeof(struct edgeport_product_info)); 438 memset(product_info, 0, sizeof(struct edgeport_product_info));
439 439
440 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP); 440 product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
441 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts; 441 product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
442 product_info->ProdInfoVer = 0; 442 product_info->ProdInfoVer = 0;
443 443
444 product_info->RomSize = edge_serial->manuf_descriptor.RomSize; 444 product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
445 product_info->RamSize = edge_serial->manuf_descriptor.RamSize; 445 product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
446 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev; 446 product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
447 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev; 447 product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
448 448
449 product_info->BootMajorVersion = 449 product_info->BootMajorVersion =
450 edge_serial->boot_descriptor.MajorVersion; 450 edge_serial->boot_descriptor.MajorVersion;
451 product_info->BootMinorVersion = 451 product_info->BootMinorVersion =
452 edge_serial->boot_descriptor.MinorVersion; 452 edge_serial->boot_descriptor.MinorVersion;
453 product_info->BootBuildNumber = 453 product_info->BootBuildNumber =
454 edge_serial->boot_descriptor.BuildNumber; 454 edge_serial->boot_descriptor.BuildNumber;
455 455
456 memcpy(product_info->ManufactureDescDate, 456 memcpy(product_info->ManufactureDescDate,
457 edge_serial->manuf_descriptor.DescDate, 457 edge_serial->manuf_descriptor.DescDate,
458 sizeof(edge_serial->manuf_descriptor.DescDate)); 458 sizeof(edge_serial->manuf_descriptor.DescDate));
459 459
460 /* check if this is 2nd generation hardware */ 460 /* check if this is 2nd generation hardware */
461 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) 461 if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
462 & ION_DEVICE_ID_80251_NETCHIP) 462 & ION_DEVICE_ID_80251_NETCHIP)
463 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251; 463 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
464 else 464 else
465 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930; 465 product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
466 466
467 /* Determine Product type and set appropriate flags */ 467 /* Determine Product type and set appropriate flags */
468 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) { 468 switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
469 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE: 469 case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
470 case ION_DEVICE_ID_EDGEPORT_4T: 470 case ION_DEVICE_ID_EDGEPORT_4T:
471 case ION_DEVICE_ID_EDGEPORT_4: 471 case ION_DEVICE_ID_EDGEPORT_4:
472 case ION_DEVICE_ID_EDGEPORT_2: 472 case ION_DEVICE_ID_EDGEPORT_2:
473 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU: 473 case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
474 case ION_DEVICE_ID_EDGEPORT_8: 474 case ION_DEVICE_ID_EDGEPORT_8:
475 case ION_DEVICE_ID_EDGEPORT_421: 475 case ION_DEVICE_ID_EDGEPORT_421:
476 case ION_DEVICE_ID_EDGEPORT_21: 476 case ION_DEVICE_ID_EDGEPORT_21:
477 case ION_DEVICE_ID_EDGEPORT_2_DIN: 477 case ION_DEVICE_ID_EDGEPORT_2_DIN:
478 case ION_DEVICE_ID_EDGEPORT_4_DIN: 478 case ION_DEVICE_ID_EDGEPORT_4_DIN:
479 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU: 479 case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
480 product_info->IsRS232 = 1; 480 product_info->IsRS232 = 1;
481 break; 481 break;
482 482
483 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */ 483 case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
484 product_info->IsRS422 = 1; 484 product_info->IsRS422 = 1;
485 product_info->IsRS485 = 1; 485 product_info->IsRS485 = 1;
486 break; 486 break;
487 487
488 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */ 488 case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
489 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */ 489 case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
490 product_info->IsRS422 = 1; 490 product_info->IsRS422 = 1;
491 break; 491 break;
492 } 492 }
493 493
494 dump_product_info(edge_serial, product_info); 494 dump_product_info(edge_serial, product_info);
495 } 495 }
496 496
497 static int get_epic_descriptor(struct edgeport_serial *ep) 497 static int get_epic_descriptor(struct edgeport_serial *ep)
498 { 498 {
499 int result; 499 int result;
500 struct usb_serial *serial = ep->serial; 500 struct usb_serial *serial = ep->serial;
501 struct edgeport_product_info *product_info = &ep->product_info; 501 struct edgeport_product_info *product_info = &ep->product_info;
502 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor; 502 struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
503 struct edge_compatibility_bits *bits; 503 struct edge_compatibility_bits *bits;
504 struct device *dev = &serial->dev->dev; 504 struct device *dev = &serial->dev->dev;
505 505
506 ep->is_epic = 0; 506 ep->is_epic = 0;
507 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 507 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
508 USB_REQUEST_ION_GET_EPIC_DESC, 508 USB_REQUEST_ION_GET_EPIC_DESC,
509 0xC0, 0x00, 0x00, 509 0xC0, 0x00, 0x00,
510 &ep->epic_descriptor, 510 &ep->epic_descriptor,
511 sizeof(struct edge_compatibility_descriptor), 511 sizeof(struct edge_compatibility_descriptor),
512 300); 512 300);
513 513
514 if (result > 0) { 514 if (result > 0) {
515 ep->is_epic = 1; 515 ep->is_epic = 1;
516 memset(product_info, 0, sizeof(struct edgeport_product_info)); 516 memset(product_info, 0, sizeof(struct edgeport_product_info));
517 517
518 product_info->NumPorts = epic->NumPorts; 518 product_info->NumPorts = epic->NumPorts;
519 product_info->ProdInfoVer = 0; 519 product_info->ProdInfoVer = 0;
520 product_info->FirmwareMajorVersion = epic->MajorVersion; 520 product_info->FirmwareMajorVersion = epic->MajorVersion;
521 product_info->FirmwareMinorVersion = epic->MinorVersion; 521 product_info->FirmwareMinorVersion = epic->MinorVersion;
522 product_info->FirmwareBuildNumber = epic->BuildNumber; 522 product_info->FirmwareBuildNumber = epic->BuildNumber;
523 product_info->iDownloadFile = epic->iDownloadFile; 523 product_info->iDownloadFile = epic->iDownloadFile;
524 product_info->EpicVer = epic->EpicVer; 524 product_info->EpicVer = epic->EpicVer;
525 product_info->Epic = epic->Supports; 525 product_info->Epic = epic->Supports;
526 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE; 526 product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
527 dump_product_info(ep, product_info); 527 dump_product_info(ep, product_info);
528 528
529 bits = &ep->epic_descriptor.Supports; 529 bits = &ep->epic_descriptor.Supports;
530 dev_dbg(dev, "**EPIC descriptor:\n"); 530 dev_dbg(dev, "**EPIC descriptor:\n");
531 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE"); 531 dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
532 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE"); 532 dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
533 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE"); 533 dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
534 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE"); 534 dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
535 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE"); 535 dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
536 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE"); 536 dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
537 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE"); 537 dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
538 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE"); 538 dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
539 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE"); 539 dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
540 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE"); 540 dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
541 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE"); 541 dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
542 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE"); 542 dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
543 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE"); 543 dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
544 } 544 }
545 545
546 return result; 546 return result;
547 } 547 }
548 548
549 549
550 /************************************************************************/ 550 /************************************************************************/
551 /************************************************************************/ 551 /************************************************************************/
552 /* U S B C A L L B A C K F U N C T I O N S */ 552 /* U S B C A L L B A C K F U N C T I O N S */
553 /* U S B C A L L B A C K F U N C T I O N S */ 553 /* U S B C A L L B A C K F U N C T I O N S */
554 /************************************************************************/ 554 /************************************************************************/
555 /************************************************************************/ 555 /************************************************************************/
556 556
557 /***************************************************************************** 557 /*****************************************************************************
558 * edge_interrupt_callback 558 * edge_interrupt_callback
559 * this is the callback function for when we have received data on the 559 * this is the callback function for when we have received data on the
560 * interrupt endpoint. 560 * interrupt endpoint.
561 *****************************************************************************/ 561 *****************************************************************************/
562 static void edge_interrupt_callback(struct urb *urb) 562 static void edge_interrupt_callback(struct urb *urb)
563 { 563 {
564 struct edgeport_serial *edge_serial = urb->context; 564 struct edgeport_serial *edge_serial = urb->context;
565 struct device *dev; 565 struct device *dev;
566 struct edgeport_port *edge_port; 566 struct edgeport_port *edge_port;
567 struct usb_serial_port *port; 567 struct usb_serial_port *port;
568 struct tty_struct *tty;
569 unsigned char *data = urb->transfer_buffer; 568 unsigned char *data = urb->transfer_buffer;
570 int length = urb->actual_length; 569 int length = urb->actual_length;
571 int bytes_avail; 570 int bytes_avail;
572 int position; 571 int position;
573 int txCredits; 572 int txCredits;
574 int portNumber; 573 int portNumber;
575 int result; 574 int result;
576 int status = urb->status; 575 int status = urb->status;
577 576
578 switch (status) { 577 switch (status) {
579 case 0: 578 case 0:
580 /* success */ 579 /* success */
581 break; 580 break;
582 case -ECONNRESET: 581 case -ECONNRESET:
583 case -ENOENT: 582 case -ENOENT:
584 case -ESHUTDOWN: 583 case -ESHUTDOWN:
585 /* this urb is terminated, clean up */ 584 /* this urb is terminated, clean up */
586 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status); 585 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
587 return; 586 return;
588 default: 587 default:
589 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status); 588 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
590 goto exit; 589 goto exit;
591 } 590 }
592 591
593 dev = &edge_serial->serial->dev->dev; 592 dev = &edge_serial->serial->dev->dev;
594 593
595 /* process this interrupt-read even if there are no ports open */ 594 /* process this interrupt-read even if there are no ports open */
596 if (length) { 595 if (length) {
597 usb_serial_debug_data(dev, __func__, length, data); 596 usb_serial_debug_data(dev, __func__, length, data);
598 597
599 if (length > 1) { 598 if (length > 1) {
600 bytes_avail = data[0] | (data[1] << 8); 599 bytes_avail = data[0] | (data[1] << 8);
601 if (bytes_avail) { 600 if (bytes_avail) {
602 spin_lock(&edge_serial->es_lock); 601 spin_lock(&edge_serial->es_lock);
603 edge_serial->rxBytesAvail += bytes_avail; 602 edge_serial->rxBytesAvail += bytes_avail;
604 dev_dbg(dev, 603 dev_dbg(dev,
605 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n", 604 "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
606 __func__, bytes_avail, 605 __func__, bytes_avail,
607 edge_serial->rxBytesAvail, 606 edge_serial->rxBytesAvail,
608 edge_serial->read_in_progress); 607 edge_serial->read_in_progress);
609 608
610 if (edge_serial->rxBytesAvail > 0 && 609 if (edge_serial->rxBytesAvail > 0 &&
611 !edge_serial->read_in_progress) { 610 !edge_serial->read_in_progress) {
612 dev_dbg(dev, "%s - posting a read\n", __func__); 611 dev_dbg(dev, "%s - posting a read\n", __func__);
613 edge_serial->read_in_progress = true; 612 edge_serial->read_in_progress = true;
614 613
615 /* we have pending bytes on the 614 /* we have pending bytes on the
616 bulk in pipe, send a request */ 615 bulk in pipe, send a request */
617 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC); 616 result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
618 if (result) { 617 if (result) {
619 dev_err(dev, 618 dev_err(dev,
620 "%s - usb_submit_urb(read bulk) failed with result = %d\n", 619 "%s - usb_submit_urb(read bulk) failed with result = %d\n",
621 __func__, result); 620 __func__, result);
622 edge_serial->read_in_progress = false; 621 edge_serial->read_in_progress = false;
623 } 622 }
624 } 623 }
625 spin_unlock(&edge_serial->es_lock); 624 spin_unlock(&edge_serial->es_lock);
626 } 625 }
627 } 626 }
628 /* grab the txcredits for the ports if available */ 627 /* grab the txcredits for the ports if available */
629 position = 2; 628 position = 2;
630 portNumber = 0; 629 portNumber = 0;
631 while ((position < length) && 630 while ((position < length) &&
632 (portNumber < edge_serial->serial->num_ports)) { 631 (portNumber < edge_serial->serial->num_ports)) {
633 txCredits = data[position] | (data[position+1] << 8); 632 txCredits = data[position] | (data[position+1] << 8);
634 if (txCredits) { 633 if (txCredits) {
635 port = edge_serial->serial->port[portNumber]; 634 port = edge_serial->serial->port[portNumber];
636 edge_port = usb_get_serial_port_data(port); 635 edge_port = usb_get_serial_port_data(port);
637 if (edge_port->open) { 636 if (edge_port->open) {
638 spin_lock(&edge_port->ep_lock); 637 spin_lock(&edge_port->ep_lock);
639 edge_port->txCredits += txCredits; 638 edge_port->txCredits += txCredits;
640 spin_unlock(&edge_port->ep_lock); 639 spin_unlock(&edge_port->ep_lock);
641 dev_dbg(dev, "%s - txcredits for port%d = %d\n", 640 dev_dbg(dev, "%s - txcredits for port%d = %d\n",
642 __func__, portNumber, 641 __func__, portNumber,
643 edge_port->txCredits); 642 edge_port->txCredits);
644 643
645 /* tell the tty driver that something 644 /* tell the tty driver that something
646 has changed */ 645 has changed */
647 tty = tty_port_tty_get( 646 tty_port_tty_wakeup(&edge_port->port->port);
648 &edge_port->port->port);
649 if (tty) {
650 tty_wakeup(tty);
651 tty_kref_put(tty);
652 }
653 /* Since we have more credit, check 647 /* Since we have more credit, check
654 if more data can be sent */ 648 if more data can be sent */
655 send_more_port_data(edge_serial, 649 send_more_port_data(edge_serial,
656 edge_port); 650 edge_port);
657 } 651 }
658 } 652 }
659 position += 2; 653 position += 2;
660 ++portNumber; 654 ++portNumber;
661 } 655 }
662 } 656 }
663 657
664 exit: 658 exit:
665 result = usb_submit_urb(urb, GFP_ATOMIC); 659 result = usb_submit_urb(urb, GFP_ATOMIC);
666 if (result) 660 if (result)
667 dev_err(&urb->dev->dev, 661 dev_err(&urb->dev->dev,
668 "%s - Error %d submitting control urb\n", 662 "%s - Error %d submitting control urb\n",
669 __func__, result); 663 __func__, result);
670 } 664 }
671 665
672 666
673 /***************************************************************************** 667 /*****************************************************************************
674 * edge_bulk_in_callback 668 * edge_bulk_in_callback
675 * this is the callback function for when we have received data on the 669 * this is the callback function for when we have received data on the
676 * bulk in endpoint. 670 * bulk in endpoint.
677 *****************************************************************************/ 671 *****************************************************************************/
678 static void edge_bulk_in_callback(struct urb *urb) 672 static void edge_bulk_in_callback(struct urb *urb)
679 { 673 {
680 struct edgeport_serial *edge_serial = urb->context; 674 struct edgeport_serial *edge_serial = urb->context;
681 struct device *dev; 675 struct device *dev;
682 unsigned char *data = urb->transfer_buffer; 676 unsigned char *data = urb->transfer_buffer;
683 int retval; 677 int retval;
684 __u16 raw_data_length; 678 __u16 raw_data_length;
685 int status = urb->status; 679 int status = urb->status;
686 680
687 if (status) { 681 if (status) {
688 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", 682 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
689 __func__, status); 683 __func__, status);
690 edge_serial->read_in_progress = false; 684 edge_serial->read_in_progress = false;
691 return; 685 return;
692 } 686 }
693 687
694 if (urb->actual_length == 0) { 688 if (urb->actual_length == 0) {
695 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__); 689 dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
696 edge_serial->read_in_progress = false; 690 edge_serial->read_in_progress = false;
697 return; 691 return;
698 } 692 }
699 693
700 dev = &edge_serial->serial->dev->dev; 694 dev = &edge_serial->serial->dev->dev;
701 raw_data_length = urb->actual_length; 695 raw_data_length = urb->actual_length;
702 696
703 usb_serial_debug_data(dev, __func__, raw_data_length, data); 697 usb_serial_debug_data(dev, __func__, raw_data_length, data);
704 698
705 spin_lock(&edge_serial->es_lock); 699 spin_lock(&edge_serial->es_lock);
706 700
707 /* decrement our rxBytes available by the number that we just got */ 701 /* decrement our rxBytes available by the number that we just got */
708 edge_serial->rxBytesAvail -= raw_data_length; 702 edge_serial->rxBytesAvail -= raw_data_length;
709 703
710 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__, 704 dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
711 raw_data_length, edge_serial->rxBytesAvail); 705 raw_data_length, edge_serial->rxBytesAvail);
712 706
713 process_rcvd_data(edge_serial, data, urb->actual_length); 707 process_rcvd_data(edge_serial, data, urb->actual_length);
714 708
715 /* check to see if there's any more data for us to read */ 709 /* check to see if there's any more data for us to read */
716 if (edge_serial->rxBytesAvail > 0) { 710 if (edge_serial->rxBytesAvail > 0) {
717 dev_dbg(dev, "%s - posting a read\n", __func__); 711 dev_dbg(dev, "%s - posting a read\n", __func__);
718 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC); 712 retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
719 if (retval) { 713 if (retval) {
720 dev_err(dev, 714 dev_err(dev,
721 "%s - usb_submit_urb(read bulk) failed, retval = %d\n", 715 "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
722 __func__, retval); 716 __func__, retval);
723 edge_serial->read_in_progress = false; 717 edge_serial->read_in_progress = false;
724 } 718 }
725 } else { 719 } else {
726 edge_serial->read_in_progress = false; 720 edge_serial->read_in_progress = false;
727 } 721 }
728 722
729 spin_unlock(&edge_serial->es_lock); 723 spin_unlock(&edge_serial->es_lock);
730 } 724 }
731 725
732 726
733 /***************************************************************************** 727 /*****************************************************************************
734 * edge_bulk_out_data_callback 728 * edge_bulk_out_data_callback
735 * this is the callback function for when we have finished sending 729 * this is the callback function for when we have finished sending
736 * serial data on the bulk out endpoint. 730 * serial data on the bulk out endpoint.
737 *****************************************************************************/ 731 *****************************************************************************/
738 static void edge_bulk_out_data_callback(struct urb *urb) 732 static void edge_bulk_out_data_callback(struct urb *urb)
739 { 733 {
740 struct edgeport_port *edge_port = urb->context; 734 struct edgeport_port *edge_port = urb->context;
741 struct tty_struct *tty;
742 int status = urb->status; 735 int status = urb->status;
743 736
744 if (status) { 737 if (status) {
745 dev_dbg(&urb->dev->dev, 738 dev_dbg(&urb->dev->dev,
746 "%s - nonzero write bulk status received: %d\n", 739 "%s - nonzero write bulk status received: %d\n",
747 __func__, status); 740 __func__, status);
748 } 741 }
749 742
750 tty = tty_port_tty_get(&edge_port->port->port); 743 if (edge_port->open)
744 tty_port_tty_wakeup(&edge_port->port->port);
751 745
752 if (tty && edge_port->open) {
753 /* let the tty driver wakeup if it has a special
754 write_wakeup function */
755 tty_wakeup(tty);
756 }
757 tty_kref_put(tty);
758
759 /* Release the Write URB */ 746 /* Release the Write URB */
760 edge_port->write_in_progress = false; 747 edge_port->write_in_progress = false;
761 748
762 /* Check if more data needs to be sent */ 749 /* Check if more data needs to be sent */
763 send_more_port_data((struct edgeport_serial *) 750 send_more_port_data((struct edgeport_serial *)
764 (usb_get_serial_data(edge_port->port->serial)), edge_port); 751 (usb_get_serial_data(edge_port->port->serial)), edge_port);
765 } 752 }
766 753
767 754
768 /***************************************************************************** 755 /*****************************************************************************
769 * BulkOutCmdCallback 756 * BulkOutCmdCallback
770 * this is the callback function for when we have finished sending a 757 * this is the callback function for when we have finished sending a
771 * command on the bulk out endpoint. 758 * command on the bulk out endpoint.
772 *****************************************************************************/ 759 *****************************************************************************/
773 static void edge_bulk_out_cmd_callback(struct urb *urb) 760 static void edge_bulk_out_cmd_callback(struct urb *urb)
774 { 761 {
775 struct edgeport_port *edge_port = urb->context; 762 struct edgeport_port *edge_port = urb->context;
776 struct tty_struct *tty;
777 int status = urb->status; 763 int status = urb->status;
778 764
779 atomic_dec(&CmdUrbs); 765 atomic_dec(&CmdUrbs);
780 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n", 766 dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
781 __func__, urb, atomic_read(&CmdUrbs)); 767 __func__, urb, atomic_read(&CmdUrbs));
782 768
783 769
784 /* clean up the transfer buffer */ 770 /* clean up the transfer buffer */
785 kfree(urb->transfer_buffer); 771 kfree(urb->transfer_buffer);
786 772
787 /* Free the command urb */ 773 /* Free the command urb */
788 usb_free_urb(urb); 774 usb_free_urb(urb);
789 775
790 if (status) { 776 if (status) {
791 dev_dbg(&urb->dev->dev, 777 dev_dbg(&urb->dev->dev,
792 "%s - nonzero write bulk status received: %d\n", 778 "%s - nonzero write bulk status received: %d\n",
793 __func__, status); 779 __func__, status);
794 return; 780 return;
795 } 781 }
796 782
797 /* Get pointer to tty */
798 tty = tty_port_tty_get(&edge_port->port->port);
799
800 /* tell the tty driver that something has changed */ 783 /* tell the tty driver that something has changed */
801 if (tty && edge_port->open) 784 if (edge_port->open)
802 tty_wakeup(tty); 785 tty_port_tty_wakeup(&edge_port->port->port);
803 tty_kref_put(tty);
804 786
805 /* we have completed the command */ 787 /* we have completed the command */
806 edge_port->commandPending = false; 788 edge_port->commandPending = false;
807 wake_up(&edge_port->wait_command); 789 wake_up(&edge_port->wait_command);
808 } 790 }
809 791
810 792
811 /***************************************************************************** 793 /*****************************************************************************
812 * Driver tty interface functions 794 * Driver tty interface functions
813 *****************************************************************************/ 795 *****************************************************************************/
814 796
815 /***************************************************************************** 797 /*****************************************************************************
816 * SerialOpen 798 * SerialOpen
817 * this function is called by the tty driver when a port is opened 799 * this function is called by the tty driver when a port is opened
818 * If successful, we return 0 800 * If successful, we return 0
819 * Otherwise we return a negative error number. 801 * Otherwise we return a negative error number.
820 *****************************************************************************/ 802 *****************************************************************************/
821 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port) 803 static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
822 { 804 {
823 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 805 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
824 struct device *dev = &port->dev; 806 struct device *dev = &port->dev;
825 struct usb_serial *serial; 807 struct usb_serial *serial;
826 struct edgeport_serial *edge_serial; 808 struct edgeport_serial *edge_serial;
827 int response; 809 int response;
828 810
829 if (edge_port == NULL) 811 if (edge_port == NULL)
830 return -ENODEV; 812 return -ENODEV;
831 813
832 /* see if we've set up our endpoint info yet (can't set it up 814 /* see if we've set up our endpoint info yet (can't set it up
833 in edge_startup as the structures were not set up at that time.) */ 815 in edge_startup as the structures were not set up at that time.) */
834 serial = port->serial; 816 serial = port->serial;
835 edge_serial = usb_get_serial_data(serial); 817 edge_serial = usb_get_serial_data(serial);
836 if (edge_serial == NULL) 818 if (edge_serial == NULL)
837 return -ENODEV; 819 return -ENODEV;
838 if (edge_serial->interrupt_in_buffer == NULL) { 820 if (edge_serial->interrupt_in_buffer == NULL) {
839 struct usb_serial_port *port0 = serial->port[0]; 821 struct usb_serial_port *port0 = serial->port[0];
840 822
841 /* not set up yet, so do it now */ 823 /* not set up yet, so do it now */
842 edge_serial->interrupt_in_buffer = 824 edge_serial->interrupt_in_buffer =
843 port0->interrupt_in_buffer; 825 port0->interrupt_in_buffer;
844 edge_serial->interrupt_in_endpoint = 826 edge_serial->interrupt_in_endpoint =
845 port0->interrupt_in_endpointAddress; 827 port0->interrupt_in_endpointAddress;
846 edge_serial->interrupt_read_urb = port0->interrupt_in_urb; 828 edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
847 edge_serial->bulk_in_buffer = port0->bulk_in_buffer; 829 edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
848 edge_serial->bulk_in_endpoint = 830 edge_serial->bulk_in_endpoint =
849 port0->bulk_in_endpointAddress; 831 port0->bulk_in_endpointAddress;
850 edge_serial->read_urb = port0->read_urb; 832 edge_serial->read_urb = port0->read_urb;
851 edge_serial->bulk_out_endpoint = 833 edge_serial->bulk_out_endpoint =
852 port0->bulk_out_endpointAddress; 834 port0->bulk_out_endpointAddress;
853 835
854 /* set up our interrupt urb */ 836 /* set up our interrupt urb */
855 usb_fill_int_urb(edge_serial->interrupt_read_urb, 837 usb_fill_int_urb(edge_serial->interrupt_read_urb,
856 serial->dev, 838 serial->dev,
857 usb_rcvintpipe(serial->dev, 839 usb_rcvintpipe(serial->dev,
858 port0->interrupt_in_endpointAddress), 840 port0->interrupt_in_endpointAddress),
859 port0->interrupt_in_buffer, 841 port0->interrupt_in_buffer,
860 edge_serial->interrupt_read_urb->transfer_buffer_length, 842 edge_serial->interrupt_read_urb->transfer_buffer_length,
861 edge_interrupt_callback, edge_serial, 843 edge_interrupt_callback, edge_serial,
862 edge_serial->interrupt_read_urb->interval); 844 edge_serial->interrupt_read_urb->interval);
863 845
864 /* set up our bulk in urb */ 846 /* set up our bulk in urb */
865 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev, 847 usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
866 usb_rcvbulkpipe(serial->dev, 848 usb_rcvbulkpipe(serial->dev,
867 port0->bulk_in_endpointAddress), 849 port0->bulk_in_endpointAddress),
868 port0->bulk_in_buffer, 850 port0->bulk_in_buffer,
869 edge_serial->read_urb->transfer_buffer_length, 851 edge_serial->read_urb->transfer_buffer_length,
870 edge_bulk_in_callback, edge_serial); 852 edge_bulk_in_callback, edge_serial);
871 edge_serial->read_in_progress = false; 853 edge_serial->read_in_progress = false;
872 854
873 /* start interrupt read for this edgeport 855 /* start interrupt read for this edgeport
874 * this interrupt will continue as long 856 * this interrupt will continue as long
875 * as the edgeport is connected */ 857 * as the edgeport is connected */
876 response = usb_submit_urb(edge_serial->interrupt_read_urb, 858 response = usb_submit_urb(edge_serial->interrupt_read_urb,
877 GFP_KERNEL); 859 GFP_KERNEL);
878 if (response) { 860 if (response) {
879 dev_err(dev, "%s - Error %d submitting control urb\n", 861 dev_err(dev, "%s - Error %d submitting control urb\n",
880 __func__, response); 862 __func__, response);
881 } 863 }
882 } 864 }
883 865
884 /* initialize our wait queues */ 866 /* initialize our wait queues */
885 init_waitqueue_head(&edge_port->wait_open); 867 init_waitqueue_head(&edge_port->wait_open);
886 init_waitqueue_head(&edge_port->wait_chase); 868 init_waitqueue_head(&edge_port->wait_chase);
887 init_waitqueue_head(&edge_port->delta_msr_wait); 869 init_waitqueue_head(&edge_port->delta_msr_wait);
888 init_waitqueue_head(&edge_port->wait_command); 870 init_waitqueue_head(&edge_port->wait_command);
889 871
890 /* initialize our icount structure */ 872 /* initialize our icount structure */
891 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount)); 873 memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
892 874
893 /* initialize our port settings */ 875 /* initialize our port settings */
894 edge_port->txCredits = 0; /* Can't send any data yet */ 876 edge_port->txCredits = 0; /* Can't send any data yet */
895 /* Must always set this bit to enable ints! */ 877 /* Must always set this bit to enable ints! */
896 edge_port->shadowMCR = MCR_MASTER_IE; 878 edge_port->shadowMCR = MCR_MASTER_IE;
897 edge_port->chaseResponsePending = false; 879 edge_port->chaseResponsePending = false;
898 880
899 /* send a open port command */ 881 /* send a open port command */
900 edge_port->openPending = true; 882 edge_port->openPending = true;
901 edge_port->open = false; 883 edge_port->open = false;
902 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0); 884 response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
903 885
904 if (response < 0) { 886 if (response < 0) {
905 dev_err(dev, "%s - error sending open port command\n", __func__); 887 dev_err(dev, "%s - error sending open port command\n", __func__);
906 edge_port->openPending = false; 888 edge_port->openPending = false;
907 return -ENODEV; 889 return -ENODEV;
908 } 890 }
909 891
910 /* now wait for the port to be completely opened */ 892 /* now wait for the port to be completely opened */
911 wait_event_timeout(edge_port->wait_open, !edge_port->openPending, 893 wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
912 OPEN_TIMEOUT); 894 OPEN_TIMEOUT);
913 895
914 if (!edge_port->open) { 896 if (!edge_port->open) {
915 /* open timed out */ 897 /* open timed out */
916 dev_dbg(dev, "%s - open timedout\n", __func__); 898 dev_dbg(dev, "%s - open timedout\n", __func__);
917 edge_port->openPending = false; 899 edge_port->openPending = false;
918 return -ENODEV; 900 return -ENODEV;
919 } 901 }
920 902
921 /* create the txfifo */ 903 /* create the txfifo */
922 edge_port->txfifo.head = 0; 904 edge_port->txfifo.head = 0;
923 edge_port->txfifo.tail = 0; 905 edge_port->txfifo.tail = 0;
924 edge_port->txfifo.count = 0; 906 edge_port->txfifo.count = 0;
925 edge_port->txfifo.size = edge_port->maxTxCredits; 907 edge_port->txfifo.size = edge_port->maxTxCredits;
926 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL); 908 edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
927 909
928 if (!edge_port->txfifo.fifo) { 910 if (!edge_port->txfifo.fifo) {
929 dev_dbg(dev, "%s - no memory\n", __func__); 911 dev_dbg(dev, "%s - no memory\n", __func__);
930 edge_close(port); 912 edge_close(port);
931 return -ENOMEM; 913 return -ENOMEM;
932 } 914 }
933 915
934 /* Allocate a URB for the write */ 916 /* Allocate a URB for the write */
935 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL); 917 edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
936 edge_port->write_in_progress = false; 918 edge_port->write_in_progress = false;
937 919
938 if (!edge_port->write_urb) { 920 if (!edge_port->write_urb) {
939 dev_dbg(dev, "%s - no memory\n", __func__); 921 dev_dbg(dev, "%s - no memory\n", __func__);
940 edge_close(port); 922 edge_close(port);
941 return -ENOMEM; 923 return -ENOMEM;
942 } 924 }
943 925
944 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n", 926 dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
945 __func__, port->number, edge_port->maxTxCredits); 927 __func__, port->number, edge_port->maxTxCredits);
946 928
947 return 0; 929 return 0;
948 } 930 }
949 931
950 932
951 /************************************************************************ 933 /************************************************************************
952 * 934 *
953 * block_until_chase_response 935 * block_until_chase_response
954 * 936 *
955 * This function will block the close until one of the following: 937 * This function will block the close until one of the following:
956 * 1. Response to our Chase comes from Edgeport 938 * 1. Response to our Chase comes from Edgeport
957 * 2. A timeout of 10 seconds without activity has expired 939 * 2. A timeout of 10 seconds without activity has expired
958 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty) 940 * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
959 * 941 *
960 ************************************************************************/ 942 ************************************************************************/
961 static void block_until_chase_response(struct edgeport_port *edge_port) 943 static void block_until_chase_response(struct edgeport_port *edge_port)
962 { 944 {
963 struct device *dev = &edge_port->port->dev; 945 struct device *dev = &edge_port->port->dev;
964 DEFINE_WAIT(wait); 946 DEFINE_WAIT(wait);
965 __u16 lastCredits; 947 __u16 lastCredits;
966 int timeout = 1*HZ; 948 int timeout = 1*HZ;
967 int loop = 10; 949 int loop = 10;
968 950
969 while (1) { 951 while (1) {
970 /* Save Last credits */ 952 /* Save Last credits */
971 lastCredits = edge_port->txCredits; 953 lastCredits = edge_port->txCredits;
972 954
973 /* Did we get our Chase response */ 955 /* Did we get our Chase response */
974 if (!edge_port->chaseResponsePending) { 956 if (!edge_port->chaseResponsePending) {
975 dev_dbg(dev, "%s - Got Chase Response\n", __func__); 957 dev_dbg(dev, "%s - Got Chase Response\n", __func__);
976 958
977 /* did we get all of our credit back? */ 959 /* did we get all of our credit back? */
978 if (edge_port->txCredits == edge_port->maxTxCredits) { 960 if (edge_port->txCredits == edge_port->maxTxCredits) {
979 dev_dbg(dev, "%s - Got all credits\n", __func__); 961 dev_dbg(dev, "%s - Got all credits\n", __func__);
980 return; 962 return;
981 } 963 }
982 } 964 }
983 965
984 /* Block the thread for a while */ 966 /* Block the thread for a while */
985 prepare_to_wait(&edge_port->wait_chase, &wait, 967 prepare_to_wait(&edge_port->wait_chase, &wait,
986 TASK_UNINTERRUPTIBLE); 968 TASK_UNINTERRUPTIBLE);
987 schedule_timeout(timeout); 969 schedule_timeout(timeout);
988 finish_wait(&edge_port->wait_chase, &wait); 970 finish_wait(&edge_port->wait_chase, &wait);
989 971
990 if (lastCredits == edge_port->txCredits) { 972 if (lastCredits == edge_port->txCredits) {
991 /* No activity.. count down. */ 973 /* No activity.. count down. */
992 loop--; 974 loop--;
993 if (loop == 0) { 975 if (loop == 0) {
994 edge_port->chaseResponsePending = false; 976 edge_port->chaseResponsePending = false;
995 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__); 977 dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
996 return; 978 return;
997 } 979 }
998 } else { 980 } else {
999 /* Reset timeout value back to 10 seconds */ 981 /* Reset timeout value back to 10 seconds */
1000 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__, 982 dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
1001 lastCredits, edge_port->txCredits); 983 lastCredits, edge_port->txCredits);
1002 loop = 10; 984 loop = 10;
1003 } 985 }
1004 } 986 }
1005 } 987 }
1006 988
1007 989
1008 /************************************************************************ 990 /************************************************************************
1009 * 991 *
1010 * block_until_tx_empty 992 * block_until_tx_empty
1011 * 993 *
1012 * This function will block the close until one of the following: 994 * This function will block the close until one of the following:
1013 * 1. TX count are 0 995 * 1. TX count are 0
1014 * 2. The edgeport has stopped 996 * 2. The edgeport has stopped
1015 * 3. A timeout of 3 seconds without activity has expired 997 * 3. A timeout of 3 seconds without activity has expired
1016 * 998 *
1017 ************************************************************************/ 999 ************************************************************************/
1018 static void block_until_tx_empty(struct edgeport_port *edge_port) 1000 static void block_until_tx_empty(struct edgeport_port *edge_port)
1019 { 1001 {
1020 struct device *dev = &edge_port->port->dev; 1002 struct device *dev = &edge_port->port->dev;
1021 DEFINE_WAIT(wait); 1003 DEFINE_WAIT(wait);
1022 struct TxFifo *fifo = &edge_port->txfifo; 1004 struct TxFifo *fifo = &edge_port->txfifo;
1023 __u32 lastCount; 1005 __u32 lastCount;
1024 int timeout = HZ/10; 1006 int timeout = HZ/10;
1025 int loop = 30; 1007 int loop = 30;
1026 1008
1027 while (1) { 1009 while (1) {
1028 /* Save Last count */ 1010 /* Save Last count */
1029 lastCount = fifo->count; 1011 lastCount = fifo->count;
1030 1012
1031 /* Is the Edgeport Buffer empty? */ 1013 /* Is the Edgeport Buffer empty? */
1032 if (lastCount == 0) { 1014 if (lastCount == 0) {
1033 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__); 1015 dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
1034 return; 1016 return;
1035 } 1017 }
1036 1018
1037 /* Block the thread for a while */ 1019 /* Block the thread for a while */
1038 prepare_to_wait(&edge_port->wait_chase, &wait, 1020 prepare_to_wait(&edge_port->wait_chase, &wait,
1039 TASK_UNINTERRUPTIBLE); 1021 TASK_UNINTERRUPTIBLE);
1040 schedule_timeout(timeout); 1022 schedule_timeout(timeout);
1041 finish_wait(&edge_port->wait_chase, &wait); 1023 finish_wait(&edge_port->wait_chase, &wait);
1042 1024
1043 dev_dbg(dev, "%s wait\n", __func__); 1025 dev_dbg(dev, "%s wait\n", __func__);
1044 1026
1045 if (lastCount == fifo->count) { 1027 if (lastCount == fifo->count) {
1046 /* No activity.. count down. */ 1028 /* No activity.. count down. */
1047 loop--; 1029 loop--;
1048 if (loop == 0) { 1030 if (loop == 0) {
1049 dev_dbg(dev, "%s - TIMEOUT\n", __func__); 1031 dev_dbg(dev, "%s - TIMEOUT\n", __func__);
1050 return; 1032 return;
1051 } 1033 }
1052 } else { 1034 } else {
1053 /* Reset timeout value back to seconds */ 1035 /* Reset timeout value back to seconds */
1054 loop = 30; 1036 loop = 30;
1055 } 1037 }
1056 } 1038 }
1057 } 1039 }
1058 1040
1059 1041
1060 /***************************************************************************** 1042 /*****************************************************************************
1061 * edge_close 1043 * edge_close
1062 * this function is called by the tty driver when a port is closed 1044 * this function is called by the tty driver when a port is closed
1063 *****************************************************************************/ 1045 *****************************************************************************/
1064 static void edge_close(struct usb_serial_port *port) 1046 static void edge_close(struct usb_serial_port *port)
1065 { 1047 {
1066 struct edgeport_serial *edge_serial; 1048 struct edgeport_serial *edge_serial;
1067 struct edgeport_port *edge_port; 1049 struct edgeport_port *edge_port;
1068 int status; 1050 int status;
1069 1051
1070 edge_serial = usb_get_serial_data(port->serial); 1052 edge_serial = usb_get_serial_data(port->serial);
1071 edge_port = usb_get_serial_port_data(port); 1053 edge_port = usb_get_serial_port_data(port);
1072 if (edge_serial == NULL || edge_port == NULL) 1054 if (edge_serial == NULL || edge_port == NULL)
1073 return; 1055 return;
1074 1056
1075 /* block until tx is empty */ 1057 /* block until tx is empty */
1076 block_until_tx_empty(edge_port); 1058 block_until_tx_empty(edge_port);
1077 1059
1078 edge_port->closePending = true; 1060 edge_port->closePending = true;
1079 1061
1080 if ((!edge_serial->is_epic) || 1062 if ((!edge_serial->is_epic) ||
1081 ((edge_serial->is_epic) && 1063 ((edge_serial->is_epic) &&
1082 (edge_serial->epic_descriptor.Supports.IOSPChase))) { 1064 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1083 /* flush and chase */ 1065 /* flush and chase */
1084 edge_port->chaseResponsePending = true; 1066 edge_port->chaseResponsePending = true;
1085 1067
1086 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__); 1068 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1087 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0); 1069 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1088 if (status == 0) 1070 if (status == 0)
1089 /* block until chase finished */ 1071 /* block until chase finished */
1090 block_until_chase_response(edge_port); 1072 block_until_chase_response(edge_port);
1091 else 1073 else
1092 edge_port->chaseResponsePending = false; 1074 edge_port->chaseResponsePending = false;
1093 } 1075 }
1094 1076
1095 if ((!edge_serial->is_epic) || 1077 if ((!edge_serial->is_epic) ||
1096 ((edge_serial->is_epic) && 1078 ((edge_serial->is_epic) &&
1097 (edge_serial->epic_descriptor.Supports.IOSPClose))) { 1079 (edge_serial->epic_descriptor.Supports.IOSPClose))) {
1098 /* close the port */ 1080 /* close the port */
1099 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__); 1081 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
1100 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0); 1082 send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
1101 } 1083 }
1102 1084
1103 /* port->close = true; */ 1085 /* port->close = true; */
1104 edge_port->closePending = false; 1086 edge_port->closePending = false;
1105 edge_port->open = false; 1087 edge_port->open = false;
1106 edge_port->openPending = false; 1088 edge_port->openPending = false;
1107 1089
1108 usb_kill_urb(edge_port->write_urb); 1090 usb_kill_urb(edge_port->write_urb);
1109 1091
1110 if (edge_port->write_urb) { 1092 if (edge_port->write_urb) {
1111 /* if this urb had a transfer buffer already 1093 /* if this urb had a transfer buffer already
1112 (old transfer) free it */ 1094 (old transfer) free it */
1113 kfree(edge_port->write_urb->transfer_buffer); 1095 kfree(edge_port->write_urb->transfer_buffer);
1114 usb_free_urb(edge_port->write_urb); 1096 usb_free_urb(edge_port->write_urb);
1115 edge_port->write_urb = NULL; 1097 edge_port->write_urb = NULL;
1116 } 1098 }
1117 kfree(edge_port->txfifo.fifo); 1099 kfree(edge_port->txfifo.fifo);
1118 edge_port->txfifo.fifo = NULL; 1100 edge_port->txfifo.fifo = NULL;
1119 } 1101 }
1120 1102
1121 /***************************************************************************** 1103 /*****************************************************************************
1122 * SerialWrite 1104 * SerialWrite
1123 * this function is called by the tty driver when data should be written 1105 * this function is called by the tty driver when data should be written
1124 * to the port. 1106 * to the port.
1125 * If successful, we return the number of bytes written, otherwise we 1107 * If successful, we return the number of bytes written, otherwise we
1126 * return a negative error number. 1108 * return a negative error number.
1127 *****************************************************************************/ 1109 *****************************************************************************/
1128 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port, 1110 static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
1129 const unsigned char *data, int count) 1111 const unsigned char *data, int count)
1130 { 1112 {
1131 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1113 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1132 struct TxFifo *fifo; 1114 struct TxFifo *fifo;
1133 int copySize; 1115 int copySize;
1134 int bytesleft; 1116 int bytesleft;
1135 int firsthalf; 1117 int firsthalf;
1136 int secondhalf; 1118 int secondhalf;
1137 unsigned long flags; 1119 unsigned long flags;
1138 1120
1139 if (edge_port == NULL) 1121 if (edge_port == NULL)
1140 return -ENODEV; 1122 return -ENODEV;
1141 1123
1142 /* get a pointer to the Tx fifo */ 1124 /* get a pointer to the Tx fifo */
1143 fifo = &edge_port->txfifo; 1125 fifo = &edge_port->txfifo;
1144 1126
1145 spin_lock_irqsave(&edge_port->ep_lock, flags); 1127 spin_lock_irqsave(&edge_port->ep_lock, flags);
1146 1128
1147 /* calculate number of bytes to put in fifo */ 1129 /* calculate number of bytes to put in fifo */
1148 copySize = min((unsigned int)count, 1130 copySize = min((unsigned int)count,
1149 (edge_port->txCredits - fifo->count)); 1131 (edge_port->txCredits - fifo->count));
1150 1132
1151 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n", 1133 dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
1152 __func__, port->number, count, 1134 __func__, port->number, count,
1153 edge_port->txCredits - fifo->count, copySize); 1135 edge_port->txCredits - fifo->count, copySize);
1154 1136
1155 /* catch writes of 0 bytes which the tty driver likes to give us, 1137 /* catch writes of 0 bytes which the tty driver likes to give us,
1156 and when txCredits is empty */ 1138 and when txCredits is empty */
1157 if (copySize == 0) { 1139 if (copySize == 0) {
1158 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__); 1140 dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
1159 goto finish_write; 1141 goto finish_write;
1160 } 1142 }
1161 1143
1162 /* queue the data 1144 /* queue the data
1163 * since we can never overflow the buffer we do not have to check for a 1145 * since we can never overflow the buffer we do not have to check for a
1164 * full condition 1146 * full condition
1165 * 1147 *
1166 * the copy is done is two parts -- first fill to the end of the buffer 1148 * the copy is done is two parts -- first fill to the end of the buffer
1167 * then copy the reset from the start of the buffer 1149 * then copy the reset from the start of the buffer
1168 */ 1150 */
1169 bytesleft = fifo->size - fifo->head; 1151 bytesleft = fifo->size - fifo->head;
1170 firsthalf = min(bytesleft, copySize); 1152 firsthalf = min(bytesleft, copySize);
1171 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__, 1153 dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
1172 firsthalf, bytesleft); 1154 firsthalf, bytesleft);
1173 1155
1174 /* now copy our data */ 1156 /* now copy our data */
1175 memcpy(&fifo->fifo[fifo->head], data, firsthalf); 1157 memcpy(&fifo->fifo[fifo->head], data, firsthalf);
1176 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]); 1158 usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
1177 1159
1178 /* update the index and size */ 1160 /* update the index and size */
1179 fifo->head += firsthalf; 1161 fifo->head += firsthalf;
1180 fifo->count += firsthalf; 1162 fifo->count += firsthalf;
1181 1163
1182 /* wrap the index */ 1164 /* wrap the index */
1183 if (fifo->head == fifo->size) 1165 if (fifo->head == fifo->size)
1184 fifo->head = 0; 1166 fifo->head = 0;
1185 1167
1186 secondhalf = copySize-firsthalf; 1168 secondhalf = copySize-firsthalf;
1187 1169
1188 if (secondhalf) { 1170 if (secondhalf) {
1189 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf); 1171 dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
1190 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf); 1172 memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
1191 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]); 1173 usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
1192 /* update the index and size */ 1174 /* update the index and size */
1193 fifo->count += secondhalf; 1175 fifo->count += secondhalf;
1194 fifo->head += secondhalf; 1176 fifo->head += secondhalf;
1195 /* No need to check for wrap since we can not get to end of 1177 /* No need to check for wrap since we can not get to end of
1196 * the fifo in this part 1178 * the fifo in this part
1197 */ 1179 */
1198 } 1180 }
1199 1181
1200 finish_write: 1182 finish_write:
1201 spin_unlock_irqrestore(&edge_port->ep_lock, flags); 1183 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1202 1184
1203 send_more_port_data((struct edgeport_serial *) 1185 send_more_port_data((struct edgeport_serial *)
1204 usb_get_serial_data(port->serial), edge_port); 1186 usb_get_serial_data(port->serial), edge_port);
1205 1187
1206 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n", 1188 dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
1207 __func__, copySize, edge_port->txCredits, fifo->count); 1189 __func__, copySize, edge_port->txCredits, fifo->count);
1208 1190
1209 return copySize; 1191 return copySize;
1210 } 1192 }
1211 1193
1212 1194
1213 /************************************************************************ 1195 /************************************************************************
1214 * 1196 *
1215 * send_more_port_data() 1197 * send_more_port_data()
1216 * 1198 *
1217 * This routine attempts to write additional UART transmit data 1199 * This routine attempts to write additional UART transmit data
1218 * to a port over the USB bulk pipe. It is called (1) when new 1200 * to a port over the USB bulk pipe. It is called (1) when new
1219 * data has been written to a port's TxBuffer from higher layers 1201 * data has been written to a port's TxBuffer from higher layers
1220 * (2) when the peripheral sends us additional TxCredits indicating 1202 * (2) when the peripheral sends us additional TxCredits indicating
1221 * that it can accept more Tx data for a given port; and (3) when 1203 * that it can accept more Tx data for a given port; and (3) when
1222 * a bulk write completes successfully and we want to see if we 1204 * a bulk write completes successfully and we want to see if we
1223 * can transmit more. 1205 * can transmit more.
1224 * 1206 *
1225 ************************************************************************/ 1207 ************************************************************************/
1226 static void send_more_port_data(struct edgeport_serial *edge_serial, 1208 static void send_more_port_data(struct edgeport_serial *edge_serial,
1227 struct edgeport_port *edge_port) 1209 struct edgeport_port *edge_port)
1228 { 1210 {
1229 struct TxFifo *fifo = &edge_port->txfifo; 1211 struct TxFifo *fifo = &edge_port->txfifo;
1230 struct device *dev = &edge_port->port->dev; 1212 struct device *dev = &edge_port->port->dev;
1231 struct urb *urb; 1213 struct urb *urb;
1232 unsigned char *buffer; 1214 unsigned char *buffer;
1233 int status; 1215 int status;
1234 int count; 1216 int count;
1235 int bytesleft; 1217 int bytesleft;
1236 int firsthalf; 1218 int firsthalf;
1237 int secondhalf; 1219 int secondhalf;
1238 unsigned long flags; 1220 unsigned long flags;
1239 1221
1240 spin_lock_irqsave(&edge_port->ep_lock, flags); 1222 spin_lock_irqsave(&edge_port->ep_lock, flags);
1241 1223
1242 if (edge_port->write_in_progress || 1224 if (edge_port->write_in_progress ||
1243 !edge_port->open || 1225 !edge_port->open ||
1244 (fifo->count == 0)) { 1226 (fifo->count == 0)) {
1245 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n", 1227 dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
1246 __func__, edge_port->port->number, 1228 __func__, edge_port->port->number,
1247 fifo->count, edge_port->write_in_progress); 1229 fifo->count, edge_port->write_in_progress);
1248 goto exit_send; 1230 goto exit_send;
1249 } 1231 }
1250 1232
1251 /* since the amount of data in the fifo will always fit into the 1233 /* since the amount of data in the fifo will always fit into the
1252 * edgeport buffer we do not need to check the write length 1234 * edgeport buffer we do not need to check the write length
1253 * 1235 *
1254 * Do we have enough credits for this port to make it worthwhile 1236 * Do we have enough credits for this port to make it worthwhile
1255 * to bother queueing a write. If it's too small, say a few bytes, 1237 * to bother queueing a write. If it's too small, say a few bytes,
1256 * it's better to wait for more credits so we can do a larger write. 1238 * it's better to wait for more credits so we can do a larger write.
1257 */ 1239 */
1258 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) { 1240 if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
1259 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n", 1241 dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
1260 __func__, edge_port->port->number, fifo->count, 1242 __func__, edge_port->port->number, fifo->count,
1261 edge_port->txCredits); 1243 edge_port->txCredits);
1262 goto exit_send; 1244 goto exit_send;
1263 } 1245 }
1264 1246
1265 /* lock this write */ 1247 /* lock this write */
1266 edge_port->write_in_progress = true; 1248 edge_port->write_in_progress = true;
1267 1249
1268 /* get a pointer to the write_urb */ 1250 /* get a pointer to the write_urb */
1269 urb = edge_port->write_urb; 1251 urb = edge_port->write_urb;
1270 1252
1271 /* make sure transfer buffer is freed */ 1253 /* make sure transfer buffer is freed */
1272 kfree(urb->transfer_buffer); 1254 kfree(urb->transfer_buffer);
1273 urb->transfer_buffer = NULL; 1255 urb->transfer_buffer = NULL;
1274 1256
1275 /* build the data header for the buffer and port that we are about 1257 /* build the data header for the buffer and port that we are about
1276 to send out */ 1258 to send out */
1277 count = fifo->count; 1259 count = fifo->count;
1278 buffer = kmalloc(count+2, GFP_ATOMIC); 1260 buffer = kmalloc(count+2, GFP_ATOMIC);
1279 if (buffer == NULL) { 1261 if (buffer == NULL) {
1280 dev_err_console(edge_port->port, 1262 dev_err_console(edge_port->port,
1281 "%s - no more kernel memory...\n", __func__); 1263 "%s - no more kernel memory...\n", __func__);
1282 edge_port->write_in_progress = false; 1264 edge_port->write_in_progress = false;
1283 goto exit_send; 1265 goto exit_send;
1284 } 1266 }
1285 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number 1267 buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
1286 - edge_port->port->serial->minor, count); 1268 - edge_port->port->serial->minor, count);
1287 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number 1269 buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
1288 - edge_port->port->serial->minor, count); 1270 - edge_port->port->serial->minor, count);
1289 1271
1290 /* now copy our data */ 1272 /* now copy our data */
1291 bytesleft = fifo->size - fifo->tail; 1273 bytesleft = fifo->size - fifo->tail;
1292 firsthalf = min(bytesleft, count); 1274 firsthalf = min(bytesleft, count);
1293 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf); 1275 memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
1294 fifo->tail += firsthalf; 1276 fifo->tail += firsthalf;
1295 fifo->count -= firsthalf; 1277 fifo->count -= firsthalf;
1296 if (fifo->tail == fifo->size) 1278 if (fifo->tail == fifo->size)
1297 fifo->tail = 0; 1279 fifo->tail = 0;
1298 1280
1299 secondhalf = count-firsthalf; 1281 secondhalf = count-firsthalf;
1300 if (secondhalf) { 1282 if (secondhalf) {
1301 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail], 1283 memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
1302 secondhalf); 1284 secondhalf);
1303 fifo->tail += secondhalf; 1285 fifo->tail += secondhalf;
1304 fifo->count -= secondhalf; 1286 fifo->count -= secondhalf;
1305 } 1287 }
1306 1288
1307 if (count) 1289 if (count)
1308 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]); 1290 usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
1309 1291
1310 /* fill up the urb with all of our data and submit it */ 1292 /* fill up the urb with all of our data and submit it */
1311 usb_fill_bulk_urb(urb, edge_serial->serial->dev, 1293 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
1312 usb_sndbulkpipe(edge_serial->serial->dev, 1294 usb_sndbulkpipe(edge_serial->serial->dev,
1313 edge_serial->bulk_out_endpoint), 1295 edge_serial->bulk_out_endpoint),
1314 buffer, count+2, 1296 buffer, count+2,
1315 edge_bulk_out_data_callback, edge_port); 1297 edge_bulk_out_data_callback, edge_port);
1316 1298
1317 /* decrement the number of credits we have by the number we just sent */ 1299 /* decrement the number of credits we have by the number we just sent */
1318 edge_port->txCredits -= count; 1300 edge_port->txCredits -= count;
1319 edge_port->icount.tx += count; 1301 edge_port->icount.tx += count;
1320 1302
1321 status = usb_submit_urb(urb, GFP_ATOMIC); 1303 status = usb_submit_urb(urb, GFP_ATOMIC);
1322 if (status) { 1304 if (status) {
1323 /* something went wrong */ 1305 /* something went wrong */
1324 dev_err_console(edge_port->port, 1306 dev_err_console(edge_port->port,
1325 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n", 1307 "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
1326 __func__, status); 1308 __func__, status);
1327 edge_port->write_in_progress = false; 1309 edge_port->write_in_progress = false;
1328 1310
1329 /* revert the credits as something bad happened. */ 1311 /* revert the credits as something bad happened. */
1330 edge_port->txCredits += count; 1312 edge_port->txCredits += count;
1331 edge_port->icount.tx -= count; 1313 edge_port->icount.tx -= count;
1332 } 1314 }
1333 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n", 1315 dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
1334 __func__, count, edge_port->txCredits, fifo->count); 1316 __func__, count, edge_port->txCredits, fifo->count);
1335 1317
1336 exit_send: 1318 exit_send:
1337 spin_unlock_irqrestore(&edge_port->ep_lock, flags); 1319 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1338 } 1320 }
1339 1321
1340 1322
1341 /***************************************************************************** 1323 /*****************************************************************************
1342 * edge_write_room 1324 * edge_write_room
1343 * this function is called by the tty driver when it wants to know how 1325 * this function is called by the tty driver when it wants to know how
1344 * many bytes of data we can accept for a specific port. If successful, 1326 * many bytes of data we can accept for a specific port. If successful,
1345 * we return the amount of room that we have for this port (the txCredits) 1327 * we return the amount of room that we have for this port (the txCredits)
1346 * otherwise we return a negative error number. 1328 * otherwise we return a negative error number.
1347 *****************************************************************************/ 1329 *****************************************************************************/
1348 static int edge_write_room(struct tty_struct *tty) 1330 static int edge_write_room(struct tty_struct *tty)
1349 { 1331 {
1350 struct usb_serial_port *port = tty->driver_data; 1332 struct usb_serial_port *port = tty->driver_data;
1351 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1333 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1352 int room; 1334 int room;
1353 unsigned long flags; 1335 unsigned long flags;
1354 1336
1355 if (edge_port == NULL) 1337 if (edge_port == NULL)
1356 return 0; 1338 return 0;
1357 if (edge_port->closePending) 1339 if (edge_port->closePending)
1358 return 0; 1340 return 0;
1359 1341
1360 if (!edge_port->open) { 1342 if (!edge_port->open) {
1361 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1343 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1362 return 0; 1344 return 0;
1363 } 1345 }
1364 1346
1365 /* total of both buffers is still txCredit */ 1347 /* total of both buffers is still txCredit */
1366 spin_lock_irqsave(&edge_port->ep_lock, flags); 1348 spin_lock_irqsave(&edge_port->ep_lock, flags);
1367 room = edge_port->txCredits - edge_port->txfifo.count; 1349 room = edge_port->txCredits - edge_port->txfifo.count;
1368 spin_unlock_irqrestore(&edge_port->ep_lock, flags); 1350 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1369 1351
1370 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room); 1352 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1371 return room; 1353 return room;
1372 } 1354 }
1373 1355
1374 1356
1375 /***************************************************************************** 1357 /*****************************************************************************
1376 * edge_chars_in_buffer 1358 * edge_chars_in_buffer
1377 * this function is called by the tty driver when it wants to know how 1359 * this function is called by the tty driver when it wants to know how
1378 * many bytes of data we currently have outstanding in the port (data that 1360 * many bytes of data we currently have outstanding in the port (data that
1379 * has been written, but hasn't made it out the port yet) 1361 * has been written, but hasn't made it out the port yet)
1380 * If successful, we return the number of bytes left to be written in the 1362 * If successful, we return the number of bytes left to be written in the
1381 * system, 1363 * system,
1382 * Otherwise we return a negative error number. 1364 * Otherwise we return a negative error number.
1383 *****************************************************************************/ 1365 *****************************************************************************/
1384 static int edge_chars_in_buffer(struct tty_struct *tty) 1366 static int edge_chars_in_buffer(struct tty_struct *tty)
1385 { 1367 {
1386 struct usb_serial_port *port = tty->driver_data; 1368 struct usb_serial_port *port = tty->driver_data;
1387 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1369 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1388 int num_chars; 1370 int num_chars;
1389 unsigned long flags; 1371 unsigned long flags;
1390 1372
1391 if (edge_port == NULL) 1373 if (edge_port == NULL)
1392 return 0; 1374 return 0;
1393 if (edge_port->closePending) 1375 if (edge_port->closePending)
1394 return 0; 1376 return 0;
1395 1377
1396 if (!edge_port->open) { 1378 if (!edge_port->open) {
1397 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1379 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1398 return 0; 1380 return 0;
1399 } 1381 }
1400 1382
1401 spin_lock_irqsave(&edge_port->ep_lock, flags); 1383 spin_lock_irqsave(&edge_port->ep_lock, flags);
1402 num_chars = edge_port->maxTxCredits - edge_port->txCredits + 1384 num_chars = edge_port->maxTxCredits - edge_port->txCredits +
1403 edge_port->txfifo.count; 1385 edge_port->txfifo.count;
1404 spin_unlock_irqrestore(&edge_port->ep_lock, flags); 1386 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1405 if (num_chars) { 1387 if (num_chars) {
1406 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__, 1388 dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
1407 port->number, num_chars); 1389 port->number, num_chars);
1408 } 1390 }
1409 1391
1410 return num_chars; 1392 return num_chars;
1411 } 1393 }
1412 1394
1413 1395
1414 /***************************************************************************** 1396 /*****************************************************************************
1415 * SerialThrottle 1397 * SerialThrottle
1416 * this function is called by the tty driver when it wants to stop the data 1398 * this function is called by the tty driver when it wants to stop the data
1417 * being read from the port. 1399 * being read from the port.
1418 *****************************************************************************/ 1400 *****************************************************************************/
1419 static void edge_throttle(struct tty_struct *tty) 1401 static void edge_throttle(struct tty_struct *tty)
1420 { 1402 {
1421 struct usb_serial_port *port = tty->driver_data; 1403 struct usb_serial_port *port = tty->driver_data;
1422 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1404 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1423 int status; 1405 int status;
1424 1406
1425 if (edge_port == NULL) 1407 if (edge_port == NULL)
1426 return; 1408 return;
1427 1409
1428 if (!edge_port->open) { 1410 if (!edge_port->open) {
1429 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1411 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1430 return; 1412 return;
1431 } 1413 }
1432 1414
1433 /* if we are implementing XON/XOFF, send the stop character */ 1415 /* if we are implementing XON/XOFF, send the stop character */
1434 if (I_IXOFF(tty)) { 1416 if (I_IXOFF(tty)) {
1435 unsigned char stop_char = STOP_CHAR(tty); 1417 unsigned char stop_char = STOP_CHAR(tty);
1436 status = edge_write(tty, port, &stop_char, 1); 1418 status = edge_write(tty, port, &stop_char, 1);
1437 if (status <= 0) 1419 if (status <= 0)
1438 return; 1420 return;
1439 } 1421 }
1440 1422
1441 /* if we are implementing RTS/CTS, toggle that line */ 1423 /* if we are implementing RTS/CTS, toggle that line */
1442 if (tty->termios.c_cflag & CRTSCTS) { 1424 if (tty->termios.c_cflag & CRTSCTS) {
1443 edge_port->shadowMCR &= ~MCR_RTS; 1425 edge_port->shadowMCR &= ~MCR_RTS;
1444 status = send_cmd_write_uart_register(edge_port, MCR, 1426 status = send_cmd_write_uart_register(edge_port, MCR,
1445 edge_port->shadowMCR); 1427 edge_port->shadowMCR);
1446 if (status != 0) 1428 if (status != 0)
1447 return; 1429 return;
1448 } 1430 }
1449 } 1431 }
1450 1432
1451 1433
1452 /***************************************************************************** 1434 /*****************************************************************************
1453 * edge_unthrottle 1435 * edge_unthrottle
1454 * this function is called by the tty driver when it wants to resume the 1436 * this function is called by the tty driver when it wants to resume the
1455 * data being read from the port (called after SerialThrottle is called) 1437 * data being read from the port (called after SerialThrottle is called)
1456 *****************************************************************************/ 1438 *****************************************************************************/
1457 static void edge_unthrottle(struct tty_struct *tty) 1439 static void edge_unthrottle(struct tty_struct *tty)
1458 { 1440 {
1459 struct usb_serial_port *port = tty->driver_data; 1441 struct usb_serial_port *port = tty->driver_data;
1460 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1442 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1461 int status; 1443 int status;
1462 1444
1463 if (edge_port == NULL) 1445 if (edge_port == NULL)
1464 return; 1446 return;
1465 1447
1466 if (!edge_port->open) { 1448 if (!edge_port->open) {
1467 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1449 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1468 return; 1450 return;
1469 } 1451 }
1470 1452
1471 /* if we are implementing XON/XOFF, send the start character */ 1453 /* if we are implementing XON/XOFF, send the start character */
1472 if (I_IXOFF(tty)) { 1454 if (I_IXOFF(tty)) {
1473 unsigned char start_char = START_CHAR(tty); 1455 unsigned char start_char = START_CHAR(tty);
1474 status = edge_write(tty, port, &start_char, 1); 1456 status = edge_write(tty, port, &start_char, 1);
1475 if (status <= 0) 1457 if (status <= 0)
1476 return; 1458 return;
1477 } 1459 }
1478 /* if we are implementing RTS/CTS, toggle that line */ 1460 /* if we are implementing RTS/CTS, toggle that line */
1479 if (tty->termios.c_cflag & CRTSCTS) { 1461 if (tty->termios.c_cflag & CRTSCTS) {
1480 edge_port->shadowMCR |= MCR_RTS; 1462 edge_port->shadowMCR |= MCR_RTS;
1481 send_cmd_write_uart_register(edge_port, MCR, 1463 send_cmd_write_uart_register(edge_port, MCR,
1482 edge_port->shadowMCR); 1464 edge_port->shadowMCR);
1483 } 1465 }
1484 } 1466 }
1485 1467
1486 1468
1487 /***************************************************************************** 1469 /*****************************************************************************
1488 * SerialSetTermios 1470 * SerialSetTermios
1489 * this function is called by the tty driver when it wants to change 1471 * this function is called by the tty driver when it wants to change
1490 * the termios structure 1472 * the termios structure
1491 *****************************************************************************/ 1473 *****************************************************************************/
1492 static void edge_set_termios(struct tty_struct *tty, 1474 static void edge_set_termios(struct tty_struct *tty,
1493 struct usb_serial_port *port, struct ktermios *old_termios) 1475 struct usb_serial_port *port, struct ktermios *old_termios)
1494 { 1476 {
1495 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1477 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1496 unsigned int cflag; 1478 unsigned int cflag;
1497 1479
1498 cflag = tty->termios.c_cflag; 1480 cflag = tty->termios.c_cflag;
1499 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios.c_cflag, tty->termios.c_iflag); 1481 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios.c_cflag, tty->termios.c_iflag);
1500 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, old_termios->c_cflag, old_termios->c_iflag); 1482 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, old_termios->c_cflag, old_termios->c_iflag);
1501 1483
1502 if (edge_port == NULL) 1484 if (edge_port == NULL)
1503 return; 1485 return;
1504 1486
1505 if (!edge_port->open) { 1487 if (!edge_port->open) {
1506 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1488 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1507 return; 1489 return;
1508 } 1490 }
1509 1491
1510 /* change the port settings to the new ones specified */ 1492 /* change the port settings to the new ones specified */
1511 change_port_settings(tty, edge_port, old_termios); 1493 change_port_settings(tty, edge_port, old_termios);
1512 } 1494 }
1513 1495
1514 1496
1515 /***************************************************************************** 1497 /*****************************************************************************
1516 * get_lsr_info - get line status register info 1498 * get_lsr_info - get line status register info
1517 * 1499 *
1518 * Purpose: Let user call ioctl() to get info when the UART physically 1500 * Purpose: Let user call ioctl() to get info when the UART physically
1519 * is emptied. On bus types like RS485, the transmitter must 1501 * is emptied. On bus types like RS485, the transmitter must
1520 * release the bus after transmitting. This must be done when 1502 * release the bus after transmitting. This must be done when
1521 * the transmit shift register is empty, not be done when the 1503 * the transmit shift register is empty, not be done when the
1522 * transmit holding register is empty. This functionality 1504 * transmit holding register is empty. This functionality
1523 * allows an RS485 driver to be written in user space. 1505 * allows an RS485 driver to be written in user space.
1524 *****************************************************************************/ 1506 *****************************************************************************/
1525 static int get_lsr_info(struct edgeport_port *edge_port, 1507 static int get_lsr_info(struct edgeport_port *edge_port,
1526 unsigned int __user *value) 1508 unsigned int __user *value)
1527 { 1509 {
1528 unsigned int result = 0; 1510 unsigned int result = 0;
1529 unsigned long flags; 1511 unsigned long flags;
1530 1512
1531 spin_lock_irqsave(&edge_port->ep_lock, flags); 1513 spin_lock_irqsave(&edge_port->ep_lock, flags);
1532 if (edge_port->maxTxCredits == edge_port->txCredits && 1514 if (edge_port->maxTxCredits == edge_port->txCredits &&
1533 edge_port->txfifo.count == 0) { 1515 edge_port->txfifo.count == 0) {
1534 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__); 1516 dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
1535 result = TIOCSER_TEMT; 1517 result = TIOCSER_TEMT;
1536 } 1518 }
1537 spin_unlock_irqrestore(&edge_port->ep_lock, flags); 1519 spin_unlock_irqrestore(&edge_port->ep_lock, flags);
1538 1520
1539 if (copy_to_user(value, &result, sizeof(int))) 1521 if (copy_to_user(value, &result, sizeof(int)))
1540 return -EFAULT; 1522 return -EFAULT;
1541 return 0; 1523 return 0;
1542 } 1524 }
1543 1525
1544 static int edge_tiocmset(struct tty_struct *tty, 1526 static int edge_tiocmset(struct tty_struct *tty,
1545 unsigned int set, unsigned int clear) 1527 unsigned int set, unsigned int clear)
1546 { 1528 {
1547 struct usb_serial_port *port = tty->driver_data; 1529 struct usb_serial_port *port = tty->driver_data;
1548 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1530 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1549 unsigned int mcr; 1531 unsigned int mcr;
1550 1532
1551 mcr = edge_port->shadowMCR; 1533 mcr = edge_port->shadowMCR;
1552 if (set & TIOCM_RTS) 1534 if (set & TIOCM_RTS)
1553 mcr |= MCR_RTS; 1535 mcr |= MCR_RTS;
1554 if (set & TIOCM_DTR) 1536 if (set & TIOCM_DTR)
1555 mcr |= MCR_DTR; 1537 mcr |= MCR_DTR;
1556 if (set & TIOCM_LOOP) 1538 if (set & TIOCM_LOOP)
1557 mcr |= MCR_LOOPBACK; 1539 mcr |= MCR_LOOPBACK;
1558 1540
1559 if (clear & TIOCM_RTS) 1541 if (clear & TIOCM_RTS)
1560 mcr &= ~MCR_RTS; 1542 mcr &= ~MCR_RTS;
1561 if (clear & TIOCM_DTR) 1543 if (clear & TIOCM_DTR)
1562 mcr &= ~MCR_DTR; 1544 mcr &= ~MCR_DTR;
1563 if (clear & TIOCM_LOOP) 1545 if (clear & TIOCM_LOOP)
1564 mcr &= ~MCR_LOOPBACK; 1546 mcr &= ~MCR_LOOPBACK;
1565 1547
1566 edge_port->shadowMCR = mcr; 1548 edge_port->shadowMCR = mcr;
1567 1549
1568 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR); 1550 send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
1569 1551
1570 return 0; 1552 return 0;
1571 } 1553 }
1572 1554
1573 static int edge_tiocmget(struct tty_struct *tty) 1555 static int edge_tiocmget(struct tty_struct *tty)
1574 { 1556 {
1575 struct usb_serial_port *port = tty->driver_data; 1557 struct usb_serial_port *port = tty->driver_data;
1576 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1558 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1577 unsigned int result = 0; 1559 unsigned int result = 0;
1578 unsigned int msr; 1560 unsigned int msr;
1579 unsigned int mcr; 1561 unsigned int mcr;
1580 1562
1581 msr = edge_port->shadowMSR; 1563 msr = edge_port->shadowMSR;
1582 mcr = edge_port->shadowMCR; 1564 mcr = edge_port->shadowMCR;
1583 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */ 1565 result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
1584 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */ 1566 | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
1585 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */ 1567 | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
1586 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */ 1568 | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
1587 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */ 1569 | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
1588 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */ 1570 | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
1589 1571
1590 return result; 1572 return result;
1591 } 1573 }
1592 1574
1593 static int edge_get_icount(struct tty_struct *tty, 1575 static int edge_get_icount(struct tty_struct *tty,
1594 struct serial_icounter_struct *icount) 1576 struct serial_icounter_struct *icount)
1595 { 1577 {
1596 struct usb_serial_port *port = tty->driver_data; 1578 struct usb_serial_port *port = tty->driver_data;
1597 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1579 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1598 struct async_icount cnow; 1580 struct async_icount cnow;
1599 cnow = edge_port->icount; 1581 cnow = edge_port->icount;
1600 1582
1601 icount->cts = cnow.cts; 1583 icount->cts = cnow.cts;
1602 icount->dsr = cnow.dsr; 1584 icount->dsr = cnow.dsr;
1603 icount->rng = cnow.rng; 1585 icount->rng = cnow.rng;
1604 icount->dcd = cnow.dcd; 1586 icount->dcd = cnow.dcd;
1605 icount->rx = cnow.rx; 1587 icount->rx = cnow.rx;
1606 icount->tx = cnow.tx; 1588 icount->tx = cnow.tx;
1607 icount->frame = cnow.frame; 1589 icount->frame = cnow.frame;
1608 icount->overrun = cnow.overrun; 1590 icount->overrun = cnow.overrun;
1609 icount->parity = cnow.parity; 1591 icount->parity = cnow.parity;
1610 icount->brk = cnow.brk; 1592 icount->brk = cnow.brk;
1611 icount->buf_overrun = cnow.buf_overrun; 1593 icount->buf_overrun = cnow.buf_overrun;
1612 1594
1613 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__, 1595 dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1614 port->number, icount->rx, icount->tx); 1596 port->number, icount->rx, icount->tx);
1615 return 0; 1597 return 0;
1616 } 1598 }
1617 1599
1618 static int get_serial_info(struct edgeport_port *edge_port, 1600 static int get_serial_info(struct edgeport_port *edge_port,
1619 struct serial_struct __user *retinfo) 1601 struct serial_struct __user *retinfo)
1620 { 1602 {
1621 struct serial_struct tmp; 1603 struct serial_struct tmp;
1622 1604
1623 if (!retinfo) 1605 if (!retinfo)
1624 return -EFAULT; 1606 return -EFAULT;
1625 1607
1626 memset(&tmp, 0, sizeof(tmp)); 1608 memset(&tmp, 0, sizeof(tmp));
1627 1609
1628 tmp.type = PORT_16550A; 1610 tmp.type = PORT_16550A;
1629 tmp.line = edge_port->port->serial->minor; 1611 tmp.line = edge_port->port->serial->minor;
1630 tmp.port = edge_port->port->number; 1612 tmp.port = edge_port->port->number;
1631 tmp.irq = 0; 1613 tmp.irq = 0;
1632 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 1614 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1633 tmp.xmit_fifo_size = edge_port->maxTxCredits; 1615 tmp.xmit_fifo_size = edge_port->maxTxCredits;
1634 tmp.baud_base = 9600; 1616 tmp.baud_base = 9600;
1635 tmp.close_delay = 5*HZ; 1617 tmp.close_delay = 5*HZ;
1636 tmp.closing_wait = 30*HZ; 1618 tmp.closing_wait = 30*HZ;
1637 1619
1638 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 1620 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1639 return -EFAULT; 1621 return -EFAULT;
1640 return 0; 1622 return 0;
1641 } 1623 }
1642 1624
1643 1625
1644 /***************************************************************************** 1626 /*****************************************************************************
1645 * SerialIoctl 1627 * SerialIoctl
1646 * this function handles any ioctl calls to the driver 1628 * this function handles any ioctl calls to the driver
1647 *****************************************************************************/ 1629 *****************************************************************************/
1648 static int edge_ioctl(struct tty_struct *tty, 1630 static int edge_ioctl(struct tty_struct *tty,
1649 unsigned int cmd, unsigned long arg) 1631 unsigned int cmd, unsigned long arg)
1650 { 1632 {
1651 struct usb_serial_port *port = tty->driver_data; 1633 struct usb_serial_port *port = tty->driver_data;
1652 DEFINE_WAIT(wait); 1634 DEFINE_WAIT(wait);
1653 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1635 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1654 struct async_icount cnow; 1636 struct async_icount cnow;
1655 struct async_icount cprev; 1637 struct async_icount cprev;
1656 1638
1657 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd); 1639 dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
1658 1640
1659 switch (cmd) { 1641 switch (cmd) {
1660 case TIOCSERGETLSR: 1642 case TIOCSERGETLSR:
1661 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number); 1643 dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
1662 return get_lsr_info(edge_port, (unsigned int __user *) arg); 1644 return get_lsr_info(edge_port, (unsigned int __user *) arg);
1663 1645
1664 case TIOCGSERIAL: 1646 case TIOCGSERIAL:
1665 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number); 1647 dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
1666 return get_serial_info(edge_port, (struct serial_struct __user *) arg); 1648 return get_serial_info(edge_port, (struct serial_struct __user *) arg);
1667 1649
1668 case TIOCMIWAIT: 1650 case TIOCMIWAIT:
1669 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number); 1651 dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
1670 cprev = edge_port->icount; 1652 cprev = edge_port->icount;
1671 while (1) { 1653 while (1) {
1672 prepare_to_wait(&edge_port->delta_msr_wait, 1654 prepare_to_wait(&edge_port->delta_msr_wait,
1673 &wait, TASK_INTERRUPTIBLE); 1655 &wait, TASK_INTERRUPTIBLE);
1674 schedule(); 1656 schedule();
1675 finish_wait(&edge_port->delta_msr_wait, &wait); 1657 finish_wait(&edge_port->delta_msr_wait, &wait);
1676 /* see if a signal did it */ 1658 /* see if a signal did it */
1677 if (signal_pending(current)) 1659 if (signal_pending(current))
1678 return -ERESTARTSYS; 1660 return -ERESTARTSYS;
1679 cnow = edge_port->icount; 1661 cnow = edge_port->icount;
1680 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 1662 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1681 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) 1663 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1682 return -EIO; /* no change => error */ 1664 return -EIO; /* no change => error */
1683 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 1665 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1684 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 1666 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1685 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 1667 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1686 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 1668 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1687 return 0; 1669 return 0;
1688 } 1670 }
1689 cprev = cnow; 1671 cprev = cnow;
1690 } 1672 }
1691 /* NOTREACHED */ 1673 /* NOTREACHED */
1692 break; 1674 break;
1693 1675
1694 } 1676 }
1695 return -ENOIOCTLCMD; 1677 return -ENOIOCTLCMD;
1696 } 1678 }
1697 1679
1698 1680
1699 /***************************************************************************** 1681 /*****************************************************************************
1700 * SerialBreak 1682 * SerialBreak
1701 * this function sends a break to the port 1683 * this function sends a break to the port
1702 *****************************************************************************/ 1684 *****************************************************************************/
1703 static void edge_break(struct tty_struct *tty, int break_state) 1685 static void edge_break(struct tty_struct *tty, int break_state)
1704 { 1686 {
1705 struct usb_serial_port *port = tty->driver_data; 1687 struct usb_serial_port *port = tty->driver_data;
1706 struct edgeport_port *edge_port = usb_get_serial_port_data(port); 1688 struct edgeport_port *edge_port = usb_get_serial_port_data(port);
1707 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial); 1689 struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
1708 int status; 1690 int status;
1709 1691
1710 if ((!edge_serial->is_epic) || 1692 if ((!edge_serial->is_epic) ||
1711 ((edge_serial->is_epic) && 1693 ((edge_serial->is_epic) &&
1712 (edge_serial->epic_descriptor.Supports.IOSPChase))) { 1694 (edge_serial->epic_descriptor.Supports.IOSPChase))) {
1713 /* flush and chase */ 1695 /* flush and chase */
1714 edge_port->chaseResponsePending = true; 1696 edge_port->chaseResponsePending = true;
1715 1697
1716 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__); 1698 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
1717 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0); 1699 status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
1718 if (status == 0) { 1700 if (status == 0) {
1719 /* block until chase finished */ 1701 /* block until chase finished */
1720 block_until_chase_response(edge_port); 1702 block_until_chase_response(edge_port);
1721 } else { 1703 } else {
1722 edge_port->chaseResponsePending = false; 1704 edge_port->chaseResponsePending = false;
1723 } 1705 }
1724 } 1706 }
1725 1707
1726 if ((!edge_serial->is_epic) || 1708 if ((!edge_serial->is_epic) ||
1727 ((edge_serial->is_epic) && 1709 ((edge_serial->is_epic) &&
1728 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) { 1710 (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
1729 if (break_state == -1) { 1711 if (break_state == -1) {
1730 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__); 1712 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
1731 status = send_iosp_ext_cmd(edge_port, 1713 status = send_iosp_ext_cmd(edge_port,
1732 IOSP_CMD_SET_BREAK, 0); 1714 IOSP_CMD_SET_BREAK, 0);
1733 } else { 1715 } else {
1734 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__); 1716 dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
1735 status = send_iosp_ext_cmd(edge_port, 1717 status = send_iosp_ext_cmd(edge_port,
1736 IOSP_CMD_CLEAR_BREAK, 0); 1718 IOSP_CMD_CLEAR_BREAK, 0);
1737 } 1719 }
1738 if (status) 1720 if (status)
1739 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n", 1721 dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
1740 __func__); 1722 __func__);
1741 } 1723 }
1742 } 1724 }
1743 1725
1744 1726
1745 /***************************************************************************** 1727 /*****************************************************************************
1746 * process_rcvd_data 1728 * process_rcvd_data
1747 * this function handles the data received on the bulk in pipe. 1729 * this function handles the data received on the bulk in pipe.
1748 *****************************************************************************/ 1730 *****************************************************************************/
1749 static void process_rcvd_data(struct edgeport_serial *edge_serial, 1731 static void process_rcvd_data(struct edgeport_serial *edge_serial,
1750 unsigned char *buffer, __u16 bufferLength) 1732 unsigned char *buffer, __u16 bufferLength)
1751 { 1733 {
1752 struct device *dev = &edge_serial->serial->dev->dev; 1734 struct device *dev = &edge_serial->serial->dev->dev;
1753 struct usb_serial_port *port; 1735 struct usb_serial_port *port;
1754 struct edgeport_port *edge_port; 1736 struct edgeport_port *edge_port;
1755 __u16 lastBufferLength; 1737 __u16 lastBufferLength;
1756 __u16 rxLen; 1738 __u16 rxLen;
1757 1739
1758 lastBufferLength = bufferLength + 1; 1740 lastBufferLength = bufferLength + 1;
1759 1741
1760 while (bufferLength > 0) { 1742 while (bufferLength > 0) {
1761 /* failsafe incase we get a message that we don't understand */ 1743 /* failsafe incase we get a message that we don't understand */
1762 if (lastBufferLength == bufferLength) { 1744 if (lastBufferLength == bufferLength) {
1763 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__); 1745 dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
1764 break; 1746 break;
1765 } 1747 }
1766 lastBufferLength = bufferLength; 1748 lastBufferLength = bufferLength;
1767 1749
1768 switch (edge_serial->rxState) { 1750 switch (edge_serial->rxState) {
1769 case EXPECT_HDR1: 1751 case EXPECT_HDR1:
1770 edge_serial->rxHeader1 = *buffer; 1752 edge_serial->rxHeader1 = *buffer;
1771 ++buffer; 1753 ++buffer;
1772 --bufferLength; 1754 --bufferLength;
1773 1755
1774 if (bufferLength == 0) { 1756 if (bufferLength == 0) {
1775 edge_serial->rxState = EXPECT_HDR2; 1757 edge_serial->rxState = EXPECT_HDR2;
1776 break; 1758 break;
1777 } 1759 }
1778 /* otherwise, drop on through */ 1760 /* otherwise, drop on through */
1779 case EXPECT_HDR2: 1761 case EXPECT_HDR2:
1780 edge_serial->rxHeader2 = *buffer; 1762 edge_serial->rxHeader2 = *buffer;
1781 ++buffer; 1763 ++buffer;
1782 --bufferLength; 1764 --bufferLength;
1783 1765
1784 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__, 1766 dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
1785 edge_serial->rxHeader1, edge_serial->rxHeader2); 1767 edge_serial->rxHeader1, edge_serial->rxHeader2);
1786 /* Process depending on whether this header is 1768 /* Process depending on whether this header is
1787 * data or status */ 1769 * data or status */
1788 1770
1789 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) { 1771 if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
1790 /* Decode this status header and go to 1772 /* Decode this status header and go to
1791 * EXPECT_HDR1 (if we can process the status 1773 * EXPECT_HDR1 (if we can process the status
1792 * with only 2 bytes), or go to EXPECT_HDR3 to 1774 * with only 2 bytes), or go to EXPECT_HDR3 to
1793 * get the third byte. */ 1775 * get the third byte. */
1794 edge_serial->rxPort = 1776 edge_serial->rxPort =
1795 IOSP_GET_HDR_PORT(edge_serial->rxHeader1); 1777 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1796 edge_serial->rxStatusCode = 1778 edge_serial->rxStatusCode =
1797 IOSP_GET_STATUS_CODE( 1779 IOSP_GET_STATUS_CODE(
1798 edge_serial->rxHeader1); 1780 edge_serial->rxHeader1);
1799 1781
1800 if (!IOSP_STATUS_IS_2BYTE( 1782 if (!IOSP_STATUS_IS_2BYTE(
1801 edge_serial->rxStatusCode)) { 1783 edge_serial->rxStatusCode)) {
1802 /* This status needs additional bytes. 1784 /* This status needs additional bytes.
1803 * Save what we have and then wait for 1785 * Save what we have and then wait for
1804 * more data. 1786 * more data.
1805 */ 1787 */
1806 edge_serial->rxStatusParam 1788 edge_serial->rxStatusParam
1807 = edge_serial->rxHeader2; 1789 = edge_serial->rxHeader2;
1808 edge_serial->rxState = EXPECT_HDR3; 1790 edge_serial->rxState = EXPECT_HDR3;
1809 break; 1791 break;
1810 } 1792 }
1811 /* We have all the header bytes, process the 1793 /* We have all the header bytes, process the
1812 status now */ 1794 status now */
1813 process_rcvd_status(edge_serial, 1795 process_rcvd_status(edge_serial,
1814 edge_serial->rxHeader2, 0); 1796 edge_serial->rxHeader2, 0);
1815 edge_serial->rxState = EXPECT_HDR1; 1797 edge_serial->rxState = EXPECT_HDR1;
1816 break; 1798 break;
1817 } else { 1799 } else {
1818 edge_serial->rxPort = 1800 edge_serial->rxPort =
1819 IOSP_GET_HDR_PORT(edge_serial->rxHeader1); 1801 IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
1820 edge_serial->rxBytesRemaining = 1802 edge_serial->rxBytesRemaining =
1821 IOSP_GET_HDR_DATA_LEN( 1803 IOSP_GET_HDR_DATA_LEN(
1822 edge_serial->rxHeader1, 1804 edge_serial->rxHeader1,
1823 edge_serial->rxHeader2); 1805 edge_serial->rxHeader2);
1824 dev_dbg(dev, "%s - Data for Port %u Len %u\n", 1806 dev_dbg(dev, "%s - Data for Port %u Len %u\n",
1825 __func__, 1807 __func__,
1826 edge_serial->rxPort, 1808 edge_serial->rxPort,
1827 edge_serial->rxBytesRemaining); 1809 edge_serial->rxBytesRemaining);
1828 1810
1829 /* ASSERT(DevExt->RxPort < DevExt->NumPorts); 1811 /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
1830 * ASSERT(DevExt->RxBytesRemaining < 1812 * ASSERT(DevExt->RxBytesRemaining <
1831 * IOSP_MAX_DATA_LENGTH); 1813 * IOSP_MAX_DATA_LENGTH);
1832 */ 1814 */
1833 1815
1834 if (bufferLength == 0) { 1816 if (bufferLength == 0) {
1835 edge_serial->rxState = EXPECT_DATA; 1817 edge_serial->rxState = EXPECT_DATA;
1836 break; 1818 break;
1837 } 1819 }
1838 /* Else, drop through */ 1820 /* Else, drop through */
1839 } 1821 }
1840 case EXPECT_DATA: /* Expect data */ 1822 case EXPECT_DATA: /* Expect data */
1841 if (bufferLength < edge_serial->rxBytesRemaining) { 1823 if (bufferLength < edge_serial->rxBytesRemaining) {
1842 rxLen = bufferLength; 1824 rxLen = bufferLength;
1843 /* Expect data to start next buffer */ 1825 /* Expect data to start next buffer */
1844 edge_serial->rxState = EXPECT_DATA; 1826 edge_serial->rxState = EXPECT_DATA;
1845 } else { 1827 } else {
1846 /* BufLen >= RxBytesRemaining */ 1828 /* BufLen >= RxBytesRemaining */
1847 rxLen = edge_serial->rxBytesRemaining; 1829 rxLen = edge_serial->rxBytesRemaining;
1848 /* Start another header next time */ 1830 /* Start another header next time */
1849 edge_serial->rxState = EXPECT_HDR1; 1831 edge_serial->rxState = EXPECT_HDR1;
1850 } 1832 }
1851 1833
1852 bufferLength -= rxLen; 1834 bufferLength -= rxLen;
1853 edge_serial->rxBytesRemaining -= rxLen; 1835 edge_serial->rxBytesRemaining -= rxLen;
1854 1836
1855 /* spit this data back into the tty driver if this 1837 /* spit this data back into the tty driver if this
1856 port is open */ 1838 port is open */
1857 if (rxLen) { 1839 if (rxLen) {
1858 port = edge_serial->serial->port[ 1840 port = edge_serial->serial->port[
1859 edge_serial->rxPort]; 1841 edge_serial->rxPort];
1860 edge_port = usb_get_serial_port_data(port); 1842 edge_port = usb_get_serial_port_data(port);
1861 if (edge_port->open) { 1843 if (edge_port->open) {
1862 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n", 1844 dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
1863 __func__, rxLen, 1845 __func__, rxLen,
1864 edge_serial->rxPort); 1846 edge_serial->rxPort);
1865 edge_tty_recv(edge_port->port, buffer, 1847 edge_tty_recv(edge_port->port, buffer,
1866 rxLen); 1848 rxLen);
1867 edge_port->icount.rx += rxLen; 1849 edge_port->icount.rx += rxLen;
1868 } 1850 }
1869 buffer += rxLen; 1851 buffer += rxLen;
1870 } 1852 }
1871 break; 1853 break;
1872 1854
1873 case EXPECT_HDR3: /* Expect 3rd byte of status header */ 1855 case EXPECT_HDR3: /* Expect 3rd byte of status header */
1874 edge_serial->rxHeader3 = *buffer; 1856 edge_serial->rxHeader3 = *buffer;
1875 ++buffer; 1857 ++buffer;
1876 --bufferLength; 1858 --bufferLength;
1877 1859
1878 /* We have all the header bytes, process the 1860 /* We have all the header bytes, process the
1879 status now */ 1861 status now */
1880 process_rcvd_status(edge_serial, 1862 process_rcvd_status(edge_serial,
1881 edge_serial->rxStatusParam, 1863 edge_serial->rxStatusParam,
1882 edge_serial->rxHeader3); 1864 edge_serial->rxHeader3);
1883 edge_serial->rxState = EXPECT_HDR1; 1865 edge_serial->rxState = EXPECT_HDR1;
1884 break; 1866 break;
1885 } 1867 }
1886 } 1868 }
1887 } 1869 }
1888 1870
1889 1871
1890 /***************************************************************************** 1872 /*****************************************************************************
1891 * process_rcvd_status 1873 * process_rcvd_status
1892 * this function handles the any status messages received on the 1874 * this function handles the any status messages received on the
1893 * bulk in pipe. 1875 * bulk in pipe.
1894 *****************************************************************************/ 1876 *****************************************************************************/
1895 static void process_rcvd_status(struct edgeport_serial *edge_serial, 1877 static void process_rcvd_status(struct edgeport_serial *edge_serial,
1896 __u8 byte2, __u8 byte3) 1878 __u8 byte2, __u8 byte3)
1897 { 1879 {
1898 struct usb_serial_port *port; 1880 struct usb_serial_port *port;
1899 struct edgeport_port *edge_port; 1881 struct edgeport_port *edge_port;
1900 struct tty_struct *tty; 1882 struct tty_struct *tty;
1901 struct device *dev; 1883 struct device *dev;
1902 __u8 code = edge_serial->rxStatusCode; 1884 __u8 code = edge_serial->rxStatusCode;
1903 1885
1904 /* switch the port pointer to the one being currently talked about */ 1886 /* switch the port pointer to the one being currently talked about */
1905 port = edge_serial->serial->port[edge_serial->rxPort]; 1887 port = edge_serial->serial->port[edge_serial->rxPort];
1906 edge_port = usb_get_serial_port_data(port); 1888 edge_port = usb_get_serial_port_data(port);
1907 if (edge_port == NULL) { 1889 if (edge_port == NULL) {
1908 dev_err(&edge_serial->serial->dev->dev, 1890 dev_err(&edge_serial->serial->dev->dev,
1909 "%s - edge_port == NULL for port %d\n", 1891 "%s - edge_port == NULL for port %d\n",
1910 __func__, edge_serial->rxPort); 1892 __func__, edge_serial->rxPort);
1911 return; 1893 return;
1912 } 1894 }
1913 dev = &port->dev; 1895 dev = &port->dev;
1914 1896
1915 if (code == IOSP_EXT_STATUS) { 1897 if (code == IOSP_EXT_STATUS) {
1916 switch (byte2) { 1898 switch (byte2) {
1917 case IOSP_EXT_STATUS_CHASE_RSP: 1899 case IOSP_EXT_STATUS_CHASE_RSP:
1918 /* we want to do EXT status regardless of port 1900 /* we want to do EXT status regardless of port
1919 * open/closed */ 1901 * open/closed */
1920 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n", 1902 dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
1921 __func__, edge_serial->rxPort, byte3); 1903 __func__, edge_serial->rxPort, byte3);
1922 /* Currently, the only EXT_STATUS is Chase, so process 1904 /* Currently, the only EXT_STATUS is Chase, so process
1923 * here instead of one more call to one more subroutine 1905 * here instead of one more call to one more subroutine
1924 * If/when more EXT_STATUS, there'll be more work to do 1906 * If/when more EXT_STATUS, there'll be more work to do
1925 * Also, we currently clear flag and close the port 1907 * Also, we currently clear flag and close the port
1926 * regardless of content of above's Byte3. 1908 * regardless of content of above's Byte3.
1927 * We could choose to do something else when Byte3 says 1909 * We could choose to do something else when Byte3 says
1928 * Timeout on Chase from Edgeport, like wait longer in 1910 * Timeout on Chase from Edgeport, like wait longer in
1929 * block_until_chase_response, but for now we don't. 1911 * block_until_chase_response, but for now we don't.
1930 */ 1912 */
1931 edge_port->chaseResponsePending = false; 1913 edge_port->chaseResponsePending = false;
1932 wake_up(&edge_port->wait_chase); 1914 wake_up(&edge_port->wait_chase);
1933 return; 1915 return;
1934 1916
1935 case IOSP_EXT_STATUS_RX_CHECK_RSP: 1917 case IOSP_EXT_STATUS_RX_CHECK_RSP:
1936 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", 1918 dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
1937 __func__, edge_serial->rxPort, byte3); 1919 __func__, edge_serial->rxPort, byte3);
1938 /* Port->RxCheckRsp = true; */ 1920 /* Port->RxCheckRsp = true; */
1939 return; 1921 return;
1940 } 1922 }
1941 } 1923 }
1942 1924
1943 if (code == IOSP_STATUS_OPEN_RSP) { 1925 if (code == IOSP_STATUS_OPEN_RSP) {
1944 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3); 1926 edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
1945 edge_port->maxTxCredits = edge_port->txCredits; 1927 edge_port->maxTxCredits = edge_port->txCredits;
1946 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n", 1928 dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
1947 __func__, edge_serial->rxPort, byte2, edge_port->txCredits); 1929 __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
1948 handle_new_msr(edge_port, byte2); 1930 handle_new_msr(edge_port, byte2);
1949 1931
1950 /* send the current line settings to the port so we are 1932 /* send the current line settings to the port so we are
1951 in sync with any further termios calls */ 1933 in sync with any further termios calls */
1952 tty = tty_port_tty_get(&edge_port->port->port); 1934 tty = tty_port_tty_get(&edge_port->port->port);
1953 if (tty) { 1935 if (tty) {
1954 change_port_settings(tty, 1936 change_port_settings(tty,
1955 edge_port, &tty->termios); 1937 edge_port, &tty->termios);
1956 tty_kref_put(tty); 1938 tty_kref_put(tty);
1957 } 1939 }
1958 1940
1959 /* we have completed the open */ 1941 /* we have completed the open */
1960 edge_port->openPending = false; 1942 edge_port->openPending = false;
1961 edge_port->open = true; 1943 edge_port->open = true;
1962 wake_up(&edge_port->wait_open); 1944 wake_up(&edge_port->wait_open);
1963 return; 1945 return;
1964 } 1946 }
1965 1947
1966 /* If port is closed, silently discard all rcvd status. We can 1948 /* If port is closed, silently discard all rcvd status. We can
1967 * have cases where buffered status is received AFTER the close 1949 * have cases where buffered status is received AFTER the close
1968 * port command is sent to the Edgeport. 1950 * port command is sent to the Edgeport.
1969 */ 1951 */
1970 if (!edge_port->open || edge_port->closePending) 1952 if (!edge_port->open || edge_port->closePending)
1971 return; 1953 return;
1972 1954
1973 switch (code) { 1955 switch (code) {
1974 /* Not currently sent by Edgeport */ 1956 /* Not currently sent by Edgeport */
1975 case IOSP_STATUS_LSR: 1957 case IOSP_STATUS_LSR:
1976 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n", 1958 dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
1977 __func__, edge_serial->rxPort, byte2); 1959 __func__, edge_serial->rxPort, byte2);
1978 handle_new_lsr(edge_port, false, byte2, 0); 1960 handle_new_lsr(edge_port, false, byte2, 0);
1979 break; 1961 break;
1980 1962
1981 case IOSP_STATUS_LSR_DATA: 1963 case IOSP_STATUS_LSR_DATA:
1982 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n", 1964 dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
1983 __func__, edge_serial->rxPort, byte2, byte3); 1965 __func__, edge_serial->rxPort, byte2, byte3);
1984 /* byte2 is LSR Register */ 1966 /* byte2 is LSR Register */
1985 /* byte3 is broken data byte */ 1967 /* byte3 is broken data byte */
1986 handle_new_lsr(edge_port, true, byte2, byte3); 1968 handle_new_lsr(edge_port, true, byte2, byte3);
1987 break; 1969 break;
1988 /* 1970 /*
1989 * case IOSP_EXT_4_STATUS: 1971 * case IOSP_EXT_4_STATUS:
1990 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n", 1972 * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
1991 * __func__, edge_serial->rxPort, byte2, byte3); 1973 * __func__, edge_serial->rxPort, byte2, byte3);
1992 * break; 1974 * break;
1993 */ 1975 */
1994 case IOSP_STATUS_MSR: 1976 case IOSP_STATUS_MSR:
1995 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n", 1977 dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
1996 __func__, edge_serial->rxPort, byte2); 1978 __func__, edge_serial->rxPort, byte2);
1997 /* 1979 /*
1998 * Process this new modem status and generate appropriate 1980 * Process this new modem status and generate appropriate
1999 * events, etc, based on the new status. This routine 1981 * events, etc, based on the new status. This routine
2000 * also saves the MSR in Port->ShadowMsr. 1982 * also saves the MSR in Port->ShadowMsr.
2001 */ 1983 */
2002 handle_new_msr(edge_port, byte2); 1984 handle_new_msr(edge_port, byte2);
2003 break; 1985 break;
2004 1986
2005 default: 1987 default:
2006 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code); 1988 dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
2007 break; 1989 break;
2008 } 1990 }
2009 } 1991 }
2010 1992
2011 1993
2012 /***************************************************************************** 1994 /*****************************************************************************
2013 * edge_tty_recv 1995 * edge_tty_recv
2014 * this function passes data on to the tty flip buffer 1996 * this function passes data on to the tty flip buffer
2015 *****************************************************************************/ 1997 *****************************************************************************/
2016 static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data, 1998 static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
2017 int length) 1999 int length)
2018 { 2000 {
2019 int cnt; 2001 int cnt;
2020 2002
2021 cnt = tty_insert_flip_string(&port->port, data, length); 2003 cnt = tty_insert_flip_string(&port->port, data, length);
2022 if (cnt < length) { 2004 if (cnt < length) {
2023 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n", 2005 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
2024 __func__, length - cnt); 2006 __func__, length - cnt);
2025 } 2007 }
2026 data += cnt; 2008 data += cnt;
2027 length -= cnt; 2009 length -= cnt;
2028 2010
2029 tty_flip_buffer_push(&port->port); 2011 tty_flip_buffer_push(&port->port);
2030 } 2012 }
2031 2013
2032 2014
2033 /***************************************************************************** 2015 /*****************************************************************************
2034 * handle_new_msr 2016 * handle_new_msr
2035 * this function handles any change to the msr register for a port. 2017 * this function handles any change to the msr register for a port.
2036 *****************************************************************************/ 2018 *****************************************************************************/
2037 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr) 2019 static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
2038 { 2020 {
2039 struct async_icount *icount; 2021 struct async_icount *icount;
2040 2022
2041 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR | 2023 if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
2042 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) { 2024 EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
2043 icount = &edge_port->icount; 2025 icount = &edge_port->icount;
2044 2026
2045 /* update input line counters */ 2027 /* update input line counters */
2046 if (newMsr & EDGEPORT_MSR_DELTA_CTS) 2028 if (newMsr & EDGEPORT_MSR_DELTA_CTS)
2047 icount->cts++; 2029 icount->cts++;
2048 if (newMsr & EDGEPORT_MSR_DELTA_DSR) 2030 if (newMsr & EDGEPORT_MSR_DELTA_DSR)
2049 icount->dsr++; 2031 icount->dsr++;
2050 if (newMsr & EDGEPORT_MSR_DELTA_CD) 2032 if (newMsr & EDGEPORT_MSR_DELTA_CD)
2051 icount->dcd++; 2033 icount->dcd++;
2052 if (newMsr & EDGEPORT_MSR_DELTA_RI) 2034 if (newMsr & EDGEPORT_MSR_DELTA_RI)
2053 icount->rng++; 2035 icount->rng++;
2054 wake_up_interruptible(&edge_port->delta_msr_wait); 2036 wake_up_interruptible(&edge_port->delta_msr_wait);
2055 } 2037 }
2056 2038
2057 /* Save the new modem status */ 2039 /* Save the new modem status */
2058 edge_port->shadowMSR = newMsr & 0xf0; 2040 edge_port->shadowMSR = newMsr & 0xf0;
2059 } 2041 }
2060 2042
2061 2043
2062 /***************************************************************************** 2044 /*****************************************************************************
2063 * handle_new_lsr 2045 * handle_new_lsr
2064 * this function handles any change to the lsr register for a port. 2046 * this function handles any change to the lsr register for a port.
2065 *****************************************************************************/ 2047 *****************************************************************************/
2066 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData, 2048 static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
2067 __u8 lsr, __u8 data) 2049 __u8 lsr, __u8 data)
2068 { 2050 {
2069 __u8 newLsr = (__u8) (lsr & (__u8) 2051 __u8 newLsr = (__u8) (lsr & (__u8)
2070 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK)); 2052 (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
2071 struct async_icount *icount; 2053 struct async_icount *icount;
2072 2054
2073 edge_port->shadowLSR = lsr; 2055 edge_port->shadowLSR = lsr;
2074 2056
2075 if (newLsr & LSR_BREAK) { 2057 if (newLsr & LSR_BREAK) {
2076 /* 2058 /*
2077 * Parity and Framing errors only count if they 2059 * Parity and Framing errors only count if they
2078 * occur exclusive of a break being 2060 * occur exclusive of a break being
2079 * received. 2061 * received.
2080 */ 2062 */
2081 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK); 2063 newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
2082 } 2064 }
2083 2065
2084 /* Place LSR data byte into Rx buffer */ 2066 /* Place LSR data byte into Rx buffer */
2085 if (lsrData) 2067 if (lsrData)
2086 edge_tty_recv(edge_port->port, &data, 1); 2068 edge_tty_recv(edge_port->port, &data, 1);
2087 2069
2088 /* update input line counters */ 2070 /* update input line counters */
2089 icount = &edge_port->icount; 2071 icount = &edge_port->icount;
2090 if (newLsr & LSR_BREAK) 2072 if (newLsr & LSR_BREAK)
2091 icount->brk++; 2073 icount->brk++;
2092 if (newLsr & LSR_OVER_ERR) 2074 if (newLsr & LSR_OVER_ERR)
2093 icount->overrun++; 2075 icount->overrun++;
2094 if (newLsr & LSR_PAR_ERR) 2076 if (newLsr & LSR_PAR_ERR)
2095 icount->parity++; 2077 icount->parity++;
2096 if (newLsr & LSR_FRM_ERR) 2078 if (newLsr & LSR_FRM_ERR)
2097 icount->frame++; 2079 icount->frame++;
2098 } 2080 }
2099 2081
2100 2082
2101 /**************************************************************************** 2083 /****************************************************************************
2102 * sram_write 2084 * sram_write
2103 * writes a number of bytes to the Edgeport device's sram starting at the 2085 * writes a number of bytes to the Edgeport device's sram starting at the
2104 * given address. 2086 * given address.
2105 * If successful returns the number of bytes written, otherwise it returns 2087 * If successful returns the number of bytes written, otherwise it returns
2106 * a negative error number of the problem. 2088 * a negative error number of the problem.
2107 ****************************************************************************/ 2089 ****************************************************************************/
2108 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr, 2090 static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2109 __u16 length, const __u8 *data) 2091 __u16 length, const __u8 *data)
2110 { 2092 {
2111 int result; 2093 int result;
2112 __u16 current_length; 2094 __u16 current_length;
2113 unsigned char *transfer_buffer; 2095 unsigned char *transfer_buffer;
2114 2096
2115 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length); 2097 dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
2116 2098
2117 transfer_buffer = kmalloc(64, GFP_KERNEL); 2099 transfer_buffer = kmalloc(64, GFP_KERNEL);
2118 if (!transfer_buffer) { 2100 if (!transfer_buffer) {
2119 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", 2101 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2120 __func__, 64); 2102 __func__, 64);
2121 return -ENOMEM; 2103 return -ENOMEM;
2122 } 2104 }
2123 2105
2124 /* need to split these writes up into 64 byte chunks */ 2106 /* need to split these writes up into 64 byte chunks */
2125 result = 0; 2107 result = 0;
2126 while (length > 0) { 2108 while (length > 0) {
2127 if (length > 64) 2109 if (length > 64)
2128 current_length = 64; 2110 current_length = 64;
2129 else 2111 else
2130 current_length = length; 2112 current_length = length;
2131 2113
2132 /* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */ 2114 /* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
2133 memcpy(transfer_buffer, data, current_length); 2115 memcpy(transfer_buffer, data, current_length);
2134 result = usb_control_msg(serial->dev, 2116 result = usb_control_msg(serial->dev,
2135 usb_sndctrlpipe(serial->dev, 0), 2117 usb_sndctrlpipe(serial->dev, 0),
2136 USB_REQUEST_ION_WRITE_RAM, 2118 USB_REQUEST_ION_WRITE_RAM,
2137 0x40, addr, extAddr, transfer_buffer, 2119 0x40, addr, extAddr, transfer_buffer,
2138 current_length, 300); 2120 current_length, 300);
2139 if (result < 0) 2121 if (result < 0)
2140 break; 2122 break;
2141 length -= current_length; 2123 length -= current_length;
2142 addr += current_length; 2124 addr += current_length;
2143 data += current_length; 2125 data += current_length;
2144 } 2126 }
2145 2127
2146 kfree(transfer_buffer); 2128 kfree(transfer_buffer);
2147 return result; 2129 return result;
2148 } 2130 }
2149 2131
2150 2132
2151 /**************************************************************************** 2133 /****************************************************************************
2152 * rom_write 2134 * rom_write
2153 * writes a number of bytes to the Edgeport device's ROM starting at the 2135 * writes a number of bytes to the Edgeport device's ROM starting at the
2154 * given address. 2136 * given address.
2155 * If successful returns the number of bytes written, otherwise it returns 2137 * If successful returns the number of bytes written, otherwise it returns
2156 * a negative error number of the problem. 2138 * a negative error number of the problem.
2157 ****************************************************************************/ 2139 ****************************************************************************/
2158 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr, 2140 static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
2159 __u16 length, const __u8 *data) 2141 __u16 length, const __u8 *data)
2160 { 2142 {
2161 int result; 2143 int result;
2162 __u16 current_length; 2144 __u16 current_length;
2163 unsigned char *transfer_buffer; 2145 unsigned char *transfer_buffer;
2164 2146
2165 transfer_buffer = kmalloc(64, GFP_KERNEL); 2147 transfer_buffer = kmalloc(64, GFP_KERNEL);
2166 if (!transfer_buffer) { 2148 if (!transfer_buffer) {
2167 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n", 2149 dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
2168 __func__, 64); 2150 __func__, 64);
2169 return -ENOMEM; 2151 return -ENOMEM;
2170 } 2152 }
2171 2153
2172 /* need to split these writes up into 64 byte chunks */ 2154 /* need to split these writes up into 64 byte chunks */
2173 result = 0; 2155 result = 0;
2174 while (length > 0) { 2156 while (length > 0) {
2175 if (length > 64) 2157 if (length > 64)
2176 current_length = 64; 2158 current_length = 64;
2177 else 2159 else
2178 current_length = length; 2160 current_length = length;
2179 memcpy(transfer_buffer, data, current_length); 2161 memcpy(transfer_buffer, data, current_length);
2180 result = usb_control_msg(serial->dev, 2162 result = usb_control_msg(serial->dev,
2181 usb_sndctrlpipe(serial->dev, 0), 2163 usb_sndctrlpipe(serial->dev, 0),
2182 USB_REQUEST_ION_WRITE_ROM, 0x40, 2164 USB_REQUEST_ION_WRITE_ROM, 0x40,
2183 addr, extAddr, 2165 addr, extAddr,
2184 transfer_buffer, current_length, 300); 2166 transfer_buffer, current_length, 300);
2185 if (result < 0) 2167 if (result < 0)
2186 break; 2168 break;
2187 length -= current_length; 2169 length -= current_length;
2188 addr += current_length; 2170 addr += current_length;
2189 data += current_length; 2171 data += current_length;
2190 } 2172 }
2191 2173
2192 kfree(transfer_buffer); 2174 kfree(transfer_buffer);
2193 return result; 2175 return result;
2194 } 2176 }
2195 2177
2196 2178
2197 /**************************************************************************** 2179 /****************************************************************************
2198 * rom_read 2180 * rom_read
2199 * reads a number of bytes from the Edgeport device starting at the given 2181 * reads a number of bytes from the Edgeport device starting at the given
2200 * address. 2182 * address.
2201 * If successful returns the number of bytes read, otherwise it returns 2183 * If successful returns the number of bytes read, otherwise it returns
2202 * a negative error number of the problem. 2184 * a negative error number of the problem.
2203 ****************************************************************************/ 2185 ****************************************************************************/
2204 static int rom_read(struct usb_serial *serial, __u16 extAddr, 2186 static int rom_read(struct usb_serial *serial, __u16 extAddr,
2205 __u16 addr, __u16 length, __u8 *data) 2187 __u16 addr, __u16 length, __u8 *data)
2206 { 2188 {
2207 int result; 2189 int result;
2208 __u16 current_length; 2190 __u16 current_length;
2209 unsigned char *transfer_buffer; 2191 unsigned char *transfer_buffer;
2210 2192
2211 transfer_buffer = kmalloc(64, GFP_KERNEL); 2193 transfer_buffer = kmalloc(64, GFP_KERNEL);
2212 if (!transfer_buffer) { 2194 if (!transfer_buffer) {
2213 dev_err(&serial->dev->dev, 2195 dev_err(&serial->dev->dev,
2214 "%s - kmalloc(%d) failed.\n", __func__, 64); 2196 "%s - kmalloc(%d) failed.\n", __func__, 64);
2215 return -ENOMEM; 2197 return -ENOMEM;
2216 } 2198 }
2217 2199
2218 /* need to split these reads up into 64 byte chunks */ 2200 /* need to split these reads up into 64 byte chunks */
2219 result = 0; 2201 result = 0;
2220 while (length > 0) { 2202 while (length > 0) {
2221 if (length > 64) 2203 if (length > 64)
2222 current_length = 64; 2204 current_length = 64;
2223 else 2205 else
2224 current_length = length; 2206 current_length = length;
2225 result = usb_control_msg(serial->dev, 2207 result = usb_control_msg(serial->dev,
2226 usb_rcvctrlpipe(serial->dev, 0), 2208 usb_rcvctrlpipe(serial->dev, 0),
2227 USB_REQUEST_ION_READ_ROM, 2209 USB_REQUEST_ION_READ_ROM,
2228 0xC0, addr, extAddr, transfer_buffer, 2210 0xC0, addr, extAddr, transfer_buffer,
2229 current_length, 300); 2211 current_length, 300);
2230 if (result < 0) 2212 if (result < 0)
2231 break; 2213 break;
2232 memcpy(data, transfer_buffer, current_length); 2214 memcpy(data, transfer_buffer, current_length);
2233 length -= current_length; 2215 length -= current_length;
2234 addr += current_length; 2216 addr += current_length;
2235 data += current_length; 2217 data += current_length;
2236 } 2218 }
2237 2219
2238 kfree(transfer_buffer); 2220 kfree(transfer_buffer);
2239 return result; 2221 return result;
2240 } 2222 }
2241 2223
2242 2224
2243 /**************************************************************************** 2225 /****************************************************************************
2244 * send_iosp_ext_cmd 2226 * send_iosp_ext_cmd
2245 * Is used to send a IOSP message to the Edgeport device 2227 * Is used to send a IOSP message to the Edgeport device
2246 ****************************************************************************/ 2228 ****************************************************************************/
2247 static int send_iosp_ext_cmd(struct edgeport_port *edge_port, 2229 static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
2248 __u8 command, __u8 param) 2230 __u8 command, __u8 param)
2249 { 2231 {
2250 unsigned char *buffer; 2232 unsigned char *buffer;
2251 unsigned char *currentCommand; 2233 unsigned char *currentCommand;
2252 int length = 0; 2234 int length = 0;
2253 int status = 0; 2235 int status = 0;
2254 2236
2255 buffer = kmalloc(10, GFP_ATOMIC); 2237 buffer = kmalloc(10, GFP_ATOMIC);
2256 if (!buffer) { 2238 if (!buffer) {
2257 dev_err(&edge_port->port->dev, 2239 dev_err(&edge_port->port->dev,
2258 "%s - kmalloc(%d) failed.\n", __func__, 10); 2240 "%s - kmalloc(%d) failed.\n", __func__, 10);
2259 return -ENOMEM; 2241 return -ENOMEM;
2260 } 2242 }
2261 2243
2262 currentCommand = buffer; 2244 currentCommand = buffer;
2263 2245
2264 MAKE_CMD_EXT_CMD(&currentCommand, &length, 2246 MAKE_CMD_EXT_CMD(&currentCommand, &length,
2265 edge_port->port->number - edge_port->port->serial->minor, 2247 edge_port->port->number - edge_port->port->serial->minor,
2266 command, param); 2248 command, param);
2267 2249
2268 status = write_cmd_usb(edge_port, buffer, length); 2250 status = write_cmd_usb(edge_port, buffer, length);
2269 if (status) { 2251 if (status) {
2270 /* something bad happened, let's free up the memory */ 2252 /* something bad happened, let's free up the memory */
2271 kfree(buffer); 2253 kfree(buffer);
2272 } 2254 }
2273 2255
2274 return status; 2256 return status;
2275 } 2257 }
2276 2258
2277 2259
2278 /***************************************************************************** 2260 /*****************************************************************************
2279 * write_cmd_usb 2261 * write_cmd_usb
2280 * this function writes the given buffer out to the bulk write endpoint. 2262 * this function writes the given buffer out to the bulk write endpoint.
2281 *****************************************************************************/ 2263 *****************************************************************************/
2282 static int write_cmd_usb(struct edgeport_port *edge_port, 2264 static int write_cmd_usb(struct edgeport_port *edge_port,
2283 unsigned char *buffer, int length) 2265 unsigned char *buffer, int length)
2284 { 2266 {
2285 struct edgeport_serial *edge_serial = 2267 struct edgeport_serial *edge_serial =
2286 usb_get_serial_data(edge_port->port->serial); 2268 usb_get_serial_data(edge_port->port->serial);
2287 struct device *dev = &edge_port->port->dev; 2269 struct device *dev = &edge_port->port->dev;
2288 int status = 0; 2270 int status = 0;
2289 struct urb *urb; 2271 struct urb *urb;
2290 2272
2291 usb_serial_debug_data(dev, __func__, length, buffer); 2273 usb_serial_debug_data(dev, __func__, length, buffer);
2292 2274
2293 /* Allocate our next urb */ 2275 /* Allocate our next urb */
2294 urb = usb_alloc_urb(0, GFP_ATOMIC); 2276 urb = usb_alloc_urb(0, GFP_ATOMIC);
2295 if (!urb) 2277 if (!urb)
2296 return -ENOMEM; 2278 return -ENOMEM;
2297 2279
2298 atomic_inc(&CmdUrbs); 2280 atomic_inc(&CmdUrbs);
2299 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n", 2281 dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
2300 __func__, urb, atomic_read(&CmdUrbs)); 2282 __func__, urb, atomic_read(&CmdUrbs));
2301 2283
2302 usb_fill_bulk_urb(urb, edge_serial->serial->dev, 2284 usb_fill_bulk_urb(urb, edge_serial->serial->dev,
2303 usb_sndbulkpipe(edge_serial->serial->dev, 2285 usb_sndbulkpipe(edge_serial->serial->dev,
2304 edge_serial->bulk_out_endpoint), 2286 edge_serial->bulk_out_endpoint),
2305 buffer, length, edge_bulk_out_cmd_callback, edge_port); 2287 buffer, length, edge_bulk_out_cmd_callback, edge_port);
2306 2288
2307 edge_port->commandPending = true; 2289 edge_port->commandPending = true;
2308 status = usb_submit_urb(urb, GFP_ATOMIC); 2290 status = usb_submit_urb(urb, GFP_ATOMIC);
2309 2291
2310 if (status) { 2292 if (status) {
2311 /* something went wrong */ 2293 /* something went wrong */
2312 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n", 2294 dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
2313 __func__, status); 2295 __func__, status);
2314 usb_kill_urb(urb); 2296 usb_kill_urb(urb);
2315 usb_free_urb(urb); 2297 usb_free_urb(urb);
2316 atomic_dec(&CmdUrbs); 2298 atomic_dec(&CmdUrbs);
2317 return status; 2299 return status;
2318 } 2300 }
2319 2301
2320 #if 0 2302 #if 0
2321 wait_event(&edge_port->wait_command, !edge_port->commandPending); 2303 wait_event(&edge_port->wait_command, !edge_port->commandPending);
2322 2304
2323 if (edge_port->commandPending) { 2305 if (edge_port->commandPending) {
2324 /* command timed out */ 2306 /* command timed out */
2325 dev_dbg(dev, "%s - command timed out\n", __func__); 2307 dev_dbg(dev, "%s - command timed out\n", __func__);
2326 status = -EINVAL; 2308 status = -EINVAL;
2327 } 2309 }
2328 #endif 2310 #endif
2329 return status; 2311 return status;
2330 } 2312 }
2331 2313
2332 2314
2333 /***************************************************************************** 2315 /*****************************************************************************
2334 * send_cmd_write_baud_rate 2316 * send_cmd_write_baud_rate
2335 * this function sends the proper command to change the baud rate of the 2317 * this function sends the proper command to change the baud rate of the
2336 * specified port. 2318 * specified port.
2337 *****************************************************************************/ 2319 *****************************************************************************/
2338 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port, 2320 static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
2339 int baudRate) 2321 int baudRate)
2340 { 2322 {
2341 struct edgeport_serial *edge_serial = 2323 struct edgeport_serial *edge_serial =
2342 usb_get_serial_data(edge_port->port->serial); 2324 usb_get_serial_data(edge_port->port->serial);
2343 struct device *dev = &edge_port->port->dev; 2325 struct device *dev = &edge_port->port->dev;
2344 unsigned char *cmdBuffer; 2326 unsigned char *cmdBuffer;
2345 unsigned char *currCmd; 2327 unsigned char *currCmd;
2346 int cmdLen = 0; 2328 int cmdLen = 0;
2347 int divisor; 2329 int divisor;
2348 int status; 2330 int status;
2349 unsigned char number = 2331 unsigned char number =
2350 edge_port->port->number - edge_port->port->serial->minor; 2332 edge_port->port->number - edge_port->port->serial->minor;
2351 2333
2352 if (edge_serial->is_epic && 2334 if (edge_serial->is_epic &&
2353 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) { 2335 !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
2354 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n", 2336 dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
2355 edge_port->port->number, baudRate); 2337 edge_port->port->number, baudRate);
2356 return 0; 2338 return 0;
2357 } 2339 }
2358 2340
2359 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__, 2341 dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
2360 edge_port->port->number, baudRate); 2342 edge_port->port->number, baudRate);
2361 2343
2362 status = calc_baud_rate_divisor(dev, baudRate, &divisor); 2344 status = calc_baud_rate_divisor(dev, baudRate, &divisor);
2363 if (status) { 2345 if (status) {
2364 dev_err(dev, "%s - bad baud rate\n", __func__); 2346 dev_err(dev, "%s - bad baud rate\n", __func__);
2365 return status; 2347 return status;
2366 } 2348 }
2367 2349
2368 /* Alloc memory for the string of commands. */ 2350 /* Alloc memory for the string of commands. */
2369 cmdBuffer = kmalloc(0x100, GFP_ATOMIC); 2351 cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
2370 if (!cmdBuffer) { 2352 if (!cmdBuffer) {
2371 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100); 2353 dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
2372 return -ENOMEM; 2354 return -ENOMEM;
2373 } 2355 }
2374 currCmd = cmdBuffer; 2356 currCmd = cmdBuffer;
2375 2357
2376 /* Enable access to divisor latch */ 2358 /* Enable access to divisor latch */
2377 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE); 2359 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
2378 2360
2379 /* Write the divisor itself */ 2361 /* Write the divisor itself */
2380 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor)); 2362 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
2381 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor)); 2363 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
2382 2364
2383 /* Restore original value to disable access to divisor latch */ 2365 /* Restore original value to disable access to divisor latch */
2384 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, 2366 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
2385 edge_port->shadowLCR); 2367 edge_port->shadowLCR);
2386 2368
2387 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen); 2369 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2388 if (status) { 2370 if (status) {
2389 /* something bad happened, let's free up the memory */ 2371 /* something bad happened, let's free up the memory */
2390 kfree(cmdBuffer); 2372 kfree(cmdBuffer);
2391 } 2373 }
2392 2374
2393 return status; 2375 return status;
2394 } 2376 }
2395 2377
2396 2378
2397 /***************************************************************************** 2379 /*****************************************************************************
2398 * calc_baud_rate_divisor 2380 * calc_baud_rate_divisor
2399 * this function calculates the proper baud rate divisor for the specified 2381 * this function calculates the proper baud rate divisor for the specified
2400 * baud rate. 2382 * baud rate.
2401 *****************************************************************************/ 2383 *****************************************************************************/
2402 static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor) 2384 static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
2403 { 2385 {
2404 int i; 2386 int i;
2405 __u16 custom; 2387 __u16 custom;
2406 2388
2407 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) { 2389 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
2408 if (divisor_table[i].BaudRate == baudrate) { 2390 if (divisor_table[i].BaudRate == baudrate) {
2409 *divisor = divisor_table[i].Divisor; 2391 *divisor = divisor_table[i].Divisor;
2410 return 0; 2392 return 0;
2411 } 2393 }
2412 } 2394 }
2413 2395
2414 /* We have tried all of the standard baud rates 2396 /* We have tried all of the standard baud rates
2415 * lets try to calculate the divisor for this baud rate 2397 * lets try to calculate the divisor for this baud rate
2416 * Make sure the baud rate is reasonable */ 2398 * Make sure the baud rate is reasonable */
2417 if (baudrate > 50 && baudrate < 230400) { 2399 if (baudrate > 50 && baudrate < 230400) {
2418 /* get divisor */ 2400 /* get divisor */
2419 custom = (__u16)((230400L + baudrate/2) / baudrate); 2401 custom = (__u16)((230400L + baudrate/2) / baudrate);
2420 2402
2421 *divisor = custom; 2403 *divisor = custom;
2422 2404
2423 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom); 2405 dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
2424 return 0; 2406 return 0;
2425 } 2407 }
2426 2408
2427 return -1; 2409 return -1;
2428 } 2410 }
2429 2411
2430 2412
2431 /***************************************************************************** 2413 /*****************************************************************************
2432 * send_cmd_write_uart_register 2414 * send_cmd_write_uart_register
2433 * this function builds up a uart register message and sends to the device. 2415 * this function builds up a uart register message and sends to the device.
2434 *****************************************************************************/ 2416 *****************************************************************************/
2435 static int send_cmd_write_uart_register(struct edgeport_port *edge_port, 2417 static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
2436 __u8 regNum, __u8 regValue) 2418 __u8 regNum, __u8 regValue)
2437 { 2419 {
2438 struct edgeport_serial *edge_serial = 2420 struct edgeport_serial *edge_serial =
2439 usb_get_serial_data(edge_port->port->serial); 2421 usb_get_serial_data(edge_port->port->serial);
2440 struct device *dev = &edge_port->port->dev; 2422 struct device *dev = &edge_port->port->dev;
2441 unsigned char *cmdBuffer; 2423 unsigned char *cmdBuffer;
2442 unsigned char *currCmd; 2424 unsigned char *currCmd;
2443 unsigned long cmdLen = 0; 2425 unsigned long cmdLen = 0;
2444 int status; 2426 int status;
2445 2427
2446 dev_dbg(dev, "%s - write to %s register 0x%02x\n", 2428 dev_dbg(dev, "%s - write to %s register 0x%02x\n",
2447 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue); 2429 (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
2448 2430
2449 if (edge_serial->is_epic && 2431 if (edge_serial->is_epic &&
2450 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR && 2432 !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
2451 regNum == MCR) { 2433 regNum == MCR) {
2452 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n"); 2434 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
2453 return 0; 2435 return 0;
2454 } 2436 }
2455 2437
2456 if (edge_serial->is_epic && 2438 if (edge_serial->is_epic &&
2457 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR && 2439 !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
2458 regNum == LCR) { 2440 regNum == LCR) {
2459 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n"); 2441 dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
2460 return 0; 2442 return 0;
2461 } 2443 }
2462 2444
2463 /* Alloc memory for the string of commands. */ 2445 /* Alloc memory for the string of commands. */
2464 cmdBuffer = kmalloc(0x10, GFP_ATOMIC); 2446 cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
2465 if (cmdBuffer == NULL) 2447 if (cmdBuffer == NULL)
2466 return -ENOMEM; 2448 return -ENOMEM;
2467 2449
2468 currCmd = cmdBuffer; 2450 currCmd = cmdBuffer;
2469 2451
2470 /* Build a cmd in the buffer to write the given register */ 2452 /* Build a cmd in the buffer to write the given register */
2471 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, 2453 MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
2472 edge_port->port->number - edge_port->port->serial->minor, 2454 edge_port->port->number - edge_port->port->serial->minor,
2473 regNum, regValue); 2455 regNum, regValue);
2474 2456
2475 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen); 2457 status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
2476 if (status) { 2458 if (status) {
2477 /* something bad happened, let's free up the memory */ 2459 /* something bad happened, let's free up the memory */
2478 kfree(cmdBuffer); 2460 kfree(cmdBuffer);
2479 } 2461 }
2480 2462
2481 return status; 2463 return status;
2482 } 2464 }
2483 2465
2484 2466
2485 /***************************************************************************** 2467 /*****************************************************************************
2486 * change_port_settings 2468 * change_port_settings
2487 * This routine is called to set the UART on the device to match the 2469 * This routine is called to set the UART on the device to match the
2488 * specified new settings. 2470 * specified new settings.
2489 *****************************************************************************/ 2471 *****************************************************************************/
2490 2472
2491 static void change_port_settings(struct tty_struct *tty, 2473 static void change_port_settings(struct tty_struct *tty,
2492 struct edgeport_port *edge_port, struct ktermios *old_termios) 2474 struct edgeport_port *edge_port, struct ktermios *old_termios)
2493 { 2475 {
2494 struct device *dev = &edge_port->port->dev; 2476 struct device *dev = &edge_port->port->dev;
2495 struct edgeport_serial *edge_serial = 2477 struct edgeport_serial *edge_serial =
2496 usb_get_serial_data(edge_port->port->serial); 2478 usb_get_serial_data(edge_port->port->serial);
2497 int baud; 2479 int baud;
2498 unsigned cflag; 2480 unsigned cflag;
2499 __u8 mask = 0xff; 2481 __u8 mask = 0xff;
2500 __u8 lData; 2482 __u8 lData;
2501 __u8 lParity; 2483 __u8 lParity;
2502 __u8 lStop; 2484 __u8 lStop;
2503 __u8 rxFlow; 2485 __u8 rxFlow;
2504 __u8 txFlow; 2486 __u8 txFlow;
2505 int status; 2487 int status;
2506 2488
2507 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number); 2489 dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
2508 2490
2509 if (!edge_port->open && 2491 if (!edge_port->open &&
2510 !edge_port->openPending) { 2492 !edge_port->openPending) {
2511 dev_dbg(dev, "%s - port not opened\n", __func__); 2493 dev_dbg(dev, "%s - port not opened\n", __func__);
2512 return; 2494 return;
2513 } 2495 }
2514 2496
2515 cflag = tty->termios.c_cflag; 2497 cflag = tty->termios.c_cflag;
2516 2498
2517 switch (cflag & CSIZE) { 2499 switch (cflag & CSIZE) {
2518 case CS5: 2500 case CS5:
2519 lData = LCR_BITS_5; mask = 0x1f; 2501 lData = LCR_BITS_5; mask = 0x1f;
2520 dev_dbg(dev, "%s - data bits = 5\n", __func__); 2502 dev_dbg(dev, "%s - data bits = 5\n", __func__);
2521 break; 2503 break;
2522 case CS6: 2504 case CS6:
2523 lData = LCR_BITS_6; mask = 0x3f; 2505 lData = LCR_BITS_6; mask = 0x3f;
2524 dev_dbg(dev, "%s - data bits = 6\n", __func__); 2506 dev_dbg(dev, "%s - data bits = 6\n", __func__);
2525 break; 2507 break;
2526 case CS7: 2508 case CS7:
2527 lData = LCR_BITS_7; mask = 0x7f; 2509 lData = LCR_BITS_7; mask = 0x7f;
2528 dev_dbg(dev, "%s - data bits = 7\n", __func__); 2510 dev_dbg(dev, "%s - data bits = 7\n", __func__);
2529 break; 2511 break;
2530 default: 2512 default:
2531 case CS8: 2513 case CS8:
2532 lData = LCR_BITS_8; 2514 lData = LCR_BITS_8;
2533 dev_dbg(dev, "%s - data bits = 8\n", __func__); 2515 dev_dbg(dev, "%s - data bits = 8\n", __func__);
2534 break; 2516 break;
2535 } 2517 }
2536 2518
2537 lParity = LCR_PAR_NONE; 2519 lParity = LCR_PAR_NONE;
2538 if (cflag & PARENB) { 2520 if (cflag & PARENB) {
2539 if (cflag & CMSPAR) { 2521 if (cflag & CMSPAR) {
2540 if (cflag & PARODD) { 2522 if (cflag & PARODD) {
2541 lParity = LCR_PAR_MARK; 2523 lParity = LCR_PAR_MARK;
2542 dev_dbg(dev, "%s - parity = mark\n", __func__); 2524 dev_dbg(dev, "%s - parity = mark\n", __func__);
2543 } else { 2525 } else {
2544 lParity = LCR_PAR_SPACE; 2526 lParity = LCR_PAR_SPACE;
2545 dev_dbg(dev, "%s - parity = space\n", __func__); 2527 dev_dbg(dev, "%s - parity = space\n", __func__);
2546 } 2528 }
2547 } else if (cflag & PARODD) { 2529 } else if (cflag & PARODD) {
2548 lParity = LCR_PAR_ODD; 2530 lParity = LCR_PAR_ODD;
2549 dev_dbg(dev, "%s - parity = odd\n", __func__); 2531 dev_dbg(dev, "%s - parity = odd\n", __func__);
2550 } else { 2532 } else {
2551 lParity = LCR_PAR_EVEN; 2533 lParity = LCR_PAR_EVEN;
2552 dev_dbg(dev, "%s - parity = even\n", __func__); 2534 dev_dbg(dev, "%s - parity = even\n", __func__);
2553 } 2535 }
2554 } else { 2536 } else {
2555 dev_dbg(dev, "%s - parity = none\n", __func__); 2537 dev_dbg(dev, "%s - parity = none\n", __func__);
2556 } 2538 }
2557 2539
2558 if (cflag & CSTOPB) { 2540 if (cflag & CSTOPB) {
2559 lStop = LCR_STOP_2; 2541 lStop = LCR_STOP_2;
2560 dev_dbg(dev, "%s - stop bits = 2\n", __func__); 2542 dev_dbg(dev, "%s - stop bits = 2\n", __func__);
2561 } else { 2543 } else {
2562 lStop = LCR_STOP_1; 2544 lStop = LCR_STOP_1;
2563 dev_dbg(dev, "%s - stop bits = 1\n", __func__); 2545 dev_dbg(dev, "%s - stop bits = 1\n", __func__);
2564 } 2546 }
2565 2547
2566 /* figure out the flow control settings */ 2548 /* figure out the flow control settings */
2567 rxFlow = txFlow = 0x00; 2549 rxFlow = txFlow = 0x00;
2568 if (cflag & CRTSCTS) { 2550 if (cflag & CRTSCTS) {
2569 rxFlow |= IOSP_RX_FLOW_RTS; 2551 rxFlow |= IOSP_RX_FLOW_RTS;
2570 txFlow |= IOSP_TX_FLOW_CTS; 2552 txFlow |= IOSP_TX_FLOW_CTS;
2571 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__); 2553 dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
2572 } else { 2554 } else {
2573 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__); 2555 dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
2574 } 2556 }
2575 2557
2576 /* if we are implementing XON/XOFF, set the start and stop character 2558 /* if we are implementing XON/XOFF, set the start and stop character
2577 in the device */ 2559 in the device */
2578 if (I_IXOFF(tty) || I_IXON(tty)) { 2560 if (I_IXOFF(tty) || I_IXON(tty)) {
2579 unsigned char stop_char = STOP_CHAR(tty); 2561 unsigned char stop_char = STOP_CHAR(tty);
2580 unsigned char start_char = START_CHAR(tty); 2562 unsigned char start_char = START_CHAR(tty);
2581 2563
2582 if ((!edge_serial->is_epic) || 2564 if ((!edge_serial->is_epic) ||
2583 ((edge_serial->is_epic) && 2565 ((edge_serial->is_epic) &&
2584 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) { 2566 (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
2585 send_iosp_ext_cmd(edge_port, 2567 send_iosp_ext_cmd(edge_port,
2586 IOSP_CMD_SET_XON_CHAR, start_char); 2568 IOSP_CMD_SET_XON_CHAR, start_char);
2587 send_iosp_ext_cmd(edge_port, 2569 send_iosp_ext_cmd(edge_port,
2588 IOSP_CMD_SET_XOFF_CHAR, stop_char); 2570 IOSP_CMD_SET_XOFF_CHAR, stop_char);
2589 } 2571 }
2590 2572
2591 /* if we are implementing INBOUND XON/XOFF */ 2573 /* if we are implementing INBOUND XON/XOFF */
2592 if (I_IXOFF(tty)) { 2574 if (I_IXOFF(tty)) {
2593 rxFlow |= IOSP_RX_FLOW_XON_XOFF; 2575 rxFlow |= IOSP_RX_FLOW_XON_XOFF;
2594 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n", 2576 dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2595 __func__, start_char, stop_char); 2577 __func__, start_char, stop_char);
2596 } else { 2578 } else {
2597 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__); 2579 dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
2598 } 2580 }
2599 2581
2600 /* if we are implementing OUTBOUND XON/XOFF */ 2582 /* if we are implementing OUTBOUND XON/XOFF */
2601 if (I_IXON(tty)) { 2583 if (I_IXON(tty)) {
2602 txFlow |= IOSP_TX_FLOW_XON_XOFF; 2584 txFlow |= IOSP_TX_FLOW_XON_XOFF;
2603 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n", 2585 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
2604 __func__, start_char, stop_char); 2586 __func__, start_char, stop_char);
2605 } else { 2587 } else {
2606 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__); 2588 dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
2607 } 2589 }
2608 } 2590 }
2609 2591
2610 /* Set flow control to the configured value */ 2592 /* Set flow control to the configured value */
2611 if ((!edge_serial->is_epic) || 2593 if ((!edge_serial->is_epic) ||
2612 ((edge_serial->is_epic) && 2594 ((edge_serial->is_epic) &&
2613 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow))) 2595 (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
2614 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow); 2596 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
2615 if ((!edge_serial->is_epic) || 2597 if ((!edge_serial->is_epic) ||
2616 ((edge_serial->is_epic) && 2598 ((edge_serial->is_epic) &&
2617 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow))) 2599 (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
2618 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow); 2600 send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
2619 2601
2620 2602
2621 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); 2603 edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2622 edge_port->shadowLCR |= (lData | lParity | lStop); 2604 edge_port->shadowLCR |= (lData | lParity | lStop);
2623 2605
2624 edge_port->validDataMask = mask; 2606 edge_port->validDataMask = mask;
2625 2607
2626 /* Send the updated LCR value to the EdgePort */ 2608 /* Send the updated LCR value to the EdgePort */
2627 status = send_cmd_write_uart_register(edge_port, LCR, 2609 status = send_cmd_write_uart_register(edge_port, LCR,
2628 edge_port->shadowLCR); 2610 edge_port->shadowLCR);
2629 if (status != 0) 2611 if (status != 0)
2630 return; 2612 return;
2631 2613
2632 /* set up the MCR register and send it to the EdgePort */ 2614 /* set up the MCR register and send it to the EdgePort */
2633 edge_port->shadowMCR = MCR_MASTER_IE; 2615 edge_port->shadowMCR = MCR_MASTER_IE;
2634 if (cflag & CBAUD) 2616 if (cflag & CBAUD)
2635 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS); 2617 edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2636 2618
2637 status = send_cmd_write_uart_register(edge_port, MCR, 2619 status = send_cmd_write_uart_register(edge_port, MCR,
2638 edge_port->shadowMCR); 2620 edge_port->shadowMCR);
2639 if (status != 0) 2621 if (status != 0)
2640 return; 2622 return;
2641 2623
2642 /* Determine divisor based on baud rate */ 2624 /* Determine divisor based on baud rate */
2643 baud = tty_get_baud_rate(tty); 2625 baud = tty_get_baud_rate(tty);
2644 if (!baud) { 2626 if (!baud) {
2645 /* pick a default, any default... */ 2627 /* pick a default, any default... */
2646 baud = 9600; 2628 baud = 9600;
2647 } 2629 }
2648 2630
2649 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud); 2631 dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
2650 status = send_cmd_write_baud_rate(edge_port, baud); 2632 status = send_cmd_write_baud_rate(edge_port, baud);
2651 if (status == -1) { 2633 if (status == -1) {
2652 /* Speed change was not possible - put back the old speed */ 2634 /* Speed change was not possible - put back the old speed */
2653 baud = tty_termios_baud_rate(old_termios); 2635 baud = tty_termios_baud_rate(old_termios);
2654 tty_encode_baud_rate(tty, baud, baud); 2636 tty_encode_baud_rate(tty, baud, baud);
2655 } 2637 }
2656 } 2638 }
2657 2639
2658 2640
2659 /**************************************************************************** 2641 /****************************************************************************
2660 * unicode_to_ascii 2642 * unicode_to_ascii
2661 * Turns a string from Unicode into ASCII. 2643 * Turns a string from Unicode into ASCII.
2662 * Doesn't do a good job with any characters that are outside the normal 2644 * Doesn't do a good job with any characters that are outside the normal
2663 * ASCII range, but it's only for debugging... 2645 * ASCII range, but it's only for debugging...
2664 * NOTE: expects the unicode in LE format 2646 * NOTE: expects the unicode in LE format
2665 ****************************************************************************/ 2647 ****************************************************************************/
2666 static void unicode_to_ascii(char *string, int buflen, 2648 static void unicode_to_ascii(char *string, int buflen,
2667 __le16 *unicode, int unicode_size) 2649 __le16 *unicode, int unicode_size)
2668 { 2650 {
2669 int i; 2651 int i;
2670 2652
2671 if (buflen <= 0) /* never happens, but... */ 2653 if (buflen <= 0) /* never happens, but... */
2672 return; 2654 return;
2673 --buflen; /* space for nul */ 2655 --buflen; /* space for nul */
2674 2656
2675 for (i = 0; i < unicode_size; i++) { 2657 for (i = 0; i < unicode_size; i++) {
2676 if (i >= buflen) 2658 if (i >= buflen)
2677 break; 2659 break;
2678 string[i] = (char)(le16_to_cpu(unicode[i])); 2660 string[i] = (char)(le16_to_cpu(unicode[i]));
2679 } 2661 }
2680 string[i] = 0x00; 2662 string[i] = 0x00;
2681 } 2663 }
2682 2664
2683 2665
2684 /**************************************************************************** 2666 /****************************************************************************
2685 * get_manufacturing_desc 2667 * get_manufacturing_desc
2686 * reads in the manufacturing descriptor and stores it into the serial 2668 * reads in the manufacturing descriptor and stores it into the serial
2687 * structure. 2669 * structure.
2688 ****************************************************************************/ 2670 ****************************************************************************/
2689 static void get_manufacturing_desc(struct edgeport_serial *edge_serial) 2671 static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
2690 { 2672 {
2691 struct device *dev = &edge_serial->serial->dev->dev; 2673 struct device *dev = &edge_serial->serial->dev->dev;
2692 int response; 2674 int response;
2693 2675
2694 dev_dbg(dev, "getting manufacturer descriptor\n"); 2676 dev_dbg(dev, "getting manufacturer descriptor\n");
2695 2677
2696 response = rom_read(edge_serial->serial, 2678 response = rom_read(edge_serial->serial,
2697 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16, 2679 (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
2698 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff), 2680 (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
2699 EDGE_MANUF_DESC_LEN, 2681 EDGE_MANUF_DESC_LEN,
2700 (__u8 *)(&edge_serial->manuf_descriptor)); 2682 (__u8 *)(&edge_serial->manuf_descriptor));
2701 2683
2702 if (response < 1) 2684 if (response < 1)
2703 dev_err(dev, "error in getting manufacturer descriptor\n"); 2685 dev_err(dev, "error in getting manufacturer descriptor\n");
2704 else { 2686 else {
2705 char string[30]; 2687 char string[30];
2706 dev_dbg(dev, "**Manufacturer Descriptor\n"); 2688 dev_dbg(dev, "**Manufacturer Descriptor\n");
2707 dev_dbg(dev, " RomSize: %dK\n", 2689 dev_dbg(dev, " RomSize: %dK\n",
2708 edge_serial->manuf_descriptor.RomSize); 2690 edge_serial->manuf_descriptor.RomSize);
2709 dev_dbg(dev, " RamSize: %dK\n", 2691 dev_dbg(dev, " RamSize: %dK\n",
2710 edge_serial->manuf_descriptor.RamSize); 2692 edge_serial->manuf_descriptor.RamSize);
2711 dev_dbg(dev, " CpuRev: %d\n", 2693 dev_dbg(dev, " CpuRev: %d\n",
2712 edge_serial->manuf_descriptor.CpuRev); 2694 edge_serial->manuf_descriptor.CpuRev);
2713 dev_dbg(dev, " BoardRev: %d\n", 2695 dev_dbg(dev, " BoardRev: %d\n",
2714 edge_serial->manuf_descriptor.BoardRev); 2696 edge_serial->manuf_descriptor.BoardRev);
2715 dev_dbg(dev, " NumPorts: %d\n", 2697 dev_dbg(dev, " NumPorts: %d\n",
2716 edge_serial->manuf_descriptor.NumPorts); 2698 edge_serial->manuf_descriptor.NumPorts);
2717 dev_dbg(dev, " DescDate: %d/%d/%d\n", 2699 dev_dbg(dev, " DescDate: %d/%d/%d\n",
2718 edge_serial->manuf_descriptor.DescDate[0], 2700 edge_serial->manuf_descriptor.DescDate[0],
2719 edge_serial->manuf_descriptor.DescDate[1], 2701 edge_serial->manuf_descriptor.DescDate[1],
2720 edge_serial->manuf_descriptor.DescDate[2]+1900); 2702 edge_serial->manuf_descriptor.DescDate[2]+1900);
2721 unicode_to_ascii(string, sizeof(string), 2703 unicode_to_ascii(string, sizeof(string),
2722 edge_serial->manuf_descriptor.SerialNumber, 2704 edge_serial->manuf_descriptor.SerialNumber,
2723 edge_serial->manuf_descriptor.SerNumLength/2); 2705 edge_serial->manuf_descriptor.SerNumLength/2);
2724 dev_dbg(dev, " SerialNumber: %s\n", string); 2706 dev_dbg(dev, " SerialNumber: %s\n", string);
2725 unicode_to_ascii(string, sizeof(string), 2707 unicode_to_ascii(string, sizeof(string),
2726 edge_serial->manuf_descriptor.AssemblyNumber, 2708 edge_serial->manuf_descriptor.AssemblyNumber,
2727 edge_serial->manuf_descriptor.AssemblyNumLength/2); 2709 edge_serial->manuf_descriptor.AssemblyNumLength/2);
2728 dev_dbg(dev, " AssemblyNumber: %s\n", string); 2710 dev_dbg(dev, " AssemblyNumber: %s\n", string);
2729 unicode_to_ascii(string, sizeof(string), 2711 unicode_to_ascii(string, sizeof(string),
2730 edge_serial->manuf_descriptor.OemAssyNumber, 2712 edge_serial->manuf_descriptor.OemAssyNumber,
2731 edge_serial->manuf_descriptor.OemAssyNumLength/2); 2713 edge_serial->manuf_descriptor.OemAssyNumLength/2);
2732 dev_dbg(dev, " OemAssyNumber: %s\n", string); 2714 dev_dbg(dev, " OemAssyNumber: %s\n", string);
2733 dev_dbg(dev, " UartType: %d\n", 2715 dev_dbg(dev, " UartType: %d\n",
2734 edge_serial->manuf_descriptor.UartType); 2716 edge_serial->manuf_descriptor.UartType);
2735 dev_dbg(dev, " IonPid: %d\n", 2717 dev_dbg(dev, " IonPid: %d\n",
2736 edge_serial->manuf_descriptor.IonPid); 2718 edge_serial->manuf_descriptor.IonPid);
2737 dev_dbg(dev, " IonConfig: %d\n", 2719 dev_dbg(dev, " IonConfig: %d\n",
2738 edge_serial->manuf_descriptor.IonConfig); 2720 edge_serial->manuf_descriptor.IonConfig);
2739 } 2721 }
2740 } 2722 }
2741 2723
2742 2724
2743 /**************************************************************************** 2725 /****************************************************************************
2744 * get_boot_desc 2726 * get_boot_desc
2745 * reads in the bootloader descriptor and stores it into the serial 2727 * reads in the bootloader descriptor and stores it into the serial
2746 * structure. 2728 * structure.
2747 ****************************************************************************/ 2729 ****************************************************************************/
2748 static void get_boot_desc(struct edgeport_serial *edge_serial) 2730 static void get_boot_desc(struct edgeport_serial *edge_serial)
2749 { 2731 {
2750 struct device *dev = &edge_serial->serial->dev->dev; 2732 struct device *dev = &edge_serial->serial->dev->dev;
2751 int response; 2733 int response;
2752 2734
2753 dev_dbg(dev, "getting boot descriptor\n"); 2735 dev_dbg(dev, "getting boot descriptor\n");
2754 2736
2755 response = rom_read(edge_serial->serial, 2737 response = rom_read(edge_serial->serial,
2756 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16, 2738 (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
2757 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff), 2739 (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
2758 EDGE_BOOT_DESC_LEN, 2740 EDGE_BOOT_DESC_LEN,
2759 (__u8 *)(&edge_serial->boot_descriptor)); 2741 (__u8 *)(&edge_serial->boot_descriptor));
2760 2742
2761 if (response < 1) 2743 if (response < 1)
2762 dev_err(dev, "error in getting boot descriptor\n"); 2744 dev_err(dev, "error in getting boot descriptor\n");
2763 else { 2745 else {
2764 dev_dbg(dev, "**Boot Descriptor:\n"); 2746 dev_dbg(dev, "**Boot Descriptor:\n");
2765 dev_dbg(dev, " BootCodeLength: %d\n", 2747 dev_dbg(dev, " BootCodeLength: %d\n",
2766 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength)); 2748 le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
2767 dev_dbg(dev, " MajorVersion: %d\n", 2749 dev_dbg(dev, " MajorVersion: %d\n",
2768 edge_serial->boot_descriptor.MajorVersion); 2750 edge_serial->boot_descriptor.MajorVersion);
2769 dev_dbg(dev, " MinorVersion: %d\n", 2751 dev_dbg(dev, " MinorVersion: %d\n",
2770 edge_serial->boot_descriptor.MinorVersion); 2752 edge_serial->boot_descriptor.MinorVersion);
2771 dev_dbg(dev, " BuildNumber: %d\n", 2753 dev_dbg(dev, " BuildNumber: %d\n",
2772 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber)); 2754 le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
2773 dev_dbg(dev, " Capabilities: 0x%x\n", 2755 dev_dbg(dev, " Capabilities: 0x%x\n",
2774 le16_to_cpu(edge_serial->boot_descriptor.Capabilities)); 2756 le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
2775 dev_dbg(dev, " UConfig0: %d\n", 2757 dev_dbg(dev, " UConfig0: %d\n",
2776 edge_serial->boot_descriptor.UConfig0); 2758 edge_serial->boot_descriptor.UConfig0);
2777 dev_dbg(dev, " UConfig1: %d\n", 2759 dev_dbg(dev, " UConfig1: %d\n",
2778 edge_serial->boot_descriptor.UConfig1); 2760 edge_serial->boot_descriptor.UConfig1);
2779 } 2761 }
2780 } 2762 }
2781 2763
2782 2764
2783 /**************************************************************************** 2765 /****************************************************************************
2784 * load_application_firmware 2766 * load_application_firmware
2785 * This is called to load the application firmware to the device 2767 * This is called to load the application firmware to the device
2786 ****************************************************************************/ 2768 ****************************************************************************/
2787 static void load_application_firmware(struct edgeport_serial *edge_serial) 2769 static void load_application_firmware(struct edgeport_serial *edge_serial)
2788 { 2770 {
2789 struct device *dev = &edge_serial->serial->dev->dev; 2771 struct device *dev = &edge_serial->serial->dev->dev;
2790 const struct ihex_binrec *rec; 2772 const struct ihex_binrec *rec;
2791 const struct firmware *fw; 2773 const struct firmware *fw;
2792 const char *fw_name; 2774 const char *fw_name;
2793 const char *fw_info; 2775 const char *fw_info;
2794 int response; 2776 int response;
2795 __u32 Operaddr; 2777 __u32 Operaddr;
2796 __u16 build; 2778 __u16 build;
2797 2779
2798 switch (edge_serial->product_info.iDownloadFile) { 2780 switch (edge_serial->product_info.iDownloadFile) {
2799 case EDGE_DOWNLOAD_FILE_I930: 2781 case EDGE_DOWNLOAD_FILE_I930:
2800 fw_info = "downloading firmware version (930)"; 2782 fw_info = "downloading firmware version (930)";
2801 fw_name = "edgeport/down.fw"; 2783 fw_name = "edgeport/down.fw";
2802 break; 2784 break;
2803 2785
2804 case EDGE_DOWNLOAD_FILE_80251: 2786 case EDGE_DOWNLOAD_FILE_80251:
2805 fw_info = "downloading firmware version (80251)"; 2787 fw_info = "downloading firmware version (80251)";
2806 fw_name = "edgeport/down2.fw"; 2788 fw_name = "edgeport/down2.fw";
2807 break; 2789 break;
2808 2790
2809 case EDGE_DOWNLOAD_FILE_NONE: 2791 case EDGE_DOWNLOAD_FILE_NONE:
2810 dev_dbg(dev, "No download file specified, skipping download\n"); 2792 dev_dbg(dev, "No download file specified, skipping download\n");
2811 return; 2793 return;
2812 2794
2813 default: 2795 default:
2814 return; 2796 return;
2815 } 2797 }
2816 2798
2817 response = request_ihex_firmware(&fw, fw_name, 2799 response = request_ihex_firmware(&fw, fw_name,
2818 &edge_serial->serial->dev->dev); 2800 &edge_serial->serial->dev->dev);
2819 if (response) { 2801 if (response) {
2820 dev_err(dev, "Failed to load image \"%s\" err %d\n", 2802 dev_err(dev, "Failed to load image \"%s\" err %d\n",
2821 fw_name, response); 2803 fw_name, response);
2822 return; 2804 return;
2823 } 2805 }
2824 2806
2825 rec = (const struct ihex_binrec *)fw->data; 2807 rec = (const struct ihex_binrec *)fw->data;
2826 build = (rec->data[2] << 8) | rec->data[3]; 2808 build = (rec->data[2] << 8) | rec->data[3];
2827 2809
2828 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build); 2810 dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
2829 2811
2830 edge_serial->product_info.FirmwareMajorVersion = rec->data[0]; 2812 edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
2831 edge_serial->product_info.FirmwareMinorVersion = rec->data[1]; 2813 edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
2832 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build); 2814 edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
2833 2815
2834 for (rec = ihex_next_binrec(rec); rec; 2816 for (rec = ihex_next_binrec(rec); rec;
2835 rec = ihex_next_binrec(rec)) { 2817 rec = ihex_next_binrec(rec)) {
2836 Operaddr = be32_to_cpu(rec->addr); 2818 Operaddr = be32_to_cpu(rec->addr);
2837 response = sram_write(edge_serial->serial, 2819 response = sram_write(edge_serial->serial,
2838 Operaddr >> 16, 2820 Operaddr >> 16,
2839 Operaddr & 0xFFFF, 2821 Operaddr & 0xFFFF,
2840 be16_to_cpu(rec->len), 2822 be16_to_cpu(rec->len),
2841 &rec->data[0]); 2823 &rec->data[0]);
2842 if (response < 0) { 2824 if (response < 0) {
2843 dev_err(&edge_serial->serial->dev->dev, 2825 dev_err(&edge_serial->serial->dev->dev,
2844 "sram_write failed (%x, %x, %d)\n", 2826 "sram_write failed (%x, %x, %d)\n",
2845 Operaddr >> 16, Operaddr & 0xFFFF, 2827 Operaddr >> 16, Operaddr & 0xFFFF,
2846 be16_to_cpu(rec->len)); 2828 be16_to_cpu(rec->len));
2847 break; 2829 break;
2848 } 2830 }
2849 } 2831 }
2850 2832
2851 dev_dbg(dev, "sending exec_dl_code\n"); 2833 dev_dbg(dev, "sending exec_dl_code\n");
2852 response = usb_control_msg (edge_serial->serial->dev, 2834 response = usb_control_msg (edge_serial->serial->dev,
2853 usb_sndctrlpipe(edge_serial->serial->dev, 0), 2835 usb_sndctrlpipe(edge_serial->serial->dev, 0),
2854 USB_REQUEST_ION_EXEC_DL_CODE, 2836 USB_REQUEST_ION_EXEC_DL_CODE,
2855 0x40, 0x4000, 0x0001, NULL, 0, 3000); 2837 0x40, 0x4000, 0x0001, NULL, 0, 3000);
2856 2838
2857 release_firmware(fw); 2839 release_firmware(fw);
2858 } 2840 }
2859 2841
2860 2842
2861 /**************************************************************************** 2843 /****************************************************************************
2862 * edge_startup 2844 * edge_startup
2863 ****************************************************************************/ 2845 ****************************************************************************/
2864 static int edge_startup(struct usb_serial *serial) 2846 static int edge_startup(struct usb_serial *serial)
2865 { 2847 {
2866 struct edgeport_serial *edge_serial; 2848 struct edgeport_serial *edge_serial;
2867 struct usb_device *dev; 2849 struct usb_device *dev;
2868 struct device *ddev = &serial->dev->dev; 2850 struct device *ddev = &serial->dev->dev;
2869 int i; 2851 int i;
2870 int response; 2852 int response;
2871 bool interrupt_in_found; 2853 bool interrupt_in_found;
2872 bool bulk_in_found; 2854 bool bulk_in_found;
2873 bool bulk_out_found; 2855 bool bulk_out_found;
2874 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0, 2856 static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
2875 EDGE_COMPATIBILITY_MASK1, 2857 EDGE_COMPATIBILITY_MASK1,
2876 EDGE_COMPATIBILITY_MASK2 }; 2858 EDGE_COMPATIBILITY_MASK2 };
2877 2859
2878 dev = serial->dev; 2860 dev = serial->dev;
2879 2861
2880 /* create our private serial structure */ 2862 /* create our private serial structure */
2881 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL); 2863 edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
2882 if (edge_serial == NULL) { 2864 if (edge_serial == NULL) {
2883 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__); 2865 dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
2884 return -ENOMEM; 2866 return -ENOMEM;
2885 } 2867 }
2886 spin_lock_init(&edge_serial->es_lock); 2868 spin_lock_init(&edge_serial->es_lock);
2887 edge_serial->serial = serial; 2869 edge_serial->serial = serial;
2888 usb_set_serial_data(serial, edge_serial); 2870 usb_set_serial_data(serial, edge_serial);
2889 2871
2890 /* get the name for the device from the device */ 2872 /* get the name for the device from the device */
2891 i = usb_string(dev, dev->descriptor.iManufacturer, 2873 i = usb_string(dev, dev->descriptor.iManufacturer,
2892 &edge_serial->name[0], MAX_NAME_LEN+1); 2874 &edge_serial->name[0], MAX_NAME_LEN+1);
2893 if (i < 0) 2875 if (i < 0)
2894 i = 0; 2876 i = 0;
2895 edge_serial->name[i++] = ' '; 2877 edge_serial->name[i++] = ' ';
2896 usb_string(dev, dev->descriptor.iProduct, 2878 usb_string(dev, dev->descriptor.iProduct,
2897 &edge_serial->name[i], MAX_NAME_LEN+2 - i); 2879 &edge_serial->name[i], MAX_NAME_LEN+2 - i);
2898 2880
2899 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name); 2881 dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
2900 2882
2901 /* Read the epic descriptor */ 2883 /* Read the epic descriptor */
2902 if (get_epic_descriptor(edge_serial) <= 0) { 2884 if (get_epic_descriptor(edge_serial) <= 0) {
2903 /* memcpy descriptor to Supports structures */ 2885 /* memcpy descriptor to Supports structures */
2904 memcpy(&edge_serial->epic_descriptor.Supports, descriptor, 2886 memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
2905 sizeof(struct edge_compatibility_bits)); 2887 sizeof(struct edge_compatibility_bits));
2906 2888
2907 /* get the manufacturing descriptor for this device */ 2889 /* get the manufacturing descriptor for this device */
2908 get_manufacturing_desc(edge_serial); 2890 get_manufacturing_desc(edge_serial);
2909 2891
2910 /* get the boot descriptor */ 2892 /* get the boot descriptor */
2911 get_boot_desc(edge_serial); 2893 get_boot_desc(edge_serial);
2912 2894
2913 get_product_info(edge_serial); 2895 get_product_info(edge_serial);
2914 } 2896 }
2915 2897
2916 /* set the number of ports from the manufacturing description */ 2898 /* set the number of ports from the manufacturing description */
2917 /* serial->num_ports = serial->product_info.NumPorts; */ 2899 /* serial->num_ports = serial->product_info.NumPorts; */
2918 if ((!edge_serial->is_epic) && 2900 if ((!edge_serial->is_epic) &&
2919 (edge_serial->product_info.NumPorts != serial->num_ports)) { 2901 (edge_serial->product_info.NumPorts != serial->num_ports)) {
2920 dev_warn(ddev, 2902 dev_warn(ddev,
2921 "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n", 2903 "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
2922 edge_serial->product_info.NumPorts, 2904 edge_serial->product_info.NumPorts,
2923 serial->num_ports); 2905 serial->num_ports);
2924 } 2906 }
2925 2907
2926 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies); 2908 dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
2927 2909
2928 /* If not an EPiC device */ 2910 /* If not an EPiC device */
2929 if (!edge_serial->is_epic) { 2911 if (!edge_serial->is_epic) {
2930 /* now load the application firmware into this device */ 2912 /* now load the application firmware into this device */
2931 load_application_firmware(edge_serial); 2913 load_application_firmware(edge_serial);
2932 2914
2933 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies); 2915 dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
2934 2916
2935 /* Check current Edgeport EEPROM and update if necessary */ 2917 /* Check current Edgeport EEPROM and update if necessary */
2936 update_edgeport_E2PROM(edge_serial); 2918 update_edgeport_E2PROM(edge_serial);
2937 2919
2938 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies); 2920 dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
2939 2921
2940 /* set the configuration to use #1 */ 2922 /* set the configuration to use #1 */
2941 /* dev_dbg(ddev, "set_configuration 1\n"); */ 2923 /* dev_dbg(ddev, "set_configuration 1\n"); */
2942 /* usb_set_configuration (dev, 1); */ 2924 /* usb_set_configuration (dev, 1); */
2943 } 2925 }
2944 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n", 2926 dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
2945 edge_serial->product_info.FirmwareMajorVersion, 2927 edge_serial->product_info.FirmwareMajorVersion,
2946 edge_serial->product_info.FirmwareMinorVersion, 2928 edge_serial->product_info.FirmwareMinorVersion,
2947 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber)); 2929 le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
2948 2930
2949 /* we set up the pointers to the endpoints in the edge_open function, 2931 /* we set up the pointers to the endpoints in the edge_open function,
2950 * as the structures aren't created yet. */ 2932 * as the structures aren't created yet. */
2951 2933
2952 response = 0; 2934 response = 0;
2953 2935
2954 if (edge_serial->is_epic) { 2936 if (edge_serial->is_epic) {
2955 /* EPIC thing, set up our interrupt polling now and our read 2937 /* EPIC thing, set up our interrupt polling now and our read
2956 * urb, so that the device knows it really is connected. */ 2938 * urb, so that the device knows it really is connected. */
2957 interrupt_in_found = bulk_in_found = bulk_out_found = false; 2939 interrupt_in_found = bulk_in_found = bulk_out_found = false;
2958 for (i = 0; i < serial->interface->altsetting[0] 2940 for (i = 0; i < serial->interface->altsetting[0]
2959 .desc.bNumEndpoints; ++i) { 2941 .desc.bNumEndpoints; ++i) {
2960 struct usb_endpoint_descriptor *endpoint; 2942 struct usb_endpoint_descriptor *endpoint;
2961 int buffer_size; 2943 int buffer_size;
2962 2944
2963 endpoint = &serial->interface->altsetting[0]. 2945 endpoint = &serial->interface->altsetting[0].
2964 endpoint[i].desc; 2946 endpoint[i].desc;
2965 buffer_size = usb_endpoint_maxp(endpoint); 2947 buffer_size = usb_endpoint_maxp(endpoint);
2966 if (!interrupt_in_found && 2948 if (!interrupt_in_found &&
2967 (usb_endpoint_is_int_in(endpoint))) { 2949 (usb_endpoint_is_int_in(endpoint))) {
2968 /* we found a interrupt in endpoint */ 2950 /* we found a interrupt in endpoint */
2969 dev_dbg(ddev, "found interrupt in\n"); 2951 dev_dbg(ddev, "found interrupt in\n");
2970 2952
2971 /* not set up yet, so do it now */ 2953 /* not set up yet, so do it now */
2972 edge_serial->interrupt_read_urb = 2954 edge_serial->interrupt_read_urb =
2973 usb_alloc_urb(0, GFP_KERNEL); 2955 usb_alloc_urb(0, GFP_KERNEL);
2974 if (!edge_serial->interrupt_read_urb) { 2956 if (!edge_serial->interrupt_read_urb) {
2975 dev_err(ddev, "out of memory\n"); 2957 dev_err(ddev, "out of memory\n");
2976 return -ENOMEM; 2958 return -ENOMEM;
2977 } 2959 }
2978 edge_serial->interrupt_in_buffer = 2960 edge_serial->interrupt_in_buffer =
2979 kmalloc(buffer_size, GFP_KERNEL); 2961 kmalloc(buffer_size, GFP_KERNEL);
2980 if (!edge_serial->interrupt_in_buffer) { 2962 if (!edge_serial->interrupt_in_buffer) {
2981 dev_err(ddev, "out of memory\n"); 2963 dev_err(ddev, "out of memory\n");
2982 usb_free_urb(edge_serial->interrupt_read_urb); 2964 usb_free_urb(edge_serial->interrupt_read_urb);
2983 return -ENOMEM; 2965 return -ENOMEM;
2984 } 2966 }
2985 edge_serial->interrupt_in_endpoint = 2967 edge_serial->interrupt_in_endpoint =
2986 endpoint->bEndpointAddress; 2968 endpoint->bEndpointAddress;
2987 2969
2988 /* set up our interrupt urb */ 2970 /* set up our interrupt urb */
2989 usb_fill_int_urb( 2971 usb_fill_int_urb(
2990 edge_serial->interrupt_read_urb, 2972 edge_serial->interrupt_read_urb,
2991 dev, 2973 dev,
2992 usb_rcvintpipe(dev, 2974 usb_rcvintpipe(dev,
2993 endpoint->bEndpointAddress), 2975 endpoint->bEndpointAddress),
2994 edge_serial->interrupt_in_buffer, 2976 edge_serial->interrupt_in_buffer,
2995 buffer_size, 2977 buffer_size,
2996 edge_interrupt_callback, 2978 edge_interrupt_callback,
2997 edge_serial, 2979 edge_serial,
2998 endpoint->bInterval); 2980 endpoint->bInterval);
2999 2981
3000 interrupt_in_found = true; 2982 interrupt_in_found = true;
3001 } 2983 }
3002 2984
3003 if (!bulk_in_found && 2985 if (!bulk_in_found &&
3004 (usb_endpoint_is_bulk_in(endpoint))) { 2986 (usb_endpoint_is_bulk_in(endpoint))) {
3005 /* we found a bulk in endpoint */ 2987 /* we found a bulk in endpoint */
3006 dev_dbg(ddev, "found bulk in\n"); 2988 dev_dbg(ddev, "found bulk in\n");
3007 2989
3008 /* not set up yet, so do it now */ 2990 /* not set up yet, so do it now */
3009 edge_serial->read_urb = 2991 edge_serial->read_urb =
3010 usb_alloc_urb(0, GFP_KERNEL); 2992 usb_alloc_urb(0, GFP_KERNEL);
3011 if (!edge_serial->read_urb) { 2993 if (!edge_serial->read_urb) {
3012 dev_err(ddev, "out of memory\n"); 2994 dev_err(ddev, "out of memory\n");
3013 return -ENOMEM; 2995 return -ENOMEM;
3014 } 2996 }
3015 edge_serial->bulk_in_buffer = 2997 edge_serial->bulk_in_buffer =
3016 kmalloc(buffer_size, GFP_KERNEL); 2998 kmalloc(buffer_size, GFP_KERNEL);
3017 if (!edge_serial->bulk_in_buffer) { 2999 if (!edge_serial->bulk_in_buffer) {
3018 dev_err(&dev->dev, "out of memory\n"); 3000 dev_err(&dev->dev, "out of memory\n");
3019 usb_free_urb(edge_serial->read_urb); 3001 usb_free_urb(edge_serial->read_urb);
3020 return -ENOMEM; 3002 return -ENOMEM;
3021 } 3003 }
3022 edge_serial->bulk_in_endpoint = 3004 edge_serial->bulk_in_endpoint =
3023 endpoint->bEndpointAddress; 3005 endpoint->bEndpointAddress;
3024 3006
3025 /* set up our bulk in urb */ 3007 /* set up our bulk in urb */
3026 usb_fill_bulk_urb(edge_serial->read_urb, dev, 3008 usb_fill_bulk_urb(edge_serial->read_urb, dev,
3027 usb_rcvbulkpipe(dev, 3009 usb_rcvbulkpipe(dev,
3028 endpoint->bEndpointAddress), 3010 endpoint->bEndpointAddress),
3029 edge_serial->bulk_in_buffer, 3011 edge_serial->bulk_in_buffer,
3030 usb_endpoint_maxp(endpoint), 3012 usb_endpoint_maxp(endpoint),
3031 edge_bulk_in_callback, 3013 edge_bulk_in_callback,
3032 edge_serial); 3014 edge_serial);
3033 bulk_in_found = true; 3015 bulk_in_found = true;
3034 } 3016 }
3035 3017
3036 if (!bulk_out_found && 3018 if (!bulk_out_found &&
3037 (usb_endpoint_is_bulk_out(endpoint))) { 3019 (usb_endpoint_is_bulk_out(endpoint))) {
3038 /* we found a bulk out endpoint */ 3020 /* we found a bulk out endpoint */
3039 dev_dbg(ddev, "found bulk out\n"); 3021 dev_dbg(ddev, "found bulk out\n");
3040 edge_serial->bulk_out_endpoint = 3022 edge_serial->bulk_out_endpoint =
3041 endpoint->bEndpointAddress; 3023 endpoint->bEndpointAddress;
3042 bulk_out_found = true; 3024 bulk_out_found = true;
3043 } 3025 }
3044 } 3026 }
3045 3027
3046 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) { 3028 if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
3047 dev_err(ddev, "Error - the proper endpoints were not found!\n"); 3029 dev_err(ddev, "Error - the proper endpoints were not found!\n");
3048 return -ENODEV; 3030 return -ENODEV;
3049 } 3031 }
3050 3032
3051 /* start interrupt read for this edgeport this interrupt will 3033 /* start interrupt read for this edgeport this interrupt will
3052 * continue as long as the edgeport is connected */ 3034 * continue as long as the edgeport is connected */
3053 response = usb_submit_urb(edge_serial->interrupt_read_urb, 3035 response = usb_submit_urb(edge_serial->interrupt_read_urb,
3054 GFP_KERNEL); 3036 GFP_KERNEL);
3055 if (response) 3037 if (response)
3056 dev_err(ddev, "%s - Error %d submitting control urb\n", 3038 dev_err(ddev, "%s - Error %d submitting control urb\n",
3057 __func__, response); 3039 __func__, response);
3058 } 3040 }
3059 return response; 3041 return response;
3060 } 3042 }
3061 3043
3062 3044
3063 /**************************************************************************** 3045 /****************************************************************************
3064 * edge_disconnect 3046 * edge_disconnect
3065 * This function is called whenever the device is removed from the usb bus. 3047 * This function is called whenever the device is removed from the usb bus.
3066 ****************************************************************************/ 3048 ****************************************************************************/
3067 static void edge_disconnect(struct usb_serial *serial) 3049 static void edge_disconnect(struct usb_serial *serial)
3068 { 3050 {
3069 struct edgeport_serial *edge_serial = usb_get_serial_data(serial); 3051 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3070 3052
3071 /* stop reads and writes on all ports */ 3053 /* stop reads and writes on all ports */
3072 /* free up our endpoint stuff */ 3054 /* free up our endpoint stuff */
3073 if (edge_serial->is_epic) { 3055 if (edge_serial->is_epic) {
3074 usb_kill_urb(edge_serial->interrupt_read_urb); 3056 usb_kill_urb(edge_serial->interrupt_read_urb);
3075 usb_free_urb(edge_serial->interrupt_read_urb); 3057 usb_free_urb(edge_serial->interrupt_read_urb);
3076 kfree(edge_serial->interrupt_in_buffer); 3058 kfree(edge_serial->interrupt_in_buffer);
3077 3059
3078 usb_kill_urb(edge_serial->read_urb); 3060 usb_kill_urb(edge_serial->read_urb);
3079 usb_free_urb(edge_serial->read_urb); 3061 usb_free_urb(edge_serial->read_urb);
3080 kfree(edge_serial->bulk_in_buffer); 3062 kfree(edge_serial->bulk_in_buffer);
3081 } 3063 }
3082 } 3064 }
3083 3065
3084 3066
3085 /**************************************************************************** 3067 /****************************************************************************
3086 * edge_release 3068 * edge_release
3087 * This function is called when the device structure is deallocated. 3069 * This function is called when the device structure is deallocated.
3088 ****************************************************************************/ 3070 ****************************************************************************/
3089 static void edge_release(struct usb_serial *serial) 3071 static void edge_release(struct usb_serial *serial)
3090 { 3072 {
3091 struct edgeport_serial *edge_serial = usb_get_serial_data(serial); 3073 struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
3092 3074
3093 kfree(edge_serial); 3075 kfree(edge_serial);
3094 } 3076 }
3095 3077
3096 static int edge_port_probe(struct usb_serial_port *port) 3078 static int edge_port_probe(struct usb_serial_port *port)
3097 { 3079 {
3098 struct edgeport_port *edge_port; 3080 struct edgeport_port *edge_port;
3099 3081
3100 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL); 3082 edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
3101 if (!edge_port) 3083 if (!edge_port)
3102 return -ENOMEM; 3084 return -ENOMEM;
3103 3085
3104 spin_lock_init(&edge_port->ep_lock); 3086 spin_lock_init(&edge_port->ep_lock);
3105 edge_port->port = port; 3087 edge_port->port = port;
3106 3088
3107 usb_set_serial_port_data(port, edge_port); 3089 usb_set_serial_port_data(port, edge_port);
3108 3090
3109 return 0; 3091 return 0;
3110 } 3092 }
3111 3093
3112 static int edge_port_remove(struct usb_serial_port *port) 3094 static int edge_port_remove(struct usb_serial_port *port)
3113 { 3095 {
3114 struct edgeport_port *edge_port; 3096 struct edgeport_port *edge_port;
3115 3097
3116 edge_port = usb_get_serial_port_data(port); 3098 edge_port = usb_get_serial_port_data(port);
3117 kfree(edge_port); 3099 kfree(edge_port);
3118 3100
3119 return 0; 3101 return 0;
3120 } 3102 }
3121 3103
3122 module_usb_serial_driver(serial_drivers, id_table_combined); 3104 module_usb_serial_driver(serial_drivers, id_table_combined);
3123 3105
3124 MODULE_AUTHOR(DRIVER_AUTHOR); 3106 MODULE_AUTHOR(DRIVER_AUTHOR);
3125 MODULE_DESCRIPTION(DRIVER_DESC); 3107 MODULE_DESCRIPTION(DRIVER_DESC);
3126 MODULE_LICENSE("GPL"); 3108 MODULE_LICENSE("GPL");
3127 MODULE_FIRMWARE("edgeport/boot.fw"); 3109 MODULE_FIRMWARE("edgeport/boot.fw");
3128 MODULE_FIRMWARE("edgeport/boot2.fw"); 3110 MODULE_FIRMWARE("edgeport/boot2.fw");
3129 MODULE_FIRMWARE("edgeport/down.fw"); 3111 MODULE_FIRMWARE("edgeport/down.fw");
3130 MODULE_FIRMWARE("edgeport/down2.fw"); 3112 MODULE_FIRMWARE("edgeport/down2.fw");
drivers/usb/serial/keyspan_pda.c
1 /* 1 /*
2 * USB Keyspan PDA / Xircom / Entregra Converter driver 2 * USB Keyspan PDA / Xircom / Entregra Converter driver
3 * 3 *
4 * Copyright (C) 1999 - 2001 Greg Kroah-Hartman <greg@kroah.com> 4 * Copyright (C) 1999 - 2001 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 1999, 2000 Brian Warner <warner@lothar.com> 5 * Copyright (C) 1999, 2000 Brian Warner <warner@lothar.com>
6 * Copyright (C) 2000 Al Borchers <borchers@steinerpoint.com> 6 * Copyright (C) 2000 Al Borchers <borchers@steinerpoint.com>
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 11 * (at your option) any later version.
12 * 12 *
13 * See Documentation/usb/usb-serial.txt for more information on using this 13 * See Documentation/usb/usb-serial.txt for more information on using this
14 * driver 14 * driver
15 */ 15 */
16 16
17 17
18 #include <linux/kernel.h> 18 #include <linux/kernel.h>
19 #include <linux/errno.h> 19 #include <linux/errno.h>
20 #include <linux/init.h> 20 #include <linux/init.h>
21 #include <linux/slab.h> 21 #include <linux/slab.h>
22 #include <linux/tty.h> 22 #include <linux/tty.h>
23 #include <linux/tty_driver.h> 23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h> 24 #include <linux/tty_flip.h>
25 #include <linux/module.h> 25 #include <linux/module.h>
26 #include <linux/spinlock.h> 26 #include <linux/spinlock.h>
27 #include <linux/workqueue.h> 27 #include <linux/workqueue.h>
28 #include <linux/uaccess.h> 28 #include <linux/uaccess.h>
29 #include <linux/usb.h> 29 #include <linux/usb.h>
30 #include <linux/usb/serial.h> 30 #include <linux/usb/serial.h>
31 #include <linux/usb/ezusb.h> 31 #include <linux/usb/ezusb.h>
32 32
33 /* make a simple define to handle if we are compiling keyspan_pda or xircom support */ 33 /* make a simple define to handle if we are compiling keyspan_pda or xircom support */
34 #if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE) 34 #if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE)
35 #define KEYSPAN 35 #define KEYSPAN
36 #else 36 #else
37 #undef KEYSPAN 37 #undef KEYSPAN
38 #endif 38 #endif
39 #if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE) 39 #if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE)
40 #define XIRCOM 40 #define XIRCOM
41 #else 41 #else
42 #undef XIRCOM 42 #undef XIRCOM
43 #endif 43 #endif
44 44
45 #define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>" 45 #define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>"
46 #define DRIVER_DESC "USB Keyspan PDA Converter driver" 46 #define DRIVER_DESC "USB Keyspan PDA Converter driver"
47 47
48 struct keyspan_pda_private { 48 struct keyspan_pda_private {
49 int tx_room; 49 int tx_room;
50 int tx_throttled; 50 int tx_throttled;
51 struct work_struct wakeup_work; 51 struct work_struct wakeup_work;
52 struct work_struct unthrottle_work; 52 struct work_struct unthrottle_work;
53 struct usb_serial *serial; 53 struct usb_serial *serial;
54 struct usb_serial_port *port; 54 struct usb_serial_port *port;
55 }; 55 };
56 56
57 57
58 #define KEYSPAN_VENDOR_ID 0x06cd 58 #define KEYSPAN_VENDOR_ID 0x06cd
59 #define KEYSPAN_PDA_FAKE_ID 0x0103 59 #define KEYSPAN_PDA_FAKE_ID 0x0103
60 #define KEYSPAN_PDA_ID 0x0104 /* no clue */ 60 #define KEYSPAN_PDA_ID 0x0104 /* no clue */
61 61
62 /* For Xircom PGSDB9 and older Entregra version of the same device */ 62 /* For Xircom PGSDB9 and older Entregra version of the same device */
63 #define XIRCOM_VENDOR_ID 0x085a 63 #define XIRCOM_VENDOR_ID 0x085a
64 #define XIRCOM_FAKE_ID 0x8027 64 #define XIRCOM_FAKE_ID 0x8027
65 #define ENTREGRA_VENDOR_ID 0x1645 65 #define ENTREGRA_VENDOR_ID 0x1645
66 #define ENTREGRA_FAKE_ID 0x8093 66 #define ENTREGRA_FAKE_ID 0x8093
67 67
68 static const struct usb_device_id id_table_combined[] = { 68 static const struct usb_device_id id_table_combined[] = {
69 #ifdef KEYSPAN 69 #ifdef KEYSPAN
70 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) }, 70 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
71 #endif 71 #endif
72 #ifdef XIRCOM 72 #ifdef XIRCOM
73 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) }, 73 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
74 { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) }, 74 { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
75 #endif 75 #endif
76 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) }, 76 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
77 { } /* Terminating entry */ 77 { } /* Terminating entry */
78 }; 78 };
79 79
80 MODULE_DEVICE_TABLE(usb, id_table_combined); 80 MODULE_DEVICE_TABLE(usb, id_table_combined);
81 81
82 static const struct usb_device_id id_table_std[] = { 82 static const struct usb_device_id id_table_std[] = {
83 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) }, 83 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
84 { } /* Terminating entry */ 84 { } /* Terminating entry */
85 }; 85 };
86 86
87 #ifdef KEYSPAN 87 #ifdef KEYSPAN
88 static const struct usb_device_id id_table_fake[] = { 88 static const struct usb_device_id id_table_fake[] = {
89 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) }, 89 { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
90 { } /* Terminating entry */ 90 { } /* Terminating entry */
91 }; 91 };
92 #endif 92 #endif
93 93
94 #ifdef XIRCOM 94 #ifdef XIRCOM
95 static const struct usb_device_id id_table_fake_xircom[] = { 95 static const struct usb_device_id id_table_fake_xircom[] = {
96 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) }, 96 { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
97 { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) }, 97 { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
98 { } 98 { }
99 }; 99 };
100 #endif 100 #endif
101 101
102 static void keyspan_pda_wakeup_write(struct work_struct *work) 102 static void keyspan_pda_wakeup_write(struct work_struct *work)
103 { 103 {
104 struct keyspan_pda_private *priv = 104 struct keyspan_pda_private *priv =
105 container_of(work, struct keyspan_pda_private, wakeup_work); 105 container_of(work, struct keyspan_pda_private, wakeup_work);
106 struct usb_serial_port *port = priv->port; 106 struct usb_serial_port *port = priv->port;
107 struct tty_struct *tty = tty_port_tty_get(&port->port); 107
108 if (tty) 108 tty_port_tty_wakeup(&port->port);
109 tty_wakeup(tty);
110 tty_kref_put(tty);
111 } 109 }
112 110
113 static void keyspan_pda_request_unthrottle(struct work_struct *work) 111 static void keyspan_pda_request_unthrottle(struct work_struct *work)
114 { 112 {
115 struct keyspan_pda_private *priv = 113 struct keyspan_pda_private *priv =
116 container_of(work, struct keyspan_pda_private, unthrottle_work); 114 container_of(work, struct keyspan_pda_private, unthrottle_work);
117 struct usb_serial *serial = priv->serial; 115 struct usb_serial *serial = priv->serial;
118 int result; 116 int result;
119 117
120 /* ask the device to tell us when the tx buffer becomes 118 /* ask the device to tell us when the tx buffer becomes
121 sufficiently empty */ 119 sufficiently empty */
122 result = usb_control_msg(serial->dev, 120 result = usb_control_msg(serial->dev,
123 usb_sndctrlpipe(serial->dev, 0), 121 usb_sndctrlpipe(serial->dev, 0),
124 7, /* request_unthrottle */ 122 7, /* request_unthrottle */
125 USB_TYPE_VENDOR | USB_RECIP_INTERFACE 123 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
126 | USB_DIR_OUT, 124 | USB_DIR_OUT,
127 16, /* value: threshold */ 125 16, /* value: threshold */
128 0, /* index */ 126 0, /* index */
129 NULL, 127 NULL,
130 0, 128 0,
131 2000); 129 2000);
132 if (result < 0) 130 if (result < 0)
133 dev_dbg(&serial->dev->dev, "%s - error %d from usb_control_msg\n", 131 dev_dbg(&serial->dev->dev, "%s - error %d from usb_control_msg\n",
134 __func__, result); 132 __func__, result);
135 } 133 }
136 134
137 135
138 static void keyspan_pda_rx_interrupt(struct urb *urb) 136 static void keyspan_pda_rx_interrupt(struct urb *urb)
139 { 137 {
140 struct usb_serial_port *port = urb->context; 138 struct usb_serial_port *port = urb->context;
141 unsigned char *data = urb->transfer_buffer; 139 unsigned char *data = urb->transfer_buffer;
142 int retval; 140 int retval;
143 int status = urb->status; 141 int status = urb->status;
144 struct keyspan_pda_private *priv; 142 struct keyspan_pda_private *priv;
145 priv = usb_get_serial_port_data(port); 143 priv = usb_get_serial_port_data(port);
146 144
147 switch (status) { 145 switch (status) {
148 case 0: 146 case 0:
149 /* success */ 147 /* success */
150 break; 148 break;
151 case -ECONNRESET: 149 case -ECONNRESET:
152 case -ENOENT: 150 case -ENOENT:
153 case -ESHUTDOWN: 151 case -ESHUTDOWN:
154 /* this urb is terminated, clean up */ 152 /* this urb is terminated, clean up */
155 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status); 153 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
156 return; 154 return;
157 default: 155 default:
158 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status); 156 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
159 goto exit; 157 goto exit;
160 } 158 }
161 159
162 /* see if the message is data or a status interrupt */ 160 /* see if the message is data or a status interrupt */
163 switch (data[0]) { 161 switch (data[0]) {
164 case 0: 162 case 0:
165 /* rest of message is rx data */ 163 /* rest of message is rx data */
166 if (urb->actual_length) { 164 if (urb->actual_length) {
167 tty_insert_flip_string(&port->port, data + 1, 165 tty_insert_flip_string(&port->port, data + 1,
168 urb->actual_length - 1); 166 urb->actual_length - 1);
169 tty_flip_buffer_push(&port->port); 167 tty_flip_buffer_push(&port->port);
170 } 168 }
171 break; 169 break;
172 case 1: 170 case 1:
173 /* status interrupt */ 171 /* status interrupt */
174 dev_dbg(&port->dev, "rx int, d1=%d, d2=%d\n", data[1], data[2]); 172 dev_dbg(&port->dev, "rx int, d1=%d, d2=%d\n", data[1], data[2]);
175 switch (data[1]) { 173 switch (data[1]) {
176 case 1: /* modemline change */ 174 case 1: /* modemline change */
177 break; 175 break;
178 case 2: /* tx unthrottle interrupt */ 176 case 2: /* tx unthrottle interrupt */
179 priv->tx_throttled = 0; 177 priv->tx_throttled = 0;
180 /* queue up a wakeup at scheduler time */ 178 /* queue up a wakeup at scheduler time */
181 schedule_work(&priv->wakeup_work); 179 schedule_work(&priv->wakeup_work);
182 break; 180 break;
183 default: 181 default:
184 break; 182 break;
185 } 183 }
186 break; 184 break;
187 default: 185 default:
188 break; 186 break;
189 } 187 }
190 188
191 exit: 189 exit:
192 retval = usb_submit_urb(urb, GFP_ATOMIC); 190 retval = usb_submit_urb(urb, GFP_ATOMIC);
193 if (retval) 191 if (retval)
194 dev_err(&port->dev, 192 dev_err(&port->dev,
195 "%s - usb_submit_urb failed with result %d", 193 "%s - usb_submit_urb failed with result %d",
196 __func__, retval); 194 __func__, retval);
197 } 195 }
198 196
199 197
200 static void keyspan_pda_rx_throttle(struct tty_struct *tty) 198 static void keyspan_pda_rx_throttle(struct tty_struct *tty)
201 { 199 {
202 /* stop receiving characters. We just turn off the URB request, and 200 /* stop receiving characters. We just turn off the URB request, and
203 let chars pile up in the device. If we're doing hardware 201 let chars pile up in the device. If we're doing hardware
204 flowcontrol, the device will signal the other end when its buffer 202 flowcontrol, the device will signal the other end when its buffer
205 fills up. If we're doing XON/XOFF, this would be a good time to 203 fills up. If we're doing XON/XOFF, this would be a good time to
206 send an XOFF, although it might make sense to foist that off 204 send an XOFF, although it might make sense to foist that off
207 upon the device too. */ 205 upon the device too. */
208 struct usb_serial_port *port = tty->driver_data; 206 struct usb_serial_port *port = tty->driver_data;
209 207
210 usb_kill_urb(port->interrupt_in_urb); 208 usb_kill_urb(port->interrupt_in_urb);
211 } 209 }
212 210
213 211
214 static void keyspan_pda_rx_unthrottle(struct tty_struct *tty) 212 static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
215 { 213 {
216 struct usb_serial_port *port = tty->driver_data; 214 struct usb_serial_port *port = tty->driver_data;
217 /* just restart the receive interrupt URB */ 215 /* just restart the receive interrupt URB */
218 216
219 if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL)) 217 if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
220 dev_dbg(&port->dev, "usb_submit_urb(read urb) failed\n"); 218 dev_dbg(&port->dev, "usb_submit_urb(read urb) failed\n");
221 } 219 }
222 220
223 221
224 static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud) 222 static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
225 { 223 {
226 int rc; 224 int rc;
227 int bindex; 225 int bindex;
228 226
229 switch (baud) { 227 switch (baud) {
230 case 110: 228 case 110:
231 bindex = 0; 229 bindex = 0;
232 break; 230 break;
233 case 300: 231 case 300:
234 bindex = 1; 232 bindex = 1;
235 break; 233 break;
236 case 1200: 234 case 1200:
237 bindex = 2; 235 bindex = 2;
238 break; 236 break;
239 case 2400: 237 case 2400:
240 bindex = 3; 238 bindex = 3;
241 break; 239 break;
242 case 4800: 240 case 4800:
243 bindex = 4; 241 bindex = 4;
244 break; 242 break;
245 case 9600: 243 case 9600:
246 bindex = 5; 244 bindex = 5;
247 break; 245 break;
248 case 19200: 246 case 19200:
249 bindex = 6; 247 bindex = 6;
250 break; 248 break;
251 case 38400: 249 case 38400:
252 bindex = 7; 250 bindex = 7;
253 break; 251 break;
254 case 57600: 252 case 57600:
255 bindex = 8; 253 bindex = 8;
256 break; 254 break;
257 case 115200: 255 case 115200:
258 bindex = 9; 256 bindex = 9;
259 break; 257 break;
260 default: 258 default:
261 bindex = 5; /* Default to 9600 */ 259 bindex = 5; /* Default to 9600 */
262 baud = 9600; 260 baud = 9600;
263 } 261 }
264 262
265 /* rather than figure out how to sleep while waiting for this 263 /* rather than figure out how to sleep while waiting for this
266 to complete, I just use the "legacy" API. */ 264 to complete, I just use the "legacy" API. */
267 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 265 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
268 0, /* set baud */ 266 0, /* set baud */
269 USB_TYPE_VENDOR 267 USB_TYPE_VENDOR
270 | USB_RECIP_INTERFACE 268 | USB_RECIP_INTERFACE
271 | USB_DIR_OUT, /* type */ 269 | USB_DIR_OUT, /* type */
272 bindex, /* value */ 270 bindex, /* value */
273 0, /* index */ 271 0, /* index */
274 NULL, /* &data */ 272 NULL, /* &data */
275 0, /* size */ 273 0, /* size */
276 2000); /* timeout */ 274 2000); /* timeout */
277 if (rc < 0) 275 if (rc < 0)
278 return 0; 276 return 0;
279 return baud; 277 return baud;
280 } 278 }
281 279
282 280
283 static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state) 281 static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
284 { 282 {
285 struct usb_serial_port *port = tty->driver_data; 283 struct usb_serial_port *port = tty->driver_data;
286 struct usb_serial *serial = port->serial; 284 struct usb_serial *serial = port->serial;
287 int value; 285 int value;
288 int result; 286 int result;
289 287
290 if (break_state == -1) 288 if (break_state == -1)
291 value = 1; /* start break */ 289 value = 1; /* start break */
292 else 290 else
293 value = 0; /* clear break */ 291 value = 0; /* clear break */
294 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 292 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
295 4, /* set break */ 293 4, /* set break */
296 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT, 294 USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
297 value, 0, NULL, 0, 2000); 295 value, 0, NULL, 0, 2000);
298 if (result < 0) 296 if (result < 0)
299 dev_dbg(&port->dev, "%s - error %d from usb_control_msg\n", 297 dev_dbg(&port->dev, "%s - error %d from usb_control_msg\n",
300 __func__, result); 298 __func__, result);
301 /* there is something funky about this.. the TCSBRK that 'cu' performs 299 /* there is something funky about this.. the TCSBRK that 'cu' performs
302 ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4 300 ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4
303 seconds apart, but it feels like the break sent isn't as long as it 301 seconds apart, but it feels like the break sent isn't as long as it
304 is on /dev/ttyS0 */ 302 is on /dev/ttyS0 */
305 } 303 }
306 304
307 305
308 static void keyspan_pda_set_termios(struct tty_struct *tty, 306 static void keyspan_pda_set_termios(struct tty_struct *tty,
309 struct usb_serial_port *port, struct ktermios *old_termios) 307 struct usb_serial_port *port, struct ktermios *old_termios)
310 { 308 {
311 struct usb_serial *serial = port->serial; 309 struct usb_serial *serial = port->serial;
312 speed_t speed; 310 speed_t speed;
313 311
314 /* cflag specifies lots of stuff: number of stop bits, parity, number 312 /* cflag specifies lots of stuff: number of stop bits, parity, number
315 of data bits, baud. What can the device actually handle?: 313 of data bits, baud. What can the device actually handle?:
316 CSTOPB (1 stop bit or 2) 314 CSTOPB (1 stop bit or 2)
317 PARENB (parity) 315 PARENB (parity)
318 CSIZE (5bit .. 8bit) 316 CSIZE (5bit .. 8bit)
319 There is minimal hw support for parity (a PSW bit seems to hold the 317 There is minimal hw support for parity (a PSW bit seems to hold the
320 parity of whatever is in the accumulator). The UART either deals 318 parity of whatever is in the accumulator). The UART either deals
321 with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data, 319 with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
322 1 special, stop). So, with firmware changes, we could do: 320 1 special, stop). So, with firmware changes, we could do:
323 8N1: 10 bit 321 8N1: 10 bit
324 8N2: 11 bit, extra bit always (mark?) 322 8N2: 11 bit, extra bit always (mark?)
325 8[EOMS]1: 11 bit, extra bit is parity 323 8[EOMS]1: 11 bit, extra bit is parity
326 7[EOMS]1: 10 bit, b0/b7 is parity 324 7[EOMS]1: 10 bit, b0/b7 is parity
327 7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?) 325 7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)
328 326
329 HW flow control is dictated by the tty->termios.c_cflags & CRTSCTS 327 HW flow control is dictated by the tty->termios.c_cflags & CRTSCTS
330 bit. 328 bit.
331 329
332 For now, just do baud. */ 330 For now, just do baud. */
333 331
334 speed = tty_get_baud_rate(tty); 332 speed = tty_get_baud_rate(tty);
335 speed = keyspan_pda_setbaud(serial, speed); 333 speed = keyspan_pda_setbaud(serial, speed);
336 334
337 if (speed == 0) { 335 if (speed == 0) {
338 dev_dbg(&port->dev, "can't handle requested baud rate\n"); 336 dev_dbg(&port->dev, "can't handle requested baud rate\n");
339 /* It hasn't changed so.. */ 337 /* It hasn't changed so.. */
340 speed = tty_termios_baud_rate(old_termios); 338 speed = tty_termios_baud_rate(old_termios);
341 } 339 }
342 /* Only speed can change so copy the old h/w parameters 340 /* Only speed can change so copy the old h/w parameters
343 then encode the new speed */ 341 then encode the new speed */
344 tty_termios_copy_hw(&tty->termios, old_termios); 342 tty_termios_copy_hw(&tty->termios, old_termios);
345 tty_encode_baud_rate(tty, speed, speed); 343 tty_encode_baud_rate(tty, speed, speed);
346 } 344 }
347 345
348 346
349 /* modem control pins: DTR and RTS are outputs and can be controlled. 347 /* modem control pins: DTR and RTS are outputs and can be controlled.
350 DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be 348 DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
351 read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */ 349 read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */
352 350
353 static int keyspan_pda_get_modem_info(struct usb_serial *serial, 351 static int keyspan_pda_get_modem_info(struct usb_serial *serial,
354 unsigned char *value) 352 unsigned char *value)
355 { 353 {
356 int rc; 354 int rc;
357 u8 *data; 355 u8 *data;
358 356
359 data = kmalloc(1, GFP_KERNEL); 357 data = kmalloc(1, GFP_KERNEL);
360 if (!data) 358 if (!data)
361 return -ENOMEM; 359 return -ENOMEM;
362 360
363 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 361 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
364 3, /* get pins */ 362 3, /* get pins */
365 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN, 363 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
366 0, 0, data, 1, 2000); 364 0, 0, data, 1, 2000);
367 if (rc >= 0) 365 if (rc >= 0)
368 *value = *data; 366 *value = *data;
369 367
370 kfree(data); 368 kfree(data);
371 return rc; 369 return rc;
372 } 370 }
373 371
374 372
375 static int keyspan_pda_set_modem_info(struct usb_serial *serial, 373 static int keyspan_pda_set_modem_info(struct usb_serial *serial,
376 unsigned char value) 374 unsigned char value)
377 { 375 {
378 int rc; 376 int rc;
379 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 377 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
380 3, /* set pins */ 378 3, /* set pins */
381 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT, 379 USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
382 value, 0, NULL, 0, 2000); 380 value, 0, NULL, 0, 2000);
383 return rc; 381 return rc;
384 } 382 }
385 383
386 static int keyspan_pda_tiocmget(struct tty_struct *tty) 384 static int keyspan_pda_tiocmget(struct tty_struct *tty)
387 { 385 {
388 struct usb_serial_port *port = tty->driver_data; 386 struct usb_serial_port *port = tty->driver_data;
389 struct usb_serial *serial = port->serial; 387 struct usb_serial *serial = port->serial;
390 int rc; 388 int rc;
391 unsigned char status; 389 unsigned char status;
392 int value; 390 int value;
393 391
394 rc = keyspan_pda_get_modem_info(serial, &status); 392 rc = keyspan_pda_get_modem_info(serial, &status);
395 if (rc < 0) 393 if (rc < 0)
396 return rc; 394 return rc;
397 value = 395 value =
398 ((status & (1<<7)) ? TIOCM_DTR : 0) | 396 ((status & (1<<7)) ? TIOCM_DTR : 0) |
399 ((status & (1<<6)) ? TIOCM_CAR : 0) | 397 ((status & (1<<6)) ? TIOCM_CAR : 0) |
400 ((status & (1<<5)) ? TIOCM_RNG : 0) | 398 ((status & (1<<5)) ? TIOCM_RNG : 0) |
401 ((status & (1<<4)) ? TIOCM_DSR : 0) | 399 ((status & (1<<4)) ? TIOCM_DSR : 0) |
402 ((status & (1<<3)) ? TIOCM_CTS : 0) | 400 ((status & (1<<3)) ? TIOCM_CTS : 0) |
403 ((status & (1<<2)) ? TIOCM_RTS : 0); 401 ((status & (1<<2)) ? TIOCM_RTS : 0);
404 return value; 402 return value;
405 } 403 }
406 404
407 static int keyspan_pda_tiocmset(struct tty_struct *tty, 405 static int keyspan_pda_tiocmset(struct tty_struct *tty,
408 unsigned int set, unsigned int clear) 406 unsigned int set, unsigned int clear)
409 { 407 {
410 struct usb_serial_port *port = tty->driver_data; 408 struct usb_serial_port *port = tty->driver_data;
411 struct usb_serial *serial = port->serial; 409 struct usb_serial *serial = port->serial;
412 int rc; 410 int rc;
413 unsigned char status; 411 unsigned char status;
414 412
415 rc = keyspan_pda_get_modem_info(serial, &status); 413 rc = keyspan_pda_get_modem_info(serial, &status);
416 if (rc < 0) 414 if (rc < 0)
417 return rc; 415 return rc;
418 416
419 if (set & TIOCM_RTS) 417 if (set & TIOCM_RTS)
420 status |= (1<<2); 418 status |= (1<<2);
421 if (set & TIOCM_DTR) 419 if (set & TIOCM_DTR)
422 status |= (1<<7); 420 status |= (1<<7);
423 421
424 if (clear & TIOCM_RTS) 422 if (clear & TIOCM_RTS)
425 status &= ~(1<<2); 423 status &= ~(1<<2);
426 if (clear & TIOCM_DTR) 424 if (clear & TIOCM_DTR)
427 status &= ~(1<<7); 425 status &= ~(1<<7);
428 rc = keyspan_pda_set_modem_info(serial, status); 426 rc = keyspan_pda_set_modem_info(serial, status);
429 return rc; 427 return rc;
430 } 428 }
431 429
432 static int keyspan_pda_write(struct tty_struct *tty, 430 static int keyspan_pda_write(struct tty_struct *tty,
433 struct usb_serial_port *port, const unsigned char *buf, int count) 431 struct usb_serial_port *port, const unsigned char *buf, int count)
434 { 432 {
435 struct usb_serial *serial = port->serial; 433 struct usb_serial *serial = port->serial;
436 int request_unthrottle = 0; 434 int request_unthrottle = 0;
437 int rc = 0; 435 int rc = 0;
438 struct keyspan_pda_private *priv; 436 struct keyspan_pda_private *priv;
439 437
440 priv = usb_get_serial_port_data(port); 438 priv = usb_get_serial_port_data(port);
441 /* guess how much room is left in the device's ring buffer, and if we 439 /* guess how much room is left in the device's ring buffer, and if we
442 want to send more than that, check first, updating our notion of 440 want to send more than that, check first, updating our notion of
443 what is left. If our write will result in no room left, ask the 441 what is left. If our write will result in no room left, ask the
444 device to give us an interrupt when the room available rises above 442 device to give us an interrupt when the room available rises above
445 a threshold, and hold off all writers (eventually, those using 443 a threshold, and hold off all writers (eventually, those using
446 select() or poll() too) until we receive that unthrottle interrupt. 444 select() or poll() too) until we receive that unthrottle interrupt.
447 Block if we can't write anything at all, otherwise write as much as 445 Block if we can't write anything at all, otherwise write as much as
448 we can. */ 446 we can. */
449 if (count == 0) { 447 if (count == 0) {
450 dev_dbg(&port->dev, "write request of 0 bytes\n"); 448 dev_dbg(&port->dev, "write request of 0 bytes\n");
451 return 0; 449 return 0;
452 } 450 }
453 451
454 /* we might block because of: 452 /* we might block because of:
455 the TX urb is in-flight (wait until it completes) 453 the TX urb is in-flight (wait until it completes)
456 the device is full (wait until it says there is room) 454 the device is full (wait until it says there is room)
457 */ 455 */
458 spin_lock_bh(&port->lock); 456 spin_lock_bh(&port->lock);
459 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled) { 457 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled) {
460 spin_unlock_bh(&port->lock); 458 spin_unlock_bh(&port->lock);
461 return 0; 459 return 0;
462 } 460 }
463 clear_bit(0, &port->write_urbs_free); 461 clear_bit(0, &port->write_urbs_free);
464 spin_unlock_bh(&port->lock); 462 spin_unlock_bh(&port->lock);
465 463
466 /* At this point the URB is in our control, nobody else can submit it 464 /* At this point the URB is in our control, nobody else can submit it
467 again (the only sudden transition was the one from EINPROGRESS to 465 again (the only sudden transition was the one from EINPROGRESS to
468 finished). Also, the tx process is not throttled. So we are 466 finished). Also, the tx process is not throttled. So we are
469 ready to write. */ 467 ready to write. */
470 468
471 count = (count > port->bulk_out_size) ? port->bulk_out_size : count; 469 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
472 470
473 /* Check if we might overrun the Tx buffer. If so, ask the 471 /* Check if we might overrun the Tx buffer. If so, ask the
474 device how much room it really has. This is done only on 472 device how much room it really has. This is done only on
475 scheduler time, since usb_control_msg() sleeps. */ 473 scheduler time, since usb_control_msg() sleeps. */
476 if (count > priv->tx_room && !in_interrupt()) { 474 if (count > priv->tx_room && !in_interrupt()) {
477 u8 *room; 475 u8 *room;
478 476
479 room = kmalloc(1, GFP_KERNEL); 477 room = kmalloc(1, GFP_KERNEL);
480 if (!room) { 478 if (!room) {
481 rc = -ENOMEM; 479 rc = -ENOMEM;
482 goto exit; 480 goto exit;
483 } 481 }
484 482
485 rc = usb_control_msg(serial->dev, 483 rc = usb_control_msg(serial->dev,
486 usb_rcvctrlpipe(serial->dev, 0), 484 usb_rcvctrlpipe(serial->dev, 0),
487 6, /* write_room */ 485 6, /* write_room */
488 USB_TYPE_VENDOR | USB_RECIP_INTERFACE 486 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
489 | USB_DIR_IN, 487 | USB_DIR_IN,
490 0, /* value: 0 means "remaining room" */ 488 0, /* value: 0 means "remaining room" */
491 0, /* index */ 489 0, /* index */
492 room, 490 room,
493 1, 491 1,
494 2000); 492 2000);
495 if (rc > 0) { 493 if (rc > 0) {
496 dev_dbg(&port->dev, "roomquery says %d\n", *room); 494 dev_dbg(&port->dev, "roomquery says %d\n", *room);
497 priv->tx_room = *room; 495 priv->tx_room = *room;
498 } 496 }
499 kfree(room); 497 kfree(room);
500 if (rc < 0) { 498 if (rc < 0) {
501 dev_dbg(&port->dev, "roomquery failed\n"); 499 dev_dbg(&port->dev, "roomquery failed\n");
502 goto exit; 500 goto exit;
503 } 501 }
504 if (rc == 0) { 502 if (rc == 0) {
505 dev_dbg(&port->dev, "roomquery returned 0 bytes\n"); 503 dev_dbg(&port->dev, "roomquery returned 0 bytes\n");
506 rc = -EIO; /* device didn't return any data */ 504 rc = -EIO; /* device didn't return any data */
507 goto exit; 505 goto exit;
508 } 506 }
509 } 507 }
510 if (count > priv->tx_room) { 508 if (count > priv->tx_room) {
511 /* we're about to completely fill the Tx buffer, so 509 /* we're about to completely fill the Tx buffer, so
512 we'll be throttled afterwards. */ 510 we'll be throttled afterwards. */
513 count = priv->tx_room; 511 count = priv->tx_room;
514 request_unthrottle = 1; 512 request_unthrottle = 1;
515 } 513 }
516 514
517 if (count) { 515 if (count) {
518 /* now transfer data */ 516 /* now transfer data */
519 memcpy(port->write_urb->transfer_buffer, buf, count); 517 memcpy(port->write_urb->transfer_buffer, buf, count);
520 /* send the data out the bulk port */ 518 /* send the data out the bulk port */
521 port->write_urb->transfer_buffer_length = count; 519 port->write_urb->transfer_buffer_length = count;
522 520
523 priv->tx_room -= count; 521 priv->tx_room -= count;
524 522
525 rc = usb_submit_urb(port->write_urb, GFP_ATOMIC); 523 rc = usb_submit_urb(port->write_urb, GFP_ATOMIC);
526 if (rc) { 524 if (rc) {
527 dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed\n"); 525 dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed\n");
528 goto exit; 526 goto exit;
529 } 527 }
530 } else { 528 } else {
531 /* There wasn't any room left, so we are throttled until 529 /* There wasn't any room left, so we are throttled until
532 the buffer empties a bit */ 530 the buffer empties a bit */
533 request_unthrottle = 1; 531 request_unthrottle = 1;
534 } 532 }
535 533
536 if (request_unthrottle) { 534 if (request_unthrottle) {
537 priv->tx_throttled = 1; /* block writers */ 535 priv->tx_throttled = 1; /* block writers */
538 schedule_work(&priv->unthrottle_work); 536 schedule_work(&priv->unthrottle_work);
539 } 537 }
540 538
541 rc = count; 539 rc = count;
542 exit: 540 exit:
543 if (rc < 0) 541 if (rc < 0)
544 set_bit(0, &port->write_urbs_free); 542 set_bit(0, &port->write_urbs_free);
545 return rc; 543 return rc;
546 } 544 }
547 545
548 546
549 static void keyspan_pda_write_bulk_callback(struct urb *urb) 547 static void keyspan_pda_write_bulk_callback(struct urb *urb)
550 { 548 {
551 struct usb_serial_port *port = urb->context; 549 struct usb_serial_port *port = urb->context;
552 struct keyspan_pda_private *priv; 550 struct keyspan_pda_private *priv;
553 551
554 set_bit(0, &port->write_urbs_free); 552 set_bit(0, &port->write_urbs_free);
555 priv = usb_get_serial_port_data(port); 553 priv = usb_get_serial_port_data(port);
556 554
557 /* queue up a wakeup at scheduler time */ 555 /* queue up a wakeup at scheduler time */
558 schedule_work(&priv->wakeup_work); 556 schedule_work(&priv->wakeup_work);
559 } 557 }
560 558
561 559
562 static int keyspan_pda_write_room(struct tty_struct *tty) 560 static int keyspan_pda_write_room(struct tty_struct *tty)
563 { 561 {
564 struct usb_serial_port *port = tty->driver_data; 562 struct usb_serial_port *port = tty->driver_data;
565 struct keyspan_pda_private *priv; 563 struct keyspan_pda_private *priv;
566 priv = usb_get_serial_port_data(port); 564 priv = usb_get_serial_port_data(port);
567 /* used by n_tty.c for processing of tabs and such. Giving it our 565 /* used by n_tty.c for processing of tabs and such. Giving it our
568 conservative guess is probably good enough, but needs testing by 566 conservative guess is probably good enough, but needs testing by
569 running a console through the device. */ 567 running a console through the device. */
570 return priv->tx_room; 568 return priv->tx_room;
571 } 569 }
572 570
573 571
574 static int keyspan_pda_chars_in_buffer(struct tty_struct *tty) 572 static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
575 { 573 {
576 struct usb_serial_port *port = tty->driver_data; 574 struct usb_serial_port *port = tty->driver_data;
577 struct keyspan_pda_private *priv; 575 struct keyspan_pda_private *priv;
578 unsigned long flags; 576 unsigned long flags;
579 int ret = 0; 577 int ret = 0;
580 578
581 priv = usb_get_serial_port_data(port); 579 priv = usb_get_serial_port_data(port);
582 580
583 /* when throttled, return at least WAKEUP_CHARS to tell select() (via 581 /* when throttled, return at least WAKEUP_CHARS to tell select() (via
584 n_tty.c:normal_poll() ) that we're not writeable. */ 582 n_tty.c:normal_poll() ) that we're not writeable. */
585 583
586 spin_lock_irqsave(&port->lock, flags); 584 spin_lock_irqsave(&port->lock, flags);
587 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled) 585 if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled)
588 ret = 256; 586 ret = 256;
589 spin_unlock_irqrestore(&port->lock, flags); 587 spin_unlock_irqrestore(&port->lock, flags);
590 return ret; 588 return ret;
591 } 589 }
592 590
593 591
594 static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on) 592 static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
595 { 593 {
596 struct usb_serial *serial = port->serial; 594 struct usb_serial *serial = port->serial;
597 595
598 if (serial->dev) { 596 if (serial->dev) {
599 if (on) 597 if (on)
600 keyspan_pda_set_modem_info(serial, (1<<7) | (1<< 2)); 598 keyspan_pda_set_modem_info(serial, (1<<7) | (1<< 2));
601 else 599 else
602 keyspan_pda_set_modem_info(serial, 0); 600 keyspan_pda_set_modem_info(serial, 0);
603 } 601 }
604 } 602 }
605 603
606 604
607 static int keyspan_pda_open(struct tty_struct *tty, 605 static int keyspan_pda_open(struct tty_struct *tty,
608 struct usb_serial_port *port) 606 struct usb_serial_port *port)
609 { 607 {
610 struct usb_serial *serial = port->serial; 608 struct usb_serial *serial = port->serial;
611 u8 *room; 609 u8 *room;
612 int rc = 0; 610 int rc = 0;
613 struct keyspan_pda_private *priv; 611 struct keyspan_pda_private *priv;
614 612
615 /* find out how much room is in the Tx ring */ 613 /* find out how much room is in the Tx ring */
616 room = kmalloc(1, GFP_KERNEL); 614 room = kmalloc(1, GFP_KERNEL);
617 if (!room) 615 if (!room)
618 return -ENOMEM; 616 return -ENOMEM;
619 617
620 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 618 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
621 6, /* write_room */ 619 6, /* write_room */
622 USB_TYPE_VENDOR | USB_RECIP_INTERFACE 620 USB_TYPE_VENDOR | USB_RECIP_INTERFACE
623 | USB_DIR_IN, 621 | USB_DIR_IN,
624 0, /* value */ 622 0, /* value */
625 0, /* index */ 623 0, /* index */
626 room, 624 room,
627 1, 625 1,
628 2000); 626 2000);
629 if (rc < 0) { 627 if (rc < 0) {
630 dev_dbg(&port->dev, "%s - roomquery failed\n", __func__); 628 dev_dbg(&port->dev, "%s - roomquery failed\n", __func__);
631 goto error; 629 goto error;
632 } 630 }
633 if (rc == 0) { 631 if (rc == 0) {
634 dev_dbg(&port->dev, "%s - roomquery returned 0 bytes\n", __func__); 632 dev_dbg(&port->dev, "%s - roomquery returned 0 bytes\n", __func__);
635 rc = -EIO; 633 rc = -EIO;
636 goto error; 634 goto error;
637 } 635 }
638 priv = usb_get_serial_port_data(port); 636 priv = usb_get_serial_port_data(port);
639 priv->tx_room = *room; 637 priv->tx_room = *room;
640 priv->tx_throttled = *room ? 0 : 1; 638 priv->tx_throttled = *room ? 0 : 1;
641 639
642 /*Start reading from the device*/ 640 /*Start reading from the device*/
643 rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 641 rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
644 if (rc) { 642 if (rc) {
645 dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__); 643 dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__);
646 goto error; 644 goto error;
647 } 645 }
648 error: 646 error:
649 kfree(room); 647 kfree(room);
650 return rc; 648 return rc;
651 } 649 }
652 static void keyspan_pda_close(struct usb_serial_port *port) 650 static void keyspan_pda_close(struct usb_serial_port *port)
653 { 651 {
654 struct usb_serial *serial = port->serial; 652 struct usb_serial *serial = port->serial;
655 653
656 if (serial->dev) { 654 if (serial->dev) {
657 /* shutdown our bulk reads and writes */ 655 /* shutdown our bulk reads and writes */
658 usb_kill_urb(port->write_urb); 656 usb_kill_urb(port->write_urb);
659 usb_kill_urb(port->interrupt_in_urb); 657 usb_kill_urb(port->interrupt_in_urb);
660 } 658 }
661 } 659 }
662 660
663 661
664 /* download the firmware to a "fake" device (pre-renumeration) */ 662 /* download the firmware to a "fake" device (pre-renumeration) */
665 static int keyspan_pda_fake_startup(struct usb_serial *serial) 663 static int keyspan_pda_fake_startup(struct usb_serial *serial)
666 { 664 {
667 int response; 665 int response;
668 const char *fw_name; 666 const char *fw_name;
669 667
670 /* download the firmware here ... */ 668 /* download the firmware here ... */
671 response = ezusb_fx1_set_reset(serial->dev, 1); 669 response = ezusb_fx1_set_reset(serial->dev, 1);
672 670
673 if (0) { ; } 671 if (0) { ; }
674 #ifdef KEYSPAN 672 #ifdef KEYSPAN
675 else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID) 673 else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
676 fw_name = "keyspan_pda/keyspan_pda.fw"; 674 fw_name = "keyspan_pda/keyspan_pda.fw";
677 #endif 675 #endif
678 #ifdef XIRCOM 676 #ifdef XIRCOM
679 else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) || 677 else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
680 (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGRA_VENDOR_ID)) 678 (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGRA_VENDOR_ID))
681 fw_name = "keyspan_pda/xircom_pgs.fw"; 679 fw_name = "keyspan_pda/xircom_pgs.fw";
682 #endif 680 #endif
683 else { 681 else {
684 dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n", 682 dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
685 __func__); 683 __func__);
686 return -ENODEV; 684 return -ENODEV;
687 } 685 }
688 686
689 if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) { 687 if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
690 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n", 688 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
691 fw_name); 689 fw_name);
692 return -ENOENT; 690 return -ENOENT;
693 } 691 }
694 692
695 /* after downloading firmware Renumeration will occur in a 693 /* after downloading firmware Renumeration will occur in a
696 moment and the new device will bind to the real driver */ 694 moment and the new device will bind to the real driver */
697 695
698 /* we want this device to fail to have a driver assigned to it. */ 696 /* we want this device to fail to have a driver assigned to it. */
699 return 1; 697 return 1;
700 } 698 }
701 699
702 #ifdef KEYSPAN 700 #ifdef KEYSPAN
703 MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw"); 701 MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
704 #endif 702 #endif
705 #ifdef XIRCOM 703 #ifdef XIRCOM
706 MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw"); 704 MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
707 #endif 705 #endif
708 706
709 static int keyspan_pda_port_probe(struct usb_serial_port *port) 707 static int keyspan_pda_port_probe(struct usb_serial_port *port)
710 { 708 {
711 709
712 struct keyspan_pda_private *priv; 710 struct keyspan_pda_private *priv;
713 711
714 priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL); 712 priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
715 if (!priv) 713 if (!priv)
716 return -ENOMEM; 714 return -ENOMEM;
717 715
718 INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write); 716 INIT_WORK(&priv->wakeup_work, keyspan_pda_wakeup_write);
719 INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle); 717 INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
720 priv->serial = port->serial; 718 priv->serial = port->serial;
721 priv->port = port; 719 priv->port = port;
722 720
723 usb_set_serial_port_data(port, priv); 721 usb_set_serial_port_data(port, priv);
724 722
725 return 0; 723 return 0;
726 } 724 }
727 725
728 static int keyspan_pda_port_remove(struct usb_serial_port *port) 726 static int keyspan_pda_port_remove(struct usb_serial_port *port)
729 { 727 {
730 struct keyspan_pda_private *priv; 728 struct keyspan_pda_private *priv;
731 729
732 priv = usb_get_serial_port_data(port); 730 priv = usb_get_serial_port_data(port);
733 kfree(priv); 731 kfree(priv);
734 732
735 return 0; 733 return 0;
736 } 734 }
737 735
738 #ifdef KEYSPAN 736 #ifdef KEYSPAN
739 static struct usb_serial_driver keyspan_pda_fake_device = { 737 static struct usb_serial_driver keyspan_pda_fake_device = {
740 .driver = { 738 .driver = {
741 .owner = THIS_MODULE, 739 .owner = THIS_MODULE,
742 .name = "keyspan_pda_pre", 740 .name = "keyspan_pda_pre",
743 }, 741 },
744 .description = "Keyspan PDA - (prerenumeration)", 742 .description = "Keyspan PDA - (prerenumeration)",
745 .id_table = id_table_fake, 743 .id_table = id_table_fake,
746 .num_ports = 1, 744 .num_ports = 1,
747 .attach = keyspan_pda_fake_startup, 745 .attach = keyspan_pda_fake_startup,
748 }; 746 };
749 #endif 747 #endif
750 748
751 #ifdef XIRCOM 749 #ifdef XIRCOM
752 static struct usb_serial_driver xircom_pgs_fake_device = { 750 static struct usb_serial_driver xircom_pgs_fake_device = {
753 .driver = { 751 .driver = {
754 .owner = THIS_MODULE, 752 .owner = THIS_MODULE,
755 .name = "xircom_no_firm", 753 .name = "xircom_no_firm",
756 }, 754 },
757 .description = "Xircom / Entregra PGS - (prerenumeration)", 755 .description = "Xircom / Entregra PGS - (prerenumeration)",
758 .id_table = id_table_fake_xircom, 756 .id_table = id_table_fake_xircom,
759 .num_ports = 1, 757 .num_ports = 1,
760 .attach = keyspan_pda_fake_startup, 758 .attach = keyspan_pda_fake_startup,
761 }; 759 };
762 #endif 760 #endif
763 761
764 static struct usb_serial_driver keyspan_pda_device = { 762 static struct usb_serial_driver keyspan_pda_device = {
765 .driver = { 763 .driver = {
766 .owner = THIS_MODULE, 764 .owner = THIS_MODULE,
767 .name = "keyspan_pda", 765 .name = "keyspan_pda",
768 }, 766 },
769 .description = "Keyspan PDA", 767 .description = "Keyspan PDA",
770 .id_table = id_table_std, 768 .id_table = id_table_std,
771 .num_ports = 1, 769 .num_ports = 1,
772 .dtr_rts = keyspan_pda_dtr_rts, 770 .dtr_rts = keyspan_pda_dtr_rts,
773 .open = keyspan_pda_open, 771 .open = keyspan_pda_open,
774 .close = keyspan_pda_close, 772 .close = keyspan_pda_close,
775 .write = keyspan_pda_write, 773 .write = keyspan_pda_write,
776 .write_room = keyspan_pda_write_room, 774 .write_room = keyspan_pda_write_room,
777 .write_bulk_callback = keyspan_pda_write_bulk_callback, 775 .write_bulk_callback = keyspan_pda_write_bulk_callback,
778 .read_int_callback = keyspan_pda_rx_interrupt, 776 .read_int_callback = keyspan_pda_rx_interrupt,
779 .chars_in_buffer = keyspan_pda_chars_in_buffer, 777 .chars_in_buffer = keyspan_pda_chars_in_buffer,
780 .throttle = keyspan_pda_rx_throttle, 778 .throttle = keyspan_pda_rx_throttle,
781 .unthrottle = keyspan_pda_rx_unthrottle, 779 .unthrottle = keyspan_pda_rx_unthrottle,
782 .set_termios = keyspan_pda_set_termios, 780 .set_termios = keyspan_pda_set_termios,
783 .break_ctl = keyspan_pda_break_ctl, 781 .break_ctl = keyspan_pda_break_ctl,
784 .tiocmget = keyspan_pda_tiocmget, 782 .tiocmget = keyspan_pda_tiocmget,
785 .tiocmset = keyspan_pda_tiocmset, 783 .tiocmset = keyspan_pda_tiocmset,
786 .port_probe = keyspan_pda_port_probe, 784 .port_probe = keyspan_pda_port_probe,
787 .port_remove = keyspan_pda_port_remove, 785 .port_remove = keyspan_pda_port_remove,
788 }; 786 };
789 787
790 static struct usb_serial_driver * const serial_drivers[] = { 788 static struct usb_serial_driver * const serial_drivers[] = {
791 &keyspan_pda_device, 789 &keyspan_pda_device,
792 #ifdef KEYSPAN 790 #ifdef KEYSPAN
793 &keyspan_pda_fake_device, 791 &keyspan_pda_fake_device,
794 #endif 792 #endif
795 #ifdef XIRCOM 793 #ifdef XIRCOM
796 &xircom_pgs_fake_device, 794 &xircom_pgs_fake_device,
797 #endif 795 #endif
798 NULL 796 NULL
799 }; 797 };
800 798
801 module_usb_serial_driver(serial_drivers, id_table_combined); 799 module_usb_serial_driver(serial_drivers, id_table_combined);
802 800
803 MODULE_AUTHOR(DRIVER_AUTHOR); 801 MODULE_AUTHOR(DRIVER_AUTHOR);
804 MODULE_DESCRIPTION(DRIVER_DESC); 802 MODULE_DESCRIPTION(DRIVER_DESC);
805 MODULE_LICENSE("GPL"); 803 MODULE_LICENSE("GPL");
806 804
drivers/usb/serial/mos7720.c
1 /* 1 /*
2 * mos7720.c 2 * mos7720.c
3 * Controls the Moschip 7720 usb to dual port serial convertor 3 * Controls the Moschip 7720 usb to dual port serial convertor
4 * 4 *
5 * Copyright 2006 Moschip Semiconductor Tech. Ltd. 5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License. 9 * the Free Software Foundation, version 2 of the License.
10 * 10 *
11 * Developed by: 11 * Developed by:
12 * Vijaya Kumar <vijaykumar.gn@gmail.com> 12 * Vijaya Kumar <vijaykumar.gn@gmail.com>
13 * Ajay Kumar <naanuajay@yahoo.com> 13 * Ajay Kumar <naanuajay@yahoo.com>
14 * Gurudeva <ngurudeva@yahoo.com> 14 * Gurudeva <ngurudeva@yahoo.com>
15 * 15 *
16 * Cleaned up from the original by: 16 * Cleaned up from the original by:
17 * Greg Kroah-Hartman <gregkh@suse.de> 17 * Greg Kroah-Hartman <gregkh@suse.de>
18 * 18 *
19 * Originally based on drivers/usb/serial/io_edgeport.c which is: 19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved. 20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> 21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22 */ 22 */
23 #include <linux/kernel.h> 23 #include <linux/kernel.h>
24 #include <linux/errno.h> 24 #include <linux/errno.h>
25 #include <linux/init.h> 25 #include <linux/init.h>
26 #include <linux/slab.h> 26 #include <linux/slab.h>
27 #include <linux/tty.h> 27 #include <linux/tty.h>
28 #include <linux/tty_driver.h> 28 #include <linux/tty_driver.h>
29 #include <linux/tty_flip.h> 29 #include <linux/tty_flip.h>
30 #include <linux/module.h> 30 #include <linux/module.h>
31 #include <linux/spinlock.h> 31 #include <linux/spinlock.h>
32 #include <linux/serial.h> 32 #include <linux/serial.h>
33 #include <linux/serial_reg.h> 33 #include <linux/serial_reg.h>
34 #include <linux/usb.h> 34 #include <linux/usb.h>
35 #include <linux/usb/serial.h> 35 #include <linux/usb/serial.h>
36 #include <linux/uaccess.h> 36 #include <linux/uaccess.h>
37 #include <linux/parport.h> 37 #include <linux/parport.h>
38 38
39 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd." 39 #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
40 #define DRIVER_DESC "Moschip USB Serial Driver" 40 #define DRIVER_DESC "Moschip USB Serial Driver"
41 41
42 /* default urb timeout */ 42 /* default urb timeout */
43 #define MOS_WDR_TIMEOUT (HZ * 5) 43 #define MOS_WDR_TIMEOUT (HZ * 5)
44 44
45 #define MOS_MAX_PORT 0x02 45 #define MOS_MAX_PORT 0x02
46 #define MOS_WRITE 0x0E 46 #define MOS_WRITE 0x0E
47 #define MOS_READ 0x0D 47 #define MOS_READ 0x0D
48 48
49 /* Interrupt Rotinue Defines */ 49 /* Interrupt Rotinue Defines */
50 #define SERIAL_IIR_RLS 0x06 50 #define SERIAL_IIR_RLS 0x06
51 #define SERIAL_IIR_RDA 0x04 51 #define SERIAL_IIR_RDA 0x04
52 #define SERIAL_IIR_CTI 0x0c 52 #define SERIAL_IIR_CTI 0x0c
53 #define SERIAL_IIR_THR 0x02 53 #define SERIAL_IIR_THR 0x02
54 #define SERIAL_IIR_MS 0x00 54 #define SERIAL_IIR_MS 0x00
55 55
56 #define NUM_URBS 16 /* URB Count */ 56 #define NUM_URBS 16 /* URB Count */
57 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */ 57 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
58 58
59 /* This structure holds all of the local serial port information */ 59 /* This structure holds all of the local serial port information */
60 struct moschip_port { 60 struct moschip_port {
61 __u8 shadowLCR; /* last LCR value received */ 61 __u8 shadowLCR; /* last LCR value received */
62 __u8 shadowMCR; /* last MCR value received */ 62 __u8 shadowMCR; /* last MCR value received */
63 __u8 shadowMSR; /* last MSR value received */ 63 __u8 shadowMSR; /* last MSR value received */
64 char open; 64 char open;
65 struct async_icount icount; 65 struct async_icount icount;
66 struct usb_serial_port *port; /* loop back to the owner */ 66 struct usb_serial_port *port; /* loop back to the owner */
67 struct urb *write_urb_pool[NUM_URBS]; 67 struct urb *write_urb_pool[NUM_URBS];
68 }; 68 };
69 69
70 static struct usb_serial_driver moschip7720_2port_driver; 70 static struct usb_serial_driver moschip7720_2port_driver;
71 71
72 #define USB_VENDOR_ID_MOSCHIP 0x9710 72 #define USB_VENDOR_ID_MOSCHIP 0x9710
73 #define MOSCHIP_DEVICE_ID_7720 0x7720 73 #define MOSCHIP_DEVICE_ID_7720 0x7720
74 #define MOSCHIP_DEVICE_ID_7715 0x7715 74 #define MOSCHIP_DEVICE_ID_7715 0x7715
75 75
76 static const struct usb_device_id id_table[] = { 76 static const struct usb_device_id id_table[] = {
77 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) }, 77 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
78 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) }, 78 { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
79 { } /* terminating entry */ 79 { } /* terminating entry */
80 }; 80 };
81 MODULE_DEVICE_TABLE(usb, id_table); 81 MODULE_DEVICE_TABLE(usb, id_table);
82 82
83 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT 83 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
84 84
85 /* initial values for parport regs */ 85 /* initial values for parport regs */
86 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */ 86 #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
87 #define ECR_INIT_VAL 0x00 /* SPP mode */ 87 #define ECR_INIT_VAL 0x00 /* SPP mode */
88 88
89 struct urbtracker { 89 struct urbtracker {
90 struct mos7715_parport *mos_parport; 90 struct mos7715_parport *mos_parport;
91 struct list_head urblist_entry; 91 struct list_head urblist_entry;
92 struct kref ref_count; 92 struct kref ref_count;
93 struct urb *urb; 93 struct urb *urb;
94 }; 94 };
95 95
96 enum mos7715_pp_modes { 96 enum mos7715_pp_modes {
97 SPP = 0<<5, 97 SPP = 0<<5,
98 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */ 98 PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
99 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */ 99 PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
100 }; 100 };
101 101
102 struct mos7715_parport { 102 struct mos7715_parport {
103 struct parport *pp; /* back to containing struct */ 103 struct parport *pp; /* back to containing struct */
104 struct kref ref_count; /* to instance of this struct */ 104 struct kref ref_count; /* to instance of this struct */
105 struct list_head deferred_urbs; /* list deferred async urbs */ 105 struct list_head deferred_urbs; /* list deferred async urbs */
106 struct list_head active_urbs; /* list async urbs in flight */ 106 struct list_head active_urbs; /* list async urbs in flight */
107 spinlock_t listlock; /* protects list access */ 107 spinlock_t listlock; /* protects list access */
108 bool msg_pending; /* usb sync call pending */ 108 bool msg_pending; /* usb sync call pending */
109 struct completion syncmsg_compl; /* usb sync call completed */ 109 struct completion syncmsg_compl; /* usb sync call completed */
110 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */ 110 struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
111 struct usb_serial *serial; /* back to containing struct */ 111 struct usb_serial *serial; /* back to containing struct */
112 __u8 shadowECR; /* parallel port regs... */ 112 __u8 shadowECR; /* parallel port regs... */
113 __u8 shadowDCR; 113 __u8 shadowDCR;
114 atomic_t shadowDSR; /* updated in int-in callback */ 114 atomic_t shadowDSR; /* updated in int-in callback */
115 }; 115 };
116 116
117 /* lock guards against dereferencing NULL ptr in parport ops callbacks */ 117 /* lock guards against dereferencing NULL ptr in parport ops callbacks */
118 static DEFINE_SPINLOCK(release_lock); 118 static DEFINE_SPINLOCK(release_lock);
119 119
120 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */ 120 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
121 121
122 static const unsigned int dummy; /* for clarity in register access fns */ 122 static const unsigned int dummy; /* for clarity in register access fns */
123 123
124 enum mos_regs { 124 enum mos_regs {
125 THR, /* serial port regs */ 125 THR, /* serial port regs */
126 RHR, 126 RHR,
127 IER, 127 IER,
128 FCR, 128 FCR,
129 ISR, 129 ISR,
130 LCR, 130 LCR,
131 MCR, 131 MCR,
132 LSR, 132 LSR,
133 MSR, 133 MSR,
134 SPR, 134 SPR,
135 DLL, 135 DLL,
136 DLM, 136 DLM,
137 DPR, /* parallel port regs */ 137 DPR, /* parallel port regs */
138 DSR, 138 DSR,
139 DCR, 139 DCR,
140 ECR, 140 ECR,
141 SP1_REG, /* device control regs */ 141 SP1_REG, /* device control regs */
142 SP2_REG, /* serial port 2 (7720 only) */ 142 SP2_REG, /* serial port 2 (7720 only) */
143 PP_REG, 143 PP_REG,
144 SP_CONTROL_REG, 144 SP_CONTROL_REG,
145 }; 145 };
146 146
147 /* 147 /*
148 * Return the correct value for the Windex field of the setup packet 148 * Return the correct value for the Windex field of the setup packet
149 * for a control endpoint message. See the 7715 datasheet. 149 * for a control endpoint message. See the 7715 datasheet.
150 */ 150 */
151 static inline __u16 get_reg_index(enum mos_regs reg) 151 static inline __u16 get_reg_index(enum mos_regs reg)
152 { 152 {
153 static const __u16 mos7715_index_lookup_table[] = { 153 static const __u16 mos7715_index_lookup_table[] = {
154 0x00, /* THR */ 154 0x00, /* THR */
155 0x00, /* RHR */ 155 0x00, /* RHR */
156 0x01, /* IER */ 156 0x01, /* IER */
157 0x02, /* FCR */ 157 0x02, /* FCR */
158 0x02, /* ISR */ 158 0x02, /* ISR */
159 0x03, /* LCR */ 159 0x03, /* LCR */
160 0x04, /* MCR */ 160 0x04, /* MCR */
161 0x05, /* LSR */ 161 0x05, /* LSR */
162 0x06, /* MSR */ 162 0x06, /* MSR */
163 0x07, /* SPR */ 163 0x07, /* SPR */
164 0x00, /* DLL */ 164 0x00, /* DLL */
165 0x01, /* DLM */ 165 0x01, /* DLM */
166 0x00, /* DPR */ 166 0x00, /* DPR */
167 0x01, /* DSR */ 167 0x01, /* DSR */
168 0x02, /* DCR */ 168 0x02, /* DCR */
169 0x0a, /* ECR */ 169 0x0a, /* ECR */
170 0x01, /* SP1_REG */ 170 0x01, /* SP1_REG */
171 0x02, /* SP2_REG (7720 only) */ 171 0x02, /* SP2_REG (7720 only) */
172 0x04, /* PP_REG (7715 only) */ 172 0x04, /* PP_REG (7715 only) */
173 0x08, /* SP_CONTROL_REG */ 173 0x08, /* SP_CONTROL_REG */
174 }; 174 };
175 return mos7715_index_lookup_table[reg]; 175 return mos7715_index_lookup_table[reg];
176 } 176 }
177 177
178 /* 178 /*
179 * Return the correct value for the upper byte of the Wvalue field of 179 * Return the correct value for the upper byte of the Wvalue field of
180 * the setup packet for a control endpoint message. 180 * the setup packet for a control endpoint message.
181 */ 181 */
182 static inline __u16 get_reg_value(enum mos_regs reg, 182 static inline __u16 get_reg_value(enum mos_regs reg,
183 unsigned int serial_portnum) 183 unsigned int serial_portnum)
184 { 184 {
185 if (reg >= SP1_REG) /* control reg */ 185 if (reg >= SP1_REG) /* control reg */
186 return 0x0000; 186 return 0x0000;
187 187
188 else if (reg >= DPR) /* parallel port reg (7715 only) */ 188 else if (reg >= DPR) /* parallel port reg (7715 only) */
189 return 0x0100; 189 return 0x0100;
190 190
191 else /* serial port reg */ 191 else /* serial port reg */
192 return (serial_portnum + 2) << 8; 192 return (serial_portnum + 2) << 8;
193 } 193 }
194 194
195 /* 195 /*
196 * Write data byte to the specified device register. The data is embedded in 196 * Write data byte to the specified device register. The data is embedded in
197 * the value field of the setup packet. serial_portnum is ignored for registers 197 * the value field of the setup packet. serial_portnum is ignored for registers
198 * not specific to a particular serial port. 198 * not specific to a particular serial port.
199 */ 199 */
200 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum, 200 static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
201 enum mos_regs reg, __u8 data) 201 enum mos_regs reg, __u8 data)
202 { 202 {
203 struct usb_device *usbdev = serial->dev; 203 struct usb_device *usbdev = serial->dev;
204 unsigned int pipe = usb_sndctrlpipe(usbdev, 0); 204 unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
205 __u8 request = (__u8)0x0e; 205 __u8 request = (__u8)0x0e;
206 __u8 requesttype = (__u8)0x40; 206 __u8 requesttype = (__u8)0x40;
207 __u16 index = get_reg_index(reg); 207 __u16 index = get_reg_index(reg);
208 __u16 value = get_reg_value(reg, serial_portnum) + data; 208 __u16 value = get_reg_value(reg, serial_portnum) + data;
209 int status = usb_control_msg(usbdev, pipe, request, requesttype, value, 209 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
210 index, NULL, 0, MOS_WDR_TIMEOUT); 210 index, NULL, 0, MOS_WDR_TIMEOUT);
211 if (status < 0) 211 if (status < 0)
212 dev_err(&usbdev->dev, 212 dev_err(&usbdev->dev,
213 "mos7720: usb_control_msg() failed: %d", status); 213 "mos7720: usb_control_msg() failed: %d", status);
214 return status; 214 return status;
215 } 215 }
216 216
217 /* 217 /*
218 * Read data byte from the specified device register. The data returned by the 218 * Read data byte from the specified device register. The data returned by the
219 * device is embedded in the value field of the setup packet. serial_portnum is 219 * device is embedded in the value field of the setup packet. serial_portnum is
220 * ignored for registers that are not specific to a particular serial port. 220 * ignored for registers that are not specific to a particular serial port.
221 */ 221 */
222 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum, 222 static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
223 enum mos_regs reg, __u8 *data) 223 enum mos_regs reg, __u8 *data)
224 { 224 {
225 struct usb_device *usbdev = serial->dev; 225 struct usb_device *usbdev = serial->dev;
226 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0); 226 unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
227 __u8 request = (__u8)0x0d; 227 __u8 request = (__u8)0x0d;
228 __u8 requesttype = (__u8)0xc0; 228 __u8 requesttype = (__u8)0xc0;
229 __u16 index = get_reg_index(reg); 229 __u16 index = get_reg_index(reg);
230 __u16 value = get_reg_value(reg, serial_portnum); 230 __u16 value = get_reg_value(reg, serial_portnum);
231 int status = usb_control_msg(usbdev, pipe, request, requesttype, value, 231 int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
232 index, data, 1, MOS_WDR_TIMEOUT); 232 index, data, 1, MOS_WDR_TIMEOUT);
233 if (status < 0) 233 if (status < 0)
234 dev_err(&usbdev->dev, 234 dev_err(&usbdev->dev,
235 "mos7720: usb_control_msg() failed: %d", status); 235 "mos7720: usb_control_msg() failed: %d", status);
236 return status; 236 return status;
237 } 237 }
238 238
239 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT 239 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
240 240
241 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport, 241 static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
242 enum mos7715_pp_modes mode) 242 enum mos7715_pp_modes mode)
243 { 243 {
244 mos_parport->shadowECR = mode; 244 mos_parport->shadowECR = mode;
245 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR); 245 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
246 return 0; 246 return 0;
247 } 247 }
248 248
249 static void destroy_mos_parport(struct kref *kref) 249 static void destroy_mos_parport(struct kref *kref)
250 { 250 {
251 struct mos7715_parport *mos_parport = 251 struct mos7715_parport *mos_parport =
252 container_of(kref, struct mos7715_parport, ref_count); 252 container_of(kref, struct mos7715_parport, ref_count);
253 253
254 kfree(mos_parport); 254 kfree(mos_parport);
255 } 255 }
256 256
257 static void destroy_urbtracker(struct kref *kref) 257 static void destroy_urbtracker(struct kref *kref)
258 { 258 {
259 struct urbtracker *urbtrack = 259 struct urbtracker *urbtrack =
260 container_of(kref, struct urbtracker, ref_count); 260 container_of(kref, struct urbtracker, ref_count);
261 struct mos7715_parport *mos_parport = urbtrack->mos_parport; 261 struct mos7715_parport *mos_parport = urbtrack->mos_parport;
262 262
263 usb_free_urb(urbtrack->urb); 263 usb_free_urb(urbtrack->urb);
264 kfree(urbtrack); 264 kfree(urbtrack);
265 kref_put(&mos_parport->ref_count, destroy_mos_parport); 265 kref_put(&mos_parport->ref_count, destroy_mos_parport);
266 } 266 }
267 267
268 /* 268 /*
269 * This runs as a tasklet when sending an urb in a non-blocking parallel 269 * This runs as a tasklet when sending an urb in a non-blocking parallel
270 * port callback had to be deferred because the disconnect mutex could not be 270 * port callback had to be deferred because the disconnect mutex could not be
271 * obtained at the time. 271 * obtained at the time.
272 */ 272 */
273 static void send_deferred_urbs(unsigned long _mos_parport) 273 static void send_deferred_urbs(unsigned long _mos_parport)
274 { 274 {
275 int ret_val; 275 int ret_val;
276 unsigned long flags; 276 unsigned long flags;
277 struct mos7715_parport *mos_parport = (void *)_mos_parport; 277 struct mos7715_parport *mos_parport = (void *)_mos_parport;
278 struct urbtracker *urbtrack, *tmp; 278 struct urbtracker *urbtrack, *tmp;
279 struct list_head *cursor, *next; 279 struct list_head *cursor, *next;
280 struct device *dev; 280 struct device *dev;
281 281
282 /* if release function ran, game over */ 282 /* if release function ran, game over */
283 if (unlikely(mos_parport->serial == NULL)) 283 if (unlikely(mos_parport->serial == NULL))
284 return; 284 return;
285 285
286 dev = &mos_parport->serial->dev->dev; 286 dev = &mos_parport->serial->dev->dev;
287 287
288 /* try again to get the mutex */ 288 /* try again to get the mutex */
289 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) { 289 if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
290 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__); 290 dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
291 tasklet_schedule(&mos_parport->urb_tasklet); 291 tasklet_schedule(&mos_parport->urb_tasklet);
292 return; 292 return;
293 } 293 }
294 294
295 /* if device disconnected, game over */ 295 /* if device disconnected, game over */
296 if (unlikely(mos_parport->serial->disconnected)) { 296 if (unlikely(mos_parport->serial->disconnected)) {
297 mutex_unlock(&mos_parport->serial->disc_mutex); 297 mutex_unlock(&mos_parport->serial->disc_mutex);
298 return; 298 return;
299 } 299 }
300 300
301 spin_lock_irqsave(&mos_parport->listlock, flags); 301 spin_lock_irqsave(&mos_parport->listlock, flags);
302 if (list_empty(&mos_parport->deferred_urbs)) { 302 if (list_empty(&mos_parport->deferred_urbs)) {
303 spin_unlock_irqrestore(&mos_parport->listlock, flags); 303 spin_unlock_irqrestore(&mos_parport->listlock, flags);
304 mutex_unlock(&mos_parport->serial->disc_mutex); 304 mutex_unlock(&mos_parport->serial->disc_mutex);
305 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__); 305 dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
306 return; 306 return;
307 } 307 }
308 308
309 /* move contents of deferred_urbs list to active_urbs list and submit */ 309 /* move contents of deferred_urbs list to active_urbs list and submit */
310 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs) 310 list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
311 list_move_tail(cursor, &mos_parport->active_urbs); 311 list_move_tail(cursor, &mos_parport->active_urbs);
312 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs, 312 list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
313 urblist_entry) { 313 urblist_entry) {
314 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC); 314 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
315 dev_dbg(dev, "%s: urb submitted\n", __func__); 315 dev_dbg(dev, "%s: urb submitted\n", __func__);
316 if (ret_val) { 316 if (ret_val) {
317 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val); 317 dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
318 list_del(&urbtrack->urblist_entry); 318 list_del(&urbtrack->urblist_entry);
319 kref_put(&urbtrack->ref_count, destroy_urbtracker); 319 kref_put(&urbtrack->ref_count, destroy_urbtracker);
320 } 320 }
321 } 321 }
322 spin_unlock_irqrestore(&mos_parport->listlock, flags); 322 spin_unlock_irqrestore(&mos_parport->listlock, flags);
323 mutex_unlock(&mos_parport->serial->disc_mutex); 323 mutex_unlock(&mos_parport->serial->disc_mutex);
324 } 324 }
325 325
326 /* callback for parallel port control urbs submitted asynchronously */ 326 /* callback for parallel port control urbs submitted asynchronously */
327 static void async_complete(struct urb *urb) 327 static void async_complete(struct urb *urb)
328 { 328 {
329 struct urbtracker *urbtrack = urb->context; 329 struct urbtracker *urbtrack = urb->context;
330 int status = urb->status; 330 int status = urb->status;
331 331
332 if (unlikely(status)) 332 if (unlikely(status))
333 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status); 333 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
334 334
335 /* remove the urbtracker from the active_urbs list */ 335 /* remove the urbtracker from the active_urbs list */
336 spin_lock(&urbtrack->mos_parport->listlock); 336 spin_lock(&urbtrack->mos_parport->listlock);
337 list_del(&urbtrack->urblist_entry); 337 list_del(&urbtrack->urblist_entry);
338 spin_unlock(&urbtrack->mos_parport->listlock); 338 spin_unlock(&urbtrack->mos_parport->listlock);
339 kref_put(&urbtrack->ref_count, destroy_urbtracker); 339 kref_put(&urbtrack->ref_count, destroy_urbtracker);
340 } 340 }
341 341
342 static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport, 342 static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
343 enum mos_regs reg, __u8 data) 343 enum mos_regs reg, __u8 data)
344 { 344 {
345 struct urbtracker *urbtrack; 345 struct urbtracker *urbtrack;
346 int ret_val; 346 int ret_val;
347 unsigned long flags; 347 unsigned long flags;
348 struct usb_ctrlrequest setup; 348 struct usb_ctrlrequest setup;
349 struct usb_serial *serial = mos_parport->serial; 349 struct usb_serial *serial = mos_parport->serial;
350 struct usb_device *usbdev = serial->dev; 350 struct usb_device *usbdev = serial->dev;
351 351
352 /* create and initialize the control urb and containing urbtracker */ 352 /* create and initialize the control urb and containing urbtracker */
353 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC); 353 urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
354 if (urbtrack == NULL) { 354 if (urbtrack == NULL) {
355 dev_err(&usbdev->dev, "out of memory"); 355 dev_err(&usbdev->dev, "out of memory");
356 return -ENOMEM; 356 return -ENOMEM;
357 } 357 }
358 kref_get(&mos_parport->ref_count); 358 kref_get(&mos_parport->ref_count);
359 urbtrack->mos_parport = mos_parport; 359 urbtrack->mos_parport = mos_parport;
360 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC); 360 urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
361 if (urbtrack->urb == NULL) { 361 if (urbtrack->urb == NULL) {
362 dev_err(&usbdev->dev, "out of urbs"); 362 dev_err(&usbdev->dev, "out of urbs");
363 kfree(urbtrack); 363 kfree(urbtrack);
364 return -ENOMEM; 364 return -ENOMEM;
365 } 365 }
366 setup.bRequestType = (__u8)0x40; 366 setup.bRequestType = (__u8)0x40;
367 setup.bRequest = (__u8)0x0e; 367 setup.bRequest = (__u8)0x0e;
368 setup.wValue = get_reg_value(reg, dummy); 368 setup.wValue = get_reg_value(reg, dummy);
369 setup.wIndex = get_reg_index(reg); 369 setup.wIndex = get_reg_index(reg);
370 setup.wLength = 0; 370 setup.wLength = 0;
371 usb_fill_control_urb(urbtrack->urb, usbdev, 371 usb_fill_control_urb(urbtrack->urb, usbdev,
372 usb_sndctrlpipe(usbdev, 0), 372 usb_sndctrlpipe(usbdev, 0),
373 (unsigned char *)&setup, 373 (unsigned char *)&setup,
374 NULL, 0, async_complete, urbtrack); 374 NULL, 0, async_complete, urbtrack);
375 kref_init(&urbtrack->ref_count); 375 kref_init(&urbtrack->ref_count);
376 INIT_LIST_HEAD(&urbtrack->urblist_entry); 376 INIT_LIST_HEAD(&urbtrack->urblist_entry);
377 377
378 /* 378 /*
379 * get the disconnect mutex, or add tracker to the deferred_urbs list 379 * get the disconnect mutex, or add tracker to the deferred_urbs list
380 * and schedule a tasklet to try again later 380 * and schedule a tasklet to try again later
381 */ 381 */
382 if (!mutex_trylock(&serial->disc_mutex)) { 382 if (!mutex_trylock(&serial->disc_mutex)) {
383 spin_lock_irqsave(&mos_parport->listlock, flags); 383 spin_lock_irqsave(&mos_parport->listlock, flags);
384 list_add_tail(&urbtrack->urblist_entry, 384 list_add_tail(&urbtrack->urblist_entry,
385 &mos_parport->deferred_urbs); 385 &mos_parport->deferred_urbs);
386 spin_unlock_irqrestore(&mos_parport->listlock, flags); 386 spin_unlock_irqrestore(&mos_parport->listlock, flags);
387 tasklet_schedule(&mos_parport->urb_tasklet); 387 tasklet_schedule(&mos_parport->urb_tasklet);
388 dev_dbg(&usbdev->dev, "tasklet scheduled"); 388 dev_dbg(&usbdev->dev, "tasklet scheduled");
389 return 0; 389 return 0;
390 } 390 }
391 391
392 /* bail if device disconnected */ 392 /* bail if device disconnected */
393 if (serial->disconnected) { 393 if (serial->disconnected) {
394 kref_put(&urbtrack->ref_count, destroy_urbtracker); 394 kref_put(&urbtrack->ref_count, destroy_urbtracker);
395 mutex_unlock(&serial->disc_mutex); 395 mutex_unlock(&serial->disc_mutex);
396 return -ENODEV; 396 return -ENODEV;
397 } 397 }
398 398
399 /* add the tracker to the active_urbs list and submit */ 399 /* add the tracker to the active_urbs list and submit */
400 spin_lock_irqsave(&mos_parport->listlock, flags); 400 spin_lock_irqsave(&mos_parport->listlock, flags);
401 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs); 401 list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
402 spin_unlock_irqrestore(&mos_parport->listlock, flags); 402 spin_unlock_irqrestore(&mos_parport->listlock, flags);
403 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC); 403 ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
404 mutex_unlock(&serial->disc_mutex); 404 mutex_unlock(&serial->disc_mutex);
405 if (ret_val) { 405 if (ret_val) {
406 dev_err(&usbdev->dev, 406 dev_err(&usbdev->dev,
407 "%s: submit_urb() failed: %d", __func__, ret_val); 407 "%s: submit_urb() failed: %d", __func__, ret_val);
408 spin_lock_irqsave(&mos_parport->listlock, flags); 408 spin_lock_irqsave(&mos_parport->listlock, flags);
409 list_del(&urbtrack->urblist_entry); 409 list_del(&urbtrack->urblist_entry);
410 spin_unlock_irqrestore(&mos_parport->listlock, flags); 410 spin_unlock_irqrestore(&mos_parport->listlock, flags);
411 kref_put(&urbtrack->ref_count, destroy_urbtracker); 411 kref_put(&urbtrack->ref_count, destroy_urbtracker);
412 return ret_val; 412 return ret_val;
413 } 413 }
414 return 0; 414 return 0;
415 } 415 }
416 416
417 /* 417 /*
418 * This is the the common top part of all parallel port callback operations that 418 * This is the the common top part of all parallel port callback operations that
419 * send synchronous messages to the device. This implements convoluted locking 419 * send synchronous messages to the device. This implements convoluted locking
420 * that avoids two scenarios: (1) a port operation is called after usbserial 420 * that avoids two scenarios: (1) a port operation is called after usbserial
421 * has called our release function, at which point struct mos7715_parport has 421 * has called our release function, at which point struct mos7715_parport has
422 * been destroyed, and (2) the device has been disconnected, but usbserial has 422 * been destroyed, and (2) the device has been disconnected, but usbserial has
423 * not called the release function yet because someone has a serial port open. 423 * not called the release function yet because someone has a serial port open.
424 * The shared release_lock prevents the first, and the mutex and disconnected 424 * The shared release_lock prevents the first, and the mutex and disconnected
425 * flag maintained by usbserial covers the second. We also use the msg_pending 425 * flag maintained by usbserial covers the second. We also use the msg_pending
426 * flag to ensure that all synchronous usb messgage calls have completed before 426 * flag to ensure that all synchronous usb messgage calls have completed before
427 * our release function can return. 427 * our release function can return.
428 */ 428 */
429 static int parport_prologue(struct parport *pp) 429 static int parport_prologue(struct parport *pp)
430 { 430 {
431 struct mos7715_parport *mos_parport; 431 struct mos7715_parport *mos_parport;
432 432
433 spin_lock(&release_lock); 433 spin_lock(&release_lock);
434 mos_parport = pp->private_data; 434 mos_parport = pp->private_data;
435 if (unlikely(mos_parport == NULL)) { 435 if (unlikely(mos_parport == NULL)) {
436 /* release fn called, port struct destroyed */ 436 /* release fn called, port struct destroyed */
437 spin_unlock(&release_lock); 437 spin_unlock(&release_lock);
438 return -1; 438 return -1;
439 } 439 }
440 mos_parport->msg_pending = true; /* synch usb call pending */ 440 mos_parport->msg_pending = true; /* synch usb call pending */
441 INIT_COMPLETION(mos_parport->syncmsg_compl); 441 INIT_COMPLETION(mos_parport->syncmsg_compl);
442 spin_unlock(&release_lock); 442 spin_unlock(&release_lock);
443 443
444 mutex_lock(&mos_parport->serial->disc_mutex); 444 mutex_lock(&mos_parport->serial->disc_mutex);
445 if (mos_parport->serial->disconnected) { 445 if (mos_parport->serial->disconnected) {
446 /* device disconnected */ 446 /* device disconnected */
447 mutex_unlock(&mos_parport->serial->disc_mutex); 447 mutex_unlock(&mos_parport->serial->disc_mutex);
448 mos_parport->msg_pending = false; 448 mos_parport->msg_pending = false;
449 complete(&mos_parport->syncmsg_compl); 449 complete(&mos_parport->syncmsg_compl);
450 return -1; 450 return -1;
451 } 451 }
452 452
453 return 0; 453 return 0;
454 } 454 }
455 455
456 /* 456 /*
457 * This is the the common bottom part of all parallel port functions that send 457 * This is the the common bottom part of all parallel port functions that send
458 * synchronous messages to the device. 458 * synchronous messages to the device.
459 */ 459 */
460 static inline void parport_epilogue(struct parport *pp) 460 static inline void parport_epilogue(struct parport *pp)
461 { 461 {
462 struct mos7715_parport *mos_parport = pp->private_data; 462 struct mos7715_parport *mos_parport = pp->private_data;
463 mutex_unlock(&mos_parport->serial->disc_mutex); 463 mutex_unlock(&mos_parport->serial->disc_mutex);
464 mos_parport->msg_pending = false; 464 mos_parport->msg_pending = false;
465 complete(&mos_parport->syncmsg_compl); 465 complete(&mos_parport->syncmsg_compl);
466 } 466 }
467 467
468 static void parport_mos7715_write_data(struct parport *pp, unsigned char d) 468 static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
469 { 469 {
470 struct mos7715_parport *mos_parport = pp->private_data; 470 struct mos7715_parport *mos_parport = pp->private_data;
471 471
472 if (parport_prologue(pp) < 0) 472 if (parport_prologue(pp) < 0)
473 return; 473 return;
474 mos7715_change_mode(mos_parport, SPP); 474 mos7715_change_mode(mos_parport, SPP);
475 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d); 475 write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
476 parport_epilogue(pp); 476 parport_epilogue(pp);
477 } 477 }
478 478
479 static unsigned char parport_mos7715_read_data(struct parport *pp) 479 static unsigned char parport_mos7715_read_data(struct parport *pp)
480 { 480 {
481 struct mos7715_parport *mos_parport = pp->private_data; 481 struct mos7715_parport *mos_parport = pp->private_data;
482 unsigned char d; 482 unsigned char d;
483 483
484 if (parport_prologue(pp) < 0) 484 if (parport_prologue(pp) < 0)
485 return 0; 485 return 0;
486 read_mos_reg(mos_parport->serial, dummy, DPR, &d); 486 read_mos_reg(mos_parport->serial, dummy, DPR, &d);
487 parport_epilogue(pp); 487 parport_epilogue(pp);
488 return d; 488 return d;
489 } 489 }
490 490
491 static void parport_mos7715_write_control(struct parport *pp, unsigned char d) 491 static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
492 { 492 {
493 struct mos7715_parport *mos_parport = pp->private_data; 493 struct mos7715_parport *mos_parport = pp->private_data;
494 __u8 data; 494 __u8 data;
495 495
496 if (parport_prologue(pp) < 0) 496 if (parport_prologue(pp) < 0)
497 return; 497 return;
498 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0); 498 data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
499 write_mos_reg(mos_parport->serial, dummy, DCR, data); 499 write_mos_reg(mos_parport->serial, dummy, DCR, data);
500 mos_parport->shadowDCR = data; 500 mos_parport->shadowDCR = data;
501 parport_epilogue(pp); 501 parport_epilogue(pp);
502 } 502 }
503 503
504 static unsigned char parport_mos7715_read_control(struct parport *pp) 504 static unsigned char parport_mos7715_read_control(struct parport *pp)
505 { 505 {
506 struct mos7715_parport *mos_parport = pp->private_data; 506 struct mos7715_parport *mos_parport = pp->private_data;
507 __u8 dcr; 507 __u8 dcr;
508 508
509 spin_lock(&release_lock); 509 spin_lock(&release_lock);
510 mos_parport = pp->private_data; 510 mos_parport = pp->private_data;
511 if (unlikely(mos_parport == NULL)) { 511 if (unlikely(mos_parport == NULL)) {
512 spin_unlock(&release_lock); 512 spin_unlock(&release_lock);
513 return 0; 513 return 0;
514 } 514 }
515 dcr = mos_parport->shadowDCR & 0x0f; 515 dcr = mos_parport->shadowDCR & 0x0f;
516 spin_unlock(&release_lock); 516 spin_unlock(&release_lock);
517 return dcr; 517 return dcr;
518 } 518 }
519 519
520 static unsigned char parport_mos7715_frob_control(struct parport *pp, 520 static unsigned char parport_mos7715_frob_control(struct parport *pp,
521 unsigned char mask, 521 unsigned char mask,
522 unsigned char val) 522 unsigned char val)
523 { 523 {
524 struct mos7715_parport *mos_parport = pp->private_data; 524 struct mos7715_parport *mos_parport = pp->private_data;
525 __u8 dcr; 525 __u8 dcr;
526 526
527 mask &= 0x0f; 527 mask &= 0x0f;
528 val &= 0x0f; 528 val &= 0x0f;
529 if (parport_prologue(pp) < 0) 529 if (parport_prologue(pp) < 0)
530 return 0; 530 return 0;
531 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val; 531 mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
532 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR); 532 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
533 dcr = mos_parport->shadowDCR & 0x0f; 533 dcr = mos_parport->shadowDCR & 0x0f;
534 parport_epilogue(pp); 534 parport_epilogue(pp);
535 return dcr; 535 return dcr;
536 } 536 }
537 537
538 static unsigned char parport_mos7715_read_status(struct parport *pp) 538 static unsigned char parport_mos7715_read_status(struct parport *pp)
539 { 539 {
540 unsigned char status; 540 unsigned char status;
541 struct mos7715_parport *mos_parport = pp->private_data; 541 struct mos7715_parport *mos_parport = pp->private_data;
542 542
543 spin_lock(&release_lock); 543 spin_lock(&release_lock);
544 mos_parport = pp->private_data; 544 mos_parport = pp->private_data;
545 if (unlikely(mos_parport == NULL)) { /* release called */ 545 if (unlikely(mos_parport == NULL)) { /* release called */
546 spin_unlock(&release_lock); 546 spin_unlock(&release_lock);
547 return 0; 547 return 0;
548 } 548 }
549 status = atomic_read(&mos_parport->shadowDSR) & 0xf8; 549 status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
550 spin_unlock(&release_lock); 550 spin_unlock(&release_lock);
551 return status; 551 return status;
552 } 552 }
553 553
554 static void parport_mos7715_enable_irq(struct parport *pp) 554 static void parport_mos7715_enable_irq(struct parport *pp)
555 { 555 {
556 } 556 }
557 557
558 static void parport_mos7715_disable_irq(struct parport *pp) 558 static void parport_mos7715_disable_irq(struct parport *pp)
559 { 559 {
560 } 560 }
561 561
562 static void parport_mos7715_data_forward(struct parport *pp) 562 static void parport_mos7715_data_forward(struct parport *pp)
563 { 563 {
564 struct mos7715_parport *mos_parport = pp->private_data; 564 struct mos7715_parport *mos_parport = pp->private_data;
565 565
566 if (parport_prologue(pp) < 0) 566 if (parport_prologue(pp) < 0)
567 return; 567 return;
568 mos7715_change_mode(mos_parport, PS2); 568 mos7715_change_mode(mos_parport, PS2);
569 mos_parport->shadowDCR &= ~0x20; 569 mos_parport->shadowDCR &= ~0x20;
570 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR); 570 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
571 parport_epilogue(pp); 571 parport_epilogue(pp);
572 } 572 }
573 573
574 static void parport_mos7715_data_reverse(struct parport *pp) 574 static void parport_mos7715_data_reverse(struct parport *pp)
575 { 575 {
576 struct mos7715_parport *mos_parport = pp->private_data; 576 struct mos7715_parport *mos_parport = pp->private_data;
577 577
578 if (parport_prologue(pp) < 0) 578 if (parport_prologue(pp) < 0)
579 return; 579 return;
580 mos7715_change_mode(mos_parport, PS2); 580 mos7715_change_mode(mos_parport, PS2);
581 mos_parport->shadowDCR |= 0x20; 581 mos_parport->shadowDCR |= 0x20;
582 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR); 582 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
583 parport_epilogue(pp); 583 parport_epilogue(pp);
584 } 584 }
585 585
586 static void parport_mos7715_init_state(struct pardevice *dev, 586 static void parport_mos7715_init_state(struct pardevice *dev,
587 struct parport_state *s) 587 struct parport_state *s)
588 { 588 {
589 s->u.pc.ctr = DCR_INIT_VAL; 589 s->u.pc.ctr = DCR_INIT_VAL;
590 s->u.pc.ecr = ECR_INIT_VAL; 590 s->u.pc.ecr = ECR_INIT_VAL;
591 } 591 }
592 592
593 /* N.B. Parport core code requires that this function not block */ 593 /* N.B. Parport core code requires that this function not block */
594 static void parport_mos7715_save_state(struct parport *pp, 594 static void parport_mos7715_save_state(struct parport *pp,
595 struct parport_state *s) 595 struct parport_state *s)
596 { 596 {
597 struct mos7715_parport *mos_parport; 597 struct mos7715_parport *mos_parport;
598 598
599 spin_lock(&release_lock); 599 spin_lock(&release_lock);
600 mos_parport = pp->private_data; 600 mos_parport = pp->private_data;
601 if (unlikely(mos_parport == NULL)) { /* release called */ 601 if (unlikely(mos_parport == NULL)) { /* release called */
602 spin_unlock(&release_lock); 602 spin_unlock(&release_lock);
603 return; 603 return;
604 } 604 }
605 s->u.pc.ctr = mos_parport->shadowDCR; 605 s->u.pc.ctr = mos_parport->shadowDCR;
606 s->u.pc.ecr = mos_parport->shadowECR; 606 s->u.pc.ecr = mos_parport->shadowECR;
607 spin_unlock(&release_lock); 607 spin_unlock(&release_lock);
608 } 608 }
609 609
610 /* N.B. Parport core code requires that this function not block */ 610 /* N.B. Parport core code requires that this function not block */
611 static void parport_mos7715_restore_state(struct parport *pp, 611 static void parport_mos7715_restore_state(struct parport *pp,
612 struct parport_state *s) 612 struct parport_state *s)
613 { 613 {
614 struct mos7715_parport *mos_parport; 614 struct mos7715_parport *mos_parport;
615 615
616 spin_lock(&release_lock); 616 spin_lock(&release_lock);
617 mos_parport = pp->private_data; 617 mos_parport = pp->private_data;
618 if (unlikely(mos_parport == NULL)) { /* release called */ 618 if (unlikely(mos_parport == NULL)) { /* release called */
619 spin_unlock(&release_lock); 619 spin_unlock(&release_lock);
620 return; 620 return;
621 } 621 }
622 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR); 622 write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
623 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR); 623 write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
624 spin_unlock(&release_lock); 624 spin_unlock(&release_lock);
625 } 625 }
626 626
627 static size_t parport_mos7715_write_compat(struct parport *pp, 627 static size_t parport_mos7715_write_compat(struct parport *pp,
628 const void *buffer, 628 const void *buffer,
629 size_t len, int flags) 629 size_t len, int flags)
630 { 630 {
631 int retval; 631 int retval;
632 struct mos7715_parport *mos_parport = pp->private_data; 632 struct mos7715_parport *mos_parport = pp->private_data;
633 int actual_len; 633 int actual_len;
634 634
635 if (parport_prologue(pp) < 0) 635 if (parport_prologue(pp) < 0)
636 return 0; 636 return 0;
637 mos7715_change_mode(mos_parport, PPF); 637 mos7715_change_mode(mos_parport, PPF);
638 retval = usb_bulk_msg(mos_parport->serial->dev, 638 retval = usb_bulk_msg(mos_parport->serial->dev,
639 usb_sndbulkpipe(mos_parport->serial->dev, 2), 639 usb_sndbulkpipe(mos_parport->serial->dev, 2),
640 (void *)buffer, len, &actual_len, 640 (void *)buffer, len, &actual_len,
641 MOS_WDR_TIMEOUT); 641 MOS_WDR_TIMEOUT);
642 parport_epilogue(pp); 642 parport_epilogue(pp);
643 if (retval) { 643 if (retval) {
644 dev_err(&mos_parport->serial->dev->dev, 644 dev_err(&mos_parport->serial->dev->dev,
645 "mos7720: usb_bulk_msg() failed: %d", retval); 645 "mos7720: usb_bulk_msg() failed: %d", retval);
646 return 0; 646 return 0;
647 } 647 }
648 return actual_len; 648 return actual_len;
649 } 649 }
650 650
651 static struct parport_operations parport_mos7715_ops = { 651 static struct parport_operations parport_mos7715_ops = {
652 .owner = THIS_MODULE, 652 .owner = THIS_MODULE,
653 .write_data = parport_mos7715_write_data, 653 .write_data = parport_mos7715_write_data,
654 .read_data = parport_mos7715_read_data, 654 .read_data = parport_mos7715_read_data,
655 655
656 .write_control = parport_mos7715_write_control, 656 .write_control = parport_mos7715_write_control,
657 .read_control = parport_mos7715_read_control, 657 .read_control = parport_mos7715_read_control,
658 .frob_control = parport_mos7715_frob_control, 658 .frob_control = parport_mos7715_frob_control,
659 659
660 .read_status = parport_mos7715_read_status, 660 .read_status = parport_mos7715_read_status,
661 661
662 .enable_irq = parport_mos7715_enable_irq, 662 .enable_irq = parport_mos7715_enable_irq,
663 .disable_irq = parport_mos7715_disable_irq, 663 .disable_irq = parport_mos7715_disable_irq,
664 664
665 .data_forward = parport_mos7715_data_forward, 665 .data_forward = parport_mos7715_data_forward,
666 .data_reverse = parport_mos7715_data_reverse, 666 .data_reverse = parport_mos7715_data_reverse,
667 667
668 .init_state = parport_mos7715_init_state, 668 .init_state = parport_mos7715_init_state,
669 .save_state = parport_mos7715_save_state, 669 .save_state = parport_mos7715_save_state,
670 .restore_state = parport_mos7715_restore_state, 670 .restore_state = parport_mos7715_restore_state,
671 671
672 .compat_write_data = parport_mos7715_write_compat, 672 .compat_write_data = parport_mos7715_write_compat,
673 673
674 .nibble_read_data = parport_ieee1284_read_nibble, 674 .nibble_read_data = parport_ieee1284_read_nibble,
675 .byte_read_data = parport_ieee1284_read_byte, 675 .byte_read_data = parport_ieee1284_read_byte,
676 }; 676 };
677 677
678 /* 678 /*
679 * Allocate and initialize parallel port control struct, initialize 679 * Allocate and initialize parallel port control struct, initialize
680 * the parallel port hardware device, and register with the parport subsystem. 680 * the parallel port hardware device, and register with the parport subsystem.
681 */ 681 */
682 static int mos7715_parport_init(struct usb_serial *serial) 682 static int mos7715_parport_init(struct usb_serial *serial)
683 { 683 {
684 struct mos7715_parport *mos_parport; 684 struct mos7715_parport *mos_parport;
685 685
686 /* allocate and initialize parallel port control struct */ 686 /* allocate and initialize parallel port control struct */
687 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL); 687 mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
688 if (mos_parport == NULL) { 688 if (mos_parport == NULL) {
689 dev_dbg(&serial->dev->dev, "%s: kzalloc failed\n", __func__); 689 dev_dbg(&serial->dev->dev, "%s: kzalloc failed\n", __func__);
690 return -ENOMEM; 690 return -ENOMEM;
691 } 691 }
692 mos_parport->msg_pending = false; 692 mos_parport->msg_pending = false;
693 kref_init(&mos_parport->ref_count); 693 kref_init(&mos_parport->ref_count);
694 spin_lock_init(&mos_parport->listlock); 694 spin_lock_init(&mos_parport->listlock);
695 INIT_LIST_HEAD(&mos_parport->active_urbs); 695 INIT_LIST_HEAD(&mos_parport->active_urbs);
696 INIT_LIST_HEAD(&mos_parport->deferred_urbs); 696 INIT_LIST_HEAD(&mos_parport->deferred_urbs);
697 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */ 697 usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
698 mos_parport->serial = serial; 698 mos_parport->serial = serial;
699 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs, 699 tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
700 (unsigned long) mos_parport); 700 (unsigned long) mos_parport);
701 init_completion(&mos_parport->syncmsg_compl); 701 init_completion(&mos_parport->syncmsg_compl);
702 702
703 /* cycle parallel port reset bit */ 703 /* cycle parallel port reset bit */
704 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80); 704 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
705 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00); 705 write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
706 706
707 /* initialize device registers */ 707 /* initialize device registers */
708 mos_parport->shadowDCR = DCR_INIT_VAL; 708 mos_parport->shadowDCR = DCR_INIT_VAL;
709 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR); 709 write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
710 mos_parport->shadowECR = ECR_INIT_VAL; 710 mos_parport->shadowECR = ECR_INIT_VAL;
711 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR); 711 write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
712 712
713 /* register with parport core */ 713 /* register with parport core */
714 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE, 714 mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
715 PARPORT_DMA_NONE, 715 PARPORT_DMA_NONE,
716 &parport_mos7715_ops); 716 &parport_mos7715_ops);
717 if (mos_parport->pp == NULL) { 717 if (mos_parport->pp == NULL) {
718 dev_err(&serial->interface->dev, 718 dev_err(&serial->interface->dev,
719 "Could not register parport\n"); 719 "Could not register parport\n");
720 kref_put(&mos_parport->ref_count, destroy_mos_parport); 720 kref_put(&mos_parport->ref_count, destroy_mos_parport);
721 return -EIO; 721 return -EIO;
722 } 722 }
723 mos_parport->pp->private_data = mos_parport; 723 mos_parport->pp->private_data = mos_parport;
724 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP; 724 mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
725 mos_parport->pp->dev = &serial->interface->dev; 725 mos_parport->pp->dev = &serial->interface->dev;
726 parport_announce_port(mos_parport->pp); 726 parport_announce_port(mos_parport->pp);
727 727
728 return 0; 728 return 0;
729 } 729 }
730 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */ 730 #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
731 731
732 /* 732 /*
733 * mos7720_interrupt_callback 733 * mos7720_interrupt_callback
734 * this is the callback function for when we have received data on the 734 * this is the callback function for when we have received data on the
735 * interrupt endpoint. 735 * interrupt endpoint.
736 */ 736 */
737 static void mos7720_interrupt_callback(struct urb *urb) 737 static void mos7720_interrupt_callback(struct urb *urb)
738 { 738 {
739 int result; 739 int result;
740 int length; 740 int length;
741 int status = urb->status; 741 int status = urb->status;
742 struct device *dev = &urb->dev->dev; 742 struct device *dev = &urb->dev->dev;
743 __u8 *data; 743 __u8 *data;
744 __u8 sp1; 744 __u8 sp1;
745 __u8 sp2; 745 __u8 sp2;
746 746
747 switch (status) { 747 switch (status) {
748 case 0: 748 case 0:
749 /* success */ 749 /* success */
750 break; 750 break;
751 case -ECONNRESET: 751 case -ECONNRESET:
752 case -ENOENT: 752 case -ENOENT:
753 case -ESHUTDOWN: 753 case -ESHUTDOWN:
754 /* this urb is terminated, clean up */ 754 /* this urb is terminated, clean up */
755 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status); 755 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
756 return; 756 return;
757 default: 757 default:
758 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status); 758 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
759 goto exit; 759 goto exit;
760 } 760 }
761 761
762 length = urb->actual_length; 762 length = urb->actual_length;
763 data = urb->transfer_buffer; 763 data = urb->transfer_buffer;
764 764
765 /* Moschip get 4 bytes 765 /* Moschip get 4 bytes
766 * Byte 1 IIR Port 1 (port.number is 0) 766 * Byte 1 IIR Port 1 (port.number is 0)
767 * Byte 2 IIR Port 2 (port.number is 1) 767 * Byte 2 IIR Port 2 (port.number is 1)
768 * Byte 3 -------------- 768 * Byte 3 --------------
769 * Byte 4 FIFO status for both */ 769 * Byte 4 FIFO status for both */
770 770
771 /* the above description is inverted 771 /* the above description is inverted
772 * oneukum 2007-03-14 */ 772 * oneukum 2007-03-14 */
773 773
774 if (unlikely(length != 4)) { 774 if (unlikely(length != 4)) {
775 dev_dbg(dev, "Wrong data !!!\n"); 775 dev_dbg(dev, "Wrong data !!!\n");
776 return; 776 return;
777 } 777 }
778 778
779 sp1 = data[3]; 779 sp1 = data[3];
780 sp2 = data[2]; 780 sp2 = data[2];
781 781
782 if ((sp1 | sp2) & 0x01) { 782 if ((sp1 | sp2) & 0x01) {
783 /* No Interrupt Pending in both the ports */ 783 /* No Interrupt Pending in both the ports */
784 dev_dbg(dev, "No Interrupt !!!\n"); 784 dev_dbg(dev, "No Interrupt !!!\n");
785 } else { 785 } else {
786 switch (sp1 & 0x0f) { 786 switch (sp1 & 0x0f) {
787 case SERIAL_IIR_RLS: 787 case SERIAL_IIR_RLS:
788 dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n"); 788 dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
789 break; 789 break;
790 case SERIAL_IIR_CTI: 790 case SERIAL_IIR_CTI:
791 dev_dbg(dev, "Serial Port 1: Receiver time out\n"); 791 dev_dbg(dev, "Serial Port 1: Receiver time out\n");
792 break; 792 break;
793 case SERIAL_IIR_MS: 793 case SERIAL_IIR_MS:
794 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */ 794 /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
795 break; 795 break;
796 } 796 }
797 797
798 switch (sp2 & 0x0f) { 798 switch (sp2 & 0x0f) {
799 case SERIAL_IIR_RLS: 799 case SERIAL_IIR_RLS:
800 dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n"); 800 dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
801 break; 801 break;
802 case SERIAL_IIR_CTI: 802 case SERIAL_IIR_CTI:
803 dev_dbg(dev, "Serial Port 2: Receiver time out\n"); 803 dev_dbg(dev, "Serial Port 2: Receiver time out\n");
804 break; 804 break;
805 case SERIAL_IIR_MS: 805 case SERIAL_IIR_MS:
806 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */ 806 /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
807 break; 807 break;
808 } 808 }
809 } 809 }
810 810
811 exit: 811 exit:
812 result = usb_submit_urb(urb, GFP_ATOMIC); 812 result = usb_submit_urb(urb, GFP_ATOMIC);
813 if (result) 813 if (result)
814 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result); 814 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
815 } 815 }
816 816
817 /* 817 /*
818 * mos7715_interrupt_callback 818 * mos7715_interrupt_callback
819 * this is the 7715's callback function for when we have received data on 819 * this is the 7715's callback function for when we have received data on
820 * the interrupt endpoint. 820 * the interrupt endpoint.
821 */ 821 */
822 static void mos7715_interrupt_callback(struct urb *urb) 822 static void mos7715_interrupt_callback(struct urb *urb)
823 { 823 {
824 int result; 824 int result;
825 int length; 825 int length;
826 int status = urb->status; 826 int status = urb->status;
827 struct device *dev = &urb->dev->dev; 827 struct device *dev = &urb->dev->dev;
828 __u8 *data; 828 __u8 *data;
829 __u8 iir; 829 __u8 iir;
830 830
831 switch (status) { 831 switch (status) {
832 case 0: 832 case 0:
833 /* success */ 833 /* success */
834 break; 834 break;
835 case -ECONNRESET: 835 case -ECONNRESET:
836 case -ENOENT: 836 case -ENOENT:
837 case -ESHUTDOWN: 837 case -ESHUTDOWN:
838 case -ENODEV: 838 case -ENODEV:
839 /* this urb is terminated, clean up */ 839 /* this urb is terminated, clean up */
840 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status); 840 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
841 return; 841 return;
842 default: 842 default:
843 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status); 843 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
844 goto exit; 844 goto exit;
845 } 845 }
846 846
847 length = urb->actual_length; 847 length = urb->actual_length;
848 data = urb->transfer_buffer; 848 data = urb->transfer_buffer;
849 849
850 /* Structure of data from 7715 device: 850 /* Structure of data from 7715 device:
851 * Byte 1: IIR serial Port 851 * Byte 1: IIR serial Port
852 * Byte 2: unused 852 * Byte 2: unused
853 * Byte 2: DSR parallel port 853 * Byte 2: DSR parallel port
854 * Byte 4: FIFO status for both */ 854 * Byte 4: FIFO status for both */
855 855
856 if (unlikely(length != 4)) { 856 if (unlikely(length != 4)) {
857 dev_dbg(dev, "Wrong data !!!\n"); 857 dev_dbg(dev, "Wrong data !!!\n");
858 return; 858 return;
859 } 859 }
860 860
861 iir = data[0]; 861 iir = data[0];
862 if (!(iir & 0x01)) { /* serial port interrupt pending */ 862 if (!(iir & 0x01)) { /* serial port interrupt pending */
863 switch (iir & 0x0f) { 863 switch (iir & 0x0f) {
864 case SERIAL_IIR_RLS: 864 case SERIAL_IIR_RLS:
865 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n\n"); 865 dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n\n");
866 break; 866 break;
867 case SERIAL_IIR_CTI: 867 case SERIAL_IIR_CTI:
868 dev_dbg(dev, "Serial Port: Receiver time out\n"); 868 dev_dbg(dev, "Serial Port: Receiver time out\n");
869 break; 869 break;
870 case SERIAL_IIR_MS: 870 case SERIAL_IIR_MS:
871 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */ 871 /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
872 break; 872 break;
873 } 873 }
874 } 874 }
875 875
876 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT 876 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
877 { /* update local copy of DSR reg */ 877 { /* update local copy of DSR reg */
878 struct usb_serial_port *port = urb->context; 878 struct usb_serial_port *port = urb->context;
879 struct mos7715_parport *mos_parport = port->serial->private; 879 struct mos7715_parport *mos_parport = port->serial->private;
880 if (unlikely(mos_parport == NULL)) 880 if (unlikely(mos_parport == NULL))
881 return; 881 return;
882 atomic_set(&mos_parport->shadowDSR, data[2]); 882 atomic_set(&mos_parport->shadowDSR, data[2]);
883 } 883 }
884 #endif 884 #endif
885 885
886 exit: 886 exit:
887 result = usb_submit_urb(urb, GFP_ATOMIC); 887 result = usb_submit_urb(urb, GFP_ATOMIC);
888 if (result) 888 if (result)
889 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result); 889 dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
890 } 890 }
891 891
892 /* 892 /*
893 * mos7720_bulk_in_callback 893 * mos7720_bulk_in_callback
894 * this is the callback function for when we have received data on the 894 * this is the callback function for when we have received data on the
895 * bulk in endpoint. 895 * bulk in endpoint.
896 */ 896 */
897 static void mos7720_bulk_in_callback(struct urb *urb) 897 static void mos7720_bulk_in_callback(struct urb *urb)
898 { 898 {
899 int retval; 899 int retval;
900 unsigned char *data ; 900 unsigned char *data ;
901 struct usb_serial_port *port; 901 struct usb_serial_port *port;
902 int status = urb->status; 902 int status = urb->status;
903 903
904 if (status) { 904 if (status) {
905 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status); 905 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
906 return; 906 return;
907 } 907 }
908 908
909 port = urb->context; 909 port = urb->context;
910 910
911 dev_dbg(&port->dev, "Entering...%s\n", __func__); 911 dev_dbg(&port->dev, "Entering...%s\n", __func__);
912 912
913 data = urb->transfer_buffer; 913 data = urb->transfer_buffer;
914 914
915 if (urb->actual_length) { 915 if (urb->actual_length) {
916 tty_insert_flip_string(&port->port, data, urb->actual_length); 916 tty_insert_flip_string(&port->port, data, urb->actual_length);
917 tty_flip_buffer_push(&port->port); 917 tty_flip_buffer_push(&port->port);
918 } 918 }
919 919
920 if (port->read_urb->status != -EINPROGRESS) { 920 if (port->read_urb->status != -EINPROGRESS) {
921 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC); 921 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
922 if (retval) 922 if (retval)
923 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval); 923 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
924 } 924 }
925 } 925 }
926 926
927 /* 927 /*
928 * mos7720_bulk_out_data_callback 928 * mos7720_bulk_out_data_callback
929 * this is the callback function for when we have finished sending serial 929 * this is the callback function for when we have finished sending serial
930 * data on the bulk out endpoint. 930 * data on the bulk out endpoint.
931 */ 931 */
932 static void mos7720_bulk_out_data_callback(struct urb *urb) 932 static void mos7720_bulk_out_data_callback(struct urb *urb)
933 { 933 {
934 struct moschip_port *mos7720_port; 934 struct moschip_port *mos7720_port;
935 struct tty_struct *tty;
936 int status = urb->status; 935 int status = urb->status;
937 936
938 if (status) { 937 if (status) {
939 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status); 938 dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
940 return; 939 return;
941 } 940 }
942 941
943 mos7720_port = urb->context; 942 mos7720_port = urb->context;
944 if (!mos7720_port) { 943 if (!mos7720_port) {
945 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n"); 944 dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
946 return ; 945 return ;
947 } 946 }
948 947
949 tty = tty_port_tty_get(&mos7720_port->port->port); 948 if (mos7720_port->open)
950 949 tty_port_tty_wakeup(&mos7720_port->port->port);
951 if (tty && mos7720_port->open)
952 tty_wakeup(tty);
953 tty_kref_put(tty);
954 } 950 }
955 951
956 /* 952 /*
957 * mos77xx_probe 953 * mos77xx_probe
958 * this function installs the appropriate read interrupt endpoint callback 954 * this function installs the appropriate read interrupt endpoint callback
959 * depending on whether the device is a 7720 or 7715, thus avoiding costly 955 * depending on whether the device is a 7720 or 7715, thus avoiding costly
960 * run-time checks in the high-frequency callback routine itself. 956 * run-time checks in the high-frequency callback routine itself.
961 */ 957 */
962 static int mos77xx_probe(struct usb_serial *serial, 958 static int mos77xx_probe(struct usb_serial *serial,
963 const struct usb_device_id *id) 959 const struct usb_device_id *id)
964 { 960 {
965 if (id->idProduct == MOSCHIP_DEVICE_ID_7715) 961 if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
966 moschip7720_2port_driver.read_int_callback = 962 moschip7720_2port_driver.read_int_callback =
967 mos7715_interrupt_callback; 963 mos7715_interrupt_callback;
968 else 964 else
969 moschip7720_2port_driver.read_int_callback = 965 moschip7720_2port_driver.read_int_callback =
970 mos7720_interrupt_callback; 966 mos7720_interrupt_callback;
971 967
972 return 0; 968 return 0;
973 } 969 }
974 970
975 static int mos77xx_calc_num_ports(struct usb_serial *serial) 971 static int mos77xx_calc_num_ports(struct usb_serial *serial)
976 { 972 {
977 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct); 973 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
978 if (product == MOSCHIP_DEVICE_ID_7715) 974 if (product == MOSCHIP_DEVICE_ID_7715)
979 return 1; 975 return 1;
980 976
981 return 2; 977 return 2;
982 } 978 }
983 979
984 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port) 980 static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
985 { 981 {
986 struct usb_serial *serial; 982 struct usb_serial *serial;
987 struct urb *urb; 983 struct urb *urb;
988 struct moschip_port *mos7720_port; 984 struct moschip_port *mos7720_port;
989 int response; 985 int response;
990 int port_number; 986 int port_number;
991 __u8 data; 987 __u8 data;
992 int allocated_urbs = 0; 988 int allocated_urbs = 0;
993 int j; 989 int j;
994 990
995 serial = port->serial; 991 serial = port->serial;
996 992
997 mos7720_port = usb_get_serial_port_data(port); 993 mos7720_port = usb_get_serial_port_data(port);
998 if (mos7720_port == NULL) 994 if (mos7720_port == NULL)
999 return -ENODEV; 995 return -ENODEV;
1000 996
1001 usb_clear_halt(serial->dev, port->write_urb->pipe); 997 usb_clear_halt(serial->dev, port->write_urb->pipe);
1002 usb_clear_halt(serial->dev, port->read_urb->pipe); 998 usb_clear_halt(serial->dev, port->read_urb->pipe);
1003 999
1004 /* Initialising the write urb pool */ 1000 /* Initialising the write urb pool */
1005 for (j = 0; j < NUM_URBS; ++j) { 1001 for (j = 0; j < NUM_URBS; ++j) {
1006 urb = usb_alloc_urb(0, GFP_KERNEL); 1002 urb = usb_alloc_urb(0, GFP_KERNEL);
1007 mos7720_port->write_urb_pool[j] = urb; 1003 mos7720_port->write_urb_pool[j] = urb;
1008 1004
1009 if (urb == NULL) { 1005 if (urb == NULL) {
1010 dev_err(&port->dev, "No more urbs???\n"); 1006 dev_err(&port->dev, "No more urbs???\n");
1011 continue; 1007 continue;
1012 } 1008 }
1013 1009
1014 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, 1010 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1015 GFP_KERNEL); 1011 GFP_KERNEL);
1016 if (!urb->transfer_buffer) { 1012 if (!urb->transfer_buffer) {
1017 dev_err(&port->dev, 1013 dev_err(&port->dev,
1018 "%s-out of memory for urb buffers.\n", 1014 "%s-out of memory for urb buffers.\n",
1019 __func__); 1015 __func__);
1020 usb_free_urb(mos7720_port->write_urb_pool[j]); 1016 usb_free_urb(mos7720_port->write_urb_pool[j]);
1021 mos7720_port->write_urb_pool[j] = NULL; 1017 mos7720_port->write_urb_pool[j] = NULL;
1022 continue; 1018 continue;
1023 } 1019 }
1024 allocated_urbs++; 1020 allocated_urbs++;
1025 } 1021 }
1026 1022
1027 if (!allocated_urbs) 1023 if (!allocated_urbs)
1028 return -ENOMEM; 1024 return -ENOMEM;
1029 1025
1030 /* Initialize MCS7720 -- Write Init values to corresponding Registers 1026 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1031 * 1027 *
1032 * Register Index 1028 * Register Index
1033 * 0 : THR/RHR 1029 * 0 : THR/RHR
1034 * 1 : IER 1030 * 1 : IER
1035 * 2 : FCR 1031 * 2 : FCR
1036 * 3 : LCR 1032 * 3 : LCR
1037 * 4 : MCR 1033 * 4 : MCR
1038 * 5 : LSR 1034 * 5 : LSR
1039 * 6 : MSR 1035 * 6 : MSR
1040 * 7 : SPR 1036 * 7 : SPR
1041 * 1037 *
1042 * 0x08 : SP1/2 Control Reg 1038 * 0x08 : SP1/2 Control Reg
1043 */ 1039 */
1044 port_number = port->number - port->serial->minor; 1040 port_number = port->number - port->serial->minor;
1045 read_mos_reg(serial, port_number, LSR, &data); 1041 read_mos_reg(serial, port_number, LSR, &data);
1046 1042
1047 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data); 1043 dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
1048 1044
1049 write_mos_reg(serial, dummy, SP1_REG, 0x02); 1045 write_mos_reg(serial, dummy, SP1_REG, 0x02);
1050 write_mos_reg(serial, dummy, SP2_REG, 0x02); 1046 write_mos_reg(serial, dummy, SP2_REG, 0x02);
1051 1047
1052 write_mos_reg(serial, port_number, IER, 0x00); 1048 write_mos_reg(serial, port_number, IER, 0x00);
1053 write_mos_reg(serial, port_number, FCR, 0x00); 1049 write_mos_reg(serial, port_number, FCR, 0x00);
1054 1050
1055 write_mos_reg(serial, port_number, FCR, 0xcf); 1051 write_mos_reg(serial, port_number, FCR, 0xcf);
1056 mos7720_port->shadowLCR = 0x03; 1052 mos7720_port->shadowLCR = 0x03;
1057 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); 1053 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1058 mos7720_port->shadowMCR = 0x0b; 1054 mos7720_port->shadowMCR = 0x0b;
1059 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR); 1055 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1060 1056
1061 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00); 1057 write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1062 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data); 1058 read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
1063 data = data | (port->number - port->serial->minor + 1); 1059 data = data | (port->number - port->serial->minor + 1);
1064 write_mos_reg(serial, dummy, SP_CONTROL_REG, data); 1060 write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1065 mos7720_port->shadowLCR = 0x83; 1061 mos7720_port->shadowLCR = 0x83;
1066 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); 1062 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1067 write_mos_reg(serial, port_number, THR, 0x0c); 1063 write_mos_reg(serial, port_number, THR, 0x0c);
1068 write_mos_reg(serial, port_number, IER, 0x00); 1064 write_mos_reg(serial, port_number, IER, 0x00);
1069 mos7720_port->shadowLCR = 0x03; 1065 mos7720_port->shadowLCR = 0x03;
1070 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); 1066 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1071 write_mos_reg(serial, port_number, IER, 0x0c); 1067 write_mos_reg(serial, port_number, IER, 0x0c);
1072 1068
1073 response = usb_submit_urb(port->read_urb, GFP_KERNEL); 1069 response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1074 if (response) 1070 if (response)
1075 dev_err(&port->dev, "%s - Error %d submitting read urb\n", 1071 dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1076 __func__, response); 1072 __func__, response);
1077 1073
1078 /* initialize our icount structure */ 1074 /* initialize our icount structure */
1079 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount)); 1075 memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
1080 1076
1081 /* initialize our port settings */ 1077 /* initialize our port settings */
1082 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */ 1078 mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1083 1079
1084 /* send a open port command */ 1080 /* send a open port command */
1085 mos7720_port->open = 1; 1081 mos7720_port->open = 1;
1086 1082
1087 return 0; 1083 return 0;
1088 } 1084 }
1089 1085
1090 /* 1086 /*
1091 * mos7720_chars_in_buffer 1087 * mos7720_chars_in_buffer
1092 * this function is called by the tty driver when it wants to know how many 1088 * this function is called by the tty driver when it wants to know how many
1093 * bytes of data we currently have outstanding in the port (data that has 1089 * bytes of data we currently have outstanding in the port (data that has
1094 * been written, but hasn't made it out the port yet) 1090 * been written, but hasn't made it out the port yet)
1095 * If successful, we return the number of bytes left to be written in the 1091 * If successful, we return the number of bytes left to be written in the
1096 * system, 1092 * system,
1097 * Otherwise we return a negative error number. 1093 * Otherwise we return a negative error number.
1098 */ 1094 */
1099 static int mos7720_chars_in_buffer(struct tty_struct *tty) 1095 static int mos7720_chars_in_buffer(struct tty_struct *tty)
1100 { 1096 {
1101 struct usb_serial_port *port = tty->driver_data; 1097 struct usb_serial_port *port = tty->driver_data;
1102 int i; 1098 int i;
1103 int chars = 0; 1099 int chars = 0;
1104 struct moschip_port *mos7720_port; 1100 struct moschip_port *mos7720_port;
1105 1101
1106 mos7720_port = usb_get_serial_port_data(port); 1102 mos7720_port = usb_get_serial_port_data(port);
1107 if (mos7720_port == NULL) 1103 if (mos7720_port == NULL)
1108 return 0; 1104 return 0;
1109 1105
1110 for (i = 0; i < NUM_URBS; ++i) { 1106 for (i = 0; i < NUM_URBS; ++i) {
1111 if (mos7720_port->write_urb_pool[i] && 1107 if (mos7720_port->write_urb_pool[i] &&
1112 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS) 1108 mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1113 chars += URB_TRANSFER_BUFFER_SIZE; 1109 chars += URB_TRANSFER_BUFFER_SIZE;
1114 } 1110 }
1115 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); 1111 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1116 return chars; 1112 return chars;
1117 } 1113 }
1118 1114
1119 static void mos7720_close(struct usb_serial_port *port) 1115 static void mos7720_close(struct usb_serial_port *port)
1120 { 1116 {
1121 struct usb_serial *serial; 1117 struct usb_serial *serial;
1122 struct moschip_port *mos7720_port; 1118 struct moschip_port *mos7720_port;
1123 int j; 1119 int j;
1124 1120
1125 serial = port->serial; 1121 serial = port->serial;
1126 1122
1127 mos7720_port = usb_get_serial_port_data(port); 1123 mos7720_port = usb_get_serial_port_data(port);
1128 if (mos7720_port == NULL) 1124 if (mos7720_port == NULL)
1129 return; 1125 return;
1130 1126
1131 for (j = 0; j < NUM_URBS; ++j) 1127 for (j = 0; j < NUM_URBS; ++j)
1132 usb_kill_urb(mos7720_port->write_urb_pool[j]); 1128 usb_kill_urb(mos7720_port->write_urb_pool[j]);
1133 1129
1134 /* Freeing Write URBs */ 1130 /* Freeing Write URBs */
1135 for (j = 0; j < NUM_URBS; ++j) { 1131 for (j = 0; j < NUM_URBS; ++j) {
1136 if (mos7720_port->write_urb_pool[j]) { 1132 if (mos7720_port->write_urb_pool[j]) {
1137 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer); 1133 kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1138 usb_free_urb(mos7720_port->write_urb_pool[j]); 1134 usb_free_urb(mos7720_port->write_urb_pool[j]);
1139 } 1135 }
1140 } 1136 }
1141 1137
1142 /* While closing port, shutdown all bulk read, write * 1138 /* While closing port, shutdown all bulk read, write *
1143 * and interrupt read if they exists, otherwise nop */ 1139 * and interrupt read if they exists, otherwise nop */
1144 usb_kill_urb(port->write_urb); 1140 usb_kill_urb(port->write_urb);
1145 usb_kill_urb(port->read_urb); 1141 usb_kill_urb(port->read_urb);
1146 1142
1147 mutex_lock(&serial->disc_mutex); 1143 mutex_lock(&serial->disc_mutex);
1148 /* these commands must not be issued if the device has 1144 /* these commands must not be issued if the device has
1149 * been disconnected */ 1145 * been disconnected */
1150 if (!serial->disconnected) { 1146 if (!serial->disconnected) {
1151 write_mos_reg(serial, port->number - port->serial->minor, 1147 write_mos_reg(serial, port->number - port->serial->minor,
1152 MCR, 0x00); 1148 MCR, 0x00);
1153 write_mos_reg(serial, port->number - port->serial->minor, 1149 write_mos_reg(serial, port->number - port->serial->minor,
1154 IER, 0x00); 1150 IER, 0x00);
1155 } 1151 }
1156 mutex_unlock(&serial->disc_mutex); 1152 mutex_unlock(&serial->disc_mutex);
1157 mos7720_port->open = 0; 1153 mos7720_port->open = 0;
1158 } 1154 }
1159 1155
1160 static void mos7720_break(struct tty_struct *tty, int break_state) 1156 static void mos7720_break(struct tty_struct *tty, int break_state)
1161 { 1157 {
1162 struct usb_serial_port *port = tty->driver_data; 1158 struct usb_serial_port *port = tty->driver_data;
1163 unsigned char data; 1159 unsigned char data;
1164 struct usb_serial *serial; 1160 struct usb_serial *serial;
1165 struct moschip_port *mos7720_port; 1161 struct moschip_port *mos7720_port;
1166 1162
1167 serial = port->serial; 1163 serial = port->serial;
1168 1164
1169 mos7720_port = usb_get_serial_port_data(port); 1165 mos7720_port = usb_get_serial_port_data(port);
1170 if (mos7720_port == NULL) 1166 if (mos7720_port == NULL)
1171 return; 1167 return;
1172 1168
1173 if (break_state == -1) 1169 if (break_state == -1)
1174 data = mos7720_port->shadowLCR | UART_LCR_SBC; 1170 data = mos7720_port->shadowLCR | UART_LCR_SBC;
1175 else 1171 else
1176 data = mos7720_port->shadowLCR & ~UART_LCR_SBC; 1172 data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1177 1173
1178 mos7720_port->shadowLCR = data; 1174 mos7720_port->shadowLCR = data;
1179 write_mos_reg(serial, port->number - port->serial->minor, 1175 write_mos_reg(serial, port->number - port->serial->minor,
1180 LCR, mos7720_port->shadowLCR); 1176 LCR, mos7720_port->shadowLCR);
1181 } 1177 }
1182 1178
1183 /* 1179 /*
1184 * mos7720_write_room 1180 * mos7720_write_room
1185 * this function is called by the tty driver when it wants to know how many 1181 * this function is called by the tty driver when it wants to know how many
1186 * bytes of data we can accept for a specific port. 1182 * bytes of data we can accept for a specific port.
1187 * If successful, we return the amount of room that we have for this port 1183 * If successful, we return the amount of room that we have for this port
1188 * Otherwise we return a negative error number. 1184 * Otherwise we return a negative error number.
1189 */ 1185 */
1190 static int mos7720_write_room(struct tty_struct *tty) 1186 static int mos7720_write_room(struct tty_struct *tty)
1191 { 1187 {
1192 struct usb_serial_port *port = tty->driver_data; 1188 struct usb_serial_port *port = tty->driver_data;
1193 struct moschip_port *mos7720_port; 1189 struct moschip_port *mos7720_port;
1194 int room = 0; 1190 int room = 0;
1195 int i; 1191 int i;
1196 1192
1197 mos7720_port = usb_get_serial_port_data(port); 1193 mos7720_port = usb_get_serial_port_data(port);
1198 if (mos7720_port == NULL) 1194 if (mos7720_port == NULL)
1199 return -ENODEV; 1195 return -ENODEV;
1200 1196
1201 /* FIXME: Locking */ 1197 /* FIXME: Locking */
1202 for (i = 0; i < NUM_URBS; ++i) { 1198 for (i = 0; i < NUM_URBS; ++i) {
1203 if (mos7720_port->write_urb_pool[i] && 1199 if (mos7720_port->write_urb_pool[i] &&
1204 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) 1200 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1205 room += URB_TRANSFER_BUFFER_SIZE; 1201 room += URB_TRANSFER_BUFFER_SIZE;
1206 } 1202 }
1207 1203
1208 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room); 1204 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1209 return room; 1205 return room;
1210 } 1206 }
1211 1207
1212 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port, 1208 static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1213 const unsigned char *data, int count) 1209 const unsigned char *data, int count)
1214 { 1210 {
1215 int status; 1211 int status;
1216 int i; 1212 int i;
1217 int bytes_sent = 0; 1213 int bytes_sent = 0;
1218 int transfer_size; 1214 int transfer_size;
1219 1215
1220 struct moschip_port *mos7720_port; 1216 struct moschip_port *mos7720_port;
1221 struct usb_serial *serial; 1217 struct usb_serial *serial;
1222 struct urb *urb; 1218 struct urb *urb;
1223 const unsigned char *current_position = data; 1219 const unsigned char *current_position = data;
1224 1220
1225 serial = port->serial; 1221 serial = port->serial;
1226 1222
1227 mos7720_port = usb_get_serial_port_data(port); 1223 mos7720_port = usb_get_serial_port_data(port);
1228 if (mos7720_port == NULL) 1224 if (mos7720_port == NULL)
1229 return -ENODEV; 1225 return -ENODEV;
1230 1226
1231 /* try to find a free urb in the list */ 1227 /* try to find a free urb in the list */
1232 urb = NULL; 1228 urb = NULL;
1233 1229
1234 for (i = 0; i < NUM_URBS; ++i) { 1230 for (i = 0; i < NUM_URBS; ++i) {
1235 if (mos7720_port->write_urb_pool[i] && 1231 if (mos7720_port->write_urb_pool[i] &&
1236 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) { 1232 mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1237 urb = mos7720_port->write_urb_pool[i]; 1233 urb = mos7720_port->write_urb_pool[i];
1238 dev_dbg(&port->dev, "URB:%d\n", i); 1234 dev_dbg(&port->dev, "URB:%d\n", i);
1239 break; 1235 break;
1240 } 1236 }
1241 } 1237 }
1242 1238
1243 if (urb == NULL) { 1239 if (urb == NULL) {
1244 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__); 1240 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1245 goto exit; 1241 goto exit;
1246 } 1242 }
1247 1243
1248 if (urb->transfer_buffer == NULL) { 1244 if (urb->transfer_buffer == NULL) {
1249 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, 1245 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1250 GFP_KERNEL); 1246 GFP_KERNEL);
1251 if (urb->transfer_buffer == NULL) { 1247 if (urb->transfer_buffer == NULL) {
1252 dev_err_console(port, "%s no more kernel memory...\n", 1248 dev_err_console(port, "%s no more kernel memory...\n",
1253 __func__); 1249 __func__);
1254 goto exit; 1250 goto exit;
1255 } 1251 }
1256 } 1252 }
1257 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE); 1253 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1258 1254
1259 memcpy(urb->transfer_buffer, current_position, transfer_size); 1255 memcpy(urb->transfer_buffer, current_position, transfer_size);
1260 usb_serial_debug_data(&port->dev, __func__, transfer_size, 1256 usb_serial_debug_data(&port->dev, __func__, transfer_size,
1261 urb->transfer_buffer); 1257 urb->transfer_buffer);
1262 1258
1263 /* fill urb with data and submit */ 1259 /* fill urb with data and submit */
1264 usb_fill_bulk_urb(urb, serial->dev, 1260 usb_fill_bulk_urb(urb, serial->dev,
1265 usb_sndbulkpipe(serial->dev, 1261 usb_sndbulkpipe(serial->dev,
1266 port->bulk_out_endpointAddress), 1262 port->bulk_out_endpointAddress),
1267 urb->transfer_buffer, transfer_size, 1263 urb->transfer_buffer, transfer_size,
1268 mos7720_bulk_out_data_callback, mos7720_port); 1264 mos7720_bulk_out_data_callback, mos7720_port);
1269 1265
1270 /* send it down the pipe */ 1266 /* send it down the pipe */
1271 status = usb_submit_urb(urb, GFP_ATOMIC); 1267 status = usb_submit_urb(urb, GFP_ATOMIC);
1272 if (status) { 1268 if (status) {
1273 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed " 1269 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1274 "with status = %d\n", __func__, status); 1270 "with status = %d\n", __func__, status);
1275 bytes_sent = status; 1271 bytes_sent = status;
1276 goto exit; 1272 goto exit;
1277 } 1273 }
1278 bytes_sent = transfer_size; 1274 bytes_sent = transfer_size;
1279 1275
1280 exit: 1276 exit:
1281 return bytes_sent; 1277 return bytes_sent;
1282 } 1278 }
1283 1279
1284 static void mos7720_throttle(struct tty_struct *tty) 1280 static void mos7720_throttle(struct tty_struct *tty)
1285 { 1281 {
1286 struct usb_serial_port *port = tty->driver_data; 1282 struct usb_serial_port *port = tty->driver_data;
1287 struct moschip_port *mos7720_port; 1283 struct moschip_port *mos7720_port;
1288 int status; 1284 int status;
1289 1285
1290 mos7720_port = usb_get_serial_port_data(port); 1286 mos7720_port = usb_get_serial_port_data(port);
1291 1287
1292 if (mos7720_port == NULL) 1288 if (mos7720_port == NULL)
1293 return; 1289 return;
1294 1290
1295 if (!mos7720_port->open) { 1291 if (!mos7720_port->open) {
1296 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1292 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1297 return; 1293 return;
1298 } 1294 }
1299 1295
1300 /* if we are implementing XON/XOFF, send the stop character */ 1296 /* if we are implementing XON/XOFF, send the stop character */
1301 if (I_IXOFF(tty)) { 1297 if (I_IXOFF(tty)) {
1302 unsigned char stop_char = STOP_CHAR(tty); 1298 unsigned char stop_char = STOP_CHAR(tty);
1303 status = mos7720_write(tty, port, &stop_char, 1); 1299 status = mos7720_write(tty, port, &stop_char, 1);
1304 if (status <= 0) 1300 if (status <= 0)
1305 return; 1301 return;
1306 } 1302 }
1307 1303
1308 /* if we are implementing RTS/CTS, toggle that line */ 1304 /* if we are implementing RTS/CTS, toggle that line */
1309 if (tty->termios.c_cflag & CRTSCTS) { 1305 if (tty->termios.c_cflag & CRTSCTS) {
1310 mos7720_port->shadowMCR &= ~UART_MCR_RTS; 1306 mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1311 write_mos_reg(port->serial, port->number - port->serial->minor, 1307 write_mos_reg(port->serial, port->number - port->serial->minor,
1312 MCR, mos7720_port->shadowMCR); 1308 MCR, mos7720_port->shadowMCR);
1313 if (status != 0) 1309 if (status != 0)
1314 return; 1310 return;
1315 } 1311 }
1316 } 1312 }
1317 1313
1318 static void mos7720_unthrottle(struct tty_struct *tty) 1314 static void mos7720_unthrottle(struct tty_struct *tty)
1319 { 1315 {
1320 struct usb_serial_port *port = tty->driver_data; 1316 struct usb_serial_port *port = tty->driver_data;
1321 struct moschip_port *mos7720_port = usb_get_serial_port_data(port); 1317 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1322 int status; 1318 int status;
1323 1319
1324 if (mos7720_port == NULL) 1320 if (mos7720_port == NULL)
1325 return; 1321 return;
1326 1322
1327 if (!mos7720_port->open) { 1323 if (!mos7720_port->open) {
1328 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1324 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1329 return; 1325 return;
1330 } 1326 }
1331 1327
1332 /* if we are implementing XON/XOFF, send the start character */ 1328 /* if we are implementing XON/XOFF, send the start character */
1333 if (I_IXOFF(tty)) { 1329 if (I_IXOFF(tty)) {
1334 unsigned char start_char = START_CHAR(tty); 1330 unsigned char start_char = START_CHAR(tty);
1335 status = mos7720_write(tty, port, &start_char, 1); 1331 status = mos7720_write(tty, port, &start_char, 1);
1336 if (status <= 0) 1332 if (status <= 0)
1337 return; 1333 return;
1338 } 1334 }
1339 1335
1340 /* if we are implementing RTS/CTS, toggle that line */ 1336 /* if we are implementing RTS/CTS, toggle that line */
1341 if (tty->termios.c_cflag & CRTSCTS) { 1337 if (tty->termios.c_cflag & CRTSCTS) {
1342 mos7720_port->shadowMCR |= UART_MCR_RTS; 1338 mos7720_port->shadowMCR |= UART_MCR_RTS;
1343 write_mos_reg(port->serial, port->number - port->serial->minor, 1339 write_mos_reg(port->serial, port->number - port->serial->minor,
1344 MCR, mos7720_port->shadowMCR); 1340 MCR, mos7720_port->shadowMCR);
1345 if (status != 0) 1341 if (status != 0)
1346 return; 1342 return;
1347 } 1343 }
1348 } 1344 }
1349 1345
1350 /* FIXME: this function does not work */ 1346 /* FIXME: this function does not work */
1351 static int set_higher_rates(struct moschip_port *mos7720_port, 1347 static int set_higher_rates(struct moschip_port *mos7720_port,
1352 unsigned int baud) 1348 unsigned int baud)
1353 { 1349 {
1354 struct usb_serial_port *port; 1350 struct usb_serial_port *port;
1355 struct usb_serial *serial; 1351 struct usb_serial *serial;
1356 int port_number; 1352 int port_number;
1357 enum mos_regs sp_reg; 1353 enum mos_regs sp_reg;
1358 if (mos7720_port == NULL) 1354 if (mos7720_port == NULL)
1359 return -EINVAL; 1355 return -EINVAL;
1360 1356
1361 port = mos7720_port->port; 1357 port = mos7720_port->port;
1362 serial = port->serial; 1358 serial = port->serial;
1363 1359
1364 /*********************************************** 1360 /***********************************************
1365 * Init Sequence for higher rates 1361 * Init Sequence for higher rates
1366 ***********************************************/ 1362 ***********************************************/
1367 dev_dbg(&port->dev, "Sending Setting Commands ..........\n"); 1363 dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
1368 port_number = port->number - port->serial->minor; 1364 port_number = port->number - port->serial->minor;
1369 1365
1370 write_mos_reg(serial, port_number, IER, 0x00); 1366 write_mos_reg(serial, port_number, IER, 0x00);
1371 write_mos_reg(serial, port_number, FCR, 0x00); 1367 write_mos_reg(serial, port_number, FCR, 0x00);
1372 write_mos_reg(serial, port_number, FCR, 0xcf); 1368 write_mos_reg(serial, port_number, FCR, 0xcf);
1373 mos7720_port->shadowMCR = 0x0b; 1369 mos7720_port->shadowMCR = 0x0b;
1374 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR); 1370 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1375 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00); 1371 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
1376 1372
1377 /*********************************************** 1373 /***********************************************
1378 * Set for higher rates * 1374 * Set for higher rates *
1379 ***********************************************/ 1375 ***********************************************/
1380 /* writing baud rate verbatum into uart clock field clearly not right */ 1376 /* writing baud rate verbatum into uart clock field clearly not right */
1381 if (port_number == 0) 1377 if (port_number == 0)
1382 sp_reg = SP1_REG; 1378 sp_reg = SP1_REG;
1383 else 1379 else
1384 sp_reg = SP2_REG; 1380 sp_reg = SP2_REG;
1385 write_mos_reg(serial, dummy, sp_reg, baud * 0x10); 1381 write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1386 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03); 1382 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1387 mos7720_port->shadowMCR = 0x2b; 1383 mos7720_port->shadowMCR = 0x2b;
1388 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR); 1384 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1389 1385
1390 /*********************************************** 1386 /***********************************************
1391 * Set DLL/DLM 1387 * Set DLL/DLM
1392 ***********************************************/ 1388 ***********************************************/
1393 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB; 1389 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1394 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); 1390 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1395 write_mos_reg(serial, port_number, DLL, 0x01); 1391 write_mos_reg(serial, port_number, DLL, 0x01);
1396 write_mos_reg(serial, port_number, DLM, 0x00); 1392 write_mos_reg(serial, port_number, DLM, 0x00);
1397 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB; 1393 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1398 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); 1394 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1399 1395
1400 return 0; 1396 return 0;
1401 } 1397 }
1402 1398
1403 /* baud rate information */ 1399 /* baud rate information */
1404 struct divisor_table_entry { 1400 struct divisor_table_entry {
1405 __u32 baudrate; 1401 __u32 baudrate;
1406 __u16 divisor; 1402 __u16 divisor;
1407 }; 1403 };
1408 1404
1409 /* Define table of divisors for moschip 7720 hardware * 1405 /* Define table of divisors for moschip 7720 hardware *
1410 * These assume a 3.6864MHz crystal, the standard /16, and * 1406 * These assume a 3.6864MHz crystal, the standard /16, and *
1411 * MCR.7 = 0. */ 1407 * MCR.7 = 0. */
1412 static struct divisor_table_entry divisor_table[] = { 1408 static struct divisor_table_entry divisor_table[] = {
1413 { 50, 2304}, 1409 { 50, 2304},
1414 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */ 1410 { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
1415 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */ 1411 { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
1416 { 150, 768}, 1412 { 150, 768},
1417 { 300, 384}, 1413 { 300, 384},
1418 { 600, 192}, 1414 { 600, 192},
1419 { 1200, 96}, 1415 { 1200, 96},
1420 { 1800, 64}, 1416 { 1800, 64},
1421 { 2400, 48}, 1417 { 2400, 48},
1422 { 4800, 24}, 1418 { 4800, 24},
1423 { 7200, 16}, 1419 { 7200, 16},
1424 { 9600, 12}, 1420 { 9600, 12},
1425 { 19200, 6}, 1421 { 19200, 6},
1426 { 38400, 3}, 1422 { 38400, 3},
1427 { 57600, 2}, 1423 { 57600, 2},
1428 { 115200, 1}, 1424 { 115200, 1},
1429 }; 1425 };
1430 1426
1431 /***************************************************************************** 1427 /*****************************************************************************
1432 * calc_baud_rate_divisor 1428 * calc_baud_rate_divisor
1433 * this function calculates the proper baud rate divisor for the specified 1429 * this function calculates the proper baud rate divisor for the specified
1434 * baud rate. 1430 * baud rate.
1435 *****************************************************************************/ 1431 *****************************************************************************/
1436 static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor) 1432 static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
1437 { 1433 {
1438 int i; 1434 int i;
1439 __u16 custom; 1435 __u16 custom;
1440 __u16 round1; 1436 __u16 round1;
1441 __u16 round; 1437 __u16 round;
1442 1438
1443 1439
1444 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate); 1440 dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
1445 1441
1446 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) { 1442 for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1447 if (divisor_table[i].baudrate == baudrate) { 1443 if (divisor_table[i].baudrate == baudrate) {
1448 *divisor = divisor_table[i].divisor; 1444 *divisor = divisor_table[i].divisor;
1449 return 0; 1445 return 0;
1450 } 1446 }
1451 } 1447 }
1452 1448
1453 /* After trying for all the standard baud rates * 1449 /* After trying for all the standard baud rates *
1454 * Try calculating the divisor for this baud rate */ 1450 * Try calculating the divisor for this baud rate */
1455 if (baudrate > 75 && baudrate < 230400) { 1451 if (baudrate > 75 && baudrate < 230400) {
1456 /* get the divisor */ 1452 /* get the divisor */
1457 custom = (__u16)(230400L / baudrate); 1453 custom = (__u16)(230400L / baudrate);
1458 1454
1459 /* Check for round off */ 1455 /* Check for round off */
1460 round1 = (__u16)(2304000L / baudrate); 1456 round1 = (__u16)(2304000L / baudrate);
1461 round = (__u16)(round1 - (custom * 10)); 1457 round = (__u16)(round1 - (custom * 10));
1462 if (round > 4) 1458 if (round > 4)
1463 custom++; 1459 custom++;
1464 *divisor = custom; 1460 *divisor = custom;
1465 1461
1466 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom); 1462 dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
1467 return 0; 1463 return 0;
1468 } 1464 }
1469 1465
1470 dev_dbg(&port->dev, "Baud calculation Failed...\n"); 1466 dev_dbg(&port->dev, "Baud calculation Failed...\n");
1471 return -EINVAL; 1467 return -EINVAL;
1472 } 1468 }
1473 1469
1474 /* 1470 /*
1475 * send_cmd_write_baud_rate 1471 * send_cmd_write_baud_rate
1476 * this function sends the proper command to change the baud rate of the 1472 * this function sends the proper command to change the baud rate of the
1477 * specified port. 1473 * specified port.
1478 */ 1474 */
1479 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port, 1475 static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1480 int baudrate) 1476 int baudrate)
1481 { 1477 {
1482 struct usb_serial_port *port; 1478 struct usb_serial_port *port;
1483 struct usb_serial *serial; 1479 struct usb_serial *serial;
1484 int divisor; 1480 int divisor;
1485 int status; 1481 int status;
1486 unsigned char number; 1482 unsigned char number;
1487 1483
1488 if (mos7720_port == NULL) 1484 if (mos7720_port == NULL)
1489 return -1; 1485 return -1;
1490 1486
1491 port = mos7720_port->port; 1487 port = mos7720_port->port;
1492 serial = port->serial; 1488 serial = port->serial;
1493 1489
1494 number = port->number - port->serial->minor; 1490 number = port->number - port->serial->minor;
1495 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate); 1491 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
1496 1492
1497 /* Calculate the Divisor */ 1493 /* Calculate the Divisor */
1498 status = calc_baud_rate_divisor(port, baudrate, &divisor); 1494 status = calc_baud_rate_divisor(port, baudrate, &divisor);
1499 if (status) { 1495 if (status) {
1500 dev_err(&port->dev, "%s - bad baud rate\n", __func__); 1496 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1501 return status; 1497 return status;
1502 } 1498 }
1503 1499
1504 /* Enable access to divisor latch */ 1500 /* Enable access to divisor latch */
1505 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB; 1501 mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1506 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR); 1502 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1507 1503
1508 /* Write the divisor */ 1504 /* Write the divisor */
1509 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff)); 1505 write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1510 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8)); 1506 write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
1511 1507
1512 /* Disable access to divisor latch */ 1508 /* Disable access to divisor latch */
1513 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB; 1509 mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1514 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR); 1510 write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1515 1511
1516 return status; 1512 return status;
1517 } 1513 }
1518 1514
1519 /* 1515 /*
1520 * change_port_settings 1516 * change_port_settings
1521 * This routine is called to set the UART on the device to match 1517 * This routine is called to set the UART on the device to match
1522 * the specified new settings. 1518 * the specified new settings.
1523 */ 1519 */
1524 static void change_port_settings(struct tty_struct *tty, 1520 static void change_port_settings(struct tty_struct *tty,
1525 struct moschip_port *mos7720_port, 1521 struct moschip_port *mos7720_port,
1526 struct ktermios *old_termios) 1522 struct ktermios *old_termios)
1527 { 1523 {
1528 struct usb_serial_port *port; 1524 struct usb_serial_port *port;
1529 struct usb_serial *serial; 1525 struct usb_serial *serial;
1530 int baud; 1526 int baud;
1531 unsigned cflag; 1527 unsigned cflag;
1532 unsigned iflag; 1528 unsigned iflag;
1533 __u8 mask = 0xff; 1529 __u8 mask = 0xff;
1534 __u8 lData; 1530 __u8 lData;
1535 __u8 lParity; 1531 __u8 lParity;
1536 __u8 lStop; 1532 __u8 lStop;
1537 int status; 1533 int status;
1538 int port_number; 1534 int port_number;
1539 1535
1540 if (mos7720_port == NULL) 1536 if (mos7720_port == NULL)
1541 return ; 1537 return ;
1542 1538
1543 port = mos7720_port->port; 1539 port = mos7720_port->port;
1544 serial = port->serial; 1540 serial = port->serial;
1545 port_number = port->number - port->serial->minor; 1541 port_number = port->number - port->serial->minor;
1546 1542
1547 if (!mos7720_port->open) { 1543 if (!mos7720_port->open) {
1548 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1544 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1549 return; 1545 return;
1550 } 1546 }
1551 1547
1552 lData = UART_LCR_WLEN8; 1548 lData = UART_LCR_WLEN8;
1553 lStop = 0x00; /* 1 stop bit */ 1549 lStop = 0x00; /* 1 stop bit */
1554 lParity = 0x00; /* No parity */ 1550 lParity = 0x00; /* No parity */
1555 1551
1556 cflag = tty->termios.c_cflag; 1552 cflag = tty->termios.c_cflag;
1557 iflag = tty->termios.c_iflag; 1553 iflag = tty->termios.c_iflag;
1558 1554
1559 /* Change the number of bits */ 1555 /* Change the number of bits */
1560 switch (cflag & CSIZE) { 1556 switch (cflag & CSIZE) {
1561 case CS5: 1557 case CS5:
1562 lData = UART_LCR_WLEN5; 1558 lData = UART_LCR_WLEN5;
1563 mask = 0x1f; 1559 mask = 0x1f;
1564 break; 1560 break;
1565 1561
1566 case CS6: 1562 case CS6:
1567 lData = UART_LCR_WLEN6; 1563 lData = UART_LCR_WLEN6;
1568 mask = 0x3f; 1564 mask = 0x3f;
1569 break; 1565 break;
1570 1566
1571 case CS7: 1567 case CS7:
1572 lData = UART_LCR_WLEN7; 1568 lData = UART_LCR_WLEN7;
1573 mask = 0x7f; 1569 mask = 0x7f;
1574 break; 1570 break;
1575 default: 1571 default:
1576 case CS8: 1572 case CS8:
1577 lData = UART_LCR_WLEN8; 1573 lData = UART_LCR_WLEN8;
1578 break; 1574 break;
1579 } 1575 }
1580 1576
1581 /* Change the Parity bit */ 1577 /* Change the Parity bit */
1582 if (cflag & PARENB) { 1578 if (cflag & PARENB) {
1583 if (cflag & PARODD) { 1579 if (cflag & PARODD) {
1584 lParity = UART_LCR_PARITY; 1580 lParity = UART_LCR_PARITY;
1585 dev_dbg(&port->dev, "%s - parity = odd\n", __func__); 1581 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1586 } else { 1582 } else {
1587 lParity = (UART_LCR_EPAR | UART_LCR_PARITY); 1583 lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1588 dev_dbg(&port->dev, "%s - parity = even\n", __func__); 1584 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1589 } 1585 }
1590 1586
1591 } else { 1587 } else {
1592 dev_dbg(&port->dev, "%s - parity = none\n", __func__); 1588 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1593 } 1589 }
1594 1590
1595 if (cflag & CMSPAR) 1591 if (cflag & CMSPAR)
1596 lParity = lParity | 0x20; 1592 lParity = lParity | 0x20;
1597 1593
1598 /* Change the Stop bit */ 1594 /* Change the Stop bit */
1599 if (cflag & CSTOPB) { 1595 if (cflag & CSTOPB) {
1600 lStop = UART_LCR_STOP; 1596 lStop = UART_LCR_STOP;
1601 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__); 1597 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1602 } else { 1598 } else {
1603 lStop = 0x00; 1599 lStop = 0x00;
1604 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__); 1600 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1605 } 1601 }
1606 1602
1607 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */ 1603 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
1608 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */ 1604 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
1609 #define LCR_PAR_MASK 0x38 /* Mask for parity field */ 1605 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
1610 1606
1611 /* Update the LCR with the correct value */ 1607 /* Update the LCR with the correct value */
1612 mos7720_port->shadowLCR &= 1608 mos7720_port->shadowLCR &=
1613 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); 1609 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1614 mos7720_port->shadowLCR |= (lData | lParity | lStop); 1610 mos7720_port->shadowLCR |= (lData | lParity | lStop);
1615 1611
1616 1612
1617 /* Disable Interrupts */ 1613 /* Disable Interrupts */
1618 write_mos_reg(serial, port_number, IER, 0x00); 1614 write_mos_reg(serial, port_number, IER, 0x00);
1619 write_mos_reg(serial, port_number, FCR, 0x00); 1615 write_mos_reg(serial, port_number, FCR, 0x00);
1620 write_mos_reg(serial, port_number, FCR, 0xcf); 1616 write_mos_reg(serial, port_number, FCR, 0xcf);
1621 1617
1622 /* Send the updated LCR value to the mos7720 */ 1618 /* Send the updated LCR value to the mos7720 */
1623 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR); 1619 write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1624 mos7720_port->shadowMCR = 0x0b; 1620 mos7720_port->shadowMCR = 0x0b;
1625 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR); 1621 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1626 1622
1627 /* set up the MCR register and send it to the mos7720 */ 1623 /* set up the MCR register and send it to the mos7720 */
1628 mos7720_port->shadowMCR = UART_MCR_OUT2; 1624 mos7720_port->shadowMCR = UART_MCR_OUT2;
1629 if (cflag & CBAUD) 1625 if (cflag & CBAUD)
1630 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS); 1626 mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1631 1627
1632 if (cflag & CRTSCTS) { 1628 if (cflag & CRTSCTS) {
1633 mos7720_port->shadowMCR |= (UART_MCR_XONANY); 1629 mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1634 /* To set hardware flow control to the specified * 1630 /* To set hardware flow control to the specified *
1635 * serial port, in SP1/2_CONTROL_REG */ 1631 * serial port, in SP1/2_CONTROL_REG */
1636 if (port->number) 1632 if (port->number)
1637 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01); 1633 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
1638 else 1634 else
1639 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02); 1635 write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
1640 1636
1641 } else 1637 } else
1642 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY); 1638 mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1643 1639
1644 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR); 1640 write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1645 1641
1646 /* Determine divisor based on baud rate */ 1642 /* Determine divisor based on baud rate */
1647 baud = tty_get_baud_rate(tty); 1643 baud = tty_get_baud_rate(tty);
1648 if (!baud) { 1644 if (!baud) {
1649 /* pick a default, any default... */ 1645 /* pick a default, any default... */
1650 dev_dbg(&port->dev, "Picked default baud...\n"); 1646 dev_dbg(&port->dev, "Picked default baud...\n");
1651 baud = 9600; 1647 baud = 9600;
1652 } 1648 }
1653 1649
1654 if (baud >= 230400) { 1650 if (baud >= 230400) {
1655 set_higher_rates(mos7720_port, baud); 1651 set_higher_rates(mos7720_port, baud);
1656 /* Enable Interrupts */ 1652 /* Enable Interrupts */
1657 write_mos_reg(serial, port_number, IER, 0x0c); 1653 write_mos_reg(serial, port_number, IER, 0x0c);
1658 return; 1654 return;
1659 } 1655 }
1660 1656
1661 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud); 1657 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1662 status = send_cmd_write_baud_rate(mos7720_port, baud); 1658 status = send_cmd_write_baud_rate(mos7720_port, baud);
1663 /* FIXME: needs to write actual resulting baud back not just 1659 /* FIXME: needs to write actual resulting baud back not just
1664 blindly do so */ 1660 blindly do so */
1665 if (cflag & CBAUD) 1661 if (cflag & CBAUD)
1666 tty_encode_baud_rate(tty, baud, baud); 1662 tty_encode_baud_rate(tty, baud, baud);
1667 /* Enable Interrupts */ 1663 /* Enable Interrupts */
1668 write_mos_reg(serial, port_number, IER, 0x0c); 1664 write_mos_reg(serial, port_number, IER, 0x0c);
1669 1665
1670 if (port->read_urb->status != -EINPROGRESS) { 1666 if (port->read_urb->status != -EINPROGRESS) {
1671 status = usb_submit_urb(port->read_urb, GFP_ATOMIC); 1667 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1672 if (status) 1668 if (status)
1673 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status); 1669 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1674 } 1670 }
1675 } 1671 }
1676 1672
1677 /* 1673 /*
1678 * mos7720_set_termios 1674 * mos7720_set_termios
1679 * this function is called by the tty driver when it wants to change the 1675 * this function is called by the tty driver when it wants to change the
1680 * termios structure. 1676 * termios structure.
1681 */ 1677 */
1682 static void mos7720_set_termios(struct tty_struct *tty, 1678 static void mos7720_set_termios(struct tty_struct *tty,
1683 struct usb_serial_port *port, struct ktermios *old_termios) 1679 struct usb_serial_port *port, struct ktermios *old_termios)
1684 { 1680 {
1685 int status; 1681 int status;
1686 unsigned int cflag; 1682 unsigned int cflag;
1687 struct usb_serial *serial; 1683 struct usb_serial *serial;
1688 struct moschip_port *mos7720_port; 1684 struct moschip_port *mos7720_port;
1689 1685
1690 serial = port->serial; 1686 serial = port->serial;
1691 1687
1692 mos7720_port = usb_get_serial_port_data(port); 1688 mos7720_port = usb_get_serial_port_data(port);
1693 1689
1694 if (mos7720_port == NULL) 1690 if (mos7720_port == NULL)
1695 return; 1691 return;
1696 1692
1697 if (!mos7720_port->open) { 1693 if (!mos7720_port->open) {
1698 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1694 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1699 return; 1695 return;
1700 } 1696 }
1701 1697
1702 dev_dbg(&port->dev, "setting termios - ASPIRE\n"); 1698 dev_dbg(&port->dev, "setting termios - ASPIRE\n");
1703 1699
1704 cflag = tty->termios.c_cflag; 1700 cflag = tty->termios.c_cflag;
1705 1701
1706 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__, 1702 dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
1707 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag)); 1703 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
1708 1704
1709 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__, 1705 dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1710 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag)); 1706 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
1711 1707
1712 /* change the port settings to the new ones specified */ 1708 /* change the port settings to the new ones specified */
1713 change_port_settings(tty, mos7720_port, old_termios); 1709 change_port_settings(tty, mos7720_port, old_termios);
1714 1710
1715 if (port->read_urb->status != -EINPROGRESS) { 1711 if (port->read_urb->status != -EINPROGRESS) {
1716 status = usb_submit_urb(port->read_urb, GFP_ATOMIC); 1712 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1717 if (status) 1713 if (status)
1718 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status); 1714 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1719 } 1715 }
1720 } 1716 }
1721 1717
1722 /* 1718 /*
1723 * get_lsr_info - get line status register info 1719 * get_lsr_info - get line status register info
1724 * 1720 *
1725 * Purpose: Let user call ioctl() to get info when the UART physically 1721 * Purpose: Let user call ioctl() to get info when the UART physically
1726 * is emptied. On bus types like RS485, the transmitter must 1722 * is emptied. On bus types like RS485, the transmitter must
1727 * release the bus after transmitting. This must be done when 1723 * release the bus after transmitting. This must be done when
1728 * the transmit shift register is empty, not be done when the 1724 * the transmit shift register is empty, not be done when the
1729 * transmit holding register is empty. This functionality 1725 * transmit holding register is empty. This functionality
1730 * allows an RS485 driver to be written in user space. 1726 * allows an RS485 driver to be written in user space.
1731 */ 1727 */
1732 static int get_lsr_info(struct tty_struct *tty, 1728 static int get_lsr_info(struct tty_struct *tty,
1733 struct moschip_port *mos7720_port, unsigned int __user *value) 1729 struct moschip_port *mos7720_port, unsigned int __user *value)
1734 { 1730 {
1735 struct usb_serial_port *port = tty->driver_data; 1731 struct usb_serial_port *port = tty->driver_data;
1736 unsigned int result = 0; 1732 unsigned int result = 0;
1737 unsigned char data = 0; 1733 unsigned char data = 0;
1738 int port_number = port->number - port->serial->minor; 1734 int port_number = port->number - port->serial->minor;
1739 int count; 1735 int count;
1740 1736
1741 count = mos7720_chars_in_buffer(tty); 1737 count = mos7720_chars_in_buffer(tty);
1742 if (count == 0) { 1738 if (count == 0) {
1743 read_mos_reg(port->serial, port_number, LSR, &data); 1739 read_mos_reg(port->serial, port_number, LSR, &data);
1744 if ((data & (UART_LSR_TEMT | UART_LSR_THRE)) 1740 if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1745 == (UART_LSR_TEMT | UART_LSR_THRE)) { 1741 == (UART_LSR_TEMT | UART_LSR_THRE)) {
1746 dev_dbg(&port->dev, "%s -- Empty\n", __func__); 1742 dev_dbg(&port->dev, "%s -- Empty\n", __func__);
1747 result = TIOCSER_TEMT; 1743 result = TIOCSER_TEMT;
1748 } 1744 }
1749 } 1745 }
1750 if (copy_to_user(value, &result, sizeof(int))) 1746 if (copy_to_user(value, &result, sizeof(int)))
1751 return -EFAULT; 1747 return -EFAULT;
1752 return 0; 1748 return 0;
1753 } 1749 }
1754 1750
1755 static int mos7720_tiocmget(struct tty_struct *tty) 1751 static int mos7720_tiocmget(struct tty_struct *tty)
1756 { 1752 {
1757 struct usb_serial_port *port = tty->driver_data; 1753 struct usb_serial_port *port = tty->driver_data;
1758 struct moschip_port *mos7720_port = usb_get_serial_port_data(port); 1754 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1759 unsigned int result = 0; 1755 unsigned int result = 0;
1760 unsigned int mcr ; 1756 unsigned int mcr ;
1761 unsigned int msr ; 1757 unsigned int msr ;
1762 1758
1763 mcr = mos7720_port->shadowMCR; 1759 mcr = mos7720_port->shadowMCR;
1764 msr = mos7720_port->shadowMSR; 1760 msr = mos7720_port->shadowMSR;
1765 1761
1766 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */ 1762 result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
1767 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */ 1763 | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
1768 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */ 1764 | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
1769 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */ 1765 | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
1770 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */ 1766 | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
1771 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */ 1767 | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
1772 1768
1773 return result; 1769 return result;
1774 } 1770 }
1775 1771
1776 static int mos7720_tiocmset(struct tty_struct *tty, 1772 static int mos7720_tiocmset(struct tty_struct *tty,
1777 unsigned int set, unsigned int clear) 1773 unsigned int set, unsigned int clear)
1778 { 1774 {
1779 struct usb_serial_port *port = tty->driver_data; 1775 struct usb_serial_port *port = tty->driver_data;
1780 struct moschip_port *mos7720_port = usb_get_serial_port_data(port); 1776 struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1781 unsigned int mcr ; 1777 unsigned int mcr ;
1782 1778
1783 mcr = mos7720_port->shadowMCR; 1779 mcr = mos7720_port->shadowMCR;
1784 1780
1785 if (set & TIOCM_RTS) 1781 if (set & TIOCM_RTS)
1786 mcr |= UART_MCR_RTS; 1782 mcr |= UART_MCR_RTS;
1787 if (set & TIOCM_DTR) 1783 if (set & TIOCM_DTR)
1788 mcr |= UART_MCR_DTR; 1784 mcr |= UART_MCR_DTR;
1789 if (set & TIOCM_LOOP) 1785 if (set & TIOCM_LOOP)
1790 mcr |= UART_MCR_LOOP; 1786 mcr |= UART_MCR_LOOP;
1791 1787
1792 if (clear & TIOCM_RTS) 1788 if (clear & TIOCM_RTS)
1793 mcr &= ~UART_MCR_RTS; 1789 mcr &= ~UART_MCR_RTS;
1794 if (clear & TIOCM_DTR) 1790 if (clear & TIOCM_DTR)
1795 mcr &= ~UART_MCR_DTR; 1791 mcr &= ~UART_MCR_DTR;
1796 if (clear & TIOCM_LOOP) 1792 if (clear & TIOCM_LOOP)
1797 mcr &= ~UART_MCR_LOOP; 1793 mcr &= ~UART_MCR_LOOP;
1798 1794
1799 mos7720_port->shadowMCR = mcr; 1795 mos7720_port->shadowMCR = mcr;
1800 write_mos_reg(port->serial, port->number - port->serial->minor, 1796 write_mos_reg(port->serial, port->number - port->serial->minor,
1801 MCR, mos7720_port->shadowMCR); 1797 MCR, mos7720_port->shadowMCR);
1802 1798
1803 return 0; 1799 return 0;
1804 } 1800 }
1805 1801
1806 static int mos7720_get_icount(struct tty_struct *tty, 1802 static int mos7720_get_icount(struct tty_struct *tty,
1807 struct serial_icounter_struct *icount) 1803 struct serial_icounter_struct *icount)
1808 { 1804 {
1809 struct usb_serial_port *port = tty->driver_data; 1805 struct usb_serial_port *port = tty->driver_data;
1810 struct moschip_port *mos7720_port; 1806 struct moschip_port *mos7720_port;
1811 struct async_icount cnow; 1807 struct async_icount cnow;
1812 1808
1813 mos7720_port = usb_get_serial_port_data(port); 1809 mos7720_port = usb_get_serial_port_data(port);
1814 cnow = mos7720_port->icount; 1810 cnow = mos7720_port->icount;
1815 1811
1816 icount->cts = cnow.cts; 1812 icount->cts = cnow.cts;
1817 icount->dsr = cnow.dsr; 1813 icount->dsr = cnow.dsr;
1818 icount->rng = cnow.rng; 1814 icount->rng = cnow.rng;
1819 icount->dcd = cnow.dcd; 1815 icount->dcd = cnow.dcd;
1820 icount->rx = cnow.rx; 1816 icount->rx = cnow.rx;
1821 icount->tx = cnow.tx; 1817 icount->tx = cnow.tx;
1822 icount->frame = cnow.frame; 1818 icount->frame = cnow.frame;
1823 icount->overrun = cnow.overrun; 1819 icount->overrun = cnow.overrun;
1824 icount->parity = cnow.parity; 1820 icount->parity = cnow.parity;
1825 icount->brk = cnow.brk; 1821 icount->brk = cnow.brk;
1826 icount->buf_overrun = cnow.buf_overrun; 1822 icount->buf_overrun = cnow.buf_overrun;
1827 1823
1828 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__, 1824 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
1829 icount->rx, icount->tx); 1825 icount->rx, icount->tx);
1830 return 0; 1826 return 0;
1831 } 1827 }
1832 1828
1833 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd, 1829 static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1834 unsigned int __user *value) 1830 unsigned int __user *value)
1835 { 1831 {
1836 unsigned int mcr; 1832 unsigned int mcr;
1837 unsigned int arg; 1833 unsigned int arg;
1838 1834
1839 struct usb_serial_port *port; 1835 struct usb_serial_port *port;
1840 1836
1841 if (mos7720_port == NULL) 1837 if (mos7720_port == NULL)
1842 return -1; 1838 return -1;
1843 1839
1844 port = (struct usb_serial_port *)mos7720_port->port; 1840 port = (struct usb_serial_port *)mos7720_port->port;
1845 mcr = mos7720_port->shadowMCR; 1841 mcr = mos7720_port->shadowMCR;
1846 1842
1847 if (copy_from_user(&arg, value, sizeof(int))) 1843 if (copy_from_user(&arg, value, sizeof(int)))
1848 return -EFAULT; 1844 return -EFAULT;
1849 1845
1850 switch (cmd) { 1846 switch (cmd) {
1851 case TIOCMBIS: 1847 case TIOCMBIS:
1852 if (arg & TIOCM_RTS) 1848 if (arg & TIOCM_RTS)
1853 mcr |= UART_MCR_RTS; 1849 mcr |= UART_MCR_RTS;
1854 if (arg & TIOCM_DTR) 1850 if (arg & TIOCM_DTR)
1855 mcr |= UART_MCR_RTS; 1851 mcr |= UART_MCR_RTS;
1856 if (arg & TIOCM_LOOP) 1852 if (arg & TIOCM_LOOP)
1857 mcr |= UART_MCR_LOOP; 1853 mcr |= UART_MCR_LOOP;
1858 break; 1854 break;
1859 1855
1860 case TIOCMBIC: 1856 case TIOCMBIC:
1861 if (arg & TIOCM_RTS) 1857 if (arg & TIOCM_RTS)
1862 mcr &= ~UART_MCR_RTS; 1858 mcr &= ~UART_MCR_RTS;
1863 if (arg & TIOCM_DTR) 1859 if (arg & TIOCM_DTR)
1864 mcr &= ~UART_MCR_RTS; 1860 mcr &= ~UART_MCR_RTS;
1865 if (arg & TIOCM_LOOP) 1861 if (arg & TIOCM_LOOP)
1866 mcr &= ~UART_MCR_LOOP; 1862 mcr &= ~UART_MCR_LOOP;
1867 break; 1863 break;
1868 1864
1869 } 1865 }
1870 1866
1871 mos7720_port->shadowMCR = mcr; 1867 mos7720_port->shadowMCR = mcr;
1872 write_mos_reg(port->serial, port->number - port->serial->minor, 1868 write_mos_reg(port->serial, port->number - port->serial->minor,
1873 MCR, mos7720_port->shadowMCR); 1869 MCR, mos7720_port->shadowMCR);
1874 1870
1875 return 0; 1871 return 0;
1876 } 1872 }
1877 1873
1878 static int get_serial_info(struct moschip_port *mos7720_port, 1874 static int get_serial_info(struct moschip_port *mos7720_port,
1879 struct serial_struct __user *retinfo) 1875 struct serial_struct __user *retinfo)
1880 { 1876 {
1881 struct serial_struct tmp; 1877 struct serial_struct tmp;
1882 1878
1883 if (!retinfo) 1879 if (!retinfo)
1884 return -EFAULT; 1880 return -EFAULT;
1885 1881
1886 memset(&tmp, 0, sizeof(tmp)); 1882 memset(&tmp, 0, sizeof(tmp));
1887 1883
1888 tmp.type = PORT_16550A; 1884 tmp.type = PORT_16550A;
1889 tmp.line = mos7720_port->port->serial->minor; 1885 tmp.line = mos7720_port->port->serial->minor;
1890 tmp.port = mos7720_port->port->number; 1886 tmp.port = mos7720_port->port->number;
1891 tmp.irq = 0; 1887 tmp.irq = 0;
1892 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 1888 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1893 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE; 1889 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1894 tmp.baud_base = 9600; 1890 tmp.baud_base = 9600;
1895 tmp.close_delay = 5*HZ; 1891 tmp.close_delay = 5*HZ;
1896 tmp.closing_wait = 30*HZ; 1892 tmp.closing_wait = 30*HZ;
1897 1893
1898 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 1894 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1899 return -EFAULT; 1895 return -EFAULT;
1900 return 0; 1896 return 0;
1901 } 1897 }
1902 1898
1903 static int mos7720_ioctl(struct tty_struct *tty, 1899 static int mos7720_ioctl(struct tty_struct *tty,
1904 unsigned int cmd, unsigned long arg) 1900 unsigned int cmd, unsigned long arg)
1905 { 1901 {
1906 struct usb_serial_port *port = tty->driver_data; 1902 struct usb_serial_port *port = tty->driver_data;
1907 struct moschip_port *mos7720_port; 1903 struct moschip_port *mos7720_port;
1908 struct async_icount cnow; 1904 struct async_icount cnow;
1909 struct async_icount cprev; 1905 struct async_icount cprev;
1910 1906
1911 mos7720_port = usb_get_serial_port_data(port); 1907 mos7720_port = usb_get_serial_port_data(port);
1912 if (mos7720_port == NULL) 1908 if (mos7720_port == NULL)
1913 return -ENODEV; 1909 return -ENODEV;
1914 1910
1915 dev_dbg(&port->dev, "%s - cmd = 0x%x", __func__, cmd); 1911 dev_dbg(&port->dev, "%s - cmd = 0x%x", __func__, cmd);
1916 1912
1917 switch (cmd) { 1913 switch (cmd) {
1918 case TIOCSERGETLSR: 1914 case TIOCSERGETLSR:
1919 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__); 1915 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1920 return get_lsr_info(tty, mos7720_port, 1916 return get_lsr_info(tty, mos7720_port,
1921 (unsigned int __user *)arg); 1917 (unsigned int __user *)arg);
1922 1918
1923 /* FIXME: These should be using the mode methods */ 1919 /* FIXME: These should be using the mode methods */
1924 case TIOCMBIS: 1920 case TIOCMBIS:
1925 case TIOCMBIC: 1921 case TIOCMBIC:
1926 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__); 1922 dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
1927 return set_modem_info(mos7720_port, cmd, 1923 return set_modem_info(mos7720_port, cmd,
1928 (unsigned int __user *)arg); 1924 (unsigned int __user *)arg);
1929 1925
1930 case TIOCGSERIAL: 1926 case TIOCGSERIAL:
1931 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__); 1927 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
1932 return get_serial_info(mos7720_port, 1928 return get_serial_info(mos7720_port,
1933 (struct serial_struct __user *)arg); 1929 (struct serial_struct __user *)arg);
1934 1930
1935 case TIOCMIWAIT: 1931 case TIOCMIWAIT:
1936 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__); 1932 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
1937 cprev = mos7720_port->icount; 1933 cprev = mos7720_port->icount;
1938 while (1) { 1934 while (1) {
1939 if (signal_pending(current)) 1935 if (signal_pending(current))
1940 return -ERESTARTSYS; 1936 return -ERESTARTSYS;
1941 cnow = mos7720_port->icount; 1937 cnow = mos7720_port->icount;
1942 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 1938 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1943 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) 1939 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1944 return -EIO; /* no change => error */ 1940 return -EIO; /* no change => error */
1945 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 1941 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1946 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 1942 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1947 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 1943 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1948 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 1944 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1949 return 0; 1945 return 0;
1950 } 1946 }
1951 cprev = cnow; 1947 cprev = cnow;
1952 } 1948 }
1953 /* NOTREACHED */ 1949 /* NOTREACHED */
1954 break; 1950 break;
1955 } 1951 }
1956 1952
1957 return -ENOIOCTLCMD; 1953 return -ENOIOCTLCMD;
1958 } 1954 }
1959 1955
1960 static int mos7720_startup(struct usb_serial *serial) 1956 static int mos7720_startup(struct usb_serial *serial)
1961 { 1957 {
1962 struct usb_device *dev; 1958 struct usb_device *dev;
1963 char data; 1959 char data;
1964 u16 product; 1960 u16 product;
1965 int ret_val; 1961 int ret_val;
1966 1962
1967 product = le16_to_cpu(serial->dev->descriptor.idProduct); 1963 product = le16_to_cpu(serial->dev->descriptor.idProduct);
1968 dev = serial->dev; 1964 dev = serial->dev;
1969 1965
1970 /* 1966 /*
1971 * The 7715 uses the first bulk in/out endpoint pair for the parallel 1967 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1972 * port, and the second for the serial port. Because the usbserial core 1968 * port, and the second for the serial port. Because the usbserial core
1973 * assumes both pairs are serial ports, we must engage in a bit of 1969 * assumes both pairs are serial ports, we must engage in a bit of
1974 * subterfuge and swap the pointers for ports 0 and 1 in order to make 1970 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1975 * port 0 point to the serial port. However, both moschip devices use a 1971 * port 0 point to the serial port. However, both moschip devices use a
1976 * single interrupt-in endpoint for both ports (as mentioned a little 1972 * single interrupt-in endpoint for both ports (as mentioned a little
1977 * further down), and this endpoint was assigned to port 0. So after 1973 * further down), and this endpoint was assigned to port 0. So after
1978 * the swap, we must copy the interrupt endpoint elements from port 1 1974 * the swap, we must copy the interrupt endpoint elements from port 1
1979 * (as newly assigned) to port 0, and null out port 1 pointers. 1975 * (as newly assigned) to port 0, and null out port 1 pointers.
1980 */ 1976 */
1981 if (product == MOSCHIP_DEVICE_ID_7715) { 1977 if (product == MOSCHIP_DEVICE_ID_7715) {
1982 struct usb_serial_port *tmp = serial->port[0]; 1978 struct usb_serial_port *tmp = serial->port[0];
1983 serial->port[0] = serial->port[1]; 1979 serial->port[0] = serial->port[1];
1984 serial->port[1] = tmp; 1980 serial->port[1] = tmp;
1985 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb; 1981 serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1986 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer; 1982 serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1987 serial->port[0]->interrupt_in_endpointAddress = 1983 serial->port[0]->interrupt_in_endpointAddress =
1988 tmp->interrupt_in_endpointAddress; 1984 tmp->interrupt_in_endpointAddress;
1989 serial->port[1]->interrupt_in_urb = NULL; 1985 serial->port[1]->interrupt_in_urb = NULL;
1990 serial->port[1]->interrupt_in_buffer = NULL; 1986 serial->port[1]->interrupt_in_buffer = NULL;
1991 } 1987 }
1992 1988
1993 /* setting configuration feature to one */ 1989 /* setting configuration feature to one */
1994 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 1990 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1995 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ); 1991 (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
1996 1992
1997 /* start the interrupt urb */ 1993 /* start the interrupt urb */
1998 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL); 1994 ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1999 if (ret_val) 1995 if (ret_val)
2000 dev_err(&dev->dev, 1996 dev_err(&dev->dev,
2001 "%s - Error %d submitting control urb\n", 1997 "%s - Error %d submitting control urb\n",
2002 __func__, ret_val); 1998 __func__, ret_val);
2003 1999
2004 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT 2000 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2005 if (product == MOSCHIP_DEVICE_ID_7715) { 2001 if (product == MOSCHIP_DEVICE_ID_7715) {
2006 ret_val = mos7715_parport_init(serial); 2002 ret_val = mos7715_parport_init(serial);
2007 if (ret_val < 0) 2003 if (ret_val < 0)
2008 return ret_val; 2004 return ret_val;
2009 } 2005 }
2010 #endif 2006 #endif
2011 /* LSR For Port 1 */ 2007 /* LSR For Port 1 */
2012 read_mos_reg(serial, 0, LSR, &data); 2008 read_mos_reg(serial, 0, LSR, &data);
2013 dev_dbg(&dev->dev, "LSR:%x\n", data); 2009 dev_dbg(&dev->dev, "LSR:%x\n", data);
2014 2010
2015 return 0; 2011 return 0;
2016 } 2012 }
2017 2013
2018 static void mos7720_release(struct usb_serial *serial) 2014 static void mos7720_release(struct usb_serial *serial)
2019 { 2015 {
2020 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT 2016 #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
2021 /* close the parallel port */ 2017 /* close the parallel port */
2022 2018
2023 if (le16_to_cpu(serial->dev->descriptor.idProduct) 2019 if (le16_to_cpu(serial->dev->descriptor.idProduct)
2024 == MOSCHIP_DEVICE_ID_7715) { 2020 == MOSCHIP_DEVICE_ID_7715) {
2025 struct urbtracker *urbtrack; 2021 struct urbtracker *urbtrack;
2026 unsigned long flags; 2022 unsigned long flags;
2027 struct mos7715_parport *mos_parport = 2023 struct mos7715_parport *mos_parport =
2028 usb_get_serial_data(serial); 2024 usb_get_serial_data(serial);
2029 2025
2030 /* prevent NULL ptr dereference in port callbacks */ 2026 /* prevent NULL ptr dereference in port callbacks */
2031 spin_lock(&release_lock); 2027 spin_lock(&release_lock);
2032 mos_parport->pp->private_data = NULL; 2028 mos_parport->pp->private_data = NULL;
2033 spin_unlock(&release_lock); 2029 spin_unlock(&release_lock);
2034 2030
2035 /* wait for synchronous usb calls to return */ 2031 /* wait for synchronous usb calls to return */
2036 if (mos_parport->msg_pending) 2032 if (mos_parport->msg_pending)
2037 wait_for_completion_timeout(&mos_parport->syncmsg_compl, 2033 wait_for_completion_timeout(&mos_parport->syncmsg_compl,
2038 MOS_WDR_TIMEOUT); 2034 MOS_WDR_TIMEOUT);
2039 2035
2040 parport_remove_port(mos_parport->pp); 2036 parport_remove_port(mos_parport->pp);
2041 usb_set_serial_data(serial, NULL); 2037 usb_set_serial_data(serial, NULL);
2042 mos_parport->serial = NULL; 2038 mos_parport->serial = NULL;
2043 2039
2044 /* if tasklet currently scheduled, wait for it to complete */ 2040 /* if tasklet currently scheduled, wait for it to complete */
2045 tasklet_kill(&mos_parport->urb_tasklet); 2041 tasklet_kill(&mos_parport->urb_tasklet);
2046 2042
2047 /* unlink any urbs sent by the tasklet */ 2043 /* unlink any urbs sent by the tasklet */
2048 spin_lock_irqsave(&mos_parport->listlock, flags); 2044 spin_lock_irqsave(&mos_parport->listlock, flags);
2049 list_for_each_entry(urbtrack, 2045 list_for_each_entry(urbtrack,
2050 &mos_parport->active_urbs, 2046 &mos_parport->active_urbs,
2051 urblist_entry) 2047 urblist_entry)
2052 usb_unlink_urb(urbtrack->urb); 2048 usb_unlink_urb(urbtrack->urb);
2053 spin_unlock_irqrestore(&mos_parport->listlock, flags); 2049 spin_unlock_irqrestore(&mos_parport->listlock, flags);
2054 2050
2055 kref_put(&mos_parport->ref_count, destroy_mos_parport); 2051 kref_put(&mos_parport->ref_count, destroy_mos_parport);
2056 } 2052 }
2057 #endif 2053 #endif
2058 } 2054 }
2059 2055
2060 static int mos7720_port_probe(struct usb_serial_port *port) 2056 static int mos7720_port_probe(struct usb_serial_port *port)
2061 { 2057 {
2062 struct moschip_port *mos7720_port; 2058 struct moschip_port *mos7720_port;
2063 2059
2064 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL); 2060 mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
2065 if (!mos7720_port) 2061 if (!mos7720_port)
2066 return -ENOMEM; 2062 return -ENOMEM;
2067 2063
2068 /* Initialize all port interrupt end point to port 0 int endpoint. 2064 /* Initialize all port interrupt end point to port 0 int endpoint.
2069 * Our device has only one interrupt endpoint common to all ports. 2065 * Our device has only one interrupt endpoint common to all ports.
2070 */ 2066 */
2071 port->interrupt_in_endpointAddress = 2067 port->interrupt_in_endpointAddress =
2072 port->serial->port[0]->interrupt_in_endpointAddress; 2068 port->serial->port[0]->interrupt_in_endpointAddress;
2073 mos7720_port->port = port; 2069 mos7720_port->port = port;
2074 2070
2075 usb_set_serial_port_data(port, mos7720_port); 2071 usb_set_serial_port_data(port, mos7720_port);
2076 2072
2077 return 0; 2073 return 0;
2078 } 2074 }
2079 2075
2080 static int mos7720_port_remove(struct usb_serial_port *port) 2076 static int mos7720_port_remove(struct usb_serial_port *port)
2081 { 2077 {
2082 struct moschip_port *mos7720_port; 2078 struct moschip_port *mos7720_port;
2083 2079
2084 mos7720_port = usb_get_serial_port_data(port); 2080 mos7720_port = usb_get_serial_port_data(port);
2085 kfree(mos7720_port); 2081 kfree(mos7720_port);
2086 2082
2087 return 0; 2083 return 0;
2088 } 2084 }
2089 2085
2090 static struct usb_serial_driver moschip7720_2port_driver = { 2086 static struct usb_serial_driver moschip7720_2port_driver = {
2091 .driver = { 2087 .driver = {
2092 .owner = THIS_MODULE, 2088 .owner = THIS_MODULE,
2093 .name = "moschip7720", 2089 .name = "moschip7720",
2094 }, 2090 },
2095 .description = "Moschip 2 port adapter", 2091 .description = "Moschip 2 port adapter",
2096 .id_table = id_table, 2092 .id_table = id_table,
2097 .calc_num_ports = mos77xx_calc_num_ports, 2093 .calc_num_ports = mos77xx_calc_num_ports,
2098 .open = mos7720_open, 2094 .open = mos7720_open,
2099 .close = mos7720_close, 2095 .close = mos7720_close,
2100 .throttle = mos7720_throttle, 2096 .throttle = mos7720_throttle,
2101 .unthrottle = mos7720_unthrottle, 2097 .unthrottle = mos7720_unthrottle,
2102 .probe = mos77xx_probe, 2098 .probe = mos77xx_probe,
2103 .attach = mos7720_startup, 2099 .attach = mos7720_startup,
2104 .release = mos7720_release, 2100 .release = mos7720_release,
2105 .port_probe = mos7720_port_probe, 2101 .port_probe = mos7720_port_probe,
2106 .port_remove = mos7720_port_remove, 2102 .port_remove = mos7720_port_remove,
2107 .ioctl = mos7720_ioctl, 2103 .ioctl = mos7720_ioctl,
2108 .tiocmget = mos7720_tiocmget, 2104 .tiocmget = mos7720_tiocmget,
2109 .tiocmset = mos7720_tiocmset, 2105 .tiocmset = mos7720_tiocmset,
2110 .get_icount = mos7720_get_icount, 2106 .get_icount = mos7720_get_icount,
2111 .set_termios = mos7720_set_termios, 2107 .set_termios = mos7720_set_termios,
2112 .write = mos7720_write, 2108 .write = mos7720_write,
2113 .write_room = mos7720_write_room, 2109 .write_room = mos7720_write_room,
2114 .chars_in_buffer = mos7720_chars_in_buffer, 2110 .chars_in_buffer = mos7720_chars_in_buffer,
2115 .break_ctl = mos7720_break, 2111 .break_ctl = mos7720_break,
2116 .read_bulk_callback = mos7720_bulk_in_callback, 2112 .read_bulk_callback = mos7720_bulk_in_callback,
2117 .read_int_callback = NULL /* dynamically assigned in probe() */ 2113 .read_int_callback = NULL /* dynamically assigned in probe() */
2118 }; 2114 };
2119 2115
2120 static struct usb_serial_driver * const serial_drivers[] = { 2116 static struct usb_serial_driver * const serial_drivers[] = {
2121 &moschip7720_2port_driver, NULL 2117 &moschip7720_2port_driver, NULL
2122 }; 2118 };
2123 2119
2124 module_usb_serial_driver(serial_drivers, id_table); 2120 module_usb_serial_driver(serial_drivers, id_table);
2125 2121
2126 MODULE_AUTHOR(DRIVER_AUTHOR); 2122 MODULE_AUTHOR(DRIVER_AUTHOR);
2127 MODULE_DESCRIPTION(DRIVER_DESC); 2123 MODULE_DESCRIPTION(DRIVER_DESC);
2128 MODULE_LICENSE("GPL"); 2124 MODULE_LICENSE("GPL");
2129 2125
drivers/usb/serial/mos7840.c
1 /* 1 /*
2 * This program is free software; you can redistribute it and/or modify 2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by 3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or 4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version. 5 * (at your option) any later version.
6 * 6 *
7 * This program is distributed in the hope that it will be useful, 7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details. 10 * GNU General Public License for more details.
11 * 11 *
12 * You should have received a copy of the GNU General Public License 12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software 13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 * 15 *
16 * Clean ups from Moschip version and a few ioctl implementations by: 16 * Clean ups from Moschip version and a few ioctl implementations by:
17 * Paul B Schroeder <pschroeder "at" uplogix "dot" com> 17 * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
18 * 18 *
19 * Originally based on drivers/usb/serial/io_edgeport.c which is: 19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved. 20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com> 21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22 * 22 *
23 */ 23 */
24 24
25 #include <linux/kernel.h> 25 #include <linux/kernel.h>
26 #include <linux/errno.h> 26 #include <linux/errno.h>
27 #include <linux/init.h> 27 #include <linux/init.h>
28 #include <linux/slab.h> 28 #include <linux/slab.h>
29 #include <linux/tty.h> 29 #include <linux/tty.h>
30 #include <linux/tty_driver.h> 30 #include <linux/tty_driver.h>
31 #include <linux/tty_flip.h> 31 #include <linux/tty_flip.h>
32 #include <linux/module.h> 32 #include <linux/module.h>
33 #include <linux/serial.h> 33 #include <linux/serial.h>
34 #include <linux/usb.h> 34 #include <linux/usb.h>
35 #include <linux/usb/serial.h> 35 #include <linux/usb/serial.h>
36 #include <linux/uaccess.h> 36 #include <linux/uaccess.h>
37 37
38 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver" 38 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
39 39
40 /* 40 /*
41 * 16C50 UART register defines 41 * 16C50 UART register defines
42 */ 42 */
43 43
44 #define LCR_BITS_5 0x00 /* 5 bits/char */ 44 #define LCR_BITS_5 0x00 /* 5 bits/char */
45 #define LCR_BITS_6 0x01 /* 6 bits/char */ 45 #define LCR_BITS_6 0x01 /* 6 bits/char */
46 #define LCR_BITS_7 0x02 /* 7 bits/char */ 46 #define LCR_BITS_7 0x02 /* 7 bits/char */
47 #define LCR_BITS_8 0x03 /* 8 bits/char */ 47 #define LCR_BITS_8 0x03 /* 8 bits/char */
48 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */ 48 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
49 49
50 #define LCR_STOP_1 0x00 /* 1 stop bit */ 50 #define LCR_STOP_1 0x00 /* 1 stop bit */
51 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */ 51 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
52 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */ 52 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
53 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */ 53 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
54 54
55 #define LCR_PAR_NONE 0x00 /* No parity */ 55 #define LCR_PAR_NONE 0x00 /* No parity */
56 #define LCR_PAR_ODD 0x08 /* Odd parity */ 56 #define LCR_PAR_ODD 0x08 /* Odd parity */
57 #define LCR_PAR_EVEN 0x18 /* Even parity */ 57 #define LCR_PAR_EVEN 0x18 /* Even parity */
58 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */ 58 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
59 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */ 59 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
60 #define LCR_PAR_MASK 0x38 /* Mask for parity field */ 60 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
61 61
62 #define LCR_SET_BREAK 0x40 /* Set Break condition */ 62 #define LCR_SET_BREAK 0x40 /* Set Break condition */
63 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */ 63 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
64 64
65 #define MCR_DTR 0x01 /* Assert DTR */ 65 #define MCR_DTR 0x01 /* Assert DTR */
66 #define MCR_RTS 0x02 /* Assert RTS */ 66 #define MCR_RTS 0x02 /* Assert RTS */
67 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */ 67 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
68 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */ 68 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
69 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */ 69 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
70 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */ 70 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
71 71
72 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */ 72 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
73 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */ 73 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
74 #define MOS7840_MSR_RI 0x40 /* Current state of RI */ 74 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
75 #define MOS7840_MSR_CD 0x80 /* Current state of CD */ 75 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
76 76
77 /* 77 /*
78 * Defines used for sending commands to port 78 * Defines used for sending commands to port
79 */ 79 */
80 80
81 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */ 81 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
82 82
83 #define MOS_PORT1 0x0200 83 #define MOS_PORT1 0x0200
84 #define MOS_PORT2 0x0300 84 #define MOS_PORT2 0x0300
85 #define MOS_VENREG 0x0000 85 #define MOS_VENREG 0x0000
86 #define MOS_MAX_PORT 0x02 86 #define MOS_MAX_PORT 0x02
87 #define MOS_WRITE 0x0E 87 #define MOS_WRITE 0x0E
88 #define MOS_READ 0x0D 88 #define MOS_READ 0x0D
89 89
90 /* Requests */ 90 /* Requests */
91 #define MCS_RD_RTYPE 0xC0 91 #define MCS_RD_RTYPE 0xC0
92 #define MCS_WR_RTYPE 0x40 92 #define MCS_WR_RTYPE 0x40
93 #define MCS_RDREQ 0x0D 93 #define MCS_RDREQ 0x0D
94 #define MCS_WRREQ 0x0E 94 #define MCS_WRREQ 0x0E
95 #define MCS_CTRL_TIMEOUT 500 95 #define MCS_CTRL_TIMEOUT 500
96 #define VENDOR_READ_LENGTH (0x01) 96 #define VENDOR_READ_LENGTH (0x01)
97 97
98 #define MAX_NAME_LEN 64 98 #define MAX_NAME_LEN 64
99 99
100 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */ 100 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
101 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */ 101 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
102 102
103 /* For higher baud Rates use TIOCEXBAUD */ 103 /* For higher baud Rates use TIOCEXBAUD */
104 #define TIOCEXBAUD 0x5462 104 #define TIOCEXBAUD 0x5462
105 105
106 /* vendor id and device id defines */ 106 /* vendor id and device id defines */
107 107
108 /* The native mos7840/7820 component */ 108 /* The native mos7840/7820 component */
109 #define USB_VENDOR_ID_MOSCHIP 0x9710 109 #define USB_VENDOR_ID_MOSCHIP 0x9710
110 #define MOSCHIP_DEVICE_ID_7840 0x7840 110 #define MOSCHIP_DEVICE_ID_7840 0x7840
111 #define MOSCHIP_DEVICE_ID_7820 0x7820 111 #define MOSCHIP_DEVICE_ID_7820 0x7820
112 #define MOSCHIP_DEVICE_ID_7810 0x7810 112 #define MOSCHIP_DEVICE_ID_7810 0x7810
113 /* The native component can have its vendor/device id's overridden 113 /* The native component can have its vendor/device id's overridden
114 * in vendor-specific implementations. Such devices can be handled 114 * in vendor-specific implementations. Such devices can be handled
115 * by making a change here, in id_table. 115 * by making a change here, in id_table.
116 */ 116 */
117 #define USB_VENDOR_ID_BANDB 0x0856 117 #define USB_VENDOR_ID_BANDB 0x0856
118 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22 118 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
119 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00 119 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
120 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24 120 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
121 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01 121 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
122 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29 122 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
123 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30 123 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
124 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31 124 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
125 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32 125 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
126 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42 126 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
127 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02 127 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
128 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44 128 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
129 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03 129 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
130 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24 130 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
131 131
132 /* This driver also supports 132 /* This driver also supports
133 * ATEN UC2324 device using Moschip MCS7840 133 * ATEN UC2324 device using Moschip MCS7840
134 * ATEN UC2322 device using Moschip MCS7820 134 * ATEN UC2322 device using Moschip MCS7820
135 */ 135 */
136 #define USB_VENDOR_ID_ATENINTL 0x0557 136 #define USB_VENDOR_ID_ATENINTL 0x0557
137 #define ATENINTL_DEVICE_ID_UC2324 0x2011 137 #define ATENINTL_DEVICE_ID_UC2324 0x2011
138 #define ATENINTL_DEVICE_ID_UC2322 0x7820 138 #define ATENINTL_DEVICE_ID_UC2322 0x7820
139 139
140 /* Interrupt Routine Defines */ 140 /* Interrupt Routine Defines */
141 141
142 #define SERIAL_IIR_RLS 0x06 142 #define SERIAL_IIR_RLS 0x06
143 #define SERIAL_IIR_MS 0x00 143 #define SERIAL_IIR_MS 0x00
144 144
145 /* 145 /*
146 * Emulation of the bit mask on the LINE STATUS REGISTER. 146 * Emulation of the bit mask on the LINE STATUS REGISTER.
147 */ 147 */
148 #define SERIAL_LSR_DR 0x0001 148 #define SERIAL_LSR_DR 0x0001
149 #define SERIAL_LSR_OE 0x0002 149 #define SERIAL_LSR_OE 0x0002
150 #define SERIAL_LSR_PE 0x0004 150 #define SERIAL_LSR_PE 0x0004
151 #define SERIAL_LSR_FE 0x0008 151 #define SERIAL_LSR_FE 0x0008
152 #define SERIAL_LSR_BI 0x0010 152 #define SERIAL_LSR_BI 0x0010
153 153
154 #define MOS_MSR_DELTA_CTS 0x10 154 #define MOS_MSR_DELTA_CTS 0x10
155 #define MOS_MSR_DELTA_DSR 0x20 155 #define MOS_MSR_DELTA_DSR 0x20
156 #define MOS_MSR_DELTA_RI 0x40 156 #define MOS_MSR_DELTA_RI 0x40
157 #define MOS_MSR_DELTA_CD 0x80 157 #define MOS_MSR_DELTA_CD 0x80
158 158
159 /* Serial Port register Address */ 159 /* Serial Port register Address */
160 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01)) 160 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
161 #define FIFO_CONTROL_REGISTER ((__u16)(0x02)) 161 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
162 #define LINE_CONTROL_REGISTER ((__u16)(0x03)) 162 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
163 #define MODEM_CONTROL_REGISTER ((__u16)(0x04)) 163 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
164 #define LINE_STATUS_REGISTER ((__u16)(0x05)) 164 #define LINE_STATUS_REGISTER ((__u16)(0x05))
165 #define MODEM_STATUS_REGISTER ((__u16)(0x06)) 165 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
166 #define SCRATCH_PAD_REGISTER ((__u16)(0x07)) 166 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
167 #define DIVISOR_LATCH_LSB ((__u16)(0x00)) 167 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
168 #define DIVISOR_LATCH_MSB ((__u16)(0x01)) 168 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
169 169
170 #define CLK_MULTI_REGISTER ((__u16)(0x02)) 170 #define CLK_MULTI_REGISTER ((__u16)(0x02))
171 #define CLK_START_VALUE_REGISTER ((__u16)(0x03)) 171 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
172 #define GPIO_REGISTER ((__u16)(0x07)) 172 #define GPIO_REGISTER ((__u16)(0x07))
173 173
174 #define SERIAL_LCR_DLAB ((__u16)(0x0080)) 174 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
175 175
176 /* 176 /*
177 * URB POOL related defines 177 * URB POOL related defines
178 */ 178 */
179 #define NUM_URBS 16 /* URB Count */ 179 #define NUM_URBS 16 /* URB Count */
180 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */ 180 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
181 181
182 /* LED on/off milliseconds*/ 182 /* LED on/off milliseconds*/
183 #define LED_ON_MS 500 183 #define LED_ON_MS 500
184 #define LED_OFF_MS 500 184 #define LED_OFF_MS 500
185 185
186 static int device_type; 186 static int device_type;
187 187
188 static const struct usb_device_id id_table[] = { 188 static const struct usb_device_id id_table[] = {
189 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)}, 189 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
190 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)}, 190 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
191 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)}, 191 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
192 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)}, 192 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
193 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)}, 193 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)}, 194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)}, 195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)}, 196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)}, 197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)}, 198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)}, 199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)}, 200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)}, 201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)}, 202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)}, 203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)}, 204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
205 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)}, 205 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
206 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)}, 206 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
207 {} /* terminating entry */ 207 {} /* terminating entry */
208 }; 208 };
209 MODULE_DEVICE_TABLE(usb, id_table); 209 MODULE_DEVICE_TABLE(usb, id_table);
210 210
211 /* This structure holds all of the local port information */ 211 /* This structure holds all of the local port information */
212 212
213 struct moschip_port { 213 struct moschip_port {
214 int port_num; /*Actual port number in the device(1,2,etc) */ 214 int port_num; /*Actual port number in the device(1,2,etc) */
215 struct urb *write_urb; /* write URB for this port */ 215 struct urb *write_urb; /* write URB for this port */
216 struct urb *read_urb; /* read URB for this port */ 216 struct urb *read_urb; /* read URB for this port */
217 __u8 shadowLCR; /* last LCR value received */ 217 __u8 shadowLCR; /* last LCR value received */
218 __u8 shadowMCR; /* last MCR value received */ 218 __u8 shadowMCR; /* last MCR value received */
219 char open; 219 char open;
220 char open_ports; 220 char open_ports;
221 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */ 221 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
222 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */ 222 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
223 int delta_msr_cond; 223 int delta_msr_cond;
224 struct async_icount icount; 224 struct async_icount icount;
225 struct usb_serial_port *port; /* loop back to the owner of this object */ 225 struct usb_serial_port *port; /* loop back to the owner of this object */
226 226
227 /* Offsets */ 227 /* Offsets */
228 __u8 SpRegOffset; 228 __u8 SpRegOffset;
229 __u8 ControlRegOffset; 229 __u8 ControlRegOffset;
230 __u8 DcrRegOffset; 230 __u8 DcrRegOffset;
231 /* for processing control URBS in interrupt context */ 231 /* for processing control URBS in interrupt context */
232 struct urb *control_urb; 232 struct urb *control_urb;
233 struct usb_ctrlrequest *dr; 233 struct usb_ctrlrequest *dr;
234 char *ctrl_buf; 234 char *ctrl_buf;
235 int MsrLsr; 235 int MsrLsr;
236 236
237 spinlock_t pool_lock; 237 spinlock_t pool_lock;
238 struct urb *write_urb_pool[NUM_URBS]; 238 struct urb *write_urb_pool[NUM_URBS];
239 char busy[NUM_URBS]; 239 char busy[NUM_URBS];
240 bool read_urb_busy; 240 bool read_urb_busy;
241 241
242 /* For device(s) with LED indicator */ 242 /* For device(s) with LED indicator */
243 bool has_led; 243 bool has_led;
244 bool led_flag; 244 bool led_flag;
245 struct timer_list led_timer1; /* Timer for LED on */ 245 struct timer_list led_timer1; /* Timer for LED on */
246 struct timer_list led_timer2; /* Timer for LED off */ 246 struct timer_list led_timer2; /* Timer for LED off */
247 }; 247 };
248 248
249 /* 249 /*
250 * mos7840_set_reg_sync 250 * mos7840_set_reg_sync
251 * To set the Control register by calling usb_fill_control_urb function 251 * To set the Control register by calling usb_fill_control_urb function
252 * by passing usb_sndctrlpipe function as parameter. 252 * by passing usb_sndctrlpipe function as parameter.
253 */ 253 */
254 254
255 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg, 255 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
256 __u16 val) 256 __u16 val)
257 { 257 {
258 struct usb_device *dev = port->serial->dev; 258 struct usb_device *dev = port->serial->dev;
259 val = val & 0x00ff; 259 val = val & 0x00ff;
260 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val); 260 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
261 261
262 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, 262 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
263 MCS_WR_RTYPE, val, reg, NULL, 0, 263 MCS_WR_RTYPE, val, reg, NULL, 0,
264 MOS_WDR_TIMEOUT); 264 MOS_WDR_TIMEOUT);
265 } 265 }
266 266
267 /* 267 /*
268 * mos7840_get_reg_sync 268 * mos7840_get_reg_sync
269 * To set the Uart register by calling usb_fill_control_urb function by 269 * To set the Uart register by calling usb_fill_control_urb function by
270 * passing usb_rcvctrlpipe function as parameter. 270 * passing usb_rcvctrlpipe function as parameter.
271 */ 271 */
272 272
273 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg, 273 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
274 __u16 *val) 274 __u16 *val)
275 { 275 {
276 struct usb_device *dev = port->serial->dev; 276 struct usb_device *dev = port->serial->dev;
277 int ret = 0; 277 int ret = 0;
278 u8 *buf; 278 u8 *buf;
279 279
280 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); 280 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
281 if (!buf) 281 if (!buf)
282 return -ENOMEM; 282 return -ENOMEM;
283 283
284 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, 284 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
285 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH, 285 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
286 MOS_WDR_TIMEOUT); 286 MOS_WDR_TIMEOUT);
287 *val = buf[0]; 287 *val = buf[0];
288 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val); 288 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
289 289
290 kfree(buf); 290 kfree(buf);
291 return ret; 291 return ret;
292 } 292 }
293 293
294 /* 294 /*
295 * mos7840_set_uart_reg 295 * mos7840_set_uart_reg
296 * To set the Uart register by calling usb_fill_control_urb function by 296 * To set the Uart register by calling usb_fill_control_urb function by
297 * passing usb_sndctrlpipe function as parameter. 297 * passing usb_sndctrlpipe function as parameter.
298 */ 298 */
299 299
300 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg, 300 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
301 __u16 val) 301 __u16 val)
302 { 302 {
303 303
304 struct usb_device *dev = port->serial->dev; 304 struct usb_device *dev = port->serial->dev;
305 val = val & 0x00ff; 305 val = val & 0x00ff;
306 /* For the UART control registers, the application number need 306 /* For the UART control registers, the application number need
307 to be Or'ed */ 307 to be Or'ed */
308 if (port->serial->num_ports == 4) { 308 if (port->serial->num_ports == 4) {
309 val |= (((__u16) port->number - 309 val |= (((__u16) port->number -
310 (__u16) (port->serial->minor)) + 1) << 8; 310 (__u16) (port->serial->minor)) + 1) << 8;
311 } else { 311 } else {
312 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { 312 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
313 val |= (((__u16) port->number - 313 val |= (((__u16) port->number -
314 (__u16) (port->serial->minor)) + 1) << 8; 314 (__u16) (port->serial->minor)) + 1) << 8;
315 } else { 315 } else {
316 val |= (((__u16) port->number - 316 val |= (((__u16) port->number -
317 (__u16) (port->serial->minor)) + 2) << 8; 317 (__u16) (port->serial->minor)) + 2) << 8;
318 } 318 }
319 } 319 }
320 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val); 320 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
321 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, 321 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
322 MCS_WR_RTYPE, val, reg, NULL, 0, 322 MCS_WR_RTYPE, val, reg, NULL, 0,
323 MOS_WDR_TIMEOUT); 323 MOS_WDR_TIMEOUT);
324 324
325 } 325 }
326 326
327 /* 327 /*
328 * mos7840_get_uart_reg 328 * mos7840_get_uart_reg
329 * To set the Control register by calling usb_fill_control_urb function 329 * To set the Control register by calling usb_fill_control_urb function
330 * by passing usb_rcvctrlpipe function as parameter. 330 * by passing usb_rcvctrlpipe function as parameter.
331 */ 331 */
332 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg, 332 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
333 __u16 *val) 333 __u16 *val)
334 { 334 {
335 struct usb_device *dev = port->serial->dev; 335 struct usb_device *dev = port->serial->dev;
336 int ret = 0; 336 int ret = 0;
337 __u16 Wval; 337 __u16 Wval;
338 u8 *buf; 338 u8 *buf;
339 339
340 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL); 340 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
341 if (!buf) 341 if (!buf)
342 return -ENOMEM; 342 return -ENOMEM;
343 343
344 /* Wval is same as application number */ 344 /* Wval is same as application number */
345 if (port->serial->num_ports == 4) { 345 if (port->serial->num_ports == 4) {
346 Wval = 346 Wval =
347 (((__u16) port->number - (__u16) (port->serial->minor)) + 347 (((__u16) port->number - (__u16) (port->serial->minor)) +
348 1) << 8; 348 1) << 8;
349 } else { 349 } else {
350 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) { 350 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
351 Wval = (((__u16) port->number - 351 Wval = (((__u16) port->number -
352 (__u16) (port->serial->minor)) + 1) << 8; 352 (__u16) (port->serial->minor)) + 1) << 8;
353 } else { 353 } else {
354 Wval = (((__u16) port->number - 354 Wval = (((__u16) port->number -
355 (__u16) (port->serial->minor)) + 2) << 8; 355 (__u16) (port->serial->minor)) + 2) << 8;
356 } 356 }
357 } 357 }
358 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval); 358 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
359 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ, 359 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
360 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH, 360 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
361 MOS_WDR_TIMEOUT); 361 MOS_WDR_TIMEOUT);
362 *val = buf[0]; 362 *val = buf[0];
363 363
364 kfree(buf); 364 kfree(buf);
365 return ret; 365 return ret;
366 } 366 }
367 367
368 static void mos7840_dump_serial_port(struct usb_serial_port *port, 368 static void mos7840_dump_serial_port(struct usb_serial_port *port,
369 struct moschip_port *mos7840_port) 369 struct moschip_port *mos7840_port)
370 { 370 {
371 371
372 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset); 372 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
373 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset); 373 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
374 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset); 374 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
375 375
376 } 376 }
377 377
378 /************************************************************************/ 378 /************************************************************************/
379 /************************************************************************/ 379 /************************************************************************/
380 /* I N T E R F A C E F U N C T I O N S */ 380 /* I N T E R F A C E F U N C T I O N S */
381 /* I N T E R F A C E F U N C T I O N S */ 381 /* I N T E R F A C E F U N C T I O N S */
382 /************************************************************************/ 382 /************************************************************************/
383 /************************************************************************/ 383 /************************************************************************/
384 384
385 static inline void mos7840_set_port_private(struct usb_serial_port *port, 385 static inline void mos7840_set_port_private(struct usb_serial_port *port,
386 struct moschip_port *data) 386 struct moschip_port *data)
387 { 387 {
388 usb_set_serial_port_data(port, (void *)data); 388 usb_set_serial_port_data(port, (void *)data);
389 } 389 }
390 390
391 static inline struct moschip_port *mos7840_get_port_private(struct 391 static inline struct moschip_port *mos7840_get_port_private(struct
392 usb_serial_port 392 usb_serial_port
393 *port) 393 *port)
394 { 394 {
395 return (struct moschip_port *)usb_get_serial_port_data(port); 395 return (struct moschip_port *)usb_get_serial_port_data(port);
396 } 396 }
397 397
398 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr) 398 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
399 { 399 {
400 struct moschip_port *mos7840_port; 400 struct moschip_port *mos7840_port;
401 struct async_icount *icount; 401 struct async_icount *icount;
402 mos7840_port = port; 402 mos7840_port = port;
403 icount = &mos7840_port->icount; 403 icount = &mos7840_port->icount;
404 if (new_msr & 404 if (new_msr &
405 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI | 405 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
406 MOS_MSR_DELTA_CD)) { 406 MOS_MSR_DELTA_CD)) {
407 icount = &mos7840_port->icount; 407 icount = &mos7840_port->icount;
408 408
409 /* update input line counters */ 409 /* update input line counters */
410 if (new_msr & MOS_MSR_DELTA_CTS) { 410 if (new_msr & MOS_MSR_DELTA_CTS) {
411 icount->cts++; 411 icount->cts++;
412 smp_wmb(); 412 smp_wmb();
413 } 413 }
414 if (new_msr & MOS_MSR_DELTA_DSR) { 414 if (new_msr & MOS_MSR_DELTA_DSR) {
415 icount->dsr++; 415 icount->dsr++;
416 smp_wmb(); 416 smp_wmb();
417 } 417 }
418 if (new_msr & MOS_MSR_DELTA_CD) { 418 if (new_msr & MOS_MSR_DELTA_CD) {
419 icount->dcd++; 419 icount->dcd++;
420 smp_wmb(); 420 smp_wmb();
421 } 421 }
422 if (new_msr & MOS_MSR_DELTA_RI) { 422 if (new_msr & MOS_MSR_DELTA_RI) {
423 icount->rng++; 423 icount->rng++;
424 smp_wmb(); 424 smp_wmb();
425 } 425 }
426 } 426 }
427 } 427 }
428 428
429 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr) 429 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
430 { 430 {
431 struct async_icount *icount; 431 struct async_icount *icount;
432 432
433 if (new_lsr & SERIAL_LSR_BI) { 433 if (new_lsr & SERIAL_LSR_BI) {
434 /* 434 /*
435 * Parity and Framing errors only count if they 435 * Parity and Framing errors only count if they
436 * occur exclusive of a break being 436 * occur exclusive of a break being
437 * received. 437 * received.
438 */ 438 */
439 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI); 439 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
440 } 440 }
441 441
442 /* update input line counters */ 442 /* update input line counters */
443 icount = &port->icount; 443 icount = &port->icount;
444 if (new_lsr & SERIAL_LSR_BI) { 444 if (new_lsr & SERIAL_LSR_BI) {
445 icount->brk++; 445 icount->brk++;
446 smp_wmb(); 446 smp_wmb();
447 } 447 }
448 if (new_lsr & SERIAL_LSR_OE) { 448 if (new_lsr & SERIAL_LSR_OE) {
449 icount->overrun++; 449 icount->overrun++;
450 smp_wmb(); 450 smp_wmb();
451 } 451 }
452 if (new_lsr & SERIAL_LSR_PE) { 452 if (new_lsr & SERIAL_LSR_PE) {
453 icount->parity++; 453 icount->parity++;
454 smp_wmb(); 454 smp_wmb();
455 } 455 }
456 if (new_lsr & SERIAL_LSR_FE) { 456 if (new_lsr & SERIAL_LSR_FE) {
457 icount->frame++; 457 icount->frame++;
458 smp_wmb(); 458 smp_wmb();
459 } 459 }
460 } 460 }
461 461
462 /************************************************************************/ 462 /************************************************************************/
463 /************************************************************************/ 463 /************************************************************************/
464 /* U S B C A L L B A C K F U N C T I O N S */ 464 /* U S B C A L L B A C K F U N C T I O N S */
465 /* U S B C A L L B A C K F U N C T I O N S */ 465 /* U S B C A L L B A C K F U N C T I O N S */
466 /************************************************************************/ 466 /************************************************************************/
467 /************************************************************************/ 467 /************************************************************************/
468 468
469 static void mos7840_control_callback(struct urb *urb) 469 static void mos7840_control_callback(struct urb *urb)
470 { 470 {
471 unsigned char *data; 471 unsigned char *data;
472 struct moschip_port *mos7840_port; 472 struct moschip_port *mos7840_port;
473 struct device *dev = &urb->dev->dev; 473 struct device *dev = &urb->dev->dev;
474 __u8 regval = 0x0; 474 __u8 regval = 0x0;
475 int status = urb->status; 475 int status = urb->status;
476 476
477 mos7840_port = urb->context; 477 mos7840_port = urb->context;
478 478
479 switch (status) { 479 switch (status) {
480 case 0: 480 case 0:
481 /* success */ 481 /* success */
482 break; 482 break;
483 case -ECONNRESET: 483 case -ECONNRESET:
484 case -ENOENT: 484 case -ENOENT:
485 case -ESHUTDOWN: 485 case -ESHUTDOWN:
486 /* this urb is terminated, clean up */ 486 /* this urb is terminated, clean up */
487 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status); 487 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
488 return; 488 return;
489 default: 489 default:
490 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status); 490 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
491 return; 491 return;
492 } 492 }
493 493
494 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length); 494 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
495 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__, 495 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
496 mos7840_port->MsrLsr, mos7840_port->port_num); 496 mos7840_port->MsrLsr, mos7840_port->port_num);
497 data = urb->transfer_buffer; 497 data = urb->transfer_buffer;
498 regval = (__u8) data[0]; 498 regval = (__u8) data[0];
499 dev_dbg(dev, "%s data is %x\n", __func__, regval); 499 dev_dbg(dev, "%s data is %x\n", __func__, regval);
500 if (mos7840_port->MsrLsr == 0) 500 if (mos7840_port->MsrLsr == 0)
501 mos7840_handle_new_msr(mos7840_port, regval); 501 mos7840_handle_new_msr(mos7840_port, regval);
502 else if (mos7840_port->MsrLsr == 1) 502 else if (mos7840_port->MsrLsr == 1)
503 mos7840_handle_new_lsr(mos7840_port, regval); 503 mos7840_handle_new_lsr(mos7840_port, regval);
504 } 504 }
505 505
506 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg, 506 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
507 __u16 *val) 507 __u16 *val)
508 { 508 {
509 struct usb_device *dev = mcs->port->serial->dev; 509 struct usb_device *dev = mcs->port->serial->dev;
510 struct usb_ctrlrequest *dr = mcs->dr; 510 struct usb_ctrlrequest *dr = mcs->dr;
511 unsigned char *buffer = mcs->ctrl_buf; 511 unsigned char *buffer = mcs->ctrl_buf;
512 int ret; 512 int ret;
513 513
514 dr->bRequestType = MCS_RD_RTYPE; 514 dr->bRequestType = MCS_RD_RTYPE;
515 dr->bRequest = MCS_RDREQ; 515 dr->bRequest = MCS_RDREQ;
516 dr->wValue = cpu_to_le16(Wval); /* 0 */ 516 dr->wValue = cpu_to_le16(Wval); /* 0 */
517 dr->wIndex = cpu_to_le16(reg); 517 dr->wIndex = cpu_to_le16(reg);
518 dr->wLength = cpu_to_le16(2); 518 dr->wLength = cpu_to_le16(2);
519 519
520 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0), 520 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
521 (unsigned char *)dr, buffer, 2, 521 (unsigned char *)dr, buffer, 2,
522 mos7840_control_callback, mcs); 522 mos7840_control_callback, mcs);
523 mcs->control_urb->transfer_buffer_length = 2; 523 mcs->control_urb->transfer_buffer_length = 2;
524 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC); 524 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
525 return ret; 525 return ret;
526 } 526 }
527 527
528 static void mos7840_set_led_callback(struct urb *urb) 528 static void mos7840_set_led_callback(struct urb *urb)
529 { 529 {
530 switch (urb->status) { 530 switch (urb->status) {
531 case 0: 531 case 0:
532 /* Success */ 532 /* Success */
533 break; 533 break;
534 case -ECONNRESET: 534 case -ECONNRESET:
535 case -ENOENT: 535 case -ENOENT:
536 case -ESHUTDOWN: 536 case -ESHUTDOWN:
537 /* This urb is terminated, clean up */ 537 /* This urb is terminated, clean up */
538 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d", 538 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
539 __func__, urb->status); 539 __func__, urb->status);
540 break; 540 break;
541 default: 541 default:
542 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d", 542 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
543 __func__, urb->status); 543 __func__, urb->status);
544 } 544 }
545 } 545 }
546 546
547 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval, 547 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
548 __u16 reg) 548 __u16 reg)
549 { 549 {
550 struct usb_device *dev = mcs->port->serial->dev; 550 struct usb_device *dev = mcs->port->serial->dev;
551 struct usb_ctrlrequest *dr = mcs->dr; 551 struct usb_ctrlrequest *dr = mcs->dr;
552 552
553 dr->bRequestType = MCS_WR_RTYPE; 553 dr->bRequestType = MCS_WR_RTYPE;
554 dr->bRequest = MCS_WRREQ; 554 dr->bRequest = MCS_WRREQ;
555 dr->wValue = cpu_to_le16(wval); 555 dr->wValue = cpu_to_le16(wval);
556 dr->wIndex = cpu_to_le16(reg); 556 dr->wIndex = cpu_to_le16(reg);
557 dr->wLength = cpu_to_le16(0); 557 dr->wLength = cpu_to_le16(0);
558 558
559 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0), 559 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
560 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL); 560 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
561 561
562 usb_submit_urb(mcs->control_urb, GFP_ATOMIC); 562 usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
563 } 563 }
564 564
565 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg, 565 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
566 __u16 val) 566 __u16 val)
567 { 567 {
568 struct usb_device *dev = port->serial->dev; 568 struct usb_device *dev = port->serial->dev;
569 569
570 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE, 570 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
571 val, reg, NULL, 0, MOS_WDR_TIMEOUT); 571 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
572 } 572 }
573 573
574 static void mos7840_led_off(unsigned long arg) 574 static void mos7840_led_off(unsigned long arg)
575 { 575 {
576 struct moschip_port *mcs = (struct moschip_port *) arg; 576 struct moschip_port *mcs = (struct moschip_port *) arg;
577 577
578 /* Turn off LED */ 578 /* Turn off LED */
579 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER); 579 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
580 mod_timer(&mcs->led_timer2, 580 mod_timer(&mcs->led_timer2,
581 jiffies + msecs_to_jiffies(LED_OFF_MS)); 581 jiffies + msecs_to_jiffies(LED_OFF_MS));
582 } 582 }
583 583
584 static void mos7840_led_flag_off(unsigned long arg) 584 static void mos7840_led_flag_off(unsigned long arg)
585 { 585 {
586 struct moschip_port *mcs = (struct moschip_port *) arg; 586 struct moschip_port *mcs = (struct moschip_port *) arg;
587 587
588 mcs->led_flag = false; 588 mcs->led_flag = false;
589 } 589 }
590 590
591 /***************************************************************************** 591 /*****************************************************************************
592 * mos7840_interrupt_callback 592 * mos7840_interrupt_callback
593 * this is the callback function for when we have received data on the 593 * this is the callback function for when we have received data on the
594 * interrupt endpoint. 594 * interrupt endpoint.
595 *****************************************************************************/ 595 *****************************************************************************/
596 596
597 static void mos7840_interrupt_callback(struct urb *urb) 597 static void mos7840_interrupt_callback(struct urb *urb)
598 { 598 {
599 int result; 599 int result;
600 int length; 600 int length;
601 struct moschip_port *mos7840_port; 601 struct moschip_port *mos7840_port;
602 struct usb_serial *serial; 602 struct usb_serial *serial;
603 __u16 Data; 603 __u16 Data;
604 unsigned char *data; 604 unsigned char *data;
605 __u8 sp[5], st; 605 __u8 sp[5], st;
606 int i, rv = 0; 606 int i, rv = 0;
607 __u16 wval, wreg = 0; 607 __u16 wval, wreg = 0;
608 int status = urb->status; 608 int status = urb->status;
609 609
610 switch (status) { 610 switch (status) {
611 case 0: 611 case 0:
612 /* success */ 612 /* success */
613 break; 613 break;
614 case -ECONNRESET: 614 case -ECONNRESET:
615 case -ENOENT: 615 case -ENOENT:
616 case -ESHUTDOWN: 616 case -ESHUTDOWN:
617 /* this urb is terminated, clean up */ 617 /* this urb is terminated, clean up */
618 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", 618 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
619 __func__, status); 619 __func__, status);
620 return; 620 return;
621 default: 621 default:
622 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", 622 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
623 __func__, status); 623 __func__, status);
624 goto exit; 624 goto exit;
625 } 625 }
626 626
627 length = urb->actual_length; 627 length = urb->actual_length;
628 data = urb->transfer_buffer; 628 data = urb->transfer_buffer;
629 629
630 serial = urb->context; 630 serial = urb->context;
631 631
632 /* Moschip get 5 bytes 632 /* Moschip get 5 bytes
633 * Byte 1 IIR Port 1 (port.number is 0) 633 * Byte 1 IIR Port 1 (port.number is 0)
634 * Byte 2 IIR Port 2 (port.number is 1) 634 * Byte 2 IIR Port 2 (port.number is 1)
635 * Byte 3 IIR Port 3 (port.number is 2) 635 * Byte 3 IIR Port 3 (port.number is 2)
636 * Byte 4 IIR Port 4 (port.number is 3) 636 * Byte 4 IIR Port 4 (port.number is 3)
637 * Byte 5 FIFO status for both */ 637 * Byte 5 FIFO status for both */
638 638
639 if (length && length > 5) { 639 if (length && length > 5) {
640 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n"); 640 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
641 return; 641 return;
642 } 642 }
643 643
644 sp[0] = (__u8) data[0]; 644 sp[0] = (__u8) data[0];
645 sp[1] = (__u8) data[1]; 645 sp[1] = (__u8) data[1];
646 sp[2] = (__u8) data[2]; 646 sp[2] = (__u8) data[2];
647 sp[3] = (__u8) data[3]; 647 sp[3] = (__u8) data[3];
648 st = (__u8) data[4]; 648 st = (__u8) data[4];
649 649
650 for (i = 0; i < serial->num_ports; i++) { 650 for (i = 0; i < serial->num_ports; i++) {
651 mos7840_port = mos7840_get_port_private(serial->port[i]); 651 mos7840_port = mos7840_get_port_private(serial->port[i]);
652 wval = 652 wval =
653 (((__u16) serial->port[i]->number - 653 (((__u16) serial->port[i]->number -
654 (__u16) (serial->minor)) + 1) << 8; 654 (__u16) (serial->minor)) + 1) << 8;
655 if (mos7840_port->open) { 655 if (mos7840_port->open) {
656 if (sp[i] & 0x01) { 656 if (sp[i] & 0x01) {
657 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i); 657 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
658 } else { 658 } else {
659 switch (sp[i] & 0x0f) { 659 switch (sp[i] & 0x0f) {
660 case SERIAL_IIR_RLS: 660 case SERIAL_IIR_RLS:
661 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i); 661 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
662 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n"); 662 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
663 mos7840_port->MsrLsr = 1; 663 mos7840_port->MsrLsr = 1;
664 wreg = LINE_STATUS_REGISTER; 664 wreg = LINE_STATUS_REGISTER;
665 break; 665 break;
666 case SERIAL_IIR_MS: 666 case SERIAL_IIR_MS:
667 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i); 667 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
668 mos7840_port->MsrLsr = 0; 668 mos7840_port->MsrLsr = 0;
669 wreg = MODEM_STATUS_REGISTER; 669 wreg = MODEM_STATUS_REGISTER;
670 break; 670 break;
671 } 671 }
672 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data); 672 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
673 } 673 }
674 } 674 }
675 } 675 }
676 if (!(rv < 0)) 676 if (!(rv < 0))
677 /* the completion handler for the control urb will resubmit */ 677 /* the completion handler for the control urb will resubmit */
678 return; 678 return;
679 exit: 679 exit:
680 result = usb_submit_urb(urb, GFP_ATOMIC); 680 result = usb_submit_urb(urb, GFP_ATOMIC);
681 if (result) { 681 if (result) {
682 dev_err(&urb->dev->dev, 682 dev_err(&urb->dev->dev,
683 "%s - Error %d submitting interrupt urb\n", 683 "%s - Error %d submitting interrupt urb\n",
684 __func__, result); 684 __func__, result);
685 } 685 }
686 } 686 }
687 687
688 static int mos7840_port_paranoia_check(struct usb_serial_port *port, 688 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
689 const char *function) 689 const char *function)
690 { 690 {
691 if (!port) { 691 if (!port) {
692 pr_debug("%s - port == NULL\n", function); 692 pr_debug("%s - port == NULL\n", function);
693 return -1; 693 return -1;
694 } 694 }
695 if (!port->serial) { 695 if (!port->serial) {
696 pr_debug("%s - port->serial == NULL\n", function); 696 pr_debug("%s - port->serial == NULL\n", function);
697 return -1; 697 return -1;
698 } 698 }
699 699
700 return 0; 700 return 0;
701 } 701 }
702 702
703 /* Inline functions to check the sanity of a pointer that is passed to us */ 703 /* Inline functions to check the sanity of a pointer that is passed to us */
704 static int mos7840_serial_paranoia_check(struct usb_serial *serial, 704 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
705 const char *function) 705 const char *function)
706 { 706 {
707 if (!serial) { 707 if (!serial) {
708 pr_debug("%s - serial == NULL\n", function); 708 pr_debug("%s - serial == NULL\n", function);
709 return -1; 709 return -1;
710 } 710 }
711 if (!serial->type) { 711 if (!serial->type) {
712 pr_debug("%s - serial->type == NULL!\n", function); 712 pr_debug("%s - serial->type == NULL!\n", function);
713 return -1; 713 return -1;
714 } 714 }
715 715
716 return 0; 716 return 0;
717 } 717 }
718 718
719 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port, 719 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
720 const char *function) 720 const char *function)
721 { 721 {
722 /* if no port was specified, or it fails a paranoia check */ 722 /* if no port was specified, or it fails a paranoia check */
723 if (!port || 723 if (!port ||
724 mos7840_port_paranoia_check(port, function) || 724 mos7840_port_paranoia_check(port, function) ||
725 mos7840_serial_paranoia_check(port->serial, function)) { 725 mos7840_serial_paranoia_check(port->serial, function)) {
726 /* then say that we don't have a valid usb_serial thing, 726 /* then say that we don't have a valid usb_serial thing,
727 * which will end up genrating -ENODEV return values */ 727 * which will end up genrating -ENODEV return values */
728 return NULL; 728 return NULL;
729 } 729 }
730 730
731 return port->serial; 731 return port->serial;
732 } 732 }
733 733
734 /***************************************************************************** 734 /*****************************************************************************
735 * mos7840_bulk_in_callback 735 * mos7840_bulk_in_callback
736 * this is the callback function for when we have received data on the 736 * this is the callback function for when we have received data on the
737 * bulk in endpoint. 737 * bulk in endpoint.
738 *****************************************************************************/ 738 *****************************************************************************/
739 739
740 static void mos7840_bulk_in_callback(struct urb *urb) 740 static void mos7840_bulk_in_callback(struct urb *urb)
741 { 741 {
742 int retval; 742 int retval;
743 unsigned char *data; 743 unsigned char *data;
744 struct usb_serial *serial; 744 struct usb_serial *serial;
745 struct usb_serial_port *port; 745 struct usb_serial_port *port;
746 struct moschip_port *mos7840_port; 746 struct moschip_port *mos7840_port;
747 int status = urb->status; 747 int status = urb->status;
748 748
749 mos7840_port = urb->context; 749 mos7840_port = urb->context;
750 if (!mos7840_port) 750 if (!mos7840_port)
751 return; 751 return;
752 752
753 if (status) { 753 if (status) {
754 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status); 754 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
755 mos7840_port->read_urb_busy = false; 755 mos7840_port->read_urb_busy = false;
756 return; 756 return;
757 } 757 }
758 758
759 port = mos7840_port->port; 759 port = mos7840_port->port;
760 if (mos7840_port_paranoia_check(port, __func__)) { 760 if (mos7840_port_paranoia_check(port, __func__)) {
761 mos7840_port->read_urb_busy = false; 761 mos7840_port->read_urb_busy = false;
762 return; 762 return;
763 } 763 }
764 764
765 serial = mos7840_get_usb_serial(port, __func__); 765 serial = mos7840_get_usb_serial(port, __func__);
766 if (!serial) { 766 if (!serial) {
767 mos7840_port->read_urb_busy = false; 767 mos7840_port->read_urb_busy = false;
768 return; 768 return;
769 } 769 }
770 770
771 data = urb->transfer_buffer; 771 data = urb->transfer_buffer;
772 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data); 772 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
773 773
774 if (urb->actual_length) { 774 if (urb->actual_length) {
775 struct tty_port *tport = &mos7840_port->port->port; 775 struct tty_port *tport = &mos7840_port->port->port;
776 tty_insert_flip_string(tport, data, urb->actual_length); 776 tty_insert_flip_string(tport, data, urb->actual_length);
777 tty_flip_buffer_push(tport); 777 tty_flip_buffer_push(tport);
778 mos7840_port->icount.rx += urb->actual_length; 778 mos7840_port->icount.rx += urb->actual_length;
779 smp_wmb(); 779 smp_wmb();
780 dev_dbg(&port->dev, "mos7840_port->icount.rx is %d:\n", mos7840_port->icount.rx); 780 dev_dbg(&port->dev, "mos7840_port->icount.rx is %d:\n", mos7840_port->icount.rx);
781 } 781 }
782 782
783 if (!mos7840_port->read_urb) { 783 if (!mos7840_port->read_urb) {
784 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n"); 784 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
785 mos7840_port->read_urb_busy = false; 785 mos7840_port->read_urb_busy = false;
786 return; 786 return;
787 } 787 }
788 788
789 /* Turn on LED */ 789 /* Turn on LED */
790 if (mos7840_port->has_led && !mos7840_port->led_flag) { 790 if (mos7840_port->has_led && !mos7840_port->led_flag) {
791 mos7840_port->led_flag = true; 791 mos7840_port->led_flag = true;
792 mos7840_set_led_async(mos7840_port, 0x0301, 792 mos7840_set_led_async(mos7840_port, 0x0301,
793 MODEM_CONTROL_REGISTER); 793 MODEM_CONTROL_REGISTER);
794 mod_timer(&mos7840_port->led_timer1, 794 mod_timer(&mos7840_port->led_timer1,
795 jiffies + msecs_to_jiffies(LED_ON_MS)); 795 jiffies + msecs_to_jiffies(LED_ON_MS));
796 } 796 }
797 797
798 mos7840_port->read_urb_busy = true; 798 mos7840_port->read_urb_busy = true;
799 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); 799 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
800 800
801 if (retval) { 801 if (retval) {
802 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval); 802 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
803 mos7840_port->read_urb_busy = false; 803 mos7840_port->read_urb_busy = false;
804 } 804 }
805 } 805 }
806 806
807 /***************************************************************************** 807 /*****************************************************************************
808 * mos7840_bulk_out_data_callback 808 * mos7840_bulk_out_data_callback
809 * this is the callback function for when we have finished sending 809 * this is the callback function for when we have finished sending
810 * serial data on the bulk out endpoint. 810 * serial data on the bulk out endpoint.
811 *****************************************************************************/ 811 *****************************************************************************/
812 812
813 static void mos7840_bulk_out_data_callback(struct urb *urb) 813 static void mos7840_bulk_out_data_callback(struct urb *urb)
814 { 814 {
815 struct moschip_port *mos7840_port; 815 struct moschip_port *mos7840_port;
816 struct usb_serial_port *port; 816 struct usb_serial_port *port;
817 struct tty_struct *tty;
818 int status = urb->status; 817 int status = urb->status;
819 int i; 818 int i;
820 819
821 mos7840_port = urb->context; 820 mos7840_port = urb->context;
822 port = mos7840_port->port; 821 port = mos7840_port->port;
823 spin_lock(&mos7840_port->pool_lock); 822 spin_lock(&mos7840_port->pool_lock);
824 for (i = 0; i < NUM_URBS; i++) { 823 for (i = 0; i < NUM_URBS; i++) {
825 if (urb == mos7840_port->write_urb_pool[i]) { 824 if (urb == mos7840_port->write_urb_pool[i]) {
826 mos7840_port->busy[i] = 0; 825 mos7840_port->busy[i] = 0;
827 break; 826 break;
828 } 827 }
829 } 828 }
830 spin_unlock(&mos7840_port->pool_lock); 829 spin_unlock(&mos7840_port->pool_lock);
831 830
832 if (status) { 831 if (status) {
833 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status); 832 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
834 return; 833 return;
835 } 834 }
836 835
837 if (mos7840_port_paranoia_check(port, __func__)) 836 if (mos7840_port_paranoia_check(port, __func__))
838 return; 837 return;
839 838
840 tty = tty_port_tty_get(&port->port); 839 if (mos7840_port->open)
841 if (tty && mos7840_port->open) 840 tty_port_tty_wakeup(&port->port);
842 tty_wakeup(tty);
843 tty_kref_put(tty);
844 841
845 } 842 }
846 843
847 /************************************************************************/ 844 /************************************************************************/
848 /* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */ 845 /* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */
849 /************************************************************************/ 846 /************************************************************************/
850 #ifdef MCSSerialProbe 847 #ifdef MCSSerialProbe
851 static int mos7840_serial_probe(struct usb_serial *serial, 848 static int mos7840_serial_probe(struct usb_serial *serial,
852 const struct usb_device_id *id) 849 const struct usb_device_id *id)
853 { 850 {
854 851
855 /*need to implement the mode_reg reading and updating\ 852 /*need to implement the mode_reg reading and updating\
856 structures usb_serial_ device_type\ 853 structures usb_serial_ device_type\
857 (i.e num_ports, num_bulkin,bulkout etc) */ 854 (i.e num_ports, num_bulkin,bulkout etc) */
858 /* Also we can update the changes attach */ 855 /* Also we can update the changes attach */
859 return 1; 856 return 1;
860 } 857 }
861 #endif 858 #endif
862 859
863 /***************************************************************************** 860 /*****************************************************************************
864 * mos7840_open 861 * mos7840_open
865 * this function is called by the tty driver when a port is opened 862 * this function is called by the tty driver when a port is opened
866 * If successful, we return 0 863 * If successful, we return 0
867 * Otherwise we return a negative error number. 864 * Otherwise we return a negative error number.
868 *****************************************************************************/ 865 *****************************************************************************/
869 866
870 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port) 867 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
871 { 868 {
872 int response; 869 int response;
873 int j; 870 int j;
874 struct usb_serial *serial; 871 struct usb_serial *serial;
875 struct urb *urb; 872 struct urb *urb;
876 __u16 Data; 873 __u16 Data;
877 int status; 874 int status;
878 struct moschip_port *mos7840_port; 875 struct moschip_port *mos7840_port;
879 struct moschip_port *port0; 876 struct moschip_port *port0;
880 877
881 if (mos7840_port_paranoia_check(port, __func__)) 878 if (mos7840_port_paranoia_check(port, __func__))
882 return -ENODEV; 879 return -ENODEV;
883 880
884 serial = port->serial; 881 serial = port->serial;
885 882
886 if (mos7840_serial_paranoia_check(serial, __func__)) 883 if (mos7840_serial_paranoia_check(serial, __func__))
887 return -ENODEV; 884 return -ENODEV;
888 885
889 mos7840_port = mos7840_get_port_private(port); 886 mos7840_port = mos7840_get_port_private(port);
890 port0 = mos7840_get_port_private(serial->port[0]); 887 port0 = mos7840_get_port_private(serial->port[0]);
891 888
892 if (mos7840_port == NULL || port0 == NULL) 889 if (mos7840_port == NULL || port0 == NULL)
893 return -ENODEV; 890 return -ENODEV;
894 891
895 usb_clear_halt(serial->dev, port->write_urb->pipe); 892 usb_clear_halt(serial->dev, port->write_urb->pipe);
896 usb_clear_halt(serial->dev, port->read_urb->pipe); 893 usb_clear_halt(serial->dev, port->read_urb->pipe);
897 port0->open_ports++; 894 port0->open_ports++;
898 895
899 /* Initialising the write urb pool */ 896 /* Initialising the write urb pool */
900 for (j = 0; j < NUM_URBS; ++j) { 897 for (j = 0; j < NUM_URBS; ++j) {
901 urb = usb_alloc_urb(0, GFP_KERNEL); 898 urb = usb_alloc_urb(0, GFP_KERNEL);
902 mos7840_port->write_urb_pool[j] = urb; 899 mos7840_port->write_urb_pool[j] = urb;
903 900
904 if (urb == NULL) { 901 if (urb == NULL) {
905 dev_err(&port->dev, "No more urbs???\n"); 902 dev_err(&port->dev, "No more urbs???\n");
906 continue; 903 continue;
907 } 904 }
908 905
909 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, 906 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
910 GFP_KERNEL); 907 GFP_KERNEL);
911 if (!urb->transfer_buffer) { 908 if (!urb->transfer_buffer) {
912 usb_free_urb(urb); 909 usb_free_urb(urb);
913 mos7840_port->write_urb_pool[j] = NULL; 910 mos7840_port->write_urb_pool[j] = NULL;
914 dev_err(&port->dev, 911 dev_err(&port->dev,
915 "%s-out of memory for urb buffers.\n", 912 "%s-out of memory for urb buffers.\n",
916 __func__); 913 __func__);
917 continue; 914 continue;
918 } 915 }
919 } 916 }
920 917
921 /***************************************************************************** 918 /*****************************************************************************
922 * Initialize MCS7840 -- Write Init values to corresponding Registers 919 * Initialize MCS7840 -- Write Init values to corresponding Registers
923 * 920 *
924 * Register Index 921 * Register Index
925 * 1 : IER 922 * 1 : IER
926 * 2 : FCR 923 * 2 : FCR
927 * 3 : LCR 924 * 3 : LCR
928 * 4 : MCR 925 * 4 : MCR
929 * 926 *
930 * 0x08 : SP1/2 Control Reg 927 * 0x08 : SP1/2 Control Reg
931 *****************************************************************************/ 928 *****************************************************************************/
932 929
933 /* NEED to check the following Block */ 930 /* NEED to check the following Block */
934 931
935 Data = 0x0; 932 Data = 0x0;
936 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); 933 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
937 if (status < 0) { 934 if (status < 0) {
938 dev_dbg(&port->dev, "Reading Spreg failed\n"); 935 dev_dbg(&port->dev, "Reading Spreg failed\n");
939 return -1; 936 return -1;
940 } 937 }
941 Data |= 0x80; 938 Data |= 0x80;
942 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 939 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
943 if (status < 0) { 940 if (status < 0) {
944 dev_dbg(&port->dev, "writing Spreg failed\n"); 941 dev_dbg(&port->dev, "writing Spreg failed\n");
945 return -1; 942 return -1;
946 } 943 }
947 944
948 Data &= ~0x80; 945 Data &= ~0x80;
949 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 946 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
950 if (status < 0) { 947 if (status < 0) {
951 dev_dbg(&port->dev, "writing Spreg failed\n"); 948 dev_dbg(&port->dev, "writing Spreg failed\n");
952 return -1; 949 return -1;
953 } 950 }
954 /* End of block to be checked */ 951 /* End of block to be checked */
955 952
956 Data = 0x0; 953 Data = 0x0;
957 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 954 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
958 &Data); 955 &Data);
959 if (status < 0) { 956 if (status < 0) {
960 dev_dbg(&port->dev, "Reading Controlreg failed\n"); 957 dev_dbg(&port->dev, "Reading Controlreg failed\n");
961 return -1; 958 return -1;
962 } 959 }
963 Data |= 0x08; /* Driver done bit */ 960 Data |= 0x08; /* Driver done bit */
964 Data |= 0x20; /* rx_disable */ 961 Data |= 0x20; /* rx_disable */
965 status = mos7840_set_reg_sync(port, 962 status = mos7840_set_reg_sync(port,
966 mos7840_port->ControlRegOffset, Data); 963 mos7840_port->ControlRegOffset, Data);
967 if (status < 0) { 964 if (status < 0) {
968 dev_dbg(&port->dev, "writing Controlreg failed\n"); 965 dev_dbg(&port->dev, "writing Controlreg failed\n");
969 return -1; 966 return -1;
970 } 967 }
971 /* do register settings here */ 968 /* do register settings here */
972 /* Set all regs to the device default values. */ 969 /* Set all regs to the device default values. */
973 /*********************************** 970 /***********************************
974 * First Disable all interrupts. 971 * First Disable all interrupts.
975 ***********************************/ 972 ***********************************/
976 Data = 0x00; 973 Data = 0x00;
977 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 974 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
978 if (status < 0) { 975 if (status < 0) {
979 dev_dbg(&port->dev, "disabling interrupts failed\n"); 976 dev_dbg(&port->dev, "disabling interrupts failed\n");
980 return -1; 977 return -1;
981 } 978 }
982 /* Set FIFO_CONTROL_REGISTER to the default value */ 979 /* Set FIFO_CONTROL_REGISTER to the default value */
983 Data = 0x00; 980 Data = 0x00;
984 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 981 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
985 if (status < 0) { 982 if (status < 0) {
986 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n"); 983 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
987 return -1; 984 return -1;
988 } 985 }
989 986
990 Data = 0xcf; 987 Data = 0xcf;
991 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 988 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
992 if (status < 0) { 989 if (status < 0) {
993 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n"); 990 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
994 return -1; 991 return -1;
995 } 992 }
996 993
997 Data = 0x03; 994 Data = 0x03;
998 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 995 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
999 mos7840_port->shadowLCR = Data; 996 mos7840_port->shadowLCR = Data;
1000 997
1001 Data = 0x0b; 998 Data = 0x0b;
1002 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 999 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1003 mos7840_port->shadowMCR = Data; 1000 mos7840_port->shadowMCR = Data;
1004 1001
1005 Data = 0x00; 1002 Data = 0x00;
1006 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 1003 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1007 mos7840_port->shadowLCR = Data; 1004 mos7840_port->shadowLCR = Data;
1008 1005
1009 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */ 1006 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1010 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1007 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1011 1008
1012 Data = 0x0c; 1009 Data = 0x0c;
1013 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); 1010 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1014 1011
1015 Data = 0x0; 1012 Data = 0x0;
1016 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); 1013 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1017 1014
1018 Data = 0x00; 1015 Data = 0x00;
1019 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 1016 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1020 1017
1021 Data = Data & ~SERIAL_LCR_DLAB; 1018 Data = Data & ~SERIAL_LCR_DLAB;
1022 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1019 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1023 mos7840_port->shadowLCR = Data; 1020 mos7840_port->shadowLCR = Data;
1024 1021
1025 /* clearing Bulkin and Bulkout Fifo */ 1022 /* clearing Bulkin and Bulkout Fifo */
1026 Data = 0x0; 1023 Data = 0x0;
1027 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data); 1024 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1028 1025
1029 Data = Data | 0x0c; 1026 Data = Data | 0x0c;
1030 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 1027 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1031 1028
1032 Data = Data & ~0x0c; 1029 Data = Data & ~0x0c;
1033 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data); 1030 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1034 /* Finally enable all interrupts */ 1031 /* Finally enable all interrupts */
1035 Data = 0x0c; 1032 Data = 0x0c;
1036 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 1033 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1037 1034
1038 /* clearing rx_disable */ 1035 /* clearing rx_disable */
1039 Data = 0x0; 1036 Data = 0x0;
1040 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 1037 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1041 &Data); 1038 &Data);
1042 Data = Data & ~0x20; 1039 Data = Data & ~0x20;
1043 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, 1040 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1044 Data); 1041 Data);
1045 1042
1046 /* rx_negate */ 1043 /* rx_negate */
1047 Data = 0x0; 1044 Data = 0x0;
1048 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, 1045 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1049 &Data); 1046 &Data);
1050 Data = Data | 0x10; 1047 Data = Data | 0x10;
1051 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, 1048 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1052 Data); 1049 Data);
1053 1050
1054 /* Check to see if we've set up our endpoint info yet * 1051 /* Check to see if we've set up our endpoint info yet *
1055 * (can't set it up in mos7840_startup as the structures * 1052 * (can't set it up in mos7840_startup as the structures *
1056 * were not set up at that time.) */ 1053 * were not set up at that time.) */
1057 if (port0->open_ports == 1) { 1054 if (port0->open_ports == 1) {
1058 if (serial->port[0]->interrupt_in_buffer == NULL) { 1055 if (serial->port[0]->interrupt_in_buffer == NULL) {
1059 /* set up interrupt urb */ 1056 /* set up interrupt urb */
1060 usb_fill_int_urb(serial->port[0]->interrupt_in_urb, 1057 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1061 serial->dev, 1058 serial->dev,
1062 usb_rcvintpipe(serial->dev, 1059 usb_rcvintpipe(serial->dev,
1063 serial->port[0]->interrupt_in_endpointAddress), 1060 serial->port[0]->interrupt_in_endpointAddress),
1064 serial->port[0]->interrupt_in_buffer, 1061 serial->port[0]->interrupt_in_buffer,
1065 serial->port[0]->interrupt_in_urb-> 1062 serial->port[0]->interrupt_in_urb->
1066 transfer_buffer_length, 1063 transfer_buffer_length,
1067 mos7840_interrupt_callback, 1064 mos7840_interrupt_callback,
1068 serial, 1065 serial,
1069 serial->port[0]->interrupt_in_urb->interval); 1066 serial->port[0]->interrupt_in_urb->interval);
1070 1067
1071 /* start interrupt read for mos7840 * 1068 /* start interrupt read for mos7840 *
1072 * will continue as long as mos7840 is connected */ 1069 * will continue as long as mos7840 is connected */
1073 1070
1074 response = 1071 response =
1075 usb_submit_urb(serial->port[0]->interrupt_in_urb, 1072 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1076 GFP_KERNEL); 1073 GFP_KERNEL);
1077 if (response) { 1074 if (response) {
1078 dev_err(&port->dev, "%s - Error %d submitting " 1075 dev_err(&port->dev, "%s - Error %d submitting "
1079 "interrupt urb\n", __func__, response); 1076 "interrupt urb\n", __func__, response);
1080 } 1077 }
1081 1078
1082 } 1079 }
1083 1080
1084 } 1081 }
1085 1082
1086 /* see if we've set up our endpoint info yet * 1083 /* see if we've set up our endpoint info yet *
1087 * (can't set it up in mos7840_startup as the * 1084 * (can't set it up in mos7840_startup as the *
1088 * structures were not set up at that time.) */ 1085 * structures were not set up at that time.) */
1089 1086
1090 dev_dbg(&port->dev, "port number is %d\n", port->number); 1087 dev_dbg(&port->dev, "port number is %d\n", port->number);
1091 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor); 1088 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
1092 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress); 1089 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1093 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress); 1090 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1094 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress); 1091 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1095 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num); 1092 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
1096 mos7840_port->read_urb = port->read_urb; 1093 mos7840_port->read_urb = port->read_urb;
1097 1094
1098 /* set up our bulk in urb */ 1095 /* set up our bulk in urb */
1099 if ((serial->num_ports == 2) 1096 if ((serial->num_ports == 2)
1100 && ((((__u16)port->number - 1097 && ((((__u16)port->number -
1101 (__u16)(port->serial->minor)) % 2) != 0)) { 1098 (__u16)(port->serial->minor)) % 2) != 0)) {
1102 usb_fill_bulk_urb(mos7840_port->read_urb, 1099 usb_fill_bulk_urb(mos7840_port->read_urb,
1103 serial->dev, 1100 serial->dev,
1104 usb_rcvbulkpipe(serial->dev, 1101 usb_rcvbulkpipe(serial->dev,
1105 (port->bulk_in_endpointAddress) + 2), 1102 (port->bulk_in_endpointAddress) + 2),
1106 port->bulk_in_buffer, 1103 port->bulk_in_buffer,
1107 mos7840_port->read_urb->transfer_buffer_length, 1104 mos7840_port->read_urb->transfer_buffer_length,
1108 mos7840_bulk_in_callback, mos7840_port); 1105 mos7840_bulk_in_callback, mos7840_port);
1109 } else { 1106 } else {
1110 usb_fill_bulk_urb(mos7840_port->read_urb, 1107 usb_fill_bulk_urb(mos7840_port->read_urb,
1111 serial->dev, 1108 serial->dev,
1112 usb_rcvbulkpipe(serial->dev, 1109 usb_rcvbulkpipe(serial->dev,
1113 port->bulk_in_endpointAddress), 1110 port->bulk_in_endpointAddress),
1114 port->bulk_in_buffer, 1111 port->bulk_in_buffer,
1115 mos7840_port->read_urb->transfer_buffer_length, 1112 mos7840_port->read_urb->transfer_buffer_length,
1116 mos7840_bulk_in_callback, mos7840_port); 1113 mos7840_bulk_in_callback, mos7840_port);
1117 } 1114 }
1118 1115
1119 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress); 1116 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
1120 mos7840_port->read_urb_busy = true; 1117 mos7840_port->read_urb_busy = true;
1121 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL); 1118 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1122 if (response) { 1119 if (response) {
1123 dev_err(&port->dev, "%s - Error %d submitting control urb\n", 1120 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1124 __func__, response); 1121 __func__, response);
1125 mos7840_port->read_urb_busy = false; 1122 mos7840_port->read_urb_busy = false;
1126 } 1123 }
1127 1124
1128 /* initialize our wait queues */ 1125 /* initialize our wait queues */
1129 init_waitqueue_head(&mos7840_port->wait_chase); 1126 init_waitqueue_head(&mos7840_port->wait_chase);
1130 init_waitqueue_head(&mos7840_port->delta_msr_wait); 1127 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1131 1128
1132 /* initialize our icount structure */ 1129 /* initialize our icount structure */
1133 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount)); 1130 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1134 1131
1135 /* initialize our port settings */ 1132 /* initialize our port settings */
1136 /* Must set to enable ints! */ 1133 /* Must set to enable ints! */
1137 mos7840_port->shadowMCR = MCR_MASTER_IE; 1134 mos7840_port->shadowMCR = MCR_MASTER_IE;
1138 /* send a open port command */ 1135 /* send a open port command */
1139 mos7840_port->open = 1; 1136 mos7840_port->open = 1;
1140 /* mos7840_change_port_settings(mos7840_port,old_termios); */ 1137 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1141 mos7840_port->icount.tx = 0; 1138 mos7840_port->icount.tx = 0;
1142 mos7840_port->icount.rx = 0; 1139 mos7840_port->icount.rx = 0;
1143 1140
1144 return 0; 1141 return 0;
1145 } 1142 }
1146 1143
1147 /***************************************************************************** 1144 /*****************************************************************************
1148 * mos7840_chars_in_buffer 1145 * mos7840_chars_in_buffer
1149 * this function is called by the tty driver when it wants to know how many 1146 * this function is called by the tty driver when it wants to know how many
1150 * bytes of data we currently have outstanding in the port (data that has 1147 * bytes of data we currently have outstanding in the port (data that has
1151 * been written, but hasn't made it out the port yet) 1148 * been written, but hasn't made it out the port yet)
1152 * If successful, we return the number of bytes left to be written in the 1149 * If successful, we return the number of bytes left to be written in the
1153 * system, 1150 * system,
1154 * Otherwise we return zero. 1151 * Otherwise we return zero.
1155 *****************************************************************************/ 1152 *****************************************************************************/
1156 1153
1157 static int mos7840_chars_in_buffer(struct tty_struct *tty) 1154 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1158 { 1155 {
1159 struct usb_serial_port *port = tty->driver_data; 1156 struct usb_serial_port *port = tty->driver_data;
1160 int i; 1157 int i;
1161 int chars = 0; 1158 int chars = 0;
1162 unsigned long flags; 1159 unsigned long flags;
1163 struct moschip_port *mos7840_port; 1160 struct moschip_port *mos7840_port;
1164 1161
1165 if (mos7840_port_paranoia_check(port, __func__)) 1162 if (mos7840_port_paranoia_check(port, __func__))
1166 return 0; 1163 return 0;
1167 1164
1168 mos7840_port = mos7840_get_port_private(port); 1165 mos7840_port = mos7840_get_port_private(port);
1169 if (mos7840_port == NULL) 1166 if (mos7840_port == NULL)
1170 return 0; 1167 return 0;
1171 1168
1172 spin_lock_irqsave(&mos7840_port->pool_lock, flags); 1169 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1173 for (i = 0; i < NUM_URBS; ++i) { 1170 for (i = 0; i < NUM_URBS; ++i) {
1174 if (mos7840_port->busy[i]) { 1171 if (mos7840_port->busy[i]) {
1175 struct urb *urb = mos7840_port->write_urb_pool[i]; 1172 struct urb *urb = mos7840_port->write_urb_pool[i];
1176 chars += urb->transfer_buffer_length; 1173 chars += urb->transfer_buffer_length;
1177 } 1174 }
1178 } 1175 }
1179 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 1176 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1180 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); 1177 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1181 return chars; 1178 return chars;
1182 1179
1183 } 1180 }
1184 1181
1185 /***************************************************************************** 1182 /*****************************************************************************
1186 * mos7840_close 1183 * mos7840_close
1187 * this function is called by the tty driver when a port is closed 1184 * this function is called by the tty driver when a port is closed
1188 *****************************************************************************/ 1185 *****************************************************************************/
1189 1186
1190 static void mos7840_close(struct usb_serial_port *port) 1187 static void mos7840_close(struct usb_serial_port *port)
1191 { 1188 {
1192 struct usb_serial *serial; 1189 struct usb_serial *serial;
1193 struct moschip_port *mos7840_port; 1190 struct moschip_port *mos7840_port;
1194 struct moschip_port *port0; 1191 struct moschip_port *port0;
1195 int j; 1192 int j;
1196 __u16 Data; 1193 __u16 Data;
1197 1194
1198 if (mos7840_port_paranoia_check(port, __func__)) 1195 if (mos7840_port_paranoia_check(port, __func__))
1199 return; 1196 return;
1200 1197
1201 serial = mos7840_get_usb_serial(port, __func__); 1198 serial = mos7840_get_usb_serial(port, __func__);
1202 if (!serial) 1199 if (!serial)
1203 return; 1200 return;
1204 1201
1205 mos7840_port = mos7840_get_port_private(port); 1202 mos7840_port = mos7840_get_port_private(port);
1206 port0 = mos7840_get_port_private(serial->port[0]); 1203 port0 = mos7840_get_port_private(serial->port[0]);
1207 1204
1208 if (mos7840_port == NULL || port0 == NULL) 1205 if (mos7840_port == NULL || port0 == NULL)
1209 return; 1206 return;
1210 1207
1211 for (j = 0; j < NUM_URBS; ++j) 1208 for (j = 0; j < NUM_URBS; ++j)
1212 usb_kill_urb(mos7840_port->write_urb_pool[j]); 1209 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1213 1210
1214 /* Freeing Write URBs */ 1211 /* Freeing Write URBs */
1215 for (j = 0; j < NUM_URBS; ++j) { 1212 for (j = 0; j < NUM_URBS; ++j) {
1216 if (mos7840_port->write_urb_pool[j]) { 1213 if (mos7840_port->write_urb_pool[j]) {
1217 if (mos7840_port->write_urb_pool[j]->transfer_buffer) 1214 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1218 kfree(mos7840_port->write_urb_pool[j]-> 1215 kfree(mos7840_port->write_urb_pool[j]->
1219 transfer_buffer); 1216 transfer_buffer);
1220 1217
1221 usb_free_urb(mos7840_port->write_urb_pool[j]); 1218 usb_free_urb(mos7840_port->write_urb_pool[j]);
1222 } 1219 }
1223 } 1220 }
1224 1221
1225 /* While closing port, shutdown all bulk read, write * 1222 /* While closing port, shutdown all bulk read, write *
1226 * and interrupt read if they exists */ 1223 * and interrupt read if they exists */
1227 if (serial->dev) { 1224 if (serial->dev) {
1228 if (mos7840_port->write_urb) { 1225 if (mos7840_port->write_urb) {
1229 dev_dbg(&port->dev, "%s", "Shutdown bulk write\n"); 1226 dev_dbg(&port->dev, "%s", "Shutdown bulk write\n");
1230 usb_kill_urb(mos7840_port->write_urb); 1227 usb_kill_urb(mos7840_port->write_urb);
1231 } 1228 }
1232 if (mos7840_port->read_urb) { 1229 if (mos7840_port->read_urb) {
1233 dev_dbg(&port->dev, "%s", "Shutdown bulk read\n"); 1230 dev_dbg(&port->dev, "%s", "Shutdown bulk read\n");
1234 usb_kill_urb(mos7840_port->read_urb); 1231 usb_kill_urb(mos7840_port->read_urb);
1235 mos7840_port->read_urb_busy = false; 1232 mos7840_port->read_urb_busy = false;
1236 } 1233 }
1237 if ((&mos7840_port->control_urb)) { 1234 if ((&mos7840_port->control_urb)) {
1238 dev_dbg(&port->dev, "%s", "Shutdown control read\n"); 1235 dev_dbg(&port->dev, "%s", "Shutdown control read\n");
1239 /*/ usb_kill_urb (mos7840_port->control_urb); */ 1236 /*/ usb_kill_urb (mos7840_port->control_urb); */
1240 } 1237 }
1241 } 1238 }
1242 /* if(mos7840_port->ctrl_buf != NULL) */ 1239 /* if(mos7840_port->ctrl_buf != NULL) */
1243 /* kfree(mos7840_port->ctrl_buf); */ 1240 /* kfree(mos7840_port->ctrl_buf); */
1244 port0->open_ports--; 1241 port0->open_ports--;
1245 dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number); 1242 dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number);
1246 if (port0->open_ports == 0) { 1243 if (port0->open_ports == 0) {
1247 if (serial->port[0]->interrupt_in_urb) { 1244 if (serial->port[0]->interrupt_in_urb) {
1248 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n"); 1245 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1249 usb_kill_urb(serial->port[0]->interrupt_in_urb); 1246 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1250 } 1247 }
1251 } 1248 }
1252 1249
1253 if (mos7840_port->write_urb) { 1250 if (mos7840_port->write_urb) {
1254 /* if this urb had a transfer buffer already (old tx) free it */ 1251 /* if this urb had a transfer buffer already (old tx) free it */
1255 if (mos7840_port->write_urb->transfer_buffer != NULL) 1252 if (mos7840_port->write_urb->transfer_buffer != NULL)
1256 kfree(mos7840_port->write_urb->transfer_buffer); 1253 kfree(mos7840_port->write_urb->transfer_buffer);
1257 usb_free_urb(mos7840_port->write_urb); 1254 usb_free_urb(mos7840_port->write_urb);
1258 } 1255 }
1259 1256
1260 Data = 0x0; 1257 Data = 0x0;
1261 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 1258 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1262 1259
1263 Data = 0x00; 1260 Data = 0x00;
1264 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 1261 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1265 1262
1266 mos7840_port->open = 0; 1263 mos7840_port->open = 0;
1267 } 1264 }
1268 1265
1269 /************************************************************************ 1266 /************************************************************************
1270 * 1267 *
1271 * mos7840_block_until_chase_response 1268 * mos7840_block_until_chase_response
1272 * 1269 *
1273 * This function will block the close until one of the following: 1270 * This function will block the close until one of the following:
1274 * 1. Response to our Chase comes from mos7840 1271 * 1. Response to our Chase comes from mos7840
1275 * 2. A timeout of 10 seconds without activity has expired 1272 * 2. A timeout of 10 seconds without activity has expired
1276 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty) 1273 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1277 * 1274 *
1278 ************************************************************************/ 1275 ************************************************************************/
1279 1276
1280 static void mos7840_block_until_chase_response(struct tty_struct *tty, 1277 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1281 struct moschip_port *mos7840_port) 1278 struct moschip_port *mos7840_port)
1282 { 1279 {
1283 int timeout = msecs_to_jiffies(1000); 1280 int timeout = msecs_to_jiffies(1000);
1284 int wait = 10; 1281 int wait = 10;
1285 int count; 1282 int count;
1286 1283
1287 while (1) { 1284 while (1) {
1288 count = mos7840_chars_in_buffer(tty); 1285 count = mos7840_chars_in_buffer(tty);
1289 1286
1290 /* Check for Buffer status */ 1287 /* Check for Buffer status */
1291 if (count <= 0) 1288 if (count <= 0)
1292 return; 1289 return;
1293 1290
1294 /* Block the thread for a while */ 1291 /* Block the thread for a while */
1295 interruptible_sleep_on_timeout(&mos7840_port->wait_chase, 1292 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1296 timeout); 1293 timeout);
1297 /* No activity.. count down section */ 1294 /* No activity.. count down section */
1298 wait--; 1295 wait--;
1299 if (wait == 0) { 1296 if (wait == 0) {
1300 dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__); 1297 dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
1301 return; 1298 return;
1302 } else { 1299 } else {
1303 /* Reset timeout value back to seconds */ 1300 /* Reset timeout value back to seconds */
1304 wait = 10; 1301 wait = 10;
1305 } 1302 }
1306 } 1303 }
1307 1304
1308 } 1305 }
1309 1306
1310 /***************************************************************************** 1307 /*****************************************************************************
1311 * mos7840_break 1308 * mos7840_break
1312 * this function sends a break to the port 1309 * this function sends a break to the port
1313 *****************************************************************************/ 1310 *****************************************************************************/
1314 static void mos7840_break(struct tty_struct *tty, int break_state) 1311 static void mos7840_break(struct tty_struct *tty, int break_state)
1315 { 1312 {
1316 struct usb_serial_port *port = tty->driver_data; 1313 struct usb_serial_port *port = tty->driver_data;
1317 unsigned char data; 1314 unsigned char data;
1318 struct usb_serial *serial; 1315 struct usb_serial *serial;
1319 struct moschip_port *mos7840_port; 1316 struct moschip_port *mos7840_port;
1320 1317
1321 if (mos7840_port_paranoia_check(port, __func__)) 1318 if (mos7840_port_paranoia_check(port, __func__))
1322 return; 1319 return;
1323 1320
1324 serial = mos7840_get_usb_serial(port, __func__); 1321 serial = mos7840_get_usb_serial(port, __func__);
1325 if (!serial) 1322 if (!serial)
1326 return; 1323 return;
1327 1324
1328 mos7840_port = mos7840_get_port_private(port); 1325 mos7840_port = mos7840_get_port_private(port);
1329 1326
1330 if (mos7840_port == NULL) 1327 if (mos7840_port == NULL)
1331 return; 1328 return;
1332 1329
1333 if (serial->dev) 1330 if (serial->dev)
1334 /* flush and block until tx is empty */ 1331 /* flush and block until tx is empty */
1335 mos7840_block_until_chase_response(tty, mos7840_port); 1332 mos7840_block_until_chase_response(tty, mos7840_port);
1336 1333
1337 if (break_state == -1) 1334 if (break_state == -1)
1338 data = mos7840_port->shadowLCR | LCR_SET_BREAK; 1335 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1339 else 1336 else
1340 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK; 1337 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1341 1338
1342 /* FIXME: no locking on shadowLCR anywhere in driver */ 1339 /* FIXME: no locking on shadowLCR anywhere in driver */
1343 mos7840_port->shadowLCR = data; 1340 mos7840_port->shadowLCR = data;
1344 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); 1341 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1345 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, 1342 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1346 mos7840_port->shadowLCR); 1343 mos7840_port->shadowLCR);
1347 } 1344 }
1348 1345
1349 /***************************************************************************** 1346 /*****************************************************************************
1350 * mos7840_write_room 1347 * mos7840_write_room
1351 * this function is called by the tty driver when it wants to know how many 1348 * this function is called by the tty driver when it wants to know how many
1352 * bytes of data we can accept for a specific port. 1349 * bytes of data we can accept for a specific port.
1353 * If successful, we return the amount of room that we have for this port 1350 * If successful, we return the amount of room that we have for this port
1354 * Otherwise we return a negative error number. 1351 * Otherwise we return a negative error number.
1355 *****************************************************************************/ 1352 *****************************************************************************/
1356 1353
1357 static int mos7840_write_room(struct tty_struct *tty) 1354 static int mos7840_write_room(struct tty_struct *tty)
1358 { 1355 {
1359 struct usb_serial_port *port = tty->driver_data; 1356 struct usb_serial_port *port = tty->driver_data;
1360 int i; 1357 int i;
1361 int room = 0; 1358 int room = 0;
1362 unsigned long flags; 1359 unsigned long flags;
1363 struct moschip_port *mos7840_port; 1360 struct moschip_port *mos7840_port;
1364 1361
1365 if (mos7840_port_paranoia_check(port, __func__)) 1362 if (mos7840_port_paranoia_check(port, __func__))
1366 return -1; 1363 return -1;
1367 1364
1368 mos7840_port = mos7840_get_port_private(port); 1365 mos7840_port = mos7840_get_port_private(port);
1369 if (mos7840_port == NULL) 1366 if (mos7840_port == NULL)
1370 return -1; 1367 return -1;
1371 1368
1372 spin_lock_irqsave(&mos7840_port->pool_lock, flags); 1369 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1373 for (i = 0; i < NUM_URBS; ++i) { 1370 for (i = 0; i < NUM_URBS; ++i) {
1374 if (!mos7840_port->busy[i]) 1371 if (!mos7840_port->busy[i])
1375 room += URB_TRANSFER_BUFFER_SIZE; 1372 room += URB_TRANSFER_BUFFER_SIZE;
1376 } 1373 }
1377 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 1374 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1378 1375
1379 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1; 1376 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1380 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room); 1377 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
1381 return room; 1378 return room;
1382 1379
1383 } 1380 }
1384 1381
1385 /***************************************************************************** 1382 /*****************************************************************************
1386 * mos7840_write 1383 * mos7840_write
1387 * this function is called by the tty driver when data should be written to 1384 * this function is called by the tty driver when data should be written to
1388 * the port. 1385 * the port.
1389 * If successful, we return the number of bytes written, otherwise we 1386 * If successful, we return the number of bytes written, otherwise we
1390 * return a negative error number. 1387 * return a negative error number.
1391 *****************************************************************************/ 1388 *****************************************************************************/
1392 1389
1393 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port, 1390 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1394 const unsigned char *data, int count) 1391 const unsigned char *data, int count)
1395 { 1392 {
1396 int status; 1393 int status;
1397 int i; 1394 int i;
1398 int bytes_sent = 0; 1395 int bytes_sent = 0;
1399 int transfer_size; 1396 int transfer_size;
1400 unsigned long flags; 1397 unsigned long flags;
1401 1398
1402 struct moschip_port *mos7840_port; 1399 struct moschip_port *mos7840_port;
1403 struct usb_serial *serial; 1400 struct usb_serial *serial;
1404 struct urb *urb; 1401 struct urb *urb;
1405 /* __u16 Data; */ 1402 /* __u16 Data; */
1406 const unsigned char *current_position = data; 1403 const unsigned char *current_position = data;
1407 unsigned char *data1; 1404 unsigned char *data1;
1408 1405
1409 #ifdef NOTMOS7840 1406 #ifdef NOTMOS7840
1410 Data = 0x00; 1407 Data = 0x00;
1411 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data); 1408 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1412 mos7840_port->shadowLCR = Data; 1409 mos7840_port->shadowLCR = Data;
1413 dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data); 1410 dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
1414 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); 1411 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1415 1412
1416 /* Data = 0x03; */ 1413 /* Data = 0x03; */
1417 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */ 1414 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1418 /* mos7840_port->shadowLCR=Data;//Need to add later */ 1415 /* mos7840_port->shadowLCR=Data;//Need to add later */
1419 1416
1420 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */ 1417 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1421 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1418 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1422 1419
1423 /* Data = 0x0c; */ 1420 /* Data = 0x0c; */
1424 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */ 1421 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1425 Data = 0x00; 1422 Data = 0x00;
1426 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data); 1423 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1427 dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data); 1424 dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
1428 1425
1429 Data = 0x0; 1426 Data = 0x0;
1430 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data); 1427 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1431 dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data); 1428 dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
1432 1429
1433 Data = Data & ~SERIAL_LCR_DLAB; 1430 Data = Data & ~SERIAL_LCR_DLAB;
1434 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR); 1431 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1435 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1432 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1436 #endif 1433 #endif
1437 1434
1438 if (mos7840_port_paranoia_check(port, __func__)) 1435 if (mos7840_port_paranoia_check(port, __func__))
1439 return -1; 1436 return -1;
1440 1437
1441 serial = port->serial; 1438 serial = port->serial;
1442 if (mos7840_serial_paranoia_check(serial, __func__)) 1439 if (mos7840_serial_paranoia_check(serial, __func__))
1443 return -1; 1440 return -1;
1444 1441
1445 mos7840_port = mos7840_get_port_private(port); 1442 mos7840_port = mos7840_get_port_private(port);
1446 if (mos7840_port == NULL) 1443 if (mos7840_port == NULL)
1447 return -1; 1444 return -1;
1448 1445
1449 /* try to find a free urb in the list */ 1446 /* try to find a free urb in the list */
1450 urb = NULL; 1447 urb = NULL;
1451 1448
1452 spin_lock_irqsave(&mos7840_port->pool_lock, flags); 1449 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1453 for (i = 0; i < NUM_URBS; ++i) { 1450 for (i = 0; i < NUM_URBS; ++i) {
1454 if (!mos7840_port->busy[i]) { 1451 if (!mos7840_port->busy[i]) {
1455 mos7840_port->busy[i] = 1; 1452 mos7840_port->busy[i] = 1;
1456 urb = mos7840_port->write_urb_pool[i]; 1453 urb = mos7840_port->write_urb_pool[i];
1457 dev_dbg(&port->dev, "URB:%d\n", i); 1454 dev_dbg(&port->dev, "URB:%d\n", i);
1458 break; 1455 break;
1459 } 1456 }
1460 } 1457 }
1461 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags); 1458 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1462 1459
1463 if (urb == NULL) { 1460 if (urb == NULL) {
1464 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__); 1461 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1465 goto exit; 1462 goto exit;
1466 } 1463 }
1467 1464
1468 if (urb->transfer_buffer == NULL) { 1465 if (urb->transfer_buffer == NULL) {
1469 urb->transfer_buffer = 1466 urb->transfer_buffer =
1470 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL); 1467 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1471 1468
1472 if (urb->transfer_buffer == NULL) { 1469 if (urb->transfer_buffer == NULL) {
1473 dev_err_console(port, "%s no more kernel memory...\n", 1470 dev_err_console(port, "%s no more kernel memory...\n",
1474 __func__); 1471 __func__);
1475 goto exit; 1472 goto exit;
1476 } 1473 }
1477 } 1474 }
1478 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE); 1475 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1479 1476
1480 memcpy(urb->transfer_buffer, current_position, transfer_size); 1477 memcpy(urb->transfer_buffer, current_position, transfer_size);
1481 1478
1482 /* fill urb with data and submit */ 1479 /* fill urb with data and submit */
1483 if ((serial->num_ports == 2) 1480 if ((serial->num_ports == 2)
1484 && ((((__u16)port->number - 1481 && ((((__u16)port->number -
1485 (__u16)(port->serial->minor)) % 2) != 0)) { 1482 (__u16)(port->serial->minor)) % 2) != 0)) {
1486 usb_fill_bulk_urb(urb, 1483 usb_fill_bulk_urb(urb,
1487 serial->dev, 1484 serial->dev,
1488 usb_sndbulkpipe(serial->dev, 1485 usb_sndbulkpipe(serial->dev,
1489 (port->bulk_out_endpointAddress) + 2), 1486 (port->bulk_out_endpointAddress) + 2),
1490 urb->transfer_buffer, 1487 urb->transfer_buffer,
1491 transfer_size, 1488 transfer_size,
1492 mos7840_bulk_out_data_callback, mos7840_port); 1489 mos7840_bulk_out_data_callback, mos7840_port);
1493 } else { 1490 } else {
1494 usb_fill_bulk_urb(urb, 1491 usb_fill_bulk_urb(urb,
1495 serial->dev, 1492 serial->dev,
1496 usb_sndbulkpipe(serial->dev, 1493 usb_sndbulkpipe(serial->dev,
1497 port->bulk_out_endpointAddress), 1494 port->bulk_out_endpointAddress),
1498 urb->transfer_buffer, 1495 urb->transfer_buffer,
1499 transfer_size, 1496 transfer_size,
1500 mos7840_bulk_out_data_callback, mos7840_port); 1497 mos7840_bulk_out_data_callback, mos7840_port);
1501 } 1498 }
1502 1499
1503 data1 = urb->transfer_buffer; 1500 data1 = urb->transfer_buffer;
1504 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress); 1501 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
1505 1502
1506 /* Turn on LED */ 1503 /* Turn on LED */
1507 if (mos7840_port->has_led && !mos7840_port->led_flag) { 1504 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1508 mos7840_port->led_flag = true; 1505 mos7840_port->led_flag = true;
1509 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301); 1506 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1510 mod_timer(&mos7840_port->led_timer1, 1507 mod_timer(&mos7840_port->led_timer1,
1511 jiffies + msecs_to_jiffies(LED_ON_MS)); 1508 jiffies + msecs_to_jiffies(LED_ON_MS));
1512 } 1509 }
1513 1510
1514 /* send it down the pipe */ 1511 /* send it down the pipe */
1515 status = usb_submit_urb(urb, GFP_ATOMIC); 1512 status = usb_submit_urb(urb, GFP_ATOMIC);
1516 1513
1517 if (status) { 1514 if (status) {
1518 mos7840_port->busy[i] = 0; 1515 mos7840_port->busy[i] = 0;
1519 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed " 1516 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1520 "with status = %d\n", __func__, status); 1517 "with status = %d\n", __func__, status);
1521 bytes_sent = status; 1518 bytes_sent = status;
1522 goto exit; 1519 goto exit;
1523 } 1520 }
1524 bytes_sent = transfer_size; 1521 bytes_sent = transfer_size;
1525 mos7840_port->icount.tx += transfer_size; 1522 mos7840_port->icount.tx += transfer_size;
1526 smp_wmb(); 1523 smp_wmb();
1527 dev_dbg(&port->dev, "mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx); 1524 dev_dbg(&port->dev, "mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1528 exit: 1525 exit:
1529 return bytes_sent; 1526 return bytes_sent;
1530 1527
1531 } 1528 }
1532 1529
1533 /***************************************************************************** 1530 /*****************************************************************************
1534 * mos7840_throttle 1531 * mos7840_throttle
1535 * this function is called by the tty driver when it wants to stop the data 1532 * this function is called by the tty driver when it wants to stop the data
1536 * being read from the port. 1533 * being read from the port.
1537 *****************************************************************************/ 1534 *****************************************************************************/
1538 1535
1539 static void mos7840_throttle(struct tty_struct *tty) 1536 static void mos7840_throttle(struct tty_struct *tty)
1540 { 1537 {
1541 struct usb_serial_port *port = tty->driver_data; 1538 struct usb_serial_port *port = tty->driver_data;
1542 struct moschip_port *mos7840_port; 1539 struct moschip_port *mos7840_port;
1543 int status; 1540 int status;
1544 1541
1545 if (mos7840_port_paranoia_check(port, __func__)) 1542 if (mos7840_port_paranoia_check(port, __func__))
1546 return; 1543 return;
1547 1544
1548 mos7840_port = mos7840_get_port_private(port); 1545 mos7840_port = mos7840_get_port_private(port);
1549 1546
1550 if (mos7840_port == NULL) 1547 if (mos7840_port == NULL)
1551 return; 1548 return;
1552 1549
1553 if (!mos7840_port->open) { 1550 if (!mos7840_port->open) {
1554 dev_dbg(&port->dev, "%s", "port not opened\n"); 1551 dev_dbg(&port->dev, "%s", "port not opened\n");
1555 return; 1552 return;
1556 } 1553 }
1557 1554
1558 /* if we are implementing XON/XOFF, send the stop character */ 1555 /* if we are implementing XON/XOFF, send the stop character */
1559 if (I_IXOFF(tty)) { 1556 if (I_IXOFF(tty)) {
1560 unsigned char stop_char = STOP_CHAR(tty); 1557 unsigned char stop_char = STOP_CHAR(tty);
1561 status = mos7840_write(tty, port, &stop_char, 1); 1558 status = mos7840_write(tty, port, &stop_char, 1);
1562 if (status <= 0) 1559 if (status <= 0)
1563 return; 1560 return;
1564 } 1561 }
1565 /* if we are implementing RTS/CTS, toggle that line */ 1562 /* if we are implementing RTS/CTS, toggle that line */
1566 if (tty->termios.c_cflag & CRTSCTS) { 1563 if (tty->termios.c_cflag & CRTSCTS) {
1567 mos7840_port->shadowMCR &= ~MCR_RTS; 1564 mos7840_port->shadowMCR &= ~MCR_RTS;
1568 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1565 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1569 mos7840_port->shadowMCR); 1566 mos7840_port->shadowMCR);
1570 if (status < 0) 1567 if (status < 0)
1571 return; 1568 return;
1572 } 1569 }
1573 } 1570 }
1574 1571
1575 /***************************************************************************** 1572 /*****************************************************************************
1576 * mos7840_unthrottle 1573 * mos7840_unthrottle
1577 * this function is called by the tty driver when it wants to resume 1574 * this function is called by the tty driver when it wants to resume
1578 * the data being read from the port (called after mos7840_throttle is 1575 * the data being read from the port (called after mos7840_throttle is
1579 * called) 1576 * called)
1580 *****************************************************************************/ 1577 *****************************************************************************/
1581 static void mos7840_unthrottle(struct tty_struct *tty) 1578 static void mos7840_unthrottle(struct tty_struct *tty)
1582 { 1579 {
1583 struct usb_serial_port *port = tty->driver_data; 1580 struct usb_serial_port *port = tty->driver_data;
1584 int status; 1581 int status;
1585 struct moschip_port *mos7840_port = mos7840_get_port_private(port); 1582 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1586 1583
1587 if (mos7840_port_paranoia_check(port, __func__)) 1584 if (mos7840_port_paranoia_check(port, __func__))
1588 return; 1585 return;
1589 1586
1590 if (mos7840_port == NULL) 1587 if (mos7840_port == NULL)
1591 return; 1588 return;
1592 1589
1593 if (!mos7840_port->open) { 1590 if (!mos7840_port->open) {
1594 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1591 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1595 return; 1592 return;
1596 } 1593 }
1597 1594
1598 /* if we are implementing XON/XOFF, send the start character */ 1595 /* if we are implementing XON/XOFF, send the start character */
1599 if (I_IXOFF(tty)) { 1596 if (I_IXOFF(tty)) {
1600 unsigned char start_char = START_CHAR(tty); 1597 unsigned char start_char = START_CHAR(tty);
1601 status = mos7840_write(tty, port, &start_char, 1); 1598 status = mos7840_write(tty, port, &start_char, 1);
1602 if (status <= 0) 1599 if (status <= 0)
1603 return; 1600 return;
1604 } 1601 }
1605 1602
1606 /* if we are implementing RTS/CTS, toggle that line */ 1603 /* if we are implementing RTS/CTS, toggle that line */
1607 if (tty->termios.c_cflag & CRTSCTS) { 1604 if (tty->termios.c_cflag & CRTSCTS) {
1608 mos7840_port->shadowMCR |= MCR_RTS; 1605 mos7840_port->shadowMCR |= MCR_RTS;
1609 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1606 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1610 mos7840_port->shadowMCR); 1607 mos7840_port->shadowMCR);
1611 if (status < 0) 1608 if (status < 0)
1612 return; 1609 return;
1613 } 1610 }
1614 } 1611 }
1615 1612
1616 static int mos7840_tiocmget(struct tty_struct *tty) 1613 static int mos7840_tiocmget(struct tty_struct *tty)
1617 { 1614 {
1618 struct usb_serial_port *port = tty->driver_data; 1615 struct usb_serial_port *port = tty->driver_data;
1619 struct moschip_port *mos7840_port; 1616 struct moschip_port *mos7840_port;
1620 unsigned int result; 1617 unsigned int result;
1621 __u16 msr; 1618 __u16 msr;
1622 __u16 mcr; 1619 __u16 mcr;
1623 int status; 1620 int status;
1624 mos7840_port = mos7840_get_port_private(port); 1621 mos7840_port = mos7840_get_port_private(port);
1625 1622
1626 if (mos7840_port == NULL) 1623 if (mos7840_port == NULL)
1627 return -ENODEV; 1624 return -ENODEV;
1628 1625
1629 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr); 1626 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1630 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr); 1627 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1631 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) 1628 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1632 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0) 1629 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1633 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0) 1630 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1634 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) 1631 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1635 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) 1632 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1636 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) 1633 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1637 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); 1634 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1638 1635
1639 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result); 1636 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1640 1637
1641 return result; 1638 return result;
1642 } 1639 }
1643 1640
1644 static int mos7840_tiocmset(struct tty_struct *tty, 1641 static int mos7840_tiocmset(struct tty_struct *tty,
1645 unsigned int set, unsigned int clear) 1642 unsigned int set, unsigned int clear)
1646 { 1643 {
1647 struct usb_serial_port *port = tty->driver_data; 1644 struct usb_serial_port *port = tty->driver_data;
1648 struct moschip_port *mos7840_port; 1645 struct moschip_port *mos7840_port;
1649 unsigned int mcr; 1646 unsigned int mcr;
1650 int status; 1647 int status;
1651 1648
1652 mos7840_port = mos7840_get_port_private(port); 1649 mos7840_port = mos7840_get_port_private(port);
1653 1650
1654 if (mos7840_port == NULL) 1651 if (mos7840_port == NULL)
1655 return -ENODEV; 1652 return -ENODEV;
1656 1653
1657 /* FIXME: What locks the port registers ? */ 1654 /* FIXME: What locks the port registers ? */
1658 mcr = mos7840_port->shadowMCR; 1655 mcr = mos7840_port->shadowMCR;
1659 if (clear & TIOCM_RTS) 1656 if (clear & TIOCM_RTS)
1660 mcr &= ~MCR_RTS; 1657 mcr &= ~MCR_RTS;
1661 if (clear & TIOCM_DTR) 1658 if (clear & TIOCM_DTR)
1662 mcr &= ~MCR_DTR; 1659 mcr &= ~MCR_DTR;
1663 if (clear & TIOCM_LOOP) 1660 if (clear & TIOCM_LOOP)
1664 mcr &= ~MCR_LOOPBACK; 1661 mcr &= ~MCR_LOOPBACK;
1665 1662
1666 if (set & TIOCM_RTS) 1663 if (set & TIOCM_RTS)
1667 mcr |= MCR_RTS; 1664 mcr |= MCR_RTS;
1668 if (set & TIOCM_DTR) 1665 if (set & TIOCM_DTR)
1669 mcr |= MCR_DTR; 1666 mcr |= MCR_DTR;
1670 if (set & TIOCM_LOOP) 1667 if (set & TIOCM_LOOP)
1671 mcr |= MCR_LOOPBACK; 1668 mcr |= MCR_LOOPBACK;
1672 1669
1673 mos7840_port->shadowMCR = mcr; 1670 mos7840_port->shadowMCR = mcr;
1674 1671
1675 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr); 1672 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1676 if (status < 0) { 1673 if (status < 0) {
1677 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n"); 1674 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1678 return status; 1675 return status;
1679 } 1676 }
1680 1677
1681 return 0; 1678 return 0;
1682 } 1679 }
1683 1680
1684 /***************************************************************************** 1681 /*****************************************************************************
1685 * mos7840_calc_baud_rate_divisor 1682 * mos7840_calc_baud_rate_divisor
1686 * this function calculates the proper baud rate divisor for the specified 1683 * this function calculates the proper baud rate divisor for the specified
1687 * baud rate. 1684 * baud rate.
1688 *****************************************************************************/ 1685 *****************************************************************************/
1689 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port, 1686 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1690 int baudRate, int *divisor, 1687 int baudRate, int *divisor,
1691 __u16 *clk_sel_val) 1688 __u16 *clk_sel_val)
1692 { 1689 {
1693 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate); 1690 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1694 1691
1695 if (baudRate <= 115200) { 1692 if (baudRate <= 115200) {
1696 *divisor = 115200 / baudRate; 1693 *divisor = 115200 / baudRate;
1697 *clk_sel_val = 0x0; 1694 *clk_sel_val = 0x0;
1698 } 1695 }
1699 if ((baudRate > 115200) && (baudRate <= 230400)) { 1696 if ((baudRate > 115200) && (baudRate <= 230400)) {
1700 *divisor = 230400 / baudRate; 1697 *divisor = 230400 / baudRate;
1701 *clk_sel_val = 0x10; 1698 *clk_sel_val = 0x10;
1702 } else if ((baudRate > 230400) && (baudRate <= 403200)) { 1699 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1703 *divisor = 403200 / baudRate; 1700 *divisor = 403200 / baudRate;
1704 *clk_sel_val = 0x20; 1701 *clk_sel_val = 0x20;
1705 } else if ((baudRate > 403200) && (baudRate <= 460800)) { 1702 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1706 *divisor = 460800 / baudRate; 1703 *divisor = 460800 / baudRate;
1707 *clk_sel_val = 0x30; 1704 *clk_sel_val = 0x30;
1708 } else if ((baudRate > 460800) && (baudRate <= 806400)) { 1705 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1709 *divisor = 806400 / baudRate; 1706 *divisor = 806400 / baudRate;
1710 *clk_sel_val = 0x40; 1707 *clk_sel_val = 0x40;
1711 } else if ((baudRate > 806400) && (baudRate <= 921600)) { 1708 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1712 *divisor = 921600 / baudRate; 1709 *divisor = 921600 / baudRate;
1713 *clk_sel_val = 0x50; 1710 *clk_sel_val = 0x50;
1714 } else if ((baudRate > 921600) && (baudRate <= 1572864)) { 1711 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1715 *divisor = 1572864 / baudRate; 1712 *divisor = 1572864 / baudRate;
1716 *clk_sel_val = 0x60; 1713 *clk_sel_val = 0x60;
1717 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) { 1714 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1718 *divisor = 3145728 / baudRate; 1715 *divisor = 3145728 / baudRate;
1719 *clk_sel_val = 0x70; 1716 *clk_sel_val = 0x70;
1720 } 1717 }
1721 return 0; 1718 return 0;
1722 1719
1723 #ifdef NOTMCS7840 1720 #ifdef NOTMCS7840
1724 1721
1725 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) { 1722 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1726 if (mos7840_divisor_table[i].BaudRate == baudrate) { 1723 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1727 *divisor = mos7840_divisor_table[i].Divisor; 1724 *divisor = mos7840_divisor_table[i].Divisor;
1728 return 0; 1725 return 0;
1729 } 1726 }
1730 } 1727 }
1731 1728
1732 /* After trying for all the standard baud rates * 1729 /* After trying for all the standard baud rates *
1733 * Try calculating the divisor for this baud rate */ 1730 * Try calculating the divisor for this baud rate */
1734 1731
1735 if (baudrate > 75 && baudrate < 230400) { 1732 if (baudrate > 75 && baudrate < 230400) {
1736 /* get the divisor */ 1733 /* get the divisor */
1737 custom = (__u16) (230400L / baudrate); 1734 custom = (__u16) (230400L / baudrate);
1738 1735
1739 /* Check for round off */ 1736 /* Check for round off */
1740 round1 = (__u16) (2304000L / baudrate); 1737 round1 = (__u16) (2304000L / baudrate);
1741 round = (__u16) (round1 - (custom * 10)); 1738 round = (__u16) (round1 - (custom * 10));
1742 if (round > 4) 1739 if (round > 4)
1743 custom++; 1740 custom++;
1744 *divisor = custom; 1741 *divisor = custom;
1745 1742
1746 dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom); 1743 dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
1747 return 0; 1744 return 0;
1748 } 1745 }
1749 1746
1750 dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n"); 1747 dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
1751 return -1; 1748 return -1;
1752 #endif 1749 #endif
1753 } 1750 }
1754 1751
1755 /***************************************************************************** 1752 /*****************************************************************************
1756 * mos7840_send_cmd_write_baud_rate 1753 * mos7840_send_cmd_write_baud_rate
1757 * this function sends the proper command to change the baud rate of the 1754 * this function sends the proper command to change the baud rate of the
1758 * specified port. 1755 * specified port.
1759 *****************************************************************************/ 1756 *****************************************************************************/
1760 1757
1761 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port, 1758 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1762 int baudRate) 1759 int baudRate)
1763 { 1760 {
1764 int divisor = 0; 1761 int divisor = 0;
1765 int status; 1762 int status;
1766 __u16 Data; 1763 __u16 Data;
1767 unsigned char number; 1764 unsigned char number;
1768 __u16 clk_sel_val; 1765 __u16 clk_sel_val;
1769 struct usb_serial_port *port; 1766 struct usb_serial_port *port;
1770 1767
1771 if (mos7840_port == NULL) 1768 if (mos7840_port == NULL)
1772 return -1; 1769 return -1;
1773 1770
1774 port = mos7840_port->port; 1771 port = mos7840_port->port;
1775 if (mos7840_port_paranoia_check(port, __func__)) 1772 if (mos7840_port_paranoia_check(port, __func__))
1776 return -1; 1773 return -1;
1777 1774
1778 if (mos7840_serial_paranoia_check(port->serial, __func__)) 1775 if (mos7840_serial_paranoia_check(port->serial, __func__))
1779 return -1; 1776 return -1;
1780 1777
1781 number = mos7840_port->port->number - mos7840_port->port->serial->minor; 1778 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1782 1779
1783 dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__, 1780 dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
1784 mos7840_port->port->number, baudRate); 1781 mos7840_port->port->number, baudRate);
1785 /* reset clk_uart_sel in spregOffset */ 1782 /* reset clk_uart_sel in spregOffset */
1786 if (baudRate > 115200) { 1783 if (baudRate > 115200) {
1787 #ifdef HW_flow_control 1784 #ifdef HW_flow_control
1788 /* NOTE: need to see the pther register to modify */ 1785 /* NOTE: need to see the pther register to modify */
1789 /* setting h/w flow control bit to 1 */ 1786 /* setting h/w flow control bit to 1 */
1790 Data = 0x2b; 1787 Data = 0x2b;
1791 mos7840_port->shadowMCR = Data; 1788 mos7840_port->shadowMCR = Data;
1792 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1789 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1793 Data); 1790 Data);
1794 if (status < 0) { 1791 if (status < 0) {
1795 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); 1792 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1796 return -1; 1793 return -1;
1797 } 1794 }
1798 #endif 1795 #endif
1799 1796
1800 } else { 1797 } else {
1801 #ifdef HW_flow_control 1798 #ifdef HW_flow_control
1802 /* setting h/w flow control bit to 0 */ 1799 /* setting h/w flow control bit to 0 */
1803 Data = 0xb; 1800 Data = 0xb;
1804 mos7840_port->shadowMCR = Data; 1801 mos7840_port->shadowMCR = Data;
1805 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, 1802 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1806 Data); 1803 Data);
1807 if (status < 0) { 1804 if (status < 0) {
1808 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); 1805 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1809 return -1; 1806 return -1;
1810 } 1807 }
1811 #endif 1808 #endif
1812 1809
1813 } 1810 }
1814 1811
1815 if (1) { /* baudRate <= 115200) */ 1812 if (1) { /* baudRate <= 115200) */
1816 clk_sel_val = 0x0; 1813 clk_sel_val = 0x0;
1817 Data = 0x0; 1814 Data = 0x0;
1818 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor, 1815 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1819 &clk_sel_val); 1816 &clk_sel_val);
1820 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, 1817 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1821 &Data); 1818 &Data);
1822 if (status < 0) { 1819 if (status < 0) {
1823 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n"); 1820 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1824 return -1; 1821 return -1;
1825 } 1822 }
1826 Data = (Data & 0x8f) | clk_sel_val; 1823 Data = (Data & 0x8f) | clk_sel_val;
1827 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, 1824 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1828 Data); 1825 Data);
1829 if (status < 0) { 1826 if (status < 0) {
1830 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n"); 1827 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1831 return -1; 1828 return -1;
1832 } 1829 }
1833 /* Calculate the Divisor */ 1830 /* Calculate the Divisor */
1834 1831
1835 if (status) { 1832 if (status) {
1836 dev_err(&port->dev, "%s - bad baud rate\n", __func__); 1833 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1837 return status; 1834 return status;
1838 } 1835 }
1839 /* Enable access to divisor latch */ 1836 /* Enable access to divisor latch */
1840 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB; 1837 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1841 mos7840_port->shadowLCR = Data; 1838 mos7840_port->shadowLCR = Data;
1842 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1839 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1843 1840
1844 /* Write the divisor */ 1841 /* Write the divisor */
1845 Data = (unsigned char)(divisor & 0xff); 1842 Data = (unsigned char)(divisor & 0xff);
1846 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data); 1843 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1847 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data); 1844 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1848 1845
1849 Data = (unsigned char)((divisor & 0xff00) >> 8); 1846 Data = (unsigned char)((divisor & 0xff00) >> 8);
1850 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data); 1847 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1851 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data); 1848 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1852 1849
1853 /* Disable access to divisor latch */ 1850 /* Disable access to divisor latch */
1854 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB; 1851 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1855 mos7840_port->shadowLCR = Data; 1852 mos7840_port->shadowLCR = Data;
1856 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1853 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1857 1854
1858 } 1855 }
1859 return status; 1856 return status;
1860 } 1857 }
1861 1858
1862 /***************************************************************************** 1859 /*****************************************************************************
1863 * mos7840_change_port_settings 1860 * mos7840_change_port_settings
1864 * This routine is called to set the UART on the device to match 1861 * This routine is called to set the UART on the device to match
1865 * the specified new settings. 1862 * the specified new settings.
1866 *****************************************************************************/ 1863 *****************************************************************************/
1867 1864
1868 static void mos7840_change_port_settings(struct tty_struct *tty, 1865 static void mos7840_change_port_settings(struct tty_struct *tty,
1869 struct moschip_port *mos7840_port, struct ktermios *old_termios) 1866 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1870 { 1867 {
1871 int baud; 1868 int baud;
1872 unsigned cflag; 1869 unsigned cflag;
1873 unsigned iflag; 1870 unsigned iflag;
1874 __u8 lData; 1871 __u8 lData;
1875 __u8 lParity; 1872 __u8 lParity;
1876 __u8 lStop; 1873 __u8 lStop;
1877 int status; 1874 int status;
1878 __u16 Data; 1875 __u16 Data;
1879 struct usb_serial_port *port; 1876 struct usb_serial_port *port;
1880 struct usb_serial *serial; 1877 struct usb_serial *serial;
1881 1878
1882 if (mos7840_port == NULL) 1879 if (mos7840_port == NULL)
1883 return; 1880 return;
1884 1881
1885 port = mos7840_port->port; 1882 port = mos7840_port->port;
1886 1883
1887 if (mos7840_port_paranoia_check(port, __func__)) 1884 if (mos7840_port_paranoia_check(port, __func__))
1888 return; 1885 return;
1889 1886
1890 if (mos7840_serial_paranoia_check(port->serial, __func__)) 1887 if (mos7840_serial_paranoia_check(port->serial, __func__))
1891 return; 1888 return;
1892 1889
1893 serial = port->serial; 1890 serial = port->serial;
1894 1891
1895 if (!mos7840_port->open) { 1892 if (!mos7840_port->open) {
1896 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 1893 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1897 return; 1894 return;
1898 } 1895 }
1899 1896
1900 lData = LCR_BITS_8; 1897 lData = LCR_BITS_8;
1901 lStop = LCR_STOP_1; 1898 lStop = LCR_STOP_1;
1902 lParity = LCR_PAR_NONE; 1899 lParity = LCR_PAR_NONE;
1903 1900
1904 cflag = tty->termios.c_cflag; 1901 cflag = tty->termios.c_cflag;
1905 iflag = tty->termios.c_iflag; 1902 iflag = tty->termios.c_iflag;
1906 1903
1907 /* Change the number of bits */ 1904 /* Change the number of bits */
1908 if (cflag & CSIZE) { 1905 if (cflag & CSIZE) {
1909 switch (cflag & CSIZE) { 1906 switch (cflag & CSIZE) {
1910 case CS5: 1907 case CS5:
1911 lData = LCR_BITS_5; 1908 lData = LCR_BITS_5;
1912 break; 1909 break;
1913 1910
1914 case CS6: 1911 case CS6:
1915 lData = LCR_BITS_6; 1912 lData = LCR_BITS_6;
1916 break; 1913 break;
1917 1914
1918 case CS7: 1915 case CS7:
1919 lData = LCR_BITS_7; 1916 lData = LCR_BITS_7;
1920 break; 1917 break;
1921 default: 1918 default:
1922 case CS8: 1919 case CS8:
1923 lData = LCR_BITS_8; 1920 lData = LCR_BITS_8;
1924 break; 1921 break;
1925 } 1922 }
1926 } 1923 }
1927 /* Change the Parity bit */ 1924 /* Change the Parity bit */
1928 if (cflag & PARENB) { 1925 if (cflag & PARENB) {
1929 if (cflag & PARODD) { 1926 if (cflag & PARODD) {
1930 lParity = LCR_PAR_ODD; 1927 lParity = LCR_PAR_ODD;
1931 dev_dbg(&port->dev, "%s - parity = odd\n", __func__); 1928 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1932 } else { 1929 } else {
1933 lParity = LCR_PAR_EVEN; 1930 lParity = LCR_PAR_EVEN;
1934 dev_dbg(&port->dev, "%s - parity = even\n", __func__); 1931 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1935 } 1932 }
1936 1933
1937 } else { 1934 } else {
1938 dev_dbg(&port->dev, "%s - parity = none\n", __func__); 1935 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1939 } 1936 }
1940 1937
1941 if (cflag & CMSPAR) 1938 if (cflag & CMSPAR)
1942 lParity = lParity | 0x20; 1939 lParity = lParity | 0x20;
1943 1940
1944 /* Change the Stop bit */ 1941 /* Change the Stop bit */
1945 if (cflag & CSTOPB) { 1942 if (cflag & CSTOPB) {
1946 lStop = LCR_STOP_2; 1943 lStop = LCR_STOP_2;
1947 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__); 1944 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1948 } else { 1945 } else {
1949 lStop = LCR_STOP_1; 1946 lStop = LCR_STOP_1;
1950 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__); 1947 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1951 } 1948 }
1952 1949
1953 /* Update the LCR with the correct value */ 1950 /* Update the LCR with the correct value */
1954 mos7840_port->shadowLCR &= 1951 mos7840_port->shadowLCR &=
1955 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK); 1952 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1956 mos7840_port->shadowLCR |= (lData | lParity | lStop); 1953 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1957 1954
1958 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__, 1955 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1959 mos7840_port->shadowLCR); 1956 mos7840_port->shadowLCR);
1960 /* Disable Interrupts */ 1957 /* Disable Interrupts */
1961 Data = 0x00; 1958 Data = 0x00;
1962 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 1959 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1963 1960
1964 Data = 0x00; 1961 Data = 0x00;
1965 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 1962 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1966 1963
1967 Data = 0xcf; 1964 Data = 0xcf;
1968 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data); 1965 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1969 1966
1970 /* Send the updated LCR value to the mos7840 */ 1967 /* Send the updated LCR value to the mos7840 */
1971 Data = mos7840_port->shadowLCR; 1968 Data = mos7840_port->shadowLCR;
1972 1969
1973 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data); 1970 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1974 1971
1975 Data = 0x00b; 1972 Data = 0x00b;
1976 mos7840_port->shadowMCR = Data; 1973 mos7840_port->shadowMCR = Data;
1977 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 1974 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1978 Data = 0x00b; 1975 Data = 0x00b;
1979 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 1976 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1980 1977
1981 /* set up the MCR register and send it to the mos7840 */ 1978 /* set up the MCR register and send it to the mos7840 */
1982 1979
1983 mos7840_port->shadowMCR = MCR_MASTER_IE; 1980 mos7840_port->shadowMCR = MCR_MASTER_IE;
1984 if (cflag & CBAUD) 1981 if (cflag & CBAUD)
1985 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS); 1982 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
1986 1983
1987 if (cflag & CRTSCTS) 1984 if (cflag & CRTSCTS)
1988 mos7840_port->shadowMCR |= (MCR_XON_ANY); 1985 mos7840_port->shadowMCR |= (MCR_XON_ANY);
1989 else 1986 else
1990 mos7840_port->shadowMCR &= ~(MCR_XON_ANY); 1987 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
1991 1988
1992 Data = mos7840_port->shadowMCR; 1989 Data = mos7840_port->shadowMCR;
1993 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data); 1990 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1994 1991
1995 /* Determine divisor based on baud rate */ 1992 /* Determine divisor based on baud rate */
1996 baud = tty_get_baud_rate(tty); 1993 baud = tty_get_baud_rate(tty);
1997 1994
1998 if (!baud) { 1995 if (!baud) {
1999 /* pick a default, any default... */ 1996 /* pick a default, any default... */
2000 dev_dbg(&port->dev, "%s", "Picked default baud...\n"); 1997 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
2001 baud = 9600; 1998 baud = 9600;
2002 } 1999 }
2003 2000
2004 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud); 2001 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
2005 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud); 2002 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2006 2003
2007 /* Enable Interrupts */ 2004 /* Enable Interrupts */
2008 Data = 0x0c; 2005 Data = 0x0c;
2009 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data); 2006 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2010 2007
2011 if (mos7840_port->read_urb_busy == false) { 2008 if (mos7840_port->read_urb_busy == false) {
2012 mos7840_port->read_urb_busy = true; 2009 mos7840_port->read_urb_busy = true;
2013 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); 2010 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2014 if (status) { 2011 if (status) {
2015 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", 2012 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2016 status); 2013 status);
2017 mos7840_port->read_urb_busy = false; 2014 mos7840_port->read_urb_busy = false;
2018 } 2015 }
2019 } 2016 }
2020 wake_up(&mos7840_port->delta_msr_wait); 2017 wake_up(&mos7840_port->delta_msr_wait);
2021 mos7840_port->delta_msr_cond = 1; 2018 mos7840_port->delta_msr_cond = 1;
2022 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__, 2019 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
2023 mos7840_port->shadowLCR); 2020 mos7840_port->shadowLCR);
2024 } 2021 }
2025 2022
2026 /***************************************************************************** 2023 /*****************************************************************************
2027 * mos7840_set_termios 2024 * mos7840_set_termios
2028 * this function is called by the tty driver when it wants to change 2025 * this function is called by the tty driver when it wants to change
2029 * the termios structure 2026 * the termios structure
2030 *****************************************************************************/ 2027 *****************************************************************************/
2031 2028
2032 static void mos7840_set_termios(struct tty_struct *tty, 2029 static void mos7840_set_termios(struct tty_struct *tty,
2033 struct usb_serial_port *port, 2030 struct usb_serial_port *port,
2034 struct ktermios *old_termios) 2031 struct ktermios *old_termios)
2035 { 2032 {
2036 int status; 2033 int status;
2037 unsigned int cflag; 2034 unsigned int cflag;
2038 struct usb_serial *serial; 2035 struct usb_serial *serial;
2039 struct moschip_port *mos7840_port; 2036 struct moschip_port *mos7840_port;
2040 2037
2041 if (mos7840_port_paranoia_check(port, __func__)) 2038 if (mos7840_port_paranoia_check(port, __func__))
2042 return; 2039 return;
2043 2040
2044 serial = port->serial; 2041 serial = port->serial;
2045 2042
2046 if (mos7840_serial_paranoia_check(serial, __func__)) 2043 if (mos7840_serial_paranoia_check(serial, __func__))
2047 return; 2044 return;
2048 2045
2049 mos7840_port = mos7840_get_port_private(port); 2046 mos7840_port = mos7840_get_port_private(port);
2050 2047
2051 if (mos7840_port == NULL) 2048 if (mos7840_port == NULL)
2052 return; 2049 return;
2053 2050
2054 if (!mos7840_port->open) { 2051 if (!mos7840_port->open) {
2055 dev_dbg(&port->dev, "%s - port not opened\n", __func__); 2052 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
2056 return; 2053 return;
2057 } 2054 }
2058 2055
2059 dev_dbg(&port->dev, "%s", "setting termios - \n"); 2056 dev_dbg(&port->dev, "%s", "setting termios - \n");
2060 2057
2061 cflag = tty->termios.c_cflag; 2058 cflag = tty->termios.c_cflag;
2062 2059
2063 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, 2060 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2064 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag)); 2061 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
2065 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, 2062 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2066 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag)); 2063 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2067 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number); 2064 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
2068 2065
2069 /* change the port settings to the new ones specified */ 2066 /* change the port settings to the new ones specified */
2070 2067
2071 mos7840_change_port_settings(tty, mos7840_port, old_termios); 2068 mos7840_change_port_settings(tty, mos7840_port, old_termios);
2072 2069
2073 if (!mos7840_port->read_urb) { 2070 if (!mos7840_port->read_urb) {
2074 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n"); 2071 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
2075 return; 2072 return;
2076 } 2073 }
2077 2074
2078 if (mos7840_port->read_urb_busy == false) { 2075 if (mos7840_port->read_urb_busy == false) {
2079 mos7840_port->read_urb_busy = true; 2076 mos7840_port->read_urb_busy = true;
2080 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC); 2077 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2081 if (status) { 2078 if (status) {
2082 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", 2079 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2083 status); 2080 status);
2084 mos7840_port->read_urb_busy = false; 2081 mos7840_port->read_urb_busy = false;
2085 } 2082 }
2086 } 2083 }
2087 } 2084 }
2088 2085
2089 /***************************************************************************** 2086 /*****************************************************************************
2090 * mos7840_get_lsr_info - get line status register info 2087 * mos7840_get_lsr_info - get line status register info
2091 * 2088 *
2092 * Purpose: Let user call ioctl() to get info when the UART physically 2089 * Purpose: Let user call ioctl() to get info when the UART physically
2093 * is emptied. On bus types like RS485, the transmitter must 2090 * is emptied. On bus types like RS485, the transmitter must
2094 * release the bus after transmitting. This must be done when 2091 * release the bus after transmitting. This must be done when
2095 * the transmit shift register is empty, not be done when the 2092 * the transmit shift register is empty, not be done when the
2096 * transmit holding register is empty. This functionality 2093 * transmit holding register is empty. This functionality
2097 * allows an RS485 driver to be written in user space. 2094 * allows an RS485 driver to be written in user space.
2098 *****************************************************************************/ 2095 *****************************************************************************/
2099 2096
2100 static int mos7840_get_lsr_info(struct tty_struct *tty, 2097 static int mos7840_get_lsr_info(struct tty_struct *tty,
2101 unsigned int __user *value) 2098 unsigned int __user *value)
2102 { 2099 {
2103 int count; 2100 int count;
2104 unsigned int result = 0; 2101 unsigned int result = 0;
2105 2102
2106 count = mos7840_chars_in_buffer(tty); 2103 count = mos7840_chars_in_buffer(tty);
2107 if (count == 0) 2104 if (count == 0)
2108 result = TIOCSER_TEMT; 2105 result = TIOCSER_TEMT;
2109 2106
2110 if (copy_to_user(value, &result, sizeof(int))) 2107 if (copy_to_user(value, &result, sizeof(int)))
2111 return -EFAULT; 2108 return -EFAULT;
2112 return 0; 2109 return 0;
2113 } 2110 }
2114 2111
2115 /***************************************************************************** 2112 /*****************************************************************************
2116 * mos7840_get_serial_info 2113 * mos7840_get_serial_info
2117 * function to get information about serial port 2114 * function to get information about serial port
2118 *****************************************************************************/ 2115 *****************************************************************************/
2119 2116
2120 static int mos7840_get_serial_info(struct moschip_port *mos7840_port, 2117 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2121 struct serial_struct __user *retinfo) 2118 struct serial_struct __user *retinfo)
2122 { 2119 {
2123 struct serial_struct tmp; 2120 struct serial_struct tmp;
2124 2121
2125 if (mos7840_port == NULL) 2122 if (mos7840_port == NULL)
2126 return -1; 2123 return -1;
2127 2124
2128 if (!retinfo) 2125 if (!retinfo)
2129 return -EFAULT; 2126 return -EFAULT;
2130 2127
2131 memset(&tmp, 0, sizeof(tmp)); 2128 memset(&tmp, 0, sizeof(tmp));
2132 2129
2133 tmp.type = PORT_16550A; 2130 tmp.type = PORT_16550A;
2134 tmp.line = mos7840_port->port->serial->minor; 2131 tmp.line = mos7840_port->port->serial->minor;
2135 tmp.port = mos7840_port->port->number; 2132 tmp.port = mos7840_port->port->number;
2136 tmp.irq = 0; 2133 tmp.irq = 0;
2137 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; 2134 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2138 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE; 2135 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2139 tmp.baud_base = 9600; 2136 tmp.baud_base = 9600;
2140 tmp.close_delay = 5 * HZ; 2137 tmp.close_delay = 5 * HZ;
2141 tmp.closing_wait = 30 * HZ; 2138 tmp.closing_wait = 30 * HZ;
2142 2139
2143 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 2140 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2144 return -EFAULT; 2141 return -EFAULT;
2145 return 0; 2142 return 0;
2146 } 2143 }
2147 2144
2148 static int mos7840_get_icount(struct tty_struct *tty, 2145 static int mos7840_get_icount(struct tty_struct *tty,
2149 struct serial_icounter_struct *icount) 2146 struct serial_icounter_struct *icount)
2150 { 2147 {
2151 struct usb_serial_port *port = tty->driver_data; 2148 struct usb_serial_port *port = tty->driver_data;
2152 struct moschip_port *mos7840_port; 2149 struct moschip_port *mos7840_port;
2153 struct async_icount cnow; 2150 struct async_icount cnow;
2154 2151
2155 mos7840_port = mos7840_get_port_private(port); 2152 mos7840_port = mos7840_get_port_private(port);
2156 cnow = mos7840_port->icount; 2153 cnow = mos7840_port->icount;
2157 2154
2158 smp_rmb(); 2155 smp_rmb();
2159 icount->cts = cnow.cts; 2156 icount->cts = cnow.cts;
2160 icount->dsr = cnow.dsr; 2157 icount->dsr = cnow.dsr;
2161 icount->rng = cnow.rng; 2158 icount->rng = cnow.rng;
2162 icount->dcd = cnow.dcd; 2159 icount->dcd = cnow.dcd;
2163 icount->rx = cnow.rx; 2160 icount->rx = cnow.rx;
2164 icount->tx = cnow.tx; 2161 icount->tx = cnow.tx;
2165 icount->frame = cnow.frame; 2162 icount->frame = cnow.frame;
2166 icount->overrun = cnow.overrun; 2163 icount->overrun = cnow.overrun;
2167 icount->parity = cnow.parity; 2164 icount->parity = cnow.parity;
2168 icount->brk = cnow.brk; 2165 icount->brk = cnow.brk;
2169 icount->buf_overrun = cnow.buf_overrun; 2166 icount->buf_overrun = cnow.buf_overrun;
2170 2167
2171 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__, 2168 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
2172 icount->rx, icount->tx); 2169 icount->rx, icount->tx);
2173 return 0; 2170 return 0;
2174 } 2171 }
2175 2172
2176 /***************************************************************************** 2173 /*****************************************************************************
2177 * SerialIoctl 2174 * SerialIoctl
2178 * this function handles any ioctl calls to the driver 2175 * this function handles any ioctl calls to the driver
2179 *****************************************************************************/ 2176 *****************************************************************************/
2180 2177
2181 static int mos7840_ioctl(struct tty_struct *tty, 2178 static int mos7840_ioctl(struct tty_struct *tty,
2182 unsigned int cmd, unsigned long arg) 2179 unsigned int cmd, unsigned long arg)
2183 { 2180 {
2184 struct usb_serial_port *port = tty->driver_data; 2181 struct usb_serial_port *port = tty->driver_data;
2185 void __user *argp = (void __user *)arg; 2182 void __user *argp = (void __user *)arg;
2186 struct moschip_port *mos7840_port; 2183 struct moschip_port *mos7840_port;
2187 2184
2188 struct async_icount cnow; 2185 struct async_icount cnow;
2189 struct async_icount cprev; 2186 struct async_icount cprev;
2190 2187
2191 if (mos7840_port_paranoia_check(port, __func__)) 2188 if (mos7840_port_paranoia_check(port, __func__))
2192 return -1; 2189 return -1;
2193 2190
2194 mos7840_port = mos7840_get_port_private(port); 2191 mos7840_port = mos7840_get_port_private(port);
2195 2192
2196 if (mos7840_port == NULL) 2193 if (mos7840_port == NULL)
2197 return -1; 2194 return -1;
2198 2195
2199 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd); 2196 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
2200 2197
2201 switch (cmd) { 2198 switch (cmd) {
2202 /* return number of bytes available */ 2199 /* return number of bytes available */
2203 2200
2204 case TIOCSERGETLSR: 2201 case TIOCSERGETLSR:
2205 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__); 2202 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
2206 return mos7840_get_lsr_info(tty, argp); 2203 return mos7840_get_lsr_info(tty, argp);
2207 2204
2208 case TIOCGSERIAL: 2205 case TIOCGSERIAL:
2209 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__); 2206 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
2210 return mos7840_get_serial_info(mos7840_port, argp); 2207 return mos7840_get_serial_info(mos7840_port, argp);
2211 2208
2212 case TIOCSSERIAL: 2209 case TIOCSSERIAL:
2213 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__); 2210 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
2214 break; 2211 break;
2215 2212
2216 case TIOCMIWAIT: 2213 case TIOCMIWAIT:
2217 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__); 2214 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
2218 cprev = mos7840_port->icount; 2215 cprev = mos7840_port->icount;
2219 while (1) { 2216 while (1) {
2220 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */ 2217 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2221 mos7840_port->delta_msr_cond = 0; 2218 mos7840_port->delta_msr_cond = 0;
2222 wait_event_interruptible(mos7840_port->delta_msr_wait, 2219 wait_event_interruptible(mos7840_port->delta_msr_wait,
2223 (mos7840_port-> 2220 (mos7840_port->
2224 delta_msr_cond == 1)); 2221 delta_msr_cond == 1));
2225 2222
2226 /* see if a signal did it */ 2223 /* see if a signal did it */
2227 if (signal_pending(current)) 2224 if (signal_pending(current))
2228 return -ERESTARTSYS; 2225 return -ERESTARTSYS;
2229 cnow = mos7840_port->icount; 2226 cnow = mos7840_port->icount;
2230 smp_rmb(); 2227 smp_rmb();
2231 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 2228 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2232 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) 2229 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2233 return -EIO; /* no change => error */ 2230 return -EIO; /* no change => error */
2234 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 2231 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2235 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 2232 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2236 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 2233 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2237 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) { 2234 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2238 return 0; 2235 return 0;
2239 } 2236 }
2240 cprev = cnow; 2237 cprev = cnow;
2241 } 2238 }
2242 /* NOTREACHED */ 2239 /* NOTREACHED */
2243 break; 2240 break;
2244 2241
2245 default: 2242 default:
2246 break; 2243 break;
2247 } 2244 }
2248 return -ENOIOCTLCMD; 2245 return -ENOIOCTLCMD;
2249 } 2246 }
2250 2247
2251 static int mos7810_check(struct usb_serial *serial) 2248 static int mos7810_check(struct usb_serial *serial)
2252 { 2249 {
2253 int i, pass_count = 0; 2250 int i, pass_count = 0;
2254 __u16 data = 0, mcr_data = 0; 2251 __u16 data = 0, mcr_data = 0;
2255 __u16 test_pattern = 0x55AA; 2252 __u16 test_pattern = 0x55AA;
2256 2253
2257 /* Store MCR setting */ 2254 /* Store MCR setting */
2258 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 2255 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2259 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER, 2256 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2260 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 2257 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2261 2258
2262 for (i = 0; i < 16; i++) { 2259 for (i = 0; i < 16; i++) {
2263 /* Send the 1-bit test pattern out to MCS7810 test pin */ 2260 /* Send the 1-bit test pattern out to MCS7810 test pin */
2264 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 2261 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2265 MCS_WRREQ, MCS_WR_RTYPE, 2262 MCS_WRREQ, MCS_WR_RTYPE,
2266 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)), 2263 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2267 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT); 2264 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2268 2265
2269 /* Read the test pattern back */ 2266 /* Read the test pattern back */
2270 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 2267 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2271 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data, 2268 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2272 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 2269 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2273 2270
2274 /* If this is a MCS7810 device, both test patterns must match */ 2271 /* If this is a MCS7810 device, both test patterns must match */
2275 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001) 2272 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2276 break; 2273 break;
2277 2274
2278 pass_count++; 2275 pass_count++;
2279 } 2276 }
2280 2277
2281 /* Restore MCR setting */ 2278 /* Restore MCR setting */
2282 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ, 2279 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2283 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL, 2280 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2284 0, MOS_WDR_TIMEOUT); 2281 0, MOS_WDR_TIMEOUT);
2285 2282
2286 if (pass_count == 16) 2283 if (pass_count == 16)
2287 return 1; 2284 return 1;
2288 2285
2289 return 0; 2286 return 0;
2290 } 2287 }
2291 2288
2292 static int mos7840_calc_num_ports(struct usb_serial *serial) 2289 static int mos7840_calc_num_ports(struct usb_serial *serial)
2293 { 2290 {
2294 __u16 data = 0x00; 2291 __u16 data = 0x00;
2295 int mos7840_num_ports; 2292 int mos7840_num_ports;
2296 2293
2297 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), 2294 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2298 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data, 2295 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2299 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT); 2296 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2300 2297
2301 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 || 2298 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2302 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) { 2299 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2303 device_type = serial->dev->descriptor.idProduct; 2300 device_type = serial->dev->descriptor.idProduct;
2304 } else { 2301 } else {
2305 /* For a MCS7840 device GPIO0 must be set to 1 */ 2302 /* For a MCS7840 device GPIO0 must be set to 1 */
2306 if ((data & 0x01) == 1) 2303 if ((data & 0x01) == 1)
2307 device_type = MOSCHIP_DEVICE_ID_7840; 2304 device_type = MOSCHIP_DEVICE_ID_7840;
2308 else if (mos7810_check(serial)) 2305 else if (mos7810_check(serial))
2309 device_type = MOSCHIP_DEVICE_ID_7810; 2306 device_type = MOSCHIP_DEVICE_ID_7810;
2310 else 2307 else
2311 device_type = MOSCHIP_DEVICE_ID_7820; 2308 device_type = MOSCHIP_DEVICE_ID_7820;
2312 } 2309 }
2313 2310
2314 mos7840_num_ports = (device_type >> 4) & 0x000F; 2311 mos7840_num_ports = (device_type >> 4) & 0x000F;
2315 serial->num_bulk_in = mos7840_num_ports; 2312 serial->num_bulk_in = mos7840_num_ports;
2316 serial->num_bulk_out = mos7840_num_ports; 2313 serial->num_bulk_out = mos7840_num_ports;
2317 serial->num_ports = mos7840_num_ports; 2314 serial->num_ports = mos7840_num_ports;
2318 2315
2319 return mos7840_num_ports; 2316 return mos7840_num_ports;
2320 } 2317 }
2321 2318
2322 static int mos7840_port_probe(struct usb_serial_port *port) 2319 static int mos7840_port_probe(struct usb_serial_port *port)
2323 { 2320 {
2324 struct usb_serial *serial = port->serial; 2321 struct usb_serial *serial = port->serial;
2325 struct moschip_port *mos7840_port; 2322 struct moschip_port *mos7840_port;
2326 int status; 2323 int status;
2327 int pnum; 2324 int pnum;
2328 __u16 Data; 2325 __u16 Data;
2329 2326
2330 /* we set up the pointers to the endpoints in the mos7840_open * 2327 /* we set up the pointers to the endpoints in the mos7840_open *
2331 * function, as the structures aren't created yet. */ 2328 * function, as the structures aren't created yet. */
2332 2329
2333 pnum = port->number - serial->minor; 2330 pnum = port->number - serial->minor;
2334 2331
2335 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum); 2332 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2336 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL); 2333 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2337 if (mos7840_port == NULL) { 2334 if (mos7840_port == NULL) {
2338 dev_err(&port->dev, "%s - Out of memory\n", __func__); 2335 dev_err(&port->dev, "%s - Out of memory\n", __func__);
2339 return -ENOMEM; 2336 return -ENOMEM;
2340 } 2337 }
2341 2338
2342 /* Initialize all port interrupt end point to port 0 int 2339 /* Initialize all port interrupt end point to port 0 int
2343 * endpoint. Our device has only one interrupt end point 2340 * endpoint. Our device has only one interrupt end point
2344 * common to all port */ 2341 * common to all port */
2345 2342
2346 mos7840_port->port = port; 2343 mos7840_port->port = port;
2347 mos7840_set_port_private(port, mos7840_port); 2344 mos7840_set_port_private(port, mos7840_port);
2348 spin_lock_init(&mos7840_port->pool_lock); 2345 spin_lock_init(&mos7840_port->pool_lock);
2349 2346
2350 /* minor is not initialised until later by 2347 /* minor is not initialised until later by
2351 * usb-serial.c:get_free_serial() and cannot therefore be used 2348 * usb-serial.c:get_free_serial() and cannot therefore be used
2352 * to index device instances */ 2349 * to index device instances */
2353 mos7840_port->port_num = pnum + 1; 2350 mos7840_port->port_num = pnum + 1;
2354 dev_dbg(&port->dev, "port->number = %d\n", port->number); 2351 dev_dbg(&port->dev, "port->number = %d\n", port->number);
2355 dev_dbg(&port->dev, "port->serial->minor = %d\n", port->serial->minor); 2352 dev_dbg(&port->dev, "port->serial->minor = %d\n", port->serial->minor);
2356 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num); 2353 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2357 dev_dbg(&port->dev, "serial->minor = %d\n", serial->minor); 2354 dev_dbg(&port->dev, "serial->minor = %d\n", serial->minor);
2358 2355
2359 if (mos7840_port->port_num == 1) { 2356 if (mos7840_port->port_num == 1) {
2360 mos7840_port->SpRegOffset = 0x0; 2357 mos7840_port->SpRegOffset = 0x0;
2361 mos7840_port->ControlRegOffset = 0x1; 2358 mos7840_port->ControlRegOffset = 0x1;
2362 mos7840_port->DcrRegOffset = 0x4; 2359 mos7840_port->DcrRegOffset = 0x4;
2363 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) { 2360 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
2364 mos7840_port->SpRegOffset = 0x8; 2361 mos7840_port->SpRegOffset = 0x8;
2365 mos7840_port->ControlRegOffset = 0x9; 2362 mos7840_port->ControlRegOffset = 0x9;
2366 mos7840_port->DcrRegOffset = 0x16; 2363 mos7840_port->DcrRegOffset = 0x16;
2367 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) { 2364 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
2368 mos7840_port->SpRegOffset = 0xa; 2365 mos7840_port->SpRegOffset = 0xa;
2369 mos7840_port->ControlRegOffset = 0xb; 2366 mos7840_port->ControlRegOffset = 0xb;
2370 mos7840_port->DcrRegOffset = 0x19; 2367 mos7840_port->DcrRegOffset = 0x19;
2371 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) { 2368 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
2372 mos7840_port->SpRegOffset = 0xa; 2369 mos7840_port->SpRegOffset = 0xa;
2373 mos7840_port->ControlRegOffset = 0xb; 2370 mos7840_port->ControlRegOffset = 0xb;
2374 mos7840_port->DcrRegOffset = 0x19; 2371 mos7840_port->DcrRegOffset = 0x19;
2375 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) { 2372 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
2376 mos7840_port->SpRegOffset = 0xc; 2373 mos7840_port->SpRegOffset = 0xc;
2377 mos7840_port->ControlRegOffset = 0xd; 2374 mos7840_port->ControlRegOffset = 0xd;
2378 mos7840_port->DcrRegOffset = 0x1c; 2375 mos7840_port->DcrRegOffset = 0x1c;
2379 } 2376 }
2380 mos7840_dump_serial_port(port, mos7840_port); 2377 mos7840_dump_serial_port(port, mos7840_port);
2381 mos7840_set_port_private(port, mos7840_port); 2378 mos7840_set_port_private(port, mos7840_port);
2382 2379
2383 /* enable rx_disable bit in control register */ 2380 /* enable rx_disable bit in control register */
2384 status = mos7840_get_reg_sync(port, 2381 status = mos7840_get_reg_sync(port,
2385 mos7840_port->ControlRegOffset, &Data); 2382 mos7840_port->ControlRegOffset, &Data);
2386 if (status < 0) { 2383 if (status < 0) {
2387 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status); 2384 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2388 goto out; 2385 goto out;
2389 } else 2386 } else
2390 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status); 2387 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2391 Data |= 0x08; /* setting driver done bit */ 2388 Data |= 0x08; /* setting driver done bit */
2392 Data |= 0x04; /* sp1_bit to have cts change reflect in 2389 Data |= 0x04; /* sp1_bit to have cts change reflect in
2393 modem status reg */ 2390 modem status reg */
2394 2391
2395 /* Data |= 0x20; //rx_disable bit */ 2392 /* Data |= 0x20; //rx_disable bit */
2396 status = mos7840_set_reg_sync(port, 2393 status = mos7840_set_reg_sync(port,
2397 mos7840_port->ControlRegOffset, Data); 2394 mos7840_port->ControlRegOffset, Data);
2398 if (status < 0) { 2395 if (status < 0) {
2399 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status); 2396 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2400 goto out; 2397 goto out;
2401 } else 2398 } else
2402 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status); 2399 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2403 2400
2404 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 2401 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2405 and 0x24 in DCR3 */ 2402 and 0x24 in DCR3 */
2406 Data = 0x01; 2403 Data = 0x01;
2407 status = mos7840_set_reg_sync(port, 2404 status = mos7840_set_reg_sync(port,
2408 (__u16) (mos7840_port->DcrRegOffset + 0), Data); 2405 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2409 if (status < 0) { 2406 if (status < 0) {
2410 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status); 2407 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2411 goto out; 2408 goto out;
2412 } else 2409 } else
2413 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status); 2410 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
2414 2411
2415 Data = 0x05; 2412 Data = 0x05;
2416 status = mos7840_set_reg_sync(port, 2413 status = mos7840_set_reg_sync(port,
2417 (__u16) (mos7840_port->DcrRegOffset + 1), Data); 2414 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2418 if (status < 0) { 2415 if (status < 0) {
2419 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status); 2416 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2420 goto out; 2417 goto out;
2421 } else 2418 } else
2422 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status); 2419 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
2423 2420
2424 Data = 0x24; 2421 Data = 0x24;
2425 status = mos7840_set_reg_sync(port, 2422 status = mos7840_set_reg_sync(port,
2426 (__u16) (mos7840_port->DcrRegOffset + 2), Data); 2423 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2427 if (status < 0) { 2424 if (status < 0) {
2428 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status); 2425 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2429 goto out; 2426 goto out;
2430 } else 2427 } else
2431 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status); 2428 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
2432 2429
2433 /* write values in clkstart0x0 and clkmulti 0x20 */ 2430 /* write values in clkstart0x0 and clkmulti 0x20 */
2434 Data = 0x0; 2431 Data = 0x0;
2435 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data); 2432 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2436 if (status < 0) { 2433 if (status < 0) {
2437 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status); 2434 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2438 goto out; 2435 goto out;
2439 } else 2436 } else
2440 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status); 2437 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2441 2438
2442 Data = 0x20; 2439 Data = 0x20;
2443 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data); 2440 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2444 if (status < 0) { 2441 if (status < 0) {
2445 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status); 2442 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2446 goto error; 2443 goto error;
2447 } else 2444 } else
2448 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status); 2445 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2449 2446
2450 /* write value 0x0 to scratchpad register */ 2447 /* write value 0x0 to scratchpad register */
2451 Data = 0x00; 2448 Data = 0x00;
2452 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data); 2449 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2453 if (status < 0) { 2450 if (status < 0) {
2454 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status); 2451 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2455 goto out; 2452 goto out;
2456 } else 2453 } else
2457 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status); 2454 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2458 2455
2459 /* Zero Length flag register */ 2456 /* Zero Length flag register */
2460 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) { 2457 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2461 Data = 0xff; 2458 Data = 0xff;
2462 status = mos7840_set_reg_sync(port, 2459 status = mos7840_set_reg_sync(port,
2463 (__u16) (ZLP_REG1 + 2460 (__u16) (ZLP_REG1 +
2464 ((__u16)mos7840_port->port_num)), Data); 2461 ((__u16)mos7840_port->port_num)), Data);
2465 dev_dbg(&port->dev, "ZLIP offset %x\n", 2462 dev_dbg(&port->dev, "ZLIP offset %x\n",
2466 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num))); 2463 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
2467 if (status < 0) { 2464 if (status < 0) {
2468 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status); 2465 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2469 goto out; 2466 goto out;
2470 } else 2467 } else
2471 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status); 2468 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2472 } else { 2469 } else {
2473 Data = 0xff; 2470 Data = 0xff;
2474 status = mos7840_set_reg_sync(port, 2471 status = mos7840_set_reg_sync(port,
2475 (__u16) (ZLP_REG1 + 2472 (__u16) (ZLP_REG1 +
2476 ((__u16)mos7840_port->port_num) - 0x1), Data); 2473 ((__u16)mos7840_port->port_num) - 0x1), Data);
2477 dev_dbg(&port->dev, "ZLIP offset %x\n", 2474 dev_dbg(&port->dev, "ZLIP offset %x\n",
2478 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1)); 2475 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
2479 if (status < 0) { 2476 if (status < 0) {
2480 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status); 2477 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2481 goto out; 2478 goto out;
2482 } else 2479 } else
2483 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status); 2480 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
2484 2481
2485 } 2482 }
2486 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL); 2483 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2487 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL); 2484 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2488 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), 2485 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2489 GFP_KERNEL); 2486 GFP_KERNEL);
2490 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || 2487 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2491 !mos7840_port->dr) { 2488 !mos7840_port->dr) {
2492 status = -ENOMEM; 2489 status = -ENOMEM;
2493 goto error; 2490 goto error;
2494 } 2491 }
2495 2492
2496 mos7840_port->has_led = false; 2493 mos7840_port->has_led = false;
2497 2494
2498 /* Initialize LED timers */ 2495 /* Initialize LED timers */
2499 if (device_type == MOSCHIP_DEVICE_ID_7810) { 2496 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2500 mos7840_port->has_led = true; 2497 mos7840_port->has_led = true;
2501 2498
2502 init_timer(&mos7840_port->led_timer1); 2499 init_timer(&mos7840_port->led_timer1);
2503 mos7840_port->led_timer1.function = mos7840_led_off; 2500 mos7840_port->led_timer1.function = mos7840_led_off;
2504 mos7840_port->led_timer1.expires = 2501 mos7840_port->led_timer1.expires =
2505 jiffies + msecs_to_jiffies(LED_ON_MS); 2502 jiffies + msecs_to_jiffies(LED_ON_MS);
2506 mos7840_port->led_timer1.data = (unsigned long)mos7840_port; 2503 mos7840_port->led_timer1.data = (unsigned long)mos7840_port;
2507 2504
2508 init_timer(&mos7840_port->led_timer2); 2505 init_timer(&mos7840_port->led_timer2);
2509 mos7840_port->led_timer2.function = mos7840_led_flag_off; 2506 mos7840_port->led_timer2.function = mos7840_led_flag_off;
2510 mos7840_port->led_timer2.expires = 2507 mos7840_port->led_timer2.expires =
2511 jiffies + msecs_to_jiffies(LED_OFF_MS); 2508 jiffies + msecs_to_jiffies(LED_OFF_MS);
2512 mos7840_port->led_timer2.data = (unsigned long)mos7840_port; 2509 mos7840_port->led_timer2.data = (unsigned long)mos7840_port;
2513 2510
2514 mos7840_port->led_flag = false; 2511 mos7840_port->led_flag = false;
2515 2512
2516 /* Turn off LED */ 2513 /* Turn off LED */
2517 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300); 2514 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2518 } 2515 }
2519 out: 2516 out:
2520 if (pnum == serial->num_ports - 1) { 2517 if (pnum == serial->num_ports - 1) {
2521 /* Zero Length flag enable */ 2518 /* Zero Length flag enable */
2522 Data = 0x0f; 2519 Data = 0x0f;
2523 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data); 2520 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2524 if (status < 0) { 2521 if (status < 0) {
2525 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status); 2522 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2526 goto error; 2523 goto error;
2527 } else 2524 } else
2528 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status); 2525 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2529 2526
2530 /* setting configuration feature to one */ 2527 /* setting configuration feature to one */
2531 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 2528 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2532 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 2529 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
2533 MOS_WDR_TIMEOUT); 2530 MOS_WDR_TIMEOUT);
2534 } 2531 }
2535 return 0; 2532 return 0;
2536 error: 2533 error:
2537 kfree(mos7840_port->dr); 2534 kfree(mos7840_port->dr);
2538 kfree(mos7840_port->ctrl_buf); 2535 kfree(mos7840_port->ctrl_buf);
2539 usb_free_urb(mos7840_port->control_urb); 2536 usb_free_urb(mos7840_port->control_urb);
2540 kfree(mos7840_port); 2537 kfree(mos7840_port);
2541 2538
2542 return status; 2539 return status;
2543 } 2540 }
2544 2541
2545 static int mos7840_port_remove(struct usb_serial_port *port) 2542 static int mos7840_port_remove(struct usb_serial_port *port)
2546 { 2543 {
2547 struct moschip_port *mos7840_port; 2544 struct moschip_port *mos7840_port;
2548 2545
2549 mos7840_port = mos7840_get_port_private(port); 2546 mos7840_port = mos7840_get_port_private(port);
2550 2547
2551 if (mos7840_port->has_led) { 2548 if (mos7840_port->has_led) {
2552 /* Turn off LED */ 2549 /* Turn off LED */
2553 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300); 2550 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2554 2551
2555 del_timer_sync(&mos7840_port->led_timer1); 2552 del_timer_sync(&mos7840_port->led_timer1);
2556 del_timer_sync(&mos7840_port->led_timer2); 2553 del_timer_sync(&mos7840_port->led_timer2);
2557 } 2554 }
2558 usb_kill_urb(mos7840_port->control_urb); 2555 usb_kill_urb(mos7840_port->control_urb);
2559 usb_free_urb(mos7840_port->control_urb); 2556 usb_free_urb(mos7840_port->control_urb);
2560 kfree(mos7840_port->ctrl_buf); 2557 kfree(mos7840_port->ctrl_buf);
2561 kfree(mos7840_port->dr); 2558 kfree(mos7840_port->dr);
2562 kfree(mos7840_port); 2559 kfree(mos7840_port);
2563 2560
2564 return 0; 2561 return 0;
2565 } 2562 }
2566 2563
2567 static struct usb_serial_driver moschip7840_4port_device = { 2564 static struct usb_serial_driver moschip7840_4port_device = {
2568 .driver = { 2565 .driver = {
2569 .owner = THIS_MODULE, 2566 .owner = THIS_MODULE,
2570 .name = "mos7840", 2567 .name = "mos7840",
2571 }, 2568 },
2572 .description = DRIVER_DESC, 2569 .description = DRIVER_DESC,
2573 .id_table = id_table, 2570 .id_table = id_table,
2574 .num_ports = 4, 2571 .num_ports = 4,
2575 .open = mos7840_open, 2572 .open = mos7840_open,
2576 .close = mos7840_close, 2573 .close = mos7840_close,
2577 .write = mos7840_write, 2574 .write = mos7840_write,
2578 .write_room = mos7840_write_room, 2575 .write_room = mos7840_write_room,
2579 .chars_in_buffer = mos7840_chars_in_buffer, 2576 .chars_in_buffer = mos7840_chars_in_buffer,
2580 .throttle = mos7840_throttle, 2577 .throttle = mos7840_throttle,
2581 .unthrottle = mos7840_unthrottle, 2578 .unthrottle = mos7840_unthrottle,
2582 .calc_num_ports = mos7840_calc_num_ports, 2579 .calc_num_ports = mos7840_calc_num_ports,
2583 #ifdef MCSSerialProbe 2580 #ifdef MCSSerialProbe
2584 .probe = mos7840_serial_probe, 2581 .probe = mos7840_serial_probe,
2585 #endif 2582 #endif
2586 .ioctl = mos7840_ioctl, 2583 .ioctl = mos7840_ioctl,
2587 .set_termios = mos7840_set_termios, 2584 .set_termios = mos7840_set_termios,
2588 .break_ctl = mos7840_break, 2585 .break_ctl = mos7840_break,
2589 .tiocmget = mos7840_tiocmget, 2586 .tiocmget = mos7840_tiocmget,
2590 .tiocmset = mos7840_tiocmset, 2587 .tiocmset = mos7840_tiocmset,
2591 .get_icount = mos7840_get_icount, 2588 .get_icount = mos7840_get_icount,
2592 .port_probe = mos7840_port_probe, 2589 .port_probe = mos7840_port_probe,
2593 .port_remove = mos7840_port_remove, 2590 .port_remove = mos7840_port_remove,
2594 .read_bulk_callback = mos7840_bulk_in_callback, 2591 .read_bulk_callback = mos7840_bulk_in_callback,
2595 .read_int_callback = mos7840_interrupt_callback, 2592 .read_int_callback = mos7840_interrupt_callback,
2596 }; 2593 };
2597 2594
2598 static struct usb_serial_driver * const serial_drivers[] = { 2595 static struct usb_serial_driver * const serial_drivers[] = {
2599 &moschip7840_4port_device, NULL 2596 &moschip7840_4port_device, NULL
2600 }; 2597 };
2601 2598
2602 module_usb_serial_driver(serial_drivers, id_table); 2599 module_usb_serial_driver(serial_drivers, id_table);
2603 2600
2604 MODULE_DESCRIPTION(DRIVER_DESC); 2601 MODULE_DESCRIPTION(DRIVER_DESC);
2605 MODULE_LICENSE("GPL"); 2602 MODULE_LICENSE("GPL");
2606 2603
drivers/usb/serial/ti_usb_3410_5052.c
1 /* vi: ts=8 sw=8 1 /* vi: ts=8 sw=8
2 * 2 *
3 * TI 3410/5052 USB Serial Driver 3 * TI 3410/5052 USB Serial Driver
4 * 4 *
5 * Copyright (C) 2004 Texas Instruments 5 * Copyright (C) 2004 Texas Instruments
6 * 6 *
7 * This driver is based on the Linux io_ti driver, which is 7 * This driver is based on the Linux io_ti driver, which is
8 * Copyright (C) 2000-2002 Inside Out Networks 8 * Copyright (C) 2000-2002 Inside Out Networks
9 * Copyright (C) 2001-2002 Greg Kroah-Hartman 9 * Copyright (C) 2001-2002 Greg Kroah-Hartman
10 * 10 *
11 * This program is free software; you can redistribute it and/or modify 11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by 12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or 13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version. 14 * (at your option) any later version.
15 * 15 *
16 * For questions or problems with this driver, contact Texas Instruments 16 * For questions or problems with this driver, contact Texas Instruments
17 * technical support, or Al Borchers <alborchers@steinerpoint.com>, or 17 * technical support, or Al Borchers <alborchers@steinerpoint.com>, or
18 * Peter Berger <pberger@brimson.com>. 18 * Peter Berger <pberger@brimson.com>.
19 */ 19 */
20 20
21 #include <linux/kernel.h> 21 #include <linux/kernel.h>
22 #include <linux/errno.h> 22 #include <linux/errno.h>
23 #include <linux/firmware.h> 23 #include <linux/firmware.h>
24 #include <linux/init.h> 24 #include <linux/init.h>
25 #include <linux/slab.h> 25 #include <linux/slab.h>
26 #include <linux/tty.h> 26 #include <linux/tty.h>
27 #include <linux/tty_driver.h> 27 #include <linux/tty_driver.h>
28 #include <linux/tty_flip.h> 28 #include <linux/tty_flip.h>
29 #include <linux/module.h> 29 #include <linux/module.h>
30 #include <linux/spinlock.h> 30 #include <linux/spinlock.h>
31 #include <linux/ioctl.h> 31 #include <linux/ioctl.h>
32 #include <linux/serial.h> 32 #include <linux/serial.h>
33 #include <linux/kfifo.h> 33 #include <linux/kfifo.h>
34 #include <linux/mutex.h> 34 #include <linux/mutex.h>
35 #include <linux/uaccess.h> 35 #include <linux/uaccess.h>
36 #include <linux/usb.h> 36 #include <linux/usb.h>
37 #include <linux/usb/serial.h> 37 #include <linux/usb/serial.h>
38 38
39 #include "ti_usb_3410_5052.h" 39 #include "ti_usb_3410_5052.h"
40 40
41 /* Defines */ 41 /* Defines */
42 42
43 #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>" 43 #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
44 #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver" 44 #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
45 45
46 #define TI_FIRMWARE_BUF_SIZE 16284 46 #define TI_FIRMWARE_BUF_SIZE 16284
47 47
48 #define TI_WRITE_BUF_SIZE 1024 48 #define TI_WRITE_BUF_SIZE 1024
49 49
50 #define TI_TRANSFER_TIMEOUT 2 50 #define TI_TRANSFER_TIMEOUT 2
51 51
52 #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */ 52 #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
53 53
54 /* supported setserial flags */ 54 /* supported setserial flags */
55 #define TI_SET_SERIAL_FLAGS 0 55 #define TI_SET_SERIAL_FLAGS 0
56 56
57 /* read urb states */ 57 /* read urb states */
58 #define TI_READ_URB_RUNNING 0 58 #define TI_READ_URB_RUNNING 0
59 #define TI_READ_URB_STOPPING 1 59 #define TI_READ_URB_STOPPING 1
60 #define TI_READ_URB_STOPPED 2 60 #define TI_READ_URB_STOPPED 2
61 61
62 #define TI_EXTRA_VID_PID_COUNT 5 62 #define TI_EXTRA_VID_PID_COUNT 5
63 63
64 64
65 /* Structures */ 65 /* Structures */
66 66
67 struct ti_port { 67 struct ti_port {
68 int tp_is_open; 68 int tp_is_open;
69 __u8 tp_msr; 69 __u8 tp_msr;
70 __u8 tp_lsr; 70 __u8 tp_lsr;
71 __u8 tp_shadow_mcr; 71 __u8 tp_shadow_mcr;
72 __u8 tp_uart_mode; /* 232 or 485 modes */ 72 __u8 tp_uart_mode; /* 232 or 485 modes */
73 unsigned int tp_uart_base_addr; 73 unsigned int tp_uart_base_addr;
74 int tp_flags; 74 int tp_flags;
75 int tp_closing_wait;/* in .01 secs */ 75 int tp_closing_wait;/* in .01 secs */
76 struct async_icount tp_icount; 76 struct async_icount tp_icount;
77 wait_queue_head_t tp_msr_wait; /* wait for msr change */ 77 wait_queue_head_t tp_msr_wait; /* wait for msr change */
78 wait_queue_head_t tp_write_wait; 78 wait_queue_head_t tp_write_wait;
79 struct ti_device *tp_tdev; 79 struct ti_device *tp_tdev;
80 struct usb_serial_port *tp_port; 80 struct usb_serial_port *tp_port;
81 spinlock_t tp_lock; 81 spinlock_t tp_lock;
82 int tp_read_urb_state; 82 int tp_read_urb_state;
83 int tp_write_urb_in_use; 83 int tp_write_urb_in_use;
84 struct kfifo write_fifo; 84 struct kfifo write_fifo;
85 }; 85 };
86 86
87 struct ti_device { 87 struct ti_device {
88 struct mutex td_open_close_lock; 88 struct mutex td_open_close_lock;
89 int td_open_port_count; 89 int td_open_port_count;
90 struct usb_serial *td_serial; 90 struct usb_serial *td_serial;
91 int td_is_3410; 91 int td_is_3410;
92 int td_urb_error; 92 int td_urb_error;
93 }; 93 };
94 94
95 95
96 /* Function Declarations */ 96 /* Function Declarations */
97 97
98 static int ti_startup(struct usb_serial *serial); 98 static int ti_startup(struct usb_serial *serial);
99 static void ti_release(struct usb_serial *serial); 99 static void ti_release(struct usb_serial *serial);
100 static int ti_port_probe(struct usb_serial_port *port); 100 static int ti_port_probe(struct usb_serial_port *port);
101 static int ti_port_remove(struct usb_serial_port *port); 101 static int ti_port_remove(struct usb_serial_port *port);
102 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port); 102 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
103 static void ti_close(struct usb_serial_port *port); 103 static void ti_close(struct usb_serial_port *port);
104 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port, 104 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
105 const unsigned char *data, int count); 105 const unsigned char *data, int count);
106 static int ti_write_room(struct tty_struct *tty); 106 static int ti_write_room(struct tty_struct *tty);
107 static int ti_chars_in_buffer(struct tty_struct *tty); 107 static int ti_chars_in_buffer(struct tty_struct *tty);
108 static void ti_throttle(struct tty_struct *tty); 108 static void ti_throttle(struct tty_struct *tty);
109 static void ti_unthrottle(struct tty_struct *tty); 109 static void ti_unthrottle(struct tty_struct *tty);
110 static int ti_ioctl(struct tty_struct *tty, 110 static int ti_ioctl(struct tty_struct *tty,
111 unsigned int cmd, unsigned long arg); 111 unsigned int cmd, unsigned long arg);
112 static int ti_get_icount(struct tty_struct *tty, 112 static int ti_get_icount(struct tty_struct *tty,
113 struct serial_icounter_struct *icount); 113 struct serial_icounter_struct *icount);
114 static void ti_set_termios(struct tty_struct *tty, 114 static void ti_set_termios(struct tty_struct *tty,
115 struct usb_serial_port *port, struct ktermios *old_termios); 115 struct usb_serial_port *port, struct ktermios *old_termios);
116 static int ti_tiocmget(struct tty_struct *tty); 116 static int ti_tiocmget(struct tty_struct *tty);
117 static int ti_tiocmset(struct tty_struct *tty, 117 static int ti_tiocmset(struct tty_struct *tty,
118 unsigned int set, unsigned int clear); 118 unsigned int set, unsigned int clear);
119 static void ti_break(struct tty_struct *tty, int break_state); 119 static void ti_break(struct tty_struct *tty, int break_state);
120 static void ti_interrupt_callback(struct urb *urb); 120 static void ti_interrupt_callback(struct urb *urb);
121 static void ti_bulk_in_callback(struct urb *urb); 121 static void ti_bulk_in_callback(struct urb *urb);
122 static void ti_bulk_out_callback(struct urb *urb); 122 static void ti_bulk_out_callback(struct urb *urb);
123 123
124 static void ti_recv(struct usb_serial_port *port, unsigned char *data, 124 static void ti_recv(struct usb_serial_port *port, unsigned char *data,
125 int length); 125 int length);
126 static void ti_send(struct ti_port *tport); 126 static void ti_send(struct ti_port *tport);
127 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr); 127 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
128 static int ti_get_lsr(struct ti_port *tport); 128 static int ti_get_lsr(struct ti_port *tport);
129 static int ti_get_serial_info(struct ti_port *tport, 129 static int ti_get_serial_info(struct ti_port *tport,
130 struct serial_struct __user *ret_arg); 130 struct serial_struct __user *ret_arg);
131 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport, 131 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
132 struct serial_struct __user *new_arg); 132 struct serial_struct __user *new_arg);
133 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr); 133 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
134 134
135 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush); 135 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);
136 136
137 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty); 137 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
138 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty); 138 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
139 139
140 static int ti_command_out_sync(struct ti_device *tdev, __u8 command, 140 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
141 __u16 moduleid, __u16 value, __u8 *data, int size); 141 __u16 moduleid, __u16 value, __u8 *data, int size);
142 static int ti_command_in_sync(struct ti_device *tdev, __u8 command, 142 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
143 __u16 moduleid, __u16 value, __u8 *data, int size); 143 __u16 moduleid, __u16 value, __u8 *data, int size);
144 144
145 static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev, 145 static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
146 unsigned long addr, __u8 mask, __u8 byte); 146 unsigned long addr, __u8 mask, __u8 byte);
147 147
148 static int ti_download_firmware(struct ti_device *tdev); 148 static int ti_download_firmware(struct ti_device *tdev);
149 149
150 150
151 /* Data */ 151 /* Data */
152 152
153 /* module parameters */ 153 /* module parameters */
154 static int closing_wait = TI_DEFAULT_CLOSING_WAIT; 154 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
155 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT]; 155 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
156 static unsigned int vendor_3410_count; 156 static unsigned int vendor_3410_count;
157 static ushort product_3410[TI_EXTRA_VID_PID_COUNT]; 157 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
158 static unsigned int product_3410_count; 158 static unsigned int product_3410_count;
159 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT]; 159 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
160 static unsigned int vendor_5052_count; 160 static unsigned int vendor_5052_count;
161 static ushort product_5052[TI_EXTRA_VID_PID_COUNT]; 161 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
162 static unsigned int product_5052_count; 162 static unsigned int product_5052_count;
163 163
164 /* supported devices */ 164 /* supported devices */
165 /* the array dimension is the number of default entries plus */ 165 /* the array dimension is the number of default entries plus */
166 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */ 166 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
167 /* null entry */ 167 /* null entry */
168 static struct usb_device_id ti_id_table_3410[15+TI_EXTRA_VID_PID_COUNT+1] = { 168 static struct usb_device_id ti_id_table_3410[15+TI_EXTRA_VID_PID_COUNT+1] = {
169 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) }, 169 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
170 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) }, 170 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
171 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) }, 171 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
172 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) }, 172 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
173 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) }, 173 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
174 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) }, 174 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
175 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) }, 175 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
176 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) }, 176 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
177 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) }, 177 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
178 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) }, 178 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
179 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) }, 179 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
180 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) }, 180 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
181 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) }, 181 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
182 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) }, 182 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
183 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) }, 183 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
184 }; 184 };
185 185
186 static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = { 186 static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
187 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) }, 187 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
188 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) }, 188 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
189 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) }, 189 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
190 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) }, 190 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
191 }; 191 };
192 192
193 static struct usb_device_id ti_id_table_combined[19+2*TI_EXTRA_VID_PID_COUNT+1] = { 193 static struct usb_device_id ti_id_table_combined[19+2*TI_EXTRA_VID_PID_COUNT+1] = {
194 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) }, 194 { USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
195 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) }, 195 { USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
196 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) }, 196 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
197 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) }, 197 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
198 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) }, 198 { USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
199 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) }, 199 { USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
200 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) }, 200 { USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
201 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) }, 201 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
202 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) }, 202 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
203 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) }, 203 { USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
204 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) }, 204 { USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
205 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) }, 205 { USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
206 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) }, 206 { USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
207 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) }, 207 { USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
208 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) }, 208 { USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
209 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) }, 209 { USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
210 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) }, 210 { USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
211 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) }, 211 { USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
212 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) }, 212 { USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
213 { } 213 { }
214 }; 214 };
215 215
216 static struct usb_serial_driver ti_1port_device = { 216 static struct usb_serial_driver ti_1port_device = {
217 .driver = { 217 .driver = {
218 .owner = THIS_MODULE, 218 .owner = THIS_MODULE,
219 .name = "ti_usb_3410_5052_1", 219 .name = "ti_usb_3410_5052_1",
220 }, 220 },
221 .description = "TI USB 3410 1 port adapter", 221 .description = "TI USB 3410 1 port adapter",
222 .id_table = ti_id_table_3410, 222 .id_table = ti_id_table_3410,
223 .num_ports = 1, 223 .num_ports = 1,
224 .attach = ti_startup, 224 .attach = ti_startup,
225 .release = ti_release, 225 .release = ti_release,
226 .port_probe = ti_port_probe, 226 .port_probe = ti_port_probe,
227 .port_remove = ti_port_remove, 227 .port_remove = ti_port_remove,
228 .open = ti_open, 228 .open = ti_open,
229 .close = ti_close, 229 .close = ti_close,
230 .write = ti_write, 230 .write = ti_write,
231 .write_room = ti_write_room, 231 .write_room = ti_write_room,
232 .chars_in_buffer = ti_chars_in_buffer, 232 .chars_in_buffer = ti_chars_in_buffer,
233 .throttle = ti_throttle, 233 .throttle = ti_throttle,
234 .unthrottle = ti_unthrottle, 234 .unthrottle = ti_unthrottle,
235 .ioctl = ti_ioctl, 235 .ioctl = ti_ioctl,
236 .set_termios = ti_set_termios, 236 .set_termios = ti_set_termios,
237 .tiocmget = ti_tiocmget, 237 .tiocmget = ti_tiocmget,
238 .tiocmset = ti_tiocmset, 238 .tiocmset = ti_tiocmset,
239 .get_icount = ti_get_icount, 239 .get_icount = ti_get_icount,
240 .break_ctl = ti_break, 240 .break_ctl = ti_break,
241 .read_int_callback = ti_interrupt_callback, 241 .read_int_callback = ti_interrupt_callback,
242 .read_bulk_callback = ti_bulk_in_callback, 242 .read_bulk_callback = ti_bulk_in_callback,
243 .write_bulk_callback = ti_bulk_out_callback, 243 .write_bulk_callback = ti_bulk_out_callback,
244 }; 244 };
245 245
246 static struct usb_serial_driver ti_2port_device = { 246 static struct usb_serial_driver ti_2port_device = {
247 .driver = { 247 .driver = {
248 .owner = THIS_MODULE, 248 .owner = THIS_MODULE,
249 .name = "ti_usb_3410_5052_2", 249 .name = "ti_usb_3410_5052_2",
250 }, 250 },
251 .description = "TI USB 5052 2 port adapter", 251 .description = "TI USB 5052 2 port adapter",
252 .id_table = ti_id_table_5052, 252 .id_table = ti_id_table_5052,
253 .num_ports = 2, 253 .num_ports = 2,
254 .attach = ti_startup, 254 .attach = ti_startup,
255 .release = ti_release, 255 .release = ti_release,
256 .port_probe = ti_port_probe, 256 .port_probe = ti_port_probe,
257 .port_remove = ti_port_remove, 257 .port_remove = ti_port_remove,
258 .open = ti_open, 258 .open = ti_open,
259 .close = ti_close, 259 .close = ti_close,
260 .write = ti_write, 260 .write = ti_write,
261 .write_room = ti_write_room, 261 .write_room = ti_write_room,
262 .chars_in_buffer = ti_chars_in_buffer, 262 .chars_in_buffer = ti_chars_in_buffer,
263 .throttle = ti_throttle, 263 .throttle = ti_throttle,
264 .unthrottle = ti_unthrottle, 264 .unthrottle = ti_unthrottle,
265 .ioctl = ti_ioctl, 265 .ioctl = ti_ioctl,
266 .set_termios = ti_set_termios, 266 .set_termios = ti_set_termios,
267 .tiocmget = ti_tiocmget, 267 .tiocmget = ti_tiocmget,
268 .tiocmset = ti_tiocmset, 268 .tiocmset = ti_tiocmset,
269 .get_icount = ti_get_icount, 269 .get_icount = ti_get_icount,
270 .break_ctl = ti_break, 270 .break_ctl = ti_break,
271 .read_int_callback = ti_interrupt_callback, 271 .read_int_callback = ti_interrupt_callback,
272 .read_bulk_callback = ti_bulk_in_callback, 272 .read_bulk_callback = ti_bulk_in_callback,
273 .write_bulk_callback = ti_bulk_out_callback, 273 .write_bulk_callback = ti_bulk_out_callback,
274 }; 274 };
275 275
276 static struct usb_serial_driver * const serial_drivers[] = { 276 static struct usb_serial_driver * const serial_drivers[] = {
277 &ti_1port_device, &ti_2port_device, NULL 277 &ti_1port_device, &ti_2port_device, NULL
278 }; 278 };
279 279
280 /* Module */ 280 /* Module */
281 281
282 MODULE_AUTHOR(TI_DRIVER_AUTHOR); 282 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
283 MODULE_DESCRIPTION(TI_DRIVER_DESC); 283 MODULE_DESCRIPTION(TI_DRIVER_DESC);
284 MODULE_LICENSE("GPL"); 284 MODULE_LICENSE("GPL");
285 285
286 MODULE_FIRMWARE("ti_3410.fw"); 286 MODULE_FIRMWARE("ti_3410.fw");
287 MODULE_FIRMWARE("ti_5052.fw"); 287 MODULE_FIRMWARE("ti_5052.fw");
288 MODULE_FIRMWARE("mts_cdma.fw"); 288 MODULE_FIRMWARE("mts_cdma.fw");
289 MODULE_FIRMWARE("mts_gsm.fw"); 289 MODULE_FIRMWARE("mts_gsm.fw");
290 MODULE_FIRMWARE("mts_edge.fw"); 290 MODULE_FIRMWARE("mts_edge.fw");
291 MODULE_FIRMWARE("mts_mt9234mu.fw"); 291 MODULE_FIRMWARE("mts_mt9234mu.fw");
292 MODULE_FIRMWARE("mts_mt9234zba.fw"); 292 MODULE_FIRMWARE("mts_mt9234zba.fw");
293 293
294 module_param(closing_wait, int, S_IRUGO | S_IWUSR); 294 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
295 MODULE_PARM_DESC(closing_wait, 295 MODULE_PARM_DESC(closing_wait,
296 "Maximum wait for data to drain in close, in .01 secs, default is 4000"); 296 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
297 297
298 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO); 298 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
299 MODULE_PARM_DESC(vendor_3410, 299 MODULE_PARM_DESC(vendor_3410,
300 "Vendor ids for 3410 based devices, 1-5 short integers"); 300 "Vendor ids for 3410 based devices, 1-5 short integers");
301 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO); 301 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
302 MODULE_PARM_DESC(product_3410, 302 MODULE_PARM_DESC(product_3410,
303 "Product ids for 3410 based devices, 1-5 short integers"); 303 "Product ids for 3410 based devices, 1-5 short integers");
304 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO); 304 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
305 MODULE_PARM_DESC(vendor_5052, 305 MODULE_PARM_DESC(vendor_5052,
306 "Vendor ids for 5052 based devices, 1-5 short integers"); 306 "Vendor ids for 5052 based devices, 1-5 short integers");
307 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO); 307 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
308 MODULE_PARM_DESC(product_5052, 308 MODULE_PARM_DESC(product_5052,
309 "Product ids for 5052 based devices, 1-5 short integers"); 309 "Product ids for 5052 based devices, 1-5 short integers");
310 310
311 MODULE_DEVICE_TABLE(usb, ti_id_table_combined); 311 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
312 312
313 313
314 /* Functions */ 314 /* Functions */
315 315
316 static int __init ti_init(void) 316 static int __init ti_init(void)
317 { 317 {
318 int i, j, c; 318 int i, j, c;
319 319
320 /* insert extra vendor and product ids */ 320 /* insert extra vendor and product ids */
321 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1; 321 c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
322 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1; 322 j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
323 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) { 323 for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
324 ti_id_table_3410[j].idVendor = vendor_3410[i]; 324 ti_id_table_3410[j].idVendor = vendor_3410[i];
325 ti_id_table_3410[j].idProduct = product_3410[i]; 325 ti_id_table_3410[j].idProduct = product_3410[i];
326 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE; 326 ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
327 ti_id_table_combined[c].idVendor = vendor_3410[i]; 327 ti_id_table_combined[c].idVendor = vendor_3410[i];
328 ti_id_table_combined[c].idProduct = product_3410[i]; 328 ti_id_table_combined[c].idProduct = product_3410[i];
329 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE; 329 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
330 } 330 }
331 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1; 331 j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
332 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) { 332 for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
333 ti_id_table_5052[j].idVendor = vendor_5052[i]; 333 ti_id_table_5052[j].idVendor = vendor_5052[i];
334 ti_id_table_5052[j].idProduct = product_5052[i]; 334 ti_id_table_5052[j].idProduct = product_5052[i];
335 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE; 335 ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
336 ti_id_table_combined[c].idVendor = vendor_5052[i]; 336 ti_id_table_combined[c].idVendor = vendor_5052[i];
337 ti_id_table_combined[c].idProduct = product_5052[i]; 337 ti_id_table_combined[c].idProduct = product_5052[i];
338 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE; 338 ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
339 } 339 }
340 340
341 return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ti_id_table_combined); 341 return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ti_id_table_combined);
342 } 342 }
343 343
344 static void __exit ti_exit(void) 344 static void __exit ti_exit(void)
345 { 345 {
346 usb_serial_deregister_drivers(serial_drivers); 346 usb_serial_deregister_drivers(serial_drivers);
347 } 347 }
348 348
349 module_init(ti_init); 349 module_init(ti_init);
350 module_exit(ti_exit); 350 module_exit(ti_exit);
351 351
352 352
353 static int ti_startup(struct usb_serial *serial) 353 static int ti_startup(struct usb_serial *serial)
354 { 354 {
355 struct ti_device *tdev; 355 struct ti_device *tdev;
356 struct usb_device *dev = serial->dev; 356 struct usb_device *dev = serial->dev;
357 int status; 357 int status;
358 358
359 dev_dbg(&dev->dev, 359 dev_dbg(&dev->dev,
360 "%s - product 0x%4X, num configurations %d, configuration value %d", 360 "%s - product 0x%4X, num configurations %d, configuration value %d",
361 __func__, le16_to_cpu(dev->descriptor.idProduct), 361 __func__, le16_to_cpu(dev->descriptor.idProduct),
362 dev->descriptor.bNumConfigurations, 362 dev->descriptor.bNumConfigurations,
363 dev->actconfig->desc.bConfigurationValue); 363 dev->actconfig->desc.bConfigurationValue);
364 364
365 /* create device structure */ 365 /* create device structure */
366 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL); 366 tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
367 if (tdev == NULL) { 367 if (tdev == NULL) {
368 dev_err(&dev->dev, "%s - out of memory\n", __func__); 368 dev_err(&dev->dev, "%s - out of memory\n", __func__);
369 return -ENOMEM; 369 return -ENOMEM;
370 } 370 }
371 mutex_init(&tdev->td_open_close_lock); 371 mutex_init(&tdev->td_open_close_lock);
372 tdev->td_serial = serial; 372 tdev->td_serial = serial;
373 usb_set_serial_data(serial, tdev); 373 usb_set_serial_data(serial, tdev);
374 374
375 /* determine device type */ 375 /* determine device type */
376 if (usb_match_id(serial->interface, ti_id_table_3410)) 376 if (usb_match_id(serial->interface, ti_id_table_3410))
377 tdev->td_is_3410 = 1; 377 tdev->td_is_3410 = 1;
378 dev_dbg(&dev->dev, "%s - device type is %s\n", __func__, 378 dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
379 tdev->td_is_3410 ? "3410" : "5052"); 379 tdev->td_is_3410 ? "3410" : "5052");
380 380
381 /* if we have only 1 configuration, download firmware */ 381 /* if we have only 1 configuration, download firmware */
382 if (dev->descriptor.bNumConfigurations == 1) { 382 if (dev->descriptor.bNumConfigurations == 1) {
383 status = ti_download_firmware(tdev); 383 status = ti_download_firmware(tdev);
384 384
385 if (status != 0) 385 if (status != 0)
386 goto free_tdev; 386 goto free_tdev;
387 387
388 /* 3410 must be reset, 5052 resets itself */ 388 /* 3410 must be reset, 5052 resets itself */
389 if (tdev->td_is_3410) { 389 if (tdev->td_is_3410) {
390 msleep_interruptible(100); 390 msleep_interruptible(100);
391 usb_reset_device(dev); 391 usb_reset_device(dev);
392 } 392 }
393 393
394 status = -ENODEV; 394 status = -ENODEV;
395 goto free_tdev; 395 goto free_tdev;
396 } 396 }
397 397
398 /* the second configuration must be set */ 398 /* the second configuration must be set */
399 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) { 399 if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
400 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG); 400 status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
401 status = status ? status : -ENODEV; 401 status = status ? status : -ENODEV;
402 goto free_tdev; 402 goto free_tdev;
403 } 403 }
404 404
405 return 0; 405 return 0;
406 406
407 free_tdev: 407 free_tdev:
408 kfree(tdev); 408 kfree(tdev);
409 usb_set_serial_data(serial, NULL); 409 usb_set_serial_data(serial, NULL);
410 return status; 410 return status;
411 } 411 }
412 412
413 413
414 static void ti_release(struct usb_serial *serial) 414 static void ti_release(struct usb_serial *serial)
415 { 415 {
416 struct ti_device *tdev = usb_get_serial_data(serial); 416 struct ti_device *tdev = usb_get_serial_data(serial);
417 417
418 kfree(tdev); 418 kfree(tdev);
419 } 419 }
420 420
421 static int ti_port_probe(struct usb_serial_port *port) 421 static int ti_port_probe(struct usb_serial_port *port)
422 { 422 {
423 struct ti_port *tport; 423 struct ti_port *tport;
424 424
425 tport = kzalloc(sizeof(*tport), GFP_KERNEL); 425 tport = kzalloc(sizeof(*tport), GFP_KERNEL);
426 if (!tport) 426 if (!tport)
427 return -ENOMEM; 427 return -ENOMEM;
428 428
429 spin_lock_init(&tport->tp_lock); 429 spin_lock_init(&tport->tp_lock);
430 if (port == port->serial->port[0]) 430 if (port == port->serial->port[0])
431 tport->tp_uart_base_addr = TI_UART1_BASE_ADDR; 431 tport->tp_uart_base_addr = TI_UART1_BASE_ADDR;
432 else 432 else
433 tport->tp_uart_base_addr = TI_UART2_BASE_ADDR; 433 tport->tp_uart_base_addr = TI_UART2_BASE_ADDR;
434 tport->tp_closing_wait = closing_wait; 434 tport->tp_closing_wait = closing_wait;
435 init_waitqueue_head(&tport->tp_msr_wait); 435 init_waitqueue_head(&tport->tp_msr_wait);
436 init_waitqueue_head(&tport->tp_write_wait); 436 init_waitqueue_head(&tport->tp_write_wait);
437 if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE, GFP_KERNEL)) { 437 if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE, GFP_KERNEL)) {
438 kfree(tport); 438 kfree(tport);
439 return -ENOMEM; 439 return -ENOMEM;
440 } 440 }
441 tport->tp_port = port; 441 tport->tp_port = port;
442 tport->tp_tdev = usb_get_serial_data(port->serial); 442 tport->tp_tdev = usb_get_serial_data(port->serial);
443 tport->tp_uart_mode = 0; /* default is RS232 */ 443 tport->tp_uart_mode = 0; /* default is RS232 */
444 444
445 usb_set_serial_port_data(port, tport); 445 usb_set_serial_port_data(port, tport);
446 446
447 return 0; 447 return 0;
448 } 448 }
449 449
450 static int ti_port_remove(struct usb_serial_port *port) 450 static int ti_port_remove(struct usb_serial_port *port)
451 { 451 {
452 struct ti_port *tport; 452 struct ti_port *tport;
453 453
454 tport = usb_get_serial_port_data(port); 454 tport = usb_get_serial_port_data(port);
455 kfifo_free(&tport->write_fifo); 455 kfifo_free(&tport->write_fifo);
456 kfree(tport); 456 kfree(tport);
457 457
458 return 0; 458 return 0;
459 } 459 }
460 460
461 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port) 461 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
462 { 462 {
463 struct ti_port *tport = usb_get_serial_port_data(port); 463 struct ti_port *tport = usb_get_serial_port_data(port);
464 struct ti_device *tdev; 464 struct ti_device *tdev;
465 struct usb_device *dev; 465 struct usb_device *dev;
466 struct urb *urb; 466 struct urb *urb;
467 int port_number; 467 int port_number;
468 int status; 468 int status;
469 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS | 469 __u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
470 TI_PIPE_TIMEOUT_ENABLE | 470 TI_PIPE_TIMEOUT_ENABLE |
471 (TI_TRANSFER_TIMEOUT << 2)); 471 (TI_TRANSFER_TIMEOUT << 2));
472 472
473 if (tport == NULL) 473 if (tport == NULL)
474 return -ENODEV; 474 return -ENODEV;
475 475
476 dev = port->serial->dev; 476 dev = port->serial->dev;
477 tdev = tport->tp_tdev; 477 tdev = tport->tp_tdev;
478 478
479 /* only one open on any port on a device at a time */ 479 /* only one open on any port on a device at a time */
480 if (mutex_lock_interruptible(&tdev->td_open_close_lock)) 480 if (mutex_lock_interruptible(&tdev->td_open_close_lock))
481 return -ERESTARTSYS; 481 return -ERESTARTSYS;
482 482
483 port_number = port->number - port->serial->minor; 483 port_number = port->number - port->serial->minor;
484 484
485 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount)); 485 memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));
486 486
487 tport->tp_msr = 0; 487 tport->tp_msr = 0;
488 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR); 488 tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
489 489
490 /* start interrupt urb the first time a port is opened on this device */ 490 /* start interrupt urb the first time a port is opened on this device */
491 if (tdev->td_open_port_count == 0) { 491 if (tdev->td_open_port_count == 0) {
492 dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__); 492 dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
493 urb = tdev->td_serial->port[0]->interrupt_in_urb; 493 urb = tdev->td_serial->port[0]->interrupt_in_urb;
494 if (!urb) { 494 if (!urb) {
495 dev_err(&port->dev, "%s - no interrupt urb\n", __func__); 495 dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
496 status = -EINVAL; 496 status = -EINVAL;
497 goto release_lock; 497 goto release_lock;
498 } 498 }
499 urb->context = tdev; 499 urb->context = tdev;
500 status = usb_submit_urb(urb, GFP_KERNEL); 500 status = usb_submit_urb(urb, GFP_KERNEL);
501 if (status) { 501 if (status) {
502 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status); 502 dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
503 goto release_lock; 503 goto release_lock;
504 } 504 }
505 } 505 }
506 506
507 if (tty) 507 if (tty)
508 ti_set_termios(tty, port, &tty->termios); 508 ti_set_termios(tty, port, &tty->termios);
509 509
510 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT\n", __func__); 510 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT\n", __func__);
511 status = ti_command_out_sync(tdev, TI_OPEN_PORT, 511 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
512 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0); 512 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
513 if (status) { 513 if (status) {
514 dev_err(&port->dev, "%s - cannot send open command, %d\n", 514 dev_err(&port->dev, "%s - cannot send open command, %d\n",
515 __func__, status); 515 __func__, status);
516 goto unlink_int_urb; 516 goto unlink_int_urb;
517 } 517 }
518 518
519 dev_dbg(&port->dev, "%s - sending TI_START_PORT\n", __func__); 519 dev_dbg(&port->dev, "%s - sending TI_START_PORT\n", __func__);
520 status = ti_command_out_sync(tdev, TI_START_PORT, 520 status = ti_command_out_sync(tdev, TI_START_PORT,
521 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0); 521 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
522 if (status) { 522 if (status) {
523 dev_err(&port->dev, "%s - cannot send start command, %d\n", 523 dev_err(&port->dev, "%s - cannot send start command, %d\n",
524 __func__, status); 524 __func__, status);
525 goto unlink_int_urb; 525 goto unlink_int_urb;
526 } 526 }
527 527
528 dev_dbg(&port->dev, "%s - sending TI_PURGE_PORT\n", __func__); 528 dev_dbg(&port->dev, "%s - sending TI_PURGE_PORT\n", __func__);
529 status = ti_command_out_sync(tdev, TI_PURGE_PORT, 529 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
530 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0); 530 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
531 if (status) { 531 if (status) {
532 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n", 532 dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
533 __func__, status); 533 __func__, status);
534 goto unlink_int_urb; 534 goto unlink_int_urb;
535 } 535 }
536 status = ti_command_out_sync(tdev, TI_PURGE_PORT, 536 status = ti_command_out_sync(tdev, TI_PURGE_PORT,
537 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0); 537 (__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
538 if (status) { 538 if (status) {
539 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n", 539 dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
540 __func__, status); 540 __func__, status);
541 goto unlink_int_urb; 541 goto unlink_int_urb;
542 } 542 }
543 543
544 /* reset the data toggle on the bulk endpoints to work around bug in 544 /* reset the data toggle on the bulk endpoints to work around bug in
545 * host controllers where things get out of sync some times */ 545 * host controllers where things get out of sync some times */
546 usb_clear_halt(dev, port->write_urb->pipe); 546 usb_clear_halt(dev, port->write_urb->pipe);
547 usb_clear_halt(dev, port->read_urb->pipe); 547 usb_clear_halt(dev, port->read_urb->pipe);
548 548
549 if (tty) 549 if (tty)
550 ti_set_termios(tty, port, &tty->termios); 550 ti_set_termios(tty, port, &tty->termios);
551 551
552 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT (2)\n", __func__); 552 dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT (2)\n", __func__);
553 status = ti_command_out_sync(tdev, TI_OPEN_PORT, 553 status = ti_command_out_sync(tdev, TI_OPEN_PORT,
554 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0); 554 (__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
555 if (status) { 555 if (status) {
556 dev_err(&port->dev, "%s - cannot send open command (2), %d\n", 556 dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
557 __func__, status); 557 __func__, status);
558 goto unlink_int_urb; 558 goto unlink_int_urb;
559 } 559 }
560 560
561 dev_dbg(&port->dev, "%s - sending TI_START_PORT (2)\n", __func__); 561 dev_dbg(&port->dev, "%s - sending TI_START_PORT (2)\n", __func__);
562 status = ti_command_out_sync(tdev, TI_START_PORT, 562 status = ti_command_out_sync(tdev, TI_START_PORT,
563 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0); 563 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
564 if (status) { 564 if (status) {
565 dev_err(&port->dev, "%s - cannot send start command (2), %d\n", 565 dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
566 __func__, status); 566 __func__, status);
567 goto unlink_int_urb; 567 goto unlink_int_urb;
568 } 568 }
569 569
570 /* start read urb */ 570 /* start read urb */
571 dev_dbg(&port->dev, "%s - start read urb\n", __func__); 571 dev_dbg(&port->dev, "%s - start read urb\n", __func__);
572 urb = port->read_urb; 572 urb = port->read_urb;
573 if (!urb) { 573 if (!urb) {
574 dev_err(&port->dev, "%s - no read urb\n", __func__); 574 dev_err(&port->dev, "%s - no read urb\n", __func__);
575 status = -EINVAL; 575 status = -EINVAL;
576 goto unlink_int_urb; 576 goto unlink_int_urb;
577 } 577 }
578 tport->tp_read_urb_state = TI_READ_URB_RUNNING; 578 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
579 urb->context = tport; 579 urb->context = tport;
580 status = usb_submit_urb(urb, GFP_KERNEL); 580 status = usb_submit_urb(urb, GFP_KERNEL);
581 if (status) { 581 if (status) {
582 dev_err(&port->dev, "%s - submit read urb failed, %d\n", 582 dev_err(&port->dev, "%s - submit read urb failed, %d\n",
583 __func__, status); 583 __func__, status);
584 goto unlink_int_urb; 584 goto unlink_int_urb;
585 } 585 }
586 586
587 tport->tp_is_open = 1; 587 tport->tp_is_open = 1;
588 ++tdev->td_open_port_count; 588 ++tdev->td_open_port_count;
589 589
590 goto release_lock; 590 goto release_lock;
591 591
592 unlink_int_urb: 592 unlink_int_urb:
593 if (tdev->td_open_port_count == 0) 593 if (tdev->td_open_port_count == 0)
594 usb_kill_urb(port->serial->port[0]->interrupt_in_urb); 594 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
595 release_lock: 595 release_lock:
596 mutex_unlock(&tdev->td_open_close_lock); 596 mutex_unlock(&tdev->td_open_close_lock);
597 dev_dbg(&port->dev, "%s - exit %d\n", __func__, status); 597 dev_dbg(&port->dev, "%s - exit %d\n", __func__, status);
598 return status; 598 return status;
599 } 599 }
600 600
601 601
602 static void ti_close(struct usb_serial_port *port) 602 static void ti_close(struct usb_serial_port *port)
603 { 603 {
604 struct ti_device *tdev; 604 struct ti_device *tdev;
605 struct ti_port *tport; 605 struct ti_port *tport;
606 int port_number; 606 int port_number;
607 int status; 607 int status;
608 int do_unlock; 608 int do_unlock;
609 609
610 tdev = usb_get_serial_data(port->serial); 610 tdev = usb_get_serial_data(port->serial);
611 tport = usb_get_serial_port_data(port); 611 tport = usb_get_serial_port_data(port);
612 if (tdev == NULL || tport == NULL) 612 if (tdev == NULL || tport == NULL)
613 return; 613 return;
614 614
615 tport->tp_is_open = 0; 615 tport->tp_is_open = 0;
616 616
617 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1); 617 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);
618 618
619 usb_kill_urb(port->read_urb); 619 usb_kill_urb(port->read_urb);
620 usb_kill_urb(port->write_urb); 620 usb_kill_urb(port->write_urb);
621 tport->tp_write_urb_in_use = 0; 621 tport->tp_write_urb_in_use = 0;
622 622
623 port_number = port->number - port->serial->minor; 623 port_number = port->number - port->serial->minor;
624 624
625 dev_dbg(&port->dev, "%s - sending TI_CLOSE_PORT\n", __func__); 625 dev_dbg(&port->dev, "%s - sending TI_CLOSE_PORT\n", __func__);
626 status = ti_command_out_sync(tdev, TI_CLOSE_PORT, 626 status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
627 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0); 627 (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
628 if (status) 628 if (status)
629 dev_err(&port->dev, 629 dev_err(&port->dev,
630 "%s - cannot send close port command, %d\n" 630 "%s - cannot send close port command, %d\n"
631 , __func__, status); 631 , __func__, status);
632 632
633 /* if mutex_lock is interrupted, continue anyway */ 633 /* if mutex_lock is interrupted, continue anyway */
634 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock); 634 do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
635 --tport->tp_tdev->td_open_port_count; 635 --tport->tp_tdev->td_open_port_count;
636 if (tport->tp_tdev->td_open_port_count <= 0) { 636 if (tport->tp_tdev->td_open_port_count <= 0) {
637 /* last port is closed, shut down interrupt urb */ 637 /* last port is closed, shut down interrupt urb */
638 usb_kill_urb(port->serial->port[0]->interrupt_in_urb); 638 usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
639 tport->tp_tdev->td_open_port_count = 0; 639 tport->tp_tdev->td_open_port_count = 0;
640 } 640 }
641 if (do_unlock) 641 if (do_unlock)
642 mutex_unlock(&tdev->td_open_close_lock); 642 mutex_unlock(&tdev->td_open_close_lock);
643 } 643 }
644 644
645 645
646 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port, 646 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
647 const unsigned char *data, int count) 647 const unsigned char *data, int count)
648 { 648 {
649 struct ti_port *tport = usb_get_serial_port_data(port); 649 struct ti_port *tport = usb_get_serial_port_data(port);
650 650
651 if (count == 0) { 651 if (count == 0) {
652 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__); 652 dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
653 return 0; 653 return 0;
654 } 654 }
655 655
656 if (tport == NULL || !tport->tp_is_open) 656 if (tport == NULL || !tport->tp_is_open)
657 return -ENODEV; 657 return -ENODEV;
658 658
659 count = kfifo_in_locked(&tport->write_fifo, data, count, 659 count = kfifo_in_locked(&tport->write_fifo, data, count,
660 &tport->tp_lock); 660 &tport->tp_lock);
661 ti_send(tport); 661 ti_send(tport);
662 662
663 return count; 663 return count;
664 } 664 }
665 665
666 666
667 static int ti_write_room(struct tty_struct *tty) 667 static int ti_write_room(struct tty_struct *tty)
668 { 668 {
669 struct usb_serial_port *port = tty->driver_data; 669 struct usb_serial_port *port = tty->driver_data;
670 struct ti_port *tport = usb_get_serial_port_data(port); 670 struct ti_port *tport = usb_get_serial_port_data(port);
671 int room = 0; 671 int room = 0;
672 unsigned long flags; 672 unsigned long flags;
673 673
674 if (tport == NULL) 674 if (tport == NULL)
675 return 0; 675 return 0;
676 676
677 spin_lock_irqsave(&tport->tp_lock, flags); 677 spin_lock_irqsave(&tport->tp_lock, flags);
678 room = kfifo_avail(&tport->write_fifo); 678 room = kfifo_avail(&tport->write_fifo);
679 spin_unlock_irqrestore(&tport->tp_lock, flags); 679 spin_unlock_irqrestore(&tport->tp_lock, flags);
680 680
681 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room); 681 dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
682 return room; 682 return room;
683 } 683 }
684 684
685 685
686 static int ti_chars_in_buffer(struct tty_struct *tty) 686 static int ti_chars_in_buffer(struct tty_struct *tty)
687 { 687 {
688 struct usb_serial_port *port = tty->driver_data; 688 struct usb_serial_port *port = tty->driver_data;
689 struct ti_port *tport = usb_get_serial_port_data(port); 689 struct ti_port *tport = usb_get_serial_port_data(port);
690 int chars = 0; 690 int chars = 0;
691 unsigned long flags; 691 unsigned long flags;
692 692
693 if (tport == NULL) 693 if (tport == NULL)
694 return 0; 694 return 0;
695 695
696 spin_lock_irqsave(&tport->tp_lock, flags); 696 spin_lock_irqsave(&tport->tp_lock, flags);
697 chars = kfifo_len(&tport->write_fifo); 697 chars = kfifo_len(&tport->write_fifo);
698 spin_unlock_irqrestore(&tport->tp_lock, flags); 698 spin_unlock_irqrestore(&tport->tp_lock, flags);
699 699
700 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars); 700 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
701 return chars; 701 return chars;
702 } 702 }
703 703
704 704
705 static void ti_throttle(struct tty_struct *tty) 705 static void ti_throttle(struct tty_struct *tty)
706 { 706 {
707 struct usb_serial_port *port = tty->driver_data; 707 struct usb_serial_port *port = tty->driver_data;
708 struct ti_port *tport = usb_get_serial_port_data(port); 708 struct ti_port *tport = usb_get_serial_port_data(port);
709 709
710 if (tport == NULL) 710 if (tport == NULL)
711 return; 711 return;
712 712
713 if (I_IXOFF(tty) || C_CRTSCTS(tty)) 713 if (I_IXOFF(tty) || C_CRTSCTS(tty))
714 ti_stop_read(tport, tty); 714 ti_stop_read(tport, tty);
715 715
716 } 716 }
717 717
718 718
719 static void ti_unthrottle(struct tty_struct *tty) 719 static void ti_unthrottle(struct tty_struct *tty)
720 { 720 {
721 struct usb_serial_port *port = tty->driver_data; 721 struct usb_serial_port *port = tty->driver_data;
722 struct ti_port *tport = usb_get_serial_port_data(port); 722 struct ti_port *tport = usb_get_serial_port_data(port);
723 int status; 723 int status;
724 724
725 if (tport == NULL) 725 if (tport == NULL)
726 return; 726 return;
727 727
728 if (I_IXOFF(tty) || C_CRTSCTS(tty)) { 728 if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
729 status = ti_restart_read(tport, tty); 729 status = ti_restart_read(tport, tty);
730 if (status) 730 if (status)
731 dev_err(&port->dev, "%s - cannot restart read, %d\n", 731 dev_err(&port->dev, "%s - cannot restart read, %d\n",
732 __func__, status); 732 __func__, status);
733 } 733 }
734 } 734 }
735 735
736 static int ti_get_icount(struct tty_struct *tty, 736 static int ti_get_icount(struct tty_struct *tty,
737 struct serial_icounter_struct *icount) 737 struct serial_icounter_struct *icount)
738 { 738 {
739 struct usb_serial_port *port = tty->driver_data; 739 struct usb_serial_port *port = tty->driver_data;
740 struct ti_port *tport = usb_get_serial_port_data(port); 740 struct ti_port *tport = usb_get_serial_port_data(port);
741 struct async_icount cnow = tport->tp_icount; 741 struct async_icount cnow = tport->tp_icount;
742 742
743 dev_dbg(&port->dev, "%s - TIOCGICOUNT RX=%d, TX=%d\n", __func__, 743 dev_dbg(&port->dev, "%s - TIOCGICOUNT RX=%d, TX=%d\n", __func__,
744 cnow.rx, cnow.tx); 744 cnow.rx, cnow.tx);
745 745
746 icount->cts = cnow.cts; 746 icount->cts = cnow.cts;
747 icount->dsr = cnow.dsr; 747 icount->dsr = cnow.dsr;
748 icount->rng = cnow.rng; 748 icount->rng = cnow.rng;
749 icount->dcd = cnow.dcd; 749 icount->dcd = cnow.dcd;
750 icount->rx = cnow.rx; 750 icount->rx = cnow.rx;
751 icount->tx = cnow.tx; 751 icount->tx = cnow.tx;
752 icount->frame = cnow.frame; 752 icount->frame = cnow.frame;
753 icount->overrun = cnow.overrun; 753 icount->overrun = cnow.overrun;
754 icount->parity = cnow.parity; 754 icount->parity = cnow.parity;
755 icount->brk = cnow.brk; 755 icount->brk = cnow.brk;
756 icount->buf_overrun = cnow.buf_overrun; 756 icount->buf_overrun = cnow.buf_overrun;
757 757
758 return 0; 758 return 0;
759 } 759 }
760 760
761 static int ti_ioctl(struct tty_struct *tty, 761 static int ti_ioctl(struct tty_struct *tty,
762 unsigned int cmd, unsigned long arg) 762 unsigned int cmd, unsigned long arg)
763 { 763 {
764 struct usb_serial_port *port = tty->driver_data; 764 struct usb_serial_port *port = tty->driver_data;
765 struct ti_port *tport = usb_get_serial_port_data(port); 765 struct ti_port *tport = usb_get_serial_port_data(port);
766 struct async_icount cnow; 766 struct async_icount cnow;
767 struct async_icount cprev; 767 struct async_icount cprev;
768 768
769 dev_dbg(&port->dev, "%s - cmd = 0x%04X\n", __func__, cmd); 769 dev_dbg(&port->dev, "%s - cmd = 0x%04X\n", __func__, cmd);
770 770
771 if (tport == NULL) 771 if (tport == NULL)
772 return -ENODEV; 772 return -ENODEV;
773 773
774 switch (cmd) { 774 switch (cmd) {
775 case TIOCGSERIAL: 775 case TIOCGSERIAL:
776 dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__); 776 dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
777 return ti_get_serial_info(tport, 777 return ti_get_serial_info(tport,
778 (struct serial_struct __user *)arg); 778 (struct serial_struct __user *)arg);
779 case TIOCSSERIAL: 779 case TIOCSSERIAL:
780 dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__); 780 dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__);
781 return ti_set_serial_info(tty, tport, 781 return ti_set_serial_info(tty, tport,
782 (struct serial_struct __user *)arg); 782 (struct serial_struct __user *)arg);
783 case TIOCMIWAIT: 783 case TIOCMIWAIT:
784 dev_dbg(&port->dev, "%s - TIOCMIWAIT\n", __func__); 784 dev_dbg(&port->dev, "%s - TIOCMIWAIT\n", __func__);
785 cprev = tport->tp_icount; 785 cprev = tport->tp_icount;
786 while (1) { 786 while (1) {
787 interruptible_sleep_on(&tport->tp_msr_wait); 787 interruptible_sleep_on(&tport->tp_msr_wait);
788 if (signal_pending(current)) 788 if (signal_pending(current))
789 return -ERESTARTSYS; 789 return -ERESTARTSYS;
790 cnow = tport->tp_icount; 790 cnow = tport->tp_icount;
791 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 791 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
792 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) 792 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
793 return -EIO; /* no change => error */ 793 return -EIO; /* no change => error */
794 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || 794 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
795 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || 795 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
796 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || 796 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
797 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) 797 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)))
798 return 0; 798 return 0;
799 cprev = cnow; 799 cprev = cnow;
800 } 800 }
801 break; 801 break;
802 } 802 }
803 return -ENOIOCTLCMD; 803 return -ENOIOCTLCMD;
804 } 804 }
805 805
806 806
807 static void ti_set_termios(struct tty_struct *tty, 807 static void ti_set_termios(struct tty_struct *tty,
808 struct usb_serial_port *port, struct ktermios *old_termios) 808 struct usb_serial_port *port, struct ktermios *old_termios)
809 { 809 {
810 struct ti_port *tport = usb_get_serial_port_data(port); 810 struct ti_port *tport = usb_get_serial_port_data(port);
811 struct ti_uart_config *config; 811 struct ti_uart_config *config;
812 tcflag_t cflag, iflag; 812 tcflag_t cflag, iflag;
813 int baud; 813 int baud;
814 int status; 814 int status;
815 int port_number = port->number - port->serial->minor; 815 int port_number = port->number - port->serial->minor;
816 unsigned int mcr; 816 unsigned int mcr;
817 817
818 cflag = tty->termios.c_cflag; 818 cflag = tty->termios.c_cflag;
819 iflag = tty->termios.c_iflag; 819 iflag = tty->termios.c_iflag;
820 820
821 dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag); 821 dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
822 dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__, 822 dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__,
823 old_termios->c_cflag, old_termios->c_iflag); 823 old_termios->c_cflag, old_termios->c_iflag);
824 824
825 if (tport == NULL) 825 if (tport == NULL)
826 return; 826 return;
827 827
828 config = kmalloc(sizeof(*config), GFP_KERNEL); 828 config = kmalloc(sizeof(*config), GFP_KERNEL);
829 if (!config) { 829 if (!config) {
830 dev_err(&port->dev, "%s - out of memory\n", __func__); 830 dev_err(&port->dev, "%s - out of memory\n", __func__);
831 return; 831 return;
832 } 832 }
833 833
834 config->wFlags = 0; 834 config->wFlags = 0;
835 835
836 /* these flags must be set */ 836 /* these flags must be set */
837 config->wFlags |= TI_UART_ENABLE_MS_INTS; 837 config->wFlags |= TI_UART_ENABLE_MS_INTS;
838 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA; 838 config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
839 config->bUartMode = (__u8)(tport->tp_uart_mode); 839 config->bUartMode = (__u8)(tport->tp_uart_mode);
840 840
841 switch (cflag & CSIZE) { 841 switch (cflag & CSIZE) {
842 case CS5: 842 case CS5:
843 config->bDataBits = TI_UART_5_DATA_BITS; 843 config->bDataBits = TI_UART_5_DATA_BITS;
844 break; 844 break;
845 case CS6: 845 case CS6:
846 config->bDataBits = TI_UART_6_DATA_BITS; 846 config->bDataBits = TI_UART_6_DATA_BITS;
847 break; 847 break;
848 case CS7: 848 case CS7:
849 config->bDataBits = TI_UART_7_DATA_BITS; 849 config->bDataBits = TI_UART_7_DATA_BITS;
850 break; 850 break;
851 default: 851 default:
852 case CS8: 852 case CS8:
853 config->bDataBits = TI_UART_8_DATA_BITS; 853 config->bDataBits = TI_UART_8_DATA_BITS;
854 break; 854 break;
855 } 855 }
856 856
857 /* CMSPAR isn't supported by this driver */ 857 /* CMSPAR isn't supported by this driver */
858 tty->termios.c_cflag &= ~CMSPAR; 858 tty->termios.c_cflag &= ~CMSPAR;
859 859
860 if (cflag & PARENB) { 860 if (cflag & PARENB) {
861 if (cflag & PARODD) { 861 if (cflag & PARODD) {
862 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING; 862 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
863 config->bParity = TI_UART_ODD_PARITY; 863 config->bParity = TI_UART_ODD_PARITY;
864 } else { 864 } else {
865 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING; 865 config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
866 config->bParity = TI_UART_EVEN_PARITY; 866 config->bParity = TI_UART_EVEN_PARITY;
867 } 867 }
868 } else { 868 } else {
869 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING; 869 config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
870 config->bParity = TI_UART_NO_PARITY; 870 config->bParity = TI_UART_NO_PARITY;
871 } 871 }
872 872
873 if (cflag & CSTOPB) 873 if (cflag & CSTOPB)
874 config->bStopBits = TI_UART_2_STOP_BITS; 874 config->bStopBits = TI_UART_2_STOP_BITS;
875 else 875 else
876 config->bStopBits = TI_UART_1_STOP_BITS; 876 config->bStopBits = TI_UART_1_STOP_BITS;
877 877
878 if (cflag & CRTSCTS) { 878 if (cflag & CRTSCTS) {
879 /* RTS flow control must be off to drop RTS for baud rate B0 */ 879 /* RTS flow control must be off to drop RTS for baud rate B0 */
880 if ((cflag & CBAUD) != B0) 880 if ((cflag & CBAUD) != B0)
881 config->wFlags |= TI_UART_ENABLE_RTS_IN; 881 config->wFlags |= TI_UART_ENABLE_RTS_IN;
882 config->wFlags |= TI_UART_ENABLE_CTS_OUT; 882 config->wFlags |= TI_UART_ENABLE_CTS_OUT;
883 } else { 883 } else {
884 tty->hw_stopped = 0; 884 tty->hw_stopped = 0;
885 ti_restart_read(tport, tty); 885 ti_restart_read(tport, tty);
886 } 886 }
887 887
888 if (I_IXOFF(tty) || I_IXON(tty)) { 888 if (I_IXOFF(tty) || I_IXON(tty)) {
889 config->cXon = START_CHAR(tty); 889 config->cXon = START_CHAR(tty);
890 config->cXoff = STOP_CHAR(tty); 890 config->cXoff = STOP_CHAR(tty);
891 891
892 if (I_IXOFF(tty)) 892 if (I_IXOFF(tty))
893 config->wFlags |= TI_UART_ENABLE_X_IN; 893 config->wFlags |= TI_UART_ENABLE_X_IN;
894 else 894 else
895 ti_restart_read(tport, tty); 895 ti_restart_read(tport, tty);
896 896
897 if (I_IXON(tty)) 897 if (I_IXON(tty))
898 config->wFlags |= TI_UART_ENABLE_X_OUT; 898 config->wFlags |= TI_UART_ENABLE_X_OUT;
899 } 899 }
900 900
901 baud = tty_get_baud_rate(tty); 901 baud = tty_get_baud_rate(tty);
902 if (!baud) 902 if (!baud)
903 baud = 9600; 903 baud = 9600;
904 if (tport->tp_tdev->td_is_3410) 904 if (tport->tp_tdev->td_is_3410)
905 config->wBaudRate = (__u16)((923077 + baud/2) / baud); 905 config->wBaudRate = (__u16)((923077 + baud/2) / baud);
906 else 906 else
907 config->wBaudRate = (__u16)((461538 + baud/2) / baud); 907 config->wBaudRate = (__u16)((461538 + baud/2) / baud);
908 908
909 /* FIXME: Should calculate resulting baud here and report it back */ 909 /* FIXME: Should calculate resulting baud here and report it back */
910 if ((cflag & CBAUD) != B0) 910 if ((cflag & CBAUD) != B0)
911 tty_encode_baud_rate(tty, baud, baud); 911 tty_encode_baud_rate(tty, baud, baud);
912 912
913 dev_dbg(&port->dev, 913 dev_dbg(&port->dev,
914 "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d", 914 "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
915 __func__, baud, config->wBaudRate, config->wFlags, 915 __func__, baud, config->wBaudRate, config->wFlags,
916 config->bDataBits, config->bParity, config->bStopBits, 916 config->bDataBits, config->bParity, config->bStopBits,
917 config->cXon, config->cXoff, config->bUartMode); 917 config->cXon, config->cXoff, config->bUartMode);
918 918
919 cpu_to_be16s(&config->wBaudRate); 919 cpu_to_be16s(&config->wBaudRate);
920 cpu_to_be16s(&config->wFlags); 920 cpu_to_be16s(&config->wFlags);
921 921
922 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG, 922 status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
923 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config, 923 (__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
924 sizeof(*config)); 924 sizeof(*config));
925 if (status) 925 if (status)
926 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n", 926 dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
927 __func__, port_number, status); 927 __func__, port_number, status);
928 928
929 /* SET_CONFIG asserts RTS and DTR, reset them correctly */ 929 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
930 mcr = tport->tp_shadow_mcr; 930 mcr = tport->tp_shadow_mcr;
931 /* if baud rate is B0, clear RTS and DTR */ 931 /* if baud rate is B0, clear RTS and DTR */
932 if ((cflag & CBAUD) == B0) 932 if ((cflag & CBAUD) == B0)
933 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS); 933 mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
934 status = ti_set_mcr(tport, mcr); 934 status = ti_set_mcr(tport, mcr);
935 if (status) 935 if (status)
936 dev_err(&port->dev, 936 dev_err(&port->dev,
937 "%s - cannot set modem control on port %d, %d\n", 937 "%s - cannot set modem control on port %d, %d\n",
938 __func__, port_number, status); 938 __func__, port_number, status);
939 939
940 kfree(config); 940 kfree(config);
941 } 941 }
942 942
943 943
944 static int ti_tiocmget(struct tty_struct *tty) 944 static int ti_tiocmget(struct tty_struct *tty)
945 { 945 {
946 struct usb_serial_port *port = tty->driver_data; 946 struct usb_serial_port *port = tty->driver_data;
947 struct ti_port *tport = usb_get_serial_port_data(port); 947 struct ti_port *tport = usb_get_serial_port_data(port);
948 unsigned int result; 948 unsigned int result;
949 unsigned int msr; 949 unsigned int msr;
950 unsigned int mcr; 950 unsigned int mcr;
951 unsigned long flags; 951 unsigned long flags;
952 952
953 if (tport == NULL) 953 if (tport == NULL)
954 return -ENODEV; 954 return -ENODEV;
955 955
956 spin_lock_irqsave(&tport->tp_lock, flags); 956 spin_lock_irqsave(&tport->tp_lock, flags);
957 msr = tport->tp_msr; 957 msr = tport->tp_msr;
958 mcr = tport->tp_shadow_mcr; 958 mcr = tport->tp_shadow_mcr;
959 spin_unlock_irqrestore(&tport->tp_lock, flags); 959 spin_unlock_irqrestore(&tport->tp_lock, flags);
960 960
961 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0) 961 result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
962 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0) 962 | ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
963 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0) 963 | ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
964 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0) 964 | ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
965 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0) 965 | ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
966 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0) 966 | ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
967 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0); 967 | ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
968 968
969 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result); 969 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
970 970
971 return result; 971 return result;
972 } 972 }
973 973
974 974
975 static int ti_tiocmset(struct tty_struct *tty, 975 static int ti_tiocmset(struct tty_struct *tty,
976 unsigned int set, unsigned int clear) 976 unsigned int set, unsigned int clear)
977 { 977 {
978 struct usb_serial_port *port = tty->driver_data; 978 struct usb_serial_port *port = tty->driver_data;
979 struct ti_port *tport = usb_get_serial_port_data(port); 979 struct ti_port *tport = usb_get_serial_port_data(port);
980 unsigned int mcr; 980 unsigned int mcr;
981 unsigned long flags; 981 unsigned long flags;
982 982
983 if (tport == NULL) 983 if (tport == NULL)
984 return -ENODEV; 984 return -ENODEV;
985 985
986 spin_lock_irqsave(&tport->tp_lock, flags); 986 spin_lock_irqsave(&tport->tp_lock, flags);
987 mcr = tport->tp_shadow_mcr; 987 mcr = tport->tp_shadow_mcr;
988 988
989 if (set & TIOCM_RTS) 989 if (set & TIOCM_RTS)
990 mcr |= TI_MCR_RTS; 990 mcr |= TI_MCR_RTS;
991 if (set & TIOCM_DTR) 991 if (set & TIOCM_DTR)
992 mcr |= TI_MCR_DTR; 992 mcr |= TI_MCR_DTR;
993 if (set & TIOCM_LOOP) 993 if (set & TIOCM_LOOP)
994 mcr |= TI_MCR_LOOP; 994 mcr |= TI_MCR_LOOP;
995 995
996 if (clear & TIOCM_RTS) 996 if (clear & TIOCM_RTS)
997 mcr &= ~TI_MCR_RTS; 997 mcr &= ~TI_MCR_RTS;
998 if (clear & TIOCM_DTR) 998 if (clear & TIOCM_DTR)
999 mcr &= ~TI_MCR_DTR; 999 mcr &= ~TI_MCR_DTR;
1000 if (clear & TIOCM_LOOP) 1000 if (clear & TIOCM_LOOP)
1001 mcr &= ~TI_MCR_LOOP; 1001 mcr &= ~TI_MCR_LOOP;
1002 spin_unlock_irqrestore(&tport->tp_lock, flags); 1002 spin_unlock_irqrestore(&tport->tp_lock, flags);
1003 1003
1004 return ti_set_mcr(tport, mcr); 1004 return ti_set_mcr(tport, mcr);
1005 } 1005 }
1006 1006
1007 1007
1008 static void ti_break(struct tty_struct *tty, int break_state) 1008 static void ti_break(struct tty_struct *tty, int break_state)
1009 { 1009 {
1010 struct usb_serial_port *port = tty->driver_data; 1010 struct usb_serial_port *port = tty->driver_data;
1011 struct ti_port *tport = usb_get_serial_port_data(port); 1011 struct ti_port *tport = usb_get_serial_port_data(port);
1012 int status; 1012 int status;
1013 1013
1014 dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state); 1014 dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
1015 1015
1016 if (tport == NULL) 1016 if (tport == NULL)
1017 return; 1017 return;
1018 1018
1019 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0); 1019 ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);
1020 1020
1021 status = ti_write_byte(port, tport->tp_tdev, 1021 status = ti_write_byte(port, tport->tp_tdev,
1022 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR, 1022 tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
1023 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0); 1023 TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
1024 1024
1025 if (status) 1025 if (status)
1026 dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status); 1026 dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
1027 } 1027 }
1028 1028
1029 1029
1030 static void ti_interrupt_callback(struct urb *urb) 1030 static void ti_interrupt_callback(struct urb *urb)
1031 { 1031 {
1032 struct ti_device *tdev = urb->context; 1032 struct ti_device *tdev = urb->context;
1033 struct usb_serial_port *port; 1033 struct usb_serial_port *port;
1034 struct usb_serial *serial = tdev->td_serial; 1034 struct usb_serial *serial = tdev->td_serial;
1035 struct ti_port *tport; 1035 struct ti_port *tport;
1036 struct device *dev = &urb->dev->dev; 1036 struct device *dev = &urb->dev->dev;
1037 unsigned char *data = urb->transfer_buffer; 1037 unsigned char *data = urb->transfer_buffer;
1038 int length = urb->actual_length; 1038 int length = urb->actual_length;
1039 int port_number; 1039 int port_number;
1040 int function; 1040 int function;
1041 int status = urb->status; 1041 int status = urb->status;
1042 int retval; 1042 int retval;
1043 __u8 msr; 1043 __u8 msr;
1044 1044
1045 switch (status) { 1045 switch (status) {
1046 case 0: 1046 case 0:
1047 break; 1047 break;
1048 case -ECONNRESET: 1048 case -ECONNRESET:
1049 case -ENOENT: 1049 case -ENOENT:
1050 case -ESHUTDOWN: 1050 case -ESHUTDOWN:
1051 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status); 1051 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1052 tdev->td_urb_error = 1; 1052 tdev->td_urb_error = 1;
1053 return; 1053 return;
1054 default: 1054 default:
1055 dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status); 1055 dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
1056 tdev->td_urb_error = 1; 1056 tdev->td_urb_error = 1;
1057 goto exit; 1057 goto exit;
1058 } 1058 }
1059 1059
1060 if (length != 2) { 1060 if (length != 2) {
1061 dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length); 1061 dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
1062 goto exit; 1062 goto exit;
1063 } 1063 }
1064 1064
1065 if (data[0] == TI_CODE_HARDWARE_ERROR) { 1065 if (data[0] == TI_CODE_HARDWARE_ERROR) {
1066 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]); 1066 dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1067 goto exit; 1067 goto exit;
1068 } 1068 }
1069 1069
1070 port_number = TI_GET_PORT_FROM_CODE(data[0]); 1070 port_number = TI_GET_PORT_FROM_CODE(data[0]);
1071 function = TI_GET_FUNC_FROM_CODE(data[0]); 1071 function = TI_GET_FUNC_FROM_CODE(data[0]);
1072 1072
1073 dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n", 1073 dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
1074 __func__, port_number, function, data[1]); 1074 __func__, port_number, function, data[1]);
1075 1075
1076 if (port_number >= serial->num_ports) { 1076 if (port_number >= serial->num_ports) {
1077 dev_err(dev, "%s - bad port number, %d\n", 1077 dev_err(dev, "%s - bad port number, %d\n",
1078 __func__, port_number); 1078 __func__, port_number);
1079 goto exit; 1079 goto exit;
1080 } 1080 }
1081 1081
1082 port = serial->port[port_number]; 1082 port = serial->port[port_number];
1083 1083
1084 tport = usb_get_serial_port_data(port); 1084 tport = usb_get_serial_port_data(port);
1085 if (!tport) 1085 if (!tport)
1086 goto exit; 1086 goto exit;
1087 1087
1088 switch (function) { 1088 switch (function) {
1089 case TI_CODE_DATA_ERROR: 1089 case TI_CODE_DATA_ERROR:
1090 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n", 1090 dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1091 __func__, port_number, data[1]); 1091 __func__, port_number, data[1]);
1092 break; 1092 break;
1093 1093
1094 case TI_CODE_MODEM_STATUS: 1094 case TI_CODE_MODEM_STATUS:
1095 msr = data[1]; 1095 msr = data[1];
1096 dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr); 1096 dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
1097 ti_handle_new_msr(tport, msr); 1097 ti_handle_new_msr(tport, msr);
1098 break; 1098 break;
1099 1099
1100 default: 1100 default:
1101 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n", 1101 dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1102 __func__, data[1]); 1102 __func__, data[1]);
1103 break; 1103 break;
1104 } 1104 }
1105 1105
1106 exit: 1106 exit:
1107 retval = usb_submit_urb(urb, GFP_ATOMIC); 1107 retval = usb_submit_urb(urb, GFP_ATOMIC);
1108 if (retval) 1108 if (retval)
1109 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n", 1109 dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1110 __func__, retval); 1110 __func__, retval);
1111 } 1111 }
1112 1112
1113 1113
1114 static void ti_bulk_in_callback(struct urb *urb) 1114 static void ti_bulk_in_callback(struct urb *urb)
1115 { 1115 {
1116 struct ti_port *tport = urb->context; 1116 struct ti_port *tport = urb->context;
1117 struct usb_serial_port *port = tport->tp_port; 1117 struct usb_serial_port *port = tport->tp_port;
1118 struct device *dev = &urb->dev->dev; 1118 struct device *dev = &urb->dev->dev;
1119 int status = urb->status; 1119 int status = urb->status;
1120 int retval = 0; 1120 int retval = 0;
1121 1121
1122 switch (status) { 1122 switch (status) {
1123 case 0: 1123 case 0:
1124 break; 1124 break;
1125 case -ECONNRESET: 1125 case -ECONNRESET:
1126 case -ENOENT: 1126 case -ENOENT:
1127 case -ESHUTDOWN: 1127 case -ESHUTDOWN:
1128 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status); 1128 dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1129 tport->tp_tdev->td_urb_error = 1; 1129 tport->tp_tdev->td_urb_error = 1;
1130 wake_up_interruptible(&tport->tp_write_wait); 1130 wake_up_interruptible(&tport->tp_write_wait);
1131 return; 1131 return;
1132 default: 1132 default:
1133 dev_err(dev, "%s - nonzero urb status, %d\n", 1133 dev_err(dev, "%s - nonzero urb status, %d\n",
1134 __func__, status); 1134 __func__, status);
1135 tport->tp_tdev->td_urb_error = 1; 1135 tport->tp_tdev->td_urb_error = 1;
1136 wake_up_interruptible(&tport->tp_write_wait); 1136 wake_up_interruptible(&tport->tp_write_wait);
1137 } 1137 }
1138 1138
1139 if (status == -EPIPE) 1139 if (status == -EPIPE)
1140 goto exit; 1140 goto exit;
1141 1141
1142 if (status) { 1142 if (status) {
1143 dev_err(dev, "%s - stopping read!\n", __func__); 1143 dev_err(dev, "%s - stopping read!\n", __func__);
1144 return; 1144 return;
1145 } 1145 }
1146 1146
1147 if (urb->actual_length) { 1147 if (urb->actual_length) {
1148 usb_serial_debug_data(dev, __func__, urb->actual_length, 1148 usb_serial_debug_data(dev, __func__, urb->actual_length,
1149 urb->transfer_buffer); 1149 urb->transfer_buffer);
1150 1150
1151 if (!tport->tp_is_open) 1151 if (!tport->tp_is_open)
1152 dev_dbg(dev, "%s - port closed, dropping data\n", 1152 dev_dbg(dev, "%s - port closed, dropping data\n",
1153 __func__); 1153 __func__);
1154 else 1154 else
1155 ti_recv(port, urb->transfer_buffer, urb->actual_length); 1155 ti_recv(port, urb->transfer_buffer, urb->actual_length);
1156 spin_lock(&tport->tp_lock); 1156 spin_lock(&tport->tp_lock);
1157 tport->tp_icount.rx += urb->actual_length; 1157 tport->tp_icount.rx += urb->actual_length;
1158 spin_unlock(&tport->tp_lock); 1158 spin_unlock(&tport->tp_lock);
1159 } 1159 }
1160 1160
1161 exit: 1161 exit:
1162 /* continue to read unless stopping */ 1162 /* continue to read unless stopping */
1163 spin_lock(&tport->tp_lock); 1163 spin_lock(&tport->tp_lock);
1164 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) 1164 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1165 retval = usb_submit_urb(urb, GFP_ATOMIC); 1165 retval = usb_submit_urb(urb, GFP_ATOMIC);
1166 else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) 1166 else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
1167 tport->tp_read_urb_state = TI_READ_URB_STOPPED; 1167 tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1168 1168
1169 spin_unlock(&tport->tp_lock); 1169 spin_unlock(&tport->tp_lock);
1170 if (retval) 1170 if (retval)
1171 dev_err(dev, "%s - resubmit read urb failed, %d\n", 1171 dev_err(dev, "%s - resubmit read urb failed, %d\n",
1172 __func__, retval); 1172 __func__, retval);
1173 } 1173 }
1174 1174
1175 1175
1176 static void ti_bulk_out_callback(struct urb *urb) 1176 static void ti_bulk_out_callback(struct urb *urb)
1177 { 1177 {
1178 struct ti_port *tport = urb->context; 1178 struct ti_port *tport = urb->context;
1179 struct usb_serial_port *port = tport->tp_port; 1179 struct usb_serial_port *port = tport->tp_port;
1180 int status = urb->status; 1180 int status = urb->status;
1181 1181
1182 tport->tp_write_urb_in_use = 0; 1182 tport->tp_write_urb_in_use = 0;
1183 1183
1184 switch (status) { 1184 switch (status) {
1185 case 0: 1185 case 0:
1186 break; 1186 break;
1187 case -ECONNRESET: 1187 case -ECONNRESET:
1188 case -ENOENT: 1188 case -ENOENT:
1189 case -ESHUTDOWN: 1189 case -ESHUTDOWN:
1190 dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status); 1190 dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
1191 tport->tp_tdev->td_urb_error = 1; 1191 tport->tp_tdev->td_urb_error = 1;
1192 wake_up_interruptible(&tport->tp_write_wait); 1192 wake_up_interruptible(&tport->tp_write_wait);
1193 return; 1193 return;
1194 default: 1194 default:
1195 dev_err_console(port, "%s - nonzero urb status, %d\n", 1195 dev_err_console(port, "%s - nonzero urb status, %d\n",
1196 __func__, status); 1196 __func__, status);
1197 tport->tp_tdev->td_urb_error = 1; 1197 tport->tp_tdev->td_urb_error = 1;
1198 wake_up_interruptible(&tport->tp_write_wait); 1198 wake_up_interruptible(&tport->tp_write_wait);
1199 } 1199 }
1200 1200
1201 /* send any buffered data */ 1201 /* send any buffered data */
1202 ti_send(tport); 1202 ti_send(tport);
1203 } 1203 }
1204 1204
1205 1205
1206 static void ti_recv(struct usb_serial_port *port, unsigned char *data, 1206 static void ti_recv(struct usb_serial_port *port, unsigned char *data,
1207 int length) 1207 int length)
1208 { 1208 {
1209 int cnt; 1209 int cnt;
1210 1210
1211 do { 1211 do {
1212 cnt = tty_insert_flip_string(&port->port, data, length); 1212 cnt = tty_insert_flip_string(&port->port, data, length);
1213 if (cnt < length) { 1213 if (cnt < length) {
1214 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n", 1214 dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
1215 __func__, length - cnt); 1215 __func__, length - cnt);
1216 if (cnt == 0) 1216 if (cnt == 0)
1217 break; 1217 break;
1218 } 1218 }
1219 tty_flip_buffer_push(&port->port); 1219 tty_flip_buffer_push(&port->port);
1220 data += cnt; 1220 data += cnt;
1221 length -= cnt; 1221 length -= cnt;
1222 } while (length > 0); 1222 } while (length > 0);
1223 } 1223 }
1224 1224
1225 1225
1226 static void ti_send(struct ti_port *tport) 1226 static void ti_send(struct ti_port *tport)
1227 { 1227 {
1228 int count, result; 1228 int count, result;
1229 struct usb_serial_port *port = tport->tp_port; 1229 struct usb_serial_port *port = tport->tp_port;
1230 struct tty_struct *tty = tty_port_tty_get(&port->port); /* FIXME */
1231 unsigned long flags; 1230 unsigned long flags;
1232 1231
1233 spin_lock_irqsave(&tport->tp_lock, flags); 1232 spin_lock_irqsave(&tport->tp_lock, flags);
1234 1233
1235 if (tport->tp_write_urb_in_use) 1234 if (tport->tp_write_urb_in_use)
1236 goto unlock; 1235 goto unlock;
1237 1236
1238 count = kfifo_out(&tport->write_fifo, 1237 count = kfifo_out(&tport->write_fifo,
1239 port->write_urb->transfer_buffer, 1238 port->write_urb->transfer_buffer,
1240 port->bulk_out_size); 1239 port->bulk_out_size);
1241 1240
1242 if (count == 0) 1241 if (count == 0)
1243 goto unlock; 1242 goto unlock;
1244 1243
1245 tport->tp_write_urb_in_use = 1; 1244 tport->tp_write_urb_in_use = 1;
1246 1245
1247 spin_unlock_irqrestore(&tport->tp_lock, flags); 1246 spin_unlock_irqrestore(&tport->tp_lock, flags);
1248 1247
1249 usb_serial_debug_data(&port->dev, __func__, count, 1248 usb_serial_debug_data(&port->dev, __func__, count,
1250 port->write_urb->transfer_buffer); 1249 port->write_urb->transfer_buffer);
1251 1250
1252 usb_fill_bulk_urb(port->write_urb, port->serial->dev, 1251 usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1253 usb_sndbulkpipe(port->serial->dev, 1252 usb_sndbulkpipe(port->serial->dev,
1254 port->bulk_out_endpointAddress), 1253 port->bulk_out_endpointAddress),
1255 port->write_urb->transfer_buffer, count, 1254 port->write_urb->transfer_buffer, count,
1256 ti_bulk_out_callback, tport); 1255 ti_bulk_out_callback, tport);
1257 1256
1258 result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 1257 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1259 if (result) { 1258 if (result) {
1260 dev_err_console(port, "%s - submit write urb failed, %d\n", 1259 dev_err_console(port, "%s - submit write urb failed, %d\n",
1261 __func__, result); 1260 __func__, result);
1262 tport->tp_write_urb_in_use = 0; 1261 tport->tp_write_urb_in_use = 0;
1263 /* TODO: reschedule ti_send */ 1262 /* TODO: reschedule ti_send */
1264 } else { 1263 } else {
1265 spin_lock_irqsave(&tport->tp_lock, flags); 1264 spin_lock_irqsave(&tport->tp_lock, flags);
1266 tport->tp_icount.tx += count; 1265 tport->tp_icount.tx += count;
1267 spin_unlock_irqrestore(&tport->tp_lock, flags); 1266 spin_unlock_irqrestore(&tport->tp_lock, flags);
1268 } 1267 }
1269 1268
1270 /* more room in the buffer for new writes, wakeup */ 1269 /* more room in the buffer for new writes, wakeup */
1271 if (tty) 1270 tty_port_tty_wakeup(&port->port);
1272 tty_wakeup(tty); 1271
1273 tty_kref_put(tty);
1274 wake_up_interruptible(&tport->tp_write_wait); 1272 wake_up_interruptible(&tport->tp_write_wait);
1275 return; 1273 return;
1276 unlock: 1274 unlock:
1277 spin_unlock_irqrestore(&tport->tp_lock, flags); 1275 spin_unlock_irqrestore(&tport->tp_lock, flags);
1278 tty_kref_put(tty);
1279 return; 1276 return;
1280 } 1277 }
1281 1278
1282 1279
1283 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr) 1280 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1284 { 1281 {
1285 unsigned long flags; 1282 unsigned long flags;
1286 int status; 1283 int status;
1287 1284
1288 status = ti_write_byte(tport->tp_port, tport->tp_tdev, 1285 status = ti_write_byte(tport->tp_port, tport->tp_tdev,
1289 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR, 1286 tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1290 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr); 1287 TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1291 1288
1292 spin_lock_irqsave(&tport->tp_lock, flags); 1289 spin_lock_irqsave(&tport->tp_lock, flags);
1293 if (!status) 1290 if (!status)
1294 tport->tp_shadow_mcr = mcr; 1291 tport->tp_shadow_mcr = mcr;
1295 spin_unlock_irqrestore(&tport->tp_lock, flags); 1292 spin_unlock_irqrestore(&tport->tp_lock, flags);
1296 1293
1297 return status; 1294 return status;
1298 } 1295 }
1299 1296
1300 1297
1301 static int ti_get_lsr(struct ti_port *tport) 1298 static int ti_get_lsr(struct ti_port *tport)
1302 { 1299 {
1303 int size, status; 1300 int size, status;
1304 struct ti_device *tdev = tport->tp_tdev; 1301 struct ti_device *tdev = tport->tp_tdev;
1305 struct usb_serial_port *port = tport->tp_port; 1302 struct usb_serial_port *port = tport->tp_port;
1306 int port_number = port->number - port->serial->minor; 1303 int port_number = port->number - port->serial->minor;
1307 struct ti_port_status *data; 1304 struct ti_port_status *data;
1308 1305
1309 size = sizeof(struct ti_port_status); 1306 size = sizeof(struct ti_port_status);
1310 data = kmalloc(size, GFP_KERNEL); 1307 data = kmalloc(size, GFP_KERNEL);
1311 if (!data) { 1308 if (!data) {
1312 dev_err(&port->dev, "%s - out of memory\n", __func__); 1309 dev_err(&port->dev, "%s - out of memory\n", __func__);
1313 return -ENOMEM; 1310 return -ENOMEM;
1314 } 1311 }
1315 1312
1316 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS, 1313 status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1317 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size); 1314 (__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1318 if (status) { 1315 if (status) {
1319 dev_err(&port->dev, 1316 dev_err(&port->dev,
1320 "%s - get port status command failed, %d\n", 1317 "%s - get port status command failed, %d\n",
1321 __func__, status); 1318 __func__, status);
1322 goto free_data; 1319 goto free_data;
1323 } 1320 }
1324 1321
1325 dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR); 1322 dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
1326 1323
1327 tport->tp_lsr = data->bLSR; 1324 tport->tp_lsr = data->bLSR;
1328 1325
1329 free_data: 1326 free_data:
1330 kfree(data); 1327 kfree(data);
1331 return status; 1328 return status;
1332 } 1329 }
1333 1330
1334 1331
1335 static int ti_get_serial_info(struct ti_port *tport, 1332 static int ti_get_serial_info(struct ti_port *tport,
1336 struct serial_struct __user *ret_arg) 1333 struct serial_struct __user *ret_arg)
1337 { 1334 {
1338 struct usb_serial_port *port = tport->tp_port; 1335 struct usb_serial_port *port = tport->tp_port;
1339 struct serial_struct ret_serial; 1336 struct serial_struct ret_serial;
1340 1337
1341 if (!ret_arg) 1338 if (!ret_arg)
1342 return -EFAULT; 1339 return -EFAULT;
1343 1340
1344 memset(&ret_serial, 0, sizeof(ret_serial)); 1341 memset(&ret_serial, 0, sizeof(ret_serial));
1345 1342
1346 ret_serial.type = PORT_16550A; 1343 ret_serial.type = PORT_16550A;
1347 ret_serial.line = port->serial->minor; 1344 ret_serial.line = port->serial->minor;
1348 ret_serial.port = port->number - port->serial->minor; 1345 ret_serial.port = port->number - port->serial->minor;
1349 ret_serial.flags = tport->tp_flags; 1346 ret_serial.flags = tport->tp_flags;
1350 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE; 1347 ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1351 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800; 1348 ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1352 ret_serial.closing_wait = tport->tp_closing_wait; 1349 ret_serial.closing_wait = tport->tp_closing_wait;
1353 1350
1354 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg))) 1351 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1355 return -EFAULT; 1352 return -EFAULT;
1356 1353
1357 return 0; 1354 return 0;
1358 } 1355 }
1359 1356
1360 1357
1361 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport, 1358 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1362 struct serial_struct __user *new_arg) 1359 struct serial_struct __user *new_arg)
1363 { 1360 {
1364 struct serial_struct new_serial; 1361 struct serial_struct new_serial;
1365 1362
1366 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial))) 1363 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1367 return -EFAULT; 1364 return -EFAULT;
1368 1365
1369 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS; 1366 tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1370 tport->tp_closing_wait = new_serial.closing_wait; 1367 tport->tp_closing_wait = new_serial.closing_wait;
1371 1368
1372 return 0; 1369 return 0;
1373 } 1370 }
1374 1371
1375 1372
1376 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr) 1373 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1377 { 1374 {
1378 struct async_icount *icount; 1375 struct async_icount *icount;
1379 struct tty_struct *tty; 1376 struct tty_struct *tty;
1380 unsigned long flags; 1377 unsigned long flags;
1381 1378
1382 dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr); 1379 dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
1383 1380
1384 if (msr & TI_MSR_DELTA_MASK) { 1381 if (msr & TI_MSR_DELTA_MASK) {
1385 spin_lock_irqsave(&tport->tp_lock, flags); 1382 spin_lock_irqsave(&tport->tp_lock, flags);
1386 icount = &tport->tp_icount; 1383 icount = &tport->tp_icount;
1387 if (msr & TI_MSR_DELTA_CTS) 1384 if (msr & TI_MSR_DELTA_CTS)
1388 icount->cts++; 1385 icount->cts++;
1389 if (msr & TI_MSR_DELTA_DSR) 1386 if (msr & TI_MSR_DELTA_DSR)
1390 icount->dsr++; 1387 icount->dsr++;
1391 if (msr & TI_MSR_DELTA_CD) 1388 if (msr & TI_MSR_DELTA_CD)
1392 icount->dcd++; 1389 icount->dcd++;
1393 if (msr & TI_MSR_DELTA_RI) 1390 if (msr & TI_MSR_DELTA_RI)
1394 icount->rng++; 1391 icount->rng++;
1395 wake_up_interruptible(&tport->tp_msr_wait); 1392 wake_up_interruptible(&tport->tp_msr_wait);
1396 spin_unlock_irqrestore(&tport->tp_lock, flags); 1393 spin_unlock_irqrestore(&tport->tp_lock, flags);
1397 } 1394 }
1398 1395
1399 tport->tp_msr = msr & TI_MSR_MASK; 1396 tport->tp_msr = msr & TI_MSR_MASK;
1400 1397
1401 /* handle CTS flow control */ 1398 /* handle CTS flow control */
1402 tty = tty_port_tty_get(&tport->tp_port->port); 1399 tty = tty_port_tty_get(&tport->tp_port->port);
1403 if (tty && C_CRTSCTS(tty)) { 1400 if (tty && C_CRTSCTS(tty)) {
1404 if (msr & TI_MSR_CTS) { 1401 if (msr & TI_MSR_CTS) {
1405 tty->hw_stopped = 0; 1402 tty->hw_stopped = 0;
1406 tty_wakeup(tty); 1403 tty_wakeup(tty);
1407 } else { 1404 } else {
1408 tty->hw_stopped = 1; 1405 tty->hw_stopped = 1;
1409 } 1406 }
1410 } 1407 }
1411 tty_kref_put(tty); 1408 tty_kref_put(tty);
1412 } 1409 }
1413 1410
1414 1411
1415 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush) 1412 static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
1416 { 1413 {
1417 struct ti_device *tdev = tport->tp_tdev; 1414 struct ti_device *tdev = tport->tp_tdev;
1418 struct usb_serial_port *port = tport->tp_port; 1415 struct usb_serial_port *port = tport->tp_port;
1419 wait_queue_t wait; 1416 wait_queue_t wait;
1420 1417
1421 spin_lock_irq(&tport->tp_lock); 1418 spin_lock_irq(&tport->tp_lock);
1422 1419
1423 /* wait for data to drain from the buffer */ 1420 /* wait for data to drain from the buffer */
1424 tdev->td_urb_error = 0; 1421 tdev->td_urb_error = 0;
1425 init_waitqueue_entry(&wait, current); 1422 init_waitqueue_entry(&wait, current);
1426 add_wait_queue(&tport->tp_write_wait, &wait); 1423 add_wait_queue(&tport->tp_write_wait, &wait);
1427 for (;;) { 1424 for (;;) {
1428 set_current_state(TASK_INTERRUPTIBLE); 1425 set_current_state(TASK_INTERRUPTIBLE);
1429 if (kfifo_len(&tport->write_fifo) == 0 1426 if (kfifo_len(&tport->write_fifo) == 0
1430 || timeout == 0 || signal_pending(current) 1427 || timeout == 0 || signal_pending(current)
1431 || tdev->td_urb_error 1428 || tdev->td_urb_error
1432 || port->serial->disconnected) /* disconnect */ 1429 || port->serial->disconnected) /* disconnect */
1433 break; 1430 break;
1434 spin_unlock_irq(&tport->tp_lock); 1431 spin_unlock_irq(&tport->tp_lock);
1435 timeout = schedule_timeout(timeout); 1432 timeout = schedule_timeout(timeout);
1436 spin_lock_irq(&tport->tp_lock); 1433 spin_lock_irq(&tport->tp_lock);
1437 } 1434 }
1438 set_current_state(TASK_RUNNING); 1435 set_current_state(TASK_RUNNING);
1439 remove_wait_queue(&tport->tp_write_wait, &wait); 1436 remove_wait_queue(&tport->tp_write_wait, &wait);
1440 1437
1441 /* flush any remaining data in the buffer */ 1438 /* flush any remaining data in the buffer */
1442 if (flush) 1439 if (flush)
1443 kfifo_reset_out(&tport->write_fifo); 1440 kfifo_reset_out(&tport->write_fifo);
1444 1441
1445 spin_unlock_irq(&tport->tp_lock); 1442 spin_unlock_irq(&tport->tp_lock);
1446 1443
1447 mutex_lock(&port->serial->disc_mutex); 1444 mutex_lock(&port->serial->disc_mutex);
1448 /* wait for data to drain from the device */ 1445 /* wait for data to drain from the device */
1449 /* wait for empty tx register, plus 20 ms */ 1446 /* wait for empty tx register, plus 20 ms */
1450 timeout += jiffies; 1447 timeout += jiffies;
1451 tport->tp_lsr &= ~TI_LSR_TX_EMPTY; 1448 tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
1452 while ((long)(jiffies - timeout) < 0 && !signal_pending(current) 1449 while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1453 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error 1450 && !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1454 && !port->serial->disconnected) { 1451 && !port->serial->disconnected) {
1455 if (ti_get_lsr(tport)) 1452 if (ti_get_lsr(tport))
1456 break; 1453 break;
1457 mutex_unlock(&port->serial->disc_mutex); 1454 mutex_unlock(&port->serial->disc_mutex);
1458 msleep_interruptible(20); 1455 msleep_interruptible(20);
1459 mutex_lock(&port->serial->disc_mutex); 1456 mutex_lock(&port->serial->disc_mutex);
1460 } 1457 }
1461 mutex_unlock(&port->serial->disc_mutex); 1458 mutex_unlock(&port->serial->disc_mutex);
1462 } 1459 }
1463 1460
1464 1461
1465 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty) 1462 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1466 { 1463 {
1467 unsigned long flags; 1464 unsigned long flags;
1468 1465
1469 spin_lock_irqsave(&tport->tp_lock, flags); 1466 spin_lock_irqsave(&tport->tp_lock, flags);
1470 1467
1471 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) 1468 if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1472 tport->tp_read_urb_state = TI_READ_URB_STOPPING; 1469 tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1473 1470
1474 spin_unlock_irqrestore(&tport->tp_lock, flags); 1471 spin_unlock_irqrestore(&tport->tp_lock, flags);
1475 } 1472 }
1476 1473
1477 1474
1478 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty) 1475 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1479 { 1476 {
1480 struct urb *urb; 1477 struct urb *urb;
1481 int status = 0; 1478 int status = 0;
1482 unsigned long flags; 1479 unsigned long flags;
1483 1480
1484 spin_lock_irqsave(&tport->tp_lock, flags); 1481 spin_lock_irqsave(&tport->tp_lock, flags);
1485 1482
1486 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) { 1483 if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1487 tport->tp_read_urb_state = TI_READ_URB_RUNNING; 1484 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1488 urb = tport->tp_port->read_urb; 1485 urb = tport->tp_port->read_urb;
1489 spin_unlock_irqrestore(&tport->tp_lock, flags); 1486 spin_unlock_irqrestore(&tport->tp_lock, flags);
1490 urb->context = tport; 1487 urb->context = tport;
1491 status = usb_submit_urb(urb, GFP_KERNEL); 1488 status = usb_submit_urb(urb, GFP_KERNEL);
1492 } else { 1489 } else {
1493 tport->tp_read_urb_state = TI_READ_URB_RUNNING; 1490 tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1494 spin_unlock_irqrestore(&tport->tp_lock, flags); 1491 spin_unlock_irqrestore(&tport->tp_lock, flags);
1495 } 1492 }
1496 1493
1497 return status; 1494 return status;
1498 } 1495 }
1499 1496
1500 1497
1501 static int ti_command_out_sync(struct ti_device *tdev, __u8 command, 1498 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1502 __u16 moduleid, __u16 value, __u8 *data, int size) 1499 __u16 moduleid, __u16 value, __u8 *data, int size)
1503 { 1500 {
1504 int status; 1501 int status;
1505 1502
1506 status = usb_control_msg(tdev->td_serial->dev, 1503 status = usb_control_msg(tdev->td_serial->dev,
1507 usb_sndctrlpipe(tdev->td_serial->dev, 0), command, 1504 usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1508 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT), 1505 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1509 value, moduleid, data, size, 1000); 1506 value, moduleid, data, size, 1000);
1510 1507
1511 if (status == size) 1508 if (status == size)
1512 status = 0; 1509 status = 0;
1513 1510
1514 if (status > 0) 1511 if (status > 0)
1515 status = -ECOMM; 1512 status = -ECOMM;
1516 1513
1517 return status; 1514 return status;
1518 } 1515 }
1519 1516
1520 1517
1521 static int ti_command_in_sync(struct ti_device *tdev, __u8 command, 1518 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1522 __u16 moduleid, __u16 value, __u8 *data, int size) 1519 __u16 moduleid, __u16 value, __u8 *data, int size)
1523 { 1520 {
1524 int status; 1521 int status;
1525 1522
1526 status = usb_control_msg(tdev->td_serial->dev, 1523 status = usb_control_msg(tdev->td_serial->dev,
1527 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command, 1524 usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1528 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN), 1525 (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1529 value, moduleid, data, size, 1000); 1526 value, moduleid, data, size, 1000);
1530 1527
1531 if (status == size) 1528 if (status == size)
1532 status = 0; 1529 status = 0;
1533 1530
1534 if (status > 0) 1531 if (status > 0)
1535 status = -ECOMM; 1532 status = -ECOMM;
1536 1533
1537 return status; 1534 return status;
1538 } 1535 }
1539 1536
1540 1537
1541 static int ti_write_byte(struct usb_serial_port *port, 1538 static int ti_write_byte(struct usb_serial_port *port,
1542 struct ti_device *tdev, unsigned long addr, 1539 struct ti_device *tdev, unsigned long addr,
1543 __u8 mask, __u8 byte) 1540 __u8 mask, __u8 byte)
1544 { 1541 {
1545 int status; 1542 int status;
1546 unsigned int size; 1543 unsigned int size;
1547 struct ti_write_data_bytes *data; 1544 struct ti_write_data_bytes *data;
1548 1545
1549 dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__, 1546 dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
1550 addr, mask, byte); 1547 addr, mask, byte);
1551 1548
1552 size = sizeof(struct ti_write_data_bytes) + 2; 1549 size = sizeof(struct ti_write_data_bytes) + 2;
1553 data = kmalloc(size, GFP_KERNEL); 1550 data = kmalloc(size, GFP_KERNEL);
1554 if (!data) { 1551 if (!data) {
1555 dev_err(&port->dev, "%s - out of memory\n", __func__); 1552 dev_err(&port->dev, "%s - out of memory\n", __func__);
1556 return -ENOMEM; 1553 return -ENOMEM;
1557 } 1554 }
1558 1555
1559 data->bAddrType = TI_RW_DATA_ADDR_XDATA; 1556 data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1560 data->bDataType = TI_RW_DATA_BYTE; 1557 data->bDataType = TI_RW_DATA_BYTE;
1561 data->bDataCounter = 1; 1558 data->bDataCounter = 1;
1562 data->wBaseAddrHi = cpu_to_be16(addr>>16); 1559 data->wBaseAddrHi = cpu_to_be16(addr>>16);
1563 data->wBaseAddrLo = cpu_to_be16(addr); 1560 data->wBaseAddrLo = cpu_to_be16(addr);
1564 data->bData[0] = mask; 1561 data->bData[0] = mask;
1565 data->bData[1] = byte; 1562 data->bData[1] = byte;
1566 1563
1567 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0, 1564 status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1568 (__u8 *)data, size); 1565 (__u8 *)data, size);
1569 1566
1570 if (status < 0) 1567 if (status < 0)
1571 dev_err(&port->dev, "%s - failed, %d\n", __func__, status); 1568 dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
1572 1569
1573 kfree(data); 1570 kfree(data);
1574 1571
1575 return status; 1572 return status;
1576 } 1573 }
1577 1574
1578 static int ti_do_download(struct usb_device *dev, int pipe, 1575 static int ti_do_download(struct usb_device *dev, int pipe,
1579 u8 *buffer, int size) 1576 u8 *buffer, int size)
1580 { 1577 {
1581 int pos; 1578 int pos;
1582 u8 cs = 0; 1579 u8 cs = 0;
1583 int done; 1580 int done;
1584 struct ti_firmware_header *header; 1581 struct ti_firmware_header *header;
1585 int status = 0; 1582 int status = 0;
1586 int len; 1583 int len;
1587 1584
1588 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++) 1585 for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1589 cs = (__u8)(cs + buffer[pos]); 1586 cs = (__u8)(cs + buffer[pos]);
1590 1587
1591 header = (struct ti_firmware_header *)buffer; 1588 header = (struct ti_firmware_header *)buffer;
1592 header->wLength = cpu_to_le16((__u16)(size 1589 header->wLength = cpu_to_le16((__u16)(size
1593 - sizeof(struct ti_firmware_header))); 1590 - sizeof(struct ti_firmware_header)));
1594 header->bCheckSum = cs; 1591 header->bCheckSum = cs;
1595 1592
1596 dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__); 1593 dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
1597 for (pos = 0; pos < size; pos += done) { 1594 for (pos = 0; pos < size; pos += done) {
1598 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE); 1595 len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1599 status = usb_bulk_msg(dev, pipe, buffer + pos, len, 1596 status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1600 &done, 1000); 1597 &done, 1000);
1601 if (status) 1598 if (status)
1602 break; 1599 break;
1603 } 1600 }
1604 return status; 1601 return status;
1605 } 1602 }
1606 1603
1607 static int ti_download_firmware(struct ti_device *tdev) 1604 static int ti_download_firmware(struct ti_device *tdev)
1608 { 1605 {
1609 int status; 1606 int status;
1610 int buffer_size; 1607 int buffer_size;
1611 __u8 *buffer; 1608 __u8 *buffer;
1612 struct usb_device *dev = tdev->td_serial->dev; 1609 struct usb_device *dev = tdev->td_serial->dev;
1613 unsigned int pipe = usb_sndbulkpipe(dev, 1610 unsigned int pipe = usb_sndbulkpipe(dev,
1614 tdev->td_serial->port[0]->bulk_out_endpointAddress); 1611 tdev->td_serial->port[0]->bulk_out_endpointAddress);
1615 const struct firmware *fw_p; 1612 const struct firmware *fw_p;
1616 char buf[32]; 1613 char buf[32];
1617 1614
1618 /* try ID specific firmware first, then try generic firmware */ 1615 /* try ID specific firmware first, then try generic firmware */
1619 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor, 1616 sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
1620 dev->descriptor.idProduct); 1617 dev->descriptor.idProduct);
1621 status = request_firmware(&fw_p, buf, &dev->dev); 1618 status = request_firmware(&fw_p, buf, &dev->dev);
1622 1619
1623 if (status != 0) { 1620 if (status != 0) {
1624 buf[0] = '\0'; 1621 buf[0] = '\0';
1625 if (dev->descriptor.idVendor == MTS_VENDOR_ID) { 1622 if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
1626 switch (dev->descriptor.idProduct) { 1623 switch (dev->descriptor.idProduct) {
1627 case MTS_CDMA_PRODUCT_ID: 1624 case MTS_CDMA_PRODUCT_ID:
1628 strcpy(buf, "mts_cdma.fw"); 1625 strcpy(buf, "mts_cdma.fw");
1629 break; 1626 break;
1630 case MTS_GSM_PRODUCT_ID: 1627 case MTS_GSM_PRODUCT_ID:
1631 strcpy(buf, "mts_gsm.fw"); 1628 strcpy(buf, "mts_gsm.fw");
1632 break; 1629 break;
1633 case MTS_EDGE_PRODUCT_ID: 1630 case MTS_EDGE_PRODUCT_ID:
1634 strcpy(buf, "mts_edge.fw"); 1631 strcpy(buf, "mts_edge.fw");
1635 break; 1632 break;
1636 case MTS_MT9234MU_PRODUCT_ID: 1633 case MTS_MT9234MU_PRODUCT_ID:
1637 strcpy(buf, "mts_mt9234mu.fw"); 1634 strcpy(buf, "mts_mt9234mu.fw");
1638 break; 1635 break;
1639 case MTS_MT9234ZBA_PRODUCT_ID: 1636 case MTS_MT9234ZBA_PRODUCT_ID:
1640 strcpy(buf, "mts_mt9234zba.fw"); 1637 strcpy(buf, "mts_mt9234zba.fw");
1641 break; 1638 break;
1642 case MTS_MT9234ZBAOLD_PRODUCT_ID: 1639 case MTS_MT9234ZBAOLD_PRODUCT_ID:
1643 strcpy(buf, "mts_mt9234zba.fw"); 1640 strcpy(buf, "mts_mt9234zba.fw");
1644 break; } 1641 break; }
1645 } 1642 }
1646 if (buf[0] == '\0') { 1643 if (buf[0] == '\0') {
1647 if (tdev->td_is_3410) 1644 if (tdev->td_is_3410)
1648 strcpy(buf, "ti_3410.fw"); 1645 strcpy(buf, "ti_3410.fw");
1649 else 1646 else
1650 strcpy(buf, "ti_5052.fw"); 1647 strcpy(buf, "ti_5052.fw");
1651 } 1648 }
1652 status = request_firmware(&fw_p, buf, &dev->dev); 1649 status = request_firmware(&fw_p, buf, &dev->dev);
1653 } 1650 }
1654 if (status) { 1651 if (status) {
1655 dev_err(&dev->dev, "%s - firmware not found\n", __func__); 1652 dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1656 return -ENOENT; 1653 return -ENOENT;
1657 } 1654 }
1658 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) { 1655 if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1659 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size); 1656 dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
1660 release_firmware(fw_p); 1657 release_firmware(fw_p);
1661 return -ENOENT; 1658 return -ENOENT;
1662 } 1659 }
1663 1660
1664 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header); 1661 buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1665 buffer = kmalloc(buffer_size, GFP_KERNEL); 1662 buffer = kmalloc(buffer_size, GFP_KERNEL);
1666 if (buffer) { 1663 if (buffer) {
1667 memcpy(buffer, fw_p->data, fw_p->size); 1664 memcpy(buffer, fw_p->data, fw_p->size);
1668 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size); 1665 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1669 status = ti_do_download(dev, pipe, buffer, fw_p->size); 1666 status = ti_do_download(dev, pipe, buffer, fw_p->size);
1670 kfree(buffer); 1667 kfree(buffer);
1671 } else { 1668 } else {
1672 dev_dbg(&dev->dev, "%s ENOMEM\n", __func__); 1669 dev_dbg(&dev->dev, "%s ENOMEM\n", __func__);
1673 status = -ENOMEM; 1670 status = -ENOMEM;
1674 } 1671 }
1675 release_firmware(fw_p); 1672 release_firmware(fw_p);
1676 if (status) { 1673 if (status) {
1677 dev_err(&dev->dev, "%s - error downloading firmware, %d\n", 1674 dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1678 __func__, status); 1675 __func__, status);
1679 return status; 1676 return status;
1680 } 1677 }
1681 1678
1682 dev_dbg(&dev->dev, "%s - download successful\n", __func__); 1679 dev_dbg(&dev->dev, "%s - download successful\n", __func__);
1683 1680
1684 return 0; 1681 return 0;
1685 } 1682 }
1686 1683
drivers/usb/serial/usb-serial.c
1 /* 1 /*
2 * USB Serial Converter driver 2 * USB Serial Converter driver
3 * 3 *
4 * Copyright (C) 1999 - 2012 Greg Kroah-Hartman (greg@kroah.com) 4 * Copyright (C) 1999 - 2012 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2000 Peter Berger (pberger@brimson.com) 5 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
6 * Copyright (C) 2000 Al Borchers (borchers@steinerpoint.com) 6 * Copyright (C) 2000 Al Borchers (borchers@steinerpoint.com)
7 * 7 *
8 * This program is free software; you can redistribute it and/or 8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation. 10 * 2 as published by the Free Software Foundation.
11 * 11 *
12 * This driver was originally based on the ACM driver by Armin Fuerst (which was 12 * This driver was originally based on the ACM driver by Armin Fuerst (which was
13 * based on a driver by Brad Keryan) 13 * based on a driver by Brad Keryan)
14 * 14 *
15 * See Documentation/usb/usb-serial.txt for more information on using this 15 * See Documentation/usb/usb-serial.txt for more information on using this
16 * driver 16 * driver
17 * 17 *
18 */ 18 */
19 19
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 21
22 #include <linux/kernel.h> 22 #include <linux/kernel.h>
23 #include <linux/errno.h> 23 #include <linux/errno.h>
24 #include <linux/init.h> 24 #include <linux/init.h>
25 #include <linux/slab.h> 25 #include <linux/slab.h>
26 #include <linux/tty.h> 26 #include <linux/tty.h>
27 #include <linux/tty_driver.h> 27 #include <linux/tty_driver.h>
28 #include <linux/tty_flip.h> 28 #include <linux/tty_flip.h>
29 #include <linux/module.h> 29 #include <linux/module.h>
30 #include <linux/moduleparam.h> 30 #include <linux/moduleparam.h>
31 #include <linux/seq_file.h> 31 #include <linux/seq_file.h>
32 #include <linux/spinlock.h> 32 #include <linux/spinlock.h>
33 #include <linux/mutex.h> 33 #include <linux/mutex.h>
34 #include <linux/list.h> 34 #include <linux/list.h>
35 #include <linux/uaccess.h> 35 #include <linux/uaccess.h>
36 #include <linux/serial.h> 36 #include <linux/serial.h>
37 #include <linux/usb.h> 37 #include <linux/usb.h>
38 #include <linux/usb/serial.h> 38 #include <linux/usb/serial.h>
39 #include <linux/kfifo.h> 39 #include <linux/kfifo.h>
40 #include "pl2303.h" 40 #include "pl2303.h"
41 41
42 #define DRIVER_AUTHOR "Greg Kroah-Hartman <gregkh@linuxfoundation.org>" 42 #define DRIVER_AUTHOR "Greg Kroah-Hartman <gregkh@linuxfoundation.org>"
43 #define DRIVER_DESC "USB Serial Driver core" 43 #define DRIVER_DESC "USB Serial Driver core"
44 44
45 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead 45 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
46 the MODULE_DEVICE_TABLE declarations in each serial driver 46 the MODULE_DEVICE_TABLE declarations in each serial driver
47 cause the "hotplug" program to pull in whatever module is necessary 47 cause the "hotplug" program to pull in whatever module is necessary
48 via modprobe, and modprobe will load usbserial because the serial 48 via modprobe, and modprobe will load usbserial because the serial
49 drivers depend on it. 49 drivers depend on it.
50 */ 50 */
51 51
52 /* initially all NULL */ 52 /* initially all NULL */
53 static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; 53 static struct usb_serial *serial_table[SERIAL_TTY_MINORS];
54 static DEFINE_MUTEX(table_lock); 54 static DEFINE_MUTEX(table_lock);
55 static LIST_HEAD(usb_serial_driver_list); 55 static LIST_HEAD(usb_serial_driver_list);
56 56
57 /* 57 /*
58 * Look up the serial structure. If it is found and it hasn't been 58 * Look up the serial structure. If it is found and it hasn't been
59 * disconnected, return with its disc_mutex held and its refcount 59 * disconnected, return with its disc_mutex held and its refcount
60 * incremented. Otherwise return NULL. 60 * incremented. Otherwise return NULL.
61 */ 61 */
62 struct usb_serial *usb_serial_get_by_index(unsigned index) 62 struct usb_serial *usb_serial_get_by_index(unsigned index)
63 { 63 {
64 struct usb_serial *serial; 64 struct usb_serial *serial;
65 65
66 mutex_lock(&table_lock); 66 mutex_lock(&table_lock);
67 serial = serial_table[index]; 67 serial = serial_table[index];
68 68
69 if (serial) { 69 if (serial) {
70 mutex_lock(&serial->disc_mutex); 70 mutex_lock(&serial->disc_mutex);
71 if (serial->disconnected) { 71 if (serial->disconnected) {
72 mutex_unlock(&serial->disc_mutex); 72 mutex_unlock(&serial->disc_mutex);
73 serial = NULL; 73 serial = NULL;
74 } else { 74 } else {
75 kref_get(&serial->kref); 75 kref_get(&serial->kref);
76 } 76 }
77 } 77 }
78 mutex_unlock(&table_lock); 78 mutex_unlock(&table_lock);
79 return serial; 79 return serial;
80 } 80 }
81 81
82 static struct usb_serial *get_free_serial(struct usb_serial *serial, 82 static struct usb_serial *get_free_serial(struct usb_serial *serial,
83 int num_ports, unsigned int *minor) 83 int num_ports, unsigned int *minor)
84 { 84 {
85 unsigned int i, j; 85 unsigned int i, j;
86 int good_spot; 86 int good_spot;
87 87
88 dev_dbg(&serial->interface->dev, "%s %d\n", __func__, num_ports); 88 dev_dbg(&serial->interface->dev, "%s %d\n", __func__, num_ports);
89 89
90 *minor = 0; 90 *minor = 0;
91 mutex_lock(&table_lock); 91 mutex_lock(&table_lock);
92 for (i = 0; i < SERIAL_TTY_MINORS; ++i) { 92 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
93 if (serial_table[i]) 93 if (serial_table[i])
94 continue; 94 continue;
95 95
96 good_spot = 1; 96 good_spot = 1;
97 for (j = 1; j <= num_ports-1; ++j) 97 for (j = 1; j <= num_ports-1; ++j)
98 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) { 98 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
99 good_spot = 0; 99 good_spot = 0;
100 i += j; 100 i += j;
101 break; 101 break;
102 } 102 }
103 if (good_spot == 0) 103 if (good_spot == 0)
104 continue; 104 continue;
105 105
106 *minor = i; 106 *minor = i;
107 j = 0; 107 j = 0;
108 dev_dbg(&serial->interface->dev, "%s - minor base = %d\n", __func__, *minor); 108 dev_dbg(&serial->interface->dev, "%s - minor base = %d\n", __func__, *minor);
109 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) { 109 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
110 serial_table[i] = serial; 110 serial_table[i] = serial;
111 serial->port[j++]->number = i; 111 serial->port[j++]->number = i;
112 } 112 }
113 mutex_unlock(&table_lock); 113 mutex_unlock(&table_lock);
114 return serial; 114 return serial;
115 } 115 }
116 mutex_unlock(&table_lock); 116 mutex_unlock(&table_lock);
117 return NULL; 117 return NULL;
118 } 118 }
119 119
120 static void return_serial(struct usb_serial *serial) 120 static void return_serial(struct usb_serial *serial)
121 { 121 {
122 int i; 122 int i;
123 123
124 mutex_lock(&table_lock); 124 mutex_lock(&table_lock);
125 for (i = 0; i < serial->num_ports; ++i) 125 for (i = 0; i < serial->num_ports; ++i)
126 serial_table[serial->minor + i] = NULL; 126 serial_table[serial->minor + i] = NULL;
127 mutex_unlock(&table_lock); 127 mutex_unlock(&table_lock);
128 } 128 }
129 129
130 static void destroy_serial(struct kref *kref) 130 static void destroy_serial(struct kref *kref)
131 { 131 {
132 struct usb_serial *serial; 132 struct usb_serial *serial;
133 struct usb_serial_port *port; 133 struct usb_serial_port *port;
134 int i; 134 int i;
135 135
136 serial = to_usb_serial(kref); 136 serial = to_usb_serial(kref);
137 137
138 /* return the minor range that this device had */ 138 /* return the minor range that this device had */
139 if (serial->minor != SERIAL_TTY_NO_MINOR) 139 if (serial->minor != SERIAL_TTY_NO_MINOR)
140 return_serial(serial); 140 return_serial(serial);
141 141
142 if (serial->attached) 142 if (serial->attached)
143 serial->type->release(serial); 143 serial->type->release(serial);
144 144
145 /* Now that nothing is using the ports, they can be freed */ 145 /* Now that nothing is using the ports, they can be freed */
146 for (i = 0; i < serial->num_port_pointers; ++i) { 146 for (i = 0; i < serial->num_port_pointers; ++i) {
147 port = serial->port[i]; 147 port = serial->port[i];
148 if (port) { 148 if (port) {
149 port->serial = NULL; 149 port->serial = NULL;
150 put_device(&port->dev); 150 put_device(&port->dev);
151 } 151 }
152 } 152 }
153 153
154 usb_put_dev(serial->dev); 154 usb_put_dev(serial->dev);
155 kfree(serial); 155 kfree(serial);
156 } 156 }
157 157
158 void usb_serial_put(struct usb_serial *serial) 158 void usb_serial_put(struct usb_serial *serial)
159 { 159 {
160 kref_put(&serial->kref, destroy_serial); 160 kref_put(&serial->kref, destroy_serial);
161 } 161 }
162 162
163 /***************************************************************************** 163 /*****************************************************************************
164 * Driver tty interface functions 164 * Driver tty interface functions
165 *****************************************************************************/ 165 *****************************************************************************/
166 166
167 /** 167 /**
168 * serial_install - install tty 168 * serial_install - install tty
169 * @driver: the driver (USB in our case) 169 * @driver: the driver (USB in our case)
170 * @tty: the tty being created 170 * @tty: the tty being created
171 * 171 *
172 * Create the termios objects for this tty. We use the default 172 * Create the termios objects for this tty. We use the default
173 * USB serial settings but permit them to be overridden by 173 * USB serial settings but permit them to be overridden by
174 * serial->type->init_termios. 174 * serial->type->init_termios.
175 * 175 *
176 * This is the first place a new tty gets used. Hence this is where we 176 * This is the first place a new tty gets used. Hence this is where we
177 * acquire references to the usb_serial structure and the driver module, 177 * acquire references to the usb_serial structure and the driver module,
178 * where we store a pointer to the port, and where we do an autoresume. 178 * where we store a pointer to the port, and where we do an autoresume.
179 * All these actions are reversed in serial_cleanup(). 179 * All these actions are reversed in serial_cleanup().
180 */ 180 */
181 static int serial_install(struct tty_driver *driver, struct tty_struct *tty) 181 static int serial_install(struct tty_driver *driver, struct tty_struct *tty)
182 { 182 {
183 int idx = tty->index; 183 int idx = tty->index;
184 struct usb_serial *serial; 184 struct usb_serial *serial;
185 struct usb_serial_port *port; 185 struct usb_serial_port *port;
186 int retval = -ENODEV; 186 int retval = -ENODEV;
187 187
188 serial = usb_serial_get_by_index(idx); 188 serial = usb_serial_get_by_index(idx);
189 if (!serial) 189 if (!serial)
190 return retval; 190 return retval;
191 191
192 port = serial->port[idx - serial->minor]; 192 port = serial->port[idx - serial->minor];
193 if (!port) 193 if (!port)
194 goto error_no_port; 194 goto error_no_port;
195 if (!try_module_get(serial->type->driver.owner)) 195 if (!try_module_get(serial->type->driver.owner))
196 goto error_module_get; 196 goto error_module_get;
197 197
198 retval = usb_autopm_get_interface(serial->interface); 198 retval = usb_autopm_get_interface(serial->interface);
199 if (retval) 199 if (retval)
200 goto error_get_interface; 200 goto error_get_interface;
201 201
202 retval = tty_port_install(&port->port, driver, tty); 202 retval = tty_port_install(&port->port, driver, tty);
203 if (retval) 203 if (retval)
204 goto error_init_termios; 204 goto error_init_termios;
205 205
206 mutex_unlock(&serial->disc_mutex); 206 mutex_unlock(&serial->disc_mutex);
207 207
208 /* allow the driver to update the settings */ 208 /* allow the driver to update the settings */
209 if (serial->type->init_termios) 209 if (serial->type->init_termios)
210 serial->type->init_termios(tty); 210 serial->type->init_termios(tty);
211 211
212 tty->driver_data = port; 212 tty->driver_data = port;
213 213
214 return retval; 214 return retval;
215 215
216 error_init_termios: 216 error_init_termios:
217 usb_autopm_put_interface(serial->interface); 217 usb_autopm_put_interface(serial->interface);
218 error_get_interface: 218 error_get_interface:
219 module_put(serial->type->driver.owner); 219 module_put(serial->type->driver.owner);
220 error_module_get: 220 error_module_get:
221 error_no_port: 221 error_no_port:
222 usb_serial_put(serial); 222 usb_serial_put(serial);
223 mutex_unlock(&serial->disc_mutex); 223 mutex_unlock(&serial->disc_mutex);
224 return retval; 224 return retval;
225 } 225 }
226 226
227 static int serial_activate(struct tty_port *tport, struct tty_struct *tty) 227 static int serial_activate(struct tty_port *tport, struct tty_struct *tty)
228 { 228 {
229 struct usb_serial_port *port = 229 struct usb_serial_port *port =
230 container_of(tport, struct usb_serial_port, port); 230 container_of(tport, struct usb_serial_port, port);
231 struct usb_serial *serial = port->serial; 231 struct usb_serial *serial = port->serial;
232 int retval; 232 int retval;
233 233
234 mutex_lock(&serial->disc_mutex); 234 mutex_lock(&serial->disc_mutex);
235 if (serial->disconnected) 235 if (serial->disconnected)
236 retval = -ENODEV; 236 retval = -ENODEV;
237 else 237 else
238 retval = port->serial->type->open(tty, port); 238 retval = port->serial->type->open(tty, port);
239 mutex_unlock(&serial->disc_mutex); 239 mutex_unlock(&serial->disc_mutex);
240 240
241 if (retval < 0) 241 if (retval < 0)
242 retval = usb_translate_errors(retval); 242 retval = usb_translate_errors(retval);
243 243
244 return retval; 244 return retval;
245 } 245 }
246 246
247 static int serial_open(struct tty_struct *tty, struct file *filp) 247 static int serial_open(struct tty_struct *tty, struct file *filp)
248 { 248 {
249 struct usb_serial_port *port = tty->driver_data; 249 struct usb_serial_port *port = tty->driver_data;
250 250
251 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 251 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
252 return tty_port_open(&port->port, tty, filp); 252 return tty_port_open(&port->port, tty, filp);
253 } 253 }
254 254
255 /** 255 /**
256 * serial_down - shut down hardware 256 * serial_down - shut down hardware
257 * @tport: tty port to shut down 257 * @tport: tty port to shut down
258 * 258 *
259 * Shut down a USB serial port unless it is the console. We never 259 * Shut down a USB serial port unless it is the console. We never
260 * shut down the console hardware as it will always be in use. Serialized 260 * shut down the console hardware as it will always be in use. Serialized
261 * against activate by the tport mutex and kept to matching open/close pairs 261 * against activate by the tport mutex and kept to matching open/close pairs
262 * of calls by the ASYNCB_INITIALIZED flag. 262 * of calls by the ASYNCB_INITIALIZED flag.
263 */ 263 */
264 static void serial_down(struct tty_port *tport) 264 static void serial_down(struct tty_port *tport)
265 { 265 {
266 struct usb_serial_port *port = 266 struct usb_serial_port *port =
267 container_of(tport, struct usb_serial_port, port); 267 container_of(tport, struct usb_serial_port, port);
268 struct usb_serial_driver *drv = port->serial->type; 268 struct usb_serial_driver *drv = port->serial->type;
269 /* 269 /*
270 * The console is magical. Do not hang up the console hardware 270 * The console is magical. Do not hang up the console hardware
271 * or there will be tears. 271 * or there will be tears.
272 */ 272 */
273 if (port->port.console) 273 if (port->port.console)
274 return; 274 return;
275 if (drv->close) 275 if (drv->close)
276 drv->close(port); 276 drv->close(port);
277 } 277 }
278 278
279 static void serial_hangup(struct tty_struct *tty) 279 static void serial_hangup(struct tty_struct *tty)
280 { 280 {
281 struct usb_serial_port *port = tty->driver_data; 281 struct usb_serial_port *port = tty->driver_data;
282 282
283 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 283 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
284 tty_port_hangup(&port->port); 284 tty_port_hangup(&port->port);
285 } 285 }
286 286
287 static void serial_close(struct tty_struct *tty, struct file *filp) 287 static void serial_close(struct tty_struct *tty, struct file *filp)
288 { 288 {
289 struct usb_serial_port *port = tty->driver_data; 289 struct usb_serial_port *port = tty->driver_data;
290 290
291 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 291 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
292 tty_port_close(&port->port, tty, filp); 292 tty_port_close(&port->port, tty, filp);
293 } 293 }
294 294
295 /** 295 /**
296 * serial_cleanup - free resources post close/hangup 296 * serial_cleanup - free resources post close/hangup
297 * @port: port to free up 297 * @port: port to free up
298 * 298 *
299 * Do the resource freeing and refcount dropping for the port. 299 * Do the resource freeing and refcount dropping for the port.
300 * Avoid freeing the console. 300 * Avoid freeing the console.
301 * 301 *
302 * Called asynchronously after the last tty kref is dropped. 302 * Called asynchronously after the last tty kref is dropped.
303 */ 303 */
304 static void serial_cleanup(struct tty_struct *tty) 304 static void serial_cleanup(struct tty_struct *tty)
305 { 305 {
306 struct usb_serial_port *port = tty->driver_data; 306 struct usb_serial_port *port = tty->driver_data;
307 struct usb_serial *serial; 307 struct usb_serial *serial;
308 struct module *owner; 308 struct module *owner;
309 309
310 /* The console is magical. Do not hang up the console hardware 310 /* The console is magical. Do not hang up the console hardware
311 * or there will be tears. 311 * or there will be tears.
312 */ 312 */
313 if (port->port.console) 313 if (port->port.console)
314 return; 314 return;
315 315
316 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 316 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
317 317
318 tty->driver_data = NULL; 318 tty->driver_data = NULL;
319 319
320 serial = port->serial; 320 serial = port->serial;
321 owner = serial->type->driver.owner; 321 owner = serial->type->driver.owner;
322 322
323 mutex_lock(&serial->disc_mutex); 323 mutex_lock(&serial->disc_mutex);
324 if (!serial->disconnected) 324 if (!serial->disconnected)
325 usb_autopm_put_interface(serial->interface); 325 usb_autopm_put_interface(serial->interface);
326 mutex_unlock(&serial->disc_mutex); 326 mutex_unlock(&serial->disc_mutex);
327 327
328 usb_serial_put(serial); 328 usb_serial_put(serial);
329 module_put(owner); 329 module_put(owner);
330 } 330 }
331 331
332 static int serial_write(struct tty_struct *tty, const unsigned char *buf, 332 static int serial_write(struct tty_struct *tty, const unsigned char *buf,
333 int count) 333 int count)
334 { 334 {
335 struct usb_serial_port *port = tty->driver_data; 335 struct usb_serial_port *port = tty->driver_data;
336 int retval = -ENODEV; 336 int retval = -ENODEV;
337 337
338 if (port->serial->dev->state == USB_STATE_NOTATTACHED) 338 if (port->serial->dev->state == USB_STATE_NOTATTACHED)
339 goto exit; 339 goto exit;
340 340
341 dev_dbg(tty->dev, "%s - port %d, %d byte(s)\n", __func__, 341 dev_dbg(tty->dev, "%s - port %d, %d byte(s)\n", __func__,
342 port->number, count); 342 port->number, count);
343 343
344 /* pass on to the driver specific version of this function */ 344 /* pass on to the driver specific version of this function */
345 retval = port->serial->type->write(tty, port, buf, count); 345 retval = port->serial->type->write(tty, port, buf, count);
346 if (retval < 0) 346 if (retval < 0)
347 retval = usb_translate_errors(retval); 347 retval = usb_translate_errors(retval);
348 exit: 348 exit:
349 return retval; 349 return retval;
350 } 350 }
351 351
352 static int serial_write_room(struct tty_struct *tty) 352 static int serial_write_room(struct tty_struct *tty)
353 { 353 {
354 struct usb_serial_port *port = tty->driver_data; 354 struct usb_serial_port *port = tty->driver_data;
355 355
356 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 356 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
357 /* pass on to the driver specific version of this function */ 357 /* pass on to the driver specific version of this function */
358 return port->serial->type->write_room(tty); 358 return port->serial->type->write_room(tty);
359 } 359 }
360 360
361 static int serial_chars_in_buffer(struct tty_struct *tty) 361 static int serial_chars_in_buffer(struct tty_struct *tty)
362 { 362 {
363 struct usb_serial_port *port = tty->driver_data; 363 struct usb_serial_port *port = tty->driver_data;
364 struct usb_serial *serial = port->serial; 364 struct usb_serial *serial = port->serial;
365 int count = 0; 365 int count = 0;
366 366
367 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 367 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
368 368
369 mutex_lock(&serial->disc_mutex); 369 mutex_lock(&serial->disc_mutex);
370 /* if the device was unplugged then any remaining characters 370 /* if the device was unplugged then any remaining characters
371 fell out of the connector ;) */ 371 fell out of the connector ;) */
372 if (serial->disconnected) 372 if (serial->disconnected)
373 count = 0; 373 count = 0;
374 else 374 else
375 count = serial->type->chars_in_buffer(tty); 375 count = serial->type->chars_in_buffer(tty);
376 mutex_unlock(&serial->disc_mutex); 376 mutex_unlock(&serial->disc_mutex);
377 377
378 return count; 378 return count;
379 } 379 }
380 380
381 static void serial_throttle(struct tty_struct *tty) 381 static void serial_throttle(struct tty_struct *tty)
382 { 382 {
383 struct usb_serial_port *port = tty->driver_data; 383 struct usb_serial_port *port = tty->driver_data;
384 384
385 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 385 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
386 386
387 /* pass on to the driver specific version of this function */ 387 /* pass on to the driver specific version of this function */
388 if (port->serial->type->throttle) 388 if (port->serial->type->throttle)
389 port->serial->type->throttle(tty); 389 port->serial->type->throttle(tty);
390 } 390 }
391 391
392 static void serial_unthrottle(struct tty_struct *tty) 392 static void serial_unthrottle(struct tty_struct *tty)
393 { 393 {
394 struct usb_serial_port *port = tty->driver_data; 394 struct usb_serial_port *port = tty->driver_data;
395 395
396 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 396 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
397 397
398 /* pass on to the driver specific version of this function */ 398 /* pass on to the driver specific version of this function */
399 if (port->serial->type->unthrottle) 399 if (port->serial->type->unthrottle)
400 port->serial->type->unthrottle(tty); 400 port->serial->type->unthrottle(tty);
401 } 401 }
402 402
403 static int serial_ioctl(struct tty_struct *tty, 403 static int serial_ioctl(struct tty_struct *tty,
404 unsigned int cmd, unsigned long arg) 404 unsigned int cmd, unsigned long arg)
405 { 405 {
406 struct usb_serial_port *port = tty->driver_data; 406 struct usb_serial_port *port = tty->driver_data;
407 int retval = -ENODEV; 407 int retval = -ENODEV;
408 408
409 dev_dbg(tty->dev, "%s - port %d, cmd 0x%.4x\n", __func__, 409 dev_dbg(tty->dev, "%s - port %d, cmd 0x%.4x\n", __func__,
410 port->number, cmd); 410 port->number, cmd);
411 411
412 /* pass on to the driver specific version of this function 412 /* pass on to the driver specific version of this function
413 if it is available */ 413 if it is available */
414 if (port->serial->type->ioctl) { 414 if (port->serial->type->ioctl) {
415 retval = port->serial->type->ioctl(tty, cmd, arg); 415 retval = port->serial->type->ioctl(tty, cmd, arg);
416 } else 416 } else
417 retval = -ENOIOCTLCMD; 417 retval = -ENOIOCTLCMD;
418 return retval; 418 return retval;
419 } 419 }
420 420
421 static void serial_set_termios(struct tty_struct *tty, struct ktermios *old) 421 static void serial_set_termios(struct tty_struct *tty, struct ktermios *old)
422 { 422 {
423 struct usb_serial_port *port = tty->driver_data; 423 struct usb_serial_port *port = tty->driver_data;
424 424
425 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 425 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
426 426
427 /* pass on to the driver specific version of this function 427 /* pass on to the driver specific version of this function
428 if it is available */ 428 if it is available */
429 if (port->serial->type->set_termios) 429 if (port->serial->type->set_termios)
430 port->serial->type->set_termios(tty, port, old); 430 port->serial->type->set_termios(tty, port, old);
431 else 431 else
432 tty_termios_copy_hw(&tty->termios, old); 432 tty_termios_copy_hw(&tty->termios, old);
433 } 433 }
434 434
435 static int serial_break(struct tty_struct *tty, int break_state) 435 static int serial_break(struct tty_struct *tty, int break_state)
436 { 436 {
437 struct usb_serial_port *port = tty->driver_data; 437 struct usb_serial_port *port = tty->driver_data;
438 438
439 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 439 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
440 440
441 /* pass on to the driver specific version of this function 441 /* pass on to the driver specific version of this function
442 if it is available */ 442 if it is available */
443 if (port->serial->type->break_ctl) 443 if (port->serial->type->break_ctl)
444 port->serial->type->break_ctl(tty, break_state); 444 port->serial->type->break_ctl(tty, break_state);
445 return 0; 445 return 0;
446 } 446 }
447 447
448 static int serial_proc_show(struct seq_file *m, void *v) 448 static int serial_proc_show(struct seq_file *m, void *v)
449 { 449 {
450 struct usb_serial *serial; 450 struct usb_serial *serial;
451 int i; 451 int i;
452 char tmp[40]; 452 char tmp[40];
453 453
454 seq_puts(m, "usbserinfo:1.0 driver:2.0\n"); 454 seq_puts(m, "usbserinfo:1.0 driver:2.0\n");
455 for (i = 0; i < SERIAL_TTY_MINORS; ++i) { 455 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
456 serial = usb_serial_get_by_index(i); 456 serial = usb_serial_get_by_index(i);
457 if (serial == NULL) 457 if (serial == NULL)
458 continue; 458 continue;
459 459
460 seq_printf(m, "%d:", i); 460 seq_printf(m, "%d:", i);
461 if (serial->type->driver.owner) 461 if (serial->type->driver.owner)
462 seq_printf(m, " module:%s", 462 seq_printf(m, " module:%s",
463 module_name(serial->type->driver.owner)); 463 module_name(serial->type->driver.owner));
464 seq_printf(m, " name:\"%s\"", 464 seq_printf(m, " name:\"%s\"",
465 serial->type->description); 465 serial->type->description);
466 seq_printf(m, " vendor:%04x product:%04x", 466 seq_printf(m, " vendor:%04x product:%04x",
467 le16_to_cpu(serial->dev->descriptor.idVendor), 467 le16_to_cpu(serial->dev->descriptor.idVendor),
468 le16_to_cpu(serial->dev->descriptor.idProduct)); 468 le16_to_cpu(serial->dev->descriptor.idProduct));
469 seq_printf(m, " num_ports:%d", serial->num_ports); 469 seq_printf(m, " num_ports:%d", serial->num_ports);
470 seq_printf(m, " port:%d", i - serial->minor + 1); 470 seq_printf(m, " port:%d", i - serial->minor + 1);
471 usb_make_path(serial->dev, tmp, sizeof(tmp)); 471 usb_make_path(serial->dev, tmp, sizeof(tmp));
472 seq_printf(m, " path:%s", tmp); 472 seq_printf(m, " path:%s", tmp);
473 473
474 seq_putc(m, '\n'); 474 seq_putc(m, '\n');
475 usb_serial_put(serial); 475 usb_serial_put(serial);
476 mutex_unlock(&serial->disc_mutex); 476 mutex_unlock(&serial->disc_mutex);
477 } 477 }
478 return 0; 478 return 0;
479 } 479 }
480 480
481 static int serial_proc_open(struct inode *inode, struct file *file) 481 static int serial_proc_open(struct inode *inode, struct file *file)
482 { 482 {
483 return single_open(file, serial_proc_show, NULL); 483 return single_open(file, serial_proc_show, NULL);
484 } 484 }
485 485
486 static const struct file_operations serial_proc_fops = { 486 static const struct file_operations serial_proc_fops = {
487 .owner = THIS_MODULE, 487 .owner = THIS_MODULE,
488 .open = serial_proc_open, 488 .open = serial_proc_open,
489 .read = seq_read, 489 .read = seq_read,
490 .llseek = seq_lseek, 490 .llseek = seq_lseek,
491 .release = single_release, 491 .release = single_release,
492 }; 492 };
493 493
494 static int serial_tiocmget(struct tty_struct *tty) 494 static int serial_tiocmget(struct tty_struct *tty)
495 { 495 {
496 struct usb_serial_port *port = tty->driver_data; 496 struct usb_serial_port *port = tty->driver_data;
497 497
498 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 498 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
499 499
500 if (port->serial->type->tiocmget) 500 if (port->serial->type->tiocmget)
501 return port->serial->type->tiocmget(tty); 501 return port->serial->type->tiocmget(tty);
502 return -EINVAL; 502 return -EINVAL;
503 } 503 }
504 504
505 static int serial_tiocmset(struct tty_struct *tty, 505 static int serial_tiocmset(struct tty_struct *tty,
506 unsigned int set, unsigned int clear) 506 unsigned int set, unsigned int clear)
507 { 507 {
508 struct usb_serial_port *port = tty->driver_data; 508 struct usb_serial_port *port = tty->driver_data;
509 509
510 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 510 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
511 511
512 if (port->serial->type->tiocmset) 512 if (port->serial->type->tiocmset)
513 return port->serial->type->tiocmset(tty, set, clear); 513 return port->serial->type->tiocmset(tty, set, clear);
514 return -EINVAL; 514 return -EINVAL;
515 } 515 }
516 516
517 static int serial_get_icount(struct tty_struct *tty, 517 static int serial_get_icount(struct tty_struct *tty,
518 struct serial_icounter_struct *icount) 518 struct serial_icounter_struct *icount)
519 { 519 {
520 struct usb_serial_port *port = tty->driver_data; 520 struct usb_serial_port *port = tty->driver_data;
521 521
522 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number); 522 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
523 523
524 if (port->serial->type->get_icount) 524 if (port->serial->type->get_icount)
525 return port->serial->type->get_icount(tty, icount); 525 return port->serial->type->get_icount(tty, icount);
526 return -EINVAL; 526 return -EINVAL;
527 } 527 }
528 528
529 /* 529 /*
530 * We would be calling tty_wakeup here, but unfortunately some line 530 * We would be calling tty_wakeup here, but unfortunately some line
531 * disciplines have an annoying habit of calling tty->write from 531 * disciplines have an annoying habit of calling tty->write from
532 * the write wakeup callback (e.g. n_hdlc.c). 532 * the write wakeup callback (e.g. n_hdlc.c).
533 */ 533 */
534 void usb_serial_port_softint(struct usb_serial_port *port) 534 void usb_serial_port_softint(struct usb_serial_port *port)
535 { 535 {
536 schedule_work(&port->work); 536 schedule_work(&port->work);
537 } 537 }
538 EXPORT_SYMBOL_GPL(usb_serial_port_softint); 538 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
539 539
540 static void usb_serial_port_work(struct work_struct *work) 540 static void usb_serial_port_work(struct work_struct *work)
541 { 541 {
542 struct usb_serial_port *port = 542 struct usb_serial_port *port =
543 container_of(work, struct usb_serial_port, work); 543 container_of(work, struct usb_serial_port, work);
544 struct tty_struct *tty;
545 544
546 tty = tty_port_tty_get(&port->port); 545 tty_port_tty_wakeup(&port->port);
547 if (!tty)
548 return;
549
550 dev_dbg(tty->dev, "%s - port %d\n", __func__, port->number);
551
552 tty_wakeup(tty);
553 tty_kref_put(tty);
554 } 546 }
555 547
556 static void kill_traffic(struct usb_serial_port *port) 548 static void kill_traffic(struct usb_serial_port *port)
557 { 549 {
558 int i; 550 int i;
559 551
560 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) 552 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
561 usb_kill_urb(port->read_urbs[i]); 553 usb_kill_urb(port->read_urbs[i]);
562 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) 554 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
563 usb_kill_urb(port->write_urbs[i]); 555 usb_kill_urb(port->write_urbs[i]);
564 /* 556 /*
565 * This is tricky. 557 * This is tricky.
566 * Some drivers submit the read_urb in the 558 * Some drivers submit the read_urb in the
567 * handler for the write_urb or vice versa 559 * handler for the write_urb or vice versa
568 * this order determines the order in which 560 * this order determines the order in which
569 * usb_kill_urb() must be used to reliably 561 * usb_kill_urb() must be used to reliably
570 * kill the URBs. As it is unknown here, 562 * kill the URBs. As it is unknown here,
571 * both orders must be used in turn. 563 * both orders must be used in turn.
572 * The call below is not redundant. 564 * The call below is not redundant.
573 */ 565 */
574 usb_kill_urb(port->read_urb); 566 usb_kill_urb(port->read_urb);
575 usb_kill_urb(port->interrupt_in_urb); 567 usb_kill_urb(port->interrupt_in_urb);
576 usb_kill_urb(port->interrupt_out_urb); 568 usb_kill_urb(port->interrupt_out_urb);
577 } 569 }
578 570
579 static void port_release(struct device *dev) 571 static void port_release(struct device *dev)
580 { 572 {
581 struct usb_serial_port *port = to_usb_serial_port(dev); 573 struct usb_serial_port *port = to_usb_serial_port(dev);
582 int i; 574 int i;
583 575
584 dev_dbg(dev, "%s\n", __func__); 576 dev_dbg(dev, "%s\n", __func__);
585 577
586 /* 578 /*
587 * Stop all the traffic before cancelling the work, so that 579 * Stop all the traffic before cancelling the work, so that
588 * nobody will restart it by calling usb_serial_port_softint. 580 * nobody will restart it by calling usb_serial_port_softint.
589 */ 581 */
590 kill_traffic(port); 582 kill_traffic(port);
591 cancel_work_sync(&port->work); 583 cancel_work_sync(&port->work);
592 584
593 usb_free_urb(port->interrupt_in_urb); 585 usb_free_urb(port->interrupt_in_urb);
594 usb_free_urb(port->interrupt_out_urb); 586 usb_free_urb(port->interrupt_out_urb);
595 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) { 587 for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
596 usb_free_urb(port->read_urbs[i]); 588 usb_free_urb(port->read_urbs[i]);
597 kfree(port->bulk_in_buffers[i]); 589 kfree(port->bulk_in_buffers[i]);
598 } 590 }
599 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) { 591 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
600 usb_free_urb(port->write_urbs[i]); 592 usb_free_urb(port->write_urbs[i]);
601 kfree(port->bulk_out_buffers[i]); 593 kfree(port->bulk_out_buffers[i]);
602 } 594 }
603 kfifo_free(&port->write_fifo); 595 kfifo_free(&port->write_fifo);
604 kfree(port->interrupt_in_buffer); 596 kfree(port->interrupt_in_buffer);
605 kfree(port->interrupt_out_buffer); 597 kfree(port->interrupt_out_buffer);
606 tty_port_destroy(&port->port); 598 tty_port_destroy(&port->port);
607 kfree(port); 599 kfree(port);
608 } 600 }
609 601
610 static struct usb_serial *create_serial(struct usb_device *dev, 602 static struct usb_serial *create_serial(struct usb_device *dev,
611 struct usb_interface *interface, 603 struct usb_interface *interface,
612 struct usb_serial_driver *driver) 604 struct usb_serial_driver *driver)
613 { 605 {
614 struct usb_serial *serial; 606 struct usb_serial *serial;
615 607
616 serial = kzalloc(sizeof(*serial), GFP_KERNEL); 608 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
617 if (!serial) { 609 if (!serial) {
618 dev_err(&dev->dev, "%s - out of memory\n", __func__); 610 dev_err(&dev->dev, "%s - out of memory\n", __func__);
619 return NULL; 611 return NULL;
620 } 612 }
621 serial->dev = usb_get_dev(dev); 613 serial->dev = usb_get_dev(dev);
622 serial->type = driver; 614 serial->type = driver;
623 serial->interface = interface; 615 serial->interface = interface;
624 kref_init(&serial->kref); 616 kref_init(&serial->kref);
625 mutex_init(&serial->disc_mutex); 617 mutex_init(&serial->disc_mutex);
626 serial->minor = SERIAL_TTY_NO_MINOR; 618 serial->minor = SERIAL_TTY_NO_MINOR;
627 619
628 return serial; 620 return serial;
629 } 621 }
630 622
631 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf, 623 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
632 struct usb_serial_driver *drv) 624 struct usb_serial_driver *drv)
633 { 625 {
634 struct usb_dynid *dynid; 626 struct usb_dynid *dynid;
635 627
636 spin_lock(&drv->dynids.lock); 628 spin_lock(&drv->dynids.lock);
637 list_for_each_entry(dynid, &drv->dynids.list, node) { 629 list_for_each_entry(dynid, &drv->dynids.list, node) {
638 if (usb_match_one_id(intf, &dynid->id)) { 630 if (usb_match_one_id(intf, &dynid->id)) {
639 spin_unlock(&drv->dynids.lock); 631 spin_unlock(&drv->dynids.lock);
640 return &dynid->id; 632 return &dynid->id;
641 } 633 }
642 } 634 }
643 spin_unlock(&drv->dynids.lock); 635 spin_unlock(&drv->dynids.lock);
644 return NULL; 636 return NULL;
645 } 637 }
646 638
647 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv, 639 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
648 struct usb_interface *intf) 640 struct usb_interface *intf)
649 { 641 {
650 const struct usb_device_id *id; 642 const struct usb_device_id *id;
651 643
652 id = usb_match_id(intf, drv->id_table); 644 id = usb_match_id(intf, drv->id_table);
653 if (id) { 645 if (id) {
654 dev_dbg(&intf->dev, "static descriptor matches\n"); 646 dev_dbg(&intf->dev, "static descriptor matches\n");
655 goto exit; 647 goto exit;
656 } 648 }
657 id = match_dynamic_id(intf, drv); 649 id = match_dynamic_id(intf, drv);
658 if (id) 650 if (id)
659 dev_dbg(&intf->dev, "dynamic descriptor matches\n"); 651 dev_dbg(&intf->dev, "dynamic descriptor matches\n");
660 exit: 652 exit:
661 return id; 653 return id;
662 } 654 }
663 655
664 /* Caller must hold table_lock */ 656 /* Caller must hold table_lock */
665 static struct usb_serial_driver *search_serial_device( 657 static struct usb_serial_driver *search_serial_device(
666 struct usb_interface *iface) 658 struct usb_interface *iface)
667 { 659 {
668 const struct usb_device_id *id = NULL; 660 const struct usb_device_id *id = NULL;
669 struct usb_serial_driver *drv; 661 struct usb_serial_driver *drv;
670 struct usb_driver *driver = to_usb_driver(iface->dev.driver); 662 struct usb_driver *driver = to_usb_driver(iface->dev.driver);
671 663
672 /* Check if the usb id matches a known device */ 664 /* Check if the usb id matches a known device */
673 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) { 665 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
674 if (drv->usb_driver == driver) 666 if (drv->usb_driver == driver)
675 id = get_iface_id(drv, iface); 667 id = get_iface_id(drv, iface);
676 if (id) 668 if (id)
677 return drv; 669 return drv;
678 } 670 }
679 671
680 return NULL; 672 return NULL;
681 } 673 }
682 674
683 static int serial_carrier_raised(struct tty_port *port) 675 static int serial_carrier_raised(struct tty_port *port)
684 { 676 {
685 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port); 677 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
686 struct usb_serial_driver *drv = p->serial->type; 678 struct usb_serial_driver *drv = p->serial->type;
687 679
688 if (drv->carrier_raised) 680 if (drv->carrier_raised)
689 return drv->carrier_raised(p); 681 return drv->carrier_raised(p);
690 /* No carrier control - don't block */ 682 /* No carrier control - don't block */
691 return 1; 683 return 1;
692 } 684 }
693 685
694 static void serial_dtr_rts(struct tty_port *port, int on) 686 static void serial_dtr_rts(struct tty_port *port, int on)
695 { 687 {
696 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port); 688 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
697 struct usb_serial *serial = p->serial; 689 struct usb_serial *serial = p->serial;
698 struct usb_serial_driver *drv = serial->type; 690 struct usb_serial_driver *drv = serial->type;
699 691
700 if (!drv->dtr_rts) 692 if (!drv->dtr_rts)
701 return; 693 return;
702 /* 694 /*
703 * Work-around bug in the tty-layer which can result in dtr_rts 695 * Work-around bug in the tty-layer which can result in dtr_rts
704 * being called after a disconnect (and tty_unregister_device 696 * being called after a disconnect (and tty_unregister_device
705 * has returned). Remove once bug has been squashed. 697 * has returned). Remove once bug has been squashed.
706 */ 698 */
707 mutex_lock(&serial->disc_mutex); 699 mutex_lock(&serial->disc_mutex);
708 if (!serial->disconnected) 700 if (!serial->disconnected)
709 drv->dtr_rts(p, on); 701 drv->dtr_rts(p, on);
710 mutex_unlock(&serial->disc_mutex); 702 mutex_unlock(&serial->disc_mutex);
711 } 703 }
712 704
713 static const struct tty_port_operations serial_port_ops = { 705 static const struct tty_port_operations serial_port_ops = {
714 .carrier_raised = serial_carrier_raised, 706 .carrier_raised = serial_carrier_raised,
715 .dtr_rts = serial_dtr_rts, 707 .dtr_rts = serial_dtr_rts,
716 .activate = serial_activate, 708 .activate = serial_activate,
717 .shutdown = serial_down, 709 .shutdown = serial_down,
718 }; 710 };
719 711
720 static int usb_serial_probe(struct usb_interface *interface, 712 static int usb_serial_probe(struct usb_interface *interface,
721 const struct usb_device_id *id) 713 const struct usb_device_id *id)
722 { 714 {
723 struct device *ddev = &interface->dev; 715 struct device *ddev = &interface->dev;
724 struct usb_device *dev = interface_to_usbdev(interface); 716 struct usb_device *dev = interface_to_usbdev(interface);
725 struct usb_serial *serial = NULL; 717 struct usb_serial *serial = NULL;
726 struct usb_serial_port *port; 718 struct usb_serial_port *port;
727 struct usb_host_interface *iface_desc; 719 struct usb_host_interface *iface_desc;
728 struct usb_endpoint_descriptor *endpoint; 720 struct usb_endpoint_descriptor *endpoint;
729 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS]; 721 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
730 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS]; 722 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
731 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS]; 723 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
732 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS]; 724 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
733 struct usb_serial_driver *type = NULL; 725 struct usb_serial_driver *type = NULL;
734 int retval; 726 int retval;
735 unsigned int minor; 727 unsigned int minor;
736 int buffer_size; 728 int buffer_size;
737 int i; 729 int i;
738 int j; 730 int j;
739 int num_interrupt_in = 0; 731 int num_interrupt_in = 0;
740 int num_interrupt_out = 0; 732 int num_interrupt_out = 0;
741 int num_bulk_in = 0; 733 int num_bulk_in = 0;
742 int num_bulk_out = 0; 734 int num_bulk_out = 0;
743 int num_ports = 0; 735 int num_ports = 0;
744 int max_endpoints; 736 int max_endpoints;
745 737
746 mutex_lock(&table_lock); 738 mutex_lock(&table_lock);
747 type = search_serial_device(interface); 739 type = search_serial_device(interface);
748 if (!type) { 740 if (!type) {
749 mutex_unlock(&table_lock); 741 mutex_unlock(&table_lock);
750 dev_dbg(ddev, "none matched\n"); 742 dev_dbg(ddev, "none matched\n");
751 return -ENODEV; 743 return -ENODEV;
752 } 744 }
753 745
754 if (!try_module_get(type->driver.owner)) { 746 if (!try_module_get(type->driver.owner)) {
755 mutex_unlock(&table_lock); 747 mutex_unlock(&table_lock);
756 dev_err(ddev, "module get failed, exiting\n"); 748 dev_err(ddev, "module get failed, exiting\n");
757 return -EIO; 749 return -EIO;
758 } 750 }
759 mutex_unlock(&table_lock); 751 mutex_unlock(&table_lock);
760 752
761 serial = create_serial(dev, interface, type); 753 serial = create_serial(dev, interface, type);
762 if (!serial) { 754 if (!serial) {
763 module_put(type->driver.owner); 755 module_put(type->driver.owner);
764 dev_err(ddev, "%s - out of memory\n", __func__); 756 dev_err(ddev, "%s - out of memory\n", __func__);
765 return -ENOMEM; 757 return -ENOMEM;
766 } 758 }
767 759
768 /* if this device type has a probe function, call it */ 760 /* if this device type has a probe function, call it */
769 if (type->probe) { 761 if (type->probe) {
770 const struct usb_device_id *id; 762 const struct usb_device_id *id;
771 763
772 id = get_iface_id(type, interface); 764 id = get_iface_id(type, interface);
773 retval = type->probe(serial, id); 765 retval = type->probe(serial, id);
774 766
775 if (retval) { 767 if (retval) {
776 dev_dbg(ddev, "sub driver rejected device\n"); 768 dev_dbg(ddev, "sub driver rejected device\n");
777 usb_serial_put(serial); 769 usb_serial_put(serial);
778 module_put(type->driver.owner); 770 module_put(type->driver.owner);
779 return retval; 771 return retval;
780 } 772 }
781 } 773 }
782 774
783 /* descriptor matches, let's find the endpoints needed */ 775 /* descriptor matches, let's find the endpoints needed */
784 /* check out the endpoints */ 776 /* check out the endpoints */
785 iface_desc = interface->cur_altsetting; 777 iface_desc = interface->cur_altsetting;
786 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 778 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
787 endpoint = &iface_desc->endpoint[i].desc; 779 endpoint = &iface_desc->endpoint[i].desc;
788 780
789 if (usb_endpoint_is_bulk_in(endpoint)) { 781 if (usb_endpoint_is_bulk_in(endpoint)) {
790 /* we found a bulk in endpoint */ 782 /* we found a bulk in endpoint */
791 dev_dbg(ddev, "found bulk in on endpoint %d\n", i); 783 dev_dbg(ddev, "found bulk in on endpoint %d\n", i);
792 bulk_in_endpoint[num_bulk_in] = endpoint; 784 bulk_in_endpoint[num_bulk_in] = endpoint;
793 ++num_bulk_in; 785 ++num_bulk_in;
794 } 786 }
795 787
796 if (usb_endpoint_is_bulk_out(endpoint)) { 788 if (usb_endpoint_is_bulk_out(endpoint)) {
797 /* we found a bulk out endpoint */ 789 /* we found a bulk out endpoint */
798 dev_dbg(ddev, "found bulk out on endpoint %d\n", i); 790 dev_dbg(ddev, "found bulk out on endpoint %d\n", i);
799 bulk_out_endpoint[num_bulk_out] = endpoint; 791 bulk_out_endpoint[num_bulk_out] = endpoint;
800 ++num_bulk_out; 792 ++num_bulk_out;
801 } 793 }
802 794
803 if (usb_endpoint_is_int_in(endpoint)) { 795 if (usb_endpoint_is_int_in(endpoint)) {
804 /* we found a interrupt in endpoint */ 796 /* we found a interrupt in endpoint */
805 dev_dbg(ddev, "found interrupt in on endpoint %d\n", i); 797 dev_dbg(ddev, "found interrupt in on endpoint %d\n", i);
806 interrupt_in_endpoint[num_interrupt_in] = endpoint; 798 interrupt_in_endpoint[num_interrupt_in] = endpoint;
807 ++num_interrupt_in; 799 ++num_interrupt_in;
808 } 800 }
809 801
810 if (usb_endpoint_is_int_out(endpoint)) { 802 if (usb_endpoint_is_int_out(endpoint)) {
811 /* we found an interrupt out endpoint */ 803 /* we found an interrupt out endpoint */
812 dev_dbg(ddev, "found interrupt out on endpoint %d\n", i); 804 dev_dbg(ddev, "found interrupt out on endpoint %d\n", i);
813 interrupt_out_endpoint[num_interrupt_out] = endpoint; 805 interrupt_out_endpoint[num_interrupt_out] = endpoint;
814 ++num_interrupt_out; 806 ++num_interrupt_out;
815 } 807 }
816 } 808 }
817 809
818 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE) 810 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
819 /* BEGIN HORRIBLE HACK FOR PL2303 */ 811 /* BEGIN HORRIBLE HACK FOR PL2303 */
820 /* this is needed due to the looney way its endpoints are set up */ 812 /* this is needed due to the looney way its endpoints are set up */
821 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) && 813 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
822 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) || 814 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
823 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) && 815 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
824 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) || 816 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
825 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) && 817 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
826 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) || 818 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) ||
827 ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) && 819 ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
828 (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) { 820 (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) {
829 if (interface != dev->actconfig->interface[0]) { 821 if (interface != dev->actconfig->interface[0]) {
830 /* check out the endpoints of the other interface*/ 822 /* check out the endpoints of the other interface*/
831 iface_desc = dev->actconfig->interface[0]->cur_altsetting; 823 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
832 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 824 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
833 endpoint = &iface_desc->endpoint[i].desc; 825 endpoint = &iface_desc->endpoint[i].desc;
834 if (usb_endpoint_is_int_in(endpoint)) { 826 if (usb_endpoint_is_int_in(endpoint)) {
835 /* we found a interrupt in endpoint */ 827 /* we found a interrupt in endpoint */
836 dev_dbg(ddev, "found interrupt in for Prolific device on separate interface\n"); 828 dev_dbg(ddev, "found interrupt in for Prolific device on separate interface\n");
837 interrupt_in_endpoint[num_interrupt_in] = endpoint; 829 interrupt_in_endpoint[num_interrupt_in] = endpoint;
838 ++num_interrupt_in; 830 ++num_interrupt_in;
839 } 831 }
840 } 832 }
841 } 833 }
842 834
843 /* Now make sure the PL-2303 is configured correctly. 835 /* Now make sure the PL-2303 is configured correctly.
844 * If not, give up now and hope this hack will work 836 * If not, give up now and hope this hack will work
845 * properly during a later invocation of usb_serial_probe 837 * properly during a later invocation of usb_serial_probe
846 */ 838 */
847 if (num_bulk_in == 0 || num_bulk_out == 0) { 839 if (num_bulk_in == 0 || num_bulk_out == 0) {
848 dev_info(ddev, "PL-2303 hack: descriptors matched but endpoints did not\n"); 840 dev_info(ddev, "PL-2303 hack: descriptors matched but endpoints did not\n");
849 usb_serial_put(serial); 841 usb_serial_put(serial);
850 module_put(type->driver.owner); 842 module_put(type->driver.owner);
851 return -ENODEV; 843 return -ENODEV;
852 } 844 }
853 } 845 }
854 /* END HORRIBLE HACK FOR PL2303 */ 846 /* END HORRIBLE HACK FOR PL2303 */
855 #endif 847 #endif
856 848
857 #ifdef CONFIG_USB_SERIAL_GENERIC 849 #ifdef CONFIG_USB_SERIAL_GENERIC
858 if (type == &usb_serial_generic_device) { 850 if (type == &usb_serial_generic_device) {
859 num_ports = num_bulk_out; 851 num_ports = num_bulk_out;
860 if (num_ports == 0) { 852 if (num_ports == 0) {
861 dev_err(ddev, "Generic device with no bulk out, not allowed.\n"); 853 dev_err(ddev, "Generic device with no bulk out, not allowed.\n");
862 usb_serial_put(serial); 854 usb_serial_put(serial);
863 module_put(type->driver.owner); 855 module_put(type->driver.owner);
864 return -EIO; 856 return -EIO;
865 } 857 }
866 dev_info(ddev, "The \"generic\" usb-serial driver is only for testing and one-off prototypes.\n"); 858 dev_info(ddev, "The \"generic\" usb-serial driver is only for testing and one-off prototypes.\n");
867 dev_info(ddev, "Tell linux-usb@vger.kernel.org to add your device to a proper driver.\n"); 859 dev_info(ddev, "Tell linux-usb@vger.kernel.org to add your device to a proper driver.\n");
868 } 860 }
869 #endif 861 #endif
870 if (!num_ports) { 862 if (!num_ports) {
871 /* if this device type has a calc_num_ports function, call it */ 863 /* if this device type has a calc_num_ports function, call it */
872 if (type->calc_num_ports) 864 if (type->calc_num_ports)
873 num_ports = type->calc_num_ports(serial); 865 num_ports = type->calc_num_ports(serial);
874 if (!num_ports) 866 if (!num_ports)
875 num_ports = type->num_ports; 867 num_ports = type->num_ports;
876 } 868 }
877 869
878 serial->num_ports = num_ports; 870 serial->num_ports = num_ports;
879 serial->num_bulk_in = num_bulk_in; 871 serial->num_bulk_in = num_bulk_in;
880 serial->num_bulk_out = num_bulk_out; 872 serial->num_bulk_out = num_bulk_out;
881 serial->num_interrupt_in = num_interrupt_in; 873 serial->num_interrupt_in = num_interrupt_in;
882 serial->num_interrupt_out = num_interrupt_out; 874 serial->num_interrupt_out = num_interrupt_out;
883 875
884 /* found all that we need */ 876 /* found all that we need */
885 dev_info(ddev, "%s converter detected\n", type->description); 877 dev_info(ddev, "%s converter detected\n", type->description);
886 878
887 /* create our ports, we need as many as the max endpoints */ 879 /* create our ports, we need as many as the max endpoints */
888 /* we don't use num_ports here because some devices have more 880 /* we don't use num_ports here because some devices have more
889 endpoint pairs than ports */ 881 endpoint pairs than ports */
890 max_endpoints = max(num_bulk_in, num_bulk_out); 882 max_endpoints = max(num_bulk_in, num_bulk_out);
891 max_endpoints = max(max_endpoints, num_interrupt_in); 883 max_endpoints = max(max_endpoints, num_interrupt_in);
892 max_endpoints = max(max_endpoints, num_interrupt_out); 884 max_endpoints = max(max_endpoints, num_interrupt_out);
893 max_endpoints = max(max_endpoints, (int)serial->num_ports); 885 max_endpoints = max(max_endpoints, (int)serial->num_ports);
894 serial->num_port_pointers = max_endpoints; 886 serial->num_port_pointers = max_endpoints;
895 887
896 dev_dbg(ddev, "setting up %d port structures for this device", max_endpoints); 888 dev_dbg(ddev, "setting up %d port structures for this device", max_endpoints);
897 for (i = 0; i < max_endpoints; ++i) { 889 for (i = 0; i < max_endpoints; ++i) {
898 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL); 890 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
899 if (!port) 891 if (!port)
900 goto probe_error; 892 goto probe_error;
901 tty_port_init(&port->port); 893 tty_port_init(&port->port);
902 port->port.ops = &serial_port_ops; 894 port->port.ops = &serial_port_ops;
903 port->serial = serial; 895 port->serial = serial;
904 spin_lock_init(&port->lock); 896 spin_lock_init(&port->lock);
905 /* Keep this for private driver use for the moment but 897 /* Keep this for private driver use for the moment but
906 should probably go away */ 898 should probably go away */
907 INIT_WORK(&port->work, usb_serial_port_work); 899 INIT_WORK(&port->work, usb_serial_port_work);
908 serial->port[i] = port; 900 serial->port[i] = port;
909 port->dev.parent = &interface->dev; 901 port->dev.parent = &interface->dev;
910 port->dev.driver = NULL; 902 port->dev.driver = NULL;
911 port->dev.bus = &usb_serial_bus_type; 903 port->dev.bus = &usb_serial_bus_type;
912 port->dev.release = &port_release; 904 port->dev.release = &port_release;
913 device_initialize(&port->dev); 905 device_initialize(&port->dev);
914 } 906 }
915 907
916 /* set up the endpoint information */ 908 /* set up the endpoint information */
917 for (i = 0; i < num_bulk_in; ++i) { 909 for (i = 0; i < num_bulk_in; ++i) {
918 endpoint = bulk_in_endpoint[i]; 910 endpoint = bulk_in_endpoint[i];
919 port = serial->port[i]; 911 port = serial->port[i];
920 buffer_size = max_t(int, serial->type->bulk_in_size, 912 buffer_size = max_t(int, serial->type->bulk_in_size,
921 usb_endpoint_maxp(endpoint)); 913 usb_endpoint_maxp(endpoint));
922 port->bulk_in_size = buffer_size; 914 port->bulk_in_size = buffer_size;
923 port->bulk_in_endpointAddress = endpoint->bEndpointAddress; 915 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
924 916
925 for (j = 0; j < ARRAY_SIZE(port->read_urbs); ++j) { 917 for (j = 0; j < ARRAY_SIZE(port->read_urbs); ++j) {
926 set_bit(j, &port->read_urbs_free); 918 set_bit(j, &port->read_urbs_free);
927 port->read_urbs[j] = usb_alloc_urb(0, GFP_KERNEL); 919 port->read_urbs[j] = usb_alloc_urb(0, GFP_KERNEL);
928 if (!port->read_urbs[j]) { 920 if (!port->read_urbs[j]) {
929 dev_err(ddev, "No free urbs available\n"); 921 dev_err(ddev, "No free urbs available\n");
930 goto probe_error; 922 goto probe_error;
931 } 923 }
932 port->bulk_in_buffers[j] = kmalloc(buffer_size, 924 port->bulk_in_buffers[j] = kmalloc(buffer_size,
933 GFP_KERNEL); 925 GFP_KERNEL);
934 if (!port->bulk_in_buffers[j]) { 926 if (!port->bulk_in_buffers[j]) {
935 dev_err(ddev, "Couldn't allocate bulk_in_buffer\n"); 927 dev_err(ddev, "Couldn't allocate bulk_in_buffer\n");
936 goto probe_error; 928 goto probe_error;
937 } 929 }
938 usb_fill_bulk_urb(port->read_urbs[j], dev, 930 usb_fill_bulk_urb(port->read_urbs[j], dev,
939 usb_rcvbulkpipe(dev, 931 usb_rcvbulkpipe(dev,
940 endpoint->bEndpointAddress), 932 endpoint->bEndpointAddress),
941 port->bulk_in_buffers[j], buffer_size, 933 port->bulk_in_buffers[j], buffer_size,
942 serial->type->read_bulk_callback, 934 serial->type->read_bulk_callback,
943 port); 935 port);
944 } 936 }
945 937
946 port->read_urb = port->read_urbs[0]; 938 port->read_urb = port->read_urbs[0];
947 port->bulk_in_buffer = port->bulk_in_buffers[0]; 939 port->bulk_in_buffer = port->bulk_in_buffers[0];
948 } 940 }
949 941
950 for (i = 0; i < num_bulk_out; ++i) { 942 for (i = 0; i < num_bulk_out; ++i) {
951 endpoint = bulk_out_endpoint[i]; 943 endpoint = bulk_out_endpoint[i];
952 port = serial->port[i]; 944 port = serial->port[i];
953 if (kfifo_alloc(&port->write_fifo, PAGE_SIZE, GFP_KERNEL)) 945 if (kfifo_alloc(&port->write_fifo, PAGE_SIZE, GFP_KERNEL))
954 goto probe_error; 946 goto probe_error;
955 buffer_size = serial->type->bulk_out_size; 947 buffer_size = serial->type->bulk_out_size;
956 if (!buffer_size) 948 if (!buffer_size)
957 buffer_size = usb_endpoint_maxp(endpoint); 949 buffer_size = usb_endpoint_maxp(endpoint);
958 port->bulk_out_size = buffer_size; 950 port->bulk_out_size = buffer_size;
959 port->bulk_out_endpointAddress = endpoint->bEndpointAddress; 951 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
960 952
961 for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) { 953 for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
962 set_bit(j, &port->write_urbs_free); 954 set_bit(j, &port->write_urbs_free);
963 port->write_urbs[j] = usb_alloc_urb(0, GFP_KERNEL); 955 port->write_urbs[j] = usb_alloc_urb(0, GFP_KERNEL);
964 if (!port->write_urbs[j]) { 956 if (!port->write_urbs[j]) {
965 dev_err(ddev, "No free urbs available\n"); 957 dev_err(ddev, "No free urbs available\n");
966 goto probe_error; 958 goto probe_error;
967 } 959 }
968 port->bulk_out_buffers[j] = kmalloc(buffer_size, 960 port->bulk_out_buffers[j] = kmalloc(buffer_size,
969 GFP_KERNEL); 961 GFP_KERNEL);
970 if (!port->bulk_out_buffers[j]) { 962 if (!port->bulk_out_buffers[j]) {
971 dev_err(ddev, "Couldn't allocate bulk_out_buffer\n"); 963 dev_err(ddev, "Couldn't allocate bulk_out_buffer\n");
972 goto probe_error; 964 goto probe_error;
973 } 965 }
974 usb_fill_bulk_urb(port->write_urbs[j], dev, 966 usb_fill_bulk_urb(port->write_urbs[j], dev,
975 usb_sndbulkpipe(dev, 967 usb_sndbulkpipe(dev,
976 endpoint->bEndpointAddress), 968 endpoint->bEndpointAddress),
977 port->bulk_out_buffers[j], buffer_size, 969 port->bulk_out_buffers[j], buffer_size,
978 serial->type->write_bulk_callback, 970 serial->type->write_bulk_callback,
979 port); 971 port);
980 } 972 }
981 973
982 port->write_urb = port->write_urbs[0]; 974 port->write_urb = port->write_urbs[0];
983 port->bulk_out_buffer = port->bulk_out_buffers[0]; 975 port->bulk_out_buffer = port->bulk_out_buffers[0];
984 } 976 }
985 977
986 if (serial->type->read_int_callback) { 978 if (serial->type->read_int_callback) {
987 for (i = 0; i < num_interrupt_in; ++i) { 979 for (i = 0; i < num_interrupt_in; ++i) {
988 endpoint = interrupt_in_endpoint[i]; 980 endpoint = interrupt_in_endpoint[i];
989 port = serial->port[i]; 981 port = serial->port[i];
990 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 982 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
991 if (!port->interrupt_in_urb) { 983 if (!port->interrupt_in_urb) {
992 dev_err(ddev, "No free urbs available\n"); 984 dev_err(ddev, "No free urbs available\n");
993 goto probe_error; 985 goto probe_error;
994 } 986 }
995 buffer_size = usb_endpoint_maxp(endpoint); 987 buffer_size = usb_endpoint_maxp(endpoint);
996 port->interrupt_in_endpointAddress = 988 port->interrupt_in_endpointAddress =
997 endpoint->bEndpointAddress; 989 endpoint->bEndpointAddress;
998 port->interrupt_in_buffer = kmalloc(buffer_size, 990 port->interrupt_in_buffer = kmalloc(buffer_size,
999 GFP_KERNEL); 991 GFP_KERNEL);
1000 if (!port->interrupt_in_buffer) { 992 if (!port->interrupt_in_buffer) {
1001 dev_err(ddev, "Couldn't allocate interrupt_in_buffer\n"); 993 dev_err(ddev, "Couldn't allocate interrupt_in_buffer\n");
1002 goto probe_error; 994 goto probe_error;
1003 } 995 }
1004 usb_fill_int_urb(port->interrupt_in_urb, dev, 996 usb_fill_int_urb(port->interrupt_in_urb, dev,
1005 usb_rcvintpipe(dev, 997 usb_rcvintpipe(dev,
1006 endpoint->bEndpointAddress), 998 endpoint->bEndpointAddress),
1007 port->interrupt_in_buffer, buffer_size, 999 port->interrupt_in_buffer, buffer_size,
1008 serial->type->read_int_callback, port, 1000 serial->type->read_int_callback, port,
1009 endpoint->bInterval); 1001 endpoint->bInterval);
1010 } 1002 }
1011 } else if (num_interrupt_in) { 1003 } else if (num_interrupt_in) {
1012 dev_dbg(ddev, "The device claims to support interrupt in transfers, but read_int_callback is not defined\n"); 1004 dev_dbg(ddev, "The device claims to support interrupt in transfers, but read_int_callback is not defined\n");
1013 } 1005 }
1014 1006
1015 if (serial->type->write_int_callback) { 1007 if (serial->type->write_int_callback) {
1016 for (i = 0; i < num_interrupt_out; ++i) { 1008 for (i = 0; i < num_interrupt_out; ++i) {
1017 endpoint = interrupt_out_endpoint[i]; 1009 endpoint = interrupt_out_endpoint[i];
1018 port = serial->port[i]; 1010 port = serial->port[i];
1019 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 1011 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
1020 if (!port->interrupt_out_urb) { 1012 if (!port->interrupt_out_urb) {
1021 dev_err(ddev, "No free urbs available\n"); 1013 dev_err(ddev, "No free urbs available\n");
1022 goto probe_error; 1014 goto probe_error;
1023 } 1015 }
1024 buffer_size = usb_endpoint_maxp(endpoint); 1016 buffer_size = usb_endpoint_maxp(endpoint);
1025 port->interrupt_out_size = buffer_size; 1017 port->interrupt_out_size = buffer_size;
1026 port->interrupt_out_endpointAddress = 1018 port->interrupt_out_endpointAddress =
1027 endpoint->bEndpointAddress; 1019 endpoint->bEndpointAddress;
1028 port->interrupt_out_buffer = kmalloc(buffer_size, 1020 port->interrupt_out_buffer = kmalloc(buffer_size,
1029 GFP_KERNEL); 1021 GFP_KERNEL);
1030 if (!port->interrupt_out_buffer) { 1022 if (!port->interrupt_out_buffer) {
1031 dev_err(ddev, "Couldn't allocate interrupt_out_buffer\n"); 1023 dev_err(ddev, "Couldn't allocate interrupt_out_buffer\n");
1032 goto probe_error; 1024 goto probe_error;
1033 } 1025 }
1034 usb_fill_int_urb(port->interrupt_out_urb, dev, 1026 usb_fill_int_urb(port->interrupt_out_urb, dev,
1035 usb_sndintpipe(dev, 1027 usb_sndintpipe(dev,
1036 endpoint->bEndpointAddress), 1028 endpoint->bEndpointAddress),
1037 port->interrupt_out_buffer, buffer_size, 1029 port->interrupt_out_buffer, buffer_size,
1038 serial->type->write_int_callback, port, 1030 serial->type->write_int_callback, port,
1039 endpoint->bInterval); 1031 endpoint->bInterval);
1040 } 1032 }
1041 } else if (num_interrupt_out) { 1033 } else if (num_interrupt_out) {
1042 dev_dbg(ddev, "The device claims to support interrupt out transfers, but write_int_callback is not defined\n"); 1034 dev_dbg(ddev, "The device claims to support interrupt out transfers, but write_int_callback is not defined\n");
1043 } 1035 }
1044 1036
1045 usb_set_intfdata(interface, serial); 1037 usb_set_intfdata(interface, serial);
1046 1038
1047 /* if this device type has an attach function, call it */ 1039 /* if this device type has an attach function, call it */
1048 if (type->attach) { 1040 if (type->attach) {
1049 retval = type->attach(serial); 1041 retval = type->attach(serial);
1050 if (retval < 0) 1042 if (retval < 0)
1051 goto probe_error; 1043 goto probe_error;
1052 serial->attached = 1; 1044 serial->attached = 1;
1053 if (retval > 0) { 1045 if (retval > 0) {
1054 /* quietly accept this device, but don't bind to a 1046 /* quietly accept this device, but don't bind to a
1055 serial port as it's about to disappear */ 1047 serial port as it's about to disappear */
1056 serial->num_ports = 0; 1048 serial->num_ports = 0;
1057 goto exit; 1049 goto exit;
1058 } 1050 }
1059 } else { 1051 } else {
1060 serial->attached = 1; 1052 serial->attached = 1;
1061 } 1053 }
1062 1054
1063 /* Avoid race with tty_open and serial_install by setting the 1055 /* Avoid race with tty_open and serial_install by setting the
1064 * disconnected flag and not clearing it until all ports have been 1056 * disconnected flag and not clearing it until all ports have been
1065 * registered. 1057 * registered.
1066 */ 1058 */
1067 serial->disconnected = 1; 1059 serial->disconnected = 1;
1068 1060
1069 if (get_free_serial(serial, num_ports, &minor) == NULL) { 1061 if (get_free_serial(serial, num_ports, &minor) == NULL) {
1070 dev_err(ddev, "No more free serial devices\n"); 1062 dev_err(ddev, "No more free serial devices\n");
1071 goto probe_error; 1063 goto probe_error;
1072 } 1064 }
1073 serial->minor = minor; 1065 serial->minor = minor;
1074 1066
1075 /* register all of the individual ports with the driver core */ 1067 /* register all of the individual ports with the driver core */
1076 for (i = 0; i < num_ports; ++i) { 1068 for (i = 0; i < num_ports; ++i) {
1077 port = serial->port[i]; 1069 port = serial->port[i];
1078 dev_set_name(&port->dev, "ttyUSB%d", port->number); 1070 dev_set_name(&port->dev, "ttyUSB%d", port->number);
1079 dev_dbg(ddev, "registering %s", dev_name(&port->dev)); 1071 dev_dbg(ddev, "registering %s", dev_name(&port->dev));
1080 device_enable_async_suspend(&port->dev); 1072 device_enable_async_suspend(&port->dev);
1081 1073
1082 retval = device_add(&port->dev); 1074 retval = device_add(&port->dev);
1083 if (retval) 1075 if (retval)
1084 dev_err(ddev, "Error registering port device, continuing\n"); 1076 dev_err(ddev, "Error registering port device, continuing\n");
1085 } 1077 }
1086 1078
1087 serial->disconnected = 0; 1079 serial->disconnected = 0;
1088 1080
1089 usb_serial_console_init(minor); 1081 usb_serial_console_init(minor);
1090 exit: 1082 exit:
1091 module_put(type->driver.owner); 1083 module_put(type->driver.owner);
1092 return 0; 1084 return 0;
1093 1085
1094 probe_error: 1086 probe_error:
1095 usb_serial_put(serial); 1087 usb_serial_put(serial);
1096 module_put(type->driver.owner); 1088 module_put(type->driver.owner);
1097 return -EIO; 1089 return -EIO;
1098 } 1090 }
1099 1091
1100 static void usb_serial_disconnect(struct usb_interface *interface) 1092 static void usb_serial_disconnect(struct usb_interface *interface)
1101 { 1093 {
1102 int i; 1094 int i;
1103 struct usb_serial *serial = usb_get_intfdata(interface); 1095 struct usb_serial *serial = usb_get_intfdata(interface);
1104 struct device *dev = &interface->dev; 1096 struct device *dev = &interface->dev;
1105 struct usb_serial_port *port; 1097 struct usb_serial_port *port;
1106 1098
1107 usb_serial_console_disconnect(serial); 1099 usb_serial_console_disconnect(serial);
1108 1100
1109 mutex_lock(&serial->disc_mutex); 1101 mutex_lock(&serial->disc_mutex);
1110 /* must set a flag, to signal subdrivers */ 1102 /* must set a flag, to signal subdrivers */
1111 serial->disconnected = 1; 1103 serial->disconnected = 1;
1112 mutex_unlock(&serial->disc_mutex); 1104 mutex_unlock(&serial->disc_mutex);
1113 1105
1114 for (i = 0; i < serial->num_ports; ++i) { 1106 for (i = 0; i < serial->num_ports; ++i) {
1115 port = serial->port[i]; 1107 port = serial->port[i];
1116 if (port) { 1108 if (port) {
1117 struct tty_struct *tty = tty_port_tty_get(&port->port); 1109 struct tty_struct *tty = tty_port_tty_get(&port->port);
1118 if (tty) { 1110 if (tty) {
1119 tty_vhangup(tty); 1111 tty_vhangup(tty);
1120 tty_kref_put(tty); 1112 tty_kref_put(tty);
1121 } 1113 }
1122 kill_traffic(port); 1114 kill_traffic(port);
1123 cancel_work_sync(&port->work); 1115 cancel_work_sync(&port->work);
1124 if (device_is_registered(&port->dev)) 1116 if (device_is_registered(&port->dev))
1125 device_del(&port->dev); 1117 device_del(&port->dev);
1126 } 1118 }
1127 } 1119 }
1128 serial->type->disconnect(serial); 1120 serial->type->disconnect(serial);
1129 1121
1130 /* let the last holder of this object cause it to be cleaned up */ 1122 /* let the last holder of this object cause it to be cleaned up */
1131 usb_serial_put(serial); 1123 usb_serial_put(serial);
1132 dev_info(dev, "device disconnected\n"); 1124 dev_info(dev, "device disconnected\n");
1133 } 1125 }
1134 1126
1135 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message) 1127 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1136 { 1128 {
1137 struct usb_serial *serial = usb_get_intfdata(intf); 1129 struct usb_serial *serial = usb_get_intfdata(intf);
1138 struct usb_serial_port *port; 1130 struct usb_serial_port *port;
1139 int i, r = 0; 1131 int i, r = 0;
1140 1132
1141 serial->suspending = 1; 1133 serial->suspending = 1;
1142 1134
1143 if (serial->type->suspend) { 1135 if (serial->type->suspend) {
1144 r = serial->type->suspend(serial, message); 1136 r = serial->type->suspend(serial, message);
1145 if (r < 0) { 1137 if (r < 0) {
1146 serial->suspending = 0; 1138 serial->suspending = 0;
1147 goto err_out; 1139 goto err_out;
1148 } 1140 }
1149 } 1141 }
1150 1142
1151 for (i = 0; i < serial->num_ports; ++i) { 1143 for (i = 0; i < serial->num_ports; ++i) {
1152 port = serial->port[i]; 1144 port = serial->port[i];
1153 if (port) 1145 if (port)
1154 kill_traffic(port); 1146 kill_traffic(port);
1155 } 1147 }
1156 1148
1157 err_out: 1149 err_out:
1158 return r; 1150 return r;
1159 } 1151 }
1160 EXPORT_SYMBOL(usb_serial_suspend); 1152 EXPORT_SYMBOL(usb_serial_suspend);
1161 1153
1162 int usb_serial_resume(struct usb_interface *intf) 1154 int usb_serial_resume(struct usb_interface *intf)
1163 { 1155 {
1164 struct usb_serial *serial = usb_get_intfdata(intf); 1156 struct usb_serial *serial = usb_get_intfdata(intf);
1165 int rv; 1157 int rv;
1166 1158
1167 serial->suspending = 0; 1159 serial->suspending = 0;
1168 if (serial->type->resume) 1160 if (serial->type->resume)
1169 rv = serial->type->resume(serial); 1161 rv = serial->type->resume(serial);
1170 else 1162 else
1171 rv = usb_serial_generic_resume(serial); 1163 rv = usb_serial_generic_resume(serial);
1172 1164
1173 return rv; 1165 return rv;
1174 } 1166 }
1175 EXPORT_SYMBOL(usb_serial_resume); 1167 EXPORT_SYMBOL(usb_serial_resume);
1176 1168
1177 static int usb_serial_reset_resume(struct usb_interface *intf) 1169 static int usb_serial_reset_resume(struct usb_interface *intf)
1178 { 1170 {
1179 struct usb_serial *serial = usb_get_intfdata(intf); 1171 struct usb_serial *serial = usb_get_intfdata(intf);
1180 int rv; 1172 int rv;
1181 1173
1182 serial->suspending = 0; 1174 serial->suspending = 0;
1183 if (serial->type->reset_resume) 1175 if (serial->type->reset_resume)
1184 rv = serial->type->reset_resume(serial); 1176 rv = serial->type->reset_resume(serial);
1185 else { 1177 else {
1186 rv = -EOPNOTSUPP; 1178 rv = -EOPNOTSUPP;
1187 intf->needs_binding = 1; 1179 intf->needs_binding = 1;
1188 } 1180 }
1189 1181
1190 return rv; 1182 return rv;
1191 } 1183 }
1192 1184
1193 static const struct tty_operations serial_ops = { 1185 static const struct tty_operations serial_ops = {
1194 .open = serial_open, 1186 .open = serial_open,
1195 .close = serial_close, 1187 .close = serial_close,
1196 .write = serial_write, 1188 .write = serial_write,
1197 .hangup = serial_hangup, 1189 .hangup = serial_hangup,
1198 .write_room = serial_write_room, 1190 .write_room = serial_write_room,
1199 .ioctl = serial_ioctl, 1191 .ioctl = serial_ioctl,
1200 .set_termios = serial_set_termios, 1192 .set_termios = serial_set_termios,
1201 .throttle = serial_throttle, 1193 .throttle = serial_throttle,
1202 .unthrottle = serial_unthrottle, 1194 .unthrottle = serial_unthrottle,
1203 .break_ctl = serial_break, 1195 .break_ctl = serial_break,
1204 .chars_in_buffer = serial_chars_in_buffer, 1196 .chars_in_buffer = serial_chars_in_buffer,
1205 .tiocmget = serial_tiocmget, 1197 .tiocmget = serial_tiocmget,
1206 .tiocmset = serial_tiocmset, 1198 .tiocmset = serial_tiocmset,
1207 .get_icount = serial_get_icount, 1199 .get_icount = serial_get_icount,
1208 .cleanup = serial_cleanup, 1200 .cleanup = serial_cleanup,
1209 .install = serial_install, 1201 .install = serial_install,
1210 .proc_fops = &serial_proc_fops, 1202 .proc_fops = &serial_proc_fops,
1211 }; 1203 };
1212 1204
1213 1205
1214 struct tty_driver *usb_serial_tty_driver; 1206 struct tty_driver *usb_serial_tty_driver;
1215 1207
1216 /* Driver structure we register with the USB core */ 1208 /* Driver structure we register with the USB core */
1217 static struct usb_driver usb_serial_driver = { 1209 static struct usb_driver usb_serial_driver = {
1218 .name = "usbserial", 1210 .name = "usbserial",
1219 .probe = usb_serial_probe, 1211 .probe = usb_serial_probe,
1220 .disconnect = usb_serial_disconnect, 1212 .disconnect = usb_serial_disconnect,
1221 .suspend = usb_serial_suspend, 1213 .suspend = usb_serial_suspend,
1222 .resume = usb_serial_resume, 1214 .resume = usb_serial_resume,
1223 .no_dynamic_id = 1, 1215 .no_dynamic_id = 1,
1224 .supports_autosuspend = 1, 1216 .supports_autosuspend = 1,
1225 }; 1217 };
1226 1218
1227 static int __init usb_serial_init(void) 1219 static int __init usb_serial_init(void)
1228 { 1220 {
1229 int i; 1221 int i;
1230 int result; 1222 int result;
1231 1223
1232 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS); 1224 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1233 if (!usb_serial_tty_driver) 1225 if (!usb_serial_tty_driver)
1234 return -ENOMEM; 1226 return -ENOMEM;
1235 1227
1236 /* Initialize our global data */ 1228 /* Initialize our global data */
1237 for (i = 0; i < SERIAL_TTY_MINORS; ++i) 1229 for (i = 0; i < SERIAL_TTY_MINORS; ++i)
1238 serial_table[i] = NULL; 1230 serial_table[i] = NULL;
1239 1231
1240 result = bus_register(&usb_serial_bus_type); 1232 result = bus_register(&usb_serial_bus_type);
1241 if (result) { 1233 if (result) {
1242 pr_err("%s - registering bus driver failed\n", __func__); 1234 pr_err("%s - registering bus driver failed\n", __func__);
1243 goto exit_bus; 1235 goto exit_bus;
1244 } 1236 }
1245 1237
1246 usb_serial_tty_driver->driver_name = "usbserial"; 1238 usb_serial_tty_driver->driver_name = "usbserial";
1247 usb_serial_tty_driver->name = "ttyUSB"; 1239 usb_serial_tty_driver->name = "ttyUSB";
1248 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR; 1240 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1249 usb_serial_tty_driver->minor_start = 0; 1241 usb_serial_tty_driver->minor_start = 0;
1250 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; 1242 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1251 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL; 1243 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1252 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | 1244 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1253 TTY_DRIVER_DYNAMIC_DEV; 1245 TTY_DRIVER_DYNAMIC_DEV;
1254 usb_serial_tty_driver->init_termios = tty_std_termios; 1246 usb_serial_tty_driver->init_termios = tty_std_termios;
1255 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD 1247 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1256 | HUPCL | CLOCAL; 1248 | HUPCL | CLOCAL;
1257 usb_serial_tty_driver->init_termios.c_ispeed = 9600; 1249 usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1258 usb_serial_tty_driver->init_termios.c_ospeed = 9600; 1250 usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1259 tty_set_operations(usb_serial_tty_driver, &serial_ops); 1251 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1260 result = tty_register_driver(usb_serial_tty_driver); 1252 result = tty_register_driver(usb_serial_tty_driver);
1261 if (result) { 1253 if (result) {
1262 pr_err("%s - tty_register_driver failed\n", __func__); 1254 pr_err("%s - tty_register_driver failed\n", __func__);
1263 goto exit_reg_driver; 1255 goto exit_reg_driver;
1264 } 1256 }
1265 1257
1266 /* register the USB driver */ 1258 /* register the USB driver */
1267 result = usb_register(&usb_serial_driver); 1259 result = usb_register(&usb_serial_driver);
1268 if (result < 0) { 1260 if (result < 0) {
1269 pr_err("%s - usb_register failed\n", __func__); 1261 pr_err("%s - usb_register failed\n", __func__);
1270 goto exit_tty; 1262 goto exit_tty;
1271 } 1263 }
1272 1264
1273 /* register the generic driver, if we should */ 1265 /* register the generic driver, if we should */
1274 result = usb_serial_generic_register(); 1266 result = usb_serial_generic_register();
1275 if (result < 0) { 1267 if (result < 0) {
1276 pr_err("%s - registering generic driver failed\n", __func__); 1268 pr_err("%s - registering generic driver failed\n", __func__);
1277 goto exit_generic; 1269 goto exit_generic;
1278 } 1270 }
1279 1271
1280 return result; 1272 return result;
1281 1273
1282 exit_generic: 1274 exit_generic:
1283 usb_deregister(&usb_serial_driver); 1275 usb_deregister(&usb_serial_driver);
1284 1276
1285 exit_tty: 1277 exit_tty:
1286 tty_unregister_driver(usb_serial_tty_driver); 1278 tty_unregister_driver(usb_serial_tty_driver);
1287 1279
1288 exit_reg_driver: 1280 exit_reg_driver:
1289 bus_unregister(&usb_serial_bus_type); 1281 bus_unregister(&usb_serial_bus_type);
1290 1282
1291 exit_bus: 1283 exit_bus:
1292 pr_err("%s - returning with error %d\n", __func__, result); 1284 pr_err("%s - returning with error %d\n", __func__, result);
1293 put_tty_driver(usb_serial_tty_driver); 1285 put_tty_driver(usb_serial_tty_driver);
1294 return result; 1286 return result;
1295 } 1287 }
1296 1288
1297 1289
1298 static void __exit usb_serial_exit(void) 1290 static void __exit usb_serial_exit(void)
1299 { 1291 {
1300 usb_serial_console_exit(); 1292 usb_serial_console_exit();
1301 1293
1302 usb_serial_generic_deregister(); 1294 usb_serial_generic_deregister();
1303 1295
1304 usb_deregister(&usb_serial_driver); 1296 usb_deregister(&usb_serial_driver);
1305 tty_unregister_driver(usb_serial_tty_driver); 1297 tty_unregister_driver(usb_serial_tty_driver);
1306 put_tty_driver(usb_serial_tty_driver); 1298 put_tty_driver(usb_serial_tty_driver);
1307 bus_unregister(&usb_serial_bus_type); 1299 bus_unregister(&usb_serial_bus_type);
1308 } 1300 }
1309 1301
1310 1302
1311 module_init(usb_serial_init); 1303 module_init(usb_serial_init);
1312 module_exit(usb_serial_exit); 1304 module_exit(usb_serial_exit);
1313 1305
1314 #define set_to_generic_if_null(type, function) \ 1306 #define set_to_generic_if_null(type, function) \
1315 do { \ 1307 do { \
1316 if (!type->function) { \ 1308 if (!type->function) { \
1317 type->function = usb_serial_generic_##function; \ 1309 type->function = usb_serial_generic_##function; \
1318 pr_debug("Had to override the " #function \ 1310 pr_debug("Had to override the " #function \
1319 " usb serial operation with the generic one.");\ 1311 " usb serial operation with the generic one.");\
1320 } \ 1312 } \
1321 } while (0) 1313 } while (0)
1322 1314
1323 static void fixup_generic(struct usb_serial_driver *device) 1315 static void fixup_generic(struct usb_serial_driver *device)
1324 { 1316 {
1325 set_to_generic_if_null(device, open); 1317 set_to_generic_if_null(device, open);
1326 set_to_generic_if_null(device, write); 1318 set_to_generic_if_null(device, write);
1327 set_to_generic_if_null(device, close); 1319 set_to_generic_if_null(device, close);
1328 set_to_generic_if_null(device, write_room); 1320 set_to_generic_if_null(device, write_room);
1329 set_to_generic_if_null(device, chars_in_buffer); 1321 set_to_generic_if_null(device, chars_in_buffer);
1330 set_to_generic_if_null(device, read_bulk_callback); 1322 set_to_generic_if_null(device, read_bulk_callback);
1331 set_to_generic_if_null(device, write_bulk_callback); 1323 set_to_generic_if_null(device, write_bulk_callback);
1332 set_to_generic_if_null(device, disconnect); 1324 set_to_generic_if_null(device, disconnect);
1333 set_to_generic_if_null(device, release); 1325 set_to_generic_if_null(device, release);
1334 set_to_generic_if_null(device, process_read_urb); 1326 set_to_generic_if_null(device, process_read_urb);
1335 set_to_generic_if_null(device, prepare_write_buffer); 1327 set_to_generic_if_null(device, prepare_write_buffer);
1336 } 1328 }
1337 1329
1338 static int usb_serial_register(struct usb_serial_driver *driver) 1330 static int usb_serial_register(struct usb_serial_driver *driver)
1339 { 1331 {
1340 int retval; 1332 int retval;
1341 1333
1342 if (usb_disabled()) 1334 if (usb_disabled())
1343 return -ENODEV; 1335 return -ENODEV;
1344 1336
1345 fixup_generic(driver); 1337 fixup_generic(driver);
1346 1338
1347 if (!driver->description) 1339 if (!driver->description)
1348 driver->description = driver->driver.name; 1340 driver->description = driver->driver.name;
1349 if (!driver->usb_driver) { 1341 if (!driver->usb_driver) {
1350 WARN(1, "Serial driver %s has no usb_driver\n", 1342 WARN(1, "Serial driver %s has no usb_driver\n",
1351 driver->description); 1343 driver->description);
1352 return -EINVAL; 1344 return -EINVAL;
1353 } 1345 }
1354 1346
1355 /* Add this device to our list of devices */ 1347 /* Add this device to our list of devices */
1356 mutex_lock(&table_lock); 1348 mutex_lock(&table_lock);
1357 list_add(&driver->driver_list, &usb_serial_driver_list); 1349 list_add(&driver->driver_list, &usb_serial_driver_list);
1358 1350
1359 retval = usb_serial_bus_register(driver); 1351 retval = usb_serial_bus_register(driver);
1360 if (retval) { 1352 if (retval) {
1361 pr_err("problem %d when registering driver %s\n", retval, driver->description); 1353 pr_err("problem %d when registering driver %s\n", retval, driver->description);
1362 list_del(&driver->driver_list); 1354 list_del(&driver->driver_list);
1363 } else 1355 } else
1364 pr_info("USB Serial support registered for %s\n", driver->description); 1356 pr_info("USB Serial support registered for %s\n", driver->description);
1365 1357
1366 mutex_unlock(&table_lock); 1358 mutex_unlock(&table_lock);
1367 return retval; 1359 return retval;
1368 } 1360 }
1369 1361
1370 static void usb_serial_deregister(struct usb_serial_driver *device) 1362 static void usb_serial_deregister(struct usb_serial_driver *device)
1371 { 1363 {
1372 pr_info("USB Serial deregistering driver %s\n", device->description); 1364 pr_info("USB Serial deregistering driver %s\n", device->description);
1373 mutex_lock(&table_lock); 1365 mutex_lock(&table_lock);
1374 list_del(&device->driver_list); 1366 list_del(&device->driver_list);
1375 usb_serial_bus_deregister(device); 1367 usb_serial_bus_deregister(device);
1376 mutex_unlock(&table_lock); 1368 mutex_unlock(&table_lock);
1377 } 1369 }
1378 1370
1379 /** 1371 /**
1380 * usb_serial_register_drivers - register drivers for a usb-serial module 1372 * usb_serial_register_drivers - register drivers for a usb-serial module
1381 * @serial_drivers: NULL-terminated array of pointers to drivers to be registered 1373 * @serial_drivers: NULL-terminated array of pointers to drivers to be registered
1382 * @name: name of the usb_driver for this set of @serial_drivers 1374 * @name: name of the usb_driver for this set of @serial_drivers
1383 * @id_table: list of all devices this @serial_drivers set binds to 1375 * @id_table: list of all devices this @serial_drivers set binds to
1384 * 1376 *
1385 * Registers all the drivers in the @serial_drivers array, and dynamically 1377 * Registers all the drivers in the @serial_drivers array, and dynamically
1386 * creates a struct usb_driver with the name @name and id_table of @id_table. 1378 * creates a struct usb_driver with the name @name and id_table of @id_table.
1387 */ 1379 */
1388 int usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[], 1380 int usb_serial_register_drivers(struct usb_serial_driver *const serial_drivers[],
1389 const char *name, 1381 const char *name,
1390 const struct usb_device_id *id_table) 1382 const struct usb_device_id *id_table)
1391 { 1383 {
1392 int rc; 1384 int rc;
1393 struct usb_driver *udriver; 1385 struct usb_driver *udriver;
1394 struct usb_serial_driver * const *sd; 1386 struct usb_serial_driver * const *sd;
1395 1387
1396 /* 1388 /*
1397 * udriver must be registered before any of the serial drivers, 1389 * udriver must be registered before any of the serial drivers,
1398 * because the store_new_id() routine for the serial drivers (in 1390 * because the store_new_id() routine for the serial drivers (in
1399 * bus.c) probes udriver. 1391 * bus.c) probes udriver.
1400 * 1392 *
1401 * Performance hack: We don't want udriver to be probed until 1393 * Performance hack: We don't want udriver to be probed until
1402 * the serial drivers are registered, because the probe would 1394 * the serial drivers are registered, because the probe would
1403 * simply fail for lack of a matching serial driver. 1395 * simply fail for lack of a matching serial driver.
1404 * So we leave udriver's id_table set to NULL until we are all set. 1396 * So we leave udriver's id_table set to NULL until we are all set.
1405 * 1397 *
1406 * Suspend/resume support is implemented in the usb-serial core, 1398 * Suspend/resume support is implemented in the usb-serial core,
1407 * so fill in the PM-related fields in udriver. 1399 * so fill in the PM-related fields in udriver.
1408 */ 1400 */
1409 udriver = kzalloc(sizeof(*udriver), GFP_KERNEL); 1401 udriver = kzalloc(sizeof(*udriver), GFP_KERNEL);
1410 if (!udriver) 1402 if (!udriver)
1411 return -ENOMEM; 1403 return -ENOMEM;
1412 1404
1413 udriver->name = name; 1405 udriver->name = name;
1414 udriver->no_dynamic_id = 1; 1406 udriver->no_dynamic_id = 1;
1415 udriver->supports_autosuspend = 1; 1407 udriver->supports_autosuspend = 1;
1416 udriver->suspend = usb_serial_suspend; 1408 udriver->suspend = usb_serial_suspend;
1417 udriver->resume = usb_serial_resume; 1409 udriver->resume = usb_serial_resume;
1418 udriver->probe = usb_serial_probe; 1410 udriver->probe = usb_serial_probe;
1419 udriver->disconnect = usb_serial_disconnect; 1411 udriver->disconnect = usb_serial_disconnect;
1420 1412
1421 /* we only set the reset_resume field if the serial_driver has one */ 1413 /* we only set the reset_resume field if the serial_driver has one */
1422 for (sd = serial_drivers; *sd; ++sd) { 1414 for (sd = serial_drivers; *sd; ++sd) {
1423 if ((*sd)->reset_resume) { 1415 if ((*sd)->reset_resume) {
1424 udriver->reset_resume = usb_serial_reset_resume; 1416 udriver->reset_resume = usb_serial_reset_resume;
1425 break; 1417 break;
1426 } 1418 }
1427 } 1419 }
1428 1420
1429 rc = usb_register(udriver); 1421 rc = usb_register(udriver);
1430 if (rc) 1422 if (rc)
1431 return rc; 1423 return rc;
1432 1424
1433 for (sd = serial_drivers; *sd; ++sd) { 1425 for (sd = serial_drivers; *sd; ++sd) {
1434 (*sd)->usb_driver = udriver; 1426 (*sd)->usb_driver = udriver;
1435 rc = usb_serial_register(*sd); 1427 rc = usb_serial_register(*sd);
1436 if (rc) 1428 if (rc)
1437 goto failed; 1429 goto failed;
1438 } 1430 }
1439 1431
1440 /* Now set udriver's id_table and look for matches */ 1432 /* Now set udriver's id_table and look for matches */
1441 udriver->id_table = id_table; 1433 udriver->id_table = id_table;
1442 rc = driver_attach(&udriver->drvwrap.driver); 1434 rc = driver_attach(&udriver->drvwrap.driver);
1443 return 0; 1435 return 0;
1444 1436
1445 failed: 1437 failed:
1446 while (sd-- > serial_drivers) 1438 while (sd-- > serial_drivers)
1447 usb_serial_deregister(*sd); 1439 usb_serial_deregister(*sd);
1448 usb_deregister(udriver); 1440 usb_deregister(udriver);
1449 return rc; 1441 return rc;
1450 } 1442 }
1451 EXPORT_SYMBOL_GPL(usb_serial_register_drivers); 1443 EXPORT_SYMBOL_GPL(usb_serial_register_drivers);
1452 1444
1453 /** 1445 /**
1454 * usb_serial_deregister_drivers - deregister drivers for a usb-serial module 1446 * usb_serial_deregister_drivers - deregister drivers for a usb-serial module
1455 * @serial_drivers: NULL-terminated array of pointers to drivers to be deregistered 1447 * @serial_drivers: NULL-terminated array of pointers to drivers to be deregistered
1456 * 1448 *
1457 * Deregisters all the drivers in the @serial_drivers array and deregisters and 1449 * Deregisters all the drivers in the @serial_drivers array and deregisters and
1458 * frees the struct usb_driver that was created by the call to 1450 * frees the struct usb_driver that was created by the call to
1459 * usb_serial_register_drivers(). 1451 * usb_serial_register_drivers().
1460 */ 1452 */
1461 void usb_serial_deregister_drivers(struct usb_serial_driver *const serial_drivers[]) 1453 void usb_serial_deregister_drivers(struct usb_serial_driver *const serial_drivers[])
1462 { 1454 {
1463 struct usb_driver *udriver = (*serial_drivers)->usb_driver; 1455 struct usb_driver *udriver = (*serial_drivers)->usb_driver;
1464 1456
1465 for (; *serial_drivers; ++serial_drivers) 1457 for (; *serial_drivers; ++serial_drivers)
1466 usb_serial_deregister(*serial_drivers); 1458 usb_serial_deregister(*serial_drivers);
1467 usb_deregister(udriver); 1459 usb_deregister(udriver);
1468 kfree(udriver); 1460 kfree(udriver);
1469 } 1461 }
1470 EXPORT_SYMBOL_GPL(usb_serial_deregister_drivers); 1462 EXPORT_SYMBOL_GPL(usb_serial_deregister_drivers);
1471 1463
1472 /* Module information */ 1464 /* Module information */
1473 MODULE_AUTHOR(DRIVER_AUTHOR); 1465 MODULE_AUTHOR(DRIVER_AUTHOR);
1474 MODULE_DESCRIPTION(DRIVER_DESC); 1466 MODULE_DESCRIPTION(DRIVER_DESC);
1475 MODULE_LICENSE("GPL"); 1467 MODULE_LICENSE("GPL");
1476 1468
1 #ifndef _LINUX_TTY_H 1 #ifndef _LINUX_TTY_H
2 #define _LINUX_TTY_H 2 #define _LINUX_TTY_H
3 3
4 #include <linux/fs.h> 4 #include <linux/fs.h>
5 #include <linux/major.h> 5 #include <linux/major.h>
6 #include <linux/termios.h> 6 #include <linux/termios.h>
7 #include <linux/workqueue.h> 7 #include <linux/workqueue.h>
8 #include <linux/tty_driver.h> 8 #include <linux/tty_driver.h>
9 #include <linux/tty_ldisc.h> 9 #include <linux/tty_ldisc.h>
10 #include <linux/mutex.h> 10 #include <linux/mutex.h>
11 #include <linux/tty_flags.h> 11 #include <linux/tty_flags.h>
12 #include <uapi/linux/tty.h> 12 #include <uapi/linux/tty.h>
13 13
14 14
15 15
16 /* 16 /*
17 * (Note: the *_driver.minor_start values 1, 64, 128, 192 are 17 * (Note: the *_driver.minor_start values 1, 64, 128, 192 are
18 * hardcoded at present.) 18 * hardcoded at present.)
19 */ 19 */
20 #define NR_UNIX98_PTY_DEFAULT 4096 /* Default maximum for Unix98 ptys */ 20 #define NR_UNIX98_PTY_DEFAULT 4096 /* Default maximum for Unix98 ptys */
21 #define NR_UNIX98_PTY_RESERVE 1024 /* Default reserve for main devpts */ 21 #define NR_UNIX98_PTY_RESERVE 1024 /* Default reserve for main devpts */
22 #define NR_UNIX98_PTY_MAX (1 << MINORBITS) /* Absolute limit */ 22 #define NR_UNIX98_PTY_MAX (1 << MINORBITS) /* Absolute limit */
23 23
24 /* 24 /*
25 * This character is the same as _POSIX_VDISABLE: it cannot be used as 25 * This character is the same as _POSIX_VDISABLE: it cannot be used as
26 * a c_cc[] character, but indicates that a particular special character 26 * a c_cc[] character, but indicates that a particular special character
27 * isn't in use (eg VINTR has no character etc) 27 * isn't in use (eg VINTR has no character etc)
28 */ 28 */
29 #define __DISABLED_CHAR '\0' 29 #define __DISABLED_CHAR '\0'
30 30
31 struct tty_buffer { 31 struct tty_buffer {
32 struct tty_buffer *next; 32 struct tty_buffer *next;
33 char *char_buf_ptr; 33 char *char_buf_ptr;
34 unsigned char *flag_buf_ptr; 34 unsigned char *flag_buf_ptr;
35 int used; 35 int used;
36 int size; 36 int size;
37 int commit; 37 int commit;
38 int read; 38 int read;
39 /* Data points here */ 39 /* Data points here */
40 unsigned long data[0]; 40 unsigned long data[0];
41 }; 41 };
42 42
43 /* 43 /*
44 * We default to dicing tty buffer allocations to this many characters 44 * We default to dicing tty buffer allocations to this many characters
45 * in order to avoid multiple page allocations. We know the size of 45 * in order to avoid multiple page allocations. We know the size of
46 * tty_buffer itself but it must also be taken into account that the 46 * tty_buffer itself but it must also be taken into account that the
47 * the buffer is 256 byte aligned. See tty_buffer_find for the allocation 47 * the buffer is 256 byte aligned. See tty_buffer_find for the allocation
48 * logic this must match 48 * logic this must match
49 */ 49 */
50 50
51 #define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF) 51 #define TTY_BUFFER_PAGE (((PAGE_SIZE - sizeof(struct tty_buffer)) / 2) & ~0xFF)
52 52
53 53
54 struct tty_bufhead { 54 struct tty_bufhead {
55 struct work_struct work; 55 struct work_struct work;
56 spinlock_t lock; 56 spinlock_t lock;
57 struct tty_buffer *head; /* Queue head */ 57 struct tty_buffer *head; /* Queue head */
58 struct tty_buffer *tail; /* Active buffer */ 58 struct tty_buffer *tail; /* Active buffer */
59 struct tty_buffer *free; /* Free queue head */ 59 struct tty_buffer *free; /* Free queue head */
60 int memory_used; /* Buffer space used excluding 60 int memory_used; /* Buffer space used excluding
61 free queue */ 61 free queue */
62 }; 62 };
63 /* 63 /*
64 * When a break, frame error, or parity error happens, these codes are 64 * When a break, frame error, or parity error happens, these codes are
65 * stuffed into the flags buffer. 65 * stuffed into the flags buffer.
66 */ 66 */
67 #define TTY_NORMAL 0 67 #define TTY_NORMAL 0
68 #define TTY_BREAK 1 68 #define TTY_BREAK 1
69 #define TTY_FRAME 2 69 #define TTY_FRAME 2
70 #define TTY_PARITY 3 70 #define TTY_PARITY 3
71 #define TTY_OVERRUN 4 71 #define TTY_OVERRUN 4
72 72
73 #define INTR_CHAR(tty) ((tty)->termios.c_cc[VINTR]) 73 #define INTR_CHAR(tty) ((tty)->termios.c_cc[VINTR])
74 #define QUIT_CHAR(tty) ((tty)->termios.c_cc[VQUIT]) 74 #define QUIT_CHAR(tty) ((tty)->termios.c_cc[VQUIT])
75 #define ERASE_CHAR(tty) ((tty)->termios.c_cc[VERASE]) 75 #define ERASE_CHAR(tty) ((tty)->termios.c_cc[VERASE])
76 #define KILL_CHAR(tty) ((tty)->termios.c_cc[VKILL]) 76 #define KILL_CHAR(tty) ((tty)->termios.c_cc[VKILL])
77 #define EOF_CHAR(tty) ((tty)->termios.c_cc[VEOF]) 77 #define EOF_CHAR(tty) ((tty)->termios.c_cc[VEOF])
78 #define TIME_CHAR(tty) ((tty)->termios.c_cc[VTIME]) 78 #define TIME_CHAR(tty) ((tty)->termios.c_cc[VTIME])
79 #define MIN_CHAR(tty) ((tty)->termios.c_cc[VMIN]) 79 #define MIN_CHAR(tty) ((tty)->termios.c_cc[VMIN])
80 #define SWTC_CHAR(tty) ((tty)->termios.c_cc[VSWTC]) 80 #define SWTC_CHAR(tty) ((tty)->termios.c_cc[VSWTC])
81 #define START_CHAR(tty) ((tty)->termios.c_cc[VSTART]) 81 #define START_CHAR(tty) ((tty)->termios.c_cc[VSTART])
82 #define STOP_CHAR(tty) ((tty)->termios.c_cc[VSTOP]) 82 #define STOP_CHAR(tty) ((tty)->termios.c_cc[VSTOP])
83 #define SUSP_CHAR(tty) ((tty)->termios.c_cc[VSUSP]) 83 #define SUSP_CHAR(tty) ((tty)->termios.c_cc[VSUSP])
84 #define EOL_CHAR(tty) ((tty)->termios.c_cc[VEOL]) 84 #define EOL_CHAR(tty) ((tty)->termios.c_cc[VEOL])
85 #define REPRINT_CHAR(tty) ((tty)->termios.c_cc[VREPRINT]) 85 #define REPRINT_CHAR(tty) ((tty)->termios.c_cc[VREPRINT])
86 #define DISCARD_CHAR(tty) ((tty)->termios.c_cc[VDISCARD]) 86 #define DISCARD_CHAR(tty) ((tty)->termios.c_cc[VDISCARD])
87 #define WERASE_CHAR(tty) ((tty)->termios.c_cc[VWERASE]) 87 #define WERASE_CHAR(tty) ((tty)->termios.c_cc[VWERASE])
88 #define LNEXT_CHAR(tty) ((tty)->termios.c_cc[VLNEXT]) 88 #define LNEXT_CHAR(tty) ((tty)->termios.c_cc[VLNEXT])
89 #define EOL2_CHAR(tty) ((tty)->termios.c_cc[VEOL2]) 89 #define EOL2_CHAR(tty) ((tty)->termios.c_cc[VEOL2])
90 90
91 #define _I_FLAG(tty, f) ((tty)->termios.c_iflag & (f)) 91 #define _I_FLAG(tty, f) ((tty)->termios.c_iflag & (f))
92 #define _O_FLAG(tty, f) ((tty)->termios.c_oflag & (f)) 92 #define _O_FLAG(tty, f) ((tty)->termios.c_oflag & (f))
93 #define _C_FLAG(tty, f) ((tty)->termios.c_cflag & (f)) 93 #define _C_FLAG(tty, f) ((tty)->termios.c_cflag & (f))
94 #define _L_FLAG(tty, f) ((tty)->termios.c_lflag & (f)) 94 #define _L_FLAG(tty, f) ((tty)->termios.c_lflag & (f))
95 95
96 #define I_IGNBRK(tty) _I_FLAG((tty), IGNBRK) 96 #define I_IGNBRK(tty) _I_FLAG((tty), IGNBRK)
97 #define I_BRKINT(tty) _I_FLAG((tty), BRKINT) 97 #define I_BRKINT(tty) _I_FLAG((tty), BRKINT)
98 #define I_IGNPAR(tty) _I_FLAG((tty), IGNPAR) 98 #define I_IGNPAR(tty) _I_FLAG((tty), IGNPAR)
99 #define I_PARMRK(tty) _I_FLAG((tty), PARMRK) 99 #define I_PARMRK(tty) _I_FLAG((tty), PARMRK)
100 #define I_INPCK(tty) _I_FLAG((tty), INPCK) 100 #define I_INPCK(tty) _I_FLAG((tty), INPCK)
101 #define I_ISTRIP(tty) _I_FLAG((tty), ISTRIP) 101 #define I_ISTRIP(tty) _I_FLAG((tty), ISTRIP)
102 #define I_INLCR(tty) _I_FLAG((tty), INLCR) 102 #define I_INLCR(tty) _I_FLAG((tty), INLCR)
103 #define I_IGNCR(tty) _I_FLAG((tty), IGNCR) 103 #define I_IGNCR(tty) _I_FLAG((tty), IGNCR)
104 #define I_ICRNL(tty) _I_FLAG((tty), ICRNL) 104 #define I_ICRNL(tty) _I_FLAG((tty), ICRNL)
105 #define I_IUCLC(tty) _I_FLAG((tty), IUCLC) 105 #define I_IUCLC(tty) _I_FLAG((tty), IUCLC)
106 #define I_IXON(tty) _I_FLAG((tty), IXON) 106 #define I_IXON(tty) _I_FLAG((tty), IXON)
107 #define I_IXANY(tty) _I_FLAG((tty), IXANY) 107 #define I_IXANY(tty) _I_FLAG((tty), IXANY)
108 #define I_IXOFF(tty) _I_FLAG((tty), IXOFF) 108 #define I_IXOFF(tty) _I_FLAG((tty), IXOFF)
109 #define I_IMAXBEL(tty) _I_FLAG((tty), IMAXBEL) 109 #define I_IMAXBEL(tty) _I_FLAG((tty), IMAXBEL)
110 #define I_IUTF8(tty) _I_FLAG((tty), IUTF8) 110 #define I_IUTF8(tty) _I_FLAG((tty), IUTF8)
111 111
112 #define O_OPOST(tty) _O_FLAG((tty), OPOST) 112 #define O_OPOST(tty) _O_FLAG((tty), OPOST)
113 #define O_OLCUC(tty) _O_FLAG((tty), OLCUC) 113 #define O_OLCUC(tty) _O_FLAG((tty), OLCUC)
114 #define O_ONLCR(tty) _O_FLAG((tty), ONLCR) 114 #define O_ONLCR(tty) _O_FLAG((tty), ONLCR)
115 #define O_OCRNL(tty) _O_FLAG((tty), OCRNL) 115 #define O_OCRNL(tty) _O_FLAG((tty), OCRNL)
116 #define O_ONOCR(tty) _O_FLAG((tty), ONOCR) 116 #define O_ONOCR(tty) _O_FLAG((tty), ONOCR)
117 #define O_ONLRET(tty) _O_FLAG((tty), ONLRET) 117 #define O_ONLRET(tty) _O_FLAG((tty), ONLRET)
118 #define O_OFILL(tty) _O_FLAG((tty), OFILL) 118 #define O_OFILL(tty) _O_FLAG((tty), OFILL)
119 #define O_OFDEL(tty) _O_FLAG((tty), OFDEL) 119 #define O_OFDEL(tty) _O_FLAG((tty), OFDEL)
120 #define O_NLDLY(tty) _O_FLAG((tty), NLDLY) 120 #define O_NLDLY(tty) _O_FLAG((tty), NLDLY)
121 #define O_CRDLY(tty) _O_FLAG((tty), CRDLY) 121 #define O_CRDLY(tty) _O_FLAG((tty), CRDLY)
122 #define O_TABDLY(tty) _O_FLAG((tty), TABDLY) 122 #define O_TABDLY(tty) _O_FLAG((tty), TABDLY)
123 #define O_BSDLY(tty) _O_FLAG((tty), BSDLY) 123 #define O_BSDLY(tty) _O_FLAG((tty), BSDLY)
124 #define O_VTDLY(tty) _O_FLAG((tty), VTDLY) 124 #define O_VTDLY(tty) _O_FLAG((tty), VTDLY)
125 #define O_FFDLY(tty) _O_FLAG((tty), FFDLY) 125 #define O_FFDLY(tty) _O_FLAG((tty), FFDLY)
126 126
127 #define C_BAUD(tty) _C_FLAG((tty), CBAUD) 127 #define C_BAUD(tty) _C_FLAG((tty), CBAUD)
128 #define C_CSIZE(tty) _C_FLAG((tty), CSIZE) 128 #define C_CSIZE(tty) _C_FLAG((tty), CSIZE)
129 #define C_CSTOPB(tty) _C_FLAG((tty), CSTOPB) 129 #define C_CSTOPB(tty) _C_FLAG((tty), CSTOPB)
130 #define C_CREAD(tty) _C_FLAG((tty), CREAD) 130 #define C_CREAD(tty) _C_FLAG((tty), CREAD)
131 #define C_PARENB(tty) _C_FLAG((tty), PARENB) 131 #define C_PARENB(tty) _C_FLAG((tty), PARENB)
132 #define C_PARODD(tty) _C_FLAG((tty), PARODD) 132 #define C_PARODD(tty) _C_FLAG((tty), PARODD)
133 #define C_HUPCL(tty) _C_FLAG((tty), HUPCL) 133 #define C_HUPCL(tty) _C_FLAG((tty), HUPCL)
134 #define C_CLOCAL(tty) _C_FLAG((tty), CLOCAL) 134 #define C_CLOCAL(tty) _C_FLAG((tty), CLOCAL)
135 #define C_CIBAUD(tty) _C_FLAG((tty), CIBAUD) 135 #define C_CIBAUD(tty) _C_FLAG((tty), CIBAUD)
136 #define C_CRTSCTS(tty) _C_FLAG((tty), CRTSCTS) 136 #define C_CRTSCTS(tty) _C_FLAG((tty), CRTSCTS)
137 137
138 #define L_ISIG(tty) _L_FLAG((tty), ISIG) 138 #define L_ISIG(tty) _L_FLAG((tty), ISIG)
139 #define L_ICANON(tty) _L_FLAG((tty), ICANON) 139 #define L_ICANON(tty) _L_FLAG((tty), ICANON)
140 #define L_XCASE(tty) _L_FLAG((tty), XCASE) 140 #define L_XCASE(tty) _L_FLAG((tty), XCASE)
141 #define L_ECHO(tty) _L_FLAG((tty), ECHO) 141 #define L_ECHO(tty) _L_FLAG((tty), ECHO)
142 #define L_ECHOE(tty) _L_FLAG((tty), ECHOE) 142 #define L_ECHOE(tty) _L_FLAG((tty), ECHOE)
143 #define L_ECHOK(tty) _L_FLAG((tty), ECHOK) 143 #define L_ECHOK(tty) _L_FLAG((tty), ECHOK)
144 #define L_ECHONL(tty) _L_FLAG((tty), ECHONL) 144 #define L_ECHONL(tty) _L_FLAG((tty), ECHONL)
145 #define L_NOFLSH(tty) _L_FLAG((tty), NOFLSH) 145 #define L_NOFLSH(tty) _L_FLAG((tty), NOFLSH)
146 #define L_TOSTOP(tty) _L_FLAG((tty), TOSTOP) 146 #define L_TOSTOP(tty) _L_FLAG((tty), TOSTOP)
147 #define L_ECHOCTL(tty) _L_FLAG((tty), ECHOCTL) 147 #define L_ECHOCTL(tty) _L_FLAG((tty), ECHOCTL)
148 #define L_ECHOPRT(tty) _L_FLAG((tty), ECHOPRT) 148 #define L_ECHOPRT(tty) _L_FLAG((tty), ECHOPRT)
149 #define L_ECHOKE(tty) _L_FLAG((tty), ECHOKE) 149 #define L_ECHOKE(tty) _L_FLAG((tty), ECHOKE)
150 #define L_FLUSHO(tty) _L_FLAG((tty), FLUSHO) 150 #define L_FLUSHO(tty) _L_FLAG((tty), FLUSHO)
151 #define L_PENDIN(tty) _L_FLAG((tty), PENDIN) 151 #define L_PENDIN(tty) _L_FLAG((tty), PENDIN)
152 #define L_IEXTEN(tty) _L_FLAG((tty), IEXTEN) 152 #define L_IEXTEN(tty) _L_FLAG((tty), IEXTEN)
153 #define L_EXTPROC(tty) _L_FLAG((tty), EXTPROC) 153 #define L_EXTPROC(tty) _L_FLAG((tty), EXTPROC)
154 154
155 struct device; 155 struct device;
156 struct signal_struct; 156 struct signal_struct;
157 157
158 /* 158 /*
159 * Port level information. Each device keeps its own port level information 159 * Port level information. Each device keeps its own port level information
160 * so provide a common structure for those ports wanting to use common support 160 * so provide a common structure for those ports wanting to use common support
161 * routines. 161 * routines.
162 * 162 *
163 * The tty port has a different lifetime to the tty so must be kept apart. 163 * The tty port has a different lifetime to the tty so must be kept apart.
164 * In addition be careful as tty -> port mappings are valid for the life 164 * In addition be careful as tty -> port mappings are valid for the life
165 * of the tty object but in many cases port -> tty mappings are valid only 165 * of the tty object but in many cases port -> tty mappings are valid only
166 * until a hangup so don't use the wrong path. 166 * until a hangup so don't use the wrong path.
167 */ 167 */
168 168
169 struct tty_port; 169 struct tty_port;
170 170
171 struct tty_port_operations { 171 struct tty_port_operations {
172 /* Return 1 if the carrier is raised */ 172 /* Return 1 if the carrier is raised */
173 int (*carrier_raised)(struct tty_port *port); 173 int (*carrier_raised)(struct tty_port *port);
174 /* Control the DTR line */ 174 /* Control the DTR line */
175 void (*dtr_rts)(struct tty_port *port, int raise); 175 void (*dtr_rts)(struct tty_port *port, int raise);
176 /* Called when the last close completes or a hangup finishes 176 /* Called when the last close completes or a hangup finishes
177 IFF the port was initialized. Do not use to free resources. Called 177 IFF the port was initialized. Do not use to free resources. Called
178 under the port mutex to serialize against activate/shutdowns */ 178 under the port mutex to serialize against activate/shutdowns */
179 void (*shutdown)(struct tty_port *port); 179 void (*shutdown)(struct tty_port *port);
180 void (*drop)(struct tty_port *port); 180 void (*drop)(struct tty_port *port);
181 /* Called under the port mutex from tty_port_open, serialized using 181 /* Called under the port mutex from tty_port_open, serialized using
182 the port mutex */ 182 the port mutex */
183 /* FIXME: long term getting the tty argument *out* of this would be 183 /* FIXME: long term getting the tty argument *out* of this would be
184 good for consoles */ 184 good for consoles */
185 int (*activate)(struct tty_port *port, struct tty_struct *tty); 185 int (*activate)(struct tty_port *port, struct tty_struct *tty);
186 /* Called on the final put of a port */ 186 /* Called on the final put of a port */
187 void (*destruct)(struct tty_port *port); 187 void (*destruct)(struct tty_port *port);
188 }; 188 };
189 189
190 struct tty_port { 190 struct tty_port {
191 struct tty_bufhead buf; /* Locked internally */ 191 struct tty_bufhead buf; /* Locked internally */
192 struct tty_struct *tty; /* Back pointer */ 192 struct tty_struct *tty; /* Back pointer */
193 struct tty_struct *itty; /* internal back ptr */ 193 struct tty_struct *itty; /* internal back ptr */
194 const struct tty_port_operations *ops; /* Port operations */ 194 const struct tty_port_operations *ops; /* Port operations */
195 spinlock_t lock; /* Lock protecting tty field */ 195 spinlock_t lock; /* Lock protecting tty field */
196 int blocked_open; /* Waiting to open */ 196 int blocked_open; /* Waiting to open */
197 int count; /* Usage count */ 197 int count; /* Usage count */
198 wait_queue_head_t open_wait; /* Open waiters */ 198 wait_queue_head_t open_wait; /* Open waiters */
199 wait_queue_head_t close_wait; /* Close waiters */ 199 wait_queue_head_t close_wait; /* Close waiters */
200 wait_queue_head_t delta_msr_wait; /* Modem status change */ 200 wait_queue_head_t delta_msr_wait; /* Modem status change */
201 unsigned long flags; /* TTY flags ASY_*/ 201 unsigned long flags; /* TTY flags ASY_*/
202 unsigned long iflags; /* TTYP_ internal flags */ 202 unsigned long iflags; /* TTYP_ internal flags */
203 #define TTYP_FLUSHING 1 /* Flushing to ldisc in progress */ 203 #define TTYP_FLUSHING 1 /* Flushing to ldisc in progress */
204 #define TTYP_FLUSHPENDING 2 /* Queued buffer flush pending */ 204 #define TTYP_FLUSHPENDING 2 /* Queued buffer flush pending */
205 unsigned char console:1, /* port is a console */ 205 unsigned char console:1, /* port is a console */
206 low_latency:1; /* direct buffer flush */ 206 low_latency:1; /* direct buffer flush */
207 struct mutex mutex; /* Locking */ 207 struct mutex mutex; /* Locking */
208 struct mutex buf_mutex; /* Buffer alloc lock */ 208 struct mutex buf_mutex; /* Buffer alloc lock */
209 unsigned char *xmit_buf; /* Optional buffer */ 209 unsigned char *xmit_buf; /* Optional buffer */
210 unsigned int close_delay; /* Close port delay */ 210 unsigned int close_delay; /* Close port delay */
211 unsigned int closing_wait; /* Delay for output */ 211 unsigned int closing_wait; /* Delay for output */
212 int drain_delay; /* Set to zero if no pure time 212 int drain_delay; /* Set to zero if no pure time
213 based drain is needed else 213 based drain is needed else
214 set to size of fifo */ 214 set to size of fifo */
215 struct kref kref; /* Ref counter */ 215 struct kref kref; /* Ref counter */
216 }; 216 };
217 217
218 /* 218 /*
219 * Where all of the state associated with a tty is kept while the tty 219 * Where all of the state associated with a tty is kept while the tty
220 * is open. Since the termios state should be kept even if the tty 220 * is open. Since the termios state should be kept even if the tty
221 * has been closed --- for things like the baud rate, etc --- it is 221 * has been closed --- for things like the baud rate, etc --- it is
222 * not stored here, but rather a pointer to the real state is stored 222 * not stored here, but rather a pointer to the real state is stored
223 * here. Possible the winsize structure should have the same 223 * here. Possible the winsize structure should have the same
224 * treatment, but (1) the default 80x24 is usually right and (2) it's 224 * treatment, but (1) the default 80x24 is usually right and (2) it's
225 * most often used by a windowing system, which will set the correct 225 * most often used by a windowing system, which will set the correct
226 * size each time the window is created or resized anyway. 226 * size each time the window is created or resized anyway.
227 * - TYT, 9/14/92 227 * - TYT, 9/14/92
228 */ 228 */
229 229
230 struct tty_operations; 230 struct tty_operations;
231 231
232 struct tty_struct { 232 struct tty_struct {
233 int magic; 233 int magic;
234 struct kref kref; 234 struct kref kref;
235 struct device *dev; 235 struct device *dev;
236 struct tty_driver *driver; 236 struct tty_driver *driver;
237 const struct tty_operations *ops; 237 const struct tty_operations *ops;
238 int index; 238 int index;
239 239
240 /* Protects ldisc changes: Lock tty not pty */ 240 /* Protects ldisc changes: Lock tty not pty */
241 struct mutex ldisc_mutex; 241 struct mutex ldisc_mutex;
242 struct tty_ldisc *ldisc; 242 struct tty_ldisc *ldisc;
243 243
244 struct mutex atomic_write_lock; 244 struct mutex atomic_write_lock;
245 struct mutex legacy_mutex; 245 struct mutex legacy_mutex;
246 struct mutex termios_mutex; 246 struct mutex termios_mutex;
247 spinlock_t ctrl_lock; 247 spinlock_t ctrl_lock;
248 /* Termios values are protected by the termios mutex */ 248 /* Termios values are protected by the termios mutex */
249 struct ktermios termios, termios_locked; 249 struct ktermios termios, termios_locked;
250 struct termiox *termiox; /* May be NULL for unsupported */ 250 struct termiox *termiox; /* May be NULL for unsupported */
251 char name[64]; 251 char name[64];
252 struct pid *pgrp; /* Protected by ctrl lock */ 252 struct pid *pgrp; /* Protected by ctrl lock */
253 struct pid *session; 253 struct pid *session;
254 unsigned long flags; 254 unsigned long flags;
255 int count; 255 int count;
256 struct winsize winsize; /* termios mutex */ 256 struct winsize winsize; /* termios mutex */
257 unsigned char stopped:1, hw_stopped:1, flow_stopped:1, packet:1; 257 unsigned char stopped:1, hw_stopped:1, flow_stopped:1, packet:1;
258 unsigned char ctrl_status; /* ctrl_lock */ 258 unsigned char ctrl_status; /* ctrl_lock */
259 unsigned int receive_room; /* Bytes free for queue */ 259 unsigned int receive_room; /* Bytes free for queue */
260 int flow_change; 260 int flow_change;
261 261
262 struct tty_struct *link; 262 struct tty_struct *link;
263 struct fasync_struct *fasync; 263 struct fasync_struct *fasync;
264 int alt_speed; /* For magic substitution of 38400 bps */ 264 int alt_speed; /* For magic substitution of 38400 bps */
265 wait_queue_head_t write_wait; 265 wait_queue_head_t write_wait;
266 wait_queue_head_t read_wait; 266 wait_queue_head_t read_wait;
267 struct work_struct hangup_work; 267 struct work_struct hangup_work;
268 void *disc_data; 268 void *disc_data;
269 void *driver_data; 269 void *driver_data;
270 struct list_head tty_files; 270 struct list_head tty_files;
271 271
272 #define N_TTY_BUF_SIZE 4096 272 #define N_TTY_BUF_SIZE 4096
273 273
274 unsigned char closing:1; 274 unsigned char closing:1;
275 unsigned short minimum_to_wake; 275 unsigned short minimum_to_wake;
276 unsigned char *write_buf; 276 unsigned char *write_buf;
277 int write_cnt; 277 int write_cnt;
278 /* If the tty has a pending do_SAK, queue it here - akpm */ 278 /* If the tty has a pending do_SAK, queue it here - akpm */
279 struct work_struct SAK_work; 279 struct work_struct SAK_work;
280 struct tty_port *port; 280 struct tty_port *port;
281 }; 281 };
282 282
283 /* Each of a tty's open files has private_data pointing to tty_file_private */ 283 /* Each of a tty's open files has private_data pointing to tty_file_private */
284 struct tty_file_private { 284 struct tty_file_private {
285 struct tty_struct *tty; 285 struct tty_struct *tty;
286 struct file *file; 286 struct file *file;
287 struct list_head list; 287 struct list_head list;
288 }; 288 };
289 289
290 /* tty magic number */ 290 /* tty magic number */
291 #define TTY_MAGIC 0x5401 291 #define TTY_MAGIC 0x5401
292 292
293 /* 293 /*
294 * These bits are used in the flags field of the tty structure. 294 * These bits are used in the flags field of the tty structure.
295 * 295 *
296 * So that interrupts won't be able to mess up the queues, 296 * So that interrupts won't be able to mess up the queues,
297 * copy_to_cooked must be atomic with respect to itself, as must 297 * copy_to_cooked must be atomic with respect to itself, as must
298 * tty->write. Thus, you must use the inline functions set_bit() and 298 * tty->write. Thus, you must use the inline functions set_bit() and
299 * clear_bit() to make things atomic. 299 * clear_bit() to make things atomic.
300 */ 300 */
301 #define TTY_THROTTLED 0 /* Call unthrottle() at threshold min */ 301 #define TTY_THROTTLED 0 /* Call unthrottle() at threshold min */
302 #define TTY_IO_ERROR 1 /* Cause an I/O error (may be no ldisc too) */ 302 #define TTY_IO_ERROR 1 /* Cause an I/O error (may be no ldisc too) */
303 #define TTY_OTHER_CLOSED 2 /* Other side (if any) has closed */ 303 #define TTY_OTHER_CLOSED 2 /* Other side (if any) has closed */
304 #define TTY_EXCLUSIVE 3 /* Exclusive open mode */ 304 #define TTY_EXCLUSIVE 3 /* Exclusive open mode */
305 #define TTY_DEBUG 4 /* Debugging */ 305 #define TTY_DEBUG 4 /* Debugging */
306 #define TTY_DO_WRITE_WAKEUP 5 /* Call write_wakeup after queuing new */ 306 #define TTY_DO_WRITE_WAKEUP 5 /* Call write_wakeup after queuing new */
307 #define TTY_PUSH 6 /* n_tty private */ 307 #define TTY_PUSH 6 /* n_tty private */
308 #define TTY_CLOSING 7 /* ->close() in progress */ 308 #define TTY_CLOSING 7 /* ->close() in progress */
309 #define TTY_LDISC 9 /* Line discipline attached */ 309 #define TTY_LDISC 9 /* Line discipline attached */
310 #define TTY_LDISC_CHANGING 10 /* Line discipline changing */ 310 #define TTY_LDISC_CHANGING 10 /* Line discipline changing */
311 #define TTY_LDISC_OPEN 11 /* Line discipline is open */ 311 #define TTY_LDISC_OPEN 11 /* Line discipline is open */
312 #define TTY_HW_COOK_OUT 14 /* Hardware can do output cooking */ 312 #define TTY_HW_COOK_OUT 14 /* Hardware can do output cooking */
313 #define TTY_HW_COOK_IN 15 /* Hardware can do input cooking */ 313 #define TTY_HW_COOK_IN 15 /* Hardware can do input cooking */
314 #define TTY_PTY_LOCK 16 /* pty private */ 314 #define TTY_PTY_LOCK 16 /* pty private */
315 #define TTY_NO_WRITE_SPLIT 17 /* Preserve write boundaries to driver */ 315 #define TTY_NO_WRITE_SPLIT 17 /* Preserve write boundaries to driver */
316 #define TTY_HUPPED 18 /* Post driver->hangup() */ 316 #define TTY_HUPPED 18 /* Post driver->hangup() */
317 #define TTY_HUPPING 21 /* ->hangup() in progress */ 317 #define TTY_HUPPING 21 /* ->hangup() in progress */
318 318
319 #define TTY_WRITE_FLUSH(tty) tty_write_flush((tty)) 319 #define TTY_WRITE_FLUSH(tty) tty_write_flush((tty))
320 320
321 /* Values for tty->flow_change */ 321 /* Values for tty->flow_change */
322 #define TTY_THROTTLE_SAFE 1 322 #define TTY_THROTTLE_SAFE 1
323 #define TTY_UNTHROTTLE_SAFE 2 323 #define TTY_UNTHROTTLE_SAFE 2
324 324
325 static inline void __tty_set_flow_change(struct tty_struct *tty, int val) 325 static inline void __tty_set_flow_change(struct tty_struct *tty, int val)
326 { 326 {
327 tty->flow_change = val; 327 tty->flow_change = val;
328 } 328 }
329 329
330 static inline void tty_set_flow_change(struct tty_struct *tty, int val) 330 static inline void tty_set_flow_change(struct tty_struct *tty, int val)
331 { 331 {
332 tty->flow_change = val; 332 tty->flow_change = val;
333 smp_mb(); 333 smp_mb();
334 } 334 }
335 335
336 #ifdef CONFIG_TTY 336 #ifdef CONFIG_TTY
337 extern void console_init(void); 337 extern void console_init(void);
338 extern void tty_kref_put(struct tty_struct *tty); 338 extern void tty_kref_put(struct tty_struct *tty);
339 extern struct pid *tty_get_pgrp(struct tty_struct *tty); 339 extern struct pid *tty_get_pgrp(struct tty_struct *tty);
340 extern void tty_vhangup_self(void); 340 extern void tty_vhangup_self(void);
341 extern void disassociate_ctty(int priv); 341 extern void disassociate_ctty(int priv);
342 extern dev_t tty_devnum(struct tty_struct *tty); 342 extern dev_t tty_devnum(struct tty_struct *tty);
343 extern void proc_clear_tty(struct task_struct *p); 343 extern void proc_clear_tty(struct task_struct *p);
344 extern struct tty_struct *get_current_tty(void); 344 extern struct tty_struct *get_current_tty(void);
345 /* tty_io.c */ 345 /* tty_io.c */
346 extern int __init tty_init(void); 346 extern int __init tty_init(void);
347 #else 347 #else
348 static inline void console_init(void) 348 static inline void console_init(void)
349 { } 349 { }
350 static inline void tty_kref_put(struct tty_struct *tty) 350 static inline void tty_kref_put(struct tty_struct *tty)
351 { } 351 { }
352 static inline struct pid *tty_get_pgrp(struct tty_struct *tty) 352 static inline struct pid *tty_get_pgrp(struct tty_struct *tty)
353 { return NULL; } 353 { return NULL; }
354 static inline void tty_vhangup_self(void) 354 static inline void tty_vhangup_self(void)
355 { } 355 { }
356 static inline void disassociate_ctty(int priv) 356 static inline void disassociate_ctty(int priv)
357 { } 357 { }
358 static inline dev_t tty_devnum(struct tty_struct *tty) 358 static inline dev_t tty_devnum(struct tty_struct *tty)
359 { return 0; } 359 { return 0; }
360 static inline void proc_clear_tty(struct task_struct *p) 360 static inline void proc_clear_tty(struct task_struct *p)
361 { } 361 { }
362 static inline struct tty_struct *get_current_tty(void) 362 static inline struct tty_struct *get_current_tty(void)
363 { return NULL; } 363 { return NULL; }
364 /* tty_io.c */ 364 /* tty_io.c */
365 static inline int __init tty_init(void) 365 static inline int __init tty_init(void)
366 { return 0; } 366 { return 0; }
367 #endif 367 #endif
368 368
369 extern void tty_write_flush(struct tty_struct *); 369 extern void tty_write_flush(struct tty_struct *);
370 370
371 extern struct ktermios tty_std_termios; 371 extern struct ktermios tty_std_termios;
372 372
373 extern int vcs_init(void); 373 extern int vcs_init(void);
374 374
375 extern struct class *tty_class; 375 extern struct class *tty_class;
376 376
377 /** 377 /**
378 * tty_kref_get - get a tty reference 378 * tty_kref_get - get a tty reference
379 * @tty: tty device 379 * @tty: tty device
380 * 380 *
381 * Return a new reference to a tty object. The caller must hold 381 * Return a new reference to a tty object. The caller must hold
382 * sufficient locks/counts to ensure that their existing reference cannot 382 * sufficient locks/counts to ensure that their existing reference cannot
383 * go away 383 * go away
384 */ 384 */
385 385
386 static inline struct tty_struct *tty_kref_get(struct tty_struct *tty) 386 static inline struct tty_struct *tty_kref_get(struct tty_struct *tty)
387 { 387 {
388 if (tty) 388 if (tty)
389 kref_get(&tty->kref); 389 kref_get(&tty->kref);
390 return tty; 390 return tty;
391 } 391 }
392 392
393 extern int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, 393 extern int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
394 const char *routine); 394 const char *routine);
395 extern char *tty_name(struct tty_struct *tty, char *buf); 395 extern char *tty_name(struct tty_struct *tty, char *buf);
396 extern void tty_wait_until_sent(struct tty_struct *tty, long timeout); 396 extern void tty_wait_until_sent(struct tty_struct *tty, long timeout);
397 extern int tty_check_change(struct tty_struct *tty); 397 extern int tty_check_change(struct tty_struct *tty);
398 extern void stop_tty(struct tty_struct *tty); 398 extern void stop_tty(struct tty_struct *tty);
399 extern void start_tty(struct tty_struct *tty); 399 extern void start_tty(struct tty_struct *tty);
400 extern int tty_register_driver(struct tty_driver *driver); 400 extern int tty_register_driver(struct tty_driver *driver);
401 extern int tty_unregister_driver(struct tty_driver *driver); 401 extern int tty_unregister_driver(struct tty_driver *driver);
402 extern struct device *tty_register_device(struct tty_driver *driver, 402 extern struct device *tty_register_device(struct tty_driver *driver,
403 unsigned index, struct device *dev); 403 unsigned index, struct device *dev);
404 extern struct device *tty_register_device_attr(struct tty_driver *driver, 404 extern struct device *tty_register_device_attr(struct tty_driver *driver,
405 unsigned index, struct device *device, 405 unsigned index, struct device *device,
406 void *drvdata, 406 void *drvdata,
407 const struct attribute_group **attr_grp); 407 const struct attribute_group **attr_grp);
408 extern void tty_unregister_device(struct tty_driver *driver, unsigned index); 408 extern void tty_unregister_device(struct tty_driver *driver, unsigned index);
409 extern int tty_read_raw_data(struct tty_struct *tty, unsigned char *bufp, 409 extern int tty_read_raw_data(struct tty_struct *tty, unsigned char *bufp,
410 int buflen); 410 int buflen);
411 extern void tty_write_message(struct tty_struct *tty, char *msg); 411 extern void tty_write_message(struct tty_struct *tty, char *msg);
412 extern int tty_put_char(struct tty_struct *tty, unsigned char c); 412 extern int tty_put_char(struct tty_struct *tty, unsigned char c);
413 extern int tty_chars_in_buffer(struct tty_struct *tty); 413 extern int tty_chars_in_buffer(struct tty_struct *tty);
414 extern int tty_write_room(struct tty_struct *tty); 414 extern int tty_write_room(struct tty_struct *tty);
415 extern void tty_driver_flush_buffer(struct tty_struct *tty); 415 extern void tty_driver_flush_buffer(struct tty_struct *tty);
416 extern void tty_throttle(struct tty_struct *tty); 416 extern void tty_throttle(struct tty_struct *tty);
417 extern void tty_unthrottle(struct tty_struct *tty); 417 extern void tty_unthrottle(struct tty_struct *tty);
418 extern int tty_throttle_safe(struct tty_struct *tty); 418 extern int tty_throttle_safe(struct tty_struct *tty);
419 extern int tty_unthrottle_safe(struct tty_struct *tty); 419 extern int tty_unthrottle_safe(struct tty_struct *tty);
420 extern int tty_do_resize(struct tty_struct *tty, struct winsize *ws); 420 extern int tty_do_resize(struct tty_struct *tty, struct winsize *ws);
421 extern void tty_driver_remove_tty(struct tty_driver *driver, 421 extern void tty_driver_remove_tty(struct tty_driver *driver,
422 struct tty_struct *tty); 422 struct tty_struct *tty);
423 extern void tty_free_termios(struct tty_struct *tty); 423 extern void tty_free_termios(struct tty_struct *tty);
424 extern int is_current_pgrp_orphaned(void); 424 extern int is_current_pgrp_orphaned(void);
425 extern int is_ignored(int sig); 425 extern int is_ignored(int sig);
426 extern int tty_signal(int sig, struct tty_struct *tty); 426 extern int tty_signal(int sig, struct tty_struct *tty);
427 extern void tty_hangup(struct tty_struct *tty); 427 extern void tty_hangup(struct tty_struct *tty);
428 extern void tty_vhangup(struct tty_struct *tty); 428 extern void tty_vhangup(struct tty_struct *tty);
429 extern void tty_vhangup_locked(struct tty_struct *tty); 429 extern void tty_vhangup_locked(struct tty_struct *tty);
430 extern void tty_unhangup(struct file *filp); 430 extern void tty_unhangup(struct file *filp);
431 extern int tty_hung_up_p(struct file *filp); 431 extern int tty_hung_up_p(struct file *filp);
432 extern void do_SAK(struct tty_struct *tty); 432 extern void do_SAK(struct tty_struct *tty);
433 extern void __do_SAK(struct tty_struct *tty); 433 extern void __do_SAK(struct tty_struct *tty);
434 extern void no_tty(void); 434 extern void no_tty(void);
435 extern void tty_flush_to_ldisc(struct tty_struct *tty); 435 extern void tty_flush_to_ldisc(struct tty_struct *tty);
436 extern void tty_buffer_free_all(struct tty_port *port); 436 extern void tty_buffer_free_all(struct tty_port *port);
437 extern void tty_buffer_flush(struct tty_struct *tty); 437 extern void tty_buffer_flush(struct tty_struct *tty);
438 extern void tty_buffer_init(struct tty_port *port); 438 extern void tty_buffer_init(struct tty_port *port);
439 extern speed_t tty_termios_baud_rate(struct ktermios *termios); 439 extern speed_t tty_termios_baud_rate(struct ktermios *termios);
440 extern speed_t tty_termios_input_baud_rate(struct ktermios *termios); 440 extern speed_t tty_termios_input_baud_rate(struct ktermios *termios);
441 extern void tty_termios_encode_baud_rate(struct ktermios *termios, 441 extern void tty_termios_encode_baud_rate(struct ktermios *termios,
442 speed_t ibaud, speed_t obaud); 442 speed_t ibaud, speed_t obaud);
443 extern void tty_encode_baud_rate(struct tty_struct *tty, 443 extern void tty_encode_baud_rate(struct tty_struct *tty,
444 speed_t ibaud, speed_t obaud); 444 speed_t ibaud, speed_t obaud);
445 445
446 /** 446 /**
447 * tty_get_baud_rate - get tty bit rates 447 * tty_get_baud_rate - get tty bit rates
448 * @tty: tty to query 448 * @tty: tty to query
449 * 449 *
450 * Returns the baud rate as an integer for this terminal. The 450 * Returns the baud rate as an integer for this terminal. The
451 * termios lock must be held by the caller and the terminal bit 451 * termios lock must be held by the caller and the terminal bit
452 * flags may be updated. 452 * flags may be updated.
453 * 453 *
454 * Locking: none 454 * Locking: none
455 */ 455 */
456 static inline speed_t tty_get_baud_rate(struct tty_struct *tty) 456 static inline speed_t tty_get_baud_rate(struct tty_struct *tty)
457 { 457 {
458 return tty_termios_baud_rate(&tty->termios); 458 return tty_termios_baud_rate(&tty->termios);
459 } 459 }
460 460
461 extern void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old); 461 extern void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old);
462 extern int tty_termios_hw_change(struct ktermios *a, struct ktermios *b); 462 extern int tty_termios_hw_change(struct ktermios *a, struct ktermios *b);
463 extern int tty_set_termios(struct tty_struct *tty, struct ktermios *kt); 463 extern int tty_set_termios(struct tty_struct *tty, struct ktermios *kt);
464 464
465 extern struct tty_ldisc *tty_ldisc_ref(struct tty_struct *); 465 extern struct tty_ldisc *tty_ldisc_ref(struct tty_struct *);
466 extern void tty_ldisc_deref(struct tty_ldisc *); 466 extern void tty_ldisc_deref(struct tty_ldisc *);
467 extern struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *); 467 extern struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *);
468 extern void tty_ldisc_hangup(struct tty_struct *tty); 468 extern void tty_ldisc_hangup(struct tty_struct *tty);
469 extern const struct file_operations tty_ldiscs_proc_fops; 469 extern const struct file_operations tty_ldiscs_proc_fops;
470 470
471 extern void tty_wakeup(struct tty_struct *tty); 471 extern void tty_wakeup(struct tty_struct *tty);
472 extern void tty_ldisc_flush(struct tty_struct *tty); 472 extern void tty_ldisc_flush(struct tty_struct *tty);
473 473
474 extern long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 474 extern long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
475 extern int tty_mode_ioctl(struct tty_struct *tty, struct file *file, 475 extern int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
476 unsigned int cmd, unsigned long arg); 476 unsigned int cmd, unsigned long arg);
477 extern int tty_perform_flush(struct tty_struct *tty, unsigned long arg); 477 extern int tty_perform_flush(struct tty_struct *tty, unsigned long arg);
478 extern void tty_default_fops(struct file_operations *fops); 478 extern void tty_default_fops(struct file_operations *fops);
479 extern struct tty_struct *alloc_tty_struct(void); 479 extern struct tty_struct *alloc_tty_struct(void);
480 extern int tty_alloc_file(struct file *file); 480 extern int tty_alloc_file(struct file *file);
481 extern void tty_add_file(struct tty_struct *tty, struct file *file); 481 extern void tty_add_file(struct tty_struct *tty, struct file *file);
482 extern void tty_free_file(struct file *file); 482 extern void tty_free_file(struct file *file);
483 extern void free_tty_struct(struct tty_struct *tty); 483 extern void free_tty_struct(struct tty_struct *tty);
484 extern void initialize_tty_struct(struct tty_struct *tty, 484 extern void initialize_tty_struct(struct tty_struct *tty,
485 struct tty_driver *driver, int idx); 485 struct tty_driver *driver, int idx);
486 extern void deinitialize_tty_struct(struct tty_struct *tty); 486 extern void deinitialize_tty_struct(struct tty_struct *tty);
487 extern struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx); 487 extern struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx);
488 extern int tty_release(struct inode *inode, struct file *filp); 488 extern int tty_release(struct inode *inode, struct file *filp);
489 extern int tty_init_termios(struct tty_struct *tty); 489 extern int tty_init_termios(struct tty_struct *tty);
490 extern int tty_standard_install(struct tty_driver *driver, 490 extern int tty_standard_install(struct tty_driver *driver,
491 struct tty_struct *tty); 491 struct tty_struct *tty);
492 492
493 extern struct tty_struct *tty_pair_get_tty(struct tty_struct *tty); 493 extern struct tty_struct *tty_pair_get_tty(struct tty_struct *tty);
494 extern struct tty_struct *tty_pair_get_pty(struct tty_struct *tty); 494 extern struct tty_struct *tty_pair_get_pty(struct tty_struct *tty);
495 495
496 extern struct mutex tty_mutex; 496 extern struct mutex tty_mutex;
497 extern spinlock_t tty_files_lock; 497 extern spinlock_t tty_files_lock;
498 498
499 extern void tty_write_unlock(struct tty_struct *tty); 499 extern void tty_write_unlock(struct tty_struct *tty);
500 extern int tty_write_lock(struct tty_struct *tty, int ndelay); 500 extern int tty_write_lock(struct tty_struct *tty, int ndelay);
501 #define tty_is_writelocked(tty) (mutex_is_locked(&tty->atomic_write_lock)) 501 #define tty_is_writelocked(tty) (mutex_is_locked(&tty->atomic_write_lock))
502 502
503 extern void tty_port_init(struct tty_port *port); 503 extern void tty_port_init(struct tty_port *port);
504 extern void tty_port_link_device(struct tty_port *port, 504 extern void tty_port_link_device(struct tty_port *port,
505 struct tty_driver *driver, unsigned index); 505 struct tty_driver *driver, unsigned index);
506 extern struct device *tty_port_register_device(struct tty_port *port, 506 extern struct device *tty_port_register_device(struct tty_port *port,
507 struct tty_driver *driver, unsigned index, 507 struct tty_driver *driver, unsigned index,
508 struct device *device); 508 struct device *device);
509 extern struct device *tty_port_register_device_attr(struct tty_port *port, 509 extern struct device *tty_port_register_device_attr(struct tty_port *port,
510 struct tty_driver *driver, unsigned index, 510 struct tty_driver *driver, unsigned index,
511 struct device *device, void *drvdata, 511 struct device *device, void *drvdata,
512 const struct attribute_group **attr_grp); 512 const struct attribute_group **attr_grp);
513 extern int tty_port_alloc_xmit_buf(struct tty_port *port); 513 extern int tty_port_alloc_xmit_buf(struct tty_port *port);
514 extern void tty_port_free_xmit_buf(struct tty_port *port); 514 extern void tty_port_free_xmit_buf(struct tty_port *port);
515 extern void tty_port_destroy(struct tty_port *port); 515 extern void tty_port_destroy(struct tty_port *port);
516 extern void tty_port_put(struct tty_port *port); 516 extern void tty_port_put(struct tty_port *port);
517 517
518 static inline struct tty_port *tty_port_get(struct tty_port *port) 518 static inline struct tty_port *tty_port_get(struct tty_port *port)
519 { 519 {
520 if (port) 520 if (port)
521 kref_get(&port->kref); 521 kref_get(&port->kref);
522 return port; 522 return port;
523 } 523 }
524 524
525 /* If the cts flow control is enabled, return true. */ 525 /* If the cts flow control is enabled, return true. */
526 static inline bool tty_port_cts_enabled(struct tty_port *port) 526 static inline bool tty_port_cts_enabled(struct tty_port *port)
527 { 527 {
528 return port->flags & ASYNC_CTS_FLOW; 528 return port->flags & ASYNC_CTS_FLOW;
529 } 529 }
530 530
531 extern struct tty_struct *tty_port_tty_get(struct tty_port *port); 531 extern struct tty_struct *tty_port_tty_get(struct tty_port *port);
532 extern void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty); 532 extern void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty);
533 extern int tty_port_carrier_raised(struct tty_port *port); 533 extern int tty_port_carrier_raised(struct tty_port *port);
534 extern void tty_port_raise_dtr_rts(struct tty_port *port); 534 extern void tty_port_raise_dtr_rts(struct tty_port *port);
535 extern void tty_port_lower_dtr_rts(struct tty_port *port); 535 extern void tty_port_lower_dtr_rts(struct tty_port *port);
536 extern void tty_port_hangup(struct tty_port *port); 536 extern void tty_port_hangup(struct tty_port *port);
537 extern void tty_port_tty_wakeup(struct tty_port *port);
537 extern int tty_port_block_til_ready(struct tty_port *port, 538 extern int tty_port_block_til_ready(struct tty_port *port,
538 struct tty_struct *tty, struct file *filp); 539 struct tty_struct *tty, struct file *filp);
539 extern int tty_port_close_start(struct tty_port *port, 540 extern int tty_port_close_start(struct tty_port *port,
540 struct tty_struct *tty, struct file *filp); 541 struct tty_struct *tty, struct file *filp);
541 extern void tty_port_close_end(struct tty_port *port, struct tty_struct *tty); 542 extern void tty_port_close_end(struct tty_port *port, struct tty_struct *tty);
542 extern void tty_port_close(struct tty_port *port, 543 extern void tty_port_close(struct tty_port *port,
543 struct tty_struct *tty, struct file *filp); 544 struct tty_struct *tty, struct file *filp);
544 extern int tty_port_install(struct tty_port *port, struct tty_driver *driver, 545 extern int tty_port_install(struct tty_port *port, struct tty_driver *driver,
545 struct tty_struct *tty); 546 struct tty_struct *tty);
546 extern int tty_port_open(struct tty_port *port, 547 extern int tty_port_open(struct tty_port *port,
547 struct tty_struct *tty, struct file *filp); 548 struct tty_struct *tty, struct file *filp);
548 static inline int tty_port_users(struct tty_port *port) 549 static inline int tty_port_users(struct tty_port *port)
549 { 550 {
550 return port->count + port->blocked_open; 551 return port->count + port->blocked_open;
551 } 552 }
552 553
553 extern int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc); 554 extern int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc);
554 extern int tty_unregister_ldisc(int disc); 555 extern int tty_unregister_ldisc(int disc);
555 extern int tty_set_ldisc(struct tty_struct *tty, int ldisc); 556 extern int tty_set_ldisc(struct tty_struct *tty, int ldisc);
556 extern int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty); 557 extern int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty);
557 extern void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty); 558 extern void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty);
558 extern void tty_ldisc_init(struct tty_struct *tty); 559 extern void tty_ldisc_init(struct tty_struct *tty);
559 extern void tty_ldisc_deinit(struct tty_struct *tty); 560 extern void tty_ldisc_deinit(struct tty_struct *tty);
560 extern void tty_ldisc_begin(void); 561 extern void tty_ldisc_begin(void);
561 /* This last one is just for the tty layer internals and shouldn't be used elsewhere */ 562 /* This last one is just for the tty layer internals and shouldn't be used elsewhere */
562 extern void tty_ldisc_enable(struct tty_struct *tty); 563 extern void tty_ldisc_enable(struct tty_struct *tty);
563 564
564 565
565 /* n_tty.c */ 566 /* n_tty.c */
566 extern struct tty_ldisc_ops tty_ldisc_N_TTY; 567 extern struct tty_ldisc_ops tty_ldisc_N_TTY;
567 extern void n_tty_inherit_ops(struct tty_ldisc_ops *ops); 568 extern void n_tty_inherit_ops(struct tty_ldisc_ops *ops);
568 569
569 /* tty_audit.c */ 570 /* tty_audit.c */
570 #ifdef CONFIG_AUDIT 571 #ifdef CONFIG_AUDIT
571 extern void tty_audit_add_data(struct tty_struct *tty, unsigned char *data, 572 extern void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
572 size_t size, unsigned icanon); 573 size_t size, unsigned icanon);
573 extern void tty_audit_exit(void); 574 extern void tty_audit_exit(void);
574 extern void tty_audit_fork(struct signal_struct *sig); 575 extern void tty_audit_fork(struct signal_struct *sig);
575 extern void tty_audit_tiocsti(struct tty_struct *tty, char ch); 576 extern void tty_audit_tiocsti(struct tty_struct *tty, char ch);
576 extern void tty_audit_push(struct tty_struct *tty); 577 extern void tty_audit_push(struct tty_struct *tty);
577 extern int tty_audit_push_task(struct task_struct *tsk, 578 extern int tty_audit_push_task(struct task_struct *tsk,
578 kuid_t loginuid, u32 sessionid); 579 kuid_t loginuid, u32 sessionid);
579 #else 580 #else
580 static inline void tty_audit_add_data(struct tty_struct *tty, 581 static inline void tty_audit_add_data(struct tty_struct *tty,
581 unsigned char *data, size_t size, unsigned icanon) 582 unsigned char *data, size_t size, unsigned icanon)
582 { 583 {
583 } 584 }
584 static inline void tty_audit_tiocsti(struct tty_struct *tty, char ch) 585 static inline void tty_audit_tiocsti(struct tty_struct *tty, char ch)
585 { 586 {
586 } 587 }
587 static inline void tty_audit_exit(void) 588 static inline void tty_audit_exit(void)
588 { 589 {
589 } 590 }
590 static inline void tty_audit_fork(struct signal_struct *sig) 591 static inline void tty_audit_fork(struct signal_struct *sig)
591 { 592 {
592 } 593 }
593 static inline void tty_audit_push(struct tty_struct *tty) 594 static inline void tty_audit_push(struct tty_struct *tty)
594 { 595 {
595 } 596 }
596 static inline int tty_audit_push_task(struct task_struct *tsk, 597 static inline int tty_audit_push_task(struct task_struct *tsk,
597 kuid_t loginuid, u32 sessionid) 598 kuid_t loginuid, u32 sessionid)
598 { 599 {
599 return 0; 600 return 0;
600 } 601 }
601 #endif 602 #endif
602 603
603 /* tty_ioctl.c */ 604 /* tty_ioctl.c */
604 extern int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file, 605 extern int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
605 unsigned int cmd, unsigned long arg); 606 unsigned int cmd, unsigned long arg);
606 extern long n_tty_compat_ioctl_helper(struct tty_struct *tty, struct file *file, 607 extern long n_tty_compat_ioctl_helper(struct tty_struct *tty, struct file *file,
607 unsigned int cmd, unsigned long arg); 608 unsigned int cmd, unsigned long arg);
608 609
609 /* serial.c */ 610 /* serial.c */
610 611
611 extern void serial_console_init(void); 612 extern void serial_console_init(void);
612 613
613 /* pcxx.c */ 614 /* pcxx.c */
614 615
615 extern int pcxe_open(struct tty_struct *tty, struct file *filp); 616 extern int pcxe_open(struct tty_struct *tty, struct file *filp);
616 617
617 /* vt.c */ 618 /* vt.c */
618 619
619 extern int vt_ioctl(struct tty_struct *tty, 620 extern int vt_ioctl(struct tty_struct *tty,
620 unsigned int cmd, unsigned long arg); 621 unsigned int cmd, unsigned long arg);
621 622
622 extern long vt_compat_ioctl(struct tty_struct *tty, 623 extern long vt_compat_ioctl(struct tty_struct *tty,
623 unsigned int cmd, unsigned long arg); 624 unsigned int cmd, unsigned long arg);
624 625
625 /* tty_mutex.c */ 626 /* tty_mutex.c */
626 /* functions for preparation of BKL removal */ 627 /* functions for preparation of BKL removal */
627 extern void __lockfunc tty_lock(struct tty_struct *tty); 628 extern void __lockfunc tty_lock(struct tty_struct *tty);
628 extern void __lockfunc tty_unlock(struct tty_struct *tty); 629 extern void __lockfunc tty_unlock(struct tty_struct *tty);
629 extern void __lockfunc tty_lock_pair(struct tty_struct *tty, 630 extern void __lockfunc tty_lock_pair(struct tty_struct *tty,
630 struct tty_struct *tty2); 631 struct tty_struct *tty2);
631 extern void __lockfunc tty_unlock_pair(struct tty_struct *tty, 632 extern void __lockfunc tty_unlock_pair(struct tty_struct *tty,
632 struct tty_struct *tty2); 633 struct tty_struct *tty2);
633 634
634 /* 635 /*
635 * this shall be called only from where BTM is held (like close) 636 * this shall be called only from where BTM is held (like close)
636 * 637 *
637 * We need this to ensure nobody waits for us to finish while we are waiting. 638 * We need this to ensure nobody waits for us to finish while we are waiting.
638 * Without this we were encountering system stalls. 639 * Without this we were encountering system stalls.
639 * 640 *
640 * This should be indeed removed with BTM removal later. 641 * This should be indeed removed with BTM removal later.
641 * 642 *
642 * Locking: BTM required. Nobody is allowed to hold port->mutex. 643 * Locking: BTM required. Nobody is allowed to hold port->mutex.
643 */ 644 */
644 static inline void tty_wait_until_sent_from_close(struct tty_struct *tty, 645 static inline void tty_wait_until_sent_from_close(struct tty_struct *tty,
645 long timeout) 646 long timeout)
646 { 647 {
647 tty_unlock(tty); /* tty->ops->close holds the BTM, drop it while waiting */ 648 tty_unlock(tty); /* tty->ops->close holds the BTM, drop it while waiting */
648 tty_wait_until_sent(tty, timeout); 649 tty_wait_until_sent(tty, timeout);
649 tty_lock(tty); 650 tty_lock(tty);
650 } 651 }
651 652
652 /* 653 /*
653 * wait_event_interruptible_tty -- wait for a condition with the tty lock held 654 * wait_event_interruptible_tty -- wait for a condition with the tty lock held
654 * 655 *
655 * The condition we are waiting for might take a long time to 656 * The condition we are waiting for might take a long time to
656 * become true, or might depend on another thread taking the 657 * become true, or might depend on another thread taking the
657 * BTM. In either case, we need to drop the BTM to guarantee 658 * BTM. In either case, we need to drop the BTM to guarantee
658 * forward progress. This is a leftover from the conversion 659 * forward progress. This is a leftover from the conversion
659 * from the BKL and should eventually get removed as the BTM 660 * from the BKL and should eventually get removed as the BTM
660 * falls out of use. 661 * falls out of use.
661 * 662 *
662 * Do not use in new code. 663 * Do not use in new code.
663 */ 664 */
664 #define wait_event_interruptible_tty(tty, wq, condition) \ 665 #define wait_event_interruptible_tty(tty, wq, condition) \
665 ({ \ 666 ({ \
666 int __ret = 0; \ 667 int __ret = 0; \
667 if (!(condition)) { \ 668 if (!(condition)) { \
668 __wait_event_interruptible_tty(tty, wq, condition, __ret); \ 669 __wait_event_interruptible_tty(tty, wq, condition, __ret); \
669 } \ 670 } \
670 __ret; \ 671 __ret; \
671 }) 672 })
672 673
673 #define __wait_event_interruptible_tty(tty, wq, condition, ret) \ 674 #define __wait_event_interruptible_tty(tty, wq, condition, ret) \
674 do { \ 675 do { \
675 DEFINE_WAIT(__wait); \ 676 DEFINE_WAIT(__wait); \
676 \ 677 \
677 for (;;) { \ 678 for (;;) { \
678 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ 679 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
679 if (condition) \ 680 if (condition) \
680 break; \ 681 break; \
681 if (!signal_pending(current)) { \ 682 if (!signal_pending(current)) { \
682 tty_unlock(tty); \ 683 tty_unlock(tty); \
683 schedule(); \ 684 schedule(); \
684 tty_lock(tty); \ 685 tty_lock(tty); \
685 continue; \ 686 continue; \
686 } \ 687 } \
687 ret = -ERESTARTSYS; \ 688 ret = -ERESTARTSYS; \
688 break; \ 689 break; \
689 } \ 690 } \
690 finish_wait(&wq, &__wait); \ 691 finish_wait(&wq, &__wait); \
691 } while (0) 692 } while (0)
692 693
693 694
694 #endif 695 #endif
695 696