sclp_vt220.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * drivers/s390/char/sclp_vt220.c
  3. * SCLP VT220 terminal driver.
  4. *
  5. * S390 version
  6. * Copyright IBM Corp. 2003,2008
  7. * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/list.h>
  12. #include <linux/wait.h>
  13. #include <linux/timer.h>
  14. #include <linux/kernel.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_driver.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/errno.h>
  19. #include <linux/mm.h>
  20. #include <linux/major.h>
  21. #include <linux/console.h>
  22. #include <linux/kdev_t.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/reboot.h>
  27. #include <asm/uaccess.h>
  28. #include "sclp.h"
  29. #define SCLP_VT220_MAJOR TTY_MAJOR
  30. #define SCLP_VT220_MINOR 65
  31. #define SCLP_VT220_DRIVER_NAME "sclp_vt220"
  32. #define SCLP_VT220_DEVICE_NAME "ttysclp"
  33. #define SCLP_VT220_CONSOLE_NAME "ttyS"
  34. #define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */
  35. #define SCLP_VT220_BUF_SIZE 80
  36. /* Representation of a single write request */
  37. struct sclp_vt220_request {
  38. struct list_head list;
  39. struct sclp_req sclp_req;
  40. int retry_count;
  41. };
  42. /* VT220 SCCB */
  43. struct sclp_vt220_sccb {
  44. struct sccb_header header;
  45. struct evbuf_header evbuf;
  46. };
  47. #define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
  48. sizeof(struct sclp_vt220_request) - \
  49. sizeof(struct sclp_vt220_sccb))
  50. /* Structures and data needed to register tty driver */
  51. static struct tty_driver *sclp_vt220_driver;
  52. /* The tty_struct that the kernel associated with us */
  53. static struct tty_struct *sclp_vt220_tty;
  54. /* Lock to protect internal data from concurrent access */
  55. static spinlock_t sclp_vt220_lock;
  56. /* List of empty pages to be used as write request buffers */
  57. static struct list_head sclp_vt220_empty;
  58. /* List of pending requests */
  59. static struct list_head sclp_vt220_outqueue;
  60. /* Number of requests in outqueue */
  61. static int sclp_vt220_outqueue_count;
  62. /* Timer used for delaying write requests to merge subsequent messages into
  63. * a single buffer */
  64. static struct timer_list sclp_vt220_timer;
  65. /* Pointer to current request buffer which has been partially filled but not
  66. * yet sent */
  67. static struct sclp_vt220_request *sclp_vt220_current_request;
  68. /* Number of characters in current request buffer */
  69. static int sclp_vt220_buffered_chars;
  70. /* Counter controlling core driver initialization. */
  71. static int __initdata sclp_vt220_init_count;
  72. /* Flag indicating that sclp_vt220_current_request should really
  73. * have been already queued but wasn't because the SCLP was processing
  74. * another buffer */
  75. static int sclp_vt220_flush_later;
  76. static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
  77. static int __sclp_vt220_emit(struct sclp_vt220_request *request);
  78. static void sclp_vt220_emit_current(void);
  79. /* Registration structure for our interest in SCLP event buffers */
  80. static struct sclp_register sclp_vt220_register = {
  81. .send_mask = EVTYP_VT220MSG_MASK,
  82. .receive_mask = EVTYP_VT220MSG_MASK,
  83. .state_change_fn = NULL,
  84. .receiver_fn = sclp_vt220_receiver_fn
  85. };
  86. /*
  87. * Put provided request buffer back into queue and check emit pending
  88. * buffers if necessary.
  89. */
  90. static void
  91. sclp_vt220_process_queue(struct sclp_vt220_request *request)
  92. {
  93. unsigned long flags;
  94. void *page;
  95. do {
  96. /* Put buffer back to list of empty buffers */
  97. page = request->sclp_req.sccb;
  98. spin_lock_irqsave(&sclp_vt220_lock, flags);
  99. /* Move request from outqueue to empty queue */
  100. list_del(&request->list);
  101. sclp_vt220_outqueue_count--;
  102. list_add_tail((struct list_head *) page, &sclp_vt220_empty);
  103. /* Check if there is a pending buffer on the out queue. */
  104. request = NULL;
  105. if (!list_empty(&sclp_vt220_outqueue))
  106. request = list_entry(sclp_vt220_outqueue.next,
  107. struct sclp_vt220_request, list);
  108. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  109. } while (request && __sclp_vt220_emit(request));
  110. if (request == NULL && sclp_vt220_flush_later)
  111. sclp_vt220_emit_current();
  112. /* Check if the tty needs a wake up call */
  113. if (sclp_vt220_tty != NULL) {
  114. tty_wakeup(sclp_vt220_tty);
  115. }
  116. }
  117. #define SCLP_BUFFER_MAX_RETRY 1
  118. /*
  119. * Callback through which the result of a write request is reported by the
  120. * SCLP.
  121. */
  122. static void
  123. sclp_vt220_callback(struct sclp_req *request, void *data)
  124. {
  125. struct sclp_vt220_request *vt220_request;
  126. struct sclp_vt220_sccb *sccb;
  127. vt220_request = (struct sclp_vt220_request *) data;
  128. if (request->status == SCLP_REQ_FAILED) {
  129. sclp_vt220_process_queue(vt220_request);
  130. return;
  131. }
  132. sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
  133. /* Check SCLP response code and choose suitable action */
  134. switch (sccb->header.response_code) {
  135. case 0x0020 :
  136. break;
  137. case 0x05f0: /* Target resource in improper state */
  138. break;
  139. case 0x0340: /* Contained SCLP equipment check */
  140. if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
  141. break;
  142. /* Remove processed buffers and requeue rest */
  143. if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
  144. /* Not all buffers were processed */
  145. sccb->header.response_code = 0x0000;
  146. vt220_request->sclp_req.status = SCLP_REQ_FILLED;
  147. if (sclp_add_request(request) == 0)
  148. return;
  149. }
  150. break;
  151. case 0x0040: /* SCLP equipment check */
  152. if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
  153. break;
  154. sccb->header.response_code = 0x0000;
  155. vt220_request->sclp_req.status = SCLP_REQ_FILLED;
  156. if (sclp_add_request(request) == 0)
  157. return;
  158. break;
  159. default:
  160. break;
  161. }
  162. sclp_vt220_process_queue(vt220_request);
  163. }
  164. /*
  165. * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
  166. * otherwise.
  167. */
  168. static int
  169. __sclp_vt220_emit(struct sclp_vt220_request *request)
  170. {
  171. if (!(sclp_vt220_register.sclp_receive_mask & EVTYP_VT220MSG_MASK)) {
  172. request->sclp_req.status = SCLP_REQ_FAILED;
  173. return -EIO;
  174. }
  175. request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
  176. request->sclp_req.status = SCLP_REQ_FILLED;
  177. request->sclp_req.callback = sclp_vt220_callback;
  178. request->sclp_req.callback_data = (void *) request;
  179. return sclp_add_request(&request->sclp_req);
  180. }
  181. /*
  182. * Queue and emit given request.
  183. */
  184. static void
  185. sclp_vt220_emit(struct sclp_vt220_request *request)
  186. {
  187. unsigned long flags;
  188. int count;
  189. spin_lock_irqsave(&sclp_vt220_lock, flags);
  190. list_add_tail(&request->list, &sclp_vt220_outqueue);
  191. count = sclp_vt220_outqueue_count++;
  192. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  193. /* Emit only the first buffer immediately - callback takes care of
  194. * the rest */
  195. if (count == 0 && __sclp_vt220_emit(request))
  196. sclp_vt220_process_queue(request);
  197. }
  198. /*
  199. * Queue and emit current request. Return zero on success, non-zero otherwise.
  200. */
  201. static void
  202. sclp_vt220_emit_current(void)
  203. {
  204. unsigned long flags;
  205. struct sclp_vt220_request *request;
  206. struct sclp_vt220_sccb *sccb;
  207. spin_lock_irqsave(&sclp_vt220_lock, flags);
  208. request = NULL;
  209. if (sclp_vt220_current_request != NULL) {
  210. sccb = (struct sclp_vt220_sccb *)
  211. sclp_vt220_current_request->sclp_req.sccb;
  212. /* Only emit buffers with content */
  213. if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
  214. request = sclp_vt220_current_request;
  215. sclp_vt220_current_request = NULL;
  216. if (timer_pending(&sclp_vt220_timer))
  217. del_timer(&sclp_vt220_timer);
  218. }
  219. sclp_vt220_flush_later = 0;
  220. }
  221. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  222. if (request != NULL)
  223. sclp_vt220_emit(request);
  224. }
  225. #define SCLP_NORMAL_WRITE 0x00
  226. /*
  227. * Helper function to initialize a page with the sclp request structure.
  228. */
  229. static struct sclp_vt220_request *
  230. sclp_vt220_initialize_page(void *page)
  231. {
  232. struct sclp_vt220_request *request;
  233. struct sclp_vt220_sccb *sccb;
  234. /* Place request structure at end of page */
  235. request = ((struct sclp_vt220_request *)
  236. ((addr_t) page + PAGE_SIZE)) - 1;
  237. request->retry_count = 0;
  238. request->sclp_req.sccb = page;
  239. /* SCCB goes at start of page */
  240. sccb = (struct sclp_vt220_sccb *) page;
  241. memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
  242. sccb->header.length = sizeof(struct sclp_vt220_sccb);
  243. sccb->header.function_code = SCLP_NORMAL_WRITE;
  244. sccb->header.response_code = 0x0000;
  245. sccb->evbuf.type = EVTYP_VT220MSG;
  246. sccb->evbuf.length = sizeof(struct evbuf_header);
  247. return request;
  248. }
  249. static inline unsigned int
  250. sclp_vt220_space_left(struct sclp_vt220_request *request)
  251. {
  252. struct sclp_vt220_sccb *sccb;
  253. sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
  254. return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
  255. sccb->header.length;
  256. }
  257. static inline unsigned int
  258. sclp_vt220_chars_stored(struct sclp_vt220_request *request)
  259. {
  260. struct sclp_vt220_sccb *sccb;
  261. sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
  262. return sccb->evbuf.length - sizeof(struct evbuf_header);
  263. }
  264. /*
  265. * Add msg to buffer associated with request. Return the number of characters
  266. * added.
  267. */
  268. static int
  269. sclp_vt220_add_msg(struct sclp_vt220_request *request,
  270. const unsigned char *msg, int count, int convertlf)
  271. {
  272. struct sclp_vt220_sccb *sccb;
  273. void *buffer;
  274. unsigned char c;
  275. int from;
  276. int to;
  277. if (count > sclp_vt220_space_left(request))
  278. count = sclp_vt220_space_left(request);
  279. if (count <= 0)
  280. return 0;
  281. sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
  282. buffer = (void *) ((addr_t) sccb + sccb->header.length);
  283. if (convertlf) {
  284. /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
  285. for (from=0, to=0;
  286. (from < count) && (to < sclp_vt220_space_left(request));
  287. from++) {
  288. /* Retrieve character */
  289. c = msg[from];
  290. /* Perform conversion */
  291. if (c == 0x0a) {
  292. if (to + 1 < sclp_vt220_space_left(request)) {
  293. ((unsigned char *) buffer)[to++] = c;
  294. ((unsigned char *) buffer)[to++] = 0x0d;
  295. } else
  296. break;
  297. } else
  298. ((unsigned char *) buffer)[to++] = c;
  299. }
  300. sccb->header.length += to;
  301. sccb->evbuf.length += to;
  302. return from;
  303. } else {
  304. memcpy(buffer, (const void *) msg, count);
  305. sccb->header.length += count;
  306. sccb->evbuf.length += count;
  307. return count;
  308. }
  309. }
  310. /*
  311. * Emit buffer after having waited long enough for more data to arrive.
  312. */
  313. static void
  314. sclp_vt220_timeout(unsigned long data)
  315. {
  316. sclp_vt220_emit_current();
  317. }
  318. #define BUFFER_MAX_DELAY HZ/20
  319. /*
  320. * Internal implementation of the write function. Write COUNT bytes of data
  321. * from memory at BUF
  322. * to the SCLP interface. In case that the data does not fit into the current
  323. * write buffer, emit the current one and allocate a new one. If there are no
  324. * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
  325. * is non-zero, the buffer will be scheduled for emitting after a timeout -
  326. * otherwise the user has to explicitly call the flush function.
  327. * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
  328. * buffer should be converted to 0x0a 0x0d. After completion, return the number
  329. * of bytes written.
  330. */
  331. static int
  332. __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
  333. int convertlf, int may_fail)
  334. {
  335. unsigned long flags;
  336. void *page;
  337. int written;
  338. int overall_written;
  339. if (count <= 0)
  340. return 0;
  341. overall_written = 0;
  342. spin_lock_irqsave(&sclp_vt220_lock, flags);
  343. do {
  344. /* Create an sclp output buffer if none exists yet */
  345. if (sclp_vt220_current_request == NULL) {
  346. while (list_empty(&sclp_vt220_empty)) {
  347. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  348. if (may_fail)
  349. goto out;
  350. else
  351. sclp_sync_wait();
  352. spin_lock_irqsave(&sclp_vt220_lock, flags);
  353. }
  354. page = (void *) sclp_vt220_empty.next;
  355. list_del((struct list_head *) page);
  356. sclp_vt220_current_request =
  357. sclp_vt220_initialize_page(page);
  358. }
  359. /* Try to write the string to the current request buffer */
  360. written = sclp_vt220_add_msg(sclp_vt220_current_request,
  361. buf, count, convertlf);
  362. overall_written += written;
  363. if (written == count)
  364. break;
  365. /*
  366. * Not all characters could be written to the current
  367. * output buffer. Emit the buffer, create a new buffer
  368. * and then output the rest of the string.
  369. */
  370. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  371. sclp_vt220_emit_current();
  372. spin_lock_irqsave(&sclp_vt220_lock, flags);
  373. buf += written;
  374. count -= written;
  375. } while (count > 0);
  376. /* Setup timer to output current console buffer after some time */
  377. if (sclp_vt220_current_request != NULL &&
  378. !timer_pending(&sclp_vt220_timer) && do_schedule) {
  379. sclp_vt220_timer.function = sclp_vt220_timeout;
  380. sclp_vt220_timer.data = 0UL;
  381. sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
  382. add_timer(&sclp_vt220_timer);
  383. }
  384. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  385. out:
  386. return overall_written;
  387. }
  388. /*
  389. * This routine is called by the kernel to write a series of
  390. * characters to the tty device. The characters may come from
  391. * user space or kernel space. This routine will return the
  392. * number of characters actually accepted for writing.
  393. */
  394. static int
  395. sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
  396. {
  397. return __sclp_vt220_write(buf, count, 1, 0, 1);
  398. }
  399. #define SCLP_VT220_SESSION_ENDED 0x01
  400. #define SCLP_VT220_SESSION_STARTED 0x80
  401. #define SCLP_VT220_SESSION_DATA 0x00
  402. /*
  403. * Called by the SCLP to report incoming event buffers.
  404. */
  405. static void
  406. sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
  407. {
  408. char *buffer;
  409. unsigned int count;
  410. /* Ignore input if device is not open */
  411. if (sclp_vt220_tty == NULL)
  412. return;
  413. buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
  414. count = evbuf->length - sizeof(struct evbuf_header);
  415. switch (*buffer) {
  416. case SCLP_VT220_SESSION_ENDED:
  417. case SCLP_VT220_SESSION_STARTED:
  418. break;
  419. case SCLP_VT220_SESSION_DATA:
  420. /* Send input to line discipline */
  421. buffer++;
  422. count--;
  423. tty_insert_flip_string(sclp_vt220_tty, buffer, count);
  424. tty_flip_buffer_push(sclp_vt220_tty);
  425. break;
  426. }
  427. }
  428. /*
  429. * This routine is called when a particular tty device is opened.
  430. */
  431. static int
  432. sclp_vt220_open(struct tty_struct *tty, struct file *filp)
  433. {
  434. if (tty->count == 1) {
  435. sclp_vt220_tty = tty;
  436. tty->driver_data = kmalloc(SCLP_VT220_BUF_SIZE, GFP_KERNEL);
  437. if (tty->driver_data == NULL)
  438. return -ENOMEM;
  439. tty->low_latency = 0;
  440. }
  441. return 0;
  442. }
  443. /*
  444. * This routine is called when a particular tty device is closed.
  445. */
  446. static void
  447. sclp_vt220_close(struct tty_struct *tty, struct file *filp)
  448. {
  449. if (tty->count == 1) {
  450. sclp_vt220_tty = NULL;
  451. kfree(tty->driver_data);
  452. tty->driver_data = NULL;
  453. }
  454. }
  455. /*
  456. * This routine is called by the kernel to write a single
  457. * character to the tty device. If the kernel uses this routine,
  458. * it must call the flush_chars() routine (if defined) when it is
  459. * done stuffing characters into the driver.
  460. */
  461. static int
  462. sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
  463. {
  464. return __sclp_vt220_write(&ch, 1, 0, 0, 1);
  465. }
  466. /*
  467. * This routine is called by the kernel after it has written a
  468. * series of characters to the tty device using put_char().
  469. */
  470. static void
  471. sclp_vt220_flush_chars(struct tty_struct *tty)
  472. {
  473. if (sclp_vt220_outqueue_count == 0)
  474. sclp_vt220_emit_current();
  475. else
  476. sclp_vt220_flush_later = 1;
  477. }
  478. /*
  479. * This routine returns the numbers of characters the tty driver
  480. * will accept for queuing to be written. This number is subject
  481. * to change as output buffers get emptied, or if the output flow
  482. * control is acted.
  483. */
  484. static int
  485. sclp_vt220_write_room(struct tty_struct *tty)
  486. {
  487. unsigned long flags;
  488. struct list_head *l;
  489. int count;
  490. spin_lock_irqsave(&sclp_vt220_lock, flags);
  491. count = 0;
  492. if (sclp_vt220_current_request != NULL)
  493. count = sclp_vt220_space_left(sclp_vt220_current_request);
  494. list_for_each(l, &sclp_vt220_empty)
  495. count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
  496. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  497. return count;
  498. }
  499. /*
  500. * Return number of buffered chars.
  501. */
  502. static int
  503. sclp_vt220_chars_in_buffer(struct tty_struct *tty)
  504. {
  505. unsigned long flags;
  506. struct list_head *l;
  507. struct sclp_vt220_request *r;
  508. int count;
  509. spin_lock_irqsave(&sclp_vt220_lock, flags);
  510. count = 0;
  511. if (sclp_vt220_current_request != NULL)
  512. count = sclp_vt220_chars_stored(sclp_vt220_current_request);
  513. list_for_each(l, &sclp_vt220_outqueue) {
  514. r = list_entry(l, struct sclp_vt220_request, list);
  515. count += sclp_vt220_chars_stored(r);
  516. }
  517. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  518. return count;
  519. }
  520. static void
  521. __sclp_vt220_flush_buffer(void)
  522. {
  523. unsigned long flags;
  524. sclp_vt220_emit_current();
  525. spin_lock_irqsave(&sclp_vt220_lock, flags);
  526. if (timer_pending(&sclp_vt220_timer))
  527. del_timer(&sclp_vt220_timer);
  528. while (sclp_vt220_outqueue_count > 0) {
  529. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  530. sclp_sync_wait();
  531. spin_lock_irqsave(&sclp_vt220_lock, flags);
  532. }
  533. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  534. }
  535. /*
  536. * Pass on all buffers to the hardware. Return only when there are no more
  537. * buffers pending.
  538. */
  539. static void
  540. sclp_vt220_flush_buffer(struct tty_struct *tty)
  541. {
  542. sclp_vt220_emit_current();
  543. }
  544. /* Release allocated pages. */
  545. static void __init __sclp_vt220_free_pages(void)
  546. {
  547. struct list_head *page, *p;
  548. list_for_each_safe(page, p, &sclp_vt220_empty) {
  549. list_del(page);
  550. if (slab_is_available())
  551. free_page((unsigned long) page);
  552. else
  553. free_bootmem((unsigned long) page, PAGE_SIZE);
  554. }
  555. }
  556. /* Release memory and unregister from sclp core. Controlled by init counting -
  557. * only the last invoker will actually perform these actions. */
  558. static void __init __sclp_vt220_cleanup(void)
  559. {
  560. sclp_vt220_init_count--;
  561. if (sclp_vt220_init_count != 0)
  562. return;
  563. sclp_unregister(&sclp_vt220_register);
  564. __sclp_vt220_free_pages();
  565. }
  566. /* Allocate buffer pages and register with sclp core. Controlled by init
  567. * counting - only the first invoker will actually perform these actions. */
  568. static int __init __sclp_vt220_init(int num_pages)
  569. {
  570. void *page;
  571. int i;
  572. int rc;
  573. sclp_vt220_init_count++;
  574. if (sclp_vt220_init_count != 1)
  575. return 0;
  576. spin_lock_init(&sclp_vt220_lock);
  577. INIT_LIST_HEAD(&sclp_vt220_empty);
  578. INIT_LIST_HEAD(&sclp_vt220_outqueue);
  579. init_timer(&sclp_vt220_timer);
  580. sclp_vt220_current_request = NULL;
  581. sclp_vt220_buffered_chars = 0;
  582. sclp_vt220_outqueue_count = 0;
  583. sclp_vt220_tty = NULL;
  584. sclp_vt220_flush_later = 0;
  585. /* Allocate pages for output buffering */
  586. for (i = 0; i < num_pages; i++) {
  587. if (slab_is_available())
  588. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  589. else
  590. page = alloc_bootmem_low_pages(PAGE_SIZE);
  591. if (!page) {
  592. rc = -ENOMEM;
  593. goto out;
  594. }
  595. list_add_tail((struct list_head *) page, &sclp_vt220_empty);
  596. }
  597. rc = sclp_register(&sclp_vt220_register);
  598. out:
  599. if (rc) {
  600. __sclp_vt220_free_pages();
  601. sclp_vt220_init_count--;
  602. }
  603. return rc;
  604. }
  605. static const struct tty_operations sclp_vt220_ops = {
  606. .open = sclp_vt220_open,
  607. .close = sclp_vt220_close,
  608. .write = sclp_vt220_write,
  609. .put_char = sclp_vt220_put_char,
  610. .flush_chars = sclp_vt220_flush_chars,
  611. .write_room = sclp_vt220_write_room,
  612. .chars_in_buffer = sclp_vt220_chars_in_buffer,
  613. .flush_buffer = sclp_vt220_flush_buffer,
  614. };
  615. /*
  616. * Register driver with SCLP and Linux and initialize internal tty structures.
  617. */
  618. static int __init sclp_vt220_tty_init(void)
  619. {
  620. struct tty_driver *driver;
  621. int rc;
  622. /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
  623. * symmetry between VM and LPAR systems regarding ttyS1. */
  624. driver = alloc_tty_driver(1);
  625. if (!driver)
  626. return -ENOMEM;
  627. rc = __sclp_vt220_init(MAX_KMEM_PAGES);
  628. if (rc)
  629. goto out_driver;
  630. driver->owner = THIS_MODULE;
  631. driver->driver_name = SCLP_VT220_DRIVER_NAME;
  632. driver->name = SCLP_VT220_DEVICE_NAME;
  633. driver->major = SCLP_VT220_MAJOR;
  634. driver->minor_start = SCLP_VT220_MINOR;
  635. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  636. driver->subtype = SYSTEM_TYPE_TTY;
  637. driver->init_termios = tty_std_termios;
  638. driver->flags = TTY_DRIVER_REAL_RAW;
  639. tty_set_operations(driver, &sclp_vt220_ops);
  640. rc = tty_register_driver(driver);
  641. if (rc)
  642. goto out_init;
  643. sclp_vt220_driver = driver;
  644. return 0;
  645. out_init:
  646. __sclp_vt220_cleanup();
  647. out_driver:
  648. put_tty_driver(driver);
  649. return rc;
  650. }
  651. __initcall(sclp_vt220_tty_init);
  652. #ifdef CONFIG_SCLP_VT220_CONSOLE
  653. static void
  654. sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
  655. {
  656. __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
  657. }
  658. static struct tty_driver *
  659. sclp_vt220_con_device(struct console *c, int *index)
  660. {
  661. *index = 0;
  662. return sclp_vt220_driver;
  663. }
  664. static int
  665. sclp_vt220_notify(struct notifier_block *self,
  666. unsigned long event, void *data)
  667. {
  668. __sclp_vt220_flush_buffer();
  669. return NOTIFY_OK;
  670. }
  671. static struct notifier_block on_panic_nb = {
  672. .notifier_call = sclp_vt220_notify,
  673. .priority = 1,
  674. };
  675. static struct notifier_block on_reboot_nb = {
  676. .notifier_call = sclp_vt220_notify,
  677. .priority = 1,
  678. };
  679. /* Structure needed to register with printk */
  680. static struct console sclp_vt220_console =
  681. {
  682. .name = SCLP_VT220_CONSOLE_NAME,
  683. .write = sclp_vt220_con_write,
  684. .device = sclp_vt220_con_device,
  685. .flags = CON_PRINTBUFFER,
  686. .index = SCLP_VT220_CONSOLE_INDEX
  687. };
  688. static int __init
  689. sclp_vt220_con_init(void)
  690. {
  691. int rc;
  692. if (!CONSOLE_IS_SCLP)
  693. return 0;
  694. rc = __sclp_vt220_init(MAX_CONSOLE_PAGES);
  695. if (rc)
  696. return rc;
  697. /* Attach linux console */
  698. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  699. register_reboot_notifier(&on_reboot_nb);
  700. register_console(&sclp_vt220_console);
  701. return 0;
  702. }
  703. console_initcall(sclp_vt220_con_init);
  704. #endif /* CONFIG_SCLP_VT220_CONSOLE */