mos7720.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  1. /*
  2. * mos7720.c
  3. * Controls the Moschip 7720 usb to dual port serial convertor
  4. *
  5. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. *
  11. * Developed by:
  12. * Vijaya Kumar <vijaykumar.gn@gmail.com>
  13. * Ajay Kumar <naanuajay@yahoo.com>
  14. * Gurudeva <ngurudeva@yahoo.com>
  15. *
  16. * Cleaned up from the original by:
  17. * Greg Kroah-Hartman <gregkh@suse.de>
  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. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/module.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/serial.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/parport.h>
  38. /*
  39. * Version Information
  40. */
  41. #define DRIVER_VERSION "2.1"
  42. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  43. #define DRIVER_DESC "Moschip USB Serial Driver"
  44. /* default urb timeout */
  45. #define MOS_WDR_TIMEOUT (HZ * 5)
  46. #define MOS_MAX_PORT 0x02
  47. #define MOS_WRITE 0x0E
  48. #define MOS_READ 0x0D
  49. /* Interrupt Rotinue Defines */
  50. #define SERIAL_IIR_RLS 0x06
  51. #define SERIAL_IIR_RDA 0x04
  52. #define SERIAL_IIR_CTI 0x0c
  53. #define SERIAL_IIR_THR 0x02
  54. #define SERIAL_IIR_MS 0x00
  55. #define NUM_URBS 16 /* URB Count */
  56. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  57. /* This structure holds all of the local serial port information */
  58. struct moschip_port {
  59. __u8 shadowLCR; /* last LCR value received */
  60. __u8 shadowMCR; /* last MCR value received */
  61. __u8 shadowMSR; /* last MSR value received */
  62. char open;
  63. struct async_icount icount;
  64. struct usb_serial_port *port; /* loop back to the owner */
  65. struct urb *write_urb_pool[NUM_URBS];
  66. };
  67. static bool debug;
  68. static struct usb_serial_driver moschip7720_2port_driver;
  69. #define USB_VENDOR_ID_MOSCHIP 0x9710
  70. #define MOSCHIP_DEVICE_ID_7720 0x7720
  71. #define MOSCHIP_DEVICE_ID_7715 0x7715
  72. static const struct usb_device_id id_table[] = {
  73. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
  74. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
  75. { } /* terminating entry */
  76. };
  77. MODULE_DEVICE_TABLE(usb, id_table);
  78. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  79. /* initial values for parport regs */
  80. #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
  81. #define ECR_INIT_VAL 0x00 /* SPP mode */
  82. struct urbtracker {
  83. struct mos7715_parport *mos_parport;
  84. struct list_head urblist_entry;
  85. struct kref ref_count;
  86. struct urb *urb;
  87. };
  88. enum mos7715_pp_modes {
  89. SPP = 0<<5,
  90. PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
  91. PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
  92. };
  93. struct mos7715_parport {
  94. struct parport *pp; /* back to containing struct */
  95. struct kref ref_count; /* to instance of this struct */
  96. struct list_head deferred_urbs; /* list deferred async urbs */
  97. struct list_head active_urbs; /* list async urbs in flight */
  98. spinlock_t listlock; /* protects list access */
  99. bool msg_pending; /* usb sync call pending */
  100. struct completion syncmsg_compl; /* usb sync call completed */
  101. struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
  102. struct usb_serial *serial; /* back to containing struct */
  103. __u8 shadowECR; /* parallel port regs... */
  104. __u8 shadowDCR;
  105. atomic_t shadowDSR; /* updated in int-in callback */
  106. };
  107. /* lock guards against dereferencing NULL ptr in parport ops callbacks */
  108. static DEFINE_SPINLOCK(release_lock);
  109. #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
  110. static const unsigned int dummy; /* for clarity in register access fns */
  111. enum mos_regs {
  112. THR, /* serial port regs */
  113. RHR,
  114. IER,
  115. FCR,
  116. ISR,
  117. LCR,
  118. MCR,
  119. LSR,
  120. MSR,
  121. SPR,
  122. DLL,
  123. DLM,
  124. DPR, /* parallel port regs */
  125. DSR,
  126. DCR,
  127. ECR,
  128. SP1_REG, /* device control regs */
  129. SP2_REG, /* serial port 2 (7720 only) */
  130. PP_REG,
  131. SP_CONTROL_REG,
  132. };
  133. /*
  134. * Return the correct value for the Windex field of the setup packet
  135. * for a control endpoint message. See the 7715 datasheet.
  136. */
  137. static inline __u16 get_reg_index(enum mos_regs reg)
  138. {
  139. static const __u16 mos7715_index_lookup_table[] = {
  140. 0x00, /* THR */
  141. 0x00, /* RHR */
  142. 0x01, /* IER */
  143. 0x02, /* FCR */
  144. 0x02, /* ISR */
  145. 0x03, /* LCR */
  146. 0x04, /* MCR */
  147. 0x05, /* LSR */
  148. 0x06, /* MSR */
  149. 0x07, /* SPR */
  150. 0x00, /* DLL */
  151. 0x01, /* DLM */
  152. 0x00, /* DPR */
  153. 0x01, /* DSR */
  154. 0x02, /* DCR */
  155. 0x0a, /* ECR */
  156. 0x01, /* SP1_REG */
  157. 0x02, /* SP2_REG (7720 only) */
  158. 0x04, /* PP_REG (7715 only) */
  159. 0x08, /* SP_CONTROL_REG */
  160. };
  161. return mos7715_index_lookup_table[reg];
  162. }
  163. /*
  164. * Return the correct value for the upper byte of the Wvalue field of
  165. * the setup packet for a control endpoint message.
  166. */
  167. static inline __u16 get_reg_value(enum mos_regs reg,
  168. unsigned int serial_portnum)
  169. {
  170. if (reg >= SP1_REG) /* control reg */
  171. return 0x0000;
  172. else if (reg >= DPR) /* parallel port reg (7715 only) */
  173. return 0x0100;
  174. else /* serial port reg */
  175. return (serial_portnum + 2) << 8;
  176. }
  177. /*
  178. * Write data byte to the specified device register. The data is embedded in
  179. * the value field of the setup packet. serial_portnum is ignored for registers
  180. * not specific to a particular serial port.
  181. */
  182. static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
  183. enum mos_regs reg, __u8 data)
  184. {
  185. struct usb_device *usbdev = serial->dev;
  186. unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
  187. __u8 request = (__u8)0x0e;
  188. __u8 requesttype = (__u8)0x40;
  189. __u16 index = get_reg_index(reg);
  190. __u16 value = get_reg_value(reg, serial_portnum) + data;
  191. int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
  192. index, NULL, 0, MOS_WDR_TIMEOUT);
  193. if (status < 0)
  194. dev_err(&usbdev->dev,
  195. "mos7720: usb_control_msg() failed: %d", status);
  196. return status;
  197. }
  198. /*
  199. * Read data byte from the specified device register. The data returned by the
  200. * device is embedded in the value field of the setup packet. serial_portnum is
  201. * ignored for registers that are not specific to a particular serial port.
  202. */
  203. static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
  204. enum mos_regs reg, __u8 *data)
  205. {
  206. struct usb_device *usbdev = serial->dev;
  207. unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
  208. __u8 request = (__u8)0x0d;
  209. __u8 requesttype = (__u8)0xc0;
  210. __u16 index = get_reg_index(reg);
  211. __u16 value = get_reg_value(reg, serial_portnum);
  212. int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
  213. index, data, 1, MOS_WDR_TIMEOUT);
  214. if (status < 0)
  215. dev_err(&usbdev->dev,
  216. "mos7720: usb_control_msg() failed: %d", status);
  217. return status;
  218. }
  219. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  220. static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
  221. enum mos7715_pp_modes mode)
  222. {
  223. mos_parport->shadowECR = mode;
  224. write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
  225. return 0;
  226. }
  227. static void destroy_mos_parport(struct kref *kref)
  228. {
  229. struct mos7715_parport *mos_parport =
  230. container_of(kref, struct mos7715_parport, ref_count);
  231. kfree(mos_parport);
  232. }
  233. static void destroy_urbtracker(struct kref *kref)
  234. {
  235. struct urbtracker *urbtrack =
  236. container_of(kref, struct urbtracker, ref_count);
  237. struct mos7715_parport *mos_parport = urbtrack->mos_parport;
  238. usb_free_urb(urbtrack->urb);
  239. kfree(urbtrack);
  240. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  241. }
  242. /*
  243. * This runs as a tasklet when sending an urb in a non-blocking parallel
  244. * port callback had to be deferred because the disconnect mutex could not be
  245. * obtained at the time.
  246. */
  247. static void send_deferred_urbs(unsigned long _mos_parport)
  248. {
  249. int ret_val;
  250. unsigned long flags;
  251. struct mos7715_parport *mos_parport = (void *)_mos_parport;
  252. struct urbtracker *urbtrack;
  253. struct list_head *cursor, *next;
  254. /* if release function ran, game over */
  255. if (unlikely(mos_parport->serial == NULL))
  256. return;
  257. /* try again to get the mutex */
  258. if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
  259. dbg("%s: rescheduling tasklet", __func__);
  260. tasklet_schedule(&mos_parport->urb_tasklet);
  261. return;
  262. }
  263. /* if device disconnected, game over */
  264. if (unlikely(mos_parport->serial->disconnected)) {
  265. mutex_unlock(&mos_parport->serial->disc_mutex);
  266. return;
  267. }
  268. spin_lock_irqsave(&mos_parport->listlock, flags);
  269. if (list_empty(&mos_parport->deferred_urbs)) {
  270. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  271. mutex_unlock(&mos_parport->serial->disc_mutex);
  272. dbg("%s: deferred_urbs list empty", __func__);
  273. return;
  274. }
  275. /* move contents of deferred_urbs list to active_urbs list and submit */
  276. list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
  277. list_move_tail(cursor, &mos_parport->active_urbs);
  278. list_for_each_entry(urbtrack, &mos_parport->active_urbs,
  279. urblist_entry) {
  280. ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
  281. dbg("%s: urb submitted", __func__);
  282. if (ret_val) {
  283. dev_err(&mos_parport->serial->dev->dev,
  284. "usb_submit_urb() failed: %d", ret_val);
  285. list_del(&urbtrack->urblist_entry);
  286. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  287. }
  288. }
  289. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  290. mutex_unlock(&mos_parport->serial->disc_mutex);
  291. }
  292. /* callback for parallel port control urbs submitted asynchronously */
  293. static void async_complete(struct urb *urb)
  294. {
  295. struct urbtracker *urbtrack = urb->context;
  296. int status = urb->status;
  297. if (unlikely(status))
  298. dbg("%s - nonzero urb status received: %d", __func__, status);
  299. /* remove the urbtracker from the active_urbs list */
  300. spin_lock(&urbtrack->mos_parport->listlock);
  301. list_del(&urbtrack->urblist_entry);
  302. spin_unlock(&urbtrack->mos_parport->listlock);
  303. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  304. }
  305. static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
  306. enum mos_regs reg, __u8 data)
  307. {
  308. struct urbtracker *urbtrack;
  309. int ret_val;
  310. unsigned long flags;
  311. struct usb_ctrlrequest setup;
  312. struct usb_serial *serial = mos_parport->serial;
  313. struct usb_device *usbdev = serial->dev;
  314. /* create and initialize the control urb and containing urbtracker */
  315. urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
  316. if (urbtrack == NULL) {
  317. dev_err(&usbdev->dev, "out of memory");
  318. return -ENOMEM;
  319. }
  320. kref_get(&mos_parport->ref_count);
  321. urbtrack->mos_parport = mos_parport;
  322. urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
  323. if (urbtrack->urb == NULL) {
  324. dev_err(&usbdev->dev, "out of urbs");
  325. kfree(urbtrack);
  326. return -ENOMEM;
  327. }
  328. setup.bRequestType = (__u8)0x40;
  329. setup.bRequest = (__u8)0x0e;
  330. setup.wValue = get_reg_value(reg, dummy);
  331. setup.wIndex = get_reg_index(reg);
  332. setup.wLength = 0;
  333. usb_fill_control_urb(urbtrack->urb, usbdev,
  334. usb_sndctrlpipe(usbdev, 0),
  335. (unsigned char *)&setup,
  336. NULL, 0, async_complete, urbtrack);
  337. kref_init(&urbtrack->ref_count);
  338. INIT_LIST_HEAD(&urbtrack->urblist_entry);
  339. /*
  340. * get the disconnect mutex, or add tracker to the deferred_urbs list
  341. * and schedule a tasklet to try again later
  342. */
  343. if (!mutex_trylock(&serial->disc_mutex)) {
  344. spin_lock_irqsave(&mos_parport->listlock, flags);
  345. list_add_tail(&urbtrack->urblist_entry,
  346. &mos_parport->deferred_urbs);
  347. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  348. tasklet_schedule(&mos_parport->urb_tasklet);
  349. dbg("tasklet scheduled");
  350. return 0;
  351. }
  352. /* bail if device disconnected */
  353. if (serial->disconnected) {
  354. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  355. mutex_unlock(&serial->disc_mutex);
  356. return -ENODEV;
  357. }
  358. /* add the tracker to the active_urbs list and submit */
  359. spin_lock_irqsave(&mos_parport->listlock, flags);
  360. list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
  361. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  362. ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
  363. mutex_unlock(&serial->disc_mutex);
  364. if (ret_val) {
  365. dev_err(&usbdev->dev,
  366. "%s: submit_urb() failed: %d", __func__, ret_val);
  367. spin_lock_irqsave(&mos_parport->listlock, flags);
  368. list_del(&urbtrack->urblist_entry);
  369. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  370. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  371. return ret_val;
  372. }
  373. return 0;
  374. }
  375. /*
  376. * This is the the common top part of all parallel port callback operations that
  377. * send synchronous messages to the device. This implements convoluted locking
  378. * that avoids two scenarios: (1) a port operation is called after usbserial
  379. * has called our release function, at which point struct mos7715_parport has
  380. * been destroyed, and (2) the device has been disconnected, but usbserial has
  381. * not called the release function yet because someone has a serial port open.
  382. * The shared release_lock prevents the first, and the mutex and disconnected
  383. * flag maintained by usbserial covers the second. We also use the msg_pending
  384. * flag to ensure that all synchronous usb messgage calls have completed before
  385. * our release function can return.
  386. */
  387. static int parport_prologue(struct parport *pp)
  388. {
  389. struct mos7715_parport *mos_parport;
  390. spin_lock(&release_lock);
  391. mos_parport = pp->private_data;
  392. if (unlikely(mos_parport == NULL)) {
  393. /* release fn called, port struct destroyed */
  394. spin_unlock(&release_lock);
  395. return -1;
  396. }
  397. mos_parport->msg_pending = true; /* synch usb call pending */
  398. INIT_COMPLETION(mos_parport->syncmsg_compl);
  399. spin_unlock(&release_lock);
  400. mutex_lock(&mos_parport->serial->disc_mutex);
  401. if (mos_parport->serial->disconnected) {
  402. /* device disconnected */
  403. mutex_unlock(&mos_parport->serial->disc_mutex);
  404. mos_parport->msg_pending = false;
  405. complete(&mos_parport->syncmsg_compl);
  406. return -1;
  407. }
  408. return 0;
  409. }
  410. /*
  411. * This is the the common bottom part of all parallel port functions that send
  412. * synchronous messages to the device.
  413. */
  414. static inline void parport_epilogue(struct parport *pp)
  415. {
  416. struct mos7715_parport *mos_parport = pp->private_data;
  417. mutex_unlock(&mos_parport->serial->disc_mutex);
  418. mos_parport->msg_pending = false;
  419. complete(&mos_parport->syncmsg_compl);
  420. }
  421. static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
  422. {
  423. struct mos7715_parport *mos_parport = pp->private_data;
  424. if (parport_prologue(pp) < 0)
  425. return;
  426. mos7715_change_mode(mos_parport, SPP);
  427. write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
  428. parport_epilogue(pp);
  429. }
  430. static unsigned char parport_mos7715_read_data(struct parport *pp)
  431. {
  432. struct mos7715_parport *mos_parport = pp->private_data;
  433. unsigned char d;
  434. if (parport_prologue(pp) < 0)
  435. return 0;
  436. read_mos_reg(mos_parport->serial, dummy, DPR, &d);
  437. parport_epilogue(pp);
  438. return d;
  439. }
  440. static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
  441. {
  442. struct mos7715_parport *mos_parport = pp->private_data;
  443. __u8 data;
  444. if (parport_prologue(pp) < 0)
  445. return;
  446. data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
  447. write_mos_reg(mos_parport->serial, dummy, DCR, data);
  448. mos_parport->shadowDCR = data;
  449. parport_epilogue(pp);
  450. }
  451. static unsigned char parport_mos7715_read_control(struct parport *pp)
  452. {
  453. struct mos7715_parport *mos_parport = pp->private_data;
  454. __u8 dcr;
  455. spin_lock(&release_lock);
  456. mos_parport = pp->private_data;
  457. if (unlikely(mos_parport == NULL)) {
  458. spin_unlock(&release_lock);
  459. return 0;
  460. }
  461. dcr = mos_parport->shadowDCR & 0x0f;
  462. spin_unlock(&release_lock);
  463. return dcr;
  464. }
  465. static unsigned char parport_mos7715_frob_control(struct parport *pp,
  466. unsigned char mask,
  467. unsigned char val)
  468. {
  469. struct mos7715_parport *mos_parport = pp->private_data;
  470. __u8 dcr;
  471. mask &= 0x0f;
  472. val &= 0x0f;
  473. if (parport_prologue(pp) < 0)
  474. return 0;
  475. mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
  476. write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
  477. dcr = mos_parport->shadowDCR & 0x0f;
  478. parport_epilogue(pp);
  479. return dcr;
  480. }
  481. static unsigned char parport_mos7715_read_status(struct parport *pp)
  482. {
  483. unsigned char status;
  484. struct mos7715_parport *mos_parport = pp->private_data;
  485. spin_lock(&release_lock);
  486. mos_parport = pp->private_data;
  487. if (unlikely(mos_parport == NULL)) { /* release called */
  488. spin_unlock(&release_lock);
  489. return 0;
  490. }
  491. status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
  492. spin_unlock(&release_lock);
  493. return status;
  494. }
  495. static void parport_mos7715_enable_irq(struct parport *pp)
  496. {
  497. }
  498. static void parport_mos7715_disable_irq(struct parport *pp)
  499. {
  500. }
  501. static void parport_mos7715_data_forward(struct parport *pp)
  502. {
  503. struct mos7715_parport *mos_parport = pp->private_data;
  504. if (parport_prologue(pp) < 0)
  505. return;
  506. mos7715_change_mode(mos_parport, PS2);
  507. mos_parport->shadowDCR &= ~0x20;
  508. write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
  509. parport_epilogue(pp);
  510. }
  511. static void parport_mos7715_data_reverse(struct parport *pp)
  512. {
  513. struct mos7715_parport *mos_parport = pp->private_data;
  514. if (parport_prologue(pp) < 0)
  515. return;
  516. mos7715_change_mode(mos_parport, PS2);
  517. mos_parport->shadowDCR |= 0x20;
  518. write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
  519. parport_epilogue(pp);
  520. }
  521. static void parport_mos7715_init_state(struct pardevice *dev,
  522. struct parport_state *s)
  523. {
  524. s->u.pc.ctr = DCR_INIT_VAL;
  525. s->u.pc.ecr = ECR_INIT_VAL;
  526. }
  527. /* N.B. Parport core code requires that this function not block */
  528. static void parport_mos7715_save_state(struct parport *pp,
  529. struct parport_state *s)
  530. {
  531. struct mos7715_parport *mos_parport;
  532. spin_lock(&release_lock);
  533. mos_parport = pp->private_data;
  534. if (unlikely(mos_parport == NULL)) { /* release called */
  535. spin_unlock(&release_lock);
  536. return;
  537. }
  538. s->u.pc.ctr = mos_parport->shadowDCR;
  539. s->u.pc.ecr = mos_parport->shadowECR;
  540. spin_unlock(&release_lock);
  541. }
  542. /* N.B. Parport core code requires that this function not block */
  543. static void parport_mos7715_restore_state(struct parport *pp,
  544. struct parport_state *s)
  545. {
  546. struct mos7715_parport *mos_parport;
  547. spin_lock(&release_lock);
  548. mos_parport = pp->private_data;
  549. if (unlikely(mos_parport == NULL)) { /* release called */
  550. spin_unlock(&release_lock);
  551. return;
  552. }
  553. write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
  554. write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
  555. spin_unlock(&release_lock);
  556. }
  557. static size_t parport_mos7715_write_compat(struct parport *pp,
  558. const void *buffer,
  559. size_t len, int flags)
  560. {
  561. int retval;
  562. struct mos7715_parport *mos_parport = pp->private_data;
  563. int actual_len;
  564. if (parport_prologue(pp) < 0)
  565. return 0;
  566. mos7715_change_mode(mos_parport, PPF);
  567. retval = usb_bulk_msg(mos_parport->serial->dev,
  568. usb_sndbulkpipe(mos_parport->serial->dev, 2),
  569. (void *)buffer, len, &actual_len,
  570. MOS_WDR_TIMEOUT);
  571. parport_epilogue(pp);
  572. if (retval) {
  573. dev_err(&mos_parport->serial->dev->dev,
  574. "mos7720: usb_bulk_msg() failed: %d", retval);
  575. return 0;
  576. }
  577. return actual_len;
  578. }
  579. static struct parport_operations parport_mos7715_ops = {
  580. .owner = THIS_MODULE,
  581. .write_data = parport_mos7715_write_data,
  582. .read_data = parport_mos7715_read_data,
  583. .write_control = parport_mos7715_write_control,
  584. .read_control = parport_mos7715_read_control,
  585. .frob_control = parport_mos7715_frob_control,
  586. .read_status = parport_mos7715_read_status,
  587. .enable_irq = parport_mos7715_enable_irq,
  588. .disable_irq = parport_mos7715_disable_irq,
  589. .data_forward = parport_mos7715_data_forward,
  590. .data_reverse = parport_mos7715_data_reverse,
  591. .init_state = parport_mos7715_init_state,
  592. .save_state = parport_mos7715_save_state,
  593. .restore_state = parport_mos7715_restore_state,
  594. .compat_write_data = parport_mos7715_write_compat,
  595. .nibble_read_data = parport_ieee1284_read_nibble,
  596. .byte_read_data = parport_ieee1284_read_byte,
  597. };
  598. /*
  599. * Allocate and initialize parallel port control struct, initialize
  600. * the parallel port hardware device, and register with the parport subsystem.
  601. */
  602. static int mos7715_parport_init(struct usb_serial *serial)
  603. {
  604. struct mos7715_parport *mos_parport;
  605. /* allocate and initialize parallel port control struct */
  606. mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
  607. if (mos_parport == NULL) {
  608. dbg("mos7715_parport_init: kzalloc failed");
  609. return -ENOMEM;
  610. }
  611. mos_parport->msg_pending = false;
  612. kref_init(&mos_parport->ref_count);
  613. spin_lock_init(&mos_parport->listlock);
  614. INIT_LIST_HEAD(&mos_parport->active_urbs);
  615. INIT_LIST_HEAD(&mos_parport->deferred_urbs);
  616. usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
  617. mos_parport->serial = serial;
  618. tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
  619. (unsigned long) mos_parport);
  620. init_completion(&mos_parport->syncmsg_compl);
  621. /* cycle parallel port reset bit */
  622. write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
  623. write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
  624. /* initialize device registers */
  625. mos_parport->shadowDCR = DCR_INIT_VAL;
  626. write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
  627. mos_parport->shadowECR = ECR_INIT_VAL;
  628. write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
  629. /* register with parport core */
  630. mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
  631. PARPORT_DMA_NONE,
  632. &parport_mos7715_ops);
  633. if (mos_parport->pp == NULL) {
  634. dev_err(&serial->interface->dev,
  635. "Could not register parport\n");
  636. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  637. return -EIO;
  638. }
  639. mos_parport->pp->private_data = mos_parport;
  640. mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
  641. mos_parport->pp->dev = &serial->interface->dev;
  642. parport_announce_port(mos_parport->pp);
  643. return 0;
  644. }
  645. #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
  646. /*
  647. * mos7720_interrupt_callback
  648. * this is the callback function for when we have received data on the
  649. * interrupt endpoint.
  650. */
  651. static void mos7720_interrupt_callback(struct urb *urb)
  652. {
  653. int result;
  654. int length;
  655. int status = urb->status;
  656. __u8 *data;
  657. __u8 sp1;
  658. __u8 sp2;
  659. switch (status) {
  660. case 0:
  661. /* success */
  662. break;
  663. case -ECONNRESET:
  664. case -ENOENT:
  665. case -ESHUTDOWN:
  666. /* this urb is terminated, clean up */
  667. dbg("%s - urb shutting down with status: %d", __func__,
  668. status);
  669. return;
  670. default:
  671. dbg("%s - nonzero urb status received: %d", __func__,
  672. status);
  673. goto exit;
  674. }
  675. length = urb->actual_length;
  676. data = urb->transfer_buffer;
  677. /* Moschip get 4 bytes
  678. * Byte 1 IIR Port 1 (port.number is 0)
  679. * Byte 2 IIR Port 2 (port.number is 1)
  680. * Byte 3 --------------
  681. * Byte 4 FIFO status for both */
  682. /* the above description is inverted
  683. * oneukum 2007-03-14 */
  684. if (unlikely(length != 4)) {
  685. dbg("Wrong data !!!");
  686. return;
  687. }
  688. sp1 = data[3];
  689. sp2 = data[2];
  690. if ((sp1 | sp2) & 0x01) {
  691. /* No Interrupt Pending in both the ports */
  692. dbg("No Interrupt !!!");
  693. } else {
  694. switch (sp1 & 0x0f) {
  695. case SERIAL_IIR_RLS:
  696. dbg("Serial Port 1: Receiver status error or address "
  697. "bit detected in 9-bit mode\n");
  698. break;
  699. case SERIAL_IIR_CTI:
  700. dbg("Serial Port 1: Receiver time out");
  701. break;
  702. case SERIAL_IIR_MS:
  703. /* dbg("Serial Port 1: Modem status change"); */
  704. break;
  705. }
  706. switch (sp2 & 0x0f) {
  707. case SERIAL_IIR_RLS:
  708. dbg("Serial Port 2: Receiver status error or address "
  709. "bit detected in 9-bit mode");
  710. break;
  711. case SERIAL_IIR_CTI:
  712. dbg("Serial Port 2: Receiver time out");
  713. break;
  714. case SERIAL_IIR_MS:
  715. /* dbg("Serial Port 2: Modem status change"); */
  716. break;
  717. }
  718. }
  719. exit:
  720. result = usb_submit_urb(urb, GFP_ATOMIC);
  721. if (result)
  722. dev_err(&urb->dev->dev,
  723. "%s - Error %d submitting control urb\n",
  724. __func__, result);
  725. }
  726. /*
  727. * mos7715_interrupt_callback
  728. * this is the 7715's callback function for when we have received data on
  729. * the interrupt endpoint.
  730. */
  731. static void mos7715_interrupt_callback(struct urb *urb)
  732. {
  733. int result;
  734. int length;
  735. int status = urb->status;
  736. __u8 *data;
  737. __u8 iir;
  738. switch (status) {
  739. case 0:
  740. /* success */
  741. break;
  742. case -ECONNRESET:
  743. case -ENOENT:
  744. case -ESHUTDOWN:
  745. case -ENODEV:
  746. /* this urb is terminated, clean up */
  747. dbg("%s - urb shutting down with status: %d", __func__,
  748. status);
  749. return;
  750. default:
  751. dbg("%s - nonzero urb status received: %d", __func__,
  752. status);
  753. goto exit;
  754. }
  755. length = urb->actual_length;
  756. data = urb->transfer_buffer;
  757. /* Structure of data from 7715 device:
  758. * Byte 1: IIR serial Port
  759. * Byte 2: unused
  760. * Byte 2: DSR parallel port
  761. * Byte 4: FIFO status for both */
  762. if (unlikely(length != 4)) {
  763. dbg("Wrong data !!!");
  764. return;
  765. }
  766. iir = data[0];
  767. if (!(iir & 0x01)) { /* serial port interrupt pending */
  768. switch (iir & 0x0f) {
  769. case SERIAL_IIR_RLS:
  770. dbg("Serial Port: Receiver status error or address "
  771. "bit detected in 9-bit mode\n");
  772. break;
  773. case SERIAL_IIR_CTI:
  774. dbg("Serial Port: Receiver time out");
  775. break;
  776. case SERIAL_IIR_MS:
  777. /* dbg("Serial Port: Modem status change"); */
  778. break;
  779. }
  780. }
  781. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  782. { /* update local copy of DSR reg */
  783. struct usb_serial_port *port = urb->context;
  784. struct mos7715_parport *mos_parport = port->serial->private;
  785. if (unlikely(mos_parport == NULL))
  786. return;
  787. atomic_set(&mos_parport->shadowDSR, data[2]);
  788. }
  789. #endif
  790. exit:
  791. result = usb_submit_urb(urb, GFP_ATOMIC);
  792. if (result)
  793. dev_err(&urb->dev->dev,
  794. "%s - Error %d submitting control urb\n",
  795. __func__, result);
  796. }
  797. /*
  798. * mos7720_bulk_in_callback
  799. * this is the callback function for when we have received data on the
  800. * bulk in endpoint.
  801. */
  802. static void mos7720_bulk_in_callback(struct urb *urb)
  803. {
  804. int retval;
  805. unsigned char *data ;
  806. struct usb_serial_port *port;
  807. struct tty_struct *tty;
  808. int status = urb->status;
  809. if (status) {
  810. dbg("nonzero read bulk status received: %d", status);
  811. return;
  812. }
  813. port = urb->context;
  814. dbg("Entering...%s", __func__);
  815. data = urb->transfer_buffer;
  816. tty = tty_port_tty_get(&port->port);
  817. if (tty && urb->actual_length) {
  818. tty_insert_flip_string(tty, data, urb->actual_length);
  819. tty_flip_buffer_push(tty);
  820. }
  821. tty_kref_put(tty);
  822. if (port->read_urb->status != -EINPROGRESS) {
  823. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  824. if (retval)
  825. dbg("usb_submit_urb(read bulk) failed, retval = %d",
  826. retval);
  827. }
  828. }
  829. /*
  830. * mos7720_bulk_out_data_callback
  831. * this is the callback function for when we have finished sending serial
  832. * data on the bulk out endpoint.
  833. */
  834. static void mos7720_bulk_out_data_callback(struct urb *urb)
  835. {
  836. struct moschip_port *mos7720_port;
  837. struct tty_struct *tty;
  838. int status = urb->status;
  839. if (status) {
  840. dbg("nonzero write bulk status received:%d", status);
  841. return;
  842. }
  843. mos7720_port = urb->context;
  844. if (!mos7720_port) {
  845. dbg("NULL mos7720_port pointer");
  846. return ;
  847. }
  848. tty = tty_port_tty_get(&mos7720_port->port->port);
  849. if (tty && mos7720_port->open)
  850. tty_wakeup(tty);
  851. tty_kref_put(tty);
  852. }
  853. /*
  854. * mos77xx_probe
  855. * this function installs the appropriate read interrupt endpoint callback
  856. * depending on whether the device is a 7720 or 7715, thus avoiding costly
  857. * run-time checks in the high-frequency callback routine itself.
  858. */
  859. static int mos77xx_probe(struct usb_serial *serial,
  860. const struct usb_device_id *id)
  861. {
  862. if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
  863. moschip7720_2port_driver.read_int_callback =
  864. mos7715_interrupt_callback;
  865. else
  866. moschip7720_2port_driver.read_int_callback =
  867. mos7720_interrupt_callback;
  868. return 0;
  869. }
  870. static int mos77xx_calc_num_ports(struct usb_serial *serial)
  871. {
  872. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  873. if (product == MOSCHIP_DEVICE_ID_7715)
  874. return 1;
  875. return 2;
  876. }
  877. static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
  878. {
  879. struct usb_serial *serial;
  880. struct urb *urb;
  881. struct moschip_port *mos7720_port;
  882. int response;
  883. int port_number;
  884. __u8 data;
  885. int allocated_urbs = 0;
  886. int j;
  887. serial = port->serial;
  888. mos7720_port = usb_get_serial_port_data(port);
  889. if (mos7720_port == NULL)
  890. return -ENODEV;
  891. usb_clear_halt(serial->dev, port->write_urb->pipe);
  892. usb_clear_halt(serial->dev, port->read_urb->pipe);
  893. /* Initialising the write urb pool */
  894. for (j = 0; j < NUM_URBS; ++j) {
  895. urb = usb_alloc_urb(0, GFP_KERNEL);
  896. mos7720_port->write_urb_pool[j] = urb;
  897. if (urb == NULL) {
  898. dev_err(&port->dev, "No more urbs???\n");
  899. continue;
  900. }
  901. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  902. GFP_KERNEL);
  903. if (!urb->transfer_buffer) {
  904. dev_err(&port->dev,
  905. "%s-out of memory for urb buffers.\n",
  906. __func__);
  907. usb_free_urb(mos7720_port->write_urb_pool[j]);
  908. mos7720_port->write_urb_pool[j] = NULL;
  909. continue;
  910. }
  911. allocated_urbs++;
  912. }
  913. if (!allocated_urbs)
  914. return -ENOMEM;
  915. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  916. *
  917. * Register Index
  918. * 0 : THR/RHR
  919. * 1 : IER
  920. * 2 : FCR
  921. * 3 : LCR
  922. * 4 : MCR
  923. * 5 : LSR
  924. * 6 : MSR
  925. * 7 : SPR
  926. *
  927. * 0x08 : SP1/2 Control Reg
  928. */
  929. port_number = port->number - port->serial->minor;
  930. read_mos_reg(serial, port_number, LSR, &data);
  931. dbg("SS::%p LSR:%x", mos7720_port, data);
  932. dbg("Check:Sending Command ..........");
  933. write_mos_reg(serial, dummy, SP1_REG, 0x02);
  934. write_mos_reg(serial, dummy, SP2_REG, 0x02);
  935. write_mos_reg(serial, port_number, IER, 0x00);
  936. write_mos_reg(serial, port_number, FCR, 0x00);
  937. write_mos_reg(serial, port_number, FCR, 0xcf);
  938. mos7720_port->shadowLCR = 0x03;
  939. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  940. mos7720_port->shadowMCR = 0x0b;
  941. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  942. write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
  943. read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
  944. data = data | (port->number - port->serial->minor + 1);
  945. write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
  946. mos7720_port->shadowLCR = 0x83;
  947. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  948. write_mos_reg(serial, port_number, THR, 0x0c);
  949. write_mos_reg(serial, port_number, IER, 0x00);
  950. mos7720_port->shadowLCR = 0x03;
  951. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  952. write_mos_reg(serial, port_number, IER, 0x0c);
  953. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  954. if (response)
  955. dev_err(&port->dev, "%s - Error %d submitting read urb\n",
  956. __func__, response);
  957. /* initialize our icount structure */
  958. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  959. /* initialize our port settings */
  960. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  961. /* send a open port command */
  962. mos7720_port->open = 1;
  963. return 0;
  964. }
  965. /*
  966. * mos7720_chars_in_buffer
  967. * this function is called by the tty driver when it wants to know how many
  968. * bytes of data we currently have outstanding in the port (data that has
  969. * been written, but hasn't made it out the port yet)
  970. * If successful, we return the number of bytes left to be written in the
  971. * system,
  972. * Otherwise we return a negative error number.
  973. */
  974. static int mos7720_chars_in_buffer(struct tty_struct *tty)
  975. {
  976. struct usb_serial_port *port = tty->driver_data;
  977. int i;
  978. int chars = 0;
  979. struct moschip_port *mos7720_port;
  980. dbg("%s:entering ...........", __func__);
  981. mos7720_port = usb_get_serial_port_data(port);
  982. if (mos7720_port == NULL) {
  983. dbg("%s:leaving ...........", __func__);
  984. return 0;
  985. }
  986. for (i = 0; i < NUM_URBS; ++i) {
  987. if (mos7720_port->write_urb_pool[i] &&
  988. mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  989. chars += URB_TRANSFER_BUFFER_SIZE;
  990. }
  991. dbg("%s - returns %d", __func__, chars);
  992. return chars;
  993. }
  994. static void mos7720_close(struct usb_serial_port *port)
  995. {
  996. struct usb_serial *serial;
  997. struct moschip_port *mos7720_port;
  998. int j;
  999. dbg("mos7720_close:entering...");
  1000. serial = port->serial;
  1001. mos7720_port = usb_get_serial_port_data(port);
  1002. if (mos7720_port == NULL)
  1003. return;
  1004. for (j = 0; j < NUM_URBS; ++j)
  1005. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  1006. /* Freeing Write URBs */
  1007. for (j = 0; j < NUM_URBS; ++j) {
  1008. if (mos7720_port->write_urb_pool[j]) {
  1009. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  1010. usb_free_urb(mos7720_port->write_urb_pool[j]);
  1011. }
  1012. }
  1013. /* While closing port, shutdown all bulk read, write *
  1014. * and interrupt read if they exists, otherwise nop */
  1015. dbg("Shutdown bulk write");
  1016. usb_kill_urb(port->write_urb);
  1017. dbg("Shutdown bulk read");
  1018. usb_kill_urb(port->read_urb);
  1019. mutex_lock(&serial->disc_mutex);
  1020. /* these commands must not be issued if the device has
  1021. * been disconnected */
  1022. if (!serial->disconnected) {
  1023. write_mos_reg(serial, port->number - port->serial->minor,
  1024. MCR, 0x00);
  1025. write_mos_reg(serial, port->number - port->serial->minor,
  1026. IER, 0x00);
  1027. }
  1028. mutex_unlock(&serial->disc_mutex);
  1029. mos7720_port->open = 0;
  1030. dbg("Leaving %s", __func__);
  1031. }
  1032. static void mos7720_break(struct tty_struct *tty, int break_state)
  1033. {
  1034. struct usb_serial_port *port = tty->driver_data;
  1035. unsigned char data;
  1036. struct usb_serial *serial;
  1037. struct moschip_port *mos7720_port;
  1038. dbg("Entering %s", __func__);
  1039. serial = port->serial;
  1040. mos7720_port = usb_get_serial_port_data(port);
  1041. if (mos7720_port == NULL)
  1042. return;
  1043. if (break_state == -1)
  1044. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  1045. else
  1046. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  1047. mos7720_port->shadowLCR = data;
  1048. write_mos_reg(serial, port->number - port->serial->minor,
  1049. LCR, mos7720_port->shadowLCR);
  1050. }
  1051. /*
  1052. * mos7720_write_room
  1053. * this function is called by the tty driver when it wants to know how many
  1054. * bytes of data we can accept for a specific port.
  1055. * If successful, we return the amount of room that we have for this port
  1056. * Otherwise we return a negative error number.
  1057. */
  1058. static int mos7720_write_room(struct tty_struct *tty)
  1059. {
  1060. struct usb_serial_port *port = tty->driver_data;
  1061. struct moschip_port *mos7720_port;
  1062. int room = 0;
  1063. int i;
  1064. dbg("%s:entering ...........", __func__);
  1065. mos7720_port = usb_get_serial_port_data(port);
  1066. if (mos7720_port == NULL) {
  1067. dbg("%s:leaving ...........", __func__);
  1068. return -ENODEV;
  1069. }
  1070. /* FIXME: Locking */
  1071. for (i = 0; i < NUM_URBS; ++i) {
  1072. if (mos7720_port->write_urb_pool[i] &&
  1073. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  1074. room += URB_TRANSFER_BUFFER_SIZE;
  1075. }
  1076. dbg("%s - returns %d", __func__, room);
  1077. return room;
  1078. }
  1079. static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
  1080. const unsigned char *data, int count)
  1081. {
  1082. int status;
  1083. int i;
  1084. int bytes_sent = 0;
  1085. int transfer_size;
  1086. struct moschip_port *mos7720_port;
  1087. struct usb_serial *serial;
  1088. struct urb *urb;
  1089. const unsigned char *current_position = data;
  1090. dbg("%s:entering ...........", __func__);
  1091. serial = port->serial;
  1092. mos7720_port = usb_get_serial_port_data(port);
  1093. if (mos7720_port == NULL) {
  1094. dbg("mos7720_port is NULL");
  1095. return -ENODEV;
  1096. }
  1097. /* try to find a free urb in the list */
  1098. urb = NULL;
  1099. for (i = 0; i < NUM_URBS; ++i) {
  1100. if (mos7720_port->write_urb_pool[i] &&
  1101. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  1102. urb = mos7720_port->write_urb_pool[i];
  1103. dbg("URB:%d", i);
  1104. break;
  1105. }
  1106. }
  1107. if (urb == NULL) {
  1108. dbg("%s - no more free urbs", __func__);
  1109. goto exit;
  1110. }
  1111. if (urb->transfer_buffer == NULL) {
  1112. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  1113. GFP_KERNEL);
  1114. if (urb->transfer_buffer == NULL) {
  1115. dev_err_console(port, "%s no more kernel memory...\n",
  1116. __func__);
  1117. goto exit;
  1118. }
  1119. }
  1120. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  1121. memcpy(urb->transfer_buffer, current_position, transfer_size);
  1122. usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
  1123. urb->transfer_buffer);
  1124. /* fill urb with data and submit */
  1125. usb_fill_bulk_urb(urb, serial->dev,
  1126. usb_sndbulkpipe(serial->dev,
  1127. port->bulk_out_endpointAddress),
  1128. urb->transfer_buffer, transfer_size,
  1129. mos7720_bulk_out_data_callback, mos7720_port);
  1130. /* send it down the pipe */
  1131. status = usb_submit_urb(urb, GFP_ATOMIC);
  1132. if (status) {
  1133. dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
  1134. "with status = %d\n", __func__, status);
  1135. bytes_sent = status;
  1136. goto exit;
  1137. }
  1138. bytes_sent = transfer_size;
  1139. exit:
  1140. return bytes_sent;
  1141. }
  1142. static void mos7720_throttle(struct tty_struct *tty)
  1143. {
  1144. struct usb_serial_port *port = tty->driver_data;
  1145. struct moschip_port *mos7720_port;
  1146. int status;
  1147. dbg("%s- port %d", __func__, port->number);
  1148. mos7720_port = usb_get_serial_port_data(port);
  1149. if (mos7720_port == NULL)
  1150. return;
  1151. if (!mos7720_port->open) {
  1152. dbg("port not opened");
  1153. return;
  1154. }
  1155. dbg("%s: Entering ..........", __func__);
  1156. /* if we are implementing XON/XOFF, send the stop character */
  1157. if (I_IXOFF(tty)) {
  1158. unsigned char stop_char = STOP_CHAR(tty);
  1159. status = mos7720_write(tty, port, &stop_char, 1);
  1160. if (status <= 0)
  1161. return;
  1162. }
  1163. /* if we are implementing RTS/CTS, toggle that line */
  1164. if (tty->termios->c_cflag & CRTSCTS) {
  1165. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  1166. write_mos_reg(port->serial, port->number - port->serial->minor,
  1167. MCR, mos7720_port->shadowMCR);
  1168. if (status != 0)
  1169. return;
  1170. }
  1171. }
  1172. static void mos7720_unthrottle(struct tty_struct *tty)
  1173. {
  1174. struct usb_serial_port *port = tty->driver_data;
  1175. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1176. int status;
  1177. if (mos7720_port == NULL)
  1178. return;
  1179. if (!mos7720_port->open) {
  1180. dbg("%s - port not opened", __func__);
  1181. return;
  1182. }
  1183. dbg("%s: Entering ..........", __func__);
  1184. /* if we are implementing XON/XOFF, send the start character */
  1185. if (I_IXOFF(tty)) {
  1186. unsigned char start_char = START_CHAR(tty);
  1187. status = mos7720_write(tty, port, &start_char, 1);
  1188. if (status <= 0)
  1189. return;
  1190. }
  1191. /* if we are implementing RTS/CTS, toggle that line */
  1192. if (tty->termios->c_cflag & CRTSCTS) {
  1193. mos7720_port->shadowMCR |= UART_MCR_RTS;
  1194. write_mos_reg(port->serial, port->number - port->serial->minor,
  1195. MCR, mos7720_port->shadowMCR);
  1196. if (status != 0)
  1197. return;
  1198. }
  1199. }
  1200. /* FIXME: this function does not work */
  1201. static int set_higher_rates(struct moschip_port *mos7720_port,
  1202. unsigned int baud)
  1203. {
  1204. struct usb_serial_port *port;
  1205. struct usb_serial *serial;
  1206. int port_number;
  1207. enum mos_regs sp_reg;
  1208. if (mos7720_port == NULL)
  1209. return -EINVAL;
  1210. port = mos7720_port->port;
  1211. serial = port->serial;
  1212. /***********************************************
  1213. * Init Sequence for higher rates
  1214. ***********************************************/
  1215. dbg("Sending Setting Commands ..........");
  1216. port_number = port->number - port->serial->minor;
  1217. write_mos_reg(serial, port_number, IER, 0x00);
  1218. write_mos_reg(serial, port_number, FCR, 0x00);
  1219. write_mos_reg(serial, port_number, FCR, 0xcf);
  1220. mos7720_port->shadowMCR = 0x0b;
  1221. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1222. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
  1223. /***********************************************
  1224. * Set for higher rates *
  1225. ***********************************************/
  1226. /* writing baud rate verbatum into uart clock field clearly not right */
  1227. if (port_number == 0)
  1228. sp_reg = SP1_REG;
  1229. else
  1230. sp_reg = SP2_REG;
  1231. write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
  1232. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
  1233. mos7720_port->shadowMCR = 0x2b;
  1234. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1235. /***********************************************
  1236. * Set DLL/DLM
  1237. ***********************************************/
  1238. mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
  1239. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  1240. write_mos_reg(serial, port_number, DLL, 0x01);
  1241. write_mos_reg(serial, port_number, DLM, 0x00);
  1242. mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  1243. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  1244. return 0;
  1245. }
  1246. /* baud rate information */
  1247. struct divisor_table_entry {
  1248. __u32 baudrate;
  1249. __u16 divisor;
  1250. };
  1251. /* Define table of divisors for moschip 7720 hardware *
  1252. * These assume a 3.6864MHz crystal, the standard /16, and *
  1253. * MCR.7 = 0. */
  1254. static struct divisor_table_entry divisor_table[] = {
  1255. { 50, 2304},
  1256. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  1257. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  1258. { 150, 768},
  1259. { 300, 384},
  1260. { 600, 192},
  1261. { 1200, 96},
  1262. { 1800, 64},
  1263. { 2400, 48},
  1264. { 4800, 24},
  1265. { 7200, 16},
  1266. { 9600, 12},
  1267. { 19200, 6},
  1268. { 38400, 3},
  1269. { 57600, 2},
  1270. { 115200, 1},
  1271. };
  1272. /*****************************************************************************
  1273. * calc_baud_rate_divisor
  1274. * this function calculates the proper baud rate divisor for the specified
  1275. * baud rate.
  1276. *****************************************************************************/
  1277. static int calc_baud_rate_divisor(int baudrate, int *divisor)
  1278. {
  1279. int i;
  1280. __u16 custom;
  1281. __u16 round1;
  1282. __u16 round;
  1283. dbg("%s - %d", __func__, baudrate);
  1284. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  1285. if (divisor_table[i].baudrate == baudrate) {
  1286. *divisor = divisor_table[i].divisor;
  1287. return 0;
  1288. }
  1289. }
  1290. /* After trying for all the standard baud rates *
  1291. * Try calculating the divisor for this baud rate */
  1292. if (baudrate > 75 && baudrate < 230400) {
  1293. /* get the divisor */
  1294. custom = (__u16)(230400L / baudrate);
  1295. /* Check for round off */
  1296. round1 = (__u16)(2304000L / baudrate);
  1297. round = (__u16)(round1 - (custom * 10));
  1298. if (round > 4)
  1299. custom++;
  1300. *divisor = custom;
  1301. dbg("Baud %d = %d", baudrate, custom);
  1302. return 0;
  1303. }
  1304. dbg("Baud calculation Failed...");
  1305. return -EINVAL;
  1306. }
  1307. /*
  1308. * send_cmd_write_baud_rate
  1309. * this function sends the proper command to change the baud rate of the
  1310. * specified port.
  1311. */
  1312. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  1313. int baudrate)
  1314. {
  1315. struct usb_serial_port *port;
  1316. struct usb_serial *serial;
  1317. int divisor;
  1318. int status;
  1319. unsigned char number;
  1320. if (mos7720_port == NULL)
  1321. return -1;
  1322. port = mos7720_port->port;
  1323. serial = port->serial;
  1324. dbg("%s: Entering ..........", __func__);
  1325. number = port->number - port->serial->minor;
  1326. dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
  1327. /* Calculate the Divisor */
  1328. status = calc_baud_rate_divisor(baudrate, &divisor);
  1329. if (status) {
  1330. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  1331. return status;
  1332. }
  1333. /* Enable access to divisor latch */
  1334. mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
  1335. write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
  1336. /* Write the divisor */
  1337. write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
  1338. write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
  1339. /* Disable access to divisor latch */
  1340. mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  1341. write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
  1342. return status;
  1343. }
  1344. /*
  1345. * change_port_settings
  1346. * This routine is called to set the UART on the device to match
  1347. * the specified new settings.
  1348. */
  1349. static void change_port_settings(struct tty_struct *tty,
  1350. struct moschip_port *mos7720_port,
  1351. struct ktermios *old_termios)
  1352. {
  1353. struct usb_serial_port *port;
  1354. struct usb_serial *serial;
  1355. int baud;
  1356. unsigned cflag;
  1357. unsigned iflag;
  1358. __u8 mask = 0xff;
  1359. __u8 lData;
  1360. __u8 lParity;
  1361. __u8 lStop;
  1362. int status;
  1363. int port_number;
  1364. if (mos7720_port == NULL)
  1365. return ;
  1366. port = mos7720_port->port;
  1367. serial = port->serial;
  1368. port_number = port->number - port->serial->minor;
  1369. dbg("%s - port %d", __func__, port->number);
  1370. if (!mos7720_port->open) {
  1371. dbg("%s - port not opened", __func__);
  1372. return;
  1373. }
  1374. dbg("%s: Entering ..........", __func__);
  1375. lData = UART_LCR_WLEN8;
  1376. lStop = 0x00; /* 1 stop bit */
  1377. lParity = 0x00; /* No parity */
  1378. cflag = tty->termios->c_cflag;
  1379. iflag = tty->termios->c_iflag;
  1380. /* Change the number of bits */
  1381. switch (cflag & CSIZE) {
  1382. case CS5:
  1383. lData = UART_LCR_WLEN5;
  1384. mask = 0x1f;
  1385. break;
  1386. case CS6:
  1387. lData = UART_LCR_WLEN6;
  1388. mask = 0x3f;
  1389. break;
  1390. case CS7:
  1391. lData = UART_LCR_WLEN7;
  1392. mask = 0x7f;
  1393. break;
  1394. default:
  1395. case CS8:
  1396. lData = UART_LCR_WLEN8;
  1397. break;
  1398. }
  1399. /* Change the Parity bit */
  1400. if (cflag & PARENB) {
  1401. if (cflag & PARODD) {
  1402. lParity = UART_LCR_PARITY;
  1403. dbg("%s - parity = odd", __func__);
  1404. } else {
  1405. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  1406. dbg("%s - parity = even", __func__);
  1407. }
  1408. } else {
  1409. dbg("%s - parity = none", __func__);
  1410. }
  1411. if (cflag & CMSPAR)
  1412. lParity = lParity | 0x20;
  1413. /* Change the Stop bit */
  1414. if (cflag & CSTOPB) {
  1415. lStop = UART_LCR_STOP;
  1416. dbg("%s - stop bits = 2", __func__);
  1417. } else {
  1418. lStop = 0x00;
  1419. dbg("%s - stop bits = 1", __func__);
  1420. }
  1421. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  1422. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  1423. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  1424. /* Update the LCR with the correct value */
  1425. mos7720_port->shadowLCR &=
  1426. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1427. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  1428. /* Disable Interrupts */
  1429. write_mos_reg(serial, port_number, IER, 0x00);
  1430. write_mos_reg(serial, port_number, FCR, 0x00);
  1431. write_mos_reg(serial, port_number, FCR, 0xcf);
  1432. /* Send the updated LCR value to the mos7720 */
  1433. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  1434. mos7720_port->shadowMCR = 0x0b;
  1435. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1436. /* set up the MCR register and send it to the mos7720 */
  1437. mos7720_port->shadowMCR = UART_MCR_OUT2;
  1438. if (cflag & CBAUD)
  1439. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  1440. if (cflag & CRTSCTS) {
  1441. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  1442. /* To set hardware flow control to the specified *
  1443. * serial port, in SP1/2_CONTROL_REG */
  1444. if (port->number)
  1445. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
  1446. else
  1447. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
  1448. } else
  1449. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  1450. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1451. /* Determine divisor based on baud rate */
  1452. baud = tty_get_baud_rate(tty);
  1453. if (!baud) {
  1454. /* pick a default, any default... */
  1455. dbg("Picked default baud...");
  1456. baud = 9600;
  1457. }
  1458. if (baud >= 230400) {
  1459. set_higher_rates(mos7720_port, baud);
  1460. /* Enable Interrupts */
  1461. write_mos_reg(serial, port_number, IER, 0x0c);
  1462. return;
  1463. }
  1464. dbg("%s - baud rate = %d", __func__, baud);
  1465. status = send_cmd_write_baud_rate(mos7720_port, baud);
  1466. /* FIXME: needs to write actual resulting baud back not just
  1467. blindly do so */
  1468. if (cflag & CBAUD)
  1469. tty_encode_baud_rate(tty, baud, baud);
  1470. /* Enable Interrupts */
  1471. write_mos_reg(serial, port_number, IER, 0x0c);
  1472. if (port->read_urb->status != -EINPROGRESS) {
  1473. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1474. if (status)
  1475. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1476. status);
  1477. }
  1478. }
  1479. /*
  1480. * mos7720_set_termios
  1481. * this function is called by the tty driver when it wants to change the
  1482. * termios structure.
  1483. */
  1484. static void mos7720_set_termios(struct tty_struct *tty,
  1485. struct usb_serial_port *port, struct ktermios *old_termios)
  1486. {
  1487. int status;
  1488. unsigned int cflag;
  1489. struct usb_serial *serial;
  1490. struct moschip_port *mos7720_port;
  1491. serial = port->serial;
  1492. mos7720_port = usb_get_serial_port_data(port);
  1493. if (mos7720_port == NULL)
  1494. return;
  1495. if (!mos7720_port->open) {
  1496. dbg("%s - port not opened", __func__);
  1497. return;
  1498. }
  1499. dbg("%s\n", "setting termios - ASPIRE");
  1500. cflag = tty->termios->c_cflag;
  1501. dbg("%s - cflag %08x iflag %08x", __func__,
  1502. tty->termios->c_cflag,
  1503. RELEVANT_IFLAG(tty->termios->c_iflag));
  1504. dbg("%s - old cflag %08x old iflag %08x", __func__,
  1505. old_termios->c_cflag,
  1506. RELEVANT_IFLAG(old_termios->c_iflag));
  1507. dbg("%s - port %d", __func__, port->number);
  1508. /* change the port settings to the new ones specified */
  1509. change_port_settings(tty, mos7720_port, old_termios);
  1510. if (port->read_urb->status != -EINPROGRESS) {
  1511. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1512. if (status)
  1513. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1514. status);
  1515. }
  1516. }
  1517. /*
  1518. * get_lsr_info - get line status register info
  1519. *
  1520. * Purpose: Let user call ioctl() to get info when the UART physically
  1521. * is emptied. On bus types like RS485, the transmitter must
  1522. * release the bus after transmitting. This must be done when
  1523. * the transmit shift register is empty, not be done when the
  1524. * transmit holding register is empty. This functionality
  1525. * allows an RS485 driver to be written in user space.
  1526. */
  1527. static int get_lsr_info(struct tty_struct *tty,
  1528. struct moschip_port *mos7720_port, unsigned int __user *value)
  1529. {
  1530. struct usb_serial_port *port = tty->driver_data;
  1531. unsigned int result = 0;
  1532. unsigned char data = 0;
  1533. int port_number = port->number - port->serial->minor;
  1534. int count;
  1535. count = mos7720_chars_in_buffer(tty);
  1536. if (count == 0) {
  1537. read_mos_reg(port->serial, port_number, LSR, &data);
  1538. if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
  1539. == (UART_LSR_TEMT | UART_LSR_THRE)) {
  1540. dbg("%s -- Empty", __func__);
  1541. result = TIOCSER_TEMT;
  1542. }
  1543. }
  1544. if (copy_to_user(value, &result, sizeof(int)))
  1545. return -EFAULT;
  1546. return 0;
  1547. }
  1548. static int mos7720_tiocmget(struct tty_struct *tty)
  1549. {
  1550. struct usb_serial_port *port = tty->driver_data;
  1551. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1552. unsigned int result = 0;
  1553. unsigned int mcr ;
  1554. unsigned int msr ;
  1555. dbg("%s - port %d", __func__, port->number);
  1556. mcr = mos7720_port->shadowMCR;
  1557. msr = mos7720_port->shadowMSR;
  1558. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1559. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1560. | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1561. | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
  1562. | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1563. | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1564. dbg("%s -- %x", __func__, result);
  1565. return result;
  1566. }
  1567. static int mos7720_tiocmset(struct tty_struct *tty,
  1568. unsigned int set, unsigned int clear)
  1569. {
  1570. struct usb_serial_port *port = tty->driver_data;
  1571. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1572. unsigned int mcr ;
  1573. dbg("%s - port %d", __func__, port->number);
  1574. dbg("he was at tiocmset");
  1575. mcr = mos7720_port->shadowMCR;
  1576. if (set & TIOCM_RTS)
  1577. mcr |= UART_MCR_RTS;
  1578. if (set & TIOCM_DTR)
  1579. mcr |= UART_MCR_DTR;
  1580. if (set & TIOCM_LOOP)
  1581. mcr |= UART_MCR_LOOP;
  1582. if (clear & TIOCM_RTS)
  1583. mcr &= ~UART_MCR_RTS;
  1584. if (clear & TIOCM_DTR)
  1585. mcr &= ~UART_MCR_DTR;
  1586. if (clear & TIOCM_LOOP)
  1587. mcr &= ~UART_MCR_LOOP;
  1588. mos7720_port->shadowMCR = mcr;
  1589. write_mos_reg(port->serial, port->number - port->serial->minor,
  1590. MCR, mos7720_port->shadowMCR);
  1591. return 0;
  1592. }
  1593. static int mos7720_get_icount(struct tty_struct *tty,
  1594. struct serial_icounter_struct *icount)
  1595. {
  1596. struct usb_serial_port *port = tty->driver_data;
  1597. struct moschip_port *mos7720_port;
  1598. struct async_icount cnow;
  1599. mos7720_port = usb_get_serial_port_data(port);
  1600. cnow = mos7720_port->icount;
  1601. icount->cts = cnow.cts;
  1602. icount->dsr = cnow.dsr;
  1603. icount->rng = cnow.rng;
  1604. icount->dcd = cnow.dcd;
  1605. icount->rx = cnow.rx;
  1606. icount->tx = cnow.tx;
  1607. icount->frame = cnow.frame;
  1608. icount->overrun = cnow.overrun;
  1609. icount->parity = cnow.parity;
  1610. icount->brk = cnow.brk;
  1611. icount->buf_overrun = cnow.buf_overrun;
  1612. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  1613. port->number, icount->rx, icount->tx);
  1614. return 0;
  1615. }
  1616. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1617. unsigned int __user *value)
  1618. {
  1619. unsigned int mcr;
  1620. unsigned int arg;
  1621. struct usb_serial_port *port;
  1622. if (mos7720_port == NULL)
  1623. return -1;
  1624. port = (struct usb_serial_port *)mos7720_port->port;
  1625. mcr = mos7720_port->shadowMCR;
  1626. if (copy_from_user(&arg, value, sizeof(int)))
  1627. return -EFAULT;
  1628. switch (cmd) {
  1629. case TIOCMBIS:
  1630. if (arg & TIOCM_RTS)
  1631. mcr |= UART_MCR_RTS;
  1632. if (arg & TIOCM_DTR)
  1633. mcr |= UART_MCR_RTS;
  1634. if (arg & TIOCM_LOOP)
  1635. mcr |= UART_MCR_LOOP;
  1636. break;
  1637. case TIOCMBIC:
  1638. if (arg & TIOCM_RTS)
  1639. mcr &= ~UART_MCR_RTS;
  1640. if (arg & TIOCM_DTR)
  1641. mcr &= ~UART_MCR_RTS;
  1642. if (arg & TIOCM_LOOP)
  1643. mcr &= ~UART_MCR_LOOP;
  1644. break;
  1645. }
  1646. mos7720_port->shadowMCR = mcr;
  1647. write_mos_reg(port->serial, port->number - port->serial->minor,
  1648. MCR, mos7720_port->shadowMCR);
  1649. return 0;
  1650. }
  1651. static int get_serial_info(struct moschip_port *mos7720_port,
  1652. struct serial_struct __user *retinfo)
  1653. {
  1654. struct serial_struct tmp;
  1655. if (!retinfo)
  1656. return -EFAULT;
  1657. memset(&tmp, 0, sizeof(tmp));
  1658. tmp.type = PORT_16550A;
  1659. tmp.line = mos7720_port->port->serial->minor;
  1660. tmp.port = mos7720_port->port->number;
  1661. tmp.irq = 0;
  1662. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1663. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1664. tmp.baud_base = 9600;
  1665. tmp.close_delay = 5*HZ;
  1666. tmp.closing_wait = 30*HZ;
  1667. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1668. return -EFAULT;
  1669. return 0;
  1670. }
  1671. static int mos7720_ioctl(struct tty_struct *tty,
  1672. unsigned int cmd, unsigned long arg)
  1673. {
  1674. struct usb_serial_port *port = tty->driver_data;
  1675. struct moschip_port *mos7720_port;
  1676. struct async_icount cnow;
  1677. struct async_icount cprev;
  1678. mos7720_port = usb_get_serial_port_data(port);
  1679. if (mos7720_port == NULL)
  1680. return -ENODEV;
  1681. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1682. switch (cmd) {
  1683. case TIOCSERGETLSR:
  1684. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1685. return get_lsr_info(tty, mos7720_port,
  1686. (unsigned int __user *)arg);
  1687. /* FIXME: These should be using the mode methods */
  1688. case TIOCMBIS:
  1689. case TIOCMBIC:
  1690. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
  1691. __func__, port->number);
  1692. return set_modem_info(mos7720_port, cmd,
  1693. (unsigned int __user *)arg);
  1694. case TIOCGSERIAL:
  1695. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1696. return get_serial_info(mos7720_port,
  1697. (struct serial_struct __user *)arg);
  1698. case TIOCMIWAIT:
  1699. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1700. cprev = mos7720_port->icount;
  1701. while (1) {
  1702. if (signal_pending(current))
  1703. return -ERESTARTSYS;
  1704. cnow = mos7720_port->icount;
  1705. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1706. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1707. return -EIO; /* no change => error */
  1708. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1709. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1710. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1711. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1712. return 0;
  1713. }
  1714. cprev = cnow;
  1715. }
  1716. /* NOTREACHED */
  1717. break;
  1718. }
  1719. return -ENOIOCTLCMD;
  1720. }
  1721. static int mos7720_startup(struct usb_serial *serial)
  1722. {
  1723. struct moschip_port *mos7720_port;
  1724. struct usb_device *dev;
  1725. int i;
  1726. char data;
  1727. u16 product;
  1728. int ret_val;
  1729. dbg("%s: Entering ..........", __func__);
  1730. if (!serial) {
  1731. dbg("Invalid Handler");
  1732. return -ENODEV;
  1733. }
  1734. product = le16_to_cpu(serial->dev->descriptor.idProduct);
  1735. dev = serial->dev;
  1736. /*
  1737. * The 7715 uses the first bulk in/out endpoint pair for the parallel
  1738. * port, and the second for the serial port. Because the usbserial core
  1739. * assumes both pairs are serial ports, we must engage in a bit of
  1740. * subterfuge and swap the pointers for ports 0 and 1 in order to make
  1741. * port 0 point to the serial port. However, both moschip devices use a
  1742. * single interrupt-in endpoint for both ports (as mentioned a little
  1743. * further down), and this endpoint was assigned to port 0. So after
  1744. * the swap, we must copy the interrupt endpoint elements from port 1
  1745. * (as newly assigned) to port 0, and null out port 1 pointers.
  1746. */
  1747. if (product == MOSCHIP_DEVICE_ID_7715) {
  1748. struct usb_serial_port *tmp = serial->port[0];
  1749. serial->port[0] = serial->port[1];
  1750. serial->port[1] = tmp;
  1751. serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
  1752. serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
  1753. serial->port[0]->interrupt_in_endpointAddress =
  1754. tmp->interrupt_in_endpointAddress;
  1755. serial->port[1]->interrupt_in_urb = NULL;
  1756. serial->port[1]->interrupt_in_buffer = NULL;
  1757. }
  1758. /* set up serial port private structures */
  1759. for (i = 0; i < serial->num_ports; ++i) {
  1760. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1761. if (mos7720_port == NULL) {
  1762. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1763. return -ENOMEM;
  1764. }
  1765. /* Initialize all port interrupt end point to port 0 int
  1766. * endpoint. Our device has only one interrupt endpoint
  1767. * common to all ports */
  1768. serial->port[i]->interrupt_in_endpointAddress =
  1769. serial->port[0]->interrupt_in_endpointAddress;
  1770. mos7720_port->port = serial->port[i];
  1771. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1772. dbg("port number is %d", serial->port[i]->number);
  1773. dbg("serial number is %d", serial->minor);
  1774. }
  1775. /* setting configuration feature to one */
  1776. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1777. (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
  1778. /* start the interrupt urb */
  1779. ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
  1780. if (ret_val)
  1781. dev_err(&dev->dev,
  1782. "%s - Error %d submitting control urb\n",
  1783. __func__, ret_val);
  1784. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  1785. if (product == MOSCHIP_DEVICE_ID_7715) {
  1786. ret_val = mos7715_parport_init(serial);
  1787. if (ret_val < 0)
  1788. return ret_val;
  1789. }
  1790. #endif
  1791. /* LSR For Port 1 */
  1792. read_mos_reg(serial, 0, LSR, &data);
  1793. dbg("LSR:%x", data);
  1794. return 0;
  1795. }
  1796. static void mos7720_release(struct usb_serial *serial)
  1797. {
  1798. int i;
  1799. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  1800. /* close the parallel port */
  1801. if (le16_to_cpu(serial->dev->descriptor.idProduct)
  1802. == MOSCHIP_DEVICE_ID_7715) {
  1803. struct urbtracker *urbtrack;
  1804. unsigned long flags;
  1805. struct mos7715_parport *mos_parport =
  1806. usb_get_serial_data(serial);
  1807. /* prevent NULL ptr dereference in port callbacks */
  1808. spin_lock(&release_lock);
  1809. mos_parport->pp->private_data = NULL;
  1810. spin_unlock(&release_lock);
  1811. /* wait for synchronous usb calls to return */
  1812. if (mos_parport->msg_pending)
  1813. wait_for_completion_timeout(&mos_parport->syncmsg_compl,
  1814. MOS_WDR_TIMEOUT);
  1815. parport_remove_port(mos_parport->pp);
  1816. usb_set_serial_data(serial, NULL);
  1817. mos_parport->serial = NULL;
  1818. /* if tasklet currently scheduled, wait for it to complete */
  1819. tasklet_kill(&mos_parport->urb_tasklet);
  1820. /* unlink any urbs sent by the tasklet */
  1821. spin_lock_irqsave(&mos_parport->listlock, flags);
  1822. list_for_each_entry(urbtrack,
  1823. &mos_parport->active_urbs,
  1824. urblist_entry)
  1825. usb_unlink_urb(urbtrack->urb);
  1826. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  1827. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  1828. }
  1829. #endif
  1830. /* free private structure allocated for serial port */
  1831. for (i = 0; i < serial->num_ports; ++i)
  1832. kfree(usb_get_serial_port_data(serial->port[i]));
  1833. }
  1834. static struct usb_serial_driver moschip7720_2port_driver = {
  1835. .driver = {
  1836. .owner = THIS_MODULE,
  1837. .name = "moschip7720",
  1838. },
  1839. .description = "Moschip 2 port adapter",
  1840. .id_table = id_table,
  1841. .calc_num_ports = mos77xx_calc_num_ports,
  1842. .open = mos7720_open,
  1843. .close = mos7720_close,
  1844. .throttle = mos7720_throttle,
  1845. .unthrottle = mos7720_unthrottle,
  1846. .probe = mos77xx_probe,
  1847. .attach = mos7720_startup,
  1848. .release = mos7720_release,
  1849. .ioctl = mos7720_ioctl,
  1850. .tiocmget = mos7720_tiocmget,
  1851. .tiocmset = mos7720_tiocmset,
  1852. .get_icount = mos7720_get_icount,
  1853. .set_termios = mos7720_set_termios,
  1854. .write = mos7720_write,
  1855. .write_room = mos7720_write_room,
  1856. .chars_in_buffer = mos7720_chars_in_buffer,
  1857. .break_ctl = mos7720_break,
  1858. .read_bulk_callback = mos7720_bulk_in_callback,
  1859. .read_int_callback = NULL /* dynamically assigned in probe() */
  1860. };
  1861. static struct usb_serial_driver * const serial_drivers[] = {
  1862. &moschip7720_2port_driver, NULL
  1863. };
  1864. module_usb_serial_driver(serial_drivers, id_table);
  1865. MODULE_AUTHOR(DRIVER_AUTHOR);
  1866. MODULE_DESCRIPTION(DRIVER_DESC);
  1867. MODULE_LICENSE("GPL");
  1868. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1869. MODULE_PARM_DESC(debug, "Debug enabled or not");