mos7720.c 58 KB

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