]>
Commit | Line | Data |
---|---|---|
ee8a4b7f HL |
1 | /* |
2 | * interface to user space for the gigaset driver | |
3 | * | |
4 | * Copyright (c) 2004 by Hansjoerg Lipp <hjlipp@web.de> | |
5 | * | |
6 | * ===================================================================== | |
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 | |
9 | * published by the Free Software Foundation; either version 2 of | |
10 | * the License, or (at your option) any later version. | |
11 | * ===================================================================== | |
ee8a4b7f HL |
12 | */ |
13 | ||
14 | #include "gigaset.h" | |
15 | #include <linux/gigaset_dev.h> | |
16 | #include <linux/tty.h> | |
17 | #include <linux/tty_flip.h> | |
18 | ||
19 | /*** our ioctls ***/ | |
20 | ||
21 | static int if_lock(struct cardstate *cs, int *arg) | |
22 | { | |
23 | int cmd = *arg; | |
24 | ||
784d5858 | 25 | gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd); |
ee8a4b7f HL |
26 | |
27 | if (cmd > 1) | |
28 | return -EINVAL; | |
29 | ||
30 | if (cmd < 0) { | |
9d4bee2b | 31 | *arg = cs->mstate == MS_LOCKED; |
ee8a4b7f HL |
32 | return 0; |
33 | } | |
34 | ||
9d4bee2b | 35 | if (!cmd && cs->mstate == MS_LOCKED && cs->connected) { |
ee8a4b7f HL |
36 | cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS); |
37 | cs->ops->baud_rate(cs, B115200); | |
38 | cs->ops->set_line_ctrl(cs, CS8); | |
39 | cs->control_state = TIOCM_DTR|TIOCM_RTS; | |
40 | } | |
41 | ||
42 | cs->waiting = 1; | |
43 | if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK, | |
784d5858 | 44 | NULL, cmd, NULL)) { |
ee8a4b7f HL |
45 | cs->waiting = 0; |
46 | return -ENOMEM; | |
47 | } | |
ee8a4b7f HL |
48 | gigaset_schedule_event(cs); |
49 | ||
50 | wait_event(cs->waitqueue, !cs->waiting); | |
51 | ||
52 | if (cs->cmd_result >= 0) { | |
53 | *arg = cs->cmd_result; | |
54 | return 0; | |
55 | } | |
56 | ||
57 | return cs->cmd_result; | |
58 | } | |
59 | ||
60 | static int if_version(struct cardstate *cs, unsigned arg[4]) | |
61 | { | |
62 | static const unsigned version[4] = GIG_VERSION; | |
63 | static const unsigned compat[4] = GIG_COMPAT; | |
64 | unsigned cmd = arg[0]; | |
65 | ||
784d5858 | 66 | gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd); |
ee8a4b7f HL |
67 | |
68 | switch (cmd) { | |
69 | case GIGVER_DRIVER: | |
70 | memcpy(arg, version, sizeof version); | |
71 | return 0; | |
72 | case GIGVER_COMPAT: | |
73 | memcpy(arg, compat, sizeof compat); | |
74 | return 0; | |
75 | case GIGVER_FWBASE: | |
76 | cs->waiting = 1; | |
77 | if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER, | |
784d5858 | 78 | NULL, 0, arg)) { |
ee8a4b7f HL |
79 | cs->waiting = 0; |
80 | return -ENOMEM; | |
81 | } | |
ee8a4b7f HL |
82 | gigaset_schedule_event(cs); |
83 | ||
84 | wait_event(cs->waitqueue, !cs->waiting); | |
85 | ||
86 | if (cs->cmd_result >= 0) | |
87 | return 0; | |
88 | ||
89 | return cs->cmd_result; | |
90 | default: | |
91 | return -EINVAL; | |
92 | } | |
93 | } | |
94 | ||
95 | static int if_config(struct cardstate *cs, int *arg) | |
96 | { | |
784d5858 | 97 | gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg); |
ee8a4b7f HL |
98 | |
99 | if (*arg != 1) | |
100 | return -EINVAL; | |
101 | ||
9d4bee2b | 102 | if (cs->mstate != MS_LOCKED) |
ee8a4b7f HL |
103 | return -EBUSY; |
104 | ||
69049cc8 | 105 | if (!cs->connected) { |
c8770dca | 106 | pr_err("%s: not connected\n", __func__); |
69049cc8 TS |
107 | return -ENODEV; |
108 | } | |
109 | ||
ee8a4b7f HL |
110 | *arg = 0; |
111 | return gigaset_enterconfigmode(cs); | |
112 | } | |
113 | ||
114 | /*** the terminal driver ***/ | |
115 | /* stolen from usbserial and some other tty drivers */ | |
116 | ||
117 | static int if_open(struct tty_struct *tty, struct file *filp); | |
118 | static void if_close(struct tty_struct *tty, struct file *filp); | |
119 | static int if_ioctl(struct tty_struct *tty, struct file *file, | |
784d5858 | 120 | unsigned int cmd, unsigned long arg); |
ee8a4b7f HL |
121 | static int if_write_room(struct tty_struct *tty); |
122 | static int if_chars_in_buffer(struct tty_struct *tty); | |
123 | static void if_throttle(struct tty_struct *tty); | |
124 | static void if_unthrottle(struct tty_struct *tty); | |
606d099c | 125 | static void if_set_termios(struct tty_struct *tty, struct ktermios *old); |
ee8a4b7f HL |
126 | static int if_tiocmget(struct tty_struct *tty, struct file *file); |
127 | static int if_tiocmset(struct tty_struct *tty, struct file *file, | |
784d5858 | 128 | unsigned int set, unsigned int clear); |
ee8a4b7f | 129 | static int if_write(struct tty_struct *tty, |
784d5858 | 130 | const unsigned char *buf, int count); |
ee8a4b7f | 131 | |
b68e31d0 | 132 | static const struct tty_operations if_ops = { |
ee8a4b7f HL |
133 | .open = if_open, |
134 | .close = if_close, | |
135 | .ioctl = if_ioctl, | |
136 | .write = if_write, | |
137 | .write_room = if_write_room, | |
138 | .chars_in_buffer = if_chars_in_buffer, | |
139 | .set_termios = if_set_termios, | |
140 | .throttle = if_throttle, | |
141 | .unthrottle = if_unthrottle, | |
ee8a4b7f HL |
142 | .tiocmget = if_tiocmget, |
143 | .tiocmset = if_tiocmset, | |
144 | }; | |
145 | ||
146 | static int if_open(struct tty_struct *tty, struct file *filp) | |
147 | { | |
148 | struct cardstate *cs; | |
149 | unsigned long flags; | |
150 | ||
784d5858 TS |
151 | gig_dbg(DEBUG_IF, "%d+%d: %s()", |
152 | tty->driver->minor_start, tty->index, __func__); | |
ee8a4b7f HL |
153 | |
154 | tty->driver_data = NULL; | |
155 | ||
156 | cs = gigaset_get_cs_by_tty(tty); | |
e468c048 | 157 | if (!cs || !try_module_get(cs->driver->owner)) |
ee8a4b7f HL |
158 | return -ENODEV; |
159 | ||
abfd1dc7 | 160 | if (mutex_lock_interruptible(&cs->mutex)) |
d9ba9c91 | 161 | return -ERESTARTSYS; |
ee8a4b7f HL |
162 | tty->driver_data = cs; |
163 | ||
164 | ++cs->open_count; | |
165 | ||
166 | if (cs->open_count == 1) { | |
167 | spin_lock_irqsave(&cs->lock, flags); | |
168 | cs->tty = tty; | |
169 | spin_unlock_irqrestore(&cs->lock, flags); | |
d9ba9c91 | 170 | tty->low_latency = 1; |
ee8a4b7f HL |
171 | } |
172 | ||
abfd1dc7 | 173 | mutex_unlock(&cs->mutex); |
ee8a4b7f HL |
174 | return 0; |
175 | } | |
176 | ||
177 | static void if_close(struct tty_struct *tty, struct file *filp) | |
178 | { | |
179 | struct cardstate *cs; | |
180 | unsigned long flags; | |
181 | ||
182 | cs = (struct cardstate *) tty->driver_data; | |
183 | if (!cs) { | |
c8770dca | 184 | pr_err("%s: no cardstate\n", __func__); |
ee8a4b7f HL |
185 | return; |
186 | } | |
187 | ||
784d5858 | 188 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
ee8a4b7f | 189 | |
abfd1dc7 | 190 | mutex_lock(&cs->mutex); |
ee8a4b7f | 191 | |
51370e5b TS |
192 | if (!cs->connected) |
193 | gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ | |
194 | else if (!cs->open_count) | |
5002779d | 195 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
ee8a4b7f HL |
196 | else { |
197 | if (!--cs->open_count) { | |
198 | spin_lock_irqsave(&cs->lock, flags); | |
199 | cs->tty = NULL; | |
200 | spin_unlock_irqrestore(&cs->lock, flags); | |
ee8a4b7f HL |
201 | } |
202 | } | |
203 | ||
abfd1dc7 | 204 | mutex_unlock(&cs->mutex); |
e468c048 TS |
205 | |
206 | module_put(cs->driver->owner); | |
ee8a4b7f HL |
207 | } |
208 | ||
209 | static int if_ioctl(struct tty_struct *tty, struct file *file, | |
784d5858 | 210 | unsigned int cmd, unsigned long arg) |
ee8a4b7f HL |
211 | { |
212 | struct cardstate *cs; | |
213 | int retval = -ENODEV; | |
214 | int int_arg; | |
215 | unsigned char buf[6]; | |
216 | unsigned version[4]; | |
217 | ||
218 | cs = (struct cardstate *) tty->driver_data; | |
219 | if (!cs) { | |
c8770dca | 220 | pr_err("%s: no cardstate\n", __func__); |
ee8a4b7f HL |
221 | return -ENODEV; |
222 | } | |
223 | ||
784d5858 | 224 | gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd); |
ee8a4b7f | 225 | |
abfd1dc7 | 226 | if (mutex_lock_interruptible(&cs->mutex)) |
d9ba9c91 | 227 | return -ERESTARTSYS; |
ee8a4b7f | 228 | |
51370e5b TS |
229 | if (!cs->connected) { |
230 | gig_dbg(DEBUG_IF, "not connected"); | |
231 | retval = -ENODEV; | |
232 | } else if (!cs->open_count) | |
5002779d | 233 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
ee8a4b7f HL |
234 | else { |
235 | retval = 0; | |
236 | switch (cmd) { | |
237 | case GIGASET_REDIR: | |
238 | retval = get_user(int_arg, (int __user *) arg); | |
239 | if (retval >= 0) | |
240 | retval = if_lock(cs, &int_arg); | |
241 | if (retval >= 0) | |
242 | retval = put_user(int_arg, (int __user *) arg); | |
243 | break; | |
244 | case GIGASET_CONFIG: | |
245 | retval = get_user(int_arg, (int __user *) arg); | |
246 | if (retval >= 0) | |
247 | retval = if_config(cs, &int_arg); | |
248 | if (retval >= 0) | |
249 | retval = put_user(int_arg, (int __user *) arg); | |
250 | break; | |
251 | case GIGASET_BRKCHARS: | |
ee8a4b7f | 252 | retval = copy_from_user(&buf, |
784d5858 TS |
253 | (const unsigned char __user *) arg, 6) |
254 | ? -EFAULT : 0; | |
01371500 TS |
255 | if (retval >= 0) { |
256 | gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS", | |
257 | 6, (const unsigned char *) arg); | |
ee8a4b7f | 258 | retval = cs->ops->brkchars(cs, buf); |
01371500 | 259 | } |
ee8a4b7f HL |
260 | break; |
261 | case GIGASET_VERSION: | |
917f5085 | 262 | retval = copy_from_user(version, |
784d5858 TS |
263 | (unsigned __user *) arg, sizeof version) |
264 | ? -EFAULT : 0; | |
ee8a4b7f HL |
265 | if (retval >= 0) |
266 | retval = if_version(cs, version); | |
267 | if (retval >= 0) | |
917f5085 | 268 | retval = copy_to_user((unsigned __user *) arg, |
784d5858 TS |
269 | version, sizeof version) |
270 | ? -EFAULT : 0; | |
ee8a4b7f | 271 | break; |
784d5858 | 272 | default: |
1528b18f | 273 | gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x", |
784d5858 | 274 | __func__, cmd); |
ee8a4b7f HL |
275 | retval = -ENOIOCTLCMD; |
276 | } | |
277 | } | |
278 | ||
abfd1dc7 | 279 | mutex_unlock(&cs->mutex); |
ee8a4b7f HL |
280 | |
281 | return retval; | |
282 | } | |
283 | ||
284 | static int if_tiocmget(struct tty_struct *tty, struct file *file) | |
285 | { | |
286 | struct cardstate *cs; | |
287 | int retval; | |
288 | ||
289 | cs = (struct cardstate *) tty->driver_data; | |
290 | if (!cs) { | |
c8770dca | 291 | pr_err("%s: no cardstate\n", __func__); |
ee8a4b7f HL |
292 | return -ENODEV; |
293 | } | |
294 | ||
784d5858 | 295 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
ee8a4b7f | 296 | |
abfd1dc7 | 297 | if (mutex_lock_interruptible(&cs->mutex)) |
d9ba9c91 | 298 | return -ERESTARTSYS; |
ee8a4b7f | 299 | |
ee8a4b7f HL |
300 | retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR); |
301 | ||
abfd1dc7 | 302 | mutex_unlock(&cs->mutex); |
ee8a4b7f HL |
303 | |
304 | return retval; | |
305 | } | |
306 | ||
307 | static int if_tiocmset(struct tty_struct *tty, struct file *file, | |
784d5858 | 308 | unsigned int set, unsigned int clear) |
ee8a4b7f HL |
309 | { |
310 | struct cardstate *cs; | |
311 | int retval; | |
312 | unsigned mc; | |
313 | ||
314 | cs = (struct cardstate *) tty->driver_data; | |
315 | if (!cs) { | |
c8770dca | 316 | pr_err("%s: no cardstate\n", __func__); |
ee8a4b7f HL |
317 | return -ENODEV; |
318 | } | |
319 | ||
784d5858 TS |
320 | gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)", |
321 | cs->minor_index, __func__, set, clear); | |
ee8a4b7f | 322 | |
abfd1dc7 | 323 | if (mutex_lock_interruptible(&cs->mutex)) |
d9ba9c91 | 324 | return -ERESTARTSYS; |
ee8a4b7f | 325 | |
69049cc8 | 326 | if (!cs->connected) { |
51370e5b | 327 | gig_dbg(DEBUG_IF, "not connected"); |
ee8a4b7f HL |
328 | retval = -ENODEV; |
329 | } else { | |
330 | mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR); | |
331 | retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc); | |
332 | cs->control_state = mc; | |
333 | } | |
334 | ||
abfd1dc7 | 335 | mutex_unlock(&cs->mutex); |
ee8a4b7f HL |
336 | |
337 | return retval; | |
338 | } | |
339 | ||
340 | static int if_write(struct tty_struct *tty, const unsigned char *buf, int count) | |
341 | { | |
342 | struct cardstate *cs; | |
343 | int retval = -ENODEV; | |
344 | ||
345 | cs = (struct cardstate *) tty->driver_data; | |
346 | if (!cs) { | |
c8770dca | 347 | pr_err("%s: no cardstate\n", __func__); |
ee8a4b7f HL |
348 | return -ENODEV; |
349 | } | |
350 | ||
784d5858 | 351 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
ee8a4b7f | 352 | |
abfd1dc7 | 353 | if (mutex_lock_interruptible(&cs->mutex)) |
d9ba9c91 | 354 | return -ERESTARTSYS; |
ee8a4b7f | 355 | |
51370e5b TS |
356 | if (!cs->connected) { |
357 | gig_dbg(DEBUG_IF, "not connected"); | |
358 | retval = -ENODEV; | |
359 | } else if (!cs->open_count) | |
5002779d | 360 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
9d4bee2b | 361 | else if (cs->mstate != MS_LOCKED) { |
5002779d | 362 | dev_warn(cs->dev, "can't write to unlocked device\n"); |
ee8a4b7f | 363 | retval = -EBUSY; |
ee8a4b7f HL |
364 | } else { |
365 | retval = cs->ops->write_cmd(cs, buf, count, | |
784d5858 | 366 | &cs->if_wake_tasklet); |
ee8a4b7f HL |
367 | } |
368 | ||
abfd1dc7 | 369 | mutex_unlock(&cs->mutex); |
ee8a4b7f HL |
370 | |
371 | return retval; | |
372 | } | |
373 | ||
374 | static int if_write_room(struct tty_struct *tty) | |
375 | { | |
376 | struct cardstate *cs; | |
377 | int retval = -ENODEV; | |
378 | ||
379 | cs = (struct cardstate *) tty->driver_data; | |
380 | if (!cs) { | |
c8770dca | 381 | pr_err("%s: no cardstate\n", __func__); |
ee8a4b7f HL |
382 | return -ENODEV; |
383 | } | |
384 | ||
784d5858 | 385 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
ee8a4b7f | 386 | |
abfd1dc7 | 387 | if (mutex_lock_interruptible(&cs->mutex)) |
d9ba9c91 | 388 | return -ERESTARTSYS; |
ee8a4b7f | 389 | |
51370e5b TS |
390 | if (!cs->connected) { |
391 | gig_dbg(DEBUG_IF, "not connected"); | |
392 | retval = -ENODEV; | |
393 | } else if (!cs->open_count) | |
5002779d | 394 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
9d4bee2b | 395 | else if (cs->mstate != MS_LOCKED) { |
5002779d | 396 | dev_warn(cs->dev, "can't write to unlocked device\n"); |
9d4bee2b | 397 | retval = -EBUSY; |
ee8a4b7f HL |
398 | } else |
399 | retval = cs->ops->write_room(cs); | |
400 | ||
abfd1dc7 | 401 | mutex_unlock(&cs->mutex); |
ee8a4b7f HL |
402 | |
403 | return retval; | |
404 | } | |
405 | ||
406 | static int if_chars_in_buffer(struct tty_struct *tty) | |
407 | { | |
408 | struct cardstate *cs; | |
a4304f2d | 409 | int retval = 0; |
ee8a4b7f HL |
410 | |
411 | cs = (struct cardstate *) tty->driver_data; | |
412 | if (!cs) { | |
c8770dca | 413 | pr_err("%s: no cardstate\n", __func__); |
a4304f2d | 414 | return 0; |
ee8a4b7f HL |
415 | } |
416 | ||
784d5858 | 417 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
ee8a4b7f | 418 | |
a4304f2d | 419 | mutex_lock(&cs->mutex); |
ee8a4b7f | 420 | |
a4304f2d | 421 | if (!cs->connected) |
51370e5b | 422 | gig_dbg(DEBUG_IF, "not connected"); |
a4304f2d | 423 | else if (!cs->open_count) |
5002779d | 424 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
a4304f2d | 425 | else if (cs->mstate != MS_LOCKED) |
5002779d | 426 | dev_warn(cs->dev, "can't write to unlocked device\n"); |
a4304f2d | 427 | else |
ee8a4b7f HL |
428 | retval = cs->ops->chars_in_buffer(cs); |
429 | ||
abfd1dc7 | 430 | mutex_unlock(&cs->mutex); |
ee8a4b7f HL |
431 | |
432 | return retval; | |
433 | } | |
434 | ||
435 | static void if_throttle(struct tty_struct *tty) | |
436 | { | |
437 | struct cardstate *cs; | |
438 | ||
439 | cs = (struct cardstate *) tty->driver_data; | |
440 | if (!cs) { | |
c8770dca | 441 | pr_err("%s: no cardstate\n", __func__); |
ee8a4b7f HL |
442 | return; |
443 | } | |
444 | ||
784d5858 | 445 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
ee8a4b7f | 446 | |
abfd1dc7 | 447 | mutex_lock(&cs->mutex); |
ee8a4b7f | 448 | |
51370e5b TS |
449 | if (!cs->connected) |
450 | gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ | |
451 | else if (!cs->open_count) | |
5002779d | 452 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
d9ba9c91 | 453 | else |
1528b18f | 454 | gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__); |
ee8a4b7f | 455 | |
abfd1dc7 | 456 | mutex_unlock(&cs->mutex); |
ee8a4b7f HL |
457 | } |
458 | ||
459 | static void if_unthrottle(struct tty_struct *tty) | |
460 | { | |
461 | struct cardstate *cs; | |
462 | ||
463 | cs = (struct cardstate *) tty->driver_data; | |
464 | if (!cs) { | |
c8770dca | 465 | pr_err("%s: no cardstate\n", __func__); |
ee8a4b7f HL |
466 | return; |
467 | } | |
468 | ||
784d5858 | 469 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
ee8a4b7f | 470 | |
abfd1dc7 | 471 | mutex_lock(&cs->mutex); |
ee8a4b7f | 472 | |
51370e5b TS |
473 | if (!cs->connected) |
474 | gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */ | |
475 | else if (!cs->open_count) | |
5002779d | 476 | dev_warn(cs->dev, "%s: device not opened\n", __func__); |
d9ba9c91 | 477 | else |
1528b18f | 478 | gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__); |
ee8a4b7f | 479 | |
abfd1dc7 | 480 | mutex_unlock(&cs->mutex); |
ee8a4b7f HL |
481 | } |
482 | ||
606d099c | 483 | static void if_set_termios(struct tty_struct *tty, struct ktermios *old) |
ee8a4b7f HL |
484 | { |
485 | struct cardstate *cs; | |
486 | unsigned int iflag; | |
487 | unsigned int cflag; | |
488 | unsigned int old_cflag; | |
489 | unsigned int control_state, new_state; | |
490 | ||
491 | cs = (struct cardstate *) tty->driver_data; | |
492 | if (!cs) { | |
c8770dca | 493 | pr_err("%s: no cardstate\n", __func__); |
ee8a4b7f HL |
494 | return; |
495 | } | |
496 | ||
784d5858 | 497 | gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__); |
ee8a4b7f | 498 | |
abfd1dc7 | 499 | mutex_lock(&cs->mutex); |
ee8a4b7f | 500 | |
51370e5b TS |
501 | if (!cs->connected) { |
502 | gig_dbg(DEBUG_IF, "not connected"); | |
ee8a4b7f HL |
503 | goto out; |
504 | } | |
505 | ||
51370e5b TS |
506 | if (!cs->open_count) { |
507 | dev_warn(cs->dev, "%s: device not opened\n", __func__); | |
ee8a4b7f HL |
508 | goto out; |
509 | } | |
510 | ||
ee8a4b7f HL |
511 | iflag = tty->termios->c_iflag; |
512 | cflag = tty->termios->c_cflag; | |
d9ba9c91 | 513 | old_cflag = old ? old->c_cflag : cflag; |
784d5858 TS |
514 | gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x", |
515 | cs->minor_index, iflag, cflag, old_cflag); | |
ee8a4b7f HL |
516 | |
517 | /* get a local copy of the current port settings */ | |
518 | control_state = cs->control_state; | |
519 | ||
520 | /* | |
521 | * Update baud rate. | |
522 | * Do not attempt to cache old rates and skip settings, | |
523 | * disconnects screw such tricks up completely. | |
524 | * Premature optimization is the root of all evil. | |
525 | */ | |
526 | ||
784d5858 | 527 | /* reassert DTR and (maybe) RTS on transition from B0 */ |
ee8a4b7f HL |
528 | if ((old_cflag & CBAUD) == B0) { |
529 | new_state = control_state | TIOCM_DTR; | |
530 | /* don't set RTS if using hardware flow control */ | |
531 | if (!(old_cflag & CRTSCTS)) | |
532 | new_state |= TIOCM_RTS; | |
784d5858 TS |
533 | gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s", |
534 | cs->minor_index, | |
535 | (new_state & TIOCM_RTS) ? " only" : "/RTS"); | |
ee8a4b7f HL |
536 | cs->ops->set_modem_ctrl(cs, control_state, new_state); |
537 | control_state = new_state; | |
538 | } | |
539 | ||
540 | cs->ops->baud_rate(cs, cflag & CBAUD); | |
541 | ||
542 | if ((cflag & CBAUD) == B0) { | |
543 | /* Drop RTS and DTR */ | |
784d5858 | 544 | gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index); |
ee8a4b7f HL |
545 | new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS); |
546 | cs->ops->set_modem_ctrl(cs, control_state, new_state); | |
547 | control_state = new_state; | |
548 | } | |
549 | ||
550 | /* | |
551 | * Update line control register (LCR) | |
552 | */ | |
553 | ||
554 | cs->ops->set_line_ctrl(cs, cflag); | |
555 | ||
ee8a4b7f HL |
556 | /* save off the modified port settings */ |
557 | cs->control_state = control_state; | |
558 | ||
559 | out: | |
abfd1dc7 | 560 | mutex_unlock(&cs->mutex); |
ee8a4b7f HL |
561 | } |
562 | ||
563 | ||
564 | /* wakeup tasklet for the write operation */ | |
565 | static void if_wake(unsigned long data) | |
566 | { | |
567 | struct cardstate *cs = (struct cardstate *) data; | |
ee8a4b7f | 568 | |
b963a844 JS |
569 | if (cs->tty) |
570 | tty_wakeup(cs->tty); | |
ee8a4b7f HL |
571 | } |
572 | ||
573 | /*** interface to common ***/ | |
574 | ||
575 | void gigaset_if_init(struct cardstate *cs) | |
576 | { | |
577 | struct gigaset_driver *drv; | |
578 | ||
579 | drv = cs->driver; | |
580 | if (!drv->have_tty) | |
581 | return; | |
582 | ||
5452fee2 | 583 | tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs); |
7435f50e TS |
584 | |
585 | mutex_lock(&cs->mutex); | |
01107d34 | 586 | cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL); |
3dda4e37 | 587 | |
01107d34 GKH |
588 | if (!IS_ERR(cs->tty_dev)) |
589 | dev_set_drvdata(cs->tty_dev, cs); | |
3dda4e37 | 590 | else { |
42e3d611 | 591 | pr_warning("could not register device to the tty subsystem\n"); |
01107d34 | 592 | cs->tty_dev = NULL; |
3dda4e37 | 593 | } |
7435f50e | 594 | mutex_unlock(&cs->mutex); |
ee8a4b7f HL |
595 | } |
596 | ||
597 | void gigaset_if_free(struct cardstate *cs) | |
598 | { | |
599 | struct gigaset_driver *drv; | |
600 | ||
601 | drv = cs->driver; | |
602 | if (!drv->have_tty) | |
603 | return; | |
604 | ||
605 | tasklet_disable(&cs->if_wake_tasklet); | |
606 | tasklet_kill(&cs->if_wake_tasklet); | |
01107d34 | 607 | cs->tty_dev = NULL; |
ee8a4b7f HL |
608 | tty_unregister_device(drv->tty, cs->minor_index); |
609 | } | |
610 | ||
1cec9727 TS |
611 | /** |
612 | * gigaset_if_receive() - pass a received block of data to the tty device | |
613 | * @cs: device descriptor structure. | |
614 | * @buffer: received data. | |
615 | * @len: number of bytes received. | |
616 | * | |
617 | * Called by asyncdata/isocdata if a block of data received from the | |
618 | * device must be sent to userspace through the ttyG* device. | |
619 | */ | |
ee8a4b7f | 620 | void gigaset_if_receive(struct cardstate *cs, |
784d5858 | 621 | unsigned char *buffer, size_t len) |
ee8a4b7f HL |
622 | { |
623 | unsigned long flags; | |
624 | struct tty_struct *tty; | |
625 | ||
626 | spin_lock_irqsave(&cs->lock, flags); | |
d9ba9c91 TS |
627 | tty = cs->tty; |
628 | if (tty == NULL) | |
1528b18f | 629 | gig_dbg(DEBUG_IF, "receive on closed device"); |
ee8a4b7f HL |
630 | else { |
631 | tty_buffer_request_room(tty, len); | |
632 | tty_insert_flip_string(tty, buffer, len); | |
633 | tty_flip_buffer_push(tty); | |
634 | } | |
635 | spin_unlock_irqrestore(&cs->lock, flags); | |
636 | } | |
637 | EXPORT_SYMBOL_GPL(gigaset_if_receive); | |
638 | ||
639 | /* gigaset_if_initdriver | |
640 | * Initialize tty interface. | |
641 | * parameters: | |
784d5858 TS |
642 | * drv Driver |
643 | * procname Name of the driver (e.g. for /proc/tty/drivers) | |
644 | * devname Name of the device files (prefix without minor number) | |
ee8a4b7f HL |
645 | */ |
646 | void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname, | |
f4eaa370 | 647 | const char *devname) |
ee8a4b7f HL |
648 | { |
649 | unsigned minors = drv->minors; | |
650 | int ret; | |
651 | struct tty_driver *tty; | |
652 | ||
653 | drv->have_tty = 0; | |
654 | ||
d9ba9c91 TS |
655 | drv->tty = tty = alloc_tty_driver(minors); |
656 | if (tty == NULL) | |
ee8a4b7f | 657 | goto enomem; |
ee8a4b7f HL |
658 | |
659 | tty->magic = TTY_DRIVER_MAGIC, | |
660 | tty->major = GIG_MAJOR, | |
661 | tty->type = TTY_DRIVER_TYPE_SERIAL, | |
662 | tty->subtype = SERIAL_TYPE_NORMAL, | |
331b8319 | 663 | tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
ee8a4b7f HL |
664 | |
665 | tty->driver_name = procname; | |
666 | tty->name = devname; | |
667 | tty->minor_start = drv->minor; | |
668 | tty->num = drv->minors; | |
669 | ||
670 | tty->owner = THIS_MODULE; | |
ee8a4b7f | 671 | |
d9ba9c91 TS |
672 | tty->init_termios = tty_std_termios; |
673 | tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; | |
ee8a4b7f HL |
674 | tty_set_operations(tty, &if_ops); |
675 | ||
676 | ret = tty_register_driver(tty); | |
677 | if (ret < 0) { | |
c8770dca | 678 | pr_err("error %d registering tty driver\n", ret); |
ee8a4b7f HL |
679 | goto error; |
680 | } | |
784d5858 | 681 | gig_dbg(DEBUG_IF, "tty driver initialized"); |
ee8a4b7f HL |
682 | drv->have_tty = 1; |
683 | return; | |
684 | ||
685 | enomem: | |
c8770dca | 686 | pr_err("out of memory\n"); |
ee8a4b7f HL |
687 | error: |
688 | if (drv->tty) | |
689 | put_tty_driver(drv->tty); | |
690 | } | |
691 | ||
692 | void gigaset_if_freedriver(struct gigaset_driver *drv) | |
693 | { | |
694 | if (!drv->have_tty) | |
695 | return; | |
696 | ||
697 | drv->have_tty = 0; | |
698 | tty_unregister_driver(drv->tty); | |
699 | put_tty_driver(drv->tty); | |
700 | } |