isicom.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  116. #include <linux/module.h>
  117. #include <linux/firmware.h>
  118. #include <linux/kernel.h>
  119. #include <linux/tty.h>
  120. #include <linux/tty_flip.h>
  121. #include <linux/termios.h>
  122. #include <linux/fs.h>
  123. #include <linux/sched.h>
  124. #include <linux/serial.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/slab.h>
  131. #include <linux/uaccess.h>
  132. #include <linux/io.h>
  133. #include <asm/system.h>
  134. #include <linux/pci.h>
  135. #include <linux/isicom.h>
  136. #define InterruptTheCard(base) outw(0, (base) + 0xc)
  137. #define ClearInterrupt(base) inw((base) + 0x0a)
  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. pr_warning("Failed to lock Card (0x%lx)\n", card->base);
  230. return 0; /* Failed to acquire the card! */
  231. }
  232. static void unlock_card(struct isi_board *card)
  233. {
  234. spin_unlock_irqrestore(&card->card_lock, card->flags);
  235. }
  236. /*
  237. * ISI Card specific ops ...
  238. */
  239. /* card->lock HAS to be held */
  240. static void raise_dtr(struct isi_port *port)
  241. {
  242. struct isi_board *card = port->card;
  243. unsigned long base = card->base;
  244. u16 channel = port->channel;
  245. if (WaitTillCardIsFree(base))
  246. return;
  247. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  248. outw(0x0504, base);
  249. InterruptTheCard(base);
  250. port->status |= ISI_DTR;
  251. }
  252. /* card->lock HAS to be held */
  253. static inline void drop_dtr(struct isi_port *port)
  254. {
  255. struct isi_board *card = port->card;
  256. unsigned long base = card->base;
  257. u16 channel = port->channel;
  258. if (WaitTillCardIsFree(base))
  259. return;
  260. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  261. outw(0x0404, base);
  262. InterruptTheCard(base);
  263. port->status &= ~ISI_DTR;
  264. }
  265. /* card->lock HAS to be held */
  266. static inline void raise_rts(struct isi_port *port)
  267. {
  268. struct isi_board *card = port->card;
  269. unsigned long base = card->base;
  270. u16 channel = port->channel;
  271. if (WaitTillCardIsFree(base))
  272. return;
  273. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  274. outw(0x0a04, base);
  275. InterruptTheCard(base);
  276. port->status |= ISI_RTS;
  277. }
  278. /* card->lock HAS to be held */
  279. static inline void drop_rts(struct isi_port *port)
  280. {
  281. struct isi_board *card = port->card;
  282. unsigned long base = card->base;
  283. u16 channel = port->channel;
  284. if (WaitTillCardIsFree(base))
  285. return;
  286. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  287. outw(0x0804, base);
  288. InterruptTheCard(base);
  289. port->status &= ~ISI_RTS;
  290. }
  291. /* card->lock MUST NOT be held */
  292. static void isicom_dtr_rts(struct tty_port *port, int on)
  293. {
  294. struct isi_port *ip = container_of(port, struct isi_port, port);
  295. struct isi_board *card = ip->card;
  296. unsigned long base = card->base;
  297. u16 channel = ip->channel;
  298. if (!lock_card(card))
  299. return;
  300. if (on) {
  301. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  302. outw(0x0f04, base);
  303. InterruptTheCard(base);
  304. ip->status |= (ISI_DTR | ISI_RTS);
  305. } else {
  306. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  307. outw(0x0C04, base);
  308. InterruptTheCard(base);
  309. ip->status &= ~(ISI_DTR | ISI_RTS);
  310. }
  311. unlock_card(card);
  312. }
  313. /* card->lock HAS to be held */
  314. static void drop_dtr_rts(struct isi_port *port)
  315. {
  316. struct isi_board *card = port->card;
  317. unsigned long base = card->base;
  318. u16 channel = port->channel;
  319. if (WaitTillCardIsFree(base))
  320. return;
  321. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  322. outw(0x0c04, base);
  323. InterruptTheCard(base);
  324. port->status &= ~(ISI_RTS | ISI_DTR);
  325. }
  326. /*
  327. * ISICOM Driver specific routines ...
  328. *
  329. */
  330. static inline int __isicom_paranoia_check(struct isi_port const *port,
  331. char *name, const char *routine)
  332. {
  333. if (!port) {
  334. pr_warning("Warning: bad isicom magic for dev %s in %s.\n",
  335. name, routine);
  336. return 1;
  337. }
  338. if (port->magic != ISICOM_MAGIC) {
  339. pr_warning("Warning: NULL isicom port for dev %s in %s.\n",
  340. name, routine);
  341. return 1;
  342. }
  343. return 0;
  344. }
  345. /*
  346. * Transmitter.
  347. *
  348. * We shovel data into the card buffers on a regular basis. The card
  349. * will do the rest of the work for us.
  350. */
  351. static void isicom_tx(unsigned long _data)
  352. {
  353. unsigned long flags, base;
  354. unsigned int retries;
  355. short count = (BOARD_COUNT-1), card;
  356. short txcount, wrd, residue, word_count, cnt;
  357. struct isi_port *port;
  358. struct tty_struct *tty;
  359. /* find next active board */
  360. card = (prev_card + 1) & 0x0003;
  361. while (count-- > 0) {
  362. if (isi_card[card].status & BOARD_ACTIVE)
  363. break;
  364. card = (card + 1) & 0x0003;
  365. }
  366. if (!(isi_card[card].status & BOARD_ACTIVE))
  367. goto sched_again;
  368. prev_card = card;
  369. count = isi_card[card].port_count;
  370. port = isi_card[card].ports;
  371. base = isi_card[card].base;
  372. spin_lock_irqsave(&isi_card[card].card_lock, flags);
  373. for (retries = 0; retries < 100; retries++) {
  374. if (inw(base + 0xe) & 0x1)
  375. break;
  376. udelay(2);
  377. }
  378. if (retries >= 100)
  379. goto unlock;
  380. tty = tty_port_tty_get(&port->port);
  381. if (tty == NULL)
  382. goto put_unlock;
  383. for (; count > 0; count--, port++) {
  384. /* port not active or tx disabled to force flow control */
  385. if (!(port->port.flags & ASYNC_INITIALIZED) ||
  386. !(port->status & ISI_TXOK))
  387. continue;
  388. txcount = min_t(short, TX_SIZE, port->xmit_cnt);
  389. if (txcount <= 0 || tty->stopped || tty->hw_stopped)
  390. continue;
  391. if (!(inw(base + 0x02) & (1 << port->channel)))
  392. continue;
  393. pr_debug("txing %d bytes, port%d.\n",
  394. txcount, port->channel + 1);
  395. outw((port->channel << isi_card[card].shift_count) | txcount,
  396. base);
  397. residue = NO;
  398. wrd = 0;
  399. while (1) {
  400. cnt = min_t(int, txcount, (SERIAL_XMIT_SIZE
  401. - port->xmit_tail));
  402. if (residue == YES) {
  403. residue = NO;
  404. if (cnt > 0) {
  405. wrd |= (port->port.xmit_buf[port->xmit_tail]
  406. << 8);
  407. port->xmit_tail = (port->xmit_tail + 1)
  408. & (SERIAL_XMIT_SIZE - 1);
  409. port->xmit_cnt--;
  410. txcount--;
  411. cnt--;
  412. outw(wrd, base);
  413. } else {
  414. outw(wrd, base);
  415. break;
  416. }
  417. }
  418. if (cnt <= 0)
  419. break;
  420. word_count = cnt >> 1;
  421. outsw(base, port->port.xmit_buf+port->xmit_tail, word_count);
  422. port->xmit_tail = (port->xmit_tail
  423. + (word_count << 1)) & (SERIAL_XMIT_SIZE - 1);
  424. txcount -= (word_count << 1);
  425. port->xmit_cnt -= (word_count << 1);
  426. if (cnt & 0x0001) {
  427. residue = YES;
  428. wrd = port->port.xmit_buf[port->xmit_tail];
  429. port->xmit_tail = (port->xmit_tail + 1)
  430. & (SERIAL_XMIT_SIZE - 1);
  431. port->xmit_cnt--;
  432. txcount--;
  433. }
  434. }
  435. InterruptTheCard(base);
  436. if (port->xmit_cnt <= 0)
  437. port->status &= ~ISI_TXOK;
  438. if (port->xmit_cnt <= WAKEUP_CHARS)
  439. tty_wakeup(tty);
  440. }
  441. put_unlock:
  442. tty_kref_put(tty);
  443. unlock:
  444. spin_unlock_irqrestore(&isi_card[card].card_lock, flags);
  445. /* schedule another tx for hopefully in about 10ms */
  446. sched_again:
  447. mod_timer(&tx, jiffies + msecs_to_jiffies(10));
  448. }
  449. /*
  450. * Main interrupt handler routine
  451. */
  452. static irqreturn_t isicom_interrupt(int irq, void *dev_id)
  453. {
  454. struct isi_board *card = dev_id;
  455. struct isi_port *port;
  456. struct tty_struct *tty;
  457. unsigned long base;
  458. u16 header, word_count, count, channel;
  459. short byte_count;
  460. unsigned char *rp;
  461. if (!card || !(card->status & FIRMWARE_LOADED))
  462. return IRQ_NONE;
  463. base = card->base;
  464. /* did the card interrupt us? */
  465. if (!(inw(base + 0x0e) & 0x02))
  466. return IRQ_NONE;
  467. spin_lock(&card->card_lock);
  468. /*
  469. * disable any interrupts from the PCI card and lower the
  470. * interrupt line
  471. */
  472. outw(0x8000, base+0x04);
  473. ClearInterrupt(base);
  474. inw(base); /* get the dummy word out */
  475. header = inw(base);
  476. channel = (header & 0x7800) >> card->shift_count;
  477. byte_count = header & 0xff;
  478. if (channel + 1 > card->port_count) {
  479. pr_warning("%s(0x%lx): %d(channel) > port_count.\n",
  480. __func__, base, channel+1);
  481. outw(0x0000, base+0x04); /* enable interrupts */
  482. spin_unlock(&card->card_lock);
  483. return IRQ_HANDLED;
  484. }
  485. port = card->ports + channel;
  486. if (!(port->port.flags & ASYNC_INITIALIZED)) {
  487. outw(0x0000, base+0x04); /* enable interrupts */
  488. spin_unlock(&card->card_lock);
  489. return IRQ_HANDLED;
  490. }
  491. tty = tty_port_tty_get(&port->port);
  492. if (tty == NULL) {
  493. word_count = byte_count >> 1;
  494. while (byte_count > 1) {
  495. inw(base);
  496. byte_count -= 2;
  497. }
  498. if (byte_count & 0x01)
  499. inw(base);
  500. outw(0x0000, base+0x04); /* enable interrupts */
  501. spin_unlock(&card->card_lock);
  502. return IRQ_HANDLED;
  503. }
  504. if (header & 0x8000) { /* Status Packet */
  505. header = inw(base);
  506. switch (header & 0xff) {
  507. case 0: /* Change in EIA signals */
  508. if (port->port.flags & ASYNC_CHECK_CD) {
  509. if (port->status & ISI_DCD) {
  510. if (!(header & ISI_DCD)) {
  511. /* Carrier has been lost */
  512. pr_debug("%s: DCD->low.\n",
  513. __func__);
  514. port->status &= ~ISI_DCD;
  515. tty_hangup(tty);
  516. }
  517. } else if (header & ISI_DCD) {
  518. /* Carrier has been detected */
  519. pr_debug("%s: DCD->high.\n",
  520. __func__);
  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_debug("%s: stats!!!\n", __func__);
  567. break;
  568. default:
  569. pr_debug("%s: Unknown code in status packet.\n",
  570. __func__);
  571. break;
  572. }
  573. } else { /* Data Packet */
  574. count = tty_prepare_flip_string(tty, &rp, byte_count & ~1);
  575. pr_debug("%s: Can rx %d of %d bytes.\n",
  576. __func__, count, byte_count);
  577. word_count = count >> 1;
  578. insw(base, rp, word_count);
  579. byte_count -= (word_count << 1);
  580. if (count & 0x0001) {
  581. tty_insert_flip_char(tty, inw(base) & 0xff,
  582. TTY_NORMAL);
  583. byte_count -= 2;
  584. }
  585. if (byte_count > 0) {
  586. pr_debug("%s(0x%lx:%d): Flip buffer overflow! dropping bytes...\n",
  587. __func__, base, channel + 1);
  588. /* drain out unread xtra data */
  589. while (byte_count > 0) {
  590. inw(base);
  591. byte_count -= 2;
  592. }
  593. }
  594. tty_flip_buffer_push(tty);
  595. }
  596. outw(0x0000, base+0x04); /* enable interrupts */
  597. spin_unlock(&card->card_lock);
  598. tty_kref_put(tty);
  599. return IRQ_HANDLED;
  600. }
  601. static void isicom_config_port(struct tty_struct *tty)
  602. {
  603. struct isi_port *port = tty->driver_data;
  604. struct isi_board *card = port->card;
  605. unsigned long baud;
  606. unsigned long base = card->base;
  607. u16 channel_setup, channel = port->channel,
  608. shift_count = card->shift_count;
  609. unsigned char flow_ctrl;
  610. /* FIXME: Switch to new tty baud API */
  611. baud = C_BAUD(tty);
  612. if (baud & CBAUDEX) {
  613. baud &= ~CBAUDEX;
  614. /* if CBAUDEX bit is on and the baud is set to either 50 or 75
  615. * then the card is programmed for 57.6Kbps or 115Kbps
  616. * respectively.
  617. */
  618. /* 1,2,3,4 => 57.6, 115.2, 230, 460 kbps resp. */
  619. if (baud < 1 || baud > 4)
  620. tty->termios->c_cflag &= ~CBAUDEX;
  621. else
  622. baud += 15;
  623. }
  624. if (baud == 15) {
  625. /* the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set
  626. * by the set_serial_info ioctl ... this is done by
  627. * the 'setserial' utility.
  628. */
  629. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  630. baud++; /* 57.6 Kbps */
  631. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  632. baud += 2; /* 115 Kbps */
  633. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  634. baud += 3; /* 230 kbps*/
  635. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  636. baud += 4; /* 460 kbps*/
  637. }
  638. if (linuxb_to_isib[baud] == -1) {
  639. /* hang up */
  640. drop_dtr(port);
  641. return;
  642. } else
  643. raise_dtr(port);
  644. if (WaitTillCardIsFree(base) == 0) {
  645. outw(0x8000 | (channel << shift_count) | 0x03, base);
  646. outw(linuxb_to_isib[baud] << 8 | 0x03, base);
  647. channel_setup = 0;
  648. switch (C_CSIZE(tty)) {
  649. case CS5:
  650. channel_setup |= ISICOM_CS5;
  651. break;
  652. case CS6:
  653. channel_setup |= ISICOM_CS6;
  654. break;
  655. case CS7:
  656. channel_setup |= ISICOM_CS7;
  657. break;
  658. case CS8:
  659. channel_setup |= ISICOM_CS8;
  660. break;
  661. }
  662. if (C_CSTOPB(tty))
  663. channel_setup |= ISICOM_2SB;
  664. if (C_PARENB(tty)) {
  665. channel_setup |= ISICOM_EVPAR;
  666. if (C_PARODD(tty))
  667. channel_setup |= ISICOM_ODPAR;
  668. }
  669. outw(channel_setup, base);
  670. InterruptTheCard(base);
  671. }
  672. if (C_CLOCAL(tty))
  673. port->port.flags &= ~ASYNC_CHECK_CD;
  674. else
  675. port->port.flags |= ASYNC_CHECK_CD;
  676. /* flow control settings ...*/
  677. flow_ctrl = 0;
  678. port->port.flags &= ~ASYNC_CTS_FLOW;
  679. if (C_CRTSCTS(tty)) {
  680. port->port.flags |= ASYNC_CTS_FLOW;
  681. flow_ctrl |= ISICOM_CTSRTS;
  682. }
  683. if (I_IXON(tty))
  684. flow_ctrl |= ISICOM_RESPOND_XONXOFF;
  685. if (I_IXOFF(tty))
  686. flow_ctrl |= ISICOM_INITIATE_XONXOFF;
  687. if (WaitTillCardIsFree(base) == 0) {
  688. outw(0x8000 | (channel << shift_count) | 0x04, base);
  689. outw(flow_ctrl << 8 | 0x05, base);
  690. outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
  691. InterruptTheCard(base);
  692. }
  693. /* rx enabled -> enable port for rx on the card */
  694. if (C_CREAD(tty)) {
  695. card->port_status |= (1 << channel);
  696. outw(card->port_status, base + 0x02);
  697. }
  698. }
  699. /* open et all */
  700. static inline void isicom_setup_board(struct isi_board *bp)
  701. {
  702. int channel;
  703. struct isi_port *port;
  704. bp->count++;
  705. if (!(bp->status & BOARD_INIT)) {
  706. port = bp->ports;
  707. for (channel = 0; channel < bp->port_count; channel++, port++)
  708. drop_dtr_rts(port);
  709. }
  710. bp->status |= BOARD_ACTIVE | BOARD_INIT;
  711. }
  712. /* Activate and thus setup board are protected from races against shutdown
  713. by the tty_port mutex */
  714. static int isicom_activate(struct tty_port *tport, struct tty_struct *tty)
  715. {
  716. struct isi_port *port = container_of(tport, struct isi_port, port);
  717. struct isi_board *card = port->card;
  718. unsigned long flags;
  719. if (tty_port_alloc_xmit_buf(tport) < 0)
  720. return -ENOMEM;
  721. spin_lock_irqsave(&card->card_lock, flags);
  722. isicom_setup_board(card);
  723. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  724. /* discard any residual data */
  725. if (WaitTillCardIsFree(card->base) == 0) {
  726. outw(0x8000 | (port->channel << card->shift_count) | 0x02,
  727. card->base);
  728. outw(((ISICOM_KILLTX | ISICOM_KILLRX) << 8) | 0x06, card->base);
  729. InterruptTheCard(card->base);
  730. }
  731. isicom_config_port(tty);
  732. spin_unlock_irqrestore(&card->card_lock, flags);
  733. return 0;
  734. }
  735. static int isicom_carrier_raised(struct tty_port *port)
  736. {
  737. struct isi_port *ip = container_of(port, struct isi_port, port);
  738. return (ip->status & ISI_DCD)?1 : 0;
  739. }
  740. static struct tty_port *isicom_find_port(struct tty_struct *tty)
  741. {
  742. struct isi_port *port;
  743. struct isi_board *card;
  744. unsigned int board;
  745. int line = tty->index;
  746. if (line < 0 || line > PORT_COUNT-1)
  747. return NULL;
  748. board = BOARD(line);
  749. card = &isi_card[board];
  750. if (!(card->status & FIRMWARE_LOADED))
  751. return NULL;
  752. /* open on a port greater than the port count for the card !!! */
  753. if (line > ((board * 16) + card->port_count - 1))
  754. return NULL;
  755. port = &isi_ports[line];
  756. if (isicom_paranoia_check(port, tty->name, "isicom_open"))
  757. return NULL;
  758. return &port->port;
  759. }
  760. static int isicom_open(struct tty_struct *tty, struct file *filp)
  761. {
  762. struct isi_port *port;
  763. struct tty_port *tport;
  764. tport = isicom_find_port(tty);
  765. if (tport == NULL)
  766. return -ENODEV;
  767. port = container_of(tport, struct isi_port, port);
  768. tty->driver_data = port;
  769. return tty_port_open(tport, tty, filp);
  770. }
  771. /* close et all */
  772. /* card->lock HAS to be held */
  773. static void isicom_shutdown_port(struct isi_port *port)
  774. {
  775. struct isi_board *card = port->card;
  776. if (--card->count < 0) {
  777. pr_debug("%s: bad board(0x%lx) count %d.\n",
  778. __func__, card->base, card->count);
  779. card->count = 0;
  780. }
  781. /* last port was closed, shutdown that board too */
  782. if (!card->count)
  783. card->status &= BOARD_ACTIVE;
  784. }
  785. static void isicom_flush_buffer(struct tty_struct *tty)
  786. {
  787. struct isi_port *port = tty->driver_data;
  788. struct isi_board *card = port->card;
  789. unsigned long flags;
  790. if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
  791. return;
  792. spin_lock_irqsave(&card->card_lock, flags);
  793. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  794. spin_unlock_irqrestore(&card->card_lock, flags);
  795. tty_wakeup(tty);
  796. }
  797. static void isicom_shutdown(struct tty_port *port)
  798. {
  799. struct isi_port *ip = container_of(port, struct isi_port, port);
  800. struct isi_board *card = ip->card;
  801. unsigned long flags;
  802. /* indicate to the card that no more data can be received
  803. on this port */
  804. spin_lock_irqsave(&card->card_lock, flags);
  805. card->port_status &= ~(1 << ip->channel);
  806. outw(card->port_status, card->base + 0x02);
  807. isicom_shutdown_port(ip);
  808. spin_unlock_irqrestore(&card->card_lock, flags);
  809. tty_port_free_xmit_buf(port);
  810. }
  811. static void isicom_close(struct tty_struct *tty, struct file *filp)
  812. {
  813. struct isi_port *ip = tty->driver_data;
  814. struct tty_port *port;
  815. if (ip == NULL)
  816. return;
  817. port = &ip->port;
  818. if (isicom_paranoia_check(ip, tty->name, "isicom_close"))
  819. return;
  820. tty_port_close(port, tty, filp);
  821. }
  822. /* write et all */
  823. static int isicom_write(struct tty_struct *tty, const unsigned char *buf,
  824. int count)
  825. {
  826. struct isi_port *port = tty->driver_data;
  827. struct isi_board *card = port->card;
  828. unsigned long flags;
  829. int cnt, total = 0;
  830. if (isicom_paranoia_check(port, tty->name, "isicom_write"))
  831. return 0;
  832. spin_lock_irqsave(&card->card_lock, flags);
  833. while (1) {
  834. cnt = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt
  835. - 1, SERIAL_XMIT_SIZE - port->xmit_head));
  836. if (cnt <= 0)
  837. break;
  838. memcpy(port->port.xmit_buf + port->xmit_head, buf, cnt);
  839. port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE
  840. - 1);
  841. port->xmit_cnt += cnt;
  842. buf += cnt;
  843. count -= cnt;
  844. total += cnt;
  845. }
  846. if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
  847. port->status |= ISI_TXOK;
  848. spin_unlock_irqrestore(&card->card_lock, flags);
  849. return total;
  850. }
  851. /* put_char et all */
  852. static int isicom_put_char(struct tty_struct *tty, unsigned char ch)
  853. {
  854. struct isi_port *port = tty->driver_data;
  855. struct isi_board *card = port->card;
  856. unsigned long flags;
  857. if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
  858. return 0;
  859. spin_lock_irqsave(&card->card_lock, flags);
  860. if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
  861. spin_unlock_irqrestore(&card->card_lock, flags);
  862. return 0;
  863. }
  864. port->port.xmit_buf[port->xmit_head++] = ch;
  865. port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
  866. port->xmit_cnt++;
  867. spin_unlock_irqrestore(&card->card_lock, flags);
  868. return 1;
  869. }
  870. /* flush_chars et all */
  871. static void isicom_flush_chars(struct tty_struct *tty)
  872. {
  873. struct isi_port *port = tty->driver_data;
  874. if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
  875. return;
  876. if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  877. !port->port.xmit_buf)
  878. return;
  879. /* this tells the transmitter to consider this port for
  880. data output to the card ... that's the best we can do. */
  881. port->status |= ISI_TXOK;
  882. }
  883. /* write_room et all */
  884. static int isicom_write_room(struct tty_struct *tty)
  885. {
  886. struct isi_port *port = tty->driver_data;
  887. int free;
  888. if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
  889. return 0;
  890. free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
  891. if (free < 0)
  892. free = 0;
  893. return free;
  894. }
  895. /* chars_in_buffer et all */
  896. static int isicom_chars_in_buffer(struct tty_struct *tty)
  897. {
  898. struct isi_port *port = tty->driver_data;
  899. if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
  900. return 0;
  901. return port->xmit_cnt;
  902. }
  903. /* ioctl et all */
  904. static int isicom_send_break(struct tty_struct *tty, int length)
  905. {
  906. struct isi_port *port = tty->driver_data;
  907. struct isi_board *card = port->card;
  908. unsigned long base = card->base;
  909. if (length == -1)
  910. return -EOPNOTSUPP;
  911. if (!lock_card(card))
  912. return -EINVAL;
  913. outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
  914. outw((length & 0xff) << 8 | 0x00, base);
  915. outw((length & 0xff00), base);
  916. InterruptTheCard(base);
  917. unlock_card(card);
  918. return 0;
  919. }
  920. static int isicom_tiocmget(struct tty_struct *tty)
  921. {
  922. struct isi_port *port = tty->driver_data;
  923. /* just send the port status */
  924. u16 status = port->status;
  925. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  926. return -ENODEV;
  927. return ((status & ISI_RTS) ? TIOCM_RTS : 0) |
  928. ((status & ISI_DTR) ? TIOCM_DTR : 0) |
  929. ((status & ISI_DCD) ? TIOCM_CAR : 0) |
  930. ((status & ISI_DSR) ? TIOCM_DSR : 0) |
  931. ((status & ISI_CTS) ? TIOCM_CTS : 0) |
  932. ((status & ISI_RI ) ? TIOCM_RI : 0);
  933. }
  934. static int isicom_tiocmset(struct tty_struct *tty,
  935. unsigned int set, unsigned int clear)
  936. {
  937. struct isi_port *port = tty->driver_data;
  938. unsigned long flags;
  939. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  940. return -ENODEV;
  941. spin_lock_irqsave(&port->card->card_lock, flags);
  942. if (set & TIOCM_RTS)
  943. raise_rts(port);
  944. if (set & TIOCM_DTR)
  945. raise_dtr(port);
  946. if (clear & TIOCM_RTS)
  947. drop_rts(port);
  948. if (clear & TIOCM_DTR)
  949. drop_dtr(port);
  950. spin_unlock_irqrestore(&port->card->card_lock, flags);
  951. return 0;
  952. }
  953. static int isicom_set_serial_info(struct tty_struct *tty,
  954. struct serial_struct __user *info)
  955. {
  956. struct isi_port *port = tty->driver_data;
  957. struct serial_struct newinfo;
  958. int reconfig_port;
  959. if (copy_from_user(&newinfo, info, sizeof(newinfo)))
  960. return -EFAULT;
  961. mutex_lock(&port->port.mutex);
  962. reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) !=
  963. (newinfo.flags & ASYNC_SPD_MASK));
  964. if (!capable(CAP_SYS_ADMIN)) {
  965. if ((newinfo.close_delay != port->port.close_delay) ||
  966. (newinfo.closing_wait != port->port.closing_wait) ||
  967. ((newinfo.flags & ~ASYNC_USR_MASK) !=
  968. (port->port.flags & ~ASYNC_USR_MASK))) {
  969. mutex_unlock(&port->port.mutex);
  970. return -EPERM;
  971. }
  972. port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
  973. (newinfo.flags & ASYNC_USR_MASK));
  974. } else {
  975. port->port.close_delay = newinfo.close_delay;
  976. port->port.closing_wait = newinfo.closing_wait;
  977. port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
  978. (newinfo.flags & ASYNC_FLAGS));
  979. }
  980. if (reconfig_port) {
  981. unsigned long flags;
  982. spin_lock_irqsave(&port->card->card_lock, flags);
  983. isicom_config_port(tty);
  984. spin_unlock_irqrestore(&port->card->card_lock, flags);
  985. }
  986. mutex_unlock(&port->port.mutex);
  987. return 0;
  988. }
  989. static int isicom_get_serial_info(struct isi_port *port,
  990. struct serial_struct __user *info)
  991. {
  992. struct serial_struct out_info;
  993. mutex_lock(&port->port.mutex);
  994. memset(&out_info, 0, sizeof(out_info));
  995. /* out_info.type = ? */
  996. out_info.line = port - isi_ports;
  997. out_info.port = port->card->base;
  998. out_info.irq = port->card->irq;
  999. out_info.flags = port->port.flags;
  1000. /* out_info.baud_base = ? */
  1001. out_info.close_delay = port->port.close_delay;
  1002. out_info.closing_wait = port->port.closing_wait;
  1003. mutex_unlock(&port->port.mutex);
  1004. if (copy_to_user(info, &out_info, sizeof(out_info)))
  1005. return -EFAULT;
  1006. return 0;
  1007. }
  1008. static int isicom_ioctl(struct tty_struct *tty,
  1009. unsigned int cmd, unsigned long arg)
  1010. {
  1011. struct isi_port *port = tty->driver_data;
  1012. void __user *argp = (void __user *)arg;
  1013. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  1014. return -ENODEV;
  1015. switch (cmd) {
  1016. case TIOCGSERIAL:
  1017. return isicom_get_serial_info(port, argp);
  1018. case TIOCSSERIAL:
  1019. return isicom_set_serial_info(tty, argp);
  1020. default:
  1021. return -ENOIOCTLCMD;
  1022. }
  1023. return 0;
  1024. }
  1025. /* set_termios et all */
  1026. static void isicom_set_termios(struct tty_struct *tty,
  1027. struct ktermios *old_termios)
  1028. {
  1029. struct isi_port *port = tty->driver_data;
  1030. unsigned long flags;
  1031. if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
  1032. return;
  1033. if (tty->termios->c_cflag == old_termios->c_cflag &&
  1034. tty->termios->c_iflag == old_termios->c_iflag)
  1035. return;
  1036. spin_lock_irqsave(&port->card->card_lock, flags);
  1037. isicom_config_port(tty);
  1038. spin_unlock_irqrestore(&port->card->card_lock, flags);
  1039. if ((old_termios->c_cflag & CRTSCTS) &&
  1040. !(tty->termios->c_cflag & CRTSCTS)) {
  1041. tty->hw_stopped = 0;
  1042. isicom_start(tty);
  1043. }
  1044. }
  1045. /* throttle et all */
  1046. static void isicom_throttle(struct tty_struct *tty)
  1047. {
  1048. struct isi_port *port = tty->driver_data;
  1049. struct isi_board *card = port->card;
  1050. if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
  1051. return;
  1052. /* tell the card that this port cannot handle any more data for now */
  1053. card->port_status &= ~(1 << port->channel);
  1054. outw(card->port_status, card->base + 0x02);
  1055. }
  1056. /* unthrottle et all */
  1057. static void isicom_unthrottle(struct tty_struct *tty)
  1058. {
  1059. struct isi_port *port = tty->driver_data;
  1060. struct isi_board *card = port->card;
  1061. if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
  1062. return;
  1063. /* tell the card that this port is ready to accept more data */
  1064. card->port_status |= (1 << port->channel);
  1065. outw(card->port_status, card->base + 0x02);
  1066. }
  1067. /* stop et all */
  1068. static void isicom_stop(struct tty_struct *tty)
  1069. {
  1070. struct isi_port *port = tty->driver_data;
  1071. if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
  1072. return;
  1073. /* this tells the transmitter not to consider this port for
  1074. data output to the card. */
  1075. port->status &= ~ISI_TXOK;
  1076. }
  1077. /* start et all */
  1078. static void isicom_start(struct tty_struct *tty)
  1079. {
  1080. struct isi_port *port = tty->driver_data;
  1081. if (isicom_paranoia_check(port, tty->name, "isicom_start"))
  1082. return;
  1083. /* this tells the transmitter to consider this port for
  1084. data output to the card. */
  1085. port->status |= ISI_TXOK;
  1086. }
  1087. static void isicom_hangup(struct tty_struct *tty)
  1088. {
  1089. struct isi_port *port = tty->driver_data;
  1090. if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
  1091. return;
  1092. tty_port_hangup(&port->port);
  1093. }
  1094. /*
  1095. * Driver init and deinit functions
  1096. */
  1097. static const struct tty_operations isicom_ops = {
  1098. .open = isicom_open,
  1099. .close = isicom_close,
  1100. .write = isicom_write,
  1101. .put_char = isicom_put_char,
  1102. .flush_chars = isicom_flush_chars,
  1103. .write_room = isicom_write_room,
  1104. .chars_in_buffer = isicom_chars_in_buffer,
  1105. .ioctl = isicom_ioctl,
  1106. .set_termios = isicom_set_termios,
  1107. .throttle = isicom_throttle,
  1108. .unthrottle = isicom_unthrottle,
  1109. .stop = isicom_stop,
  1110. .start = isicom_start,
  1111. .hangup = isicom_hangup,
  1112. .flush_buffer = isicom_flush_buffer,
  1113. .tiocmget = isicom_tiocmget,
  1114. .tiocmset = isicom_tiocmset,
  1115. .break_ctl = isicom_send_break,
  1116. };
  1117. static const struct tty_port_operations isicom_port_ops = {
  1118. .carrier_raised = isicom_carrier_raised,
  1119. .dtr_rts = isicom_dtr_rts,
  1120. .activate = isicom_activate,
  1121. .shutdown = isicom_shutdown,
  1122. };
  1123. static int __devinit reset_card(struct pci_dev *pdev,
  1124. const unsigned int card, unsigned int *signature)
  1125. {
  1126. struct isi_board *board = pci_get_drvdata(pdev);
  1127. unsigned long base = board->base;
  1128. unsigned int sig, portcount = 0;
  1129. int retval = 0;
  1130. dev_dbg(&pdev->dev, "ISILoad:Resetting Card%d at 0x%lx\n", card + 1,
  1131. base);
  1132. inw(base + 0x8);
  1133. msleep(10);
  1134. outw(0, base + 0x8); /* Reset */
  1135. msleep(1000);
  1136. sig = inw(base + 0x4) & 0xff;
  1137. if (sig != 0xa5 && sig != 0xbb && sig != 0xcc && sig != 0xdd &&
  1138. sig != 0xee) {
  1139. dev_warn(&pdev->dev, "ISILoad:Card%u reset failure (Possible "
  1140. "bad I/O Port Address 0x%lx).\n", card + 1, base);
  1141. dev_dbg(&pdev->dev, "Sig=0x%x\n", sig);
  1142. retval = -EIO;
  1143. goto end;
  1144. }
  1145. msleep(10);
  1146. portcount = inw(base + 0x2);
  1147. if (!(inw(base + 0xe) & 0x1) || (portcount != 0 && portcount != 4 &&
  1148. portcount != 8 && portcount != 16)) {
  1149. dev_err(&pdev->dev, "ISILoad:PCI Card%d reset failure.\n",
  1150. card + 1);
  1151. retval = -EIO;
  1152. goto end;
  1153. }
  1154. switch (sig) {
  1155. case 0xa5:
  1156. case 0xbb:
  1157. case 0xdd:
  1158. board->port_count = (portcount == 4) ? 4 : 8;
  1159. board->shift_count = 12;
  1160. break;
  1161. case 0xcc:
  1162. case 0xee:
  1163. board->port_count = 16;
  1164. board->shift_count = 11;
  1165. break;
  1166. }
  1167. dev_info(&pdev->dev, "-Done\n");
  1168. *signature = sig;
  1169. end:
  1170. return retval;
  1171. }
  1172. static int __devinit load_firmware(struct pci_dev *pdev,
  1173. const unsigned int index, const unsigned int signature)
  1174. {
  1175. struct isi_board *board = pci_get_drvdata(pdev);
  1176. const struct firmware *fw;
  1177. unsigned long base = board->base;
  1178. unsigned int a;
  1179. u16 word_count, status;
  1180. int retval = -EIO;
  1181. char *name;
  1182. u8 *data;
  1183. struct stframe {
  1184. u16 addr;
  1185. u16 count;
  1186. u8 data[0];
  1187. } *frame;
  1188. switch (signature) {
  1189. case 0xa5:
  1190. name = "isi608.bin";
  1191. break;
  1192. case 0xbb:
  1193. name = "isi608em.bin";
  1194. break;
  1195. case 0xcc:
  1196. name = "isi616em.bin";
  1197. break;
  1198. case 0xdd:
  1199. name = "isi4608.bin";
  1200. break;
  1201. case 0xee:
  1202. name = "isi4616.bin";
  1203. break;
  1204. default:
  1205. dev_err(&pdev->dev, "Unknown signature.\n");
  1206. goto end;
  1207. }
  1208. retval = request_firmware(&fw, name, &pdev->dev);
  1209. if (retval)
  1210. goto end;
  1211. retval = -EIO;
  1212. for (frame = (struct stframe *)fw->data;
  1213. frame < (struct stframe *)(fw->data + fw->size);
  1214. frame = (struct stframe *)((u8 *)(frame + 1) +
  1215. frame->count)) {
  1216. if (WaitTillCardIsFree(base))
  1217. goto errrelfw;
  1218. outw(0xf0, base); /* start upload sequence */
  1219. outw(0x00, base);
  1220. outw(frame->addr, base); /* lsb of address */
  1221. word_count = frame->count / 2 + frame->count % 2;
  1222. outw(word_count, base);
  1223. InterruptTheCard(base);
  1224. udelay(100); /* 0x2f */
  1225. if (WaitTillCardIsFree(base))
  1226. goto errrelfw;
  1227. status = inw(base + 0x4);
  1228. if (status != 0) {
  1229. dev_warn(&pdev->dev, "Card%d rejected load header:\n"
  1230. "Address:0x%x\n"
  1231. "Count:0x%x\n"
  1232. "Status:0x%x\n",
  1233. index + 1, frame->addr, frame->count, status);
  1234. goto errrelfw;
  1235. }
  1236. outsw(base, frame->data, word_count);
  1237. InterruptTheCard(base);
  1238. udelay(50); /* 0x0f */
  1239. if (WaitTillCardIsFree(base))
  1240. goto errrelfw;
  1241. status = inw(base + 0x4);
  1242. if (status != 0) {
  1243. dev_err(&pdev->dev, "Card%d got out of sync.Card "
  1244. "Status:0x%x\n", index + 1, status);
  1245. goto errrelfw;
  1246. }
  1247. }
  1248. /* XXX: should we test it by reading it back and comparing with original like
  1249. * in load firmware package? */
  1250. for (frame = (struct stframe *)fw->data;
  1251. frame < (struct stframe *)(fw->data + fw->size);
  1252. frame = (struct stframe *)((u8 *)(frame + 1) +
  1253. frame->count)) {
  1254. if (WaitTillCardIsFree(base))
  1255. goto errrelfw;
  1256. outw(0xf1, base); /* start download sequence */
  1257. outw(0x00, base);
  1258. outw(frame->addr, base); /* lsb of address */
  1259. word_count = (frame->count >> 1) + frame->count % 2;
  1260. outw(word_count + 1, base);
  1261. InterruptTheCard(base);
  1262. udelay(50); /* 0xf */
  1263. if (WaitTillCardIsFree(base))
  1264. goto errrelfw;
  1265. status = inw(base + 0x4);
  1266. if (status != 0) {
  1267. dev_warn(&pdev->dev, "Card%d rejected verify header:\n"
  1268. "Address:0x%x\n"
  1269. "Count:0x%x\n"
  1270. "Status: 0x%x\n",
  1271. index + 1, frame->addr, frame->count, status);
  1272. goto errrelfw;
  1273. }
  1274. data = kmalloc(word_count * 2, GFP_KERNEL);
  1275. if (data == NULL) {
  1276. dev_err(&pdev->dev, "Card%d, firmware upload "
  1277. "failed, not enough memory\n", index + 1);
  1278. goto errrelfw;
  1279. }
  1280. inw(base);
  1281. insw(base, data, word_count);
  1282. InterruptTheCard(base);
  1283. for (a = 0; a < frame->count; a++)
  1284. if (data[a] != frame->data[a]) {
  1285. kfree(data);
  1286. dev_err(&pdev->dev, "Card%d, firmware upload "
  1287. "failed\n", index + 1);
  1288. goto errrelfw;
  1289. }
  1290. kfree(data);
  1291. udelay(50); /* 0xf */
  1292. if (WaitTillCardIsFree(base))
  1293. goto errrelfw;
  1294. status = inw(base + 0x4);
  1295. if (status != 0) {
  1296. dev_err(&pdev->dev, "Card%d verify got out of sync. "
  1297. "Card Status:0x%x\n", index + 1, status);
  1298. goto errrelfw;
  1299. }
  1300. }
  1301. /* xfer ctrl */
  1302. if (WaitTillCardIsFree(base))
  1303. goto errrelfw;
  1304. outw(0xf2, base);
  1305. outw(0x800, base);
  1306. outw(0x0, base);
  1307. outw(0x0, base);
  1308. InterruptTheCard(base);
  1309. outw(0x0, base + 0x4); /* for ISI4608 cards */
  1310. board->status |= FIRMWARE_LOADED;
  1311. retval = 0;
  1312. errrelfw:
  1313. release_firmware(fw);
  1314. end:
  1315. return retval;
  1316. }
  1317. /*
  1318. * Insmod can set static symbols so keep these static
  1319. */
  1320. static unsigned int card_count;
  1321. static int __devinit isicom_probe(struct pci_dev *pdev,
  1322. const struct pci_device_id *ent)
  1323. {
  1324. unsigned int uninitialized_var(signature), index;
  1325. int retval = -EPERM;
  1326. struct isi_board *board = NULL;
  1327. if (card_count >= BOARD_COUNT)
  1328. goto err;
  1329. retval = pci_enable_device(pdev);
  1330. if (retval) {
  1331. dev_err(&pdev->dev, "failed to enable\n");
  1332. goto err;
  1333. }
  1334. dev_info(&pdev->dev, "ISI PCI Card(Device ID 0x%x)\n", ent->device);
  1335. /* allot the first empty slot in the array */
  1336. for (index = 0; index < BOARD_COUNT; index++) {
  1337. if (isi_card[index].base == 0) {
  1338. board = &isi_card[index];
  1339. break;
  1340. }
  1341. }
  1342. if (index == BOARD_COUNT) {
  1343. retval = -ENODEV;
  1344. goto err_disable;
  1345. }
  1346. board->index = index;
  1347. board->base = pci_resource_start(pdev, 3);
  1348. board->irq = pdev->irq;
  1349. card_count++;
  1350. pci_set_drvdata(pdev, board);
  1351. retval = pci_request_region(pdev, 3, ISICOM_NAME);
  1352. if (retval) {
  1353. dev_err(&pdev->dev, "I/O Region 0x%lx-0x%lx is busy. Card%d "
  1354. "will be disabled.\n", board->base, board->base + 15,
  1355. index + 1);
  1356. retval = -EBUSY;
  1357. goto errdec;
  1358. }
  1359. retval = request_irq(board->irq, isicom_interrupt,
  1360. IRQF_SHARED | IRQF_DISABLED, ISICOM_NAME, board);
  1361. if (retval < 0) {
  1362. dev_err(&pdev->dev, "Could not install handler at Irq %d. "
  1363. "Card%d will be disabled.\n", board->irq, index + 1);
  1364. goto errunrr;
  1365. }
  1366. retval = reset_card(pdev, index, &signature);
  1367. if (retval < 0)
  1368. goto errunri;
  1369. retval = load_firmware(pdev, index, signature);
  1370. if (retval < 0)
  1371. goto errunri;
  1372. for (index = 0; index < board->port_count; index++)
  1373. tty_register_device(isicom_normal, board->index * 16 + index,
  1374. &pdev->dev);
  1375. return 0;
  1376. errunri:
  1377. free_irq(board->irq, board);
  1378. errunrr:
  1379. pci_release_region(pdev, 3);
  1380. errdec:
  1381. board->base = 0;
  1382. card_count--;
  1383. err_disable:
  1384. pci_disable_device(pdev);
  1385. err:
  1386. return retval;
  1387. }
  1388. static void __devexit isicom_remove(struct pci_dev *pdev)
  1389. {
  1390. struct isi_board *board = pci_get_drvdata(pdev);
  1391. unsigned int i;
  1392. for (i = 0; i < board->port_count; i++)
  1393. tty_unregister_device(isicom_normal, board->index * 16 + i);
  1394. free_irq(board->irq, board);
  1395. pci_release_region(pdev, 3);
  1396. board->base = 0;
  1397. card_count--;
  1398. pci_disable_device(pdev);
  1399. }
  1400. static int __init isicom_init(void)
  1401. {
  1402. int retval, idx, channel;
  1403. struct isi_port *port;
  1404. for (idx = 0; idx < BOARD_COUNT; idx++) {
  1405. port = &isi_ports[idx * 16];
  1406. isi_card[idx].ports = port;
  1407. spin_lock_init(&isi_card[idx].card_lock);
  1408. for (channel = 0; channel < 16; channel++, port++) {
  1409. tty_port_init(&port->port);
  1410. port->port.ops = &isicom_port_ops;
  1411. port->magic = ISICOM_MAGIC;
  1412. port->card = &isi_card[idx];
  1413. port->channel = channel;
  1414. port->port.close_delay = 50 * HZ/100;
  1415. port->port.closing_wait = 3000 * HZ/100;
  1416. port->status = 0;
  1417. /* . . . */
  1418. }
  1419. isi_card[idx].base = 0;
  1420. isi_card[idx].irq = 0;
  1421. }
  1422. /* tty driver structure initialization */
  1423. isicom_normal = alloc_tty_driver(PORT_COUNT);
  1424. if (!isicom_normal) {
  1425. retval = -ENOMEM;
  1426. goto error;
  1427. }
  1428. isicom_normal->owner = THIS_MODULE;
  1429. isicom_normal->name = "ttyM";
  1430. isicom_normal->major = ISICOM_NMAJOR;
  1431. isicom_normal->minor_start = 0;
  1432. isicom_normal->type = TTY_DRIVER_TYPE_SERIAL;
  1433. isicom_normal->subtype = SERIAL_TYPE_NORMAL;
  1434. isicom_normal->init_termios = tty_std_termios;
  1435. isicom_normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
  1436. CLOCAL;
  1437. isicom_normal->flags = TTY_DRIVER_REAL_RAW |
  1438. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK;
  1439. tty_set_operations(isicom_normal, &isicom_ops);
  1440. retval = tty_register_driver(isicom_normal);
  1441. if (retval) {
  1442. pr_debug("Couldn't register the dialin driver\n");
  1443. goto err_puttty;
  1444. }
  1445. retval = pci_register_driver(&isicom_driver);
  1446. if (retval < 0) {
  1447. pr_err("Unable to register pci driver.\n");
  1448. goto err_unrtty;
  1449. }
  1450. mod_timer(&tx, jiffies + 1);
  1451. return 0;
  1452. err_unrtty:
  1453. tty_unregister_driver(isicom_normal);
  1454. err_puttty:
  1455. put_tty_driver(isicom_normal);
  1456. error:
  1457. return retval;
  1458. }
  1459. static void __exit isicom_exit(void)
  1460. {
  1461. del_timer_sync(&tx);
  1462. pci_unregister_driver(&isicom_driver);
  1463. tty_unregister_driver(isicom_normal);
  1464. put_tty_driver(isicom_normal);
  1465. }
  1466. module_init(isicom_init);
  1467. module_exit(isicom_exit);
  1468. MODULE_AUTHOR("MultiTech");
  1469. MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
  1470. MODULE_LICENSE("GPL");
  1471. MODULE_FIRMWARE("isi608.bin");
  1472. MODULE_FIRMWARE("isi608em.bin");
  1473. MODULE_FIRMWARE("isi616em.bin");
  1474. MODULE_FIRMWARE("isi4608.bin");
  1475. MODULE_FIRMWARE("isi4616.bin");