isicom.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  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. bp->count++;
  703. if (!(bp->status & BOARD_INIT)) {
  704. port = bp->ports;
  705. for (channel = 0; channel < bp->port_count; channel++, port++)
  706. drop_dtr_rts(port);
  707. }
  708. bp->status |= BOARD_ACTIVE | BOARD_INIT;
  709. }
  710. /* Activate and thus setup board are protected from races against shutdown
  711. by the tty_port mutex */
  712. static int isicom_activate(struct tty_port *tport, struct tty_struct *tty)
  713. {
  714. struct isi_port *port = container_of(tport, struct isi_port, port);
  715. struct isi_board *card = port->card;
  716. unsigned long flags;
  717. if (tty_port_alloc_xmit_buf(tport) < 0)
  718. return -ENOMEM;
  719. spin_lock_irqsave(&card->card_lock, flags);
  720. isicom_setup_board(card);
  721. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  722. /* discard any residual data */
  723. if (WaitTillCardIsFree(card->base) == 0) {
  724. outw(0x8000 | (port->channel << card->shift_count) | 0x02,
  725. card->base);
  726. outw(((ISICOM_KILLTX | ISICOM_KILLRX) << 8) | 0x06, card->base);
  727. InterruptTheCard(card->base);
  728. }
  729. isicom_config_port(tty);
  730. spin_unlock_irqrestore(&card->card_lock, flags);
  731. return 0;
  732. }
  733. static int isicom_carrier_raised(struct tty_port *port)
  734. {
  735. struct isi_port *ip = container_of(port, struct isi_port, port);
  736. return (ip->status & ISI_DCD)?1 : 0;
  737. }
  738. static struct tty_port *isicom_find_port(struct tty_struct *tty)
  739. {
  740. struct isi_port *port;
  741. struct isi_board *card;
  742. unsigned int board;
  743. int line = tty->index;
  744. if (line < 0 || line > PORT_COUNT-1)
  745. return NULL;
  746. board = BOARD(line);
  747. card = &isi_card[board];
  748. if (!(card->status & FIRMWARE_LOADED))
  749. return NULL;
  750. /* open on a port greater than the port count for the card !!! */
  751. if (line > ((board * 16) + card->port_count - 1))
  752. return NULL;
  753. port = &isi_ports[line];
  754. if (isicom_paranoia_check(port, tty->name, "isicom_open"))
  755. return NULL;
  756. return &port->port;
  757. }
  758. static int isicom_open(struct tty_struct *tty, struct file *filp)
  759. {
  760. struct isi_port *port;
  761. struct isi_board *card;
  762. struct tty_port *tport;
  763. tport = isicom_find_port(tty);
  764. if (tport == NULL)
  765. return -ENODEV;
  766. port = container_of(tport, struct isi_port, port);
  767. card = &isi_card[BOARD(tty->index)];
  768. return tty_port_open(tport, tty, filp);
  769. }
  770. /* close et all */
  771. /* card->lock HAS to be held */
  772. static void isicom_shutdown_port(struct isi_port *port)
  773. {
  774. struct isi_board *card = port->card;
  775. if (--card->count < 0) {
  776. pr_dbg("isicom_shutdown_port: bad board(0x%lx) count %d.\n",
  777. card->base, card->count);
  778. card->count = 0;
  779. }
  780. /* last port was closed, shutdown that board too */
  781. if (!card->count)
  782. card->status &= BOARD_ACTIVE;
  783. }
  784. static void isicom_flush_buffer(struct tty_struct *tty)
  785. {
  786. struct isi_port *port = tty->driver_data;
  787. struct isi_board *card = port->card;
  788. unsigned long flags;
  789. if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
  790. return;
  791. spin_lock_irqsave(&card->card_lock, flags);
  792. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  793. spin_unlock_irqrestore(&card->card_lock, flags);
  794. tty_wakeup(tty);
  795. }
  796. static void isicom_shutdown(struct tty_port *port)
  797. {
  798. struct isi_port *ip = container_of(port, struct isi_port, port);
  799. struct isi_board *card = ip->card;
  800. unsigned long flags;
  801. /* indicate to the card that no more data can be received
  802. on this port */
  803. spin_lock_irqsave(&card->card_lock, flags);
  804. card->port_status &= ~(1 << ip->channel);
  805. outw(card->port_status, card->base + 0x02);
  806. isicom_shutdown_port(ip);
  807. spin_unlock_irqrestore(&card->card_lock, flags);
  808. tty_port_free_xmit_buf(port);
  809. }
  810. static void isicom_close(struct tty_struct *tty, struct file *filp)
  811. {
  812. struct isi_port *ip = tty->driver_data;
  813. struct tty_port *port = &ip->port;
  814. if (isicom_paranoia_check(ip, tty->name, "isicom_close"))
  815. return;
  816. tty_port_close(port, tty, filp);
  817. }
  818. /* write et all */
  819. static int isicom_write(struct tty_struct *tty, const unsigned char *buf,
  820. int count)
  821. {
  822. struct isi_port *port = tty->driver_data;
  823. struct isi_board *card = port->card;
  824. unsigned long flags;
  825. int cnt, total = 0;
  826. if (isicom_paranoia_check(port, tty->name, "isicom_write"))
  827. return 0;
  828. spin_lock_irqsave(&card->card_lock, flags);
  829. while (1) {
  830. cnt = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt
  831. - 1, SERIAL_XMIT_SIZE - port->xmit_head));
  832. if (cnt <= 0)
  833. break;
  834. memcpy(port->port.xmit_buf + port->xmit_head, buf, cnt);
  835. port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE
  836. - 1);
  837. port->xmit_cnt += cnt;
  838. buf += cnt;
  839. count -= cnt;
  840. total += cnt;
  841. }
  842. if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
  843. port->status |= ISI_TXOK;
  844. spin_unlock_irqrestore(&card->card_lock, flags);
  845. return total;
  846. }
  847. /* put_char et all */
  848. static int isicom_put_char(struct tty_struct *tty, unsigned char ch)
  849. {
  850. struct isi_port *port = tty->driver_data;
  851. struct isi_board *card = port->card;
  852. unsigned long flags;
  853. if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
  854. return 0;
  855. spin_lock_irqsave(&card->card_lock, flags);
  856. if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
  857. spin_unlock_irqrestore(&card->card_lock, flags);
  858. return 0;
  859. }
  860. port->port.xmit_buf[port->xmit_head++] = ch;
  861. port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
  862. port->xmit_cnt++;
  863. spin_unlock_irqrestore(&card->card_lock, flags);
  864. return 1;
  865. }
  866. /* flush_chars et all */
  867. static void isicom_flush_chars(struct tty_struct *tty)
  868. {
  869. struct isi_port *port = tty->driver_data;
  870. if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
  871. return;
  872. if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  873. !port->port.xmit_buf)
  874. return;
  875. /* this tells the transmitter to consider this port for
  876. data output to the card ... that's the best we can do. */
  877. port->status |= ISI_TXOK;
  878. }
  879. /* write_room et all */
  880. static int isicom_write_room(struct tty_struct *tty)
  881. {
  882. struct isi_port *port = tty->driver_data;
  883. int free;
  884. if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
  885. return 0;
  886. free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
  887. if (free < 0)
  888. free = 0;
  889. return free;
  890. }
  891. /* chars_in_buffer et all */
  892. static int isicom_chars_in_buffer(struct tty_struct *tty)
  893. {
  894. struct isi_port *port = tty->driver_data;
  895. if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
  896. return 0;
  897. return port->xmit_cnt;
  898. }
  899. /* ioctl et all */
  900. static int isicom_send_break(struct tty_struct *tty, int length)
  901. {
  902. struct isi_port *port = tty->driver_data;
  903. struct isi_board *card = port->card;
  904. unsigned long base = card->base;
  905. if (length == -1)
  906. return -EOPNOTSUPP;
  907. if (!lock_card(card))
  908. return -EINVAL;
  909. outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
  910. outw((length & 0xff) << 8 | 0x00, base);
  911. outw((length & 0xff00), base);
  912. InterruptTheCard(base);
  913. unlock_card(card);
  914. return 0;
  915. }
  916. static int isicom_tiocmget(struct tty_struct *tty, struct file *file)
  917. {
  918. struct isi_port *port = tty->driver_data;
  919. /* just send the port status */
  920. u16 status = port->status;
  921. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  922. return -ENODEV;
  923. return ((status & ISI_RTS) ? TIOCM_RTS : 0) |
  924. ((status & ISI_DTR) ? TIOCM_DTR : 0) |
  925. ((status & ISI_DCD) ? TIOCM_CAR : 0) |
  926. ((status & ISI_DSR) ? TIOCM_DSR : 0) |
  927. ((status & ISI_CTS) ? TIOCM_CTS : 0) |
  928. ((status & ISI_RI ) ? TIOCM_RI : 0);
  929. }
  930. static int isicom_tiocmset(struct tty_struct *tty, struct file *file,
  931. unsigned int set, unsigned int clear)
  932. {
  933. struct isi_port *port = tty->driver_data;
  934. unsigned long flags;
  935. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  936. return -ENODEV;
  937. spin_lock_irqsave(&port->card->card_lock, flags);
  938. if (set & TIOCM_RTS)
  939. raise_rts(port);
  940. if (set & TIOCM_DTR)
  941. raise_dtr(port);
  942. if (clear & TIOCM_RTS)
  943. drop_rts(port);
  944. if (clear & TIOCM_DTR)
  945. drop_dtr(port);
  946. spin_unlock_irqrestore(&port->card->card_lock, flags);
  947. return 0;
  948. }
  949. static int isicom_set_serial_info(struct tty_struct *tty,
  950. struct serial_struct __user *info)
  951. {
  952. struct isi_port *port = tty->driver_data;
  953. struct serial_struct newinfo;
  954. int reconfig_port;
  955. if (copy_from_user(&newinfo, info, sizeof(newinfo)))
  956. return -EFAULT;
  957. lock_kernel();
  958. reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) !=
  959. (newinfo.flags & ASYNC_SPD_MASK));
  960. if (!capable(CAP_SYS_ADMIN)) {
  961. if ((newinfo.close_delay != port->port.close_delay) ||
  962. (newinfo.closing_wait != port->port.closing_wait) ||
  963. ((newinfo.flags & ~ASYNC_USR_MASK) !=
  964. (port->port.flags & ~ASYNC_USR_MASK))) {
  965. unlock_kernel();
  966. return -EPERM;
  967. }
  968. port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
  969. (newinfo.flags & ASYNC_USR_MASK));
  970. } else {
  971. port->port.close_delay = newinfo.close_delay;
  972. port->port.closing_wait = newinfo.closing_wait;
  973. port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
  974. (newinfo.flags & ASYNC_FLAGS));
  975. }
  976. if (reconfig_port) {
  977. unsigned long flags;
  978. spin_lock_irqsave(&port->card->card_lock, flags);
  979. isicom_config_port(tty);
  980. spin_unlock_irqrestore(&port->card->card_lock, flags);
  981. }
  982. unlock_kernel();
  983. return 0;
  984. }
  985. static int isicom_get_serial_info(struct isi_port *port,
  986. struct serial_struct __user *info)
  987. {
  988. struct serial_struct out_info;
  989. lock_kernel();
  990. memset(&out_info, 0, sizeof(out_info));
  991. /* out_info.type = ? */
  992. out_info.line = port - isi_ports;
  993. out_info.port = port->card->base;
  994. out_info.irq = port->card->irq;
  995. out_info.flags = port->port.flags;
  996. /* out_info.baud_base = ? */
  997. out_info.close_delay = port->port.close_delay;
  998. out_info.closing_wait = port->port.closing_wait;
  999. unlock_kernel();
  1000. if (copy_to_user(info, &out_info, sizeof(out_info)))
  1001. return -EFAULT;
  1002. return 0;
  1003. }
  1004. static int isicom_ioctl(struct tty_struct *tty, struct file *filp,
  1005. unsigned int cmd, unsigned long arg)
  1006. {
  1007. struct isi_port *port = tty->driver_data;
  1008. void __user *argp = (void __user *)arg;
  1009. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  1010. return -ENODEV;
  1011. switch (cmd) {
  1012. case TIOCGSERIAL:
  1013. return isicom_get_serial_info(port, argp);
  1014. case TIOCSSERIAL:
  1015. return isicom_set_serial_info(tty, argp);
  1016. default:
  1017. return -ENOIOCTLCMD;
  1018. }
  1019. return 0;
  1020. }
  1021. /* set_termios et all */
  1022. static void isicom_set_termios(struct tty_struct *tty,
  1023. struct ktermios *old_termios)
  1024. {
  1025. struct isi_port *port = tty->driver_data;
  1026. unsigned long flags;
  1027. if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
  1028. return;
  1029. if (tty->termios->c_cflag == old_termios->c_cflag &&
  1030. tty->termios->c_iflag == old_termios->c_iflag)
  1031. return;
  1032. spin_lock_irqsave(&port->card->card_lock, flags);
  1033. isicom_config_port(tty);
  1034. spin_unlock_irqrestore(&port->card->card_lock, flags);
  1035. if ((old_termios->c_cflag & CRTSCTS) &&
  1036. !(tty->termios->c_cflag & CRTSCTS)) {
  1037. tty->hw_stopped = 0;
  1038. isicom_start(tty);
  1039. }
  1040. }
  1041. /* throttle et all */
  1042. static void isicom_throttle(struct tty_struct *tty)
  1043. {
  1044. struct isi_port *port = tty->driver_data;
  1045. struct isi_board *card = port->card;
  1046. if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
  1047. return;
  1048. /* tell the card that this port cannot handle any more data for now */
  1049. card->port_status &= ~(1 << port->channel);
  1050. outw(card->port_status, card->base + 0x02);
  1051. }
  1052. /* unthrottle et all */
  1053. static void isicom_unthrottle(struct tty_struct *tty)
  1054. {
  1055. struct isi_port *port = tty->driver_data;
  1056. struct isi_board *card = port->card;
  1057. if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
  1058. return;
  1059. /* tell the card that this port is ready to accept more data */
  1060. card->port_status |= (1 << port->channel);
  1061. outw(card->port_status, card->base + 0x02);
  1062. }
  1063. /* stop et all */
  1064. static void isicom_stop(struct tty_struct *tty)
  1065. {
  1066. struct isi_port *port = tty->driver_data;
  1067. if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
  1068. return;
  1069. /* this tells the transmitter not to consider this port for
  1070. data output to the card. */
  1071. port->status &= ~ISI_TXOK;
  1072. }
  1073. /* start et all */
  1074. static void isicom_start(struct tty_struct *tty)
  1075. {
  1076. struct isi_port *port = tty->driver_data;
  1077. if (isicom_paranoia_check(port, tty->name, "isicom_start"))
  1078. return;
  1079. /* this tells the transmitter to consider this port for
  1080. data output to the card. */
  1081. port->status |= ISI_TXOK;
  1082. }
  1083. static void isicom_hangup(struct tty_struct *tty)
  1084. {
  1085. struct isi_port *port = tty->driver_data;
  1086. if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
  1087. return;
  1088. tty_port_hangup(&port->port);
  1089. }
  1090. /*
  1091. * Driver init and deinit functions
  1092. */
  1093. static const struct tty_operations isicom_ops = {
  1094. .open = isicom_open,
  1095. .close = isicom_close,
  1096. .write = isicom_write,
  1097. .put_char = isicom_put_char,
  1098. .flush_chars = isicom_flush_chars,
  1099. .write_room = isicom_write_room,
  1100. .chars_in_buffer = isicom_chars_in_buffer,
  1101. .ioctl = isicom_ioctl,
  1102. .set_termios = isicom_set_termios,
  1103. .throttle = isicom_throttle,
  1104. .unthrottle = isicom_unthrottle,
  1105. .stop = isicom_stop,
  1106. .start = isicom_start,
  1107. .hangup = isicom_hangup,
  1108. .flush_buffer = isicom_flush_buffer,
  1109. .tiocmget = isicom_tiocmget,
  1110. .tiocmset = isicom_tiocmset,
  1111. .break_ctl = isicom_send_break,
  1112. };
  1113. static const struct tty_port_operations isicom_port_ops = {
  1114. .carrier_raised = isicom_carrier_raised,
  1115. .dtr_rts = isicom_dtr_rts,
  1116. .activate = isicom_activate,
  1117. .shutdown = isicom_shutdown,
  1118. };
  1119. static int __devinit reset_card(struct pci_dev *pdev,
  1120. const unsigned int card, unsigned int *signature)
  1121. {
  1122. struct isi_board *board = pci_get_drvdata(pdev);
  1123. unsigned long base = board->base;
  1124. unsigned int sig, portcount = 0;
  1125. int retval = 0;
  1126. dev_dbg(&pdev->dev, "ISILoad:Resetting Card%d at 0x%lx\n", card + 1,
  1127. base);
  1128. inw(base + 0x8);
  1129. msleep(10);
  1130. outw(0, base + 0x8); /* Reset */
  1131. msleep(1000);
  1132. sig = inw(base + 0x4) & 0xff;
  1133. if (sig != 0xa5 && sig != 0xbb && sig != 0xcc && sig != 0xdd &&
  1134. sig != 0xee) {
  1135. dev_warn(&pdev->dev, "ISILoad:Card%u reset failure (Possible "
  1136. "bad I/O Port Address 0x%lx).\n", card + 1, base);
  1137. dev_dbg(&pdev->dev, "Sig=0x%x\n", sig);
  1138. retval = -EIO;
  1139. goto end;
  1140. }
  1141. msleep(10);
  1142. portcount = inw(base + 0x2);
  1143. if (!(inw(base + 0xe) & 0x1) || (portcount != 0 && portcount != 4 &&
  1144. portcount != 8 && portcount != 16)) {
  1145. dev_err(&pdev->dev, "ISILoad:PCI Card%d reset failure.\n",
  1146. card + 1);
  1147. retval = -EIO;
  1148. goto end;
  1149. }
  1150. switch (sig) {
  1151. case 0xa5:
  1152. case 0xbb:
  1153. case 0xdd:
  1154. board->port_count = (portcount == 4) ? 4 : 8;
  1155. board->shift_count = 12;
  1156. break;
  1157. case 0xcc:
  1158. case 0xee:
  1159. board->port_count = 16;
  1160. board->shift_count = 11;
  1161. break;
  1162. }
  1163. dev_info(&pdev->dev, "-Done\n");
  1164. *signature = sig;
  1165. end:
  1166. return retval;
  1167. }
  1168. static int __devinit load_firmware(struct pci_dev *pdev,
  1169. const unsigned int index, const unsigned int signature)
  1170. {
  1171. struct isi_board *board = pci_get_drvdata(pdev);
  1172. const struct firmware *fw;
  1173. unsigned long base = board->base;
  1174. unsigned int a;
  1175. u16 word_count, status;
  1176. int retval = -EIO;
  1177. char *name;
  1178. u8 *data;
  1179. struct stframe {
  1180. u16 addr;
  1181. u16 count;
  1182. u8 data[0];
  1183. } *frame;
  1184. switch (signature) {
  1185. case 0xa5:
  1186. name = "isi608.bin";
  1187. break;
  1188. case 0xbb:
  1189. name = "isi608em.bin";
  1190. break;
  1191. case 0xcc:
  1192. name = "isi616em.bin";
  1193. break;
  1194. case 0xdd:
  1195. name = "isi4608.bin";
  1196. break;
  1197. case 0xee:
  1198. name = "isi4616.bin";
  1199. break;
  1200. default:
  1201. dev_err(&pdev->dev, "Unknown signature.\n");
  1202. goto end;
  1203. }
  1204. retval = request_firmware(&fw, name, &pdev->dev);
  1205. if (retval)
  1206. goto end;
  1207. retval = -EIO;
  1208. for (frame = (struct stframe *)fw->data;
  1209. frame < (struct stframe *)(fw->data + fw->size);
  1210. frame = (struct stframe *)((u8 *)(frame + 1) +
  1211. frame->count)) {
  1212. if (WaitTillCardIsFree(base))
  1213. goto errrelfw;
  1214. outw(0xf0, base); /* start upload sequence */
  1215. outw(0x00, base);
  1216. outw(frame->addr, base); /* lsb of address */
  1217. word_count = frame->count / 2 + frame->count % 2;
  1218. outw(word_count, base);
  1219. InterruptTheCard(base);
  1220. udelay(100); /* 0x2f */
  1221. if (WaitTillCardIsFree(base))
  1222. goto errrelfw;
  1223. status = inw(base + 0x4);
  1224. if (status != 0) {
  1225. dev_warn(&pdev->dev, "Card%d rejected load header:\n"
  1226. "Address:0x%x\n"
  1227. "Count:0x%x\n"
  1228. "Status:0x%x\n",
  1229. index + 1, frame->addr, frame->count, status);
  1230. goto errrelfw;
  1231. }
  1232. outsw(base, frame->data, word_count);
  1233. InterruptTheCard(base);
  1234. udelay(50); /* 0x0f */
  1235. if (WaitTillCardIsFree(base))
  1236. goto errrelfw;
  1237. status = inw(base + 0x4);
  1238. if (status != 0) {
  1239. dev_err(&pdev->dev, "Card%d got out of sync.Card "
  1240. "Status:0x%x\n", index + 1, status);
  1241. goto errrelfw;
  1242. }
  1243. }
  1244. /* XXX: should we test it by reading it back and comparing with original like
  1245. * in load firmware package? */
  1246. for (frame = (struct stframe *)fw->data;
  1247. frame < (struct stframe *)(fw->data + fw->size);
  1248. frame = (struct stframe *)((u8 *)(frame + 1) +
  1249. frame->count)) {
  1250. if (WaitTillCardIsFree(base))
  1251. goto errrelfw;
  1252. outw(0xf1, base); /* start download sequence */
  1253. outw(0x00, base);
  1254. outw(frame->addr, base); /* lsb of address */
  1255. word_count = (frame->count >> 1) + frame->count % 2;
  1256. outw(word_count + 1, base);
  1257. InterruptTheCard(base);
  1258. udelay(50); /* 0xf */
  1259. if (WaitTillCardIsFree(base))
  1260. goto errrelfw;
  1261. status = inw(base + 0x4);
  1262. if (status != 0) {
  1263. dev_warn(&pdev->dev, "Card%d rejected verify header:\n"
  1264. "Address:0x%x\n"
  1265. "Count:0x%x\n"
  1266. "Status: 0x%x\n",
  1267. index + 1, frame->addr, frame->count, status);
  1268. goto errrelfw;
  1269. }
  1270. data = kmalloc(word_count * 2, GFP_KERNEL);
  1271. if (data == NULL) {
  1272. dev_err(&pdev->dev, "Card%d, firmware upload "
  1273. "failed, not enough memory\n", index + 1);
  1274. goto errrelfw;
  1275. }
  1276. inw(base);
  1277. insw(base, data, word_count);
  1278. InterruptTheCard(base);
  1279. for (a = 0; a < frame->count; a++)
  1280. if (data[a] != frame->data[a]) {
  1281. kfree(data);
  1282. dev_err(&pdev->dev, "Card%d, firmware upload "
  1283. "failed\n", index + 1);
  1284. goto errrelfw;
  1285. }
  1286. kfree(data);
  1287. udelay(50); /* 0xf */
  1288. if (WaitTillCardIsFree(base))
  1289. goto errrelfw;
  1290. status = inw(base + 0x4);
  1291. if (status != 0) {
  1292. dev_err(&pdev->dev, "Card%d verify got out of sync. "
  1293. "Card Status:0x%x\n", index + 1, status);
  1294. goto errrelfw;
  1295. }
  1296. }
  1297. /* xfer ctrl */
  1298. if (WaitTillCardIsFree(base))
  1299. goto errrelfw;
  1300. outw(0xf2, base);
  1301. outw(0x800, base);
  1302. outw(0x0, base);
  1303. outw(0x0, base);
  1304. InterruptTheCard(base);
  1305. outw(0x0, base + 0x4); /* for ISI4608 cards */
  1306. board->status |= FIRMWARE_LOADED;
  1307. retval = 0;
  1308. errrelfw:
  1309. release_firmware(fw);
  1310. end:
  1311. return retval;
  1312. }
  1313. /*
  1314. * Insmod can set static symbols so keep these static
  1315. */
  1316. static unsigned int card_count;
  1317. static int __devinit isicom_probe(struct pci_dev *pdev,
  1318. const struct pci_device_id *ent)
  1319. {
  1320. unsigned int uninitialized_var(signature), index;
  1321. int retval = -EPERM;
  1322. struct isi_board *board = NULL;
  1323. if (card_count >= BOARD_COUNT)
  1324. goto err;
  1325. retval = pci_enable_device(pdev);
  1326. if (retval) {
  1327. dev_err(&pdev->dev, "failed to enable\n");
  1328. goto err;
  1329. }
  1330. dev_info(&pdev->dev, "ISI PCI Card(Device ID 0x%x)\n", ent->device);
  1331. /* allot the first empty slot in the array */
  1332. for (index = 0; index < BOARD_COUNT; index++)
  1333. if (isi_card[index].base == 0) {
  1334. board = &isi_card[index];
  1335. break;
  1336. }
  1337. board->index = index;
  1338. board->base = pci_resource_start(pdev, 3);
  1339. board->irq = pdev->irq;
  1340. card_count++;
  1341. pci_set_drvdata(pdev, board);
  1342. retval = pci_request_region(pdev, 3, ISICOM_NAME);
  1343. if (retval) {
  1344. dev_err(&pdev->dev, "I/O Region 0x%lx-0x%lx is busy. Card%d "
  1345. "will be disabled.\n", board->base, board->base + 15,
  1346. index + 1);
  1347. retval = -EBUSY;
  1348. goto errdec;
  1349. }
  1350. retval = request_irq(board->irq, isicom_interrupt,
  1351. IRQF_SHARED | IRQF_DISABLED, ISICOM_NAME, board);
  1352. if (retval < 0) {
  1353. dev_err(&pdev->dev, "Could not install handler at Irq %d. "
  1354. "Card%d will be disabled.\n", board->irq, index + 1);
  1355. goto errunrr;
  1356. }
  1357. retval = reset_card(pdev, index, &signature);
  1358. if (retval < 0)
  1359. goto errunri;
  1360. retval = load_firmware(pdev, index, signature);
  1361. if (retval < 0)
  1362. goto errunri;
  1363. for (index = 0; index < board->port_count; index++)
  1364. tty_register_device(isicom_normal, board->index * 16 + index,
  1365. &pdev->dev);
  1366. return 0;
  1367. errunri:
  1368. free_irq(board->irq, board);
  1369. errunrr:
  1370. pci_release_region(pdev, 3);
  1371. errdec:
  1372. board->base = 0;
  1373. card_count--;
  1374. pci_disable_device(pdev);
  1375. err:
  1376. return retval;
  1377. }
  1378. static void __devexit isicom_remove(struct pci_dev *pdev)
  1379. {
  1380. struct isi_board *board = pci_get_drvdata(pdev);
  1381. unsigned int i;
  1382. for (i = 0; i < board->port_count; i++)
  1383. tty_unregister_device(isicom_normal, board->index * 16 + i);
  1384. free_irq(board->irq, board);
  1385. pci_release_region(pdev, 3);
  1386. board->base = 0;
  1387. card_count--;
  1388. pci_disable_device(pdev);
  1389. }
  1390. static int __init isicom_init(void)
  1391. {
  1392. int retval, idx, channel;
  1393. struct isi_port *port;
  1394. for (idx = 0; idx < BOARD_COUNT; idx++) {
  1395. port = &isi_ports[idx * 16];
  1396. isi_card[idx].ports = port;
  1397. spin_lock_init(&isi_card[idx].card_lock);
  1398. for (channel = 0; channel < 16; channel++, port++) {
  1399. tty_port_init(&port->port);
  1400. port->port.ops = &isicom_port_ops;
  1401. port->magic = ISICOM_MAGIC;
  1402. port->card = &isi_card[idx];
  1403. port->channel = channel;
  1404. port->port.close_delay = 50 * HZ/100;
  1405. port->port.closing_wait = 3000 * HZ/100;
  1406. port->status = 0;
  1407. /* . . . */
  1408. }
  1409. isi_card[idx].base = 0;
  1410. isi_card[idx].irq = 0;
  1411. }
  1412. /* tty driver structure initialization */
  1413. isicom_normal = alloc_tty_driver(PORT_COUNT);
  1414. if (!isicom_normal) {
  1415. retval = -ENOMEM;
  1416. goto error;
  1417. }
  1418. isicom_normal->owner = THIS_MODULE;
  1419. isicom_normal->name = "ttyM";
  1420. isicom_normal->major = ISICOM_NMAJOR;
  1421. isicom_normal->minor_start = 0;
  1422. isicom_normal->type = TTY_DRIVER_TYPE_SERIAL;
  1423. isicom_normal->subtype = SERIAL_TYPE_NORMAL;
  1424. isicom_normal->init_termios = tty_std_termios;
  1425. isicom_normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
  1426. CLOCAL;
  1427. isicom_normal->flags = TTY_DRIVER_REAL_RAW |
  1428. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK;
  1429. tty_set_operations(isicom_normal, &isicom_ops);
  1430. retval = tty_register_driver(isicom_normal);
  1431. if (retval) {
  1432. pr_dbg("Couldn't register the dialin driver\n");
  1433. goto err_puttty;
  1434. }
  1435. retval = pci_register_driver(&isicom_driver);
  1436. if (retval < 0) {
  1437. printk(KERN_ERR "ISICOM: Unable to register pci driver.\n");
  1438. goto err_unrtty;
  1439. }
  1440. mod_timer(&tx, jiffies + 1);
  1441. return 0;
  1442. err_unrtty:
  1443. tty_unregister_driver(isicom_normal);
  1444. err_puttty:
  1445. put_tty_driver(isicom_normal);
  1446. error:
  1447. return retval;
  1448. }
  1449. static void __exit isicom_exit(void)
  1450. {
  1451. del_timer_sync(&tx);
  1452. pci_unregister_driver(&isicom_driver);
  1453. tty_unregister_driver(isicom_normal);
  1454. put_tty_driver(isicom_normal);
  1455. }
  1456. module_init(isicom_init);
  1457. module_exit(isicom_exit);
  1458. MODULE_AUTHOR("MultiTech");
  1459. MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
  1460. MODULE_LICENSE("GPL");