mos7840.c 77 KB

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