raw3270.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. /*
  2. * IBM/3270 Driver - core functions.
  3. *
  4. * Author(s):
  5. * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
  6. * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. * Copyright IBM Corp. 2003, 2009
  8. */
  9. #include <linux/kernel_stat.h>
  10. #include <linux/module.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/list.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <linux/wait.h>
  18. #include <asm/ccwdev.h>
  19. #include <asm/cio.h>
  20. #include <asm/ebcdic.h>
  21. #include <asm/diag.h>
  22. #include "raw3270.h"
  23. #include <linux/major.h>
  24. #include <linux/kdev_t.h>
  25. #include <linux/device.h>
  26. #include <linux/mutex.h>
  27. static struct class *class3270;
  28. /* The main 3270 data structure. */
  29. struct raw3270 {
  30. struct list_head list;
  31. struct ccw_device *cdev;
  32. int minor;
  33. short model, rows, cols;
  34. unsigned long flags;
  35. struct list_head req_queue; /* Request queue. */
  36. struct list_head view_list; /* List of available views. */
  37. struct raw3270_view *view; /* Active view. */
  38. struct timer_list timer; /* Device timer. */
  39. unsigned char *ascebc; /* ascii -> ebcdic table */
  40. struct device *clttydev; /* 3270-class tty device ptr */
  41. struct device *cltubdev; /* 3270-class tub device ptr */
  42. struct raw3270_request init_request;
  43. unsigned char init_data[256];
  44. };
  45. /* raw3270->flags */
  46. #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
  47. #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
  48. #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
  49. #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
  50. #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
  51. #define RAW3270_FLAGS_FROZEN 16 /* set if 3270 is frozen for suspend */
  52. /* Semaphore to protect global data of raw3270 (devices, views, etc). */
  53. static DEFINE_MUTEX(raw3270_mutex);
  54. /* List of 3270 devices. */
  55. static LIST_HEAD(raw3270_devices);
  56. /*
  57. * Flag to indicate if the driver has been registered. Some operations
  58. * like waiting for the end of i/o need to be done differently as long
  59. * as the kernel is still starting up (console support).
  60. */
  61. static int raw3270_registered;
  62. /* Module parameters */
  63. static int tubxcorrect = 0;
  64. module_param(tubxcorrect, bool, 0);
  65. /*
  66. * Wait queue for device init/delete, view delete.
  67. */
  68. DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
  69. /*
  70. * Encode array for 12 bit 3270 addresses.
  71. */
  72. static unsigned char raw3270_ebcgraf[64] = {
  73. 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  74. 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  75. 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  76. 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  77. 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  78. 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  79. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  80. 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
  81. };
  82. void
  83. raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
  84. {
  85. if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
  86. cp[0] = (addr >> 8) & 0x3f;
  87. cp[1] = addr & 0xff;
  88. } else {
  89. cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
  90. cp[1] = raw3270_ebcgraf[addr & 0x3f];
  91. }
  92. }
  93. /*
  94. * Allocate a new 3270 ccw request
  95. */
  96. struct raw3270_request *
  97. raw3270_request_alloc(size_t size)
  98. {
  99. struct raw3270_request *rq;
  100. /* Allocate request structure */
  101. rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
  102. if (!rq)
  103. return ERR_PTR(-ENOMEM);
  104. /* alloc output buffer. */
  105. if (size > 0) {
  106. rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
  107. if (!rq->buffer) {
  108. kfree(rq);
  109. return ERR_PTR(-ENOMEM);
  110. }
  111. }
  112. rq->size = size;
  113. INIT_LIST_HEAD(&rq->list);
  114. /*
  115. * Setup ccw.
  116. */
  117. rq->ccw.cda = __pa(rq->buffer);
  118. rq->ccw.flags = CCW_FLAG_SLI;
  119. return rq;
  120. }
  121. /*
  122. * Free 3270 ccw request
  123. */
  124. void
  125. raw3270_request_free (struct raw3270_request *rq)
  126. {
  127. kfree(rq->buffer);
  128. kfree(rq);
  129. }
  130. /*
  131. * Reset request to initial state.
  132. */
  133. void
  134. raw3270_request_reset(struct raw3270_request *rq)
  135. {
  136. BUG_ON(!list_empty(&rq->list));
  137. rq->ccw.cmd_code = 0;
  138. rq->ccw.count = 0;
  139. rq->ccw.cda = __pa(rq->buffer);
  140. rq->ccw.flags = CCW_FLAG_SLI;
  141. rq->rescnt = 0;
  142. rq->rc = 0;
  143. }
  144. /*
  145. * Set command code to ccw of a request.
  146. */
  147. void
  148. raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
  149. {
  150. rq->ccw.cmd_code = cmd;
  151. }
  152. /*
  153. * Add data fragment to output buffer.
  154. */
  155. int
  156. raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
  157. {
  158. if (size + rq->ccw.count > rq->size)
  159. return -E2BIG;
  160. memcpy(rq->buffer + rq->ccw.count, data, size);
  161. rq->ccw.count += size;
  162. return 0;
  163. }
  164. /*
  165. * Set address/length pair to ccw of a request.
  166. */
  167. void
  168. raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
  169. {
  170. rq->ccw.cda = __pa(data);
  171. rq->ccw.count = size;
  172. }
  173. /*
  174. * Set idal buffer to ccw of a request.
  175. */
  176. void
  177. raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
  178. {
  179. rq->ccw.cda = __pa(ib->data);
  180. rq->ccw.count = ib->size;
  181. rq->ccw.flags |= CCW_FLAG_IDA;
  182. }
  183. /*
  184. * Stop running ccw.
  185. */
  186. static int
  187. raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
  188. {
  189. int retries;
  190. int rc;
  191. if (raw3270_request_final(rq))
  192. return 0;
  193. /* Check if interrupt has already been processed */
  194. for (retries = 0; retries < 5; retries++) {
  195. if (retries < 2)
  196. rc = ccw_device_halt(rp->cdev, (long) rq);
  197. else
  198. rc = ccw_device_clear(rp->cdev, (long) rq);
  199. if (rc == 0)
  200. break; /* termination successful */
  201. }
  202. return rc;
  203. }
  204. static int
  205. raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
  206. {
  207. unsigned long flags;
  208. int rc;
  209. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  210. rc = raw3270_halt_io_nolock(rp, rq);
  211. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  212. return rc;
  213. }
  214. /*
  215. * Add the request to the request queue, try to start it if the
  216. * 3270 device is idle. Return without waiting for end of i/o.
  217. */
  218. static int
  219. __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
  220. struct raw3270_request *rq)
  221. {
  222. rq->view = view;
  223. raw3270_get_view(view);
  224. if (list_empty(&rp->req_queue) &&
  225. !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  226. /* No other requests are on the queue. Start this one. */
  227. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  228. (unsigned long) rq, 0, 0);
  229. if (rq->rc) {
  230. raw3270_put_view(view);
  231. return rq->rc;
  232. }
  233. }
  234. list_add_tail(&rq->list, &rp->req_queue);
  235. return 0;
  236. }
  237. int
  238. raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
  239. {
  240. unsigned long flags;
  241. struct raw3270 *rp;
  242. int rc;
  243. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  244. rp = view->dev;
  245. if (!rp || rp->view != view ||
  246. test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  247. rc = -EACCES;
  248. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  249. rc = -ENODEV;
  250. else
  251. rc = __raw3270_start(rp, view, rq);
  252. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  253. return rc;
  254. }
  255. int
  256. raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
  257. {
  258. struct raw3270 *rp;
  259. int rc;
  260. rp = view->dev;
  261. if (!rp || rp->view != view ||
  262. test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  263. rc = -EACCES;
  264. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  265. rc = -ENODEV;
  266. else
  267. rc = __raw3270_start(rp, view, rq);
  268. return rc;
  269. }
  270. int
  271. raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
  272. {
  273. struct raw3270 *rp;
  274. rp = view->dev;
  275. rq->view = view;
  276. raw3270_get_view(view);
  277. list_add_tail(&rq->list, &rp->req_queue);
  278. return 0;
  279. }
  280. /*
  281. * 3270 interrupt routine, called from the ccw_device layer
  282. */
  283. static void
  284. raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  285. {
  286. struct raw3270 *rp;
  287. struct raw3270_view *view;
  288. struct raw3270_request *rq;
  289. int rc;
  290. kstat_cpu(smp_processor_id()).irqs[IOINT_C70]++;
  291. rp = dev_get_drvdata(&cdev->dev);
  292. if (!rp)
  293. return;
  294. rq = (struct raw3270_request *) intparm;
  295. view = rq ? rq->view : rp->view;
  296. if (IS_ERR(irb))
  297. rc = RAW3270_IO_RETRY;
  298. else if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
  299. rq->rc = -EIO;
  300. rc = RAW3270_IO_DONE;
  301. } else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
  302. DEV_STAT_UNIT_EXCEP)) {
  303. /* Handle CE-DE-UE and subsequent UDE */
  304. set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  305. rc = RAW3270_IO_BUSY;
  306. } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  307. /* Wait for UDE if busy flag is set. */
  308. if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
  309. clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  310. /* Got it, now retry. */
  311. rc = RAW3270_IO_RETRY;
  312. } else
  313. rc = RAW3270_IO_BUSY;
  314. } else if (view)
  315. rc = view->fn->intv(view, rq, irb);
  316. else
  317. rc = RAW3270_IO_DONE;
  318. switch (rc) {
  319. case RAW3270_IO_DONE:
  320. break;
  321. case RAW3270_IO_BUSY:
  322. /*
  323. * Intervention required by the operator. We have to wait
  324. * for unsolicited device end.
  325. */
  326. return;
  327. case RAW3270_IO_RETRY:
  328. if (!rq)
  329. break;
  330. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  331. (unsigned long) rq, 0, 0);
  332. if (rq->rc == 0)
  333. return; /* Successfully restarted. */
  334. break;
  335. case RAW3270_IO_STOP:
  336. if (!rq)
  337. break;
  338. raw3270_halt_io_nolock(rp, rq);
  339. rq->rc = -EIO;
  340. break;
  341. default:
  342. BUG();
  343. }
  344. if (rq) {
  345. BUG_ON(list_empty(&rq->list));
  346. /* The request completed, remove from queue and do callback. */
  347. list_del_init(&rq->list);
  348. if (rq->callback)
  349. rq->callback(rq, rq->callback_data);
  350. /* Do put_device for get_device in raw3270_start. */
  351. raw3270_put_view(view);
  352. }
  353. /*
  354. * Try to start each request on request queue until one is
  355. * started successful.
  356. */
  357. while (!list_empty(&rp->req_queue)) {
  358. rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
  359. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  360. (unsigned long) rq, 0, 0);
  361. if (rq->rc == 0)
  362. break;
  363. /* Start failed. Remove request and do callback. */
  364. list_del_init(&rq->list);
  365. if (rq->callback)
  366. rq->callback(rq, rq->callback_data);
  367. /* Do put_device for get_device in raw3270_start. */
  368. raw3270_put_view(view);
  369. }
  370. }
  371. /*
  372. * Size sensing.
  373. */
  374. struct raw3270_ua { /* Query Reply structure for Usable Area */
  375. struct { /* Usable Area Query Reply Base */
  376. short l; /* Length of this structured field */
  377. char sfid; /* 0x81 if Query Reply */
  378. char qcode; /* 0x81 if Usable Area */
  379. char flags0;
  380. char flags1;
  381. short w; /* Width of usable area */
  382. short h; /* Heigth of usavle area */
  383. char units; /* 0x00:in; 0x01:mm */
  384. int xr;
  385. int yr;
  386. char aw;
  387. char ah;
  388. short buffsz; /* Character buffer size, bytes */
  389. char xmin;
  390. char ymin;
  391. char xmax;
  392. char ymax;
  393. } __attribute__ ((packed)) uab;
  394. struct { /* Alternate Usable Area Self-Defining Parameter */
  395. char l; /* Length of this Self-Defining Parm */
  396. char sdpid; /* 0x02 if Alternate Usable Area */
  397. char res;
  398. char auaid; /* 0x01 is Id for the A U A */
  399. short wauai; /* Width of AUAi */
  400. short hauai; /* Height of AUAi */
  401. char auaunits; /* 0x00:in, 0x01:mm */
  402. int auaxr;
  403. int auayr;
  404. char awauai;
  405. char ahauai;
  406. } __attribute__ ((packed)) aua;
  407. } __attribute__ ((packed));
  408. static struct diag210 raw3270_init_diag210;
  409. static DEFINE_MUTEX(raw3270_init_mutex);
  410. static int
  411. raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
  412. struct irb *irb)
  413. {
  414. /*
  415. * Unit-Check Processing:
  416. * Expect Command Reject or Intervention Required.
  417. */
  418. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
  419. /* Request finished abnormally. */
  420. if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
  421. set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
  422. return RAW3270_IO_BUSY;
  423. }
  424. }
  425. if (rq) {
  426. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
  427. if (irb->ecw[0] & SNS0_CMD_REJECT)
  428. rq->rc = -EOPNOTSUPP;
  429. else
  430. rq->rc = -EIO;
  431. } else
  432. /* Request finished normally. Copy residual count. */
  433. rq->rescnt = irb->scsw.cmd.count;
  434. }
  435. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  436. set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
  437. wake_up(&raw3270_wait_queue);
  438. }
  439. return RAW3270_IO_DONE;
  440. }
  441. static struct raw3270_fn raw3270_init_fn = {
  442. .intv = raw3270_init_irq
  443. };
  444. static struct raw3270_view raw3270_init_view = {
  445. .fn = &raw3270_init_fn
  446. };
  447. /*
  448. * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
  449. * Wait for end of request. The request must have been started
  450. * with raw3270_start, rc = 0. The device lock may NOT have been
  451. * released between calling raw3270_start and raw3270_wait.
  452. */
  453. static void
  454. raw3270_wake_init(struct raw3270_request *rq, void *data)
  455. {
  456. wake_up((wait_queue_head_t *) data);
  457. }
  458. /*
  459. * Special wait function that can cope with console initialization.
  460. */
  461. static int
  462. raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
  463. struct raw3270_request *rq)
  464. {
  465. unsigned long flags;
  466. int rc;
  467. #ifdef CONFIG_TN3270_CONSOLE
  468. if (raw3270_registered == 0) {
  469. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  470. rq->callback = NULL;
  471. rc = __raw3270_start(rp, view, rq);
  472. if (rc == 0)
  473. while (!raw3270_request_final(rq)) {
  474. wait_cons_dev();
  475. barrier();
  476. }
  477. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  478. return rq->rc;
  479. }
  480. #endif
  481. rq->callback = raw3270_wake_init;
  482. rq->callback_data = &raw3270_wait_queue;
  483. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  484. rc = __raw3270_start(rp, view, rq);
  485. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  486. if (rc)
  487. return rc;
  488. /* Now wait for the completion. */
  489. rc = wait_event_interruptible(raw3270_wait_queue,
  490. raw3270_request_final(rq));
  491. if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
  492. raw3270_halt_io(view->dev, rq);
  493. /* No wait for the halt to complete. */
  494. wait_event(raw3270_wait_queue, raw3270_request_final(rq));
  495. return -ERESTARTSYS;
  496. }
  497. return rq->rc;
  498. }
  499. static int
  500. __raw3270_size_device_vm(struct raw3270 *rp)
  501. {
  502. int rc, model;
  503. struct ccw_dev_id dev_id;
  504. ccw_device_get_id(rp->cdev, &dev_id);
  505. raw3270_init_diag210.vrdcdvno = dev_id.devno;
  506. raw3270_init_diag210.vrdclen = sizeof(struct diag210);
  507. rc = diag210(&raw3270_init_diag210);
  508. if (rc)
  509. return rc;
  510. model = raw3270_init_diag210.vrdccrmd;
  511. switch (model) {
  512. case 2:
  513. rp->model = model;
  514. rp->rows = 24;
  515. rp->cols = 80;
  516. break;
  517. case 3:
  518. rp->model = model;
  519. rp->rows = 32;
  520. rp->cols = 80;
  521. break;
  522. case 4:
  523. rp->model = model;
  524. rp->rows = 43;
  525. rp->cols = 80;
  526. break;
  527. case 5:
  528. rp->model = model;
  529. rp->rows = 27;
  530. rp->cols = 132;
  531. break;
  532. default:
  533. rc = -EOPNOTSUPP;
  534. break;
  535. }
  536. return rc;
  537. }
  538. static int
  539. __raw3270_size_device(struct raw3270 *rp)
  540. {
  541. static const unsigned char wbuf[] =
  542. { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
  543. struct raw3270_ua *uap;
  544. int rc;
  545. /*
  546. * To determine the size of the 3270 device we need to do:
  547. * 1) send a 'read partition' data stream to the device
  548. * 2) wait for the attn interrupt that precedes the query reply
  549. * 3) do a read modified to get the query reply
  550. * To make things worse we have to cope with intervention
  551. * required (3270 device switched to 'stand-by') and command
  552. * rejects (old devices that can't do 'read partition').
  553. */
  554. memset(&rp->init_request, 0, sizeof(rp->init_request));
  555. memset(&rp->init_data, 0, 256);
  556. /* Store 'read partition' data stream to init_data */
  557. memcpy(&rp->init_data, wbuf, sizeof(wbuf));
  558. INIT_LIST_HEAD(&rp->init_request.list);
  559. rp->init_request.ccw.cmd_code = TC_WRITESF;
  560. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  561. rp->init_request.ccw.count = sizeof(wbuf);
  562. rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
  563. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  564. if (rc)
  565. /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
  566. return rc;
  567. /* Wait for attention interrupt. */
  568. #ifdef CONFIG_TN3270_CONSOLE
  569. if (raw3270_registered == 0) {
  570. unsigned long flags;
  571. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  572. while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
  573. wait_cons_dev();
  574. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  575. } else
  576. #endif
  577. rc = wait_event_interruptible(raw3270_wait_queue,
  578. test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
  579. if (rc)
  580. return rc;
  581. /*
  582. * The device accepted the 'read partition' command. Now
  583. * set up a read ccw and issue it.
  584. */
  585. rp->init_request.ccw.cmd_code = TC_READMOD;
  586. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  587. rp->init_request.ccw.count = sizeof(rp->init_data);
  588. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  589. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  590. if (rc)
  591. return rc;
  592. /* Got a Query Reply */
  593. uap = (struct raw3270_ua *) (rp->init_data + 1);
  594. /* Paranoia check. */
  595. if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
  596. return -EOPNOTSUPP;
  597. /* Copy rows/columns of default Usable Area */
  598. rp->rows = uap->uab.h;
  599. rp->cols = uap->uab.w;
  600. /* Check for 14 bit addressing */
  601. if ((uap->uab.flags0 & 0x0d) == 0x01)
  602. set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
  603. /* Check for Alternate Usable Area */
  604. if (uap->uab.l == sizeof(struct raw3270_ua) &&
  605. uap->aua.sdpid == 0x02) {
  606. rp->rows = uap->aua.hauai;
  607. rp->cols = uap->aua.wauai;
  608. }
  609. return 0;
  610. }
  611. static int
  612. raw3270_size_device(struct raw3270 *rp)
  613. {
  614. int rc;
  615. mutex_lock(&raw3270_init_mutex);
  616. rp->view = &raw3270_init_view;
  617. raw3270_init_view.dev = rp;
  618. if (MACHINE_IS_VM)
  619. rc = __raw3270_size_device_vm(rp);
  620. else
  621. rc = __raw3270_size_device(rp);
  622. raw3270_init_view.dev = NULL;
  623. rp->view = NULL;
  624. mutex_unlock(&raw3270_init_mutex);
  625. if (rc == 0) { /* Found something. */
  626. /* Try to find a model. */
  627. rp->model = 0;
  628. if (rp->rows == 24 && rp->cols == 80)
  629. rp->model = 2;
  630. if (rp->rows == 32 && rp->cols == 80)
  631. rp->model = 3;
  632. if (rp->rows == 43 && rp->cols == 80)
  633. rp->model = 4;
  634. if (rp->rows == 27 && rp->cols == 132)
  635. rp->model = 5;
  636. } else {
  637. /* Couldn't detect size. Use default model 2. */
  638. rp->model = 2;
  639. rp->rows = 24;
  640. rp->cols = 80;
  641. return 0;
  642. }
  643. return rc;
  644. }
  645. static int
  646. raw3270_reset_device(struct raw3270 *rp)
  647. {
  648. int rc;
  649. mutex_lock(&raw3270_init_mutex);
  650. memset(&rp->init_request, 0, sizeof(rp->init_request));
  651. memset(&rp->init_data, 0, sizeof(rp->init_data));
  652. /* Store reset data stream to init_data/init_request */
  653. rp->init_data[0] = TW_KR;
  654. INIT_LIST_HEAD(&rp->init_request.list);
  655. rp->init_request.ccw.cmd_code = TC_EWRITEA;
  656. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  657. rp->init_request.ccw.count = 1;
  658. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  659. rp->view = &raw3270_init_view;
  660. raw3270_init_view.dev = rp;
  661. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  662. raw3270_init_view.dev = NULL;
  663. rp->view = NULL;
  664. mutex_unlock(&raw3270_init_mutex);
  665. return rc;
  666. }
  667. int
  668. raw3270_reset(struct raw3270_view *view)
  669. {
  670. struct raw3270 *rp;
  671. int rc;
  672. rp = view->dev;
  673. if (!rp || rp->view != view ||
  674. test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  675. rc = -EACCES;
  676. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  677. rc = -ENODEV;
  678. else
  679. rc = raw3270_reset_device(view->dev);
  680. return rc;
  681. }
  682. /*
  683. * Setup new 3270 device.
  684. */
  685. static int
  686. raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
  687. {
  688. struct list_head *l;
  689. struct raw3270 *tmp;
  690. int minor;
  691. memset(rp, 0, sizeof(struct raw3270));
  692. /* Copy ebcdic -> ascii translation table. */
  693. memcpy(ascebc, _ascebc, 256);
  694. if (tubxcorrect) {
  695. /* correct brackets and circumflex */
  696. ascebc['['] = 0xad;
  697. ascebc[']'] = 0xbd;
  698. ascebc['^'] = 0xb0;
  699. }
  700. rp->ascebc = ascebc;
  701. /* Set defaults. */
  702. rp->rows = 24;
  703. rp->cols = 80;
  704. INIT_LIST_HEAD(&rp->req_queue);
  705. INIT_LIST_HEAD(&rp->view_list);
  706. /*
  707. * Add device to list and find the smallest unused minor
  708. * number for it. Note: there is no device with minor 0,
  709. * see special case for fs3270.c:fs3270_open().
  710. */
  711. mutex_lock(&raw3270_mutex);
  712. /* Keep the list sorted. */
  713. minor = RAW3270_FIRSTMINOR;
  714. rp->minor = -1;
  715. list_for_each(l, &raw3270_devices) {
  716. tmp = list_entry(l, struct raw3270, list);
  717. if (tmp->minor > minor) {
  718. rp->minor = minor;
  719. __list_add(&rp->list, l->prev, l);
  720. break;
  721. }
  722. minor++;
  723. }
  724. if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
  725. rp->minor = minor;
  726. list_add_tail(&rp->list, &raw3270_devices);
  727. }
  728. mutex_unlock(&raw3270_mutex);
  729. /* No free minor number? Then give up. */
  730. if (rp->minor == -1)
  731. return -EUSERS;
  732. rp->cdev = cdev;
  733. dev_set_drvdata(&cdev->dev, rp);
  734. cdev->handler = raw3270_irq;
  735. return 0;
  736. }
  737. #ifdef CONFIG_TN3270_CONSOLE
  738. /*
  739. * Setup 3270 device configured as console.
  740. */
  741. struct raw3270 __init *raw3270_setup_console(struct ccw_device *cdev)
  742. {
  743. struct raw3270 *rp;
  744. char *ascebc;
  745. int rc;
  746. rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
  747. ascebc = kzalloc(256, GFP_KERNEL);
  748. rc = raw3270_setup_device(cdev, rp, ascebc);
  749. if (rc)
  750. return ERR_PTR(rc);
  751. set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
  752. rc = raw3270_reset_device(rp);
  753. if (rc)
  754. return ERR_PTR(rc);
  755. rc = raw3270_size_device(rp);
  756. if (rc)
  757. return ERR_PTR(rc);
  758. rc = raw3270_reset_device(rp);
  759. if (rc)
  760. return ERR_PTR(rc);
  761. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  762. return rp;
  763. }
  764. void
  765. raw3270_wait_cons_dev(struct raw3270 *rp)
  766. {
  767. unsigned long flags;
  768. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  769. wait_cons_dev();
  770. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  771. }
  772. #endif
  773. /*
  774. * Create a 3270 device structure.
  775. */
  776. static struct raw3270 *
  777. raw3270_create_device(struct ccw_device *cdev)
  778. {
  779. struct raw3270 *rp;
  780. char *ascebc;
  781. int rc;
  782. rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
  783. if (!rp)
  784. return ERR_PTR(-ENOMEM);
  785. ascebc = kmalloc(256, GFP_KERNEL);
  786. if (!ascebc) {
  787. kfree(rp);
  788. return ERR_PTR(-ENOMEM);
  789. }
  790. rc = raw3270_setup_device(cdev, rp, ascebc);
  791. if (rc) {
  792. kfree(rp->ascebc);
  793. kfree(rp);
  794. rp = ERR_PTR(rc);
  795. }
  796. /* Get reference to ccw_device structure. */
  797. get_device(&cdev->dev);
  798. return rp;
  799. }
  800. /*
  801. * Activate a view.
  802. */
  803. int
  804. raw3270_activate_view(struct raw3270_view *view)
  805. {
  806. struct raw3270 *rp;
  807. struct raw3270_view *oldview, *nv;
  808. unsigned long flags;
  809. int rc;
  810. rp = view->dev;
  811. if (!rp)
  812. return -ENODEV;
  813. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  814. if (rp->view == view)
  815. rc = 0;
  816. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  817. rc = -ENODEV;
  818. else if (test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  819. rc = -EACCES;
  820. else {
  821. oldview = NULL;
  822. if (rp->view) {
  823. oldview = rp->view;
  824. oldview->fn->deactivate(oldview);
  825. }
  826. rp->view = view;
  827. rc = view->fn->activate(view);
  828. if (rc) {
  829. /* Didn't work. Try to reactivate the old view. */
  830. rp->view = oldview;
  831. if (!oldview || oldview->fn->activate(oldview) != 0) {
  832. /* Didn't work as well. Try any other view. */
  833. list_for_each_entry(nv, &rp->view_list, list)
  834. if (nv != view && nv != oldview) {
  835. rp->view = nv;
  836. if (nv->fn->activate(nv) == 0)
  837. break;
  838. rp->view = NULL;
  839. }
  840. }
  841. }
  842. }
  843. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  844. return rc;
  845. }
  846. /*
  847. * Deactivate current view.
  848. */
  849. void
  850. raw3270_deactivate_view(struct raw3270_view *view)
  851. {
  852. unsigned long flags;
  853. struct raw3270 *rp;
  854. rp = view->dev;
  855. if (!rp)
  856. return;
  857. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  858. if (rp->view == view) {
  859. view->fn->deactivate(view);
  860. rp->view = NULL;
  861. /* Move deactivated view to end of list. */
  862. list_del_init(&view->list);
  863. list_add_tail(&view->list, &rp->view_list);
  864. /* Try to activate another view. */
  865. if (test_bit(RAW3270_FLAGS_READY, &rp->flags) &&
  866. !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
  867. list_for_each_entry(view, &rp->view_list, list) {
  868. rp->view = view;
  869. if (view->fn->activate(view) == 0)
  870. break;
  871. rp->view = NULL;
  872. }
  873. }
  874. }
  875. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  876. }
  877. /*
  878. * Add view to device with minor "minor".
  879. */
  880. int
  881. raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
  882. {
  883. unsigned long flags;
  884. struct raw3270 *rp;
  885. int rc;
  886. if (minor <= 0)
  887. return -ENODEV;
  888. mutex_lock(&raw3270_mutex);
  889. rc = -ENODEV;
  890. list_for_each_entry(rp, &raw3270_devices, list) {
  891. if (rp->minor != minor)
  892. continue;
  893. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  894. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  895. atomic_set(&view->ref_count, 2);
  896. view->dev = rp;
  897. view->fn = fn;
  898. view->model = rp->model;
  899. view->rows = rp->rows;
  900. view->cols = rp->cols;
  901. view->ascebc = rp->ascebc;
  902. spin_lock_init(&view->lock);
  903. list_add(&view->list, &rp->view_list);
  904. rc = 0;
  905. }
  906. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  907. break;
  908. }
  909. mutex_unlock(&raw3270_mutex);
  910. return rc;
  911. }
  912. /*
  913. * Find specific view of device with minor "minor".
  914. */
  915. struct raw3270_view *
  916. raw3270_find_view(struct raw3270_fn *fn, int minor)
  917. {
  918. struct raw3270 *rp;
  919. struct raw3270_view *view, *tmp;
  920. unsigned long flags;
  921. mutex_lock(&raw3270_mutex);
  922. view = ERR_PTR(-ENODEV);
  923. list_for_each_entry(rp, &raw3270_devices, list) {
  924. if (rp->minor != minor)
  925. continue;
  926. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  927. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  928. view = ERR_PTR(-ENOENT);
  929. list_for_each_entry(tmp, &rp->view_list, list) {
  930. if (tmp->fn == fn) {
  931. raw3270_get_view(tmp);
  932. view = tmp;
  933. break;
  934. }
  935. }
  936. }
  937. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  938. break;
  939. }
  940. mutex_unlock(&raw3270_mutex);
  941. return view;
  942. }
  943. /*
  944. * Remove view from device and free view structure via call to view->fn->free.
  945. */
  946. void
  947. raw3270_del_view(struct raw3270_view *view)
  948. {
  949. unsigned long flags;
  950. struct raw3270 *rp;
  951. struct raw3270_view *nv;
  952. rp = view->dev;
  953. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  954. if (rp->view == view) {
  955. view->fn->deactivate(view);
  956. rp->view = NULL;
  957. }
  958. list_del_init(&view->list);
  959. if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags) &&
  960. !test_bit(RAW3270_FLAGS_FROZEN, &rp->flags)) {
  961. /* Try to activate another view. */
  962. list_for_each_entry(nv, &rp->view_list, list) {
  963. if (nv->fn->activate(nv) == 0) {
  964. rp->view = nv;
  965. break;
  966. }
  967. }
  968. }
  969. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  970. /* Wait for reference counter to drop to zero. */
  971. atomic_dec(&view->ref_count);
  972. wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
  973. if (view->fn->free)
  974. view->fn->free(view);
  975. }
  976. /*
  977. * Remove a 3270 device structure.
  978. */
  979. static void
  980. raw3270_delete_device(struct raw3270 *rp)
  981. {
  982. struct ccw_device *cdev;
  983. /* Remove from device chain. */
  984. mutex_lock(&raw3270_mutex);
  985. if (rp->clttydev && !IS_ERR(rp->clttydev))
  986. device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  987. if (rp->cltubdev && !IS_ERR(rp->cltubdev))
  988. device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, rp->minor));
  989. list_del_init(&rp->list);
  990. mutex_unlock(&raw3270_mutex);
  991. /* Disconnect from ccw_device. */
  992. cdev = rp->cdev;
  993. rp->cdev = NULL;
  994. dev_set_drvdata(&cdev->dev, NULL);
  995. cdev->handler = NULL;
  996. /* Put ccw_device structure. */
  997. put_device(&cdev->dev);
  998. /* Now free raw3270 structure. */
  999. kfree(rp->ascebc);
  1000. kfree(rp);
  1001. }
  1002. static int
  1003. raw3270_probe (struct ccw_device *cdev)
  1004. {
  1005. return 0;
  1006. }
  1007. /*
  1008. * Additional attributes for a 3270 device
  1009. */
  1010. static ssize_t
  1011. raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
  1012. {
  1013. return snprintf(buf, PAGE_SIZE, "%i\n",
  1014. ((struct raw3270 *) dev_get_drvdata(dev))->model);
  1015. }
  1016. static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
  1017. static ssize_t
  1018. raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
  1019. {
  1020. return snprintf(buf, PAGE_SIZE, "%i\n",
  1021. ((struct raw3270 *) dev_get_drvdata(dev))->rows);
  1022. }
  1023. static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
  1024. static ssize_t
  1025. raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
  1026. {
  1027. return snprintf(buf, PAGE_SIZE, "%i\n",
  1028. ((struct raw3270 *) dev_get_drvdata(dev))->cols);
  1029. }
  1030. static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
  1031. static struct attribute * raw3270_attrs[] = {
  1032. &dev_attr_model.attr,
  1033. &dev_attr_rows.attr,
  1034. &dev_attr_columns.attr,
  1035. NULL,
  1036. };
  1037. static struct attribute_group raw3270_attr_group = {
  1038. .attrs = raw3270_attrs,
  1039. };
  1040. static int raw3270_create_attributes(struct raw3270 *rp)
  1041. {
  1042. int rc;
  1043. rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1044. if (rc)
  1045. goto out;
  1046. rp->clttydev = device_create(class3270, &rp->cdev->dev,
  1047. MKDEV(IBM_TTY3270_MAJOR, rp->minor), NULL,
  1048. "tty%s", dev_name(&rp->cdev->dev));
  1049. if (IS_ERR(rp->clttydev)) {
  1050. rc = PTR_ERR(rp->clttydev);
  1051. goto out_ttydev;
  1052. }
  1053. rp->cltubdev = device_create(class3270, &rp->cdev->dev,
  1054. MKDEV(IBM_FS3270_MAJOR, rp->minor), NULL,
  1055. "tub%s", dev_name(&rp->cdev->dev));
  1056. if (!IS_ERR(rp->cltubdev))
  1057. goto out;
  1058. rc = PTR_ERR(rp->cltubdev);
  1059. device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1060. out_ttydev:
  1061. sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1062. out:
  1063. return rc;
  1064. }
  1065. /*
  1066. * Notifier for device addition/removal
  1067. */
  1068. struct raw3270_notifier {
  1069. struct list_head list;
  1070. void (*notifier)(int, int);
  1071. };
  1072. static LIST_HEAD(raw3270_notifier);
  1073. int raw3270_register_notifier(void (*notifier)(int, int))
  1074. {
  1075. struct raw3270_notifier *np;
  1076. struct raw3270 *rp;
  1077. np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
  1078. if (!np)
  1079. return -ENOMEM;
  1080. np->notifier = notifier;
  1081. mutex_lock(&raw3270_mutex);
  1082. list_add_tail(&np->list, &raw3270_notifier);
  1083. list_for_each_entry(rp, &raw3270_devices, list) {
  1084. get_device(&rp->cdev->dev);
  1085. notifier(rp->minor, 1);
  1086. }
  1087. mutex_unlock(&raw3270_mutex);
  1088. return 0;
  1089. }
  1090. void raw3270_unregister_notifier(void (*notifier)(int, int))
  1091. {
  1092. struct raw3270_notifier *np;
  1093. mutex_lock(&raw3270_mutex);
  1094. list_for_each_entry(np, &raw3270_notifier, list)
  1095. if (np->notifier == notifier) {
  1096. list_del(&np->list);
  1097. kfree(np);
  1098. break;
  1099. }
  1100. mutex_unlock(&raw3270_mutex);
  1101. }
  1102. /*
  1103. * Set 3270 device online.
  1104. */
  1105. static int
  1106. raw3270_set_online (struct ccw_device *cdev)
  1107. {
  1108. struct raw3270 *rp;
  1109. struct raw3270_notifier *np;
  1110. int rc;
  1111. rp = raw3270_create_device(cdev);
  1112. if (IS_ERR(rp))
  1113. return PTR_ERR(rp);
  1114. rc = raw3270_reset_device(rp);
  1115. if (rc)
  1116. goto failure;
  1117. rc = raw3270_size_device(rp);
  1118. if (rc)
  1119. goto failure;
  1120. rc = raw3270_reset_device(rp);
  1121. if (rc)
  1122. goto failure;
  1123. rc = raw3270_create_attributes(rp);
  1124. if (rc)
  1125. goto failure;
  1126. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  1127. mutex_lock(&raw3270_mutex);
  1128. list_for_each_entry(np, &raw3270_notifier, list)
  1129. np->notifier(rp->minor, 1);
  1130. mutex_unlock(&raw3270_mutex);
  1131. return 0;
  1132. failure:
  1133. raw3270_delete_device(rp);
  1134. return rc;
  1135. }
  1136. /*
  1137. * Remove 3270 device structure.
  1138. */
  1139. static void
  1140. raw3270_remove (struct ccw_device *cdev)
  1141. {
  1142. unsigned long flags;
  1143. struct raw3270 *rp;
  1144. struct raw3270_view *v;
  1145. struct raw3270_notifier *np;
  1146. rp = dev_get_drvdata(&cdev->dev);
  1147. /*
  1148. * _remove is the opposite of _probe; it's probe that
  1149. * should set up rp. raw3270_remove gets entered for
  1150. * devices even if they haven't been varied online.
  1151. * Thus, rp may validly be NULL here.
  1152. */
  1153. if (rp == NULL)
  1154. return;
  1155. clear_bit(RAW3270_FLAGS_READY, &rp->flags);
  1156. sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
  1157. /* Deactivate current view and remove all views. */
  1158. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1159. if (rp->view) {
  1160. rp->view->fn->deactivate(rp->view);
  1161. rp->view = NULL;
  1162. }
  1163. while (!list_empty(&rp->view_list)) {
  1164. v = list_entry(rp->view_list.next, struct raw3270_view, list);
  1165. if (v->fn->release)
  1166. v->fn->release(v);
  1167. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1168. raw3270_del_view(v);
  1169. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1170. }
  1171. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1172. mutex_lock(&raw3270_mutex);
  1173. list_for_each_entry(np, &raw3270_notifier, list)
  1174. np->notifier(rp->minor, 0);
  1175. mutex_unlock(&raw3270_mutex);
  1176. /* Reset 3270 device. */
  1177. raw3270_reset_device(rp);
  1178. /* And finally remove it. */
  1179. raw3270_delete_device(rp);
  1180. }
  1181. /*
  1182. * Set 3270 device offline.
  1183. */
  1184. static int
  1185. raw3270_set_offline (struct ccw_device *cdev)
  1186. {
  1187. struct raw3270 *rp;
  1188. rp = dev_get_drvdata(&cdev->dev);
  1189. if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
  1190. return -EBUSY;
  1191. raw3270_remove(cdev);
  1192. return 0;
  1193. }
  1194. static int raw3270_pm_stop(struct ccw_device *cdev)
  1195. {
  1196. struct raw3270 *rp;
  1197. struct raw3270_view *view;
  1198. unsigned long flags;
  1199. rp = dev_get_drvdata(&cdev->dev);
  1200. if (!rp)
  1201. return 0;
  1202. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  1203. if (rp->view)
  1204. rp->view->fn->deactivate(rp->view);
  1205. if (!test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags)) {
  1206. /*
  1207. * Release tty and fullscreen for all non-console
  1208. * devices.
  1209. */
  1210. list_for_each_entry(view, &rp->view_list, list) {
  1211. if (view->fn->release)
  1212. view->fn->release(view);
  1213. }
  1214. }
  1215. set_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
  1216. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  1217. return 0;
  1218. }
  1219. static int raw3270_pm_start(struct ccw_device *cdev)
  1220. {
  1221. struct raw3270 *rp;
  1222. unsigned long flags;
  1223. rp = dev_get_drvdata(&cdev->dev);
  1224. if (!rp)
  1225. return 0;
  1226. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  1227. clear_bit(RAW3270_FLAGS_FROZEN, &rp->flags);
  1228. if (rp->view)
  1229. rp->view->fn->activate(rp->view);
  1230. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  1231. return 0;
  1232. }
  1233. void raw3270_pm_unfreeze(struct raw3270_view *view)
  1234. {
  1235. #ifdef CONFIG_TN3270_CONSOLE
  1236. struct raw3270 *rp;
  1237. rp = view->dev;
  1238. if (rp && test_bit(RAW3270_FLAGS_FROZEN, &rp->flags))
  1239. ccw_device_force_console();
  1240. #endif
  1241. }
  1242. static struct ccw_device_id raw3270_id[] = {
  1243. { CCW_DEVICE(0x3270, 0) },
  1244. { CCW_DEVICE(0x3271, 0) },
  1245. { CCW_DEVICE(0x3272, 0) },
  1246. { CCW_DEVICE(0x3273, 0) },
  1247. { CCW_DEVICE(0x3274, 0) },
  1248. { CCW_DEVICE(0x3275, 0) },
  1249. { CCW_DEVICE(0x3276, 0) },
  1250. { CCW_DEVICE(0x3277, 0) },
  1251. { CCW_DEVICE(0x3278, 0) },
  1252. { CCW_DEVICE(0x3279, 0) },
  1253. { CCW_DEVICE(0x3174, 0) },
  1254. { /* end of list */ },
  1255. };
  1256. static struct ccw_driver raw3270_ccw_driver = {
  1257. .driver = {
  1258. .name = "3270",
  1259. .owner = THIS_MODULE,
  1260. },
  1261. .ids = raw3270_id,
  1262. .probe = &raw3270_probe,
  1263. .remove = &raw3270_remove,
  1264. .set_online = &raw3270_set_online,
  1265. .set_offline = &raw3270_set_offline,
  1266. .freeze = &raw3270_pm_stop,
  1267. .thaw = &raw3270_pm_start,
  1268. .restore = &raw3270_pm_start,
  1269. };
  1270. static int
  1271. raw3270_init(void)
  1272. {
  1273. struct raw3270 *rp;
  1274. int rc;
  1275. if (raw3270_registered)
  1276. return 0;
  1277. raw3270_registered = 1;
  1278. rc = ccw_driver_register(&raw3270_ccw_driver);
  1279. if (rc == 0) {
  1280. /* Create attributes for early (= console) device. */
  1281. mutex_lock(&raw3270_mutex);
  1282. class3270 = class_create(THIS_MODULE, "3270");
  1283. list_for_each_entry(rp, &raw3270_devices, list) {
  1284. get_device(&rp->cdev->dev);
  1285. raw3270_create_attributes(rp);
  1286. }
  1287. mutex_unlock(&raw3270_mutex);
  1288. }
  1289. return rc;
  1290. }
  1291. static void
  1292. raw3270_exit(void)
  1293. {
  1294. ccw_driver_unregister(&raw3270_ccw_driver);
  1295. class_destroy(class3270);
  1296. }
  1297. MODULE_LICENSE("GPL");
  1298. module_init(raw3270_init);
  1299. module_exit(raw3270_exit);
  1300. EXPORT_SYMBOL(raw3270_request_alloc);
  1301. EXPORT_SYMBOL(raw3270_request_free);
  1302. EXPORT_SYMBOL(raw3270_request_reset);
  1303. EXPORT_SYMBOL(raw3270_request_set_cmd);
  1304. EXPORT_SYMBOL(raw3270_request_add_data);
  1305. EXPORT_SYMBOL(raw3270_request_set_data);
  1306. EXPORT_SYMBOL(raw3270_request_set_idal);
  1307. EXPORT_SYMBOL(raw3270_buffer_address);
  1308. EXPORT_SYMBOL(raw3270_add_view);
  1309. EXPORT_SYMBOL(raw3270_del_view);
  1310. EXPORT_SYMBOL(raw3270_find_view);
  1311. EXPORT_SYMBOL(raw3270_activate_view);
  1312. EXPORT_SYMBOL(raw3270_deactivate_view);
  1313. EXPORT_SYMBOL(raw3270_start);
  1314. EXPORT_SYMBOL(raw3270_start_locked);
  1315. EXPORT_SYMBOL(raw3270_start_irq);
  1316. EXPORT_SYMBOL(raw3270_reset);
  1317. EXPORT_SYMBOL(raw3270_register_notifier);
  1318. EXPORT_SYMBOL(raw3270_unregister_notifier);
  1319. EXPORT_SYMBOL(raw3270_wait_queue);