xref: /minix/minix/lib/libchardriver/chardriver.c (revision 83133719)
1 /* This file contains the device independent character driver interface.
2  *
3  * Character drivers support the following requests. Message format m10 is
4  * used. Field names are prefixed with CDEV_. Separate field names are used for
5  * the "access", "ops", "user", and "request" fields.
6  *
7  *     m_type      MINOR    GRANT   COUNT   FLAGS   ID     POS_LO    POS_HI
8  * +-------------+-------+--------+-------+-------+------+---------+--------+
9  * | CDEV_OPEN   | minor | access | user  |       |  id  |         |        |
10  * |-------------+-------+--------+-------+-------+------+---------+--------|
11  * | CDEV_CLOSE  | minor |        |       |       |  id  |         |        |
12  * |-------------+-------+--------+-------+-------+------+---------+--------|
13  * | CDEV_READ   | minor | grant  | bytes | flags |  id  |     position     |
14  * |-------------+-------+--------+-------+-------+------+---------+--------|
15  * | CDEV_WRITE  | minor | grant  | bytes | flags |  id  |     position     |
16  * |-------------+-------+--------+-------+-------+------+---------+--------|
17  * | CDEV_IOCTL  | minor | grant  | user  | flags |  id  | request |        |
18  * |-------------+-------+--------+-------+-------+------+---------+--------|
19  * | CDEV_CANCEL | minor |        |       |       |  id  |         |        |
20  * |-------------+-------+--------+-------+-------+------+---------+--------|
21  * | CDEV_SELECT | minor |  ops   |       |       |      |         |        |
22  * --------------------------------------------------------------------------
23  *
24  * The following reply messages are used.
25  *
26  *       m_type        MINOR   STATUS                ID
27  * +-----------------+-------+--------+-----+-----+------+---------+--------+
28  * | CDEV_REPLY      |       | status |     |     |  id  |         |        |
29  * |-----------------+-------+--------+-----+-----+------+---------+--------|
30  * | CDEV_SEL1_REPLY | minor | status |     |     |      |         |        |
31  * |-----------------+-------+--------+-----+-----+------+---------+--------|
32  * | CDEV_SEL2_REPLY | minor | status |     |     |      |         |        |
33  * --------------------------------------------------------------------------
34  *
35  * Changes:
36  *   Sep 01, 2013   complete rewrite of the API  (D.C. van Moolenboek)
37  *   Aug 20, 2013   retire synchronous protocol  (D.C. van Moolenbroek)
38  *   Oct 16, 2011   split character and block protocol  (D.C. van Moolenbroek)
39  *   Aug 27, 2011   move common functions into driver.c  (A. Welzel)
40  *   Jul 25, 2005   added SYS_SIG type for signals  (Jorrit N. Herder)
41  *   Sep 15, 2004   added SYN_ALARM type for timeouts  (Jorrit N. Herder)
42  *   Jul 23, 2004   removed kernel dependencies  (Jorrit N. Herder)
43  *   Apr 02, 1992   constructed from AT wini and floppy driver  (Kees J. Bot)
44  */
45 
46 #include <assert.h>
47 
48 #include <minix/drivers.h>
49 #include <minix/chardriver.h>
50 #include <minix/ds.h>
51 
52 static int running;
53 
54 /* Management data for opened devices. */
55 static devminor_t open_devs[MAX_NR_OPEN_DEVICES];
56 static int next_open_devs_slot = 0;
57 
58 /*===========================================================================*
59  *				clear_open_devs				     *
60  *===========================================================================*/
61 static void clear_open_devs(void)
62 {
63 /* Reset the set of previously opened minor devices. */
64   next_open_devs_slot = 0;
65 }
66 
67 /*===========================================================================*
68  *				is_open_dev				     *
69  *===========================================================================*/
70 static int is_open_dev(devminor_t minor)
71 {
72 /* Check whether the given minor device has previously been opened. */
73   int i;
74 
75   for (i = 0; i < next_open_devs_slot; i++)
76 	if (open_devs[i] == minor)
77 		return TRUE;
78 
79   return FALSE;
80 }
81 
82 /*===========================================================================*
83  *				set_open_dev				     *
84  *===========================================================================*/
85 static void set_open_dev(devminor_t minor)
86 {
87 /* Mark the given minor device as having been opened. */
88 
89   if (next_open_devs_slot >= MAX_NR_OPEN_DEVICES)
90 	panic("out of slots for open devices");
91 
92   open_devs[next_open_devs_slot] = minor;
93   next_open_devs_slot++;
94 }
95 
96 /*===========================================================================*
97  *				chardriver_announce			     *
98  *===========================================================================*/
99 void chardriver_announce(void)
100 {
101 /* Announce we are up after a fresh start or restart. */
102   int r;
103   char key[DS_MAX_KEYLEN];
104   char label[DS_MAX_KEYLEN];
105   char *driver_prefix = "drv.chr.";
106 
107   /* Callers are allowed to use ipc_sendrec to communicate with drivers.
108    * For this reason, there may blocked callers when a driver restarts.
109    * Ask the kernel to unblock them (if any).
110    */
111   if ((r = sys_statectl(SYS_STATE_CLEAR_IPC_REFS)) != OK)
112 	panic("chardriver_announce: sys_statectl failed: %d", r);
113 
114   /* Publish a driver up event. */
115   if ((r = ds_retrieve_label_name(label, sef_self())) != OK)
116 	panic("chardriver_announce: unable to get own label: %d", r);
117 
118   snprintf(key, DS_MAX_KEYLEN, "%s%s", driver_prefix, label);
119   if ((r = ds_publish_u32(key, DS_DRIVER_UP, DSF_OVERWRITE)) != OK)
120 	panic("chardriver_announce: unable to publish driver up event: %d", r);
121 
122   /* Expect an open for any device before serving regular driver requests. */
123   clear_open_devs();
124 }
125 
126 /*===========================================================================*
127  *				chardriver_reply_task			     *
128  *===========================================================================*/
129 void chardriver_reply_task(endpoint_t endpt, cdev_id_t id, int r)
130 {
131 /* Reply to a (read, write, ioctl) task request that was suspended earlier.
132  * Not-so-well-written drivers may use this function to send a reply to a
133  * request that is being processed right now, and then return EDONTREPLY later.
134  */
135   message m_reply;
136 
137   if (r == EDONTREPLY || r == SUSPEND)
138 	panic("chardriver: bad task reply: %d", r);
139 
140   memset(&m_reply, 0, sizeof(m_reply));
141 
142   m_reply.m_type = CDEV_REPLY;
143   m_reply.m_lchardriver_vfs_reply.status = r;
144   m_reply.m_lchardriver_vfs_reply.id = id;
145 
146   if ((r = asynsend3(endpt, &m_reply, AMF_NOREPLY)) != OK)
147 	printf("chardriver_reply_task: send to %d failed: %d\n", endpt, r);
148 }
149 
150 /*===========================================================================*
151  *				chardriver_reply_select			     *
152  *===========================================================================*/
153 void chardriver_reply_select(endpoint_t endpt, devminor_t minor, int r)
154 {
155 /* Reply to a select request with a status update. This must not be used to
156  * reply to a select request that is being processed right now.
157  */
158   message m_reply;
159 
160   /* Replying with an error is allowed (if unusual). */
161   if (r == EDONTREPLY || r == SUSPEND)
162 	panic("chardriver: bad select reply: %d", r);
163 
164   memset(&m_reply, 0, sizeof(m_reply));
165 
166   m_reply.m_type = CDEV_SEL2_REPLY;
167   m_reply.m_lchardriver_vfs_sel2.minor = minor;
168   m_reply.m_lchardriver_vfs_sel2.status = r;
169 
170   if ((r = asynsend3(endpt, &m_reply, AMF_NOREPLY)) != OK)
171 	printf("chardriver_reply_select: send to %d failed: %d\n", endpt, r);
172 }
173 
174 /*===========================================================================*
175  *				send_reply				     *
176  *===========================================================================*/
177 static void send_reply(endpoint_t endpt, message *m_ptr, int ipc_status)
178 {
179 /* Send a reply message to a request. */
180   int r;
181 
182   /* If we would block sending the message, send it asynchronously. */
183   if (IPC_STATUS_CALL(ipc_status) == SENDREC)
184 	r = ipc_sendnb(endpt, m_ptr);
185   else
186 	r = asynsend3(endpt, m_ptr, AMF_NOREPLY);
187 
188   if (r != OK)
189 	printf("chardriver: unable to send reply to %d: %d\n", endpt, r);
190 }
191 
192 /*===========================================================================*
193  *				chardriver_reply			     *
194  *===========================================================================*/
195 static void chardriver_reply(message *mess, int ipc_status, int r)
196 {
197 /* Prepare and send a reply message. */
198   message reply_mess;
199 
200   /* If the EDONTREPLY pseudo-reply is given, we do not reply. This is however
201    * allowed only for blocking task calls. Perform a sanity check.
202    */
203   if (r == EDONTREPLY) {
204 	switch (mess->m_type) {
205 	case CDEV_READ:
206 	case CDEV_WRITE:
207 	case CDEV_IOCTL:
208 		/* FIXME: we should be able to check FLAGS against
209 		 * CDEV_NONBLOCK here, but in practice, several drivers do not
210 		 * send a reply through this path (eg TTY) or simply do not
211 		 * implement nonblocking calls properly (eg audio, LWIP).
212 		 */
213 #if 0
214 		if (mess->m_vfs_lchardriver_readwrite.flags & CDEV_NONBLOCK)
215 			panic("chardriver: cannot suspend nonblocking I/O");
216 #endif
217 		/*fall-through*/
218 	case CDEV_CANCEL:
219 		return;	/* alright */
220 	default:
221 		panic("chardriver: cannot suspend request %d", mess->m_type);
222 	}
223   }
224 
225   if (r == SUSPEND)
226 	panic("chardriver: SUSPEND should not be used anymore");
227 
228   /* Do not reply with ERESTART. The only possible caller, VFS, will find out
229    * through other means when we have restarted, and is not (fully) ready to
230    * deal with ERESTART errors.
231    */
232   if (r == ERESTART)
233 	return;
234 
235   memset(&reply_mess, 0, sizeof(reply_mess));
236 
237   switch (mess->m_type) {
238   case CDEV_OPEN:
239   case CDEV_CLOSE:
240 	reply_mess.m_type = CDEV_REPLY;
241 	reply_mess.m_lchardriver_vfs_reply.status = r;
242 	reply_mess.m_lchardriver_vfs_reply.id =
243 		mess->m_vfs_lchardriver_openclose.id;
244 	break;
245 
246   case CDEV_READ:
247   case CDEV_WRITE:
248   case CDEV_IOCTL:
249 	reply_mess.m_type = CDEV_REPLY;
250 	reply_mess.m_lchardriver_vfs_reply.status = r;
251 	reply_mess.m_lchardriver_vfs_reply.id =
252 		mess->m_vfs_lchardriver_readwrite.id;
253 	break;
254 
255   case CDEV_CANCEL: /* For cancel, this is a reply to the original request! */
256 	reply_mess.m_type = CDEV_REPLY;
257 	reply_mess.m_lchardriver_vfs_reply.status = r;
258 	reply_mess.m_lchardriver_vfs_reply.id =
259 		mess->m_vfs_lchardriver_cancel.id;
260 	break;
261 
262   case CDEV_SELECT:
263 	reply_mess.m_type = CDEV_SEL1_REPLY;
264 	reply_mess.m_lchardriver_vfs_sel1.status = r;
265 	reply_mess.m_lchardriver_vfs_sel1.minor =
266 		mess->m_vfs_lchardriver_select.minor;
267 	break;
268 
269   default:
270 	panic("chardriver: unknown request %d", mess->m_type);
271   }
272 
273   send_reply(mess->m_source, &reply_mess, ipc_status);
274 }
275 
276 /*===========================================================================*
277  *				do_open					     *
278  *===========================================================================*/
279 static int do_open(struct chardriver *cdp, message *m_ptr)
280 {
281 /* Open a minor device. */
282   endpoint_t user_endpt;
283   devminor_t minor;
284   int r, access;
285 
286   /* Default action if no open hook is in place. */
287   if (cdp->cdr_open == NULL)
288 	return OK;
289 
290   /* Call the open hook. */
291   minor = m_ptr->m_vfs_lchardriver_openclose.minor;
292   access = m_ptr->m_vfs_lchardriver_openclose.access;
293   user_endpt = m_ptr->m_vfs_lchardriver_openclose.user;
294 
295   r = cdp->cdr_open(minor, access, user_endpt);
296 
297   /* If the device has been cloned, mark the new minor as open too. */
298   if (r >= 0 && (r & CDEV_CLONED)) {
299 	minor = r & ~(CDEV_CLONED | CDEV_CTTY);
300 	if (!is_open_dev(minor))
301 		set_open_dev(minor);
302   }
303 
304   return r;
305 }
306 
307 /*===========================================================================*
308  *				do_close				     *
309  *===========================================================================*/
310 static int do_close(struct chardriver *cdp, message *m_ptr)
311 {
312 /* Close a minor device. */
313   devminor_t minor;
314 
315   /* Default action if no close hook is in place. */
316   if (cdp->cdr_close == NULL)
317 	return OK;
318 
319   /* Call the close hook. */
320   minor = m_ptr->m_vfs_lchardriver_openclose.minor;
321 
322   return cdp->cdr_close(minor);
323 }
324 
325 /*===========================================================================*
326  *				do_trasnfer				     *
327  *===========================================================================*/
328 static int do_transfer(struct chardriver *cdp, message *m_ptr, int do_write)
329 {
330 /* Carry out a read or write task request. */
331   devminor_t minor;
332   off_t position;
333   endpoint_t endpt;
334   cp_grant_id_t grant;
335   size_t size;
336   int flags;
337   cdev_id_t id;
338   ssize_t r;
339 
340   minor = m_ptr->m_vfs_lchardriver_readwrite.minor;
341   position = m_ptr->m_vfs_lchardriver_readwrite.pos;
342   endpt = m_ptr->m_source;
343   grant = m_ptr->m_vfs_lchardriver_readwrite.grant;
344   size = m_ptr->m_vfs_lchardriver_readwrite.count;
345   flags = m_ptr->m_vfs_lchardriver_readwrite.flags;
346   id = m_ptr->m_vfs_lchardriver_readwrite.id;
347 
348   /* Call the read/write hook, if the appropriate one is in place. */
349   if (!do_write && cdp->cdr_read != NULL)
350 	r = cdp->cdr_read(minor, position, endpt, grant, size, flags, id);
351   else if (do_write && cdp->cdr_write != NULL)
352 	r = cdp->cdr_write(minor, position, endpt, grant, size, flags, id);
353   else
354 	r = EIO; /* Default action if no read/write hook is in place. */
355 
356   return r;
357 }
358 
359 /*===========================================================================*
360  *				do_ioctl				     *
361  *===========================================================================*/
362 static int do_ioctl(struct chardriver *cdp, message *m_ptr)
363 {
364 /* Carry out an I/O control task request. */
365   devminor_t minor;
366   unsigned long request;
367   cp_grant_id_t grant;
368   endpoint_t endpt, user_endpt;
369   int flags;
370   cdev_id_t id;
371 
372   /* Default action if no ioctl hook is in place. */
373   if (cdp->cdr_ioctl == NULL)
374 	return ENOTTY;
375 
376   /* Call the ioctl hook. */
377   minor = m_ptr->m_vfs_lchardriver_readwrite.minor;
378   request = m_ptr->m_vfs_lchardriver_readwrite.request;
379   endpt = m_ptr->m_source;
380   grant = m_ptr->m_vfs_lchardriver_readwrite.grant;
381   flags = m_ptr->m_vfs_lchardriver_readwrite.flags;
382   user_endpt = m_ptr->m_vfs_lchardriver_readwrite.user;
383   id = m_ptr->m_vfs_lchardriver_readwrite.id;
384 
385   return cdp->cdr_ioctl(minor, request, endpt, grant, flags, user_endpt, id);
386 }
387 
388 /*===========================================================================*
389  *				do_cancel				     *
390  *===========================================================================*/
391 static int do_cancel(struct chardriver *cdp, message *m_ptr)
392 {
393 /* Cancel a suspended (read, write, ioctl) task request. The original request
394  * may already have finished, in which case no reply should be sent.
395  */
396   devminor_t minor;
397   endpoint_t endpt;
398   cdev_id_t id;
399 
400   /* Default action if no cancel hook is in place: let the request finish. */
401   if (cdp->cdr_cancel == NULL)
402 	return EDONTREPLY;
403 
404   /* Call the cancel hook. */
405   minor = m_ptr->m_vfs_lchardriver_cancel.minor;
406   endpt = m_ptr->m_source;
407   id = m_ptr->m_vfs_lchardriver_cancel.id;
408 
409   return cdp->cdr_cancel(minor, endpt, id);
410 }
411 
412 /*===========================================================================*
413  *				do_select				     *
414  *===========================================================================*/
415 static int do_select(struct chardriver *cdp, message *m_ptr)
416 {
417 /* Perform a select query on a minor device. */
418   devminor_t minor;
419   unsigned int ops;
420   endpoint_t endpt;
421 
422   /* Default action if no select hook is in place. */
423   if (cdp->cdr_select == NULL)
424 	return EBADF;
425 
426   /* Call the select hook. */
427   minor = m_ptr->m_vfs_lchardriver_select.minor;
428   ops = m_ptr->m_vfs_lchardriver_select.ops;
429   endpt = m_ptr->m_source;
430 
431   return cdp->cdr_select(minor, ops, endpt);
432 }
433 
434 /*===========================================================================*
435  *				do_block_open				     *
436  *===========================================================================*/
437 static void do_block_open(message *m_ptr, int ipc_status)
438 {
439 /* Reply to a block driver open request stating there is no such device. */
440   message m_reply;
441 
442   memset(&m_reply, 0, sizeof(m_reply));
443 
444   m_reply.m_type = BDEV_REPLY;
445   m_reply.m_lblockdriver_lbdev_reply.status = ENXIO;
446   m_reply.m_lblockdriver_lbdev_reply.id = m_ptr->m_lbdev_lblockdriver_msg.id;
447 
448   send_reply(m_ptr->m_source, &m_reply, ipc_status);
449 }
450 
451 /*===========================================================================*
452  *				chardriver_process			     *
453  *===========================================================================*/
454 void chardriver_process(struct chardriver *cdp, message *m_ptr, int ipc_status)
455 {
456 /* Call the appropiate driver function, based on the type of request. Send a
457  * reply to the caller if necessary.
458  */
459   int r, reply;
460 
461   /* Check for notifications first. We never reply to notifications. */
462   if (is_ipc_notify(ipc_status)) {
463 	switch (_ENDPOINT_P(m_ptr->m_source)) {
464 	case HARDWARE:
465 		if (cdp->cdr_intr)
466 			cdp->cdr_intr(m_ptr->m_notify.interrupts);
467 		break;
468 
469 	case CLOCK:
470 		if (cdp->cdr_alarm)
471 			cdp->cdr_alarm(m_ptr->m_notify.timestamp);
472 		break;
473 
474 	default:
475 		if (cdp->cdr_other)
476 			cdp->cdr_other(m_ptr, ipc_status);
477 	}
478 
479 	return; /* do not send a reply */
480   }
481 
482   /* Reply to block driver open requests with an error code. Otherwise, if
483    * someone creates a block device node for a character driver, opening that
484    * device node will cause the corresponding VFS thread to block forever.
485    */
486   if (m_ptr->m_type == BDEV_OPEN) {
487 	do_block_open(m_ptr, ipc_status);
488 
489 	return;
490   }
491 
492   if (IS_CDEV_RQ(m_ptr->m_type)) {
493 	int minor;
494 
495 	/* Try to retrieve minor device number */
496 	r = chardriver_get_minor(m_ptr, &minor);
497 
498 	if (OK != r)
499 		return;
500 
501 	/* We might get spurious requests if the driver has been restarted.
502 	 * Deny any requests on devices that have not previously been opened.
503 	 */
504 	if (!is_open_dev(minor)) {
505 		/* Ignore spurious requests for unopened devices. */
506 		if (m_ptr->m_type != CDEV_OPEN)
507 			return; /* do not send a reply */
508 
509 		/* Mark the device as opened otherwise. */
510 		set_open_dev(minor);
511 	}
512   }
513 
514   /* Call the appropriate function(s) for this request. */
515   switch (m_ptr->m_type) {
516   case CDEV_OPEN:	r = do_open(cdp, m_ptr);		break;
517   case CDEV_CLOSE:	r = do_close(cdp, m_ptr);		break;
518   case CDEV_READ:	r = do_transfer(cdp, m_ptr, FALSE);	break;
519   case CDEV_WRITE:	r = do_transfer(cdp, m_ptr, TRUE);	break;
520   case CDEV_IOCTL:	r = do_ioctl(cdp, m_ptr);		break;
521   case CDEV_CANCEL:	r = do_cancel(cdp, m_ptr);		break;
522   case CDEV_SELECT:	r = do_select(cdp, m_ptr);		break;
523   default:
524 	if (cdp->cdr_other)
525 		cdp->cdr_other(m_ptr, ipc_status);
526 	return; /* do not send a reply */
527   }
528 
529   chardriver_reply(m_ptr, ipc_status, r);
530 }
531 
532 /*===========================================================================*
533  *				chardriver_terminate			     *
534  *===========================================================================*/
535 void chardriver_terminate(void)
536 {
537 /* Break out of the main loop after finishing the current request. */
538 
539   running = FALSE;
540 
541   sef_cancel();
542 }
543 
544 /*===========================================================================*
545  *				chardriver_task				     *
546  *===========================================================================*/
547 void chardriver_task(struct chardriver *cdp)
548 {
549 /* Main program of any character device driver task. */
550   int r, ipc_status;
551   message mess;
552 
553   running = TRUE;
554 
555   /* Here is the main loop of the character driver task.  It waits for a
556    * message, carries it out, and sends a reply.
557    */
558   while (running) {
559 	if ((r = sef_receive_status(ANY, &mess, &ipc_status)) != OK) {
560 		if (r == EINTR && !running)
561 			break;
562 
563 		panic("chardriver: sef_receive_status failed: %d", r);
564 	}
565 
566 	chardriver_process(cdp, &mess, ipc_status);
567   }
568 }
569 
570 /*===========================================================================*
571  *				chardriver_get_minor			     *
572  *===========================================================================*/
573 int chardriver_get_minor(message *m, devminor_t *minor)
574 {
575   assert(NULL != m);
576   assert(NULL != minor);
577 
578   switch(m->m_type)
579   {
580 	case CDEV_OPEN:
581 	case CDEV_CLOSE:
582 	    *minor = m->m_vfs_lchardriver_openclose.minor;
583 	    return OK;
584 	case CDEV_CANCEL:
585 	    *minor = m->m_vfs_lchardriver_cancel.minor;
586 	    return OK;
587 	case CDEV_SELECT:
588 	    *minor = m->m_vfs_lchardriver_select.minor;
589 	    return OK;
590 	case CDEV_READ:
591 	case CDEV_WRITE:
592 	case CDEV_IOCTL:
593 	    *minor = m->m_vfs_lchardriver_readwrite.minor;
594 	    return OK;
595 	default:
596 	    return EINVAL;
597   }
598 }
599