con3215.c 29 KB

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