summaryrefslogtreecommitdiff
path: root/drivers/char/mxc_sdma_tty.c
blob: d0b59e4ff69eee39afe5ab9c9042edf14d8598f3 (plain)
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*!
 * @file mxc_sdma_tty.c
 * @brief This file contains functions for SDMA TTY driver
 *
 * SDMA TTY driver is used for moving data between MCU and DSP using line discipline API
 *
 * @ingroup IPC
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <asm/dma.h>
#include <asm/mach/dma.h>
#include <linux/interrupt.h>
#include <asm/irq.h>
#include <asm/hardware.h>
#include <asm/semaphore.h>
#include <asm/system.h>		/* save/local_irq_restore */
#include <asm/uaccess.h>	/* For copy_from_user */
#include <linux/sched.h>	/* For schedule */
#include <linux/device.h>

#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/circ_buf.h>

#include <asm/arch/mxc_sdma_tty.h>

#define DEBUG 0

#if DEBUG
#define DPRINTK(fmt, args...) printk("%s: " fmt, __FUNCTION__ , ## args)
#else
#define DPRINTK(fmt, args...)
#endif

#define SDMA_TTY_NORMAL_MAJOR 0

#define WRITE_ROOM 512

/*!
 * This define returns the number of read SDMA channel
 */
#define READ_CHANNEL(line) (line < IPC_NB_CH_BIDIR) ? 2 * line + 1 : \
                           (line < (IPC_NB_CH_BIDIR + IPC_NB_CH_DSPMCU)) ? \
                           (line + 3) : -1

/*!
 * This define returns the number of write SDMA channel
 */
#define WRITE_CHANNEL(line) (line < IPC_NB_CH_BIDIR) ? 2 * line + 2 : -1

/*!
 * This define returns 1 if the device is unidirectional
 * DSP to MCU
 */
#define DSPMCU_DEVICE(line) line < (IPC_NB_CH_BIDIR + IPC_NB_CH_DSPMCU) && \
                            line >= IPC_NB_CH_BIDIR

/*!
 * This holds tty driver definitions
 */
static struct tty_driver *sdma_tty_driver;

/*!
 * This holds the transmit tasklet
 */
static struct tasklet_struct sdma_tty_tasklet;

static struct class *sdma_tty_class;

/*!
 * Structure containing sdma tty line status
 */
typedef struct {
	struct tty_struct *tty;	/*!< pointer to tty struct of the line */
	int loopback_mode;	/*!< Loopback mode: 0 - disable, 1 - enable */
	struct circ_buf write_buf;	/*!< Write buffer */
	char *write_buf_phys;	/*!< Write buffer dma pointer */
	char *read_buf;		/*!< Read buffer */
	char *read_buf_phys;	/*!< Read buffer dma pointer */
	int chars_in_buffer;	/*!< Number of characters in write buffer */
	int sending;		/*!< Sending flag */
	int sending_count;	/*!< Number of characters sent
				   in last write operation */
	int f_mode;		/*!< File mode (read/write) */
} sdma_tty_struct;

static sdma_tty_struct sdma_tty_data[IPC_NB_CH_BIDIR + IPC_NB_CH_DSPMCU];

static void sdma_tty_write_tasklet(unsigned long arg)
{
	int line, tx_num;
	struct tty_struct *tty;
	dma_request_t writechnl_request;
	struct circ_buf *circ;

	tty = (struct tty_struct *)arg;
	line = tty->index;
	DPRINTK("SDMA %s on line %d\n", __FUNCTION__, line);

	if (sdma_tty_data[line].chars_in_buffer > 0) {
		circ = &sdma_tty_data[line].write_buf;
		tx_num = CIRC_CNT_TO_END(circ->head, circ->tail, WRITE_ROOM);
		writechnl_request.sourceAddr =
		    sdma_tty_data[line].write_buf_phys + circ->tail;
		writechnl_request.count = tx_num;
		mxc_dma_set_config(WRITE_CHANNEL(line), &writechnl_request, 0);
		mxc_dma_start(WRITE_CHANNEL(line));
	} else {
		sdma_tty_data[line].sending = 0;
	}
}

/*!
 * This function called on SDMA write channel interrupt.
 *
 * @param       arg   number of SDMA channel
 */
static void sdma_tty_write_callback(void *arg)
{
	dma_request_t sdma_read_request, sdma_write_request;
	int line, count = 0;
	struct tty_struct *tty;
	struct circ_buf *circ;

	tty = (struct tty_struct *)arg;
	line = tty->index;

	mxc_dma_get_config(WRITE_CHANNEL(line), &sdma_write_request, 0);

	count = sdma_write_request.count;
	circ = &sdma_tty_data[line].write_buf;

	DPRINTK("SDMA %s on line %d count %d\n", __FUNCTION__, line, count);

	if (sdma_tty_data[line].loopback_mode) {
		sdma_tty_data[line].sending_count = count;

		mxc_dma_get_config(READ_CHANNEL(line), &sdma_read_request, 0);
		sdma_read_request.count = count;
		sdma_read_request.destAddr = sdma_tty_data[line].read_buf_phys;

		if (count <= tty_buffer_request_room(tty, count)) {
			tty_flip_buffer_push(tty);
		}

		mxc_dma_set_config(READ_CHANNEL(line), &sdma_read_request, 0);
		mxc_dma_start(READ_CHANNEL(line));
	}
	sdma_tty_data[line].chars_in_buffer -= count;
	circ->tail = (circ->tail + count) & (WRITE_ROOM - 1);
	DPRINTK("SDMA %s on head %x tail %x\n", __FUNCTION__, circ->head,
		circ->tail);
	wake_up_interruptible(&tty->write_wait);

	if (sdma_tty_data[line].chars_in_buffer > 0) {
		tasklet_schedule(&sdma_tty_tasklet);
	} else {
		sdma_tty_data[line].sending = 0;
	}
}

/*!
 * This function called on SDMA read channel interrupt.
 *
 * @param       arg   number of SDMA channel
 */
static void sdma_tty_read_callback(void *arg)
{
	dma_request_t read_request;
	struct tty_struct *tty;
	int line;
	int count;

	tty = (struct tty_struct *)arg;
	line = tty->index;

	if (sdma_tty_data[line].loopback_mode &&
	    sdma_tty_data[line].sending_count <= 0) {
		return;
	}

	mxc_dma_get_config(READ_CHANNEL(line), &read_request, 0);

	if (read_request.bd_done == 1) {
		/* BD is not done yet */
		return;
	}

	count = read_request.count;
	/* Check for space availability in the TTY Flip buffer */
	count = tty_buffer_request_room(tty, count);
	if (!count) {
		goto drop_data;
	}

	/* Set the real_raw flag to prevent processing on the received chars */
	tty->real_raw = 1;
	tty_insert_flip_string(tty, sdma_tty_data[line].read_buf, count);
	tty_flip_buffer_push(tty);

	wake_up_interruptible(&tty->read_wait);

      drop_data:
	if (!sdma_tty_data[line].loopback_mode) {
		read_request.count = WRITE_ROOM;
		read_request.destAddr = sdma_tty_data[line].read_buf_phys;

		mxc_dma_set_config(READ_CHANNEL(line), &read_request, 0);
		mxc_dma_start(READ_CHANNEL(line));
	} else {
		sdma_tty_data[line].sending_count = 0;
	}

	DPRINTK("Exit\n");
}

/*!
 * This function changes loopback mode
 *
 * @param       tty    pointer to current tty line structure
 * @param       mode   loopback mode: 0 - disable, 1 enable
 * @param       line   tty line
 * @return      0 on success, error code on fail
 */
static int sdma_tty_change_mode(struct tty_struct *tty, int mode, int line)
{
	dma_channel_params read_sdma_params, write_sdma_params;
	dma_request_t sdma_read_request;
	int res = 0;

	if (!(mode == 0 || mode == 1)) {
		printk(KERN_WARNING "Illegal loopback mode value\n");
		return -EINVAL;
	}

	if ((mode == 1) && (!((sdma_tty_data[line].f_mode & FMODE_READ) &&
			      (sdma_tty_data[line].f_mode & FMODE_WRITE)))) {
		printk(KERN_WARNING
		       "Loopback mode requires channel to be have read and write modes\n");
		return -EINVAL;
	}

	/* Mode not changed */
	if (sdma_tty_data[line].loopback_mode == mode) {
		return 0;
	}

	sdma_tty_data[line].loopback_mode = mode;

	if (sdma_tty_data[line].f_mode & FMODE_READ) {
		mxc_dma_stop(READ_CHANNEL(line));
	}
	if (sdma_tty_data[line].f_mode & FMODE_WRITE) {
		mxc_dma_stop(WRITE_CHANNEL(line));
	}

	if (sdma_tty_data[line].f_mode & FMODE_READ) {
		/* SDMA read channel setup */
		memset(&read_sdma_params, 0, sizeof(dma_channel_params));
		read_sdma_params.peripheral_type = DSP;
		read_sdma_params.transfer_type =
		    (mode == 0) ? dsp_2_emi : dsp_2_emi_loop;
		read_sdma_params.event_id = 0;
		read_sdma_params.callback = sdma_tty_read_callback;
		read_sdma_params.arg = tty;
		res =
		    mxc_dma_setup_channel(READ_CHANNEL(line),
					  &read_sdma_params);
		if (res < 0) {
			return res;
		}
		tty_flip_buffer_push(tty);

		/* SDMA read request setup */
		memset(&sdma_read_request, 0, sizeof(dma_request_t));
		sdma_read_request.destAddr = sdma_tty_data[line].read_buf_phys;

		mxc_dma_set_config(READ_CHANNEL(line), &sdma_read_request, 0);

		if (!sdma_tty_data[line].loopback_mode) {
			mxc_dma_start(READ_CHANNEL(line));
		}

	}

	if (sdma_tty_data[line].f_mode & FMODE_WRITE) {
		/* Write SDMA channel setup */
		memset(&write_sdma_params, 0, sizeof(dma_channel_params));
		write_sdma_params.peripheral_type = DSP;
		write_sdma_params.transfer_type =
		    (mode == 0) ? emi_2_dsp : emi_2_dsp_loop;
		write_sdma_params.event_id = 0;
		write_sdma_params.callback = sdma_tty_write_callback;
		write_sdma_params.arg = tty;
		res =
		    mxc_dma_setup_channel(WRITE_CHANNEL(line),
					  &write_sdma_params);
		if (res < 0) {
			return res;
		}
	}

	if (mode) {
		/* Echo off to avoid infinite loop */
		tty->termios->c_lflag = 0;
	}

	return 0;
}

/*!
 * This function opens tty line
 *
 * @param       tty   pointer to current tty line structure
 * @param       filp  pointer to file structure
 * @return      0 on success, error code on fail
 */
static int sdma_tty_open(struct tty_struct *tty, struct file *filp)
{
	int channels[2];
	int line;
	int res;

	line = tty->index;

	if (DSPMCU_DEVICE(line) && (filp->f_mode & FMODE_WRITE)) {
		printk(KERN_WARNING
		       "Error: Cannot open this channel for writing.\n");
		return -EINVAL;
	}

	memset(&sdma_tty_data[line], 0, sizeof(sdma_tty_struct));

	channels[0] = READ_CHANNEL(line);
	channels[1] = WRITE_CHANNEL(line);

	sdma_tty_data[line].tty = tty;
	sdma_tty_data[line].f_mode = filp->f_mode;

	if (sdma_tty_data[line].f_mode & FMODE_READ) {
		res = mxc_request_dma(channels, "SDMA TTY");
		if (res < 0) {	// For read
			printk(KERN_WARNING
			       "Error: SDMA DSP read channel busy\n");
			goto read_dma_req_failed;
		}
	}
	if (sdma_tty_data[line].f_mode & FMODE_WRITE) {
		res = mxc_request_dma(channels + 1, "SDMA TTY");
		if (res < 0) {	// For write
			printk(KERN_WARNING
			       "Error: SDMA DSP write channel busy\n");
			goto write_dma_req_failed;
		}
	}

	sdma_tty_data[line].chars_in_buffer = 0;
	sdma_tty_data[line].sending = 0;
	sdma_tty_data[line].loopback_mode = -1;

	if (sdma_tty_data[line].f_mode & FMODE_WRITE) {
		sdma_tty_data[line].write_buf.buf = sdma_malloc(WRITE_ROOM);
		sdma_tty_data[line].write_buf.head = 0;
		sdma_tty_data[line].write_buf.tail = 0;
		sdma_tty_data[line].write_buf_phys =
		    (char *)sdma_virt_to_phys(sdma_tty_data[line].write_buf.
					      buf);
	}
	if (sdma_tty_data[line].f_mode & FMODE_READ) {
		sdma_tty_data[line].read_buf = sdma_malloc(WRITE_ROOM);
		sdma_tty_data[line].read_buf_phys =
		    (char *)sdma_virt_to_phys(sdma_tty_data[line].read_buf);
	}

	tty->low_latency = 1;	// High rate

	res = sdma_tty_change_mode(tty, DEFAULT_LOOPBACK_MODE, line);
	if (res < 0) {
		goto change_mode_failed;
	}
	tasklet_init(&sdma_tty_tasklet, sdma_tty_write_tasklet,
		     (unsigned long)tty);
	DPRINTK("SDMA %s raw = %d real_raw = %d\n", __FUNCTION__, tty->raw,
		tty->real_raw);

	return 0;

      change_mode_failed:
	if (sdma_tty_data[line].f_mode & FMODE_WRITE) {
		sdma_free(sdma_tty_data[line].write_buf.buf);
	}
	if (sdma_tty_data[line].f_mode & FMODE_READ) {
		sdma_free(sdma_tty_data[line].read_buf);
	}
      write_dma_req_failed:
	if (sdma_tty_data[line].f_mode & FMODE_READ) {
		mxc_free_dma(READ_CHANNEL(line));
	}
      read_dma_req_failed:
	return res;
}

/*!
 * This function closes tty line
 *
 * @param       tty   pointer to current tty line structure
 * @param       filp  pointer to file structure
 */
static void sdma_tty_close(struct tty_struct *tty, struct file *filp)
{
	int line;

	line = tty->index;

	if (sdma_tty_data[line].f_mode & FMODE_READ) {
		mxc_free_dma(READ_CHANNEL(line));
		sdma_free(sdma_tty_data[line].read_buf);
	}

	if (sdma_tty_data[line].f_mode & FMODE_WRITE) {
		mxc_free_dma(WRITE_CHANNEL(line));
		sdma_free(sdma_tty_data[line].write_buf.buf);
		sdma_tty_data[line].write_buf.buf = NULL;
	}
}

/*!
 * This function performs ioctl commands
 *
 * The driver supports 2 ioctls: TIOCLPBACK and TCGETS
 * Other ioctls are supported by upper tty layers.
 *
 * @param       tty   pointer to current tty line structure
 * @param       file  pointer to file structure
 * @param       cmd   command
 * @param       arg   pointer to argument
 * @return      0 on success, error code on fail
 */
static int sdma_tty_ioctl(struct tty_struct *tty, struct file *file,
			  unsigned int cmd, unsigned long arg)
{
	int res;
	int line;

	line = tty->index;

	switch (cmd) {
	case TIOCLPBACK:
		res = sdma_tty_change_mode(tty, *((int *)arg), line);

		return res;
		break;
	case TCGETS:
		if (copy_to_user((struct termios *)arg,
				 tty->termios, sizeof(struct termios))) {
			return -EFAULT;
		}
		return (0);
		break;
	default:
		/* printk(KERN_WARNING "Unsupported %d ioctl\n", cmd); */
		break;
	}

	return -ENOIOCTLCMD;
}

/*!
 * This function starts SDMA for sending data from write buffer
 *
 * @param       tty       pointer to current tty line structure
 * @param       count     number of characters
 * @return      number of characters copied to write buffer
 */
static ssize_t sdma_tty_write_to_device(struct tty_struct *tty, int count)
{
	dma_request_t sdma_write_request;
	int line;

	line = tty->index;

	DPRINTK("SDMA %s on line %d %d\n", __FUNCTION__, line, count);

	memset(&sdma_write_request, 0, sizeof(dma_request_t));

	sdma_tty_data[line].sending_count = count;

	sdma_write_request.sourceAddr = sdma_tty_data[line].write_buf_phys +
	    sdma_tty_data[line].write_buf.tail;
	sdma_write_request.count = count;

	mxc_dma_set_config(WRITE_CHANNEL(line), &sdma_write_request, 0);

	mxc_dma_start(WRITE_CHANNEL(line));

	return count;
}

/*!
 * This function flushes chars from write buffer
 *
 * @param       tty   pointer to current tty line structure
 */
static void sdma_tty_flush_chars(struct tty_struct *tty)
{
	int line, count = 0;
	struct circ_buf *circ;

	line = tty->index;
	circ = &sdma_tty_data[line].write_buf;

	if (sdma_tty_data[line].sending == 1) {
		return;
	}

	if (sdma_tty_data[line].chars_in_buffer == 0) {
		return;
	}

	sdma_tty_data[line].sending = 1;
	count = CIRC_CNT_TO_END(circ->head, circ->tail, WRITE_ROOM);
	sdma_tty_write_to_device(tty, count);
}

/*!
 * This function writes characters to write buffer
 *
 * @param       tty       pointer to current tty line structure
 * @param       buf       pointer to buffer
 * @param       count     number of characters
 * @return      number of characters copied to write buffer
 */
static ssize_t sdma_tty_write(struct tty_struct *tty,
			      const unsigned char *buf, int count)
{
	int line;
	int write_room, ret = 0;
	struct circ_buf *circ;
	line = tty->index;

	DPRINTK("SDMA %s on line %d %d\n", __FUNCTION__, line, count);
	circ = &sdma_tty_data[line].write_buf;

	if (circ->head == circ->tail) {
		circ->head = 0;
		circ->tail = 0;
	}

	while (1) {
		write_room =
		    CIRC_SPACE_TO_END(circ->head, circ->tail, WRITE_ROOM);
		if (count < write_room) {
			write_room = count;
		}
		if (write_room <= 0) {
			break;
		}

		memcpy(circ->buf + circ->head, buf, write_room);
		circ->head = (circ->head + write_room) & (WRITE_ROOM - 1);
		buf += write_room;
		count -= write_room;
		ret += write_room;
	}
	DPRINTK("SDMA %s on head %x tail %x\n", __FUNCTION__, circ->head,
		circ->tail);
	sdma_tty_data[line].chars_in_buffer += ret;
	sdma_tty_flush_chars(tty);

	return ret;
}

/*!
 * This function returns the number of characters in write buffer
 *
 * @param       tty       pointer to current tty line structure
 * @return      number of characters in write buffer
 */
static int sdma_tty_chars_in_buffer(struct tty_struct *tty)
{
	int res;
	int line;

	line = tty->index;

	res = sdma_tty_data[line].chars_in_buffer;

	return res;
}

/*!
 * This function returns how much room the tty driver has available in the write buffer
 *
 * @param       tty       pointer to current tty line structure
 * @return      how much room the tty driver has available in the write buffer
 */
static int sdma_tty_write_room(struct tty_struct *tty)
{
	int res;
	int line;
	struct circ_buf *circ;

	line = tty->index;
	circ = &sdma_tty_data[line].write_buf;

	res = CIRC_SPACE(circ->head, circ->tail, WRITE_ROOM);

	return res;
}

/*!
 * TTY operation structure
 */
static struct tty_operations sdma_tty_ops = {
	.open = sdma_tty_open,
	.close = sdma_tty_close,
	.write_room = sdma_tty_write_room,
	.write = sdma_tty_write,
	.flush_chars = sdma_tty_flush_chars,
	.ioctl = sdma_tty_ioctl,
	.chars_in_buffer = sdma_tty_chars_in_buffer,
};

/*!
 * This function registers the tty driver
 */
static int __init sdma_tty_init(void)
{
	int error;
	int dev_id, dev_number;
	int dev_mode;
	struct class_device *temp_class;

	dev_number = IPC_NB_CH_BIDIR + IPC_NB_CH_DSPMCU;

	sdma_tty_driver = alloc_tty_driver(dev_number);

	if (!sdma_tty_driver) {
		return -ENOMEM;
	}

	sdma_tty_driver->owner = THIS_MODULE;
	sdma_tty_driver->name = "mxc_sdma_tty";
	sdma_tty_driver->name_base = 0;
	sdma_tty_driver->major = SDMA_TTY_NORMAL_MAJOR;
	sdma_tty_driver->minor_start = 0;
	sdma_tty_driver->num = dev_number;
	sdma_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
	sdma_tty_driver->subtype = SERIAL_TYPE_NORMAL;
	sdma_tty_driver->init_termios = tty_std_termios;
	sdma_tty_driver->flags = TTY_DRIVER_DYNAMIC_DEV;
	sdma_tty_driver->flags |= TTY_DRIVER_REAL_RAW;	/* lpj */
	tty_set_operations(sdma_tty_driver, &sdma_tty_ops);

	if ((error = tty_register_driver(sdma_tty_driver))) {
		printk(KERN_ERR "SDMA_TTY: Couldn't register SDMA_TTY driver,\
 error = %d\n", error);
		put_tty_driver(sdma_tty_driver);

		return error;
	}

	sdma_tty_class = class_create(THIS_MODULE, "sdma");
	if (IS_ERR(sdma_tty_class)) {
		printk(KERN_ERR "Error creating sdma tty class.\n");
		return PTR_ERR(sdma_tty_class);
	}

	dev_mode = S_IFCHR | S_IRUGO | S_IWUGO;
	for (dev_id = 0; dev_id < dev_number; dev_id++) {
		temp_class =
		    class_device_create(sdma_tty_class, NULL,
					MKDEV(sdma_tty_driver->major,
					      dev_id), NULL, "sdma%u", dev_id);
		if (IS_ERR(temp_class))
			goto err_out;

		if (dev_id == IPC_NB_CH_BIDIR) {
			dev_mode = S_IFCHR | S_IRUGO;
		}
		if (dev_id == IPC_NB_CH_BIDIR + IPC_NB_CH_DSPMCU) {
			dev_mode = S_IFCHR | S_IWUGO;
		}
	}

	printk("SDMA TTY Driver initialized\n");
	return error;

      err_out:
	printk(KERN_ERR "Error creating sdma class or class device.\n");
	for (dev_id = 0; dev_id < dev_number; dev_id++) {
		class_device_destroy(sdma_tty_class, MKDEV
				     (sdma_tty_driver->major, dev_id));
	}
	class_destroy(sdma_tty_class);
	return -1;
}

/*!
 * This function unregisters the tty driver
 */
static void __exit sdma_tty_exit(void)
{

	int dev_id, dev_number;

	dev_number = IPC_NB_CH_BIDIR + IPC_NB_CH_DSPMCU;

	if (!sdma_tty_driver) {
		for (dev_id = 0; dev_id < dev_number; dev_id++) {
			class_device_destroy(sdma_tty_class,
					     MKDEV(sdma_tty_driver->major,
						   dev_id));
		}
		class_destroy(sdma_tty_class);
	}

	tty_unregister_driver(sdma_tty_driver);
	put_tty_driver(sdma_tty_driver);
}

module_init(sdma_tty_init);
module_exit(sdma_tty_exit);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("MXC SDMA TTY driver");
MODULE_LICENSE("GPL");