io_ti.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617
  1. /*
  2. * Edgeport USB Serial Converter driver
  3. *
  4. * Copyright (C) 2000-2002 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. * EP/1 EP/2 EP/4 EP/21 EP/22 EP/221 EP/42 EP/421 WATCHPORT
  14. *
  15. * For questions or problems with this driver, contact Inside Out
  16. * Networks technical support, or Peter Berger <pberger@brimson.com>,
  17. * or Al Borchers <alborchers@steinerpoint.com>.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/tty.h>
  25. #include <linux/tty_driver.h>
  26. #include <linux/tty_flip.h>
  27. #include <linux/module.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/mutex.h>
  30. #include <linux/serial.h>
  31. #include <linux/kfifo.h>
  32. #include <linux/ioctl.h>
  33. #include <linux/firmware.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/usb.h>
  36. #include <linux/usb/serial.h>
  37. #include "io_16654.h"
  38. #include "io_usbvend.h"
  39. #include "io_ti.h"
  40. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
  41. #define DRIVER_DESC "Edgeport USB Serial Driver"
  42. #define EPROM_PAGE_SIZE 64
  43. /* different hardware types */
  44. #define HARDWARE_TYPE_930 0
  45. #define HARDWARE_TYPE_TIUMP 1
  46. /* IOCTL_PRIVATE_TI_GET_MODE Definitions */
  47. #define TI_MODE_CONFIGURING 0 /* Device has not entered start device */
  48. #define TI_MODE_BOOT 1 /* Staying in boot mode */
  49. #define TI_MODE_DOWNLOAD 2 /* Made it to download mode */
  50. #define TI_MODE_TRANSITIONING 3 /* Currently in boot mode but
  51. transitioning to download mode */
  52. /* read urb state */
  53. #define EDGE_READ_URB_RUNNING 0
  54. #define EDGE_READ_URB_STOPPING 1
  55. #define EDGE_READ_URB_STOPPED 2
  56. #define EDGE_CLOSING_WAIT 4000 /* in .01 sec */
  57. #define EDGE_OUT_BUF_SIZE 1024
  58. /* Product information read from the Edgeport */
  59. struct product_info {
  60. int TiMode; /* Current TI Mode */
  61. __u8 hardware_type; /* Type of hardware */
  62. } __attribute__((packed));
  63. struct edgeport_port {
  64. __u16 uart_base;
  65. __u16 dma_address;
  66. __u8 shadow_msr;
  67. __u8 shadow_mcr;
  68. __u8 shadow_lsr;
  69. __u8 lsr_mask;
  70. __u32 ump_read_timeout; /* Number of milliseconds the UMP will
  71. wait without data before completing
  72. a read short */
  73. int baud_rate;
  74. int close_pending;
  75. int lsr_event;
  76. struct edgeport_serial *edge_serial;
  77. struct usb_serial_port *port;
  78. __u8 bUartMode; /* Port type, 0: RS232, etc. */
  79. spinlock_t ep_lock;
  80. int ep_read_urb_state;
  81. int ep_write_urb_in_use;
  82. struct kfifo write_fifo;
  83. };
  84. struct edgeport_serial {
  85. struct product_info product_info;
  86. u8 TI_I2C_Type; /* Type of I2C in UMP */
  87. u8 TiReadI2C; /* Set to TRUE if we have read the
  88. I2c in Boot Mode */
  89. struct mutex es_lock;
  90. int num_ports_open;
  91. struct usb_serial *serial;
  92. };
  93. /* Devices that this driver supports */
  94. static const struct usb_device_id edgeport_1port_id_table[] = {
  95. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
  96. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
  97. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
  98. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
  99. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
  100. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
  101. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
  102. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
  103. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
  104. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
  105. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
  106. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
  107. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
  108. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
  109. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
  110. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
  111. { }
  112. };
  113. static const struct usb_device_id edgeport_2port_id_table[] = {
  114. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
  115. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
  116. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
  117. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
  118. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
  119. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
  120. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
  121. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
  122. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
  123. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
  124. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
  125. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
  126. /* The 4, 8 and 16 port devices show up as multiple 2 port devices */
  127. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
  128. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
  129. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
  130. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
  131. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
  132. { }
  133. };
  134. /* Devices that this driver supports */
  135. static const struct usb_device_id id_table_combined[] = {
  136. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_1) },
  137. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1) },
  138. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_TI3410_EDGEPORT_1I) },
  139. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROXIMITY) },
  140. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOTION) },
  141. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_MOISTURE) },
  142. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_TEMPERATURE) },
  143. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_HUMIDITY) },
  144. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_POWER) },
  145. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_LIGHT) },
  146. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_RADIATION) },
  147. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_DISTANCE) },
  148. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_ACCELERATION) },
  149. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_WP_PROX_DIST) },
  150. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_HP4CD) },
  151. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_PLUS_PWR_PCI) },
  152. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2) },
  153. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2C) },
  154. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_2I) },
  155. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_421) },
  156. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21) },
  157. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_42) },
  158. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4) },
  159. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4I) },
  160. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22I) },
  161. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_221C) },
  162. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_22C) },
  163. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_21C) },
  164. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_4S) },
  165. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8) },
  166. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_8S) },
  167. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416) },
  168. { USB_DEVICE(USB_VENDOR_ID_ION, ION_DEVICE_ID_TI_EDGEPORT_416B) },
  169. { }
  170. };
  171. MODULE_DEVICE_TABLE(usb, id_table_combined);
  172. static unsigned char OperationalMajorVersion;
  173. static unsigned char OperationalMinorVersion;
  174. static unsigned short OperationalBuildNumber;
  175. static int closing_wait = EDGE_CLOSING_WAIT;
  176. static bool ignore_cpu_rev;
  177. static int default_uart_mode; /* RS232 */
  178. static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
  179. int length);
  180. static void stop_read(struct edgeport_port *edge_port);
  181. static int restart_read(struct edgeport_port *edge_port);
  182. static void edge_set_termios(struct tty_struct *tty,
  183. struct usb_serial_port *port, struct ktermios *old_termios);
  184. static void edge_send(struct usb_serial_port *port, struct tty_struct *tty);
  185. /* sysfs attributes */
  186. static int edge_create_sysfs_attrs(struct usb_serial_port *port);
  187. static int edge_remove_sysfs_attrs(struct usb_serial_port *port);
  188. static int ti_vread_sync(struct usb_device *dev, __u8 request,
  189. __u16 value, __u16 index, u8 *data, int size)
  190. {
  191. int status;
  192. status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
  193. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
  194. value, index, data, size, 1000);
  195. if (status < 0)
  196. return status;
  197. if (status != size) {
  198. dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
  199. __func__, size, status);
  200. return -ECOMM;
  201. }
  202. return 0;
  203. }
  204. static int ti_vsend_sync(struct usb_device *dev, __u8 request,
  205. __u16 value, __u16 index, u8 *data, int size)
  206. {
  207. int status;
  208. status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
  209. (USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
  210. value, index, data, size, 1000);
  211. if (status < 0)
  212. return status;
  213. if (status != size) {
  214. dev_dbg(&dev->dev, "%s - wanted to write %d, but only wrote %d\n",
  215. __func__, size, status);
  216. return -ECOMM;
  217. }
  218. return 0;
  219. }
  220. static int send_cmd(struct usb_device *dev, __u8 command,
  221. __u8 moduleid, __u16 value, u8 *data,
  222. int size)
  223. {
  224. return ti_vsend_sync(dev, command, value, moduleid, data, size);
  225. }
  226. /* clear tx/rx buffers and fifo in TI UMP */
  227. static int purge_port(struct usb_serial_port *port, __u16 mask)
  228. {
  229. int port_number = port->number - port->serial->minor;
  230. dev_dbg(&port->dev, "%s - port %d, mask %x\n", __func__, port_number, mask);
  231. return send_cmd(port->serial->dev,
  232. UMPC_PURGE_PORT,
  233. (__u8)(UMPM_UART1_PORT + port_number),
  234. mask,
  235. NULL,
  236. 0);
  237. }
  238. /**
  239. * read_download_mem - Read edgeport memory from TI chip
  240. * @dev: usb device pointer
  241. * @start_address: Device CPU address at which to read
  242. * @length: Length of above data
  243. * @address_type: Can read both XDATA and I2C
  244. * @buffer: pointer to input data buffer
  245. */
  246. static int read_download_mem(struct usb_device *dev, int start_address,
  247. int length, __u8 address_type, __u8 *buffer)
  248. {
  249. int status = 0;
  250. __u8 read_length;
  251. __be16 be_start_address;
  252. dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, length);
  253. /* Read in blocks of 64 bytes
  254. * (TI firmware can't handle more than 64 byte reads)
  255. */
  256. while (length) {
  257. if (length > 64)
  258. read_length = 64;
  259. else
  260. read_length = (__u8)length;
  261. if (read_length > 1) {
  262. dev_dbg(&dev->dev, "%s - @ %x for %d\n", __func__, start_address, read_length);
  263. }
  264. be_start_address = cpu_to_be16(start_address);
  265. status = ti_vread_sync(dev, UMPC_MEMORY_READ,
  266. (__u16)address_type,
  267. (__force __u16)be_start_address,
  268. buffer, read_length);
  269. if (status) {
  270. dev_dbg(&dev->dev, "%s - ERROR %x\n", __func__, status);
  271. return status;
  272. }
  273. if (read_length > 1)
  274. usb_serial_debug_data(&dev->dev, __func__, read_length, buffer);
  275. /* Update pointers/length */
  276. start_address += read_length;
  277. buffer += read_length;
  278. length -= read_length;
  279. }
  280. return status;
  281. }
  282. static int read_ram(struct usb_device *dev, int start_address,
  283. int length, __u8 *buffer)
  284. {
  285. return read_download_mem(dev, start_address, length,
  286. DTK_ADDR_SPACE_XDATA, buffer);
  287. }
  288. /* Read edgeport memory to a given block */
  289. static int read_boot_mem(struct edgeport_serial *serial,
  290. int start_address, int length, __u8 *buffer)
  291. {
  292. int status = 0;
  293. int i;
  294. for (i = 0; i < length; i++) {
  295. status = ti_vread_sync(serial->serial->dev,
  296. UMPC_MEMORY_READ, serial->TI_I2C_Type,
  297. (__u16)(start_address+i), &buffer[i], 0x01);
  298. if (status) {
  299. dev_dbg(&serial->serial->dev->dev, "%s - ERROR %x\n", __func__, status);
  300. return status;
  301. }
  302. }
  303. dev_dbg(&serial->serial->dev->dev, "%s - start_address = %x, length = %d\n",
  304. __func__, start_address, length);
  305. usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
  306. serial->TiReadI2C = 1;
  307. return status;
  308. }
  309. /* Write given block to TI EPROM memory */
  310. static int write_boot_mem(struct edgeport_serial *serial,
  311. int start_address, int length, __u8 *buffer)
  312. {
  313. int status = 0;
  314. int i;
  315. u8 *temp;
  316. /* Must do a read before write */
  317. if (!serial->TiReadI2C) {
  318. temp = kmalloc(1, GFP_KERNEL);
  319. if (!temp) {
  320. dev_err(&serial->serial->dev->dev,
  321. "%s - out of memory\n", __func__);
  322. return -ENOMEM;
  323. }
  324. status = read_boot_mem(serial, 0, 1, temp);
  325. kfree(temp);
  326. if (status)
  327. return status;
  328. }
  329. for (i = 0; i < length; ++i) {
  330. status = ti_vsend_sync(serial->serial->dev,
  331. UMPC_MEMORY_WRITE, buffer[i],
  332. (__u16)(i + start_address), NULL, 0);
  333. if (status)
  334. return status;
  335. }
  336. dev_dbg(&serial->serial->dev->dev, "%s - start_sddr = %x, length = %d\n", __func__, start_address, length);
  337. usb_serial_debug_data(&serial->serial->dev->dev, __func__, length, buffer);
  338. return status;
  339. }
  340. /* Write edgeport I2C memory to TI chip */
  341. static int write_i2c_mem(struct edgeport_serial *serial,
  342. int start_address, int length, __u8 address_type, __u8 *buffer)
  343. {
  344. struct device *dev = &serial->serial->dev->dev;
  345. int status = 0;
  346. int write_length;
  347. __be16 be_start_address;
  348. /* We can only send a maximum of 1 aligned byte page at a time */
  349. /* calculate the number of bytes left in the first page */
  350. write_length = EPROM_PAGE_SIZE -
  351. (start_address & (EPROM_PAGE_SIZE - 1));
  352. if (write_length > length)
  353. write_length = length;
  354. dev_dbg(dev, "%s - BytesInFirstPage Addr = %x, length = %d\n",
  355. __func__, start_address, write_length);
  356. usb_serial_debug_data(dev, __func__, write_length, buffer);
  357. /* Write first page */
  358. be_start_address = cpu_to_be16(start_address);
  359. status = ti_vsend_sync(serial->serial->dev,
  360. UMPC_MEMORY_WRITE, (__u16)address_type,
  361. (__force __u16)be_start_address,
  362. buffer, write_length);
  363. if (status) {
  364. dev_dbg(dev, "%s - ERROR %d\n", __func__, status);
  365. return status;
  366. }
  367. length -= write_length;
  368. start_address += write_length;
  369. buffer += write_length;
  370. /* We should be aligned now -- can write
  371. max page size bytes at a time */
  372. while (length) {
  373. if (length > EPROM_PAGE_SIZE)
  374. write_length = EPROM_PAGE_SIZE;
  375. else
  376. write_length = length;
  377. dev_dbg(dev, "%s - Page Write Addr = %x, length = %d\n",
  378. __func__, start_address, write_length);
  379. usb_serial_debug_data(dev, __func__, write_length, buffer);
  380. /* Write next page */
  381. be_start_address = cpu_to_be16(start_address);
  382. status = ti_vsend_sync(serial->serial->dev, UMPC_MEMORY_WRITE,
  383. (__u16)address_type,
  384. (__force __u16)be_start_address,
  385. buffer, write_length);
  386. if (status) {
  387. dev_err(dev, "%s - ERROR %d\n", __func__, status);
  388. return status;
  389. }
  390. length -= write_length;
  391. start_address += write_length;
  392. buffer += write_length;
  393. }
  394. return status;
  395. }
  396. /* Examine the UMP DMA registers and LSR
  397. *
  398. * Check the MSBit of the X and Y DMA byte count registers.
  399. * A zero in this bit indicates that the TX DMA buffers are empty
  400. * then check the TX Empty bit in the UART.
  401. */
  402. static int tx_active(struct edgeport_port *port)
  403. {
  404. int status;
  405. struct out_endpoint_desc_block *oedb;
  406. __u8 *lsr;
  407. int bytes_left = 0;
  408. oedb = kmalloc(sizeof(*oedb), GFP_KERNEL);
  409. if (!oedb) {
  410. dev_err(&port->port->dev, "%s - out of memory\n", __func__);
  411. return -ENOMEM;
  412. }
  413. lsr = kmalloc(1, GFP_KERNEL); /* Sigh, that's right, just one byte,
  414. as not all platforms can do DMA
  415. from stack */
  416. if (!lsr) {
  417. kfree(oedb);
  418. return -ENOMEM;
  419. }
  420. /* Read the DMA Count Registers */
  421. status = read_ram(port->port->serial->dev, port->dma_address,
  422. sizeof(*oedb), (void *)oedb);
  423. if (status)
  424. goto exit_is_tx_active;
  425. dev_dbg(&port->port->dev, "%s - XByteCount 0x%X\n", __func__, oedb->XByteCount);
  426. /* and the LSR */
  427. status = read_ram(port->port->serial->dev,
  428. port->uart_base + UMPMEM_OFFS_UART_LSR, 1, lsr);
  429. if (status)
  430. goto exit_is_tx_active;
  431. dev_dbg(&port->port->dev, "%s - LSR = 0x%X\n", __func__, *lsr);
  432. /* If either buffer has data or we are transmitting then return TRUE */
  433. if ((oedb->XByteCount & 0x80) != 0)
  434. bytes_left += 64;
  435. if ((*lsr & UMP_UART_LSR_TX_MASK) == 0)
  436. bytes_left += 1;
  437. /* We return Not Active if we get any kind of error */
  438. exit_is_tx_active:
  439. dev_dbg(&port->port->dev, "%s - return %d\n", __func__, bytes_left);
  440. kfree(lsr);
  441. kfree(oedb);
  442. return bytes_left;
  443. }
  444. static int choose_config(struct usb_device *dev)
  445. {
  446. /*
  447. * There may be multiple configurations on this device, in which case
  448. * we would need to read and parse all of them to find out which one
  449. * we want. However, we just support one config at this point,
  450. * configuration # 1, which is Config Descriptor 0.
  451. */
  452. dev_dbg(&dev->dev, "%s - Number of Interfaces = %d\n",
  453. __func__, dev->config->desc.bNumInterfaces);
  454. dev_dbg(&dev->dev, "%s - MAX Power = %d\n",
  455. __func__, dev->config->desc.bMaxPower * 2);
  456. if (dev->config->desc.bNumInterfaces != 1) {
  457. dev_err(&dev->dev, "%s - bNumInterfaces is not 1, ERROR!\n", __func__);
  458. return -ENODEV;
  459. }
  460. return 0;
  461. }
  462. static int read_rom(struct edgeport_serial *serial,
  463. int start_address, int length, __u8 *buffer)
  464. {
  465. int status;
  466. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
  467. status = read_download_mem(serial->serial->dev,
  468. start_address,
  469. length,
  470. serial->TI_I2C_Type,
  471. buffer);
  472. } else {
  473. status = read_boot_mem(serial, start_address, length,
  474. buffer);
  475. }
  476. return status;
  477. }
  478. static int write_rom(struct edgeport_serial *serial, int start_address,
  479. int length, __u8 *buffer)
  480. {
  481. if (serial->product_info.TiMode == TI_MODE_BOOT)
  482. return write_boot_mem(serial, start_address, length,
  483. buffer);
  484. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD)
  485. return write_i2c_mem(serial, start_address, length,
  486. serial->TI_I2C_Type, buffer);
  487. return -EINVAL;
  488. }
  489. /* Read a descriptor header from I2C based on type */
  490. static int get_descriptor_addr(struct edgeport_serial *serial,
  491. int desc_type, struct ti_i2c_desc *rom_desc)
  492. {
  493. int start_address;
  494. int status;
  495. /* Search for requested descriptor in I2C */
  496. start_address = 2;
  497. do {
  498. status = read_rom(serial,
  499. start_address,
  500. sizeof(struct ti_i2c_desc),
  501. (__u8 *)rom_desc);
  502. if (status)
  503. return 0;
  504. if (rom_desc->Type == desc_type)
  505. return start_address;
  506. start_address = start_address + sizeof(struct ti_i2c_desc)
  507. + rom_desc->Size;
  508. } while ((start_address < TI_MAX_I2C_SIZE) && rom_desc->Type);
  509. return 0;
  510. }
  511. /* Validate descriptor checksum */
  512. static int valid_csum(struct ti_i2c_desc *rom_desc, __u8 *buffer)
  513. {
  514. __u16 i;
  515. __u8 cs = 0;
  516. for (i = 0; i < rom_desc->Size; i++)
  517. cs = (__u8)(cs + buffer[i]);
  518. if (cs != rom_desc->CheckSum) {
  519. pr_debug("%s - Mismatch %x - %x", __func__, rom_desc->CheckSum, cs);
  520. return -EINVAL;
  521. }
  522. return 0;
  523. }
  524. /* Make sure that the I2C image is good */
  525. static int check_i2c_image(struct edgeport_serial *serial)
  526. {
  527. struct device *dev = &serial->serial->dev->dev;
  528. int status = 0;
  529. struct ti_i2c_desc *rom_desc;
  530. int start_address = 2;
  531. __u8 *buffer;
  532. __u16 ttype;
  533. rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
  534. if (!rom_desc) {
  535. dev_err(dev, "%s - out of memory\n", __func__);
  536. return -ENOMEM;
  537. }
  538. buffer = kmalloc(TI_MAX_I2C_SIZE, GFP_KERNEL);
  539. if (!buffer) {
  540. dev_err(dev, "%s - out of memory when allocating buffer\n",
  541. __func__);
  542. kfree(rom_desc);
  543. return -ENOMEM;
  544. }
  545. /* Read the first byte (Signature0) must be 0x52 or 0x10 */
  546. status = read_rom(serial, 0, 1, buffer);
  547. if (status)
  548. goto out;
  549. if (*buffer != UMP5152 && *buffer != UMP3410) {
  550. dev_err(dev, "%s - invalid buffer signature\n", __func__);
  551. status = -ENODEV;
  552. goto out;
  553. }
  554. do {
  555. /* Validate the I2C */
  556. status = read_rom(serial,
  557. start_address,
  558. sizeof(struct ti_i2c_desc),
  559. (__u8 *)rom_desc);
  560. if (status)
  561. break;
  562. if ((start_address + sizeof(struct ti_i2c_desc) +
  563. rom_desc->Size) > TI_MAX_I2C_SIZE) {
  564. status = -ENODEV;
  565. dev_dbg(dev, "%s - structure too big, erroring out.\n", __func__);
  566. break;
  567. }
  568. dev_dbg(dev, "%s Type = 0x%x\n", __func__, rom_desc->Type);
  569. /* Skip type 2 record */
  570. ttype = rom_desc->Type & 0x0f;
  571. if (ttype != I2C_DESC_TYPE_FIRMWARE_BASIC
  572. && ttype != I2C_DESC_TYPE_FIRMWARE_AUTO) {
  573. /* Read the descriptor data */
  574. status = read_rom(serial, start_address +
  575. sizeof(struct ti_i2c_desc),
  576. rom_desc->Size, buffer);
  577. if (status)
  578. break;
  579. status = valid_csum(rom_desc, buffer);
  580. if (status)
  581. break;
  582. }
  583. start_address = start_address + sizeof(struct ti_i2c_desc) +
  584. rom_desc->Size;
  585. } while ((rom_desc->Type != I2C_DESC_TYPE_ION) &&
  586. (start_address < TI_MAX_I2C_SIZE));
  587. if ((rom_desc->Type != I2C_DESC_TYPE_ION) ||
  588. (start_address > TI_MAX_I2C_SIZE))
  589. status = -ENODEV;
  590. out:
  591. kfree(buffer);
  592. kfree(rom_desc);
  593. return status;
  594. }
  595. static int get_manuf_info(struct edgeport_serial *serial, __u8 *buffer)
  596. {
  597. int status;
  598. int start_address;
  599. struct ti_i2c_desc *rom_desc;
  600. struct edge_ti_manuf_descriptor *desc;
  601. struct device *dev = &serial->serial->dev->dev;
  602. rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
  603. if (!rom_desc) {
  604. dev_err(dev, "%s - out of memory\n", __func__);
  605. return -ENOMEM;
  606. }
  607. start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_ION,
  608. rom_desc);
  609. if (!start_address) {
  610. dev_dbg(dev, "%s - Edge Descriptor not found in I2C\n", __func__);
  611. status = -ENODEV;
  612. goto exit;
  613. }
  614. /* Read the descriptor data */
  615. status = read_rom(serial, start_address+sizeof(struct ti_i2c_desc),
  616. rom_desc->Size, buffer);
  617. if (status)
  618. goto exit;
  619. status = valid_csum(rom_desc, buffer);
  620. desc = (struct edge_ti_manuf_descriptor *)buffer;
  621. dev_dbg(dev, "%s - IonConfig 0x%x\n", __func__, desc->IonConfig);
  622. dev_dbg(dev, "%s - Version %d\n", __func__, desc->Version);
  623. dev_dbg(dev, "%s - Cpu/Board 0x%x\n", __func__, desc->CpuRev_BoardRev);
  624. dev_dbg(dev, "%s - NumPorts %d\n", __func__, desc->NumPorts);
  625. dev_dbg(dev, "%s - NumVirtualPorts %d\n", __func__, desc->NumVirtualPorts);
  626. dev_dbg(dev, "%s - TotalPorts %d\n", __func__, desc->TotalPorts);
  627. exit:
  628. kfree(rom_desc);
  629. return status;
  630. }
  631. /* Build firmware header used for firmware update */
  632. static int build_i2c_fw_hdr(__u8 *header, struct device *dev)
  633. {
  634. __u8 *buffer;
  635. int buffer_size;
  636. int i;
  637. int err;
  638. __u8 cs = 0;
  639. struct ti_i2c_desc *i2c_header;
  640. struct ti_i2c_image_header *img_header;
  641. struct ti_i2c_firmware_rec *firmware_rec;
  642. const struct firmware *fw;
  643. const char *fw_name = "edgeport/down3.bin";
  644. /* In order to update the I2C firmware we must change the type 2 record
  645. * to type 0xF2. This will force the UMP to come up in Boot Mode.
  646. * Then while in boot mode, the driver will download the latest
  647. * firmware (padded to 15.5k) into the UMP ram. And finally when the
  648. * device comes back up in download mode the driver will cause the new
  649. * firmware to be copied from the UMP Ram to I2C and the firmware will
  650. * update the record type from 0xf2 to 0x02.
  651. */
  652. /* Allocate a 15.5k buffer + 2 bytes for version number
  653. * (Firmware Record) */
  654. buffer_size = (((1024 * 16) - 512 ) +
  655. sizeof(struct ti_i2c_firmware_rec));
  656. buffer = kmalloc(buffer_size, GFP_KERNEL);
  657. if (!buffer) {
  658. dev_err(dev, "%s - out of memory\n", __func__);
  659. return -ENOMEM;
  660. }
  661. // Set entire image of 0xffs
  662. memset(buffer, 0xff, buffer_size);
  663. err = request_firmware(&fw, fw_name, dev);
  664. if (err) {
  665. dev_err(dev, "Failed to load image \"%s\" err %d\n",
  666. fw_name, err);
  667. kfree(buffer);
  668. return err;
  669. }
  670. /* Save Download Version Number */
  671. OperationalMajorVersion = fw->data[0];
  672. OperationalMinorVersion = fw->data[1];
  673. OperationalBuildNumber = fw->data[2] | (fw->data[3] << 8);
  674. /* Copy version number into firmware record */
  675. firmware_rec = (struct ti_i2c_firmware_rec *)buffer;
  676. firmware_rec->Ver_Major = OperationalMajorVersion;
  677. firmware_rec->Ver_Minor = OperationalMinorVersion;
  678. /* Pointer to fw_down memory image */
  679. img_header = (struct ti_i2c_image_header *)&fw->data[4];
  680. memcpy(buffer + sizeof(struct ti_i2c_firmware_rec),
  681. &fw->data[4 + sizeof(struct ti_i2c_image_header)],
  682. le16_to_cpu(img_header->Length));
  683. release_firmware(fw);
  684. for (i=0; i < buffer_size; i++) {
  685. cs = (__u8)(cs + buffer[i]);
  686. }
  687. kfree(buffer);
  688. /* Build new header */
  689. i2c_header = (struct ti_i2c_desc *)header;
  690. firmware_rec = (struct ti_i2c_firmware_rec*)i2c_header->Data;
  691. i2c_header->Type = I2C_DESC_TYPE_FIRMWARE_BLANK;
  692. i2c_header->Size = (__u16)buffer_size;
  693. i2c_header->CheckSum = cs;
  694. firmware_rec->Ver_Major = OperationalMajorVersion;
  695. firmware_rec->Ver_Minor = OperationalMinorVersion;
  696. return 0;
  697. }
  698. /* Try to figure out what type of I2c we have */
  699. static int i2c_type_bootmode(struct edgeport_serial *serial)
  700. {
  701. struct device *dev = &serial->serial->dev->dev;
  702. int status;
  703. u8 *data;
  704. data = kmalloc(1, GFP_KERNEL);
  705. if (!data) {
  706. dev_err(dev, "%s - out of memory\n", __func__);
  707. return -ENOMEM;
  708. }
  709. /* Try to read type 2 */
  710. status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
  711. DTK_ADDR_SPACE_I2C_TYPE_II, 0, data, 0x01);
  712. if (status)
  713. dev_dbg(dev, "%s - read 2 status error = %d\n", __func__, status);
  714. else
  715. dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
  716. if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
  717. dev_dbg(dev, "%s - ROM_TYPE_II\n", __func__);
  718. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  719. goto out;
  720. }
  721. /* Try to read type 3 */
  722. status = ti_vread_sync(serial->serial->dev, UMPC_MEMORY_READ,
  723. DTK_ADDR_SPACE_I2C_TYPE_III, 0, data, 0x01);
  724. if (status)
  725. dev_dbg(dev, "%s - read 3 status error = %d\n", __func__, status);
  726. else
  727. dev_dbg(dev, "%s - read 2 data = 0x%x\n", __func__, *data);
  728. if ((!status) && (*data == UMP5152 || *data == UMP3410)) {
  729. dev_dbg(dev, "%s - ROM_TYPE_III\n", __func__);
  730. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_III;
  731. goto out;
  732. }
  733. dev_dbg(dev, "%s - Unknown\n", __func__);
  734. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  735. status = -ENODEV;
  736. out:
  737. kfree(data);
  738. return status;
  739. }
  740. static int bulk_xfer(struct usb_serial *serial, void *buffer,
  741. int length, int *num_sent)
  742. {
  743. int status;
  744. status = usb_bulk_msg(serial->dev,
  745. usb_sndbulkpipe(serial->dev,
  746. serial->port[0]->bulk_out_endpointAddress),
  747. buffer, length, num_sent, 1000);
  748. return status;
  749. }
  750. /* Download given firmware image to the device (IN BOOT MODE) */
  751. static int download_code(struct edgeport_serial *serial, __u8 *image,
  752. int image_length)
  753. {
  754. int status = 0;
  755. int pos;
  756. int transfer;
  757. int done;
  758. /* Transfer firmware image */
  759. for (pos = 0; pos < image_length; ) {
  760. /* Read the next buffer from file */
  761. transfer = image_length - pos;
  762. if (transfer > EDGE_FW_BULK_MAX_PACKET_SIZE)
  763. transfer = EDGE_FW_BULK_MAX_PACKET_SIZE;
  764. /* Transfer data */
  765. status = bulk_xfer(serial->serial, &image[pos],
  766. transfer, &done);
  767. if (status)
  768. break;
  769. /* Advance buffer pointer */
  770. pos += done;
  771. }
  772. return status;
  773. }
  774. /* FIXME!!! */
  775. static int config_boot_dev(struct usb_device *dev)
  776. {
  777. return 0;
  778. }
  779. static int ti_cpu_rev(struct edge_ti_manuf_descriptor *desc)
  780. {
  781. return TI_GET_CPU_REVISION(desc->CpuRev_BoardRev);
  782. }
  783. /**
  784. * DownloadTIFirmware - Download run-time operating firmware to the TI5052
  785. *
  786. * This routine downloads the main operating code into the TI5052, using the
  787. * boot code already burned into E2PROM or ROM.
  788. */
  789. static int download_fw(struct edgeport_serial *serial)
  790. {
  791. struct device *dev = &serial->serial->dev->dev;
  792. int status = 0;
  793. int start_address;
  794. struct edge_ti_manuf_descriptor *ti_manuf_desc;
  795. struct usb_interface_descriptor *interface;
  796. int download_cur_ver;
  797. int download_new_ver;
  798. /* This routine is entered by both the BOOT mode and the Download mode
  799. * We can determine which code is running by the reading the config
  800. * descriptor and if we have only one bulk pipe it is in boot mode
  801. */
  802. serial->product_info.hardware_type = HARDWARE_TYPE_TIUMP;
  803. /* Default to type 2 i2c */
  804. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  805. status = choose_config(serial->serial->dev);
  806. if (status)
  807. return status;
  808. interface = &serial->serial->interface->cur_altsetting->desc;
  809. if (!interface) {
  810. dev_err(dev, "%s - no interface set, error!\n", __func__);
  811. return -ENODEV;
  812. }
  813. /*
  814. * Setup initial mode -- the default mode 0 is TI_MODE_CONFIGURING
  815. * if we have more than one endpoint we are definitely in download
  816. * mode
  817. */
  818. if (interface->bNumEndpoints > 1)
  819. serial->product_info.TiMode = TI_MODE_DOWNLOAD;
  820. else
  821. /* Otherwise we will remain in configuring mode */
  822. serial->product_info.TiMode = TI_MODE_CONFIGURING;
  823. /********************************************************************/
  824. /* Download Mode */
  825. /********************************************************************/
  826. if (serial->product_info.TiMode == TI_MODE_DOWNLOAD) {
  827. struct ti_i2c_desc *rom_desc;
  828. dev_dbg(dev, "%s - RUNNING IN DOWNLOAD MODE\n", __func__);
  829. status = check_i2c_image(serial);
  830. if (status) {
  831. dev_dbg(dev, "%s - DOWNLOAD MODE -- BAD I2C\n", __func__);
  832. return status;
  833. }
  834. /* Validate Hardware version number
  835. * Read Manufacturing Descriptor from TI Based Edgeport
  836. */
  837. ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
  838. if (!ti_manuf_desc) {
  839. dev_err(dev, "%s - out of memory.\n", __func__);
  840. return -ENOMEM;
  841. }
  842. status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
  843. if (status) {
  844. kfree(ti_manuf_desc);
  845. return status;
  846. }
  847. /* Check version number of ION descriptor */
  848. if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
  849. dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
  850. __func__, ti_cpu_rev(ti_manuf_desc));
  851. kfree(ti_manuf_desc);
  852. return -EINVAL;
  853. }
  854. rom_desc = kmalloc(sizeof(*rom_desc), GFP_KERNEL);
  855. if (!rom_desc) {
  856. dev_err(dev, "%s - out of memory.\n", __func__);
  857. kfree(ti_manuf_desc);
  858. return -ENOMEM;
  859. }
  860. /* Search for type 2 record (firmware record) */
  861. start_address = get_descriptor_addr(serial,
  862. I2C_DESC_TYPE_FIRMWARE_BASIC, rom_desc);
  863. if (start_address != 0) {
  864. struct ti_i2c_firmware_rec *firmware_version;
  865. u8 *record;
  866. dev_dbg(dev, "%s - Found Type FIRMWARE (Type 2) record\n", __func__);
  867. firmware_version = kmalloc(sizeof(*firmware_version),
  868. GFP_KERNEL);
  869. if (!firmware_version) {
  870. dev_err(dev, "%s - out of memory.\n", __func__);
  871. kfree(rom_desc);
  872. kfree(ti_manuf_desc);
  873. return -ENOMEM;
  874. }
  875. /* Validate version number
  876. * Read the descriptor data
  877. */
  878. status = read_rom(serial, start_address +
  879. sizeof(struct ti_i2c_desc),
  880. sizeof(struct ti_i2c_firmware_rec),
  881. (__u8 *)firmware_version);
  882. if (status) {
  883. kfree(firmware_version);
  884. kfree(rom_desc);
  885. kfree(ti_manuf_desc);
  886. return status;
  887. }
  888. /* Check version number of download with current
  889. version in I2c */
  890. download_cur_ver = (firmware_version->Ver_Major << 8) +
  891. (firmware_version->Ver_Minor);
  892. download_new_ver = (OperationalMajorVersion << 8) +
  893. (OperationalMinorVersion);
  894. dev_dbg(dev, "%s - >> FW Versions Device %d.%d Driver %d.%d\n",
  895. __func__, firmware_version->Ver_Major,
  896. firmware_version->Ver_Minor,
  897. OperationalMajorVersion,
  898. OperationalMinorVersion);
  899. /* Check if we have an old version in the I2C and
  900. update if necessary */
  901. if (download_cur_ver < download_new_ver) {
  902. dev_dbg(dev, "%s - Update I2C dld from %d.%d to %d.%d\n",
  903. __func__,
  904. firmware_version->Ver_Major,
  905. firmware_version->Ver_Minor,
  906. OperationalMajorVersion,
  907. OperationalMinorVersion);
  908. record = kmalloc(1, GFP_KERNEL);
  909. if (!record) {
  910. dev_err(dev, "%s - out of memory.\n",
  911. __func__);
  912. kfree(firmware_version);
  913. kfree(rom_desc);
  914. kfree(ti_manuf_desc);
  915. return -ENOMEM;
  916. }
  917. /* In order to update the I2C firmware we must
  918. * change the type 2 record to type 0xF2. This
  919. * will force the UMP to come up in Boot Mode.
  920. * Then while in boot mode, the driver will
  921. * download the latest firmware (padded to
  922. * 15.5k) into the UMP ram. Finally when the
  923. * device comes back up in download mode the
  924. * driver will cause the new firmware to be
  925. * copied from the UMP Ram to I2C and the
  926. * firmware will update the record type from
  927. * 0xf2 to 0x02.
  928. */
  929. *record = I2C_DESC_TYPE_FIRMWARE_BLANK;
  930. /* Change the I2C Firmware record type to
  931. 0xf2 to trigger an update */
  932. status = write_rom(serial, start_address,
  933. sizeof(*record), record);
  934. if (status) {
  935. kfree(record);
  936. kfree(firmware_version);
  937. kfree(rom_desc);
  938. kfree(ti_manuf_desc);
  939. return status;
  940. }
  941. /* verify the write -- must do this in order
  942. * for write to complete before we do the
  943. * hardware reset
  944. */
  945. status = read_rom(serial,
  946. start_address,
  947. sizeof(*record),
  948. record);
  949. if (status) {
  950. kfree(record);
  951. kfree(firmware_version);
  952. kfree(rom_desc);
  953. kfree(ti_manuf_desc);
  954. return status;
  955. }
  956. if (*record != I2C_DESC_TYPE_FIRMWARE_BLANK) {
  957. dev_err(dev, "%s - error resetting device\n", __func__);
  958. kfree(record);
  959. kfree(firmware_version);
  960. kfree(rom_desc);
  961. kfree(ti_manuf_desc);
  962. return -ENODEV;
  963. }
  964. dev_dbg(dev, "%s - HARDWARE RESET\n", __func__);
  965. /* Reset UMP -- Back to BOOT MODE */
  966. status = ti_vsend_sync(serial->serial->dev,
  967. UMPC_HARDWARE_RESET,
  968. 0, 0, NULL, 0);
  969. dev_dbg(dev, "%s - HARDWARE RESET return %d\n", __func__, status);
  970. /* return an error on purpose. */
  971. kfree(record);
  972. kfree(firmware_version);
  973. kfree(rom_desc);
  974. kfree(ti_manuf_desc);
  975. return -ENODEV;
  976. }
  977. kfree(firmware_version);
  978. }
  979. /* Search for type 0xF2 record (firmware blank record) */
  980. else if ((start_address = get_descriptor_addr(serial, I2C_DESC_TYPE_FIRMWARE_BLANK, rom_desc)) != 0) {
  981. #define HEADER_SIZE (sizeof(struct ti_i2c_desc) + \
  982. sizeof(struct ti_i2c_firmware_rec))
  983. __u8 *header;
  984. __u8 *vheader;
  985. header = kmalloc(HEADER_SIZE, GFP_KERNEL);
  986. if (!header) {
  987. dev_err(dev, "%s - out of memory.\n", __func__);
  988. kfree(rom_desc);
  989. kfree(ti_manuf_desc);
  990. return -ENOMEM;
  991. }
  992. vheader = kmalloc(HEADER_SIZE, GFP_KERNEL);
  993. if (!vheader) {
  994. dev_err(dev, "%s - out of memory.\n", __func__);
  995. kfree(header);
  996. kfree(rom_desc);
  997. kfree(ti_manuf_desc);
  998. return -ENOMEM;
  999. }
  1000. dev_dbg(dev, "%s - Found Type BLANK FIRMWARE (Type F2) record\n", __func__);
  1001. /*
  1002. * In order to update the I2C firmware we must change
  1003. * the type 2 record to type 0xF2. This will force the
  1004. * UMP to come up in Boot Mode. Then while in boot
  1005. * mode, the driver will download the latest firmware
  1006. * (padded to 15.5k) into the UMP ram. Finally when the
  1007. * device comes back up in download mode the driver
  1008. * will cause the new firmware to be copied from the
  1009. * UMP Ram to I2C and the firmware will update the
  1010. * record type from 0xf2 to 0x02.
  1011. */
  1012. status = build_i2c_fw_hdr(header, dev);
  1013. if (status) {
  1014. kfree(vheader);
  1015. kfree(header);
  1016. kfree(rom_desc);
  1017. kfree(ti_manuf_desc);
  1018. return -EINVAL;
  1019. }
  1020. /* Update I2C with type 0xf2 record with correct
  1021. size and checksum */
  1022. status = write_rom(serial,
  1023. start_address,
  1024. HEADER_SIZE,
  1025. header);
  1026. if (status) {
  1027. kfree(vheader);
  1028. kfree(header);
  1029. kfree(rom_desc);
  1030. kfree(ti_manuf_desc);
  1031. return -EINVAL;
  1032. }
  1033. /* verify the write -- must do this in order for
  1034. write to complete before we do the hardware reset */
  1035. status = read_rom(serial, start_address,
  1036. HEADER_SIZE, vheader);
  1037. if (status) {
  1038. dev_dbg(dev, "%s - can't read header back\n", __func__);
  1039. kfree(vheader);
  1040. kfree(header);
  1041. kfree(rom_desc);
  1042. kfree(ti_manuf_desc);
  1043. return status;
  1044. }
  1045. if (memcmp(vheader, header, HEADER_SIZE)) {
  1046. dev_dbg(dev, "%s - write download record failed\n", __func__);
  1047. kfree(vheader);
  1048. kfree(header);
  1049. kfree(rom_desc);
  1050. kfree(ti_manuf_desc);
  1051. return -EINVAL;
  1052. }
  1053. kfree(vheader);
  1054. kfree(header);
  1055. dev_dbg(dev, "%s - Start firmware update\n", __func__);
  1056. /* Tell firmware to copy download image into I2C */
  1057. status = ti_vsend_sync(serial->serial->dev,
  1058. UMPC_COPY_DNLD_TO_I2C, 0, 0, NULL, 0);
  1059. dev_dbg(dev, "%s - Update complete 0x%x\n", __func__, status);
  1060. if (status) {
  1061. dev_err(dev,
  1062. "%s - UMPC_COPY_DNLD_TO_I2C failed\n",
  1063. __func__);
  1064. kfree(rom_desc);
  1065. kfree(ti_manuf_desc);
  1066. return status;
  1067. }
  1068. }
  1069. // The device is running the download code
  1070. kfree(rom_desc);
  1071. kfree(ti_manuf_desc);
  1072. return 0;
  1073. }
  1074. /********************************************************************/
  1075. /* Boot Mode */
  1076. /********************************************************************/
  1077. dev_dbg(dev, "%s - RUNNING IN BOOT MODE\n", __func__);
  1078. /* Configure the TI device so we can use the BULK pipes for download */
  1079. status = config_boot_dev(serial->serial->dev);
  1080. if (status)
  1081. return status;
  1082. if (le16_to_cpu(serial->serial->dev->descriptor.idVendor)
  1083. != USB_VENDOR_ID_ION) {
  1084. dev_dbg(dev, "%s - VID = 0x%x\n", __func__,
  1085. le16_to_cpu(serial->serial->dev->descriptor.idVendor));
  1086. serial->TI_I2C_Type = DTK_ADDR_SPACE_I2C_TYPE_II;
  1087. goto stayinbootmode;
  1088. }
  1089. /* We have an ION device (I2c Must be programmed)
  1090. Determine I2C image type */
  1091. if (i2c_type_bootmode(serial))
  1092. goto stayinbootmode;
  1093. /* Check for ION Vendor ID and that the I2C is valid */
  1094. if (!check_i2c_image(serial)) {
  1095. struct ti_i2c_image_header *header;
  1096. int i;
  1097. __u8 cs = 0;
  1098. __u8 *buffer;
  1099. int buffer_size;
  1100. int err;
  1101. const struct firmware *fw;
  1102. const char *fw_name = "edgeport/down3.bin";
  1103. /* Validate Hardware version number
  1104. * Read Manufacturing Descriptor from TI Based Edgeport
  1105. */
  1106. ti_manuf_desc = kmalloc(sizeof(*ti_manuf_desc), GFP_KERNEL);
  1107. if (!ti_manuf_desc) {
  1108. dev_err(dev, "%s - out of memory.\n", __func__);
  1109. return -ENOMEM;
  1110. }
  1111. status = get_manuf_info(serial, (__u8 *)ti_manuf_desc);
  1112. if (status) {
  1113. kfree(ti_manuf_desc);
  1114. goto stayinbootmode;
  1115. }
  1116. /* Check for version 2 */
  1117. if (!ignore_cpu_rev && ti_cpu_rev(ti_manuf_desc) < 2) {
  1118. dev_dbg(dev, "%s - Wrong CPU Rev %d (Must be 2)\n",
  1119. __func__, ti_cpu_rev(ti_manuf_desc));
  1120. kfree(ti_manuf_desc);
  1121. goto stayinbootmode;
  1122. }
  1123. kfree(ti_manuf_desc);
  1124. /*
  1125. * In order to update the I2C firmware we must change the type
  1126. * 2 record to type 0xF2. This will force the UMP to come up
  1127. * in Boot Mode. Then while in boot mode, the driver will
  1128. * download the latest firmware (padded to 15.5k) into the
  1129. * UMP ram. Finally when the device comes back up in download
  1130. * mode the driver will cause the new firmware to be copied
  1131. * from the UMP Ram to I2C and the firmware will update the
  1132. * record type from 0xf2 to 0x02.
  1133. *
  1134. * Do we really have to copy the whole firmware image,
  1135. * or could we do this in place!
  1136. */
  1137. /* Allocate a 15.5k buffer + 3 byte header */
  1138. buffer_size = (((1024 * 16) - 512) +
  1139. sizeof(struct ti_i2c_image_header));
  1140. buffer = kmalloc(buffer_size, GFP_KERNEL);
  1141. if (!buffer) {
  1142. dev_err(dev, "%s - out of memory\n", __func__);
  1143. return -ENOMEM;
  1144. }
  1145. /* Initialize the buffer to 0xff (pad the buffer) */
  1146. memset(buffer, 0xff, buffer_size);
  1147. err = request_firmware(&fw, fw_name, dev);
  1148. if (err) {
  1149. dev_err(dev, "Failed to load image \"%s\" err %d\n",
  1150. fw_name, err);
  1151. kfree(buffer);
  1152. return err;
  1153. }
  1154. memcpy(buffer, &fw->data[4], fw->size - 4);
  1155. release_firmware(fw);
  1156. for (i = sizeof(struct ti_i2c_image_header);
  1157. i < buffer_size; i++) {
  1158. cs = (__u8)(cs + buffer[i]);
  1159. }
  1160. header = (struct ti_i2c_image_header *)buffer;
  1161. /* update length and checksum after padding */
  1162. header->Length = cpu_to_le16((__u16)(buffer_size -
  1163. sizeof(struct ti_i2c_image_header)));
  1164. header->CheckSum = cs;
  1165. /* Download the operational code */
  1166. dev_dbg(dev, "%s - Downloading operational code image (TI UMP)\n", __func__);
  1167. status = download_code(serial, buffer, buffer_size);
  1168. kfree(buffer);
  1169. if (status) {
  1170. dev_dbg(dev, "%s - Error downloading operational code image\n", __func__);
  1171. return status;
  1172. }
  1173. /* Device will reboot */
  1174. serial->product_info.TiMode = TI_MODE_TRANSITIONING;
  1175. dev_dbg(dev, "%s - Download successful -- Device rebooting...\n", __func__);
  1176. /* return an error on purpose */
  1177. return -ENODEV;
  1178. }
  1179. stayinbootmode:
  1180. /* Eprom is invalid or blank stay in boot mode */
  1181. dev_dbg(dev, "%s - STAYING IN BOOT MODE\n", __func__);
  1182. serial->product_info.TiMode = TI_MODE_BOOT;
  1183. return 0;
  1184. }
  1185. static int ti_do_config(struct edgeport_port *port, int feature, int on)
  1186. {
  1187. int port_number = port->port->number - port->port->serial->minor;
  1188. on = !!on; /* 1 or 0 not bitmask */
  1189. return send_cmd(port->port->serial->dev,
  1190. feature, (__u8)(UMPM_UART1_PORT + port_number),
  1191. on, NULL, 0);
  1192. }
  1193. static int restore_mcr(struct edgeport_port *port, __u8 mcr)
  1194. {
  1195. int status = 0;
  1196. dev_dbg(&port->port->dev, "%s - %x\n", __func__, mcr);
  1197. status = ti_do_config(port, UMPC_SET_CLR_DTR, mcr & MCR_DTR);
  1198. if (status)
  1199. return status;
  1200. status = ti_do_config(port, UMPC_SET_CLR_RTS, mcr & MCR_RTS);
  1201. if (status)
  1202. return status;
  1203. return ti_do_config(port, UMPC_SET_CLR_LOOPBACK, mcr & MCR_LOOPBACK);
  1204. }
  1205. /* Convert TI LSR to standard UART flags */
  1206. static __u8 map_line_status(__u8 ti_lsr)
  1207. {
  1208. __u8 lsr = 0;
  1209. #define MAP_FLAG(flagUmp, flagUart) \
  1210. if (ti_lsr & flagUmp) \
  1211. lsr |= flagUart;
  1212. MAP_FLAG(UMP_UART_LSR_OV_MASK, LSR_OVER_ERR) /* overrun */
  1213. MAP_FLAG(UMP_UART_LSR_PE_MASK, LSR_PAR_ERR) /* parity error */
  1214. MAP_FLAG(UMP_UART_LSR_FE_MASK, LSR_FRM_ERR) /* framing error */
  1215. MAP_FLAG(UMP_UART_LSR_BR_MASK, LSR_BREAK) /* break detected */
  1216. MAP_FLAG(UMP_UART_LSR_RX_MASK, LSR_RX_AVAIL) /* rx data available */
  1217. MAP_FLAG(UMP_UART_LSR_TX_MASK, LSR_TX_EMPTY) /* tx hold reg empty */
  1218. #undef MAP_FLAG
  1219. return lsr;
  1220. }
  1221. static void handle_new_msr(struct edgeport_port *edge_port, __u8 msr)
  1222. {
  1223. struct async_icount *icount;
  1224. struct tty_struct *tty;
  1225. dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, msr);
  1226. if (msr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
  1227. EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
  1228. icount = &edge_port->port->icount;
  1229. /* update input line counters */
  1230. if (msr & EDGEPORT_MSR_DELTA_CTS)
  1231. icount->cts++;
  1232. if (msr & EDGEPORT_MSR_DELTA_DSR)
  1233. icount->dsr++;
  1234. if (msr & EDGEPORT_MSR_DELTA_CD)
  1235. icount->dcd++;
  1236. if (msr & EDGEPORT_MSR_DELTA_RI)
  1237. icount->rng++;
  1238. wake_up_interruptible(&edge_port->port->port.delta_msr_wait);
  1239. }
  1240. /* Save the new modem status */
  1241. edge_port->shadow_msr = msr & 0xf0;
  1242. tty = tty_port_tty_get(&edge_port->port->port);
  1243. /* handle CTS flow control */
  1244. if (tty && C_CRTSCTS(tty)) {
  1245. if (msr & EDGEPORT_MSR_CTS) {
  1246. tty->hw_stopped = 0;
  1247. tty_wakeup(tty);
  1248. } else {
  1249. tty->hw_stopped = 1;
  1250. }
  1251. }
  1252. tty_kref_put(tty);
  1253. }
  1254. static void handle_new_lsr(struct edgeport_port *edge_port, int lsr_data,
  1255. __u8 lsr, __u8 data)
  1256. {
  1257. struct async_icount *icount;
  1258. __u8 new_lsr = (__u8)(lsr & (__u8)(LSR_OVER_ERR | LSR_PAR_ERR |
  1259. LSR_FRM_ERR | LSR_BREAK));
  1260. dev_dbg(&edge_port->port->dev, "%s - %02x\n", __func__, new_lsr);
  1261. edge_port->shadow_lsr = lsr;
  1262. if (new_lsr & LSR_BREAK)
  1263. /*
  1264. * Parity and Framing errors only count if they
  1265. * occur exclusive of a break being received.
  1266. */
  1267. new_lsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
  1268. /* Place LSR data byte into Rx buffer */
  1269. if (lsr_data)
  1270. edge_tty_recv(edge_port->port, &data, 1);
  1271. /* update input line counters */
  1272. icount = &edge_port->port->icount;
  1273. if (new_lsr & LSR_BREAK)
  1274. icount->brk++;
  1275. if (new_lsr & LSR_OVER_ERR)
  1276. icount->overrun++;
  1277. if (new_lsr & LSR_PAR_ERR)
  1278. icount->parity++;
  1279. if (new_lsr & LSR_FRM_ERR)
  1280. icount->frame++;
  1281. }
  1282. static void edge_interrupt_callback(struct urb *urb)
  1283. {
  1284. struct edgeport_serial *edge_serial = urb->context;
  1285. struct usb_serial_port *port;
  1286. struct edgeport_port *edge_port;
  1287. struct device *dev;
  1288. unsigned char *data = urb->transfer_buffer;
  1289. int length = urb->actual_length;
  1290. int port_number;
  1291. int function;
  1292. int retval;
  1293. __u8 lsr;
  1294. __u8 msr;
  1295. int status = urb->status;
  1296. switch (status) {
  1297. case 0:
  1298. /* success */
  1299. break;
  1300. case -ECONNRESET:
  1301. case -ENOENT:
  1302. case -ESHUTDOWN:
  1303. /* this urb is terminated, clean up */
  1304. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
  1305. __func__, status);
  1306. return;
  1307. default:
  1308. dev_err(&urb->dev->dev, "%s - nonzero urb status received: "
  1309. "%d\n", __func__, status);
  1310. goto exit;
  1311. }
  1312. if (!length) {
  1313. dev_dbg(&urb->dev->dev, "%s - no data in urb\n", __func__);
  1314. goto exit;
  1315. }
  1316. dev = &edge_serial->serial->dev->dev;
  1317. usb_serial_debug_data(dev, __func__, length, data);
  1318. if (length != 2) {
  1319. dev_dbg(dev, "%s - expecting packet of size 2, got %d\n", __func__, length);
  1320. goto exit;
  1321. }
  1322. port_number = TIUMP_GET_PORT_FROM_CODE(data[0]);
  1323. function = TIUMP_GET_FUNC_FROM_CODE(data[0]);
  1324. dev_dbg(dev, "%s - port_number %d, function %d, info 0x%x\n", __func__,
  1325. port_number, function, data[1]);
  1326. port = edge_serial->serial->port[port_number];
  1327. edge_port = usb_get_serial_port_data(port);
  1328. if (!edge_port) {
  1329. dev_dbg(dev, "%s - edge_port not found\n", __func__);
  1330. return;
  1331. }
  1332. switch (function) {
  1333. case TIUMP_INTERRUPT_CODE_LSR:
  1334. lsr = map_line_status(data[1]);
  1335. if (lsr & UMP_UART_LSR_DATA_MASK) {
  1336. /* Save the LSR event for bulk read
  1337. completion routine */
  1338. dev_dbg(dev, "%s - LSR Event Port %u LSR Status = %02x\n",
  1339. __func__, port_number, lsr);
  1340. edge_port->lsr_event = 1;
  1341. edge_port->lsr_mask = lsr;
  1342. } else {
  1343. dev_dbg(dev, "%s - ===== Port %d LSR Status = %02x ======\n",
  1344. __func__, port_number, lsr);
  1345. handle_new_lsr(edge_port, 0, lsr, 0);
  1346. }
  1347. break;
  1348. case TIUMP_INTERRUPT_CODE_MSR: /* MSR */
  1349. /* Copy MSR from UMP */
  1350. msr = data[1];
  1351. dev_dbg(dev, "%s - ===== Port %u MSR Status = %02x ======\n",
  1352. __func__, port_number, msr);
  1353. handle_new_msr(edge_port, msr);
  1354. break;
  1355. default:
  1356. dev_err(&urb->dev->dev,
  1357. "%s - Unknown Interrupt code from UMP %x\n",
  1358. __func__, data[1]);
  1359. break;
  1360. }
  1361. exit:
  1362. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1363. if (retval)
  1364. dev_err(&urb->dev->dev,
  1365. "%s - usb_submit_urb failed with result %d\n",
  1366. __func__, retval);
  1367. }
  1368. static void edge_bulk_in_callback(struct urb *urb)
  1369. {
  1370. struct edgeport_port *edge_port = urb->context;
  1371. struct device *dev = &edge_port->port->dev;
  1372. unsigned char *data = urb->transfer_buffer;
  1373. int retval = 0;
  1374. int port_number;
  1375. int status = urb->status;
  1376. switch (status) {
  1377. case 0:
  1378. /* success */
  1379. break;
  1380. case -ECONNRESET:
  1381. case -ENOENT:
  1382. case -ESHUTDOWN:
  1383. /* this urb is terminated, clean up */
  1384. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
  1385. return;
  1386. default:
  1387. dev_err(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n", __func__, status);
  1388. }
  1389. if (status == -EPIPE)
  1390. goto exit;
  1391. if (status) {
  1392. dev_err(&urb->dev->dev, "%s - stopping read!\n", __func__);
  1393. return;
  1394. }
  1395. port_number = edge_port->port->number - edge_port->port->serial->minor;
  1396. if (edge_port->lsr_event) {
  1397. edge_port->lsr_event = 0;
  1398. dev_dbg(dev, "%s ===== Port %u LSR Status = %02x, Data = %02x ======\n",
  1399. __func__, port_number, edge_port->lsr_mask, *data);
  1400. handle_new_lsr(edge_port, 1, edge_port->lsr_mask, *data);
  1401. /* Adjust buffer length/pointer */
  1402. --urb->actual_length;
  1403. ++data;
  1404. }
  1405. if (urb->actual_length) {
  1406. usb_serial_debug_data(dev, __func__, urb->actual_length, data);
  1407. if (edge_port->close_pending)
  1408. dev_dbg(dev, "%s - close pending, dropping data on the floor\n",
  1409. __func__);
  1410. else
  1411. edge_tty_recv(edge_port->port, data,
  1412. urb->actual_length);
  1413. edge_port->port->icount.rx += urb->actual_length;
  1414. }
  1415. exit:
  1416. /* continue read unless stopped */
  1417. spin_lock(&edge_port->ep_lock);
  1418. if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
  1419. retval = usb_submit_urb(urb, GFP_ATOMIC);
  1420. else if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPING)
  1421. edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPED;
  1422. spin_unlock(&edge_port->ep_lock);
  1423. if (retval)
  1424. dev_err(dev, "%s - usb_submit_urb failed with result %d\n", __func__, retval);
  1425. }
  1426. static void edge_tty_recv(struct usb_serial_port *port, unsigned char *data,
  1427. int length)
  1428. {
  1429. int queued;
  1430. queued = tty_insert_flip_string(&port->port, data, length);
  1431. if (queued < length)
  1432. dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
  1433. __func__, length - queued);
  1434. tty_flip_buffer_push(&port->port);
  1435. }
  1436. static void edge_bulk_out_callback(struct urb *urb)
  1437. {
  1438. struct usb_serial_port *port = urb->context;
  1439. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1440. int status = urb->status;
  1441. struct tty_struct *tty;
  1442. edge_port->ep_write_urb_in_use = 0;
  1443. switch (status) {
  1444. case 0:
  1445. /* success */
  1446. break;
  1447. case -ECONNRESET:
  1448. case -ENOENT:
  1449. case -ESHUTDOWN:
  1450. /* this urb is terminated, clean up */
  1451. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
  1452. __func__, status);
  1453. return;
  1454. default:
  1455. dev_err_console(port, "%s - nonzero write bulk status "
  1456. "received: %d\n", __func__, status);
  1457. }
  1458. /* send any buffered data */
  1459. tty = tty_port_tty_get(&port->port);
  1460. edge_send(port, tty);
  1461. tty_kref_put(tty);
  1462. }
  1463. static int edge_open(struct tty_struct *tty, struct usb_serial_port *port)
  1464. {
  1465. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1466. struct edgeport_serial *edge_serial;
  1467. struct usb_device *dev;
  1468. struct urb *urb;
  1469. int port_number;
  1470. int status;
  1471. u16 open_settings;
  1472. u8 transaction_timeout;
  1473. if (edge_port == NULL)
  1474. return -ENODEV;
  1475. port_number = port->number - port->serial->minor;
  1476. switch (port_number) {
  1477. case 0:
  1478. edge_port->uart_base = UMPMEM_BASE_UART1;
  1479. edge_port->dma_address = UMPD_OEDB1_ADDRESS;
  1480. break;
  1481. case 1:
  1482. edge_port->uart_base = UMPMEM_BASE_UART2;
  1483. edge_port->dma_address = UMPD_OEDB2_ADDRESS;
  1484. break;
  1485. default:
  1486. dev_err(&port->dev, "Unknown port number!!!\n");
  1487. return -ENODEV;
  1488. }
  1489. dev_dbg(&port->dev, "%s - port_number = %d, uart_base = %04x, dma_address = %04x\n",
  1490. __func__, port_number, edge_port->uart_base, edge_port->dma_address);
  1491. dev = port->serial->dev;
  1492. /* turn off loopback */
  1493. status = ti_do_config(edge_port, UMPC_SET_CLR_LOOPBACK, 0);
  1494. if (status) {
  1495. dev_err(&port->dev,
  1496. "%s - cannot send clear loopback command, %d\n",
  1497. __func__, status);
  1498. return status;
  1499. }
  1500. /* set up the port settings */
  1501. if (tty)
  1502. edge_set_termios(tty, port, &tty->termios);
  1503. /* open up the port */
  1504. /* milliseconds to timeout for DMA transfer */
  1505. transaction_timeout = 2;
  1506. edge_port->ump_read_timeout =
  1507. max(20, ((transaction_timeout * 3) / 2));
  1508. /* milliseconds to timeout for DMA transfer */
  1509. open_settings = (u8)(UMP_DMA_MODE_CONTINOUS |
  1510. UMP_PIPE_TRANS_TIMEOUT_ENA |
  1511. (transaction_timeout << 2));
  1512. dev_dbg(&port->dev, "%s - Sending UMPC_OPEN_PORT\n", __func__);
  1513. /* Tell TI to open and start the port */
  1514. status = send_cmd(dev, UMPC_OPEN_PORT,
  1515. (u8)(UMPM_UART1_PORT + port_number), open_settings, NULL, 0);
  1516. if (status) {
  1517. dev_err(&port->dev, "%s - cannot send open command, %d\n",
  1518. __func__, status);
  1519. return status;
  1520. }
  1521. /* Start the DMA? */
  1522. status = send_cmd(dev, UMPC_START_PORT,
  1523. (u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
  1524. if (status) {
  1525. dev_err(&port->dev, "%s - cannot send start DMA command, %d\n",
  1526. __func__, status);
  1527. return status;
  1528. }
  1529. /* Clear TX and RX buffers in UMP */
  1530. status = purge_port(port, UMP_PORT_DIR_OUT | UMP_PORT_DIR_IN);
  1531. if (status) {
  1532. dev_err(&port->dev,
  1533. "%s - cannot send clear buffers command, %d\n",
  1534. __func__, status);
  1535. return status;
  1536. }
  1537. /* Read Initial MSR */
  1538. status = ti_vread_sync(dev, UMPC_READ_MSR, 0,
  1539. (__u16)(UMPM_UART1_PORT + port_number),
  1540. &edge_port->shadow_msr, 1);
  1541. if (status) {
  1542. dev_err(&port->dev, "%s - cannot send read MSR command, %d\n",
  1543. __func__, status);
  1544. return status;
  1545. }
  1546. dev_dbg(&port->dev, "ShadowMSR 0x%X\n", edge_port->shadow_msr);
  1547. /* Set Initial MCR */
  1548. edge_port->shadow_mcr = MCR_RTS | MCR_DTR;
  1549. dev_dbg(&port->dev, "ShadowMCR 0x%X\n", edge_port->shadow_mcr);
  1550. edge_serial = edge_port->edge_serial;
  1551. if (mutex_lock_interruptible(&edge_serial->es_lock))
  1552. return -ERESTARTSYS;
  1553. if (edge_serial->num_ports_open == 0) {
  1554. /* we are the first port to open, post the interrupt urb */
  1555. urb = edge_serial->serial->port[0]->interrupt_in_urb;
  1556. if (!urb) {
  1557. dev_err(&port->dev,
  1558. "%s - no interrupt urb present, exiting\n",
  1559. __func__);
  1560. status = -EINVAL;
  1561. goto release_es_lock;
  1562. }
  1563. urb->context = edge_serial;
  1564. status = usb_submit_urb(urb, GFP_KERNEL);
  1565. if (status) {
  1566. dev_err(&port->dev,
  1567. "%s - usb_submit_urb failed with value %d\n",
  1568. __func__, status);
  1569. goto release_es_lock;
  1570. }
  1571. }
  1572. /*
  1573. * reset the data toggle on the bulk endpoints to work around bug in
  1574. * host controllers where things get out of sync some times
  1575. */
  1576. usb_clear_halt(dev, port->write_urb->pipe);
  1577. usb_clear_halt(dev, port->read_urb->pipe);
  1578. /* start up our bulk read urb */
  1579. urb = port->read_urb;
  1580. if (!urb) {
  1581. dev_err(&port->dev, "%s - no read urb present, exiting\n",
  1582. __func__);
  1583. status = -EINVAL;
  1584. goto unlink_int_urb;
  1585. }
  1586. edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
  1587. urb->context = edge_port;
  1588. status = usb_submit_urb(urb, GFP_KERNEL);
  1589. if (status) {
  1590. dev_err(&port->dev,
  1591. "%s - read bulk usb_submit_urb failed with value %d\n",
  1592. __func__, status);
  1593. goto unlink_int_urb;
  1594. }
  1595. ++edge_serial->num_ports_open;
  1596. port->port.drain_delay = 1;
  1597. goto release_es_lock;
  1598. unlink_int_urb:
  1599. if (edge_port->edge_serial->num_ports_open == 0)
  1600. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  1601. release_es_lock:
  1602. mutex_unlock(&edge_serial->es_lock);
  1603. return status;
  1604. }
  1605. static void edge_close(struct usb_serial_port *port)
  1606. {
  1607. struct edgeport_serial *edge_serial;
  1608. struct edgeport_port *edge_port;
  1609. struct usb_serial *serial = port->serial;
  1610. unsigned long flags;
  1611. int port_number;
  1612. edge_serial = usb_get_serial_data(port->serial);
  1613. edge_port = usb_get_serial_port_data(port);
  1614. if (edge_serial == NULL || edge_port == NULL)
  1615. return;
  1616. /* The bulkreadcompletion routine will check
  1617. * this flag and dump add read data */
  1618. edge_port->close_pending = 1;
  1619. usb_kill_urb(port->read_urb);
  1620. usb_kill_urb(port->write_urb);
  1621. edge_port->ep_write_urb_in_use = 0;
  1622. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1623. kfifo_reset_out(&edge_port->write_fifo);
  1624. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1625. dev_dbg(&port->dev, "%s - send umpc_close_port\n", __func__);
  1626. port_number = port->number - port->serial->minor;
  1627. send_cmd(serial->dev, UMPC_CLOSE_PORT,
  1628. (__u8)(UMPM_UART1_PORT + port_number), 0, NULL, 0);
  1629. mutex_lock(&edge_serial->es_lock);
  1630. --edge_port->edge_serial->num_ports_open;
  1631. if (edge_port->edge_serial->num_ports_open <= 0) {
  1632. /* last port is now closed, let's shut down our interrupt urb */
  1633. usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
  1634. edge_port->edge_serial->num_ports_open = 0;
  1635. }
  1636. mutex_unlock(&edge_serial->es_lock);
  1637. edge_port->close_pending = 0;
  1638. }
  1639. static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
  1640. const unsigned char *data, int count)
  1641. {
  1642. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1643. if (count == 0) {
  1644. dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
  1645. return 0;
  1646. }
  1647. if (edge_port == NULL)
  1648. return -ENODEV;
  1649. if (edge_port->close_pending == 1)
  1650. return -ENODEV;
  1651. count = kfifo_in_locked(&edge_port->write_fifo, data, count,
  1652. &edge_port->ep_lock);
  1653. edge_send(port, tty);
  1654. return count;
  1655. }
  1656. static void edge_send(struct usb_serial_port *port, struct tty_struct *tty)
  1657. {
  1658. int count, result;
  1659. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1660. unsigned long flags;
  1661. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1662. if (edge_port->ep_write_urb_in_use) {
  1663. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1664. return;
  1665. }
  1666. count = kfifo_out(&edge_port->write_fifo,
  1667. port->write_urb->transfer_buffer,
  1668. port->bulk_out_size);
  1669. if (count == 0) {
  1670. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1671. return;
  1672. }
  1673. edge_port->ep_write_urb_in_use = 1;
  1674. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1675. usb_serial_debug_data(&port->dev, __func__, count, port->write_urb->transfer_buffer);
  1676. /* set up our urb */
  1677. port->write_urb->transfer_buffer_length = count;
  1678. /* send the data out the bulk port */
  1679. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  1680. if (result) {
  1681. dev_err_console(port,
  1682. "%s - failed submitting write urb, error %d\n",
  1683. __func__, result);
  1684. edge_port->ep_write_urb_in_use = 0;
  1685. /* TODO: reschedule edge_send */
  1686. } else
  1687. edge_port->port->icount.tx += count;
  1688. /* wakeup any process waiting for writes to complete */
  1689. /* there is now more room in the buffer for new writes */
  1690. if (tty)
  1691. tty_wakeup(tty);
  1692. }
  1693. static int edge_write_room(struct tty_struct *tty)
  1694. {
  1695. struct usb_serial_port *port = tty->driver_data;
  1696. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1697. int room = 0;
  1698. unsigned long flags;
  1699. if (edge_port == NULL)
  1700. return 0;
  1701. if (edge_port->close_pending == 1)
  1702. return 0;
  1703. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1704. room = kfifo_avail(&edge_port->write_fifo);
  1705. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1706. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  1707. return room;
  1708. }
  1709. static int edge_chars_in_buffer(struct tty_struct *tty)
  1710. {
  1711. struct usb_serial_port *port = tty->driver_data;
  1712. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1713. int chars = 0;
  1714. unsigned long flags;
  1715. int ret;
  1716. if (edge_port == NULL)
  1717. return 0;
  1718. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1719. chars = kfifo_len(&edge_port->write_fifo);
  1720. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1721. if (!chars) {
  1722. ret = tx_active(edge_port);
  1723. if (ret > 0)
  1724. chars = ret;
  1725. }
  1726. dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
  1727. return chars;
  1728. }
  1729. static void edge_throttle(struct tty_struct *tty)
  1730. {
  1731. struct usb_serial_port *port = tty->driver_data;
  1732. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1733. int status;
  1734. if (edge_port == NULL)
  1735. return;
  1736. /* if we are implementing XON/XOFF, send the stop character */
  1737. if (I_IXOFF(tty)) {
  1738. unsigned char stop_char = STOP_CHAR(tty);
  1739. status = edge_write(tty, port, &stop_char, 1);
  1740. if (status <= 0) {
  1741. dev_err(&port->dev, "%s - failed to write stop character, %d\n", __func__, status);
  1742. }
  1743. }
  1744. /* if we are implementing RTS/CTS, stop reads */
  1745. /* and the Edgeport will clear the RTS line */
  1746. if (C_CRTSCTS(tty))
  1747. stop_read(edge_port);
  1748. }
  1749. static void edge_unthrottle(struct tty_struct *tty)
  1750. {
  1751. struct usb_serial_port *port = tty->driver_data;
  1752. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1753. int status;
  1754. if (edge_port == NULL)
  1755. return;
  1756. /* if we are implementing XON/XOFF, send the start character */
  1757. if (I_IXOFF(tty)) {
  1758. unsigned char start_char = START_CHAR(tty);
  1759. status = edge_write(tty, port, &start_char, 1);
  1760. if (status <= 0) {
  1761. dev_err(&port->dev, "%s - failed to write start character, %d\n", __func__, status);
  1762. }
  1763. }
  1764. /* if we are implementing RTS/CTS, restart reads */
  1765. /* are the Edgeport will assert the RTS line */
  1766. if (C_CRTSCTS(tty)) {
  1767. status = restart_read(edge_port);
  1768. if (status)
  1769. dev_err(&port->dev,
  1770. "%s - read bulk usb_submit_urb failed: %d\n",
  1771. __func__, status);
  1772. }
  1773. }
  1774. static void stop_read(struct edgeport_port *edge_port)
  1775. {
  1776. unsigned long flags;
  1777. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1778. if (edge_port->ep_read_urb_state == EDGE_READ_URB_RUNNING)
  1779. edge_port->ep_read_urb_state = EDGE_READ_URB_STOPPING;
  1780. edge_port->shadow_mcr &= ~MCR_RTS;
  1781. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1782. }
  1783. static int restart_read(struct edgeport_port *edge_port)
  1784. {
  1785. struct urb *urb;
  1786. int status = 0;
  1787. unsigned long flags;
  1788. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1789. if (edge_port->ep_read_urb_state == EDGE_READ_URB_STOPPED) {
  1790. urb = edge_port->port->read_urb;
  1791. status = usb_submit_urb(urb, GFP_ATOMIC);
  1792. }
  1793. edge_port->ep_read_urb_state = EDGE_READ_URB_RUNNING;
  1794. edge_port->shadow_mcr |= MCR_RTS;
  1795. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1796. return status;
  1797. }
  1798. static void change_port_settings(struct tty_struct *tty,
  1799. struct edgeport_port *edge_port, struct ktermios *old_termios)
  1800. {
  1801. struct device *dev = &edge_port->port->dev;
  1802. struct ump_uart_config *config;
  1803. int baud;
  1804. unsigned cflag;
  1805. int status;
  1806. int port_number = edge_port->port->number -
  1807. edge_port->port->serial->minor;
  1808. dev_dbg(dev, "%s - port %d\n", __func__, edge_port->port->number);
  1809. config = kmalloc (sizeof (*config), GFP_KERNEL);
  1810. if (!config) {
  1811. tty->termios = *old_termios;
  1812. dev_err(dev, "%s - out of memory\n", __func__);
  1813. return;
  1814. }
  1815. cflag = tty->termios.c_cflag;
  1816. config->wFlags = 0;
  1817. /* These flags must be set */
  1818. config->wFlags |= UMP_MASK_UART_FLAGS_RECEIVE_MS_INT;
  1819. config->wFlags |= UMP_MASK_UART_FLAGS_AUTO_START_ON_ERR;
  1820. config->bUartMode = (__u8)(edge_port->bUartMode);
  1821. switch (cflag & CSIZE) {
  1822. case CS5:
  1823. config->bDataBits = UMP_UART_CHAR5BITS;
  1824. dev_dbg(dev, "%s - data bits = 5\n", __func__);
  1825. break;
  1826. case CS6:
  1827. config->bDataBits = UMP_UART_CHAR6BITS;
  1828. dev_dbg(dev, "%s - data bits = 6\n", __func__);
  1829. break;
  1830. case CS7:
  1831. config->bDataBits = UMP_UART_CHAR7BITS;
  1832. dev_dbg(dev, "%s - data bits = 7\n", __func__);
  1833. break;
  1834. default:
  1835. case CS8:
  1836. config->bDataBits = UMP_UART_CHAR8BITS;
  1837. dev_dbg(dev, "%s - data bits = 8\n", __func__);
  1838. break;
  1839. }
  1840. if (cflag & PARENB) {
  1841. if (cflag & PARODD) {
  1842. config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
  1843. config->bParity = UMP_UART_ODDPARITY;
  1844. dev_dbg(dev, "%s - parity = odd\n", __func__);
  1845. } else {
  1846. config->wFlags |= UMP_MASK_UART_FLAGS_PARITY;
  1847. config->bParity = UMP_UART_EVENPARITY;
  1848. dev_dbg(dev, "%s - parity = even\n", __func__);
  1849. }
  1850. } else {
  1851. config->bParity = UMP_UART_NOPARITY;
  1852. dev_dbg(dev, "%s - parity = none\n", __func__);
  1853. }
  1854. if (cflag & CSTOPB) {
  1855. config->bStopBits = UMP_UART_STOPBIT2;
  1856. dev_dbg(dev, "%s - stop bits = 2\n", __func__);
  1857. } else {
  1858. config->bStopBits = UMP_UART_STOPBIT1;
  1859. dev_dbg(dev, "%s - stop bits = 1\n", __func__);
  1860. }
  1861. /* figure out the flow control settings */
  1862. if (cflag & CRTSCTS) {
  1863. config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X_CTS_FLOW;
  1864. config->wFlags |= UMP_MASK_UART_FLAGS_RTS_FLOW;
  1865. dev_dbg(dev, "%s - RTS/CTS is enabled\n", __func__);
  1866. } else {
  1867. dev_dbg(dev, "%s - RTS/CTS is disabled\n", __func__);
  1868. tty->hw_stopped = 0;
  1869. restart_read(edge_port);
  1870. }
  1871. /* if we are implementing XON/XOFF, set the start and stop
  1872. character in the device */
  1873. config->cXon = START_CHAR(tty);
  1874. config->cXoff = STOP_CHAR(tty);
  1875. /* if we are implementing INBOUND XON/XOFF */
  1876. if (I_IXOFF(tty)) {
  1877. config->wFlags |= UMP_MASK_UART_FLAGS_IN_X;
  1878. dev_dbg(dev, "%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
  1879. __func__, config->cXon, config->cXoff);
  1880. } else
  1881. dev_dbg(dev, "%s - INBOUND XON/XOFF is disabled\n", __func__);
  1882. /* if we are implementing OUTBOUND XON/XOFF */
  1883. if (I_IXON(tty)) {
  1884. config->wFlags |= UMP_MASK_UART_FLAGS_OUT_X;
  1885. dev_dbg(dev, "%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x\n",
  1886. __func__, config->cXon, config->cXoff);
  1887. } else
  1888. dev_dbg(dev, "%s - OUTBOUND XON/XOFF is disabled\n", __func__);
  1889. tty->termios.c_cflag &= ~CMSPAR;
  1890. /* Round the baud rate */
  1891. baud = tty_get_baud_rate(tty);
  1892. if (!baud) {
  1893. /* pick a default, any default... */
  1894. baud = 9600;
  1895. } else
  1896. tty_encode_baud_rate(tty, baud, baud);
  1897. edge_port->baud_rate = baud;
  1898. config->wBaudRate = (__u16)((461550L + baud/2) / baud);
  1899. /* FIXME: Recompute actual baud from divisor here */
  1900. dev_dbg(dev, "%s - baud rate = %d, wBaudRate = %d\n", __func__, baud, config->wBaudRate);
  1901. dev_dbg(dev, "wBaudRate: %d\n", (int)(461550L / config->wBaudRate));
  1902. dev_dbg(dev, "wFlags: 0x%x\n", config->wFlags);
  1903. dev_dbg(dev, "bDataBits: %d\n", config->bDataBits);
  1904. dev_dbg(dev, "bParity: %d\n", config->bParity);
  1905. dev_dbg(dev, "bStopBits: %d\n", config->bStopBits);
  1906. dev_dbg(dev, "cXon: %d\n", config->cXon);
  1907. dev_dbg(dev, "cXoff: %d\n", config->cXoff);
  1908. dev_dbg(dev, "bUartMode: %d\n", config->bUartMode);
  1909. /* move the word values into big endian mode */
  1910. cpu_to_be16s(&config->wFlags);
  1911. cpu_to_be16s(&config->wBaudRate);
  1912. status = send_cmd(edge_port->port->serial->dev, UMPC_SET_CONFIG,
  1913. (__u8)(UMPM_UART1_PORT + port_number),
  1914. 0, (__u8 *)config, sizeof(*config));
  1915. if (status)
  1916. dev_dbg(dev, "%s - error %d when trying to write config to device\n",
  1917. __func__, status);
  1918. kfree(config);
  1919. }
  1920. static void edge_set_termios(struct tty_struct *tty,
  1921. struct usb_serial_port *port, struct ktermios *old_termios)
  1922. {
  1923. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1924. unsigned int cflag;
  1925. cflag = tty->termios.c_cflag;
  1926. dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
  1927. tty->termios.c_cflag, tty->termios.c_iflag);
  1928. dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
  1929. old_termios->c_cflag, old_termios->c_iflag);
  1930. dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
  1931. if (edge_port == NULL)
  1932. return;
  1933. /* change the port settings to the new ones specified */
  1934. change_port_settings(tty, edge_port, old_termios);
  1935. }
  1936. static int edge_tiocmset(struct tty_struct *tty,
  1937. unsigned int set, unsigned int clear)
  1938. {
  1939. struct usb_serial_port *port = tty->driver_data;
  1940. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1941. unsigned int mcr;
  1942. unsigned long flags;
  1943. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1944. mcr = edge_port->shadow_mcr;
  1945. if (set & TIOCM_RTS)
  1946. mcr |= MCR_RTS;
  1947. if (set & TIOCM_DTR)
  1948. mcr |= MCR_DTR;
  1949. if (set & TIOCM_LOOP)
  1950. mcr |= MCR_LOOPBACK;
  1951. if (clear & TIOCM_RTS)
  1952. mcr &= ~MCR_RTS;
  1953. if (clear & TIOCM_DTR)
  1954. mcr &= ~MCR_DTR;
  1955. if (clear & TIOCM_LOOP)
  1956. mcr &= ~MCR_LOOPBACK;
  1957. edge_port->shadow_mcr = mcr;
  1958. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1959. restore_mcr(edge_port, mcr);
  1960. return 0;
  1961. }
  1962. static int edge_tiocmget(struct tty_struct *tty)
  1963. {
  1964. struct usb_serial_port *port = tty->driver_data;
  1965. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1966. unsigned int result = 0;
  1967. unsigned int msr;
  1968. unsigned int mcr;
  1969. unsigned long flags;
  1970. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1971. msr = edge_port->shadow_msr;
  1972. mcr = edge_port->shadow_mcr;
  1973. result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1974. | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1975. | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1976. | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
  1977. | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1978. | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1979. dev_dbg(&port->dev, "%s -- %x\n", __func__, result);
  1980. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1981. return result;
  1982. }
  1983. static int get_serial_info(struct edgeport_port *edge_port,
  1984. struct serial_struct __user *retinfo)
  1985. {
  1986. struct serial_struct tmp;
  1987. unsigned cwait;
  1988. if (!retinfo)
  1989. return -EFAULT;
  1990. cwait = edge_port->port->port.closing_wait;
  1991. if (cwait != ASYNC_CLOSING_WAIT_NONE)
  1992. cwait = jiffies_to_msecs(cwait) / 10;
  1993. memset(&tmp, 0, sizeof(tmp));
  1994. tmp.type = PORT_16550A;
  1995. tmp.line = edge_port->port->serial->minor;
  1996. tmp.port = edge_port->port->number;
  1997. tmp.irq = 0;
  1998. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1999. tmp.xmit_fifo_size = edge_port->port->bulk_out_size;
  2000. tmp.baud_base = 9600;
  2001. tmp.close_delay = 5*HZ;
  2002. tmp.closing_wait = cwait;
  2003. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  2004. return -EFAULT;
  2005. return 0;
  2006. }
  2007. static int edge_ioctl(struct tty_struct *tty,
  2008. unsigned int cmd, unsigned long arg)
  2009. {
  2010. struct usb_serial_port *port = tty->driver_data;
  2011. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2012. dev_dbg(&port->dev, "%s - port %d, cmd = 0x%x\n", __func__, port->number, cmd);
  2013. switch (cmd) {
  2014. case TIOCGSERIAL:
  2015. dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
  2016. return get_serial_info(edge_port,
  2017. (struct serial_struct __user *) arg);
  2018. }
  2019. return -ENOIOCTLCMD;
  2020. }
  2021. static void edge_break(struct tty_struct *tty, int break_state)
  2022. {
  2023. struct usb_serial_port *port = tty->driver_data;
  2024. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2025. int status;
  2026. int bv = 0; /* Off */
  2027. if (break_state == -1)
  2028. bv = 1; /* On */
  2029. status = ti_do_config(edge_port, UMPC_SET_CLR_BREAK, bv);
  2030. if (status)
  2031. dev_dbg(&port->dev, "%s - error %d sending break set/clear command.\n",
  2032. __func__, status);
  2033. }
  2034. static int edge_startup(struct usb_serial *serial)
  2035. {
  2036. struct edgeport_serial *edge_serial;
  2037. int status;
  2038. /* create our private serial structure */
  2039. edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
  2040. if (edge_serial == NULL) {
  2041. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  2042. return -ENOMEM;
  2043. }
  2044. mutex_init(&edge_serial->es_lock);
  2045. edge_serial->serial = serial;
  2046. usb_set_serial_data(serial, edge_serial);
  2047. status = download_fw(edge_serial);
  2048. if (status) {
  2049. kfree(edge_serial);
  2050. return status;
  2051. }
  2052. return 0;
  2053. }
  2054. static void edge_disconnect(struct usb_serial *serial)
  2055. {
  2056. }
  2057. static void edge_release(struct usb_serial *serial)
  2058. {
  2059. kfree(usb_get_serial_data(serial));
  2060. }
  2061. static int edge_port_probe(struct usb_serial_port *port)
  2062. {
  2063. struct edgeport_port *edge_port;
  2064. int ret;
  2065. edge_port = kzalloc(sizeof(*edge_port), GFP_KERNEL);
  2066. if (!edge_port)
  2067. return -ENOMEM;
  2068. ret = kfifo_alloc(&edge_port->write_fifo, EDGE_OUT_BUF_SIZE,
  2069. GFP_KERNEL);
  2070. if (ret) {
  2071. kfree(edge_port);
  2072. return -ENOMEM;
  2073. }
  2074. spin_lock_init(&edge_port->ep_lock);
  2075. edge_port->port = port;
  2076. edge_port->edge_serial = usb_get_serial_data(port->serial);
  2077. edge_port->bUartMode = default_uart_mode;
  2078. usb_set_serial_port_data(port, edge_port);
  2079. ret = edge_create_sysfs_attrs(port);
  2080. if (ret) {
  2081. kfifo_free(&edge_port->write_fifo);
  2082. kfree(edge_port);
  2083. return ret;
  2084. }
  2085. port->port.closing_wait = msecs_to_jiffies(closing_wait * 10);
  2086. return 0;
  2087. }
  2088. static int edge_port_remove(struct usb_serial_port *port)
  2089. {
  2090. struct edgeport_port *edge_port;
  2091. edge_port = usb_get_serial_port_data(port);
  2092. edge_remove_sysfs_attrs(port);
  2093. kfifo_free(&edge_port->write_fifo);
  2094. kfree(edge_port);
  2095. return 0;
  2096. }
  2097. /* Sysfs Attributes */
  2098. static ssize_t show_uart_mode(struct device *dev,
  2099. struct device_attribute *attr, char *buf)
  2100. {
  2101. struct usb_serial_port *port = to_usb_serial_port(dev);
  2102. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2103. return sprintf(buf, "%d\n", edge_port->bUartMode);
  2104. }
  2105. static ssize_t store_uart_mode(struct device *dev,
  2106. struct device_attribute *attr, const char *valbuf, size_t count)
  2107. {
  2108. struct usb_serial_port *port = to_usb_serial_port(dev);
  2109. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  2110. unsigned int v = simple_strtoul(valbuf, NULL, 0);
  2111. dev_dbg(dev, "%s: setting uart_mode = %d\n", __func__, v);
  2112. if (v < 256)
  2113. edge_port->bUartMode = v;
  2114. else
  2115. dev_err(dev, "%s - uart_mode %d is invalid\n", __func__, v);
  2116. return count;
  2117. }
  2118. static DEVICE_ATTR(uart_mode, S_IWUSR | S_IRUGO, show_uart_mode,
  2119. store_uart_mode);
  2120. static int edge_create_sysfs_attrs(struct usb_serial_port *port)
  2121. {
  2122. return device_create_file(&port->dev, &dev_attr_uart_mode);
  2123. }
  2124. static int edge_remove_sysfs_attrs(struct usb_serial_port *port)
  2125. {
  2126. device_remove_file(&port->dev, &dev_attr_uart_mode);
  2127. return 0;
  2128. }
  2129. static struct usb_serial_driver edgeport_1port_device = {
  2130. .driver = {
  2131. .owner = THIS_MODULE,
  2132. .name = "edgeport_ti_1",
  2133. },
  2134. .description = "Edgeport TI 1 port adapter",
  2135. .id_table = edgeport_1port_id_table,
  2136. .num_ports = 1,
  2137. .open = edge_open,
  2138. .close = edge_close,
  2139. .throttle = edge_throttle,
  2140. .unthrottle = edge_unthrottle,
  2141. .attach = edge_startup,
  2142. .disconnect = edge_disconnect,
  2143. .release = edge_release,
  2144. .port_probe = edge_port_probe,
  2145. .port_remove = edge_port_remove,
  2146. .ioctl = edge_ioctl,
  2147. .set_termios = edge_set_termios,
  2148. .tiocmget = edge_tiocmget,
  2149. .tiocmset = edge_tiocmset,
  2150. .tiocmiwait = usb_serial_generic_tiocmiwait,
  2151. .get_icount = usb_serial_generic_get_icount,
  2152. .write = edge_write,
  2153. .write_room = edge_write_room,
  2154. .chars_in_buffer = edge_chars_in_buffer,
  2155. .break_ctl = edge_break,
  2156. .read_int_callback = edge_interrupt_callback,
  2157. .read_bulk_callback = edge_bulk_in_callback,
  2158. .write_bulk_callback = edge_bulk_out_callback,
  2159. };
  2160. static struct usb_serial_driver edgeport_2port_device = {
  2161. .driver = {
  2162. .owner = THIS_MODULE,
  2163. .name = "edgeport_ti_2",
  2164. },
  2165. .description = "Edgeport TI 2 port adapter",
  2166. .id_table = edgeport_2port_id_table,
  2167. .num_ports = 2,
  2168. .open = edge_open,
  2169. .close = edge_close,
  2170. .throttle = edge_throttle,
  2171. .unthrottle = edge_unthrottle,
  2172. .attach = edge_startup,
  2173. .disconnect = edge_disconnect,
  2174. .release = edge_release,
  2175. .port_probe = edge_port_probe,
  2176. .port_remove = edge_port_remove,
  2177. .ioctl = edge_ioctl,
  2178. .set_termios = edge_set_termios,
  2179. .tiocmget = edge_tiocmget,
  2180. .tiocmset = edge_tiocmset,
  2181. .tiocmiwait = usb_serial_generic_tiocmiwait,
  2182. .get_icount = usb_serial_generic_get_icount,
  2183. .write = edge_write,
  2184. .write_room = edge_write_room,
  2185. .chars_in_buffer = edge_chars_in_buffer,
  2186. .break_ctl = edge_break,
  2187. .read_int_callback = edge_interrupt_callback,
  2188. .read_bulk_callback = edge_bulk_in_callback,
  2189. .write_bulk_callback = edge_bulk_out_callback,
  2190. };
  2191. static struct usb_serial_driver * const serial_drivers[] = {
  2192. &edgeport_1port_device, &edgeport_2port_device, NULL
  2193. };
  2194. module_usb_serial_driver(serial_drivers, id_table_combined);
  2195. MODULE_AUTHOR(DRIVER_AUTHOR);
  2196. MODULE_DESCRIPTION(DRIVER_DESC);
  2197. MODULE_LICENSE("GPL");
  2198. MODULE_FIRMWARE("edgeport/down3.bin");
  2199. module_param(closing_wait, int, S_IRUGO | S_IWUSR);
  2200. MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain, in .01 secs");
  2201. module_param(ignore_cpu_rev, bool, S_IRUGO | S_IWUSR);
  2202. MODULE_PARM_DESC(ignore_cpu_rev,
  2203. "Ignore the cpu revision when connecting to a device");
  2204. module_param(default_uart_mode, int, S_IRUGO | S_IWUSR);
  2205. MODULE_PARM_DESC(default_uart_mode, "Default uart_mode, 0=RS232, ...");