isicom.c 41 KB

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