raw3270.c 33 KB

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