con3215.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /*
  2. * 3215 line mode terminal driver.
  3. *
  4. * Copyright IBM Corp. 1999, 2009
  5. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  6. *
  7. * Updated:
  8. * Aug-2000: Added tab support
  9. * Dan Morrison, IBM Corporation <dmorriso@cse.buffalo.edu>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/kdev_t.h>
  14. #include <linux/tty.h>
  15. #include <linux/tty_flip.h>
  16. #include <linux/vt_kern.h>
  17. #include <linux/init.h>
  18. #include <linux/console.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/err.h>
  21. #include <linux/reboot.h>
  22. #include <linux/slab.h>
  23. #include <asm/ccwdev.h>
  24. #include <asm/cio.h>
  25. #include <asm/io.h>
  26. #include <asm/ebcdic.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/delay.h>
  29. #include <asm/cpcmd.h>
  30. #include <asm/setup.h>
  31. #include "ctrlchar.h"
  32. #define NR_3215 1
  33. #define NR_3215_REQ (4*NR_3215)
  34. #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
  35. #define RAW3215_INBUF_SIZE 256 /* input buffer size */
  36. #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
  37. #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
  38. #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
  39. #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
  40. #define RAW3215_NR_CCWS 3
  41. #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
  42. #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
  43. #define RAW3215_ACTIVE 2 /* set if the device is in use */
  44. #define RAW3215_WORKING 4 /* set if a request is being worked on */
  45. #define RAW3215_THROTTLED 8 /* set if reading is disabled */
  46. #define RAW3215_STOPPED 16 /* set if writing is disabled */
  47. #define RAW3215_CLOSING 32 /* set while in close process */
  48. #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
  49. #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
  50. #define RAW3215_FROZEN 256 /* set if 3215 is frozen for suspend */
  51. #define TAB_STOP_SIZE 8 /* tab stop size */
  52. /*
  53. * Request types for a 3215 device
  54. */
  55. enum raw3215_type {
  56. RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
  57. };
  58. /*
  59. * Request structure for a 3215 device
  60. */
  61. struct raw3215_req {
  62. enum raw3215_type type; /* type of the request */
  63. int start, len; /* start index & len in output buffer */
  64. int delayable; /* indication to wait for more data */
  65. int residual; /* residual count for read request */
  66. struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
  67. struct raw3215_info *info; /* pointer to main structure */
  68. struct raw3215_req *next; /* pointer to next request */
  69. } __attribute__ ((aligned(8)));
  70. struct raw3215_info {
  71. struct ccw_device *cdev; /* device for tty driver */
  72. spinlock_t *lock; /* pointer to irq lock */
  73. int flags; /* state flags */
  74. char *buffer; /* pointer to output buffer */
  75. char *inbuf; /* pointer to input buffer */
  76. int head; /* first free byte in output buffer */
  77. int count; /* number of bytes in output buffer */
  78. int written; /* number of bytes in write requests */
  79. struct tty_struct *tty; /* pointer to tty structure if present */
  80. struct raw3215_req *queued_read; /* pointer to queued read requests */
  81. struct raw3215_req *queued_write;/* pointer to queued write requests */
  82. struct tasklet_struct tlet; /* tasklet to invoke tty_wakeup */
  83. wait_queue_head_t empty_wait; /* wait queue for flushing */
  84. struct timer_list timer; /* timer for delayed output */
  85. int line_pos; /* position on the line (for tabs) */
  86. char ubuffer[80]; /* copy_from_user buffer */
  87. };
  88. /* array of 3215 devices structures */
  89. static struct raw3215_info *raw3215[NR_3215];
  90. /* spinlock to protect the raw3215 array */
  91. static DEFINE_SPINLOCK(raw3215_device_lock);
  92. /* list of free request structures */
  93. static struct raw3215_req *raw3215_freelist;
  94. /* spinlock to protect free list */
  95. static spinlock_t raw3215_freelist_lock;
  96. static struct tty_driver *tty3215_driver;
  97. /*
  98. * Get a request structure from the free list
  99. */
  100. static inline struct raw3215_req *raw3215_alloc_req(void)
  101. {
  102. struct raw3215_req *req;
  103. unsigned long flags;
  104. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  105. req = raw3215_freelist;
  106. raw3215_freelist = req->next;
  107. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  108. return req;
  109. }
  110. /*
  111. * Put a request structure back to the free list
  112. */
  113. static inline void raw3215_free_req(struct raw3215_req *req)
  114. {
  115. unsigned long flags;
  116. if (req->type == RAW3215_FREE)
  117. return; /* don't free a free request */
  118. req->type = RAW3215_FREE;
  119. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  120. req->next = raw3215_freelist;
  121. raw3215_freelist = req;
  122. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  123. }
  124. /*
  125. * Set up a read request that reads up to 160 byte from the 3215 device.
  126. * If there is a queued read request it is used, but that shouldn't happen
  127. * because a 3215 terminal won't accept a new read before the old one is
  128. * completed.
  129. */
  130. static void raw3215_mk_read_req(struct raw3215_info *raw)
  131. {
  132. struct raw3215_req *req;
  133. struct ccw1 *ccw;
  134. /* there can only be ONE read request at a time */
  135. req = raw->queued_read;
  136. if (req == NULL) {
  137. /* no queued read request, use new req structure */
  138. req = raw3215_alloc_req();
  139. req->type = RAW3215_READ;
  140. req->info = raw;
  141. raw->queued_read = req;
  142. }
  143. ccw = req->ccws;
  144. ccw->cmd_code = 0x0A; /* read inquiry */
  145. ccw->flags = 0x20; /* ignore incorrect length */
  146. ccw->count = 160;
  147. ccw->cda = (__u32) __pa(raw->inbuf);
  148. }
  149. /*
  150. * Set up a write request with the information from the main structure.
  151. * A ccw chain is created that writes as much as possible from the output
  152. * buffer to the 3215 device. If a queued write exists it is replaced by
  153. * the new, probably lengthened request.
  154. */
  155. static void raw3215_mk_write_req(struct raw3215_info *raw)
  156. {
  157. struct raw3215_req *req;
  158. struct ccw1 *ccw;
  159. int len, count, ix, lines;
  160. if (raw->count <= raw->written)
  161. return;
  162. /* check if there is a queued write request */
  163. req = raw->queued_write;
  164. if (req == NULL) {
  165. /* no queued write request, use new req structure */
  166. req = raw3215_alloc_req();
  167. req->type = RAW3215_WRITE;
  168. req->info = raw;
  169. raw->queued_write = req;
  170. } else {
  171. raw->written -= req->len;
  172. }
  173. ccw = req->ccws;
  174. req->start = (raw->head - raw->count + raw->written) &
  175. (RAW3215_BUFFER_SIZE - 1);
  176. /*
  177. * now we have to count newlines. We can at max accept
  178. * RAW3215_MAX_NEWLINE newlines in a single ssch due to
  179. * a restriction in VM
  180. */
  181. lines = 0;
  182. ix = req->start;
  183. while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
  184. if (raw->buffer[ix] == 0x15)
  185. lines++;
  186. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  187. }
  188. len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
  189. if (len > RAW3215_MAX_BYTES)
  190. len = RAW3215_MAX_BYTES;
  191. req->len = len;
  192. raw->written += len;
  193. /* set the indication if we should try to enlarge this request */
  194. req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
  195. ix = req->start;
  196. while (len > 0) {
  197. if (ccw > req->ccws)
  198. ccw[-1].flags |= 0x40; /* use command chaining */
  199. ccw->cmd_code = 0x01; /* write, auto carrier return */
  200. ccw->flags = 0x20; /* ignore incorrect length ind. */
  201. ccw->cda =
  202. (__u32) __pa(raw->buffer + ix);
  203. count = len;
  204. if (ix + count > RAW3215_BUFFER_SIZE)
  205. count = RAW3215_BUFFER_SIZE - ix;
  206. ccw->count = count;
  207. len -= count;
  208. ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
  209. ccw++;
  210. }
  211. /*
  212. * Add a NOP to the channel program. 3215 devices are purely
  213. * emulated and its much better to avoid the channel end
  214. * interrupt in this case.
  215. */
  216. if (ccw > req->ccws)
  217. ccw[-1].flags |= 0x40; /* use command chaining */
  218. ccw->cmd_code = 0x03; /* NOP */
  219. ccw->flags = 0;
  220. ccw->cda = 0;
  221. ccw->count = 1;
  222. }
  223. /*
  224. * Start a read or a write request
  225. */
  226. static void raw3215_start_io(struct raw3215_info *raw)
  227. {
  228. struct raw3215_req *req;
  229. int res;
  230. req = raw->queued_read;
  231. if (req != NULL &&
  232. !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
  233. /* dequeue request */
  234. raw->queued_read = NULL;
  235. res = ccw_device_start(raw->cdev, req->ccws,
  236. (unsigned long) req, 0, 0);
  237. if (res != 0) {
  238. /* do_IO failed, put request back to queue */
  239. raw->queued_read = req;
  240. } else {
  241. raw->flags |= RAW3215_WORKING;
  242. }
  243. }
  244. req = raw->queued_write;
  245. if (req != NULL &&
  246. !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
  247. /* dequeue request */
  248. raw->queued_write = NULL;
  249. res = ccw_device_start(raw->cdev, req->ccws,
  250. (unsigned long) req, 0, 0);
  251. if (res != 0) {
  252. /* do_IO failed, put request back to queue */
  253. raw->queued_write = req;
  254. } else {
  255. raw->flags |= RAW3215_WORKING;
  256. }
  257. }
  258. }
  259. /*
  260. * Function to start a delayed output after RAW3215_TIMEOUT seconds
  261. */
  262. static void raw3215_timeout(unsigned long __data)
  263. {
  264. struct raw3215_info *raw = (struct raw3215_info *) __data;
  265. unsigned long flags;
  266. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  267. if (raw->flags & RAW3215_TIMER_RUNS) {
  268. del_timer(&raw->timer);
  269. raw->flags &= ~RAW3215_TIMER_RUNS;
  270. if (!(raw->flags & RAW3215_FROZEN)) {
  271. raw3215_mk_write_req(raw);
  272. raw3215_start_io(raw);
  273. }
  274. }
  275. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  276. }
  277. /*
  278. * Function to conditionally start an IO. A read is started immediately,
  279. * a write is only started immediately if the flush flag is on or the
  280. * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
  281. * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
  282. */
  283. static inline void raw3215_try_io(struct raw3215_info *raw)
  284. {
  285. if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FROZEN))
  286. return;
  287. if (raw->queued_read != NULL)
  288. raw3215_start_io(raw);
  289. else if (raw->queued_write != NULL) {
  290. if ((raw->queued_write->delayable == 0) ||
  291. (raw->flags & RAW3215_FLUSHING)) {
  292. /* execute write requests bigger than minimum size */
  293. raw3215_start_io(raw);
  294. if (raw->flags & RAW3215_TIMER_RUNS) {
  295. del_timer(&raw->timer);
  296. raw->flags &= ~RAW3215_TIMER_RUNS;
  297. }
  298. } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
  299. /* delay small writes */
  300. init_timer(&raw->timer);
  301. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  302. raw->timer.data = (unsigned long) raw;
  303. raw->timer.function = raw3215_timeout;
  304. add_timer(&raw->timer);
  305. raw->flags |= RAW3215_TIMER_RUNS;
  306. }
  307. }
  308. }
  309. /*
  310. * Call tty_wakeup from tasklet context
  311. */
  312. static void raw3215_wakeup(unsigned long data)
  313. {
  314. struct raw3215_info *raw = (struct raw3215_info *) data;
  315. tty_wakeup(raw->tty);
  316. }
  317. /*
  318. * Try to start the next IO and wake up processes waiting on the tty.
  319. */
  320. static void raw3215_next_io(struct raw3215_info *raw)
  321. {
  322. raw3215_mk_write_req(raw);
  323. raw3215_try_io(raw);
  324. if (raw->tty && RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE)
  325. tasklet_schedule(&raw->tlet);
  326. }
  327. /*
  328. * Interrupt routine, called from common io layer
  329. */
  330. static void raw3215_irq(struct ccw_device *cdev, unsigned long intparm,
  331. struct irb *irb)
  332. {
  333. struct raw3215_info *raw;
  334. struct raw3215_req *req;
  335. struct tty_struct *tty;
  336. int cstat, dstat;
  337. int count;
  338. raw = dev_get_drvdata(&cdev->dev);
  339. req = (struct raw3215_req *) intparm;
  340. cstat = irb->scsw.cmd.cstat;
  341. dstat = irb->scsw.cmd.dstat;
  342. if (cstat != 0)
  343. raw3215_next_io(raw);
  344. if (dstat & 0x01) { /* we got a unit exception */
  345. dstat &= ~0x01; /* we can ignore it */
  346. }
  347. switch (dstat) {
  348. case 0x80:
  349. if (cstat != 0)
  350. break;
  351. /* Attention interrupt, someone hit the enter key */
  352. raw3215_mk_read_req(raw);
  353. raw3215_next_io(raw);
  354. break;
  355. case 0x08:
  356. case 0x0C:
  357. /* Channel end interrupt. */
  358. if ((raw = req->info) == NULL)
  359. return; /* That shouldn't happen ... */
  360. if (req->type == RAW3215_READ) {
  361. /* store residual count, then wait for device end */
  362. req->residual = irb->scsw.cmd.count;
  363. }
  364. if (dstat == 0x08)
  365. break;
  366. case 0x04:
  367. /* Device end interrupt. */
  368. if ((raw = req->info) == NULL)
  369. return; /* That shouldn't happen ... */
  370. if (req->type == RAW3215_READ && raw->tty != NULL) {
  371. unsigned int cchar;
  372. tty = raw->tty;
  373. count = 160 - req->residual;
  374. EBCASC(raw->inbuf, count);
  375. cchar = ctrlchar_handle(raw->inbuf, count, tty);
  376. switch (cchar & CTRLCHAR_MASK) {
  377. case CTRLCHAR_SYSRQ:
  378. break;
  379. case CTRLCHAR_CTRL:
  380. tty_insert_flip_char(tty, cchar, TTY_NORMAL);
  381. tty_flip_buffer_push(raw->tty);
  382. break;
  383. case CTRLCHAR_NONE:
  384. if (count < 2 ||
  385. (strncmp(raw->inbuf+count-2, "\252n", 2) &&
  386. strncmp(raw->inbuf+count-2, "^n", 2)) ) {
  387. /* add the auto \n */
  388. raw->inbuf[count] = '\n';
  389. count++;
  390. } else
  391. count -= 2;
  392. tty_insert_flip_string(tty, raw->inbuf, count);
  393. tty_flip_buffer_push(raw->tty);
  394. break;
  395. }
  396. } else if (req->type == RAW3215_WRITE) {
  397. raw->count -= req->len;
  398. raw->written -= req->len;
  399. }
  400. raw->flags &= ~RAW3215_WORKING;
  401. raw3215_free_req(req);
  402. /* check for empty wait */
  403. if (waitqueue_active(&raw->empty_wait) &&
  404. raw->queued_write == NULL &&
  405. raw->queued_read == NULL) {
  406. wake_up_interruptible(&raw->empty_wait);
  407. }
  408. raw3215_next_io(raw);
  409. break;
  410. default:
  411. /* Strange interrupt, I'll do my best to clean up */
  412. if (req != NULL && req->type != RAW3215_FREE) {
  413. if (req->type == RAW3215_WRITE) {
  414. raw->count -= req->len;
  415. raw->written -= req->len;
  416. }
  417. raw->flags &= ~RAW3215_WORKING;
  418. raw3215_free_req(req);
  419. }
  420. raw3215_next_io(raw);
  421. }
  422. return;
  423. }
  424. /*
  425. * Drop the oldest line from the output buffer.
  426. */
  427. static void raw3215_drop_line(struct raw3215_info *raw)
  428. {
  429. int ix;
  430. char ch;
  431. BUG_ON(raw->written != 0);
  432. ix = (raw->head - raw->count) & (RAW3215_BUFFER_SIZE - 1);
  433. while (raw->count > 0) {
  434. ch = raw->buffer[ix];
  435. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  436. raw->count--;
  437. if (ch == 0x15)
  438. break;
  439. }
  440. raw->head = ix;
  441. }
  442. /*
  443. * Wait until length bytes are available int the output buffer.
  444. * Has to be called with the s390irq lock held. Can be called
  445. * disabled.
  446. */
  447. static void raw3215_make_room(struct raw3215_info *raw, unsigned int length)
  448. {
  449. while (RAW3215_BUFFER_SIZE - raw->count < length) {
  450. /* While console is frozen for suspend we have no other
  451. * choice but to drop message from the buffer to make
  452. * room for even more messages. */
  453. if (raw->flags & RAW3215_FROZEN) {
  454. raw3215_drop_line(raw);
  455. continue;
  456. }
  457. /* there might be a request pending */
  458. raw->flags |= RAW3215_FLUSHING;
  459. raw3215_mk_write_req(raw);
  460. raw3215_try_io(raw);
  461. raw->flags &= ~RAW3215_FLUSHING;
  462. #ifdef CONFIG_TN3215_CONSOLE
  463. wait_cons_dev();
  464. #endif
  465. /* Enough room freed up ? */
  466. if (RAW3215_BUFFER_SIZE - raw->count >= length)
  467. break;
  468. /* there might be another cpu waiting for the lock */
  469. spin_unlock(get_ccwdev_lock(raw->cdev));
  470. udelay(100);
  471. spin_lock(get_ccwdev_lock(raw->cdev));
  472. }
  473. }
  474. /*
  475. * String write routine for 3215 devices
  476. */
  477. static void raw3215_write(struct raw3215_info *raw, const char *str,
  478. unsigned int length)
  479. {
  480. unsigned long flags;
  481. int c, count;
  482. while (length > 0) {
  483. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  484. count = (length > RAW3215_BUFFER_SIZE) ?
  485. RAW3215_BUFFER_SIZE : length;
  486. length -= count;
  487. raw3215_make_room(raw, count);
  488. /* copy string to output buffer and convert it to EBCDIC */
  489. while (1) {
  490. c = min_t(int, count,
  491. min(RAW3215_BUFFER_SIZE - raw->count,
  492. RAW3215_BUFFER_SIZE - raw->head));
  493. if (c <= 0)
  494. break;
  495. memcpy(raw->buffer + raw->head, str, c);
  496. ASCEBC(raw->buffer + raw->head, c);
  497. raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
  498. raw->count += c;
  499. raw->line_pos += c;
  500. str += c;
  501. count -= c;
  502. }
  503. if (!(raw->flags & RAW3215_WORKING)) {
  504. raw3215_mk_write_req(raw);
  505. /* start or queue request */
  506. raw3215_try_io(raw);
  507. }
  508. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  509. }
  510. }
  511. /*
  512. * Put character routine for 3215 devices
  513. */
  514. static void raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
  515. {
  516. unsigned long flags;
  517. unsigned int length, i;
  518. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  519. if (ch == '\t') {
  520. length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
  521. raw->line_pos += length;
  522. ch = ' ';
  523. } else if (ch == '\n') {
  524. length = 1;
  525. raw->line_pos = 0;
  526. } else {
  527. length = 1;
  528. raw->line_pos++;
  529. }
  530. raw3215_make_room(raw, length);
  531. for (i = 0; i < length; i++) {
  532. raw->buffer[raw->head] = (char) _ascebc[(int) ch];
  533. raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
  534. raw->count++;
  535. }
  536. if (!(raw->flags & RAW3215_WORKING)) {
  537. raw3215_mk_write_req(raw);
  538. /* start or queue request */
  539. raw3215_try_io(raw);
  540. }
  541. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  542. }
  543. /*
  544. * Flush routine, it simply sets the flush flag and tries to start
  545. * pending IO.
  546. */
  547. static void raw3215_flush_buffer(struct raw3215_info *raw)
  548. {
  549. unsigned long flags;
  550. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  551. if (raw->count > 0) {
  552. raw->flags |= RAW3215_FLUSHING;
  553. raw3215_try_io(raw);
  554. raw->flags &= ~RAW3215_FLUSHING;
  555. }
  556. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  557. }
  558. /*
  559. * Fire up a 3215 device.
  560. */
  561. static int raw3215_startup(struct raw3215_info *raw)
  562. {
  563. unsigned long flags;
  564. if (raw->flags & RAW3215_ACTIVE)
  565. return 0;
  566. raw->line_pos = 0;
  567. raw->flags |= RAW3215_ACTIVE;
  568. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  569. raw3215_try_io(raw);
  570. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  571. return 0;
  572. }
  573. /*
  574. * Shutdown a 3215 device.
  575. */
  576. static void raw3215_shutdown(struct raw3215_info *raw)
  577. {
  578. DECLARE_WAITQUEUE(wait, current);
  579. unsigned long flags;
  580. if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
  581. return;
  582. /* Wait for outstanding requests, then free irq */
  583. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  584. if ((raw->flags & RAW3215_WORKING) ||
  585. raw->queued_write != NULL ||
  586. raw->queued_read != NULL) {
  587. raw->flags |= RAW3215_CLOSING;
  588. add_wait_queue(&raw->empty_wait, &wait);
  589. set_current_state(TASK_INTERRUPTIBLE);
  590. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  591. schedule();
  592. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  593. remove_wait_queue(&raw->empty_wait, &wait);
  594. set_current_state(TASK_RUNNING);
  595. raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
  596. }
  597. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  598. }
  599. static int raw3215_probe (struct ccw_device *cdev)
  600. {
  601. struct raw3215_info *raw;
  602. int line;
  603. /* Console is special. */
  604. if (raw3215[0] && (raw3215[0] == dev_get_drvdata(&cdev->dev)))
  605. return 0;
  606. raw = kmalloc(sizeof(struct raw3215_info) +
  607. RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
  608. if (raw == NULL)
  609. return -ENOMEM;
  610. spin_lock(&raw3215_device_lock);
  611. for (line = 0; line < NR_3215; line++) {
  612. if (!raw3215[line]) {
  613. raw3215[line] = raw;
  614. break;
  615. }
  616. }
  617. spin_unlock(&raw3215_device_lock);
  618. if (line == NR_3215) {
  619. kfree(raw);
  620. return -ENODEV;
  621. }
  622. raw->cdev = cdev;
  623. raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
  624. memset(raw, 0, sizeof(struct raw3215_info));
  625. raw->buffer = kmalloc(RAW3215_BUFFER_SIZE,
  626. GFP_KERNEL|GFP_DMA);
  627. if (raw->buffer == NULL) {
  628. spin_lock(&raw3215_device_lock);
  629. raw3215[line] = NULL;
  630. spin_unlock(&raw3215_device_lock);
  631. kfree(raw);
  632. return -ENOMEM;
  633. }
  634. init_waitqueue_head(&raw->empty_wait);
  635. tasklet_init(&raw->tlet, raw3215_wakeup, (unsigned long) raw);
  636. dev_set_drvdata(&cdev->dev, raw);
  637. cdev->handler = raw3215_irq;
  638. return 0;
  639. }
  640. static void raw3215_remove (struct ccw_device *cdev)
  641. {
  642. struct raw3215_info *raw;
  643. ccw_device_set_offline(cdev);
  644. raw = dev_get_drvdata(&cdev->dev);
  645. if (raw) {
  646. dev_set_drvdata(&cdev->dev, NULL);
  647. kfree(raw->buffer);
  648. kfree(raw);
  649. }
  650. }
  651. static int raw3215_set_online (struct ccw_device *cdev)
  652. {
  653. struct raw3215_info *raw;
  654. raw = dev_get_drvdata(&cdev->dev);
  655. if (!raw)
  656. return -ENODEV;
  657. return raw3215_startup(raw);
  658. }
  659. static int raw3215_set_offline (struct ccw_device *cdev)
  660. {
  661. struct raw3215_info *raw;
  662. raw = dev_get_drvdata(&cdev->dev);
  663. if (!raw)
  664. return -ENODEV;
  665. raw3215_shutdown(raw);
  666. return 0;
  667. }
  668. static int raw3215_pm_stop(struct ccw_device *cdev)
  669. {
  670. struct raw3215_info *raw;
  671. unsigned long flags;
  672. /* Empty the output buffer, then prevent new I/O. */
  673. raw = dev_get_drvdata(&cdev->dev);
  674. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  675. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  676. raw->flags |= RAW3215_FROZEN;
  677. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  678. return 0;
  679. }
  680. static int raw3215_pm_start(struct ccw_device *cdev)
  681. {
  682. struct raw3215_info *raw;
  683. unsigned long flags;
  684. /* Allow I/O again and flush output buffer. */
  685. raw = dev_get_drvdata(&cdev->dev);
  686. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  687. raw->flags &= ~RAW3215_FROZEN;
  688. raw->flags |= RAW3215_FLUSHING;
  689. raw3215_try_io(raw);
  690. raw->flags &= ~RAW3215_FLUSHING;
  691. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  692. return 0;
  693. }
  694. static struct ccw_device_id raw3215_id[] = {
  695. { CCW_DEVICE(0x3215, 0) },
  696. { /* end of list */ },
  697. };
  698. static struct ccw_driver raw3215_ccw_driver = {
  699. .driver = {
  700. .name = "3215",
  701. .owner = THIS_MODULE,
  702. },
  703. .ids = raw3215_id,
  704. .probe = &raw3215_probe,
  705. .remove = &raw3215_remove,
  706. .set_online = &raw3215_set_online,
  707. .set_offline = &raw3215_set_offline,
  708. .freeze = &raw3215_pm_stop,
  709. .thaw = &raw3215_pm_start,
  710. .restore = &raw3215_pm_start,
  711. .int_class = IOINT_C15,
  712. };
  713. #ifdef CONFIG_TN3215_CONSOLE
  714. /*
  715. * Write a string to the 3215 console
  716. */
  717. static void con3215_write(struct console *co, const char *str,
  718. unsigned int count)
  719. {
  720. struct raw3215_info *raw;
  721. int i;
  722. if (count <= 0)
  723. return;
  724. raw = raw3215[0]; /* console 3215 is the first one */
  725. while (count > 0) {
  726. for (i = 0; i < count; i++)
  727. if (str[i] == '\t' || str[i] == '\n')
  728. break;
  729. raw3215_write(raw, str, i);
  730. count -= i;
  731. str += i;
  732. if (count > 0) {
  733. raw3215_putchar(raw, *str);
  734. count--;
  735. str++;
  736. }
  737. }
  738. }
  739. static struct tty_driver *con3215_device(struct console *c, int *index)
  740. {
  741. *index = c->index;
  742. return tty3215_driver;
  743. }
  744. /*
  745. * panic() calls con3215_flush through a panic_notifier
  746. * before the system enters a disabled, endless loop.
  747. */
  748. static void con3215_flush(void)
  749. {
  750. struct raw3215_info *raw;
  751. unsigned long flags;
  752. raw = raw3215[0]; /* console 3215 is the first one */
  753. if (raw->flags & RAW3215_FROZEN)
  754. /* The console is still frozen for suspend. */
  755. if (ccw_device_force_console())
  756. /* Forcing didn't work, no panic message .. */
  757. return;
  758. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  759. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  760. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  761. }
  762. static int con3215_notify(struct notifier_block *self,
  763. unsigned long event, void *data)
  764. {
  765. con3215_flush();
  766. return NOTIFY_OK;
  767. }
  768. static struct notifier_block on_panic_nb = {
  769. .notifier_call = con3215_notify,
  770. .priority = 0,
  771. };
  772. static struct notifier_block on_reboot_nb = {
  773. .notifier_call = con3215_notify,
  774. .priority = 0,
  775. };
  776. /*
  777. * The console structure for the 3215 console
  778. */
  779. static struct console con3215 = {
  780. .name = "ttyS",
  781. .write = con3215_write,
  782. .device = con3215_device,
  783. .flags = CON_PRINTBUFFER,
  784. };
  785. /*
  786. * 3215 console initialization code called from console_init().
  787. */
  788. static int __init con3215_init(void)
  789. {
  790. struct ccw_device *cdev;
  791. struct raw3215_info *raw;
  792. struct raw3215_req *req;
  793. int i;
  794. /* Check if 3215 is to be the console */
  795. if (!CONSOLE_IS_3215)
  796. return -ENODEV;
  797. /* Set the console mode for VM */
  798. if (MACHINE_IS_VM) {
  799. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  800. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  801. }
  802. /* allocate 3215 request structures */
  803. raw3215_freelist = NULL;
  804. spin_lock_init(&raw3215_freelist_lock);
  805. for (i = 0; i < NR_3215_REQ; i++) {
  806. req = kzalloc(sizeof(struct raw3215_req), GFP_KERNEL | GFP_DMA);
  807. req->next = raw3215_freelist;
  808. raw3215_freelist = req;
  809. }
  810. cdev = ccw_device_probe_console();
  811. if (IS_ERR(cdev))
  812. return -ENODEV;
  813. raw3215[0] = raw = (struct raw3215_info *)
  814. kzalloc(sizeof(struct raw3215_info), GFP_KERNEL | GFP_DMA);
  815. raw->buffer = kzalloc(RAW3215_BUFFER_SIZE, GFP_KERNEL | GFP_DMA);
  816. raw->inbuf = kzalloc(RAW3215_INBUF_SIZE, GFP_KERNEL | GFP_DMA);
  817. raw->cdev = cdev;
  818. dev_set_drvdata(&cdev->dev, raw);
  819. cdev->handler = raw3215_irq;
  820. raw->flags |= RAW3215_FIXED;
  821. init_waitqueue_head(&raw->empty_wait);
  822. tasklet_init(&raw->tlet, raw3215_wakeup, (unsigned long) raw);
  823. /* Request the console irq */
  824. if (raw3215_startup(raw) != 0) {
  825. kfree(raw->inbuf);
  826. kfree(raw->buffer);
  827. kfree(raw);
  828. raw3215[0] = NULL;
  829. return -ENODEV;
  830. }
  831. atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
  832. register_reboot_notifier(&on_reboot_nb);
  833. register_console(&con3215);
  834. return 0;
  835. }
  836. console_initcall(con3215_init);
  837. #endif
  838. /*
  839. * tty3215_open
  840. *
  841. * This routine is called whenever a 3215 tty is opened.
  842. */
  843. static int tty3215_open(struct tty_struct *tty, struct file * filp)
  844. {
  845. struct raw3215_info *raw;
  846. int retval;
  847. raw = raw3215[tty->index];
  848. if (raw == NULL)
  849. return -ENODEV;
  850. tty->driver_data = raw;
  851. raw->tty = tty;
  852. tty->low_latency = 0; /* don't use bottom half for pushing chars */
  853. /*
  854. * Start up 3215 device
  855. */
  856. retval = raw3215_startup(raw);
  857. if (retval)
  858. return retval;
  859. return 0;
  860. }
  861. /*
  862. * tty3215_close()
  863. *
  864. * This routine is called when the 3215 tty is closed. We wait
  865. * for the remaining request to be completed. Then we clean up.
  866. */
  867. static void tty3215_close(struct tty_struct *tty, struct file * filp)
  868. {
  869. struct raw3215_info *raw;
  870. raw = (struct raw3215_info *) tty->driver_data;
  871. if (raw == NULL || tty->count > 1)
  872. return;
  873. tty->closing = 1;
  874. /* Shutdown the terminal */
  875. raw3215_shutdown(raw);
  876. tasklet_kill(&raw->tlet);
  877. tty->closing = 0;
  878. raw->tty = NULL;
  879. }
  880. /*
  881. * Returns the amount of free space in the output buffer.
  882. */
  883. static int tty3215_write_room(struct tty_struct *tty)
  884. {
  885. struct raw3215_info *raw;
  886. raw = (struct raw3215_info *) tty->driver_data;
  887. /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
  888. if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
  889. return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
  890. else
  891. return 0;
  892. }
  893. /*
  894. * String write routine for 3215 ttys
  895. */
  896. static int tty3215_write(struct tty_struct * tty,
  897. const unsigned char *buf, int count)
  898. {
  899. struct raw3215_info *raw;
  900. int i, written;
  901. if (!tty)
  902. return 0;
  903. raw = (struct raw3215_info *) tty->driver_data;
  904. written = count;
  905. while (count > 0) {
  906. for (i = 0; i < count; i++)
  907. if (buf[i] == '\t' || buf[i] == '\n')
  908. break;
  909. raw3215_write(raw, buf, i);
  910. count -= i;
  911. buf += i;
  912. if (count > 0) {
  913. raw3215_putchar(raw, *buf);
  914. count--;
  915. buf++;
  916. }
  917. }
  918. return written;
  919. }
  920. /*
  921. * Put character routine for 3215 ttys
  922. */
  923. static int tty3215_put_char(struct tty_struct *tty, unsigned char ch)
  924. {
  925. struct raw3215_info *raw;
  926. if (!tty)
  927. return 0;
  928. raw = (struct raw3215_info *) tty->driver_data;
  929. raw3215_putchar(raw, ch);
  930. return 1;
  931. }
  932. static void tty3215_flush_chars(struct tty_struct *tty)
  933. {
  934. }
  935. /*
  936. * Returns the number of characters in the output buffer
  937. */
  938. static int tty3215_chars_in_buffer(struct tty_struct *tty)
  939. {
  940. struct raw3215_info *raw;
  941. raw = (struct raw3215_info *) tty->driver_data;
  942. return raw->count;
  943. }
  944. static void tty3215_flush_buffer(struct tty_struct *tty)
  945. {
  946. struct raw3215_info *raw;
  947. raw = (struct raw3215_info *) tty->driver_data;
  948. raw3215_flush_buffer(raw);
  949. tty_wakeup(tty);
  950. }
  951. /*
  952. * Disable reading from a 3215 tty
  953. */
  954. static void tty3215_throttle(struct tty_struct * tty)
  955. {
  956. struct raw3215_info *raw;
  957. raw = (struct raw3215_info *) tty->driver_data;
  958. raw->flags |= RAW3215_THROTTLED;
  959. }
  960. /*
  961. * Enable reading from a 3215 tty
  962. */
  963. static void tty3215_unthrottle(struct tty_struct * tty)
  964. {
  965. struct raw3215_info *raw;
  966. unsigned long flags;
  967. raw = (struct raw3215_info *) tty->driver_data;
  968. if (raw->flags & RAW3215_THROTTLED) {
  969. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  970. raw->flags &= ~RAW3215_THROTTLED;
  971. raw3215_try_io(raw);
  972. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  973. }
  974. }
  975. /*
  976. * Disable writing to a 3215 tty
  977. */
  978. static void tty3215_stop(struct tty_struct *tty)
  979. {
  980. struct raw3215_info *raw;
  981. raw = (struct raw3215_info *) tty->driver_data;
  982. raw->flags |= RAW3215_STOPPED;
  983. }
  984. /*
  985. * Enable writing to a 3215 tty
  986. */
  987. static void tty3215_start(struct tty_struct *tty)
  988. {
  989. struct raw3215_info *raw;
  990. unsigned long flags;
  991. raw = (struct raw3215_info *) tty->driver_data;
  992. if (raw->flags & RAW3215_STOPPED) {
  993. spin_lock_irqsave(get_ccwdev_lock(raw->cdev), flags);
  994. raw->flags &= ~RAW3215_STOPPED;
  995. raw3215_try_io(raw);
  996. spin_unlock_irqrestore(get_ccwdev_lock(raw->cdev), flags);
  997. }
  998. }
  999. static const struct tty_operations tty3215_ops = {
  1000. .open = tty3215_open,
  1001. .close = tty3215_close,
  1002. .write = tty3215_write,
  1003. .put_char = tty3215_put_char,
  1004. .flush_chars = tty3215_flush_chars,
  1005. .write_room = tty3215_write_room,
  1006. .chars_in_buffer = tty3215_chars_in_buffer,
  1007. .flush_buffer = tty3215_flush_buffer,
  1008. .throttle = tty3215_throttle,
  1009. .unthrottle = tty3215_unthrottle,
  1010. .stop = tty3215_stop,
  1011. .start = tty3215_start,
  1012. };
  1013. /*
  1014. * 3215 tty registration code called from tty_init().
  1015. * Most kernel services (incl. kmalloc) are available at this poimt.
  1016. */
  1017. static int __init tty3215_init(void)
  1018. {
  1019. struct tty_driver *driver;
  1020. int ret;
  1021. if (!CONSOLE_IS_3215)
  1022. return 0;
  1023. driver = alloc_tty_driver(NR_3215);
  1024. if (!driver)
  1025. return -ENOMEM;
  1026. ret = ccw_driver_register(&raw3215_ccw_driver);
  1027. if (ret) {
  1028. put_tty_driver(driver);
  1029. return ret;
  1030. }
  1031. /*
  1032. * Initialize the tty_driver structure
  1033. * Entries in tty3215_driver that are NOT initialized:
  1034. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1035. */
  1036. driver->driver_name = "tty3215";
  1037. driver->name = "ttyS";
  1038. driver->major = TTY_MAJOR;
  1039. driver->minor_start = 64;
  1040. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1041. driver->subtype = SYSTEM_TYPE_TTY;
  1042. driver->init_termios = tty_std_termios;
  1043. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  1044. driver->init_termios.c_oflag = ONLCR;
  1045. driver->init_termios.c_lflag = ISIG;
  1046. driver->flags = TTY_DRIVER_REAL_RAW;
  1047. tty_set_operations(driver, &tty3215_ops);
  1048. ret = tty_register_driver(driver);
  1049. if (ret) {
  1050. put_tty_driver(driver);
  1051. return ret;
  1052. }
  1053. tty3215_driver = driver;
  1054. return 0;
  1055. }
  1056. static void __exit tty3215_exit(void)
  1057. {
  1058. tty_unregister_driver(tty3215_driver);
  1059. put_tty_driver(tty3215_driver);
  1060. ccw_driver_unregister(&raw3215_ccw_driver);
  1061. }
  1062. module_init(tty3215_init);
  1063. module_exit(tty3215_exit);