mos7840.c 76 KB

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