blob: da2486e5910282fea84a49f042af1a3db55ceded [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>
Paul Gortmaker07a97fe2011-08-30 12:08:51 -040017#include <linux/module.h>
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080018
19/*** our ioctls ***/
20
21static int if_lock(struct cardstate *cs, int *arg)
22{
23 int cmd = *arg;
24
Tilman Schmidt784d5852006-04-10 22:55:04 -070025 gig_dbg(DEBUG_IF, "%u: if_lock (%d)", cs->minor_index, cmd);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080026
27 if (cmd > 1)
28 return -EINVAL;
29
30 if (cmd < 0) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -080031 *arg = cs->mstate == MS_LOCKED;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080032 return 0;
33 }
34
Tilman Schmidt9d4bee22008-02-06 01:38:28 -080035 if (!cmd && cs->mstate == MS_LOCKED && cs->connected) {
Joe Perches475be4d2012-02-19 19:52:38 -080036 cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080037 cs->ops->baud_rate(cs, B115200);
38 cs->ops->set_line_ctrl(cs, CS8);
Joe Perches475be4d2012-02-19 19:52:38 -080039 cs->control_state = TIOCM_DTR | TIOCM_RTS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080040 }
41
42 cs->waiting = 1;
43 if (!gigaset_add_event(cs, &cs->at_state, EV_IF_LOCK,
Tilman Schmidt784d5852006-04-10 22:55:04 -070044 NULL, cmd, NULL)) {
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080045 cs->waiting = 0;
46 return -ENOMEM;
47 }
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080048 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
60static 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
Tilman Schmidt784d5852006-04-10 22:55:04 -070066 gig_dbg(DEBUG_IF, "%u: if_version (%d)", cs->minor_index, cmd);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080067
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,
Tilman Schmidt784d5852006-04-10 22:55:04 -070078 NULL, 0, arg)) {
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080079 cs->waiting = 0;
80 return -ENOMEM;
81 }
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080082 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
95static int if_config(struct cardstate *cs, int *arg)
96{
Tilman Schmidt784d5852006-04-10 22:55:04 -070097 gig_dbg(DEBUG_IF, "%u: if_config (%d)", cs->minor_index, *arg);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -080098
99 if (*arg != 1)
100 return -EINVAL;
101
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800102 if (cs->mstate != MS_LOCKED)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800103 return -EBUSY;
104
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700105 if (!cs->connected) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800106 pr_err("%s: not connected\n", __func__);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700107 return -ENODEV;
108 }
109
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800110 *arg = 0;
111 return gigaset_enterconfigmode(cs);
112}
113
114/*** the terminal driver ***/
115/* stolen from usbserial and some other tty drivers */
116
117static int if_open(struct tty_struct *tty, struct file *filp);
118static void if_close(struct tty_struct *tty, struct file *filp);
Alan Cox6caa76b2011-02-14 16:27:22 +0000119static int if_ioctl(struct tty_struct *tty,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700120 unsigned int cmd, unsigned long arg);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800121static int if_write_room(struct tty_struct *tty);
122static int if_chars_in_buffer(struct tty_struct *tty);
123static void if_throttle(struct tty_struct *tty);
124static void if_unthrottle(struct tty_struct *tty);
Alan Cox606d0992006-12-08 02:38:45 -0800125static void if_set_termios(struct tty_struct *tty, struct ktermios *old);
Alan Cox60b33c12011-02-14 16:26:14 +0000126static int if_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000127static int if_tiocmset(struct tty_struct *tty,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700128 unsigned int set, unsigned int clear);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800129static int if_write(struct tty_struct *tty,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700130 const unsigned char *buf, int count);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800131
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700132static const struct tty_operations if_ops = {
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800133 .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,
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800142 .tiocmget = if_tiocmget,
143 .tiocmset = if_tiocmset,
144};
145
146static int if_open(struct tty_struct *tty, struct file *filp)
147{
148 struct cardstate *cs;
149 unsigned long flags;
150
Tilman Schmidt784d5852006-04-10 22:55:04 -0700151 gig_dbg(DEBUG_IF, "%d+%d: %s()",
152 tty->driver->minor_start, tty->index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800153
154 tty->driver_data = NULL;
155
156 cs = gigaset_get_cs_by_tty(tty);
Tilman Schmidte468c042008-02-06 01:38:29 -0800157 if (!cs || !try_module_get(cs->driver->owner))
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800158 return -ENODEV;
159
Pavel Shved2f9381e2011-06-17 06:25:11 +0000160 if (mutex_lock_interruptible(&cs->mutex)) {
161 module_put(cs->driver->owner);
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000162 return -ERESTARTSYS;
Pavel Shved2f9381e2011-06-17 06:25:11 +0000163 }
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800164 tty->driver_data = cs;
165
166 ++cs->open_count;
167
168 if (cs->open_count == 1) {
169 spin_lock_irqsave(&cs->lock, flags);
170 cs->tty = tty;
171 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000172 tty->low_latency = 1;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800173 }
174
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700175 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800176 return 0;
177}
178
179static void if_close(struct tty_struct *tty, struct file *filp)
180{
181 struct cardstate *cs;
182 unsigned long flags;
183
184 cs = (struct cardstate *) tty->driver_data;
185 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800186 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800187 return;
188 }
189
Tilman Schmidt784d5852006-04-10 22:55:04 -0700190 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800191
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700192 mutex_lock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800193
Tilman Schmidt51370e52009-04-05 06:39:33 +0000194 if (!cs->connected)
195 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
196 else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700197 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800198 else {
199 if (!--cs->open_count) {
200 spin_lock_irqsave(&cs->lock, flags);
201 cs->tty = NULL;
202 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800203 }
204 }
205
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700206 mutex_unlock(&cs->mutex);
Tilman Schmidte468c042008-02-06 01:38:29 -0800207
208 module_put(cs->driver->owner);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800209}
210
Alan Cox6caa76b2011-02-14 16:27:22 +0000211static int if_ioctl(struct tty_struct *tty,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700212 unsigned int cmd, unsigned long arg)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800213{
214 struct cardstate *cs;
215 int retval = -ENODEV;
216 int int_arg;
217 unsigned char buf[6];
218 unsigned version[4];
219
220 cs = (struct cardstate *) tty->driver_data;
221 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800222 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800223 return -ENODEV;
224 }
225
Tilman Schmidt784d5852006-04-10 22:55:04 -0700226 gig_dbg(DEBUG_IF, "%u: %s(0x%x)", cs->minor_index, __func__, cmd);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800227
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700228 if (mutex_lock_interruptible(&cs->mutex))
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000229 return -ERESTARTSYS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800230
Tilman Schmidt51370e52009-04-05 06:39:33 +0000231 if (!cs->connected) {
232 gig_dbg(DEBUG_IF, "not connected");
233 retval = -ENODEV;
234 } else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700235 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800236 else {
237 retval = 0;
238 switch (cmd) {
239 case GIGASET_REDIR:
240 retval = get_user(int_arg, (int __user *) arg);
241 if (retval >= 0)
242 retval = if_lock(cs, &int_arg);
243 if (retval >= 0)
244 retval = put_user(int_arg, (int __user *) arg);
245 break;
246 case GIGASET_CONFIG:
247 retval = get_user(int_arg, (int __user *) arg);
248 if (retval >= 0)
249 retval = if_config(cs, &int_arg);
250 if (retval >= 0)
251 retval = put_user(int_arg, (int __user *) arg);
252 break;
253 case GIGASET_BRKCHARS:
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800254 retval = copy_from_user(&buf,
Joe Perches475be4d2012-02-19 19:52:38 -0800255 (const unsigned char __user *) arg, 6)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700256 ? -EFAULT : 0;
Tilman Schmidt01371502006-04-10 22:55:11 -0700257 if (retval >= 0) {
258 gigaset_dbg_buffer(DEBUG_IF, "GIGASET_BRKCHARS",
Joe Perches475be4d2012-02-19 19:52:38 -0800259 6, (const unsigned char *) arg);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800260 retval = cs->ops->brkchars(cs, buf);
Tilman Schmidt01371502006-04-10 22:55:11 -0700261 }
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800262 break;
263 case GIGASET_VERSION:
Tilman Schmidt917f5082006-04-10 22:55:00 -0700264 retval = copy_from_user(version,
Joe Perches475be4d2012-02-19 19:52:38 -0800265 (unsigned __user *) arg, sizeof version)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700266 ? -EFAULT : 0;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800267 if (retval >= 0)
268 retval = if_version(cs, version);
269 if (retval >= 0)
Tilman Schmidt917f5082006-04-10 22:55:00 -0700270 retval = copy_to_user((unsigned __user *) arg,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700271 version, sizeof version)
272 ? -EFAULT : 0;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800273 break;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700274 default:
Tilman Schmidt1528b182010-02-22 13:09:52 +0000275 gig_dbg(DEBUG_IF, "%s: arg not supported - 0x%04x",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700276 __func__, cmd);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800277 retval = -ENOIOCTLCMD;
278 }
279 }
280
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700281 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800282
283 return retval;
284}
285
Alan Cox60b33c12011-02-14 16:26:14 +0000286static int if_tiocmget(struct tty_struct *tty)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800287{
288 struct cardstate *cs;
289 int retval;
290
291 cs = (struct cardstate *) tty->driver_data;
292 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800293 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800294 return -ENODEV;
295 }
296
Tilman Schmidt784d5852006-04-10 22:55:04 -0700297 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800298
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700299 if (mutex_lock_interruptible(&cs->mutex))
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000300 return -ERESTARTSYS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800301
Joe Perches475be4d2012-02-19 19:52:38 -0800302 retval = cs->control_state & (TIOCM_RTS | TIOCM_DTR);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800303
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700304 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800305
306 return retval;
307}
308
Alan Cox20b9d172011-02-14 16:26:50 +0000309static int if_tiocmset(struct tty_struct *tty,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700310 unsigned int set, unsigned int clear)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800311{
312 struct cardstate *cs;
313 int retval;
314 unsigned mc;
315
316 cs = (struct cardstate *) tty->driver_data;
317 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800318 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800319 return -ENODEV;
320 }
321
Tilman Schmidt784d5852006-04-10 22:55:04 -0700322 gig_dbg(DEBUG_IF, "%u: %s(0x%x, 0x%x)",
323 cs->minor_index, __func__, set, clear);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800324
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700325 if (mutex_lock_interruptible(&cs->mutex))
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000326 return -ERESTARTSYS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800327
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700328 if (!cs->connected) {
Tilman Schmidt51370e52009-04-05 06:39:33 +0000329 gig_dbg(DEBUG_IF, "not connected");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800330 retval = -ENODEV;
331 } else {
Joe Perches475be4d2012-02-19 19:52:38 -0800332 mc = (cs->control_state | set) & ~clear & (TIOCM_RTS | TIOCM_DTR);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800333 retval = cs->ops->set_modem_ctrl(cs, cs->control_state, mc);
334 cs->control_state = mc;
335 }
336
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700337 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800338
339 return retval;
340}
341
342static int if_write(struct tty_struct *tty, const unsigned char *buf, int count)
343{
344 struct cardstate *cs;
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000345 struct cmdbuf_t *cb;
346 int retval;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800347
348 cs = (struct cardstate *) tty->driver_data;
349 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800350 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800351 return -ENODEV;
352 }
353
Tilman Schmidt784d5852006-04-10 22:55:04 -0700354 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800355
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700356 if (mutex_lock_interruptible(&cs->mutex))
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000357 return -ERESTARTSYS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800358
Tilman Schmidt51370e52009-04-05 06:39:33 +0000359 if (!cs->connected) {
360 gig_dbg(DEBUG_IF, "not connected");
361 retval = -ENODEV;
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000362 goto done;
363 }
364 if (!cs->open_count) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700365 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000366 retval = -ENODEV;
367 goto done;
368 }
369 if (cs->mstate != MS_LOCKED) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700370 dev_warn(cs->dev, "can't write to unlocked device\n");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800371 retval = -EBUSY;
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000372 goto done;
373 }
374 if (count <= 0) {
375 /* nothing to do */
376 retval = 0;
377 goto done;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800378 }
379
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000380 cb = kmalloc(sizeof(struct cmdbuf_t) + count, GFP_KERNEL);
381 if (!cb) {
382 dev_err(cs->dev, "%s: out of memory\n", __func__);
383 retval = -ENOMEM;
384 goto done;
385 }
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800386
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000387 memcpy(cb->buf, buf, count);
388 cb->len = count;
389 cb->offset = 0;
390 cb->next = NULL;
391 cb->wake_tasklet = &cs->if_wake_tasklet;
392 retval = cs->ops->write_cmd(cs, cb);
393done:
394 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800395 return retval;
396}
397
398static int if_write_room(struct tty_struct *tty)
399{
400 struct cardstate *cs;
401 int retval = -ENODEV;
402
403 cs = (struct cardstate *) tty->driver_data;
404 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800405 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800406 return -ENODEV;
407 }
408
Tilman Schmidt784d5852006-04-10 22:55:04 -0700409 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800410
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700411 if (mutex_lock_interruptible(&cs->mutex))
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000412 return -ERESTARTSYS;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800413
Tilman Schmidt51370e52009-04-05 06:39:33 +0000414 if (!cs->connected) {
415 gig_dbg(DEBUG_IF, "not connected");
416 retval = -ENODEV;
417 } else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700418 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800419 else if (cs->mstate != MS_LOCKED) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700420 dev_warn(cs->dev, "can't write to unlocked device\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800421 retval = -EBUSY;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800422 } else
423 retval = cs->ops->write_room(cs);
424
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700425 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800426
427 return retval;
428}
429
430static int if_chars_in_buffer(struct tty_struct *tty)
431{
432 struct cardstate *cs;
Tilman Schmidta4304f22009-08-06 20:33:33 -0700433 int retval = 0;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800434
435 cs = (struct cardstate *) tty->driver_data;
436 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800437 pr_err("%s: no cardstate\n", __func__);
Tilman Schmidta4304f22009-08-06 20:33:33 -0700438 return 0;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800439 }
440
Tilman Schmidt784d5852006-04-10 22:55:04 -0700441 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800442
Tilman Schmidta4304f22009-08-06 20:33:33 -0700443 mutex_lock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800444
Tilman Schmidta4304f22009-08-06 20:33:33 -0700445 if (!cs->connected)
Tilman Schmidt51370e52009-04-05 06:39:33 +0000446 gig_dbg(DEBUG_IF, "not connected");
Tilman Schmidta4304f22009-08-06 20:33:33 -0700447 else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700448 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Tilman Schmidta4304f22009-08-06 20:33:33 -0700449 else if (cs->mstate != MS_LOCKED)
Tilman Schmidt50027792008-07-23 21:28:27 -0700450 dev_warn(cs->dev, "can't write to unlocked device\n");
Tilman Schmidta4304f22009-08-06 20:33:33 -0700451 else
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800452 retval = cs->ops->chars_in_buffer(cs);
453
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700454 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800455
456 return retval;
457}
458
459static void if_throttle(struct tty_struct *tty)
460{
461 struct cardstate *cs;
462
463 cs = (struct cardstate *) tty->driver_data;
464 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800465 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800466 return;
467 }
468
Tilman Schmidt784d5852006-04-10 22:55:04 -0700469 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800470
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700471 mutex_lock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800472
Tilman Schmidt51370e52009-04-05 06:39:33 +0000473 if (!cs->connected)
474 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
475 else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700476 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000477 else
Tilman Schmidt1528b182010-02-22 13:09:52 +0000478 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800479
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700480 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800481}
482
483static void if_unthrottle(struct tty_struct *tty)
484{
485 struct cardstate *cs;
486
487 cs = (struct cardstate *) tty->driver_data;
488 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800489 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800490 return;
491 }
492
Tilman Schmidt784d5852006-04-10 22:55:04 -0700493 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800494
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700495 mutex_lock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800496
Tilman Schmidt51370e52009-04-05 06:39:33 +0000497 if (!cs->connected)
498 gig_dbg(DEBUG_IF, "not connected"); /* nothing to do */
499 else if (!cs->open_count)
Tilman Schmidt50027792008-07-23 21:28:27 -0700500 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000501 else
Tilman Schmidt1528b182010-02-22 13:09:52 +0000502 gig_dbg(DEBUG_IF, "%s: not implemented\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800503
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700504 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800505}
506
Alan Cox606d0992006-12-08 02:38:45 -0800507static void if_set_termios(struct tty_struct *tty, struct ktermios *old)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800508{
509 struct cardstate *cs;
510 unsigned int iflag;
511 unsigned int cflag;
512 unsigned int old_cflag;
513 unsigned int control_state, new_state;
514
515 cs = (struct cardstate *) tty->driver_data;
516 if (!cs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800517 pr_err("%s: no cardstate\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800518 return;
519 }
520
Tilman Schmidt784d5852006-04-10 22:55:04 -0700521 gig_dbg(DEBUG_IF, "%u: %s()", cs->minor_index, __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800522
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700523 mutex_lock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800524
Tilman Schmidt51370e52009-04-05 06:39:33 +0000525 if (!cs->connected) {
526 gig_dbg(DEBUG_IF, "not connected");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800527 goto out;
528 }
529
Tilman Schmidt51370e52009-04-05 06:39:33 +0000530 if (!cs->open_count) {
531 dev_warn(cs->dev, "%s: device not opened\n", __func__);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800532 goto out;
533 }
534
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800535 iflag = tty->termios->c_iflag;
536 cflag = tty->termios->c_cflag;
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000537 old_cflag = old ? old->c_cflag : cflag;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700538 gig_dbg(DEBUG_IF, "%u: iflag %x cflag %x old %x",
539 cs->minor_index, iflag, cflag, old_cflag);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800540
541 /* get a local copy of the current port settings */
542 control_state = cs->control_state;
543
544 /*
545 * Update baud rate.
546 * Do not attempt to cache old rates and skip settings,
547 * disconnects screw such tricks up completely.
548 * Premature optimization is the root of all evil.
549 */
550
Tilman Schmidt784d5852006-04-10 22:55:04 -0700551 /* reassert DTR and (maybe) RTS on transition from B0 */
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800552 if ((old_cflag & CBAUD) == B0) {
553 new_state = control_state | TIOCM_DTR;
554 /* don't set RTS if using hardware flow control */
555 if (!(old_cflag & CRTSCTS))
556 new_state |= TIOCM_RTS;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700557 gig_dbg(DEBUG_IF, "%u: from B0 - set DTR%s",
558 cs->minor_index,
559 (new_state & TIOCM_RTS) ? " only" : "/RTS");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800560 cs->ops->set_modem_ctrl(cs, control_state, new_state);
561 control_state = new_state;
562 }
563
564 cs->ops->baud_rate(cs, cflag & CBAUD);
565
566 if ((cflag & CBAUD) == B0) {
567 /* Drop RTS and DTR */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700568 gig_dbg(DEBUG_IF, "%u: to B0 - drop DTR/RTS", cs->minor_index);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800569 new_state = control_state & ~(TIOCM_DTR | TIOCM_RTS);
570 cs->ops->set_modem_ctrl(cs, control_state, new_state);
571 control_state = new_state;
572 }
573
574 /*
575 * Update line control register (LCR)
576 */
577
578 cs->ops->set_line_ctrl(cs, cflag);
579
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800580 /* save off the modified port settings */
581 cs->control_state = control_state;
582
583out:
Tilman Schmidtabfd1dc2006-04-10 22:55:12 -0700584 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800585}
586
587
588/* wakeup tasklet for the write operation */
589static void if_wake(unsigned long data)
590{
591 struct cardstate *cs = (struct cardstate *) data;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800592
Jiri Slabyb963a842007-02-10 01:44:55 -0800593 if (cs->tty)
594 tty_wakeup(cs->tty);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800595}
596
597/*** interface to common ***/
598
599void gigaset_if_init(struct cardstate *cs)
600{
601 struct gigaset_driver *drv;
602
603 drv = cs->driver;
604 if (!drv->have_tty)
605 return;
606
Joe Perches5452fee2009-11-19 09:55:53 +0000607 tasklet_init(&cs->if_wake_tasklet, if_wake, (unsigned long) cs);
Tilman Schmidt7435f502007-02-12 00:52:24 -0800608
609 mutex_lock(&cs->mutex);
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -0700610 cs->tty_dev = tty_register_device(drv->tty, cs->minor_index, NULL);
Hansjoerg Lipp3dda4e32006-04-22 18:43:00 +0200611
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -0700612 if (!IS_ERR(cs->tty_dev))
613 dev_set_drvdata(cs->tty_dev, cs);
Hansjoerg Lipp3dda4e32006-04-22 18:43:00 +0200614 else {
Tilman Schmidt42e3d612009-05-13 12:44:17 +0000615 pr_warning("could not register device to the tty subsystem\n");
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -0700616 cs->tty_dev = NULL;
Hansjoerg Lipp3dda4e32006-04-22 18:43:00 +0200617 }
Tilman Schmidt7435f502007-02-12 00:52:24 -0800618 mutex_unlock(&cs->mutex);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800619}
620
621void gigaset_if_free(struct cardstate *cs)
622{
623 struct gigaset_driver *drv;
624
625 drv = cs->driver;
626 if (!drv->have_tty)
627 return;
628
629 tasklet_disable(&cs->if_wake_tasklet);
630 tasklet_kill(&cs->if_wake_tasklet);
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -0700631 cs->tty_dev = NULL;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800632 tty_unregister_device(drv->tty, cs->minor_index);
633}
634
Tilman Schmidt1cec9722009-10-06 12:19:01 +0000635/**
636 * gigaset_if_receive() - pass a received block of data to the tty device
637 * @cs: device descriptor structure.
638 * @buffer: received data.
639 * @len: number of bytes received.
640 *
641 * Called by asyncdata/isocdata if a block of data received from the
642 * device must be sent to userspace through the ttyG* device.
643 */
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800644void gigaset_if_receive(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700645 unsigned char *buffer, size_t len)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800646{
647 unsigned long flags;
648 struct tty_struct *tty;
649
650 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000651 tty = cs->tty;
652 if (tty == NULL)
Tilman Schmidt1528b182010-02-22 13:09:52 +0000653 gig_dbg(DEBUG_IF, "receive on closed device");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800654 else {
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800655 tty_insert_flip_string(tty, buffer, len);
656 tty_flip_buffer_push(tty);
657 }
658 spin_unlock_irqrestore(&cs->lock, flags);
659}
660EXPORT_SYMBOL_GPL(gigaset_if_receive);
661
662/* gigaset_if_initdriver
663 * Initialize tty interface.
664 * parameters:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700665 * drv Driver
666 * procname Name of the driver (e.g. for /proc/tty/drivers)
667 * devname Name of the device files (prefix without minor number)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800668 */
669void gigaset_if_initdriver(struct gigaset_driver *drv, const char *procname,
Greg Kroah-Hartmanf4eaa372005-06-20 21:15:16 -0700670 const char *devname)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800671{
672 unsigned minors = drv->minors;
673 int ret;
674 struct tty_driver *tty;
675
676 drv->have_tty = 0;
677
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000678 drv->tty = tty = alloc_tty_driver(minors);
679 if (tty == NULL)
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800680 goto enomem;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800681
682 tty->magic = TTY_DRIVER_MAGIC,
Joe Perches475be4d2012-02-19 19:52:38 -0800683 tty->type = TTY_DRIVER_TYPE_SERIAL,
684 tty->subtype = SERIAL_TYPE_NORMAL,
685 tty->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800686
687 tty->driver_name = procname;
688 tty->name = devname;
689 tty->minor_start = drv->minor;
690 tty->num = drv->minors;
691
692 tty->owner = THIS_MODULE;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800693
Tilman Schmidtd9ba9c92009-10-25 09:30:17 +0000694 tty->init_termios = tty_std_termios;
695 tty->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800696 tty_set_operations(tty, &if_ops);
697
698 ret = tty_register_driver(tty);
699 if (ret < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800700 pr_err("error %d registering tty driver\n", ret);
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800701 goto error;
702 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700703 gig_dbg(DEBUG_IF, "tty driver initialized");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800704 drv->have_tty = 1;
705 return;
706
707enomem:
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800708 pr_err("out of memory\n");
Hansjoerg Lippee8a4b72006-03-26 01:38:32 -0800709error:
710 if (drv->tty)
711 put_tty_driver(drv->tty);
712}
713
714void gigaset_if_freedriver(struct gigaset_driver *drv)
715{
716 if (!drv->have_tty)
717 return;
718
719 drv->have_tty = 0;
720 tty_unregister_driver(drv->tty);
721 put_tty_driver(drv->tty);
722}