isicom.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  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/smp_lock.h>
  126. #include <linux/mm.h>
  127. #include <linux/interrupt.h>
  128. #include <linux/timer.h>
  129. #include <linux/delay.h>
  130. #include <linux/ioport.h>
  131. #include <linux/slab.h>
  132. #include <linux/uaccess.h>
  133. #include <linux/io.h>
  134. #include <asm/system.h>
  135. #include <linux/pci.h>
  136. #include <linux/isicom.h>
  137. #define InterruptTheCard(base) outw(0, (base) + 0xc)
  138. #define ClearInterrupt(base) inw((base) + 0x0a)
  139. #ifdef DEBUG
  140. #define isicom_paranoia_check(a, b, c) __isicom_paranoia_check((a), (b), (c))
  141. #else
  142. #define isicom_paranoia_check(a, b, c) 0
  143. #endif
  144. static int isicom_probe(struct pci_dev *, const struct pci_device_id *);
  145. static void __devexit isicom_remove(struct pci_dev *);
  146. static struct pci_device_id isicom_pci_tbl[] = {
  147. { PCI_DEVICE(VENDOR_ID, 0x2028) },
  148. { PCI_DEVICE(VENDOR_ID, 0x2051) },
  149. { PCI_DEVICE(VENDOR_ID, 0x2052) },
  150. { PCI_DEVICE(VENDOR_ID, 0x2053) },
  151. { PCI_DEVICE(VENDOR_ID, 0x2054) },
  152. { PCI_DEVICE(VENDOR_ID, 0x2055) },
  153. { PCI_DEVICE(VENDOR_ID, 0x2056) },
  154. { PCI_DEVICE(VENDOR_ID, 0x2057) },
  155. { PCI_DEVICE(VENDOR_ID, 0x2058) },
  156. { 0 }
  157. };
  158. MODULE_DEVICE_TABLE(pci, isicom_pci_tbl);
  159. static struct pci_driver isicom_driver = {
  160. .name = "isicom",
  161. .id_table = isicom_pci_tbl,
  162. .probe = isicom_probe,
  163. .remove = __devexit_p(isicom_remove)
  164. };
  165. static int prev_card = 3; /* start servicing isi_card[0] */
  166. static struct tty_driver *isicom_normal;
  167. static void isicom_tx(unsigned long _data);
  168. static void isicom_start(struct tty_struct *tty);
  169. static DEFINE_TIMER(tx, isicom_tx, 0, 0);
  170. /* baud index mappings from linux defns to isi */
  171. static signed char linuxb_to_isib[] = {
  172. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 16, 17, 18, 19, 20, 21
  173. };
  174. struct isi_board {
  175. unsigned long base;
  176. int irq;
  177. unsigned char port_count;
  178. unsigned short status;
  179. unsigned short port_status; /* each bit for each port */
  180. unsigned short shift_count;
  181. struct isi_port *ports;
  182. signed char count;
  183. spinlock_t card_lock; /* Card wide lock 11/5/00 -sameer */
  184. unsigned long flags;
  185. unsigned int index;
  186. };
  187. struct isi_port {
  188. unsigned short magic;
  189. struct tty_port port;
  190. u16 channel;
  191. u16 status;
  192. struct isi_board *card;
  193. unsigned char *xmit_buf;
  194. int xmit_head;
  195. int xmit_tail;
  196. int xmit_cnt;
  197. };
  198. static struct isi_board isi_card[BOARD_COUNT];
  199. static struct isi_port isi_ports[PORT_COUNT];
  200. /*
  201. * Locking functions for card level locking. We need to own both
  202. * the kernel lock for the card and have the card in a position that
  203. * it wants to talk.
  204. */
  205. static inline int WaitTillCardIsFree(unsigned long base)
  206. {
  207. unsigned int count = 0;
  208. unsigned int a = in_atomic(); /* do we run under spinlock? */
  209. while (!(inw(base + 0xe) & 0x1) && count++ < 100)
  210. if (a)
  211. mdelay(1);
  212. else
  213. msleep(1);
  214. return !(inw(base + 0xe) & 0x1);
  215. }
  216. static int lock_card(struct isi_board *card)
  217. {
  218. unsigned long base = card->base;
  219. unsigned int retries, a;
  220. for (retries = 0; retries < 10; retries++) {
  221. spin_lock_irqsave(&card->card_lock, card->flags);
  222. for (a = 0; a < 10; a++) {
  223. if (inw(base + 0xe) & 0x1)
  224. return 1;
  225. udelay(10);
  226. }
  227. spin_unlock_irqrestore(&card->card_lock, card->flags);
  228. msleep(10);
  229. }
  230. pr_warning("Failed to lock Card (0x%lx)\n", 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. pr_warning("Warning: bad isicom magic for dev %s in %s.\n",
  336. name, routine);
  337. return 1;
  338. }
  339. if (port->magic != ISICOM_MAGIC) {
  340. pr_warning("Warning: NULL isicom port for dev %s in %s.\n",
  341. 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_debug("txing %d bytes, port%d.\n",
  395. txcount, 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. pr_warning("%s(0x%lx): %d(channel) > port_count.\n",
  481. __func__, 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_debug("%s: DCD->low.\n",
  514. __func__);
  515. port->status &= ~ISI_DCD;
  516. tty_hangup(tty);
  517. }
  518. } else if (header & ISI_DCD) {
  519. /* Carrier has been detected */
  520. pr_debug("%s: DCD->high.\n",
  521. __func__);
  522. port->status |= ISI_DCD;
  523. wake_up_interruptible(&port->port.open_wait);
  524. }
  525. } else {
  526. if (header & ISI_DCD)
  527. port->status |= ISI_DCD;
  528. else
  529. port->status &= ~ISI_DCD;
  530. }
  531. if (port->port.flags & ASYNC_CTS_FLOW) {
  532. if (tty->hw_stopped) {
  533. if (header & ISI_CTS) {
  534. port->port.tty->hw_stopped = 0;
  535. /* start tx ing */
  536. port->status |= (ISI_TXOK
  537. | ISI_CTS);
  538. tty_wakeup(tty);
  539. }
  540. } else if (!(header & ISI_CTS)) {
  541. tty->hw_stopped = 1;
  542. /* stop tx ing */
  543. port->status &= ~(ISI_TXOK | ISI_CTS);
  544. }
  545. } else {
  546. if (header & ISI_CTS)
  547. port->status |= ISI_CTS;
  548. else
  549. port->status &= ~ISI_CTS;
  550. }
  551. if (header & ISI_DSR)
  552. port->status |= ISI_DSR;
  553. else
  554. port->status &= ~ISI_DSR;
  555. if (header & ISI_RI)
  556. port->status |= ISI_RI;
  557. else
  558. port->status &= ~ISI_RI;
  559. break;
  560. case 1: /* Received Break !!! */
  561. tty_insert_flip_char(tty, 0, TTY_BREAK);
  562. if (port->port.flags & ASYNC_SAK)
  563. do_SAK(tty);
  564. tty_flip_buffer_push(tty);
  565. break;
  566. case 2: /* Statistics */
  567. pr_debug("%s: stats!!!\n", __func__);
  568. break;
  569. default:
  570. pr_debug("%s: Unknown code in status packet.\n",
  571. __func__);
  572. break;
  573. }
  574. } else { /* Data Packet */
  575. count = tty_prepare_flip_string(tty, &rp, byte_count & ~1);
  576. pr_debug("%s: Can rx %d of %d bytes.\n",
  577. __func__, count, byte_count);
  578. word_count = count >> 1;
  579. insw(base, rp, word_count);
  580. byte_count -= (word_count << 1);
  581. if (count & 0x0001) {
  582. tty_insert_flip_char(tty, inw(base) & 0xff,
  583. TTY_NORMAL);
  584. byte_count -= 2;
  585. }
  586. if (byte_count > 0) {
  587. pr_debug("%s(0x%lx:%d): Flip buffer overflow! dropping bytes...\n",
  588. __func__, base, channel + 1);
  589. /* drain out unread xtra data */
  590. while (byte_count > 0) {
  591. inw(base);
  592. byte_count -= 2;
  593. }
  594. }
  595. tty_flip_buffer_push(tty);
  596. }
  597. outw(0x0000, base+0x04); /* enable interrupts */
  598. spin_unlock(&card->card_lock);
  599. tty_kref_put(tty);
  600. return IRQ_HANDLED;
  601. }
  602. static void isicom_config_port(struct tty_struct *tty)
  603. {
  604. struct isi_port *port = tty->driver_data;
  605. struct isi_board *card = port->card;
  606. unsigned long baud;
  607. unsigned long base = card->base;
  608. u16 channel_setup, channel = port->channel,
  609. shift_count = card->shift_count;
  610. unsigned char flow_ctrl;
  611. /* FIXME: Switch to new tty baud API */
  612. baud = C_BAUD(tty);
  613. if (baud & CBAUDEX) {
  614. baud &= ~CBAUDEX;
  615. /* if CBAUDEX bit is on and the baud is set to either 50 or 75
  616. * then the card is programmed for 57.6Kbps or 115Kbps
  617. * respectively.
  618. */
  619. /* 1,2,3,4 => 57.6, 115.2, 230, 460 kbps resp. */
  620. if (baud < 1 || baud > 4)
  621. tty->termios->c_cflag &= ~CBAUDEX;
  622. else
  623. baud += 15;
  624. }
  625. if (baud == 15) {
  626. /* the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set
  627. * by the set_serial_info ioctl ... this is done by
  628. * the 'setserial' utility.
  629. */
  630. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  631. baud++; /* 57.6 Kbps */
  632. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  633. baud += 2; /* 115 Kbps */
  634. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  635. baud += 3; /* 230 kbps*/
  636. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  637. baud += 4; /* 460 kbps*/
  638. }
  639. if (linuxb_to_isib[baud] == -1) {
  640. /* hang up */
  641. drop_dtr(port);
  642. return;
  643. } else
  644. raise_dtr(port);
  645. if (WaitTillCardIsFree(base) == 0) {
  646. outw(0x8000 | (channel << shift_count) | 0x03, base);
  647. outw(linuxb_to_isib[baud] << 8 | 0x03, base);
  648. channel_setup = 0;
  649. switch (C_CSIZE(tty)) {
  650. case CS5:
  651. channel_setup |= ISICOM_CS5;
  652. break;
  653. case CS6:
  654. channel_setup |= ISICOM_CS6;
  655. break;
  656. case CS7:
  657. channel_setup |= ISICOM_CS7;
  658. break;
  659. case CS8:
  660. channel_setup |= ISICOM_CS8;
  661. break;
  662. }
  663. if (C_CSTOPB(tty))
  664. channel_setup |= ISICOM_2SB;
  665. if (C_PARENB(tty)) {
  666. channel_setup |= ISICOM_EVPAR;
  667. if (C_PARODD(tty))
  668. channel_setup |= ISICOM_ODPAR;
  669. }
  670. outw(channel_setup, base);
  671. InterruptTheCard(base);
  672. }
  673. if (C_CLOCAL(tty))
  674. port->port.flags &= ~ASYNC_CHECK_CD;
  675. else
  676. port->port.flags |= ASYNC_CHECK_CD;
  677. /* flow control settings ...*/
  678. flow_ctrl = 0;
  679. port->port.flags &= ~ASYNC_CTS_FLOW;
  680. if (C_CRTSCTS(tty)) {
  681. port->port.flags |= ASYNC_CTS_FLOW;
  682. flow_ctrl |= ISICOM_CTSRTS;
  683. }
  684. if (I_IXON(tty))
  685. flow_ctrl |= ISICOM_RESPOND_XONXOFF;
  686. if (I_IXOFF(tty))
  687. flow_ctrl |= ISICOM_INITIATE_XONXOFF;
  688. if (WaitTillCardIsFree(base) == 0) {
  689. outw(0x8000 | (channel << shift_count) | 0x04, base);
  690. outw(flow_ctrl << 8 | 0x05, base);
  691. outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
  692. InterruptTheCard(base);
  693. }
  694. /* rx enabled -> enable port for rx on the card */
  695. if (C_CREAD(tty)) {
  696. card->port_status |= (1 << channel);
  697. outw(card->port_status, base + 0x02);
  698. }
  699. }
  700. /* open et all */
  701. static inline void isicom_setup_board(struct isi_board *bp)
  702. {
  703. int channel;
  704. struct isi_port *port;
  705. bp->count++;
  706. if (!(bp->status & BOARD_INIT)) {
  707. port = bp->ports;
  708. for (channel = 0; channel < bp->port_count; channel++, port++)
  709. drop_dtr_rts(port);
  710. }
  711. bp->status |= BOARD_ACTIVE | BOARD_INIT;
  712. }
  713. /* Activate and thus setup board are protected from races against shutdown
  714. by the tty_port mutex */
  715. static int isicom_activate(struct tty_port *tport, struct tty_struct *tty)
  716. {
  717. struct isi_port *port = container_of(tport, struct isi_port, port);
  718. struct isi_board *card = port->card;
  719. unsigned long flags;
  720. if (tty_port_alloc_xmit_buf(tport) < 0)
  721. return -ENOMEM;
  722. spin_lock_irqsave(&card->card_lock, flags);
  723. isicom_setup_board(card);
  724. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  725. /* discard any residual data */
  726. if (WaitTillCardIsFree(card->base) == 0) {
  727. outw(0x8000 | (port->channel << card->shift_count) | 0x02,
  728. card->base);
  729. outw(((ISICOM_KILLTX | ISICOM_KILLRX) << 8) | 0x06, card->base);
  730. InterruptTheCard(card->base);
  731. }
  732. isicom_config_port(tty);
  733. spin_unlock_irqrestore(&card->card_lock, flags);
  734. return 0;
  735. }
  736. static int isicom_carrier_raised(struct tty_port *port)
  737. {
  738. struct isi_port *ip = container_of(port, struct isi_port, port);
  739. return (ip->status & ISI_DCD)?1 : 0;
  740. }
  741. static struct tty_port *isicom_find_port(struct tty_struct *tty)
  742. {
  743. struct isi_port *port;
  744. struct isi_board *card;
  745. unsigned int board;
  746. int line = tty->index;
  747. if (line < 0 || line > PORT_COUNT-1)
  748. return NULL;
  749. board = BOARD(line);
  750. card = &isi_card[board];
  751. if (!(card->status & FIRMWARE_LOADED))
  752. return NULL;
  753. /* open on a port greater than the port count for the card !!! */
  754. if (line > ((board * 16) + card->port_count - 1))
  755. return NULL;
  756. port = &isi_ports[line];
  757. if (isicom_paranoia_check(port, tty->name, "isicom_open"))
  758. return NULL;
  759. return &port->port;
  760. }
  761. static int isicom_open(struct tty_struct *tty, struct file *filp)
  762. {
  763. struct isi_port *port;
  764. struct isi_board *card;
  765. struct tty_port *tport;
  766. tport = isicom_find_port(tty);
  767. if (tport == NULL)
  768. return -ENODEV;
  769. port = container_of(tport, struct isi_port, port);
  770. card = &isi_card[BOARD(tty->index)];
  771. return tty_port_open(tport, tty, filp);
  772. }
  773. /* close et all */
  774. /* card->lock HAS to be held */
  775. static void isicom_shutdown_port(struct isi_port *port)
  776. {
  777. struct isi_board *card = port->card;
  778. if (--card->count < 0) {
  779. pr_debug("%s: bad board(0x%lx) count %d.\n",
  780. __func__, card->base, card->count);
  781. card->count = 0;
  782. }
  783. /* last port was closed, shutdown that board too */
  784. if (!card->count)
  785. card->status &= BOARD_ACTIVE;
  786. }
  787. static void isicom_flush_buffer(struct tty_struct *tty)
  788. {
  789. struct isi_port *port = tty->driver_data;
  790. struct isi_board *card = port->card;
  791. unsigned long flags;
  792. if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
  793. return;
  794. spin_lock_irqsave(&card->card_lock, flags);
  795. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  796. spin_unlock_irqrestore(&card->card_lock, flags);
  797. tty_wakeup(tty);
  798. }
  799. static void isicom_shutdown(struct tty_port *port)
  800. {
  801. struct isi_port *ip = container_of(port, struct isi_port, port);
  802. struct isi_board *card = ip->card;
  803. unsigned long flags;
  804. /* indicate to the card that no more data can be received
  805. on this port */
  806. spin_lock_irqsave(&card->card_lock, flags);
  807. card->port_status &= ~(1 << ip->channel);
  808. outw(card->port_status, card->base + 0x02);
  809. isicom_shutdown_port(ip);
  810. spin_unlock_irqrestore(&card->card_lock, flags);
  811. tty_port_free_xmit_buf(port);
  812. }
  813. static void isicom_close(struct tty_struct *tty, struct file *filp)
  814. {
  815. struct isi_port *ip = tty->driver_data;
  816. struct tty_port *port = &ip->port;
  817. if (isicom_paranoia_check(ip, tty->name, "isicom_close"))
  818. return;
  819. tty_port_close(port, tty, filp);
  820. }
  821. /* write et all */
  822. static int isicom_write(struct tty_struct *tty, const unsigned char *buf,
  823. int count)
  824. {
  825. struct isi_port *port = tty->driver_data;
  826. struct isi_board *card = port->card;
  827. unsigned long flags;
  828. int cnt, total = 0;
  829. if (isicom_paranoia_check(port, tty->name, "isicom_write"))
  830. return 0;
  831. spin_lock_irqsave(&card->card_lock, flags);
  832. while (1) {
  833. cnt = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt
  834. - 1, SERIAL_XMIT_SIZE - port->xmit_head));
  835. if (cnt <= 0)
  836. break;
  837. memcpy(port->port.xmit_buf + port->xmit_head, buf, cnt);
  838. port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE
  839. - 1);
  840. port->xmit_cnt += cnt;
  841. buf += cnt;
  842. count -= cnt;
  843. total += cnt;
  844. }
  845. if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
  846. port->status |= ISI_TXOK;
  847. spin_unlock_irqrestore(&card->card_lock, flags);
  848. return total;
  849. }
  850. /* put_char et all */
  851. static int isicom_put_char(struct tty_struct *tty, unsigned char ch)
  852. {
  853. struct isi_port *port = tty->driver_data;
  854. struct isi_board *card = port->card;
  855. unsigned long flags;
  856. if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
  857. return 0;
  858. spin_lock_irqsave(&card->card_lock, flags);
  859. if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
  860. spin_unlock_irqrestore(&card->card_lock, flags);
  861. return 0;
  862. }
  863. port->port.xmit_buf[port->xmit_head++] = ch;
  864. port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
  865. port->xmit_cnt++;
  866. spin_unlock_irqrestore(&card->card_lock, flags);
  867. return 1;
  868. }
  869. /* flush_chars et all */
  870. static void isicom_flush_chars(struct tty_struct *tty)
  871. {
  872. struct isi_port *port = tty->driver_data;
  873. if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
  874. return;
  875. if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  876. !port->port.xmit_buf)
  877. return;
  878. /* this tells the transmitter to consider this port for
  879. data output to the card ... that's the best we can do. */
  880. port->status |= ISI_TXOK;
  881. }
  882. /* write_room et all */
  883. static int isicom_write_room(struct tty_struct *tty)
  884. {
  885. struct isi_port *port = tty->driver_data;
  886. int free;
  887. if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
  888. return 0;
  889. free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
  890. if (free < 0)
  891. free = 0;
  892. return free;
  893. }
  894. /* chars_in_buffer et all */
  895. static int isicom_chars_in_buffer(struct tty_struct *tty)
  896. {
  897. struct isi_port *port = tty->driver_data;
  898. if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
  899. return 0;
  900. return port->xmit_cnt;
  901. }
  902. /* ioctl et all */
  903. static int isicom_send_break(struct tty_struct *tty, int length)
  904. {
  905. struct isi_port *port = tty->driver_data;
  906. struct isi_board *card = port->card;
  907. unsigned long base = card->base;
  908. if (length == -1)
  909. return -EOPNOTSUPP;
  910. if (!lock_card(card))
  911. return -EINVAL;
  912. outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
  913. outw((length & 0xff) << 8 | 0x00, base);
  914. outw((length & 0xff00), base);
  915. InterruptTheCard(base);
  916. unlock_card(card);
  917. return 0;
  918. }
  919. static int isicom_tiocmget(struct tty_struct *tty, struct file *file)
  920. {
  921. struct isi_port *port = tty->driver_data;
  922. /* just send the port status */
  923. u16 status = port->status;
  924. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  925. return -ENODEV;
  926. return ((status & ISI_RTS) ? TIOCM_RTS : 0) |
  927. ((status & ISI_DTR) ? TIOCM_DTR : 0) |
  928. ((status & ISI_DCD) ? TIOCM_CAR : 0) |
  929. ((status & ISI_DSR) ? TIOCM_DSR : 0) |
  930. ((status & ISI_CTS) ? TIOCM_CTS : 0) |
  931. ((status & ISI_RI ) ? TIOCM_RI : 0);
  932. }
  933. static int isicom_tiocmset(struct tty_struct *tty, struct file *file,
  934. unsigned int set, unsigned int clear)
  935. {
  936. struct isi_port *port = tty->driver_data;
  937. unsigned long flags;
  938. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  939. return -ENODEV;
  940. spin_lock_irqsave(&port->card->card_lock, flags);
  941. if (set & TIOCM_RTS)
  942. raise_rts(port);
  943. if (set & TIOCM_DTR)
  944. raise_dtr(port);
  945. if (clear & TIOCM_RTS)
  946. drop_rts(port);
  947. if (clear & TIOCM_DTR)
  948. drop_dtr(port);
  949. spin_unlock_irqrestore(&port->card->card_lock, flags);
  950. return 0;
  951. }
  952. static int isicom_set_serial_info(struct tty_struct *tty,
  953. struct serial_struct __user *info)
  954. {
  955. struct isi_port *port = tty->driver_data;
  956. struct serial_struct newinfo;
  957. int reconfig_port;
  958. if (copy_from_user(&newinfo, info, sizeof(newinfo)))
  959. return -EFAULT;
  960. lock_kernel();
  961. reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) !=
  962. (newinfo.flags & ASYNC_SPD_MASK));
  963. if (!capable(CAP_SYS_ADMIN)) {
  964. if ((newinfo.close_delay != port->port.close_delay) ||
  965. (newinfo.closing_wait != port->port.closing_wait) ||
  966. ((newinfo.flags & ~ASYNC_USR_MASK) !=
  967. (port->port.flags & ~ASYNC_USR_MASK))) {
  968. unlock_kernel();
  969. return -EPERM;
  970. }
  971. port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
  972. (newinfo.flags & ASYNC_USR_MASK));
  973. } else {
  974. port->port.close_delay = newinfo.close_delay;
  975. port->port.closing_wait = newinfo.closing_wait;
  976. port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
  977. (newinfo.flags & ASYNC_FLAGS));
  978. }
  979. if (reconfig_port) {
  980. unsigned long flags;
  981. spin_lock_irqsave(&port->card->card_lock, flags);
  982. isicom_config_port(tty);
  983. spin_unlock_irqrestore(&port->card->card_lock, flags);
  984. }
  985. unlock_kernel();
  986. return 0;
  987. }
  988. static int isicom_get_serial_info(struct isi_port *port,
  989. struct serial_struct __user *info)
  990. {
  991. struct serial_struct out_info;
  992. lock_kernel();
  993. memset(&out_info, 0, sizeof(out_info));
  994. /* out_info.type = ? */
  995. out_info.line = port - isi_ports;
  996. out_info.port = port->card->base;
  997. out_info.irq = port->card->irq;
  998. out_info.flags = port->port.flags;
  999. /* out_info.baud_base = ? */
  1000. out_info.close_delay = port->port.close_delay;
  1001. out_info.closing_wait = port->port.closing_wait;
  1002. unlock_kernel();
  1003. if (copy_to_user(info, &out_info, sizeof(out_info)))
  1004. return -EFAULT;
  1005. return 0;
  1006. }
  1007. static int isicom_ioctl(struct tty_struct *tty, struct file *filp,
  1008. unsigned int cmd, unsigned long arg)
  1009. {
  1010. struct isi_port *port = tty->driver_data;
  1011. void __user *argp = (void __user *)arg;
  1012. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  1013. return -ENODEV;
  1014. switch (cmd) {
  1015. case TIOCGSERIAL:
  1016. return isicom_get_serial_info(port, argp);
  1017. case TIOCSSERIAL:
  1018. return isicom_set_serial_info(tty, argp);
  1019. default:
  1020. return -ENOIOCTLCMD;
  1021. }
  1022. return 0;
  1023. }
  1024. /* set_termios et all */
  1025. static void isicom_set_termios(struct tty_struct *tty,
  1026. struct ktermios *old_termios)
  1027. {
  1028. struct isi_port *port = tty->driver_data;
  1029. unsigned long flags;
  1030. if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
  1031. return;
  1032. if (tty->termios->c_cflag == old_termios->c_cflag &&
  1033. tty->termios->c_iflag == old_termios->c_iflag)
  1034. return;
  1035. spin_lock_irqsave(&port->card->card_lock, flags);
  1036. isicom_config_port(tty);
  1037. spin_unlock_irqrestore(&port->card->card_lock, flags);
  1038. if ((old_termios->c_cflag & CRTSCTS) &&
  1039. !(tty->termios->c_cflag & CRTSCTS)) {
  1040. tty->hw_stopped = 0;
  1041. isicom_start(tty);
  1042. }
  1043. }
  1044. /* throttle et all */
  1045. static void isicom_throttle(struct tty_struct *tty)
  1046. {
  1047. struct isi_port *port = tty->driver_data;
  1048. struct isi_board *card = port->card;
  1049. if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
  1050. return;
  1051. /* tell the card that this port cannot handle any more data for now */
  1052. card->port_status &= ~(1 << port->channel);
  1053. outw(card->port_status, card->base + 0x02);
  1054. }
  1055. /* unthrottle et all */
  1056. static void isicom_unthrottle(struct tty_struct *tty)
  1057. {
  1058. struct isi_port *port = tty->driver_data;
  1059. struct isi_board *card = port->card;
  1060. if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
  1061. return;
  1062. /* tell the card that this port is ready to accept more data */
  1063. card->port_status |= (1 << port->channel);
  1064. outw(card->port_status, card->base + 0x02);
  1065. }
  1066. /* stop et all */
  1067. static void isicom_stop(struct tty_struct *tty)
  1068. {
  1069. struct isi_port *port = tty->driver_data;
  1070. if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
  1071. return;
  1072. /* this tells the transmitter not to consider this port for
  1073. data output to the card. */
  1074. port->status &= ~ISI_TXOK;
  1075. }
  1076. /* start et all */
  1077. static void isicom_start(struct tty_struct *tty)
  1078. {
  1079. struct isi_port *port = tty->driver_data;
  1080. if (isicom_paranoia_check(port, tty->name, "isicom_start"))
  1081. return;
  1082. /* this tells the transmitter to consider this port for
  1083. data output to the card. */
  1084. port->status |= ISI_TXOK;
  1085. }
  1086. static void isicom_hangup(struct tty_struct *tty)
  1087. {
  1088. struct isi_port *port = tty->driver_data;
  1089. if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
  1090. return;
  1091. tty_port_hangup(&port->port);
  1092. }
  1093. /*
  1094. * Driver init and deinit functions
  1095. */
  1096. static const struct tty_operations isicom_ops = {
  1097. .open = isicom_open,
  1098. .close = isicom_close,
  1099. .write = isicom_write,
  1100. .put_char = isicom_put_char,
  1101. .flush_chars = isicom_flush_chars,
  1102. .write_room = isicom_write_room,
  1103. .chars_in_buffer = isicom_chars_in_buffer,
  1104. .ioctl = isicom_ioctl,
  1105. .set_termios = isicom_set_termios,
  1106. .throttle = isicom_throttle,
  1107. .unthrottle = isicom_unthrottle,
  1108. .stop = isicom_stop,
  1109. .start = isicom_start,
  1110. .hangup = isicom_hangup,
  1111. .flush_buffer = isicom_flush_buffer,
  1112. .tiocmget = isicom_tiocmget,
  1113. .tiocmset = isicom_tiocmset,
  1114. .break_ctl = isicom_send_break,
  1115. };
  1116. static const struct tty_port_operations isicom_port_ops = {
  1117. .carrier_raised = isicom_carrier_raised,
  1118. .dtr_rts = isicom_dtr_rts,
  1119. .activate = isicom_activate,
  1120. .shutdown = isicom_shutdown,
  1121. };
  1122. static int __devinit reset_card(struct pci_dev *pdev,
  1123. const unsigned int card, unsigned int *signature)
  1124. {
  1125. struct isi_board *board = pci_get_drvdata(pdev);
  1126. unsigned long base = board->base;
  1127. unsigned int sig, portcount = 0;
  1128. int retval = 0;
  1129. dev_dbg(&pdev->dev, "ISILoad:Resetting Card%d at 0x%lx\n", card + 1,
  1130. base);
  1131. inw(base + 0x8);
  1132. msleep(10);
  1133. outw(0, base + 0x8); /* Reset */
  1134. msleep(1000);
  1135. sig = inw(base + 0x4) & 0xff;
  1136. if (sig != 0xa5 && sig != 0xbb && sig != 0xcc && sig != 0xdd &&
  1137. sig != 0xee) {
  1138. dev_warn(&pdev->dev, "ISILoad:Card%u reset failure (Possible "
  1139. "bad I/O Port Address 0x%lx).\n", card + 1, base);
  1140. dev_dbg(&pdev->dev, "Sig=0x%x\n", sig);
  1141. retval = -EIO;
  1142. goto end;
  1143. }
  1144. msleep(10);
  1145. portcount = inw(base + 0x2);
  1146. if (!(inw(base + 0xe) & 0x1) || (portcount != 0 && portcount != 4 &&
  1147. portcount != 8 && portcount != 16)) {
  1148. dev_err(&pdev->dev, "ISILoad:PCI Card%d reset failure.\n",
  1149. card + 1);
  1150. retval = -EIO;
  1151. goto end;
  1152. }
  1153. switch (sig) {
  1154. case 0xa5:
  1155. case 0xbb:
  1156. case 0xdd:
  1157. board->port_count = (portcount == 4) ? 4 : 8;
  1158. board->shift_count = 12;
  1159. break;
  1160. case 0xcc:
  1161. case 0xee:
  1162. board->port_count = 16;
  1163. board->shift_count = 11;
  1164. break;
  1165. }
  1166. dev_info(&pdev->dev, "-Done\n");
  1167. *signature = sig;
  1168. end:
  1169. return retval;
  1170. }
  1171. static int __devinit load_firmware(struct pci_dev *pdev,
  1172. const unsigned int index, const unsigned int signature)
  1173. {
  1174. struct isi_board *board = pci_get_drvdata(pdev);
  1175. const struct firmware *fw;
  1176. unsigned long base = board->base;
  1177. unsigned int a;
  1178. u16 word_count, status;
  1179. int retval = -EIO;
  1180. char *name;
  1181. u8 *data;
  1182. struct stframe {
  1183. u16 addr;
  1184. u16 count;
  1185. u8 data[0];
  1186. } *frame;
  1187. switch (signature) {
  1188. case 0xa5:
  1189. name = "isi608.bin";
  1190. break;
  1191. case 0xbb:
  1192. name = "isi608em.bin";
  1193. break;
  1194. case 0xcc:
  1195. name = "isi616em.bin";
  1196. break;
  1197. case 0xdd:
  1198. name = "isi4608.bin";
  1199. break;
  1200. case 0xee:
  1201. name = "isi4616.bin";
  1202. break;
  1203. default:
  1204. dev_err(&pdev->dev, "Unknown signature.\n");
  1205. goto end;
  1206. }
  1207. retval = request_firmware(&fw, name, &pdev->dev);
  1208. if (retval)
  1209. goto end;
  1210. retval = -EIO;
  1211. for (frame = (struct stframe *)fw->data;
  1212. frame < (struct stframe *)(fw->data + fw->size);
  1213. frame = (struct stframe *)((u8 *)(frame + 1) +
  1214. frame->count)) {
  1215. if (WaitTillCardIsFree(base))
  1216. goto errrelfw;
  1217. outw(0xf0, base); /* start upload sequence */
  1218. outw(0x00, base);
  1219. outw(frame->addr, base); /* lsb of address */
  1220. word_count = frame->count / 2 + frame->count % 2;
  1221. outw(word_count, base);
  1222. InterruptTheCard(base);
  1223. udelay(100); /* 0x2f */
  1224. if (WaitTillCardIsFree(base))
  1225. goto errrelfw;
  1226. status = inw(base + 0x4);
  1227. if (status != 0) {
  1228. dev_warn(&pdev->dev, "Card%d rejected load header:\n"
  1229. "Address:0x%x\n"
  1230. "Count:0x%x\n"
  1231. "Status:0x%x\n",
  1232. index + 1, frame->addr, frame->count, status);
  1233. goto errrelfw;
  1234. }
  1235. outsw(base, frame->data, word_count);
  1236. InterruptTheCard(base);
  1237. udelay(50); /* 0x0f */
  1238. if (WaitTillCardIsFree(base))
  1239. goto errrelfw;
  1240. status = inw(base + 0x4);
  1241. if (status != 0) {
  1242. dev_err(&pdev->dev, "Card%d got out of sync.Card "
  1243. "Status:0x%x\n", index + 1, status);
  1244. goto errrelfw;
  1245. }
  1246. }
  1247. /* XXX: should we test it by reading it back and comparing with original like
  1248. * in load firmware package? */
  1249. for (frame = (struct stframe *)fw->data;
  1250. frame < (struct stframe *)(fw->data + fw->size);
  1251. frame = (struct stframe *)((u8 *)(frame + 1) +
  1252. frame->count)) {
  1253. if (WaitTillCardIsFree(base))
  1254. goto errrelfw;
  1255. outw(0xf1, base); /* start download sequence */
  1256. outw(0x00, base);
  1257. outw(frame->addr, base); /* lsb of address */
  1258. word_count = (frame->count >> 1) + frame->count % 2;
  1259. outw(word_count + 1, base);
  1260. InterruptTheCard(base);
  1261. udelay(50); /* 0xf */
  1262. if (WaitTillCardIsFree(base))
  1263. goto errrelfw;
  1264. status = inw(base + 0x4);
  1265. if (status != 0) {
  1266. dev_warn(&pdev->dev, "Card%d rejected verify header:\n"
  1267. "Address:0x%x\n"
  1268. "Count:0x%x\n"
  1269. "Status: 0x%x\n",
  1270. index + 1, frame->addr, frame->count, status);
  1271. goto errrelfw;
  1272. }
  1273. data = kmalloc(word_count * 2, GFP_KERNEL);
  1274. if (data == NULL) {
  1275. dev_err(&pdev->dev, "Card%d, firmware upload "
  1276. "failed, not enough memory\n", index + 1);
  1277. goto errrelfw;
  1278. }
  1279. inw(base);
  1280. insw(base, data, word_count);
  1281. InterruptTheCard(base);
  1282. for (a = 0; a < frame->count; a++)
  1283. if (data[a] != frame->data[a]) {
  1284. kfree(data);
  1285. dev_err(&pdev->dev, "Card%d, firmware upload "
  1286. "failed\n", index + 1);
  1287. goto errrelfw;
  1288. }
  1289. kfree(data);
  1290. udelay(50); /* 0xf */
  1291. if (WaitTillCardIsFree(base))
  1292. goto errrelfw;
  1293. status = inw(base + 0x4);
  1294. if (status != 0) {
  1295. dev_err(&pdev->dev, "Card%d verify got out of sync. "
  1296. "Card Status:0x%x\n", index + 1, status);
  1297. goto errrelfw;
  1298. }
  1299. }
  1300. /* xfer ctrl */
  1301. if (WaitTillCardIsFree(base))
  1302. goto errrelfw;
  1303. outw(0xf2, base);
  1304. outw(0x800, base);
  1305. outw(0x0, base);
  1306. outw(0x0, base);
  1307. InterruptTheCard(base);
  1308. outw(0x0, base + 0x4); /* for ISI4608 cards */
  1309. board->status |= FIRMWARE_LOADED;
  1310. retval = 0;
  1311. errrelfw:
  1312. release_firmware(fw);
  1313. end:
  1314. return retval;
  1315. }
  1316. /*
  1317. * Insmod can set static symbols so keep these static
  1318. */
  1319. static unsigned int card_count;
  1320. static int __devinit isicom_probe(struct pci_dev *pdev,
  1321. const struct pci_device_id *ent)
  1322. {
  1323. unsigned int uninitialized_var(signature), index;
  1324. int retval = -EPERM;
  1325. struct isi_board *board = NULL;
  1326. if (card_count >= BOARD_COUNT)
  1327. goto err;
  1328. retval = pci_enable_device(pdev);
  1329. if (retval) {
  1330. dev_err(&pdev->dev, "failed to enable\n");
  1331. goto err;
  1332. }
  1333. dev_info(&pdev->dev, "ISI PCI Card(Device ID 0x%x)\n", ent->device);
  1334. /* allot the first empty slot in the array */
  1335. for (index = 0; index < BOARD_COUNT; index++)
  1336. if (isi_card[index].base == 0) {
  1337. board = &isi_card[index];
  1338. break;
  1339. }
  1340. board->index = index;
  1341. board->base = pci_resource_start(pdev, 3);
  1342. board->irq = pdev->irq;
  1343. card_count++;
  1344. pci_set_drvdata(pdev, board);
  1345. retval = pci_request_region(pdev, 3, ISICOM_NAME);
  1346. if (retval) {
  1347. dev_err(&pdev->dev, "I/O Region 0x%lx-0x%lx is busy. Card%d "
  1348. "will be disabled.\n", board->base, board->base + 15,
  1349. index + 1);
  1350. retval = -EBUSY;
  1351. goto errdec;
  1352. }
  1353. retval = request_irq(board->irq, isicom_interrupt,
  1354. IRQF_SHARED | IRQF_DISABLED, ISICOM_NAME, board);
  1355. if (retval < 0) {
  1356. dev_err(&pdev->dev, "Could not install handler at Irq %d. "
  1357. "Card%d will be disabled.\n", board->irq, index + 1);
  1358. goto errunrr;
  1359. }
  1360. retval = reset_card(pdev, index, &signature);
  1361. if (retval < 0)
  1362. goto errunri;
  1363. retval = load_firmware(pdev, index, signature);
  1364. if (retval < 0)
  1365. goto errunri;
  1366. for (index = 0; index < board->port_count; index++)
  1367. tty_register_device(isicom_normal, board->index * 16 + index,
  1368. &pdev->dev);
  1369. return 0;
  1370. errunri:
  1371. free_irq(board->irq, board);
  1372. errunrr:
  1373. pci_release_region(pdev, 3);
  1374. errdec:
  1375. board->base = 0;
  1376. card_count--;
  1377. pci_disable_device(pdev);
  1378. err:
  1379. return retval;
  1380. }
  1381. static void __devexit isicom_remove(struct pci_dev *pdev)
  1382. {
  1383. struct isi_board *board = pci_get_drvdata(pdev);
  1384. unsigned int i;
  1385. for (i = 0; i < board->port_count; i++)
  1386. tty_unregister_device(isicom_normal, board->index * 16 + i);
  1387. free_irq(board->irq, board);
  1388. pci_release_region(pdev, 3);
  1389. board->base = 0;
  1390. card_count--;
  1391. pci_disable_device(pdev);
  1392. }
  1393. static int __init isicom_init(void)
  1394. {
  1395. int retval, idx, channel;
  1396. struct isi_port *port;
  1397. for (idx = 0; idx < BOARD_COUNT; idx++) {
  1398. port = &isi_ports[idx * 16];
  1399. isi_card[idx].ports = port;
  1400. spin_lock_init(&isi_card[idx].card_lock);
  1401. for (channel = 0; channel < 16; channel++, port++) {
  1402. tty_port_init(&port->port);
  1403. port->port.ops = &isicom_port_ops;
  1404. port->magic = ISICOM_MAGIC;
  1405. port->card = &isi_card[idx];
  1406. port->channel = channel;
  1407. port->port.close_delay = 50 * HZ/100;
  1408. port->port.closing_wait = 3000 * HZ/100;
  1409. port->status = 0;
  1410. /* . . . */
  1411. }
  1412. isi_card[idx].base = 0;
  1413. isi_card[idx].irq = 0;
  1414. }
  1415. /* tty driver structure initialization */
  1416. isicom_normal = alloc_tty_driver(PORT_COUNT);
  1417. if (!isicom_normal) {
  1418. retval = -ENOMEM;
  1419. goto error;
  1420. }
  1421. isicom_normal->owner = THIS_MODULE;
  1422. isicom_normal->name = "ttyM";
  1423. isicom_normal->major = ISICOM_NMAJOR;
  1424. isicom_normal->minor_start = 0;
  1425. isicom_normal->type = TTY_DRIVER_TYPE_SERIAL;
  1426. isicom_normal->subtype = SERIAL_TYPE_NORMAL;
  1427. isicom_normal->init_termios = tty_std_termios;
  1428. isicom_normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
  1429. CLOCAL;
  1430. isicom_normal->flags = TTY_DRIVER_REAL_RAW |
  1431. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK;
  1432. tty_set_operations(isicom_normal, &isicom_ops);
  1433. retval = tty_register_driver(isicom_normal);
  1434. if (retval) {
  1435. pr_debug("Couldn't register the dialin driver\n");
  1436. goto err_puttty;
  1437. }
  1438. retval = pci_register_driver(&isicom_driver);
  1439. if (retval < 0) {
  1440. pr_err("Unable to register pci driver.\n");
  1441. goto err_unrtty;
  1442. }
  1443. mod_timer(&tx, jiffies + 1);
  1444. return 0;
  1445. err_unrtty:
  1446. tty_unregister_driver(isicom_normal);
  1447. err_puttty:
  1448. put_tty_driver(isicom_normal);
  1449. error:
  1450. return retval;
  1451. }
  1452. static void __exit isicom_exit(void)
  1453. {
  1454. del_timer_sync(&tx);
  1455. pci_unregister_driver(&isicom_driver);
  1456. tty_unregister_driver(isicom_normal);
  1457. put_tty_driver(isicom_normal);
  1458. }
  1459. module_init(isicom_init);
  1460. module_exit(isicom_exit);
  1461. MODULE_AUTHOR("MultiTech");
  1462. MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
  1463. MODULE_LICENSE("GPL");
  1464. MODULE_FIRMWARE("isi608.bin");
  1465. MODULE_FIRMWARE("isi608em.bin");
  1466. MODULE_FIRMWARE("isi616em.bin");
  1467. MODULE_FIRMWARE("isi4608.bin");
  1468. MODULE_FIRMWARE("isi4616.bin");