sclp_vt220.c 23 KB

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