keyspan.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. Keyspan USB to Serial Converter driver
  4. (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org>
  5. (C) Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com>
  6. See http://blemings.org/hugh/keyspan.html for more information.
  7. Code in this driver inspired by and in a number of places taken
  8. from Brian Warner's original Keyspan-PDA driver.
  9. This driver has been put together with the support of Innosys, Inc.
  10. and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
  11. Thanks Guys :)
  12. Thanks to Paulus for miscellaneous tidy ups, some largish chunks
  13. of much nicer and/or completely new code and (perhaps most uniquely)
  14. having the patience to sit down and explain why and where he'd changed
  15. stuff.
  16. Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
  17. staff in their work on open source projects.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/errno.h>
  22. #include <linux/slab.h>
  23. #include <linux/tty.h>
  24. #include <linux/tty_driver.h>
  25. #include <linux/tty_flip.h>
  26. #include <linux/module.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/usb.h>
  30. #include <linux/usb/serial.h>
  31. #include <linux/usb/ezusb.h>
  32. #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
  33. #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
  34. /* Function prototypes for Keyspan serial converter */
  35. static int keyspan_open(struct tty_struct *tty, struct usb_serial_port *port);
  36. static void keyspan_close(struct usb_serial_port *port);
  37. static void keyspan_dtr_rts(struct usb_serial_port *port, int on);
  38. static int keyspan_startup(struct usb_serial *serial);
  39. static void keyspan_disconnect(struct usb_serial *serial);
  40. static void keyspan_release(struct usb_serial *serial);
  41. static int keyspan_port_probe(struct usb_serial_port *port);
  42. static int keyspan_port_remove(struct usb_serial_port *port);
  43. static int keyspan_write_room(struct tty_struct *tty);
  44. static int keyspan_write(struct tty_struct *tty, struct usb_serial_port *port,
  45. const unsigned char *buf, int count);
  46. static void keyspan_send_setup(struct usb_serial_port *port, int reset_port);
  47. static void keyspan_set_termios(struct tty_struct *tty,
  48. struct usb_serial_port *port,
  49. struct ktermios *old);
  50. static void keyspan_break_ctl(struct tty_struct *tty, int break_state);
  51. static int keyspan_tiocmget(struct tty_struct *tty);
  52. static int keyspan_tiocmset(struct tty_struct *tty, unsigned int set,
  53. unsigned int clear);
  54. static int keyspan_fake_startup(struct usb_serial *serial);
  55. static int keyspan_usa19_calc_baud(struct usb_serial_port *port,
  56. u32 baud_rate, u32 baudclk,
  57. u8 *rate_hi, u8 *rate_low,
  58. u8 *prescaler, int portnum);
  59. static int keyspan_usa19w_calc_baud(struct usb_serial_port *port,
  60. u32 baud_rate, u32 baudclk,
  61. u8 *rate_hi, u8 *rate_low,
  62. u8 *prescaler, int portnum);
  63. static int keyspan_usa28_calc_baud(struct usb_serial_port *port,
  64. u32 baud_rate, u32 baudclk,
  65. u8 *rate_hi, u8 *rate_low,
  66. u8 *prescaler, int portnum);
  67. static int keyspan_usa19hs_calc_baud(struct usb_serial_port *port,
  68. u32 baud_rate, u32 baudclk,
  69. u8 *rate_hi, u8 *rate_low,
  70. u8 *prescaler, int portnum);
  71. static int keyspan_usa28_send_setup(struct usb_serial *serial,
  72. struct usb_serial_port *port,
  73. int reset_port);
  74. static int keyspan_usa26_send_setup(struct usb_serial *serial,
  75. struct usb_serial_port *port,
  76. int reset_port);
  77. static int keyspan_usa49_send_setup(struct usb_serial *serial,
  78. struct usb_serial_port *port,
  79. int reset_port);
  80. static int keyspan_usa90_send_setup(struct usb_serial *serial,
  81. struct usb_serial_port *port,
  82. int reset_port);
  83. static int keyspan_usa67_send_setup(struct usb_serial *serial,
  84. struct usb_serial_port *port,
  85. int reset_port);
  86. /* Values used for baud rate calculation - device specific */
  87. #define KEYSPAN_INVALID_BAUD_RATE (-1)
  88. #define KEYSPAN_BAUD_RATE_OK (0)
  89. #define KEYSPAN_USA18X_BAUDCLK (12000000L) /* a guess */
  90. #define KEYSPAN_USA19_BAUDCLK (12000000L)
  91. #define KEYSPAN_USA19W_BAUDCLK (24000000L)
  92. #define KEYSPAN_USA19HS_BAUDCLK (14769231L)
  93. #define KEYSPAN_USA28_BAUDCLK (1843200L)
  94. #define KEYSPAN_USA28X_BAUDCLK (12000000L)
  95. #define KEYSPAN_USA49W_BAUDCLK (48000000L)
  96. /* Some constants used to characterise each device. */
  97. #define KEYSPAN_MAX_NUM_PORTS (4)
  98. #define KEYSPAN_MAX_FLIPS (2)
  99. /*
  100. * Device info for the Keyspan serial converter, used by the overall
  101. * usb-serial probe function.
  102. */
  103. #define KEYSPAN_VENDOR_ID (0x06cd)
  104. /* Product IDs for the products supported, pre-renumeration */
  105. #define keyspan_usa18x_pre_product_id 0x0105
  106. #define keyspan_usa19_pre_product_id 0x0103
  107. #define keyspan_usa19qi_pre_product_id 0x010b
  108. #define keyspan_mpr_pre_product_id 0x011b
  109. #define keyspan_usa19qw_pre_product_id 0x0118
  110. #define keyspan_usa19w_pre_product_id 0x0106
  111. #define keyspan_usa28_pre_product_id 0x0101
  112. #define keyspan_usa28x_pre_product_id 0x0102
  113. #define keyspan_usa28xa_pre_product_id 0x0114
  114. #define keyspan_usa28xb_pre_product_id 0x0113
  115. #define keyspan_usa49w_pre_product_id 0x0109
  116. #define keyspan_usa49wlc_pre_product_id 0x011a
  117. /*
  118. * Product IDs post-renumeration. Note that the 28x and 28xb have the same
  119. * id's post-renumeration but behave identically so it's not an issue. As
  120. * such, the 28xb is not listed in any of the device tables.
  121. */
  122. #define keyspan_usa18x_product_id 0x0112
  123. #define keyspan_usa19_product_id 0x0107
  124. #define keyspan_usa19qi_product_id 0x010c
  125. #define keyspan_usa19hs_product_id 0x0121
  126. #define keyspan_mpr_product_id 0x011c
  127. #define keyspan_usa19qw_product_id 0x0119
  128. #define keyspan_usa19w_product_id 0x0108
  129. #define keyspan_usa28_product_id 0x010f
  130. #define keyspan_usa28x_product_id 0x0110
  131. #define keyspan_usa28xa_product_id 0x0115
  132. #define keyspan_usa28xb_product_id 0x0110
  133. #define keyspan_usa28xg_product_id 0x0135
  134. #define keyspan_usa49w_product_id 0x010a
  135. #define keyspan_usa49wlc_product_id 0x012a
  136. #define keyspan_usa49wg_product_id 0x0131
  137. struct keyspan_device_details {
  138. /* product ID value */
  139. int product_id;
  140. enum {msg_usa26, msg_usa28, msg_usa49, msg_usa90, msg_usa67} msg_format;
  141. /* Number of physical ports */
  142. int num_ports;
  143. /* 1 if endpoint flipping used on input, 0 if not */
  144. int indat_endp_flip;
  145. /* 1 if endpoint flipping used on output, 0 if not */
  146. int outdat_endp_flip;
  147. /*
  148. * Table mapping input data endpoint IDs to physical port
  149. * number and flip if used
  150. */
  151. int indat_endpoints[KEYSPAN_MAX_NUM_PORTS];
  152. /* Same for output endpoints */
  153. int outdat_endpoints[KEYSPAN_MAX_NUM_PORTS];
  154. /* Input acknowledge endpoints */
  155. int inack_endpoints[KEYSPAN_MAX_NUM_PORTS];
  156. /* Output control endpoints */
  157. int outcont_endpoints[KEYSPAN_MAX_NUM_PORTS];
  158. /* Endpoint used for input status */
  159. int instat_endpoint;
  160. /* Endpoint used for input data 49WG only */
  161. int indat_endpoint;
  162. /* Endpoint used for global control functions */
  163. int glocont_endpoint;
  164. int (*calculate_baud_rate)(struct usb_serial_port *port,
  165. u32 baud_rate, u32 baudclk,
  166. u8 *rate_hi, u8 *rate_low, u8 *prescaler,
  167. int portnum);
  168. u32 baudclk;
  169. };
  170. /*
  171. * Now for each device type we setup the device detail structure with the
  172. * appropriate information (provided in Keyspan's documentation)
  173. */
  174. static const struct keyspan_device_details usa18x_device_details = {
  175. .product_id = keyspan_usa18x_product_id,
  176. .msg_format = msg_usa26,
  177. .num_ports = 1,
  178. .indat_endp_flip = 0,
  179. .outdat_endp_flip = 1,
  180. .indat_endpoints = {0x81},
  181. .outdat_endpoints = {0x01},
  182. .inack_endpoints = {0x85},
  183. .outcont_endpoints = {0x05},
  184. .instat_endpoint = 0x87,
  185. .indat_endpoint = -1,
  186. .glocont_endpoint = 0x07,
  187. .calculate_baud_rate = keyspan_usa19w_calc_baud,
  188. .baudclk = KEYSPAN_USA18X_BAUDCLK,
  189. };
  190. static const struct keyspan_device_details usa19_device_details = {
  191. .product_id = keyspan_usa19_product_id,
  192. .msg_format = msg_usa28,
  193. .num_ports = 1,
  194. .indat_endp_flip = 1,
  195. .outdat_endp_flip = 1,
  196. .indat_endpoints = {0x81},
  197. .outdat_endpoints = {0x01},
  198. .inack_endpoints = {0x83},
  199. .outcont_endpoints = {0x03},
  200. .instat_endpoint = 0x84,
  201. .indat_endpoint = -1,
  202. .glocont_endpoint = -1,
  203. .calculate_baud_rate = keyspan_usa19_calc_baud,
  204. .baudclk = KEYSPAN_USA19_BAUDCLK,
  205. };
  206. static const struct keyspan_device_details usa19qi_device_details = {
  207. .product_id = keyspan_usa19qi_product_id,
  208. .msg_format = msg_usa28,
  209. .num_ports = 1,
  210. .indat_endp_flip = 1,
  211. .outdat_endp_flip = 1,
  212. .indat_endpoints = {0x81},
  213. .outdat_endpoints = {0x01},
  214. .inack_endpoints = {0x83},
  215. .outcont_endpoints = {0x03},
  216. .instat_endpoint = 0x84,
  217. .indat_endpoint = -1,
  218. .glocont_endpoint = -1,
  219. .calculate_baud_rate = keyspan_usa28_calc_baud,
  220. .baudclk = KEYSPAN_USA19_BAUDCLK,
  221. };
  222. static const struct keyspan_device_details mpr_device_details = {
  223. .product_id = keyspan_mpr_product_id,
  224. .msg_format = msg_usa28,
  225. .num_ports = 1,
  226. .indat_endp_flip = 1,
  227. .outdat_endp_flip = 1,
  228. .indat_endpoints = {0x81},
  229. .outdat_endpoints = {0x01},
  230. .inack_endpoints = {0x83},
  231. .outcont_endpoints = {0x03},
  232. .instat_endpoint = 0x84,
  233. .indat_endpoint = -1,
  234. .glocont_endpoint = -1,
  235. .calculate_baud_rate = keyspan_usa28_calc_baud,
  236. .baudclk = KEYSPAN_USA19_BAUDCLK,
  237. };
  238. static const struct keyspan_device_details usa19qw_device_details = {
  239. .product_id = keyspan_usa19qw_product_id,
  240. .msg_format = msg_usa26,
  241. .num_ports = 1,
  242. .indat_endp_flip = 0,
  243. .outdat_endp_flip = 1,
  244. .indat_endpoints = {0x81},
  245. .outdat_endpoints = {0x01},
  246. .inack_endpoints = {0x85},
  247. .outcont_endpoints = {0x05},
  248. .instat_endpoint = 0x87,
  249. .indat_endpoint = -1,
  250. .glocont_endpoint = 0x07,
  251. .calculate_baud_rate = keyspan_usa19w_calc_baud,
  252. .baudclk = KEYSPAN_USA19W_BAUDCLK,
  253. };
  254. static const struct keyspan_device_details usa19w_device_details = {
  255. .product_id = keyspan_usa19w_product_id,
  256. .msg_format = msg_usa26,
  257. .num_ports = 1,
  258. .indat_endp_flip = 0,
  259. .outdat_endp_flip = 1,
  260. .indat_endpoints = {0x81},
  261. .outdat_endpoints = {0x01},
  262. .inack_endpoints = {0x85},
  263. .outcont_endpoints = {0x05},
  264. .instat_endpoint = 0x87,
  265. .indat_endpoint = -1,
  266. .glocont_endpoint = 0x07,
  267. .calculate_baud_rate = keyspan_usa19w_calc_baud,
  268. .baudclk = KEYSPAN_USA19W_BAUDCLK,
  269. };
  270. static const struct keyspan_device_details usa19hs_device_details = {
  271. .product_id = keyspan_usa19hs_product_id,
  272. .msg_format = msg_usa90,
  273. .num_ports = 1,
  274. .indat_endp_flip = 0,
  275. .outdat_endp_flip = 0,
  276. .indat_endpoints = {0x81},
  277. .outdat_endpoints = {0x01},
  278. .inack_endpoints = {-1},
  279. .outcont_endpoints = {0x02},
  280. .instat_endpoint = 0x82,
  281. .indat_endpoint = -1,
  282. .glocont_endpoint = -1,
  283. .calculate_baud_rate = keyspan_usa19hs_calc_baud,
  284. .baudclk = KEYSPAN_USA19HS_BAUDCLK,
  285. };
  286. static const struct keyspan_device_details usa28_device_details = {
  287. .product_id = keyspan_usa28_product_id,
  288. .msg_format = msg_usa28,
  289. .num_ports = 2,
  290. .indat_endp_flip = 1,
  291. .outdat_endp_flip = 1,
  292. .indat_endpoints = {0x81, 0x83},
  293. .outdat_endpoints = {0x01, 0x03},
  294. .inack_endpoints = {0x85, 0x86},
  295. .outcont_endpoints = {0x05, 0x06},
  296. .instat_endpoint = 0x87,
  297. .indat_endpoint = -1,
  298. .glocont_endpoint = 0x07,
  299. .calculate_baud_rate = keyspan_usa28_calc_baud,
  300. .baudclk = KEYSPAN_USA28_BAUDCLK,
  301. };
  302. static const struct keyspan_device_details usa28x_device_details = {
  303. .product_id = keyspan_usa28x_product_id,
  304. .msg_format = msg_usa26,
  305. .num_ports = 2,
  306. .indat_endp_flip = 0,
  307. .outdat_endp_flip = 1,
  308. .indat_endpoints = {0x81, 0x83},
  309. .outdat_endpoints = {0x01, 0x03},
  310. .inack_endpoints = {0x85, 0x86},
  311. .outcont_endpoints = {0x05, 0x06},
  312. .instat_endpoint = 0x87,
  313. .indat_endpoint = -1,
  314. .glocont_endpoint = 0x07,
  315. .calculate_baud_rate = keyspan_usa19w_calc_baud,
  316. .baudclk = KEYSPAN_USA28X_BAUDCLK,
  317. };
  318. static const struct keyspan_device_details usa28xa_device_details = {
  319. .product_id = keyspan_usa28xa_product_id,
  320. .msg_format = msg_usa26,
  321. .num_ports = 2,
  322. .indat_endp_flip = 0,
  323. .outdat_endp_flip = 1,
  324. .indat_endpoints = {0x81, 0x83},
  325. .outdat_endpoints = {0x01, 0x03},
  326. .inack_endpoints = {0x85, 0x86},
  327. .outcont_endpoints = {0x05, 0x06},
  328. .instat_endpoint = 0x87,
  329. .indat_endpoint = -1,
  330. .glocont_endpoint = 0x07,
  331. .calculate_baud_rate = keyspan_usa19w_calc_baud,
  332. .baudclk = KEYSPAN_USA28X_BAUDCLK,
  333. };
  334. static const struct keyspan_device_details usa28xg_device_details = {
  335. .product_id = keyspan_usa28xg_product_id,
  336. .msg_format = msg_usa67,
  337. .num_ports = 2,
  338. .indat_endp_flip = 0,
  339. .outdat_endp_flip = 0,
  340. .indat_endpoints = {0x84, 0x88},
  341. .outdat_endpoints = {0x02, 0x06},
  342. .inack_endpoints = {-1, -1},
  343. .outcont_endpoints = {-1, -1},
  344. .instat_endpoint = 0x81,
  345. .indat_endpoint = -1,
  346. .glocont_endpoint = 0x01,
  347. .calculate_baud_rate = keyspan_usa19w_calc_baud,
  348. .baudclk = KEYSPAN_USA28X_BAUDCLK,
  349. };
  350. /*
  351. * We don't need a separate entry for the usa28xb as it appears as a 28x
  352. * anyway.
  353. */
  354. static const struct keyspan_device_details usa49w_device_details = {
  355. .product_id = keyspan_usa49w_product_id,
  356. .msg_format = msg_usa49,
  357. .num_ports = 4,
  358. .indat_endp_flip = 0,
  359. .outdat_endp_flip = 0,
  360. .indat_endpoints = {0x81, 0x82, 0x83, 0x84},
  361. .outdat_endpoints = {0x01, 0x02, 0x03, 0x04},
  362. .inack_endpoints = {-1, -1, -1, -1},
  363. .outcont_endpoints = {-1, -1, -1, -1},
  364. .instat_endpoint = 0x87,
  365. .indat_endpoint = -1,
  366. .glocont_endpoint = 0x07,
  367. .calculate_baud_rate = keyspan_usa19w_calc_baud,
  368. .baudclk = KEYSPAN_USA49W_BAUDCLK,
  369. };
  370. static const struct keyspan_device_details usa49wlc_device_details = {
  371. .product_id = keyspan_usa49wlc_product_id,
  372. .msg_format = msg_usa49,
  373. .num_ports = 4,
  374. .indat_endp_flip = 0,
  375. .outdat_endp_flip = 0,
  376. .indat_endpoints = {0x81, 0x82, 0x83, 0x84},
  377. .outdat_endpoints = {0x01, 0x02, 0x03, 0x04},
  378. .inack_endpoints = {-1, -1, -1, -1},
  379. .outcont_endpoints = {-1, -1, -1, -1},
  380. .instat_endpoint = 0x87,
  381. .indat_endpoint = -1,
  382. .glocont_endpoint = 0x07,
  383. .calculate_baud_rate = keyspan_usa19w_calc_baud,
  384. .baudclk = KEYSPAN_USA19W_BAUDCLK,
  385. };
  386. static const struct keyspan_device_details usa49wg_device_details = {
  387. .product_id = keyspan_usa49wg_product_id,
  388. .msg_format = msg_usa49,
  389. .num_ports = 4,
  390. .indat_endp_flip = 0,
  391. .outdat_endp_flip = 0,
  392. .indat_endpoints = {-1, -1, -1, -1}, /* single 'global' data in EP */
  393. .outdat_endpoints = {0x01, 0x02, 0x04, 0x06},
  394. .inack_endpoints = {-1, -1, -1, -1},
  395. .outcont_endpoints = {-1, -1, -1, -1},
  396. .instat_endpoint = 0x81,
  397. .indat_endpoint = 0x88,
  398. .glocont_endpoint = 0x00, /* uses control EP */
  399. .calculate_baud_rate = keyspan_usa19w_calc_baud,
  400. .baudclk = KEYSPAN_USA19W_BAUDCLK,
  401. };
  402. static const struct keyspan_device_details *keyspan_devices[] = {
  403. &usa18x_device_details,
  404. &usa19_device_details,
  405. &usa19qi_device_details,
  406. &mpr_device_details,
  407. &usa19qw_device_details,
  408. &usa19w_device_details,
  409. &usa19hs_device_details,
  410. &usa28_device_details,
  411. &usa28x_device_details,
  412. &usa28xa_device_details,
  413. &usa28xg_device_details,
  414. /* 28xb not required as it renumerates as a 28x */
  415. &usa49w_device_details,
  416. &usa49wlc_device_details,
  417. &usa49wg_device_details,
  418. NULL,
  419. };
  420. static const struct usb_device_id keyspan_ids_combined[] = {
  421. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa18x_pre_product_id) },
  422. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19_pre_product_id) },
  423. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19w_pre_product_id) },
  424. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qi_pre_product_id) },
  425. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qw_pre_product_id) },
  426. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_mpr_pre_product_id) },
  427. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_pre_product_id) },
  428. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_pre_product_id) },
  429. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_pre_product_id) },
  430. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xb_pre_product_id) },
  431. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49w_pre_product_id) },
  432. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wlc_pre_product_id) },
  433. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa18x_product_id) },
  434. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19_product_id) },
  435. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19w_product_id) },
  436. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qi_product_id) },
  437. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qw_product_id) },
  438. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19hs_product_id) },
  439. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_mpr_product_id) },
  440. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_product_id) },
  441. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_product_id) },
  442. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_product_id) },
  443. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xg_product_id) },
  444. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49w_product_id)},
  445. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wlc_product_id)},
  446. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wg_product_id)},
  447. { } /* Terminating entry */
  448. };
  449. MODULE_DEVICE_TABLE(usb, keyspan_ids_combined);
  450. /* usb_device_id table for the pre-firmware download keyspan devices */
  451. static const struct usb_device_id keyspan_pre_ids[] = {
  452. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa18x_pre_product_id) },
  453. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19_pre_product_id) },
  454. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qi_pre_product_id) },
  455. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qw_pre_product_id) },
  456. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19w_pre_product_id) },
  457. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_mpr_pre_product_id) },
  458. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_pre_product_id) },
  459. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_pre_product_id) },
  460. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_pre_product_id) },
  461. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xb_pre_product_id) },
  462. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49w_pre_product_id) },
  463. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wlc_pre_product_id) },
  464. { } /* Terminating entry */
  465. };
  466. static const struct usb_device_id keyspan_1port_ids[] = {
  467. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa18x_product_id) },
  468. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19_product_id) },
  469. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qi_product_id) },
  470. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19qw_product_id) },
  471. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19w_product_id) },
  472. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa19hs_product_id) },
  473. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_mpr_product_id) },
  474. { } /* Terminating entry */
  475. };
  476. static const struct usb_device_id keyspan_2port_ids[] = {
  477. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28_product_id) },
  478. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28x_product_id) },
  479. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xa_product_id) },
  480. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa28xg_product_id) },
  481. { } /* Terminating entry */
  482. };
  483. static const struct usb_device_id keyspan_4port_ids[] = {
  484. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49w_product_id) },
  485. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wlc_product_id)},
  486. { USB_DEVICE(KEYSPAN_VENDOR_ID, keyspan_usa49wg_product_id)},
  487. { } /* Terminating entry */
  488. };
  489. #define INSTAT_BUFLEN 32
  490. #define GLOCONT_BUFLEN 64
  491. #define INDAT49W_BUFLEN 512
  492. #define IN_BUFLEN 64
  493. #define OUT_BUFLEN 64
  494. #define INACK_BUFLEN 1
  495. #define OUTCONT_BUFLEN 64
  496. /* Per device and per port private data */
  497. struct keyspan_serial_private {
  498. const struct keyspan_device_details *device_details;
  499. struct urb *instat_urb;
  500. char *instat_buf;
  501. /* added to support 49wg, where data from all 4 ports comes in
  502. on 1 EP and high-speed supported */
  503. struct urb *indat_urb;
  504. char *indat_buf;
  505. /* XXX this one probably will need a lock */
  506. struct urb *glocont_urb;
  507. char *glocont_buf;
  508. char *ctrl_buf; /* for EP0 control message */
  509. };
  510. struct keyspan_port_private {
  511. /* Keep track of which input & output endpoints to use */
  512. int in_flip;
  513. int out_flip;
  514. /* Keep duplicate of device details in each port
  515. structure as well - simplifies some of the
  516. callback functions etc. */
  517. const struct keyspan_device_details *device_details;
  518. /* Input endpoints and buffer for this port */
  519. struct urb *in_urbs[2];
  520. char *in_buffer[2];
  521. /* Output endpoints and buffer for this port */
  522. struct urb *out_urbs[2];
  523. char *out_buffer[2];
  524. /* Input ack endpoint */
  525. struct urb *inack_urb;
  526. char *inack_buffer;
  527. /* Output control endpoint */
  528. struct urb *outcont_urb;
  529. char *outcont_buffer;
  530. /* Settings for the port */
  531. int baud;
  532. int old_baud;
  533. unsigned int cflag;
  534. unsigned int old_cflag;
  535. enum {flow_none, flow_cts, flow_xon} flow_control;
  536. int rts_state; /* Handshaking pins (outputs) */
  537. int dtr_state;
  538. int cts_state; /* Handshaking pins (inputs) */
  539. int dsr_state;
  540. int dcd_state;
  541. int ri_state;
  542. int break_on;
  543. unsigned long tx_start_time[2];
  544. int resend_cont; /* need to resend control packet */
  545. };
  546. /* Include Keyspan message headers. All current Keyspan Adapters
  547. make use of one of five message formats which are referred
  548. to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
  549. within this driver. */
  550. #include "keyspan_usa26msg.h"
  551. #include "keyspan_usa28msg.h"
  552. #include "keyspan_usa49msg.h"
  553. #include "keyspan_usa90msg.h"
  554. #include "keyspan_usa67msg.h"
  555. static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
  556. {
  557. struct usb_serial_port *port = tty->driver_data;
  558. struct keyspan_port_private *p_priv;
  559. p_priv = usb_get_serial_port_data(port);
  560. if (break_state == -1)
  561. p_priv->break_on = 1;
  562. else
  563. p_priv->break_on = 0;
  564. keyspan_send_setup(port, 0);
  565. }
  566. static void keyspan_set_termios(struct tty_struct *tty,
  567. struct usb_serial_port *port, struct ktermios *old_termios)
  568. {
  569. int baud_rate, device_port;
  570. struct keyspan_port_private *p_priv;
  571. const struct keyspan_device_details *d_details;
  572. unsigned int cflag;
  573. p_priv = usb_get_serial_port_data(port);
  574. d_details = p_priv->device_details;
  575. cflag = tty->termios.c_cflag;
  576. device_port = port->port_number;
  577. /* Baud rate calculation takes baud rate as an integer
  578. so other rates can be generated if desired. */
  579. baud_rate = tty_get_baud_rate(tty);
  580. /* If no match or invalid, don't change */
  581. if (d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
  582. NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
  583. /* FIXME - more to do here to ensure rate changes cleanly */
  584. /* FIXME - calculate exact rate from divisor ? */
  585. p_priv->baud = baud_rate;
  586. } else
  587. baud_rate = tty_termios_baud_rate(old_termios);
  588. tty_encode_baud_rate(tty, baud_rate, baud_rate);
  589. /* set CTS/RTS handshake etc. */
  590. p_priv->cflag = cflag;
  591. p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
  592. /* Mark/Space not supported */
  593. tty->termios.c_cflag &= ~CMSPAR;
  594. keyspan_send_setup(port, 0);
  595. }
  596. static int keyspan_tiocmget(struct tty_struct *tty)
  597. {
  598. struct usb_serial_port *port = tty->driver_data;
  599. struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
  600. unsigned int value;
  601. value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
  602. ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
  603. ((p_priv->cts_state) ? TIOCM_CTS : 0) |
  604. ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
  605. ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
  606. ((p_priv->ri_state) ? TIOCM_RNG : 0);
  607. return value;
  608. }
  609. static int keyspan_tiocmset(struct tty_struct *tty,
  610. unsigned int set, unsigned int clear)
  611. {
  612. struct usb_serial_port *port = tty->driver_data;
  613. struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
  614. if (set & TIOCM_RTS)
  615. p_priv->rts_state = 1;
  616. if (set & TIOCM_DTR)
  617. p_priv->dtr_state = 1;
  618. if (clear & TIOCM_RTS)
  619. p_priv->rts_state = 0;
  620. if (clear & TIOCM_DTR)
  621. p_priv->dtr_state = 0;
  622. keyspan_send_setup(port, 0);
  623. return 0;
  624. }
  625. /* Write function is similar for the four protocols used
  626. with only a minor change for usa90 (usa19hs) required */
  627. static int keyspan_write(struct tty_struct *tty,
  628. struct usb_serial_port *port, const unsigned char *buf, int count)
  629. {
  630. struct keyspan_port_private *p_priv;
  631. const struct keyspan_device_details *d_details;
  632. int flip;
  633. int left, todo;
  634. struct urb *this_urb;
  635. int err, maxDataLen, dataOffset;
  636. p_priv = usb_get_serial_port_data(port);
  637. d_details = p_priv->device_details;
  638. if (d_details->msg_format == msg_usa90) {
  639. maxDataLen = 64;
  640. dataOffset = 0;
  641. } else {
  642. maxDataLen = 63;
  643. dataOffset = 1;
  644. }
  645. dev_dbg(&port->dev, "%s - %d chars, flip=%d\n", __func__, count,
  646. p_priv->out_flip);
  647. for (left = count; left > 0; left -= todo) {
  648. todo = left;
  649. if (todo > maxDataLen)
  650. todo = maxDataLen;
  651. flip = p_priv->out_flip;
  652. /* Check we have a valid urb/endpoint before we use it... */
  653. this_urb = p_priv->out_urbs[flip];
  654. if (this_urb == NULL) {
  655. /* no bulk out, so return 0 bytes written */
  656. dev_dbg(&port->dev, "%s - no output urb :(\n", __func__);
  657. return count;
  658. }
  659. dev_dbg(&port->dev, "%s - endpoint %x flip %d\n",
  660. __func__, usb_pipeendpoint(this_urb->pipe), flip);
  661. if (this_urb->status == -EINPROGRESS) {
  662. if (time_before(jiffies,
  663. p_priv->tx_start_time[flip] + 10 * HZ))
  664. break;
  665. usb_unlink_urb(this_urb);
  666. break;
  667. }
  668. /* First byte in buffer is "last flag" (except for usa19hx)
  669. - unused so for now so set to zero */
  670. ((char *)this_urb->transfer_buffer)[0] = 0;
  671. memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
  672. buf += todo;
  673. /* send the data out the bulk port */
  674. this_urb->transfer_buffer_length = todo + dataOffset;
  675. err = usb_submit_urb(this_urb, GFP_ATOMIC);
  676. if (err != 0)
  677. dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed (%d)\n", err);
  678. p_priv->tx_start_time[flip] = jiffies;
  679. /* Flip for next time if usa26 or usa28 interface
  680. (not used on usa49) */
  681. p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
  682. }
  683. return count - left;
  684. }
  685. static void usa26_indat_callback(struct urb *urb)
  686. {
  687. int i, err;
  688. int endpoint;
  689. struct usb_serial_port *port;
  690. unsigned char *data = urb->transfer_buffer;
  691. int status = urb->status;
  692. endpoint = usb_pipeendpoint(urb->pipe);
  693. if (status) {
  694. dev_dbg(&urb->dev->dev, "%s - nonzero status %d on endpoint %x\n",
  695. __func__, status, endpoint);
  696. return;
  697. }
  698. port = urb->context;
  699. if (urb->actual_length) {
  700. /* 0x80 bit is error flag */
  701. if ((data[0] & 0x80) == 0) {
  702. /* no errors on individual bytes, only
  703. possible overrun err */
  704. if (data[0] & RXERROR_OVERRUN) {
  705. tty_insert_flip_char(&port->port, 0,
  706. TTY_OVERRUN);
  707. }
  708. for (i = 1; i < urb->actual_length ; ++i)
  709. tty_insert_flip_char(&port->port, data[i],
  710. TTY_NORMAL);
  711. } else {
  712. /* some bytes had errors, every byte has status */
  713. dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
  714. for (i = 0; i + 1 < urb->actual_length; i += 2) {
  715. int stat = data[i];
  716. int flag = TTY_NORMAL;
  717. if (stat & RXERROR_OVERRUN) {
  718. tty_insert_flip_char(&port->port, 0,
  719. TTY_OVERRUN);
  720. }
  721. /* XXX should handle break (0x10) */
  722. if (stat & RXERROR_PARITY)
  723. flag = TTY_PARITY;
  724. else if (stat & RXERROR_FRAMING)
  725. flag = TTY_FRAME;
  726. tty_insert_flip_char(&port->port, data[i+1],
  727. flag);
  728. }
  729. }
  730. tty_flip_buffer_push(&port->port);
  731. }
  732. /* Resubmit urb so we continue receiving */
  733. err = usb_submit_urb(urb, GFP_ATOMIC);
  734. if (err != 0)
  735. dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
  736. }
  737. /* Outdat handling is common for all devices */
  738. static void usa2x_outdat_callback(struct urb *urb)
  739. {
  740. struct usb_serial_port *port;
  741. struct keyspan_port_private *p_priv;
  742. port = urb->context;
  743. p_priv = usb_get_serial_port_data(port);
  744. dev_dbg(&port->dev, "%s - urb %d\n", __func__, urb == p_priv->out_urbs[1]);
  745. usb_serial_port_softint(port);
  746. }
  747. static void usa26_inack_callback(struct urb *urb)
  748. {
  749. }
  750. static void usa26_outcont_callback(struct urb *urb)
  751. {
  752. struct usb_serial_port *port;
  753. struct keyspan_port_private *p_priv;
  754. port = urb->context;
  755. p_priv = usb_get_serial_port_data(port);
  756. if (p_priv->resend_cont) {
  757. dev_dbg(&port->dev, "%s - sending setup\n", __func__);
  758. keyspan_usa26_send_setup(port->serial, port,
  759. p_priv->resend_cont - 1);
  760. }
  761. }
  762. static void usa26_instat_callback(struct urb *urb)
  763. {
  764. unsigned char *data = urb->transfer_buffer;
  765. struct keyspan_usa26_portStatusMessage *msg;
  766. struct usb_serial *serial;
  767. struct usb_serial_port *port;
  768. struct keyspan_port_private *p_priv;
  769. int old_dcd_state, err;
  770. int status = urb->status;
  771. serial = urb->context;
  772. if (status) {
  773. dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
  774. __func__, status);
  775. return;
  776. }
  777. if (urb->actual_length != 9) {
  778. dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
  779. goto exit;
  780. }
  781. msg = (struct keyspan_usa26_portStatusMessage *)data;
  782. /* Check port number from message and retrieve private data */
  783. if (msg->port >= serial->num_ports) {
  784. dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
  785. goto exit;
  786. }
  787. port = serial->port[msg->port];
  788. p_priv = usb_get_serial_port_data(port);
  789. if (!p_priv)
  790. goto resubmit;
  791. /* Update handshaking pin state information */
  792. old_dcd_state = p_priv->dcd_state;
  793. p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
  794. p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
  795. p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
  796. p_priv->ri_state = ((msg->ri) ? 1 : 0);
  797. if (old_dcd_state != p_priv->dcd_state)
  798. tty_port_tty_hangup(&port->port, true);
  799. resubmit:
  800. /* Resubmit urb so we continue receiving */
  801. err = usb_submit_urb(urb, GFP_ATOMIC);
  802. if (err != 0)
  803. dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
  804. exit: ;
  805. }
  806. static void usa26_glocont_callback(struct urb *urb)
  807. {
  808. }
  809. static void usa28_indat_callback(struct urb *urb)
  810. {
  811. int err;
  812. struct usb_serial_port *port;
  813. unsigned char *data;
  814. struct keyspan_port_private *p_priv;
  815. int status = urb->status;
  816. port = urb->context;
  817. p_priv = usb_get_serial_port_data(port);
  818. data = urb->transfer_buffer;
  819. if (urb != p_priv->in_urbs[p_priv->in_flip])
  820. return;
  821. do {
  822. if (status) {
  823. dev_dbg(&urb->dev->dev, "%s - nonzero status %d on endpoint %x\n",
  824. __func__, status, usb_pipeendpoint(urb->pipe));
  825. return;
  826. }
  827. port = urb->context;
  828. p_priv = usb_get_serial_port_data(port);
  829. data = urb->transfer_buffer;
  830. if (urb->actual_length) {
  831. tty_insert_flip_string(&port->port, data,
  832. urb->actual_length);
  833. tty_flip_buffer_push(&port->port);
  834. }
  835. /* Resubmit urb so we continue receiving */
  836. err = usb_submit_urb(urb, GFP_ATOMIC);
  837. if (err != 0)
  838. dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n",
  839. __func__, err);
  840. p_priv->in_flip ^= 1;
  841. urb = p_priv->in_urbs[p_priv->in_flip];
  842. } while (urb->status != -EINPROGRESS);
  843. }
  844. static void usa28_inack_callback(struct urb *urb)
  845. {
  846. }
  847. static void usa28_outcont_callback(struct urb *urb)
  848. {
  849. struct usb_serial_port *port;
  850. struct keyspan_port_private *p_priv;
  851. port = urb->context;
  852. p_priv = usb_get_serial_port_data(port);
  853. if (p_priv->resend_cont) {
  854. dev_dbg(&port->dev, "%s - sending setup\n", __func__);
  855. keyspan_usa28_send_setup(port->serial, port,
  856. p_priv->resend_cont - 1);
  857. }
  858. }
  859. static void usa28_instat_callback(struct urb *urb)
  860. {
  861. int err;
  862. unsigned char *data = urb->transfer_buffer;
  863. struct keyspan_usa28_portStatusMessage *msg;
  864. struct usb_serial *serial;
  865. struct usb_serial_port *port;
  866. struct keyspan_port_private *p_priv;
  867. int old_dcd_state;
  868. int status = urb->status;
  869. serial = urb->context;
  870. if (status) {
  871. dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
  872. __func__, status);
  873. return;
  874. }
  875. if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
  876. dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
  877. goto exit;
  878. }
  879. msg = (struct keyspan_usa28_portStatusMessage *)data;
  880. /* Check port number from message and retrieve private data */
  881. if (msg->port >= serial->num_ports) {
  882. dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
  883. goto exit;
  884. }
  885. port = serial->port[msg->port];
  886. p_priv = usb_get_serial_port_data(port);
  887. if (!p_priv)
  888. goto resubmit;
  889. /* Update handshaking pin state information */
  890. old_dcd_state = p_priv->dcd_state;
  891. p_priv->cts_state = ((msg->cts) ? 1 : 0);
  892. p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
  893. p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
  894. p_priv->ri_state = ((msg->ri) ? 1 : 0);
  895. if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
  896. tty_port_tty_hangup(&port->port, true);
  897. resubmit:
  898. /* Resubmit urb so we continue receiving */
  899. err = usb_submit_urb(urb, GFP_ATOMIC);
  900. if (err != 0)
  901. dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
  902. exit: ;
  903. }
  904. static void usa28_glocont_callback(struct urb *urb)
  905. {
  906. }
  907. static void usa49_glocont_callback(struct urb *urb)
  908. {
  909. struct usb_serial *serial;
  910. struct usb_serial_port *port;
  911. struct keyspan_port_private *p_priv;
  912. int i;
  913. serial = urb->context;
  914. for (i = 0; i < serial->num_ports; ++i) {
  915. port = serial->port[i];
  916. p_priv = usb_get_serial_port_data(port);
  917. if (!p_priv)
  918. continue;
  919. if (p_priv->resend_cont) {
  920. dev_dbg(&port->dev, "%s - sending setup\n", __func__);
  921. keyspan_usa49_send_setup(serial, port,
  922. p_priv->resend_cont - 1);
  923. break;
  924. }
  925. }
  926. }
  927. /* This is actually called glostat in the Keyspan
  928. doco */
  929. static void usa49_instat_callback(struct urb *urb)
  930. {
  931. int err;
  932. unsigned char *data = urb->transfer_buffer;
  933. struct keyspan_usa49_portStatusMessage *msg;
  934. struct usb_serial *serial;
  935. struct usb_serial_port *port;
  936. struct keyspan_port_private *p_priv;
  937. int old_dcd_state;
  938. int status = urb->status;
  939. serial = urb->context;
  940. if (status) {
  941. dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
  942. __func__, status);
  943. return;
  944. }
  945. if (urb->actual_length !=
  946. sizeof(struct keyspan_usa49_portStatusMessage)) {
  947. dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
  948. goto exit;
  949. }
  950. msg = (struct keyspan_usa49_portStatusMessage *)data;
  951. /* Check port number from message and retrieve private data */
  952. if (msg->portNumber >= serial->num_ports) {
  953. dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
  954. __func__, msg->portNumber);
  955. goto exit;
  956. }
  957. port = serial->port[msg->portNumber];
  958. p_priv = usb_get_serial_port_data(port);
  959. if (!p_priv)
  960. goto resubmit;
  961. /* Update handshaking pin state information */
  962. old_dcd_state = p_priv->dcd_state;
  963. p_priv->cts_state = ((msg->cts) ? 1 : 0);
  964. p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
  965. p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
  966. p_priv->ri_state = ((msg->ri) ? 1 : 0);
  967. if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
  968. tty_port_tty_hangup(&port->port, true);
  969. resubmit:
  970. /* Resubmit urb so we continue receiving */
  971. err = usb_submit_urb(urb, GFP_ATOMIC);
  972. if (err != 0)
  973. dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
  974. exit: ;
  975. }
  976. static void usa49_inack_callback(struct urb *urb)
  977. {
  978. }
  979. static void usa49_indat_callback(struct urb *urb)
  980. {
  981. int i, err;
  982. int endpoint;
  983. struct usb_serial_port *port;
  984. unsigned char *data = urb->transfer_buffer;
  985. int status = urb->status;
  986. endpoint = usb_pipeendpoint(urb->pipe);
  987. if (status) {
  988. dev_dbg(&urb->dev->dev, "%s - nonzero status %d on endpoint %x\n",
  989. __func__, status, endpoint);
  990. return;
  991. }
  992. port = urb->context;
  993. if (urb->actual_length) {
  994. /* 0x80 bit is error flag */
  995. if ((data[0] & 0x80) == 0) {
  996. /* no error on any byte */
  997. tty_insert_flip_string(&port->port, data + 1,
  998. urb->actual_length - 1);
  999. } else {
  1000. /* some bytes had errors, every byte has status */
  1001. for (i = 0; i + 1 < urb->actual_length; i += 2) {
  1002. int stat = data[i];
  1003. int flag = TTY_NORMAL;
  1004. if (stat & RXERROR_OVERRUN) {
  1005. tty_insert_flip_char(&port->port, 0,
  1006. TTY_OVERRUN);
  1007. }
  1008. /* XXX should handle break (0x10) */
  1009. if (stat & RXERROR_PARITY)
  1010. flag = TTY_PARITY;
  1011. else if (stat & RXERROR_FRAMING)
  1012. flag = TTY_FRAME;
  1013. tty_insert_flip_char(&port->port, data[i+1],
  1014. flag);
  1015. }
  1016. }
  1017. tty_flip_buffer_push(&port->port);
  1018. }
  1019. /* Resubmit urb so we continue receiving */
  1020. err = usb_submit_urb(urb, GFP_ATOMIC);
  1021. if (err != 0)
  1022. dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
  1023. }
  1024. static void usa49wg_indat_callback(struct urb *urb)
  1025. {
  1026. int i, len, x, err;
  1027. struct usb_serial *serial;
  1028. struct usb_serial_port *port;
  1029. unsigned char *data = urb->transfer_buffer;
  1030. int status = urb->status;
  1031. serial = urb->context;
  1032. if (status) {
  1033. dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
  1034. __func__, status);
  1035. return;
  1036. }
  1037. /* inbound data is in the form P#, len, status, data */
  1038. i = 0;
  1039. len = 0;
  1040. while (i < urb->actual_length) {
  1041. /* Check port number from message */
  1042. if (data[i] >= serial->num_ports) {
  1043. dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
  1044. __func__, data[i]);
  1045. return;
  1046. }
  1047. port = serial->port[data[i++]];
  1048. len = data[i++];
  1049. /* 0x80 bit is error flag */
  1050. if ((data[i] & 0x80) == 0) {
  1051. /* no error on any byte */
  1052. i++;
  1053. for (x = 1; x < len && i < urb->actual_length; ++x)
  1054. tty_insert_flip_char(&port->port,
  1055. data[i++], 0);
  1056. } else {
  1057. /*
  1058. * some bytes had errors, every byte has status
  1059. */
  1060. for (x = 0; x + 1 < len &&
  1061. i + 1 < urb->actual_length; x += 2) {
  1062. int stat = data[i];
  1063. int flag = TTY_NORMAL;
  1064. if (stat & RXERROR_OVERRUN) {
  1065. tty_insert_flip_char(&port->port, 0,
  1066. TTY_OVERRUN);
  1067. }
  1068. /* XXX should handle break (0x10) */
  1069. if (stat & RXERROR_PARITY)
  1070. flag = TTY_PARITY;
  1071. else if (stat & RXERROR_FRAMING)
  1072. flag = TTY_FRAME;
  1073. tty_insert_flip_char(&port->port, data[i+1],
  1074. flag);
  1075. i += 2;
  1076. }
  1077. }
  1078. tty_flip_buffer_push(&port->port);
  1079. }
  1080. /* Resubmit urb so we continue receiving */
  1081. err = usb_submit_urb(urb, GFP_ATOMIC);
  1082. if (err != 0)
  1083. dev_dbg(&urb->dev->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
  1084. }
  1085. /* not used, usa-49 doesn't have per-port control endpoints */
  1086. static void usa49_outcont_callback(struct urb *urb)
  1087. {
  1088. }
  1089. static void usa90_indat_callback(struct urb *urb)
  1090. {
  1091. int i, err;
  1092. int endpoint;
  1093. struct usb_serial_port *port;
  1094. struct keyspan_port_private *p_priv;
  1095. unsigned char *data = urb->transfer_buffer;
  1096. int status = urb->status;
  1097. endpoint = usb_pipeendpoint(urb->pipe);
  1098. if (status) {
  1099. dev_dbg(&urb->dev->dev, "%s - nonzero status %d on endpoint %x\n",
  1100. __func__, status, endpoint);
  1101. return;
  1102. }
  1103. port = urb->context;
  1104. p_priv = usb_get_serial_port_data(port);
  1105. if (urb->actual_length) {
  1106. /* if current mode is DMA, looks like usa28 format
  1107. otherwise looks like usa26 data format */
  1108. if (p_priv->baud > 57600)
  1109. tty_insert_flip_string(&port->port, data,
  1110. urb->actual_length);
  1111. else {
  1112. /* 0x80 bit is error flag */
  1113. if ((data[0] & 0x80) == 0) {
  1114. /* no errors on individual bytes, only
  1115. possible overrun err*/
  1116. if (data[0] & RXERROR_OVERRUN) {
  1117. tty_insert_flip_char(&port->port, 0,
  1118. TTY_OVERRUN);
  1119. }
  1120. for (i = 1; i < urb->actual_length ; ++i)
  1121. tty_insert_flip_char(&port->port,
  1122. data[i], TTY_NORMAL);
  1123. } else {
  1124. /* some bytes had errors, every byte has status */
  1125. dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
  1126. for (i = 0; i + 1 < urb->actual_length; i += 2) {
  1127. int stat = data[i];
  1128. int flag = TTY_NORMAL;
  1129. if (stat & RXERROR_OVERRUN) {
  1130. tty_insert_flip_char(
  1131. &port->port, 0,
  1132. TTY_OVERRUN);
  1133. }
  1134. /* XXX should handle break (0x10) */
  1135. if (stat & RXERROR_PARITY)
  1136. flag = TTY_PARITY;
  1137. else if (stat & RXERROR_FRAMING)
  1138. flag = TTY_FRAME;
  1139. tty_insert_flip_char(&port->port,
  1140. data[i+1], flag);
  1141. }
  1142. }
  1143. }
  1144. tty_flip_buffer_push(&port->port);
  1145. }
  1146. /* Resubmit urb so we continue receiving */
  1147. err = usb_submit_urb(urb, GFP_ATOMIC);
  1148. if (err != 0)
  1149. dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
  1150. }
  1151. static void usa90_instat_callback(struct urb *urb)
  1152. {
  1153. unsigned char *data = urb->transfer_buffer;
  1154. struct keyspan_usa90_portStatusMessage *msg;
  1155. struct usb_serial *serial;
  1156. struct usb_serial_port *port;
  1157. struct keyspan_port_private *p_priv;
  1158. int old_dcd_state, err;
  1159. int status = urb->status;
  1160. serial = urb->context;
  1161. if (status) {
  1162. dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
  1163. __func__, status);
  1164. return;
  1165. }
  1166. if (urb->actual_length < 14) {
  1167. dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
  1168. goto exit;
  1169. }
  1170. msg = (struct keyspan_usa90_portStatusMessage *)data;
  1171. /* Now do something useful with the data */
  1172. port = serial->port[0];
  1173. p_priv = usb_get_serial_port_data(port);
  1174. if (!p_priv)
  1175. goto resubmit;
  1176. /* Update handshaking pin state information */
  1177. old_dcd_state = p_priv->dcd_state;
  1178. p_priv->cts_state = ((msg->cts) ? 1 : 0);
  1179. p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
  1180. p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
  1181. p_priv->ri_state = ((msg->ri) ? 1 : 0);
  1182. if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
  1183. tty_port_tty_hangup(&port->port, true);
  1184. resubmit:
  1185. /* Resubmit urb so we continue receiving */
  1186. err = usb_submit_urb(urb, GFP_ATOMIC);
  1187. if (err != 0)
  1188. dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
  1189. exit:
  1190. ;
  1191. }
  1192. static void usa90_outcont_callback(struct urb *urb)
  1193. {
  1194. struct usb_serial_port *port;
  1195. struct keyspan_port_private *p_priv;
  1196. port = urb->context;
  1197. p_priv = usb_get_serial_port_data(port);
  1198. if (p_priv->resend_cont) {
  1199. dev_dbg(&urb->dev->dev, "%s - sending setup\n", __func__);
  1200. keyspan_usa90_send_setup(port->serial, port,
  1201. p_priv->resend_cont - 1);
  1202. }
  1203. }
  1204. /* Status messages from the 28xg */
  1205. static void usa67_instat_callback(struct urb *urb)
  1206. {
  1207. int err;
  1208. unsigned char *data = urb->transfer_buffer;
  1209. struct keyspan_usa67_portStatusMessage *msg;
  1210. struct usb_serial *serial;
  1211. struct usb_serial_port *port;
  1212. struct keyspan_port_private *p_priv;
  1213. int old_dcd_state;
  1214. int status = urb->status;
  1215. serial = urb->context;
  1216. if (status) {
  1217. dev_dbg(&urb->dev->dev, "%s - nonzero status: %d\n",
  1218. __func__, status);
  1219. return;
  1220. }
  1221. if (urb->actual_length !=
  1222. sizeof(struct keyspan_usa67_portStatusMessage)) {
  1223. dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
  1224. return;
  1225. }
  1226. /* Now do something useful with the data */
  1227. msg = (struct keyspan_usa67_portStatusMessage *)data;
  1228. /* Check port number from message and retrieve private data */
  1229. if (msg->port >= serial->num_ports) {
  1230. dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
  1231. return;
  1232. }
  1233. port = serial->port[msg->port];
  1234. p_priv = usb_get_serial_port_data(port);
  1235. if (!p_priv)
  1236. goto resubmit;
  1237. /* Update handshaking pin state information */
  1238. old_dcd_state = p_priv->dcd_state;
  1239. p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
  1240. p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
  1241. if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
  1242. tty_port_tty_hangup(&port->port, true);
  1243. resubmit:
  1244. /* Resubmit urb so we continue receiving */
  1245. err = usb_submit_urb(urb, GFP_ATOMIC);
  1246. if (err != 0)
  1247. dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
  1248. }
  1249. static void usa67_glocont_callback(struct urb *urb)
  1250. {
  1251. struct usb_serial *serial;
  1252. struct usb_serial_port *port;
  1253. struct keyspan_port_private *p_priv;
  1254. int i;
  1255. serial = urb->context;
  1256. for (i = 0; i < serial->num_ports; ++i) {
  1257. port = serial->port[i];
  1258. p_priv = usb_get_serial_port_data(port);
  1259. if (!p_priv)
  1260. continue;
  1261. if (p_priv->resend_cont) {
  1262. dev_dbg(&port->dev, "%s - sending setup\n", __func__);
  1263. keyspan_usa67_send_setup(serial, port,
  1264. p_priv->resend_cont - 1);
  1265. break;
  1266. }
  1267. }
  1268. }
  1269. static int keyspan_write_room(struct tty_struct *tty)
  1270. {
  1271. struct usb_serial_port *port = tty->driver_data;
  1272. struct keyspan_port_private *p_priv;
  1273. const struct keyspan_device_details *d_details;
  1274. int flip;
  1275. int data_len;
  1276. struct urb *this_urb;
  1277. p_priv = usb_get_serial_port_data(port);
  1278. d_details = p_priv->device_details;
  1279. /* FIXME: locking */
  1280. if (d_details->msg_format == msg_usa90)
  1281. data_len = 64;
  1282. else
  1283. data_len = 63;
  1284. flip = p_priv->out_flip;
  1285. /* Check both endpoints to see if any are available. */
  1286. this_urb = p_priv->out_urbs[flip];
  1287. if (this_urb != NULL) {
  1288. if (this_urb->status != -EINPROGRESS)
  1289. return data_len;
  1290. flip = (flip + 1) & d_details->outdat_endp_flip;
  1291. this_urb = p_priv->out_urbs[flip];
  1292. if (this_urb != NULL) {
  1293. if (this_urb->status != -EINPROGRESS)
  1294. return data_len;
  1295. }
  1296. }
  1297. return 0;
  1298. }
  1299. static int keyspan_open(struct tty_struct *tty, struct usb_serial_port *port)
  1300. {
  1301. struct keyspan_port_private *p_priv;
  1302. const struct keyspan_device_details *d_details;
  1303. int i, err;
  1304. int baud_rate, device_port;
  1305. struct urb *urb;
  1306. unsigned int cflag = 0;
  1307. p_priv = usb_get_serial_port_data(port);
  1308. d_details = p_priv->device_details;
  1309. /* Set some sane defaults */
  1310. p_priv->rts_state = 1;
  1311. p_priv->dtr_state = 1;
  1312. p_priv->baud = 9600;
  1313. /* force baud and lcr to be set on open */
  1314. p_priv->old_baud = 0;
  1315. p_priv->old_cflag = 0;
  1316. p_priv->out_flip = 0;
  1317. p_priv->in_flip = 0;
  1318. /* Reset low level data toggle and start reading from endpoints */
  1319. for (i = 0; i < 2; i++) {
  1320. urb = p_priv->in_urbs[i];
  1321. if (urb == NULL)
  1322. continue;
  1323. /* make sure endpoint data toggle is synchronized
  1324. with the device */
  1325. usb_clear_halt(urb->dev, urb->pipe);
  1326. err = usb_submit_urb(urb, GFP_KERNEL);
  1327. if (err != 0)
  1328. dev_dbg(&port->dev, "%s - submit urb %d failed (%d)\n", __func__, i, err);
  1329. }
  1330. /* Reset low level data toggle on out endpoints */
  1331. for (i = 0; i < 2; i++) {
  1332. urb = p_priv->out_urbs[i];
  1333. if (urb == NULL)
  1334. continue;
  1335. /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  1336. usb_pipeout(urb->pipe), 0); */
  1337. }
  1338. /* get the terminal config for the setup message now so we don't
  1339. * need to send 2 of them */
  1340. device_port = port->port_number;
  1341. if (tty) {
  1342. cflag = tty->termios.c_cflag;
  1343. /* Baud rate calculation takes baud rate as an integer
  1344. so other rates can be generated if desired. */
  1345. baud_rate = tty_get_baud_rate(tty);
  1346. /* If no match or invalid, leave as default */
  1347. if (baud_rate >= 0
  1348. && d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
  1349. NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
  1350. p_priv->baud = baud_rate;
  1351. }
  1352. }
  1353. /* set CTS/RTS handshake etc. */
  1354. p_priv->cflag = cflag;
  1355. p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
  1356. keyspan_send_setup(port, 1);
  1357. /* mdelay(100); */
  1358. /* keyspan_set_termios(port, NULL); */
  1359. return 0;
  1360. }
  1361. static void keyspan_dtr_rts(struct usb_serial_port *port, int on)
  1362. {
  1363. struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
  1364. p_priv->rts_state = on;
  1365. p_priv->dtr_state = on;
  1366. keyspan_send_setup(port, 0);
  1367. }
  1368. static void keyspan_close(struct usb_serial_port *port)
  1369. {
  1370. int i;
  1371. struct keyspan_port_private *p_priv;
  1372. p_priv = usb_get_serial_port_data(port);
  1373. p_priv->rts_state = 0;
  1374. p_priv->dtr_state = 0;
  1375. keyspan_send_setup(port, 2);
  1376. /* pilot-xfer seems to work best with this delay */
  1377. mdelay(100);
  1378. p_priv->out_flip = 0;
  1379. p_priv->in_flip = 0;
  1380. usb_kill_urb(p_priv->inack_urb);
  1381. for (i = 0; i < 2; i++) {
  1382. usb_kill_urb(p_priv->in_urbs[i]);
  1383. usb_kill_urb(p_priv->out_urbs[i]);
  1384. }
  1385. }
  1386. /* download the firmware to a pre-renumeration device */
  1387. static int keyspan_fake_startup(struct usb_serial *serial)
  1388. {
  1389. char *fw_name;
  1390. dev_dbg(&serial->dev->dev, "Keyspan startup version %04x product %04x\n",
  1391. le16_to_cpu(serial->dev->descriptor.bcdDevice),
  1392. le16_to_cpu(serial->dev->descriptor.idProduct));
  1393. if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
  1394. != 0x8000) {
  1395. dev_dbg(&serial->dev->dev, "Firmware already loaded. Quitting.\n");
  1396. return 1;
  1397. }
  1398. /* Select firmware image on the basis of idProduct */
  1399. switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
  1400. case keyspan_usa28_pre_product_id:
  1401. fw_name = "keyspan/usa28.fw";
  1402. break;
  1403. case keyspan_usa28x_pre_product_id:
  1404. fw_name = "keyspan/usa28x.fw";
  1405. break;
  1406. case keyspan_usa28xa_pre_product_id:
  1407. fw_name = "keyspan/usa28xa.fw";
  1408. break;
  1409. case keyspan_usa28xb_pre_product_id:
  1410. fw_name = "keyspan/usa28xb.fw";
  1411. break;
  1412. case keyspan_usa19_pre_product_id:
  1413. fw_name = "keyspan/usa19.fw";
  1414. break;
  1415. case keyspan_usa19qi_pre_product_id:
  1416. fw_name = "keyspan/usa19qi.fw";
  1417. break;
  1418. case keyspan_mpr_pre_product_id:
  1419. fw_name = "keyspan/mpr.fw";
  1420. break;
  1421. case keyspan_usa19qw_pre_product_id:
  1422. fw_name = "keyspan/usa19qw.fw";
  1423. break;
  1424. case keyspan_usa18x_pre_product_id:
  1425. fw_name = "keyspan/usa18x.fw";
  1426. break;
  1427. case keyspan_usa19w_pre_product_id:
  1428. fw_name = "keyspan/usa19w.fw";
  1429. break;
  1430. case keyspan_usa49w_pre_product_id:
  1431. fw_name = "keyspan/usa49w.fw";
  1432. break;
  1433. case keyspan_usa49wlc_pre_product_id:
  1434. fw_name = "keyspan/usa49wlc.fw";
  1435. break;
  1436. default:
  1437. dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
  1438. le16_to_cpu(serial->dev->descriptor.idProduct));
  1439. return 1;
  1440. }
  1441. dev_dbg(&serial->dev->dev, "Uploading Keyspan %s firmware.\n", fw_name);
  1442. if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
  1443. dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
  1444. fw_name);
  1445. return -ENOENT;
  1446. }
  1447. /* after downloading firmware Renumeration will occur in a
  1448. moment and the new device will bind to the real driver */
  1449. /* we don't want this device to have a driver assigned to it. */
  1450. return 1;
  1451. }
  1452. /* Helper functions used by keyspan_setup_urbs */
  1453. static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
  1454. int endpoint)
  1455. {
  1456. struct usb_host_interface *iface_desc;
  1457. struct usb_endpoint_descriptor *ep;
  1458. int i;
  1459. iface_desc = serial->interface->cur_altsetting;
  1460. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1461. ep = &iface_desc->endpoint[i].desc;
  1462. if (ep->bEndpointAddress == endpoint)
  1463. return ep;
  1464. }
  1465. dev_warn(&serial->interface->dev, "found no endpoint descriptor for endpoint %x\n",
  1466. endpoint);
  1467. return NULL;
  1468. }
  1469. static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
  1470. int dir, void *ctx, char *buf, int len,
  1471. void (*callback)(struct urb *))
  1472. {
  1473. struct urb *urb;
  1474. struct usb_endpoint_descriptor const *ep_desc;
  1475. char const *ep_type_name;
  1476. if (endpoint == -1)
  1477. return NULL; /* endpoint not needed */
  1478. dev_dbg(&serial->interface->dev, "%s - alloc for endpoint %x\n",
  1479. __func__, endpoint);
  1480. urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
  1481. if (!urb)
  1482. return NULL;
  1483. if (endpoint == 0) {
  1484. /* control EP filled in when used */
  1485. return urb;
  1486. }
  1487. ep_desc = find_ep(serial, endpoint);
  1488. if (!ep_desc) {
  1489. usb_free_urb(urb);
  1490. return NULL;
  1491. }
  1492. if (usb_endpoint_xfer_int(ep_desc)) {
  1493. ep_type_name = "INT";
  1494. usb_fill_int_urb(urb, serial->dev,
  1495. usb_sndintpipe(serial->dev, endpoint) | dir,
  1496. buf, len, callback, ctx,
  1497. ep_desc->bInterval);
  1498. } else if (usb_endpoint_xfer_bulk(ep_desc)) {
  1499. ep_type_name = "BULK";
  1500. usb_fill_bulk_urb(urb, serial->dev,
  1501. usb_sndbulkpipe(serial->dev, endpoint) | dir,
  1502. buf, len, callback, ctx);
  1503. } else {
  1504. dev_warn(&serial->interface->dev,
  1505. "unsupported endpoint type %x\n",
  1506. usb_endpoint_type(ep_desc));
  1507. usb_free_urb(urb);
  1508. return NULL;
  1509. }
  1510. dev_dbg(&serial->interface->dev, "%s - using urb %p for %s endpoint %x\n",
  1511. __func__, urb, ep_type_name, endpoint);
  1512. return urb;
  1513. }
  1514. static struct callbacks {
  1515. void (*instat_callback)(struct urb *);
  1516. void (*glocont_callback)(struct urb *);
  1517. void (*indat_callback)(struct urb *);
  1518. void (*outdat_callback)(struct urb *);
  1519. void (*inack_callback)(struct urb *);
  1520. void (*outcont_callback)(struct urb *);
  1521. } keyspan_callbacks[] = {
  1522. {
  1523. /* msg_usa26 callbacks */
  1524. .instat_callback = usa26_instat_callback,
  1525. .glocont_callback = usa26_glocont_callback,
  1526. .indat_callback = usa26_indat_callback,
  1527. .outdat_callback = usa2x_outdat_callback,
  1528. .inack_callback = usa26_inack_callback,
  1529. .outcont_callback = usa26_outcont_callback,
  1530. }, {
  1531. /* msg_usa28 callbacks */
  1532. .instat_callback = usa28_instat_callback,
  1533. .glocont_callback = usa28_glocont_callback,
  1534. .indat_callback = usa28_indat_callback,
  1535. .outdat_callback = usa2x_outdat_callback,
  1536. .inack_callback = usa28_inack_callback,
  1537. .outcont_callback = usa28_outcont_callback,
  1538. }, {
  1539. /* msg_usa49 callbacks */
  1540. .instat_callback = usa49_instat_callback,
  1541. .glocont_callback = usa49_glocont_callback,
  1542. .indat_callback = usa49_indat_callback,
  1543. .outdat_callback = usa2x_outdat_callback,
  1544. .inack_callback = usa49_inack_callback,
  1545. .outcont_callback = usa49_outcont_callback,
  1546. }, {
  1547. /* msg_usa90 callbacks */
  1548. .instat_callback = usa90_instat_callback,
  1549. .glocont_callback = usa28_glocont_callback,
  1550. .indat_callback = usa90_indat_callback,
  1551. .outdat_callback = usa2x_outdat_callback,
  1552. .inack_callback = usa28_inack_callback,
  1553. .outcont_callback = usa90_outcont_callback,
  1554. }, {
  1555. /* msg_usa67 callbacks */
  1556. .instat_callback = usa67_instat_callback,
  1557. .glocont_callback = usa67_glocont_callback,
  1558. .indat_callback = usa26_indat_callback,
  1559. .outdat_callback = usa2x_outdat_callback,
  1560. .inack_callback = usa26_inack_callback,
  1561. .outcont_callback = usa26_outcont_callback,
  1562. }
  1563. };
  1564. /* Generic setup urbs function that uses
  1565. data in device_details */
  1566. static void keyspan_setup_urbs(struct usb_serial *serial)
  1567. {
  1568. struct keyspan_serial_private *s_priv;
  1569. const struct keyspan_device_details *d_details;
  1570. struct callbacks *cback;
  1571. s_priv = usb_get_serial_data(serial);
  1572. d_details = s_priv->device_details;
  1573. /* Setup values for the various callback routines */
  1574. cback = &keyspan_callbacks[d_details->msg_format];
  1575. /* Allocate and set up urbs for each one that is in use,
  1576. starting with instat endpoints */
  1577. s_priv->instat_urb = keyspan_setup_urb
  1578. (serial, d_details->instat_endpoint, USB_DIR_IN,
  1579. serial, s_priv->instat_buf, INSTAT_BUFLEN,
  1580. cback->instat_callback);
  1581. s_priv->indat_urb = keyspan_setup_urb
  1582. (serial, d_details->indat_endpoint, USB_DIR_IN,
  1583. serial, s_priv->indat_buf, INDAT49W_BUFLEN,
  1584. usa49wg_indat_callback);
  1585. s_priv->glocont_urb = keyspan_setup_urb
  1586. (serial, d_details->glocont_endpoint, USB_DIR_OUT,
  1587. serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
  1588. cback->glocont_callback);
  1589. }
  1590. /* usa19 function doesn't require prescaler */
  1591. static int keyspan_usa19_calc_baud(struct usb_serial_port *port,
  1592. u32 baud_rate, u32 baudclk, u8 *rate_hi,
  1593. u8 *rate_low, u8 *prescaler, int portnum)
  1594. {
  1595. u32 b16, /* baud rate times 16 (actual rate used internally) */
  1596. div, /* divisor */
  1597. cnt; /* inverse of divisor (programmed into 8051) */
  1598. dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
  1599. /* prevent divide by zero... */
  1600. b16 = baud_rate * 16L;
  1601. if (b16 == 0)
  1602. return KEYSPAN_INVALID_BAUD_RATE;
  1603. /* Any "standard" rate over 57k6 is marginal on the USA-19
  1604. as we run out of divisor resolution. */
  1605. if (baud_rate > 57600)
  1606. return KEYSPAN_INVALID_BAUD_RATE;
  1607. /* calculate the divisor and the counter (its inverse) */
  1608. div = baudclk / b16;
  1609. if (div == 0)
  1610. return KEYSPAN_INVALID_BAUD_RATE;
  1611. else
  1612. cnt = 0 - div;
  1613. if (div > 0xffff)
  1614. return KEYSPAN_INVALID_BAUD_RATE;
  1615. /* return the counter values if non-null */
  1616. if (rate_low)
  1617. *rate_low = (u8) (cnt & 0xff);
  1618. if (rate_hi)
  1619. *rate_hi = (u8) ((cnt >> 8) & 0xff);
  1620. if (rate_low && rate_hi)
  1621. dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
  1622. __func__, baud_rate, *rate_hi, *rate_low);
  1623. return KEYSPAN_BAUD_RATE_OK;
  1624. }
  1625. /* usa19hs function doesn't require prescaler */
  1626. static int keyspan_usa19hs_calc_baud(struct usb_serial_port *port,
  1627. u32 baud_rate, u32 baudclk, u8 *rate_hi,
  1628. u8 *rate_low, u8 *prescaler, int portnum)
  1629. {
  1630. u32 b16, /* baud rate times 16 (actual rate used internally) */
  1631. div; /* divisor */
  1632. dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
  1633. /* prevent divide by zero... */
  1634. b16 = baud_rate * 16L;
  1635. if (b16 == 0)
  1636. return KEYSPAN_INVALID_BAUD_RATE;
  1637. /* calculate the divisor */
  1638. div = baudclk / b16;
  1639. if (div == 0)
  1640. return KEYSPAN_INVALID_BAUD_RATE;
  1641. if (div > 0xffff)
  1642. return KEYSPAN_INVALID_BAUD_RATE;
  1643. /* return the counter values if non-null */
  1644. if (rate_low)
  1645. *rate_low = (u8) (div & 0xff);
  1646. if (rate_hi)
  1647. *rate_hi = (u8) ((div >> 8) & 0xff);
  1648. if (rate_low && rate_hi)
  1649. dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
  1650. __func__, baud_rate, *rate_hi, *rate_low);
  1651. return KEYSPAN_BAUD_RATE_OK;
  1652. }
  1653. static int keyspan_usa19w_calc_baud(struct usb_serial_port *port,
  1654. u32 baud_rate, u32 baudclk, u8 *rate_hi,
  1655. u8 *rate_low, u8 *prescaler, int portnum)
  1656. {
  1657. u32 b16, /* baud rate times 16 (actual rate used internally) */
  1658. clk, /* clock with 13/8 prescaler */
  1659. div, /* divisor using 13/8 prescaler */
  1660. res, /* resulting baud rate using 13/8 prescaler */
  1661. diff, /* error using 13/8 prescaler */
  1662. smallest_diff;
  1663. u8 best_prescaler;
  1664. int i;
  1665. dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
  1666. /* prevent divide by zero */
  1667. b16 = baud_rate * 16L;
  1668. if (b16 == 0)
  1669. return KEYSPAN_INVALID_BAUD_RATE;
  1670. /* Calculate prescaler by trying them all and looking
  1671. for best fit */
  1672. /* start with largest possible difference */
  1673. smallest_diff = 0xffffffff;
  1674. /* 0 is an invalid prescaler, used as a flag */
  1675. best_prescaler = 0;
  1676. for (i = 8; i <= 0xff; ++i) {
  1677. clk = (baudclk * 8) / (u32) i;
  1678. div = clk / b16;
  1679. if (div == 0)
  1680. continue;
  1681. res = clk / div;
  1682. diff = (res > b16) ? (res-b16) : (b16-res);
  1683. if (diff < smallest_diff) {
  1684. best_prescaler = i;
  1685. smallest_diff = diff;
  1686. }
  1687. }
  1688. if (best_prescaler == 0)
  1689. return KEYSPAN_INVALID_BAUD_RATE;
  1690. clk = (baudclk * 8) / (u32) best_prescaler;
  1691. div = clk / b16;
  1692. /* return the divisor and prescaler if non-null */
  1693. if (rate_low)
  1694. *rate_low = (u8) (div & 0xff);
  1695. if (rate_hi)
  1696. *rate_hi = (u8) ((div >> 8) & 0xff);
  1697. if (prescaler) {
  1698. *prescaler = best_prescaler;
  1699. /* dev_dbg(&port->dev, "%s - %d %d\n", __func__, *prescaler, div); */
  1700. }
  1701. return KEYSPAN_BAUD_RATE_OK;
  1702. }
  1703. /* USA-28 supports different maximum baud rates on each port */
  1704. static int keyspan_usa28_calc_baud(struct usb_serial_port *port,
  1705. u32 baud_rate, u32 baudclk, u8 *rate_hi,
  1706. u8 *rate_low, u8 *prescaler, int portnum)
  1707. {
  1708. u32 b16, /* baud rate times 16 (actual rate used internally) */
  1709. div, /* divisor */
  1710. cnt; /* inverse of divisor (programmed into 8051) */
  1711. dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
  1712. /* prevent divide by zero */
  1713. b16 = baud_rate * 16L;
  1714. if (b16 == 0)
  1715. return KEYSPAN_INVALID_BAUD_RATE;
  1716. /* calculate the divisor and the counter (its inverse) */
  1717. div = KEYSPAN_USA28_BAUDCLK / b16;
  1718. if (div == 0)
  1719. return KEYSPAN_INVALID_BAUD_RATE;
  1720. else
  1721. cnt = 0 - div;
  1722. /* check for out of range, based on portnum,
  1723. and return result */
  1724. if (portnum == 0) {
  1725. if (div > 0xffff)
  1726. return KEYSPAN_INVALID_BAUD_RATE;
  1727. } else {
  1728. if (portnum == 1) {
  1729. if (div > 0xff)
  1730. return KEYSPAN_INVALID_BAUD_RATE;
  1731. } else
  1732. return KEYSPAN_INVALID_BAUD_RATE;
  1733. }
  1734. /* return the counter values if not NULL
  1735. (port 1 will ignore retHi) */
  1736. if (rate_low)
  1737. *rate_low = (u8) (cnt & 0xff);
  1738. if (rate_hi)
  1739. *rate_hi = (u8) ((cnt >> 8) & 0xff);
  1740. dev_dbg(&port->dev, "%s - %d OK.\n", __func__, baud_rate);
  1741. return KEYSPAN_BAUD_RATE_OK;
  1742. }
  1743. static int keyspan_usa26_send_setup(struct usb_serial *serial,
  1744. struct usb_serial_port *port,
  1745. int reset_port)
  1746. {
  1747. struct keyspan_usa26_portControlMessage msg;
  1748. struct keyspan_serial_private *s_priv;
  1749. struct keyspan_port_private *p_priv;
  1750. const struct keyspan_device_details *d_details;
  1751. struct urb *this_urb;
  1752. int device_port, err;
  1753. dev_dbg(&port->dev, "%s reset=%d\n", __func__, reset_port);
  1754. s_priv = usb_get_serial_data(serial);
  1755. p_priv = usb_get_serial_port_data(port);
  1756. d_details = s_priv->device_details;
  1757. device_port = port->port_number;
  1758. this_urb = p_priv->outcont_urb;
  1759. /* Make sure we have an urb then send the message */
  1760. if (this_urb == NULL) {
  1761. dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
  1762. return -1;
  1763. }
  1764. dev_dbg(&port->dev, "%s - endpoint %x\n",
  1765. __func__, usb_pipeendpoint(this_urb->pipe));
  1766. /* Save reset port val for resend.
  1767. Don't overwrite resend for open/close condition. */
  1768. if ((reset_port + 1) > p_priv->resend_cont)
  1769. p_priv->resend_cont = reset_port + 1;
  1770. if (this_urb->status == -EINPROGRESS) {
  1771. /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
  1772. mdelay(5);
  1773. return -1;
  1774. }
  1775. memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));
  1776. /* Only set baud rate if it's changed */
  1777. if (p_priv->old_baud != p_priv->baud) {
  1778. p_priv->old_baud = p_priv->baud;
  1779. msg.setClocking = 0xff;
  1780. if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
  1781. &msg.baudHi, &msg.baudLo, &msg.prescaler,
  1782. device_port) == KEYSPAN_INVALID_BAUD_RATE) {
  1783. dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
  1784. __func__, p_priv->baud);
  1785. msg.baudLo = 0;
  1786. msg.baudHi = 125; /* Values for 9600 baud */
  1787. msg.prescaler = 10;
  1788. }
  1789. msg.setPrescaler = 0xff;
  1790. }
  1791. msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
  1792. switch (p_priv->cflag & CSIZE) {
  1793. case CS5:
  1794. msg.lcr |= USA_DATABITS_5;
  1795. break;
  1796. case CS6:
  1797. msg.lcr |= USA_DATABITS_6;
  1798. break;
  1799. case CS7:
  1800. msg.lcr |= USA_DATABITS_7;
  1801. break;
  1802. case CS8:
  1803. msg.lcr |= USA_DATABITS_8;
  1804. break;
  1805. }
  1806. if (p_priv->cflag & PARENB) {
  1807. /* note USA_PARITY_NONE == 0 */
  1808. msg.lcr |= (p_priv->cflag & PARODD) ?
  1809. USA_PARITY_ODD : USA_PARITY_EVEN;
  1810. }
  1811. msg.setLcr = 0xff;
  1812. msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
  1813. msg.xonFlowControl = 0;
  1814. msg.setFlowControl = 0xff;
  1815. msg.forwardingLength = 16;
  1816. msg.xonChar = 17;
  1817. msg.xoffChar = 19;
  1818. /* Opening port */
  1819. if (reset_port == 1) {
  1820. msg._txOn = 1;
  1821. msg._txOff = 0;
  1822. msg.txFlush = 0;
  1823. msg.txBreak = 0;
  1824. msg.rxOn = 1;
  1825. msg.rxOff = 0;
  1826. msg.rxFlush = 1;
  1827. msg.rxForward = 0;
  1828. msg.returnStatus = 0;
  1829. msg.resetDataToggle = 0xff;
  1830. }
  1831. /* Closing port */
  1832. else if (reset_port == 2) {
  1833. msg._txOn = 0;
  1834. msg._txOff = 1;
  1835. msg.txFlush = 0;
  1836. msg.txBreak = 0;
  1837. msg.rxOn = 0;
  1838. msg.rxOff = 1;
  1839. msg.rxFlush = 1;
  1840. msg.rxForward = 0;
  1841. msg.returnStatus = 0;
  1842. msg.resetDataToggle = 0;
  1843. }
  1844. /* Sending intermediate configs */
  1845. else {
  1846. msg._txOn = (!p_priv->break_on);
  1847. msg._txOff = 0;
  1848. msg.txFlush = 0;
  1849. msg.txBreak = (p_priv->break_on);
  1850. msg.rxOn = 0;
  1851. msg.rxOff = 0;
  1852. msg.rxFlush = 0;
  1853. msg.rxForward = 0;
  1854. msg.returnStatus = 0;
  1855. msg.resetDataToggle = 0x0;
  1856. }
  1857. /* Do handshaking outputs */
  1858. msg.setTxTriState_setRts = 0xff;
  1859. msg.txTriState_rts = p_priv->rts_state;
  1860. msg.setHskoa_setDtr = 0xff;
  1861. msg.hskoa_dtr = p_priv->dtr_state;
  1862. p_priv->resend_cont = 0;
  1863. memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
  1864. /* send the data out the device on control endpoint */
  1865. this_urb->transfer_buffer_length = sizeof(msg);
  1866. err = usb_submit_urb(this_urb, GFP_ATOMIC);
  1867. if (err != 0)
  1868. dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
  1869. return 0;
  1870. }
  1871. static int keyspan_usa28_send_setup(struct usb_serial *serial,
  1872. struct usb_serial_port *port,
  1873. int reset_port)
  1874. {
  1875. struct keyspan_usa28_portControlMessage msg;
  1876. struct keyspan_serial_private *s_priv;
  1877. struct keyspan_port_private *p_priv;
  1878. const struct keyspan_device_details *d_details;
  1879. struct urb *this_urb;
  1880. int device_port, err;
  1881. s_priv = usb_get_serial_data(serial);
  1882. p_priv = usb_get_serial_port_data(port);
  1883. d_details = s_priv->device_details;
  1884. device_port = port->port_number;
  1885. /* only do something if we have a bulk out endpoint */
  1886. this_urb = p_priv->outcont_urb;
  1887. if (this_urb == NULL) {
  1888. dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
  1889. return -1;
  1890. }
  1891. /* Save reset port val for resend.
  1892. Don't overwrite resend for open/close condition. */
  1893. if ((reset_port + 1) > p_priv->resend_cont)
  1894. p_priv->resend_cont = reset_port + 1;
  1895. if (this_urb->status == -EINPROGRESS) {
  1896. dev_dbg(&port->dev, "%s already writing\n", __func__);
  1897. mdelay(5);
  1898. return -1;
  1899. }
  1900. memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
  1901. msg.setBaudRate = 1;
  1902. if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
  1903. &msg.baudHi, &msg.baudLo, NULL,
  1904. device_port) == KEYSPAN_INVALID_BAUD_RATE) {
  1905. dev_dbg(&port->dev, "%s - Invalid baud rate requested %d.\n",
  1906. __func__, p_priv->baud);
  1907. msg.baudLo = 0xff;
  1908. msg.baudHi = 0xb2; /* Values for 9600 baud */
  1909. }
  1910. /* If parity is enabled, we must calculate it ourselves. */
  1911. msg.parity = 0; /* XXX for now */
  1912. msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
  1913. msg.xonFlowControl = 0;
  1914. /* Do handshaking outputs, DTR is inverted relative to RTS */
  1915. msg.rts = p_priv->rts_state;
  1916. msg.dtr = p_priv->dtr_state;
  1917. msg.forwardingLength = 16;
  1918. msg.forwardMs = 10;
  1919. msg.breakThreshold = 45;
  1920. msg.xonChar = 17;
  1921. msg.xoffChar = 19;
  1922. /*msg.returnStatus = 1;
  1923. msg.resetDataToggle = 0xff;*/
  1924. /* Opening port */
  1925. if (reset_port == 1) {
  1926. msg._txOn = 1;
  1927. msg._txOff = 0;
  1928. msg.txFlush = 0;
  1929. msg.txForceXoff = 0;
  1930. msg.txBreak = 0;
  1931. msg.rxOn = 1;
  1932. msg.rxOff = 0;
  1933. msg.rxFlush = 1;
  1934. msg.rxForward = 0;
  1935. msg.returnStatus = 0;
  1936. msg.resetDataToggle = 0xff;
  1937. }
  1938. /* Closing port */
  1939. else if (reset_port == 2) {
  1940. msg._txOn = 0;
  1941. msg._txOff = 1;
  1942. msg.txFlush = 0;
  1943. msg.txForceXoff = 0;
  1944. msg.txBreak = 0;
  1945. msg.rxOn = 0;
  1946. msg.rxOff = 1;
  1947. msg.rxFlush = 1;
  1948. msg.rxForward = 0;
  1949. msg.returnStatus = 0;
  1950. msg.resetDataToggle = 0;
  1951. }
  1952. /* Sending intermediate configs */
  1953. else {
  1954. msg._txOn = (!p_priv->break_on);
  1955. msg._txOff = 0;
  1956. msg.txFlush = 0;
  1957. msg.txForceXoff = 0;
  1958. msg.txBreak = (p_priv->break_on);
  1959. msg.rxOn = 0;
  1960. msg.rxOff = 0;
  1961. msg.rxFlush = 0;
  1962. msg.rxForward = 0;
  1963. msg.returnStatus = 0;
  1964. msg.resetDataToggle = 0x0;
  1965. }
  1966. p_priv->resend_cont = 0;
  1967. memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
  1968. /* send the data out the device on control endpoint */
  1969. this_urb->transfer_buffer_length = sizeof(msg);
  1970. err = usb_submit_urb(this_urb, GFP_ATOMIC);
  1971. if (err != 0)
  1972. dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed\n", __func__);
  1973. return 0;
  1974. }
  1975. static int keyspan_usa49_send_setup(struct usb_serial *serial,
  1976. struct usb_serial_port *port,
  1977. int reset_port)
  1978. {
  1979. struct keyspan_usa49_portControlMessage msg;
  1980. struct usb_ctrlrequest *dr = NULL;
  1981. struct keyspan_serial_private *s_priv;
  1982. struct keyspan_port_private *p_priv;
  1983. const struct keyspan_device_details *d_details;
  1984. struct urb *this_urb;
  1985. int err, device_port;
  1986. s_priv = usb_get_serial_data(serial);
  1987. p_priv = usb_get_serial_port_data(port);
  1988. d_details = s_priv->device_details;
  1989. this_urb = s_priv->glocont_urb;
  1990. /* Work out which port within the device is being setup */
  1991. device_port = port->port_number;
  1992. /* Make sure we have an urb then send the message */
  1993. if (this_urb == NULL) {
  1994. dev_dbg(&port->dev, "%s - oops no urb for port.\n", __func__);
  1995. return -1;
  1996. }
  1997. dev_dbg(&port->dev, "%s - endpoint %x (%d)\n",
  1998. __func__, usb_pipeendpoint(this_urb->pipe), device_port);
  1999. /* Save reset port val for resend.
  2000. Don't overwrite resend for open/close condition. */
  2001. if ((reset_port + 1) > p_priv->resend_cont)
  2002. p_priv->resend_cont = reset_port + 1;
  2003. if (this_urb->status == -EINPROGRESS) {
  2004. /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
  2005. mdelay(5);
  2006. return -1;
  2007. }
  2008. memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
  2009. msg.portNumber = device_port;
  2010. /* Only set baud rate if it's changed */
  2011. if (p_priv->old_baud != p_priv->baud) {
  2012. p_priv->old_baud = p_priv->baud;
  2013. msg.setClocking = 0xff;
  2014. if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
  2015. &msg.baudHi, &msg.baudLo, &msg.prescaler,
  2016. device_port) == KEYSPAN_INVALID_BAUD_RATE) {
  2017. dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
  2018. __func__, p_priv->baud);
  2019. msg.baudLo = 0;
  2020. msg.baudHi = 125; /* Values for 9600 baud */
  2021. msg.prescaler = 10;
  2022. }
  2023. /* msg.setPrescaler = 0xff; */
  2024. }
  2025. msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
  2026. switch (p_priv->cflag & CSIZE) {
  2027. case CS5:
  2028. msg.lcr |= USA_DATABITS_5;
  2029. break;
  2030. case CS6:
  2031. msg.lcr |= USA_DATABITS_6;
  2032. break;
  2033. case CS7:
  2034. msg.lcr |= USA_DATABITS_7;
  2035. break;
  2036. case CS8:
  2037. msg.lcr |= USA_DATABITS_8;
  2038. break;
  2039. }
  2040. if (p_priv->cflag & PARENB) {
  2041. /* note USA_PARITY_NONE == 0 */
  2042. msg.lcr |= (p_priv->cflag & PARODD) ?
  2043. USA_PARITY_ODD : USA_PARITY_EVEN;
  2044. }
  2045. msg.setLcr = 0xff;
  2046. msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
  2047. msg.xonFlowControl = 0;
  2048. msg.setFlowControl = 0xff;
  2049. msg.forwardingLength = 16;
  2050. msg.xonChar = 17;
  2051. msg.xoffChar = 19;
  2052. /* Opening port */
  2053. if (reset_port == 1) {
  2054. msg._txOn = 1;
  2055. msg._txOff = 0;
  2056. msg.txFlush = 0;
  2057. msg.txBreak = 0;
  2058. msg.rxOn = 1;
  2059. msg.rxOff = 0;
  2060. msg.rxFlush = 1;
  2061. msg.rxForward = 0;
  2062. msg.returnStatus = 0;
  2063. msg.resetDataToggle = 0xff;
  2064. msg.enablePort = 1;
  2065. msg.disablePort = 0;
  2066. }
  2067. /* Closing port */
  2068. else if (reset_port == 2) {
  2069. msg._txOn = 0;
  2070. msg._txOff = 1;
  2071. msg.txFlush = 0;
  2072. msg.txBreak = 0;
  2073. msg.rxOn = 0;
  2074. msg.rxOff = 1;
  2075. msg.rxFlush = 1;
  2076. msg.rxForward = 0;
  2077. msg.returnStatus = 0;
  2078. msg.resetDataToggle = 0;
  2079. msg.enablePort = 0;
  2080. msg.disablePort = 1;
  2081. }
  2082. /* Sending intermediate configs */
  2083. else {
  2084. msg._txOn = (!p_priv->break_on);
  2085. msg._txOff = 0;
  2086. msg.txFlush = 0;
  2087. msg.txBreak = (p_priv->break_on);
  2088. msg.rxOn = 0;
  2089. msg.rxOff = 0;
  2090. msg.rxFlush = 0;
  2091. msg.rxForward = 0;
  2092. msg.returnStatus = 0;
  2093. msg.resetDataToggle = 0x0;
  2094. msg.enablePort = 0;
  2095. msg.disablePort = 0;
  2096. }
  2097. /* Do handshaking outputs */
  2098. msg.setRts = 0xff;
  2099. msg.rts = p_priv->rts_state;
  2100. msg.setDtr = 0xff;
  2101. msg.dtr = p_priv->dtr_state;
  2102. p_priv->resend_cont = 0;
  2103. /* if the device is a 49wg, we send control message on usb
  2104. control EP 0 */
  2105. if (d_details->product_id == keyspan_usa49wg_product_id) {
  2106. dr = (void *)(s_priv->ctrl_buf);
  2107. dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
  2108. dr->bRequest = 0xB0; /* 49wg control message */
  2109. dr->wValue = 0;
  2110. dr->wIndex = 0;
  2111. dr->wLength = cpu_to_le16(sizeof(msg));
  2112. memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
  2113. usb_fill_control_urb(this_urb, serial->dev,
  2114. usb_sndctrlpipe(serial->dev, 0),
  2115. (unsigned char *)dr, s_priv->glocont_buf,
  2116. sizeof(msg), usa49_glocont_callback, serial);
  2117. } else {
  2118. memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
  2119. /* send the data out the device on control endpoint */
  2120. this_urb->transfer_buffer_length = sizeof(msg);
  2121. }
  2122. err = usb_submit_urb(this_urb, GFP_ATOMIC);
  2123. if (err != 0)
  2124. dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
  2125. return 0;
  2126. }
  2127. static int keyspan_usa90_send_setup(struct usb_serial *serial,
  2128. struct usb_serial_port *port,
  2129. int reset_port)
  2130. {
  2131. struct keyspan_usa90_portControlMessage msg;
  2132. struct keyspan_serial_private *s_priv;
  2133. struct keyspan_port_private *p_priv;
  2134. const struct keyspan_device_details *d_details;
  2135. struct urb *this_urb;
  2136. int err;
  2137. u8 prescaler;
  2138. s_priv = usb_get_serial_data(serial);
  2139. p_priv = usb_get_serial_port_data(port);
  2140. d_details = s_priv->device_details;
  2141. /* only do something if we have a bulk out endpoint */
  2142. this_urb = p_priv->outcont_urb;
  2143. if (this_urb == NULL) {
  2144. dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
  2145. return -1;
  2146. }
  2147. /* Save reset port val for resend.
  2148. Don't overwrite resend for open/close condition. */
  2149. if ((reset_port + 1) > p_priv->resend_cont)
  2150. p_priv->resend_cont = reset_port + 1;
  2151. if (this_urb->status == -EINPROGRESS) {
  2152. dev_dbg(&port->dev, "%s already writing\n", __func__);
  2153. mdelay(5);
  2154. return -1;
  2155. }
  2156. memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
  2157. /* Only set baud rate if it's changed */
  2158. if (p_priv->old_baud != p_priv->baud) {
  2159. p_priv->old_baud = p_priv->baud;
  2160. msg.setClocking = 0x01;
  2161. if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
  2162. &msg.baudHi, &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
  2163. dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
  2164. __func__, p_priv->baud);
  2165. p_priv->baud = 9600;
  2166. d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
  2167. &msg.baudHi, &msg.baudLo, &prescaler, 0);
  2168. }
  2169. msg.setRxMode = 1;
  2170. msg.setTxMode = 1;
  2171. }
  2172. /* modes must always be correctly specified */
  2173. if (p_priv->baud > 57600) {
  2174. msg.rxMode = RXMODE_DMA;
  2175. msg.txMode = TXMODE_DMA;
  2176. } else {
  2177. msg.rxMode = RXMODE_BYHAND;
  2178. msg.txMode = TXMODE_BYHAND;
  2179. }
  2180. msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
  2181. switch (p_priv->cflag & CSIZE) {
  2182. case CS5:
  2183. msg.lcr |= USA_DATABITS_5;
  2184. break;
  2185. case CS6:
  2186. msg.lcr |= USA_DATABITS_6;
  2187. break;
  2188. case CS7:
  2189. msg.lcr |= USA_DATABITS_7;
  2190. break;
  2191. case CS8:
  2192. msg.lcr |= USA_DATABITS_8;
  2193. break;
  2194. }
  2195. if (p_priv->cflag & PARENB) {
  2196. /* note USA_PARITY_NONE == 0 */
  2197. msg.lcr |= (p_priv->cflag & PARODD) ?
  2198. USA_PARITY_ODD : USA_PARITY_EVEN;
  2199. }
  2200. if (p_priv->old_cflag != p_priv->cflag) {
  2201. p_priv->old_cflag = p_priv->cflag;
  2202. msg.setLcr = 0x01;
  2203. }
  2204. if (p_priv->flow_control == flow_cts)
  2205. msg.txFlowControl = TXFLOW_CTS;
  2206. msg.setTxFlowControl = 0x01;
  2207. msg.setRxFlowControl = 0x01;
  2208. msg.rxForwardingLength = 16;
  2209. msg.rxForwardingTimeout = 16;
  2210. msg.txAckSetting = 0;
  2211. msg.xonChar = 17;
  2212. msg.xoffChar = 19;
  2213. /* Opening port */
  2214. if (reset_port == 1) {
  2215. msg.portEnabled = 1;
  2216. msg.rxFlush = 1;
  2217. msg.txBreak = (p_priv->break_on);
  2218. }
  2219. /* Closing port */
  2220. else if (reset_port == 2)
  2221. msg.portEnabled = 0;
  2222. /* Sending intermediate configs */
  2223. else {
  2224. msg.portEnabled = 1;
  2225. msg.txBreak = (p_priv->break_on);
  2226. }
  2227. /* Do handshaking outputs */
  2228. msg.setRts = 0x01;
  2229. msg.rts = p_priv->rts_state;
  2230. msg.setDtr = 0x01;
  2231. msg.dtr = p_priv->dtr_state;
  2232. p_priv->resend_cont = 0;
  2233. memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
  2234. /* send the data out the device on control endpoint */
  2235. this_urb->transfer_buffer_length = sizeof(msg);
  2236. err = usb_submit_urb(this_urb, GFP_ATOMIC);
  2237. if (err != 0)
  2238. dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
  2239. return 0;
  2240. }
  2241. static int keyspan_usa67_send_setup(struct usb_serial *serial,
  2242. struct usb_serial_port *port,
  2243. int reset_port)
  2244. {
  2245. struct keyspan_usa67_portControlMessage msg;
  2246. struct keyspan_serial_private *s_priv;
  2247. struct keyspan_port_private *p_priv;
  2248. const struct keyspan_device_details *d_details;
  2249. struct urb *this_urb;
  2250. int err, device_port;
  2251. s_priv = usb_get_serial_data(serial);
  2252. p_priv = usb_get_serial_port_data(port);
  2253. d_details = s_priv->device_details;
  2254. this_urb = s_priv->glocont_urb;
  2255. /* Work out which port within the device is being setup */
  2256. device_port = port->port_number;
  2257. /* Make sure we have an urb then send the message */
  2258. if (this_urb == NULL) {
  2259. dev_dbg(&port->dev, "%s - oops no urb for port.\n", __func__);
  2260. return -1;
  2261. }
  2262. /* Save reset port val for resend.
  2263. Don't overwrite resend for open/close condition. */
  2264. if ((reset_port + 1) > p_priv->resend_cont)
  2265. p_priv->resend_cont = reset_port + 1;
  2266. if (this_urb->status == -EINPROGRESS) {
  2267. /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
  2268. mdelay(5);
  2269. return -1;
  2270. }
  2271. memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
  2272. msg.port = device_port;
  2273. /* Only set baud rate if it's changed */
  2274. if (p_priv->old_baud != p_priv->baud) {
  2275. p_priv->old_baud = p_priv->baud;
  2276. msg.setClocking = 0xff;
  2277. if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
  2278. &msg.baudHi, &msg.baudLo, &msg.prescaler,
  2279. device_port) == KEYSPAN_INVALID_BAUD_RATE) {
  2280. dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
  2281. __func__, p_priv->baud);
  2282. msg.baudLo = 0;
  2283. msg.baudHi = 125; /* Values for 9600 baud */
  2284. msg.prescaler = 10;
  2285. }
  2286. msg.setPrescaler = 0xff;
  2287. }
  2288. msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
  2289. switch (p_priv->cflag & CSIZE) {
  2290. case CS5:
  2291. msg.lcr |= USA_DATABITS_5;
  2292. break;
  2293. case CS6:
  2294. msg.lcr |= USA_DATABITS_6;
  2295. break;
  2296. case CS7:
  2297. msg.lcr |= USA_DATABITS_7;
  2298. break;
  2299. case CS8:
  2300. msg.lcr |= USA_DATABITS_8;
  2301. break;
  2302. }
  2303. if (p_priv->cflag & PARENB) {
  2304. /* note USA_PARITY_NONE == 0 */
  2305. msg.lcr |= (p_priv->cflag & PARODD) ?
  2306. USA_PARITY_ODD : USA_PARITY_EVEN;
  2307. }
  2308. msg.setLcr = 0xff;
  2309. msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
  2310. msg.xonFlowControl = 0;
  2311. msg.setFlowControl = 0xff;
  2312. msg.forwardingLength = 16;
  2313. msg.xonChar = 17;
  2314. msg.xoffChar = 19;
  2315. if (reset_port == 1) {
  2316. /* Opening port */
  2317. msg._txOn = 1;
  2318. msg._txOff = 0;
  2319. msg.txFlush = 0;
  2320. msg.txBreak = 0;
  2321. msg.rxOn = 1;
  2322. msg.rxOff = 0;
  2323. msg.rxFlush = 1;
  2324. msg.rxForward = 0;
  2325. msg.returnStatus = 0;
  2326. msg.resetDataToggle = 0xff;
  2327. } else if (reset_port == 2) {
  2328. /* Closing port */
  2329. msg._txOn = 0;
  2330. msg._txOff = 1;
  2331. msg.txFlush = 0;
  2332. msg.txBreak = 0;
  2333. msg.rxOn = 0;
  2334. msg.rxOff = 1;
  2335. msg.rxFlush = 1;
  2336. msg.rxForward = 0;
  2337. msg.returnStatus = 0;
  2338. msg.resetDataToggle = 0;
  2339. } else {
  2340. /* Sending intermediate configs */
  2341. msg._txOn = (!p_priv->break_on);
  2342. msg._txOff = 0;
  2343. msg.txFlush = 0;
  2344. msg.txBreak = (p_priv->break_on);
  2345. msg.rxOn = 0;
  2346. msg.rxOff = 0;
  2347. msg.rxFlush = 0;
  2348. msg.rxForward = 0;
  2349. msg.returnStatus = 0;
  2350. msg.resetDataToggle = 0x0;
  2351. }
  2352. /* Do handshaking outputs */
  2353. msg.setTxTriState_setRts = 0xff;
  2354. msg.txTriState_rts = p_priv->rts_state;
  2355. msg.setHskoa_setDtr = 0xff;
  2356. msg.hskoa_dtr = p_priv->dtr_state;
  2357. p_priv->resend_cont = 0;
  2358. memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
  2359. /* send the data out the device on control endpoint */
  2360. this_urb->transfer_buffer_length = sizeof(msg);
  2361. err = usb_submit_urb(this_urb, GFP_ATOMIC);
  2362. if (err != 0)
  2363. dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
  2364. return 0;
  2365. }
  2366. static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
  2367. {
  2368. struct usb_serial *serial = port->serial;
  2369. struct keyspan_serial_private *s_priv;
  2370. const struct keyspan_device_details *d_details;
  2371. s_priv = usb_get_serial_data(serial);
  2372. d_details = s_priv->device_details;
  2373. switch (d_details->msg_format) {
  2374. case msg_usa26:
  2375. keyspan_usa26_send_setup(serial, port, reset_port);
  2376. break;
  2377. case msg_usa28:
  2378. keyspan_usa28_send_setup(serial, port, reset_port);
  2379. break;
  2380. case msg_usa49:
  2381. keyspan_usa49_send_setup(serial, port, reset_port);
  2382. break;
  2383. case msg_usa90:
  2384. keyspan_usa90_send_setup(serial, port, reset_port);
  2385. break;
  2386. case msg_usa67:
  2387. keyspan_usa67_send_setup(serial, port, reset_port);
  2388. break;
  2389. }
  2390. }
  2391. /* Gets called by the "real" driver (ie once firmware is loaded
  2392. and renumeration has taken place. */
  2393. static int keyspan_startup(struct usb_serial *serial)
  2394. {
  2395. int i, err;
  2396. struct keyspan_serial_private *s_priv;
  2397. const struct keyspan_device_details *d_details;
  2398. for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
  2399. if (d_details->product_id ==
  2400. le16_to_cpu(serial->dev->descriptor.idProduct))
  2401. break;
  2402. if (d_details == NULL) {
  2403. dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
  2404. __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
  2405. return -ENODEV;
  2406. }
  2407. /* Setup private data for serial driver */
  2408. s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
  2409. if (!s_priv)
  2410. return -ENOMEM;
  2411. s_priv->instat_buf = kzalloc(INSTAT_BUFLEN, GFP_KERNEL);
  2412. if (!s_priv->instat_buf)
  2413. goto err_instat_buf;
  2414. s_priv->indat_buf = kzalloc(INDAT49W_BUFLEN, GFP_KERNEL);
  2415. if (!s_priv->indat_buf)
  2416. goto err_indat_buf;
  2417. s_priv->glocont_buf = kzalloc(GLOCONT_BUFLEN, GFP_KERNEL);
  2418. if (!s_priv->glocont_buf)
  2419. goto err_glocont_buf;
  2420. s_priv->ctrl_buf = kzalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  2421. if (!s_priv->ctrl_buf)
  2422. goto err_ctrl_buf;
  2423. s_priv->device_details = d_details;
  2424. usb_set_serial_data(serial, s_priv);
  2425. keyspan_setup_urbs(serial);
  2426. if (s_priv->instat_urb != NULL) {
  2427. err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
  2428. if (err != 0)
  2429. dev_dbg(&serial->dev->dev, "%s - submit instat urb failed %d\n", __func__, err);
  2430. }
  2431. if (s_priv->indat_urb != NULL) {
  2432. err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
  2433. if (err != 0)
  2434. dev_dbg(&serial->dev->dev, "%s - submit indat urb failed %d\n", __func__, err);
  2435. }
  2436. return 0;
  2437. err_ctrl_buf:
  2438. kfree(s_priv->glocont_buf);
  2439. err_glocont_buf:
  2440. kfree(s_priv->indat_buf);
  2441. err_indat_buf:
  2442. kfree(s_priv->instat_buf);
  2443. err_instat_buf:
  2444. kfree(s_priv);
  2445. return -ENOMEM;
  2446. }
  2447. static void keyspan_disconnect(struct usb_serial *serial)
  2448. {
  2449. struct keyspan_serial_private *s_priv;
  2450. s_priv = usb_get_serial_data(serial);
  2451. usb_kill_urb(s_priv->instat_urb);
  2452. usb_kill_urb(s_priv->glocont_urb);
  2453. usb_kill_urb(s_priv->indat_urb);
  2454. }
  2455. static void keyspan_release(struct usb_serial *serial)
  2456. {
  2457. struct keyspan_serial_private *s_priv;
  2458. s_priv = usb_get_serial_data(serial);
  2459. /* Make sure to unlink the URBs submitted in attach. */
  2460. usb_kill_urb(s_priv->instat_urb);
  2461. usb_kill_urb(s_priv->indat_urb);
  2462. usb_free_urb(s_priv->instat_urb);
  2463. usb_free_urb(s_priv->indat_urb);
  2464. usb_free_urb(s_priv->glocont_urb);
  2465. kfree(s_priv->ctrl_buf);
  2466. kfree(s_priv->glocont_buf);
  2467. kfree(s_priv->indat_buf);
  2468. kfree(s_priv->instat_buf);
  2469. kfree(s_priv);
  2470. }
  2471. static int keyspan_port_probe(struct usb_serial_port *port)
  2472. {
  2473. struct usb_serial *serial = port->serial;
  2474. struct keyspan_serial_private *s_priv;
  2475. struct keyspan_port_private *p_priv;
  2476. const struct keyspan_device_details *d_details;
  2477. struct callbacks *cback;
  2478. int endp;
  2479. int port_num;
  2480. int i;
  2481. s_priv = usb_get_serial_data(serial);
  2482. d_details = s_priv->device_details;
  2483. p_priv = kzalloc(sizeof(*p_priv), GFP_KERNEL);
  2484. if (!p_priv)
  2485. return -ENOMEM;
  2486. for (i = 0; i < ARRAY_SIZE(p_priv->in_buffer); ++i) {
  2487. p_priv->in_buffer[i] = kzalloc(IN_BUFLEN, GFP_KERNEL);
  2488. if (!p_priv->in_buffer[i])
  2489. goto err_in_buffer;
  2490. }
  2491. for (i = 0; i < ARRAY_SIZE(p_priv->out_buffer); ++i) {
  2492. p_priv->out_buffer[i] = kzalloc(OUT_BUFLEN, GFP_KERNEL);
  2493. if (!p_priv->out_buffer[i])
  2494. goto err_out_buffer;
  2495. }
  2496. p_priv->inack_buffer = kzalloc(INACK_BUFLEN, GFP_KERNEL);
  2497. if (!p_priv->inack_buffer)
  2498. goto err_inack_buffer;
  2499. p_priv->outcont_buffer = kzalloc(OUTCONT_BUFLEN, GFP_KERNEL);
  2500. if (!p_priv->outcont_buffer)
  2501. goto err_outcont_buffer;
  2502. p_priv->device_details = d_details;
  2503. /* Setup values for the various callback routines */
  2504. cback = &keyspan_callbacks[d_details->msg_format];
  2505. port_num = port->port_number;
  2506. /* Do indat endpoints first, once for each flip */
  2507. endp = d_details->indat_endpoints[port_num];
  2508. for (i = 0; i <= d_details->indat_endp_flip; ++i, ++endp) {
  2509. p_priv->in_urbs[i] = keyspan_setup_urb(serial, endp,
  2510. USB_DIR_IN, port,
  2511. p_priv->in_buffer[i],
  2512. IN_BUFLEN,
  2513. cback->indat_callback);
  2514. }
  2515. /* outdat endpoints also have flip */
  2516. endp = d_details->outdat_endpoints[port_num];
  2517. for (i = 0; i <= d_details->outdat_endp_flip; ++i, ++endp) {
  2518. p_priv->out_urbs[i] = keyspan_setup_urb(serial, endp,
  2519. USB_DIR_OUT, port,
  2520. p_priv->out_buffer[i],
  2521. OUT_BUFLEN,
  2522. cback->outdat_callback);
  2523. }
  2524. /* inack endpoint */
  2525. p_priv->inack_urb = keyspan_setup_urb(serial,
  2526. d_details->inack_endpoints[port_num],
  2527. USB_DIR_IN, port,
  2528. p_priv->inack_buffer,
  2529. INACK_BUFLEN,
  2530. cback->inack_callback);
  2531. /* outcont endpoint */
  2532. p_priv->outcont_urb = keyspan_setup_urb(serial,
  2533. d_details->outcont_endpoints[port_num],
  2534. USB_DIR_OUT, port,
  2535. p_priv->outcont_buffer,
  2536. OUTCONT_BUFLEN,
  2537. cback->outcont_callback);
  2538. usb_set_serial_port_data(port, p_priv);
  2539. return 0;
  2540. err_outcont_buffer:
  2541. kfree(p_priv->inack_buffer);
  2542. err_inack_buffer:
  2543. for (i = 0; i < ARRAY_SIZE(p_priv->out_buffer); ++i)
  2544. kfree(p_priv->out_buffer[i]);
  2545. err_out_buffer:
  2546. for (i = 0; i < ARRAY_SIZE(p_priv->in_buffer); ++i)
  2547. kfree(p_priv->in_buffer[i]);
  2548. err_in_buffer:
  2549. kfree(p_priv);
  2550. return -ENOMEM;
  2551. }
  2552. static int keyspan_port_remove(struct usb_serial_port *port)
  2553. {
  2554. struct keyspan_port_private *p_priv;
  2555. int i;
  2556. p_priv = usb_get_serial_port_data(port);
  2557. usb_kill_urb(p_priv->inack_urb);
  2558. usb_kill_urb(p_priv->outcont_urb);
  2559. for (i = 0; i < 2; i++) {
  2560. usb_kill_urb(p_priv->in_urbs[i]);
  2561. usb_kill_urb(p_priv->out_urbs[i]);
  2562. }
  2563. usb_free_urb(p_priv->inack_urb);
  2564. usb_free_urb(p_priv->outcont_urb);
  2565. for (i = 0; i < 2; i++) {
  2566. usb_free_urb(p_priv->in_urbs[i]);
  2567. usb_free_urb(p_priv->out_urbs[i]);
  2568. }
  2569. kfree(p_priv->outcont_buffer);
  2570. kfree(p_priv->inack_buffer);
  2571. for (i = 0; i < ARRAY_SIZE(p_priv->out_buffer); ++i)
  2572. kfree(p_priv->out_buffer[i]);
  2573. for (i = 0; i < ARRAY_SIZE(p_priv->in_buffer); ++i)
  2574. kfree(p_priv->in_buffer[i]);
  2575. kfree(p_priv);
  2576. return 0;
  2577. }
  2578. /* Structs for the devices, pre and post renumeration. */
  2579. static struct usb_serial_driver keyspan_pre_device = {
  2580. .driver = {
  2581. .owner = THIS_MODULE,
  2582. .name = "keyspan_no_firm",
  2583. },
  2584. .description = "Keyspan - (without firmware)",
  2585. .id_table = keyspan_pre_ids,
  2586. .num_ports = 1,
  2587. .attach = keyspan_fake_startup,
  2588. };
  2589. static struct usb_serial_driver keyspan_1port_device = {
  2590. .driver = {
  2591. .owner = THIS_MODULE,
  2592. .name = "keyspan_1",
  2593. },
  2594. .description = "Keyspan 1 port adapter",
  2595. .id_table = keyspan_1port_ids,
  2596. .num_ports = 1,
  2597. .open = keyspan_open,
  2598. .close = keyspan_close,
  2599. .dtr_rts = keyspan_dtr_rts,
  2600. .write = keyspan_write,
  2601. .write_room = keyspan_write_room,
  2602. .set_termios = keyspan_set_termios,
  2603. .break_ctl = keyspan_break_ctl,
  2604. .tiocmget = keyspan_tiocmget,
  2605. .tiocmset = keyspan_tiocmset,
  2606. .attach = keyspan_startup,
  2607. .disconnect = keyspan_disconnect,
  2608. .release = keyspan_release,
  2609. .port_probe = keyspan_port_probe,
  2610. .port_remove = keyspan_port_remove,
  2611. };
  2612. static struct usb_serial_driver keyspan_2port_device = {
  2613. .driver = {
  2614. .owner = THIS_MODULE,
  2615. .name = "keyspan_2",
  2616. },
  2617. .description = "Keyspan 2 port adapter",
  2618. .id_table = keyspan_2port_ids,
  2619. .num_ports = 2,
  2620. .open = keyspan_open,
  2621. .close = keyspan_close,
  2622. .dtr_rts = keyspan_dtr_rts,
  2623. .write = keyspan_write,
  2624. .write_room = keyspan_write_room,
  2625. .set_termios = keyspan_set_termios,
  2626. .break_ctl = keyspan_break_ctl,
  2627. .tiocmget = keyspan_tiocmget,
  2628. .tiocmset = keyspan_tiocmset,
  2629. .attach = keyspan_startup,
  2630. .disconnect = keyspan_disconnect,
  2631. .release = keyspan_release,
  2632. .port_probe = keyspan_port_probe,
  2633. .port_remove = keyspan_port_remove,
  2634. };
  2635. static struct usb_serial_driver keyspan_4port_device = {
  2636. .driver = {
  2637. .owner = THIS_MODULE,
  2638. .name = "keyspan_4",
  2639. },
  2640. .description = "Keyspan 4 port adapter",
  2641. .id_table = keyspan_4port_ids,
  2642. .num_ports = 4,
  2643. .open = keyspan_open,
  2644. .close = keyspan_close,
  2645. .dtr_rts = keyspan_dtr_rts,
  2646. .write = keyspan_write,
  2647. .write_room = keyspan_write_room,
  2648. .set_termios = keyspan_set_termios,
  2649. .break_ctl = keyspan_break_ctl,
  2650. .tiocmget = keyspan_tiocmget,
  2651. .tiocmset = keyspan_tiocmset,
  2652. .attach = keyspan_startup,
  2653. .disconnect = keyspan_disconnect,
  2654. .release = keyspan_release,
  2655. .port_probe = keyspan_port_probe,
  2656. .port_remove = keyspan_port_remove,
  2657. };
  2658. static struct usb_serial_driver * const serial_drivers[] = {
  2659. &keyspan_pre_device, &keyspan_1port_device,
  2660. &keyspan_2port_device, &keyspan_4port_device, NULL
  2661. };
  2662. module_usb_serial_driver(serial_drivers, keyspan_ids_combined);
  2663. MODULE_AUTHOR(DRIVER_AUTHOR);
  2664. MODULE_DESCRIPTION(DRIVER_DESC);
  2665. MODULE_LICENSE("GPL");
  2666. MODULE_FIRMWARE("keyspan/usa28.fw");
  2667. MODULE_FIRMWARE("keyspan/usa28x.fw");
  2668. MODULE_FIRMWARE("keyspan/usa28xa.fw");
  2669. MODULE_FIRMWARE("keyspan/usa28xb.fw");
  2670. MODULE_FIRMWARE("keyspan/usa19.fw");
  2671. MODULE_FIRMWARE("keyspan/usa19qi.fw");
  2672. MODULE_FIRMWARE("keyspan/mpr.fw");
  2673. MODULE_FIRMWARE("keyspan/usa19qw.fw");
  2674. MODULE_FIRMWARE("keyspan/usa18x.fw");
  2675. MODULE_FIRMWARE("keyspan/usa19w.fw");
  2676. MODULE_FIRMWARE("keyspan/usa49w.fw");
  2677. MODULE_FIRMWARE("keyspan/usa49wlc.fw");