mos7720.c 59 KB

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