io_edgeport.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145
  1. /*
  2. * Edgeport USB Serial Converter driver
  3. *
  4. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  5. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Supports the following devices:
  13. * Edgeport/4
  14. * Edgeport/4t
  15. * Edgeport/2
  16. * Edgeport/4i
  17. * Edgeport/2i
  18. * Edgeport/421
  19. * Edgeport/21
  20. * Rapidport/4
  21. * Edgeport/8
  22. * Edgeport/2D8
  23. * Edgeport/4D8
  24. * Edgeport/8i
  25. *
  26. * For questions or problems with this driver, contact Inside Out
  27. * Networks technical support, or Peter Berger <pberger@brimson.com>,
  28. * or Al Borchers <alborchers@steinerpoint.com>.
  29. *
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/errno.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/tty.h>
  37. #include <linux/tty_driver.h>
  38. #include <linux/tty_flip.h>
  39. #include <linux/module.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/serial.h>
  42. #include <linux/ioctl.h>
  43. #include <linux/wait.h>
  44. #include <linux/firmware.h>
  45. #include <linux/ihex.h>
  46. #include <linux/uaccess.h>
  47. #include <linux/usb.h>
  48. #include <linux/usb/serial.h>
  49. #include "io_edgeport.h"
  50. #include "io_ionsp.h" /* info for the iosp messages */
  51. #include "io_16654.h" /* 16654 UART defines */
  52. /*
  53. * Version Information
  54. */
  55. #define DRIVER_VERSION "v2.7"
  56. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
  57. #define DRIVER_DESC "Edgeport USB Serial Driver"
  58. #define MAX_NAME_LEN 64
  59. #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
  60. #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
  61. #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
  62. /* receive port state */
  63. enum RXSTATE {
  64. EXPECT_HDR1 = 0, /* Expect header byte 1 */
  65. EXPECT_HDR2 = 1, /* Expect header byte 2 */
  66. EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
  67. EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
  68. };
  69. /* Transmit Fifo
  70. * This Transmit queue is an extension of the edgeport Rx buffer.
  71. * The maximum amount of data buffered in both the edgeport
  72. * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
  73. */
  74. struct TxFifo {
  75. unsigned int head; /* index to head pointer (write) */
  76. unsigned int tail; /* index to tail pointer (read) */
  77. unsigned int count; /* Bytes in queue */
  78. unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
  79. unsigned char *fifo; /* allocated Buffer */
  80. };
  81. /* This structure holds all of the local port information */
  82. struct edgeport_port {
  83. __u16 txCredits; /* our current credits for this port */
  84. __u16 maxTxCredits; /* the max size of the port */
  85. struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
  86. struct urb *write_urb; /* write URB for this port */
  87. bool write_in_progress; /* 'true' while a write URB is outstanding */
  88. spinlock_t ep_lock;
  89. __u8 shadowLCR; /* last LCR value received */
  90. __u8 shadowMCR; /* last MCR value received */
  91. __u8 shadowMSR; /* last MSR value received */
  92. __u8 shadowLSR; /* last LSR value received */
  93. __u8 shadowXonChar; /* last value set as XON char in Edgeport */
  94. __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
  95. __u8 validDataMask;
  96. __u32 baudRate;
  97. bool open;
  98. bool openPending;
  99. bool commandPending;
  100. bool closePending;
  101. bool chaseResponsePending;
  102. wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
  103. wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
  104. wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
  105. wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
  106. struct async_icount icount;
  107. struct usb_serial_port *port; /* loop back to the owner of this object */
  108. };
  109. /* This structure holds all of the individual device information */
  110. struct edgeport_serial {
  111. char name[MAX_NAME_LEN+2]; /* string name of this device */
  112. struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
  113. struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
  114. struct edgeport_product_info product_info; /* Product Info */
  115. struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
  116. int is_epic; /* flag if EPiC device or not */
  117. __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
  118. unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
  119. struct urb *interrupt_read_urb; /* our interrupt urb */
  120. __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
  121. unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
  122. struct urb *read_urb; /* our bulk read urb */
  123. bool read_in_progress;
  124. spinlock_t es_lock;
  125. __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
  126. __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
  127. enum RXSTATE rxState; /* the current state of the bulk receive processor */
  128. __u8 rxHeader1; /* receive header byte 1 */
  129. __u8 rxHeader2; /* receive header byte 2 */
  130. __u8 rxHeader3; /* receive header byte 3 */
  131. __u8 rxPort; /* the port that we are currently receiving data for */
  132. __u8 rxStatusCode; /* the receive status code */
  133. __u8 rxStatusParam; /* the receive status paramater */
  134. __s16 rxBytesRemaining; /* the number of port bytes left to read */
  135. struct usb_serial *serial; /* loop back to the owner of this object */
  136. };
  137. /* baud rate information */
  138. struct divisor_table_entry {
  139. __u32 BaudRate;
  140. __u16 Divisor;
  141. };
  142. /*
  143. * Define table of divisors for Rev A EdgePort/4 hardware
  144. * These assume a 3.6864MHz crystal, the standard /16, and
  145. * MCR.7 = 0.
  146. */
  147. static const struct divisor_table_entry divisor_table[] = {
  148. { 50, 4608},
  149. { 75, 3072},
  150. { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
  151. { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
  152. { 150, 1536},
  153. { 300, 768},
  154. { 600, 384},
  155. { 1200, 192},
  156. { 1800, 128},
  157. { 2400, 96},
  158. { 4800, 48},
  159. { 7200, 32},
  160. { 9600, 24},
  161. { 14400, 16},
  162. { 19200, 12},
  163. { 38400, 6},
  164. { 57600, 4},
  165. { 115200, 2},
  166. { 230400, 1},
  167. };
  168. /* local variables */
  169. static bool debug;
  170. /* Number of outstanding Command Write Urbs */
  171. static atomic_t CmdUrbs = ATOMIC_INIT(0);
  172. /* local function prototypes */
  173. /* function prototypes for all URB callbacks */
  174. static void edge_interrupt_callback(struct urb *urb);
  175. static void edge_bulk_in_callback(struct urb *urb);
  176. static void edge_bulk_out_data_callback(struct urb *urb);
  177. static void edge_bulk_out_cmd_callback(struct urb *urb);
  178. /* function prototypes for the usbserial callbacks */
  179. static int edge_open(struct tty_struct *tty, struct usb_serial_port *port);
  180. static void edge_close(struct usb_serial_port *port);
  181. static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
  182. const unsigned char *buf, int count);
  183. static int edge_write_room(struct tty_struct *tty);
  184. static int edge_chars_in_buffer(struct tty_struct *tty);
  185. static void edge_throttle(struct tty_struct *tty);
  186. static void edge_unthrottle(struct tty_struct *tty);
  187. static void edge_set_termios(struct tty_struct *tty,
  188. struct usb_serial_port *port,
  189. struct ktermios *old_termios);
  190. static int edge_ioctl(struct tty_struct *tty,
  191. unsigned int cmd, unsigned long arg);
  192. static void edge_break(struct tty_struct *tty, int break_state);
  193. static int edge_tiocmget(struct tty_struct *tty);
  194. static int edge_tiocmset(struct tty_struct *tty,
  195. unsigned int set, unsigned int clear);
  196. static int edge_get_icount(struct tty_struct *tty,
  197. struct serial_icounter_struct *icount);
  198. static int edge_startup(struct usb_serial *serial);
  199. static void edge_disconnect(struct usb_serial *serial);
  200. static void edge_release(struct usb_serial *serial);
  201. #include "io_tables.h" /* all of the devices that this driver supports */
  202. /* function prototypes for all of our local functions */
  203. static void process_rcvd_data(struct edgeport_serial *edge_serial,
  204. unsigned char *buffer, __u16 bufferLength);
  205. static void process_rcvd_status(struct edgeport_serial *edge_serial,
  206. __u8 byte2, __u8 byte3);
  207. static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
  208. unsigned char *data, int length);
  209. static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
  210. static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
  211. __u8 lsr, __u8 data);
  212. static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
  213. __u8 param);
  214. static int calc_baud_rate_divisor(struct device *dev, int baud_rate, int *divisor);
  215. static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
  216. int baudRate);
  217. static void change_port_settings(struct tty_struct *tty,
  218. struct edgeport_port *edge_port,
  219. struct ktermios *old_termios);
  220. static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
  221. __u8 regNum, __u8 regValue);
  222. static int write_cmd_usb(struct edgeport_port *edge_port,
  223. unsigned char *buffer, int writeLength);
  224. static void send_more_port_data(struct edgeport_serial *edge_serial,
  225. struct edgeport_port *edge_port);
  226. static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  227. __u16 length, const __u8 *data);
  228. static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  229. __u16 length, __u8 *data);
  230. static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  231. __u16 length, const __u8 *data);
  232. static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
  233. static void get_boot_desc(struct edgeport_serial *edge_serial);
  234. static void load_application_firmware(struct edgeport_serial *edge_serial);
  235. static void unicode_to_ascii(char *string, int buflen,
  236. __le16 *unicode, int unicode_size);
  237. /* ************************************************************************ */
  238. /* ************************************************************************ */
  239. /* ************************************************************************ */
  240. /* ************************************************************************ */
  241. /************************************************************************
  242. * *
  243. * update_edgeport_E2PROM() Compare current versions of *
  244. * Boot ROM and Manufacture *
  245. * Descriptors with versions *
  246. * embedded in this driver *
  247. * *
  248. ************************************************************************/
  249. static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
  250. {
  251. struct device *dev = &edge_serial->serial->dev->dev;
  252. __u32 BootCurVer;
  253. __u32 BootNewVer;
  254. __u8 BootMajorVersion;
  255. __u8 BootMinorVersion;
  256. __u16 BootBuildNumber;
  257. __u32 Bootaddr;
  258. const struct ihex_binrec *rec;
  259. const struct firmware *fw;
  260. const char *fw_name;
  261. int response;
  262. switch (edge_serial->product_info.iDownloadFile) {
  263. case EDGE_DOWNLOAD_FILE_I930:
  264. fw_name = "edgeport/boot.fw";
  265. break;
  266. case EDGE_DOWNLOAD_FILE_80251:
  267. fw_name = "edgeport/boot2.fw";
  268. break;
  269. default:
  270. return;
  271. }
  272. response = request_ihex_firmware(&fw, fw_name,
  273. &edge_serial->serial->dev->dev);
  274. if (response) {
  275. dev_err(dev, "Failed to load image \"%s\" err %d\n",
  276. fw_name, response);
  277. return;
  278. }
  279. rec = (const struct ihex_binrec *)fw->data;
  280. BootMajorVersion = rec->data[0];
  281. BootMinorVersion = rec->data[1];
  282. BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
  283. /* Check Boot Image Version */
  284. BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
  285. (edge_serial->boot_descriptor.MinorVersion << 16) +
  286. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
  287. BootNewVer = (BootMajorVersion << 24) +
  288. (BootMinorVersion << 16) +
  289. BootBuildNumber;
  290. dev_dbg(dev, "Current Boot Image version %d.%d.%d\n",
  291. edge_serial->boot_descriptor.MajorVersion,
  292. edge_serial->boot_descriptor.MinorVersion,
  293. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
  294. if (BootNewVer > BootCurVer) {
  295. dev_dbg(dev, "**Update Boot Image from %d.%d.%d to %d.%d.%d\n",
  296. edge_serial->boot_descriptor.MajorVersion,
  297. edge_serial->boot_descriptor.MinorVersion,
  298. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
  299. BootMajorVersion, BootMinorVersion, BootBuildNumber);
  300. dev_dbg(dev, "Downloading new Boot Image\n");
  301. for (rec = ihex_next_binrec(rec); rec;
  302. rec = ihex_next_binrec(rec)) {
  303. Bootaddr = be32_to_cpu(rec->addr);
  304. response = rom_write(edge_serial->serial,
  305. Bootaddr >> 16,
  306. Bootaddr & 0xFFFF,
  307. be16_to_cpu(rec->len),
  308. &rec->data[0]);
  309. if (response < 0) {
  310. dev_err(&edge_serial->serial->dev->dev,
  311. "rom_write failed (%x, %x, %d)\n",
  312. Bootaddr >> 16, Bootaddr & 0xFFFF,
  313. be16_to_cpu(rec->len));
  314. break;
  315. }
  316. }
  317. } else {
  318. dev_dbg(dev, "Boot Image -- already up to date\n");
  319. }
  320. release_firmware(fw);
  321. }
  322. #if 0
  323. /************************************************************************
  324. *
  325. * Get string descriptor from device
  326. *
  327. ************************************************************************/
  328. static int get_string_desc(struct usb_device *dev, int Id,
  329. struct usb_string_descriptor **pRetDesc)
  330. {
  331. struct usb_string_descriptor StringDesc;
  332. struct usb_string_descriptor *pStringDesc;
  333. dev_dbg(&dev->dev, "%s - USB String ID = %d\n", __func__, Id);
  334. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
  335. sizeof(StringDesc)))
  336. return 0;
  337. pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
  338. if (!pStringDesc)
  339. return -1;
  340. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
  341. StringDesc.bLength)) {
  342. kfree(pStringDesc);
  343. return -1;
  344. }
  345. *pRetDesc = pStringDesc;
  346. return 0;
  347. }
  348. #endif
  349. static void dump_product_info(struct edgeport_serial *edge_serial,
  350. struct edgeport_product_info *product_info)
  351. {
  352. struct device *dev = &edge_serial->serial->dev->dev;
  353. /* Dump Product Info structure */
  354. dev_dbg(dev, "**Product Information:\n");
  355. dev_dbg(dev, " ProductId %x\n", product_info->ProductId);
  356. dev_dbg(dev, " NumPorts %d\n", product_info->NumPorts);
  357. dev_dbg(dev, " ProdInfoVer %d\n", product_info->ProdInfoVer);
  358. dev_dbg(dev, " IsServer %d\n", product_info->IsServer);
  359. dev_dbg(dev, " IsRS232 %d\n", product_info->IsRS232);
  360. dev_dbg(dev, " IsRS422 %d\n", product_info->IsRS422);
  361. dev_dbg(dev, " IsRS485 %d\n", product_info->IsRS485);
  362. dev_dbg(dev, " RomSize %d\n", product_info->RomSize);
  363. dev_dbg(dev, " RamSize %d\n", product_info->RamSize);
  364. dev_dbg(dev, " CpuRev %x\n", product_info->CpuRev);
  365. dev_dbg(dev, " BoardRev %x\n", product_info->BoardRev);
  366. dev_dbg(dev, " BootMajorVersion %d.%d.%d\n",
  367. product_info->BootMajorVersion,
  368. product_info->BootMinorVersion,
  369. le16_to_cpu(product_info->BootBuildNumber));
  370. dev_dbg(dev, " FirmwareMajorVersion %d.%d.%d\n",
  371. product_info->FirmwareMajorVersion,
  372. product_info->FirmwareMinorVersion,
  373. le16_to_cpu(product_info->FirmwareBuildNumber));
  374. dev_dbg(dev, " ManufactureDescDate %d/%d/%d\n",
  375. product_info->ManufactureDescDate[0],
  376. product_info->ManufactureDescDate[1],
  377. product_info->ManufactureDescDate[2]+1900);
  378. dev_dbg(dev, " iDownloadFile 0x%x\n",
  379. product_info->iDownloadFile);
  380. dev_dbg(dev, " EpicVer %d\n", product_info->EpicVer);
  381. }
  382. static void get_product_info(struct edgeport_serial *edge_serial)
  383. {
  384. struct edgeport_product_info *product_info = &edge_serial->product_info;
  385. memset(product_info, 0, sizeof(struct edgeport_product_info));
  386. product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
  387. product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
  388. product_info->ProdInfoVer = 0;
  389. product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
  390. product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
  391. product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
  392. product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
  393. product_info->BootMajorVersion =
  394. edge_serial->boot_descriptor.MajorVersion;
  395. product_info->BootMinorVersion =
  396. edge_serial->boot_descriptor.MinorVersion;
  397. product_info->BootBuildNumber =
  398. edge_serial->boot_descriptor.BuildNumber;
  399. memcpy(product_info->ManufactureDescDate,
  400. edge_serial->manuf_descriptor.DescDate,
  401. sizeof(edge_serial->manuf_descriptor.DescDate));
  402. /* check if this is 2nd generation hardware */
  403. if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
  404. & ION_DEVICE_ID_80251_NETCHIP)
  405. product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
  406. else
  407. product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
  408. /* Determine Product type and set appropriate flags */
  409. switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
  410. case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
  411. case ION_DEVICE_ID_EDGEPORT_4T:
  412. case ION_DEVICE_ID_EDGEPORT_4:
  413. case ION_DEVICE_ID_EDGEPORT_2:
  414. case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
  415. case ION_DEVICE_ID_EDGEPORT_8:
  416. case ION_DEVICE_ID_EDGEPORT_421:
  417. case ION_DEVICE_ID_EDGEPORT_21:
  418. case ION_DEVICE_ID_EDGEPORT_2_DIN:
  419. case ION_DEVICE_ID_EDGEPORT_4_DIN:
  420. case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
  421. product_info->IsRS232 = 1;
  422. break;
  423. case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
  424. product_info->IsRS422 = 1;
  425. product_info->IsRS485 = 1;
  426. break;
  427. case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
  428. case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
  429. product_info->IsRS422 = 1;
  430. break;
  431. }
  432. dump_product_info(edge_serial, product_info);
  433. }
  434. static int get_epic_descriptor(struct edgeport_serial *ep)
  435. {
  436. int result;
  437. struct usb_serial *serial = ep->serial;
  438. struct edgeport_product_info *product_info = &ep->product_info;
  439. struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
  440. struct edge_compatibility_bits *bits;
  441. struct device *dev = &serial->dev->dev;
  442. ep->is_epic = 0;
  443. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  444. USB_REQUEST_ION_GET_EPIC_DESC,
  445. 0xC0, 0x00, 0x00,
  446. &ep->epic_descriptor,
  447. sizeof(struct edge_compatibility_descriptor),
  448. 300);
  449. if (result > 0) {
  450. ep->is_epic = 1;
  451. memset(product_info, 0, sizeof(struct edgeport_product_info));
  452. product_info->NumPorts = epic->NumPorts;
  453. product_info->ProdInfoVer = 0;
  454. product_info->FirmwareMajorVersion = epic->MajorVersion;
  455. product_info->FirmwareMinorVersion = epic->MinorVersion;
  456. product_info->FirmwareBuildNumber = epic->BuildNumber;
  457. product_info->iDownloadFile = epic->iDownloadFile;
  458. product_info->EpicVer = epic->EpicVer;
  459. product_info->Epic = epic->Supports;
  460. product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
  461. dump_product_info(ep, product_info);
  462. bits = &ep->epic_descriptor.Supports;
  463. dev_dbg(dev, "**EPIC descriptor:\n");
  464. dev_dbg(dev, " VendEnableSuspend: %s\n", bits->VendEnableSuspend ? "TRUE": "FALSE");
  465. dev_dbg(dev, " IOSPOpen : %s\n", bits->IOSPOpen ? "TRUE": "FALSE");
  466. dev_dbg(dev, " IOSPClose : %s\n", bits->IOSPClose ? "TRUE": "FALSE");
  467. dev_dbg(dev, " IOSPChase : %s\n", bits->IOSPChase ? "TRUE": "FALSE");
  468. dev_dbg(dev, " IOSPSetRxFlow : %s\n", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
  469. dev_dbg(dev, " IOSPSetTxFlow : %s\n", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
  470. dev_dbg(dev, " IOSPSetXChar : %s\n", bits->IOSPSetXChar ? "TRUE": "FALSE");
  471. dev_dbg(dev, " IOSPRxCheck : %s\n", bits->IOSPRxCheck ? "TRUE": "FALSE");
  472. dev_dbg(dev, " IOSPSetClrBreak : %s\n", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
  473. dev_dbg(dev, " IOSPWriteMCR : %s\n", bits->IOSPWriteMCR ? "TRUE": "FALSE");
  474. dev_dbg(dev, " IOSPWriteLCR : %s\n", bits->IOSPWriteLCR ? "TRUE": "FALSE");
  475. dev_dbg(dev, " IOSPSetBaudRate : %s\n", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
  476. dev_dbg(dev, " TrueEdgeport : %s\n", bits->TrueEdgeport ? "TRUE": "FALSE");
  477. }
  478. return result;
  479. }
  480. /************************************************************************/
  481. /************************************************************************/
  482. /* U S B C A L L B A C K F U N C T I O N S */
  483. /* U S B C A L L B A C K F U N C T I O N S */
  484. /************************************************************************/
  485. /************************************************************************/
  486. /*****************************************************************************
  487. * edge_interrupt_callback
  488. * this is the callback function for when we have received data on the
  489. * interrupt endpoint.
  490. *****************************************************************************/
  491. static void edge_interrupt_callback(struct urb *urb)
  492. {
  493. struct edgeport_serial *edge_serial = urb->context;
  494. struct device *dev;
  495. struct edgeport_port *edge_port;
  496. struct usb_serial_port *port;
  497. struct tty_struct *tty;
  498. unsigned char *data = urb->transfer_buffer;
  499. int length = urb->actual_length;
  500. int bytes_avail;
  501. int position;
  502. int txCredits;
  503. int portNumber;
  504. int result;
  505. int status = urb->status;
  506. switch (status) {
  507. case 0:
  508. /* success */
  509. break;
  510. case -ECONNRESET:
  511. case -ENOENT:
  512. case -ESHUTDOWN:
  513. /* this urb is terminated, clean up */
  514. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
  515. return;
  516. default:
  517. dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
  518. goto exit;
  519. }
  520. dev = &edge_serial->serial->dev->dev;
  521. /* process this interrupt-read even if there are no ports open */
  522. if (length) {
  523. usb_serial_debug_data(dev, __func__, length, data);
  524. if (length > 1) {
  525. bytes_avail = data[0] | (data[1] << 8);
  526. if (bytes_avail) {
  527. spin_lock(&edge_serial->es_lock);
  528. edge_serial->rxBytesAvail += bytes_avail;
  529. dev_dbg(dev,
  530. "%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d\n",
  531. __func__, bytes_avail,
  532. edge_serial->rxBytesAvail,
  533. edge_serial->read_in_progress);
  534. if (edge_serial->rxBytesAvail > 0 &&
  535. !edge_serial->read_in_progress) {
  536. dev_dbg(dev, "%s - posting a read\n", __func__);
  537. edge_serial->read_in_progress = true;
  538. /* we have pending bytes on the
  539. bulk in pipe, send a request */
  540. result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
  541. if (result) {
  542. dev_err(dev,
  543. "%s - usb_submit_urb(read bulk) failed with result = %d\n",
  544. __func__, result);
  545. edge_serial->read_in_progress = false;
  546. }
  547. }
  548. spin_unlock(&edge_serial->es_lock);
  549. }
  550. }
  551. /* grab the txcredits for the ports if available */
  552. position = 2;
  553. portNumber = 0;
  554. while ((position < length) &&
  555. (portNumber < edge_serial->serial->num_ports)) {
  556. txCredits = data[position] | (data[position+1] << 8);
  557. if (txCredits) {
  558. port = edge_serial->serial->port[portNumber];
  559. edge_port = usb_get_serial_port_data(port);
  560. if (edge_port->open) {
  561. spin_lock(&edge_port->ep_lock);
  562. edge_port->txCredits += txCredits;
  563. spin_unlock(&edge_port->ep_lock);
  564. dev_dbg(dev, "%s - txcredits for port%d = %d\n",
  565. __func__, portNumber,
  566. edge_port->txCredits);
  567. /* tell the tty driver that something
  568. has changed */
  569. tty = tty_port_tty_get(
  570. &edge_port->port->port);
  571. if (tty) {
  572. tty_wakeup(tty);
  573. tty_kref_put(tty);
  574. }
  575. /* Since we have more credit, check
  576. if more data can be sent */
  577. send_more_port_data(edge_serial,
  578. edge_port);
  579. }
  580. }
  581. position += 2;
  582. ++portNumber;
  583. }
  584. }
  585. exit:
  586. result = usb_submit_urb(urb, GFP_ATOMIC);
  587. if (result)
  588. dev_err(&urb->dev->dev,
  589. "%s - Error %d submitting control urb\n",
  590. __func__, result);
  591. }
  592. /*****************************************************************************
  593. * edge_bulk_in_callback
  594. * this is the callback function for when we have received data on the
  595. * bulk in endpoint.
  596. *****************************************************************************/
  597. static void edge_bulk_in_callback(struct urb *urb)
  598. {
  599. struct edgeport_serial *edge_serial = urb->context;
  600. struct device *dev;
  601. unsigned char *data = urb->transfer_buffer;
  602. int retval;
  603. __u16 raw_data_length;
  604. int status = urb->status;
  605. if (status) {
  606. dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
  607. __func__, status);
  608. edge_serial->read_in_progress = false;
  609. return;
  610. }
  611. if (urb->actual_length == 0) {
  612. dev_dbg(&urb->dev->dev, "%s - read bulk callback with no data\n", __func__);
  613. edge_serial->read_in_progress = false;
  614. return;
  615. }
  616. dev = &edge_serial->serial->dev->dev;
  617. raw_data_length = urb->actual_length;
  618. usb_serial_debug_data(dev, __func__, raw_data_length, data);
  619. spin_lock(&edge_serial->es_lock);
  620. /* decrement our rxBytes available by the number that we just got */
  621. edge_serial->rxBytesAvail -= raw_data_length;
  622. dev_dbg(dev, "%s - Received = %d, rxBytesAvail %d\n", __func__,
  623. raw_data_length, edge_serial->rxBytesAvail);
  624. process_rcvd_data(edge_serial, data, urb->actual_length);
  625. /* check to see if there's any more data for us to read */
  626. if (edge_serial->rxBytesAvail > 0) {
  627. dev_dbg(dev, "%s - posting a read\n", __func__);
  628. retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
  629. if (retval) {
  630. dev_err(dev,
  631. "%s - usb_submit_urb(read bulk) failed, retval = %d\n",
  632. __func__, retval);
  633. edge_serial->read_in_progress = false;
  634. }
  635. } else {
  636. edge_serial->read_in_progress = false;
  637. }
  638. spin_unlock(&edge_serial->es_lock);
  639. }
  640. /*****************************************************************************
  641. * edge_bulk_out_data_callback
  642. * this is the callback function for when we have finished sending
  643. * serial data on the bulk out endpoint.
  644. *****************************************************************************/
  645. static void edge_bulk_out_data_callback(struct urb *urb)
  646. {
  647. struct edgeport_port *edge_port = urb->context;
  648. struct tty_struct *tty;
  649. int status = urb->status;
  650. if (status) {
  651. dev_dbg(&urb->dev->dev,
  652. "%s - nonzero write bulk status received: %d\n",
  653. __func__, status);
  654. }
  655. tty = tty_port_tty_get(&edge_port->port->port);
  656. if (tty && edge_port->open) {
  657. /* let the tty driver wakeup if it has a special
  658. write_wakeup function */
  659. tty_wakeup(tty);
  660. }
  661. tty_kref_put(tty);
  662. /* Release the Write URB */
  663. edge_port->write_in_progress = false;
  664. /* Check if more data needs to be sent */
  665. send_more_port_data((struct edgeport_serial *)
  666. (usb_get_serial_data(edge_port->port->serial)), edge_port);
  667. }
  668. /*****************************************************************************
  669. * BulkOutCmdCallback
  670. * this is the callback function for when we have finished sending a
  671. * command on the bulk out endpoint.
  672. *****************************************************************************/
  673. static void edge_bulk_out_cmd_callback(struct urb *urb)
  674. {
  675. struct edgeport_port *edge_port = urb->context;
  676. struct tty_struct *tty;
  677. int status = urb->status;
  678. atomic_dec(&CmdUrbs);
  679. dev_dbg(&urb->dev->dev, "%s - FREE URB %p (outstanding %d)\n",
  680. __func__, urb, atomic_read(&CmdUrbs));
  681. /* clean up the transfer buffer */
  682. kfree(urb->transfer_buffer);
  683. /* Free the command urb */
  684. usb_free_urb(urb);
  685. if (status) {
  686. dev_dbg(&urb->dev->dev,
  687. "%s - nonzero write bulk status received: %d\n",
  688. __func__, status);
  689. return;
  690. }
  691. /* Get pointer to tty */
  692. tty = tty_port_tty_get(&edge_port->port->port);
  693. /* tell the tty driver that something has changed */
  694. if (tty && edge_port->open)
  695. tty_wakeup(tty);
  696. tty_kref_put(tty);
  697. /* we have completed the command */
  698. edge_port->commandPending = false;
  699. wake_up(&edge_port->wait_command);
  700. }
  701. /*****************************************************************************
  702. * Driver tty interface functions
  703. *****************************************************************************/
  704. /*****************************************************************************
  705. * SerialOpen
  706. * this function is called by the tty driver when a port is opened
  707. * If successful, we return 0
  708. * Otherwise we return a negative error number.
  709. *****************************************************************************/
  710. static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
  711. {
  712. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  713. struct device *dev = &port->dev;
  714. struct usb_serial *serial;
  715. struct edgeport_serial *edge_serial;
  716. int response;
  717. if (edge_port == NULL)
  718. return -ENODEV;
  719. /* see if we've set up our endpoint info yet (can't set it up
  720. in edge_startup as the structures were not set up at that time.) */
  721. serial = port->serial;
  722. edge_serial = usb_get_serial_data(serial);
  723. if (edge_serial == NULL)
  724. return -ENODEV;
  725. if (edge_serial->interrupt_in_buffer == NULL) {
  726. struct usb_serial_port *port0 = serial->port[0];
  727. /* not set up yet, so do it now */
  728. edge_serial->interrupt_in_buffer =
  729. port0->interrupt_in_buffer;
  730. edge_serial->interrupt_in_endpoint =
  731. port0->interrupt_in_endpointAddress;
  732. edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
  733. edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
  734. edge_serial->bulk_in_endpoint =
  735. port0->bulk_in_endpointAddress;
  736. edge_serial->read_urb = port0->read_urb;
  737. edge_serial->bulk_out_endpoint =
  738. port0->bulk_out_endpointAddress;
  739. /* set up our interrupt urb */
  740. usb_fill_int_urb(edge_serial->interrupt_read_urb,
  741. serial->dev,
  742. usb_rcvintpipe(serial->dev,
  743. port0->interrupt_in_endpointAddress),
  744. port0->interrupt_in_buffer,
  745. edge_serial->interrupt_read_urb->transfer_buffer_length,
  746. edge_interrupt_callback, edge_serial,
  747. edge_serial->interrupt_read_urb->interval);
  748. /* set up our bulk in urb */
  749. usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
  750. usb_rcvbulkpipe(serial->dev,
  751. port0->bulk_in_endpointAddress),
  752. port0->bulk_in_buffer,
  753. edge_serial->read_urb->transfer_buffer_length,
  754. edge_bulk_in_callback, edge_serial);
  755. edge_serial->read_in_progress = false;
  756. /* start interrupt read for this edgeport
  757. * this interrupt will continue as long
  758. * as the edgeport is connected */
  759. response = usb_submit_urb(edge_serial->interrupt_read_urb,
  760. GFP_KERNEL);
  761. if (response) {
  762. dev_err(dev, "%s - Error %d submitting control urb\n",
  763. __func__, response);
  764. }
  765. }
  766. /* initialize our wait queues */
  767. init_waitqueue_head(&edge_port->wait_open);
  768. init_waitqueue_head(&edge_port->wait_chase);
  769. init_waitqueue_head(&edge_port->delta_msr_wait);
  770. init_waitqueue_head(&edge_port->wait_command);
  771. /* initialize our icount structure */
  772. memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
  773. /* initialize our port settings */
  774. edge_port->txCredits = 0; /* Can't send any data yet */
  775. /* Must always set this bit to enable ints! */
  776. edge_port->shadowMCR = MCR_MASTER_IE;
  777. edge_port->chaseResponsePending = false;
  778. /* send a open port command */
  779. edge_port->openPending = true;
  780. edge_port->open = false;
  781. response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
  782. if (response < 0) {
  783. dev_err(dev, "%s - error sending open port command\n", __func__);
  784. edge_port->openPending = false;
  785. return -ENODEV;
  786. }
  787. /* now wait for the port to be completely opened */
  788. wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
  789. OPEN_TIMEOUT);
  790. if (!edge_port->open) {
  791. /* open timed out */
  792. dev_dbg(dev, "%s - open timedout\n", __func__);
  793. edge_port->openPending = false;
  794. return -ENODEV;
  795. }
  796. /* create the txfifo */
  797. edge_port->txfifo.head = 0;
  798. edge_port->txfifo.tail = 0;
  799. edge_port->txfifo.count = 0;
  800. edge_port->txfifo.size = edge_port->maxTxCredits;
  801. edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
  802. if (!edge_port->txfifo.fifo) {
  803. dev_dbg(dev, "%s - no memory\n", __func__);
  804. edge_close(port);
  805. return -ENOMEM;
  806. }
  807. /* Allocate a URB for the write */
  808. edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  809. edge_port->write_in_progress = false;
  810. if (!edge_port->write_urb) {
  811. dev_dbg(dev, "%s - no memory\n", __func__);
  812. edge_close(port);
  813. return -ENOMEM;
  814. }
  815. dev_dbg(dev, "%s(%d) - Initialize TX fifo to %d bytes\n",
  816. __func__, port->number, edge_port->maxTxCredits);
  817. return 0;
  818. }
  819. /************************************************************************
  820. *
  821. * block_until_chase_response
  822. *
  823. * This function will block the close until one of the following:
  824. * 1. Response to our Chase comes from Edgeport
  825. * 2. A timeout of 10 seconds without activity has expired
  826. * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
  827. *
  828. ************************************************************************/
  829. static void block_until_chase_response(struct edgeport_port *edge_port)
  830. {
  831. struct device *dev = &edge_port->port->dev;
  832. DEFINE_WAIT(wait);
  833. __u16 lastCredits;
  834. int timeout = 1*HZ;
  835. int loop = 10;
  836. while (1) {
  837. /* Save Last credits */
  838. lastCredits = edge_port->txCredits;
  839. /* Did we get our Chase response */
  840. if (!edge_port->chaseResponsePending) {
  841. dev_dbg(dev, "%s - Got Chase Response\n", __func__);
  842. /* did we get all of our credit back? */
  843. if (edge_port->txCredits == edge_port->maxTxCredits) {
  844. dev_dbg(dev, "%s - Got all credits\n", __func__);
  845. return;
  846. }
  847. }
  848. /* Block the thread for a while */
  849. prepare_to_wait(&edge_port->wait_chase, &wait,
  850. TASK_UNINTERRUPTIBLE);
  851. schedule_timeout(timeout);
  852. finish_wait(&edge_port->wait_chase, &wait);
  853. if (lastCredits == edge_port->txCredits) {
  854. /* No activity.. count down. */
  855. loop--;
  856. if (loop == 0) {
  857. edge_port->chaseResponsePending = false;
  858. dev_dbg(dev, "%s - Chase TIMEOUT\n", __func__);
  859. return;
  860. }
  861. } else {
  862. /* Reset timeout value back to 10 seconds */
  863. dev_dbg(dev, "%s - Last %d, Current %d\n", __func__,
  864. lastCredits, edge_port->txCredits);
  865. loop = 10;
  866. }
  867. }
  868. }
  869. /************************************************************************
  870. *
  871. * block_until_tx_empty
  872. *
  873. * This function will block the close until one of the following:
  874. * 1. TX count are 0
  875. * 2. The edgeport has stopped
  876. * 3. A timeout of 3 seconds without activity has expired
  877. *
  878. ************************************************************************/
  879. static void block_until_tx_empty(struct edgeport_port *edge_port)
  880. {
  881. struct device *dev = &edge_port->port->dev;
  882. DEFINE_WAIT(wait);
  883. struct TxFifo *fifo = &edge_port->txfifo;
  884. __u32 lastCount;
  885. int timeout = HZ/10;
  886. int loop = 30;
  887. while (1) {
  888. /* Save Last count */
  889. lastCount = fifo->count;
  890. /* Is the Edgeport Buffer empty? */
  891. if (lastCount == 0) {
  892. dev_dbg(dev, "%s - TX Buffer Empty\n", __func__);
  893. return;
  894. }
  895. /* Block the thread for a while */
  896. prepare_to_wait(&edge_port->wait_chase, &wait,
  897. TASK_UNINTERRUPTIBLE);
  898. schedule_timeout(timeout);
  899. finish_wait(&edge_port->wait_chase, &wait);
  900. dev_dbg(dev, "%s wait\n", __func__);
  901. if (lastCount == fifo->count) {
  902. /* No activity.. count down. */
  903. loop--;
  904. if (loop == 0) {
  905. dev_dbg(dev, "%s - TIMEOUT\n", __func__);
  906. return;
  907. }
  908. } else {
  909. /* Reset timeout value back to seconds */
  910. loop = 30;
  911. }
  912. }
  913. }
  914. /*****************************************************************************
  915. * edge_close
  916. * this function is called by the tty driver when a port is closed
  917. *****************************************************************************/
  918. static void edge_close(struct usb_serial_port *port)
  919. {
  920. struct edgeport_serial *edge_serial;
  921. struct edgeport_port *edge_port;
  922. int status;
  923. edge_serial = usb_get_serial_data(port->serial);
  924. edge_port = usb_get_serial_port_data(port);
  925. if (edge_serial == NULL || edge_port == NULL)
  926. return;
  927. /* block until tx is empty */
  928. block_until_tx_empty(edge_port);
  929. edge_port->closePending = true;
  930. if ((!edge_serial->is_epic) ||
  931. ((edge_serial->is_epic) &&
  932. (edge_serial->epic_descriptor.Supports.IOSPChase))) {
  933. /* flush and chase */
  934. edge_port->chaseResponsePending = true;
  935. dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
  936. status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
  937. if (status == 0)
  938. /* block until chase finished */
  939. block_until_chase_response(edge_port);
  940. else
  941. edge_port->chaseResponsePending = false;
  942. }
  943. if ((!edge_serial->is_epic) ||
  944. ((edge_serial->is_epic) &&
  945. (edge_serial->epic_descriptor.Supports.IOSPClose))) {
  946. /* close the port */
  947. dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLOSE_PORT\n", __func__);
  948. send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
  949. }
  950. /* port->close = true; */
  951. edge_port->closePending = false;
  952. edge_port->open = false;
  953. edge_port->openPending = false;
  954. usb_kill_urb(edge_port->write_urb);
  955. if (edge_port->write_urb) {
  956. /* if this urb had a transfer buffer already
  957. (old transfer) free it */
  958. kfree(edge_port->write_urb->transfer_buffer);
  959. usb_free_urb(edge_port->write_urb);
  960. edge_port->write_urb = NULL;
  961. }
  962. kfree(edge_port->txfifo.fifo);
  963. edge_port->txfifo.fifo = NULL;
  964. }
  965. /*****************************************************************************
  966. * SerialWrite
  967. * this function is called by the tty driver when data should be written
  968. * to the port.
  969. * If successful, we return the number of bytes written, otherwise we
  970. * return a negative error number.
  971. *****************************************************************************/
  972. static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
  973. const unsigned char *data, int count)
  974. {
  975. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  976. struct TxFifo *fifo;
  977. int copySize;
  978. int bytesleft;
  979. int firsthalf;
  980. int secondhalf;
  981. unsigned long flags;
  982. if (edge_port == NULL)
  983. return -ENODEV;
  984. /* get a pointer to the Tx fifo */
  985. fifo = &edge_port->txfifo;
  986. spin_lock_irqsave(&edge_port->ep_lock, flags);
  987. /* calculate number of bytes to put in fifo */
  988. copySize = min((unsigned int)count,
  989. (edge_port->txCredits - fifo->count));
  990. dev_dbg(&port->dev, "%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes\n",
  991. __func__, port->number, count,
  992. edge_port->txCredits - fifo->count, copySize);
  993. /* catch writes of 0 bytes which the tty driver likes to give us,
  994. and when txCredits is empty */
  995. if (copySize == 0) {
  996. dev_dbg(&port->dev, "%s - copySize = Zero\n", __func__);
  997. goto finish_write;
  998. }
  999. /* queue the data
  1000. * since we can never overflow the buffer we do not have to check for a
  1001. * full condition
  1002. *
  1003. * the copy is done is two parts -- first fill to the end of the buffer
  1004. * then copy the reset from the start of the buffer
  1005. */
  1006. bytesleft = fifo->size - fifo->head;
  1007. firsthalf = min(bytesleft, copySize);
  1008. dev_dbg(&port->dev, "%s - copy %d bytes of %d into fifo \n", __func__,
  1009. firsthalf, bytesleft);
  1010. /* now copy our data */
  1011. memcpy(&fifo->fifo[fifo->head], data, firsthalf);
  1012. usb_serial_debug_data(&port->dev, __func__, firsthalf, &fifo->fifo[fifo->head]);
  1013. /* update the index and size */
  1014. fifo->head += firsthalf;
  1015. fifo->count += firsthalf;
  1016. /* wrap the index */
  1017. if (fifo->head == fifo->size)
  1018. fifo->head = 0;
  1019. secondhalf = copySize-firsthalf;
  1020. if (secondhalf) {
  1021. dev_dbg(&port->dev, "%s - copy rest of data %d\n", __func__, secondhalf);
  1022. memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
  1023. usb_serial_debug_data(&port->dev, __func__, secondhalf, &fifo->fifo[fifo->head]);
  1024. /* update the index and size */
  1025. fifo->count += secondhalf;
  1026. fifo->head += secondhalf;
  1027. /* No need to check for wrap since we can not get to end of
  1028. * the fifo in this part
  1029. */
  1030. }
  1031. finish_write:
  1032. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1033. send_more_port_data((struct edgeport_serial *)
  1034. usb_get_serial_data(port->serial), edge_port);
  1035. dev_dbg(&port->dev, "%s wrote %d byte(s) TxCredits %d, Fifo %d\n",
  1036. __func__, copySize, edge_port->txCredits, fifo->count);
  1037. return copySize;
  1038. }
  1039. /************************************************************************
  1040. *
  1041. * send_more_port_data()
  1042. *
  1043. * This routine attempts to write additional UART transmit data
  1044. * to a port over the USB bulk pipe. It is called (1) when new
  1045. * data has been written to a port's TxBuffer from higher layers
  1046. * (2) when the peripheral sends us additional TxCredits indicating
  1047. * that it can accept more Tx data for a given port; and (3) when
  1048. * a bulk write completes successfully and we want to see if we
  1049. * can transmit more.
  1050. *
  1051. ************************************************************************/
  1052. static void send_more_port_data(struct edgeport_serial *edge_serial,
  1053. struct edgeport_port *edge_port)
  1054. {
  1055. struct TxFifo *fifo = &edge_port->txfifo;
  1056. struct device *dev = &edge_port->port->dev;
  1057. struct urb *urb;
  1058. unsigned char *buffer;
  1059. int status;
  1060. int count;
  1061. int bytesleft;
  1062. int firsthalf;
  1063. int secondhalf;
  1064. unsigned long flags;
  1065. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1066. if (edge_port->write_in_progress ||
  1067. !edge_port->open ||
  1068. (fifo->count == 0)) {
  1069. dev_dbg(dev, "%s(%d) EXIT - fifo %d, PendingWrite = %d\n",
  1070. __func__, edge_port->port->number,
  1071. fifo->count, edge_port->write_in_progress);
  1072. goto exit_send;
  1073. }
  1074. /* since the amount of data in the fifo will always fit into the
  1075. * edgeport buffer we do not need to check the write length
  1076. *
  1077. * Do we have enough credits for this port to make it worthwhile
  1078. * to bother queueing a write. If it's too small, say a few bytes,
  1079. * it's better to wait for more credits so we can do a larger write.
  1080. */
  1081. if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
  1082. dev_dbg(dev, "%s(%d) Not enough credit - fifo %d TxCredit %d\n",
  1083. __func__, edge_port->port->number, fifo->count,
  1084. edge_port->txCredits);
  1085. goto exit_send;
  1086. }
  1087. /* lock this write */
  1088. edge_port->write_in_progress = true;
  1089. /* get a pointer to the write_urb */
  1090. urb = edge_port->write_urb;
  1091. /* make sure transfer buffer is freed */
  1092. kfree(urb->transfer_buffer);
  1093. urb->transfer_buffer = NULL;
  1094. /* build the data header for the buffer and port that we are about
  1095. to send out */
  1096. count = fifo->count;
  1097. buffer = kmalloc(count+2, GFP_ATOMIC);
  1098. if (buffer == NULL) {
  1099. dev_err_console(edge_port->port,
  1100. "%s - no more kernel memory...\n", __func__);
  1101. edge_port->write_in_progress = false;
  1102. goto exit_send;
  1103. }
  1104. buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
  1105. - edge_port->port->serial->minor, count);
  1106. buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
  1107. - edge_port->port->serial->minor, count);
  1108. /* now copy our data */
  1109. bytesleft = fifo->size - fifo->tail;
  1110. firsthalf = min(bytesleft, count);
  1111. memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
  1112. fifo->tail += firsthalf;
  1113. fifo->count -= firsthalf;
  1114. if (fifo->tail == fifo->size)
  1115. fifo->tail = 0;
  1116. secondhalf = count-firsthalf;
  1117. if (secondhalf) {
  1118. memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
  1119. secondhalf);
  1120. fifo->tail += secondhalf;
  1121. fifo->count -= secondhalf;
  1122. }
  1123. if (count)
  1124. usb_serial_debug_data(&edge_port->port->dev, __func__, count, &buffer[2]);
  1125. /* fill up the urb with all of our data and submit it */
  1126. usb_fill_bulk_urb(urb, edge_serial->serial->dev,
  1127. usb_sndbulkpipe(edge_serial->serial->dev,
  1128. edge_serial->bulk_out_endpoint),
  1129. buffer, count+2,
  1130. edge_bulk_out_data_callback, edge_port);
  1131. /* decrement the number of credits we have by the number we just sent */
  1132. edge_port->txCredits -= count;
  1133. edge_port->icount.tx += count;
  1134. status = usb_submit_urb(urb, GFP_ATOMIC);
  1135. if (status) {
  1136. /* something went wrong */
  1137. dev_err_console(edge_port->port,
  1138. "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
  1139. __func__, status);
  1140. edge_port->write_in_progress = false;
  1141. /* revert the credits as something bad happened. */
  1142. edge_port->txCredits += count;
  1143. edge_port->icount.tx -= count;
  1144. }
  1145. dev_dbg(dev, "%s wrote %d byte(s) TxCredit %d, Fifo %d\n",
  1146. __func__, count, edge_port->txCredits, fifo->count);
  1147. exit_send:
  1148. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1149. }
  1150. /*****************************************************************************
  1151. * edge_write_room
  1152. * this function is called by the tty driver when it wants to know how
  1153. * many bytes of data we can accept for a specific port. If successful,
  1154. * we return the amount of room that we have for this port (the txCredits)
  1155. * otherwise we return a negative error number.
  1156. *****************************************************************************/
  1157. static int edge_write_room(struct tty_struct *tty)
  1158. {
  1159. struct usb_serial_port *port = tty->driver_data;
  1160. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1161. int room;
  1162. unsigned long flags;
  1163. if (edge_port == NULL)
  1164. return 0;
  1165. if (edge_port->closePending)
  1166. return 0;
  1167. if (!edge_port->open) {
  1168. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1169. return 0;
  1170. }
  1171. /* total of both buffers is still txCredit */
  1172. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1173. room = edge_port->txCredits - edge_port->txfifo.count;
  1174. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1175. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  1176. return room;
  1177. }
  1178. /*****************************************************************************
  1179. * edge_chars_in_buffer
  1180. * this function is called by the tty driver when it wants to know how
  1181. * many bytes of data we currently have outstanding in the port (data that
  1182. * has been written, but hasn't made it out the port yet)
  1183. * If successful, we return the number of bytes left to be written in the
  1184. * system,
  1185. * Otherwise we return a negative error number.
  1186. *****************************************************************************/
  1187. static int edge_chars_in_buffer(struct tty_struct *tty)
  1188. {
  1189. struct usb_serial_port *port = tty->driver_data;
  1190. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1191. int num_chars;
  1192. unsigned long flags;
  1193. if (edge_port == NULL)
  1194. return 0;
  1195. if (edge_port->closePending)
  1196. return 0;
  1197. if (!edge_port->open) {
  1198. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1199. return 0;
  1200. }
  1201. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1202. num_chars = edge_port->maxTxCredits - edge_port->txCredits +
  1203. edge_port->txfifo.count;
  1204. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1205. if (num_chars) {
  1206. dev_dbg(&port->dev, "%s(port %d) - returns %d\n", __func__,
  1207. port->number, num_chars);
  1208. }
  1209. return num_chars;
  1210. }
  1211. /*****************************************************************************
  1212. * SerialThrottle
  1213. * this function is called by the tty driver when it wants to stop the data
  1214. * being read from the port.
  1215. *****************************************************************************/
  1216. static void edge_throttle(struct tty_struct *tty)
  1217. {
  1218. struct usb_serial_port *port = tty->driver_data;
  1219. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1220. int status;
  1221. if (edge_port == NULL)
  1222. return;
  1223. if (!edge_port->open) {
  1224. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1225. return;
  1226. }
  1227. /* if we are implementing XON/XOFF, send the stop character */
  1228. if (I_IXOFF(tty)) {
  1229. unsigned char stop_char = STOP_CHAR(tty);
  1230. status = edge_write(tty, port, &stop_char, 1);
  1231. if (status <= 0)
  1232. return;
  1233. }
  1234. /* if we are implementing RTS/CTS, toggle that line */
  1235. if (tty->termios->c_cflag & CRTSCTS) {
  1236. edge_port->shadowMCR &= ~MCR_RTS;
  1237. status = send_cmd_write_uart_register(edge_port, MCR,
  1238. edge_port->shadowMCR);
  1239. if (status != 0)
  1240. return;
  1241. }
  1242. }
  1243. /*****************************************************************************
  1244. * edge_unthrottle
  1245. * this function is called by the tty driver when it wants to resume the
  1246. * data being read from the port (called after SerialThrottle is called)
  1247. *****************************************************************************/
  1248. static void edge_unthrottle(struct tty_struct *tty)
  1249. {
  1250. struct usb_serial_port *port = tty->driver_data;
  1251. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1252. int status;
  1253. if (edge_port == NULL)
  1254. return;
  1255. if (!edge_port->open) {
  1256. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1257. return;
  1258. }
  1259. /* if we are implementing XON/XOFF, send the start character */
  1260. if (I_IXOFF(tty)) {
  1261. unsigned char start_char = START_CHAR(tty);
  1262. status = edge_write(tty, port, &start_char, 1);
  1263. if (status <= 0)
  1264. return;
  1265. }
  1266. /* if we are implementing RTS/CTS, toggle that line */
  1267. if (tty->termios->c_cflag & CRTSCTS) {
  1268. edge_port->shadowMCR |= MCR_RTS;
  1269. send_cmd_write_uart_register(edge_port, MCR,
  1270. edge_port->shadowMCR);
  1271. }
  1272. }
  1273. /*****************************************************************************
  1274. * SerialSetTermios
  1275. * this function is called by the tty driver when it wants to change
  1276. * the termios structure
  1277. *****************************************************************************/
  1278. static void edge_set_termios(struct tty_struct *tty,
  1279. struct usb_serial_port *port, struct ktermios *old_termios)
  1280. {
  1281. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1282. unsigned int cflag;
  1283. cflag = tty->termios->c_cflag;
  1284. dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__, tty->termios->c_cflag, tty->termios->c_iflag);
  1285. dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__, old_termios->c_cflag, old_termios->c_iflag);
  1286. if (edge_port == NULL)
  1287. return;
  1288. if (!edge_port->open) {
  1289. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1290. return;
  1291. }
  1292. /* change the port settings to the new ones specified */
  1293. change_port_settings(tty, edge_port, old_termios);
  1294. }
  1295. /*****************************************************************************
  1296. * get_lsr_info - get line status register info
  1297. *
  1298. * Purpose: Let user call ioctl() to get info when the UART physically
  1299. * is emptied. On bus types like RS485, the transmitter must
  1300. * release the bus after transmitting. This must be done when
  1301. * the transmit shift register is empty, not be done when the
  1302. * transmit holding register is empty. This functionality
  1303. * allows an RS485 driver to be written in user space.
  1304. *****************************************************************************/
  1305. static int get_lsr_info(struct edgeport_port *edge_port,
  1306. unsigned int __user *value)
  1307. {
  1308. unsigned int result = 0;
  1309. unsigned long flags;
  1310. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1311. if (edge_port->maxTxCredits == edge_port->txCredits &&
  1312. edge_port->txfifo.count == 0) {
  1313. dev_dbg(&edge_port->port->dev, "%s -- Empty\n", __func__);
  1314. result = TIOCSER_TEMT;
  1315. }
  1316. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1317. if (copy_to_user(value, &result, sizeof(int)))
  1318. return -EFAULT;
  1319. return 0;
  1320. }
  1321. static int edge_tiocmset(struct tty_struct *tty,
  1322. unsigned int set, unsigned int clear)
  1323. {
  1324. struct usb_serial_port *port = tty->driver_data;
  1325. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1326. unsigned int mcr;
  1327. mcr = edge_port->shadowMCR;
  1328. if (set & TIOCM_RTS)
  1329. mcr |= MCR_RTS;
  1330. if (set & TIOCM_DTR)
  1331. mcr |= MCR_DTR;
  1332. if (set & TIOCM_LOOP)
  1333. mcr |= MCR_LOOPBACK;
  1334. if (clear & TIOCM_RTS)
  1335. mcr &= ~MCR_RTS;
  1336. if (clear & TIOCM_DTR)
  1337. mcr &= ~MCR_DTR;
  1338. if (clear & TIOCM_LOOP)
  1339. mcr &= ~MCR_LOOPBACK;
  1340. edge_port->shadowMCR = mcr;
  1341. send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
  1342. return 0;
  1343. }
  1344. static int edge_tiocmget(struct tty_struct *tty)
  1345. {
  1346. struct usb_serial_port *port = tty->driver_data;
  1347. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1348. unsigned int result = 0;
  1349. unsigned int msr;
  1350. unsigned int mcr;
  1351. msr = edge_port->shadowMSR;
  1352. mcr = edge_port->shadowMCR;
  1353. result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1354. | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1355. | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1356. | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
  1357. | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1358. | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1359. return result;
  1360. }
  1361. static int edge_get_icount(struct tty_struct *tty,
  1362. struct serial_icounter_struct *icount)
  1363. {
  1364. struct usb_serial_port *port = tty->driver_data;
  1365. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1366. struct async_icount cnow;
  1367. cnow = edge_port->icount;
  1368. icount->cts = cnow.cts;
  1369. icount->dsr = cnow.dsr;
  1370. icount->rng = cnow.rng;
  1371. icount->dcd = cnow.dcd;
  1372. icount->rx = cnow.rx;
  1373. icount->tx = cnow.tx;
  1374. icount->frame = cnow.frame;
  1375. icount->overrun = cnow.overrun;
  1376. icount->parity = cnow.parity;
  1377. icount->brk = cnow.brk;
  1378. icount->buf_overrun = cnow.buf_overrun;
  1379. dev_dbg(&port->dev, "%s (%d) TIOCGICOUNT RX=%d, TX=%d\n", __func__,
  1380. port->number, icount->rx, icount->tx);
  1381. return 0;
  1382. }
  1383. static int get_serial_info(struct edgeport_port *edge_port,
  1384. struct serial_struct __user *retinfo)
  1385. {
  1386. struct serial_struct tmp;
  1387. if (!retinfo)
  1388. return -EFAULT;
  1389. memset(&tmp, 0, sizeof(tmp));
  1390. tmp.type = PORT_16550A;
  1391. tmp.line = edge_port->port->serial->minor;
  1392. tmp.port = edge_port->port->number;
  1393. tmp.irq = 0;
  1394. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1395. tmp.xmit_fifo_size = edge_port->maxTxCredits;
  1396. tmp.baud_base = 9600;
  1397. tmp.close_delay = 5*HZ;
  1398. tmp.closing_wait = 30*HZ;
  1399. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1400. return -EFAULT;
  1401. return 0;
  1402. }
  1403. /*****************************************************************************
  1404. * SerialIoctl
  1405. * this function handles any ioctl calls to the driver
  1406. *****************************************************************************/
  1407. static int edge_ioctl(struct tty_struct *tty,
  1408. unsigned int cmd, unsigned long arg)
  1409. {
  1410. struct usb_serial_port *port = tty->driver_data;
  1411. DEFINE_WAIT(wait);
  1412. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1413. struct async_icount cnow;
  1414. struct async_icount cprev;
  1415. dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
  1416. switch (cmd) {
  1417. case TIOCSERGETLSR:
  1418. dev_dbg(&port->dev, "%s (%d) TIOCSERGETLSR\n", __func__, port->number);
  1419. return get_lsr_info(edge_port, (unsigned int __user *) arg);
  1420. case TIOCGSERIAL:
  1421. dev_dbg(&port->dev, "%s (%d) TIOCGSERIAL\n", __func__, port->number);
  1422. return get_serial_info(edge_port, (struct serial_struct __user *) arg);
  1423. case TIOCMIWAIT:
  1424. dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__, port->number);
  1425. cprev = edge_port->icount;
  1426. while (1) {
  1427. prepare_to_wait(&edge_port->delta_msr_wait,
  1428. &wait, TASK_INTERRUPTIBLE);
  1429. schedule();
  1430. finish_wait(&edge_port->delta_msr_wait, &wait);
  1431. /* see if a signal did it */
  1432. if (signal_pending(current))
  1433. return -ERESTARTSYS;
  1434. cnow = edge_port->icount;
  1435. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1436. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1437. return -EIO; /* no change => error */
  1438. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1439. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1440. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1441. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1442. return 0;
  1443. }
  1444. cprev = cnow;
  1445. }
  1446. /* NOTREACHED */
  1447. break;
  1448. }
  1449. return -ENOIOCTLCMD;
  1450. }
  1451. /*****************************************************************************
  1452. * SerialBreak
  1453. * this function sends a break to the port
  1454. *****************************************************************************/
  1455. static void edge_break(struct tty_struct *tty, int break_state)
  1456. {
  1457. struct usb_serial_port *port = tty->driver_data;
  1458. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1459. struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
  1460. int status;
  1461. if ((!edge_serial->is_epic) ||
  1462. ((edge_serial->is_epic) &&
  1463. (edge_serial->epic_descriptor.Supports.IOSPChase))) {
  1464. /* flush and chase */
  1465. edge_port->chaseResponsePending = true;
  1466. dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CHASE_PORT\n", __func__);
  1467. status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
  1468. if (status == 0) {
  1469. /* block until chase finished */
  1470. block_until_chase_response(edge_port);
  1471. } else {
  1472. edge_port->chaseResponsePending = false;
  1473. }
  1474. }
  1475. if ((!edge_serial->is_epic) ||
  1476. ((edge_serial->is_epic) &&
  1477. (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
  1478. if (break_state == -1) {
  1479. dev_dbg(&port->dev, "%s - Sending IOSP_CMD_SET_BREAK\n", __func__);
  1480. status = send_iosp_ext_cmd(edge_port,
  1481. IOSP_CMD_SET_BREAK, 0);
  1482. } else {
  1483. dev_dbg(&port->dev, "%s - Sending IOSP_CMD_CLEAR_BREAK\n", __func__);
  1484. status = send_iosp_ext_cmd(edge_port,
  1485. IOSP_CMD_CLEAR_BREAK, 0);
  1486. }
  1487. if (status)
  1488. dev_dbg(&port->dev, "%s - error sending break set/clear command.\n",
  1489. __func__);
  1490. }
  1491. }
  1492. /*****************************************************************************
  1493. * process_rcvd_data
  1494. * this function handles the data received on the bulk in pipe.
  1495. *****************************************************************************/
  1496. static void process_rcvd_data(struct edgeport_serial *edge_serial,
  1497. unsigned char *buffer, __u16 bufferLength)
  1498. {
  1499. struct device *dev = &edge_serial->serial->dev->dev;
  1500. struct usb_serial_port *port;
  1501. struct edgeport_port *edge_port;
  1502. struct tty_struct *tty;
  1503. __u16 lastBufferLength;
  1504. __u16 rxLen;
  1505. lastBufferLength = bufferLength + 1;
  1506. while (bufferLength > 0) {
  1507. /* failsafe incase we get a message that we don't understand */
  1508. if (lastBufferLength == bufferLength) {
  1509. dev_dbg(dev, "%s - stuck in loop, exiting it.\n", __func__);
  1510. break;
  1511. }
  1512. lastBufferLength = bufferLength;
  1513. switch (edge_serial->rxState) {
  1514. case EXPECT_HDR1:
  1515. edge_serial->rxHeader1 = *buffer;
  1516. ++buffer;
  1517. --bufferLength;
  1518. if (bufferLength == 0) {
  1519. edge_serial->rxState = EXPECT_HDR2;
  1520. break;
  1521. }
  1522. /* otherwise, drop on through */
  1523. case EXPECT_HDR2:
  1524. edge_serial->rxHeader2 = *buffer;
  1525. ++buffer;
  1526. --bufferLength;
  1527. dev_dbg(dev, "%s - Hdr1=%02X Hdr2=%02X\n", __func__,
  1528. edge_serial->rxHeader1, edge_serial->rxHeader2);
  1529. /* Process depending on whether this header is
  1530. * data or status */
  1531. if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
  1532. /* Decode this status header and go to
  1533. * EXPECT_HDR1 (if we can process the status
  1534. * with only 2 bytes), or go to EXPECT_HDR3 to
  1535. * get the third byte. */
  1536. edge_serial->rxPort =
  1537. IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
  1538. edge_serial->rxStatusCode =
  1539. IOSP_GET_STATUS_CODE(
  1540. edge_serial->rxHeader1);
  1541. if (!IOSP_STATUS_IS_2BYTE(
  1542. edge_serial->rxStatusCode)) {
  1543. /* This status needs additional bytes.
  1544. * Save what we have and then wait for
  1545. * more data.
  1546. */
  1547. edge_serial->rxStatusParam
  1548. = edge_serial->rxHeader2;
  1549. edge_serial->rxState = EXPECT_HDR3;
  1550. break;
  1551. }
  1552. /* We have all the header bytes, process the
  1553. status now */
  1554. process_rcvd_status(edge_serial,
  1555. edge_serial->rxHeader2, 0);
  1556. edge_serial->rxState = EXPECT_HDR1;
  1557. break;
  1558. } else {
  1559. edge_serial->rxPort =
  1560. IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
  1561. edge_serial->rxBytesRemaining =
  1562. IOSP_GET_HDR_DATA_LEN(
  1563. edge_serial->rxHeader1,
  1564. edge_serial->rxHeader2);
  1565. dev_dbg(dev, "%s - Data for Port %u Len %u\n",
  1566. __func__,
  1567. edge_serial->rxPort,
  1568. edge_serial->rxBytesRemaining);
  1569. /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
  1570. * ASSERT(DevExt->RxBytesRemaining <
  1571. * IOSP_MAX_DATA_LENGTH);
  1572. */
  1573. if (bufferLength == 0) {
  1574. edge_serial->rxState = EXPECT_DATA;
  1575. break;
  1576. }
  1577. /* Else, drop through */
  1578. }
  1579. case EXPECT_DATA: /* Expect data */
  1580. if (bufferLength < edge_serial->rxBytesRemaining) {
  1581. rxLen = bufferLength;
  1582. /* Expect data to start next buffer */
  1583. edge_serial->rxState = EXPECT_DATA;
  1584. } else {
  1585. /* BufLen >= RxBytesRemaining */
  1586. rxLen = edge_serial->rxBytesRemaining;
  1587. /* Start another header next time */
  1588. edge_serial->rxState = EXPECT_HDR1;
  1589. }
  1590. bufferLength -= rxLen;
  1591. edge_serial->rxBytesRemaining -= rxLen;
  1592. /* spit this data back into the tty driver if this
  1593. port is open */
  1594. if (rxLen) {
  1595. port = edge_serial->serial->port[
  1596. edge_serial->rxPort];
  1597. edge_port = usb_get_serial_port_data(port);
  1598. if (edge_port->open) {
  1599. tty = tty_port_tty_get(
  1600. &edge_port->port->port);
  1601. if (tty) {
  1602. dev_dbg(dev, "%s - Sending %d bytes to TTY for port %d\n",
  1603. __func__, rxLen, edge_serial->rxPort);
  1604. edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
  1605. tty_kref_put(tty);
  1606. }
  1607. edge_port->icount.rx += rxLen;
  1608. }
  1609. buffer += rxLen;
  1610. }
  1611. break;
  1612. case EXPECT_HDR3: /* Expect 3rd byte of status header */
  1613. edge_serial->rxHeader3 = *buffer;
  1614. ++buffer;
  1615. --bufferLength;
  1616. /* We have all the header bytes, process the
  1617. status now */
  1618. process_rcvd_status(edge_serial,
  1619. edge_serial->rxStatusParam,
  1620. edge_serial->rxHeader3);
  1621. edge_serial->rxState = EXPECT_HDR1;
  1622. break;
  1623. }
  1624. }
  1625. }
  1626. /*****************************************************************************
  1627. * process_rcvd_status
  1628. * this function handles the any status messages received on the
  1629. * bulk in pipe.
  1630. *****************************************************************************/
  1631. static void process_rcvd_status(struct edgeport_serial *edge_serial,
  1632. __u8 byte2, __u8 byte3)
  1633. {
  1634. struct usb_serial_port *port;
  1635. struct edgeport_port *edge_port;
  1636. struct tty_struct *tty;
  1637. struct device *dev;
  1638. __u8 code = edge_serial->rxStatusCode;
  1639. /* switch the port pointer to the one being currently talked about */
  1640. port = edge_serial->serial->port[edge_serial->rxPort];
  1641. edge_port = usb_get_serial_port_data(port);
  1642. if (edge_port == NULL) {
  1643. dev_err(&edge_serial->serial->dev->dev,
  1644. "%s - edge_port == NULL for port %d\n",
  1645. __func__, edge_serial->rxPort);
  1646. return;
  1647. }
  1648. dev = &port->dev;
  1649. if (code == IOSP_EXT_STATUS) {
  1650. switch (byte2) {
  1651. case IOSP_EXT_STATUS_CHASE_RSP:
  1652. /* we want to do EXT status regardless of port
  1653. * open/closed */
  1654. dev_dbg(dev, "%s - Port %u EXT CHASE_RSP Data = %02x\n",
  1655. __func__, edge_serial->rxPort, byte3);
  1656. /* Currently, the only EXT_STATUS is Chase, so process
  1657. * here instead of one more call to one more subroutine
  1658. * If/when more EXT_STATUS, there'll be more work to do
  1659. * Also, we currently clear flag and close the port
  1660. * regardless of content of above's Byte3.
  1661. * We could choose to do something else when Byte3 says
  1662. * Timeout on Chase from Edgeport, like wait longer in
  1663. * block_until_chase_response, but for now we don't.
  1664. */
  1665. edge_port->chaseResponsePending = false;
  1666. wake_up(&edge_port->wait_chase);
  1667. return;
  1668. case IOSP_EXT_STATUS_RX_CHECK_RSP:
  1669. dev_dbg(dev, "%s ========== Port %u CHECK_RSP Sequence = %02x =============\n",
  1670. __func__, edge_serial->rxPort, byte3);
  1671. /* Port->RxCheckRsp = true; */
  1672. return;
  1673. }
  1674. }
  1675. if (code == IOSP_STATUS_OPEN_RSP) {
  1676. edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
  1677. edge_port->maxTxCredits = edge_port->txCredits;
  1678. dev_dbg(dev, "%s - Port %u Open Response Initial MSR = %02x TxBufferSize = %d\n",
  1679. __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
  1680. handle_new_msr(edge_port, byte2);
  1681. /* send the current line settings to the port so we are
  1682. in sync with any further termios calls */
  1683. tty = tty_port_tty_get(&edge_port->port->port);
  1684. if (tty) {
  1685. change_port_settings(tty,
  1686. edge_port, tty->termios);
  1687. tty_kref_put(tty);
  1688. }
  1689. /* we have completed the open */
  1690. edge_port->openPending = false;
  1691. edge_port->open = true;
  1692. wake_up(&edge_port->wait_open);
  1693. return;
  1694. }
  1695. /* If port is closed, silently discard all rcvd status. We can
  1696. * have cases where buffered status is received AFTER the close
  1697. * port command is sent to the Edgeport.
  1698. */
  1699. if (!edge_port->open || edge_port->closePending)
  1700. return;
  1701. switch (code) {
  1702. /* Not currently sent by Edgeport */
  1703. case IOSP_STATUS_LSR:
  1704. dev_dbg(dev, "%s - Port %u LSR Status = %02x\n",
  1705. __func__, edge_serial->rxPort, byte2);
  1706. handle_new_lsr(edge_port, false, byte2, 0);
  1707. break;
  1708. case IOSP_STATUS_LSR_DATA:
  1709. dev_dbg(dev, "%s - Port %u LSR Status = %02x, Data = %02x\n",
  1710. __func__, edge_serial->rxPort, byte2, byte3);
  1711. /* byte2 is LSR Register */
  1712. /* byte3 is broken data byte */
  1713. handle_new_lsr(edge_port, true, byte2, byte3);
  1714. break;
  1715. /*
  1716. * case IOSP_EXT_4_STATUS:
  1717. * dev_dbg(dev, "%s - Port %u LSR Status = %02x Data = %02x\n",
  1718. * __func__, edge_serial->rxPort, byte2, byte3);
  1719. * break;
  1720. */
  1721. case IOSP_STATUS_MSR:
  1722. dev_dbg(dev, "%s - Port %u MSR Status = %02x\n",
  1723. __func__, edge_serial->rxPort, byte2);
  1724. /*
  1725. * Process this new modem status and generate appropriate
  1726. * events, etc, based on the new status. This routine
  1727. * also saves the MSR in Port->ShadowMsr.
  1728. */
  1729. handle_new_msr(edge_port, byte2);
  1730. break;
  1731. default:
  1732. dev_dbg(dev, "%s - Unrecognized IOSP status code %u\n", __func__, code);
  1733. break;
  1734. }
  1735. }
  1736. /*****************************************************************************
  1737. * edge_tty_recv
  1738. * this function passes data on to the tty flip buffer
  1739. *****************************************************************************/
  1740. static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
  1741. unsigned char *data, int length)
  1742. {
  1743. int cnt;
  1744. cnt = tty_insert_flip_string(tty, data, length);
  1745. if (cnt < length) {
  1746. dev_err(dev, "%s - dropping data, %d bytes lost\n",
  1747. __func__, length - cnt);
  1748. }
  1749. data += cnt;
  1750. length -= cnt;
  1751. tty_flip_buffer_push(tty);
  1752. }
  1753. /*****************************************************************************
  1754. * handle_new_msr
  1755. * this function handles any change to the msr register for a port.
  1756. *****************************************************************************/
  1757. static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
  1758. {
  1759. struct async_icount *icount;
  1760. if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
  1761. EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
  1762. icount = &edge_port->icount;
  1763. /* update input line counters */
  1764. if (newMsr & EDGEPORT_MSR_DELTA_CTS)
  1765. icount->cts++;
  1766. if (newMsr & EDGEPORT_MSR_DELTA_DSR)
  1767. icount->dsr++;
  1768. if (newMsr & EDGEPORT_MSR_DELTA_CD)
  1769. icount->dcd++;
  1770. if (newMsr & EDGEPORT_MSR_DELTA_RI)
  1771. icount->rng++;
  1772. wake_up_interruptible(&edge_port->delta_msr_wait);
  1773. }
  1774. /* Save the new modem status */
  1775. edge_port->shadowMSR = newMsr & 0xf0;
  1776. }
  1777. /*****************************************************************************
  1778. * handle_new_lsr
  1779. * this function handles any change to the lsr register for a port.
  1780. *****************************************************************************/
  1781. static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
  1782. __u8 lsr, __u8 data)
  1783. {
  1784. __u8 newLsr = (__u8) (lsr & (__u8)
  1785. (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
  1786. struct async_icount *icount;
  1787. edge_port->shadowLSR = lsr;
  1788. if (newLsr & LSR_BREAK) {
  1789. /*
  1790. * Parity and Framing errors only count if they
  1791. * occur exclusive of a break being
  1792. * received.
  1793. */
  1794. newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
  1795. }
  1796. /* Place LSR data byte into Rx buffer */
  1797. if (lsrData) {
  1798. struct tty_struct *tty =
  1799. tty_port_tty_get(&edge_port->port->port);
  1800. if (tty) {
  1801. edge_tty_recv(&edge_port->port->dev, tty, &data, 1);
  1802. tty_kref_put(tty);
  1803. }
  1804. }
  1805. /* update input line counters */
  1806. icount = &edge_port->icount;
  1807. if (newLsr & LSR_BREAK)
  1808. icount->brk++;
  1809. if (newLsr & LSR_OVER_ERR)
  1810. icount->overrun++;
  1811. if (newLsr & LSR_PAR_ERR)
  1812. icount->parity++;
  1813. if (newLsr & LSR_FRM_ERR)
  1814. icount->frame++;
  1815. }
  1816. /****************************************************************************
  1817. * sram_write
  1818. * writes a number of bytes to the Edgeport device's sram starting at the
  1819. * given address.
  1820. * If successful returns the number of bytes written, otherwise it returns
  1821. * a negative error number of the problem.
  1822. ****************************************************************************/
  1823. static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  1824. __u16 length, const __u8 *data)
  1825. {
  1826. int result;
  1827. __u16 current_length;
  1828. unsigned char *transfer_buffer;
  1829. dev_dbg(&serial->dev->dev, "%s - %x, %x, %d\n", __func__, extAddr, addr, length);
  1830. transfer_buffer = kmalloc(64, GFP_KERNEL);
  1831. if (!transfer_buffer) {
  1832. dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
  1833. __func__, 64);
  1834. return -ENOMEM;
  1835. }
  1836. /* need to split these writes up into 64 byte chunks */
  1837. result = 0;
  1838. while (length > 0) {
  1839. if (length > 64)
  1840. current_length = 64;
  1841. else
  1842. current_length = length;
  1843. /* dev_dbg(&serial->dev->dev, "%s - writing %x, %x, %d\n", __func__, extAddr, addr, current_length); */
  1844. memcpy(transfer_buffer, data, current_length);
  1845. result = usb_control_msg(serial->dev,
  1846. usb_sndctrlpipe(serial->dev, 0),
  1847. USB_REQUEST_ION_WRITE_RAM,
  1848. 0x40, addr, extAddr, transfer_buffer,
  1849. current_length, 300);
  1850. if (result < 0)
  1851. break;
  1852. length -= current_length;
  1853. addr += current_length;
  1854. data += current_length;
  1855. }
  1856. kfree(transfer_buffer);
  1857. return result;
  1858. }
  1859. /****************************************************************************
  1860. * rom_write
  1861. * writes a number of bytes to the Edgeport device's ROM starting at the
  1862. * given address.
  1863. * If successful returns the number of bytes written, otherwise it returns
  1864. * a negative error number of the problem.
  1865. ****************************************************************************/
  1866. static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  1867. __u16 length, const __u8 *data)
  1868. {
  1869. int result;
  1870. __u16 current_length;
  1871. unsigned char *transfer_buffer;
  1872. transfer_buffer = kmalloc(64, GFP_KERNEL);
  1873. if (!transfer_buffer) {
  1874. dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
  1875. __func__, 64);
  1876. return -ENOMEM;
  1877. }
  1878. /* need to split these writes up into 64 byte chunks */
  1879. result = 0;
  1880. while (length > 0) {
  1881. if (length > 64)
  1882. current_length = 64;
  1883. else
  1884. current_length = length;
  1885. memcpy(transfer_buffer, data, current_length);
  1886. result = usb_control_msg(serial->dev,
  1887. usb_sndctrlpipe(serial->dev, 0),
  1888. USB_REQUEST_ION_WRITE_ROM, 0x40,
  1889. addr, extAddr,
  1890. transfer_buffer, current_length, 300);
  1891. if (result < 0)
  1892. break;
  1893. length -= current_length;
  1894. addr += current_length;
  1895. data += current_length;
  1896. }
  1897. kfree(transfer_buffer);
  1898. return result;
  1899. }
  1900. /****************************************************************************
  1901. * rom_read
  1902. * reads a number of bytes from the Edgeport device starting at the given
  1903. * address.
  1904. * If successful returns the number of bytes read, otherwise it returns
  1905. * a negative error number of the problem.
  1906. ****************************************************************************/
  1907. static int rom_read(struct usb_serial *serial, __u16 extAddr,
  1908. __u16 addr, __u16 length, __u8 *data)
  1909. {
  1910. int result;
  1911. __u16 current_length;
  1912. unsigned char *transfer_buffer;
  1913. transfer_buffer = kmalloc(64, GFP_KERNEL);
  1914. if (!transfer_buffer) {
  1915. dev_err(&serial->dev->dev,
  1916. "%s - kmalloc(%d) failed.\n", __func__, 64);
  1917. return -ENOMEM;
  1918. }
  1919. /* need to split these reads up into 64 byte chunks */
  1920. result = 0;
  1921. while (length > 0) {
  1922. if (length > 64)
  1923. current_length = 64;
  1924. else
  1925. current_length = length;
  1926. result = usb_control_msg(serial->dev,
  1927. usb_rcvctrlpipe(serial->dev, 0),
  1928. USB_REQUEST_ION_READ_ROM,
  1929. 0xC0, addr, extAddr, transfer_buffer,
  1930. current_length, 300);
  1931. if (result < 0)
  1932. break;
  1933. memcpy(data, transfer_buffer, current_length);
  1934. length -= current_length;
  1935. addr += current_length;
  1936. data += current_length;
  1937. }
  1938. kfree(transfer_buffer);
  1939. return result;
  1940. }
  1941. /****************************************************************************
  1942. * send_iosp_ext_cmd
  1943. * Is used to send a IOSP message to the Edgeport device
  1944. ****************************************************************************/
  1945. static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
  1946. __u8 command, __u8 param)
  1947. {
  1948. unsigned char *buffer;
  1949. unsigned char *currentCommand;
  1950. int length = 0;
  1951. int status = 0;
  1952. buffer = kmalloc(10, GFP_ATOMIC);
  1953. if (!buffer) {
  1954. dev_err(&edge_port->port->dev,
  1955. "%s - kmalloc(%d) failed.\n", __func__, 10);
  1956. return -ENOMEM;
  1957. }
  1958. currentCommand = buffer;
  1959. MAKE_CMD_EXT_CMD(&currentCommand, &length,
  1960. edge_port->port->number - edge_port->port->serial->minor,
  1961. command, param);
  1962. status = write_cmd_usb(edge_port, buffer, length);
  1963. if (status) {
  1964. /* something bad happened, let's free up the memory */
  1965. kfree(buffer);
  1966. }
  1967. return status;
  1968. }
  1969. /*****************************************************************************
  1970. * write_cmd_usb
  1971. * this function writes the given buffer out to the bulk write endpoint.
  1972. *****************************************************************************/
  1973. static int write_cmd_usb(struct edgeport_port *edge_port,
  1974. unsigned char *buffer, int length)
  1975. {
  1976. struct edgeport_serial *edge_serial =
  1977. usb_get_serial_data(edge_port->port->serial);
  1978. struct device *dev = &edge_port->port->dev;
  1979. int status = 0;
  1980. struct urb *urb;
  1981. usb_serial_debug_data(dev, __func__, length, buffer);
  1982. /* Allocate our next urb */
  1983. urb = usb_alloc_urb(0, GFP_ATOMIC);
  1984. if (!urb)
  1985. return -ENOMEM;
  1986. atomic_inc(&CmdUrbs);
  1987. dev_dbg(dev, "%s - ALLOCATE URB %p (outstanding %d)\n",
  1988. __func__, urb, atomic_read(&CmdUrbs));
  1989. usb_fill_bulk_urb(urb, edge_serial->serial->dev,
  1990. usb_sndbulkpipe(edge_serial->serial->dev,
  1991. edge_serial->bulk_out_endpoint),
  1992. buffer, length, edge_bulk_out_cmd_callback, edge_port);
  1993. edge_port->commandPending = true;
  1994. status = usb_submit_urb(urb, GFP_ATOMIC);
  1995. if (status) {
  1996. /* something went wrong */
  1997. dev_err(dev, "%s - usb_submit_urb(write command) failed, status = %d\n",
  1998. __func__, status);
  1999. usb_kill_urb(urb);
  2000. usb_free_urb(urb);
  2001. atomic_dec(&CmdUrbs);
  2002. return status;
  2003. }
  2004. #if 0
  2005. wait_event(&edge_port->wait_command, !edge_port->commandPending);
  2006. if (edge_port->commandPending) {
  2007. /* command timed out */
  2008. dev_dbg(dev, "%s - command timed out\n", __func__);
  2009. status = -EINVAL;
  2010. }
  2011. #endif
  2012. return status;
  2013. }
  2014. /*****************************************************************************
  2015. * send_cmd_write_baud_rate
  2016. * this function sends the proper command to change the baud rate of the
  2017. * specified port.
  2018. *****************************************************************************/
  2019. static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
  2020. int baudRate)
  2021. {
  2022. struct edgeport_serial *edge_serial =
  2023. usb_get_serial_data(edge_port->port->serial);
  2024. struct device *dev = &edge_port->port->dev;
  2025. unsigned char *cmdBuffer;
  2026. unsigned char *currCmd;
  2027. int cmdLen = 0;
  2028. int divisor;
  2029. int status;
  2030. unsigned char number =
  2031. edge_port->port->number - edge_port->port->serial->minor;
  2032. if (edge_serial->is_epic &&
  2033. !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
  2034. dev_dbg(dev, "SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d\n",
  2035. edge_port->port->number, baudRate);
  2036. return 0;
  2037. }
  2038. dev_dbg(dev, "%s - port = %d, baud = %d\n", __func__,
  2039. edge_port->port->number, baudRate);
  2040. status = calc_baud_rate_divisor(dev, baudRate, &divisor);
  2041. if (status) {
  2042. dev_err(dev, "%s - bad baud rate\n", __func__);
  2043. return status;
  2044. }
  2045. /* Alloc memory for the string of commands. */
  2046. cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
  2047. if (!cmdBuffer) {
  2048. dev_err(dev, "%s - kmalloc(%d) failed.\n", __func__, 0x100);
  2049. return -ENOMEM;
  2050. }
  2051. currCmd = cmdBuffer;
  2052. /* Enable access to divisor latch */
  2053. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
  2054. /* Write the divisor itself */
  2055. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
  2056. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
  2057. /* Restore original value to disable access to divisor latch */
  2058. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
  2059. edge_port->shadowLCR);
  2060. status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
  2061. if (status) {
  2062. /* something bad happened, let's free up the memory */
  2063. kfree(cmdBuffer);
  2064. }
  2065. return status;
  2066. }
  2067. /*****************************************************************************
  2068. * calc_baud_rate_divisor
  2069. * this function calculates the proper baud rate divisor for the specified
  2070. * baud rate.
  2071. *****************************************************************************/
  2072. static int calc_baud_rate_divisor(struct device *dev, int baudrate, int *divisor)
  2073. {
  2074. int i;
  2075. __u16 custom;
  2076. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  2077. if (divisor_table[i].BaudRate == baudrate) {
  2078. *divisor = divisor_table[i].Divisor;
  2079. return 0;
  2080. }
  2081. }
  2082. /* We have tried all of the standard baud rates
  2083. * lets try to calculate the divisor for this baud rate
  2084. * Make sure the baud rate is reasonable */
  2085. if (baudrate > 50 && baudrate < 230400) {
  2086. /* get divisor */
  2087. custom = (__u16)((230400L + baudrate/2) / baudrate);
  2088. *divisor = custom;
  2089. dev_dbg(dev, "%s - Baud %d = %d\n", __func__, baudrate, custom);
  2090. return 0;
  2091. }
  2092. return -1;
  2093. }
  2094. /*****************************************************************************
  2095. * send_cmd_write_uart_register
  2096. * this function builds up a uart register message and sends to the device.
  2097. *****************************************************************************/
  2098. static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
  2099. __u8 regNum, __u8 regValue)
  2100. {
  2101. struct edgeport_serial *edge_serial =
  2102. usb_get_serial_data(edge_port->port->serial);
  2103. struct device *dev = &edge_port->port->dev;
  2104. unsigned char *cmdBuffer;
  2105. unsigned char *currCmd;
  2106. unsigned long cmdLen = 0;
  2107. int status;
  2108. dev_dbg(dev, "%s - write to %s register 0x%02x\n",
  2109. (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
  2110. if (edge_serial->is_epic &&
  2111. !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
  2112. regNum == MCR) {
  2113. dev_dbg(dev, "SendCmdWriteUartReg - Not writing to MCR Register\n");
  2114. return 0;
  2115. }
  2116. if (edge_serial->is_epic &&
  2117. !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
  2118. regNum == LCR) {
  2119. dev_dbg(dev, "SendCmdWriteUartReg - Not writing to LCR Register\n");
  2120. return 0;
  2121. }
  2122. /* Alloc memory for the string of commands. */
  2123. cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
  2124. if (cmdBuffer == NULL)
  2125. return -ENOMEM;
  2126. currCmd = cmdBuffer;
  2127. /* Build a cmd in the buffer to write the given register */
  2128. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
  2129. edge_port->port->number - edge_port->port->serial->minor,
  2130. regNum, regValue);
  2131. status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
  2132. if (status) {
  2133. /* something bad happened, let's free up the memory */
  2134. kfree(cmdBuffer);
  2135. }
  2136. return status;
  2137. }
  2138. /*****************************************************************************
  2139. * change_port_settings
  2140. * This routine is called to set the UART on the device to match the
  2141. * specified new settings.
  2142. *****************************************************************************/
  2143. static void change_port_settings(struct tty_struct *tty,
  2144. struct edgeport_port *edge_port, struct ktermios *old_termios)
  2145. {
  2146. struct device *dev = &edge_port->port->dev;
  2147. struct edgeport_serial *edge_serial =
  2148. usb_get_serial_data(edge_port->port->serial);
  2149. int baud;
  2150. unsigned cflag;
  2151. __u8 mask = 0xff;
  2152. __u8 lData;
  2153. __u8 lParity;
  2154. __u8 lStop;
  2155. __u8 rxFlow;
  2156. __u8 txFlow;
  2157. int status;
  2158. dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
  2159. if (!edge_port->open &&
  2160. !edge_port->openPending) {
  2161. dev_dbg(dev, "%s - port not opened\n", __func__);
  2162. return;
  2163. }
  2164. cflag = tty->termios->c_cflag;
  2165. switch (cflag & CSIZE) {
  2166. case CS5:
  2167. lData = LCR_BITS_5; mask = 0x1f;
  2168. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  2169. break;
  2170. case CS6:
  2171. lData = LCR_BITS_6; mask = 0x3f;
  2172. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  2173. break;
  2174. case CS7:
  2175. lData = LCR_BITS_7; mask = 0x7f;
  2176. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  2177. break;
  2178. default:
  2179. case CS8:
  2180. lData = LCR_BITS_8;
  2181. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  2182. break;
  2183. }
  2184. lParity = LCR_PAR_NONE;
  2185. if (cflag & PARENB) {
  2186. if (cflag & CMSPAR) {
  2187. if (cflag & PARODD) {
  2188. lParity = LCR_PAR_MARK;
  2189. dev_dbg(dev, "%s - parity = mark\n", __func__);
  2190. } else {
  2191. lParity = LCR_PAR_SPACE;
  2192. dev_dbg(dev, "%s - parity = space\n", __func__);
  2193. }
  2194. } else if (cflag & PARODD) {
  2195. lParity = LCR_PAR_ODD;
  2196. dev_dbg(dev, "%s - parity = odd\n", __func__);
  2197. } else {
  2198. lParity = LCR_PAR_EVEN;
  2199. dev_dbg(dev, "%s - parity = even\n", __func__);
  2200. }
  2201. } else {
  2202. dev_dbg(dev, "%s - parity = none\n", __func__);
  2203. }
  2204. if (cflag & CSTOPB) {
  2205. lStop = LCR_STOP_2;
  2206. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  2207. } else {
  2208. lStop = LCR_STOP_1;
  2209. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  2210. }
  2211. /* figure out the flow control settings */
  2212. rxFlow = txFlow = 0x00;
  2213. if (cflag & CRTSCTS) {
  2214. rxFlow |= IOSP_RX_FLOW_RTS;
  2215. txFlow |= IOSP_TX_FLOW_CTS;
  2216. dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
  2217. } else {
  2218. dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
  2219. }
  2220. /* if we are implementing XON/XOFF, set the start and stop character
  2221. in the device */
  2222. if (I_IXOFF(tty) || I_IXON(tty)) {
  2223. unsigned char stop_char = STOP_CHAR(tty);
  2224. unsigned char start_char = START_CHAR(tty);
  2225. if ((!edge_serial->is_epic) ||
  2226. ((edge_serial->is_epic) &&
  2227. (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
  2228. send_iosp_ext_cmd(edge_port,
  2229. IOSP_CMD_SET_XON_CHAR, start_char);
  2230. send_iosp_ext_cmd(edge_port,
  2231. IOSP_CMD_SET_XOFF_CHAR, stop_char);
  2232. }
  2233. /* if we are implementing INBOUND XON/XOFF */
  2234. if (I_IXOFF(tty)) {
  2235. rxFlow |= IOSP_RX_FLOW_XON_XOFF;
  2236. dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
  2237. __func__, start_char, stop_char);
  2238. } else {
  2239. dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
  2240. }
  2241. /* if we are implementing OUTBOUND XON/XOFF */
  2242. if (I_IXON(tty)) {
  2243. txFlow |= IOSP_TX_FLOW_XON_XOFF;
  2244. dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
  2245. __func__, start_char, stop_char);
  2246. } else {
  2247. dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
  2248. }
  2249. }
  2250. /* Set flow control to the configured value */
  2251. if ((!edge_serial->is_epic) ||
  2252. ((edge_serial->is_epic) &&
  2253. (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
  2254. send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
  2255. if ((!edge_serial->is_epic) ||
  2256. ((edge_serial->is_epic) &&
  2257. (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
  2258. send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
  2259. edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  2260. edge_port->shadowLCR |= (lData | lParity | lStop);
  2261. edge_port->validDataMask = mask;
  2262. /* Send the updated LCR value to the EdgePort */
  2263. status = send_cmd_write_uart_register(edge_port, LCR,
  2264. edge_port->shadowLCR);
  2265. if (status != 0)
  2266. return;
  2267. /* set up the MCR register and send it to the EdgePort */
  2268. edge_port->shadowMCR = MCR_MASTER_IE;
  2269. if (cflag & CBAUD)
  2270. edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
  2271. status = send_cmd_write_uart_register(edge_port, MCR,
  2272. edge_port->shadowMCR);
  2273. if (status != 0)
  2274. return;
  2275. /* Determine divisor based on baud rate */
  2276. baud = tty_get_baud_rate(tty);
  2277. if (!baud) {
  2278. /* pick a default, any default... */
  2279. baud = 9600;
  2280. }
  2281. dev_dbg(dev, "%s - baud rate = %d\n", __func__, baud);
  2282. status = send_cmd_write_baud_rate(edge_port, baud);
  2283. if (status == -1) {
  2284. /* Speed change was not possible - put back the old speed */
  2285. baud = tty_termios_baud_rate(old_termios);
  2286. tty_encode_baud_rate(tty, baud, baud);
  2287. }
  2288. }
  2289. /****************************************************************************
  2290. * unicode_to_ascii
  2291. * Turns a string from Unicode into ASCII.
  2292. * Doesn't do a good job with any characters that are outside the normal
  2293. * ASCII range, but it's only for debugging...
  2294. * NOTE: expects the unicode in LE format
  2295. ****************************************************************************/
  2296. static void unicode_to_ascii(char *string, int buflen,
  2297. __le16 *unicode, int unicode_size)
  2298. {
  2299. int i;
  2300. if (buflen <= 0) /* never happens, but... */
  2301. return;
  2302. --buflen; /* space for nul */
  2303. for (i = 0; i < unicode_size; i++) {
  2304. if (i >= buflen)
  2305. break;
  2306. string[i] = (char)(le16_to_cpu(unicode[i]));
  2307. }
  2308. string[i] = 0x00;
  2309. }
  2310. /****************************************************************************
  2311. * get_manufacturing_desc
  2312. * reads in the manufacturing descriptor and stores it into the serial
  2313. * structure.
  2314. ****************************************************************************/
  2315. static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
  2316. {
  2317. struct device *dev = &edge_serial->serial->dev->dev;
  2318. int response;
  2319. dev_dbg(dev, "getting manufacturer descriptor\n");
  2320. response = rom_read(edge_serial->serial,
  2321. (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
  2322. (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
  2323. EDGE_MANUF_DESC_LEN,
  2324. (__u8 *)(&edge_serial->manuf_descriptor));
  2325. if (response < 1)
  2326. dev_err(dev, "error in getting manufacturer descriptor\n");
  2327. else {
  2328. char string[30];
  2329. dev_dbg(dev, "**Manufacturer Descriptor\n");
  2330. dev_dbg(dev, " RomSize: %dK\n",
  2331. edge_serial->manuf_descriptor.RomSize);
  2332. dev_dbg(dev, " RamSize: %dK\n",
  2333. edge_serial->manuf_descriptor.RamSize);
  2334. dev_dbg(dev, " CpuRev: %d\n",
  2335. edge_serial->manuf_descriptor.CpuRev);
  2336. dev_dbg(dev, " BoardRev: %d\n",
  2337. edge_serial->manuf_descriptor.BoardRev);
  2338. dev_dbg(dev, " NumPorts: %d\n",
  2339. edge_serial->manuf_descriptor.NumPorts);
  2340. dev_dbg(dev, " DescDate: %d/%d/%d\n",
  2341. edge_serial->manuf_descriptor.DescDate[0],
  2342. edge_serial->manuf_descriptor.DescDate[1],
  2343. edge_serial->manuf_descriptor.DescDate[2]+1900);
  2344. unicode_to_ascii(string, sizeof(string),
  2345. edge_serial->manuf_descriptor.SerialNumber,
  2346. edge_serial->manuf_descriptor.SerNumLength/2);
  2347. dev_dbg(dev, " SerialNumber: %s\n", string);
  2348. unicode_to_ascii(string, sizeof(string),
  2349. edge_serial->manuf_descriptor.AssemblyNumber,
  2350. edge_serial->manuf_descriptor.AssemblyNumLength/2);
  2351. dev_dbg(dev, " AssemblyNumber: %s\n", string);
  2352. unicode_to_ascii(string, sizeof(string),
  2353. edge_serial->manuf_descriptor.OemAssyNumber,
  2354. edge_serial->manuf_descriptor.OemAssyNumLength/2);
  2355. dev_dbg(dev, " OemAssyNumber: %s\n", string);
  2356. dev_dbg(dev, " UartType: %d\n",
  2357. edge_serial->manuf_descriptor.UartType);
  2358. dev_dbg(dev, " IonPid: %d\n",
  2359. edge_serial->manuf_descriptor.IonPid);
  2360. dev_dbg(dev, " IonConfig: %d\n",
  2361. edge_serial->manuf_descriptor.IonConfig);
  2362. }
  2363. }
  2364. /****************************************************************************
  2365. * get_boot_desc
  2366. * reads in the bootloader descriptor and stores it into the serial
  2367. * structure.
  2368. ****************************************************************************/
  2369. static void get_boot_desc(struct edgeport_serial *edge_serial)
  2370. {
  2371. struct device *dev = &edge_serial->serial->dev->dev;
  2372. int response;
  2373. dev_dbg(dev, "getting boot descriptor\n");
  2374. response = rom_read(edge_serial->serial,
  2375. (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
  2376. (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
  2377. EDGE_BOOT_DESC_LEN,
  2378. (__u8 *)(&edge_serial->boot_descriptor));
  2379. if (response < 1)
  2380. dev_err(dev, "error in getting boot descriptor\n");
  2381. else {
  2382. dev_dbg(dev, "**Boot Descriptor:\n");
  2383. dev_dbg(dev, " BootCodeLength: %d\n",
  2384. le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
  2385. dev_dbg(dev, " MajorVersion: %d\n",
  2386. edge_serial->boot_descriptor.MajorVersion);
  2387. dev_dbg(dev, " MinorVersion: %d\n",
  2388. edge_serial->boot_descriptor.MinorVersion);
  2389. dev_dbg(dev, " BuildNumber: %d\n",
  2390. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
  2391. dev_dbg(dev, " Capabilities: 0x%x\n",
  2392. le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
  2393. dev_dbg(dev, " UConfig0: %d\n",
  2394. edge_serial->boot_descriptor.UConfig0);
  2395. dev_dbg(dev, " UConfig1: %d\n",
  2396. edge_serial->boot_descriptor.UConfig1);
  2397. }
  2398. }
  2399. /****************************************************************************
  2400. * load_application_firmware
  2401. * This is called to load the application firmware to the device
  2402. ****************************************************************************/
  2403. static void load_application_firmware(struct edgeport_serial *edge_serial)
  2404. {
  2405. struct device *dev = &edge_serial->serial->dev->dev;
  2406. const struct ihex_binrec *rec;
  2407. const struct firmware *fw;
  2408. const char *fw_name;
  2409. const char *fw_info;
  2410. int response;
  2411. __u32 Operaddr;
  2412. __u16 build;
  2413. switch (edge_serial->product_info.iDownloadFile) {
  2414. case EDGE_DOWNLOAD_FILE_I930:
  2415. fw_info = "downloading firmware version (930)";
  2416. fw_name = "edgeport/down.fw";
  2417. break;
  2418. case EDGE_DOWNLOAD_FILE_80251:
  2419. fw_info = "downloading firmware version (80251)";
  2420. fw_name = "edgeport/down2.fw";
  2421. break;
  2422. case EDGE_DOWNLOAD_FILE_NONE:
  2423. dev_dbg(dev, "No download file specified, skipping download\n");
  2424. return;
  2425. default:
  2426. return;
  2427. }
  2428. response = request_ihex_firmware(&fw, fw_name,
  2429. &edge_serial->serial->dev->dev);
  2430. if (response) {
  2431. dev_err(dev, "Failed to load image \"%s\" err %d\n",
  2432. fw_name, response);
  2433. return;
  2434. }
  2435. rec = (const struct ihex_binrec *)fw->data;
  2436. build = (rec->data[2] << 8) | rec->data[3];
  2437. dev_dbg(dev, "%s %d.%d.%d\n", fw_info, rec->data[0], rec->data[1], build);
  2438. edge_serial->product_info.FirmwareMajorVersion = rec->data[0];
  2439. edge_serial->product_info.FirmwareMinorVersion = rec->data[1];
  2440. edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
  2441. for (rec = ihex_next_binrec(rec); rec;
  2442. rec = ihex_next_binrec(rec)) {
  2443. Operaddr = be32_to_cpu(rec->addr);
  2444. response = sram_write(edge_serial->serial,
  2445. Operaddr >> 16,
  2446. Operaddr & 0xFFFF,
  2447. be16_to_cpu(rec->len),
  2448. &rec->data[0]);
  2449. if (response < 0) {
  2450. dev_err(&edge_serial->serial->dev->dev,
  2451. "sram_write failed (%x, %x, %d)\n",
  2452. Operaddr >> 16, Operaddr & 0xFFFF,
  2453. be16_to_cpu(rec->len));
  2454. break;
  2455. }
  2456. }
  2457. dev_dbg(dev, "sending exec_dl_code\n");
  2458. response = usb_control_msg (edge_serial->serial->dev,
  2459. usb_sndctrlpipe(edge_serial->serial->dev, 0),
  2460. USB_REQUEST_ION_EXEC_DL_CODE,
  2461. 0x40, 0x4000, 0x0001, NULL, 0, 3000);
  2462. release_firmware(fw);
  2463. }
  2464. /****************************************************************************
  2465. * edge_startup
  2466. ****************************************************************************/
  2467. static int edge_startup(struct usb_serial *serial)
  2468. {
  2469. struct edgeport_serial *edge_serial;
  2470. struct edgeport_port *edge_port;
  2471. struct usb_device *dev;
  2472. struct device *ddev = &serial->dev->dev;
  2473. int i, j;
  2474. int response;
  2475. bool interrupt_in_found;
  2476. bool bulk_in_found;
  2477. bool bulk_out_found;
  2478. static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
  2479. EDGE_COMPATIBILITY_MASK1,
  2480. EDGE_COMPATIBILITY_MASK2 };
  2481. dev = serial->dev;
  2482. /* create our private serial structure */
  2483. edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
  2484. if (edge_serial == NULL) {
  2485. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  2486. return -ENOMEM;
  2487. }
  2488. spin_lock_init(&edge_serial->es_lock);
  2489. edge_serial->serial = serial;
  2490. usb_set_serial_data(serial, edge_serial);
  2491. /* get the name for the device from the device */
  2492. i = usb_string(dev, dev->descriptor.iManufacturer,
  2493. &edge_serial->name[0], MAX_NAME_LEN+1);
  2494. if (i < 0)
  2495. i = 0;
  2496. edge_serial->name[i++] = ' ';
  2497. usb_string(dev, dev->descriptor.iProduct,
  2498. &edge_serial->name[i], MAX_NAME_LEN+2 - i);
  2499. dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
  2500. /* Read the epic descriptor */
  2501. if (get_epic_descriptor(edge_serial) <= 0) {
  2502. /* memcpy descriptor to Supports structures */
  2503. memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
  2504. sizeof(struct edge_compatibility_bits));
  2505. /* get the manufacturing descriptor for this device */
  2506. get_manufacturing_desc(edge_serial);
  2507. /* get the boot descriptor */
  2508. get_boot_desc(edge_serial);
  2509. get_product_info(edge_serial);
  2510. }
  2511. /* set the number of ports from the manufacturing description */
  2512. /* serial->num_ports = serial->product_info.NumPorts; */
  2513. if ((!edge_serial->is_epic) &&
  2514. (edge_serial->product_info.NumPorts != serial->num_ports)) {
  2515. dev_warn(ddev,
  2516. "Device Reported %d serial ports vs. core thinking we have %d ports, email greg@kroah.com this information.\n",
  2517. edge_serial->product_info.NumPorts,
  2518. serial->num_ports);
  2519. }
  2520. dev_dbg(ddev, "%s - time 1 %ld\n", __func__, jiffies);
  2521. /* If not an EPiC device */
  2522. if (!edge_serial->is_epic) {
  2523. /* now load the application firmware into this device */
  2524. load_application_firmware(edge_serial);
  2525. dev_dbg(ddev, "%s - time 2 %ld\n", __func__, jiffies);
  2526. /* Check current Edgeport EEPROM and update if necessary */
  2527. update_edgeport_E2PROM(edge_serial);
  2528. dev_dbg(ddev, "%s - time 3 %ld\n", __func__, jiffies);
  2529. /* set the configuration to use #1 */
  2530. /* dev_dbg(ddev, "set_configuration 1\n"); */
  2531. /* usb_set_configuration (dev, 1); */
  2532. }
  2533. dev_dbg(ddev, " FirmwareMajorVersion %d.%d.%d\n",
  2534. edge_serial->product_info.FirmwareMajorVersion,
  2535. edge_serial->product_info.FirmwareMinorVersion,
  2536. le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
  2537. /* we set up the pointers to the endpoints in the edge_open function,
  2538. * as the structures aren't created yet. */
  2539. /* set up our port private structures */
  2540. for (i = 0; i < serial->num_ports; ++i) {
  2541. edge_port = kzalloc(sizeof(struct edgeport_port), GFP_KERNEL);
  2542. if (edge_port == NULL) {
  2543. dev_err(ddev, "%s - Out of memory\n", __func__);
  2544. for (j = 0; j < i; ++j) {
  2545. kfree(usb_get_serial_port_data(serial->port[j]));
  2546. usb_set_serial_port_data(serial->port[j],
  2547. NULL);
  2548. }
  2549. usb_set_serial_data(serial, NULL);
  2550. kfree(edge_serial);
  2551. return -ENOMEM;
  2552. }
  2553. spin_lock_init(&edge_port->ep_lock);
  2554. edge_port->port = serial->port[i];
  2555. usb_set_serial_port_data(serial->port[i], edge_port);
  2556. }
  2557. response = 0;
  2558. if (edge_serial->is_epic) {
  2559. /* EPIC thing, set up our interrupt polling now and our read
  2560. * urb, so that the device knows it really is connected. */
  2561. interrupt_in_found = bulk_in_found = bulk_out_found = false;
  2562. for (i = 0; i < serial->interface->altsetting[0]
  2563. .desc.bNumEndpoints; ++i) {
  2564. struct usb_endpoint_descriptor *endpoint;
  2565. int buffer_size;
  2566. endpoint = &serial->interface->altsetting[0].
  2567. endpoint[i].desc;
  2568. buffer_size = usb_endpoint_maxp(endpoint);
  2569. if (!interrupt_in_found &&
  2570. (usb_endpoint_is_int_in(endpoint))) {
  2571. /* we found a interrupt in endpoint */
  2572. dev_dbg(ddev, "found interrupt in\n");
  2573. /* not set up yet, so do it now */
  2574. edge_serial->interrupt_read_urb =
  2575. usb_alloc_urb(0, GFP_KERNEL);
  2576. if (!edge_serial->interrupt_read_urb) {
  2577. dev_err(ddev, "out of memory\n");
  2578. return -ENOMEM;
  2579. }
  2580. edge_serial->interrupt_in_buffer =
  2581. kmalloc(buffer_size, GFP_KERNEL);
  2582. if (!edge_serial->interrupt_in_buffer) {
  2583. dev_err(ddev, "out of memory\n");
  2584. usb_free_urb(edge_serial->interrupt_read_urb);
  2585. return -ENOMEM;
  2586. }
  2587. edge_serial->interrupt_in_endpoint =
  2588. endpoint->bEndpointAddress;
  2589. /* set up our interrupt urb */
  2590. usb_fill_int_urb(
  2591. edge_serial->interrupt_read_urb,
  2592. dev,
  2593. usb_rcvintpipe(dev,
  2594. endpoint->bEndpointAddress),
  2595. edge_serial->interrupt_in_buffer,
  2596. buffer_size,
  2597. edge_interrupt_callback,
  2598. edge_serial,
  2599. endpoint->bInterval);
  2600. interrupt_in_found = true;
  2601. }
  2602. if (!bulk_in_found &&
  2603. (usb_endpoint_is_bulk_in(endpoint))) {
  2604. /* we found a bulk in endpoint */
  2605. dev_dbg(ddev, "found bulk in\n");
  2606. /* not set up yet, so do it now */
  2607. edge_serial->read_urb =
  2608. usb_alloc_urb(0, GFP_KERNEL);
  2609. if (!edge_serial->read_urb) {
  2610. dev_err(ddev, "out of memory\n");
  2611. return -ENOMEM;
  2612. }
  2613. edge_serial->bulk_in_buffer =
  2614. kmalloc(buffer_size, GFP_KERNEL);
  2615. if (!edge_serial->bulk_in_buffer) {
  2616. dev_err(&dev->dev, "out of memory\n");
  2617. usb_free_urb(edge_serial->read_urb);
  2618. return -ENOMEM;
  2619. }
  2620. edge_serial->bulk_in_endpoint =
  2621. endpoint->bEndpointAddress;
  2622. /* set up our bulk in urb */
  2623. usb_fill_bulk_urb(edge_serial->read_urb, dev,
  2624. usb_rcvbulkpipe(dev,
  2625. endpoint->bEndpointAddress),
  2626. edge_serial->bulk_in_buffer,
  2627. usb_endpoint_maxp(endpoint),
  2628. edge_bulk_in_callback,
  2629. edge_serial);
  2630. bulk_in_found = true;
  2631. }
  2632. if (!bulk_out_found &&
  2633. (usb_endpoint_is_bulk_out(endpoint))) {
  2634. /* we found a bulk out endpoint */
  2635. dev_dbg(ddev, "found bulk out\n");
  2636. edge_serial->bulk_out_endpoint =
  2637. endpoint->bEndpointAddress;
  2638. bulk_out_found = true;
  2639. }
  2640. }
  2641. if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
  2642. dev_err(ddev, "Error - the proper endpoints were not found!\n");
  2643. return -ENODEV;
  2644. }
  2645. /* start interrupt read for this edgeport this interrupt will
  2646. * continue as long as the edgeport is connected */
  2647. response = usb_submit_urb(edge_serial->interrupt_read_urb,
  2648. GFP_KERNEL);
  2649. if (response)
  2650. dev_err(ddev, "%s - Error %d submitting control urb\n",
  2651. __func__, response);
  2652. }
  2653. return response;
  2654. }
  2655. /****************************************************************************
  2656. * edge_disconnect
  2657. * This function is called whenever the device is removed from the usb bus.
  2658. ****************************************************************************/
  2659. static void edge_disconnect(struct usb_serial *serial)
  2660. {
  2661. struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
  2662. /* stop reads and writes on all ports */
  2663. /* free up our endpoint stuff */
  2664. if (edge_serial->is_epic) {
  2665. usb_kill_urb(edge_serial->interrupt_read_urb);
  2666. usb_free_urb(edge_serial->interrupt_read_urb);
  2667. kfree(edge_serial->interrupt_in_buffer);
  2668. usb_kill_urb(edge_serial->read_urb);
  2669. usb_free_urb(edge_serial->read_urb);
  2670. kfree(edge_serial->bulk_in_buffer);
  2671. }
  2672. }
  2673. /****************************************************************************
  2674. * edge_release
  2675. * This function is called when the device structure is deallocated.
  2676. ****************************************************************************/
  2677. static void edge_release(struct usb_serial *serial)
  2678. {
  2679. struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
  2680. int i;
  2681. for (i = 0; i < serial->num_ports; ++i)
  2682. kfree(usb_get_serial_port_data(serial->port[i]));
  2683. kfree(edge_serial);
  2684. }
  2685. module_usb_serial_driver(serial_drivers, id_table_combined);
  2686. MODULE_AUTHOR(DRIVER_AUTHOR);
  2687. MODULE_DESCRIPTION(DRIVER_DESC);
  2688. MODULE_LICENSE("GPL");
  2689. MODULE_FIRMWARE("edgeport/boot.fw");
  2690. MODULE_FIRMWARE("edgeport/boot2.fw");
  2691. MODULE_FIRMWARE("edgeport/down.fw");
  2692. MODULE_FIRMWARE("edgeport/down2.fw");
  2693. module_param(debug, bool, S_IRUGO | S_IWUSR);
  2694. MODULE_PARM_DESC(debug, "Debug enabled or not");