nozomi.c 49 KB

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