blob: 59de638225fe4b30b71bcddc1cdf4ff8b50a4fa5 [file] [log] [blame]
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -08001/*
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 * =====================================================================
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080012 */
13
14#include "gigaset.h"
15#include <linux/gigaset_dev.h>
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080016#include <linux/tty_flip.h>
17
18/*** our ioctls ***/
19
20static int if_lock(struct cardstate *cs, int *arg)
21{
22 int cmd = *arg;
23
Tilman Schmidt784d5852006-04-10 22:55:04 -070024 gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080025
26 if (cmd > 1)
27 return -EINVAL;
28
29 if (cmd < 0) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -080030 *arg = cs->mstate == MS_LOCKED;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080031 return 0;
32 }
33
Tilman Schmidt9d4bee22008-02-06 01:38:28 -080034 if (!cmd && cs->mstate == MS_LOCKED && cs->connected) {
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080035 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
36 cs->ops->baud_rate(cs, B115200);
37 cs->ops->set_line_ctrl(cs, CS8);
38 cs->control_state = TIOCM_DTR|TIOCM_RTS;
39 }
40
41 cs->waiting = 1;
42 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
Tilman Schmidt784d5852006-04-10 22:55:04 -070043 NULL, cmd, NULL)) {
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080044 cs->waiting = 0;
45 return -ENOMEM;
46 }
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080047 gigaset_schedule_event(cs);
48
49 wait_event(cs->waitqueue, !cs->waiting);
50
51 if (cs->cmd_result >= 0) {
52 *arg = cs->cmd_result;
53 return 0;
54 }
55
56 return cs->cmd_result;
57}
58
59static int if_version(struct cardstate *cs, unsigned arg[4])
60{
61 static const unsigned version[4] = GIG_VERSION;
62 static const unsigned compat[4] = GIG_COMPAT;
63 unsigned cmd = arg[0];
64
Tilman Schmidt784d5852006-04-10 22:55:04 -070065 gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080066
67 switch (cmd) {
68 case GIGVER_DRIVER:
69 memcpy(arg, version, sizeof version);
70 return 0;
71 case GIGVER_COMPAT:
72 memcpy(arg, compat, sizeof compat);
73 return 0;
74 case GIGVER_FWBASE:
75 cs->waiting = 1;
76 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_VER,
Tilman Schmidt784d5852006-04-10 22:55:04 -070077 NULL, 0, arg)) {
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080078 cs->waiting = 0;
79 return -ENOMEM;
80 }
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080081 gigaset_schedule_event(cs);
82
83 wait_event(cs->waitqueue, !cs->waiting);
84
85 if (cs->cmd_result >= 0)
86 return 0;
87
88 return cs->cmd_result;
89 default:
90 return -EINVAL;
91 }
92}
93
94static int if_config(struct cardstate *cs, int *arg)
95{
Tilman Schmidt784d5852006-04-10 22:55:04 -070096 gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080097
98 if (*arg != 1)
99 return -EINVAL;
100
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800101 if (cs->mstate != MS_LOCKED)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800102 return -EBUSY;
103
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700104 if (!cs->connected) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800105 pr_err("%s: not connected\n", __func__);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700106 return -ENODEV;
107 }
108
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800109 *arg = 0;
110 return gigaset_enterconfigmode(cs);
111}
112
113/*** the terminal driver ***/
114/* stolen from usbserial and some other tty drivers */
115
116static int if_open(struct tty_struct *tty, struct file *filp);
117static void if_close(struct tty_struct *tty, struct file *filp);
Alan Cox6caa76b2011-02-14 16:27:22 +0000118static int if_ioctl(struct tty_struct *tty,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700119 unsigned int cmd, unsigned long arg);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800120static int if_write_room(struct tty_struct *tty);
121static int if_chars_in_buffer(struct tty_struct *tty);
122static void if_throttle(struct tty_struct *tty);
123static void if_unthrottle(struct tty_struct *tty);
Alan Cox606d0992006-12-08 02:38:45 -0800124static void if_set_termios(struct tty_struct *tty, struct ktermios *old);
Alan Cox60b33c12011-02-14 16:26:14 +0000125static int if_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000126static int if_tiocmset(struct tty_struct *tty,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700127 unsigned int set, unsigned int clear);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800128static int if_write(struct tty_struct *tty,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700129 const unsigned char *buf, int count);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800130
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700131static const struct tty_operations if_ops = {
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800132 .open = if_open,
133 .close = if_close,
134 .ioctl = if_ioctl,
135 .write = if_write,
136 .write_room = if_write_room,
137 .chars_in_buffer = if_chars_in_buffer,
138 .set_termios = if_set_termios,
139 .throttle = if_throttle,
140 .unthrottle = if_unthrottle,
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800141 .tiocmget = if_tiocmget,
142 .tiocmset = if_tiocmset,
143};
144
145static int if_open(struct tty_struct *tty, struct file *filp)
146{
147 struct cardstate *cs;
148 unsigned long flags;
149
Tilman Schmidt784d5852006-04-10 22:55:04 -0700150 gig_dbg(DEBUG_IF, "%d+%d: %s()",
151 tty->driver->minor_start, tty->index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800152
153 tty->driver_data = NULL;
154
155 cs = gigaset_get_cs_by_tty(tty);
Tilman Schmidte468c042008-02-06 01:38:29 -0800156 if (!cs || !try_module_get(cs->driver->owner))
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800157 return -ENODEV;
158
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700159 if (mutex_lock_interruptible(&cs->mutex))
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000160 return -ERESTARTSYS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800161 tty->driver_data = cs;
162
163 ++cs->open_count;
164
165 if (cs->open_count == 1) {
166 spin_lock_irqsave(&cs->lock, flags);
167 cs->tty = tty;
168 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000169 tty->low_latency = 1;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800170 }
171
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700172 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800173 return 0;
174}
175
176static void if_close(struct tty_struct *tty, struct file *filp)
177{
178 struct cardstate *cs;
179 unsigned long flags;
180
181 cs = (struct cardstate *) tty->driver_data;
182 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800183 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800184 return;
185 }
186
Tilman Schmidt784d5852006-04-10 22:55:04 -0700187 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800188
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700189 mutex_lock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800190
Tilman Schmidt51370e52009-04-05 06:39:33 +0000191 if (!cs->connected)
192 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
193 else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700194 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800195 else {
196 if (!--cs->open_count) {
197 spin_lock_irqsave(&cs->lock, flags);
198 cs->tty = NULL;
199 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800200 }
201 }
202
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700203 mutex_unlock(&cs->mutex);
Tilman Schmidte468c042008-02-06 01:38:29 -0800204
205 module_put(cs->driver->owner);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800206}
207
Alan Cox6caa76b2011-02-14 16:27:22 +0000208static int if_ioctl(struct tty_struct *tty,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700209 unsigned int cmd, unsigned long arg)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800210{
211 struct cardstate *cs;
212 int retval = -ENODEV;
213 int int_arg;
214 unsigned char buf[6];
215 unsigned version[4];
216
217 cs = (struct cardstate *) tty->driver_data;
218 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800219 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800220 return -ENODEV;
221 }
222
Tilman Schmidt784d5852006-04-10 22:55:04 -0700223 gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800224
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700225 if (mutex_lock_interruptible(&cs->mutex))
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000226 return -ERESTARTSYS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800227
Tilman Schmidt51370e52009-04-05 06:39:33 +0000228 if (!cs->connected) {
229 gig_dbg(DEBUG_IF, "not connected");
230 retval = -ENODEV;
231 } else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700232 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800233 else {
234 retval = 0;
235 switch (cmd) {
236 case GIGASET_REDIR:
237 retval = get_user(int_arg, (int __user *) arg);
238 if (retval >= 0)
239 retval = if_lock(cs, &int_arg);
240 if (retval >= 0)
241 retval = put_user(int_arg, (int __user *) arg);
242 break;
243 case GIGASET_CONFIG:
244 retval = get_user(int_arg, (int __user *) arg);
245 if (retval >= 0)
246 retval = if_config(cs, &int_arg);
247 if (retval >= 0)
248 retval = put_user(int_arg, (int __user *) arg);
249 break;
250 case GIGASET_BRKCHARS:
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800251 retval = copy_from_user(&buf,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700252 (const unsigned char __user *) arg, 6)
253 ? -EFAULT : 0;
Tilman Schmidt01371502006-04-10 22:55:11 -0700254 if (retval >= 0) {
255 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
256 6, (const unsigned char *) arg);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800257 retval = cs->ops->brkchars(cs, buf);
Tilman Schmidt01371502006-04-10 22:55:11 -0700258 }
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800259 break;
260 case GIGASET_VERSION:
Tilman Schmidt917f5082006-04-10 22:55:00 -0700261 retval = copy_from_user(version,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700262 (unsigned __user *) arg, sizeof version)
263 ? -EFAULT : 0;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800264 if (retval >= 0)
265 retval = if_version(cs, version);
266 if (retval >= 0)
Tilman Schmidt917f5082006-04-10 22:55:00 -0700267 retval = copy_to_user((unsigned __user *) arg,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700268 version, sizeof version)
269 ? -EFAULT : 0;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800270 break;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700271 default:
Tilman Schmidt1528b182010-02-22 13:09:52 +0000272 gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700273 __func__, cmd);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800274 retval = -ENOIOCTLCMD;
275 }
276 }
277
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700278 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800279
280 return retval;
281}
282
Alan Cox60b33c12011-02-14 16:26:14 +0000283static int if_tiocmget(struct tty_struct *tty)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800284{
285 struct cardstate *cs;
286 int retval;
287
288 cs = (struct cardstate *) tty->driver_data;
289 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800290 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800291 return -ENODEV;
292 }
293
Tilman Schmidt784d5852006-04-10 22:55:04 -0700294 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800295
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700296 if (mutex_lock_interruptible(&cs->mutex))
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000297 return -ERESTARTSYS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800298
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800299 retval = cs->control_state & (TIOCM_RTS|TIOCM_DTR);
300
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700301 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800302
303 return retval;
304}
305
Alan Cox20b9d172011-02-14 16:26:50 +0000306static int if_tiocmset(struct tty_struct *tty,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700307 unsigned int set, unsigned int clear)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800308{
309 struct cardstate *cs;
310 int retval;
311 unsigned mc;
312
313 cs = (struct cardstate *) tty->driver_data;
314 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800315 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800316 return -ENODEV;
317 }
318
Tilman Schmidt784d5852006-04-10 22:55:04 -0700319 gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
320 cs->minor_index, __func__, set, clear);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800321
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700322 if (mutex_lock_interruptible(&cs->mutex))
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000323 return -ERESTARTSYS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800324
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700325 if (!cs->connected) {
Tilman Schmidt51370e52009-04-05 06:39:33 +0000326 gig_dbg(DEBUG_IF, "not connected");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800327 retval = -ENODEV;
328 } else {
329 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS|TIOCM_DTR);
330 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
331 cs->control_state = mc;
332 }
333
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700334 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800335
336 return retval;
337}
338
339static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
340{
341 struct cardstate *cs;
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000342 struct cmdbuf_t *cb;
343 int retval;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800344
345 cs = (struct cardstate *) tty->driver_data;
346 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800347 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800348 return -ENODEV;
349 }
350
Tilman Schmidt784d5852006-04-10 22:55:04 -0700351 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800352
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700353 if (mutex_lock_interruptible(&cs->mutex))
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000354 return -ERESTARTSYS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800355
Tilman Schmidt51370e52009-04-05 06:39:33 +0000356 if (!cs->connected) {
357 gig_dbg(DEBUG_IF, "not connected");
358 retval = -ENODEV;
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000359 goto done;
360 }
361 if (!cs->open_count) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700362 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000363 retval = -ENODEV;
364 goto done;
365 }
366 if (cs->mstate != MS_LOCKED) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700367 dev_warn(cs->dev, "can't write to unlocked device\n");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800368 retval = -EBUSY;
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000369 goto done;
370 }
371 if (count <= 0) {
372 /* nothing to do */
373 retval = 0;
374 goto done;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800375 }
376
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000377 cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);
378 if (!cb) {
379 dev_err(cs->dev, "%s: out of memory\n", __func__);
380 retval = -ENOMEM;
381 goto done;
382 }
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800383
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000384 memcpy(cb->buf, buf, count);
385 cb->len = count;
386 cb->offset = 0;
387 cb->next = NULL;
388 cb->wake_tasklet = &cs->if_wake_tasklet;
389 retval = cs->ops->write_cmd(cs, cb);
390done:
391 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800392 return retval;
393}
394
395static int if_write_room(struct tty_struct *tty)
396{
397 struct cardstate *cs;
398 int retval = -ENODEV;
399
400 cs = (struct cardstate *) tty->driver_data;
401 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800402 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800403 return -ENODEV;
404 }
405
Tilman Schmidt784d5852006-04-10 22:55:04 -0700406 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800407
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700408 if (mutex_lock_interruptible(&cs->mutex))
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000409 return -ERESTARTSYS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800410
Tilman Schmidt51370e52009-04-05 06:39:33 +0000411 if (!cs->connected) {
412 gig_dbg(DEBUG_IF, "not connected");
413 retval = -ENODEV;
414 } else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700415 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800416 else if (cs->mstate != MS_LOCKED) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700417 dev_warn(cs->dev, "can't write to unlocked device\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800418 retval = -EBUSY;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800419 } else
420 retval = cs->ops->write_room(cs);
421
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700422 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800423
424 return retval;
425}
426
427static int if_chars_in_buffer(struct tty_struct *tty)
428{
429 struct cardstate *cs;
Tilman Schmidta4304f22009-08-06 20:33:33 -0700430 int retval = 0;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800431
432 cs = (struct cardstate *) tty->driver_data;
433 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800434 pr_err("%s: no cardstate\n", __func__);
Tilman Schmidta4304f22009-08-06 20:33:33 -0700435 return 0;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800436 }
437
Tilman Schmidt784d5852006-04-10 22:55:04 -0700438 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800439
Tilman Schmidta4304f22009-08-06 20:33:33 -0700440 mutex_lock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800441
Tilman Schmidta4304f22009-08-06 20:33:33 -0700442 if (!cs->connected)
Tilman Schmidt51370e52009-04-05 06:39:33 +0000443 gig_dbg(DEBUG_IF, "not connected");
Tilman Schmidta4304f22009-08-06 20:33:33 -0700444 else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700445 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Tilman Schmidta4304f22009-08-06 20:33:33 -0700446 else if (cs->mstate != MS_LOCKED)
Tilman Schmidt50027792008-07-23 21:28:27 -0700447 dev_warn(cs->dev, "can't write to unlocked device\n");
Tilman Schmidta4304f22009-08-06 20:33:33 -0700448 else
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800449 retval = cs->ops->chars_in_buffer(cs);
450
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700451 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800452
453 return retval;
454}
455
456static void if_throttle(struct tty_struct *tty)
457{
458 struct cardstate *cs;
459
460 cs = (struct cardstate *) tty->driver_data;
461 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800462 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800463 return;
464 }
465
Tilman Schmidt784d5852006-04-10 22:55:04 -0700466 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800467
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700468 mutex_lock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800469
Tilman Schmidt51370e52009-04-05 06:39:33 +0000470 if (!cs->connected)
471 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
472 else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700473 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000474 else
Tilman Schmidt1528b182010-02-22 13:09:52 +0000475 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800476
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700477 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800478}
479
480static void if_unthrottle(struct tty_struct *tty)
481{
482 struct cardstate *cs;
483
484 cs = (struct cardstate *) tty->driver_data;
485 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800486 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800487 return;
488 }
489
Tilman Schmidt784d5852006-04-10 22:55:04 -0700490 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800491
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700492 mutex_lock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800493
Tilman Schmidt51370e52009-04-05 06:39:33 +0000494 if (!cs->connected)
495 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
496 else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700497 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000498 else
Tilman Schmidt1528b182010-02-22 13:09:52 +0000499 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800500
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700501 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800502}
503
Alan Cox606d0992006-12-08 02:38:45 -0800504static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800505{
506 struct cardstate *cs;
507 unsigned int iflag;
508 unsigned int cflag;
509 unsigned int old_cflag;
510 unsigned int control_state, new_state;
511
512 cs = (struct cardstate *) tty->driver_data;
513 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800514 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800515 return;
516 }
517
Tilman Schmidt784d5852006-04-10 22:55:04 -0700518 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800519
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700520 mutex_lock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800521
Tilman Schmidt51370e52009-04-05 06:39:33 +0000522 if (!cs->connected) {
523 gig_dbg(DEBUG_IF, "not connected");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800524 goto out;
525 }
526
Tilman Schmidt51370e52009-04-05 06:39:33 +0000527 if (!cs->open_count) {
528 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800529 goto out;
530 }
531
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800532 iflag = tty->termios->c_iflag;
533 cflag = tty->termios->c_cflag;
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000534 old_cflag = old ? old->c_cflag : cflag;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700535 gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
536 cs->minor_index, iflag, cflag, old_cflag);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800537
538 /* get a local copy of the current port settings */
539 control_state = cs->control_state;
540
541 /*
542 * Update baud rate.
543 * Do not attempt to cache old rates and skip settings,
544 * disconnects screw such tricks up completely.
545 * Premature optimization is the root of all evil.
546 */
547
Tilman Schmidt784d5852006-04-10 22:55:04 -0700548 /* reassert DTR and (maybe) RTS on transition from B0 */
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800549 if ((old_cflag & CBAUD) == B0) {
550 new_state = control_state | TIOCM_DTR;
551 /* don't set RTS if using hardware flow control */
552 if (!(old_cflag & CRTSCTS))
553 new_state |= TIOCM_RTS;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700554 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
555 cs->minor_index,
556 (new_state & TIOCM_RTS) ? " only" : "/RTS");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800557 cs->ops->set_modem_ctrl(cs, control_state, new_state);
558 control_state = new_state;
559 }
560
561 cs->ops->baud_rate(cs, cflag & CBAUD);
562
563 if ((cflag & CBAUD) == B0) {
564 /* Drop RTS and DTR */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700565 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800566 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
567 cs->ops->set_modem_ctrl(cs, control_state, new_state);
568 control_state = new_state;
569 }
570
571 /*
572 * Update line control register (LCR)
573 */
574
575 cs->ops->set_line_ctrl(cs, cflag);
576
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800577 /* save off the modified port settings */
578 cs->control_state = control_state;
579
580out:
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700581 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800582}
583
584
585/* wakeup tasklet for the write operation */
586static void if_wake(unsigned long data)
587{
588 struct cardstate *cs = (struct cardstate *) data;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800589
Jiri Slabyb963a842007-02-10 01:44:55 -0800590 if (cs->tty)
591 tty_wakeup(cs->tty);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800592}
593
594/*** interface to common ***/
595
596void gigaset_if_init(struct cardstate *cs)
597{
598 struct gigaset_driver *drv;
599
600 drv = cs->driver;
601 if (!drv->have_tty)
602 return;
603
Joe Perches5452fee2009-11-19 09:55:53 +0000604 tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
Tilman Schmidt7435f502007-02-12 00:52:24 -0800605
606 mutex_lock(&cs->mutex);
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -0700607 cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL);
Hansjoerg Lipp3dda4e32006-04-22 18:43:00 +0200608
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -0700609 if (!IS_ERR(cs->tty_dev))
610 dev_set_drvdata(cs->tty_dev, cs);
Hansjoerg Lipp3dda4e32006-04-22 18:43:00 +0200611 else {
Tilman Schmidt42e3d612009-05-13 12:44:17 +0000612 pr_warning("could not register device to the tty subsystem\n");
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -0700613 cs->tty_dev = NULL;
Hansjoerg Lipp3dda4e32006-04-22 18:43:00 +0200614 }
Tilman Schmidt7435f502007-02-12 00:52:24 -0800615 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800616}
617
618void gigaset_if_free(struct cardstate *cs)
619{
620 struct gigaset_driver *drv;
621
622 drv = cs->driver;
623 if (!drv->have_tty)
624 return;
625
626 tasklet_disable(&cs->if_wake_tasklet);
627 tasklet_kill(&cs->if_wake_tasklet);
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -0700628 cs->tty_dev = NULL;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800629 tty_unregister_device(drv->tty, cs->minor_index);
630}
631
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000632/**
633 * gigaset_if_receive() - pass a received block of data to the tty device
634 * @cs: device descriptor structure.
635 * @buffer: received data.
636 * @len: number of bytes received.
637 *
638 * Called by asyncdata/isocdata if a block of data received from the
639 * device must be sent to userspace through the ttyG* device.
640 */
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800641void gigaset_if_receive(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700642 unsigned char *buffer, size_t len)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800643{
644 unsigned long flags;
645 struct tty_struct *tty;
646
647 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000648 tty = cs->tty;
649 if (tty == NULL)
Tilman Schmidt1528b182010-02-22 13:09:52 +0000650 gig_dbg(DEBUG_IF, "receive on closed device");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800651 else {
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800652 tty_insert_flip_string(tty, buffer, len);
653 tty_flip_buffer_push(tty);
654 }
655 spin_unlock_irqrestore(&cs->lock, flags);
656}
657EXPORT_SYMBOL_GPL(gigaset_if_receive);
658
659/* gigaset_if_initdriver
660 * Initialize tty interface.
661 * parameters:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700662 * drv Driver
663 * procname Name of the driver (e.g. for /proc/tty/drivers)
664 * devname Name of the device files (prefix without minor number)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800665 */
666void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
Greg Kroah-Hartmanf4eaa372005-06-20 21:15:16 -0700667 const char *devname)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800668{
669 unsigned minors = drv->minors;
670 int ret;
671 struct tty_driver *tty;
672
673 drv->have_tty = 0;
674
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000675 drv->tty = tty = alloc_tty_driver(minors);
676 if (tty == NULL)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800677 goto enomem;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800678
679 tty->magic = TTY_DRIVER_MAGIC,
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800680 tty->type = TTY_DRIVER_TYPE_SERIAL,
681 tty->subtype = SERIAL_TYPE_NORMAL,
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -0700682 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800683
684 tty->driver_name = procname;
685 tty->name = devname;
686 tty->minor_start = drv->minor;
687 tty->num = drv->minors;
688
689 tty->owner = THIS_MODULE;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800690
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000691 tty->init_termios = tty_std_termios;
692 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800693 tty_set_operations(tty, &if_ops);
694
695 ret = tty_register_driver(tty);
696 if (ret < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800697 pr_err("error %d registering tty driver\n", ret);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800698 goto error;
699 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700700 gig_dbg(DEBUG_IF, "tty driver initialized");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800701 drv->have_tty = 1;
702 return;
703
704enomem:
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800705 pr_err("out of memory\n");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800706error:
707 if (drv->tty)
708 put_tty_driver(drv->tty);
709}
710
711void gigaset_if_freedriver(struct gigaset_driver *drv)
712{
713 if (!drv->have_tty)
714 return;
715
716 drv->have_tty = 0;
717 tty_unregister_driver(drv->tty);
718 put_tty_driver(drv->tty);
719}