isicom.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version
  5. * 2 of the License, or (at your option) any later version.
  6. *
  7. * Original driver code supplied by Multi-Tech
  8. *
  9. * Changes
  10. * 1/9/98 alan@lxorguk.ukuu.org.uk
  11. * Merge to 2.0.x kernel tree
  12. * Obtain and use official major/minors
  13. * Loader switched to a misc device
  14. * (fixed range check bug as a side effect)
  15. * Printk clean up
  16. * 9/12/98 alan@lxorguk.ukuu.org.uk
  17. * Rough port to 2.1.x
  18. *
  19. * 10/6/99 sameer Merged the ISA and PCI drivers to
  20. * a new unified driver.
  21. *
  22. * 3/9/99 sameer Added support for ISI4616 cards.
  23. *
  24. * 16/9/99 sameer We do not force RTS low anymore.
  25. * This is to prevent the firmware
  26. * from getting confused.
  27. *
  28. * 26/10/99 sameer Cosmetic changes:The driver now
  29. * dumps the Port Count information
  30. * along with I/O address and IRQ.
  31. *
  32. * 13/12/99 sameer Fixed the problem with IRQ sharing.
  33. *
  34. * 10/5/00 sameer Fixed isicom_shutdown_board()
  35. * to not lower DTR on all the ports
  36. * when the last port on the card is
  37. * closed.
  38. *
  39. * 10/5/00 sameer Signal mask setup command added
  40. * to isicom_setup_port and
  41. * isicom_shutdown_port.
  42. *
  43. * 24/5/00 sameer The driver is now SMP aware.
  44. *
  45. *
  46. * 27/11/00 Vinayak P Risbud Fixed the Driver Crash Problem
  47. *
  48. *
  49. * 03/01/01 anil .s Added support for resetting the
  50. * internal modems on ISI cards.
  51. *
  52. * 08/02/01 anil .s Upgraded the driver for kernel
  53. * 2.4.x
  54. *
  55. * 11/04/01 Kevin Fixed firmware load problem with
  56. * ISIHP-4X card
  57. *
  58. * 30/04/01 anil .s Fixed the remote login through
  59. * ISI port problem. Now the link
  60. * does not go down before password
  61. * prompt.
  62. *
  63. * 03/05/01 anil .s Fixed the problem with IRQ sharing
  64. * among ISI-PCI cards.
  65. *
  66. * 03/05/01 anil .s Added support to display the version
  67. * info during insmod as well as module
  68. * listing by lsmod.
  69. *
  70. * 10/05/01 anil .s Done the modifications to the source
  71. * file and Install script so that the
  72. * same installation can be used for
  73. * 2.2.x and 2.4.x kernel.
  74. *
  75. * 06/06/01 anil .s Now we drop both dtr and rts during
  76. * shutdown_port as well as raise them
  77. * during isicom_config_port.
  78. *
  79. * 09/06/01 acme@conectiva.com.br use capable, not suser, do
  80. * restore_flags on failure in
  81. * isicom_send_break, verify put_user
  82. * result
  83. *
  84. * 11/02/03 ranjeeth Added support for 230 Kbps and 460 Kbps
  85. * Baud index extended to 21
  86. *
  87. * 20/03/03 ranjeeth Made to work for Linux Advanced server.
  88. * Taken care of license warning.
  89. *
  90. * 10/12/03 Ravindra Made to work for Fedora Core 1 of
  91. * Red Hat Distribution
  92. *
  93. * 06/01/05 Alan Cox Merged the ISI and base kernel strands
  94. * into a single 2.6 driver
  95. *
  96. * ***********************************************************
  97. *
  98. * To use this driver you also need the support package. You
  99. * can find this in RPM format on
  100. * ftp://ftp.linux.org.uk/pub/linux/alan
  101. *
  102. * You can find the original tools for this direct from Multitech
  103. * ftp://ftp.multitech.com/ISI-Cards/
  104. *
  105. * Having installed the cards the module options (/etc/modprobe.conf)
  106. *
  107. * options isicom io=card1,card2,card3,card4 irq=card1,card2,card3,card4
  108. *
  109. * Omit those entries for boards you don't have installed.
  110. *
  111. * TODO
  112. * Merge testing
  113. * 64-bit verification
  114. */
  115. #include <linux/module.h>
  116. #include <linux/firmware.h>
  117. #include <linux/kernel.h>
  118. #include <linux/tty.h>
  119. #include <linux/tty_flip.h>
  120. #include <linux/termios.h>
  121. #include <linux/fs.h>
  122. #include <linux/sched.h>
  123. #include <linux/serial.h>
  124. #include <linux/smp_lock.h>
  125. #include <linux/mm.h>
  126. #include <linux/interrupt.h>
  127. #include <linux/timer.h>
  128. #include <linux/delay.h>
  129. #include <linux/ioport.h>
  130. #include <linux/uaccess.h>
  131. #include <linux/io.h>
  132. #include <asm/system.h>
  133. #include <linux/pci.h>
  134. #include <linux/isicom.h>
  135. #define InterruptTheCard(base) outw(0, (base) + 0xc)
  136. #define ClearInterrupt(base) inw((base) + 0x0a)
  137. #define pr_dbg(str...) pr_debug("ISICOM: " str)
  138. #ifdef DEBUG
  139. #define isicom_paranoia_check(a, b, c) __isicom_paranoia_check((a), (b), (c))
  140. #else
  141. #define isicom_paranoia_check(a, b, c) 0
  142. #endif
  143. static int isicom_probe(struct pci_dev *, const struct pci_device_id *);
  144. static void __devexit isicom_remove(struct pci_dev *);
  145. static struct pci_device_id isicom_pci_tbl[] = {
  146. { PCI_DEVICE(VENDOR_ID, 0x2028) },
  147. { PCI_DEVICE(VENDOR_ID, 0x2051) },
  148. { PCI_DEVICE(VENDOR_ID, 0x2052) },
  149. { PCI_DEVICE(VENDOR_ID, 0x2053) },
  150. { PCI_DEVICE(VENDOR_ID, 0x2054) },
  151. { PCI_DEVICE(VENDOR_ID, 0x2055) },
  152. { PCI_DEVICE(VENDOR_ID, 0x2056) },
  153. { PCI_DEVICE(VENDOR_ID, 0x2057) },
  154. { PCI_DEVICE(VENDOR_ID, 0x2058) },
  155. { 0 }
  156. };
  157. MODULE_DEVICE_TABLE(pci, isicom_pci_tbl);
  158. static struct pci_driver isicom_driver = {
  159. .name = "isicom",
  160. .id_table = isicom_pci_tbl,
  161. .probe = isicom_probe,
  162. .remove = __devexit_p(isicom_remove)
  163. };
  164. static int prev_card = 3; /* start servicing isi_card[0] */
  165. static struct tty_driver *isicom_normal;
  166. static void isicom_tx(unsigned long _data);
  167. static void isicom_start(struct tty_struct *tty);
  168. static DEFINE_TIMER(tx, isicom_tx, 0, 0);
  169. /* baud index mappings from linux defns to isi */
  170. static signed char linuxb_to_isib[] = {
  171. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 16, 17, 18, 19, 20, 21
  172. };
  173. struct isi_board {
  174. unsigned long base;
  175. int irq;
  176. unsigned char port_count;
  177. unsigned short status;
  178. unsigned short port_status; /* each bit for each port */
  179. unsigned short shift_count;
  180. struct isi_port *ports;
  181. signed char count;
  182. spinlock_t card_lock; /* Card wide lock 11/5/00 -sameer */
  183. unsigned long flags;
  184. unsigned int index;
  185. };
  186. struct isi_port {
  187. unsigned short magic;
  188. struct tty_port port;
  189. u16 channel;
  190. u16 status;
  191. struct isi_board *card;
  192. unsigned char *xmit_buf;
  193. int xmit_head;
  194. int xmit_tail;
  195. int xmit_cnt;
  196. };
  197. static struct isi_board isi_card[BOARD_COUNT];
  198. static struct isi_port isi_ports[PORT_COUNT];
  199. /*
  200. * Locking functions for card level locking. We need to own both
  201. * the kernel lock for the card and have the card in a position that
  202. * it wants to talk.
  203. */
  204. static inline int WaitTillCardIsFree(unsigned long base)
  205. {
  206. unsigned int count = 0;
  207. unsigned int a = in_atomic(); /* do we run under spinlock? */
  208. while (!(inw(base + 0xe) & 0x1) && count++ < 100)
  209. if (a)
  210. mdelay(1);
  211. else
  212. msleep(1);
  213. return !(inw(base + 0xe) & 0x1);
  214. }
  215. static int lock_card(struct isi_board *card)
  216. {
  217. unsigned long base = card->base;
  218. unsigned int retries, a;
  219. for (retries = 0; retries < 10; retries++) {
  220. spin_lock_irqsave(&card->card_lock, card->flags);
  221. for (a = 0; a < 10; a++) {
  222. if (inw(base + 0xe) & 0x1)
  223. return 1;
  224. udelay(10);
  225. }
  226. spin_unlock_irqrestore(&card->card_lock, card->flags);
  227. msleep(10);
  228. }
  229. printk(KERN_WARNING "ISICOM: Failed to lock Card (0x%lx)\n",
  230. card->base);
  231. return 0; /* Failed to acquire the card! */
  232. }
  233. static void unlock_card(struct isi_board *card)
  234. {
  235. spin_unlock_irqrestore(&card->card_lock, card->flags);
  236. }
  237. /*
  238. * ISI Card specific ops ...
  239. */
  240. /* card->lock HAS to be held */
  241. static void raise_dtr(struct isi_port *port)
  242. {
  243. struct isi_board *card = port->card;
  244. unsigned long base = card->base;
  245. u16 channel = port->channel;
  246. if (WaitTillCardIsFree(base))
  247. return;
  248. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  249. outw(0x0504, base);
  250. InterruptTheCard(base);
  251. port->status |= ISI_DTR;
  252. }
  253. /* card->lock HAS to be held */
  254. static inline void drop_dtr(struct isi_port *port)
  255. {
  256. struct isi_board *card = port->card;
  257. unsigned long base = card->base;
  258. u16 channel = port->channel;
  259. if (WaitTillCardIsFree(base))
  260. return;
  261. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  262. outw(0x0404, base);
  263. InterruptTheCard(base);
  264. port->status &= ~ISI_DTR;
  265. }
  266. /* card->lock HAS to be held */
  267. static inline void raise_rts(struct isi_port *port)
  268. {
  269. struct isi_board *card = port->card;
  270. unsigned long base = card->base;
  271. u16 channel = port->channel;
  272. if (WaitTillCardIsFree(base))
  273. return;
  274. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  275. outw(0x0a04, base);
  276. InterruptTheCard(base);
  277. port->status |= ISI_RTS;
  278. }
  279. /* card->lock HAS to be held */
  280. static inline void drop_rts(struct isi_port *port)
  281. {
  282. struct isi_board *card = port->card;
  283. unsigned long base = card->base;
  284. u16 channel = port->channel;
  285. if (WaitTillCardIsFree(base))
  286. return;
  287. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  288. outw(0x0804, base);
  289. InterruptTheCard(base);
  290. port->status &= ~ISI_RTS;
  291. }
  292. /* card->lock MUST NOT be held */
  293. static void isicom_dtr_rts(struct tty_port *port, int on)
  294. {
  295. struct isi_port *ip = container_of(port, struct isi_port, port);
  296. struct isi_board *card = ip->card;
  297. unsigned long base = card->base;
  298. u16 channel = ip->channel;
  299. if (!lock_card(card))
  300. return;
  301. if (on) {
  302. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  303. outw(0x0f04, base);
  304. InterruptTheCard(base);
  305. ip->status |= (ISI_DTR | ISI_RTS);
  306. } else {
  307. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  308. outw(0x0C04, base);
  309. InterruptTheCard(base);
  310. ip->status &= ~(ISI_DTR | ISI_RTS);
  311. }
  312. unlock_card(card);
  313. }
  314. /* card->lock HAS to be held */
  315. static void drop_dtr_rts(struct isi_port *port)
  316. {
  317. struct isi_board *card = port->card;
  318. unsigned long base = card->base;
  319. u16 channel = port->channel;
  320. if (WaitTillCardIsFree(base))
  321. return;
  322. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  323. outw(0x0c04, base);
  324. InterruptTheCard(base);
  325. port->status &= ~(ISI_RTS | ISI_DTR);
  326. }
  327. /*
  328. * ISICOM Driver specific routines ...
  329. *
  330. */
  331. static inline int __isicom_paranoia_check(struct isi_port const *port,
  332. char *name, const char *routine)
  333. {
  334. if (!port) {
  335. printk(KERN_WARNING "ISICOM: Warning: bad isicom magic for "
  336. "dev %s in %s.\n", name, routine);
  337. return 1;
  338. }
  339. if (port->magic != ISICOM_MAGIC) {
  340. printk(KERN_WARNING "ISICOM: Warning: NULL isicom port for "
  341. "dev %s in %s.\n", name, routine);
  342. return 1;
  343. }
  344. return 0;
  345. }
  346. /*
  347. * Transmitter.
  348. *
  349. * We shovel data into the card buffers on a regular basis. The card
  350. * will do the rest of the work for us.
  351. */
  352. static void isicom_tx(unsigned long _data)
  353. {
  354. unsigned long flags, base;
  355. unsigned int retries;
  356. short count = (BOARD_COUNT-1), card;
  357. short txcount, wrd, residue, word_count, cnt;
  358. struct isi_port *port;
  359. struct tty_struct *tty;
  360. /* find next active board */
  361. card = (prev_card + 1) & 0x0003;
  362. while (count-- > 0) {
  363. if (isi_card[card].status & BOARD_ACTIVE)
  364. break;
  365. card = (card + 1) & 0x0003;
  366. }
  367. if (!(isi_card[card].status & BOARD_ACTIVE))
  368. goto sched_again;
  369. prev_card = card;
  370. count = isi_card[card].port_count;
  371. port = isi_card[card].ports;
  372. base = isi_card[card].base;
  373. spin_lock_irqsave(&isi_card[card].card_lock, flags);
  374. for (retries = 0; retries < 100; retries++) {
  375. if (inw(base + 0xe) & 0x1)
  376. break;
  377. udelay(2);
  378. }
  379. if (retries >= 100)
  380. goto unlock;
  381. tty = tty_port_tty_get(&port->port);
  382. if (tty == NULL)
  383. goto put_unlock;
  384. for (; count > 0; count--, port++) {
  385. /* port not active or tx disabled to force flow control */
  386. if (!(port->port.flags & ASYNC_INITIALIZED) ||
  387. !(port->status & ISI_TXOK))
  388. continue;
  389. txcount = min_t(short, TX_SIZE, port->xmit_cnt);
  390. if (txcount <= 0 || tty->stopped || tty->hw_stopped)
  391. continue;
  392. if (!(inw(base + 0x02) & (1 << port->channel)))
  393. continue;
  394. pr_dbg("txing %d bytes, port%d.\n", txcount,
  395. port->channel + 1);
  396. outw((port->channel << isi_card[card].shift_count) | txcount,
  397. base);
  398. residue = NO;
  399. wrd = 0;
  400. while (1) {
  401. cnt = min_t(int, txcount, (SERIAL_XMIT_SIZE
  402. - port->xmit_tail));
  403. if (residue == YES) {
  404. residue = NO;
  405. if (cnt > 0) {
  406. wrd |= (port->port.xmit_buf[port->xmit_tail]
  407. << 8);
  408. port->xmit_tail = (port->xmit_tail + 1)
  409. & (SERIAL_XMIT_SIZE - 1);
  410. port->xmit_cnt--;
  411. txcount--;
  412. cnt--;
  413. outw(wrd, base);
  414. } else {
  415. outw(wrd, base);
  416. break;
  417. }
  418. }
  419. if (cnt <= 0)
  420. break;
  421. word_count = cnt >> 1;
  422. outsw(base, port->port.xmit_buf+port->xmit_tail, word_count);
  423. port->xmit_tail = (port->xmit_tail
  424. + (word_count << 1)) & (SERIAL_XMIT_SIZE - 1);
  425. txcount -= (word_count << 1);
  426. port->xmit_cnt -= (word_count << 1);
  427. if (cnt & 0x0001) {
  428. residue = YES;
  429. wrd = port->port.xmit_buf[port->xmit_tail];
  430. port->xmit_tail = (port->xmit_tail + 1)
  431. & (SERIAL_XMIT_SIZE - 1);
  432. port->xmit_cnt--;
  433. txcount--;
  434. }
  435. }
  436. InterruptTheCard(base);
  437. if (port->xmit_cnt <= 0)
  438. port->status &= ~ISI_TXOK;
  439. if (port->xmit_cnt <= WAKEUP_CHARS)
  440. tty_wakeup(tty);
  441. }
  442. put_unlock:
  443. tty_kref_put(tty);
  444. unlock:
  445. spin_unlock_irqrestore(&isi_card[card].card_lock, flags);
  446. /* schedule another tx for hopefully in about 10ms */
  447. sched_again:
  448. mod_timer(&tx, jiffies + msecs_to_jiffies(10));
  449. }
  450. /*
  451. * Main interrupt handler routine
  452. */
  453. static irqreturn_t isicom_interrupt(int irq, void *dev_id)
  454. {
  455. struct isi_board *card = dev_id;
  456. struct isi_port *port;
  457. struct tty_struct *tty;
  458. unsigned long base;
  459. u16 header, word_count, count, channel;
  460. short byte_count;
  461. unsigned char *rp;
  462. if (!card || !(card->status & FIRMWARE_LOADED))
  463. return IRQ_NONE;
  464. base = card->base;
  465. /* did the card interrupt us? */
  466. if (!(inw(base + 0x0e) & 0x02))
  467. return IRQ_NONE;
  468. spin_lock(&card->card_lock);
  469. /*
  470. * disable any interrupts from the PCI card and lower the
  471. * interrupt line
  472. */
  473. outw(0x8000, base+0x04);
  474. ClearInterrupt(base);
  475. inw(base); /* get the dummy word out */
  476. header = inw(base);
  477. channel = (header & 0x7800) >> card->shift_count;
  478. byte_count = header & 0xff;
  479. if (channel + 1 > card->port_count) {
  480. printk(KERN_WARNING "ISICOM: isicom_interrupt(0x%lx): "
  481. "%d(channel) > port_count.\n", base, channel+1);
  482. outw(0x0000, base+0x04); /* enable interrupts */
  483. spin_unlock(&card->card_lock);
  484. return IRQ_HANDLED;
  485. }
  486. port = card->ports + channel;
  487. if (!(port->port.flags & ASYNC_INITIALIZED)) {
  488. outw(0x0000, base+0x04); /* enable interrupts */
  489. spin_unlock(&card->card_lock);
  490. return IRQ_HANDLED;
  491. }
  492. tty = tty_port_tty_get(&port->port);
  493. if (tty == NULL) {
  494. word_count = byte_count >> 1;
  495. while (byte_count > 1) {
  496. inw(base);
  497. byte_count -= 2;
  498. }
  499. if (byte_count & 0x01)
  500. inw(base);
  501. outw(0x0000, base+0x04); /* enable interrupts */
  502. spin_unlock(&card->card_lock);
  503. return IRQ_HANDLED;
  504. }
  505. if (header & 0x8000) { /* Status Packet */
  506. header = inw(base);
  507. switch (header & 0xff) {
  508. case 0: /* Change in EIA signals */
  509. if (port->port.flags & ASYNC_CHECK_CD) {
  510. if (port->status & ISI_DCD) {
  511. if (!(header & ISI_DCD)) {
  512. /* Carrier has been lost */
  513. pr_dbg("interrupt: DCD->low.\n"
  514. );
  515. port->status &= ~ISI_DCD;
  516. tty_hangup(tty);
  517. }
  518. } else if (header & ISI_DCD) {
  519. /* Carrier has been detected */
  520. pr_dbg("interrupt: DCD->high.\n");
  521. port->status |= ISI_DCD;
  522. wake_up_interruptible(&port->port.open_wait);
  523. }
  524. } else {
  525. if (header & ISI_DCD)
  526. port->status |= ISI_DCD;
  527. else
  528. port->status &= ~ISI_DCD;
  529. }
  530. if (port->port.flags & ASYNC_CTS_FLOW) {
  531. if (tty->hw_stopped) {
  532. if (header & ISI_CTS) {
  533. port->port.tty->hw_stopped = 0;
  534. /* start tx ing */
  535. port->status |= (ISI_TXOK
  536. | ISI_CTS);
  537. tty_wakeup(tty);
  538. }
  539. } else if (!(header & ISI_CTS)) {
  540. tty->hw_stopped = 1;
  541. /* stop tx ing */
  542. port->status &= ~(ISI_TXOK | ISI_CTS);
  543. }
  544. } else {
  545. if (header & ISI_CTS)
  546. port->status |= ISI_CTS;
  547. else
  548. port->status &= ~ISI_CTS;
  549. }
  550. if (header & ISI_DSR)
  551. port->status |= ISI_DSR;
  552. else
  553. port->status &= ~ISI_DSR;
  554. if (header & ISI_RI)
  555. port->status |= ISI_RI;
  556. else
  557. port->status &= ~ISI_RI;
  558. break;
  559. case 1: /* Received Break !!! */
  560. tty_insert_flip_char(tty, 0, TTY_BREAK);
  561. if (port->port.flags & ASYNC_SAK)
  562. do_SAK(tty);
  563. tty_flip_buffer_push(tty);
  564. break;
  565. case 2: /* Statistics */
  566. pr_dbg("isicom_interrupt: stats!!!.\n");
  567. break;
  568. default:
  569. pr_dbg("Intr: Unknown code in status packet.\n");
  570. break;
  571. }
  572. } else { /* Data Packet */
  573. count = tty_prepare_flip_string(tty, &rp, byte_count & ~1);
  574. pr_dbg("Intr: Can rx %d of %d bytes.\n", count, byte_count);
  575. word_count = count >> 1;
  576. insw(base, rp, word_count);
  577. byte_count -= (word_count << 1);
  578. if (count & 0x0001) {
  579. tty_insert_flip_char(tty, inw(base) & 0xff,
  580. TTY_NORMAL);
  581. byte_count -= 2;
  582. }
  583. if (byte_count > 0) {
  584. pr_dbg("Intr(0x%lx:%d): Flip buffer overflow! dropping "
  585. "bytes...\n", base, channel + 1);
  586. /* drain out unread xtra data */
  587. while (byte_count > 0) {
  588. inw(base);
  589. byte_count -= 2;
  590. }
  591. }
  592. tty_flip_buffer_push(tty);
  593. }
  594. outw(0x0000, base+0x04); /* enable interrupts */
  595. spin_unlock(&card->card_lock);
  596. tty_kref_put(tty);
  597. return IRQ_HANDLED;
  598. }
  599. static void isicom_config_port(struct tty_struct *tty)
  600. {
  601. struct isi_port *port = tty->driver_data;
  602. struct isi_board *card = port->card;
  603. unsigned long baud;
  604. unsigned long base = card->base;
  605. u16 channel_setup, channel = port->channel,
  606. shift_count = card->shift_count;
  607. unsigned char flow_ctrl;
  608. /* FIXME: Switch to new tty baud API */
  609. baud = C_BAUD(tty);
  610. if (baud & CBAUDEX) {
  611. baud &= ~CBAUDEX;
  612. /* if CBAUDEX bit is on and the baud is set to either 50 or 75
  613. * then the card is programmed for 57.6Kbps or 115Kbps
  614. * respectively.
  615. */
  616. /* 1,2,3,4 => 57.6, 115.2, 230, 460 kbps resp. */
  617. if (baud < 1 || baud > 4)
  618. tty->termios->c_cflag &= ~CBAUDEX;
  619. else
  620. baud += 15;
  621. }
  622. if (baud == 15) {
  623. /* the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set
  624. * by the set_serial_info ioctl ... this is done by
  625. * the 'setserial' utility.
  626. */
  627. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  628. baud++; /* 57.6 Kbps */
  629. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  630. baud += 2; /* 115 Kbps */
  631. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  632. baud += 3; /* 230 kbps*/
  633. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  634. baud += 4; /* 460 kbps*/
  635. }
  636. if (linuxb_to_isib[baud] == -1) {
  637. /* hang up */
  638. drop_dtr(port);
  639. return;
  640. } else
  641. raise_dtr(port);
  642. if (WaitTillCardIsFree(base) == 0) {
  643. outw(0x8000 | (channel << shift_count) | 0x03, base);
  644. outw(linuxb_to_isib[baud] << 8 | 0x03, base);
  645. channel_setup = 0;
  646. switch (C_CSIZE(tty)) {
  647. case CS5:
  648. channel_setup |= ISICOM_CS5;
  649. break;
  650. case CS6:
  651. channel_setup |= ISICOM_CS6;
  652. break;
  653. case CS7:
  654. channel_setup |= ISICOM_CS7;
  655. break;
  656. case CS8:
  657. channel_setup |= ISICOM_CS8;
  658. break;
  659. }
  660. if (C_CSTOPB(tty))
  661. channel_setup |= ISICOM_2SB;
  662. if (C_PARENB(tty)) {
  663. channel_setup |= ISICOM_EVPAR;
  664. if (C_PARODD(tty))
  665. channel_setup |= ISICOM_ODPAR;
  666. }
  667. outw(channel_setup, base);
  668. InterruptTheCard(base);
  669. }
  670. if (C_CLOCAL(tty))
  671. port->port.flags &= ~ASYNC_CHECK_CD;
  672. else
  673. port->port.flags |= ASYNC_CHECK_CD;
  674. /* flow control settings ...*/
  675. flow_ctrl = 0;
  676. port->port.flags &= ~ASYNC_CTS_FLOW;
  677. if (C_CRTSCTS(tty)) {
  678. port->port.flags |= ASYNC_CTS_FLOW;
  679. flow_ctrl |= ISICOM_CTSRTS;
  680. }
  681. if (I_IXON(tty))
  682. flow_ctrl |= ISICOM_RESPOND_XONXOFF;
  683. if (I_IXOFF(tty))
  684. flow_ctrl |= ISICOM_INITIATE_XONXOFF;
  685. if (WaitTillCardIsFree(base) == 0) {
  686. outw(0x8000 | (channel << shift_count) | 0x04, base);
  687. outw(flow_ctrl << 8 | 0x05, base);
  688. outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
  689. InterruptTheCard(base);
  690. }
  691. /* rx enabled -> enable port for rx on the card */
  692. if (C_CREAD(tty)) {
  693. card->port_status |= (1 << channel);
  694. outw(card->port_status, base + 0x02);
  695. }
  696. }
  697. /* open et all */
  698. static inline void isicom_setup_board(struct isi_board *bp)
  699. {
  700. int channel;
  701. struct isi_port *port;
  702. unsigned long flags;
  703. spin_lock_irqsave(&bp->card_lock, flags);
  704. if (bp->status & BOARD_ACTIVE) {
  705. spin_unlock_irqrestore(&bp->card_lock, flags);
  706. return;
  707. }
  708. port = bp->ports;
  709. bp->status |= BOARD_ACTIVE;
  710. for (channel = 0; channel < bp->port_count; channel++, port++)
  711. drop_dtr_rts(port);
  712. bp->count++;
  713. spin_unlock_irqrestore(&bp->card_lock, flags);
  714. }
  715. static int isicom_activate(struct tty_port *tport, struct tty_struct *tty)
  716. {
  717. struct isi_port *port = container_of(tport, struct isi_port, port);
  718. struct isi_board *card = port->card;
  719. unsigned long flags;
  720. if (tty_port_alloc_xmit_buf(tport) < 0)
  721. return -ENOMEM;
  722. spin_lock_irqsave(&card->card_lock, flags);
  723. isicom_setup_board(card);
  724. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  725. /* discard any residual data */
  726. if (WaitTillCardIsFree(card->base) == 0) {
  727. outw(0x8000 | (port->channel << card->shift_count) | 0x02,
  728. card->base);
  729. outw(((ISICOM_KILLTX | ISICOM_KILLRX) << 8) | 0x06, card->base);
  730. InterruptTheCard(card->base);
  731. }
  732. isicom_config_port(tty);
  733. spin_unlock_irqrestore(&card->card_lock, flags);
  734. return 0;
  735. }
  736. static int isicom_carrier_raised(struct tty_port *port)
  737. {
  738. struct isi_port *ip = container_of(port, struct isi_port, port);
  739. return (ip->status & ISI_DCD)?1 : 0;
  740. }
  741. static struct tty_port *isicom_find_port(struct tty_struct *tty)
  742. {
  743. struct isi_port *port;
  744. struct isi_board *card;
  745. unsigned int board;
  746. int line = tty->index;
  747. if (line < 0 || line > PORT_COUNT-1)
  748. return NULL;
  749. board = BOARD(line);
  750. card = &isi_card[board];
  751. if (!(card->status & FIRMWARE_LOADED))
  752. return NULL;
  753. /* open on a port greater than the port count for the card !!! */
  754. if (line > ((board * 16) + card->port_count - 1))
  755. return NULL;
  756. port = &isi_ports[line];
  757. if (isicom_paranoia_check(port, tty->name, "isicom_open"))
  758. return NULL;
  759. return &port->port;
  760. }
  761. static int isicom_open(struct tty_struct *tty, struct file *filp)
  762. {
  763. struct isi_port *port;
  764. struct isi_board *card;
  765. struct tty_port *tport;
  766. tport = isicom_find_port(tty);
  767. if (tport == NULL)
  768. return -ENODEV;
  769. port = container_of(tport, struct isi_port, port);
  770. card = &isi_card[BOARD(tty->index)];
  771. return tty_port_open(tport, tty, filp);
  772. }
  773. /* close et all */
  774. static inline void isicom_shutdown_board(struct isi_board *bp)
  775. {
  776. if (bp->status & BOARD_ACTIVE)
  777. bp->status &= ~BOARD_ACTIVE;
  778. }
  779. /* card->lock HAS to be held */
  780. static void isicom_shutdown_port(struct isi_port *port)
  781. {
  782. struct isi_board *card = port->card;
  783. struct tty_struct *tty;
  784. tty = tty_port_tty_get(&port->port);
  785. tty_port_free_xmit_buf(&port->port);
  786. if (--card->count < 0) {
  787. pr_dbg("isicom_shutdown_port: bad board(0x%lx) count %d.\n",
  788. card->base, card->count);
  789. card->count = 0;
  790. }
  791. /* last port was closed, shutdown that board too */
  792. if (tty && C_HUPCL(tty)) {
  793. /* FIXME: this logic is bogus - it's the old logic that was
  794. bogus before but it still wants fixing */
  795. if (!card->count) {
  796. if (card->status & BOARD_ACTIVE)
  797. card->status &= ~BOARD_ACTIVE;
  798. }
  799. }
  800. tty_kref_put(tty);
  801. }
  802. static void isicom_flush_buffer(struct tty_struct *tty)
  803. {
  804. struct isi_port *port = tty->driver_data;
  805. struct isi_board *card = port->card;
  806. unsigned long flags;
  807. if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
  808. return;
  809. spin_lock_irqsave(&card->card_lock, flags);
  810. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  811. spin_unlock_irqrestore(&card->card_lock, flags);
  812. tty_wakeup(tty);
  813. }
  814. static void isicom_shutdown(struct tty_port *port)
  815. {
  816. struct isi_port *ip = container_of(port, struct isi_port, port);
  817. struct isi_board *card = ip->card;
  818. unsigned long flags;
  819. /* indicate to the card that no more data can be received
  820. on this port */
  821. spin_lock_irqsave(&card->card_lock, flags);
  822. card->port_status &= ~(1 << ip->channel);
  823. outw(card->port_status, card->base + 0x02);
  824. isicom_shutdown_port(ip);
  825. spin_unlock_irqrestore(&card->card_lock, flags);
  826. }
  827. static void isicom_close(struct tty_struct *tty, struct file *filp)
  828. {
  829. struct isi_port *ip = tty->driver_data;
  830. struct tty_port *port = &ip->port;
  831. if (isicom_paranoia_check(ip, tty->name, "isicom_close"))
  832. return;
  833. tty_port_close(port, tty, filp);
  834. }
  835. /* write et all */
  836. static int isicom_write(struct tty_struct *tty, const unsigned char *buf,
  837. int count)
  838. {
  839. struct isi_port *port = tty->driver_data;
  840. struct isi_board *card = port->card;
  841. unsigned long flags;
  842. int cnt, total = 0;
  843. if (isicom_paranoia_check(port, tty->name, "isicom_write"))
  844. return 0;
  845. spin_lock_irqsave(&card->card_lock, flags);
  846. while (1) {
  847. cnt = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt
  848. - 1, SERIAL_XMIT_SIZE - port->xmit_head));
  849. if (cnt <= 0)
  850. break;
  851. memcpy(port->port.xmit_buf + port->xmit_head, buf, cnt);
  852. port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE
  853. - 1);
  854. port->xmit_cnt += cnt;
  855. buf += cnt;
  856. count -= cnt;
  857. total += cnt;
  858. }
  859. if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
  860. port->status |= ISI_TXOK;
  861. spin_unlock_irqrestore(&card->card_lock, flags);
  862. return total;
  863. }
  864. /* put_char et all */
  865. static int isicom_put_char(struct tty_struct *tty, unsigned char ch)
  866. {
  867. struct isi_port *port = tty->driver_data;
  868. struct isi_board *card = port->card;
  869. unsigned long flags;
  870. if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
  871. return 0;
  872. spin_lock_irqsave(&card->card_lock, flags);
  873. if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
  874. spin_unlock_irqrestore(&card->card_lock, flags);
  875. return 0;
  876. }
  877. port->port.xmit_buf[port->xmit_head++] = ch;
  878. port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
  879. port->xmit_cnt++;
  880. spin_unlock_irqrestore(&card->card_lock, flags);
  881. return 1;
  882. }
  883. /* flush_chars et all */
  884. static void isicom_flush_chars(struct tty_struct *tty)
  885. {
  886. struct isi_port *port = tty->driver_data;
  887. if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
  888. return;
  889. if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  890. !port->port.xmit_buf)
  891. return;
  892. /* this tells the transmitter to consider this port for
  893. data output to the card ... that's the best we can do. */
  894. port->status |= ISI_TXOK;
  895. }
  896. /* write_room et all */
  897. static int isicom_write_room(struct tty_struct *tty)
  898. {
  899. struct isi_port *port = tty->driver_data;
  900. int free;
  901. if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
  902. return 0;
  903. free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
  904. if (free < 0)
  905. free = 0;
  906. return free;
  907. }
  908. /* chars_in_buffer et all */
  909. static int isicom_chars_in_buffer(struct tty_struct *tty)
  910. {
  911. struct isi_port *port = tty->driver_data;
  912. if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
  913. return 0;
  914. return port->xmit_cnt;
  915. }
  916. /* ioctl et all */
  917. static int isicom_send_break(struct tty_struct *tty, int length)
  918. {
  919. struct isi_port *port = tty->driver_data;
  920. struct isi_board *card = port->card;
  921. unsigned long base = card->base;
  922. if (length == -1)
  923. return -EOPNOTSUPP;
  924. if (!lock_card(card))
  925. return -EINVAL;
  926. outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
  927. outw((length & 0xff) << 8 | 0x00, base);
  928. outw((length & 0xff00), base);
  929. InterruptTheCard(base);
  930. unlock_card(card);
  931. return 0;
  932. }
  933. static int isicom_tiocmget(struct tty_struct *tty, struct file *file)
  934. {
  935. struct isi_port *port = tty->driver_data;
  936. /* just send the port status */
  937. u16 status = port->status;
  938. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  939. return -ENODEV;
  940. return ((status & ISI_RTS) ? TIOCM_RTS : 0) |
  941. ((status & ISI_DTR) ? TIOCM_DTR : 0) |
  942. ((status & ISI_DCD) ? TIOCM_CAR : 0) |
  943. ((status & ISI_DSR) ? TIOCM_DSR : 0) |
  944. ((status & ISI_CTS) ? TIOCM_CTS : 0) |
  945. ((status & ISI_RI ) ? TIOCM_RI : 0);
  946. }
  947. static int isicom_tiocmset(struct tty_struct *tty, struct file *file,
  948. unsigned int set, unsigned int clear)
  949. {
  950. struct isi_port *port = tty->driver_data;
  951. unsigned long flags;
  952. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  953. return -ENODEV;
  954. spin_lock_irqsave(&port->card->card_lock, flags);
  955. if (set & TIOCM_RTS)
  956. raise_rts(port);
  957. if (set & TIOCM_DTR)
  958. raise_dtr(port);
  959. if (clear & TIOCM_RTS)
  960. drop_rts(port);
  961. if (clear & TIOCM_DTR)
  962. drop_dtr(port);
  963. spin_unlock_irqrestore(&port->card->card_lock, flags);
  964. return 0;
  965. }
  966. static int isicom_set_serial_info(struct tty_struct *tty,
  967. struct serial_struct __user *info)
  968. {
  969. struct isi_port *port = tty->driver_data;
  970. struct serial_struct newinfo;
  971. int reconfig_port;
  972. if (copy_from_user(&newinfo, info, sizeof(newinfo)))
  973. return -EFAULT;
  974. lock_kernel();
  975. reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) !=
  976. (newinfo.flags & ASYNC_SPD_MASK));
  977. if (!capable(CAP_SYS_ADMIN)) {
  978. if ((newinfo.close_delay != port->port.close_delay) ||
  979. (newinfo.closing_wait != port->port.closing_wait) ||
  980. ((newinfo.flags & ~ASYNC_USR_MASK) !=
  981. (port->port.flags & ~ASYNC_USR_MASK))) {
  982. unlock_kernel();
  983. return -EPERM;
  984. }
  985. port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
  986. (newinfo.flags & ASYNC_USR_MASK));
  987. } else {
  988. port->port.close_delay = newinfo.close_delay;
  989. port->port.closing_wait = newinfo.closing_wait;
  990. port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
  991. (newinfo.flags & ASYNC_FLAGS));
  992. }
  993. if (reconfig_port) {
  994. unsigned long flags;
  995. spin_lock_irqsave(&port->card->card_lock, flags);
  996. isicom_config_port(tty);
  997. spin_unlock_irqrestore(&port->card->card_lock, flags);
  998. }
  999. unlock_kernel();
  1000. return 0;
  1001. }
  1002. static int isicom_get_serial_info(struct isi_port *port,
  1003. struct serial_struct __user *info)
  1004. {
  1005. struct serial_struct out_info;
  1006. lock_kernel();
  1007. memset(&out_info, 0, sizeof(out_info));
  1008. /* out_info.type = ? */
  1009. out_info.line = port - isi_ports;
  1010. out_info.port = port->card->base;
  1011. out_info.irq = port->card->irq;
  1012. out_info.flags = port->port.flags;
  1013. /* out_info.baud_base = ? */
  1014. out_info.close_delay = port->port.close_delay;
  1015. out_info.closing_wait = port->port.closing_wait;
  1016. unlock_kernel();
  1017. if (copy_to_user(info, &out_info, sizeof(out_info)))
  1018. return -EFAULT;
  1019. return 0;
  1020. }
  1021. static int isicom_ioctl(struct tty_struct *tty, struct file *filp,
  1022. unsigned int cmd, unsigned long arg)
  1023. {
  1024. struct isi_port *port = tty->driver_data;
  1025. void __user *argp = (void __user *)arg;
  1026. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  1027. return -ENODEV;
  1028. switch (cmd) {
  1029. case TIOCGSERIAL:
  1030. return isicom_get_serial_info(port, argp);
  1031. case TIOCSSERIAL:
  1032. return isicom_set_serial_info(tty, argp);
  1033. default:
  1034. return -ENOIOCTLCMD;
  1035. }
  1036. return 0;
  1037. }
  1038. /* set_termios et all */
  1039. static void isicom_set_termios(struct tty_struct *tty,
  1040. struct ktermios *old_termios)
  1041. {
  1042. struct isi_port *port = tty->driver_data;
  1043. unsigned long flags;
  1044. if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
  1045. return;
  1046. if (tty->termios->c_cflag == old_termios->c_cflag &&
  1047. tty->termios->c_iflag == old_termios->c_iflag)
  1048. return;
  1049. spin_lock_irqsave(&port->card->card_lock, flags);
  1050. isicom_config_port(tty);
  1051. spin_unlock_irqrestore(&port->card->card_lock, flags);
  1052. if ((old_termios->c_cflag & CRTSCTS) &&
  1053. !(tty->termios->c_cflag & CRTSCTS)) {
  1054. tty->hw_stopped = 0;
  1055. isicom_start(tty);
  1056. }
  1057. }
  1058. /* throttle et all */
  1059. static void isicom_throttle(struct tty_struct *tty)
  1060. {
  1061. struct isi_port *port = tty->driver_data;
  1062. struct isi_board *card = port->card;
  1063. if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
  1064. return;
  1065. /* tell the card that this port cannot handle any more data for now */
  1066. card->port_status &= ~(1 << port->channel);
  1067. outw(card->port_status, card->base + 0x02);
  1068. }
  1069. /* unthrottle et all */
  1070. static void isicom_unthrottle(struct tty_struct *tty)
  1071. {
  1072. struct isi_port *port = tty->driver_data;
  1073. struct isi_board *card = port->card;
  1074. if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
  1075. return;
  1076. /* tell the card that this port is ready to accept more data */
  1077. card->port_status |= (1 << port->channel);
  1078. outw(card->port_status, card->base + 0x02);
  1079. }
  1080. /* stop et all */
  1081. static void isicom_stop(struct tty_struct *tty)
  1082. {
  1083. struct isi_port *port = tty->driver_data;
  1084. if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
  1085. return;
  1086. /* this tells the transmitter not to consider this port for
  1087. data output to the card. */
  1088. port->status &= ~ISI_TXOK;
  1089. }
  1090. /* start et all */
  1091. static void isicom_start(struct tty_struct *tty)
  1092. {
  1093. struct isi_port *port = tty->driver_data;
  1094. if (isicom_paranoia_check(port, tty->name, "isicom_start"))
  1095. return;
  1096. /* this tells the transmitter to consider this port for
  1097. data output to the card. */
  1098. port->status |= ISI_TXOK;
  1099. }
  1100. static void isicom_hangup(struct tty_struct *tty)
  1101. {
  1102. struct isi_port *port = tty->driver_data;
  1103. if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
  1104. return;
  1105. tty_port_hangup(&port->port);
  1106. }
  1107. /*
  1108. * Driver init and deinit functions
  1109. */
  1110. static const struct tty_operations isicom_ops = {
  1111. .open = isicom_open,
  1112. .close = isicom_close,
  1113. .write = isicom_write,
  1114. .put_char = isicom_put_char,
  1115. .flush_chars = isicom_flush_chars,
  1116. .write_room = isicom_write_room,
  1117. .chars_in_buffer = isicom_chars_in_buffer,
  1118. .ioctl = isicom_ioctl,
  1119. .set_termios = isicom_set_termios,
  1120. .throttle = isicom_throttle,
  1121. .unthrottle = isicom_unthrottle,
  1122. .stop = isicom_stop,
  1123. .start = isicom_start,
  1124. .hangup = isicom_hangup,
  1125. .flush_buffer = isicom_flush_buffer,
  1126. .tiocmget = isicom_tiocmget,
  1127. .tiocmset = isicom_tiocmset,
  1128. .break_ctl = isicom_send_break,
  1129. };
  1130. static const struct tty_port_operations isicom_port_ops = {
  1131. .carrier_raised = isicom_carrier_raised,
  1132. .dtr_rts = isicom_dtr_rts,
  1133. .activate = isicom_activate,
  1134. .shutdown = isicom_shutdown,
  1135. };
  1136. static int __devinit reset_card(struct pci_dev *pdev,
  1137. const unsigned int card, unsigned int *signature)
  1138. {
  1139. struct isi_board *board = pci_get_drvdata(pdev);
  1140. unsigned long base = board->base;
  1141. unsigned int sig, portcount = 0;
  1142. int retval = 0;
  1143. dev_dbg(&pdev->dev, "ISILoad:Resetting Card%d at 0x%lx\n", card + 1,
  1144. base);
  1145. inw(base + 0x8);
  1146. msleep(10);
  1147. outw(0, base + 0x8); /* Reset */
  1148. msleep(1000);
  1149. sig = inw(base + 0x4) & 0xff;
  1150. if (sig != 0xa5 && sig != 0xbb && sig != 0xcc && sig != 0xdd &&
  1151. sig != 0xee) {
  1152. dev_warn(&pdev->dev, "ISILoad:Card%u reset failure (Possible "
  1153. "bad I/O Port Address 0x%lx).\n", card + 1, base);
  1154. dev_dbg(&pdev->dev, "Sig=0x%x\n", sig);
  1155. retval = -EIO;
  1156. goto end;
  1157. }
  1158. msleep(10);
  1159. portcount = inw(base + 0x2);
  1160. if (!(inw(base + 0xe) & 0x1) || (portcount != 0 && portcount != 4 &&
  1161. portcount != 8 && portcount != 16)) {
  1162. dev_err(&pdev->dev, "ISILoad:PCI Card%d reset failure.\n",
  1163. card + 1);
  1164. retval = -EIO;
  1165. goto end;
  1166. }
  1167. switch (sig) {
  1168. case 0xa5:
  1169. case 0xbb:
  1170. case 0xdd:
  1171. board->port_count = (portcount == 4) ? 4 : 8;
  1172. board->shift_count = 12;
  1173. break;
  1174. case 0xcc:
  1175. case 0xee:
  1176. board->port_count = 16;
  1177. board->shift_count = 11;
  1178. break;
  1179. }
  1180. dev_info(&pdev->dev, "-Done\n");
  1181. *signature = sig;
  1182. end:
  1183. return retval;
  1184. }
  1185. static int __devinit load_firmware(struct pci_dev *pdev,
  1186. const unsigned int index, const unsigned int signature)
  1187. {
  1188. struct isi_board *board = pci_get_drvdata(pdev);
  1189. const struct firmware *fw;
  1190. unsigned long base = board->base;
  1191. unsigned int a;
  1192. u16 word_count, status;
  1193. int retval = -EIO;
  1194. char *name;
  1195. u8 *data;
  1196. struct stframe {
  1197. u16 addr;
  1198. u16 count;
  1199. u8 data[0];
  1200. } *frame;
  1201. switch (signature) {
  1202. case 0xa5:
  1203. name = "isi608.bin";
  1204. break;
  1205. case 0xbb:
  1206. name = "isi608em.bin";
  1207. break;
  1208. case 0xcc:
  1209. name = "isi616em.bin";
  1210. break;
  1211. case 0xdd:
  1212. name = "isi4608.bin";
  1213. break;
  1214. case 0xee:
  1215. name = "isi4616.bin";
  1216. break;
  1217. default:
  1218. dev_err(&pdev->dev, "Unknown signature.\n");
  1219. goto end;
  1220. }
  1221. retval = request_firmware(&fw, name, &pdev->dev);
  1222. if (retval)
  1223. goto end;
  1224. retval = -EIO;
  1225. for (frame = (struct stframe *)fw->data;
  1226. frame < (struct stframe *)(fw->data + fw->size);
  1227. frame = (struct stframe *)((u8 *)(frame + 1) +
  1228. frame->count)) {
  1229. if (WaitTillCardIsFree(base))
  1230. goto errrelfw;
  1231. outw(0xf0, base); /* start upload sequence */
  1232. outw(0x00, base);
  1233. outw(frame->addr, base); /* lsb of address */
  1234. word_count = frame->count / 2 + frame->count % 2;
  1235. outw(word_count, base);
  1236. InterruptTheCard(base);
  1237. udelay(100); /* 0x2f */
  1238. if (WaitTillCardIsFree(base))
  1239. goto errrelfw;
  1240. status = inw(base + 0x4);
  1241. if (status != 0) {
  1242. dev_warn(&pdev->dev, "Card%d rejected load header:\n"
  1243. "Address:0x%x\n"
  1244. "Count:0x%x\n"
  1245. "Status:0x%x\n",
  1246. index + 1, frame->addr, frame->count, status);
  1247. goto errrelfw;
  1248. }
  1249. outsw(base, frame->data, word_count);
  1250. InterruptTheCard(base);
  1251. udelay(50); /* 0x0f */
  1252. if (WaitTillCardIsFree(base))
  1253. goto errrelfw;
  1254. status = inw(base + 0x4);
  1255. if (status != 0) {
  1256. dev_err(&pdev->dev, "Card%d got out of sync.Card "
  1257. "Status:0x%x\n", index + 1, status);
  1258. goto errrelfw;
  1259. }
  1260. }
  1261. /* XXX: should we test it by reading it back and comparing with original like
  1262. * in load firmware package? */
  1263. for (frame = (struct stframe *)fw->data;
  1264. frame < (struct stframe *)(fw->data + fw->size);
  1265. frame = (struct stframe *)((u8 *)(frame + 1) +
  1266. frame->count)) {
  1267. if (WaitTillCardIsFree(base))
  1268. goto errrelfw;
  1269. outw(0xf1, base); /* start download sequence */
  1270. outw(0x00, base);
  1271. outw(frame->addr, base); /* lsb of address */
  1272. word_count = (frame->count >> 1) + frame->count % 2;
  1273. outw(word_count + 1, base);
  1274. InterruptTheCard(base);
  1275. udelay(50); /* 0xf */
  1276. if (WaitTillCardIsFree(base))
  1277. goto errrelfw;
  1278. status = inw(base + 0x4);
  1279. if (status != 0) {
  1280. dev_warn(&pdev->dev, "Card%d rejected verify header:\n"
  1281. "Address:0x%x\n"
  1282. "Count:0x%x\n"
  1283. "Status: 0x%x\n",
  1284. index + 1, frame->addr, frame->count, status);
  1285. goto errrelfw;
  1286. }
  1287. data = kmalloc(word_count * 2, GFP_KERNEL);
  1288. if (data == NULL) {
  1289. dev_err(&pdev->dev, "Card%d, firmware upload "
  1290. "failed, not enough memory\n", index + 1);
  1291. goto errrelfw;
  1292. }
  1293. inw(base);
  1294. insw(base, data, word_count);
  1295. InterruptTheCard(base);
  1296. for (a = 0; a < frame->count; a++)
  1297. if (data[a] != frame->data[a]) {
  1298. kfree(data);
  1299. dev_err(&pdev->dev, "Card%d, firmware upload "
  1300. "failed\n", index + 1);
  1301. goto errrelfw;
  1302. }
  1303. kfree(data);
  1304. udelay(50); /* 0xf */
  1305. if (WaitTillCardIsFree(base))
  1306. goto errrelfw;
  1307. status = inw(base + 0x4);
  1308. if (status != 0) {
  1309. dev_err(&pdev->dev, "Card%d verify got out of sync. "
  1310. "Card Status:0x%x\n", index + 1, status);
  1311. goto errrelfw;
  1312. }
  1313. }
  1314. /* xfer ctrl */
  1315. if (WaitTillCardIsFree(base))
  1316. goto errrelfw;
  1317. outw(0xf2, base);
  1318. outw(0x800, base);
  1319. outw(0x0, base);
  1320. outw(0x0, base);
  1321. InterruptTheCard(base);
  1322. outw(0x0, base + 0x4); /* for ISI4608 cards */
  1323. board->status |= FIRMWARE_LOADED;
  1324. retval = 0;
  1325. errrelfw:
  1326. release_firmware(fw);
  1327. end:
  1328. return retval;
  1329. }
  1330. /*
  1331. * Insmod can set static symbols so keep these static
  1332. */
  1333. static unsigned int card_count;
  1334. static int __devinit isicom_probe(struct pci_dev *pdev,
  1335. const struct pci_device_id *ent)
  1336. {
  1337. unsigned int uninitialized_var(signature), index;
  1338. int retval = -EPERM;
  1339. struct isi_board *board = NULL;
  1340. if (card_count >= BOARD_COUNT)
  1341. goto err;
  1342. retval = pci_enable_device(pdev);
  1343. if (retval) {
  1344. dev_err(&pdev->dev, "failed to enable\n");
  1345. goto err;
  1346. }
  1347. dev_info(&pdev->dev, "ISI PCI Card(Device ID 0x%x)\n", ent->device);
  1348. /* allot the first empty slot in the array */
  1349. for (index = 0; index < BOARD_COUNT; index++)
  1350. if (isi_card[index].base == 0) {
  1351. board = &isi_card[index];
  1352. break;
  1353. }
  1354. board->index = index;
  1355. board->base = pci_resource_start(pdev, 3);
  1356. board->irq = pdev->irq;
  1357. card_count++;
  1358. pci_set_drvdata(pdev, board);
  1359. retval = pci_request_region(pdev, 3, ISICOM_NAME);
  1360. if (retval) {
  1361. dev_err(&pdev->dev, "I/O Region 0x%lx-0x%lx is busy. Card%d "
  1362. "will be disabled.\n", board->base, board->base + 15,
  1363. index + 1);
  1364. retval = -EBUSY;
  1365. goto errdec;
  1366. }
  1367. retval = request_irq(board->irq, isicom_interrupt,
  1368. IRQF_SHARED | IRQF_DISABLED, ISICOM_NAME, board);
  1369. if (retval < 0) {
  1370. dev_err(&pdev->dev, "Could not install handler at Irq %d. "
  1371. "Card%d will be disabled.\n", board->irq, index + 1);
  1372. goto errunrr;
  1373. }
  1374. retval = reset_card(pdev, index, &signature);
  1375. if (retval < 0)
  1376. goto errunri;
  1377. retval = load_firmware(pdev, index, signature);
  1378. if (retval < 0)
  1379. goto errunri;
  1380. for (index = 0; index < board->port_count; index++)
  1381. tty_register_device(isicom_normal, board->index * 16 + index,
  1382. &pdev->dev);
  1383. return 0;
  1384. errunri:
  1385. free_irq(board->irq, board);
  1386. errunrr:
  1387. pci_release_region(pdev, 3);
  1388. errdec:
  1389. board->base = 0;
  1390. card_count--;
  1391. pci_disable_device(pdev);
  1392. err:
  1393. return retval;
  1394. }
  1395. static void __devexit isicom_remove(struct pci_dev *pdev)
  1396. {
  1397. struct isi_board *board = pci_get_drvdata(pdev);
  1398. unsigned int i;
  1399. for (i = 0; i < board->port_count; i++)
  1400. tty_unregister_device(isicom_normal, board->index * 16 + i);
  1401. free_irq(board->irq, board);
  1402. pci_release_region(pdev, 3);
  1403. board->base = 0;
  1404. card_count--;
  1405. pci_disable_device(pdev);
  1406. }
  1407. static int __init isicom_init(void)
  1408. {
  1409. int retval, idx, channel;
  1410. struct isi_port *port;
  1411. for (idx = 0; idx < BOARD_COUNT; idx++) {
  1412. port = &isi_ports[idx * 16];
  1413. isi_card[idx].ports = port;
  1414. spin_lock_init(&isi_card[idx].card_lock);
  1415. for (channel = 0; channel < 16; channel++, port++) {
  1416. tty_port_init(&port->port);
  1417. port->port.ops = &isicom_port_ops;
  1418. port->magic = ISICOM_MAGIC;
  1419. port->card = &isi_card[idx];
  1420. port->channel = channel;
  1421. port->port.close_delay = 50 * HZ/100;
  1422. port->port.closing_wait = 3000 * HZ/100;
  1423. port->status = 0;
  1424. /* . . . */
  1425. }
  1426. isi_card[idx].base = 0;
  1427. isi_card[idx].irq = 0;
  1428. }
  1429. /* tty driver structure initialization */
  1430. isicom_normal = alloc_tty_driver(PORT_COUNT);
  1431. if (!isicom_normal) {
  1432. retval = -ENOMEM;
  1433. goto error;
  1434. }
  1435. isicom_normal->owner = THIS_MODULE;
  1436. isicom_normal->name = "ttyM";
  1437. isicom_normal->major = ISICOM_NMAJOR;
  1438. isicom_normal->minor_start = 0;
  1439. isicom_normal->type = TTY_DRIVER_TYPE_SERIAL;
  1440. isicom_normal->subtype = SERIAL_TYPE_NORMAL;
  1441. isicom_normal->init_termios = tty_std_termios;
  1442. isicom_normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
  1443. CLOCAL;
  1444. isicom_normal->flags = TTY_DRIVER_REAL_RAW |
  1445. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK;
  1446. tty_set_operations(isicom_normal, &isicom_ops);
  1447. retval = tty_register_driver(isicom_normal);
  1448. if (retval) {
  1449. pr_dbg("Couldn't register the dialin driver\n");
  1450. goto err_puttty;
  1451. }
  1452. retval = pci_register_driver(&isicom_driver);
  1453. if (retval < 0) {
  1454. printk(KERN_ERR "ISICOM: Unable to register pci driver.\n");
  1455. goto err_unrtty;
  1456. }
  1457. mod_timer(&tx, jiffies + 1);
  1458. return 0;
  1459. err_unrtty:
  1460. tty_unregister_driver(isicom_normal);
  1461. err_puttty:
  1462. put_tty_driver(isicom_normal);
  1463. error:
  1464. return retval;
  1465. }
  1466. static void __exit isicom_exit(void)
  1467. {
  1468. del_timer_sync(&tx);
  1469. pci_unregister_driver(&isicom_driver);
  1470. tty_unregister_driver(isicom_normal);
  1471. put_tty_driver(isicom_normal);
  1472. }
  1473. module_init(isicom_init);
  1474. module_exit(isicom_exit);
  1475. MODULE_AUTHOR("MultiTech");
  1476. MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
  1477. MODULE_LICENSE("GPL");