nozomi.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. /*
  2. * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
  3. *
  4. * Written by: Ulf Jakobsson,
  5. * Jan Åkerfeldt,
  6. * Stefan Thomasson,
  7. *
  8. * Maintained by: Paul Hardwick (p.hardwick@option.com)
  9. *
  10. * Patches:
  11. * Locking code changes for Vodafone by Sphere Systems Ltd,
  12. * Andrew Bird (ajb@spheresystems.co.uk )
  13. * & Phil Sanderson
  14. *
  15. * Source has been ported from an implementation made by Filip Aben @ Option
  16. *
  17. * --------------------------------------------------------------------------
  18. *
  19. * Copyright (c) 2005,2006 Option Wireless Sweden AB
  20. * Copyright (c) 2006 Sphere Systems Ltd
  21. * Copyright (c) 2006 Option Wireless n/v
  22. * All rights Reserved.
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License
  35. * along with this program; if not, write to the Free Software
  36. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  37. *
  38. * --------------------------------------------------------------------------
  39. */
  40. /* Enable this to have a lot of debug printouts */
  41. #define DEBUG
  42. #include <linux/kernel.h>
  43. #include <linux/module.h>
  44. #include <linux/pci.h>
  45. #include <linux/ioport.h>
  46. #include <linux/tty.h>
  47. #include <linux/tty_driver.h>
  48. #include <linux/tty_flip.h>
  49. #include <linux/sched.h>
  50. #include <linux/serial.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/kmod.h>
  53. #include <linux/init.h>
  54. #include <linux/kfifo.h>
  55. #include <linux/uaccess.h>
  56. #include <linux/slab.h>
  57. #include <asm/byteorder.h>
  58. #include <linux/delay.h>
  59. #define VERSION_STRING DRIVER_DESC " 2.1d"
  60. /* Macros definitions */
  61. /* Default debug printout level */
  62. #define NOZOMI_DEBUG_LEVEL 0x00
  63. #define P_BUF_SIZE 128
  64. #define NFO(_err_flag_, args...) \
  65. do { \
  66. char tmp[P_BUF_SIZE]; \
  67. snprintf(tmp, sizeof(tmp), ##args); \
  68. printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
  69. __func__, tmp); \
  70. } while (0)
  71. #define DBG1(args...) D_(0x01, ##args)
  72. #define DBG2(args...) D_(0x02, ##args)
  73. #define DBG3(args...) D_(0x04, ##args)
  74. #define DBG4(args...) D_(0x08, ##args)
  75. #define DBG5(args...) D_(0x10, ##args)
  76. #define DBG6(args...) D_(0x20, ##args)
  77. #define DBG7(args...) D_(0x40, ##args)
  78. #define DBG8(args...) D_(0x80, ##args)
  79. #ifdef DEBUG
  80. /* Do we need this settable at runtime? */
  81. static int debug = NOZOMI_DEBUG_LEVEL;
  82. #define D(lvl, args...) do \
  83. {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
  84. while (0)
  85. #define D_(lvl, args...) D(lvl, ##args)
  86. /* These printouts are always printed */
  87. #else
  88. static int debug;
  89. #define D_(lvl, args...)
  90. #endif
  91. /* TODO: rewrite to optimize macros... */
  92. #define TMP_BUF_MAX 256
  93. #define DUMP(buf__,len__) \
  94. do { \
  95. char tbuf[TMP_BUF_MAX] = {0};\
  96. if (len__ > 1) {\
  97. snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
  98. if (tbuf[len__-2] == '\r') {\
  99. tbuf[len__-2] = 'r';\
  100. } \
  101. DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
  102. } else {\
  103. DBG1("SENDING: '%s' (%d)", tbuf, len__);\
  104. } \
  105. } while (0)
  106. /* Defines */
  107. #define NOZOMI_NAME "nozomi"
  108. #define NOZOMI_NAME_TTY "nozomi_tty"
  109. #define DRIVER_DESC "Nozomi driver"
  110. #define NTTY_TTY_MAXMINORS 256
  111. #define NTTY_FIFO_BUFFER_SIZE 8192
  112. /* Must be power of 2 */
  113. #define FIFO_BUFFER_SIZE_UL 8192
  114. /* Size of tmp send buffer to card */
  115. #define SEND_BUF_MAX 1024
  116. #define RECEIVE_BUF_MAX 4
  117. #define R_IIR 0x0000 /* Interrupt Identity Register */
  118. #define R_FCR 0x0000 /* Flow Control Register */
  119. #define R_IER 0x0004 /* Interrupt Enable Register */
  120. #define NOZOMI_CONFIG_MAGIC 0xEFEFFEFE
  121. #define TOGGLE_VALID 0x0000
  122. /* Definition of interrupt tokens */
  123. #define MDM_DL1 0x0001
  124. #define MDM_UL1 0x0002
  125. #define MDM_DL2 0x0004
  126. #define MDM_UL2 0x0008
  127. #define DIAG_DL1 0x0010
  128. #define DIAG_DL2 0x0020
  129. #define DIAG_UL 0x0040
  130. #define APP1_DL 0x0080
  131. #define APP1_UL 0x0100
  132. #define APP2_DL 0x0200
  133. #define APP2_UL 0x0400
  134. #define CTRL_DL 0x0800
  135. #define CTRL_UL 0x1000
  136. #define RESET 0x8000
  137. #define MDM_DL (MDM_DL1 | MDM_DL2)
  138. #define MDM_UL (MDM_UL1 | MDM_UL2)
  139. #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
  140. /* modem signal definition */
  141. #define CTRL_DSR 0x0001
  142. #define CTRL_DCD 0x0002
  143. #define CTRL_RI 0x0004
  144. #define CTRL_CTS 0x0008
  145. #define CTRL_DTR 0x0001
  146. #define CTRL_RTS 0x0002
  147. #define MAX_PORT 4
  148. #define NOZOMI_MAX_PORTS 5
  149. #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
  150. /* Type definitions */
  151. /*
  152. * There are two types of nozomi cards,
  153. * one with 2048 memory and with 8192 memory
  154. */
  155. enum card_type {
  156. F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
  157. F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
  158. };
  159. /* Initialization states a card can be in */
  160. enum card_state {
  161. NOZOMI_STATE_UKNOWN = 0,
  162. NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
  163. NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
  164. NOZOMI_STATE_READY = 3, /* flowcontrols received */
  165. };
  166. /* Two different toggle channels exist */
  167. enum channel_type {
  168. CH_A = 0,
  169. CH_B = 1,
  170. };
  171. /* Port definition for the card regarding flow control */
  172. enum ctrl_port_type {
  173. CTRL_CMD = 0,
  174. CTRL_MDM = 1,
  175. CTRL_DIAG = 2,
  176. CTRL_APP1 = 3,
  177. CTRL_APP2 = 4,
  178. CTRL_ERROR = -1,
  179. };
  180. /* Ports that the nozomi has */
  181. enum port_type {
  182. PORT_MDM = 0,
  183. PORT_DIAG = 1,
  184. PORT_APP1 = 2,
  185. PORT_APP2 = 3,
  186. PORT_CTRL = 4,
  187. PORT_ERROR = -1,
  188. };
  189. #ifdef __BIG_ENDIAN
  190. /* Big endian */
  191. struct toggles {
  192. unsigned int enabled:5; /*
  193. * Toggle fields are valid if enabled is 0,
  194. * else A-channels must always be used.
  195. */
  196. unsigned int diag_dl:1;
  197. unsigned int mdm_dl:1;
  198. unsigned int mdm_ul:1;
  199. } __attribute__ ((packed));
  200. /* Configuration table to read at startup of card */
  201. /* Is for now only needed during initialization phase */
  202. struct config_table {
  203. u32 signature;
  204. u16 product_information;
  205. u16 version;
  206. u8 pad3[3];
  207. struct toggles toggle;
  208. u8 pad1[4];
  209. u16 dl_mdm_len1; /*
  210. * If this is 64, it can hold
  211. * 60 bytes + 4 that is length field
  212. */
  213. u16 dl_start;
  214. u16 dl_diag_len1;
  215. u16 dl_mdm_len2; /*
  216. * If this is 64, it can hold
  217. * 60 bytes + 4 that is length field
  218. */
  219. u16 dl_app1_len;
  220. u16 dl_diag_len2;
  221. u16 dl_ctrl_len;
  222. u16 dl_app2_len;
  223. u8 pad2[16];
  224. u16 ul_mdm_len1;
  225. u16 ul_start;
  226. u16 ul_diag_len;
  227. u16 ul_mdm_len2;
  228. u16 ul_app1_len;
  229. u16 ul_app2_len;
  230. u16 ul_ctrl_len;
  231. } __attribute__ ((packed));
  232. /* This stores all control downlink flags */
  233. struct ctrl_dl {
  234. u8 port;
  235. unsigned int reserved:4;
  236. unsigned int CTS:1;
  237. unsigned int RI:1;
  238. unsigned int DCD:1;
  239. unsigned int DSR:1;
  240. } __attribute__ ((packed));
  241. /* This stores all control uplink flags */
  242. struct ctrl_ul {
  243. u8 port;
  244. unsigned int reserved:6;
  245. unsigned int RTS:1;
  246. unsigned int DTR:1;
  247. } __attribute__ ((packed));
  248. #else
  249. /* Little endian */
  250. /* This represents the toggle information */
  251. struct toggles {
  252. unsigned int mdm_ul:1;
  253. unsigned int mdm_dl:1;
  254. unsigned int diag_dl:1;
  255. unsigned int enabled:5; /*
  256. * Toggle fields are valid if enabled is 0,
  257. * else A-channels must always be used.
  258. */
  259. } __attribute__ ((packed));
  260. /* Configuration table to read at startup of card */
  261. struct config_table {
  262. u32 signature;
  263. u16 version;
  264. u16 product_information;
  265. struct toggles toggle;
  266. u8 pad1[7];
  267. u16 dl_start;
  268. u16 dl_mdm_len1; /*
  269. * If this is 64, it can hold
  270. * 60 bytes + 4 that is length field
  271. */
  272. u16 dl_mdm_len2;
  273. u16 dl_diag_len1;
  274. u16 dl_diag_len2;
  275. u16 dl_app1_len;
  276. u16 dl_app2_len;
  277. u16 dl_ctrl_len;
  278. u8 pad2[16];
  279. u16 ul_start;
  280. u16 ul_mdm_len2;
  281. u16 ul_mdm_len1;
  282. u16 ul_diag_len;
  283. u16 ul_app1_len;
  284. u16 ul_app2_len;
  285. u16 ul_ctrl_len;
  286. } __attribute__ ((packed));
  287. /* This stores all control downlink flags */
  288. struct ctrl_dl {
  289. unsigned int DSR:1;
  290. unsigned int DCD:1;
  291. unsigned int RI:1;
  292. unsigned int CTS:1;
  293. unsigned int reserverd:4;
  294. u8 port;
  295. } __attribute__ ((packed));
  296. /* This stores all control uplink flags */
  297. struct ctrl_ul {
  298. unsigned int DTR:1;
  299. unsigned int RTS:1;
  300. unsigned int reserved:6;
  301. u8 port;
  302. } __attribute__ ((packed));
  303. #endif
  304. /* This holds all information that is needed regarding a port */
  305. struct port {
  306. struct tty_port port;
  307. u8 update_flow_control;
  308. struct ctrl_ul ctrl_ul;
  309. struct ctrl_dl ctrl_dl;
  310. struct kfifo fifo_ul;
  311. void __iomem *dl_addr[2];
  312. u32 dl_size[2];
  313. u8 toggle_dl;
  314. void __iomem *ul_addr[2];
  315. u32 ul_size[2];
  316. u8 toggle_ul;
  317. u16 token_dl;
  318. wait_queue_head_t tty_wait;
  319. struct async_icount tty_icount;
  320. struct nozomi *dc;
  321. };
  322. /* Private data one for each card in the system */
  323. struct nozomi {
  324. void __iomem *base_addr;
  325. unsigned long flip;
  326. /* Pointers to registers */
  327. void __iomem *reg_iir;
  328. void __iomem *reg_fcr;
  329. void __iomem *reg_ier;
  330. u16 last_ier;
  331. enum card_type card_type;
  332. struct config_table config_table; /* Configuration table */
  333. struct pci_dev *pdev;
  334. struct port port[NOZOMI_MAX_PORTS];
  335. u8 *send_buf;
  336. spinlock_t spin_mutex; /* secures access to registers and tty */
  337. unsigned int index_start;
  338. enum card_state state;
  339. u32 open_ttys;
  340. };
  341. /* This is a data packet that is read or written to/from card */
  342. struct buffer {
  343. u32 size; /* size is the length of the data buffer */
  344. u8 *data;
  345. } __attribute__ ((packed));
  346. /* Global variables */
  347. static const struct pci_device_id nozomi_pci_tbl[] = {
  348. {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
  349. {},
  350. };
  351. MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
  352. static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
  353. static struct tty_driver *ntty_driver;
  354. static const struct tty_port_operations noz_tty_port_ops;
  355. /*
  356. * find card by tty_index
  357. */
  358. static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
  359. {
  360. return tty ? ndevs[tty->index / MAX_PORT] : NULL;
  361. }
  362. static inline struct port *get_port_by_tty(const struct tty_struct *tty)
  363. {
  364. struct nozomi *ndev = get_dc_by_tty(tty);
  365. return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
  366. }
  367. /*
  368. * TODO:
  369. * -Optimize
  370. * -Rewrite cleaner
  371. */
  372. static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
  373. u32 size_bytes)
  374. {
  375. u32 i = 0;
  376. const u32 __iomem *ptr = mem_addr_start;
  377. u16 *buf16;
  378. if (unlikely(!ptr || !buf))
  379. goto out;
  380. /* shortcut for extremely often used cases */
  381. switch (size_bytes) {
  382. case 2: /* 2 bytes */
  383. buf16 = (u16 *) buf;
  384. *buf16 = __le16_to_cpu(readw(ptr));
  385. goto out;
  386. break;
  387. case 4: /* 4 bytes */
  388. *(buf) = __le32_to_cpu(readl(ptr));
  389. goto out;
  390. break;
  391. }
  392. while (i < size_bytes) {
  393. if (size_bytes - i == 2) {
  394. /* Handle 2 bytes in the end */
  395. buf16 = (u16 *) buf;
  396. *(buf16) = __le16_to_cpu(readw(ptr));
  397. i += 2;
  398. } else {
  399. /* Read 4 bytes */
  400. *(buf) = __le32_to_cpu(readl(ptr));
  401. i += 4;
  402. }
  403. buf++;
  404. ptr++;
  405. }
  406. out:
  407. return;
  408. }
  409. /*
  410. * TODO:
  411. * -Optimize
  412. * -Rewrite cleaner
  413. */
  414. static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
  415. u32 size_bytes)
  416. {
  417. u32 i = 0;
  418. u32 __iomem *ptr = mem_addr_start;
  419. const u16 *buf16;
  420. if (unlikely(!ptr || !buf))
  421. return 0;
  422. /* shortcut for extremely often used cases */
  423. switch (size_bytes) {
  424. case 2: /* 2 bytes */
  425. buf16 = (const u16 *)buf;
  426. writew(__cpu_to_le16(*buf16), ptr);
  427. return 2;
  428. break;
  429. case 1: /*
  430. * also needs to write 4 bytes in this case
  431. * so falling through..
  432. */
  433. case 4: /* 4 bytes */
  434. writel(__cpu_to_le32(*buf), ptr);
  435. return 4;
  436. break;
  437. }
  438. while (i < size_bytes) {
  439. if (size_bytes - i == 2) {
  440. /* 2 bytes */
  441. buf16 = (const u16 *)buf;
  442. writew(__cpu_to_le16(*buf16), ptr);
  443. i += 2;
  444. } else {
  445. /* 4 bytes */
  446. writel(__cpu_to_le32(*buf), ptr);
  447. i += 4;
  448. }
  449. buf++;
  450. ptr++;
  451. }
  452. return i;
  453. }
  454. /* Setup pointers to different channels and also setup buffer sizes. */
  455. static void nozomi_setup_memory(struct nozomi *dc)
  456. {
  457. void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
  458. /* The length reported is including the length field of 4 bytes,
  459. * hence subtract with 4.
  460. */
  461. const u16 buff_offset = 4;
  462. /* Modem port dl configuration */
  463. dc->port[PORT_MDM].dl_addr[CH_A] = offset;
  464. dc->port[PORT_MDM].dl_addr[CH_B] =
  465. (offset += dc->config_table.dl_mdm_len1);
  466. dc->port[PORT_MDM].dl_size[CH_A] =
  467. dc->config_table.dl_mdm_len1 - buff_offset;
  468. dc->port[PORT_MDM].dl_size[CH_B] =
  469. dc->config_table.dl_mdm_len2 - buff_offset;
  470. /* Diag port dl configuration */
  471. dc->port[PORT_DIAG].dl_addr[CH_A] =
  472. (offset += dc->config_table.dl_mdm_len2);
  473. dc->port[PORT_DIAG].dl_size[CH_A] =
  474. dc->config_table.dl_diag_len1 - buff_offset;
  475. dc->port[PORT_DIAG].dl_addr[CH_B] =
  476. (offset += dc->config_table.dl_diag_len1);
  477. dc->port[PORT_DIAG].dl_size[CH_B] =
  478. dc->config_table.dl_diag_len2 - buff_offset;
  479. /* App1 port dl configuration */
  480. dc->port[PORT_APP1].dl_addr[CH_A] =
  481. (offset += dc->config_table.dl_diag_len2);
  482. dc->port[PORT_APP1].dl_size[CH_A] =
  483. dc->config_table.dl_app1_len - buff_offset;
  484. /* App2 port dl configuration */
  485. dc->port[PORT_APP2].dl_addr[CH_A] =
  486. (offset += dc->config_table.dl_app1_len);
  487. dc->port[PORT_APP2].dl_size[CH_A] =
  488. dc->config_table.dl_app2_len - buff_offset;
  489. /* Ctrl dl configuration */
  490. dc->port[PORT_CTRL].dl_addr[CH_A] =
  491. (offset += dc->config_table.dl_app2_len);
  492. dc->port[PORT_CTRL].dl_size[CH_A] =
  493. dc->config_table.dl_ctrl_len - buff_offset;
  494. offset = dc->base_addr + dc->config_table.ul_start;
  495. /* Modem Port ul configuration */
  496. dc->port[PORT_MDM].ul_addr[CH_A] = offset;
  497. dc->port[PORT_MDM].ul_size[CH_A] =
  498. dc->config_table.ul_mdm_len1 - buff_offset;
  499. dc->port[PORT_MDM].ul_addr[CH_B] =
  500. (offset += dc->config_table.ul_mdm_len1);
  501. dc->port[PORT_MDM].ul_size[CH_B] =
  502. dc->config_table.ul_mdm_len2 - buff_offset;
  503. /* Diag port ul configuration */
  504. dc->port[PORT_DIAG].ul_addr[CH_A] =
  505. (offset += dc->config_table.ul_mdm_len2);
  506. dc->port[PORT_DIAG].ul_size[CH_A] =
  507. dc->config_table.ul_diag_len - buff_offset;
  508. /* App1 port ul configuration */
  509. dc->port[PORT_APP1].ul_addr[CH_A] =
  510. (offset += dc->config_table.ul_diag_len);
  511. dc->port[PORT_APP1].ul_size[CH_A] =
  512. dc->config_table.ul_app1_len - buff_offset;
  513. /* App2 port ul configuration */
  514. dc->port[PORT_APP2].ul_addr[CH_A] =
  515. (offset += dc->config_table.ul_app1_len);
  516. dc->port[PORT_APP2].ul_size[CH_A] =
  517. dc->config_table.ul_app2_len - buff_offset;
  518. /* Ctrl ul configuration */
  519. dc->port[PORT_CTRL].ul_addr[CH_A] =
  520. (offset += dc->config_table.ul_app2_len);
  521. dc->port[PORT_CTRL].ul_size[CH_A] =
  522. dc->config_table.ul_ctrl_len - buff_offset;
  523. }
  524. /* Dump config table under initalization phase */
  525. #ifdef DEBUG
  526. static void dump_table(const struct nozomi *dc)
  527. {
  528. DBG3("signature: 0x%08X", dc->config_table.signature);
  529. DBG3("version: 0x%04X", dc->config_table.version);
  530. DBG3("product_information: 0x%04X", \
  531. dc->config_table.product_information);
  532. DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
  533. DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
  534. DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
  535. DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
  536. DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
  537. DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
  538. dc->config_table.dl_mdm_len1);
  539. DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
  540. dc->config_table.dl_mdm_len2);
  541. DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
  542. dc->config_table.dl_diag_len1);
  543. DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
  544. dc->config_table.dl_diag_len2);
  545. DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
  546. dc->config_table.dl_app1_len);
  547. DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
  548. dc->config_table.dl_app2_len);
  549. DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
  550. dc->config_table.dl_ctrl_len);
  551. DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
  552. dc->config_table.ul_start);
  553. DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
  554. dc->config_table.ul_mdm_len1);
  555. DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
  556. dc->config_table.ul_mdm_len2);
  557. DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
  558. dc->config_table.ul_diag_len);
  559. DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
  560. dc->config_table.ul_app1_len);
  561. DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
  562. dc->config_table.ul_app2_len);
  563. DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
  564. dc->config_table.ul_ctrl_len);
  565. }
  566. #else
  567. static inline void dump_table(const struct nozomi *dc) { }
  568. #endif
  569. /*
  570. * Read configuration table from card under intalization phase
  571. * Returns 1 if ok, else 0
  572. */
  573. static int nozomi_read_config_table(struct nozomi *dc)
  574. {
  575. read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
  576. sizeof(struct config_table));
  577. if (dc->config_table.signature != NOZOMI_CONFIG_MAGIC) {
  578. dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
  579. dc->config_table.signature, NOZOMI_CONFIG_MAGIC);
  580. return 0;
  581. }
  582. if ((dc->config_table.version == 0)
  583. || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
  584. int i;
  585. DBG1("Second phase, configuring card");
  586. nozomi_setup_memory(dc);
  587. dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
  588. dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
  589. dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
  590. DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
  591. dc->port[PORT_MDM].toggle_ul,
  592. dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
  593. dump_table(dc);
  594. for (i = PORT_MDM; i < MAX_PORT; i++) {
  595. memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
  596. memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
  597. }
  598. /* Enable control channel */
  599. dc->last_ier = dc->last_ier | CTRL_DL;
  600. writew(dc->last_ier, dc->reg_ier);
  601. dc->state = NOZOMI_STATE_ALLOCATED;
  602. dev_info(&dc->pdev->dev, "Initialization OK!\n");
  603. return 1;
  604. }
  605. if ((dc->config_table.version > 0)
  606. && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
  607. u32 offset = 0;
  608. DBG1("First phase: pushing upload buffers, clearing download");
  609. dev_info(&dc->pdev->dev, "Version of card: %d\n",
  610. dc->config_table.version);
  611. /* Here we should disable all I/O over F32. */
  612. nozomi_setup_memory(dc);
  613. /*
  614. * We should send ALL channel pair tokens back along
  615. * with reset token
  616. */
  617. /* push upload modem buffers */
  618. write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
  619. (u32 *) &offset, 4);
  620. write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
  621. (u32 *) &offset, 4);
  622. writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
  623. DBG1("First phase done");
  624. }
  625. return 1;
  626. }
  627. /* Enable uplink interrupts */
  628. static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
  629. {
  630. static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
  631. if (port < NOZOMI_MAX_PORTS) {
  632. dc->last_ier |= mask[port];
  633. writew(dc->last_ier, dc->reg_ier);
  634. } else {
  635. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  636. }
  637. }
  638. /* Disable uplink interrupts */
  639. static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
  640. {
  641. static const u16 mask[] =
  642. {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
  643. if (port < NOZOMI_MAX_PORTS) {
  644. dc->last_ier &= mask[port];
  645. writew(dc->last_ier, dc->reg_ier);
  646. } else {
  647. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  648. }
  649. }
  650. /* Enable downlink interrupts */
  651. static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
  652. {
  653. static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
  654. if (port < NOZOMI_MAX_PORTS) {
  655. dc->last_ier |= mask[port];
  656. writew(dc->last_ier, dc->reg_ier);
  657. } else {
  658. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  659. }
  660. }
  661. /* Disable downlink interrupts */
  662. static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
  663. {
  664. static const u16 mask[] =
  665. {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
  666. if (port < NOZOMI_MAX_PORTS) {
  667. dc->last_ier &= mask[port];
  668. writew(dc->last_ier, dc->reg_ier);
  669. } else {
  670. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  671. }
  672. }
  673. /*
  674. * Return 1 - send buffer to card and ack.
  675. * Return 0 - don't ack, don't send buffer to card.
  676. */
  677. static int send_data(enum port_type index, struct nozomi *dc)
  678. {
  679. u32 size = 0;
  680. struct port *port = &dc->port[index];
  681. const u8 toggle = port->toggle_ul;
  682. void __iomem *addr = port->ul_addr[toggle];
  683. const u32 ul_size = port->ul_size[toggle];
  684. /* Get data from tty and place in buf for now */
  685. size = kfifo_out(&port->fifo_ul, dc->send_buf,
  686. ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
  687. if (size == 0) {
  688. DBG4("No more data to send, disable link:");
  689. return 0;
  690. }
  691. /* DUMP(buf, size); */
  692. /* Write length + data */
  693. write_mem32(addr, (u32 *) &size, 4);
  694. write_mem32(addr + 4, (u32 *) dc->send_buf, size);
  695. tty_port_tty_wakeup(&port->port);
  696. return 1;
  697. }
  698. /* If all data has been read, return 1, else 0 */
  699. static int receive_data(enum port_type index, struct nozomi *dc)
  700. {
  701. u8 buf[RECEIVE_BUF_MAX] = { 0 };
  702. int size;
  703. u32 offset = 4;
  704. struct port *port = &dc->port[index];
  705. void __iomem *addr = port->dl_addr[port->toggle_dl];
  706. struct tty_struct *tty = tty_port_tty_get(&port->port);
  707. int i, ret;
  708. size = __le32_to_cpu(readl(addr));
  709. /* DBG1( "%d bytes port: %d", size, index); */
  710. if (tty && test_bit(TTY_THROTTLED, &tty->flags)) {
  711. DBG1("No room in tty, don't read data, don't ack interrupt, "
  712. "disable interrupt");
  713. /* disable interrupt in downlink... */
  714. disable_transmit_dl(index, dc);
  715. ret = 0;
  716. goto put;
  717. }
  718. if (unlikely(size == 0)) {
  719. dev_err(&dc->pdev->dev, "size == 0?\n");
  720. ret = 1;
  721. goto put;
  722. }
  723. while (size > 0) {
  724. read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
  725. if (size == 1) {
  726. tty_insert_flip_char(&port->port, buf[0], TTY_NORMAL);
  727. size = 0;
  728. } else if (size < RECEIVE_BUF_MAX) {
  729. size -= tty_insert_flip_string(&port->port,
  730. (char *)buf, size);
  731. } else {
  732. i = tty_insert_flip_string(&port->port,
  733. (char *)buf, RECEIVE_BUF_MAX);
  734. size -= i;
  735. offset += i;
  736. }
  737. }
  738. set_bit(index, &dc->flip);
  739. ret = 1;
  740. put:
  741. tty_kref_put(tty);
  742. return ret;
  743. }
  744. /* Debug for interrupts */
  745. #ifdef DEBUG
  746. static char *interrupt2str(u16 interrupt)
  747. {
  748. static char buf[TMP_BUF_MAX];
  749. char *p = buf;
  750. interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
  751. interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  752. "MDM_DL2 ") : NULL;
  753. interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  754. "MDM_UL1 ") : NULL;
  755. interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  756. "MDM_UL2 ") : NULL;
  757. interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  758. "DIAG_DL1 ") : NULL;
  759. interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  760. "DIAG_DL2 ") : NULL;
  761. interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  762. "DIAG_UL ") : NULL;
  763. interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  764. "APP1_DL ") : NULL;
  765. interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  766. "APP2_DL ") : NULL;
  767. interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  768. "APP1_UL ") : NULL;
  769. interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  770. "APP2_UL ") : NULL;
  771. interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  772. "CTRL_DL ") : NULL;
  773. interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  774. "CTRL_UL ") : NULL;
  775. interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  776. "RESET ") : NULL;
  777. return buf;
  778. }
  779. #endif
  780. /*
  781. * Receive flow control
  782. * Return 1 - If ok, else 0
  783. */
  784. static int receive_flow_control(struct nozomi *dc)
  785. {
  786. enum port_type port = PORT_MDM;
  787. struct ctrl_dl ctrl_dl;
  788. struct ctrl_dl old_ctrl;
  789. u16 enable_ier = 0;
  790. read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
  791. switch (ctrl_dl.port) {
  792. case CTRL_CMD:
  793. DBG1("The Base Band sends this value as a response to a "
  794. "request for IMSI detach sent over the control "
  795. "channel uplink (see section 7.6.1).");
  796. break;
  797. case CTRL_MDM:
  798. port = PORT_MDM;
  799. enable_ier = MDM_DL;
  800. break;
  801. case CTRL_DIAG:
  802. port = PORT_DIAG;
  803. enable_ier = DIAG_DL;
  804. break;
  805. case CTRL_APP1:
  806. port = PORT_APP1;
  807. enable_ier = APP1_DL;
  808. break;
  809. case CTRL_APP2:
  810. port = PORT_APP2;
  811. enable_ier = APP2_DL;
  812. if (dc->state == NOZOMI_STATE_ALLOCATED) {
  813. /*
  814. * After card initialization the flow control
  815. * received for APP2 is always the last
  816. */
  817. dc->state = NOZOMI_STATE_READY;
  818. dev_info(&dc->pdev->dev, "Device READY!\n");
  819. }
  820. break;
  821. default:
  822. dev_err(&dc->pdev->dev,
  823. "ERROR: flow control received for non-existing port\n");
  824. return 0;
  825. }
  826. DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
  827. *((u16 *)&ctrl_dl));
  828. old_ctrl = dc->port[port].ctrl_dl;
  829. dc->port[port].ctrl_dl = ctrl_dl;
  830. if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
  831. DBG1("Disable interrupt (0x%04X) on port: %d",
  832. enable_ier, port);
  833. disable_transmit_ul(port, dc);
  834. } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
  835. if (kfifo_len(&dc->port[port].fifo_ul)) {
  836. DBG1("Enable interrupt (0x%04X) on port: %d",
  837. enable_ier, port);
  838. DBG1("Data in buffer [%d], enable transmit! ",
  839. kfifo_len(&dc->port[port].fifo_ul));
  840. enable_transmit_ul(port, dc);
  841. } else {
  842. DBG1("No data in buffer...");
  843. }
  844. }
  845. if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
  846. DBG1(" No change in mctrl");
  847. return 1;
  848. }
  849. /* Update statistics */
  850. if (old_ctrl.CTS != ctrl_dl.CTS)
  851. dc->port[port].tty_icount.cts++;
  852. if (old_ctrl.DSR != ctrl_dl.DSR)
  853. dc->port[port].tty_icount.dsr++;
  854. if (old_ctrl.RI != ctrl_dl.RI)
  855. dc->port[port].tty_icount.rng++;
  856. if (old_ctrl.DCD != ctrl_dl.DCD)
  857. dc->port[port].tty_icount.dcd++;
  858. wake_up_interruptible(&dc->port[port].tty_wait);
  859. DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
  860. port,
  861. dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
  862. dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
  863. return 1;
  864. }
  865. static enum ctrl_port_type port2ctrl(enum port_type port,
  866. const struct nozomi *dc)
  867. {
  868. switch (port) {
  869. case PORT_MDM:
  870. return CTRL_MDM;
  871. case PORT_DIAG:
  872. return CTRL_DIAG;
  873. case PORT_APP1:
  874. return CTRL_APP1;
  875. case PORT_APP2:
  876. return CTRL_APP2;
  877. default:
  878. dev_err(&dc->pdev->dev,
  879. "ERROR: send flow control " \
  880. "received for non-existing port\n");
  881. }
  882. return CTRL_ERROR;
  883. }
  884. /*
  885. * Send flow control, can only update one channel at a time
  886. * Return 0 - If we have updated all flow control
  887. * Return 1 - If we need to update more flow control, ack current enable more
  888. */
  889. static int send_flow_control(struct nozomi *dc)
  890. {
  891. u32 i, more_flow_control_to_be_updated = 0;
  892. u16 *ctrl;
  893. for (i = PORT_MDM; i < MAX_PORT; i++) {
  894. if (dc->port[i].update_flow_control) {
  895. if (more_flow_control_to_be_updated) {
  896. /* We have more flow control to be updated */
  897. return 1;
  898. }
  899. dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
  900. ctrl = (u16 *)&dc->port[i].ctrl_ul;
  901. write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
  902. (u32 *) ctrl, 2);
  903. dc->port[i].update_flow_control = 0;
  904. more_flow_control_to_be_updated = 1;
  905. }
  906. }
  907. return 0;
  908. }
  909. /*
  910. * Handle downlink data, ports that are handled are modem and diagnostics
  911. * Return 1 - ok
  912. * Return 0 - toggle fields are out of sync
  913. */
  914. static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
  915. u16 read_iir, u16 mask1, u16 mask2)
  916. {
  917. if (*toggle == 0 && read_iir & mask1) {
  918. if (receive_data(port, dc)) {
  919. writew(mask1, dc->reg_fcr);
  920. *toggle = !(*toggle);
  921. }
  922. if (read_iir & mask2) {
  923. if (receive_data(port, dc)) {
  924. writew(mask2, dc->reg_fcr);
  925. *toggle = !(*toggle);
  926. }
  927. }
  928. } else if (*toggle == 1 && read_iir & mask2) {
  929. if (receive_data(port, dc)) {
  930. writew(mask2, dc->reg_fcr);
  931. *toggle = !(*toggle);
  932. }
  933. if (read_iir & mask1) {
  934. if (receive_data(port, dc)) {
  935. writew(mask1, dc->reg_fcr);
  936. *toggle = !(*toggle);
  937. }
  938. }
  939. } else {
  940. dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
  941. *toggle);
  942. return 0;
  943. }
  944. return 1;
  945. }
  946. /*
  947. * Handle uplink data, this is currently for the modem port
  948. * Return 1 - ok
  949. * Return 0 - toggle field are out of sync
  950. */
  951. static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
  952. {
  953. u8 *toggle = &(dc->port[port].toggle_ul);
  954. if (*toggle == 0 && read_iir & MDM_UL1) {
  955. dc->last_ier &= ~MDM_UL;
  956. writew(dc->last_ier, dc->reg_ier);
  957. if (send_data(port, dc)) {
  958. writew(MDM_UL1, dc->reg_fcr);
  959. dc->last_ier = dc->last_ier | MDM_UL;
  960. writew(dc->last_ier, dc->reg_ier);
  961. *toggle = !*toggle;
  962. }
  963. if (read_iir & MDM_UL2) {
  964. dc->last_ier &= ~MDM_UL;
  965. writew(dc->last_ier, dc->reg_ier);
  966. if (send_data(port, dc)) {
  967. writew(MDM_UL2, dc->reg_fcr);
  968. dc->last_ier = dc->last_ier | MDM_UL;
  969. writew(dc->last_ier, dc->reg_ier);
  970. *toggle = !*toggle;
  971. }
  972. }
  973. } else if (*toggle == 1 && read_iir & MDM_UL2) {
  974. dc->last_ier &= ~MDM_UL;
  975. writew(dc->last_ier, dc->reg_ier);
  976. if (send_data(port, dc)) {
  977. writew(MDM_UL2, dc->reg_fcr);
  978. dc->last_ier = dc->last_ier | MDM_UL;
  979. writew(dc->last_ier, dc->reg_ier);
  980. *toggle = !*toggle;
  981. }
  982. if (read_iir & MDM_UL1) {
  983. dc->last_ier &= ~MDM_UL;
  984. writew(dc->last_ier, dc->reg_ier);
  985. if (send_data(port, dc)) {
  986. writew(MDM_UL1, dc->reg_fcr);
  987. dc->last_ier = dc->last_ier | MDM_UL;
  988. writew(dc->last_ier, dc->reg_ier);
  989. *toggle = !*toggle;
  990. }
  991. }
  992. } else {
  993. writew(read_iir & MDM_UL, dc->reg_fcr);
  994. dev_err(&dc->pdev->dev, "port out of sync!\n");
  995. return 0;
  996. }
  997. return 1;
  998. }
  999. static irqreturn_t interrupt_handler(int irq, void *dev_id)
  1000. {
  1001. struct nozomi *dc = dev_id;
  1002. unsigned int a;
  1003. u16 read_iir;
  1004. if (!dc)
  1005. return IRQ_NONE;
  1006. spin_lock(&dc->spin_mutex);
  1007. read_iir = readw(dc->reg_iir);
  1008. /* Card removed */
  1009. if (read_iir == (u16)-1)
  1010. goto none;
  1011. /*
  1012. * Just handle interrupt enabled in IER
  1013. * (by masking with dc->last_ier)
  1014. */
  1015. read_iir &= dc->last_ier;
  1016. if (read_iir == 0)
  1017. goto none;
  1018. DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
  1019. dc->last_ier);
  1020. if (read_iir & RESET) {
  1021. if (unlikely(!nozomi_read_config_table(dc))) {
  1022. dc->last_ier = 0x0;
  1023. writew(dc->last_ier, dc->reg_ier);
  1024. dev_err(&dc->pdev->dev, "Could not read status from "
  1025. "card, we should disable interface\n");
  1026. } else {
  1027. writew(RESET, dc->reg_fcr);
  1028. }
  1029. /* No more useful info if this was the reset interrupt. */
  1030. goto exit_handler;
  1031. }
  1032. if (read_iir & CTRL_UL) {
  1033. DBG1("CTRL_UL");
  1034. dc->last_ier &= ~CTRL_UL;
  1035. writew(dc->last_ier, dc->reg_ier);
  1036. if (send_flow_control(dc)) {
  1037. writew(CTRL_UL, dc->reg_fcr);
  1038. dc->last_ier = dc->last_ier | CTRL_UL;
  1039. writew(dc->last_ier, dc->reg_ier);
  1040. }
  1041. }
  1042. if (read_iir & CTRL_DL) {
  1043. receive_flow_control(dc);
  1044. writew(CTRL_DL, dc->reg_fcr);
  1045. }
  1046. if (read_iir & MDM_DL) {
  1047. if (!handle_data_dl(dc, PORT_MDM,
  1048. &(dc->port[PORT_MDM].toggle_dl), read_iir,
  1049. MDM_DL1, MDM_DL2)) {
  1050. dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
  1051. goto exit_handler;
  1052. }
  1053. }
  1054. if (read_iir & MDM_UL) {
  1055. if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
  1056. dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
  1057. goto exit_handler;
  1058. }
  1059. }
  1060. if (read_iir & DIAG_DL) {
  1061. if (!handle_data_dl(dc, PORT_DIAG,
  1062. &(dc->port[PORT_DIAG].toggle_dl), read_iir,
  1063. DIAG_DL1, DIAG_DL2)) {
  1064. dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
  1065. goto exit_handler;
  1066. }
  1067. }
  1068. if (read_iir & DIAG_UL) {
  1069. dc->last_ier &= ~DIAG_UL;
  1070. writew(dc->last_ier, dc->reg_ier);
  1071. if (send_data(PORT_DIAG, dc)) {
  1072. writew(DIAG_UL, dc->reg_fcr);
  1073. dc->last_ier = dc->last_ier | DIAG_UL;
  1074. writew(dc->last_ier, dc->reg_ier);
  1075. }
  1076. }
  1077. if (read_iir & APP1_DL) {
  1078. if (receive_data(PORT_APP1, dc))
  1079. writew(APP1_DL, dc->reg_fcr);
  1080. }
  1081. if (read_iir & APP1_UL) {
  1082. dc->last_ier &= ~APP1_UL;
  1083. writew(dc->last_ier, dc->reg_ier);
  1084. if (send_data(PORT_APP1, dc)) {
  1085. writew(APP1_UL, dc->reg_fcr);
  1086. dc->last_ier = dc->last_ier | APP1_UL;
  1087. writew(dc->last_ier, dc->reg_ier);
  1088. }
  1089. }
  1090. if (read_iir & APP2_DL) {
  1091. if (receive_data(PORT_APP2, dc))
  1092. writew(APP2_DL, dc->reg_fcr);
  1093. }
  1094. if (read_iir & APP2_UL) {
  1095. dc->last_ier &= ~APP2_UL;
  1096. writew(dc->last_ier, dc->reg_ier);
  1097. if (send_data(PORT_APP2, dc)) {
  1098. writew(APP2_UL, dc->reg_fcr);
  1099. dc->last_ier = dc->last_ier | APP2_UL;
  1100. writew(dc->last_ier, dc->reg_ier);
  1101. }
  1102. }
  1103. exit_handler:
  1104. spin_unlock(&dc->spin_mutex);
  1105. for (a = 0; a < NOZOMI_MAX_PORTS; a++)
  1106. if (test_and_clear_bit(a, &dc->flip))
  1107. tty_flip_buffer_push(&dc->port[a].port);
  1108. return IRQ_HANDLED;
  1109. none:
  1110. spin_unlock(&dc->spin_mutex);
  1111. return IRQ_NONE;
  1112. }
  1113. static void nozomi_get_card_type(struct nozomi *dc)
  1114. {
  1115. int i;
  1116. u32 size = 0;
  1117. for (i = 0; i < 6; i++)
  1118. size += pci_resource_len(dc->pdev, i);
  1119. /* Assume card type F32_8 if no match */
  1120. dc->card_type = size == 2048 ? F32_2 : F32_8;
  1121. dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
  1122. }
  1123. static void nozomi_setup_private_data(struct nozomi *dc)
  1124. {
  1125. void __iomem *offset = dc->base_addr + dc->card_type / 2;
  1126. unsigned int i;
  1127. dc->reg_fcr = (void __iomem *)(offset + R_FCR);
  1128. dc->reg_iir = (void __iomem *)(offset + R_IIR);
  1129. dc->reg_ier = (void __iomem *)(offset + R_IER);
  1130. dc->last_ier = 0;
  1131. dc->flip = 0;
  1132. dc->port[PORT_MDM].token_dl = MDM_DL;
  1133. dc->port[PORT_DIAG].token_dl = DIAG_DL;
  1134. dc->port[PORT_APP1].token_dl = APP1_DL;
  1135. dc->port[PORT_APP2].token_dl = APP2_DL;
  1136. for (i = 0; i < MAX_PORT; i++)
  1137. init_waitqueue_head(&dc->port[i].tty_wait);
  1138. }
  1139. static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
  1140. char *buf)
  1141. {
  1142. const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
  1143. return sprintf(buf, "%d\n", dc->card_type);
  1144. }
  1145. static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
  1146. static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
  1147. char *buf)
  1148. {
  1149. const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
  1150. return sprintf(buf, "%u\n", dc->open_ttys);
  1151. }
  1152. static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
  1153. static void make_sysfs_files(struct nozomi *dc)
  1154. {
  1155. if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
  1156. dev_err(&dc->pdev->dev,
  1157. "Could not create sysfs file for card_type\n");
  1158. if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
  1159. dev_err(&dc->pdev->dev,
  1160. "Could not create sysfs file for open_ttys\n");
  1161. }
  1162. static void remove_sysfs_files(struct nozomi *dc)
  1163. {
  1164. device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
  1165. device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
  1166. }
  1167. /* Allocate memory for one device */
  1168. static int nozomi_card_init(struct pci_dev *pdev,
  1169. const struct pci_device_id *ent)
  1170. {
  1171. resource_size_t start;
  1172. int ret;
  1173. struct nozomi *dc = NULL;
  1174. int ndev_idx;
  1175. int i;
  1176. dev_dbg(&pdev->dev, "Init, new card found\n");
  1177. for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
  1178. if (!ndevs[ndev_idx])
  1179. break;
  1180. if (ndev_idx >= ARRAY_SIZE(ndevs)) {
  1181. dev_err(&pdev->dev, "no free tty range for this card left\n");
  1182. ret = -EIO;
  1183. goto err;
  1184. }
  1185. dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
  1186. if (unlikely(!dc)) {
  1187. dev_err(&pdev->dev, "Could not allocate memory\n");
  1188. ret = -ENOMEM;
  1189. goto err_free;
  1190. }
  1191. dc->pdev = pdev;
  1192. ret = pci_enable_device(dc->pdev);
  1193. if (ret) {
  1194. dev_err(&pdev->dev, "Failed to enable PCI Device\n");
  1195. goto err_free;
  1196. }
  1197. ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
  1198. if (ret) {
  1199. dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
  1200. (int) /* nozomi_private.io_addr */ 0);
  1201. goto err_disable_device;
  1202. }
  1203. start = pci_resource_start(dc->pdev, 0);
  1204. if (start == 0) {
  1205. dev_err(&pdev->dev, "No I/O address for card detected\n");
  1206. ret = -ENODEV;
  1207. goto err_rel_regs;
  1208. }
  1209. /* Find out what card type it is */
  1210. nozomi_get_card_type(dc);
  1211. dc->base_addr = ioremap_nocache(start, dc->card_type);
  1212. if (!dc->base_addr) {
  1213. dev_err(&pdev->dev, "Unable to map card MMIO\n");
  1214. ret = -ENODEV;
  1215. goto err_rel_regs;
  1216. }
  1217. dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
  1218. if (!dc->send_buf) {
  1219. dev_err(&pdev->dev, "Could not allocate send buffer?\n");
  1220. ret = -ENOMEM;
  1221. goto err_free_sbuf;
  1222. }
  1223. for (i = PORT_MDM; i < MAX_PORT; i++) {
  1224. if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL,
  1225. GFP_KERNEL)) {
  1226. dev_err(&pdev->dev,
  1227. "Could not allocate kfifo buffer\n");
  1228. ret = -ENOMEM;
  1229. goto err_free_kfifo;
  1230. }
  1231. }
  1232. spin_lock_init(&dc->spin_mutex);
  1233. nozomi_setup_private_data(dc);
  1234. /* Disable all interrupts */
  1235. dc->last_ier = 0;
  1236. writew(dc->last_ier, dc->reg_ier);
  1237. ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
  1238. NOZOMI_NAME, dc);
  1239. if (unlikely(ret)) {
  1240. dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
  1241. goto err_free_kfifo;
  1242. }
  1243. DBG1("base_addr: %p", dc->base_addr);
  1244. make_sysfs_files(dc);
  1245. dc->index_start = ndev_idx * MAX_PORT;
  1246. ndevs[ndev_idx] = dc;
  1247. pci_set_drvdata(pdev, dc);
  1248. /* Enable RESET interrupt */
  1249. dc->last_ier = RESET;
  1250. iowrite16(dc->last_ier, dc->reg_ier);
  1251. dc->state = NOZOMI_STATE_ENABLED;
  1252. for (i = 0; i < MAX_PORT; i++) {
  1253. struct device *tty_dev;
  1254. struct port *port = &dc->port[i];
  1255. port->dc = dc;
  1256. tty_port_init(&port->port);
  1257. port->port.ops = &noz_tty_port_ops;
  1258. tty_dev = tty_port_register_device(&port->port, ntty_driver,
  1259. dc->index_start + i, &pdev->dev);
  1260. if (IS_ERR(tty_dev)) {
  1261. ret = PTR_ERR(tty_dev);
  1262. dev_err(&pdev->dev, "Could not allocate tty?\n");
  1263. tty_port_destroy(&port->port);
  1264. goto err_free_tty;
  1265. }
  1266. }
  1267. return 0;
  1268. err_free_tty:
  1269. for (i = 0; i < MAX_PORT; ++i) {
  1270. tty_unregister_device(ntty_driver, dc->index_start + i);
  1271. tty_port_destroy(&dc->port[i].port);
  1272. }
  1273. err_free_kfifo:
  1274. for (i = 0; i < MAX_PORT; i++)
  1275. kfifo_free(&dc->port[i].fifo_ul);
  1276. err_free_sbuf:
  1277. kfree(dc->send_buf);
  1278. iounmap(dc->base_addr);
  1279. err_rel_regs:
  1280. pci_release_regions(pdev);
  1281. err_disable_device:
  1282. pci_disable_device(pdev);
  1283. err_free:
  1284. kfree(dc);
  1285. err:
  1286. return ret;
  1287. }
  1288. static void tty_exit(struct nozomi *dc)
  1289. {
  1290. unsigned int i;
  1291. DBG1(" ");
  1292. for (i = 0; i < MAX_PORT; ++i)
  1293. tty_port_tty_hangup(&dc->port[i].port, false);
  1294. /* Racy below - surely should wait for scheduled work to be done or
  1295. complete off a hangup method ? */
  1296. while (dc->open_ttys)
  1297. msleep(1);
  1298. for (i = 0; i < MAX_PORT; ++i) {
  1299. tty_unregister_device(ntty_driver, dc->index_start + i);
  1300. tty_port_destroy(&dc->port[i].port);
  1301. }
  1302. }
  1303. /* Deallocate memory for one device */
  1304. static void nozomi_card_exit(struct pci_dev *pdev)
  1305. {
  1306. int i;
  1307. struct ctrl_ul ctrl;
  1308. struct nozomi *dc = pci_get_drvdata(pdev);
  1309. /* Disable all interrupts */
  1310. dc->last_ier = 0;
  1311. writew(dc->last_ier, dc->reg_ier);
  1312. tty_exit(dc);
  1313. /* Send 0x0001, command card to resend the reset token. */
  1314. /* This is to get the reset when the module is reloaded. */
  1315. ctrl.port = 0x00;
  1316. ctrl.reserved = 0;
  1317. ctrl.RTS = 0;
  1318. ctrl.DTR = 1;
  1319. DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
  1320. /* Setup dc->reg addresses to we can use defines here */
  1321. write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
  1322. writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
  1323. remove_sysfs_files(dc);
  1324. free_irq(pdev->irq, dc);
  1325. for (i = 0; i < MAX_PORT; i++)
  1326. kfifo_free(&dc->port[i].fifo_ul);
  1327. kfree(dc->send_buf);
  1328. iounmap(dc->base_addr);
  1329. pci_release_regions(pdev);
  1330. pci_disable_device(pdev);
  1331. ndevs[dc->index_start / MAX_PORT] = NULL;
  1332. kfree(dc);
  1333. }
  1334. static void set_rts(const struct tty_struct *tty, int rts)
  1335. {
  1336. struct port *port = get_port_by_tty(tty);
  1337. port->ctrl_ul.RTS = rts;
  1338. port->update_flow_control = 1;
  1339. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1340. }
  1341. static void set_dtr(const struct tty_struct *tty, int dtr)
  1342. {
  1343. struct port *port = get_port_by_tty(tty);
  1344. DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
  1345. port->ctrl_ul.DTR = dtr;
  1346. port->update_flow_control = 1;
  1347. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1348. }
  1349. /*
  1350. * ----------------------------------------------------------------------------
  1351. * TTY code
  1352. * ----------------------------------------------------------------------------
  1353. */
  1354. static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
  1355. {
  1356. struct port *port = get_port_by_tty(tty);
  1357. struct nozomi *dc = get_dc_by_tty(tty);
  1358. int ret;
  1359. if (!port || !dc || dc->state != NOZOMI_STATE_READY)
  1360. return -ENODEV;
  1361. ret = tty_standard_install(driver, tty);
  1362. if (ret == 0)
  1363. tty->driver_data = port;
  1364. return ret;
  1365. }
  1366. static void ntty_cleanup(struct tty_struct *tty)
  1367. {
  1368. tty->driver_data = NULL;
  1369. }
  1370. static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
  1371. {
  1372. struct port *port = container_of(tport, struct port, port);
  1373. struct nozomi *dc = port->dc;
  1374. unsigned long flags;
  1375. DBG1("open: %d", port->token_dl);
  1376. spin_lock_irqsave(&dc->spin_mutex, flags);
  1377. dc->last_ier = dc->last_ier | port->token_dl;
  1378. writew(dc->last_ier, dc->reg_ier);
  1379. dc->open_ttys++;
  1380. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1381. printk("noz: activated %d: %p\n", tty->index, tport);
  1382. return 0;
  1383. }
  1384. static int ntty_open(struct tty_struct *tty, struct file *filp)
  1385. {
  1386. struct port *port = tty->driver_data;
  1387. return tty_port_open(&port->port, tty, filp);
  1388. }
  1389. static void ntty_shutdown(struct tty_port *tport)
  1390. {
  1391. struct port *port = container_of(tport, struct port, port);
  1392. struct nozomi *dc = port->dc;
  1393. unsigned long flags;
  1394. DBG1("close: %d", port->token_dl);
  1395. spin_lock_irqsave(&dc->spin_mutex, flags);
  1396. dc->last_ier &= ~(port->token_dl);
  1397. writew(dc->last_ier, dc->reg_ier);
  1398. dc->open_ttys--;
  1399. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1400. printk("noz: shutdown %p\n", tport);
  1401. }
  1402. static void ntty_close(struct tty_struct *tty, struct file *filp)
  1403. {
  1404. struct port *port = tty->driver_data;
  1405. if (port)
  1406. tty_port_close(&port->port, tty, filp);
  1407. }
  1408. static void ntty_hangup(struct tty_struct *tty)
  1409. {
  1410. struct port *port = tty->driver_data;
  1411. tty_port_hangup(&port->port);
  1412. }
  1413. /*
  1414. * called when the userspace process writes to the tty (/dev/noz*).
  1415. * Data is inserted into a fifo, which is then read and transferred to the modem.
  1416. */
  1417. static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
  1418. int count)
  1419. {
  1420. int rval = -EINVAL;
  1421. struct nozomi *dc = get_dc_by_tty(tty);
  1422. struct port *port = tty->driver_data;
  1423. unsigned long flags;
  1424. /* DBG1( "WRITEx: %d, index = %d", count, index); */
  1425. if (!dc || !port)
  1426. return -ENODEV;
  1427. rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
  1428. spin_lock_irqsave(&dc->spin_mutex, flags);
  1429. /* CTS is only valid on the modem channel */
  1430. if (port == &(dc->port[PORT_MDM])) {
  1431. if (port->ctrl_dl.CTS) {
  1432. DBG4("Enable interrupt");
  1433. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1434. } else {
  1435. dev_err(&dc->pdev->dev,
  1436. "CTS not active on modem port?\n");
  1437. }
  1438. } else {
  1439. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1440. }
  1441. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1442. return rval;
  1443. }
  1444. /*
  1445. * Calculate how much is left in device
  1446. * This method is called by the upper tty layer.
  1447. * #according to sources N_TTY.c it expects a value >= 0 and
  1448. * does not check for negative values.
  1449. *
  1450. * If the port is unplugged report lots of room and let the bits
  1451. * dribble away so we don't block anything.
  1452. */
  1453. static int ntty_write_room(struct tty_struct *tty)
  1454. {
  1455. struct port *port = tty->driver_data;
  1456. int room = 4096;
  1457. const struct nozomi *dc = get_dc_by_tty(tty);
  1458. if (dc)
  1459. room = kfifo_avail(&port->fifo_ul);
  1460. return room;
  1461. }
  1462. /* Gets io control parameters */
  1463. static int ntty_tiocmget(struct tty_struct *tty)
  1464. {
  1465. const struct port *port = tty->driver_data;
  1466. const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
  1467. const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
  1468. /* Note: these could change under us but it is not clear this
  1469. matters if so */
  1470. return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
  1471. (ctrl_ul->DTR ? TIOCM_DTR : 0) |
  1472. (ctrl_dl->DCD ? TIOCM_CAR : 0) |
  1473. (ctrl_dl->RI ? TIOCM_RNG : 0) |
  1474. (ctrl_dl->DSR ? TIOCM_DSR : 0) |
  1475. (ctrl_dl->CTS ? TIOCM_CTS : 0);
  1476. }
  1477. /* Sets io controls parameters */
  1478. static int ntty_tiocmset(struct tty_struct *tty,
  1479. unsigned int set, unsigned int clear)
  1480. {
  1481. struct nozomi *dc = get_dc_by_tty(tty);
  1482. unsigned long flags;
  1483. spin_lock_irqsave(&dc->spin_mutex, flags);
  1484. if (set & TIOCM_RTS)
  1485. set_rts(tty, 1);
  1486. else if (clear & TIOCM_RTS)
  1487. set_rts(tty, 0);
  1488. if (set & TIOCM_DTR)
  1489. set_dtr(tty, 1);
  1490. else if (clear & TIOCM_DTR)
  1491. set_dtr(tty, 0);
  1492. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1493. return 0;
  1494. }
  1495. static int ntty_cflags_changed(struct port *port, unsigned long flags,
  1496. struct async_icount *cprev)
  1497. {
  1498. const struct async_icount cnow = port->tty_icount;
  1499. int ret;
  1500. ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
  1501. ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
  1502. ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
  1503. ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
  1504. *cprev = cnow;
  1505. return ret;
  1506. }
  1507. static int ntty_tiocgicount(struct tty_struct *tty,
  1508. struct serial_icounter_struct *icount)
  1509. {
  1510. struct port *port = tty->driver_data;
  1511. const struct async_icount cnow = port->tty_icount;
  1512. icount->cts = cnow.cts;
  1513. icount->dsr = cnow.dsr;
  1514. icount->rng = cnow.rng;
  1515. icount->dcd = cnow.dcd;
  1516. icount->rx = cnow.rx;
  1517. icount->tx = cnow.tx;
  1518. icount->frame = cnow.frame;
  1519. icount->overrun = cnow.overrun;
  1520. icount->parity = cnow.parity;
  1521. icount->brk = cnow.brk;
  1522. icount->buf_overrun = cnow.buf_overrun;
  1523. return 0;
  1524. }
  1525. static int ntty_ioctl(struct tty_struct *tty,
  1526. unsigned int cmd, unsigned long arg)
  1527. {
  1528. struct port *port = tty->driver_data;
  1529. int rval = -ENOIOCTLCMD;
  1530. DBG1("******** IOCTL, cmd: %d", cmd);
  1531. switch (cmd) {
  1532. case TIOCMIWAIT: {
  1533. struct async_icount cprev = port->tty_icount;
  1534. rval = wait_event_interruptible(port->tty_wait,
  1535. ntty_cflags_changed(port, arg, &cprev));
  1536. break;
  1537. }
  1538. default:
  1539. DBG1("ERR: 0x%08X, %d", cmd, cmd);
  1540. break;
  1541. }
  1542. return rval;
  1543. }
  1544. /*
  1545. * Called by the upper tty layer when tty buffers are ready
  1546. * to receive data again after a call to throttle.
  1547. */
  1548. static void ntty_unthrottle(struct tty_struct *tty)
  1549. {
  1550. struct nozomi *dc = get_dc_by_tty(tty);
  1551. unsigned long flags;
  1552. DBG1("UNTHROTTLE");
  1553. spin_lock_irqsave(&dc->spin_mutex, flags);
  1554. enable_transmit_dl(tty->index % MAX_PORT, dc);
  1555. set_rts(tty, 1);
  1556. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1557. }
  1558. /*
  1559. * Called by the upper tty layer when the tty buffers are almost full.
  1560. * The driver should stop send more data.
  1561. */
  1562. static void ntty_throttle(struct tty_struct *tty)
  1563. {
  1564. struct nozomi *dc = get_dc_by_tty(tty);
  1565. unsigned long flags;
  1566. DBG1("THROTTLE");
  1567. spin_lock_irqsave(&dc->spin_mutex, flags);
  1568. set_rts(tty, 0);
  1569. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1570. }
  1571. /* Returns number of chars in buffer, called by tty layer */
  1572. static s32 ntty_chars_in_buffer(struct tty_struct *tty)
  1573. {
  1574. struct port *port = tty->driver_data;
  1575. struct nozomi *dc = get_dc_by_tty(tty);
  1576. s32 rval = 0;
  1577. if (unlikely(!dc || !port)) {
  1578. goto exit_in_buffer;
  1579. }
  1580. rval = kfifo_len(&port->fifo_ul);
  1581. exit_in_buffer:
  1582. return rval;
  1583. }
  1584. static const struct tty_port_operations noz_tty_port_ops = {
  1585. .activate = ntty_activate,
  1586. .shutdown = ntty_shutdown,
  1587. };
  1588. static const struct tty_operations tty_ops = {
  1589. .ioctl = ntty_ioctl,
  1590. .open = ntty_open,
  1591. .close = ntty_close,
  1592. .hangup = ntty_hangup,
  1593. .write = ntty_write,
  1594. .write_room = ntty_write_room,
  1595. .unthrottle = ntty_unthrottle,
  1596. .throttle = ntty_throttle,
  1597. .chars_in_buffer = ntty_chars_in_buffer,
  1598. .tiocmget = ntty_tiocmget,
  1599. .tiocmset = ntty_tiocmset,
  1600. .get_icount = ntty_tiocgicount,
  1601. .install = ntty_install,
  1602. .cleanup = ntty_cleanup,
  1603. };
  1604. /* Module initialization */
  1605. static struct pci_driver nozomi_driver = {
  1606. .name = NOZOMI_NAME,
  1607. .id_table = nozomi_pci_tbl,
  1608. .probe = nozomi_card_init,
  1609. .remove = nozomi_card_exit,
  1610. };
  1611. static __init int nozomi_init(void)
  1612. {
  1613. int ret;
  1614. printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
  1615. ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
  1616. if (!ntty_driver)
  1617. return -ENOMEM;
  1618. ntty_driver->driver_name = NOZOMI_NAME_TTY;
  1619. ntty_driver->name = "noz";
  1620. ntty_driver->major = 0;
  1621. ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1622. ntty_driver->subtype = SERIAL_TYPE_NORMAL;
  1623. ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1624. ntty_driver->init_termios = tty_std_termios;
  1625. ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
  1626. HUPCL | CLOCAL;
  1627. ntty_driver->init_termios.c_ispeed = 115200;
  1628. ntty_driver->init_termios.c_ospeed = 115200;
  1629. tty_set_operations(ntty_driver, &tty_ops);
  1630. ret = tty_register_driver(ntty_driver);
  1631. if (ret) {
  1632. printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
  1633. goto free_tty;
  1634. }
  1635. ret = pci_register_driver(&nozomi_driver);
  1636. if (ret) {
  1637. printk(KERN_ERR "Nozomi: can't register pci driver\n");
  1638. goto unr_tty;
  1639. }
  1640. return 0;
  1641. unr_tty:
  1642. tty_unregister_driver(ntty_driver);
  1643. free_tty:
  1644. put_tty_driver(ntty_driver);
  1645. return ret;
  1646. }
  1647. static __exit void nozomi_exit(void)
  1648. {
  1649. printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
  1650. pci_unregister_driver(&nozomi_driver);
  1651. tty_unregister_driver(ntty_driver);
  1652. put_tty_driver(ntty_driver);
  1653. }
  1654. module_init(nozomi_init);
  1655. module_exit(nozomi_exit);
  1656. module_param(debug, int, S_IRUGO | S_IWUSR);
  1657. MODULE_LICENSE("Dual BSD/GPL");
  1658. MODULE_DESCRIPTION(DRIVER_DESC);