mos7720.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mos7720.c
  4. * Controls the Moschip 7720 usb to dual port serial converter
  5. *
  6. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  7. *
  8. * Developed by:
  9. * Vijaya Kumar <[email protected]>
  10. * Ajay Kumar <[email protected]>
  11. * Gurudeva <[email protected]>
  12. *
  13. * Cleaned up from the original by:
  14. * Greg Kroah-Hartman <[email protected]>
  15. *
  16. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  17. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  18. * Copyright (C) 2001-2002 Greg Kroah-Hartman <[email protected]>
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/slab.h>
  23. #include <linux/tty.h>
  24. #include <linux/tty_driver.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/module.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/serial.h>
  29. #include <linux/serial_reg.h>
  30. #include <linux/usb.h>
  31. #include <linux/usb/serial.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/parport.h>
  34. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  35. #define DRIVER_DESC "Moschip USB Serial Driver"
  36. /* default urb timeout */
  37. #define MOS_WDR_TIMEOUT 5000
  38. #define MOS_MAX_PORT 0x02
  39. #define MOS_WRITE 0x0E
  40. #define MOS_READ 0x0D
  41. /* Interrupt Routines Defines */
  42. #define SERIAL_IIR_RLS 0x06
  43. #define SERIAL_IIR_RDA 0x04
  44. #define SERIAL_IIR_CTI 0x0c
  45. #define SERIAL_IIR_THR 0x02
  46. #define SERIAL_IIR_MS 0x00
  47. #define NUM_URBS 16 /* URB Count */
  48. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  49. /* This structure holds all of the local serial port information */
  50. struct moschip_port {
  51. __u8 shadowLCR; /* last LCR value received */
  52. __u8 shadowMCR; /* last MCR value received */
  53. __u8 shadowMSR; /* last MSR value received */
  54. char open;
  55. struct usb_serial_port *port; /* loop back to the owner */
  56. struct urb *write_urb_pool[NUM_URBS];
  57. };
  58. #define USB_VENDOR_ID_MOSCHIP 0x9710
  59. #define MOSCHIP_DEVICE_ID_7720 0x7720
  60. #define MOSCHIP_DEVICE_ID_7715 0x7715
  61. static const struct usb_device_id id_table[] = {
  62. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
  63. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
  64. { } /* terminating entry */
  65. };
  66. MODULE_DEVICE_TABLE(usb, id_table);
  67. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  68. /* initial values for parport regs */
  69. #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
  70. #define ECR_INIT_VAL 0x00 /* SPP mode */
  71. enum mos7715_pp_modes {
  72. SPP = 0<<5,
  73. PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
  74. PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
  75. };
  76. struct mos7715_parport {
  77. struct parport *pp; /* back to containing struct */
  78. struct kref ref_count; /* to instance of this struct */
  79. bool msg_pending; /* usb sync call pending */
  80. struct completion syncmsg_compl; /* usb sync call completed */
  81. struct work_struct work; /* restore deferred writes */
  82. struct usb_serial *serial; /* back to containing struct */
  83. __u8 shadowECR; /* parallel port regs... */
  84. __u8 shadowDCR;
  85. atomic_t shadowDSR; /* updated in int-in callback */
  86. };
  87. /* lock guards against dereferencing NULL ptr in parport ops callbacks */
  88. static DEFINE_SPINLOCK(release_lock);
  89. #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
  90. static const unsigned int dummy; /* for clarity in register access fns */
  91. enum mos_regs {
  92. MOS7720_THR, /* serial port regs */
  93. MOS7720_RHR,
  94. MOS7720_IER,
  95. MOS7720_FCR,
  96. MOS7720_ISR,
  97. MOS7720_LCR,
  98. MOS7720_MCR,
  99. MOS7720_LSR,
  100. MOS7720_MSR,
  101. MOS7720_SPR,
  102. MOS7720_DLL,
  103. MOS7720_DLM,
  104. MOS7720_DPR, /* parallel port regs */
  105. MOS7720_DSR,
  106. MOS7720_DCR,
  107. MOS7720_ECR,
  108. MOS7720_SP1_REG, /* device control regs */
  109. MOS7720_SP2_REG, /* serial port 2 (7720 only) */
  110. MOS7720_PP_REG,
  111. MOS7720_SP_CONTROL_REG,
  112. };
  113. /*
  114. * Return the correct value for the Windex field of the setup packet
  115. * for a control endpoint message. See the 7715 datasheet.
  116. */
  117. static inline __u16 get_reg_index(enum mos_regs reg)
  118. {
  119. static const __u16 mos7715_index_lookup_table[] = {
  120. 0x00, /* MOS7720_THR */
  121. 0x00, /* MOS7720_RHR */
  122. 0x01, /* MOS7720_IER */
  123. 0x02, /* MOS7720_FCR */
  124. 0x02, /* MOS7720_ISR */
  125. 0x03, /* MOS7720_LCR */
  126. 0x04, /* MOS7720_MCR */
  127. 0x05, /* MOS7720_LSR */
  128. 0x06, /* MOS7720_MSR */
  129. 0x07, /* MOS7720_SPR */
  130. 0x00, /* MOS7720_DLL */
  131. 0x01, /* MOS7720_DLM */
  132. 0x00, /* MOS7720_DPR */
  133. 0x01, /* MOS7720_DSR */
  134. 0x02, /* MOS7720_DCR */
  135. 0x0a, /* MOS7720_ECR */
  136. 0x01, /* MOS7720_SP1_REG */
  137. 0x02, /* MOS7720_SP2_REG (7720 only) */
  138. 0x04, /* MOS7720_PP_REG (7715 only) */
  139. 0x08, /* MOS7720_SP_CONTROL_REG */
  140. };
  141. return mos7715_index_lookup_table[reg];
  142. }
  143. /*
  144. * Return the correct value for the upper byte of the Wvalue field of
  145. * the setup packet for a control endpoint message.
  146. */
  147. static inline __u16 get_reg_value(enum mos_regs reg,
  148. unsigned int serial_portnum)
  149. {
  150. if (reg >= MOS7720_SP1_REG) /* control reg */
  151. return 0x0000;
  152. else if (reg >= MOS7720_DPR) /* parallel port reg (7715 only) */
  153. return 0x0100;
  154. else /* serial port reg */
  155. return (serial_portnum + 2) << 8;
  156. }
  157. /*
  158. * Write data byte to the specified device register. The data is embedded in
  159. * the value field of the setup packet. serial_portnum is ignored for registers
  160. * not specific to a particular serial port.
  161. */
  162. static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
  163. enum mos_regs reg, __u8 data)
  164. {
  165. struct usb_device *usbdev = serial->dev;
  166. unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
  167. __u8 request = (__u8)0x0e;
  168. __u8 requesttype = (__u8)0x40;
  169. __u16 index = get_reg_index(reg);
  170. __u16 value = get_reg_value(reg, serial_portnum) + data;
  171. int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
  172. index, NULL, 0, MOS_WDR_TIMEOUT);
  173. if (status < 0)
  174. dev_err(&usbdev->dev,
  175. "mos7720: usb_control_msg() failed: %d\n", status);
  176. return status;
  177. }
  178. /*
  179. * Read data byte from the specified device register. The data returned by the
  180. * device is embedded in the value field of the setup packet. serial_portnum is
  181. * ignored for registers that are not specific to a particular serial port.
  182. */
  183. static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
  184. enum mos_regs reg, __u8 *data)
  185. {
  186. struct usb_device *usbdev = serial->dev;
  187. unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
  188. __u8 request = (__u8)0x0d;
  189. __u8 requesttype = (__u8)0xc0;
  190. __u16 index = get_reg_index(reg);
  191. __u16 value = get_reg_value(reg, serial_portnum);
  192. u8 *buf;
  193. int status;
  194. buf = kmalloc(1, GFP_KERNEL);
  195. if (!buf) {
  196. *data = 0;
  197. return -ENOMEM;
  198. }
  199. status = usb_control_msg(usbdev, pipe, request, requesttype, value,
  200. index, buf, 1, MOS_WDR_TIMEOUT);
  201. if (status == 1) {
  202. *data = *buf;
  203. } else {
  204. dev_err(&usbdev->dev,
  205. "mos7720: usb_control_msg() failed: %d\n", status);
  206. if (status >= 0)
  207. status = -EIO;
  208. *data = 0;
  209. }
  210. kfree(buf);
  211. return status;
  212. }
  213. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  214. static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
  215. enum mos7715_pp_modes mode)
  216. {
  217. mos_parport->shadowECR = mode;
  218. write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
  219. mos_parport->shadowECR);
  220. return 0;
  221. }
  222. static void destroy_mos_parport(struct kref *kref)
  223. {
  224. struct mos7715_parport *mos_parport =
  225. container_of(kref, struct mos7715_parport, ref_count);
  226. kfree(mos_parport);
  227. }
  228. /*
  229. * This is the common top part of all parallel port callback operations that
  230. * send synchronous messages to the device. This implements convoluted locking
  231. * that avoids two scenarios: (1) a port operation is called after usbserial
  232. * has called our release function, at which point struct mos7715_parport has
  233. * been destroyed, and (2) the device has been disconnected, but usbserial has
  234. * not called the release function yet because someone has a serial port open.
  235. * The shared release_lock prevents the first, and the mutex and disconnected
  236. * flag maintained by usbserial covers the second. We also use the msg_pending
  237. * flag to ensure that all synchronous usb message calls have completed before
  238. * our release function can return.
  239. */
  240. static int parport_prologue(struct parport *pp)
  241. {
  242. struct mos7715_parport *mos_parport;
  243. spin_lock(&release_lock);
  244. mos_parport = pp->private_data;
  245. if (unlikely(mos_parport == NULL)) {
  246. /* release fn called, port struct destroyed */
  247. spin_unlock(&release_lock);
  248. return -1;
  249. }
  250. mos_parport->msg_pending = true; /* synch usb call pending */
  251. reinit_completion(&mos_parport->syncmsg_compl);
  252. spin_unlock(&release_lock);
  253. /* ensure writes from restore are submitted before new requests */
  254. if (work_pending(&mos_parport->work))
  255. flush_work(&mos_parport->work);
  256. mutex_lock(&mos_parport->serial->disc_mutex);
  257. if (mos_parport->serial->disconnected) {
  258. /* device disconnected */
  259. mutex_unlock(&mos_parport->serial->disc_mutex);
  260. mos_parport->msg_pending = false;
  261. complete(&mos_parport->syncmsg_compl);
  262. return -1;
  263. }
  264. return 0;
  265. }
  266. /*
  267. * This is the common bottom part of all parallel port functions that send
  268. * synchronous messages to the device.
  269. */
  270. static inline void parport_epilogue(struct parport *pp)
  271. {
  272. struct mos7715_parport *mos_parport = pp->private_data;
  273. mutex_unlock(&mos_parport->serial->disc_mutex);
  274. mos_parport->msg_pending = false;
  275. complete(&mos_parport->syncmsg_compl);
  276. }
  277. static void deferred_restore_writes(struct work_struct *work)
  278. {
  279. struct mos7715_parport *mos_parport;
  280. mos_parport = container_of(work, struct mos7715_parport, work);
  281. mutex_lock(&mos_parport->serial->disc_mutex);
  282. /* if device disconnected, game over */
  283. if (mos_parport->serial->disconnected)
  284. goto done;
  285. write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
  286. mos_parport->shadowDCR);
  287. write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
  288. mos_parport->shadowECR);
  289. done:
  290. mutex_unlock(&mos_parport->serial->disc_mutex);
  291. }
  292. static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
  293. {
  294. struct mos7715_parport *mos_parport = pp->private_data;
  295. if (parport_prologue(pp) < 0)
  296. return;
  297. mos7715_change_mode(mos_parport, SPP);
  298. write_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, (__u8)d);
  299. parport_epilogue(pp);
  300. }
  301. static unsigned char parport_mos7715_read_data(struct parport *pp)
  302. {
  303. struct mos7715_parport *mos_parport = pp->private_data;
  304. unsigned char d;
  305. if (parport_prologue(pp) < 0)
  306. return 0;
  307. read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d);
  308. parport_epilogue(pp);
  309. return d;
  310. }
  311. static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
  312. {
  313. struct mos7715_parport *mos_parport = pp->private_data;
  314. __u8 data;
  315. if (parport_prologue(pp) < 0)
  316. return;
  317. data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
  318. write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, data);
  319. mos_parport->shadowDCR = data;
  320. parport_epilogue(pp);
  321. }
  322. static unsigned char parport_mos7715_read_control(struct parport *pp)
  323. {
  324. struct mos7715_parport *mos_parport;
  325. __u8 dcr;
  326. spin_lock(&release_lock);
  327. mos_parport = pp->private_data;
  328. if (unlikely(mos_parport == NULL)) {
  329. spin_unlock(&release_lock);
  330. return 0;
  331. }
  332. dcr = mos_parport->shadowDCR & 0x0f;
  333. spin_unlock(&release_lock);
  334. return dcr;
  335. }
  336. static unsigned char parport_mos7715_frob_control(struct parport *pp,
  337. unsigned char mask,
  338. unsigned char val)
  339. {
  340. struct mos7715_parport *mos_parport = pp->private_data;
  341. __u8 dcr;
  342. mask &= 0x0f;
  343. val &= 0x0f;
  344. if (parport_prologue(pp) < 0)
  345. return 0;
  346. mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
  347. write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
  348. mos_parport->shadowDCR);
  349. dcr = mos_parport->shadowDCR & 0x0f;
  350. parport_epilogue(pp);
  351. return dcr;
  352. }
  353. static unsigned char parport_mos7715_read_status(struct parport *pp)
  354. {
  355. unsigned char status;
  356. struct mos7715_parport *mos_parport;
  357. spin_lock(&release_lock);
  358. mos_parport = pp->private_data;
  359. if (unlikely(mos_parport == NULL)) { /* release called */
  360. spin_unlock(&release_lock);
  361. return 0;
  362. }
  363. status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
  364. spin_unlock(&release_lock);
  365. return status;
  366. }
  367. static void parport_mos7715_enable_irq(struct parport *pp)
  368. {
  369. }
  370. static void parport_mos7715_disable_irq(struct parport *pp)
  371. {
  372. }
  373. static void parport_mos7715_data_forward(struct parport *pp)
  374. {
  375. struct mos7715_parport *mos_parport = pp->private_data;
  376. if (parport_prologue(pp) < 0)
  377. return;
  378. mos7715_change_mode(mos_parport, PS2);
  379. mos_parport->shadowDCR &= ~0x20;
  380. write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
  381. mos_parport->shadowDCR);
  382. parport_epilogue(pp);
  383. }
  384. static void parport_mos7715_data_reverse(struct parport *pp)
  385. {
  386. struct mos7715_parport *mos_parport = pp->private_data;
  387. if (parport_prologue(pp) < 0)
  388. return;
  389. mos7715_change_mode(mos_parport, PS2);
  390. mos_parport->shadowDCR |= 0x20;
  391. write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
  392. mos_parport->shadowDCR);
  393. parport_epilogue(pp);
  394. }
  395. static void parport_mos7715_init_state(struct pardevice *dev,
  396. struct parport_state *s)
  397. {
  398. s->u.pc.ctr = DCR_INIT_VAL;
  399. s->u.pc.ecr = ECR_INIT_VAL;
  400. }
  401. /* N.B. Parport core code requires that this function not block */
  402. static void parport_mos7715_save_state(struct parport *pp,
  403. struct parport_state *s)
  404. {
  405. struct mos7715_parport *mos_parport;
  406. spin_lock(&release_lock);
  407. mos_parport = pp->private_data;
  408. if (unlikely(mos_parport == NULL)) { /* release called */
  409. spin_unlock(&release_lock);
  410. return;
  411. }
  412. s->u.pc.ctr = mos_parport->shadowDCR;
  413. s->u.pc.ecr = mos_parport->shadowECR;
  414. spin_unlock(&release_lock);
  415. }
  416. /* N.B. Parport core code requires that this function not block */
  417. static void parport_mos7715_restore_state(struct parport *pp,
  418. struct parport_state *s)
  419. {
  420. struct mos7715_parport *mos_parport;
  421. spin_lock(&release_lock);
  422. mos_parport = pp->private_data;
  423. if (unlikely(mos_parport == NULL)) { /* release called */
  424. spin_unlock(&release_lock);
  425. return;
  426. }
  427. mos_parport->shadowDCR = s->u.pc.ctr;
  428. mos_parport->shadowECR = s->u.pc.ecr;
  429. schedule_work(&mos_parport->work);
  430. spin_unlock(&release_lock);
  431. }
  432. static size_t parport_mos7715_write_compat(struct parport *pp,
  433. const void *buffer,
  434. size_t len, int flags)
  435. {
  436. int retval;
  437. struct mos7715_parport *mos_parport = pp->private_data;
  438. int actual_len;
  439. if (parport_prologue(pp) < 0)
  440. return 0;
  441. mos7715_change_mode(mos_parport, PPF);
  442. retval = usb_bulk_msg(mos_parport->serial->dev,
  443. usb_sndbulkpipe(mos_parport->serial->dev, 2),
  444. (void *)buffer, len, &actual_len,
  445. MOS_WDR_TIMEOUT);
  446. parport_epilogue(pp);
  447. if (retval) {
  448. dev_err(&mos_parport->serial->dev->dev,
  449. "mos7720: usb_bulk_msg() failed: %d\n", retval);
  450. return 0;
  451. }
  452. return actual_len;
  453. }
  454. static struct parport_operations parport_mos7715_ops = {
  455. .owner = THIS_MODULE,
  456. .write_data = parport_mos7715_write_data,
  457. .read_data = parport_mos7715_read_data,
  458. .write_control = parport_mos7715_write_control,
  459. .read_control = parport_mos7715_read_control,
  460. .frob_control = parport_mos7715_frob_control,
  461. .read_status = parport_mos7715_read_status,
  462. .enable_irq = parport_mos7715_enable_irq,
  463. .disable_irq = parport_mos7715_disable_irq,
  464. .data_forward = parport_mos7715_data_forward,
  465. .data_reverse = parport_mos7715_data_reverse,
  466. .init_state = parport_mos7715_init_state,
  467. .save_state = parport_mos7715_save_state,
  468. .restore_state = parport_mos7715_restore_state,
  469. .compat_write_data = parport_mos7715_write_compat,
  470. .nibble_read_data = parport_ieee1284_read_nibble,
  471. .byte_read_data = parport_ieee1284_read_byte,
  472. };
  473. /*
  474. * Allocate and initialize parallel port control struct, initialize
  475. * the parallel port hardware device, and register with the parport subsystem.
  476. */
  477. static int mos7715_parport_init(struct usb_serial *serial)
  478. {
  479. struct mos7715_parport *mos_parport;
  480. /* allocate and initialize parallel port control struct */
  481. mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
  482. if (!mos_parport)
  483. return -ENOMEM;
  484. mos_parport->msg_pending = false;
  485. kref_init(&mos_parport->ref_count);
  486. usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
  487. mos_parport->serial = serial;
  488. INIT_WORK(&mos_parport->work, deferred_restore_writes);
  489. init_completion(&mos_parport->syncmsg_compl);
  490. /* cycle parallel port reset bit */
  491. write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x80);
  492. write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x00);
  493. /* initialize device registers */
  494. mos_parport->shadowDCR = DCR_INIT_VAL;
  495. write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
  496. mos_parport->shadowDCR);
  497. mos_parport->shadowECR = ECR_INIT_VAL;
  498. write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
  499. mos_parport->shadowECR);
  500. /* register with parport core */
  501. mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
  502. PARPORT_DMA_NONE,
  503. &parport_mos7715_ops);
  504. if (mos_parport->pp == NULL) {
  505. dev_err(&serial->interface->dev,
  506. "Could not register parport\n");
  507. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  508. return -EIO;
  509. }
  510. mos_parport->pp->private_data = mos_parport;
  511. mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
  512. mos_parport->pp->dev = &serial->interface->dev;
  513. parport_announce_port(mos_parport->pp);
  514. return 0;
  515. }
  516. #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
  517. /*
  518. * mos7720_interrupt_callback
  519. * this is the callback function for when we have received data on the
  520. * interrupt endpoint.
  521. */
  522. static void mos7720_interrupt_callback(struct urb *urb)
  523. {
  524. int result;
  525. int length;
  526. int status = urb->status;
  527. struct device *dev = &urb->dev->dev;
  528. __u8 *data;
  529. __u8 sp1;
  530. __u8 sp2;
  531. switch (status) {
  532. case 0:
  533. /* success */
  534. break;
  535. case -ECONNRESET:
  536. case -ENOENT:
  537. case -ESHUTDOWN:
  538. /* this urb is terminated, clean up */
  539. dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
  540. return;
  541. default:
  542. dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
  543. goto exit;
  544. }
  545. length = urb->actual_length;
  546. data = urb->transfer_buffer;
  547. /* Moschip get 4 bytes
  548. * Byte 1 IIR Port 1 (port.number is 0)
  549. * Byte 2 IIR Port 2 (port.number is 1)
  550. * Byte 3 --------------
  551. * Byte 4 FIFO status for both */
  552. /* the above description is inverted
  553. * oneukum 2007-03-14 */
  554. if (unlikely(length != 4)) {
  555. dev_dbg(dev, "Wrong data !!!\n");
  556. return;
  557. }
  558. sp1 = data[3];
  559. sp2 = data[2];
  560. if ((sp1 | sp2) & 0x01) {
  561. /* No Interrupt Pending in both the ports */
  562. dev_dbg(dev, "No Interrupt !!!\n");
  563. } else {
  564. switch (sp1 & 0x0f) {
  565. case SERIAL_IIR_RLS:
  566. dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
  567. break;
  568. case SERIAL_IIR_CTI:
  569. dev_dbg(dev, "Serial Port 1: Receiver time out\n");
  570. break;
  571. case SERIAL_IIR_MS:
  572. /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
  573. break;
  574. }
  575. switch (sp2 & 0x0f) {
  576. case SERIAL_IIR_RLS:
  577. dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
  578. break;
  579. case SERIAL_IIR_CTI:
  580. dev_dbg(dev, "Serial Port 2: Receiver time out\n");
  581. break;
  582. case SERIAL_IIR_MS:
  583. /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
  584. break;
  585. }
  586. }
  587. exit:
  588. result = usb_submit_urb(urb, GFP_ATOMIC);
  589. if (result)
  590. dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
  591. }
  592. /*
  593. * mos7715_interrupt_callback
  594. * this is the 7715's callback function for when we have received data on
  595. * the interrupt endpoint.
  596. */
  597. static void mos7715_interrupt_callback(struct urb *urb)
  598. {
  599. int result;
  600. int length;
  601. int status = urb->status;
  602. struct device *dev = &urb->dev->dev;
  603. __u8 *data;
  604. __u8 iir;
  605. switch (status) {
  606. case 0:
  607. /* success */
  608. break;
  609. case -ECONNRESET:
  610. case -ENOENT:
  611. case -ESHUTDOWN:
  612. case -ENODEV:
  613. /* this urb is terminated, clean up */
  614. dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
  615. return;
  616. default:
  617. dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
  618. goto exit;
  619. }
  620. length = urb->actual_length;
  621. data = urb->transfer_buffer;
  622. /* Structure of data from 7715 device:
  623. * Byte 1: IIR serial Port
  624. * Byte 2: unused
  625. * Byte 2: DSR parallel port
  626. * Byte 4: FIFO status for both */
  627. if (unlikely(length != 4)) {
  628. dev_dbg(dev, "Wrong data !!!\n");
  629. return;
  630. }
  631. iir = data[0];
  632. if (!(iir & 0x01)) { /* serial port interrupt pending */
  633. switch (iir & 0x0f) {
  634. case SERIAL_IIR_RLS:
  635. dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
  636. break;
  637. case SERIAL_IIR_CTI:
  638. dev_dbg(dev, "Serial Port: Receiver time out\n");
  639. break;
  640. case SERIAL_IIR_MS:
  641. /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
  642. break;
  643. }
  644. }
  645. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  646. { /* update local copy of DSR reg */
  647. struct usb_serial_port *port = urb->context;
  648. struct mos7715_parport *mos_parport = port->serial->private;
  649. if (unlikely(mos_parport == NULL))
  650. return;
  651. atomic_set(&mos_parport->shadowDSR, data[2]);
  652. }
  653. #endif
  654. exit:
  655. result = usb_submit_urb(urb, GFP_ATOMIC);
  656. if (result)
  657. dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
  658. }
  659. /*
  660. * mos7720_bulk_in_callback
  661. * this is the callback function for when we have received data on the
  662. * bulk in endpoint.
  663. */
  664. static void mos7720_bulk_in_callback(struct urb *urb)
  665. {
  666. int retval;
  667. unsigned char *data ;
  668. struct usb_serial_port *port;
  669. int status = urb->status;
  670. if (status) {
  671. dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
  672. return;
  673. }
  674. port = urb->context;
  675. dev_dbg(&port->dev, "Entering...%s\n", __func__);
  676. data = urb->transfer_buffer;
  677. if (urb->actual_length) {
  678. tty_insert_flip_string(&port->port, data, urb->actual_length);
  679. tty_flip_buffer_push(&port->port);
  680. }
  681. if (port->read_urb->status != -EINPROGRESS) {
  682. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  683. if (retval)
  684. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
  685. }
  686. }
  687. /*
  688. * mos7720_bulk_out_data_callback
  689. * this is the callback function for when we have finished sending serial
  690. * data on the bulk out endpoint.
  691. */
  692. static void mos7720_bulk_out_data_callback(struct urb *urb)
  693. {
  694. struct moschip_port *mos7720_port;
  695. int status = urb->status;
  696. if (status) {
  697. dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
  698. return;
  699. }
  700. mos7720_port = urb->context;
  701. if (!mos7720_port) {
  702. dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
  703. return ;
  704. }
  705. if (mos7720_port->open)
  706. tty_port_tty_wakeup(&mos7720_port->port->port);
  707. }
  708. static int mos77xx_calc_num_ports(struct usb_serial *serial,
  709. struct usb_serial_endpoints *epds)
  710. {
  711. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  712. if (product == MOSCHIP_DEVICE_ID_7715) {
  713. /*
  714. * The 7715 uses the first bulk in/out endpoint pair for the
  715. * parallel port, and the second for the serial port. We swap
  716. * the endpoint descriptors here so that the first and
  717. * only registered port structure uses the serial-port
  718. * endpoints.
  719. */
  720. swap(epds->bulk_in[0], epds->bulk_in[1]);
  721. swap(epds->bulk_out[0], epds->bulk_out[1]);
  722. return 1;
  723. }
  724. return 2;
  725. }
  726. static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
  727. {
  728. struct usb_serial *serial;
  729. struct urb *urb;
  730. struct moschip_port *mos7720_port;
  731. int response;
  732. int port_number;
  733. __u8 data;
  734. int allocated_urbs = 0;
  735. int j;
  736. serial = port->serial;
  737. mos7720_port = usb_get_serial_port_data(port);
  738. if (mos7720_port == NULL)
  739. return -ENODEV;
  740. usb_clear_halt(serial->dev, port->write_urb->pipe);
  741. usb_clear_halt(serial->dev, port->read_urb->pipe);
  742. /* Initialising the write urb pool */
  743. for (j = 0; j < NUM_URBS; ++j) {
  744. urb = usb_alloc_urb(0, GFP_KERNEL);
  745. mos7720_port->write_urb_pool[j] = urb;
  746. if (!urb)
  747. continue;
  748. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  749. GFP_KERNEL);
  750. if (!urb->transfer_buffer) {
  751. usb_free_urb(mos7720_port->write_urb_pool[j]);
  752. mos7720_port->write_urb_pool[j] = NULL;
  753. continue;
  754. }
  755. allocated_urbs++;
  756. }
  757. if (!allocated_urbs)
  758. return -ENOMEM;
  759. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  760. *
  761. * Register Index
  762. * 0 : MOS7720_THR/MOS7720_RHR
  763. * 1 : MOS7720_IER
  764. * 2 : MOS7720_FCR
  765. * 3 : MOS7720_LCR
  766. * 4 : MOS7720_MCR
  767. * 5 : MOS7720_LSR
  768. * 6 : MOS7720_MSR
  769. * 7 : MOS7720_SPR
  770. *
  771. * 0x08 : SP1/2 Control Reg
  772. */
  773. port_number = port->port_number;
  774. read_mos_reg(serial, port_number, MOS7720_LSR, &data);
  775. dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
  776. write_mos_reg(serial, dummy, MOS7720_SP1_REG, 0x02);
  777. write_mos_reg(serial, dummy, MOS7720_SP2_REG, 0x02);
  778. write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
  779. write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
  780. write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
  781. mos7720_port->shadowLCR = 0x03;
  782. write_mos_reg(serial, port_number, MOS7720_LCR,
  783. mos7720_port->shadowLCR);
  784. mos7720_port->shadowMCR = 0x0b;
  785. write_mos_reg(serial, port_number, MOS7720_MCR,
  786. mos7720_port->shadowMCR);
  787. write_mos_reg(serial, port_number, MOS7720_SP_CONTROL_REG, 0x00);
  788. read_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, &data);
  789. data = data | (port->port_number + 1);
  790. write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, data);
  791. mos7720_port->shadowLCR = 0x83;
  792. write_mos_reg(serial, port_number, MOS7720_LCR,
  793. mos7720_port->shadowLCR);
  794. write_mos_reg(serial, port_number, MOS7720_THR, 0x0c);
  795. write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
  796. mos7720_port->shadowLCR = 0x03;
  797. write_mos_reg(serial, port_number, MOS7720_LCR,
  798. mos7720_port->shadowLCR);
  799. write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
  800. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  801. if (response)
  802. dev_err(&port->dev, "%s - Error %d submitting read urb\n",
  803. __func__, response);
  804. /* initialize our port settings */
  805. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  806. /* send a open port command */
  807. mos7720_port->open = 1;
  808. return 0;
  809. }
  810. /*
  811. * mos7720_chars_in_buffer
  812. * this function is called by the tty driver when it wants to know how many
  813. * bytes of data we currently have outstanding in the port (data that has
  814. * been written, but hasn't made it out the port yet)
  815. */
  816. static unsigned int mos7720_chars_in_buffer(struct tty_struct *tty)
  817. {
  818. struct usb_serial_port *port = tty->driver_data;
  819. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  820. int i;
  821. unsigned int chars = 0;
  822. for (i = 0; i < NUM_URBS; ++i) {
  823. if (mos7720_port->write_urb_pool[i] &&
  824. mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  825. chars += URB_TRANSFER_BUFFER_SIZE;
  826. }
  827. dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
  828. return chars;
  829. }
  830. static void mos7720_close(struct usb_serial_port *port)
  831. {
  832. struct usb_serial *serial;
  833. struct moschip_port *mos7720_port;
  834. int j;
  835. serial = port->serial;
  836. mos7720_port = usb_get_serial_port_data(port);
  837. if (mos7720_port == NULL)
  838. return;
  839. for (j = 0; j < NUM_URBS; ++j)
  840. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  841. /* Freeing Write URBs */
  842. for (j = 0; j < NUM_URBS; ++j) {
  843. if (mos7720_port->write_urb_pool[j]) {
  844. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  845. usb_free_urb(mos7720_port->write_urb_pool[j]);
  846. }
  847. }
  848. /* While closing port, shutdown all bulk read, write *
  849. * and interrupt read if they exists, otherwise nop */
  850. usb_kill_urb(port->write_urb);
  851. usb_kill_urb(port->read_urb);
  852. write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00);
  853. write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00);
  854. mos7720_port->open = 0;
  855. }
  856. static void mos7720_break(struct tty_struct *tty, int break_state)
  857. {
  858. struct usb_serial_port *port = tty->driver_data;
  859. unsigned char data;
  860. struct usb_serial *serial;
  861. struct moschip_port *mos7720_port;
  862. serial = port->serial;
  863. mos7720_port = usb_get_serial_port_data(port);
  864. if (mos7720_port == NULL)
  865. return;
  866. if (break_state == -1)
  867. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  868. else
  869. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  870. mos7720_port->shadowLCR = data;
  871. write_mos_reg(serial, port->port_number, MOS7720_LCR,
  872. mos7720_port->shadowLCR);
  873. }
  874. /*
  875. * mos7720_write_room
  876. * this function is called by the tty driver when it wants to know how many
  877. * bytes of data we can accept for a specific port.
  878. */
  879. static unsigned int mos7720_write_room(struct tty_struct *tty)
  880. {
  881. struct usb_serial_port *port = tty->driver_data;
  882. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  883. unsigned int room = 0;
  884. int i;
  885. /* FIXME: Locking */
  886. for (i = 0; i < NUM_URBS; ++i) {
  887. if (mos7720_port->write_urb_pool[i] &&
  888. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  889. room += URB_TRANSFER_BUFFER_SIZE;
  890. }
  891. dev_dbg(&port->dev, "%s - returns %u\n", __func__, room);
  892. return room;
  893. }
  894. static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
  895. const unsigned char *data, int count)
  896. {
  897. int status;
  898. int i;
  899. int bytes_sent = 0;
  900. int transfer_size;
  901. struct moschip_port *mos7720_port;
  902. struct usb_serial *serial;
  903. struct urb *urb;
  904. const unsigned char *current_position = data;
  905. serial = port->serial;
  906. mos7720_port = usb_get_serial_port_data(port);
  907. if (mos7720_port == NULL)
  908. return -ENODEV;
  909. /* try to find a free urb in the list */
  910. urb = NULL;
  911. for (i = 0; i < NUM_URBS; ++i) {
  912. if (mos7720_port->write_urb_pool[i] &&
  913. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  914. urb = mos7720_port->write_urb_pool[i];
  915. dev_dbg(&port->dev, "URB:%d\n", i);
  916. break;
  917. }
  918. }
  919. if (urb == NULL) {
  920. dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
  921. goto exit;
  922. }
  923. if (urb->transfer_buffer == NULL) {
  924. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  925. GFP_ATOMIC);
  926. if (!urb->transfer_buffer) {
  927. bytes_sent = -ENOMEM;
  928. goto exit;
  929. }
  930. }
  931. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  932. memcpy(urb->transfer_buffer, current_position, transfer_size);
  933. usb_serial_debug_data(&port->dev, __func__, transfer_size,
  934. urb->transfer_buffer);
  935. /* fill urb with data and submit */
  936. usb_fill_bulk_urb(urb, serial->dev,
  937. usb_sndbulkpipe(serial->dev,
  938. port->bulk_out_endpointAddress),
  939. urb->transfer_buffer, transfer_size,
  940. mos7720_bulk_out_data_callback, mos7720_port);
  941. /* send it down the pipe */
  942. status = usb_submit_urb(urb, GFP_ATOMIC);
  943. if (status) {
  944. dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
  945. "with status = %d\n", __func__, status);
  946. bytes_sent = status;
  947. goto exit;
  948. }
  949. bytes_sent = transfer_size;
  950. exit:
  951. return bytes_sent;
  952. }
  953. static void mos7720_throttle(struct tty_struct *tty)
  954. {
  955. struct usb_serial_port *port = tty->driver_data;
  956. struct moschip_port *mos7720_port;
  957. int status;
  958. mos7720_port = usb_get_serial_port_data(port);
  959. if (mos7720_port == NULL)
  960. return;
  961. if (!mos7720_port->open) {
  962. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  963. return;
  964. }
  965. /* if we are implementing XON/XOFF, send the stop character */
  966. if (I_IXOFF(tty)) {
  967. unsigned char stop_char = STOP_CHAR(tty);
  968. status = mos7720_write(tty, port, &stop_char, 1);
  969. if (status <= 0)
  970. return;
  971. }
  972. /* if we are implementing RTS/CTS, toggle that line */
  973. if (C_CRTSCTS(tty)) {
  974. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  975. write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
  976. mos7720_port->shadowMCR);
  977. }
  978. }
  979. static void mos7720_unthrottle(struct tty_struct *tty)
  980. {
  981. struct usb_serial_port *port = tty->driver_data;
  982. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  983. int status;
  984. if (mos7720_port == NULL)
  985. return;
  986. if (!mos7720_port->open) {
  987. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  988. return;
  989. }
  990. /* if we are implementing XON/XOFF, send the start character */
  991. if (I_IXOFF(tty)) {
  992. unsigned char start_char = START_CHAR(tty);
  993. status = mos7720_write(tty, port, &start_char, 1);
  994. if (status <= 0)
  995. return;
  996. }
  997. /* if we are implementing RTS/CTS, toggle that line */
  998. if (C_CRTSCTS(tty)) {
  999. mos7720_port->shadowMCR |= UART_MCR_RTS;
  1000. write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
  1001. mos7720_port->shadowMCR);
  1002. }
  1003. }
  1004. /* FIXME: this function does not work */
  1005. static int set_higher_rates(struct moschip_port *mos7720_port,
  1006. unsigned int baud)
  1007. {
  1008. struct usb_serial_port *port;
  1009. struct usb_serial *serial;
  1010. int port_number;
  1011. enum mos_regs sp_reg;
  1012. if (mos7720_port == NULL)
  1013. return -EINVAL;
  1014. port = mos7720_port->port;
  1015. serial = port->serial;
  1016. /***********************************************
  1017. * Init Sequence for higher rates
  1018. ***********************************************/
  1019. dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
  1020. port_number = port->port_number;
  1021. write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
  1022. write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
  1023. write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
  1024. mos7720_port->shadowMCR = 0x0b;
  1025. write_mos_reg(serial, port_number, MOS7720_MCR,
  1026. mos7720_port->shadowMCR);
  1027. write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x00);
  1028. /***********************************************
  1029. * Set for higher rates *
  1030. ***********************************************/
  1031. /* writing baud rate verbatum into uart clock field clearly not right */
  1032. if (port_number == 0)
  1033. sp_reg = MOS7720_SP1_REG;
  1034. else
  1035. sp_reg = MOS7720_SP2_REG;
  1036. write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
  1037. write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x03);
  1038. mos7720_port->shadowMCR = 0x2b;
  1039. write_mos_reg(serial, port_number, MOS7720_MCR,
  1040. mos7720_port->shadowMCR);
  1041. /***********************************************
  1042. * Set DLL/DLM
  1043. ***********************************************/
  1044. mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
  1045. write_mos_reg(serial, port_number, MOS7720_LCR,
  1046. mos7720_port->shadowLCR);
  1047. write_mos_reg(serial, port_number, MOS7720_DLL, 0x01);
  1048. write_mos_reg(serial, port_number, MOS7720_DLM, 0x00);
  1049. mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  1050. write_mos_reg(serial, port_number, MOS7720_LCR,
  1051. mos7720_port->shadowLCR);
  1052. return 0;
  1053. }
  1054. /* baud rate information */
  1055. struct divisor_table_entry {
  1056. __u32 baudrate;
  1057. __u16 divisor;
  1058. };
  1059. /* Define table of divisors for moschip 7720 hardware *
  1060. * These assume a 3.6864MHz crystal, the standard /16, and *
  1061. * MCR.7 = 0. */
  1062. static const struct divisor_table_entry divisor_table[] = {
  1063. { 50, 2304},
  1064. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  1065. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  1066. { 150, 768},
  1067. { 300, 384},
  1068. { 600, 192},
  1069. { 1200, 96},
  1070. { 1800, 64},
  1071. { 2400, 48},
  1072. { 4800, 24},
  1073. { 7200, 16},
  1074. { 9600, 12},
  1075. { 19200, 6},
  1076. { 38400, 3},
  1077. { 57600, 2},
  1078. { 115200, 1},
  1079. };
  1080. /*****************************************************************************
  1081. * calc_baud_rate_divisor
  1082. * this function calculates the proper baud rate divisor for the specified
  1083. * baud rate.
  1084. *****************************************************************************/
  1085. static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
  1086. {
  1087. int i;
  1088. __u16 custom;
  1089. __u16 round1;
  1090. __u16 round;
  1091. dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
  1092. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  1093. if (divisor_table[i].baudrate == baudrate) {
  1094. *divisor = divisor_table[i].divisor;
  1095. return 0;
  1096. }
  1097. }
  1098. /* After trying for all the standard baud rates *
  1099. * Try calculating the divisor for this baud rate */
  1100. if (baudrate > 75 && baudrate < 230400) {
  1101. /* get the divisor */
  1102. custom = (__u16)(230400L / baudrate);
  1103. /* Check for round off */
  1104. round1 = (__u16)(2304000L / baudrate);
  1105. round = (__u16)(round1 - (custom * 10));
  1106. if (round > 4)
  1107. custom++;
  1108. *divisor = custom;
  1109. dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
  1110. return 0;
  1111. }
  1112. dev_dbg(&port->dev, "Baud calculation Failed...\n");
  1113. return -EINVAL;
  1114. }
  1115. /*
  1116. * send_cmd_write_baud_rate
  1117. * this function sends the proper command to change the baud rate of the
  1118. * specified port.
  1119. */
  1120. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  1121. int baudrate)
  1122. {
  1123. struct usb_serial_port *port;
  1124. struct usb_serial *serial;
  1125. int divisor;
  1126. int status;
  1127. unsigned char number;
  1128. if (mos7720_port == NULL)
  1129. return -1;
  1130. port = mos7720_port->port;
  1131. serial = port->serial;
  1132. number = port->port_number;
  1133. dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
  1134. /* Calculate the Divisor */
  1135. status = calc_baud_rate_divisor(port, baudrate, &divisor);
  1136. if (status) {
  1137. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  1138. return status;
  1139. }
  1140. /* Enable access to divisor latch */
  1141. mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
  1142. write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
  1143. /* Write the divisor */
  1144. write_mos_reg(serial, number, MOS7720_DLL, (__u8)(divisor & 0xff));
  1145. write_mos_reg(serial, number, MOS7720_DLM,
  1146. (__u8)((divisor & 0xff00) >> 8));
  1147. /* Disable access to divisor latch */
  1148. mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  1149. write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
  1150. return status;
  1151. }
  1152. /*
  1153. * change_port_settings
  1154. * This routine is called to set the UART on the device to match
  1155. * the specified new settings.
  1156. */
  1157. static void change_port_settings(struct tty_struct *tty,
  1158. struct moschip_port *mos7720_port,
  1159. const struct ktermios *old_termios)
  1160. {
  1161. struct usb_serial_port *port;
  1162. struct usb_serial *serial;
  1163. int baud;
  1164. unsigned cflag;
  1165. __u8 lData;
  1166. __u8 lParity;
  1167. __u8 lStop;
  1168. int status;
  1169. int port_number;
  1170. if (mos7720_port == NULL)
  1171. return ;
  1172. port = mos7720_port->port;
  1173. serial = port->serial;
  1174. port_number = port->port_number;
  1175. if (!mos7720_port->open) {
  1176. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1177. return;
  1178. }
  1179. lStop = 0x00; /* 1 stop bit */
  1180. lParity = 0x00; /* No parity */
  1181. cflag = tty->termios.c_cflag;
  1182. lData = UART_LCR_WLEN(tty_get_char_size(cflag));
  1183. /* Change the Parity bit */
  1184. if (cflag & PARENB) {
  1185. if (cflag & PARODD) {
  1186. lParity = UART_LCR_PARITY;
  1187. dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
  1188. } else {
  1189. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  1190. dev_dbg(&port->dev, "%s - parity = even\n", __func__);
  1191. }
  1192. } else {
  1193. dev_dbg(&port->dev, "%s - parity = none\n", __func__);
  1194. }
  1195. if (cflag & CMSPAR)
  1196. lParity = lParity | 0x20;
  1197. /* Change the Stop bit */
  1198. if (cflag & CSTOPB) {
  1199. lStop = UART_LCR_STOP;
  1200. dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
  1201. } else {
  1202. lStop = 0x00;
  1203. dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
  1204. }
  1205. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  1206. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  1207. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  1208. /* Update the LCR with the correct value */
  1209. mos7720_port->shadowLCR &=
  1210. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1211. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  1212. /* Disable Interrupts */
  1213. write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
  1214. write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
  1215. write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
  1216. /* Send the updated LCR value to the mos7720 */
  1217. write_mos_reg(serial, port_number, MOS7720_LCR,
  1218. mos7720_port->shadowLCR);
  1219. mos7720_port->shadowMCR = 0x0b;
  1220. write_mos_reg(serial, port_number, MOS7720_MCR,
  1221. mos7720_port->shadowMCR);
  1222. /* set up the MCR register and send it to the mos7720 */
  1223. mos7720_port->shadowMCR = UART_MCR_OUT2;
  1224. if (cflag & CBAUD)
  1225. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  1226. if (cflag & CRTSCTS) {
  1227. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  1228. /* To set hardware flow control to the specified *
  1229. * serial port, in SP1/2_CONTROL_REG */
  1230. if (port_number)
  1231. write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
  1232. 0x01);
  1233. else
  1234. write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
  1235. 0x02);
  1236. } else
  1237. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  1238. write_mos_reg(serial, port_number, MOS7720_MCR,
  1239. mos7720_port->shadowMCR);
  1240. /* Determine divisor based on baud rate */
  1241. baud = tty_get_baud_rate(tty);
  1242. if (!baud) {
  1243. /* pick a default, any default... */
  1244. dev_dbg(&port->dev, "Picked default baud...\n");
  1245. baud = 9600;
  1246. }
  1247. if (baud >= 230400) {
  1248. set_higher_rates(mos7720_port, baud);
  1249. /* Enable Interrupts */
  1250. write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
  1251. return;
  1252. }
  1253. dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
  1254. status = send_cmd_write_baud_rate(mos7720_port, baud);
  1255. /* FIXME: needs to write actual resulting baud back not just
  1256. blindly do so */
  1257. if (cflag & CBAUD)
  1258. tty_encode_baud_rate(tty, baud, baud);
  1259. /* Enable Interrupts */
  1260. write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
  1261. if (port->read_urb->status != -EINPROGRESS) {
  1262. status = usb_submit_urb(port->read_urb, GFP_KERNEL);
  1263. if (status)
  1264. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
  1265. }
  1266. }
  1267. /*
  1268. * mos7720_set_termios
  1269. * this function is called by the tty driver when it wants to change the
  1270. * termios structure.
  1271. */
  1272. static void mos7720_set_termios(struct tty_struct *tty,
  1273. struct usb_serial_port *port,
  1274. const struct ktermios *old_termios)
  1275. {
  1276. int status;
  1277. struct moschip_port *mos7720_port;
  1278. mos7720_port = usb_get_serial_port_data(port);
  1279. if (mos7720_port == NULL)
  1280. return;
  1281. if (!mos7720_port->open) {
  1282. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1283. return;
  1284. }
  1285. /* change the port settings to the new ones specified */
  1286. change_port_settings(tty, mos7720_port, old_termios);
  1287. if (port->read_urb->status != -EINPROGRESS) {
  1288. status = usb_submit_urb(port->read_urb, GFP_KERNEL);
  1289. if (status)
  1290. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
  1291. }
  1292. }
  1293. /*
  1294. * get_lsr_info - get line status register info
  1295. *
  1296. * Purpose: Let user call ioctl() to get info when the UART physically
  1297. * is emptied. On bus types like RS485, the transmitter must
  1298. * release the bus after transmitting. This must be done when
  1299. * the transmit shift register is empty, not be done when the
  1300. * transmit holding register is empty. This functionality
  1301. * allows an RS485 driver to be written in user space.
  1302. */
  1303. static int get_lsr_info(struct tty_struct *tty,
  1304. struct moschip_port *mos7720_port, unsigned int __user *value)
  1305. {
  1306. struct usb_serial_port *port = tty->driver_data;
  1307. unsigned int result = 0;
  1308. unsigned char data = 0;
  1309. int port_number = port->port_number;
  1310. int count;
  1311. count = mos7720_chars_in_buffer(tty);
  1312. if (count == 0) {
  1313. read_mos_reg(port->serial, port_number, MOS7720_LSR, &data);
  1314. if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
  1315. == (UART_LSR_TEMT | UART_LSR_THRE)) {
  1316. dev_dbg(&port->dev, "%s -- Empty\n", __func__);
  1317. result = TIOCSER_TEMT;
  1318. }
  1319. }
  1320. if (copy_to_user(value, &result, sizeof(int)))
  1321. return -EFAULT;
  1322. return 0;
  1323. }
  1324. static int mos7720_tiocmget(struct tty_struct *tty)
  1325. {
  1326. struct usb_serial_port *port = tty->driver_data;
  1327. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1328. unsigned int result = 0;
  1329. unsigned int mcr ;
  1330. unsigned int msr ;
  1331. mcr = mos7720_port->shadowMCR;
  1332. msr = mos7720_port->shadowMSR;
  1333. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1334. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1335. | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1336. | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
  1337. | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1338. | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1339. return result;
  1340. }
  1341. static int mos7720_tiocmset(struct tty_struct *tty,
  1342. unsigned int set, unsigned int clear)
  1343. {
  1344. struct usb_serial_port *port = tty->driver_data;
  1345. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1346. unsigned int mcr ;
  1347. mcr = mos7720_port->shadowMCR;
  1348. if (set & TIOCM_RTS)
  1349. mcr |= UART_MCR_RTS;
  1350. if (set & TIOCM_DTR)
  1351. mcr |= UART_MCR_DTR;
  1352. if (set & TIOCM_LOOP)
  1353. mcr |= UART_MCR_LOOP;
  1354. if (clear & TIOCM_RTS)
  1355. mcr &= ~UART_MCR_RTS;
  1356. if (clear & TIOCM_DTR)
  1357. mcr &= ~UART_MCR_DTR;
  1358. if (clear & TIOCM_LOOP)
  1359. mcr &= ~UART_MCR_LOOP;
  1360. mos7720_port->shadowMCR = mcr;
  1361. write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
  1362. mos7720_port->shadowMCR);
  1363. return 0;
  1364. }
  1365. static int mos7720_ioctl(struct tty_struct *tty,
  1366. unsigned int cmd, unsigned long arg)
  1367. {
  1368. struct usb_serial_port *port = tty->driver_data;
  1369. struct moschip_port *mos7720_port;
  1370. mos7720_port = usb_get_serial_port_data(port);
  1371. if (mos7720_port == NULL)
  1372. return -ENODEV;
  1373. switch (cmd) {
  1374. case TIOCSERGETLSR:
  1375. dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
  1376. return get_lsr_info(tty, mos7720_port,
  1377. (unsigned int __user *)arg);
  1378. }
  1379. return -ENOIOCTLCMD;
  1380. }
  1381. static int mos7720_startup(struct usb_serial *serial)
  1382. {
  1383. struct usb_device *dev;
  1384. char data;
  1385. u16 product;
  1386. int ret_val;
  1387. product = le16_to_cpu(serial->dev->descriptor.idProduct);
  1388. dev = serial->dev;
  1389. if (product == MOSCHIP_DEVICE_ID_7715) {
  1390. struct urb *urb = serial->port[0]->interrupt_in_urb;
  1391. urb->complete = mos7715_interrupt_callback;
  1392. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  1393. ret_val = mos7715_parport_init(serial);
  1394. if (ret_val < 0)
  1395. return ret_val;
  1396. #endif
  1397. }
  1398. /* start the interrupt urb */
  1399. ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
  1400. if (ret_val) {
  1401. dev_err(&dev->dev, "failed to submit interrupt urb: %d\n",
  1402. ret_val);
  1403. }
  1404. /* LSR For Port 1 */
  1405. read_mos_reg(serial, 0, MOS7720_LSR, &data);
  1406. dev_dbg(&dev->dev, "LSR:%x\n", data);
  1407. return 0;
  1408. }
  1409. static void mos7720_release(struct usb_serial *serial)
  1410. {
  1411. usb_kill_urb(serial->port[0]->interrupt_in_urb);
  1412. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  1413. /* close the parallel port */
  1414. if (le16_to_cpu(serial->dev->descriptor.idProduct)
  1415. == MOSCHIP_DEVICE_ID_7715) {
  1416. struct mos7715_parport *mos_parport =
  1417. usb_get_serial_data(serial);
  1418. /* prevent NULL ptr dereference in port callbacks */
  1419. spin_lock(&release_lock);
  1420. mos_parport->pp->private_data = NULL;
  1421. spin_unlock(&release_lock);
  1422. /* wait for synchronous usb calls to return */
  1423. if (mos_parport->msg_pending)
  1424. wait_for_completion_timeout(&mos_parport->syncmsg_compl,
  1425. msecs_to_jiffies(MOS_WDR_TIMEOUT));
  1426. /*
  1427. * If delayed work is currently scheduled, wait for it to
  1428. * complete. This also implies barriers that ensure the
  1429. * below serial clearing is not hoisted above the ->work.
  1430. */
  1431. cancel_work_sync(&mos_parport->work);
  1432. parport_remove_port(mos_parport->pp);
  1433. usb_set_serial_data(serial, NULL);
  1434. mos_parport->serial = NULL;
  1435. parport_del_port(mos_parport->pp);
  1436. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  1437. }
  1438. #endif
  1439. }
  1440. static int mos7720_port_probe(struct usb_serial_port *port)
  1441. {
  1442. struct moschip_port *mos7720_port;
  1443. mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
  1444. if (!mos7720_port)
  1445. return -ENOMEM;
  1446. mos7720_port->port = port;
  1447. usb_set_serial_port_data(port, mos7720_port);
  1448. return 0;
  1449. }
  1450. static void mos7720_port_remove(struct usb_serial_port *port)
  1451. {
  1452. struct moschip_port *mos7720_port;
  1453. mos7720_port = usb_get_serial_port_data(port);
  1454. kfree(mos7720_port);
  1455. }
  1456. static struct usb_serial_driver moschip7720_2port_driver = {
  1457. .driver = {
  1458. .owner = THIS_MODULE,
  1459. .name = "moschip7720",
  1460. },
  1461. .description = "Moschip 2 port adapter",
  1462. .id_table = id_table,
  1463. .num_bulk_in = 2,
  1464. .num_bulk_out = 2,
  1465. .num_interrupt_in = 1,
  1466. .calc_num_ports = mos77xx_calc_num_ports,
  1467. .open = mos7720_open,
  1468. .close = mos7720_close,
  1469. .throttle = mos7720_throttle,
  1470. .unthrottle = mos7720_unthrottle,
  1471. .attach = mos7720_startup,
  1472. .release = mos7720_release,
  1473. .port_probe = mos7720_port_probe,
  1474. .port_remove = mos7720_port_remove,
  1475. .ioctl = mos7720_ioctl,
  1476. .tiocmget = mos7720_tiocmget,
  1477. .tiocmset = mos7720_tiocmset,
  1478. .set_termios = mos7720_set_termios,
  1479. .write = mos7720_write,
  1480. .write_room = mos7720_write_room,
  1481. .chars_in_buffer = mos7720_chars_in_buffer,
  1482. .break_ctl = mos7720_break,
  1483. .read_bulk_callback = mos7720_bulk_in_callback,
  1484. .read_int_callback = mos7720_interrupt_callback,
  1485. };
  1486. static struct usb_serial_driver * const serial_drivers[] = {
  1487. &moschip7720_2port_driver, NULL
  1488. };
  1489. module_usb_serial_driver(serial_drivers, id_table);
  1490. MODULE_AUTHOR(DRIVER_AUTHOR);
  1491. MODULE_DESCRIPTION(DRIVER_DESC);
  1492. MODULE_LICENSE("GPL v2");