mos7840.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * Clean ups from Moschip version and a few ioctl implementations by:
  17. * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/errno.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_driver.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/module.h>
  32. #include <linux/serial.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/serial.h>
  35. #include <linux/uaccess.h>
  36. /*
  37. * Version Information
  38. */
  39. #define DRIVER_VERSION "1.3.1"
  40. #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
  41. /*
  42. * 16C50 UART register defines
  43. */
  44. #define LCR_BITS_5 0x00 /* 5 bits/char */
  45. #define LCR_BITS_6 0x01 /* 6 bits/char */
  46. #define LCR_BITS_7 0x02 /* 7 bits/char */
  47. #define LCR_BITS_8 0x03 /* 8 bits/char */
  48. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  49. #define LCR_STOP_1 0x00 /* 1 stop bit */
  50. #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
  51. #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
  52. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  53. #define LCR_PAR_NONE 0x00 /* No parity */
  54. #define LCR_PAR_ODD 0x08 /* Odd parity */
  55. #define LCR_PAR_EVEN 0x18 /* Even parity */
  56. #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
  57. #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
  58. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  59. #define LCR_SET_BREAK 0x40 /* Set Break condition */
  60. #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
  61. #define MCR_DTR 0x01 /* Assert DTR */
  62. #define MCR_RTS 0x02 /* Assert RTS */
  63. #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
  64. #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
  65. #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
  66. #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
  67. #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
  68. #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
  69. #define MOS7840_MSR_RI 0x40 /* Current state of RI */
  70. #define MOS7840_MSR_CD 0x80 /* Current state of CD */
  71. /*
  72. * Defines used for sending commands to port
  73. */
  74. #define WAIT_FOR_EVER (HZ * 0) /* timeout urb is wait for ever */
  75. #define MOS_WDR_TIMEOUT (HZ * 5) /* default urb timeout */
  76. #define MOS_PORT1 0x0200
  77. #define MOS_PORT2 0x0300
  78. #define MOS_VENREG 0x0000
  79. #define MOS_MAX_PORT 0x02
  80. #define MOS_WRITE 0x0E
  81. #define MOS_READ 0x0D
  82. /* Requests */
  83. #define MCS_RD_RTYPE 0xC0
  84. #define MCS_WR_RTYPE 0x40
  85. #define MCS_RDREQ 0x0D
  86. #define MCS_WRREQ 0x0E
  87. #define MCS_CTRL_TIMEOUT 500
  88. #define VENDOR_READ_LENGTH (0x01)
  89. #define MAX_NAME_LEN 64
  90. #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
  91. #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
  92. /* For higher baud Rates use TIOCEXBAUD */
  93. #define TIOCEXBAUD 0x5462
  94. /* vendor id and device id defines */
  95. /* The native mos7840/7820 component */
  96. #define USB_VENDOR_ID_MOSCHIP 0x9710
  97. #define MOSCHIP_DEVICE_ID_7840 0x7840
  98. #define MOSCHIP_DEVICE_ID_7820 0x7820
  99. /* The native component can have its vendor/device id's overridden
  100. * in vendor-specific implementations. Such devices can be handled
  101. * by making a change here, in moschip_port_id_table, and in
  102. * moschip_id_table_combined
  103. */
  104. #define USB_VENDOR_ID_BANDB 0x0856
  105. #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
  106. #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
  107. /* Interrupt Routine Defines */
  108. #define SERIAL_IIR_RLS 0x06
  109. #define SERIAL_IIR_MS 0x00
  110. /*
  111. * Emulation of the bit mask on the LINE STATUS REGISTER.
  112. */
  113. #define SERIAL_LSR_DR 0x0001
  114. #define SERIAL_LSR_OE 0x0002
  115. #define SERIAL_LSR_PE 0x0004
  116. #define SERIAL_LSR_FE 0x0008
  117. #define SERIAL_LSR_BI 0x0010
  118. #define MOS_MSR_DELTA_CTS 0x10
  119. #define MOS_MSR_DELTA_DSR 0x20
  120. #define MOS_MSR_DELTA_RI 0x40
  121. #define MOS_MSR_DELTA_CD 0x80
  122. /* Serial Port register Address */
  123. #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
  124. #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
  125. #define LINE_CONTROL_REGISTER ((__u16)(0x03))
  126. #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
  127. #define LINE_STATUS_REGISTER ((__u16)(0x05))
  128. #define MODEM_STATUS_REGISTER ((__u16)(0x06))
  129. #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
  130. #define DIVISOR_LATCH_LSB ((__u16)(0x00))
  131. #define DIVISOR_LATCH_MSB ((__u16)(0x01))
  132. #define CLK_MULTI_REGISTER ((__u16)(0x02))
  133. #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
  134. #define SERIAL_LCR_DLAB ((__u16)(0x0080))
  135. /*
  136. * URB POOL related defines
  137. */
  138. #define NUM_URBS 16 /* URB Count */
  139. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  140. static struct usb_device_id moschip_port_id_table[] = {
  141. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
  142. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
  143. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
  144. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
  145. {} /* terminating entry */
  146. };
  147. static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
  148. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
  149. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
  150. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
  151. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
  152. {} /* terminating entry */
  153. };
  154. MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
  155. /* This structure holds all of the local port information */
  156. struct moschip_port {
  157. int port_num; /*Actual port number in the device(1,2,etc) */
  158. struct urb *write_urb; /* write URB for this port */
  159. struct urb *read_urb; /* read URB for this port */
  160. struct urb *int_urb;
  161. __u8 shadowLCR; /* last LCR value received */
  162. __u8 shadowMCR; /* last MCR value received */
  163. char open;
  164. char open_ports;
  165. char zombie;
  166. wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
  167. wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
  168. int delta_msr_cond;
  169. struct async_icount icount;
  170. struct usb_serial_port *port; /* loop back to the owner of this object */
  171. /* Offsets */
  172. __u8 SpRegOffset;
  173. __u8 ControlRegOffset;
  174. __u8 DcrRegOffset;
  175. /* for processing control URBS in interrupt context */
  176. struct urb *control_urb;
  177. struct usb_ctrlrequest *dr;
  178. char *ctrl_buf;
  179. int MsrLsr;
  180. spinlock_t pool_lock;
  181. struct urb *write_urb_pool[NUM_URBS];
  182. char busy[NUM_URBS];
  183. };
  184. static int debug;
  185. /*
  186. * mos7840_set_reg_sync
  187. * To set the Control register by calling usb_fill_control_urb function
  188. * by passing usb_sndctrlpipe function as parameter.
  189. */
  190. static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
  191. __u16 val)
  192. {
  193. struct usb_device *dev = port->serial->dev;
  194. val = val & 0x00ff;
  195. dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
  196. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  197. MCS_WR_RTYPE, val, reg, NULL, 0,
  198. MOS_WDR_TIMEOUT);
  199. }
  200. /*
  201. * mos7840_get_reg_sync
  202. * To set the Uart register by calling usb_fill_control_urb function by
  203. * passing usb_rcvctrlpipe function as parameter.
  204. */
  205. static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
  206. __u16 *val)
  207. {
  208. struct usb_device *dev = port->serial->dev;
  209. int ret = 0;
  210. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  211. MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
  212. MOS_WDR_TIMEOUT);
  213. dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val);
  214. *val = (*val) & 0x00ff;
  215. return ret;
  216. }
  217. /*
  218. * mos7840_set_uart_reg
  219. * To set the Uart register by calling usb_fill_control_urb function by
  220. * passing usb_sndctrlpipe function as parameter.
  221. */
  222. static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
  223. __u16 val)
  224. {
  225. struct usb_device *dev = port->serial->dev;
  226. val = val & 0x00ff;
  227. /* For the UART control registers, the application number need
  228. to be Or'ed */
  229. if (port->serial->num_ports == 4) {
  230. val |= (((__u16) port->number -
  231. (__u16) (port->serial->minor)) + 1) << 8;
  232. dbg("mos7840_set_uart_reg application number is %x\n", val);
  233. } else {
  234. if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
  235. val |= (((__u16) port->number -
  236. (__u16) (port->serial->minor)) + 1) << 8;
  237. dbg("mos7840_set_uart_reg application number is %x\n",
  238. val);
  239. } else {
  240. val |=
  241. (((__u16) port->number -
  242. (__u16) (port->serial->minor)) + 2) << 8;
  243. dbg("mos7840_set_uart_reg application number is %x\n",
  244. val);
  245. }
  246. }
  247. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  248. MCS_WR_RTYPE, val, reg, NULL, 0,
  249. MOS_WDR_TIMEOUT);
  250. }
  251. /*
  252. * mos7840_get_uart_reg
  253. * To set the Control register by calling usb_fill_control_urb function
  254. * by passing usb_rcvctrlpipe function as parameter.
  255. */
  256. static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
  257. __u16 *val)
  258. {
  259. struct usb_device *dev = port->serial->dev;
  260. int ret = 0;
  261. __u16 Wval;
  262. /* dbg("application number is %4x \n",
  263. (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
  264. /* Wval is same as application number */
  265. if (port->serial->num_ports == 4) {
  266. Wval =
  267. (((__u16) port->number - (__u16) (port->serial->minor)) +
  268. 1) << 8;
  269. dbg("mos7840_get_uart_reg application number is %x\n", Wval);
  270. } else {
  271. if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
  272. Wval = (((__u16) port->number -
  273. (__u16) (port->serial->minor)) + 1) << 8;
  274. dbg("mos7840_get_uart_reg application number is %x\n",
  275. Wval);
  276. } else {
  277. Wval = (((__u16) port->number -
  278. (__u16) (port->serial->minor)) + 2) << 8;
  279. dbg("mos7840_get_uart_reg application number is %x\n",
  280. Wval);
  281. }
  282. }
  283. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  284. MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
  285. MOS_WDR_TIMEOUT);
  286. *val = (*val) & 0x00ff;
  287. return ret;
  288. }
  289. static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
  290. {
  291. dbg("***************************************\n");
  292. dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
  293. dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset);
  294. dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset);
  295. dbg("***************************************\n");
  296. }
  297. /************************************************************************/
  298. /************************************************************************/
  299. /* I N T E R F A C E F U N C T I O N S */
  300. /* I N T E R F A C E F U N C T I O N S */
  301. /************************************************************************/
  302. /************************************************************************/
  303. static inline void mos7840_set_port_private(struct usb_serial_port *port,
  304. struct moschip_port *data)
  305. {
  306. usb_set_serial_port_data(port, (void *)data);
  307. }
  308. static inline struct moschip_port *mos7840_get_port_private(struct
  309. usb_serial_port
  310. *port)
  311. {
  312. return (struct moschip_port *)usb_get_serial_port_data(port);
  313. }
  314. static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
  315. {
  316. struct moschip_port *mos7840_port;
  317. struct async_icount *icount;
  318. mos7840_port = port;
  319. icount = &mos7840_port->icount;
  320. if (new_msr &
  321. (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
  322. MOS_MSR_DELTA_CD)) {
  323. icount = &mos7840_port->icount;
  324. /* update input line counters */
  325. if (new_msr & MOS_MSR_DELTA_CTS) {
  326. icount->cts++;
  327. smp_wmb();
  328. }
  329. if (new_msr & MOS_MSR_DELTA_DSR) {
  330. icount->dsr++;
  331. smp_wmb();
  332. }
  333. if (new_msr & MOS_MSR_DELTA_CD) {
  334. icount->dcd++;
  335. smp_wmb();
  336. }
  337. if (new_msr & MOS_MSR_DELTA_RI) {
  338. icount->rng++;
  339. smp_wmb();
  340. }
  341. }
  342. }
  343. static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
  344. {
  345. struct async_icount *icount;
  346. dbg("%s - %02x", __func__, new_lsr);
  347. if (new_lsr & SERIAL_LSR_BI) {
  348. /*
  349. * Parity and Framing errors only count if they
  350. * occur exclusive of a break being
  351. * received.
  352. */
  353. new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
  354. }
  355. /* update input line counters */
  356. icount = &port->icount;
  357. if (new_lsr & SERIAL_LSR_BI) {
  358. icount->brk++;
  359. smp_wmb();
  360. }
  361. if (new_lsr & SERIAL_LSR_OE) {
  362. icount->overrun++;
  363. smp_wmb();
  364. }
  365. if (new_lsr & SERIAL_LSR_PE) {
  366. icount->parity++;
  367. smp_wmb();
  368. }
  369. if (new_lsr & SERIAL_LSR_FE) {
  370. icount->frame++;
  371. smp_wmb();
  372. }
  373. }
  374. /************************************************************************/
  375. /************************************************************************/
  376. /* U S B C A L L B A C K F U N C T I O N S */
  377. /* U S B C A L L B A C K F U N C T I O N S */
  378. /************************************************************************/
  379. /************************************************************************/
  380. static void mos7840_control_callback(struct urb *urb)
  381. {
  382. unsigned char *data;
  383. struct moschip_port *mos7840_port;
  384. __u8 regval = 0x0;
  385. int result = 0;
  386. int status = urb->status;
  387. mos7840_port = urb->context;
  388. switch (status) {
  389. case 0:
  390. /* success */
  391. break;
  392. case -ECONNRESET:
  393. case -ENOENT:
  394. case -ESHUTDOWN:
  395. /* this urb is terminated, clean up */
  396. dbg("%s - urb shutting down with status: %d", __func__,
  397. status);
  398. return;
  399. default:
  400. dbg("%s - nonzero urb status received: %d", __func__,
  401. status);
  402. goto exit;
  403. }
  404. dbg("%s urb buffer size is %d\n", __func__, urb->actual_length);
  405. dbg("%s mos7840_port->MsrLsr is %d port %d\n", __func__,
  406. mos7840_port->MsrLsr, mos7840_port->port_num);
  407. data = urb->transfer_buffer;
  408. regval = (__u8) data[0];
  409. dbg("%s data is %x\n", __func__, regval);
  410. if (mos7840_port->MsrLsr == 0)
  411. mos7840_handle_new_msr(mos7840_port, regval);
  412. else if (mos7840_port->MsrLsr == 1)
  413. mos7840_handle_new_lsr(mos7840_port, regval);
  414. exit:
  415. spin_lock(&mos7840_port->pool_lock);
  416. if (!mos7840_port->zombie)
  417. result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
  418. spin_unlock(&mos7840_port->pool_lock);
  419. if (result) {
  420. dev_err(&urb->dev->dev,
  421. "%s - Error %d submitting interrupt urb\n",
  422. __func__, result);
  423. }
  424. }
  425. static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
  426. __u16 *val)
  427. {
  428. struct usb_device *dev = mcs->port->serial->dev;
  429. struct usb_ctrlrequest *dr = mcs->dr;
  430. unsigned char *buffer = mcs->ctrl_buf;
  431. int ret;
  432. dr->bRequestType = MCS_RD_RTYPE;
  433. dr->bRequest = MCS_RDREQ;
  434. dr->wValue = cpu_to_le16(Wval); /* 0 */
  435. dr->wIndex = cpu_to_le16(reg);
  436. dr->wLength = cpu_to_le16(2);
  437. usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
  438. (unsigned char *)dr, buffer, 2,
  439. mos7840_control_callback, mcs);
  440. mcs->control_urb->transfer_buffer_length = 2;
  441. ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
  442. return ret;
  443. }
  444. /*****************************************************************************
  445. * mos7840_interrupt_callback
  446. * this is the callback function for when we have received data on the
  447. * interrupt endpoint.
  448. *****************************************************************************/
  449. static void mos7840_interrupt_callback(struct urb *urb)
  450. {
  451. int result;
  452. int length;
  453. struct moschip_port *mos7840_port;
  454. struct usb_serial *serial;
  455. __u16 Data;
  456. unsigned char *data;
  457. __u8 sp[5], st;
  458. int i, rv = 0;
  459. __u16 wval, wreg = 0;
  460. int status = urb->status;
  461. dbg("%s", " : Entering\n");
  462. switch (status) {
  463. case 0:
  464. /* success */
  465. break;
  466. case -ECONNRESET:
  467. case -ENOENT:
  468. case -ESHUTDOWN:
  469. /* this urb is terminated, clean up */
  470. dbg("%s - urb shutting down with status: %d", __func__,
  471. status);
  472. return;
  473. default:
  474. dbg("%s - nonzero urb status received: %d", __func__,
  475. status);
  476. goto exit;
  477. }
  478. length = urb->actual_length;
  479. data = urb->transfer_buffer;
  480. serial = urb->context;
  481. /* Moschip get 5 bytes
  482. * Byte 1 IIR Port 1 (port.number is 0)
  483. * Byte 2 IIR Port 2 (port.number is 1)
  484. * Byte 3 IIR Port 3 (port.number is 2)
  485. * Byte 4 IIR Port 4 (port.number is 3)
  486. * Byte 5 FIFO status for both */
  487. if (length && length > 5) {
  488. dbg("%s \n", "Wrong data !!!");
  489. return;
  490. }
  491. sp[0] = (__u8) data[0];
  492. sp[1] = (__u8) data[1];
  493. sp[2] = (__u8) data[2];
  494. sp[3] = (__u8) data[3];
  495. st = (__u8) data[4];
  496. for (i = 0; i < serial->num_ports; i++) {
  497. mos7840_port = mos7840_get_port_private(serial->port[i]);
  498. wval =
  499. (((__u16) serial->port[i]->number -
  500. (__u16) (serial->minor)) + 1) << 8;
  501. if (mos7840_port->open) {
  502. if (sp[i] & 0x01) {
  503. dbg("SP%d No Interrupt !!!\n", i);
  504. } else {
  505. switch (sp[i] & 0x0f) {
  506. case SERIAL_IIR_RLS:
  507. dbg("Serial Port %d: Receiver status error or ", i);
  508. dbg("address bit detected in 9-bit mode\n");
  509. mos7840_port->MsrLsr = 1;
  510. wreg = LINE_STATUS_REGISTER;
  511. break;
  512. case SERIAL_IIR_MS:
  513. dbg("Serial Port %d: Modem status change\n", i);
  514. mos7840_port->MsrLsr = 0;
  515. wreg = MODEM_STATUS_REGISTER;
  516. break;
  517. }
  518. spin_lock(&mos7840_port->pool_lock);
  519. if (!mos7840_port->zombie) {
  520. rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
  521. } else {
  522. spin_unlock(&mos7840_port->pool_lock);
  523. return;
  524. }
  525. spin_unlock(&mos7840_port->pool_lock);
  526. }
  527. }
  528. }
  529. if (!(rv < 0))
  530. /* the completion handler for the control urb will resubmit */
  531. return;
  532. exit:
  533. result = usb_submit_urb(urb, GFP_ATOMIC);
  534. if (result) {
  535. dev_err(&urb->dev->dev,
  536. "%s - Error %d submitting interrupt urb\n",
  537. __func__, result);
  538. }
  539. }
  540. static int mos7840_port_paranoia_check(struct usb_serial_port *port,
  541. const char *function)
  542. {
  543. if (!port) {
  544. dbg("%s - port == NULL", function);
  545. return -1;
  546. }
  547. if (!port->serial) {
  548. dbg("%s - port->serial == NULL", function);
  549. return -1;
  550. }
  551. return 0;
  552. }
  553. /* Inline functions to check the sanity of a pointer that is passed to us */
  554. static int mos7840_serial_paranoia_check(struct usb_serial *serial,
  555. const char *function)
  556. {
  557. if (!serial) {
  558. dbg("%s - serial == NULL", function);
  559. return -1;
  560. }
  561. if (!serial->type) {
  562. dbg("%s - serial->type == NULL!", function);
  563. return -1;
  564. }
  565. return 0;
  566. }
  567. static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
  568. const char *function)
  569. {
  570. /* if no port was specified, or it fails a paranoia check */
  571. if (!port ||
  572. mos7840_port_paranoia_check(port, function) ||
  573. mos7840_serial_paranoia_check(port->serial, function)) {
  574. /* then say that we don't have a valid usb_serial thing,
  575. * which will end up genrating -ENODEV return values */
  576. return NULL;
  577. }
  578. return port->serial;
  579. }
  580. /*****************************************************************************
  581. * mos7840_bulk_in_callback
  582. * this is the callback function for when we have received data on the
  583. * bulk in endpoint.
  584. *****************************************************************************/
  585. static void mos7840_bulk_in_callback(struct urb *urb)
  586. {
  587. int retval;
  588. unsigned char *data;
  589. struct usb_serial *serial;
  590. struct usb_serial_port *port;
  591. struct moschip_port *mos7840_port;
  592. struct tty_struct *tty;
  593. int status = urb->status;
  594. if (status) {
  595. dbg("nonzero read bulk status received: %d", status);
  596. return;
  597. }
  598. mos7840_port = urb->context;
  599. if (!mos7840_port) {
  600. dbg("%s", "NULL mos7840_port pointer \n");
  601. return;
  602. }
  603. port = (struct usb_serial_port *)mos7840_port->port;
  604. if (mos7840_port_paranoia_check(port, __func__)) {
  605. dbg("%s", "Port Paranoia failed \n");
  606. return;
  607. }
  608. serial = mos7840_get_usb_serial(port, __func__);
  609. if (!serial) {
  610. dbg("%s\n", "Bad serial pointer ");
  611. return;
  612. }
  613. dbg("%s\n", "Entering... \n");
  614. data = urb->transfer_buffer;
  615. dbg("%s", "Entering ........... \n");
  616. if (urb->actual_length) {
  617. tty = mos7840_port->port->port.tty;
  618. if (tty) {
  619. tty_buffer_request_room(tty, urb->actual_length);
  620. tty_insert_flip_string(tty, data, urb->actual_length);
  621. dbg(" %s \n", data);
  622. tty_flip_buffer_push(tty);
  623. }
  624. mos7840_port->icount.rx += urb->actual_length;
  625. smp_wmb();
  626. dbg("mos7840_port->icount.rx is %d:\n",
  627. mos7840_port->icount.rx);
  628. }
  629. if (!mos7840_port->read_urb) {
  630. dbg("%s", "URB KILLED !!!\n");
  631. return;
  632. }
  633. mos7840_port->read_urb->dev = serial->dev;
  634. retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  635. if (retval) {
  636. dbg(" usb_submit_urb(read bulk) failed, retval = %d",
  637. retval);
  638. }
  639. }
  640. /*****************************************************************************
  641. * mos7840_bulk_out_data_callback
  642. * this is the callback function for when we have finished sending
  643. * serial data on the bulk out endpoint.
  644. *****************************************************************************/
  645. static void mos7840_bulk_out_data_callback(struct urb *urb)
  646. {
  647. struct moschip_port *mos7840_port;
  648. struct tty_struct *tty;
  649. int status = urb->status;
  650. int i;
  651. mos7840_port = urb->context;
  652. spin_lock(&mos7840_port->pool_lock);
  653. for (i = 0; i < NUM_URBS; i++) {
  654. if (urb == mos7840_port->write_urb_pool[i]) {
  655. mos7840_port->busy[i] = 0;
  656. break;
  657. }
  658. }
  659. spin_unlock(&mos7840_port->pool_lock);
  660. if (status) {
  661. dbg("nonzero write bulk status received:%d\n", status);
  662. return;
  663. }
  664. if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
  665. dbg("%s", "Port Paranoia failed \n");
  666. return;
  667. }
  668. dbg("%s \n", "Entering .........");
  669. tty = mos7840_port->port->port.tty;
  670. if (tty && mos7840_port->open)
  671. tty_wakeup(tty);
  672. }
  673. /************************************************************************/
  674. /* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */
  675. /************************************************************************/
  676. #ifdef MCSSerialProbe
  677. static int mos7840_serial_probe(struct usb_serial *serial,
  678. const struct usb_device_id *id)
  679. {
  680. /*need to implement the mode_reg reading and updating\
  681. structures usb_serial_ device_type\
  682. (i.e num_ports, num_bulkin,bulkout etc) */
  683. /* Also we can update the changes attach */
  684. return 1;
  685. }
  686. #endif
  687. /*****************************************************************************
  688. * mos7840_open
  689. * this function is called by the tty driver when a port is opened
  690. * If successful, we return 0
  691. * Otherwise we return a negative error number.
  692. *****************************************************************************/
  693. static int mos7840_open(struct tty_struct *tty,
  694. struct usb_serial_port *port, struct file *filp)
  695. {
  696. int response;
  697. int j;
  698. struct usb_serial *serial;
  699. struct urb *urb;
  700. __u16 Data;
  701. int status;
  702. struct moschip_port *mos7840_port;
  703. struct moschip_port *port0;
  704. if (mos7840_port_paranoia_check(port, __func__)) {
  705. dbg("%s", "Port Paranoia failed \n");
  706. return -ENODEV;
  707. }
  708. serial = port->serial;
  709. if (mos7840_serial_paranoia_check(serial, __func__)) {
  710. dbg("%s", "Serial Paranoia failed \n");
  711. return -ENODEV;
  712. }
  713. mos7840_port = mos7840_get_port_private(port);
  714. port0 = mos7840_get_port_private(serial->port[0]);
  715. if (mos7840_port == NULL || port0 == NULL)
  716. return -ENODEV;
  717. usb_clear_halt(serial->dev, port->write_urb->pipe);
  718. usb_clear_halt(serial->dev, port->read_urb->pipe);
  719. port0->open_ports++;
  720. /* Initialising the write urb pool */
  721. for (j = 0; j < NUM_URBS; ++j) {
  722. urb = usb_alloc_urb(0, GFP_KERNEL);
  723. mos7840_port->write_urb_pool[j] = urb;
  724. if (urb == NULL) {
  725. err("No more urbs???");
  726. continue;
  727. }
  728. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  729. GFP_KERNEL);
  730. if (!urb->transfer_buffer) {
  731. usb_free_urb(urb);
  732. mos7840_port->write_urb_pool[j] = NULL;
  733. err("%s-out of memory for urb buffers.", __func__);
  734. continue;
  735. }
  736. }
  737. /*****************************************************************************
  738. * Initialize MCS7840 -- Write Init values to corresponding Registers
  739. *
  740. * Register Index
  741. * 1 : IER
  742. * 2 : FCR
  743. * 3 : LCR
  744. * 4 : MCR
  745. *
  746. * 0x08 : SP1/2 Control Reg
  747. *****************************************************************************/
  748. /* NEED to check the following Block */
  749. Data = 0x0;
  750. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  751. if (status < 0) {
  752. dbg("Reading Spreg failed\n");
  753. return -1;
  754. }
  755. Data |= 0x80;
  756. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  757. if (status < 0) {
  758. dbg("writing Spreg failed\n");
  759. return -1;
  760. }
  761. Data &= ~0x80;
  762. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  763. if (status < 0) {
  764. dbg("writing Spreg failed\n");
  765. return -1;
  766. }
  767. /* End of block to be checked */
  768. Data = 0x0;
  769. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  770. &Data);
  771. if (status < 0) {
  772. dbg("Reading Controlreg failed\n");
  773. return -1;
  774. }
  775. Data |= 0x08; /* Driver done bit */
  776. Data |= 0x20; /* rx_disable */
  777. status = mos7840_set_reg_sync(port,
  778. mos7840_port->ControlRegOffset, Data);
  779. if (status < 0) {
  780. dbg("writing Controlreg failed\n");
  781. return -1;
  782. }
  783. /* do register settings here */
  784. /* Set all regs to the device default values. */
  785. /***********************************
  786. * First Disable all interrupts.
  787. ***********************************/
  788. Data = 0x00;
  789. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  790. if (status < 0) {
  791. dbg("disableing interrupts failed\n");
  792. return -1;
  793. }
  794. /* Set FIFO_CONTROL_REGISTER to the default value */
  795. Data = 0x00;
  796. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  797. if (status < 0) {
  798. dbg("Writing FIFO_CONTROL_REGISTER failed\n");
  799. return -1;
  800. }
  801. Data = 0xcf;
  802. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  803. if (status < 0) {
  804. dbg("Writing FIFO_CONTROL_REGISTER failed\n");
  805. return -1;
  806. }
  807. Data = 0x03;
  808. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  809. mos7840_port->shadowLCR = Data;
  810. Data = 0x0b;
  811. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  812. mos7840_port->shadowMCR = Data;
  813. Data = 0x00;
  814. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  815. mos7840_port->shadowLCR = Data;
  816. Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
  817. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  818. Data = 0x0c;
  819. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  820. Data = 0x0;
  821. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  822. Data = 0x00;
  823. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  824. Data = Data & ~SERIAL_LCR_DLAB;
  825. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  826. mos7840_port->shadowLCR = Data;
  827. /* clearing Bulkin and Bulkout Fifo */
  828. Data = 0x0;
  829. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  830. Data = Data | 0x0c;
  831. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  832. Data = Data & ~0x0c;
  833. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  834. /* Finally enable all interrupts */
  835. Data = 0x0c;
  836. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  837. /* clearing rx_disable */
  838. Data = 0x0;
  839. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  840. &Data);
  841. Data = Data & ~0x20;
  842. status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
  843. Data);
  844. /* rx_negate */
  845. Data = 0x0;
  846. status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
  847. &Data);
  848. Data = Data | 0x10;
  849. status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
  850. Data);
  851. /* force low_latency on so that our tty_push actually forces *
  852. * the data through,otherwise it is scheduled, and with *
  853. * high data rates (like with OHCI) data can get lost. */
  854. if (tty)
  855. tty->low_latency = 1;
  856. /* Check to see if we've set up our endpoint info yet *
  857. * (can't set it up in mos7840_startup as the structures *
  858. * were not set up at that time.) */
  859. if (port0->open_ports == 1) {
  860. if (serial->port[0]->interrupt_in_buffer == NULL) {
  861. /* set up interrupt urb */
  862. usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
  863. serial->dev,
  864. usb_rcvintpipe(serial->dev,
  865. serial->port[0]->interrupt_in_endpointAddress),
  866. serial->port[0]->interrupt_in_buffer,
  867. serial->port[0]->interrupt_in_urb->
  868. transfer_buffer_length,
  869. mos7840_interrupt_callback,
  870. serial,
  871. serial->port[0]->interrupt_in_urb->interval);
  872. /* start interrupt read for mos7840 *
  873. * will continue as long as mos7840 is connected */
  874. response =
  875. usb_submit_urb(serial->port[0]->interrupt_in_urb,
  876. GFP_KERNEL);
  877. if (response) {
  878. err("%s - Error %d submitting interrupt urb",
  879. __func__, response);
  880. }
  881. }
  882. }
  883. /* see if we've set up our endpoint info yet *
  884. * (can't set it up in mos7840_startup as the *
  885. * structures were not set up at that time.) */
  886. dbg("port number is %d \n", port->number);
  887. dbg("serial number is %d \n", port->serial->minor);
  888. dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
  889. dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
  890. dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
  891. dbg("port's number in the device is %d\n", mos7840_port->port_num);
  892. mos7840_port->read_urb = port->read_urb;
  893. /* set up our bulk in urb */
  894. usb_fill_bulk_urb(mos7840_port->read_urb,
  895. serial->dev,
  896. usb_rcvbulkpipe(serial->dev,
  897. port->bulk_in_endpointAddress),
  898. port->bulk_in_buffer,
  899. mos7840_port->read_urb->transfer_buffer_length,
  900. mos7840_bulk_in_callback, mos7840_port);
  901. dbg("mos7840_open: bulkin endpoint is %d\n",
  902. port->bulk_in_endpointAddress);
  903. response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
  904. if (response) {
  905. err("%s - Error %d submitting control urb", __func__,
  906. response);
  907. }
  908. /* initialize our wait queues */
  909. init_waitqueue_head(&mos7840_port->wait_chase);
  910. init_waitqueue_head(&mos7840_port->delta_msr_wait);
  911. /* initialize our icount structure */
  912. memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
  913. /* initialize our port settings */
  914. /* Must set to enable ints! */
  915. mos7840_port->shadowMCR = MCR_MASTER_IE;
  916. /* send a open port command */
  917. mos7840_port->open = 1;
  918. /* mos7840_change_port_settings(mos7840_port,old_termios); */
  919. mos7840_port->icount.tx = 0;
  920. mos7840_port->icount.rx = 0;
  921. dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n",
  922. serial, mos7840_port, port);
  923. return 0;
  924. }
  925. /*****************************************************************************
  926. * mos7840_chars_in_buffer
  927. * this function is called by the tty driver when it wants to know how many
  928. * bytes of data we currently have outstanding in the port (data that has
  929. * been written, but hasn't made it out the port yet)
  930. * If successful, we return the number of bytes left to be written in the
  931. * system,
  932. * Otherwise we return zero.
  933. *****************************************************************************/
  934. static int mos7840_chars_in_buffer(struct tty_struct *tty)
  935. {
  936. struct usb_serial_port *port = tty->driver_data;
  937. int i;
  938. int chars = 0;
  939. unsigned long flags;
  940. struct moschip_port *mos7840_port;
  941. dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
  942. if (mos7840_port_paranoia_check(port, __func__)) {
  943. dbg("%s", "Invalid port \n");
  944. return 0;
  945. }
  946. mos7840_port = mos7840_get_port_private(port);
  947. if (mos7840_port == NULL) {
  948. dbg("%s \n", "mos7840_break:leaving ...........");
  949. return 0;
  950. }
  951. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  952. for (i = 0; i < NUM_URBS; ++i)
  953. if (mos7840_port->busy[i])
  954. chars += URB_TRANSFER_BUFFER_SIZE;
  955. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  956. dbg("%s - returns %d", __func__, chars);
  957. return chars;
  958. }
  959. /************************************************************************
  960. *
  961. * mos7840_block_until_tx_empty
  962. *
  963. * This function will block the close until one of the following:
  964. * 1. TX count are 0
  965. * 2. The mos7840 has stopped
  966. * 3. A timeout of 3 seconds without activity has expired
  967. *
  968. ************************************************************************/
  969. static void mos7840_block_until_tx_empty(struct tty_struct *tty,
  970. struct moschip_port *mos7840_port)
  971. {
  972. int timeout = HZ / 10;
  973. int wait = 30;
  974. int count;
  975. while (1) {
  976. count = mos7840_chars_in_buffer(tty);
  977. /* Check for Buffer status */
  978. if (count <= 0)
  979. return;
  980. /* Block the thread for a while */
  981. interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
  982. timeout);
  983. /* No activity.. count down section */
  984. wait--;
  985. if (wait == 0) {
  986. dbg("%s - TIMEOUT", __func__);
  987. return;
  988. } else {
  989. /* Reset timeout value back to seconds */
  990. wait = 30;
  991. }
  992. }
  993. }
  994. /*****************************************************************************
  995. * mos7840_close
  996. * this function is called by the tty driver when a port is closed
  997. *****************************************************************************/
  998. static void mos7840_close(struct tty_struct *tty,
  999. struct usb_serial_port *port, struct file *filp)
  1000. {
  1001. struct usb_serial *serial;
  1002. struct moschip_port *mos7840_port;
  1003. struct moschip_port *port0;
  1004. int j;
  1005. __u16 Data;
  1006. dbg("%s\n", "mos7840_close:entering...");
  1007. if (mos7840_port_paranoia_check(port, __func__)) {
  1008. dbg("%s", "Port Paranoia failed \n");
  1009. return;
  1010. }
  1011. serial = mos7840_get_usb_serial(port, __func__);
  1012. if (!serial) {
  1013. dbg("%s", "Serial Paranoia failed \n");
  1014. return;
  1015. }
  1016. mos7840_port = mos7840_get_port_private(port);
  1017. port0 = mos7840_get_port_private(serial->port[0]);
  1018. if (mos7840_port == NULL || port0 == NULL)
  1019. return;
  1020. for (j = 0; j < NUM_URBS; ++j)
  1021. usb_kill_urb(mos7840_port->write_urb_pool[j]);
  1022. /* Freeing Write URBs */
  1023. for (j = 0; j < NUM_URBS; ++j) {
  1024. if (mos7840_port->write_urb_pool[j]) {
  1025. if (mos7840_port->write_urb_pool[j]->transfer_buffer)
  1026. kfree(mos7840_port->write_urb_pool[j]->
  1027. transfer_buffer);
  1028. usb_free_urb(mos7840_port->write_urb_pool[j]);
  1029. }
  1030. }
  1031. if (serial->dev)
  1032. /* flush and block until tx is empty */
  1033. mos7840_block_until_tx_empty(tty, mos7840_port);
  1034. /* While closing port, shutdown all bulk read, write *
  1035. * and interrupt read if they exists */
  1036. if (serial->dev) {
  1037. if (mos7840_port->write_urb) {
  1038. dbg("%s", "Shutdown bulk write\n");
  1039. usb_kill_urb(mos7840_port->write_urb);
  1040. }
  1041. if (mos7840_port->read_urb) {
  1042. dbg("%s", "Shutdown bulk read\n");
  1043. usb_kill_urb(mos7840_port->read_urb);
  1044. }
  1045. if ((&mos7840_port->control_urb)) {
  1046. dbg("%s", "Shutdown control read\n");
  1047. /*/ usb_kill_urb (mos7840_port->control_urb); */
  1048. }
  1049. }
  1050. /* if(mos7840_port->ctrl_buf != NULL) */
  1051. /* kfree(mos7840_port->ctrl_buf); */
  1052. port0->open_ports--;
  1053. dbg("mos7840_num_open_ports in close%d:in port%d\n",
  1054. port0->open_ports, port->number);
  1055. if (port0->open_ports == 0) {
  1056. if (serial->port[0]->interrupt_in_urb) {
  1057. dbg("%s", "Shutdown interrupt_in_urb\n");
  1058. usb_kill_urb(serial->port[0]->interrupt_in_urb);
  1059. }
  1060. }
  1061. if (mos7840_port->write_urb) {
  1062. /* if this urb had a transfer buffer already (old tx) free it */
  1063. if (mos7840_port->write_urb->transfer_buffer != NULL)
  1064. kfree(mos7840_port->write_urb->transfer_buffer);
  1065. usb_free_urb(mos7840_port->write_urb);
  1066. }
  1067. Data = 0x0;
  1068. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1069. Data = 0x00;
  1070. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1071. mos7840_port->open = 0;
  1072. dbg("%s \n", "Leaving ............");
  1073. }
  1074. /************************************************************************
  1075. *
  1076. * mos7840_block_until_chase_response
  1077. *
  1078. * This function will block the close until one of the following:
  1079. * 1. Response to our Chase comes from mos7840
  1080. * 2. A timeout of 10 seconds without activity has expired
  1081. * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
  1082. *
  1083. ************************************************************************/
  1084. static void mos7840_block_until_chase_response(struct tty_struct *tty,
  1085. struct moschip_port *mos7840_port)
  1086. {
  1087. int timeout = 1 * HZ;
  1088. int wait = 10;
  1089. int count;
  1090. while (1) {
  1091. count = mos7840_chars_in_buffer(tty);
  1092. /* Check for Buffer status */
  1093. if (count <= 0)
  1094. return;
  1095. /* Block the thread for a while */
  1096. interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
  1097. timeout);
  1098. /* No activity.. count down section */
  1099. wait--;
  1100. if (wait == 0) {
  1101. dbg("%s - TIMEOUT", __func__);
  1102. return;
  1103. } else {
  1104. /* Reset timeout value back to seconds */
  1105. wait = 10;
  1106. }
  1107. }
  1108. }
  1109. /*****************************************************************************
  1110. * mos7840_break
  1111. * this function sends a break to the port
  1112. *****************************************************************************/
  1113. static void mos7840_break(struct tty_struct *tty, int break_state)
  1114. {
  1115. struct usb_serial_port *port = tty->driver_data;
  1116. unsigned char data;
  1117. struct usb_serial *serial;
  1118. struct moschip_port *mos7840_port;
  1119. dbg("%s \n", "Entering ...........");
  1120. dbg("mos7840_break: Start\n");
  1121. if (mos7840_port_paranoia_check(port, __func__)) {
  1122. dbg("%s", "Port Paranoia failed \n");
  1123. return;
  1124. }
  1125. serial = mos7840_get_usb_serial(port, __func__);
  1126. if (!serial) {
  1127. dbg("%s", "Serial Paranoia failed \n");
  1128. return;
  1129. }
  1130. mos7840_port = mos7840_get_port_private(port);
  1131. if (mos7840_port == NULL)
  1132. return;
  1133. if (serial->dev)
  1134. /* flush and block until tx is empty */
  1135. mos7840_block_until_chase_response(tty, mos7840_port);
  1136. if (break_state == -1)
  1137. data = mos7840_port->shadowLCR | LCR_SET_BREAK;
  1138. else
  1139. data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
  1140. mos7840_port->shadowLCR = data;
  1141. dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
  1142. mos7840_port->shadowLCR);
  1143. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
  1144. mos7840_port->shadowLCR);
  1145. return;
  1146. }
  1147. /*****************************************************************************
  1148. * mos7840_write_room
  1149. * this function is called by the tty driver when it wants to know how many
  1150. * bytes of data we can accept for a specific port.
  1151. * If successful, we return the amount of room that we have for this port
  1152. * Otherwise we return a negative error number.
  1153. *****************************************************************************/
  1154. static int mos7840_write_room(struct tty_struct *tty)
  1155. {
  1156. struct usb_serial_port *port = tty->driver_data;
  1157. int i;
  1158. int room = 0;
  1159. unsigned long flags;
  1160. struct moschip_port *mos7840_port;
  1161. dbg("%s \n", " mos7840_write_room:entering ...........");
  1162. if (mos7840_port_paranoia_check(port, __func__)) {
  1163. dbg("%s", "Invalid port \n");
  1164. dbg("%s \n", " mos7840_write_room:leaving ...........");
  1165. return -1;
  1166. }
  1167. mos7840_port = mos7840_get_port_private(port);
  1168. if (mos7840_port == NULL) {
  1169. dbg("%s \n", "mos7840_break:leaving ...........");
  1170. return -1;
  1171. }
  1172. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  1173. for (i = 0; i < NUM_URBS; ++i) {
  1174. if (!mos7840_port->busy[i])
  1175. room += URB_TRANSFER_BUFFER_SIZE;
  1176. }
  1177. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  1178. room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
  1179. dbg("%s - returns %d", __func__, room);
  1180. return room;
  1181. }
  1182. /*****************************************************************************
  1183. * mos7840_write
  1184. * this function is called by the tty driver when data should be written to
  1185. * the port.
  1186. * If successful, we return the number of bytes written, otherwise we
  1187. * return a negative error number.
  1188. *****************************************************************************/
  1189. static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
  1190. const unsigned char *data, int count)
  1191. {
  1192. int status;
  1193. int i;
  1194. int bytes_sent = 0;
  1195. int transfer_size;
  1196. unsigned long flags;
  1197. struct moschip_port *mos7840_port;
  1198. struct usb_serial *serial;
  1199. struct urb *urb;
  1200. /* __u16 Data; */
  1201. const unsigned char *current_position = data;
  1202. unsigned char *data1;
  1203. dbg("%s \n", "entering ...........");
  1204. /* dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
  1205. mos7840_port->shadowLCR); */
  1206. #ifdef NOTMOS7840
  1207. Data = 0x00;
  1208. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  1209. mos7840_port->shadowLCR = Data;
  1210. dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
  1211. dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
  1212. mos7840_port->shadowLCR);
  1213. /* Data = 0x03; */
  1214. /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
  1215. /* mos7840_port->shadowLCR=Data;//Need to add later */
  1216. Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
  1217. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1218. /* Data = 0x0c; */
  1219. /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
  1220. Data = 0x00;
  1221. status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
  1222. dbg("mos7840_write:DLL value is %x\n", Data);
  1223. Data = 0x0;
  1224. status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
  1225. dbg("mos7840_write:DLM value is %x\n", Data);
  1226. Data = Data & ~SERIAL_LCR_DLAB;
  1227. dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
  1228. mos7840_port->shadowLCR);
  1229. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1230. #endif
  1231. if (mos7840_port_paranoia_check(port, __func__)) {
  1232. dbg("%s", "Port Paranoia failed \n");
  1233. return -1;
  1234. }
  1235. serial = port->serial;
  1236. if (mos7840_serial_paranoia_check(serial, __func__)) {
  1237. dbg("%s", "Serial Paranoia failed \n");
  1238. return -1;
  1239. }
  1240. mos7840_port = mos7840_get_port_private(port);
  1241. if (mos7840_port == NULL) {
  1242. dbg("%s", "mos7840_port is NULL\n");
  1243. return -1;
  1244. }
  1245. /* try to find a free urb in the list */
  1246. urb = NULL;
  1247. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  1248. for (i = 0; i < NUM_URBS; ++i) {
  1249. if (!mos7840_port->busy[i]) {
  1250. mos7840_port->busy[i] = 1;
  1251. urb = mos7840_port->write_urb_pool[i];
  1252. dbg("\nURB:%d", i);
  1253. break;
  1254. }
  1255. }
  1256. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  1257. if (urb == NULL) {
  1258. dbg("%s - no more free urbs", __func__);
  1259. goto exit;
  1260. }
  1261. if (urb->transfer_buffer == NULL) {
  1262. urb->transfer_buffer =
  1263. kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
  1264. if (urb->transfer_buffer == NULL) {
  1265. err("%s no more kernel memory...", __func__);
  1266. goto exit;
  1267. }
  1268. }
  1269. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  1270. memcpy(urb->transfer_buffer, current_position, transfer_size);
  1271. /* fill urb with data and submit */
  1272. usb_fill_bulk_urb(urb,
  1273. serial->dev,
  1274. usb_sndbulkpipe(serial->dev,
  1275. port->bulk_out_endpointAddress),
  1276. urb->transfer_buffer,
  1277. transfer_size,
  1278. mos7840_bulk_out_data_callback, mos7840_port);
  1279. data1 = urb->transfer_buffer;
  1280. dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
  1281. /* send it down the pipe */
  1282. status = usb_submit_urb(urb, GFP_ATOMIC);
  1283. if (status) {
  1284. mos7840_port->busy[i] = 0;
  1285. err("%s - usb_submit_urb(write bulk) failed with status = %d",
  1286. __func__, status);
  1287. bytes_sent = status;
  1288. goto exit;
  1289. }
  1290. bytes_sent = transfer_size;
  1291. mos7840_port->icount.tx += transfer_size;
  1292. smp_wmb();
  1293. dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
  1294. exit:
  1295. return bytes_sent;
  1296. }
  1297. /*****************************************************************************
  1298. * mos7840_throttle
  1299. * this function is called by the tty driver when it wants to stop the data
  1300. * being read from the port.
  1301. *****************************************************************************/
  1302. static void mos7840_throttle(struct tty_struct *tty)
  1303. {
  1304. struct usb_serial_port *port = tty->driver_data;
  1305. struct moschip_port *mos7840_port;
  1306. int status;
  1307. if (mos7840_port_paranoia_check(port, __func__)) {
  1308. dbg("%s", "Invalid port \n");
  1309. return;
  1310. }
  1311. dbg("- port %d\n", port->number);
  1312. mos7840_port = mos7840_get_port_private(port);
  1313. if (mos7840_port == NULL)
  1314. return;
  1315. if (!mos7840_port->open) {
  1316. dbg("%s\n", "port not opened");
  1317. return;
  1318. }
  1319. dbg("%s", "Entering .......... \n");
  1320. /* if we are implementing XON/XOFF, send the stop character */
  1321. if (I_IXOFF(tty)) {
  1322. unsigned char stop_char = STOP_CHAR(tty);
  1323. status = mos7840_write(tty, port, &stop_char, 1);
  1324. if (status <= 0)
  1325. return;
  1326. }
  1327. /* if we are implementing RTS/CTS, toggle that line */
  1328. if (tty->termios->c_cflag & CRTSCTS) {
  1329. mos7840_port->shadowMCR &= ~MCR_RTS;
  1330. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1331. mos7840_port->shadowMCR);
  1332. if (status < 0)
  1333. return;
  1334. }
  1335. return;
  1336. }
  1337. /*****************************************************************************
  1338. * mos7840_unthrottle
  1339. * this function is called by the tty driver when it wants to resume
  1340. * the data being read from the port (called after mos7840_throttle is
  1341. * called)
  1342. *****************************************************************************/
  1343. static void mos7840_unthrottle(struct tty_struct *tty)
  1344. {
  1345. struct usb_serial_port *port = tty->driver_data;
  1346. int status;
  1347. struct moschip_port *mos7840_port = mos7840_get_port_private(port);
  1348. if (mos7840_port_paranoia_check(port, __func__)) {
  1349. dbg("%s", "Invalid port \n");
  1350. return;
  1351. }
  1352. if (mos7840_port == NULL)
  1353. return;
  1354. if (!mos7840_port->open) {
  1355. dbg("%s - port not opened", __func__);
  1356. return;
  1357. }
  1358. dbg("%s", "Entering .......... \n");
  1359. /* if we are implementing XON/XOFF, send the start character */
  1360. if (I_IXOFF(tty)) {
  1361. unsigned char start_char = START_CHAR(tty);
  1362. status = mos7840_write(tty, port, &start_char, 1);
  1363. if (status <= 0)
  1364. return;
  1365. }
  1366. /* if we are implementing RTS/CTS, toggle that line */
  1367. if (tty->termios->c_cflag & CRTSCTS) {
  1368. mos7840_port->shadowMCR |= MCR_RTS;
  1369. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1370. mos7840_port->shadowMCR);
  1371. if (status < 0)
  1372. return;
  1373. }
  1374. }
  1375. static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
  1376. {
  1377. struct usb_serial_port *port = tty->driver_data;
  1378. struct moschip_port *mos7840_port;
  1379. unsigned int result;
  1380. __u16 msr;
  1381. __u16 mcr;
  1382. int status;
  1383. mos7840_port = mos7840_get_port_private(port);
  1384. dbg("%s - port %d", __func__, port->number);
  1385. if (mos7840_port == NULL)
  1386. return -ENODEV;
  1387. status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
  1388. status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
  1389. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
  1390. | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
  1391. | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
  1392. | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
  1393. | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
  1394. | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
  1395. | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
  1396. dbg("%s - 0x%04X", __func__, result);
  1397. return result;
  1398. }
  1399. static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
  1400. unsigned int set, unsigned int clear)
  1401. {
  1402. struct usb_serial_port *port = tty->driver_data;
  1403. struct moschip_port *mos7840_port;
  1404. unsigned int mcr;
  1405. int status;
  1406. dbg("%s - port %d", __func__, port->number);
  1407. mos7840_port = mos7840_get_port_private(port);
  1408. if (mos7840_port == NULL)
  1409. return -ENODEV;
  1410. /* FIXME: What locks the port registers ? */
  1411. mcr = mos7840_port->shadowMCR;
  1412. if (clear & TIOCM_RTS)
  1413. mcr &= ~MCR_RTS;
  1414. if (clear & TIOCM_DTR)
  1415. mcr &= ~MCR_DTR;
  1416. if (clear & TIOCM_LOOP)
  1417. mcr &= ~MCR_LOOPBACK;
  1418. if (set & TIOCM_RTS)
  1419. mcr |= MCR_RTS;
  1420. if (set & TIOCM_DTR)
  1421. mcr |= MCR_DTR;
  1422. if (set & TIOCM_LOOP)
  1423. mcr |= MCR_LOOPBACK;
  1424. mos7840_port->shadowMCR = mcr;
  1425. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
  1426. if (status < 0) {
  1427. dbg("setting MODEM_CONTROL_REGISTER Failed\n");
  1428. return status;
  1429. }
  1430. return 0;
  1431. }
  1432. /*****************************************************************************
  1433. * mos7840_calc_baud_rate_divisor
  1434. * this function calculates the proper baud rate divisor for the specified
  1435. * baud rate.
  1436. *****************************************************************************/
  1437. static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
  1438. __u16 *clk_sel_val)
  1439. {
  1440. dbg("%s - %d", __func__, baudRate);
  1441. if (baudRate <= 115200) {
  1442. *divisor = 115200 / baudRate;
  1443. *clk_sel_val = 0x0;
  1444. }
  1445. if ((baudRate > 115200) && (baudRate <= 230400)) {
  1446. *divisor = 230400 / baudRate;
  1447. *clk_sel_val = 0x10;
  1448. } else if ((baudRate > 230400) && (baudRate <= 403200)) {
  1449. *divisor = 403200 / baudRate;
  1450. *clk_sel_val = 0x20;
  1451. } else if ((baudRate > 403200) && (baudRate <= 460800)) {
  1452. *divisor = 460800 / baudRate;
  1453. *clk_sel_val = 0x30;
  1454. } else if ((baudRate > 460800) && (baudRate <= 806400)) {
  1455. *divisor = 806400 / baudRate;
  1456. *clk_sel_val = 0x40;
  1457. } else if ((baudRate > 806400) && (baudRate <= 921600)) {
  1458. *divisor = 921600 / baudRate;
  1459. *clk_sel_val = 0x50;
  1460. } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
  1461. *divisor = 1572864 / baudRate;
  1462. *clk_sel_val = 0x60;
  1463. } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
  1464. *divisor = 3145728 / baudRate;
  1465. *clk_sel_val = 0x70;
  1466. }
  1467. return 0;
  1468. #ifdef NOTMCS7840
  1469. for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
  1470. if (mos7840_divisor_table[i].BaudRate == baudrate) {
  1471. *divisor = mos7840_divisor_table[i].Divisor;
  1472. return 0;
  1473. }
  1474. }
  1475. /* After trying for all the standard baud rates *
  1476. * Try calculating the divisor for this baud rate */
  1477. if (baudrate > 75 && baudrate < 230400) {
  1478. /* get the divisor */
  1479. custom = (__u16) (230400L / baudrate);
  1480. /* Check for round off */
  1481. round1 = (__u16) (2304000L / baudrate);
  1482. round = (__u16) (round1 - (custom * 10));
  1483. if (round > 4)
  1484. custom++;
  1485. *divisor = custom;
  1486. dbg(" Baud %d = %d\n", baudrate, custom);
  1487. return 0;
  1488. }
  1489. dbg("%s\n", " Baud calculation Failed...");
  1490. return -1;
  1491. #endif
  1492. }
  1493. /*****************************************************************************
  1494. * mos7840_send_cmd_write_baud_rate
  1495. * this function sends the proper command to change the baud rate of the
  1496. * specified port.
  1497. *****************************************************************************/
  1498. static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
  1499. int baudRate)
  1500. {
  1501. int divisor = 0;
  1502. int status;
  1503. __u16 Data;
  1504. unsigned char number;
  1505. __u16 clk_sel_val;
  1506. struct usb_serial_port *port;
  1507. if (mos7840_port == NULL)
  1508. return -1;
  1509. port = (struct usb_serial_port *)mos7840_port->port;
  1510. if (mos7840_port_paranoia_check(port, __func__)) {
  1511. dbg("%s", "Invalid port \n");
  1512. return -1;
  1513. }
  1514. if (mos7840_serial_paranoia_check(port->serial, __func__)) {
  1515. dbg("%s", "Invalid Serial \n");
  1516. return -1;
  1517. }
  1518. dbg("%s", "Entering .......... \n");
  1519. number = mos7840_port->port->number - mos7840_port->port->serial->minor;
  1520. dbg("%s - port = %d, baud = %d", __func__,
  1521. mos7840_port->port->number, baudRate);
  1522. /* reset clk_uart_sel in spregOffset */
  1523. if (baudRate > 115200) {
  1524. #ifdef HW_flow_control
  1525. /* NOTE: need to see the pther register to modify */
  1526. /* setting h/w flow control bit to 1 */
  1527. Data = 0x2b;
  1528. mos7840_port->shadowMCR = Data;
  1529. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1530. Data);
  1531. if (status < 0) {
  1532. dbg("Writing spreg failed in set_serial_baud\n");
  1533. return -1;
  1534. }
  1535. #endif
  1536. } else {
  1537. #ifdef HW_flow_control
  1538. / *setting h/w flow control bit to 0 */
  1539. Data = 0xb;
  1540. mos7840_port->shadowMCR = Data;
  1541. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1542. Data);
  1543. if (status < 0) {
  1544. dbg("Writing spreg failed in set_serial_baud\n");
  1545. return -1;
  1546. }
  1547. #endif
  1548. }
  1549. if (1) { /* baudRate <= 115200) */
  1550. clk_sel_val = 0x0;
  1551. Data = 0x0;
  1552. status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
  1553. &clk_sel_val);
  1554. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
  1555. &Data);
  1556. if (status < 0) {
  1557. dbg("reading spreg failed in set_serial_baud\n");
  1558. return -1;
  1559. }
  1560. Data = (Data & 0x8f) | clk_sel_val;
  1561. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
  1562. Data);
  1563. if (status < 0) {
  1564. dbg("Writing spreg failed in set_serial_baud\n");
  1565. return -1;
  1566. }
  1567. /* Calculate the Divisor */
  1568. if (status) {
  1569. err("%s - bad baud rate", __func__);
  1570. dbg("%s\n", "bad baud rate");
  1571. return status;
  1572. }
  1573. /* Enable access to divisor latch */
  1574. Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
  1575. mos7840_port->shadowLCR = Data;
  1576. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1577. /* Write the divisor */
  1578. Data = (unsigned char)(divisor & 0xff);
  1579. dbg("set_serial_baud Value to write DLL is %x\n", Data);
  1580. mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  1581. Data = (unsigned char)((divisor & 0xff00) >> 8);
  1582. dbg("set_serial_baud Value to write DLM is %x\n", Data);
  1583. mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  1584. /* Disable access to divisor latch */
  1585. Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
  1586. mos7840_port->shadowLCR = Data;
  1587. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1588. }
  1589. return status;
  1590. }
  1591. /*****************************************************************************
  1592. * mos7840_change_port_settings
  1593. * This routine is called to set the UART on the device to match
  1594. * the specified new settings.
  1595. *****************************************************************************/
  1596. static void mos7840_change_port_settings(struct tty_struct *tty,
  1597. struct moschip_port *mos7840_port, struct ktermios *old_termios)
  1598. {
  1599. int baud;
  1600. unsigned cflag;
  1601. unsigned iflag;
  1602. __u8 lData;
  1603. __u8 lParity;
  1604. __u8 lStop;
  1605. int status;
  1606. __u16 Data;
  1607. struct usb_serial_port *port;
  1608. struct usb_serial *serial;
  1609. if (mos7840_port == NULL)
  1610. return;
  1611. port = (struct usb_serial_port *)mos7840_port->port;
  1612. if (mos7840_port_paranoia_check(port, __func__)) {
  1613. dbg("%s", "Invalid port \n");
  1614. return;
  1615. }
  1616. if (mos7840_serial_paranoia_check(port->serial, __func__)) {
  1617. dbg("%s", "Invalid Serial \n");
  1618. return;
  1619. }
  1620. serial = port->serial;
  1621. dbg("%s - port %d", __func__, mos7840_port->port->number);
  1622. if (!mos7840_port->open) {
  1623. dbg("%s - port not opened", __func__);
  1624. return;
  1625. }
  1626. dbg("%s", "Entering .......... \n");
  1627. lData = LCR_BITS_8;
  1628. lStop = LCR_STOP_1;
  1629. lParity = LCR_PAR_NONE;
  1630. cflag = tty->termios->c_cflag;
  1631. iflag = tty->termios->c_iflag;
  1632. /* Change the number of bits */
  1633. if (cflag & CSIZE) {
  1634. switch (cflag & CSIZE) {
  1635. case CS5:
  1636. lData = LCR_BITS_5;
  1637. break;
  1638. case CS6:
  1639. lData = LCR_BITS_6;
  1640. break;
  1641. case CS7:
  1642. lData = LCR_BITS_7;
  1643. break;
  1644. default:
  1645. case CS8:
  1646. lData = LCR_BITS_8;
  1647. break;
  1648. }
  1649. }
  1650. /* Change the Parity bit */
  1651. if (cflag & PARENB) {
  1652. if (cflag & PARODD) {
  1653. lParity = LCR_PAR_ODD;
  1654. dbg("%s - parity = odd", __func__);
  1655. } else {
  1656. lParity = LCR_PAR_EVEN;
  1657. dbg("%s - parity = even", __func__);
  1658. }
  1659. } else {
  1660. dbg("%s - parity = none", __func__);
  1661. }
  1662. if (cflag & CMSPAR)
  1663. lParity = lParity | 0x20;
  1664. /* Change the Stop bit */
  1665. if (cflag & CSTOPB) {
  1666. lStop = LCR_STOP_2;
  1667. dbg("%s - stop bits = 2", __func__);
  1668. } else {
  1669. lStop = LCR_STOP_1;
  1670. dbg("%s - stop bits = 1", __func__);
  1671. }
  1672. /* Update the LCR with the correct value */
  1673. mos7840_port->shadowLCR &=
  1674. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1675. mos7840_port->shadowLCR |= (lData | lParity | lStop);
  1676. dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
  1677. mos7840_port->shadowLCR);
  1678. /* Disable Interrupts */
  1679. Data = 0x00;
  1680. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1681. Data = 0x00;
  1682. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1683. Data = 0xcf;
  1684. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1685. /* Send the updated LCR value to the mos7840 */
  1686. Data = mos7840_port->shadowLCR;
  1687. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1688. Data = 0x00b;
  1689. mos7840_port->shadowMCR = Data;
  1690. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1691. Data = 0x00b;
  1692. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1693. /* set up the MCR register and send it to the mos7840 */
  1694. mos7840_port->shadowMCR = MCR_MASTER_IE;
  1695. if (cflag & CBAUD)
  1696. mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
  1697. if (cflag & CRTSCTS)
  1698. mos7840_port->shadowMCR |= (MCR_XON_ANY);
  1699. else
  1700. mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
  1701. Data = mos7840_port->shadowMCR;
  1702. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1703. /* Determine divisor based on baud rate */
  1704. baud = tty_get_baud_rate(tty);
  1705. if (!baud) {
  1706. /* pick a default, any default... */
  1707. dbg("%s\n", "Picked default baud...");
  1708. baud = 9600;
  1709. }
  1710. dbg("%s - baud rate = %d", __func__, baud);
  1711. status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
  1712. /* Enable Interrupts */
  1713. Data = 0x0c;
  1714. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1715. if (mos7840_port->read_urb->status != -EINPROGRESS) {
  1716. mos7840_port->read_urb->dev = serial->dev;
  1717. status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  1718. if (status) {
  1719. dbg(" usb_submit_urb(read bulk) failed, status = %d",
  1720. status);
  1721. }
  1722. }
  1723. wake_up(&mos7840_port->delta_msr_wait);
  1724. mos7840_port->delta_msr_cond = 1;
  1725. dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
  1726. mos7840_port->shadowLCR);
  1727. return;
  1728. }
  1729. /*****************************************************************************
  1730. * mos7840_set_termios
  1731. * this function is called by the tty driver when it wants to change
  1732. * the termios structure
  1733. *****************************************************************************/
  1734. static void mos7840_set_termios(struct tty_struct *tty,
  1735. struct usb_serial_port *port,
  1736. struct ktermios *old_termios)
  1737. {
  1738. int status;
  1739. unsigned int cflag;
  1740. struct usb_serial *serial;
  1741. struct moschip_port *mos7840_port;
  1742. dbg("mos7840_set_termios: START\n");
  1743. if (mos7840_port_paranoia_check(port, __func__)) {
  1744. dbg("%s", "Invalid port \n");
  1745. return;
  1746. }
  1747. serial = port->serial;
  1748. if (mos7840_serial_paranoia_check(serial, __func__)) {
  1749. dbg("%s", "Invalid Serial \n");
  1750. return;
  1751. }
  1752. mos7840_port = mos7840_get_port_private(port);
  1753. if (mos7840_port == NULL)
  1754. return;
  1755. if (!mos7840_port->open) {
  1756. dbg("%s - port not opened", __func__);
  1757. return;
  1758. }
  1759. dbg("%s\n", "setting termios - ");
  1760. cflag = tty->termios->c_cflag;
  1761. dbg("%s - clfag %08x iflag %08x", __func__,
  1762. tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
  1763. dbg("%s - old clfag %08x old iflag %08x", __func__,
  1764. old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
  1765. dbg("%s - port %d", __func__, port->number);
  1766. /* change the port settings to the new ones specified */
  1767. mos7840_change_port_settings(tty, mos7840_port, old_termios);
  1768. if (!mos7840_port->read_urb) {
  1769. dbg("%s", "URB KILLED !!!!!\n");
  1770. return;
  1771. }
  1772. if (mos7840_port->read_urb->status != -EINPROGRESS) {
  1773. mos7840_port->read_urb->dev = serial->dev;
  1774. status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  1775. if (status) {
  1776. dbg(" usb_submit_urb(read bulk) failed, status = %d",
  1777. status);
  1778. }
  1779. }
  1780. return;
  1781. }
  1782. /*****************************************************************************
  1783. * mos7840_get_lsr_info - get line status register info
  1784. *
  1785. * Purpose: Let user call ioctl() to get info when the UART physically
  1786. * is emptied. On bus types like RS485, the transmitter must
  1787. * release the bus after transmitting. This must be done when
  1788. * the transmit shift register is empty, not be done when the
  1789. * transmit holding register is empty. This functionality
  1790. * allows an RS485 driver to be written in user space.
  1791. *****************************************************************************/
  1792. static int mos7840_get_lsr_info(struct tty_struct *tty,
  1793. unsigned int __user *value)
  1794. {
  1795. int count;
  1796. unsigned int result = 0;
  1797. count = mos7840_chars_in_buffer(tty);
  1798. if (count == 0) {
  1799. dbg("%s -- Empty", __func__);
  1800. result = TIOCSER_TEMT;
  1801. }
  1802. if (copy_to_user(value, &result, sizeof(int)))
  1803. return -EFAULT;
  1804. return 0;
  1805. }
  1806. /*****************************************************************************
  1807. * mos7840_set_modem_info
  1808. * function to set modem info
  1809. *****************************************************************************/
  1810. /* FIXME: Should be using the model control hooks */
  1811. static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
  1812. unsigned int cmd, unsigned int __user *value)
  1813. {
  1814. unsigned int mcr;
  1815. unsigned int arg;
  1816. __u16 Data;
  1817. int status;
  1818. struct usb_serial_port *port;
  1819. if (mos7840_port == NULL)
  1820. return -1;
  1821. port = (struct usb_serial_port *)mos7840_port->port;
  1822. if (mos7840_port_paranoia_check(port, __func__)) {
  1823. dbg("%s", "Invalid port \n");
  1824. return -1;
  1825. }
  1826. mcr = mos7840_port->shadowMCR;
  1827. if (copy_from_user(&arg, value, sizeof(int)))
  1828. return -EFAULT;
  1829. switch (cmd) {
  1830. case TIOCMBIS:
  1831. if (arg & TIOCM_RTS)
  1832. mcr |= MCR_RTS;
  1833. if (arg & TIOCM_DTR)
  1834. mcr |= MCR_RTS;
  1835. if (arg & TIOCM_LOOP)
  1836. mcr |= MCR_LOOPBACK;
  1837. break;
  1838. case TIOCMBIC:
  1839. if (arg & TIOCM_RTS)
  1840. mcr &= ~MCR_RTS;
  1841. if (arg & TIOCM_DTR)
  1842. mcr &= ~MCR_RTS;
  1843. if (arg & TIOCM_LOOP)
  1844. mcr &= ~MCR_LOOPBACK;
  1845. break;
  1846. case TIOCMSET:
  1847. /* turn off the RTS and DTR and LOOPBACK
  1848. * and then only turn on what was asked to */
  1849. mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
  1850. mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
  1851. mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
  1852. mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
  1853. break;
  1854. }
  1855. mos7840_port->shadowMCR = mcr;
  1856. Data = mos7840_port->shadowMCR;
  1857. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1858. if (status < 0) {
  1859. dbg("setting MODEM_CONTROL_REGISTER Failed\n");
  1860. return -1;
  1861. }
  1862. return 0;
  1863. }
  1864. /*****************************************************************************
  1865. * mos7840_get_modem_info
  1866. * function to get modem info
  1867. *****************************************************************************/
  1868. static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
  1869. unsigned int __user *value)
  1870. {
  1871. unsigned int result = 0;
  1872. __u16 msr;
  1873. unsigned int mcr = mos7840_port->shadowMCR;
  1874. mos7840_get_uart_reg(mos7840_port->port,
  1875. MODEM_STATUS_REGISTER, &msr);
  1876. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1877. |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1878. |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1879. |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
  1880. |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1881. |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1882. dbg("%s -- %x", __func__, result);
  1883. if (copy_to_user(value, &result, sizeof(int)))
  1884. return -EFAULT;
  1885. return 0;
  1886. }
  1887. /*****************************************************************************
  1888. * mos7840_get_serial_info
  1889. * function to get information about serial port
  1890. *****************************************************************************/
  1891. static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
  1892. struct serial_struct __user *retinfo)
  1893. {
  1894. struct serial_struct tmp;
  1895. if (mos7840_port == NULL)
  1896. return -1;
  1897. if (!retinfo)
  1898. return -EFAULT;
  1899. memset(&tmp, 0, sizeof(tmp));
  1900. tmp.type = PORT_16550A;
  1901. tmp.line = mos7840_port->port->serial->minor;
  1902. tmp.port = mos7840_port->port->number;
  1903. tmp.irq = 0;
  1904. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1905. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1906. tmp.baud_base = 9600;
  1907. tmp.close_delay = 5 * HZ;
  1908. tmp.closing_wait = 30 * HZ;
  1909. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1910. return -EFAULT;
  1911. return 0;
  1912. }
  1913. /*****************************************************************************
  1914. * SerialIoctl
  1915. * this function handles any ioctl calls to the driver
  1916. *****************************************************************************/
  1917. static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
  1918. unsigned int cmd, unsigned long arg)
  1919. {
  1920. struct usb_serial_port *port = tty->driver_data;
  1921. void __user *argp = (void __user *)arg;
  1922. struct moschip_port *mos7840_port;
  1923. struct async_icount cnow;
  1924. struct async_icount cprev;
  1925. struct serial_icounter_struct icount;
  1926. int mosret = 0;
  1927. if (mos7840_port_paranoia_check(port, __func__)) {
  1928. dbg("%s", "Invalid port \n");
  1929. return -1;
  1930. }
  1931. mos7840_port = mos7840_get_port_private(port);
  1932. if (mos7840_port == NULL)
  1933. return -1;
  1934. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1935. switch (cmd) {
  1936. /* return number of bytes available */
  1937. case TIOCSERGETLSR:
  1938. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1939. return mos7840_get_lsr_info(tty, argp);
  1940. return 0;
  1941. /* FIXME: use the modem hooks and remove this */
  1942. case TIOCMBIS:
  1943. case TIOCMBIC:
  1944. case TIOCMSET:
  1945. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
  1946. port->number);
  1947. mosret =
  1948. mos7840_set_modem_info(mos7840_port, cmd, argp);
  1949. return mosret;
  1950. case TIOCMGET:
  1951. dbg("%s (%d) TIOCMGET", __func__, port->number);
  1952. return mos7840_get_modem_info(mos7840_port, argp);
  1953. case TIOCGSERIAL:
  1954. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1955. return mos7840_get_serial_info(mos7840_port, argp);
  1956. case TIOCSSERIAL:
  1957. dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
  1958. break;
  1959. case TIOCMIWAIT:
  1960. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1961. cprev = mos7840_port->icount;
  1962. while (1) {
  1963. /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
  1964. mos7840_port->delta_msr_cond = 0;
  1965. wait_event_interruptible(mos7840_port->delta_msr_wait,
  1966. (mos7840_port->
  1967. delta_msr_cond == 1));
  1968. /* see if a signal did it */
  1969. if (signal_pending(current))
  1970. return -ERESTARTSYS;
  1971. cnow = mos7840_port->icount;
  1972. smp_rmb();
  1973. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1974. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1975. return -EIO; /* no change => error */
  1976. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1977. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1978. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1979. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1980. return 0;
  1981. }
  1982. cprev = cnow;
  1983. }
  1984. /* NOTREACHED */
  1985. break;
  1986. case TIOCGICOUNT:
  1987. cnow = mos7840_port->icount;
  1988. smp_rmb();
  1989. icount.cts = cnow.cts;
  1990. icount.dsr = cnow.dsr;
  1991. icount.rng = cnow.rng;
  1992. icount.dcd = cnow.dcd;
  1993. icount.rx = cnow.rx;
  1994. icount.tx = cnow.tx;
  1995. icount.frame = cnow.frame;
  1996. icount.overrun = cnow.overrun;
  1997. icount.parity = cnow.parity;
  1998. icount.brk = cnow.brk;
  1999. icount.buf_overrun = cnow.buf_overrun;
  2000. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  2001. port->number, icount.rx, icount.tx);
  2002. if (copy_to_user(argp, &icount, sizeof(icount)))
  2003. return -EFAULT;
  2004. return 0;
  2005. default:
  2006. break;
  2007. }
  2008. return -ENOIOCTLCMD;
  2009. }
  2010. static int mos7840_calc_num_ports(struct usb_serial *serial)
  2011. {
  2012. int mos7840_num_ports = 0;
  2013. dbg("numberofendpoints: %d \n",
  2014. (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
  2015. dbg("numberofendpoints: %d \n",
  2016. (int)serial->interface->altsetting->desc.bNumEndpoints);
  2017. if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
  2018. mos7840_num_ports = serial->num_ports = 2;
  2019. } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
  2020. serial->num_bulk_in = 4;
  2021. serial->num_bulk_out = 4;
  2022. mos7840_num_ports = serial->num_ports = 4;
  2023. }
  2024. return mos7840_num_ports;
  2025. }
  2026. /****************************************************************************
  2027. * mos7840_startup
  2028. ****************************************************************************/
  2029. static int mos7840_startup(struct usb_serial *serial)
  2030. {
  2031. struct moschip_port *mos7840_port;
  2032. struct usb_device *dev;
  2033. int i, status;
  2034. __u16 Data;
  2035. dbg("%s \n", " mos7840_startup :entering..........");
  2036. if (!serial) {
  2037. dbg("%s\n", "Invalid Handler");
  2038. return -1;
  2039. }
  2040. dev = serial->dev;
  2041. dbg("%s\n", "Entering...");
  2042. /* we set up the pointers to the endpoints in the mos7840_open *
  2043. * function, as the structures aren't created yet. */
  2044. /* set up port private structures */
  2045. for (i = 0; i < serial->num_ports; ++i) {
  2046. mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  2047. if (mos7840_port == NULL) {
  2048. err("%s - Out of memory", __func__);
  2049. status = -ENOMEM;
  2050. i--; /* don't follow NULL pointer cleaning up */
  2051. goto error;
  2052. }
  2053. /* Initialize all port interrupt end point to port 0 int
  2054. * endpoint. Our device has only one interrupt end point
  2055. * common to all port */
  2056. mos7840_port->port = serial->port[i];
  2057. mos7840_set_port_private(serial->port[i], mos7840_port);
  2058. spin_lock_init(&mos7840_port->pool_lock);
  2059. mos7840_port->port_num = ((serial->port[i]->number -
  2060. (serial->port[i]->serial->minor)) +
  2061. 1);
  2062. if (mos7840_port->port_num == 1) {
  2063. mos7840_port->SpRegOffset = 0x0;
  2064. mos7840_port->ControlRegOffset = 0x1;
  2065. mos7840_port->DcrRegOffset = 0x4;
  2066. } else if ((mos7840_port->port_num == 2)
  2067. && (serial->num_ports == 4)) {
  2068. mos7840_port->SpRegOffset = 0x8;
  2069. mos7840_port->ControlRegOffset = 0x9;
  2070. mos7840_port->DcrRegOffset = 0x16;
  2071. } else if ((mos7840_port->port_num == 2)
  2072. && (serial->num_ports == 2)) {
  2073. mos7840_port->SpRegOffset = 0xa;
  2074. mos7840_port->ControlRegOffset = 0xb;
  2075. mos7840_port->DcrRegOffset = 0x19;
  2076. } else if ((mos7840_port->port_num == 3)
  2077. && (serial->num_ports == 4)) {
  2078. mos7840_port->SpRegOffset = 0xa;
  2079. mos7840_port->ControlRegOffset = 0xb;
  2080. mos7840_port->DcrRegOffset = 0x19;
  2081. } else if ((mos7840_port->port_num == 4)
  2082. && (serial->num_ports == 4)) {
  2083. mos7840_port->SpRegOffset = 0xc;
  2084. mos7840_port->ControlRegOffset = 0xd;
  2085. mos7840_port->DcrRegOffset = 0x1c;
  2086. }
  2087. mos7840_dump_serial_port(mos7840_port);
  2088. mos7840_set_port_private(serial->port[i], mos7840_port);
  2089. /* enable rx_disable bit in control register */
  2090. status = mos7840_get_reg_sync(serial->port[i],
  2091. mos7840_port->ControlRegOffset, &Data);
  2092. if (status < 0) {
  2093. dbg("Reading ControlReg failed status-0x%x\n", status);
  2094. break;
  2095. } else
  2096. dbg("ControlReg Reading success val is %x, status%d\n",
  2097. Data, status);
  2098. Data |= 0x08; /* setting driver done bit */
  2099. Data |= 0x04; /* sp1_bit to have cts change reflect in
  2100. modem status reg */
  2101. /* Data |= 0x20; //rx_disable bit */
  2102. status = mos7840_set_reg_sync(serial->port[i],
  2103. mos7840_port->ControlRegOffset, Data);
  2104. if (status < 0) {
  2105. dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
  2106. break;
  2107. } else
  2108. dbg("ControlReg Writing success(rx_disable) status%d\n",
  2109. status);
  2110. /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
  2111. and 0x24 in DCR3 */
  2112. Data = 0x01;
  2113. status = mos7840_set_reg_sync(serial->port[i],
  2114. (__u16) (mos7840_port->DcrRegOffset + 0), Data);
  2115. if (status < 0) {
  2116. dbg("Writing DCR0 failed status-0x%x\n", status);
  2117. break;
  2118. } else
  2119. dbg("DCR0 Writing success status%d\n", status);
  2120. Data = 0x05;
  2121. status = mos7840_set_reg_sync(serial->port[i],
  2122. (__u16) (mos7840_port->DcrRegOffset + 1), Data);
  2123. if (status < 0) {
  2124. dbg("Writing DCR1 failed status-0x%x\n", status);
  2125. break;
  2126. } else
  2127. dbg("DCR1 Writing success status%d\n", status);
  2128. Data = 0x24;
  2129. status = mos7840_set_reg_sync(serial->port[i],
  2130. (__u16) (mos7840_port->DcrRegOffset + 2), Data);
  2131. if (status < 0) {
  2132. dbg("Writing DCR2 failed status-0x%x\n", status);
  2133. break;
  2134. } else
  2135. dbg("DCR2 Writing success status%d\n", status);
  2136. /* write values in clkstart0x0 and clkmulti 0x20 */
  2137. Data = 0x0;
  2138. status = mos7840_set_reg_sync(serial->port[i],
  2139. CLK_START_VALUE_REGISTER, Data);
  2140. if (status < 0) {
  2141. dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
  2142. break;
  2143. } else
  2144. dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
  2145. Data = 0x20;
  2146. status = mos7840_set_reg_sync(serial->port[i],
  2147. CLK_MULTI_REGISTER, Data);
  2148. if (status < 0) {
  2149. dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
  2150. status);
  2151. goto error;
  2152. } else
  2153. dbg("CLK_MULTI_REGISTER Writing success status%d\n",
  2154. status);
  2155. /* write value 0x0 to scratchpad register */
  2156. Data = 0x00;
  2157. status = mos7840_set_uart_reg(serial->port[i],
  2158. SCRATCH_PAD_REGISTER, Data);
  2159. if (status < 0) {
  2160. dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
  2161. status);
  2162. break;
  2163. } else
  2164. dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
  2165. status);
  2166. /* Zero Length flag register */
  2167. if ((mos7840_port->port_num != 1)
  2168. && (serial->num_ports == 2)) {
  2169. Data = 0xff;
  2170. status = mos7840_set_reg_sync(serial->port[i],
  2171. (__u16) (ZLP_REG1 +
  2172. ((__u16)mos7840_port->port_num)), Data);
  2173. dbg("ZLIP offset%x\n",
  2174. (__u16) (ZLP_REG1 +
  2175. ((__u16) mos7840_port->port_num)));
  2176. if (status < 0) {
  2177. dbg("Writing ZLP_REG%d failed status-0x%x\n",
  2178. i + 2, status);
  2179. break;
  2180. } else
  2181. dbg("ZLP_REG%d Writing success status%d\n",
  2182. i + 2, status);
  2183. } else {
  2184. Data = 0xff;
  2185. status = mos7840_set_reg_sync(serial->port[i],
  2186. (__u16) (ZLP_REG1 +
  2187. ((__u16)mos7840_port->port_num) - 0x1), Data);
  2188. dbg("ZLIP offset%x\n",
  2189. (__u16) (ZLP_REG1 +
  2190. ((__u16) mos7840_port->port_num) - 0x1));
  2191. if (status < 0) {
  2192. dbg("Writing ZLP_REG%d failed status-0x%x\n",
  2193. i + 1, status);
  2194. break;
  2195. } else
  2196. dbg("ZLP_REG%d Writing success status%d\n",
  2197. i + 1, status);
  2198. }
  2199. mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
  2200. mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
  2201. mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
  2202. GFP_KERNEL);
  2203. if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
  2204. !mos7840_port->dr) {
  2205. status = -ENOMEM;
  2206. goto error;
  2207. }
  2208. }
  2209. /* Zero Length flag enable */
  2210. Data = 0x0f;
  2211. status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
  2212. if (status < 0) {
  2213. dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
  2214. goto error;
  2215. } else
  2216. dbg("ZLP_REG5 Writing success status%d\n", status);
  2217. /* setting configuration feature to one */
  2218. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  2219. (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
  2220. return 0;
  2221. error:
  2222. for (/* nothing */; i >= 0; i--) {
  2223. mos7840_port = mos7840_get_port_private(serial->port[i]);
  2224. kfree(mos7840_port->dr);
  2225. kfree(mos7840_port->ctrl_buf);
  2226. usb_free_urb(mos7840_port->control_urb);
  2227. kfree(mos7840_port);
  2228. serial->port[i] = NULL;
  2229. }
  2230. return status;
  2231. }
  2232. /****************************************************************************
  2233. * mos7840_shutdown
  2234. * This function is called whenever the device is removed from the usb bus.
  2235. ****************************************************************************/
  2236. static void mos7840_shutdown(struct usb_serial *serial)
  2237. {
  2238. int i;
  2239. unsigned long flags;
  2240. struct moschip_port *mos7840_port;
  2241. dbg("%s \n", " shutdown :entering..........");
  2242. if (!serial) {
  2243. dbg("%s", "Invalid Handler \n");
  2244. return;
  2245. }
  2246. /* check for the ports to be closed,close the ports and disconnect */
  2247. /* free private structure allocated for serial port *
  2248. * stop reads and writes on all ports */
  2249. for (i = 0; i < serial->num_ports; ++i) {
  2250. mos7840_port = mos7840_get_port_private(serial->port[i]);
  2251. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  2252. mos7840_port->zombie = 1;
  2253. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  2254. usb_kill_urb(mos7840_port->control_urb);
  2255. kfree(mos7840_port->ctrl_buf);
  2256. kfree(mos7840_port->dr);
  2257. kfree(mos7840_port);
  2258. mos7840_set_port_private(serial->port[i], NULL);
  2259. }
  2260. dbg("%s\n", "Thank u :: ");
  2261. }
  2262. static struct usb_driver io_driver = {
  2263. .name = "mos7840",
  2264. .probe = usb_serial_probe,
  2265. .disconnect = usb_serial_disconnect,
  2266. .id_table = moschip_id_table_combined,
  2267. .no_dynamic_id = 1,
  2268. };
  2269. static struct usb_serial_driver moschip7840_4port_device = {
  2270. .driver = {
  2271. .owner = THIS_MODULE,
  2272. .name = "mos7840",
  2273. },
  2274. .description = DRIVER_DESC,
  2275. .usb_driver = &io_driver,
  2276. .id_table = moschip_port_id_table,
  2277. .num_ports = 4,
  2278. .open = mos7840_open,
  2279. .close = mos7840_close,
  2280. .write = mos7840_write,
  2281. .write_room = mos7840_write_room,
  2282. .chars_in_buffer = mos7840_chars_in_buffer,
  2283. .throttle = mos7840_throttle,
  2284. .unthrottle = mos7840_unthrottle,
  2285. .calc_num_ports = mos7840_calc_num_ports,
  2286. #ifdef MCSSerialProbe
  2287. .probe = mos7840_serial_probe,
  2288. #endif
  2289. .ioctl = mos7840_ioctl,
  2290. .set_termios = mos7840_set_termios,
  2291. .break_ctl = mos7840_break,
  2292. .tiocmget = mos7840_tiocmget,
  2293. .tiocmset = mos7840_tiocmset,
  2294. .attach = mos7840_startup,
  2295. .shutdown = mos7840_shutdown,
  2296. .read_bulk_callback = mos7840_bulk_in_callback,
  2297. .read_int_callback = mos7840_interrupt_callback,
  2298. };
  2299. /****************************************************************************
  2300. * moschip7840_init
  2301. * This is called by the module subsystem, or on startup to initialize us
  2302. ****************************************************************************/
  2303. static int __init moschip7840_init(void)
  2304. {
  2305. int retval;
  2306. dbg("%s \n", " mos7840_init :entering..........");
  2307. /* Register with the usb serial */
  2308. retval = usb_serial_register(&moschip7840_4port_device);
  2309. if (retval)
  2310. goto failed_port_device_register;
  2311. dbg("%s\n", "Entring...");
  2312. info(DRIVER_DESC " " DRIVER_VERSION);
  2313. /* Register with the usb */
  2314. retval = usb_register(&io_driver);
  2315. if (retval == 0) {
  2316. dbg("%s\n", "Leaving...");
  2317. return 0;
  2318. }
  2319. usb_serial_deregister(&moschip7840_4port_device);
  2320. failed_port_device_register:
  2321. return retval;
  2322. }
  2323. /****************************************************************************
  2324. * moschip7840_exit
  2325. * Called when the driver is about to be unloaded.
  2326. ****************************************************************************/
  2327. static void __exit moschip7840_exit(void)
  2328. {
  2329. dbg("%s \n", " mos7840_exit :entering..........");
  2330. usb_deregister(&io_driver);
  2331. usb_serial_deregister(&moschip7840_4port_device);
  2332. dbg("%s\n", "Entring...");
  2333. }
  2334. module_init(moschip7840_init);
  2335. module_exit(moschip7840_exit);
  2336. /* Module information */
  2337. MODULE_DESCRIPTION(DRIVER_DESC);
  2338. MODULE_LICENSE("GPL");
  2339. module_param(debug, bool, S_IRUGO | S_IWUSR);
  2340. MODULE_PARM_DESC(debug, "Debug enabled or not");