sclp_vt220.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. * 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. /*
  425. * Called by the SCLP to report incoming event buffers.
  426. */
  427. static void
  428. sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
  429. {
  430. char *buffer;
  431. unsigned int count;
  432. buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
  433. count = evbuf->length - sizeof(struct evbuf_header);
  434. switch (*buffer) {
  435. case SCLP_VT220_SESSION_ENDED:
  436. case SCLP_VT220_SESSION_STARTED:
  437. break;
  438. case SCLP_VT220_SESSION_DATA:
  439. /* Send input to line discipline */
  440. buffer++;
  441. count--;
  442. tty_insert_flip_string(&sclp_vt220_port, buffer, count);
  443. tty_flip_buffer_push(&sclp_vt220_port);
  444. break;
  445. }
  446. }
  447. /*
  448. * This routine is called when a particular tty device is opened.
  449. */
  450. static int
  451. sclp_vt220_open(struct tty_struct *tty, struct file *filp)
  452. {
  453. if (tty->count == 1) {
  454. tty_port_tty_set(&sclp_vt220_port, tty);
  455. sclp_vt220_port.low_latency = 0;
  456. if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
  457. tty->winsize.ws_row = 24;
  458. tty->winsize.ws_col = 80;
  459. }
  460. }
  461. return 0;
  462. }
  463. /*
  464. * This routine is called when a particular tty device is closed.
  465. */
  466. static void
  467. sclp_vt220_close(struct tty_struct *tty, struct file *filp)
  468. {
  469. if (tty->count == 1)
  470. tty_port_tty_set(&sclp_vt220_port, NULL);
  471. }
  472. /*
  473. * This routine is called by the kernel to write a single
  474. * character to the tty device. If the kernel uses this routine,
  475. * it must call the flush_chars() routine (if defined) when it is
  476. * done stuffing characters into the driver.
  477. */
  478. static int
  479. sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
  480. {
  481. return __sclp_vt220_write(&ch, 1, 0, 0, 1);
  482. }
  483. /*
  484. * This routine is called by the kernel after it has written a
  485. * series of characters to the tty device using put_char().
  486. */
  487. static void
  488. sclp_vt220_flush_chars(struct tty_struct *tty)
  489. {
  490. if (!sclp_vt220_queue_running)
  491. sclp_vt220_emit_current();
  492. else
  493. sclp_vt220_flush_later = 1;
  494. }
  495. /*
  496. * This routine returns the numbers of characters the tty driver
  497. * will accept for queuing to be written. This number is subject
  498. * to change as output buffers get emptied, or if the output flow
  499. * control is acted.
  500. */
  501. static int
  502. sclp_vt220_write_room(struct tty_struct *tty)
  503. {
  504. unsigned long flags;
  505. struct list_head *l;
  506. int count;
  507. spin_lock_irqsave(&sclp_vt220_lock, flags);
  508. count = 0;
  509. if (sclp_vt220_current_request != NULL)
  510. count = sclp_vt220_space_left(sclp_vt220_current_request);
  511. list_for_each(l, &sclp_vt220_empty)
  512. count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
  513. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  514. return count;
  515. }
  516. /*
  517. * Return number of buffered chars.
  518. */
  519. static int
  520. sclp_vt220_chars_in_buffer(struct tty_struct *tty)
  521. {
  522. unsigned long flags;
  523. struct list_head *l;
  524. struct sclp_vt220_request *r;
  525. int count;
  526. spin_lock_irqsave(&sclp_vt220_lock, flags);
  527. count = 0;
  528. if (sclp_vt220_current_request != NULL)
  529. count = sclp_vt220_chars_stored(sclp_vt220_current_request);
  530. list_for_each(l, &sclp_vt220_outqueue) {
  531. r = list_entry(l, struct sclp_vt220_request, list);
  532. count += sclp_vt220_chars_stored(r);
  533. }
  534. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  535. return count;
  536. }
  537. /*
  538. * Pass on all buffers to the hardware. Return only when there are no more
  539. * buffers pending.
  540. */
  541. static void
  542. sclp_vt220_flush_buffer(struct tty_struct *tty)
  543. {
  544. sclp_vt220_emit_current();
  545. }
  546. /* Release allocated pages. */
  547. static void __init __sclp_vt220_free_pages(void)
  548. {
  549. struct list_head *page, *p;
  550. list_for_each_safe(page, p, &sclp_vt220_empty) {
  551. list_del(page);
  552. free_page((unsigned long) page);
  553. }
  554. }
  555. /* Release memory and unregister from sclp core. Controlled by init counting -
  556. * only the last invoker will actually perform these actions. */
  557. static void __init __sclp_vt220_cleanup(void)
  558. {
  559. sclp_vt220_init_count--;
  560. if (sclp_vt220_init_count != 0)
  561. return;
  562. sclp_unregister(&sclp_vt220_register);
  563. __sclp_vt220_free_pages();
  564. tty_port_destroy(&sclp_vt220_port);
  565. }
  566. /* Allocate buffer pages and register with sclp core. Controlled by init
  567. * counting - only the first invoker will actually perform these actions. */
  568. static int __init __sclp_vt220_init(int num_pages)
  569. {
  570. void *page;
  571. int i;
  572. int rc;
  573. sclp_vt220_init_count++;
  574. if (sclp_vt220_init_count != 1)
  575. return 0;
  576. spin_lock_init(&sclp_vt220_lock);
  577. INIT_LIST_HEAD(&sclp_vt220_empty);
  578. INIT_LIST_HEAD(&sclp_vt220_outqueue);
  579. init_timer(&sclp_vt220_timer);
  580. tty_port_init(&sclp_vt220_port);
  581. sclp_vt220_current_request = NULL;
  582. sclp_vt220_buffered_chars = 0;
  583. sclp_vt220_flush_later = 0;
  584. /* Allocate pages for output buffering */
  585. rc = -ENOMEM;
  586. for (i = 0; i < num_pages; i++) {
  587. page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  588. if (!page)
  589. goto out;
  590. list_add_tail(page, &sclp_vt220_empty);
  591. }
  592. rc = sclp_register(&sclp_vt220_register);
  593. out:
  594. if (rc) {
  595. __sclp_vt220_free_pages();
  596. sclp_vt220_init_count--;
  597. tty_port_destroy(&sclp_vt220_port);
  598. }
  599. return rc;
  600. }
  601. static const struct tty_operations sclp_vt220_ops = {
  602. .open = sclp_vt220_open,
  603. .close = sclp_vt220_close,
  604. .write = sclp_vt220_write,
  605. .put_char = sclp_vt220_put_char,
  606. .flush_chars = sclp_vt220_flush_chars,
  607. .write_room = sclp_vt220_write_room,
  608. .chars_in_buffer = sclp_vt220_chars_in_buffer,
  609. .flush_buffer = sclp_vt220_flush_buffer,
  610. };
  611. /*
  612. * Register driver with SCLP and Linux and initialize internal tty structures.
  613. */
  614. static int __init sclp_vt220_tty_init(void)
  615. {
  616. struct tty_driver *driver;
  617. int rc;
  618. /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
  619. * symmetry between VM and LPAR systems regarding ttyS1. */
  620. driver = alloc_tty_driver(1);
  621. if (!driver)
  622. return -ENOMEM;
  623. rc = __sclp_vt220_init(MAX_KMEM_PAGES);
  624. if (rc)
  625. goto out_driver;
  626. driver->driver_name = SCLP_VT220_DRIVER_NAME;
  627. driver->name = SCLP_VT220_DEVICE_NAME;
  628. driver->major = SCLP_VT220_MAJOR;
  629. driver->minor_start = SCLP_VT220_MINOR;
  630. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  631. driver->subtype = SYSTEM_TYPE_TTY;
  632. driver->init_termios = tty_std_termios;
  633. driver->flags = TTY_DRIVER_REAL_RAW;
  634. tty_set_operations(driver, &sclp_vt220_ops);
  635. tty_port_link_device(&sclp_vt220_port, driver, 0);
  636. rc = tty_register_driver(driver);
  637. if (rc)
  638. goto out_init;
  639. sclp_vt220_driver = driver;
  640. return 0;
  641. out_init:
  642. __sclp_vt220_cleanup();
  643. out_driver:
  644. put_tty_driver(driver);
  645. return rc;
  646. }
  647. __initcall(sclp_vt220_tty_init);
  648. static void __sclp_vt220_flush_buffer(void)
  649. {
  650. unsigned long flags;
  651. sclp_vt220_emit_current();
  652. spin_lock_irqsave(&sclp_vt220_lock, flags);
  653. if (timer_pending(&sclp_vt220_timer))
  654. del_timer(&sclp_vt220_timer);
  655. while (sclp_vt220_queue_running) {
  656. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  657. sclp_sync_wait();
  658. spin_lock_irqsave(&sclp_vt220_lock, flags);
  659. }
  660. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  661. }
  662. /*
  663. * Resume console: If there are cached messages, emit them.
  664. */
  665. static void sclp_vt220_resume(void)
  666. {
  667. unsigned long flags;
  668. spin_lock_irqsave(&sclp_vt220_lock, flags);
  669. sclp_vt220_suspended = 0;
  670. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  671. sclp_vt220_emit_current();
  672. }
  673. /*
  674. * Suspend console: Set suspend flag and flush console
  675. */
  676. static void sclp_vt220_suspend(void)
  677. {
  678. unsigned long flags;
  679. spin_lock_irqsave(&sclp_vt220_lock, flags);
  680. sclp_vt220_suspended = 1;
  681. spin_unlock_irqrestore(&sclp_vt220_lock, flags);
  682. __sclp_vt220_flush_buffer();
  683. }
  684. static void sclp_vt220_pm_event_fn(struct sclp_register *reg,
  685. enum sclp_pm_event sclp_pm_event)
  686. {
  687. switch (sclp_pm_event) {
  688. case SCLP_PM_EVENT_FREEZE:
  689. sclp_vt220_suspend();
  690. break;
  691. case SCLP_PM_EVENT_RESTORE:
  692. case SCLP_PM_EVENT_THAW:
  693. sclp_vt220_resume();
  694. break;
  695. }
  696. }
  697. #ifdef CONFIG_SCLP_VT220_CONSOLE
  698. static void
  699. sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
  700. {
  701. __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
  702. }
  703. static struct tty_driver *
  704. sclp_vt220_con_device(struct console *c, int *index)
  705. {
  706. *index = 0;
  707. return sclp_vt220_driver;
  708. }
  709. static int
  710. sclp_vt220_notify(struct notifier_block *self,
  711. unsigned long event, void *data)
  712. {
  713. __sclp_vt220_flush_buffer();
  714. return NOTIFY_OK;
  715. }
  716. static struct notifier_block on_panic_nb = {
  717. .notifier_call = sclp_vt220_notify,
  718. .priority = 1,
  719. };
  720. static struct notifier_block on_reboot_nb = {
  721. .notifier_call = sclp_vt220_notify,
  722. .priority = 1,
  723. };
  724. /* Structure needed to register with printk */
  725. static struct console sclp_vt220_console =
  726. {
  727. .name = SCLP_VT220_CONSOLE_NAME,
  728. .write = sclp_vt220_con_write,
  729. .device = sclp_vt220_con_device,
  730. .flags = CON_PRINTBUFFER,
  731. .index = SCLP_VT220_CONSOLE_INDEX
  732. };
  733. static int __init
  734. sclp_vt220_con_init(void)
  735. {
  736. int rc;
  737. if (!CONSOLE_IS_SCLP)
  738. return 0;
  739. rc = __sclp_vt220_init(sclp_console_pages);
  740. if (rc)
  741. return rc;
  742. /* Attach linux console */
  743. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  744. register_reboot_notifier(&on_reboot_nb);
  745. register_console(&sclp_vt220_console);
  746. return 0;
  747. }
  748. console_initcall(sclp_vt220_con_init);
  749. #endif /* CONFIG_SCLP_VT220_CONSOLE */