sclp_vt220.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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/sysrq.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/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/reboot.h>
  26. #include <linux/slab.h>
  27. #include <asm/uaccess.h>
  28. #include "sclp.h"
  29. #include "ctrlchar.h"
  30. #define SCLP_VT220_MAJOR TTY_MAJOR
  31. #define SCLP_VT220_MINOR 65
  32. #define SCLP_VT220_DRIVER_NAME "sclp_vt220"
  33. #define SCLP_VT220_DEVICE_NAME "ttysclp"
  34. #define SCLP_VT220_CONSOLE_NAME "ttyS"
  35. #define SCLP_VT220_CONSOLE_INDEX 1 /* console=ttyS1 */
  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. static struct tty_port sclp_vt220_port;
  53. /* Lock to protect internal data from concurrent access */
  54. static spinlock_t sclp_vt220_lock;
  55. /* List of empty pages to be used as write request buffers */
  56. static struct list_head sclp_vt220_empty;
  57. /* List of pending requests */
  58. static struct list_head sclp_vt220_outqueue;
  59. /* Suspend mode flag */
  60. static int sclp_vt220_suspended;
  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 void sclp_vt220_pm_event_fn(struct sclp_register *reg,
  79. enum sclp_pm_event sclp_pm_event);
  80. static int __sclp_vt220_emit(struct sclp_vt220_request *request);
  81. static void sclp_vt220_emit_current(void);
  82. /* Registration structure for SCLP output event buffers */
  83. static struct sclp_register sclp_vt220_register = {
  84. .send_mask = EVTYP_VT220MSG_MASK,
  85. .pm_event_fn = sclp_vt220_pm_event_fn,
  86. };
  87. /* Registration structure for SCLP input event buffers */
  88. static struct sclp_register sclp_vt220_register_input = {
  89. .receive_mask = EVTYP_VT220MSG_MASK,
  90. .receiver_fn = sclp_vt220_receiver_fn,
  91. };
  92. /*
  93. * Put provided request buffer back into queue and check emit pending
  94. * buffers if necessary.
  95. */
  96. static void
  97. sclp_vt220_process_queue(struct sclp_vt220_request *request)
  98. {
  99. unsigned long flags;
  100. void *page;
  101. do {
  102. /* Put buffer back to list of empty buffers */
  103. page = request->sclp_req.sccb;
  104. spin_lock_irqsave(&sclp_vt220_lock, flags);
  105. /* Move request from outqueue to empty queue */
  106. list_del(&request->list);
  107. list_add_tail((struct list_head *) page, &sclp_vt220_empty);
  108. /* Check if there is a pending buffer on the out queue. */
  109. request = NULL;
  110. if (!list_empty(&sclp_vt220_outqueue))
  111. request = list_entry(sclp_vt220_outqueue.next,
  112. struct sclp_vt220_request, list);
  113. if (!request || sclp_vt220_suspended) {
  114. sclp_vt220_queue_running = 0;
  115. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  116. break;
  117. }
  118. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  119. } while (__sclp_vt220_emit(request));
  120. if (request == NULL && sclp_vt220_flush_later)
  121. sclp_vt220_emit_current();
  122. tty_port_tty_wakeup(&sclp_vt220_port);
  123. }
  124. #define SCLP_BUFFER_MAX_RETRY 1
  125. /*
  126. * Callback through which the result of a write request is reported by the
  127. * SCLP.
  128. */
  129. static void
  130. sclp_vt220_callback(struct sclp_req *request, void *data)
  131. {
  132. struct sclp_vt220_request *vt220_request;
  133. struct sclp_vt220_sccb *sccb;
  134. vt220_request = (struct sclp_vt220_request *) data;
  135. if (request->status == SCLP_REQ_FAILED) {
  136. sclp_vt220_process_queue(vt220_request);
  137. return;
  138. }
  139. sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
  140. /* Check SCLP response code and choose suitable action */
  141. switch (sccb->header.response_code) {
  142. case 0x0020 :
  143. break;
  144. case 0x05f0: /* Target resource in improper state */
  145. break;
  146. case 0x0340: /* Contained SCLP equipment check */
  147. if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
  148. break;
  149. /* Remove processed buffers and requeue rest */
  150. if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
  151. /* Not all buffers were processed */
  152. sccb->header.response_code = 0x0000;
  153. vt220_request->sclp_req.status = SCLP_REQ_FILLED;
  154. if (sclp_add_request(request) == 0)
  155. return;
  156. }
  157. break;
  158. case 0x0040: /* SCLP equipment check */
  159. if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
  160. break;
  161. sccb->header.response_code = 0x0000;
  162. vt220_request->sclp_req.status = SCLP_REQ_FILLED;
  163. if (sclp_add_request(request) == 0)
  164. return;
  165. break;
  166. default:
  167. break;
  168. }
  169. sclp_vt220_process_queue(vt220_request);
  170. }
  171. /*
  172. * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
  173. * otherwise.
  174. */
  175. static int
  176. __sclp_vt220_emit(struct sclp_vt220_request *request)
  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. * Drop oldest console buffer if sclp_con_drop is set
  317. */
  318. static int
  319. sclp_vt220_drop_buffer(void)
  320. {
  321. struct list_head *list;
  322. struct sclp_vt220_request *request;
  323. void *page;
  324. if (!sclp_console_drop)
  325. return 0;
  326. list = sclp_vt220_outqueue.next;
  327. if (sclp_vt220_queue_running)
  328. /* The first element is in I/O */
  329. list = list->next;
  330. if (list == &sclp_vt220_outqueue)
  331. return 0;
  332. list_del(list);
  333. request = list_entry(list, struct sclp_vt220_request, list);
  334. page = request->sclp_req.sccb;
  335. list_add_tail((struct list_head *) page, &sclp_vt220_empty);
  336. return 1;
  337. }
  338. /*
  339. * Internal implementation of the write function. Write COUNT bytes of data
  340. * from memory at BUF
  341. * to the SCLP interface. In case that the data does not fit into the current
  342. * write buffer, emit the current one and allocate a new one. If there are no
  343. * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
  344. * is non-zero, the buffer will be scheduled for emitting after a timeout -
  345. * otherwise the user has to explicitly call the flush function.
  346. * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
  347. * buffer should be converted to 0x0a 0x0d. After completion, return the number
  348. * of bytes written.
  349. */
  350. static int
  351. __sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
  352. int convertlf, int may_fail)
  353. {
  354. unsigned long flags;
  355. void *page;
  356. int written;
  357. int overall_written;
  358. if (count <= 0)
  359. return 0;
  360. overall_written = 0;
  361. spin_lock_irqsave(&sclp_vt220_lock, flags);
  362. do {
  363. /* Create an sclp output buffer if none exists yet */
  364. if (sclp_vt220_current_request == NULL) {
  365. if (list_empty(&sclp_vt220_empty))
  366. sclp_console_full++;
  367. while (list_empty(&sclp_vt220_empty)) {
  368. if (may_fail || sclp_vt220_suspended)
  369. goto out;
  370. if (sclp_vt220_drop_buffer())
  371. break;
  372. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  373. sclp_sync_wait();
  374. spin_lock_irqsave(&sclp_vt220_lock, flags);
  375. }
  376. page = (void *) sclp_vt220_empty.next;
  377. list_del((struct list_head *) page);
  378. sclp_vt220_current_request =
  379. sclp_vt220_initialize_page(page);
  380. }
  381. /* Try to write the string to the current request buffer */
  382. written = sclp_vt220_add_msg(sclp_vt220_current_request,
  383. buf, count, convertlf);
  384. overall_written += written;
  385. if (written == count)
  386. break;
  387. /*
  388. * Not all characters could be written to the current
  389. * output buffer. Emit the buffer, create a new buffer
  390. * and then output the rest of the string.
  391. */
  392. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  393. sclp_vt220_emit_current();
  394. spin_lock_irqsave(&sclp_vt220_lock, flags);
  395. buf += written;
  396. count -= written;
  397. } while (count > 0);
  398. /* Setup timer to output current console buffer after some time */
  399. if (sclp_vt220_current_request != NULL &&
  400. !timer_pending(&sclp_vt220_timer) && do_schedule) {
  401. sclp_vt220_timer.function = sclp_vt220_timeout;
  402. sclp_vt220_timer.data = 0UL;
  403. sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
  404. add_timer(&sclp_vt220_timer);
  405. }
  406. out:
  407. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  408. return overall_written;
  409. }
  410. /*
  411. * This routine is called by the kernel to write a series of
  412. * characters to the tty device. The characters may come from
  413. * user space or kernel space. This routine will return the
  414. * number of characters actually accepted for writing.
  415. */
  416. static int
  417. sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
  418. {
  419. return __sclp_vt220_write(buf, count, 1, 0, 1);
  420. }
  421. #define SCLP_VT220_SESSION_ENDED 0x01
  422. #define SCLP_VT220_SESSION_STARTED 0x80
  423. #define SCLP_VT220_SESSION_DATA 0x00
  424. #ifdef CONFIG_MAGIC_SYSRQ
  425. static int sysrq_pressed;
  426. static struct sysrq_work sysrq;
  427. static void sclp_vt220_reset_session(void)
  428. {
  429. sysrq_pressed = 0;
  430. }
  431. static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
  432. {
  433. int i;
  434. for (i = 0; i < count; i++) {
  435. /* Handle magic sys request */
  436. if (buffer[i] == ('O' ^ 0100)) { /* CTRL-O */
  437. /*
  438. * If pressed again, reset sysrq_pressed
  439. * and flip CTRL-O character
  440. */
  441. sysrq_pressed = !sysrq_pressed;
  442. if (sysrq_pressed)
  443. continue;
  444. } else if (sysrq_pressed) {
  445. sysrq.key = buffer[i];
  446. schedule_sysrq_work(&sysrq);
  447. sysrq_pressed = 0;
  448. continue;
  449. }
  450. tty_insert_flip_char(&sclp_vt220_port, buffer[i], 0);
  451. }
  452. }
  453. #else
  454. static void sclp_vt220_reset_session(void)
  455. {
  456. }
  457. static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
  458. {
  459. tty_insert_flip_string(&sclp_vt220_port, buffer, count);
  460. }
  461. #endif
  462. /*
  463. * Called by the SCLP to report incoming event buffers.
  464. */
  465. static void
  466. sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
  467. {
  468. char *buffer;
  469. unsigned int count;
  470. buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
  471. count = evbuf->length - sizeof(struct evbuf_header);
  472. switch (*buffer) {
  473. case SCLP_VT220_SESSION_ENDED:
  474. case SCLP_VT220_SESSION_STARTED:
  475. sclp_vt220_reset_session();
  476. break;
  477. case SCLP_VT220_SESSION_DATA:
  478. /* Send input to line discipline */
  479. buffer++;
  480. count--;
  481. sclp_vt220_handle_input(buffer, count);
  482. tty_flip_buffer_push(&sclp_vt220_port);
  483. break;
  484. }
  485. }
  486. /*
  487. * This routine is called when a particular tty device is opened.
  488. */
  489. static int
  490. sclp_vt220_open(struct tty_struct *tty, struct file *filp)
  491. {
  492. if (tty->count == 1) {
  493. tty_port_tty_set(&sclp_vt220_port, tty);
  494. sclp_vt220_port.low_latency = 0;
  495. if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
  496. tty->winsize.ws_row = 24;
  497. tty->winsize.ws_col = 80;
  498. }
  499. }
  500. return 0;
  501. }
  502. /*
  503. * This routine is called when a particular tty device is closed.
  504. */
  505. static void
  506. sclp_vt220_close(struct tty_struct *tty, struct file *filp)
  507. {
  508. if (tty->count == 1)
  509. tty_port_tty_set(&sclp_vt220_port, NULL);
  510. }
  511. /*
  512. * This routine is called by the kernel to write a single
  513. * character to the tty device. If the kernel uses this routine,
  514. * it must call the flush_chars() routine (if defined) when it is
  515. * done stuffing characters into the driver.
  516. */
  517. static int
  518. sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
  519. {
  520. return __sclp_vt220_write(&ch, 1, 0, 0, 1);
  521. }
  522. /*
  523. * This routine is called by the kernel after it has written a
  524. * series of characters to the tty device using put_char().
  525. */
  526. static void
  527. sclp_vt220_flush_chars(struct tty_struct *tty)
  528. {
  529. if (!sclp_vt220_queue_running)
  530. sclp_vt220_emit_current();
  531. else
  532. sclp_vt220_flush_later = 1;
  533. }
  534. /*
  535. * This routine returns the numbers of characters the tty driver
  536. * will accept for queuing to be written. This number is subject
  537. * to change as output buffers get emptied, or if the output flow
  538. * control is acted.
  539. */
  540. static int
  541. sclp_vt220_write_room(struct tty_struct *tty)
  542. {
  543. unsigned long flags;
  544. struct list_head *l;
  545. int count;
  546. spin_lock_irqsave(&sclp_vt220_lock, flags);
  547. count = 0;
  548. if (sclp_vt220_current_request != NULL)
  549. count = sclp_vt220_space_left(sclp_vt220_current_request);
  550. list_for_each(l, &sclp_vt220_empty)
  551. count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
  552. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  553. return count;
  554. }
  555. /*
  556. * Return number of buffered chars.
  557. */
  558. static int
  559. sclp_vt220_chars_in_buffer(struct tty_struct *tty)
  560. {
  561. unsigned long flags;
  562. struct list_head *l;
  563. struct sclp_vt220_request *r;
  564. int count;
  565. spin_lock_irqsave(&sclp_vt220_lock, flags);
  566. count = 0;
  567. if (sclp_vt220_current_request != NULL)
  568. count = sclp_vt220_chars_stored(sclp_vt220_current_request);
  569. list_for_each(l, &sclp_vt220_outqueue) {
  570. r = list_entry(l, struct sclp_vt220_request, list);
  571. count += sclp_vt220_chars_stored(r);
  572. }
  573. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  574. return count;
  575. }
  576. /*
  577. * Pass on all buffers to the hardware. Return only when there are no more
  578. * buffers pending.
  579. */
  580. static void
  581. sclp_vt220_flush_buffer(struct tty_struct *tty)
  582. {
  583. sclp_vt220_emit_current();
  584. }
  585. /* Release allocated pages. */
  586. static void __init __sclp_vt220_free_pages(void)
  587. {
  588. struct list_head *page, *p;
  589. list_for_each_safe(page, p, &sclp_vt220_empty) {
  590. list_del(page);
  591. free_page((unsigned long) page);
  592. }
  593. }
  594. /* Release memory and unregister from sclp core. Controlled by init counting -
  595. * only the last invoker will actually perform these actions. */
  596. static void __init __sclp_vt220_cleanup(void)
  597. {
  598. sclp_vt220_init_count--;
  599. if (sclp_vt220_init_count != 0)
  600. return;
  601. sclp_unregister(&sclp_vt220_register);
  602. __sclp_vt220_free_pages();
  603. tty_port_destroy(&sclp_vt220_port);
  604. }
  605. /* Allocate buffer pages and register with sclp core. Controlled by init
  606. * counting - only the first invoker will actually perform these actions. */
  607. static int __init __sclp_vt220_init(int num_pages)
  608. {
  609. void *page;
  610. int i;
  611. int rc;
  612. sclp_vt220_init_count++;
  613. if (sclp_vt220_init_count != 1)
  614. return 0;
  615. spin_lock_init(&sclp_vt220_lock);
  616. INIT_LIST_HEAD(&sclp_vt220_empty);
  617. INIT_LIST_HEAD(&sclp_vt220_outqueue);
  618. init_timer(&sclp_vt220_timer);
  619. tty_port_init(&sclp_vt220_port);
  620. sclp_vt220_current_request = NULL;
  621. sclp_vt220_buffered_chars = 0;
  622. sclp_vt220_flush_later = 0;
  623. /* Allocate pages for output buffering */
  624. rc = -ENOMEM;
  625. for (i = 0; i < num_pages; i++) {
  626. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  627. if (!page)
  628. goto out;
  629. list_add_tail(page, &sclp_vt220_empty);
  630. }
  631. rc = sclp_register(&sclp_vt220_register);
  632. out:
  633. if (rc) {
  634. __sclp_vt220_free_pages();
  635. sclp_vt220_init_count--;
  636. tty_port_destroy(&sclp_vt220_port);
  637. }
  638. return rc;
  639. }
  640. static const struct tty_operations sclp_vt220_ops = {
  641. .open = sclp_vt220_open,
  642. .close = sclp_vt220_close,
  643. .write = sclp_vt220_write,
  644. .put_char = sclp_vt220_put_char,
  645. .flush_chars = sclp_vt220_flush_chars,
  646. .write_room = sclp_vt220_write_room,
  647. .chars_in_buffer = sclp_vt220_chars_in_buffer,
  648. .flush_buffer = sclp_vt220_flush_buffer,
  649. };
  650. /*
  651. * Register driver with SCLP and Linux and initialize internal tty structures.
  652. */
  653. static int __init sclp_vt220_tty_init(void)
  654. {
  655. struct tty_driver *driver;
  656. int rc;
  657. /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
  658. * symmetry between VM and LPAR systems regarding ttyS1. */
  659. driver = alloc_tty_driver(1);
  660. if (!driver)
  661. return -ENOMEM;
  662. rc = __sclp_vt220_init(MAX_KMEM_PAGES);
  663. if (rc)
  664. goto out_driver;
  665. driver->driver_name = SCLP_VT220_DRIVER_NAME;
  666. driver->name = SCLP_VT220_DEVICE_NAME;
  667. driver->major = SCLP_VT220_MAJOR;
  668. driver->minor_start = SCLP_VT220_MINOR;
  669. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  670. driver->subtype = SYSTEM_TYPE_TTY;
  671. driver->init_termios = tty_std_termios;
  672. driver->flags = TTY_DRIVER_REAL_RAW;
  673. tty_set_operations(driver, &sclp_vt220_ops);
  674. tty_port_link_device(&sclp_vt220_port, driver, 0);
  675. rc = tty_register_driver(driver);
  676. if (rc)
  677. goto out_init;
  678. rc = sclp_register(&sclp_vt220_register_input);
  679. if (rc)
  680. goto out_reg;
  681. sclp_vt220_driver = driver;
  682. return 0;
  683. out_reg:
  684. tty_unregister_driver(driver);
  685. out_init:
  686. __sclp_vt220_cleanup();
  687. out_driver:
  688. put_tty_driver(driver);
  689. return rc;
  690. }
  691. __initcall(sclp_vt220_tty_init);
  692. static void __sclp_vt220_flush_buffer(void)
  693. {
  694. unsigned long flags;
  695. sclp_vt220_emit_current();
  696. spin_lock_irqsave(&sclp_vt220_lock, flags);
  697. if (timer_pending(&sclp_vt220_timer))
  698. del_timer(&sclp_vt220_timer);
  699. while (sclp_vt220_queue_running) {
  700. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  701. sclp_sync_wait();
  702. spin_lock_irqsave(&sclp_vt220_lock, flags);
  703. }
  704. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  705. }
  706. /*
  707. * Resume console: If there are cached messages, emit them.
  708. */
  709. static void sclp_vt220_resume(void)
  710. {
  711. unsigned long flags;
  712. spin_lock_irqsave(&sclp_vt220_lock, flags);
  713. sclp_vt220_suspended = 0;
  714. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  715. sclp_vt220_emit_current();
  716. }
  717. /*
  718. * Suspend console: Set suspend flag and flush console
  719. */
  720. static void sclp_vt220_suspend(void)
  721. {
  722. unsigned long flags;
  723. spin_lock_irqsave(&sclp_vt220_lock, flags);
  724. sclp_vt220_suspended = 1;
  725. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  726. __sclp_vt220_flush_buffer();
  727. }
  728. static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
  729. enum sclp_pm_event sclp_pm_event)
  730. {
  731. switch (sclp_pm_event) {
  732. case SCLP_PM_EVENT_FREEZE:
  733. sclp_vt220_suspend();
  734. break;
  735. case SCLP_PM_EVENT_RESTORE:
  736. case SCLP_PM_EVENT_THAW:
  737. sclp_vt220_resume();
  738. break;
  739. }
  740. }
  741. #ifdef CONFIG_SCLP_VT220_CONSOLE
  742. static void
  743. sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
  744. {
  745. __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
  746. }
  747. static struct tty_driver *
  748. sclp_vt220_con_device(struct console *c, int *index)
  749. {
  750. *index = 0;
  751. return sclp_vt220_driver;
  752. }
  753. static int
  754. sclp_vt220_notify(struct notifier_block *self,
  755. unsigned long event, void *data)
  756. {
  757. __sclp_vt220_flush_buffer();
  758. return NOTIFY_OK;
  759. }
  760. static struct notifier_block on_panic_nb = {
  761. .notifier_call = sclp_vt220_notify,
  762. .priority = 1,
  763. };
  764. static struct notifier_block on_reboot_nb = {
  765. .notifier_call = sclp_vt220_notify,
  766. .priority = 1,
  767. };
  768. /* Structure needed to register with printk */
  769. static struct console sclp_vt220_console =
  770. {
  771. .name = SCLP_VT220_CONSOLE_NAME,
  772. .write = sclp_vt220_con_write,
  773. .device = sclp_vt220_con_device,
  774. .flags = CON_PRINTBUFFER,
  775. .index = SCLP_VT220_CONSOLE_INDEX
  776. };
  777. static int __init
  778. sclp_vt220_con_init(void)
  779. {
  780. int rc;
  781. rc = __sclp_vt220_init(sclp_console_pages);
  782. if (rc)
  783. return rc;
  784. /* Attach linux console */
  785. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  786. register_reboot_notifier(&on_reboot_nb);
  787. register_console(&sclp_vt220_console);
  788. return 0;
  789. }
  790. console_initcall(sclp_vt220_con_init);
  791. #endif /* CONFIG_SCLP_VT220_CONSOLE */