mos7720.c 57 KB

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