keyspan.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. /*
  2. Keyspan USB to Serial Converter driver
  3. (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org>
  4. (C) Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. See http://misc.nu/hugh/keyspan.html for more information.
  10. Code in this driver inspired by and in a number of places taken
  11. from Brian Warner's original Keyspan-PDA driver.
  12. This driver has been put together with the support of Innosys, Inc.
  13. and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
  14. Thanks Guys :)
  15. Thanks to Paulus for miscellaneous tidy ups, some largish chunks
  16. of much nicer and/or completely new code and (perhaps most uniquely)
  17. having the patience to sit down and explain why and where he'd changed
  18. stuff.
  19. Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
  20. staff in their work on open source projects.
  21. Change History
  22. 2003sep04 LPM (Keyspan) add support for new single port product USA19HS.
  23. Improve setup message handling for all devices.
  24. Wed Feb 19 22:00:00 PST 2003 (Jeffrey S. Laing <keyspan@jsl.com>)
  25. Merged the current (1/31/03) Keyspan code with the current (2.4.21-pre4)
  26. Linux source tree. The Linux tree lacked support for the 49WLC and
  27. others. The Keyspan patches didn't work with the current kernel.
  28. 2003jan30 LPM add support for the 49WLC and MPR
  29. Wed Apr 25 12:00:00 PST 2002 (Keyspan)
  30. Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
  31. now supported (including QI and QW). Modified port open, port
  32. close, and send setup() logic to fix various data and endpoint
  33. synchronization bugs and device LED status bugs. Changed keyspan_
  34. write_room() to accurately return transmit buffer availability.
  35. Changed forwardingLength from 1 to 16 for all adapters.
  36. Fri Oct 12 16:45:00 EST 2001
  37. Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
  38. Wed Apr 25 12:00:00 PST 2002 (Keyspan)
  39. Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
  40. now supported (including QI and QW). Modified port open, port
  41. close, and send setup() logic to fix various data and endpoint
  42. synchronization bugs and device LED status bugs. Changed keyspan_
  43. write_room() to accurately return transmit buffer availability.
  44. Changed forwardingLength from 1 to 16 for all adapters.
  45. Fri Oct 12 16:45:00 EST 2001
  46. Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
  47. Mon Oct 8 14:29:00 EST 2001 hugh
  48. Fixed bug that prevented mulitport devices operating correctly
  49. if they weren't the first unit attached.
  50. Sat Oct 6 12:31:21 EST 2001 hugh
  51. Added support for USA-28XA and -28XB, misc cleanups, break support
  52. for usa26 based models thanks to David Gibson.
  53. Thu May 31 11:56:42 PDT 2001 gkh
  54. switched from using spinlock to a semaphore
  55. (04/08/2001) gb
  56. Identify version on module load.
  57. (11/01/2000) Adam J. Richter
  58. usb_device_id table support.
  59. Tue Oct 10 23:15:33 EST 2000 Hugh
  60. Merged Paul's changes with my USA-49W mods. Work in progress
  61. still...
  62. Wed Jul 19 14:00:42 EST 2000 gkh
  63. Added module_init and module_exit functions to handle the fact that
  64. this driver is a loadable module now.
  65. Tue Jul 18 16:14:52 EST 2000 Hugh
  66. Basic character input/output for USA-19 now mostly works,
  67. fixed at 9600 baud for the moment.
  68. Sat Jul 8 11:11:48 EST 2000 Hugh
  69. First public release - nothing works except the firmware upload.
  70. Tested on PPC and x86 architectures, seems to behave...
  71. */
  72. #include <linux/config.h>
  73. #include <linux/kernel.h>
  74. #include <linux/jiffies.h>
  75. #include <linux/errno.h>
  76. #include <linux/init.h>
  77. #include <linux/slab.h>
  78. #include <linux/tty.h>
  79. #include <linux/tty_driver.h>
  80. #include <linux/tty_flip.h>
  81. #include <linux/module.h>
  82. #include <linux/spinlock.h>
  83. #include <asm/uaccess.h>
  84. #include <linux/usb.h>
  85. #include "usb-serial.h"
  86. #include "keyspan.h"
  87. static int debug;
  88. /*
  89. * Version Information
  90. */
  91. #define DRIVER_VERSION "v1.1.4"
  92. #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
  93. #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
  94. #define INSTAT_BUFLEN 32
  95. #define GLOCONT_BUFLEN 64
  96. /* Per device and per port private data */
  97. struct keyspan_serial_private {
  98. const struct keyspan_device_details *device_details;
  99. struct urb *instat_urb;
  100. char instat_buf[INSTAT_BUFLEN];
  101. /* XXX this one probably will need a lock */
  102. struct urb *glocont_urb;
  103. char glocont_buf[GLOCONT_BUFLEN];
  104. };
  105. struct keyspan_port_private {
  106. /* Keep track of which input & output endpoints to use */
  107. int in_flip;
  108. int out_flip;
  109. /* Keep duplicate of device details in each port
  110. structure as well - simplifies some of the
  111. callback functions etc. */
  112. const struct keyspan_device_details *device_details;
  113. /* Input endpoints and buffer for this port */
  114. struct urb *in_urbs[2];
  115. char in_buffer[2][64];
  116. /* Output endpoints and buffer for this port */
  117. struct urb *out_urbs[2];
  118. char out_buffer[2][64];
  119. /* Input ack endpoint */
  120. struct urb *inack_urb;
  121. char inack_buffer[1];
  122. /* Output control endpoint */
  123. struct urb *outcont_urb;
  124. char outcont_buffer[64];
  125. /* Settings for the port */
  126. int baud;
  127. int old_baud;
  128. unsigned int cflag;
  129. unsigned int old_cflag;
  130. enum {flow_none, flow_cts, flow_xon} flow_control;
  131. int rts_state; /* Handshaking pins (outputs) */
  132. int dtr_state;
  133. int cts_state; /* Handshaking pins (inputs) */
  134. int dsr_state;
  135. int dcd_state;
  136. int ri_state;
  137. int break_on;
  138. unsigned long tx_start_time[2];
  139. int resend_cont; /* need to resend control packet */
  140. };
  141. /* Include Keyspan message headers. All current Keyspan Adapters
  142. make use of one of four message formats which are referred
  143. to as USA-26, USA-28 and USA-49, USA-90 by Keyspan and within this driver. */
  144. #include "keyspan_usa26msg.h"
  145. #include "keyspan_usa28msg.h"
  146. #include "keyspan_usa49msg.h"
  147. #include "keyspan_usa90msg.h"
  148. /* Functions used by new usb-serial code. */
  149. static int __init keyspan_init (void)
  150. {
  151. int retval;
  152. retval = usb_serial_register(&keyspan_pre_device);
  153. if (retval)
  154. goto failed_pre_device_register;
  155. retval = usb_serial_register(&keyspan_1port_device);
  156. if (retval)
  157. goto failed_1port_device_register;
  158. retval = usb_serial_register(&keyspan_2port_device);
  159. if (retval)
  160. goto failed_2port_device_register;
  161. retval = usb_serial_register(&keyspan_4port_device);
  162. if (retval)
  163. goto failed_4port_device_register;
  164. retval = usb_register(&keyspan_driver);
  165. if (retval)
  166. goto failed_usb_register;
  167. info(DRIVER_VERSION ":" DRIVER_DESC);
  168. return 0;
  169. failed_usb_register:
  170. usb_serial_deregister(&keyspan_4port_device);
  171. failed_4port_device_register:
  172. usb_serial_deregister(&keyspan_2port_device);
  173. failed_2port_device_register:
  174. usb_serial_deregister(&keyspan_1port_device);
  175. failed_1port_device_register:
  176. usb_serial_deregister(&keyspan_pre_device);
  177. failed_pre_device_register:
  178. return retval;
  179. }
  180. static void __exit keyspan_exit (void)
  181. {
  182. usb_deregister (&keyspan_driver);
  183. usb_serial_deregister (&keyspan_pre_device);
  184. usb_serial_deregister (&keyspan_1port_device);
  185. usb_serial_deregister (&keyspan_2port_device);
  186. usb_serial_deregister (&keyspan_4port_device);
  187. }
  188. module_init(keyspan_init);
  189. module_exit(keyspan_exit);
  190. static void keyspan_rx_throttle (struct usb_serial_port *port)
  191. {
  192. dbg("%s - port %d", __FUNCTION__, port->number);
  193. }
  194. static void keyspan_rx_unthrottle (struct usb_serial_port *port)
  195. {
  196. dbg("%s - port %d", __FUNCTION__, port->number);
  197. }
  198. static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
  199. {
  200. struct keyspan_port_private *p_priv;
  201. dbg("%s", __FUNCTION__);
  202. p_priv = usb_get_serial_port_data(port);
  203. if (break_state == -1)
  204. p_priv->break_on = 1;
  205. else
  206. p_priv->break_on = 0;
  207. keyspan_send_setup(port, 0);
  208. }
  209. static void keyspan_set_termios (struct usb_serial_port *port,
  210. struct termios *old_termios)
  211. {
  212. int baud_rate, device_port;
  213. struct keyspan_port_private *p_priv;
  214. const struct keyspan_device_details *d_details;
  215. unsigned int cflag;
  216. dbg("%s", __FUNCTION__);
  217. p_priv = usb_get_serial_port_data(port);
  218. d_details = p_priv->device_details;
  219. cflag = port->tty->termios->c_cflag;
  220. device_port = port->number - port->serial->minor;
  221. /* Baud rate calculation takes baud rate as an integer
  222. so other rates can be generated if desired. */
  223. baud_rate = tty_get_baud_rate(port->tty);
  224. /* If no match or invalid, don't change */
  225. if (baud_rate >= 0
  226. && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
  227. NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
  228. /* FIXME - more to do here to ensure rate changes cleanly */
  229. p_priv->baud = baud_rate;
  230. }
  231. /* set CTS/RTS handshake etc. */
  232. p_priv->cflag = cflag;
  233. p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
  234. keyspan_send_setup(port, 0);
  235. }
  236. static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
  237. {
  238. unsigned int value;
  239. struct keyspan_port_private *p_priv;
  240. p_priv = usb_get_serial_port_data(port);
  241. value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
  242. ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
  243. ((p_priv->cts_state) ? TIOCM_CTS : 0) |
  244. ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
  245. ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
  246. ((p_priv->ri_state) ? TIOCM_RNG : 0);
  247. return value;
  248. }
  249. static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
  250. unsigned int set, unsigned int clear)
  251. {
  252. struct keyspan_port_private *p_priv;
  253. p_priv = usb_get_serial_port_data(port);
  254. if (set & TIOCM_RTS)
  255. p_priv->rts_state = 1;
  256. if (set & TIOCM_DTR)
  257. p_priv->dtr_state = 1;
  258. if (clear & TIOCM_RTS)
  259. p_priv->rts_state = 0;
  260. if (clear & TIOCM_DTR)
  261. p_priv->dtr_state = 0;
  262. keyspan_send_setup(port, 0);
  263. return 0;
  264. }
  265. static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
  266. unsigned int cmd, unsigned long arg)
  267. {
  268. return -ENOIOCTLCMD;
  269. }
  270. /* Write function is similar for the four protocols used
  271. with only a minor change for usa90 (usa19hs) required */
  272. static int keyspan_write(struct usb_serial_port *port,
  273. const unsigned char *buf, int count)
  274. {
  275. struct keyspan_port_private *p_priv;
  276. const struct keyspan_device_details *d_details;
  277. int flip;
  278. int left, todo;
  279. struct urb *this_urb;
  280. int err, maxDataLen, dataOffset;
  281. p_priv = usb_get_serial_port_data(port);
  282. d_details = p_priv->device_details;
  283. if (d_details->msg_format == msg_usa90) {
  284. maxDataLen = 64;
  285. dataOffset = 0;
  286. } else {
  287. maxDataLen = 63;
  288. dataOffset = 1;
  289. }
  290. dbg("%s - for port %d (%d chars), flip=%d",
  291. __FUNCTION__, port->number, count, p_priv->out_flip);
  292. for (left = count; left > 0; left -= todo) {
  293. todo = left;
  294. if (todo > maxDataLen)
  295. todo = maxDataLen;
  296. flip = p_priv->out_flip;
  297. /* Check we have a valid urb/endpoint before we use it... */
  298. if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
  299. /* no bulk out, so return 0 bytes written */
  300. dbg("%s - no output urb :(", __FUNCTION__);
  301. return count;
  302. }
  303. dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
  304. if (this_urb->status == -EINPROGRESS) {
  305. if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
  306. break;
  307. usb_unlink_urb(this_urb);
  308. break;
  309. }
  310. /* First byte in buffer is "last flag" (except for usa19hx) - unused so
  311. for now so set to zero */
  312. ((char *)this_urb->transfer_buffer)[0] = 0;
  313. memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
  314. buf += todo;
  315. /* send the data out the bulk port */
  316. this_urb->transfer_buffer_length = todo + dataOffset;
  317. this_urb->dev = port->serial->dev;
  318. if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
  319. dbg("usb_submit_urb(write bulk) failed (%d)", err);
  320. }
  321. p_priv->tx_start_time[flip] = jiffies;
  322. /* Flip for next time if usa26 or usa28 interface
  323. (not used on usa49) */
  324. p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
  325. }
  326. return count - left;
  327. }
  328. static void usa26_indat_callback(struct urb *urb, struct pt_regs *regs)
  329. {
  330. int i, err;
  331. int endpoint;
  332. struct usb_serial_port *port;
  333. struct tty_struct *tty;
  334. unsigned char *data = urb->transfer_buffer;
  335. dbg ("%s", __FUNCTION__);
  336. endpoint = usb_pipeendpoint(urb->pipe);
  337. if (urb->status) {
  338. dbg("%s - nonzero status: %x on endpoint %d.",
  339. __FUNCTION__, urb->status, endpoint);
  340. return;
  341. }
  342. port = (struct usb_serial_port *) urb->context;
  343. tty = port->tty;
  344. if (urb->actual_length) {
  345. /* 0x80 bit is error flag */
  346. if ((data[0] & 0x80) == 0) {
  347. /* no errors on individual bytes, only possible overrun err*/
  348. if (data[0] & RXERROR_OVERRUN)
  349. err = TTY_OVERRUN;
  350. else err = 0;
  351. for (i = 1; i < urb->actual_length ; ++i) {
  352. tty_insert_flip_char(tty, data[i], err);
  353. }
  354. } else {
  355. /* some bytes had errors, every byte has status */
  356. dbg("%s - RX error!!!!", __FUNCTION__);
  357. for (i = 0; i + 1 < urb->actual_length; i += 2) {
  358. int stat = data[i], flag = 0;
  359. if (stat & RXERROR_OVERRUN)
  360. flag |= TTY_OVERRUN;
  361. if (stat & RXERROR_FRAMING)
  362. flag |= TTY_FRAME;
  363. if (stat & RXERROR_PARITY)
  364. flag |= TTY_PARITY;
  365. /* XXX should handle break (0x10) */
  366. tty_insert_flip_char(tty, data[i+1], flag);
  367. }
  368. }
  369. tty_flip_buffer_push(tty);
  370. }
  371. /* Resubmit urb so we continue receiving */
  372. urb->dev = port->serial->dev;
  373. if (port->open_count)
  374. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
  375. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  376. }
  377. return;
  378. }
  379. /* Outdat handling is common for all devices */
  380. static void usa2x_outdat_callback(struct urb *urb, struct pt_regs *regs)
  381. {
  382. struct usb_serial_port *port;
  383. struct keyspan_port_private *p_priv;
  384. port = (struct usb_serial_port *) urb->context;
  385. p_priv = usb_get_serial_port_data(port);
  386. dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
  387. if (port->open_count)
  388. schedule_work(&port->work);
  389. }
  390. static void usa26_inack_callback(struct urb *urb, struct pt_regs *regs)
  391. {
  392. dbg ("%s", __FUNCTION__);
  393. }
  394. static void usa26_outcont_callback(struct urb *urb, struct pt_regs *regs)
  395. {
  396. struct usb_serial_port *port;
  397. struct keyspan_port_private *p_priv;
  398. port = (struct usb_serial_port *) urb->context;
  399. p_priv = usb_get_serial_port_data(port);
  400. if (p_priv->resend_cont) {
  401. dbg ("%s - sending setup", __FUNCTION__);
  402. keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
  403. }
  404. }
  405. static void usa26_instat_callback(struct urb *urb, struct pt_regs *regs)
  406. {
  407. unsigned char *data = urb->transfer_buffer;
  408. struct keyspan_usa26_portStatusMessage *msg;
  409. struct usb_serial *serial;
  410. struct usb_serial_port *port;
  411. struct keyspan_port_private *p_priv;
  412. int old_dcd_state, err;
  413. serial = (struct usb_serial *) urb->context;
  414. if (urb->status) {
  415. dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
  416. return;
  417. }
  418. if (urb->actual_length != 9) {
  419. dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
  420. goto exit;
  421. }
  422. msg = (struct keyspan_usa26_portStatusMessage *)data;
  423. #if 0
  424. dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
  425. __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
  426. msg->_txXoff, msg->rxEnabled, msg->controlResponse);
  427. #endif
  428. /* Now do something useful with the data */
  429. /* Check port number from message and retrieve private data */
  430. if (msg->port >= serial->num_ports) {
  431. dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
  432. goto exit;
  433. }
  434. port = serial->port[msg->port];
  435. p_priv = usb_get_serial_port_data(port);
  436. /* Update handshaking pin state information */
  437. old_dcd_state = p_priv->dcd_state;
  438. p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
  439. p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
  440. p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
  441. p_priv->ri_state = ((msg->ri) ? 1 : 0);
  442. if (port->tty && !C_CLOCAL(port->tty)
  443. && old_dcd_state != p_priv->dcd_state) {
  444. if (old_dcd_state)
  445. tty_hangup(port->tty);
  446. /* else */
  447. /* wake_up_interruptible(&p_priv->open_wait); */
  448. }
  449. /* Resubmit urb so we continue receiving */
  450. urb->dev = serial->dev;
  451. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
  452. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  453. }
  454. exit: ;
  455. }
  456. static void usa26_glocont_callback(struct urb *urb, struct pt_regs *regs)
  457. {
  458. dbg ("%s", __FUNCTION__);
  459. }
  460. static void usa28_indat_callback(struct urb *urb, struct pt_regs *regs)
  461. {
  462. int i, err;
  463. struct usb_serial_port *port;
  464. struct tty_struct *tty;
  465. unsigned char *data;
  466. struct keyspan_port_private *p_priv;
  467. dbg ("%s", __FUNCTION__);
  468. port = (struct usb_serial_port *) urb->context;
  469. p_priv = usb_get_serial_port_data(port);
  470. data = urb->transfer_buffer;
  471. if (urb != p_priv->in_urbs[p_priv->in_flip])
  472. return;
  473. do {
  474. if (urb->status) {
  475. dbg("%s - nonzero status: %x on endpoint %d.",
  476. __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
  477. return;
  478. }
  479. port = (struct usb_serial_port *) urb->context;
  480. p_priv = usb_get_serial_port_data(port);
  481. data = urb->transfer_buffer;
  482. tty = port->tty;
  483. if (urb->actual_length) {
  484. for (i = 0; i < urb->actual_length ; ++i) {
  485. tty_insert_flip_char(tty, data[i], 0);
  486. }
  487. tty_flip_buffer_push(tty);
  488. }
  489. /* Resubmit urb so we continue receiving */
  490. urb->dev = port->serial->dev;
  491. if (port->open_count)
  492. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
  493. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  494. }
  495. p_priv->in_flip ^= 1;
  496. urb = p_priv->in_urbs[p_priv->in_flip];
  497. } while (urb->status != -EINPROGRESS);
  498. }
  499. static void usa28_inack_callback(struct urb *urb, struct pt_regs *regs)
  500. {
  501. dbg ("%s", __FUNCTION__);
  502. }
  503. static void usa28_outcont_callback(struct urb *urb, struct pt_regs *regs)
  504. {
  505. struct usb_serial_port *port;
  506. struct keyspan_port_private *p_priv;
  507. port = (struct usb_serial_port *) urb->context;
  508. p_priv = usb_get_serial_port_data(port);
  509. if (p_priv->resend_cont) {
  510. dbg ("%s - sending setup", __FUNCTION__);
  511. keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
  512. }
  513. }
  514. static void usa28_instat_callback(struct urb *urb, struct pt_regs *regs)
  515. {
  516. int err;
  517. unsigned char *data = urb->transfer_buffer;
  518. struct keyspan_usa28_portStatusMessage *msg;
  519. struct usb_serial *serial;
  520. struct usb_serial_port *port;
  521. struct keyspan_port_private *p_priv;
  522. int old_dcd_state;
  523. serial = (struct usb_serial *) urb->context;
  524. if (urb->status) {
  525. dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
  526. return;
  527. }
  528. if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
  529. dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
  530. goto exit;
  531. }
  532. /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
  533. data[0], data[1], data[2], data[3], data[4], data[5],
  534. data[6], data[7], data[8], data[9], data[10], data[11]);*/
  535. /* Now do something useful with the data */
  536. msg = (struct keyspan_usa28_portStatusMessage *)data;
  537. /* Check port number from message and retrieve private data */
  538. if (msg->port >= serial->num_ports) {
  539. dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
  540. goto exit;
  541. }
  542. port = serial->port[msg->port];
  543. p_priv = usb_get_serial_port_data(port);
  544. /* Update handshaking pin state information */
  545. old_dcd_state = p_priv->dcd_state;
  546. p_priv->cts_state = ((msg->cts) ? 1 : 0);
  547. p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
  548. p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
  549. p_priv->ri_state = ((msg->ri) ? 1 : 0);
  550. if (port->tty && !C_CLOCAL(port->tty)
  551. && old_dcd_state != p_priv->dcd_state) {
  552. if (old_dcd_state)
  553. tty_hangup(port->tty);
  554. /* else */
  555. /* wake_up_interruptible(&p_priv->open_wait); */
  556. }
  557. /* Resubmit urb so we continue receiving */
  558. urb->dev = serial->dev;
  559. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
  560. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  561. }
  562. exit: ;
  563. }
  564. static void usa28_glocont_callback(struct urb *urb, struct pt_regs *regs)
  565. {
  566. dbg ("%s", __FUNCTION__);
  567. }
  568. static void usa49_glocont_callback(struct urb *urb, struct pt_regs *regs)
  569. {
  570. struct usb_serial *serial;
  571. struct usb_serial_port *port;
  572. struct keyspan_port_private *p_priv;
  573. int i;
  574. dbg ("%s", __FUNCTION__);
  575. serial = (struct usb_serial *) urb->context;
  576. for (i = 0; i < serial->num_ports; ++i) {
  577. port = serial->port[i];
  578. p_priv = usb_get_serial_port_data(port);
  579. if (p_priv->resend_cont) {
  580. dbg ("%s - sending setup", __FUNCTION__);
  581. keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
  582. break;
  583. }
  584. }
  585. }
  586. /* This is actually called glostat in the Keyspan
  587. doco */
  588. static void usa49_instat_callback(struct urb *urb, struct pt_regs *regs)
  589. {
  590. int err;
  591. unsigned char *data = urb->transfer_buffer;
  592. struct keyspan_usa49_portStatusMessage *msg;
  593. struct usb_serial *serial;
  594. struct usb_serial_port *port;
  595. struct keyspan_port_private *p_priv;
  596. int old_dcd_state;
  597. dbg ("%s", __FUNCTION__);
  598. serial = (struct usb_serial *) urb->context;
  599. if (urb->status) {
  600. dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
  601. return;
  602. }
  603. if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
  604. dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
  605. goto exit;
  606. }
  607. /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
  608. data[0], data[1], data[2], data[3], data[4], data[5],
  609. data[6], data[7], data[8], data[9], data[10]);*/
  610. /* Now do something useful with the data */
  611. msg = (struct keyspan_usa49_portStatusMessage *)data;
  612. /* Check port number from message and retrieve private data */
  613. if (msg->portNumber >= serial->num_ports) {
  614. dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
  615. goto exit;
  616. }
  617. port = serial->port[msg->portNumber];
  618. p_priv = usb_get_serial_port_data(port);
  619. /* Update handshaking pin state information */
  620. old_dcd_state = p_priv->dcd_state;
  621. p_priv->cts_state = ((msg->cts) ? 1 : 0);
  622. p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
  623. p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
  624. p_priv->ri_state = ((msg->ri) ? 1 : 0);
  625. if (port->tty && !C_CLOCAL(port->tty)
  626. && old_dcd_state != p_priv->dcd_state) {
  627. if (old_dcd_state)
  628. tty_hangup(port->tty);
  629. /* else */
  630. /* wake_up_interruptible(&p_priv->open_wait); */
  631. }
  632. /* Resubmit urb so we continue receiving */
  633. urb->dev = serial->dev;
  634. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
  635. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  636. }
  637. exit: ;
  638. }
  639. static void usa49_inack_callback(struct urb *urb, struct pt_regs *regs)
  640. {
  641. dbg ("%s", __FUNCTION__);
  642. }
  643. static void usa49_indat_callback(struct urb *urb, struct pt_regs *regs)
  644. {
  645. int i, err;
  646. int endpoint;
  647. struct usb_serial_port *port;
  648. struct tty_struct *tty;
  649. unsigned char *data = urb->transfer_buffer;
  650. dbg ("%s", __FUNCTION__);
  651. endpoint = usb_pipeendpoint(urb->pipe);
  652. if (urb->status) {
  653. dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
  654. urb->status, endpoint);
  655. return;
  656. }
  657. port = (struct usb_serial_port *) urb->context;
  658. tty = port->tty;
  659. if (urb->actual_length) {
  660. /* 0x80 bit is error flag */
  661. if ((data[0] & 0x80) == 0) {
  662. /* no error on any byte */
  663. for (i = 1; i < urb->actual_length ; ++i) {
  664. tty_insert_flip_char(tty, data[i], 0);
  665. }
  666. } else {
  667. /* some bytes had errors, every byte has status */
  668. for (i = 0; i + 1 < urb->actual_length; i += 2) {
  669. int stat = data[i], flag = 0;
  670. if (stat & RXERROR_OVERRUN)
  671. flag |= TTY_OVERRUN;
  672. if (stat & RXERROR_FRAMING)
  673. flag |= TTY_FRAME;
  674. if (stat & RXERROR_PARITY)
  675. flag |= TTY_PARITY;
  676. /* XXX should handle break (0x10) */
  677. tty_insert_flip_char(tty, data[i+1], flag);
  678. }
  679. }
  680. tty_flip_buffer_push(tty);
  681. }
  682. /* Resubmit urb so we continue receiving */
  683. urb->dev = port->serial->dev;
  684. if (port->open_count)
  685. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
  686. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  687. }
  688. }
  689. /* not used, usa-49 doesn't have per-port control endpoints */
  690. static void usa49_outcont_callback(struct urb *urb, struct pt_regs *regs)
  691. {
  692. dbg ("%s", __FUNCTION__);
  693. }
  694. static void usa90_indat_callback(struct urb *urb, struct pt_regs *regs)
  695. {
  696. int i, err;
  697. int endpoint;
  698. struct usb_serial_port *port;
  699. struct keyspan_port_private *p_priv;
  700. struct tty_struct *tty;
  701. unsigned char *data = urb->transfer_buffer;
  702. dbg ("%s", __FUNCTION__);
  703. endpoint = usb_pipeendpoint(urb->pipe);
  704. if (urb->status) {
  705. dbg("%s - nonzero status: %x on endpoint %d.",
  706. __FUNCTION__, urb->status, endpoint);
  707. return;
  708. }
  709. port = (struct usb_serial_port *) urb->context;
  710. p_priv = usb_get_serial_port_data(port);
  711. tty = port->tty;
  712. if (urb->actual_length) {
  713. /* if current mode is DMA, looks like usa28 format
  714. otherwise looks like usa26 data format */
  715. if (p_priv->baud > 57600) {
  716. for (i = 0; i < urb->actual_length ; ++i)
  717. tty_insert_flip_char(tty, data[i], 0);
  718. }
  719. else {
  720. /* 0x80 bit is error flag */
  721. if ((data[0] & 0x80) == 0) {
  722. /* no errors on individual bytes, only possible overrun err*/
  723. if (data[0] & RXERROR_OVERRUN)
  724. err = TTY_OVERRUN;
  725. else err = 0;
  726. for (i = 1; i < urb->actual_length ; ++i)
  727. tty_insert_flip_char(tty, data[i], err);
  728. }
  729. else {
  730. /* some bytes had errors, every byte has status */
  731. dbg("%s - RX error!!!!", __FUNCTION__);
  732. for (i = 0; i + 1 < urb->actual_length; i += 2) {
  733. int stat = data[i], flag = 0;
  734. if (stat & RXERROR_OVERRUN)
  735. flag |= TTY_OVERRUN;
  736. if (stat & RXERROR_FRAMING)
  737. flag |= TTY_FRAME;
  738. if (stat & RXERROR_PARITY)
  739. flag |= TTY_PARITY;
  740. /* XXX should handle break (0x10) */
  741. tty_insert_flip_char(tty, data[i+1], flag);
  742. }
  743. }
  744. }
  745. tty_flip_buffer_push(tty);
  746. }
  747. /* Resubmit urb so we continue receiving */
  748. urb->dev = port->serial->dev;
  749. if (port->open_count)
  750. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
  751. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  752. }
  753. return;
  754. }
  755. static void usa90_instat_callback(struct urb *urb, struct pt_regs *regs)
  756. {
  757. unsigned char *data = urb->transfer_buffer;
  758. struct keyspan_usa90_portStatusMessage *msg;
  759. struct usb_serial *serial;
  760. struct usb_serial_port *port;
  761. struct keyspan_port_private *p_priv;
  762. int old_dcd_state, err;
  763. serial = (struct usb_serial *) urb->context;
  764. if (urb->status) {
  765. dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
  766. return;
  767. }
  768. if (urb->actual_length < 14) {
  769. dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
  770. goto exit;
  771. }
  772. msg = (struct keyspan_usa90_portStatusMessage *)data;
  773. /* Now do something useful with the data */
  774. port = serial->port[0];
  775. p_priv = usb_get_serial_port_data(port);
  776. /* Update handshaking pin state information */
  777. old_dcd_state = p_priv->dcd_state;
  778. p_priv->cts_state = ((msg->cts) ? 1 : 0);
  779. p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
  780. p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
  781. p_priv->ri_state = ((msg->ri) ? 1 : 0);
  782. if (port->tty && !C_CLOCAL(port->tty)
  783. && old_dcd_state != p_priv->dcd_state) {
  784. if (old_dcd_state)
  785. tty_hangup(port->tty);
  786. /* else */
  787. /* wake_up_interruptible(&p_priv->open_wait); */
  788. }
  789. /* Resubmit urb so we continue receiving */
  790. urb->dev = serial->dev;
  791. if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
  792. dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
  793. }
  794. exit:
  795. ;
  796. }
  797. static void usa90_outcont_callback(struct urb *urb, struct pt_regs *regs)
  798. {
  799. struct usb_serial_port *port;
  800. struct keyspan_port_private *p_priv;
  801. port = (struct usb_serial_port *) urb->context;
  802. p_priv = usb_get_serial_port_data(port);
  803. if (p_priv->resend_cont) {
  804. dbg ("%s - sending setup", __FUNCTION__);
  805. keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
  806. }
  807. }
  808. static int keyspan_write_room (struct usb_serial_port *port)
  809. {
  810. struct keyspan_port_private *p_priv;
  811. const struct keyspan_device_details *d_details;
  812. int flip;
  813. int data_len;
  814. struct urb *this_urb;
  815. dbg("%s", __FUNCTION__);
  816. p_priv = usb_get_serial_port_data(port);
  817. d_details = p_priv->device_details;
  818. if (d_details->msg_format == msg_usa90)
  819. data_len = 64;
  820. else
  821. data_len = 63;
  822. flip = p_priv->out_flip;
  823. /* Check both endpoints to see if any are available. */
  824. if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
  825. if (this_urb->status != -EINPROGRESS)
  826. return (data_len);
  827. flip = (flip + 1) & d_details->outdat_endp_flip;
  828. if ((this_urb = p_priv->out_urbs[flip]) != NULL)
  829. if (this_urb->status != -EINPROGRESS)
  830. return (data_len);
  831. }
  832. return (0);
  833. }
  834. static int keyspan_chars_in_buffer (struct usb_serial_port *port)
  835. {
  836. return (0);
  837. }
  838. static int keyspan_open (struct usb_serial_port *port, struct file *filp)
  839. {
  840. struct keyspan_port_private *p_priv;
  841. struct keyspan_serial_private *s_priv;
  842. struct usb_serial *serial = port->serial;
  843. const struct keyspan_device_details *d_details;
  844. int i, err;
  845. int baud_rate, device_port;
  846. struct urb *urb;
  847. unsigned int cflag;
  848. s_priv = usb_get_serial_data(serial);
  849. p_priv = usb_get_serial_port_data(port);
  850. d_details = p_priv->device_details;
  851. dbg("%s - port%d.", __FUNCTION__, port->number);
  852. /* Set some sane defaults */
  853. p_priv->rts_state = 1;
  854. p_priv->dtr_state = 1;
  855. p_priv->baud = 9600;
  856. /* force baud and lcr to be set on open */
  857. p_priv->old_baud = 0;
  858. p_priv->old_cflag = 0;
  859. p_priv->out_flip = 0;
  860. p_priv->in_flip = 0;
  861. /* Reset low level data toggle and start reading from endpoints */
  862. for (i = 0; i < 2; i++) {
  863. if ((urb = p_priv->in_urbs[i]) == NULL)
  864. continue;
  865. urb->dev = serial->dev;
  866. /* make sure endpoint data toggle is synchronized with the device */
  867. usb_clear_halt(urb->dev, urb->pipe);
  868. if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
  869. dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
  870. }
  871. }
  872. /* Reset low level data toggle on out endpoints */
  873. for (i = 0; i < 2; i++) {
  874. if ((urb = p_priv->out_urbs[i]) == NULL)
  875. continue;
  876. urb->dev = serial->dev;
  877. /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
  878. }
  879. /* get the terminal config for the setup message now so we don't
  880. * need to send 2 of them */
  881. cflag = port->tty->termios->c_cflag;
  882. device_port = port->number - port->serial->minor;
  883. /* Baud rate calculation takes baud rate as an integer
  884. so other rates can be generated if desired. */
  885. baud_rate = tty_get_baud_rate(port->tty);
  886. /* If no match or invalid, leave as default */
  887. if (baud_rate >= 0
  888. && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
  889. NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
  890. p_priv->baud = baud_rate;
  891. }
  892. /* set CTS/RTS handshake etc. */
  893. p_priv->cflag = cflag;
  894. p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
  895. keyspan_send_setup(port, 1);
  896. //mdelay(100);
  897. //keyspan_set_termios(port, NULL);
  898. return (0);
  899. }
  900. static inline void stop_urb(struct urb *urb)
  901. {
  902. if (urb && urb->status == -EINPROGRESS)
  903. usb_kill_urb(urb);
  904. }
  905. static void keyspan_close(struct usb_serial_port *port, struct file *filp)
  906. {
  907. int i;
  908. struct usb_serial *serial = port->serial;
  909. struct keyspan_serial_private *s_priv;
  910. struct keyspan_port_private *p_priv;
  911. dbg("%s", __FUNCTION__);
  912. s_priv = usb_get_serial_data(serial);
  913. p_priv = usb_get_serial_port_data(port);
  914. p_priv->rts_state = 0;
  915. p_priv->dtr_state = 0;
  916. if (serial->dev) {
  917. keyspan_send_setup(port, 2);
  918. /* pilot-xfer seems to work best with this delay */
  919. mdelay(100);
  920. // keyspan_set_termios(port, NULL);
  921. }
  922. /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
  923. dbg("%s - urb in progress", __FUNCTION__);
  924. }*/
  925. p_priv->out_flip = 0;
  926. p_priv->in_flip = 0;
  927. if (serial->dev) {
  928. /* Stop reading/writing urbs */
  929. stop_urb(p_priv->inack_urb);
  930. /* stop_urb(p_priv->outcont_urb); */
  931. for (i = 0; i < 2; i++) {
  932. stop_urb(p_priv->in_urbs[i]);
  933. stop_urb(p_priv->out_urbs[i]);
  934. }
  935. }
  936. port->tty = NULL;
  937. }
  938. /* download the firmware to a pre-renumeration device */
  939. static int keyspan_fake_startup (struct usb_serial *serial)
  940. {
  941. int response;
  942. const struct ezusb_hex_record *record;
  943. char *fw_name;
  944. dbg("Keyspan startup version %04x product %04x",
  945. le16_to_cpu(serial->dev->descriptor.bcdDevice),
  946. le16_to_cpu(serial->dev->descriptor.idProduct));
  947. if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
  948. dbg("Firmware already loaded. Quitting.");
  949. return(1);
  950. }
  951. /* Select firmware image on the basis of idProduct */
  952. switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
  953. case keyspan_usa28_pre_product_id:
  954. record = &keyspan_usa28_firmware[0];
  955. fw_name = "USA28";
  956. break;
  957. case keyspan_usa28x_pre_product_id:
  958. record = &keyspan_usa28x_firmware[0];
  959. fw_name = "USA28X";
  960. break;
  961. case keyspan_usa28xa_pre_product_id:
  962. record = &keyspan_usa28xa_firmware[0];
  963. fw_name = "USA28XA";
  964. break;
  965. case keyspan_usa28xb_pre_product_id:
  966. record = &keyspan_usa28xb_firmware[0];
  967. fw_name = "USA28XB";
  968. break;
  969. case keyspan_usa19_pre_product_id:
  970. record = &keyspan_usa19_firmware[0];
  971. fw_name = "USA19";
  972. break;
  973. case keyspan_usa19qi_pre_product_id:
  974. record = &keyspan_usa19qi_firmware[0];
  975. fw_name = "USA19QI";
  976. break;
  977. case keyspan_mpr_pre_product_id:
  978. record = &keyspan_mpr_firmware[0];
  979. fw_name = "MPR";
  980. break;
  981. case keyspan_usa19qw_pre_product_id:
  982. record = &keyspan_usa19qw_firmware[0];
  983. fw_name = "USA19QI";
  984. break;
  985. case keyspan_usa18x_pre_product_id:
  986. record = &keyspan_usa18x_firmware[0];
  987. fw_name = "USA18X";
  988. break;
  989. case keyspan_usa19w_pre_product_id:
  990. record = &keyspan_usa19w_firmware[0];
  991. fw_name = "USA19W";
  992. break;
  993. case keyspan_usa49w_pre_product_id:
  994. record = &keyspan_usa49w_firmware[0];
  995. fw_name = "USA49W";
  996. break;
  997. case keyspan_usa49wlc_pre_product_id:
  998. record = &keyspan_usa49wlc_firmware[0];
  999. fw_name = "USA49WLC";
  1000. break;
  1001. default:
  1002. record = NULL;
  1003. fw_name = "Unknown";
  1004. break;
  1005. }
  1006. if (record == NULL) {
  1007. dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
  1008. return(1);
  1009. }
  1010. dbg("Uploading Keyspan %s firmware.", fw_name);
  1011. /* download the firmware image */
  1012. response = ezusb_set_reset(serial, 1);
  1013. while(record->address != 0xffff) {
  1014. response = ezusb_writememory(serial, record->address,
  1015. (unsigned char *)record->data,
  1016. record->data_size, 0xa0);
  1017. if (response < 0) {
  1018. dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
  1019. "firmware (%d %04X %p %d)\n",
  1020. response,
  1021. record->address, record->data, record->data_size);
  1022. break;
  1023. }
  1024. record++;
  1025. }
  1026. /* bring device out of reset. Renumeration will occur in a
  1027. moment and the new device will bind to the real driver */
  1028. response = ezusb_set_reset(serial, 0);
  1029. /* we don't want this device to have a driver assigned to it. */
  1030. return (1);
  1031. }
  1032. /* Helper functions used by keyspan_setup_urbs */
  1033. static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
  1034. int dir, void *ctx, char *buf, int len,
  1035. void (*callback)(struct urb *, struct pt_regs *regs))
  1036. {
  1037. struct urb *urb;
  1038. if (endpoint == -1)
  1039. return NULL; /* endpoint not needed */
  1040. dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
  1041. urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
  1042. if (urb == NULL) {
  1043. dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
  1044. return NULL;
  1045. }
  1046. /* Fill URB using supplied data. */
  1047. usb_fill_bulk_urb(urb, serial->dev,
  1048. usb_sndbulkpipe(serial->dev, endpoint) | dir,
  1049. buf, len, callback, ctx);
  1050. return urb;
  1051. }
  1052. static struct callbacks {
  1053. void (*instat_callback)(struct urb *, struct pt_regs *regs);
  1054. void (*glocont_callback)(struct urb *, struct pt_regs *regs);
  1055. void (*indat_callback)(struct urb *, struct pt_regs *regs);
  1056. void (*outdat_callback)(struct urb *, struct pt_regs *regs);
  1057. void (*inack_callback)(struct urb *, struct pt_regs *regs);
  1058. void (*outcont_callback)(struct urb *, struct pt_regs *regs);
  1059. } keyspan_callbacks[] = {
  1060. {
  1061. /* msg_usa26 callbacks */
  1062. .instat_callback = usa26_instat_callback,
  1063. .glocont_callback = usa26_glocont_callback,
  1064. .indat_callback = usa26_indat_callback,
  1065. .outdat_callback = usa2x_outdat_callback,
  1066. .inack_callback = usa26_inack_callback,
  1067. .outcont_callback = usa26_outcont_callback,
  1068. }, {
  1069. /* msg_usa28 callbacks */
  1070. .instat_callback = usa28_instat_callback,
  1071. .glocont_callback = usa28_glocont_callback,
  1072. .indat_callback = usa28_indat_callback,
  1073. .outdat_callback = usa2x_outdat_callback,
  1074. .inack_callback = usa28_inack_callback,
  1075. .outcont_callback = usa28_outcont_callback,
  1076. }, {
  1077. /* msg_usa49 callbacks */
  1078. .instat_callback = usa49_instat_callback,
  1079. .glocont_callback = usa49_glocont_callback,
  1080. .indat_callback = usa49_indat_callback,
  1081. .outdat_callback = usa2x_outdat_callback,
  1082. .inack_callback = usa49_inack_callback,
  1083. .outcont_callback = usa49_outcont_callback,
  1084. }, {
  1085. /* msg_usa90 callbacks */
  1086. .instat_callback = usa90_instat_callback,
  1087. .glocont_callback = usa28_glocont_callback,
  1088. .indat_callback = usa90_indat_callback,
  1089. .outdat_callback = usa2x_outdat_callback,
  1090. .inack_callback = usa28_inack_callback,
  1091. .outcont_callback = usa90_outcont_callback,
  1092. }
  1093. };
  1094. /* Generic setup urbs function that uses
  1095. data in device_details */
  1096. static void keyspan_setup_urbs(struct usb_serial *serial)
  1097. {
  1098. int i, j;
  1099. struct keyspan_serial_private *s_priv;
  1100. const struct keyspan_device_details *d_details;
  1101. struct usb_serial_port *port;
  1102. struct keyspan_port_private *p_priv;
  1103. struct callbacks *cback;
  1104. int endp;
  1105. dbg ("%s", __FUNCTION__);
  1106. s_priv = usb_get_serial_data(serial);
  1107. d_details = s_priv->device_details;
  1108. /* Setup values for the various callback routines */
  1109. cback = &keyspan_callbacks[d_details->msg_format];
  1110. /* Allocate and set up urbs for each one that is in use,
  1111. starting with instat endpoints */
  1112. s_priv->instat_urb = keyspan_setup_urb
  1113. (serial, d_details->instat_endpoint, USB_DIR_IN,
  1114. serial, s_priv->instat_buf, INSTAT_BUFLEN,
  1115. cback->instat_callback);
  1116. s_priv->glocont_urb = keyspan_setup_urb
  1117. (serial, d_details->glocont_endpoint, USB_DIR_OUT,
  1118. serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
  1119. cback->glocont_callback);
  1120. /* Setup endpoints for each port specific thing */
  1121. for (i = 0; i < d_details->num_ports; i ++) {
  1122. port = serial->port[i];
  1123. p_priv = usb_get_serial_port_data(port);
  1124. /* Do indat endpoints first, once for each flip */
  1125. endp = d_details->indat_endpoints[i];
  1126. for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
  1127. p_priv->in_urbs[j] = keyspan_setup_urb
  1128. (serial, endp, USB_DIR_IN, port,
  1129. p_priv->in_buffer[j], 64,
  1130. cback->indat_callback);
  1131. }
  1132. for (; j < 2; ++j)
  1133. p_priv->in_urbs[j] = NULL;
  1134. /* outdat endpoints also have flip */
  1135. endp = d_details->outdat_endpoints[i];
  1136. for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
  1137. p_priv->out_urbs[j] = keyspan_setup_urb
  1138. (serial, endp, USB_DIR_OUT, port,
  1139. p_priv->out_buffer[j], 64,
  1140. cback->outdat_callback);
  1141. }
  1142. for (; j < 2; ++j)
  1143. p_priv->out_urbs[j] = NULL;
  1144. /* inack endpoint */
  1145. p_priv->inack_urb = keyspan_setup_urb
  1146. (serial, d_details->inack_endpoints[i], USB_DIR_IN,
  1147. port, p_priv->inack_buffer, 1, cback->inack_callback);
  1148. /* outcont endpoint */
  1149. p_priv->outcont_urb = keyspan_setup_urb
  1150. (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
  1151. port, p_priv->outcont_buffer, 64,
  1152. cback->outcont_callback);
  1153. }
  1154. }
  1155. /* usa19 function doesn't require prescaler */
  1156. static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
  1157. u8 *rate_low, u8 *prescaler, int portnum)
  1158. {
  1159. u32 b16, /* baud rate times 16 (actual rate used internally) */
  1160. div, /* divisor */
  1161. cnt; /* inverse of divisor (programmed into 8051) */
  1162. dbg ("%s - %d.", __FUNCTION__, baud_rate);
  1163. /* prevent divide by zero... */
  1164. if( (b16 = (baud_rate * 16L)) == 0) {
  1165. return (KEYSPAN_INVALID_BAUD_RATE);
  1166. }
  1167. /* Any "standard" rate over 57k6 is marginal on the USA-19
  1168. as we run out of divisor resolution. */
  1169. if (baud_rate > 57600) {
  1170. return (KEYSPAN_INVALID_BAUD_RATE);
  1171. }
  1172. /* calculate the divisor and the counter (its inverse) */
  1173. if( (div = (baudclk / b16)) == 0) {
  1174. return (KEYSPAN_INVALID_BAUD_RATE);
  1175. }
  1176. else {
  1177. cnt = 0 - div;
  1178. }
  1179. if(div > 0xffff) {
  1180. return (KEYSPAN_INVALID_BAUD_RATE);
  1181. }
  1182. /* return the counter values if non-null */
  1183. if (rate_low) {
  1184. *rate_low = (u8) (cnt & 0xff);
  1185. }
  1186. if (rate_hi) {
  1187. *rate_hi = (u8) ((cnt >> 8) & 0xff);
  1188. }
  1189. if (rate_low && rate_hi) {
  1190. dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
  1191. }
  1192. return (KEYSPAN_BAUD_RATE_OK);
  1193. }
  1194. /* usa19hs function doesn't require prescaler */
  1195. static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
  1196. u8 *rate_low, u8 *prescaler, int portnum)
  1197. {
  1198. u32 b16, /* baud rate times 16 (actual rate used internally) */
  1199. div; /* divisor */
  1200. dbg ("%s - %d.", __FUNCTION__, baud_rate);
  1201. /* prevent divide by zero... */
  1202. if( (b16 = (baud_rate * 16L)) == 0)
  1203. return (KEYSPAN_INVALID_BAUD_RATE);
  1204. /* calculate the divisor */
  1205. if( (div = (baudclk / b16)) == 0)
  1206. return (KEYSPAN_INVALID_BAUD_RATE);
  1207. if(div > 0xffff)
  1208. return (KEYSPAN_INVALID_BAUD_RATE);
  1209. /* return the counter values if non-null */
  1210. if (rate_low)
  1211. *rate_low = (u8) (div & 0xff);
  1212. if (rate_hi)
  1213. *rate_hi = (u8) ((div >> 8) & 0xff);
  1214. if (rate_low && rate_hi)
  1215. dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
  1216. return (KEYSPAN_BAUD_RATE_OK);
  1217. }
  1218. static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
  1219. u8 *rate_low, u8 *prescaler, int portnum)
  1220. {
  1221. u32 b16, /* baud rate times 16 (actual rate used internally) */
  1222. clk, /* clock with 13/8 prescaler */
  1223. div, /* divisor using 13/8 prescaler */
  1224. res, /* resulting baud rate using 13/8 prescaler */
  1225. diff, /* error using 13/8 prescaler */
  1226. smallest_diff;
  1227. u8 best_prescaler;
  1228. int i;
  1229. dbg ("%s - %d.", __FUNCTION__, baud_rate);
  1230. /* prevent divide by zero */
  1231. if( (b16 = baud_rate * 16L) == 0) {
  1232. return (KEYSPAN_INVALID_BAUD_RATE);
  1233. }
  1234. /* Calculate prescaler by trying them all and looking
  1235. for best fit */
  1236. /* start with largest possible difference */
  1237. smallest_diff = 0xffffffff;
  1238. /* 0 is an invalid prescaler, used as a flag */
  1239. best_prescaler = 0;
  1240. for(i = 8; i <= 0xff; ++i) {
  1241. clk = (baudclk * 8) / (u32) i;
  1242. if( (div = clk / b16) == 0) {
  1243. continue;
  1244. }
  1245. res = clk / div;
  1246. diff= (res > b16) ? (res-b16) : (b16-res);
  1247. if(diff < smallest_diff) {
  1248. best_prescaler = i;
  1249. smallest_diff = diff;
  1250. }
  1251. }
  1252. if(best_prescaler == 0) {
  1253. return (KEYSPAN_INVALID_BAUD_RATE);
  1254. }
  1255. clk = (baudclk * 8) / (u32) best_prescaler;
  1256. div = clk / b16;
  1257. /* return the divisor and prescaler if non-null */
  1258. if (rate_low) {
  1259. *rate_low = (u8) (div & 0xff);
  1260. }
  1261. if (rate_hi) {
  1262. *rate_hi = (u8) ((div >> 8) & 0xff);
  1263. }
  1264. if (prescaler) {
  1265. *prescaler = best_prescaler;
  1266. /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
  1267. }
  1268. return (KEYSPAN_BAUD_RATE_OK);
  1269. }
  1270. /* USA-28 supports different maximum baud rates on each port */
  1271. static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
  1272. u8 *rate_low, u8 *prescaler, int portnum)
  1273. {
  1274. u32 b16, /* baud rate times 16 (actual rate used internally) */
  1275. div, /* divisor */
  1276. cnt; /* inverse of divisor (programmed into 8051) */
  1277. dbg ("%s - %d.", __FUNCTION__, baud_rate);
  1278. /* prevent divide by zero */
  1279. if ((b16 = baud_rate * 16L) == 0)
  1280. return (KEYSPAN_INVALID_BAUD_RATE);
  1281. /* calculate the divisor and the counter (its inverse) */
  1282. if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
  1283. return (KEYSPAN_INVALID_BAUD_RATE);
  1284. }
  1285. else {
  1286. cnt = 0 - div;
  1287. }
  1288. /* check for out of range, based on portnum,
  1289. and return result */
  1290. if(portnum == 0) {
  1291. if(div > 0xffff)
  1292. return (KEYSPAN_INVALID_BAUD_RATE);
  1293. }
  1294. else {
  1295. if(portnum == 1) {
  1296. if(div > 0xff) {
  1297. return (KEYSPAN_INVALID_BAUD_RATE);
  1298. }
  1299. }
  1300. else {
  1301. return (KEYSPAN_INVALID_BAUD_RATE);
  1302. }
  1303. }
  1304. /* return the counter values if not NULL
  1305. (port 1 will ignore retHi) */
  1306. if (rate_low) {
  1307. *rate_low = (u8) (cnt & 0xff);
  1308. }
  1309. if (rate_hi) {
  1310. *rate_hi = (u8) ((cnt >> 8) & 0xff);
  1311. }
  1312. dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
  1313. return (KEYSPAN_BAUD_RATE_OK);
  1314. }
  1315. static int keyspan_usa26_send_setup(struct usb_serial *serial,
  1316. struct usb_serial_port *port,
  1317. int reset_port)
  1318. {
  1319. struct keyspan_usa26_portControlMessage msg;
  1320. struct keyspan_serial_private *s_priv;
  1321. struct keyspan_port_private *p_priv;
  1322. const struct keyspan_device_details *d_details;
  1323. int outcont_urb;
  1324. struct urb *this_urb;
  1325. int device_port, err;
  1326. dbg ("%s reset=%d", __FUNCTION__, reset_port);
  1327. s_priv = usb_get_serial_data(serial);
  1328. p_priv = usb_get_serial_port_data(port);
  1329. d_details = s_priv->device_details;
  1330. device_port = port->number - port->serial->minor;
  1331. outcont_urb = d_details->outcont_endpoints[port->number];
  1332. this_urb = p_priv->outcont_urb;
  1333. dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
  1334. /* Make sure we have an urb then send the message */
  1335. if (this_urb == NULL) {
  1336. dbg("%s - oops no urb.", __FUNCTION__);
  1337. return -1;
  1338. }
  1339. /* Save reset port val for resend.
  1340. Don't overwrite resend for close condition. */
  1341. if (p_priv->resend_cont != 3)
  1342. p_priv->resend_cont = reset_port + 1;
  1343. if (this_urb->status == -EINPROGRESS) {
  1344. /* dbg ("%s - already writing", __FUNCTION__); */
  1345. mdelay(5);
  1346. return(-1);
  1347. }
  1348. memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
  1349. /* Only set baud rate if it's changed */
  1350. if (p_priv->old_baud != p_priv->baud) {
  1351. p_priv->old_baud = p_priv->baud;
  1352. msg.setClocking = 0xff;
  1353. if (d_details->calculate_baud_rate
  1354. (p_priv->baud, d_details->baudclk, &msg.baudHi,
  1355. &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
  1356. dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
  1357. p_priv->baud);
  1358. msg.baudLo = 0;
  1359. msg.baudHi = 125; /* Values for 9600 baud */
  1360. msg.prescaler = 10;
  1361. }
  1362. msg.setPrescaler = 0xff;
  1363. }
  1364. msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
  1365. switch (p_priv->cflag & CSIZE) {
  1366. case CS5:
  1367. msg.lcr |= USA_DATABITS_5;
  1368. break;
  1369. case CS6:
  1370. msg.lcr |= USA_DATABITS_6;
  1371. break;
  1372. case CS7:
  1373. msg.lcr |= USA_DATABITS_7;
  1374. break;
  1375. case CS8:
  1376. msg.lcr |= USA_DATABITS_8;
  1377. break;
  1378. }
  1379. if (p_priv->cflag & PARENB) {
  1380. /* note USA_PARITY_NONE == 0 */
  1381. msg.lcr |= (p_priv->cflag & PARODD)?
  1382. USA_PARITY_ODD: USA_PARITY_EVEN;
  1383. }
  1384. msg.setLcr = 0xff;
  1385. msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
  1386. msg.xonFlowControl = 0;
  1387. msg.setFlowControl = 0xff;
  1388. msg.forwardingLength = 16;
  1389. msg.xonChar = 17;
  1390. msg.xoffChar = 19;
  1391. /* Opening port */
  1392. if (reset_port == 1) {
  1393. msg._txOn = 1;
  1394. msg._txOff = 0;
  1395. msg.txFlush = 0;
  1396. msg.txBreak = 0;
  1397. msg.rxOn = 1;
  1398. msg.rxOff = 0;
  1399. msg.rxFlush = 1;
  1400. msg.rxForward = 0;
  1401. msg.returnStatus = 0;
  1402. msg.resetDataToggle = 0xff;
  1403. }
  1404. /* Closing port */
  1405. else if (reset_port == 2) {
  1406. msg._txOn = 0;
  1407. msg._txOff = 1;
  1408. msg.txFlush = 0;
  1409. msg.txBreak = 0;
  1410. msg.rxOn = 0;
  1411. msg.rxOff = 1;
  1412. msg.rxFlush = 1;
  1413. msg.rxForward = 0;
  1414. msg.returnStatus = 0;
  1415. msg.resetDataToggle = 0;
  1416. }
  1417. /* Sending intermediate configs */
  1418. else {
  1419. msg._txOn = (! p_priv->break_on);
  1420. msg._txOff = 0;
  1421. msg.txFlush = 0;
  1422. msg.txBreak = (p_priv->break_on);
  1423. msg.rxOn = 0;
  1424. msg.rxOff = 0;
  1425. msg.rxFlush = 0;
  1426. msg.rxForward = 0;
  1427. msg.returnStatus = 0;
  1428. msg.resetDataToggle = 0x0;
  1429. }
  1430. /* Do handshaking outputs */
  1431. msg.setTxTriState_setRts = 0xff;
  1432. msg.txTriState_rts = p_priv->rts_state;
  1433. msg.setHskoa_setDtr = 0xff;
  1434. msg.hskoa_dtr = p_priv->dtr_state;
  1435. p_priv->resend_cont = 0;
  1436. memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
  1437. /* send the data out the device on control endpoint */
  1438. this_urb->transfer_buffer_length = sizeof(msg);
  1439. this_urb->dev = serial->dev;
  1440. if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
  1441. dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
  1442. }
  1443. #if 0
  1444. else {
  1445. dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
  1446. outcont_urb, this_urb->transfer_buffer_length,
  1447. usb_pipeendpoint(this_urb->pipe));
  1448. }
  1449. #endif
  1450. return (0);
  1451. }
  1452. static int keyspan_usa28_send_setup(struct usb_serial *serial,
  1453. struct usb_serial_port *port,
  1454. int reset_port)
  1455. {
  1456. struct keyspan_usa28_portControlMessage msg;
  1457. struct keyspan_serial_private *s_priv;
  1458. struct keyspan_port_private *p_priv;
  1459. const struct keyspan_device_details *d_details;
  1460. struct urb *this_urb;
  1461. int device_port, err;
  1462. dbg ("%s", __FUNCTION__);
  1463. s_priv = usb_get_serial_data(serial);
  1464. p_priv = usb_get_serial_port_data(port);
  1465. d_details = s_priv->device_details;
  1466. device_port = port->number - port->serial->minor;
  1467. /* only do something if we have a bulk out endpoint */
  1468. if ((this_urb = p_priv->outcont_urb) == NULL) {
  1469. dbg("%s - oops no urb.", __FUNCTION__);
  1470. return -1;
  1471. }
  1472. /* Save reset port val for resend.
  1473. Don't overwrite resend for close condition. */
  1474. if (p_priv->resend_cont != 3)
  1475. p_priv->resend_cont = reset_port + 1;
  1476. if (this_urb->status == -EINPROGRESS) {
  1477. dbg ("%s already writing", __FUNCTION__);
  1478. mdelay(5);
  1479. return(-1);
  1480. }
  1481. memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
  1482. msg.setBaudRate = 1;
  1483. if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
  1484. &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
  1485. dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
  1486. msg.baudLo = 0xff;
  1487. msg.baudHi = 0xb2; /* Values for 9600 baud */
  1488. }
  1489. /* If parity is enabled, we must calculate it ourselves. */
  1490. msg.parity = 0; /* XXX for now */
  1491. msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
  1492. msg.xonFlowControl = 0;
  1493. /* Do handshaking outputs, DTR is inverted relative to RTS */
  1494. msg.rts = p_priv->rts_state;
  1495. msg.dtr = p_priv->dtr_state;
  1496. msg.forwardingLength = 16;
  1497. msg.forwardMs = 10;
  1498. msg.breakThreshold = 45;
  1499. msg.xonChar = 17;
  1500. msg.xoffChar = 19;
  1501. /*msg.returnStatus = 1;
  1502. msg.resetDataToggle = 0xff;*/
  1503. /* Opening port */
  1504. if (reset_port == 1) {
  1505. msg._txOn = 1;
  1506. msg._txOff = 0;
  1507. msg.txFlush = 0;
  1508. msg.txForceXoff = 0;
  1509. msg.txBreak = 0;
  1510. msg.rxOn = 1;
  1511. msg.rxOff = 0;
  1512. msg.rxFlush = 1;
  1513. msg.rxForward = 0;
  1514. msg.returnStatus = 0;
  1515. msg.resetDataToggle = 0xff;
  1516. }
  1517. /* Closing port */
  1518. else if (reset_port == 2) {
  1519. msg._txOn = 0;
  1520. msg._txOff = 1;
  1521. msg.txFlush = 0;
  1522. msg.txForceXoff = 0;
  1523. msg.txBreak = 0;
  1524. msg.rxOn = 0;
  1525. msg.rxOff = 1;
  1526. msg.rxFlush = 1;
  1527. msg.rxForward = 0;
  1528. msg.returnStatus = 0;
  1529. msg.resetDataToggle = 0;
  1530. }
  1531. /* Sending intermediate configs */
  1532. else {
  1533. msg._txOn = (! p_priv->break_on);
  1534. msg._txOff = 0;
  1535. msg.txFlush = 0;
  1536. msg.txForceXoff = 0;
  1537. msg.txBreak = (p_priv->break_on);
  1538. msg.rxOn = 0;
  1539. msg.rxOff = 0;
  1540. msg.rxFlush = 0;
  1541. msg.rxForward = 0;
  1542. msg.returnStatus = 0;
  1543. msg.resetDataToggle = 0x0;
  1544. }
  1545. p_priv->resend_cont = 0;
  1546. memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
  1547. /* send the data out the device on control endpoint */
  1548. this_urb->transfer_buffer_length = sizeof(msg);
  1549. this_urb->dev = serial->dev;
  1550. if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
  1551. dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
  1552. }
  1553. #if 0
  1554. else {
  1555. dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
  1556. this_urb->transfer_buffer_length);
  1557. }
  1558. #endif
  1559. return (0);
  1560. }
  1561. static int keyspan_usa49_send_setup(struct usb_serial *serial,
  1562. struct usb_serial_port *port,
  1563. int reset_port)
  1564. {
  1565. struct keyspan_usa49_portControlMessage msg;
  1566. struct keyspan_serial_private *s_priv;
  1567. struct keyspan_port_private *p_priv;
  1568. const struct keyspan_device_details *d_details;
  1569. int glocont_urb;
  1570. struct urb *this_urb;
  1571. int err, device_port;
  1572. dbg ("%s", __FUNCTION__);
  1573. s_priv = usb_get_serial_data(serial);
  1574. p_priv = usb_get_serial_port_data(port);
  1575. d_details = s_priv->device_details;
  1576. glocont_urb = d_details->glocont_endpoint;
  1577. this_urb = s_priv->glocont_urb;
  1578. /* Work out which port within the device is being setup */
  1579. device_port = port->number - port->serial->minor;
  1580. dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
  1581. /* Make sure we have an urb then send the message */
  1582. if (this_urb == NULL) {
  1583. dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
  1584. return -1;
  1585. }
  1586. /* Save reset port val for resend.
  1587. Don't overwrite resend for close condition. */
  1588. if (p_priv->resend_cont != 3)
  1589. p_priv->resend_cont = reset_port + 1;
  1590. if (this_urb->status == -EINPROGRESS) {
  1591. /* dbg ("%s - already writing", __FUNCTION__); */
  1592. mdelay(5);
  1593. return(-1);
  1594. }
  1595. memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
  1596. /*msg.portNumber = port->number;*/
  1597. msg.portNumber = device_port;
  1598. /* Only set baud rate if it's changed */
  1599. if (p_priv->old_baud != p_priv->baud) {
  1600. p_priv->old_baud = p_priv->baud;
  1601. msg.setClocking = 0xff;
  1602. if (d_details->calculate_baud_rate
  1603. (p_priv->baud, d_details->baudclk, &msg.baudHi,
  1604. &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
  1605. dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
  1606. p_priv->baud);
  1607. msg.baudLo = 0;
  1608. msg.baudHi = 125; /* Values for 9600 baud */
  1609. msg.prescaler = 10;
  1610. }
  1611. //msg.setPrescaler = 0xff;
  1612. }
  1613. msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
  1614. switch (p_priv->cflag & CSIZE) {
  1615. case CS5:
  1616. msg.lcr |= USA_DATABITS_5;
  1617. break;
  1618. case CS6:
  1619. msg.lcr |= USA_DATABITS_6;
  1620. break;
  1621. case CS7:
  1622. msg.lcr |= USA_DATABITS_7;
  1623. break;
  1624. case CS8:
  1625. msg.lcr |= USA_DATABITS_8;
  1626. break;
  1627. }
  1628. if (p_priv->cflag & PARENB) {
  1629. /* note USA_PARITY_NONE == 0 */
  1630. msg.lcr |= (p_priv->cflag & PARODD)?
  1631. USA_PARITY_ODD: USA_PARITY_EVEN;
  1632. }
  1633. msg.setLcr = 0xff;
  1634. msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
  1635. msg.xonFlowControl = 0;
  1636. msg.setFlowControl = 0xff;
  1637. msg.forwardingLength = 16;
  1638. msg.xonChar = 17;
  1639. msg.xoffChar = 19;
  1640. /* Opening port */
  1641. if (reset_port == 1) {
  1642. msg._txOn = 1;
  1643. msg._txOff = 0;
  1644. msg.txFlush = 0;
  1645. msg.txBreak = 0;
  1646. msg.rxOn = 1;
  1647. msg.rxOff = 0;
  1648. msg.rxFlush = 1;
  1649. msg.rxForward = 0;
  1650. msg.returnStatus = 0;
  1651. msg.resetDataToggle = 0xff;
  1652. msg.enablePort = 1;
  1653. msg.disablePort = 0;
  1654. }
  1655. /* Closing port */
  1656. else if (reset_port == 2) {
  1657. msg._txOn = 0;
  1658. msg._txOff = 1;
  1659. msg.txFlush = 0;
  1660. msg.txBreak = 0;
  1661. msg.rxOn = 0;
  1662. msg.rxOff = 1;
  1663. msg.rxFlush = 1;
  1664. msg.rxForward = 0;
  1665. msg.returnStatus = 0;
  1666. msg.resetDataToggle = 0;
  1667. msg.enablePort = 0;
  1668. msg.disablePort = 1;
  1669. }
  1670. /* Sending intermediate configs */
  1671. else {
  1672. msg._txOn = (! p_priv->break_on);
  1673. msg._txOff = 0;
  1674. msg.txFlush = 0;
  1675. msg.txBreak = (p_priv->break_on);
  1676. msg.rxOn = 0;
  1677. msg.rxOff = 0;
  1678. msg.rxFlush = 0;
  1679. msg.rxForward = 0;
  1680. msg.returnStatus = 0;
  1681. msg.resetDataToggle = 0x0;
  1682. msg.enablePort = 0;
  1683. msg.disablePort = 0;
  1684. }
  1685. /* Do handshaking outputs */
  1686. msg.setRts = 0xff;
  1687. msg.rts = p_priv->rts_state;
  1688. msg.setDtr = 0xff;
  1689. msg.dtr = p_priv->dtr_state;
  1690. p_priv->resend_cont = 0;
  1691. memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
  1692. /* send the data out the device on control endpoint */
  1693. this_urb->transfer_buffer_length = sizeof(msg);
  1694. this_urb->dev = serial->dev;
  1695. if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
  1696. dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
  1697. }
  1698. #if 0
  1699. else {
  1700. dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
  1701. outcont_urb, this_urb->transfer_buffer_length,
  1702. usb_pipeendpoint(this_urb->pipe));
  1703. }
  1704. #endif
  1705. return (0);
  1706. }
  1707. static int keyspan_usa90_send_setup(struct usb_serial *serial,
  1708. struct usb_serial_port *port,
  1709. int reset_port)
  1710. {
  1711. struct keyspan_usa90_portControlMessage msg;
  1712. struct keyspan_serial_private *s_priv;
  1713. struct keyspan_port_private *p_priv;
  1714. const struct keyspan_device_details *d_details;
  1715. struct urb *this_urb;
  1716. int err;
  1717. u8 prescaler;
  1718. dbg ("%s", __FUNCTION__);
  1719. s_priv = usb_get_serial_data(serial);
  1720. p_priv = usb_get_serial_port_data(port);
  1721. d_details = s_priv->device_details;
  1722. /* only do something if we have a bulk out endpoint */
  1723. if ((this_urb = p_priv->outcont_urb) == NULL) {
  1724. dbg("%s - oops no urb.", __FUNCTION__);
  1725. return -1;
  1726. }
  1727. /* Save reset port val for resend.
  1728. Don't overwrite resend for open/close condition. */
  1729. if ((reset_port + 1) > p_priv->resend_cont)
  1730. p_priv->resend_cont = reset_port + 1;
  1731. if (this_urb->status == -EINPROGRESS) {
  1732. dbg ("%s already writing", __FUNCTION__);
  1733. mdelay(5);
  1734. return(-1);
  1735. }
  1736. memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
  1737. /* Only set baud rate if it's changed */
  1738. if (p_priv->old_baud != p_priv->baud) {
  1739. p_priv->old_baud = p_priv->baud;
  1740. msg.setClocking = 0x01;
  1741. if (d_details->calculate_baud_rate
  1742. (p_priv->baud, d_details->baudclk, &msg.baudHi,
  1743. &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
  1744. dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
  1745. p_priv->baud);
  1746. p_priv->baud = 9600;
  1747. d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
  1748. &msg.baudHi, &msg.baudLo, &prescaler, 0);
  1749. }
  1750. msg.setRxMode = 1;
  1751. msg.setTxMode = 1;
  1752. }
  1753. /* modes must always be correctly specified */
  1754. if (p_priv->baud > 57600)
  1755. {
  1756. msg.rxMode = RXMODE_DMA;
  1757. msg.txMode = TXMODE_DMA;
  1758. }
  1759. else
  1760. {
  1761. msg.rxMode = RXMODE_BYHAND;
  1762. msg.txMode = TXMODE_BYHAND;
  1763. }
  1764. msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
  1765. switch (p_priv->cflag & CSIZE) {
  1766. case CS5:
  1767. msg.lcr |= USA_DATABITS_5;
  1768. break;
  1769. case CS6:
  1770. msg.lcr |= USA_DATABITS_6;
  1771. break;
  1772. case CS7:
  1773. msg.lcr |= USA_DATABITS_7;
  1774. break;
  1775. case CS8:
  1776. msg.lcr |= USA_DATABITS_8;
  1777. break;
  1778. }
  1779. if (p_priv->cflag & PARENB) {
  1780. /* note USA_PARITY_NONE == 0 */
  1781. msg.lcr |= (p_priv->cflag & PARODD)?
  1782. USA_PARITY_ODD: USA_PARITY_EVEN;
  1783. }
  1784. if (p_priv->old_cflag != p_priv->cflag) {
  1785. p_priv->old_cflag = p_priv->cflag;
  1786. msg.setLcr = 0x01;
  1787. }
  1788. if (p_priv->flow_control == flow_cts)
  1789. msg.txFlowControl = TXFLOW_CTS;
  1790. msg.setTxFlowControl = 0x01;
  1791. msg.setRxFlowControl = 0x01;
  1792. msg.rxForwardingLength = 16;
  1793. msg.rxForwardingTimeout = 16;
  1794. msg.txAckSetting = 0;
  1795. msg.xonChar = 17;
  1796. msg.xoffChar = 19;
  1797. /* Opening port */
  1798. if (reset_port == 1) {
  1799. msg.portEnabled = 1;
  1800. msg.rxFlush = 1;
  1801. msg.txBreak = (p_priv->break_on);
  1802. }
  1803. /* Closing port */
  1804. else if (reset_port == 2) {
  1805. msg.portEnabled = 0;
  1806. }
  1807. /* Sending intermediate configs */
  1808. else {
  1809. if (port->open_count)
  1810. msg.portEnabled = 1;
  1811. msg.txBreak = (p_priv->break_on);
  1812. }
  1813. /* Do handshaking outputs */
  1814. msg.setRts = 0x01;
  1815. msg.rts = p_priv->rts_state;
  1816. msg.setDtr = 0x01;
  1817. msg.dtr = p_priv->dtr_state;
  1818. p_priv->resend_cont = 0;
  1819. memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
  1820. /* send the data out the device on control endpoint */
  1821. this_urb->transfer_buffer_length = sizeof(msg);
  1822. this_urb->dev = serial->dev;
  1823. if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
  1824. dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
  1825. }
  1826. return (0);
  1827. }
  1828. static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
  1829. {
  1830. struct usb_serial *serial = port->serial;
  1831. struct keyspan_serial_private *s_priv;
  1832. const struct keyspan_device_details *d_details;
  1833. dbg ("%s", __FUNCTION__);
  1834. s_priv = usb_get_serial_data(serial);
  1835. d_details = s_priv->device_details;
  1836. switch (d_details->msg_format) {
  1837. case msg_usa26:
  1838. keyspan_usa26_send_setup(serial, port, reset_port);
  1839. break;
  1840. case msg_usa28:
  1841. keyspan_usa28_send_setup(serial, port, reset_port);
  1842. break;
  1843. case msg_usa49:
  1844. keyspan_usa49_send_setup(serial, port, reset_port);
  1845. break;
  1846. case msg_usa90:
  1847. keyspan_usa90_send_setup(serial, port, reset_port);
  1848. break;
  1849. }
  1850. }
  1851. /* Gets called by the "real" driver (ie once firmware is loaded
  1852. and renumeration has taken place. */
  1853. static int keyspan_startup (struct usb_serial *serial)
  1854. {
  1855. int i, err;
  1856. struct usb_serial_port *port;
  1857. struct keyspan_serial_private *s_priv;
  1858. struct keyspan_port_private *p_priv;
  1859. const struct keyspan_device_details *d_details;
  1860. dbg("%s", __FUNCTION__);
  1861. for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
  1862. if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
  1863. break;
  1864. if (d_details == NULL) {
  1865. dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, le16_to_cpu(serial->dev->descriptor.idProduct));
  1866. return 1;
  1867. }
  1868. /* Setup private data for serial driver */
  1869. s_priv = kmalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
  1870. if (!s_priv) {
  1871. dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
  1872. return -ENOMEM;
  1873. }
  1874. memset(s_priv, 0, sizeof(struct keyspan_serial_private));
  1875. s_priv->device_details = d_details;
  1876. usb_set_serial_data(serial, s_priv);
  1877. /* Now setup per port private data */
  1878. for (i = 0; i < serial->num_ports; i++) {
  1879. port = serial->port[i];
  1880. p_priv = kmalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
  1881. if (!p_priv) {
  1882. dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
  1883. return (1);
  1884. }
  1885. memset(p_priv, 0, sizeof(struct keyspan_port_private));
  1886. p_priv->device_details = d_details;
  1887. usb_set_serial_port_data(port, p_priv);
  1888. }
  1889. keyspan_setup_urbs(serial);
  1890. s_priv->instat_urb->dev = serial->dev;
  1891. if ((err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL)) != 0) {
  1892. dbg("%s - submit instat urb failed %d", __FUNCTION__, err);
  1893. }
  1894. return (0);
  1895. }
  1896. static void keyspan_shutdown (struct usb_serial *serial)
  1897. {
  1898. int i, j;
  1899. struct usb_serial_port *port;
  1900. struct keyspan_serial_private *s_priv;
  1901. struct keyspan_port_private *p_priv;
  1902. dbg("%s", __FUNCTION__);
  1903. s_priv = usb_get_serial_data(serial);
  1904. /* Stop reading/writing urbs */
  1905. stop_urb(s_priv->instat_urb);
  1906. stop_urb(s_priv->glocont_urb);
  1907. for (i = 0; i < serial->num_ports; ++i) {
  1908. port = serial->port[i];
  1909. p_priv = usb_get_serial_port_data(port);
  1910. stop_urb(p_priv->inack_urb);
  1911. stop_urb(p_priv->outcont_urb);
  1912. for (j = 0; j < 2; j++) {
  1913. stop_urb(p_priv->in_urbs[j]);
  1914. stop_urb(p_priv->out_urbs[j]);
  1915. }
  1916. }
  1917. /* Now free them */
  1918. if (s_priv->instat_urb)
  1919. usb_free_urb(s_priv->instat_urb);
  1920. if (s_priv->glocont_urb)
  1921. usb_free_urb(s_priv->glocont_urb);
  1922. for (i = 0; i < serial->num_ports; ++i) {
  1923. port = serial->port[i];
  1924. p_priv = usb_get_serial_port_data(port);
  1925. if (p_priv->inack_urb)
  1926. usb_free_urb(p_priv->inack_urb);
  1927. if (p_priv->outcont_urb)
  1928. usb_free_urb(p_priv->outcont_urb);
  1929. for (j = 0; j < 2; j++) {
  1930. if (p_priv->in_urbs[j])
  1931. usb_free_urb(p_priv->in_urbs[j]);
  1932. if (p_priv->out_urbs[j])
  1933. usb_free_urb(p_priv->out_urbs[j]);
  1934. }
  1935. }
  1936. /* dbg("Freeing serial->private."); */
  1937. kfree(s_priv);
  1938. /* dbg("Freeing port->private."); */
  1939. /* Now free per port private data */
  1940. for (i = 0; i < serial->num_ports; i++) {
  1941. port = serial->port[i];
  1942. kfree(usb_get_serial_port_data(port));
  1943. }
  1944. }
  1945. MODULE_AUTHOR( DRIVER_AUTHOR );
  1946. MODULE_DESCRIPTION( DRIVER_DESC );
  1947. MODULE_LICENSE("GPL");
  1948. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1949. MODULE_PARM_DESC(debug, "Debug enabled or not");