sclp_vt220.c 21 KB

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