mos7720.c 57 KB

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