con3215.c 30 KB

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