isicom.c 41 KB

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