mos7840.c 75 KB

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