mos7840.c 76 KB

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