sclp_vt220.c 22 KB

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