Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*********************************************************************
 *
 * Filename:      irlpt.c
 * Version:       
 * Description:   
 * Status:        Experimental.
 * Author:        Thomas Davis, <ratbert@radiks.net>
 * Created at:    Sat Feb 21 18:54:38 1998
 * Modified at:   Sun Mar  8 23:44:19 1998
 * Modified by:   Dag Brattli <dagb@cs.uit.no>
 * Sources:	  irlan.c
 *
 *     Copyright (c) 1998, Thomas Davis, <ratbert@radiks.net>,
 *			   Dag Brattli,  <dagb@cs.uit.no>
 *     All Rights Reserved.
 *
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
 *     the License, or (at your option) any later version.
 *
 *     I, Thomas Davis, provide no warranty for any of this software.
 *     This material is provided "AS-IS" and at no charge.
 *
 ********************************************************************/

#include <linux/config.h>
#include <linux/init.h>
#include <linux/module.h>

#include <asm/uaccess.h>
#include <linux/miscdevice.h>
#include <linux/proc_fs.h>

#include <net/irda/irlap.h>
#include <net/irda/irlmp.h>
#include <net/irda/iriap.h>
#include <net/irda/irias_object.h>
#include <net/irda/timer.h>
#include <net/irda/irda.h>
#include <net/irda/irlpt_common.h>
#include <net/irda/irlpt_server.h>
#include <net/irda/irlpt_server_fsm.h>

#ifdef CONFIG_PROC_FS
static int irlpt_server_proc_read(char *buf, char **start, off_t offset, 
				  int len, int unused);
#endif /* CONFIG_PROC_FS */

int irlpt_server_init(void);
static void irlpt_server_disconnect_indication(void *instance, void *sap, 
						LM_REASON reason,
						struct sk_buff *skb);
static void irlpt_server_connect_confirm(void *instance, void *sap, 
					 struct qos_info *qos,  
					 __u32 max_seg_size,
					 struct sk_buff *skb);
static void irlpt_server_connect_indication(void *instance, 
					    void *sap, 
					    struct qos_info *qos, 
					    __u32 max_seg_size,
					    struct sk_buff *skb);
static int irlpt_server_data_indication(void *instance, void *sap, 
					struct sk_buff *skb);
static void register_irlpt_server(void);
static void deregister_irlpt_server(void);

static __u32 skey; /* IrLMP service handle */

int irlpt_server_lsap = LSAP_IRLPT;
int irlpt_server_debug = 4;

#if 0
static char *rcsid = "$Id: irlpt_server.c,v 1.9 1998/10/22 12:02:22 dagb Exp $";
#endif

static char *version = "IrLPT server, v2 (Thomas Davis)";

struct file_operations irlpt_fops = {
	irlpt_seek,	/* seek */
	irlpt_read,     /* read */   
	irlpt_write,
	NULL,		/* readdir */
	NULL,           /* poll */
	NULL,		/* ioctl */
	NULL,		/* mmap */
	irlpt_open,     /* open */
	NULL,           /* flush */
	irlpt_close,    /* release */
	NULL,           /* fsync */
	NULL,           /* fasync */
	NULL,           /* check_media_change */
	NULL,           /* revalidate */
        NULL,           /* lock */
};

#ifdef CONFIG_PROC_FS

/*
 * Function irlpt_server_proc_read (buf, start, offset, len, unused)
 *
 *
 *
 */
static int irlpt_server_proc_read(char *buf, char **start, off_t offset, 
				  int len, int unused)
{
	int index;

	DEBUG( irlpt_server_debug, "--> " __FUNCTION__ "\n");

	if (irlpt_server != NULL) {
	        len = sprintf(buf, "%s\n\n", version);
		len += sprintf(buf+len, "ifname: %s\n", irlpt_server->ifname);
		len += sprintf(buf+len, "minor: %d\n", irlpt_server->ir_dev.minor);

		switch (irlpt_server->servicetype) {
		case IRLPT_UNKNOWN:
			index = 0;
			break;
		case IRLPT_THREE_WIRE_RAW:
			index = 1;
			break;
		case IRLPT_THREE_WIRE:
			index = 2;
			break;
		case IRLPT_NINE_WIRE:
			index = 3;
			break;
		case IRLPT_CENTRONICS:
			index = 4;
			break;
		case IRLPT_SERVER_MODE:
			index = 5;
			break;
		default:
			index = 0;
			break;
		}

		len += sprintf(buf+len, "servicetype: %s, porttype: %s\n",
			       irlpt_service_type[index],
			       irlpt_port_type[irlpt_server->porttype]);
		len += sprintf(buf+len, "saddr: 0x%08x, daddr: 0x%08x\n", 
			       irlpt_server->saddr, irlpt_server->daddr);
		len += sprintf(buf+len, "slsap: 0x%08x, dlsap: 0x%08x\n", 
			       irlpt_server->slsap_sel, 
			       irlpt_server->dlsap_sel);
		len += sprintf(buf+len, "retries: %d, count: %d\n", 
			       irlpt_server->open_retries,
			       irlpt_server->count);
		len += sprintf(buf+len, "fsm state: %s, rx queue depth: %d\n", 
			       irlpt_server_fsm_state[irlpt_server->state],
			       skb_queue_len(&irlpt_server->rx_queue));
		len += sprintf(buf+len, "\n");
	}

	DEBUG( irlpt_server_debug, __FUNCTION__ " -->\n");

	return len;
}

extern struct proc_dir_entry *proc_irda;

#endif /* CONFIG_PROC_FS */

/*
 * Function irlpt_init (dev)
 *
 *   Initializes the irlpt control structure
 *
 */

/*int irlpt_init( struct device *dev) {*/
__initfunc(int irlpt_server_init(void))
{
	__u16 hints;

	DEBUG( irlpt_server_debug, "--> " __FUNCTION__ "\n");

	printk( KERN_INFO "%s\n", version);

	irlpt_server = (struct irlpt_cb *) kmalloc (sizeof(struct irlpt_cb), 
						    GFP_KERNEL);

	if ( irlpt_server == NULL) {
		printk( KERN_WARNING "IrLPT server: Can't allocate" 
			" irlpt_server control block!\n");
		return -ENOMEM;
	}
	memset( irlpt_server, 0, sizeof(struct irlpt_cb));

	sprintf(irlpt_server->ifname, "irlpt_server");
	irlpt_server->ir_dev.minor = MISC_DYNAMIC_MINOR;
	irlpt_server->ir_dev.name = irlpt_server->ifname;
	irlpt_server->ir_dev.fops = &irlpt_fops;
	misc_register(&irlpt_server->ir_dev);
	irlpt_server->magic = IRLPT_MAGIC;
	irlpt_server->in_use = TRUE;
	irlpt_server->servicetype = IRLPT_THREE_WIRE_RAW;
	irlpt_server->porttype = IRLPT_SERIAL;

	skb_queue_head_init(&irlpt_server->rx_queue);

	hints = irlmp_service_to_hint(S_PRINTER);
	skey = irlmp_register_service(hints);
	
	register_irlpt_server();

#ifdef CONFIG_PROC_FS
	create_proc_entry("irlpt_server", 0, proc_irda)->get_info
		= irlpt_server_proc_read;
#endif /* CONFIG_PROC_FS */

	DEBUG( irlpt_server_debug, __FUNCTION__ " -->\n");

	return 0;
}

/*
 * Function irlpt_cleanup (void)
 *
 *
 *
 */
static void irlpt_server_cleanup(void)
{
	struct sk_buff *skb;

	DEBUG( irlpt_server_debug, "--> " __FUNCTION__ "\n");

	irlmp_unregister_service(skey);
	deregister_irlpt_server();

	while (( skb = skb_dequeue(&irlpt_server->rx_queue)) != NULL) {
		DEBUG(irlpt_server_debug, __FUNCTION__ ": freeing SKB\n");
                dev_kfree_skb( skb);
	}

	misc_deregister(&irlpt_server->ir_dev);

	kfree(irlpt_server);

#ifdef CONFIG_PROC_FS
	remove_proc_entry("irlpt_server", proc_irda);
#endif

	DEBUG( irlpt_server_debug, __FUNCTION__ " -->\n");
}

/*
 * Function irlpt_disconnect_indication (handle)
 *
 */
static void irlpt_server_disconnect_indication( void *instance, 
						void *sap, 
						LM_REASON reason,
						struct sk_buff *userdata)
{
#if 0
	struct irlpt_info info;
#endif
	struct irlpt_cb *self;

	DEBUG( irlpt_server_debug, "--> " __FUNCTION__ "\n");

	self = ( struct irlpt_cb *) instance;

	ASSERT( self != NULL, return;);
	ASSERT( self->magic == IRLPT_MAGIC, return;);

#if 0
	info.daddr = self->daddr;
#endif

	DEBUG( irlpt_server_debug, 
	       __FUNCTION__ ": reason=%d (%s), dlsap_sel=%d\n",
	       reason, irlpt_reasons[reason], self->dlsap_sel);

	self->connected = IRLPT_DISCONNECTED;
	self->eof = reason;

        wake_up_interruptible(&self->read_wait);
	wake_up_interruptible(&self->write_wait);
	wake_up_interruptible(&self->ex_wait);

	DEBUG( irlpt_server_debug, __FUNCTION__ ": rx queue length: %d\n",
	       skb_queue_len(&irlpt_server->rx_queue));

	irlpt_server_do_event( self, LMP_DISCONNECT, NULL, NULL);

	deregister_irlpt_server();
	register_irlpt_server();

	DEBUG( irlpt_server_debug, __FUNCTION__ " -->\n");
}

/*
 * Function irlpt_connect_confirm (handle, qos, skb)
 *
 *    LSAP connection confirmed!
 */
static void irlpt_server_connect_confirm(void *instance, 
					 void *sap, 
					 struct qos_info *qos,
					 __u32 max_seg_size,
					 struct sk_buff *skb)
{
	struct irlpt_cb *self;

	DEBUG( irlpt_server_debug, "--> " __FUNCTION__ "\n");
	self = ( struct irlpt_cb *) instance;

	ASSERT( self != NULL, return;);
	ASSERT( self->magic == IRLPT_MAGIC, return;);

	self->connected = TRUE;

	irlpt_server_do_event( self, LMP_CONNECT, NULL, NULL);

	DEBUG( irlpt_server_debug, __FUNCTION__ " -->\n");
}

/*
 * Function irlpt_connect_indication (handle)
 *
 */
static void irlpt_server_connect_indication(void *instance, 
					    void *sap, 
					    struct qos_info *qos, 
					    __u32 max_seg_size,
					    struct sk_buff *skb)
{
	struct irlpt_cb *self;
	struct irlpt_info info;

	DEBUG( irlpt_server_debug, "--> " __FUNCTION__ "\n");

	self = ( struct irlpt_cb *) instance;

	info.lsap  = (struct lsap_cb *) sap;
	
	ASSERT( self != NULL, return;);
	ASSERT( self->magic == IRLPT_MAGIC, return;);

	self->connected = IRLPT_CONNECTED;
	self->eof = FALSE;

	irlpt_server_do_event( self, LMP_CONNECT, NULL, &info);

	if (skb) {
		dev_kfree_skb( skb);
	}

	DEBUG( irlpt_server_debug, __FUNCTION__ " -->\n");
}

/*
 * Function irlpt_server_data_indication (handle, skb)
 *
 *    This function gets the data that is received on the data channel
 *
 */
static int irlpt_server_data_indication(void *instance, void *sap, 
					struct sk_buff *skb) 
{
	struct irlpt_cb *self;

	DEBUG( irlpt_server_debug, "--> " __FUNCTION__ "\n");

	self = ( struct irlpt_cb *) instance;
	     
	ASSERT( self != NULL, return -1;);
	ASSERT( self->magic == IRLPT_MAGIC, return -1;);

	ASSERT( skb != NULL, return -1;);

	DEBUG( irlpt_server_debug, __FUNCTION__ ": len=%d\n", (int) skb->len);

#if 0
	dump_buffer(skb);
#endif
	
	skb_queue_tail(&self->rx_queue, skb);
        wake_up_interruptible(&self->read_wait);

	DEBUG( irlpt_server_debug, __FUNCTION__ " -->\n");
	
	return 0;
}

/*
 * Function register_irlpt_server(void)
 *
 *    Register server support so we can accept incoming connections. We
 *    must register both a TSAP for control and data
 * 
 */
static void register_irlpt_server(void)
{
	struct notify_t notify;
	struct ias_object *obj;
	
	DEBUG( irlpt_server_debug, "--> " __FUNCTION__ "\n");

	/*
	 *  First register control TSAP
	 */
	if ( !irlpt_server || irlpt_server->magic != IRLPT_MAGIC) {
		DEBUG( 0, __FUNCTION__ ": unable to obtain handle!\n");
		return;
	}

	irda_notify_init(&notify);

	notify.connect_confirm = irlpt_server_connect_confirm;
	notify.connect_indication = irlpt_server_connect_indication;
	notify.disconnect_indication = irlpt_server_disconnect_indication;
	notify.data_indication = irlpt_server_data_indication;
	notify.instance = irlpt_server;
	strcpy(notify.name, "IrLPT server");

	irlpt_server->lsap = irlmp_open_lsap( irlpt_server_lsap, &notify);

	irlpt_server->connected = IRLPT_WAITING;
	irlpt_server->service_LSAP = irlpt_server_lsap;
	irlpt_server->slsap_sel = irlpt_server_lsap;

	/* 
	 *  Register with LM-IAS
	 */

	obj = irias_new_object("IrLPT", IAS_IRLPT_ID);
	irias_add_integer_attrib(obj, "IrDA:IrLMP:LsapSel", irlpt_server_lsap);
	irias_insert_object(obj);

	DEBUG( irlpt_server_debug, __FUNCTION__ 
	       ": Source LSAP sel=%d\n", irlpt_server->slsap_sel);
	DEBUG( irlpt_server_debug, __FUNCTION__ " -->\n");
}

/*
 * Function register_irlpt_server(void)
 *
 *    Register server support so we can accept incoming connections. We
 *    must register both a TSAP for control and data
 * 
 */
static void deregister_irlpt_server(void)
{
	DEBUG( irlpt_server_debug, "--> " __FUNCTION__ "\n");

	irlpt_server->connected = IRLPT_DISCONNECTED;

	/* 
	 *  de-Register with LM-IAS
	 */
	irias_delete_object( "IrLPT");

	DEBUG( irlpt_server_debug, 
	       __FUNCTION__ ": Source LSAP sel=%d\n", irlpt_server->slsap_sel);
	DEBUG( irlpt_server_debug, __FUNCTION__ " -->\n");
}

#ifdef MODULE

MODULE_AUTHOR("Thomas Davis <ratbert@radiks.net>");
MODULE_DESCRIPTION("The Linux IrDA/IrLPT server protocol");
MODULE_PARM(irlpt_server_debug,"1i");
MODULE_PARM(irlpt_server_fsm_debug, "1i");

/*
 * Function init_module (void)
 *
 *    Initialize the IrLPT server module, this function is called by the
 *    modprobe(1) program.
 */
int init_module(void)
{
	int ret;

        DEBUG( irlpt_server_debug, "--> IrLPT server: init_module\n");

        ret = irlpt_server_init();

        DEBUG( irlpt_server_debug, "IrLPT server: init_module -->\n");

        return ret;
}

/*
 * Function cleanup_module (void)
 *
 *    Remove the IrLPT server module, this function is called by the rmmod(1)
 *    program
 */
void cleanup_module(void)
{
        DEBUG( irlpt_server_debug, "--> IrLPT server: cleanup_module\n");

        /* No need to check MOD_IN_USE, as sys_delete_module() checks. */

        /* Free some memory */
        irlpt_server_cleanup();

        DEBUG( irlpt_server_debug, "IrLPT server: cleanup_module -->\n");
}

#endif /* MODULE */