1 /*********************************************************************
3 * Filename: ircomm_param.c
5 * Description: Parameter handling for the IrCOMM protocol
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Mon Jun 7 10:25:11 1999
9 * Modified at: Sun Jan 30 14:32:03 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 ********************************************************************/
29 #include <linux/gfp.h>
30 #include <linux/workqueue.h>
31 #include <linux/interrupt.h>
33 #include <net/irda/irda.h>
34 #include <net/irda/parameters.h>
36 #include <net/irda/ircomm_core.h>
37 #include <net/irda/ircomm_tty_attach.h>
38 #include <net/irda/ircomm_tty.h>
40 #include <net/irda/ircomm_param.h>
42 static int ircomm_param_service_type(void *instance
, irda_param_t
*param
,
44 static int ircomm_param_port_type(void *instance
, irda_param_t
*param
,
46 static int ircomm_param_port_name(void *instance
, irda_param_t
*param
,
48 static int ircomm_param_service_type(void *instance
, irda_param_t
*param
,
50 static int ircomm_param_data_rate(void *instance
, irda_param_t
*param
,
52 static int ircomm_param_data_format(void *instance
, irda_param_t
*param
,
54 static int ircomm_param_flow_control(void *instance
, irda_param_t
*param
,
56 static int ircomm_param_xon_xoff(void *instance
, irda_param_t
*param
, int get
);
57 static int ircomm_param_enq_ack(void *instance
, irda_param_t
*param
, int get
);
58 static int ircomm_param_line_status(void *instance
, irda_param_t
*param
,
60 static int ircomm_param_dte(void *instance
, irda_param_t
*param
, int get
);
61 static int ircomm_param_dce(void *instance
, irda_param_t
*param
, int get
);
62 static int ircomm_param_poll(void *instance
, irda_param_t
*param
, int get
);
64 static const pi_minor_info_t pi_minor_call_table_common
[] = {
65 { ircomm_param_service_type
, PV_INT_8_BITS
},
66 { ircomm_param_port_type
, PV_INT_8_BITS
},
67 { ircomm_param_port_name
, PV_STRING
}
69 static const pi_minor_info_t pi_minor_call_table_non_raw
[] = {
70 { ircomm_param_data_rate
, PV_INT_32_BITS
| PV_BIG_ENDIAN
},
71 { ircomm_param_data_format
, PV_INT_8_BITS
},
72 { ircomm_param_flow_control
, PV_INT_8_BITS
},
73 { ircomm_param_xon_xoff
, PV_INT_16_BITS
},
74 { ircomm_param_enq_ack
, PV_INT_16_BITS
},
75 { ircomm_param_line_status
, PV_INT_8_BITS
}
77 static const pi_minor_info_t pi_minor_call_table_9_wire
[] = {
78 { ircomm_param_dte
, PV_INT_8_BITS
},
79 { ircomm_param_dce
, PV_INT_8_BITS
},
80 { ircomm_param_poll
, PV_NO_VALUE
},
83 static const pi_major_info_t pi_major_call_table
[] = {
84 { pi_minor_call_table_common
, 3 },
85 { pi_minor_call_table_non_raw
, 6 },
86 { pi_minor_call_table_9_wire
, 3 }
87 /* { pi_minor_call_table_centronics } */
90 pi_param_info_t ircomm_param_info
= { pi_major_call_table
, 3, 0x0f, 4 };
93 * Function ircomm_param_request (self, pi, flush)
95 * Queue a parameter for the control channel
98 int ircomm_param_request(struct ircomm_tty_cb
*self
, __u8 pi
, int flush
)
104 IRDA_ASSERT(self
!= NULL
, return -1;);
105 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
107 /* Make sure we don't send parameters for raw mode */
108 if (self
->service_type
== IRCOMM_3_WIRE_RAW
)
111 spin_lock_irqsave(&self
->spinlock
, flags
);
113 skb
= self
->ctrl_skb
;
115 skb
= alloc_skb(256, GFP_ATOMIC
);
117 spin_unlock_irqrestore(&self
->spinlock
, flags
);
121 skb_reserve(skb
, self
->max_header_size
);
122 self
->ctrl_skb
= skb
;
125 * Inserting is a little bit tricky since we don't know how much
126 * room we will need. But this should hopefully work OK
128 count
= irda_param_insert(self
, pi
, skb_tail_pointer(skb
),
129 skb_tailroom(skb
), &ircomm_param_info
);
131 net_warn_ratelimited("%s(), no room for parameter!\n",
133 spin_unlock_irqrestore(&self
->spinlock
, flags
);
138 spin_unlock_irqrestore(&self
->spinlock
, flags
);
140 pr_debug("%s(), skb->len=%d\n", __func__
, skb
->len
);
143 /* ircomm_tty_do_softint will take care of the rest */
144 schedule_work(&self
->tqueue
);
151 * Function ircomm_param_service_type (self, buf, len)
153 * Handle service type, this function will both be called after the LM-IAS
154 * query and then the remote device sends its initial parameters
157 static int ircomm_param_service_type(void *instance
, irda_param_t
*param
,
160 struct ircomm_tty_cb
*self
= (struct ircomm_tty_cb
*) instance
;
161 __u8 service_type
= (__u8
) param
->pv
.i
;
163 IRDA_ASSERT(self
!= NULL
, return -1;);
164 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
167 param
->pv
.i
= self
->settings
.service_type
;
171 /* Find all common service types */
172 service_type
&= self
->service_type
;
174 pr_debug("%s(), No common service type to use!\n", __func__
);
177 pr_debug("%s(), services in common=%02x\n", __func__
,
181 * Now choose a preferred service type of those available
183 if (service_type
& IRCOMM_CENTRONICS
)
184 self
->settings
.service_type
= IRCOMM_CENTRONICS
;
185 else if (service_type
& IRCOMM_9_WIRE
)
186 self
->settings
.service_type
= IRCOMM_9_WIRE
;
187 else if (service_type
& IRCOMM_3_WIRE
)
188 self
->settings
.service_type
= IRCOMM_3_WIRE
;
189 else if (service_type
& IRCOMM_3_WIRE_RAW
)
190 self
->settings
.service_type
= IRCOMM_3_WIRE_RAW
;
192 pr_debug("%s(), resulting service type=0x%02x\n", __func__
,
193 self
->settings
.service_type
);
196 * Now the line is ready for some communication. Check if we are a
197 * server, and send over some initial parameters.
198 * Client do it in ircomm_tty_state_setup().
199 * Note : we may get called from ircomm_tty_getvalue_confirm(),
200 * therefore before we even have open any socket. And self->client
201 * is initialised to TRUE only later. So, we check if the link is
202 * really initialised. - Jean II
204 if ((self
->max_header_size
!= IRCOMM_TTY_HDR_UNINITIALISED
) &&
206 (self
->settings
.service_type
!= IRCOMM_3_WIRE_RAW
))
208 /* Init connection */
209 ircomm_tty_send_initial_parameters(self
);
210 ircomm_tty_link_established(self
);
217 * Function ircomm_param_port_type (self, param)
219 * The port type parameter tells if the devices are serial or parallel.
220 * Since we only advertise serial service, this parameter should only
221 * be equal to IRCOMM_SERIAL.
223 static int ircomm_param_port_type(void *instance
, irda_param_t
*param
, int get
)
225 struct ircomm_tty_cb
*self
= (struct ircomm_tty_cb
*) instance
;
227 IRDA_ASSERT(self
!= NULL
, return -1;);
228 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
231 param
->pv
.i
= IRCOMM_SERIAL
;
233 self
->settings
.port_type
= (__u8
) param
->pv
.i
;
235 pr_debug("%s(), port type=%d\n", __func__
,
236 self
->settings
.port_type
);
242 * Function ircomm_param_port_name (self, param)
247 static int ircomm_param_port_name(void *instance
, irda_param_t
*param
, int get
)
249 struct ircomm_tty_cb
*self
= (struct ircomm_tty_cb
*) instance
;
251 IRDA_ASSERT(self
!= NULL
, return -1;);
252 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
255 pr_debug("%s(), not imp!\n", __func__
);
257 pr_debug("%s(), port-name=%s\n", __func__
, param
->pv
.c
);
258 strncpy(self
->settings
.port_name
, param
->pv
.c
, 32);
265 * Function ircomm_param_data_rate (self, param)
267 * Exchange data rate to be used in this settings
270 static int ircomm_param_data_rate(void *instance
, irda_param_t
*param
, int get
)
272 struct ircomm_tty_cb
*self
= (struct ircomm_tty_cb
*) instance
;
274 IRDA_ASSERT(self
!= NULL
, return -1;);
275 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
278 param
->pv
.i
= self
->settings
.data_rate
;
280 self
->settings
.data_rate
= param
->pv
.i
;
282 pr_debug("%s(), data rate = %d\n", __func__
, param
->pv
.i
);
288 * Function ircomm_param_data_format (self, param)
290 * Exchange data format to be used in this settings
293 static int ircomm_param_data_format(void *instance
, irda_param_t
*param
,
296 struct ircomm_tty_cb
*self
= (struct ircomm_tty_cb
*) instance
;
298 IRDA_ASSERT(self
!= NULL
, return -1;);
299 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
302 param
->pv
.i
= self
->settings
.data_format
;
304 self
->settings
.data_format
= (__u8
) param
->pv
.i
;
310 * Function ircomm_param_flow_control (self, param)
312 * Exchange flow control settings to be used in this settings
315 static int ircomm_param_flow_control(void *instance
, irda_param_t
*param
,
318 struct ircomm_tty_cb
*self
= (struct ircomm_tty_cb
*) instance
;
320 IRDA_ASSERT(self
!= NULL
, return -1;);
321 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
324 param
->pv
.i
= self
->settings
.flow_control
;
326 self
->settings
.flow_control
= (__u8
) param
->pv
.i
;
328 pr_debug("%s(), flow control = 0x%02x\n", __func__
, (__u8
)param
->pv
.i
);
334 * Function ircomm_param_xon_xoff (self, param)
336 * Exchange XON/XOFF characters
339 static int ircomm_param_xon_xoff(void *instance
, irda_param_t
*param
, int get
)
341 struct ircomm_tty_cb
*self
= (struct ircomm_tty_cb
*) instance
;
343 IRDA_ASSERT(self
!= NULL
, return -1;);
344 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
347 param
->pv
.i
= self
->settings
.xonxoff
[0];
348 param
->pv
.i
|= self
->settings
.xonxoff
[1] << 8;
350 self
->settings
.xonxoff
[0] = (__u16
) param
->pv
.i
& 0xff;
351 self
->settings
.xonxoff
[1] = (__u16
) param
->pv
.i
>> 8;
354 pr_debug("%s(), XON/XOFF = 0x%02x,0x%02x\n", __func__
,
355 param
->pv
.i
& 0xff, param
->pv
.i
>> 8);
361 * Function ircomm_param_enq_ack (self, param)
363 * Exchange ENQ/ACK characters
366 static int ircomm_param_enq_ack(void *instance
, irda_param_t
*param
, int get
)
368 struct ircomm_tty_cb
*self
= (struct ircomm_tty_cb
*) instance
;
370 IRDA_ASSERT(self
!= NULL
, return -1;);
371 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
374 param
->pv
.i
= self
->settings
.enqack
[0];
375 param
->pv
.i
|= self
->settings
.enqack
[1] << 8;
377 self
->settings
.enqack
[0] = (__u16
) param
->pv
.i
& 0xff;
378 self
->settings
.enqack
[1] = (__u16
) param
->pv
.i
>> 8;
381 pr_debug("%s(), ENQ/ACK = 0x%02x,0x%02x\n", __func__
,
382 param
->pv
.i
& 0xff, param
->pv
.i
>> 8);
388 * Function ircomm_param_line_status (self, param)
393 static int ircomm_param_line_status(void *instance
, irda_param_t
*param
,
396 pr_debug("%s(), not impl.\n", __func__
);
402 * Function ircomm_param_dte (instance, param)
404 * If we get here, there must be some sort of null-modem connection, and
405 * we are probably working in server mode as well.
407 static int ircomm_param_dte(void *instance
, irda_param_t
*param
, int get
)
409 struct ircomm_tty_cb
*self
= (struct ircomm_tty_cb
*) instance
;
412 IRDA_ASSERT(self
!= NULL
, return -1;);
413 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
416 param
->pv
.i
= self
->settings
.dte
;
418 dte
= (__u8
) param
->pv
.i
;
420 self
->settings
.dce
= 0;
422 if (dte
& IRCOMM_DELTA_DTR
)
423 self
->settings
.dce
|= (IRCOMM_DELTA_DSR
|
426 if (dte
& IRCOMM_DTR
)
427 self
->settings
.dce
|= (IRCOMM_DSR
|
431 if (dte
& IRCOMM_DELTA_RTS
)
432 self
->settings
.dce
|= IRCOMM_DELTA_CTS
;
433 if (dte
& IRCOMM_RTS
)
434 self
->settings
.dce
|= IRCOMM_CTS
;
436 /* Take appropriate actions */
437 ircomm_tty_check_modem_status(self
);
439 /* Null modem cable emulator */
440 self
->settings
.null_modem
= TRUE
;
447 * Function ircomm_param_dce (instance, param)
452 static int ircomm_param_dce(void *instance
, irda_param_t
*param
, int get
)
454 struct ircomm_tty_cb
*self
= (struct ircomm_tty_cb
*) instance
;
457 pr_debug("%s(), dce = 0x%02x\n", __func__
, (__u8
)param
->pv
.i
);
459 dce
= (__u8
) param
->pv
.i
;
461 IRDA_ASSERT(self
!= NULL
, return -1;);
462 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
464 self
->settings
.dce
= dce
;
466 /* Check if any of the settings have changed */
468 if (dce
& IRCOMM_DELTA_CTS
) {
469 pr_debug("%s(), CTS\n", __func__
);
473 ircomm_tty_check_modem_status(self
);
479 * Function ircomm_param_poll (instance, param)
481 * Called when the peer device is polling for the line settings
484 static int ircomm_param_poll(void *instance
, irda_param_t
*param
, int get
)
486 struct ircomm_tty_cb
*self
= (struct ircomm_tty_cb
*) instance
;
488 IRDA_ASSERT(self
!= NULL
, return -1;);
489 IRDA_ASSERT(self
->magic
== IRCOMM_TTY_MAGIC
, return -1;);
491 /* Poll parameters are always of length 0 (just a signal) */
493 /* Respond with DTE line settings */
494 ircomm_param_request(self
, IRCOMM_DTE
, TRUE
);