Driver/tiny_tty/tiny_open()

Last-modified: 2007-09-27 (木) 14:35:49

090 :static int tiny_open(struct tty_struct *tty, struct file *file)
091 :{
092 : struct tiny_serial *tiny;
093 : struct timer_list *timer;
094 : int index;
095 :
096 : /* initialize the pointer in case something fails */
097 : tty->driver_data = NULL;
098 :
099 : /* get the serial object associated with this tty pointer */
100 : index = tty->index;
101 : tiny = tiny_table[index];
102 : if (tiny == NULL) {

もしtinyにメモリが割り振られていなければ

103 : /* first time accessing this device, let's create it */
104 : tiny = kmalloc(sizeof(*tiny), GFP_KERNEL);
105 : if (!tiny)
106 : return -ENOMEM;
107 :
108 : init_MUTEX(&tiny->sem);
109 : tiny->open_count = 0;
110 : tiny->timer = NULL;
111 :
112 : tiny_table[index] = tiny;
113 : }
114 :
115 : down(&tiny->sem);
116 :
117 : /* save our structure within the tty structure */
118 : tty->driver_data = tiny;

tinyをttyに埋め込んで後で使えるようにする。

119 : tiny->tty = tty;
120 :
121 : ++tiny->open_count;
122 : if (tiny->open_count == 1) {

open_count == 1ということは最初のオープンだから、H/Wの初期化が必要

123 : /* this is the first time this port is opened */
124 : /* do any hardware initialization needed here */
125 :
126 : /* create our timer and submit it */
127 : if (!tiny->timer) {

tiny->timerが割り振られていなければ、

128 : timer = kmalloc(sizeof(*timer), GFP_KERNEL);

メモリの割り当て

129 : if (!timer) {
130 : up(&tiny->sem);
131 : return -ENOMEM;
132 : }
133 : tiny->timer = timer;
134 : }
135 : tiny->timer->data = (unsigned long )tiny;
136 : tiny->timer->expires = jiffies + DELAY_TIME;
137 : tiny->timer->function = tiny_timer;
138 : add_timer(tiny->timer);
139 : }
140 :
141 : up(&tiny->sem);
142 : return 0;
143 :}