mos7720.c 58 KB

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