nozomi.c 49 KB

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