mos7840.c 75 KB

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