mos7840.c 76 KB

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