hso.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. *
  4. * Driver for Option High Speed Mobile Devices.
  5. *
  6. * Copyright (C) 2008 Option International
  7. * Filip Aben <f.aben@option.com>
  8. * Denis Joseph Barrow <d.barow@option.com>
  9. * Jan Dumon <j.dumon@option.com>
  10. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  11. * <ajb@spheresystems.co.uk>
  12. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  13. * Copyright (C) 2008 Novell, Inc.
  14. *
  15. *****************************************************************************/
  16. /******************************************************************************
  17. *
  18. * Description of the device:
  19. *
  20. * Interface 0: Contains the IP network interface on the bulk end points.
  21. * The multiplexed serial ports are using the interrupt and
  22. * control endpoints.
  23. * Interrupt contains a bitmap telling which multiplexed
  24. * serialport needs servicing.
  25. *
  26. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  27. * port is opened, as this have a huge impact on the network port
  28. * throughput.
  29. *
  30. * Interface 2: Standard modem interface - circuit switched interface, this
  31. * can be used to make a standard ppp connection however it
  32. * should not be used in conjunction with the IP network interface
  33. * enabled for USB performance reasons i.e. if using this set
  34. * ideally disable_net=1.
  35. *
  36. *****************************************************************************/
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #include <linux/sched/signal.h>
  39. #include <linux/slab.h>
  40. #include <linux/init.h>
  41. #include <linux/delay.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/module.h>
  44. #include <linux/ethtool.h>
  45. #include <linux/usb.h>
  46. #include <linux/tty.h>
  47. #include <linux/tty_driver.h>
  48. #include <linux/tty_flip.h>
  49. #include <linux/kmod.h>
  50. #include <linux/rfkill.h>
  51. #include <linux/ip.h>
  52. #include <linux/uaccess.h>
  53. #include <linux/usb/cdc.h>
  54. #include <net/arp.h>
  55. #include <asm/byteorder.h>
  56. #include <linux/serial_core.h>
  57. #include <linux/serial.h>
  58. #define MOD_AUTHOR "Option Wireless"
  59. #define MOD_DESCRIPTION "USB High Speed Option driver"
  60. #define HSO_MAX_NET_DEVICES 10
  61. #define HSO__MAX_MTU 2048
  62. #define DEFAULT_MTU 1500
  63. #define DEFAULT_MRU 1500
  64. #define CTRL_URB_RX_SIZE 1024
  65. #define CTRL_URB_TX_SIZE 64
  66. #define BULK_URB_RX_SIZE 4096
  67. #define BULK_URB_TX_SIZE 8192
  68. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  69. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  70. #define MUX_BULK_RX_BUF_COUNT 4
  71. #define USB_TYPE_OPTION_VENDOR 0x20
  72. /* These definitions are used with the struct hso_net flags element */
  73. /* - use *_bit operations on it. (bit indices not values.) */
  74. #define HSO_NET_RUNNING 0
  75. #define HSO_NET_TX_TIMEOUT (HZ*10)
  76. #define HSO_SERIAL_MAGIC 0x48534f31
  77. /* Number of ttys to handle */
  78. #define HSO_SERIAL_TTY_MINORS 256
  79. #define MAX_RX_URBS 2
  80. /*****************************************************************************/
  81. /* Debugging functions */
  82. /*****************************************************************************/
  83. #define hso_dbg(lvl, fmt, ...) \
  84. do { \
  85. if ((lvl) & debug) \
  86. pr_info("[%d:%s] " fmt, \
  87. __LINE__, __func__, ##__VA_ARGS__); \
  88. } while (0)
  89. /*****************************************************************************/
  90. /* Enumerators */
  91. /*****************************************************************************/
  92. enum pkt_parse_state {
  93. WAIT_IP,
  94. WAIT_DATA,
  95. WAIT_SYNC
  96. };
  97. /*****************************************************************************/
  98. /* Structs */
  99. /*****************************************************************************/
  100. struct hso_shared_int {
  101. struct usb_endpoint_descriptor *intr_endp;
  102. void *shared_intr_buf;
  103. struct urb *shared_intr_urb;
  104. struct usb_device *usb;
  105. int use_count;
  106. int ref_count;
  107. struct mutex shared_int_lock;
  108. };
  109. struct hso_net {
  110. struct hso_device *parent;
  111. struct net_device *net;
  112. struct rfkill *rfkill;
  113. char name[24];
  114. struct usb_endpoint_descriptor *in_endp;
  115. struct usb_endpoint_descriptor *out_endp;
  116. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  117. struct urb *mux_bulk_tx_urb;
  118. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  119. void *mux_bulk_tx_buf;
  120. struct sk_buff *skb_rx_buf;
  121. struct sk_buff *skb_tx_buf;
  122. enum pkt_parse_state rx_parse_state;
  123. spinlock_t net_lock;
  124. unsigned short rx_buf_size;
  125. unsigned short rx_buf_missing;
  126. struct iphdr rx_ip_hdr;
  127. unsigned long flags;
  128. };
  129. enum rx_ctrl_state{
  130. RX_IDLE,
  131. RX_SENT,
  132. RX_PENDING
  133. };
  134. #define BM_REQUEST_TYPE (0xa1)
  135. #define B_NOTIFICATION (0x20)
  136. #define W_VALUE (0x0)
  137. #define W_LENGTH (0x2)
  138. #define B_OVERRUN (0x1<<6)
  139. #define B_PARITY (0x1<<5)
  140. #define B_FRAMING (0x1<<4)
  141. #define B_RING_SIGNAL (0x1<<3)
  142. #define B_BREAK (0x1<<2)
  143. #define B_TX_CARRIER (0x1<<1)
  144. #define B_RX_CARRIER (0x1<<0)
  145. struct hso_serial_state_notification {
  146. u8 bmRequestType;
  147. u8 bNotification;
  148. u16 wValue;
  149. u16 wIndex;
  150. u16 wLength;
  151. u16 UART_state_bitmap;
  152. } __packed;
  153. struct hso_tiocmget {
  154. struct mutex mutex;
  155. wait_queue_head_t waitq;
  156. int intr_completed;
  157. struct usb_endpoint_descriptor *endp;
  158. struct urb *urb;
  159. struct hso_serial_state_notification *serial_state_notification;
  160. u16 prev_UART_state_bitmap;
  161. struct uart_icount icount;
  162. };
  163. struct hso_serial {
  164. struct hso_device *parent;
  165. int magic;
  166. u8 minor;
  167. struct hso_shared_int *shared_int;
  168. /* rx/tx urb could be either a bulk urb or a control urb depending
  169. on which serial port it is used on. */
  170. struct urb *rx_urb[MAX_RX_URBS];
  171. u8 num_rx_urbs;
  172. u8 *rx_data[MAX_RX_URBS];
  173. u16 rx_data_length; /* should contain allocated length */
  174. struct urb *tx_urb;
  175. u8 *tx_data;
  176. u8 *tx_buffer;
  177. u16 tx_data_length; /* should contain allocated length */
  178. u16 tx_data_count;
  179. u16 tx_buffer_count;
  180. struct usb_ctrlrequest ctrl_req_tx;
  181. struct usb_ctrlrequest ctrl_req_rx;
  182. struct usb_endpoint_descriptor *in_endp;
  183. struct usb_endpoint_descriptor *out_endp;
  184. enum rx_ctrl_state rx_state;
  185. u8 rts_state;
  186. u8 dtr_state;
  187. unsigned tx_urb_used:1;
  188. struct tty_port port;
  189. /* from usb_serial_port */
  190. spinlock_t serial_lock;
  191. int (*write_data) (struct hso_serial *serial);
  192. struct hso_tiocmget *tiocmget;
  193. /* Hacks required to get flow control
  194. * working on the serial receive buffers
  195. * so as not to drop characters on the floor.
  196. */
  197. int curr_rx_urb_idx;
  198. u8 rx_urb_filled[MAX_RX_URBS];
  199. struct tasklet_struct unthrottle_tasklet;
  200. };
  201. struct hso_device {
  202. union {
  203. struct hso_serial *dev_serial;
  204. struct hso_net *dev_net;
  205. } port_data;
  206. u32 port_spec;
  207. u8 is_active;
  208. u8 usb_gone;
  209. struct work_struct async_get_intf;
  210. struct work_struct async_put_intf;
  211. struct usb_device *usb;
  212. struct usb_interface *interface;
  213. struct device *dev;
  214. struct kref ref;
  215. struct mutex mutex;
  216. };
  217. /* Type of interface */
  218. #define HSO_INTF_MASK 0xFF00
  219. #define HSO_INTF_MUX 0x0100
  220. #define HSO_INTF_BULK 0x0200
  221. /* Type of port */
  222. #define HSO_PORT_MASK 0xFF
  223. #define HSO_PORT_NO_PORT 0x0
  224. #define HSO_PORT_CONTROL 0x1
  225. #define HSO_PORT_APP 0x2
  226. #define HSO_PORT_GPS 0x3
  227. #define HSO_PORT_PCSC 0x4
  228. #define HSO_PORT_APP2 0x5
  229. #define HSO_PORT_GPS_CONTROL 0x6
  230. #define HSO_PORT_MSD 0x7
  231. #define HSO_PORT_VOICE 0x8
  232. #define HSO_PORT_DIAG2 0x9
  233. #define HSO_PORT_DIAG 0x10
  234. #define HSO_PORT_MODEM 0x11
  235. #define HSO_PORT_NETWORK 0x12
  236. /* Additional device info */
  237. #define HSO_INFO_MASK 0xFF000000
  238. #define HSO_INFO_CRC_BUG 0x01000000
  239. /*****************************************************************************/
  240. /* Prototypes */
  241. /*****************************************************************************/
  242. /* Serial driver functions */
  243. static int hso_serial_tiocmset(struct tty_struct *tty,
  244. unsigned int set, unsigned int clear);
  245. static void ctrl_callback(struct urb *urb);
  246. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  247. static void hso_kick_transmit(struct hso_serial *serial);
  248. /* Helper functions */
  249. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  250. struct usb_device *usb, gfp_t gfp);
  251. static void handle_usb_error(int status, const char *function,
  252. struct hso_device *hso_dev);
  253. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  254. int type, int dir);
  255. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  256. static void hso_free_interface(struct usb_interface *intf);
  257. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  258. static int hso_stop_serial_device(struct hso_device *hso_dev);
  259. static int hso_start_net_device(struct hso_device *hso_dev);
  260. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  261. static int hso_stop_net_device(struct hso_device *hso_dev);
  262. static void hso_serial_ref_free(struct kref *ref);
  263. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  264. static int hso_mux_serial_read(struct hso_serial *serial);
  265. static void async_get_intf(struct work_struct *data);
  266. static void async_put_intf(struct work_struct *data);
  267. static int hso_put_activity(struct hso_device *hso_dev);
  268. static int hso_get_activity(struct hso_device *hso_dev);
  269. static void tiocmget_intr_callback(struct urb *urb);
  270. /*****************************************************************************/
  271. /* Helping functions */
  272. /*****************************************************************************/
  273. /* #define DEBUG */
  274. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  275. {
  276. return hso_dev->port_data.dev_net;
  277. }
  278. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  279. {
  280. return hso_dev->port_data.dev_serial;
  281. }
  282. /* Debugging functions */
  283. #ifdef DEBUG
  284. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  285. unsigned int len)
  286. {
  287. static char name[255];
  288. sprintf(name, "hso[%d:%s]", line_count, func_name);
  289. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  290. }
  291. #define DUMP(buf_, len_) \
  292. dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
  293. #define DUMP1(buf_, len_) \
  294. do { \
  295. if (0x01 & debug) \
  296. DUMP(buf_, len_); \
  297. } while (0)
  298. #else
  299. #define DUMP(buf_, len_)
  300. #define DUMP1(buf_, len_)
  301. #endif
  302. /* module parameters */
  303. static int debug;
  304. static int tty_major;
  305. static int disable_net;
  306. /* driver info */
  307. static const char driver_name[] = "hso";
  308. static const char tty_filename[] = "ttyHS";
  309. /* the usb driver itself (registered in hso_init) */
  310. static struct usb_driver hso_driver;
  311. /* serial structures */
  312. static struct tty_driver *tty_drv;
  313. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  314. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  315. static DEFINE_SPINLOCK(serial_table_lock);
  316. static const s32 default_port_spec[] = {
  317. HSO_INTF_MUX | HSO_PORT_NETWORK,
  318. HSO_INTF_BULK | HSO_PORT_DIAG,
  319. HSO_INTF_BULK | HSO_PORT_MODEM,
  320. 0
  321. };
  322. static const s32 icon321_port_spec[] = {
  323. HSO_INTF_MUX | HSO_PORT_NETWORK,
  324. HSO_INTF_BULK | HSO_PORT_DIAG2,
  325. HSO_INTF_BULK | HSO_PORT_MODEM,
  326. HSO_INTF_BULK | HSO_PORT_DIAG,
  327. 0
  328. };
  329. #define default_port_device(vendor, product) \
  330. USB_DEVICE(vendor, product), \
  331. .driver_info = (kernel_ulong_t)default_port_spec
  332. #define icon321_port_device(vendor, product) \
  333. USB_DEVICE(vendor, product), \
  334. .driver_info = (kernel_ulong_t)icon321_port_spec
  335. /* list of devices we support */
  336. static const struct usb_device_id hso_ids[] = {
  337. {default_port_device(0x0af0, 0x6711)},
  338. {default_port_device(0x0af0, 0x6731)},
  339. {default_port_device(0x0af0, 0x6751)},
  340. {default_port_device(0x0af0, 0x6771)},
  341. {default_port_device(0x0af0, 0x6791)},
  342. {default_port_device(0x0af0, 0x6811)},
  343. {default_port_device(0x0af0, 0x6911)},
  344. {default_port_device(0x0af0, 0x6951)},
  345. {default_port_device(0x0af0, 0x6971)},
  346. {default_port_device(0x0af0, 0x7011)},
  347. {default_port_device(0x0af0, 0x7031)},
  348. {default_port_device(0x0af0, 0x7051)},
  349. {default_port_device(0x0af0, 0x7071)},
  350. {default_port_device(0x0af0, 0x7111)},
  351. {default_port_device(0x0af0, 0x7211)},
  352. {default_port_device(0x0af0, 0x7251)},
  353. {default_port_device(0x0af0, 0x7271)},
  354. {default_port_device(0x0af0, 0x7311)},
  355. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  356. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  357. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  358. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  359. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  360. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  361. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  362. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  363. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  364. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  365. {USB_DEVICE(0x0af0, 0x7701)},
  366. {USB_DEVICE(0x0af0, 0x7706)},
  367. {USB_DEVICE(0x0af0, 0x7801)},
  368. {USB_DEVICE(0x0af0, 0x7901)},
  369. {USB_DEVICE(0x0af0, 0x7A01)},
  370. {USB_DEVICE(0x0af0, 0x7A05)},
  371. {USB_DEVICE(0x0af0, 0x8200)},
  372. {USB_DEVICE(0x0af0, 0x8201)},
  373. {USB_DEVICE(0x0af0, 0x8300)},
  374. {USB_DEVICE(0x0af0, 0x8302)},
  375. {USB_DEVICE(0x0af0, 0x8304)},
  376. {USB_DEVICE(0x0af0, 0x8400)},
  377. {USB_DEVICE(0x0af0, 0x8600)},
  378. {USB_DEVICE(0x0af0, 0x8800)},
  379. {USB_DEVICE(0x0af0, 0x8900)},
  380. {USB_DEVICE(0x0af0, 0x9000)},
  381. {USB_DEVICE(0x0af0, 0x9200)}, /* Option GTM671WFS */
  382. {USB_DEVICE(0x0af0, 0xd035)},
  383. {USB_DEVICE(0x0af0, 0xd055)},
  384. {USB_DEVICE(0x0af0, 0xd155)},
  385. {USB_DEVICE(0x0af0, 0xd255)},
  386. {USB_DEVICE(0x0af0, 0xd057)},
  387. {USB_DEVICE(0x0af0, 0xd157)},
  388. {USB_DEVICE(0x0af0, 0xd257)},
  389. {USB_DEVICE(0x0af0, 0xd357)},
  390. {USB_DEVICE(0x0af0, 0xd058)},
  391. {USB_DEVICE(0x0af0, 0xc100)},
  392. {}
  393. };
  394. MODULE_DEVICE_TABLE(usb, hso_ids);
  395. /* Sysfs attribute */
  396. static ssize_t hsotype_show(struct device *dev,
  397. struct device_attribute *attr, char *buf)
  398. {
  399. struct hso_device *hso_dev = dev_get_drvdata(dev);
  400. char *port_name;
  401. if (!hso_dev)
  402. return 0;
  403. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  404. case HSO_PORT_CONTROL:
  405. port_name = "Control";
  406. break;
  407. case HSO_PORT_APP:
  408. port_name = "Application";
  409. break;
  410. case HSO_PORT_APP2:
  411. port_name = "Application2";
  412. break;
  413. case HSO_PORT_GPS:
  414. port_name = "GPS";
  415. break;
  416. case HSO_PORT_GPS_CONTROL:
  417. port_name = "GPS Control";
  418. break;
  419. case HSO_PORT_PCSC:
  420. port_name = "PCSC";
  421. break;
  422. case HSO_PORT_DIAG:
  423. port_name = "Diagnostic";
  424. break;
  425. case HSO_PORT_DIAG2:
  426. port_name = "Diagnostic2";
  427. break;
  428. case HSO_PORT_MODEM:
  429. port_name = "Modem";
  430. break;
  431. case HSO_PORT_NETWORK:
  432. port_name = "Network";
  433. break;
  434. default:
  435. port_name = "Unknown";
  436. break;
  437. }
  438. return sprintf(buf, "%s\n", port_name);
  439. }
  440. static DEVICE_ATTR_RO(hsotype);
  441. static struct attribute *hso_serial_dev_attrs[] = {
  442. &dev_attr_hsotype.attr,
  443. NULL
  444. };
  445. ATTRIBUTE_GROUPS(hso_serial_dev);
  446. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  447. {
  448. int idx;
  449. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  450. if (serial->rx_urb[idx] == urb)
  451. return idx;
  452. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  453. return -1;
  454. }
  455. /* converts mux value to a port spec value */
  456. static u32 hso_mux_to_port(int mux)
  457. {
  458. u32 result;
  459. switch (mux) {
  460. case 0x1:
  461. result = HSO_PORT_CONTROL;
  462. break;
  463. case 0x2:
  464. result = HSO_PORT_APP;
  465. break;
  466. case 0x4:
  467. result = HSO_PORT_PCSC;
  468. break;
  469. case 0x8:
  470. result = HSO_PORT_GPS;
  471. break;
  472. case 0x10:
  473. result = HSO_PORT_APP2;
  474. break;
  475. default:
  476. result = HSO_PORT_NO_PORT;
  477. }
  478. return result;
  479. }
  480. /* converts port spec value to a mux value */
  481. static u32 hso_port_to_mux(int port)
  482. {
  483. u32 result;
  484. switch (port & HSO_PORT_MASK) {
  485. case HSO_PORT_CONTROL:
  486. result = 0x0;
  487. break;
  488. case HSO_PORT_APP:
  489. result = 0x1;
  490. break;
  491. case HSO_PORT_PCSC:
  492. result = 0x2;
  493. break;
  494. case HSO_PORT_GPS:
  495. result = 0x3;
  496. break;
  497. case HSO_PORT_APP2:
  498. result = 0x4;
  499. break;
  500. default:
  501. result = 0x0;
  502. }
  503. return result;
  504. }
  505. static struct hso_serial *get_serial_by_shared_int_and_type(
  506. struct hso_shared_int *shared_int,
  507. int mux)
  508. {
  509. int i, port;
  510. port = hso_mux_to_port(mux);
  511. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  512. if (serial_table[i] &&
  513. (dev2ser(serial_table[i])->shared_int == shared_int) &&
  514. ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  515. return dev2ser(serial_table[i]);
  516. }
  517. }
  518. return NULL;
  519. }
  520. static struct hso_serial *get_serial_by_index(unsigned index)
  521. {
  522. struct hso_serial *serial = NULL;
  523. unsigned long flags;
  524. spin_lock_irqsave(&serial_table_lock, flags);
  525. if (serial_table[index])
  526. serial = dev2ser(serial_table[index]);
  527. spin_unlock_irqrestore(&serial_table_lock, flags);
  528. return serial;
  529. }
  530. static int obtain_minor(struct hso_serial *serial)
  531. {
  532. int index;
  533. unsigned long flags;
  534. spin_lock_irqsave(&serial_table_lock, flags);
  535. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  536. if (serial_table[index] == NULL) {
  537. serial_table[index] = serial->parent;
  538. serial->minor = index;
  539. spin_unlock_irqrestore(&serial_table_lock, flags);
  540. return 0;
  541. }
  542. }
  543. spin_unlock_irqrestore(&serial_table_lock, flags);
  544. pr_err("%s: no free serial devices in table\n", __func__);
  545. return -1;
  546. }
  547. static void release_minor(struct hso_serial *serial)
  548. {
  549. unsigned long flags;
  550. spin_lock_irqsave(&serial_table_lock, flags);
  551. serial_table[serial->minor] = NULL;
  552. spin_unlock_irqrestore(&serial_table_lock, flags);
  553. }
  554. static void handle_usb_error(int status, const char *function,
  555. struct hso_device *hso_dev)
  556. {
  557. char *explanation;
  558. switch (status) {
  559. case -ENODEV:
  560. explanation = "no device";
  561. break;
  562. case -ENOENT:
  563. explanation = "endpoint not enabled";
  564. break;
  565. case -EPIPE:
  566. explanation = "endpoint stalled";
  567. break;
  568. case -ENOSPC:
  569. explanation = "not enough bandwidth";
  570. break;
  571. case -ESHUTDOWN:
  572. explanation = "device disabled";
  573. break;
  574. case -EHOSTUNREACH:
  575. explanation = "device suspended";
  576. break;
  577. case -EINVAL:
  578. case -EAGAIN:
  579. case -EFBIG:
  580. case -EMSGSIZE:
  581. explanation = "internal error";
  582. break;
  583. case -EILSEQ:
  584. case -EPROTO:
  585. case -ETIME:
  586. case -ETIMEDOUT:
  587. explanation = "protocol error";
  588. if (hso_dev)
  589. usb_queue_reset_device(hso_dev->interface);
  590. break;
  591. default:
  592. explanation = "unknown status";
  593. break;
  594. }
  595. /* log a meaningful explanation of an USB status */
  596. hso_dbg(0x1, "%s: received USB status - %s (%d)\n",
  597. function, explanation, status);
  598. }
  599. /* Network interface functions */
  600. /* called when net interface is brought up by ifconfig */
  601. static int hso_net_open(struct net_device *net)
  602. {
  603. struct hso_net *odev = netdev_priv(net);
  604. unsigned long flags = 0;
  605. if (!odev) {
  606. dev_err(&net->dev, "No net device !\n");
  607. return -ENODEV;
  608. }
  609. odev->skb_tx_buf = NULL;
  610. /* setup environment */
  611. spin_lock_irqsave(&odev->net_lock, flags);
  612. odev->rx_parse_state = WAIT_IP;
  613. odev->rx_buf_size = 0;
  614. odev->rx_buf_missing = sizeof(struct iphdr);
  615. spin_unlock_irqrestore(&odev->net_lock, flags);
  616. /* We are up and running. */
  617. set_bit(HSO_NET_RUNNING, &odev->flags);
  618. hso_start_net_device(odev->parent);
  619. /* Tell the kernel we are ready to start receiving from it */
  620. netif_start_queue(net);
  621. return 0;
  622. }
  623. /* called when interface is brought down by ifconfig */
  624. static int hso_net_close(struct net_device *net)
  625. {
  626. struct hso_net *odev = netdev_priv(net);
  627. /* we don't need the queue anymore */
  628. netif_stop_queue(net);
  629. /* no longer running */
  630. clear_bit(HSO_NET_RUNNING, &odev->flags);
  631. hso_stop_net_device(odev->parent);
  632. /* done */
  633. return 0;
  634. }
  635. /* USB tells is xmit done, we should start the netqueue again */
  636. static void write_bulk_callback(struct urb *urb)
  637. {
  638. struct hso_net *odev = urb->context;
  639. int status = urb->status;
  640. /* Sanity check */
  641. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  642. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  643. return;
  644. }
  645. /* Do we still have a valid kernel network device? */
  646. if (!netif_device_present(odev->net)) {
  647. dev_err(&urb->dev->dev, "%s: net device not present\n",
  648. __func__);
  649. return;
  650. }
  651. /* log status, but don't act on it, we don't need to resubmit anything
  652. * anyhow */
  653. if (status)
  654. handle_usb_error(status, __func__, odev->parent);
  655. hso_put_activity(odev->parent);
  656. /* Tell the network interface we are ready for another frame */
  657. netif_wake_queue(odev->net);
  658. }
  659. /* called by kernel when we need to transmit a packet */
  660. static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
  661. struct net_device *net)
  662. {
  663. struct hso_net *odev = netdev_priv(net);
  664. int result;
  665. /* Tell the kernel, "No more frames 'til we are done with this one." */
  666. netif_stop_queue(net);
  667. if (hso_get_activity(odev->parent) == -EAGAIN) {
  668. odev->skb_tx_buf = skb;
  669. return NETDEV_TX_OK;
  670. }
  671. /* log if asked */
  672. DUMP1(skb->data, skb->len);
  673. /* Copy it from kernel memory to OUR memory */
  674. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  675. hso_dbg(0x1, "len: %d/%d\n", skb->len, MUX_BULK_TX_BUF_SIZE);
  676. /* Fill in the URB for shipping it out. */
  677. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  678. odev->parent->usb,
  679. usb_sndbulkpipe(odev->parent->usb,
  680. odev->out_endp->
  681. bEndpointAddress & 0x7F),
  682. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  683. odev);
  684. /* Deal with the Zero Length packet problem, I hope */
  685. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  686. /* Send the URB on its merry way. */
  687. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  688. if (result) {
  689. dev_warn(&odev->parent->interface->dev,
  690. "failed mux_bulk_tx_urb %d\n", result);
  691. net->stats.tx_errors++;
  692. netif_start_queue(net);
  693. } else {
  694. net->stats.tx_packets++;
  695. net->stats.tx_bytes += skb->len;
  696. }
  697. dev_kfree_skb(skb);
  698. /* we're done */
  699. return NETDEV_TX_OK;
  700. }
  701. static const struct ethtool_ops ops = {
  702. .get_link = ethtool_op_get_link
  703. };
  704. /* called when a packet did not ack after watchdogtimeout */
  705. static void hso_net_tx_timeout(struct net_device *net, unsigned int txqueue)
  706. {
  707. struct hso_net *odev = netdev_priv(net);
  708. if (!odev)
  709. return;
  710. /* Tell syslog we are hosed. */
  711. dev_warn(&net->dev, "Tx timed out.\n");
  712. /* Tear the waiting frame off the list */
  713. if (odev->mux_bulk_tx_urb)
  714. usb_unlink_urb(odev->mux_bulk_tx_urb);
  715. /* Update statistics */
  716. net->stats.tx_errors++;
  717. }
  718. /* make a real packet from the received USB buffer */
  719. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  720. unsigned int count, unsigned char is_eop)
  721. {
  722. unsigned short temp_bytes;
  723. unsigned short buffer_offset = 0;
  724. unsigned short frame_len;
  725. /* log if needed */
  726. hso_dbg(0x1, "Rx %d bytes\n", count);
  727. DUMP(ip_pkt, min(128, (int)count));
  728. while (count) {
  729. switch (odev->rx_parse_state) {
  730. case WAIT_IP:
  731. /* waiting for IP header. */
  732. /* wanted bytes - size of ip header */
  733. temp_bytes =
  734. (count <
  735. odev->rx_buf_missing) ? count : odev->
  736. rx_buf_missing;
  737. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  738. odev->rx_buf_size, ip_pkt + buffer_offset,
  739. temp_bytes);
  740. odev->rx_buf_size += temp_bytes;
  741. buffer_offset += temp_bytes;
  742. odev->rx_buf_missing -= temp_bytes;
  743. count -= temp_bytes;
  744. if (!odev->rx_buf_missing) {
  745. /* header is complete allocate an sk_buffer and
  746. * continue to WAIT_DATA */
  747. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  748. if ((frame_len > DEFAULT_MRU) ||
  749. (frame_len < sizeof(struct iphdr))) {
  750. dev_err(&odev->net->dev,
  751. "Invalid frame (%d) length\n",
  752. frame_len);
  753. odev->rx_parse_state = WAIT_SYNC;
  754. continue;
  755. }
  756. /* Allocate an sk_buff */
  757. odev->skb_rx_buf = netdev_alloc_skb(odev->net,
  758. frame_len);
  759. if (!odev->skb_rx_buf) {
  760. /* We got no receive buffer. */
  761. hso_dbg(0x1, "could not allocate memory\n");
  762. odev->rx_parse_state = WAIT_SYNC;
  763. continue;
  764. }
  765. /* Copy what we got so far. make room for iphdr
  766. * after tail. */
  767. skb_put_data(odev->skb_rx_buf,
  768. (char *)&(odev->rx_ip_hdr),
  769. sizeof(struct iphdr));
  770. /* ETH_HLEN */
  771. odev->rx_buf_size = sizeof(struct iphdr);
  772. /* Filip actually use .tot_len */
  773. odev->rx_buf_missing =
  774. frame_len - sizeof(struct iphdr);
  775. odev->rx_parse_state = WAIT_DATA;
  776. }
  777. break;
  778. case WAIT_DATA:
  779. temp_bytes = (count < odev->rx_buf_missing)
  780. ? count : odev->rx_buf_missing;
  781. /* Copy the rest of the bytes that are left in the
  782. * buffer into the waiting sk_buf. */
  783. /* Make room for temp_bytes after tail. */
  784. skb_put_data(odev->skb_rx_buf,
  785. ip_pkt + buffer_offset,
  786. temp_bytes);
  787. odev->rx_buf_missing -= temp_bytes;
  788. count -= temp_bytes;
  789. buffer_offset += temp_bytes;
  790. odev->rx_buf_size += temp_bytes;
  791. if (!odev->rx_buf_missing) {
  792. /* Packet is complete. Inject into stack. */
  793. /* We have IP packet here */
  794. odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
  795. skb_reset_mac_header(odev->skb_rx_buf);
  796. /* Ship it off to the kernel */
  797. netif_rx(odev->skb_rx_buf);
  798. /* No longer our buffer. */
  799. odev->skb_rx_buf = NULL;
  800. /* update out statistics */
  801. odev->net->stats.rx_packets++;
  802. odev->net->stats.rx_bytes += odev->rx_buf_size;
  803. odev->rx_buf_size = 0;
  804. odev->rx_buf_missing = sizeof(struct iphdr);
  805. odev->rx_parse_state = WAIT_IP;
  806. }
  807. break;
  808. case WAIT_SYNC:
  809. hso_dbg(0x1, " W_S\n");
  810. count = 0;
  811. break;
  812. default:
  813. hso_dbg(0x1, "\n");
  814. count--;
  815. break;
  816. }
  817. }
  818. /* Recovery mechanism for WAIT_SYNC state. */
  819. if (is_eop) {
  820. if (odev->rx_parse_state == WAIT_SYNC) {
  821. odev->rx_parse_state = WAIT_IP;
  822. odev->rx_buf_size = 0;
  823. odev->rx_buf_missing = sizeof(struct iphdr);
  824. }
  825. }
  826. }
  827. static void fix_crc_bug(struct urb *urb, __le16 max_packet_size)
  828. {
  829. static const u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  830. u32 rest = urb->actual_length % le16_to_cpu(max_packet_size);
  831. if (((rest == 5) || (rest == 6)) &&
  832. !memcmp(((u8 *)urb->transfer_buffer) + urb->actual_length - 4,
  833. crc_check, 4)) {
  834. urb->actual_length -= 4;
  835. }
  836. }
  837. /* Moving data from usb to kernel (in interrupt state) */
  838. static void read_bulk_callback(struct urb *urb)
  839. {
  840. struct hso_net *odev = urb->context;
  841. struct net_device *net;
  842. int result;
  843. unsigned long flags;
  844. int status = urb->status;
  845. /* is al ok? (Filip: Who's Al ?) */
  846. if (status) {
  847. handle_usb_error(status, __func__, odev->parent);
  848. return;
  849. }
  850. /* Sanity check */
  851. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  852. hso_dbg(0x1, "BULK IN callback but driver is not active!\n");
  853. return;
  854. }
  855. usb_mark_last_busy(urb->dev);
  856. net = odev->net;
  857. if (!netif_device_present(net)) {
  858. /* Somebody killed our network interface... */
  859. return;
  860. }
  861. if (odev->parent->port_spec & HSO_INFO_CRC_BUG)
  862. fix_crc_bug(urb, odev->in_endp->wMaxPacketSize);
  863. /* do we even have a packet? */
  864. if (urb->actual_length) {
  865. /* Handle the IP stream, add header and push it onto network
  866. * stack if the packet is complete. */
  867. spin_lock_irqsave(&odev->net_lock, flags);
  868. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  869. (urb->transfer_buffer_length >
  870. urb->actual_length) ? 1 : 0);
  871. spin_unlock_irqrestore(&odev->net_lock, flags);
  872. }
  873. /* We are done with this URB, resubmit it. Prep the USB to wait for
  874. * another frame. Reuse same as received. */
  875. usb_fill_bulk_urb(urb,
  876. odev->parent->usb,
  877. usb_rcvbulkpipe(odev->parent->usb,
  878. odev->in_endp->
  879. bEndpointAddress & 0x7F),
  880. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  881. read_bulk_callback, odev);
  882. /* Give this to the USB subsystem so it can tell us when more data
  883. * arrives. */
  884. result = usb_submit_urb(urb, GFP_ATOMIC);
  885. if (result)
  886. dev_warn(&odev->parent->interface->dev,
  887. "%s failed submit mux_bulk_rx_urb %d\n", __func__,
  888. result);
  889. }
  890. /* Serial driver functions */
  891. static void hso_init_termios(struct ktermios *termios)
  892. {
  893. /*
  894. * The default requirements for this device are:
  895. */
  896. termios->c_iflag &=
  897. ~(IGNBRK /* disable ignore break */
  898. | BRKINT /* disable break causes interrupt */
  899. | PARMRK /* disable mark parity errors */
  900. | ISTRIP /* disable clear high bit of input characters */
  901. | INLCR /* disable translate NL to CR */
  902. | IGNCR /* disable ignore CR */
  903. | ICRNL /* disable translate CR to NL */
  904. | IXON); /* disable enable XON/XOFF flow control */
  905. /* disable postprocess output characters */
  906. termios->c_oflag &= ~OPOST;
  907. termios->c_lflag &=
  908. ~(ECHO /* disable echo input characters */
  909. | ECHONL /* disable echo new line */
  910. | ICANON /* disable erase, kill, werase, and rprnt
  911. special characters */
  912. | ISIG /* disable interrupt, quit, and suspend special
  913. characters */
  914. | IEXTEN); /* disable non-POSIX special characters */
  915. termios->c_cflag &=
  916. ~(CSIZE /* no size */
  917. | PARENB /* disable parity bit */
  918. | CBAUD /* clear current baud rate */
  919. | CBAUDEX); /* clear current buad rate */
  920. termios->c_cflag |= CS8; /* character size 8 bits */
  921. /* baud rate 115200 */
  922. tty_termios_encode_baud_rate(termios, 115200, 115200);
  923. }
  924. static void _hso_serial_set_termios(struct tty_struct *tty)
  925. {
  926. struct hso_serial *serial = tty->driver_data;
  927. if (!serial) {
  928. pr_err("%s: no tty structures", __func__);
  929. return;
  930. }
  931. hso_dbg(0x8, "port %d\n", serial->minor);
  932. /*
  933. * Fix up unsupported bits
  934. */
  935. tty->termios.c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  936. tty->termios.c_cflag &=
  937. ~(CSIZE /* no size */
  938. | PARENB /* disable parity bit */
  939. | CBAUD /* clear current baud rate */
  940. | CBAUDEX); /* clear current buad rate */
  941. tty->termios.c_cflag |= CS8; /* character size 8 bits */
  942. /* baud rate 115200 */
  943. tty_encode_baud_rate(tty, 115200, 115200);
  944. }
  945. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  946. {
  947. int result;
  948. /* We are done with this URB, resubmit it. Prep the USB to wait for
  949. * another frame */
  950. usb_fill_bulk_urb(urb, serial->parent->usb,
  951. usb_rcvbulkpipe(serial->parent->usb,
  952. serial->in_endp->
  953. bEndpointAddress & 0x7F),
  954. urb->transfer_buffer, serial->rx_data_length,
  955. hso_std_serial_read_bulk_callback, serial);
  956. /* Give this to the USB subsystem so it can tell us when more data
  957. * arrives. */
  958. result = usb_submit_urb(urb, GFP_ATOMIC);
  959. if (result) {
  960. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  961. __func__, result);
  962. }
  963. }
  964. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  965. {
  966. int count;
  967. struct urb *curr_urb;
  968. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  969. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  970. count = put_rxbuf_data(curr_urb, serial);
  971. if (count == -1)
  972. return;
  973. if (count == 0) {
  974. serial->curr_rx_urb_idx++;
  975. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  976. serial->curr_rx_urb_idx = 0;
  977. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  978. }
  979. }
  980. }
  981. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  982. {
  983. int count = 0;
  984. struct urb *urb;
  985. urb = serial->rx_urb[0];
  986. if (serial->port.count > 0) {
  987. count = put_rxbuf_data(urb, serial);
  988. if (count == -1)
  989. return;
  990. }
  991. /* Re issue a read as long as we receive data. */
  992. if (count == 0 && ((urb->actual_length != 0) ||
  993. (serial->rx_state == RX_PENDING))) {
  994. serial->rx_state = RX_SENT;
  995. hso_mux_serial_read(serial);
  996. } else
  997. serial->rx_state = RX_IDLE;
  998. }
  999. /* read callback for Diag and CS port */
  1000. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1001. {
  1002. struct hso_serial *serial = urb->context;
  1003. int status = urb->status;
  1004. unsigned long flags;
  1005. hso_dbg(0x8, "--- Got serial_read_bulk callback %02x ---\n", status);
  1006. /* sanity check */
  1007. if (!serial) {
  1008. hso_dbg(0x1, "serial == NULL\n");
  1009. return;
  1010. }
  1011. if (status) {
  1012. handle_usb_error(status, __func__, serial->parent);
  1013. return;
  1014. }
  1015. hso_dbg(0x1, "Actual length = %d\n", urb->actual_length);
  1016. DUMP1(urb->transfer_buffer, urb->actual_length);
  1017. /* Anyone listening? */
  1018. if (serial->port.count == 0)
  1019. return;
  1020. if (serial->parent->port_spec & HSO_INFO_CRC_BUG)
  1021. fix_crc_bug(urb, serial->in_endp->wMaxPacketSize);
  1022. /* Valid data, handle RX data */
  1023. spin_lock_irqsave(&serial->serial_lock, flags);
  1024. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1025. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1026. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1027. }
  1028. /*
  1029. * This needs to be a tasklet otherwise we will
  1030. * end up recursively calling this function.
  1031. */
  1032. static void hso_unthrottle_tasklet(struct tasklet_struct *t)
  1033. {
  1034. struct hso_serial *serial = from_tasklet(serial, t,
  1035. unthrottle_tasklet);
  1036. unsigned long flags;
  1037. spin_lock_irqsave(&serial->serial_lock, flags);
  1038. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1039. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1040. else
  1041. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1042. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1043. }
  1044. static void hso_unthrottle(struct tty_struct *tty)
  1045. {
  1046. struct hso_serial *serial = tty->driver_data;
  1047. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1048. }
  1049. /* open the requested serial port */
  1050. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1051. {
  1052. struct hso_serial *serial = get_serial_by_index(tty->index);
  1053. int result;
  1054. /* sanity check */
  1055. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1056. WARN_ON(1);
  1057. tty->driver_data = NULL;
  1058. hso_dbg(0x1, "Failed to open port\n");
  1059. return -ENODEV;
  1060. }
  1061. mutex_lock(&serial->parent->mutex);
  1062. result = usb_autopm_get_interface(serial->parent->interface);
  1063. if (result < 0)
  1064. goto err_out;
  1065. hso_dbg(0x1, "Opening %d\n", serial->minor);
  1066. /* setup */
  1067. tty->driver_data = serial;
  1068. tty_port_tty_set(&serial->port, tty);
  1069. /* check for port already opened, if not set the termios */
  1070. serial->port.count++;
  1071. if (serial->port.count == 1) {
  1072. serial->rx_state = RX_IDLE;
  1073. /* Force default termio settings */
  1074. _hso_serial_set_termios(tty);
  1075. tasklet_setup(&serial->unthrottle_tasklet,
  1076. hso_unthrottle_tasklet);
  1077. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1078. if (result) {
  1079. hso_stop_serial_device(serial->parent);
  1080. serial->port.count--;
  1081. } else {
  1082. kref_get(&serial->parent->ref);
  1083. }
  1084. } else {
  1085. hso_dbg(0x1, "Port was already open\n");
  1086. }
  1087. usb_autopm_put_interface(serial->parent->interface);
  1088. /* done */
  1089. if (result)
  1090. hso_serial_tiocmset(tty, TIOCM_RTS | TIOCM_DTR, 0);
  1091. err_out:
  1092. mutex_unlock(&serial->parent->mutex);
  1093. return result;
  1094. }
  1095. /* close the requested serial port */
  1096. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1097. {
  1098. struct hso_serial *serial = tty->driver_data;
  1099. u8 usb_gone;
  1100. hso_dbg(0x1, "Closing serial port\n");
  1101. /* Open failed, no close cleanup required */
  1102. if (serial == NULL)
  1103. return;
  1104. mutex_lock(&serial->parent->mutex);
  1105. usb_gone = serial->parent->usb_gone;
  1106. if (!usb_gone)
  1107. usb_autopm_get_interface(serial->parent->interface);
  1108. /* reset the rts and dtr */
  1109. /* do the actual close */
  1110. serial->port.count--;
  1111. if (serial->port.count <= 0) {
  1112. serial->port.count = 0;
  1113. tty_port_tty_set(&serial->port, NULL);
  1114. if (!usb_gone)
  1115. hso_stop_serial_device(serial->parent);
  1116. tasklet_kill(&serial->unthrottle_tasklet);
  1117. }
  1118. if (!usb_gone)
  1119. usb_autopm_put_interface(serial->parent->interface);
  1120. mutex_unlock(&serial->parent->mutex);
  1121. }
  1122. /* close the requested serial port */
  1123. static ssize_t hso_serial_write(struct tty_struct *tty, const u8 *buf,
  1124. size_t count)
  1125. {
  1126. struct hso_serial *serial = tty->driver_data;
  1127. unsigned long flags;
  1128. /* sanity check */
  1129. if (serial == NULL) {
  1130. pr_err("%s: serial is NULL\n", __func__);
  1131. return -ENODEV;
  1132. }
  1133. spin_lock_irqsave(&serial->serial_lock, flags);
  1134. count = min_t(size_t, serial->tx_data_length - serial->tx_buffer_count,
  1135. count);
  1136. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, count);
  1137. serial->tx_buffer_count += count;
  1138. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1139. hso_kick_transmit(serial);
  1140. /* done */
  1141. return count;
  1142. }
  1143. /* how much room is there for writing */
  1144. static unsigned int hso_serial_write_room(struct tty_struct *tty)
  1145. {
  1146. struct hso_serial *serial = tty->driver_data;
  1147. unsigned int room;
  1148. unsigned long flags;
  1149. spin_lock_irqsave(&serial->serial_lock, flags);
  1150. room = serial->tx_data_length - serial->tx_buffer_count;
  1151. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1152. /* return free room */
  1153. return room;
  1154. }
  1155. static void hso_serial_cleanup(struct tty_struct *tty)
  1156. {
  1157. struct hso_serial *serial = tty->driver_data;
  1158. if (!serial)
  1159. return;
  1160. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1161. }
  1162. /* setup the term */
  1163. static void hso_serial_set_termios(struct tty_struct *tty,
  1164. const struct ktermios *old)
  1165. {
  1166. struct hso_serial *serial = tty->driver_data;
  1167. unsigned long flags;
  1168. if (old)
  1169. hso_dbg(0x16, "Termios called with: cflags new[%u] - old[%u]\n",
  1170. (unsigned int)tty->termios.c_cflag,
  1171. (unsigned int)old->c_cflag);
  1172. /* the actual setup */
  1173. spin_lock_irqsave(&serial->serial_lock, flags);
  1174. if (serial->port.count)
  1175. _hso_serial_set_termios(tty);
  1176. else
  1177. tty->termios = *old;
  1178. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1179. /* done */
  1180. }
  1181. /* how many characters in the buffer */
  1182. static unsigned int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1183. {
  1184. struct hso_serial *serial = tty->driver_data;
  1185. unsigned long flags;
  1186. unsigned int chars;
  1187. /* sanity check */
  1188. if (serial == NULL)
  1189. return 0;
  1190. spin_lock_irqsave(&serial->serial_lock, flags);
  1191. chars = serial->tx_buffer_count;
  1192. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1193. return chars;
  1194. }
  1195. static int tiocmget_submit_urb(struct hso_serial *serial,
  1196. struct hso_tiocmget *tiocmget,
  1197. struct usb_device *usb)
  1198. {
  1199. int result;
  1200. if (serial->parent->usb_gone)
  1201. return -ENODEV;
  1202. usb_fill_int_urb(tiocmget->urb, usb,
  1203. usb_rcvintpipe(usb,
  1204. tiocmget->endp->
  1205. bEndpointAddress & 0x7F),
  1206. tiocmget->serial_state_notification,
  1207. sizeof(struct hso_serial_state_notification),
  1208. tiocmget_intr_callback, serial,
  1209. tiocmget->endp->bInterval);
  1210. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1211. if (result) {
  1212. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1213. result);
  1214. }
  1215. return result;
  1216. }
  1217. static void tiocmget_intr_callback(struct urb *urb)
  1218. {
  1219. struct hso_serial *serial = urb->context;
  1220. struct hso_tiocmget *tiocmget;
  1221. int status = urb->status;
  1222. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1223. struct uart_icount *icount;
  1224. struct hso_serial_state_notification *serial_state_notification;
  1225. struct usb_device *usb;
  1226. struct usb_interface *interface;
  1227. int if_num;
  1228. /* Sanity checks */
  1229. if (!serial)
  1230. return;
  1231. if (status) {
  1232. handle_usb_error(status, __func__, serial->parent);
  1233. return;
  1234. }
  1235. /* tiocmget is only supported on HSO_PORT_MODEM */
  1236. tiocmget = serial->tiocmget;
  1237. if (!tiocmget)
  1238. return;
  1239. BUG_ON((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM);
  1240. usb = serial->parent->usb;
  1241. interface = serial->parent->interface;
  1242. if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  1243. /* wIndex should be the USB interface number of the port to which the
  1244. * notification applies, which should always be the Modem port.
  1245. */
  1246. serial_state_notification = tiocmget->serial_state_notification;
  1247. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1248. serial_state_notification->bNotification != B_NOTIFICATION ||
  1249. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1250. le16_to_cpu(serial_state_notification->wIndex) != if_num ||
  1251. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1252. dev_warn(&usb->dev,
  1253. "hso received invalid serial state notification\n");
  1254. DUMP(serial_state_notification,
  1255. sizeof(struct hso_serial_state_notification));
  1256. } else {
  1257. unsigned long flags;
  1258. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1259. UART_state_bitmap);
  1260. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1261. icount = &tiocmget->icount;
  1262. spin_lock_irqsave(&serial->serial_lock, flags);
  1263. if ((UART_state_bitmap & B_OVERRUN) !=
  1264. (prev_UART_state_bitmap & B_OVERRUN))
  1265. icount->parity++;
  1266. if ((UART_state_bitmap & B_PARITY) !=
  1267. (prev_UART_state_bitmap & B_PARITY))
  1268. icount->parity++;
  1269. if ((UART_state_bitmap & B_FRAMING) !=
  1270. (prev_UART_state_bitmap & B_FRAMING))
  1271. icount->frame++;
  1272. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1273. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1274. icount->rng++;
  1275. if ((UART_state_bitmap & B_BREAK) !=
  1276. (prev_UART_state_bitmap & B_BREAK))
  1277. icount->brk++;
  1278. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1279. (prev_UART_state_bitmap & B_TX_CARRIER))
  1280. icount->dsr++;
  1281. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1282. (prev_UART_state_bitmap & B_RX_CARRIER))
  1283. icount->dcd++;
  1284. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1285. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1286. tiocmget->intr_completed = 1;
  1287. wake_up_interruptible(&tiocmget->waitq);
  1288. }
  1289. memset(serial_state_notification, 0,
  1290. sizeof(struct hso_serial_state_notification));
  1291. tiocmget_submit_urb(serial,
  1292. tiocmget,
  1293. serial->parent->usb);
  1294. }
  1295. /*
  1296. * next few functions largely stolen from drivers/serial/serial_core.c
  1297. */
  1298. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1299. * - mask passed in arg for lines of interest
  1300. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1301. * Caller should use TIOCGICOUNT to see which one it was
  1302. */
  1303. static int
  1304. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1305. {
  1306. DECLARE_WAITQUEUE(wait, current);
  1307. struct uart_icount cprev, cnow;
  1308. struct hso_tiocmget *tiocmget;
  1309. int ret;
  1310. tiocmget = serial->tiocmget;
  1311. if (!tiocmget)
  1312. return -ENOENT;
  1313. /*
  1314. * note the counters on entry
  1315. */
  1316. spin_lock_irq(&serial->serial_lock);
  1317. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1318. spin_unlock_irq(&serial->serial_lock);
  1319. add_wait_queue(&tiocmget->waitq, &wait);
  1320. for (;;) {
  1321. spin_lock_irq(&serial->serial_lock);
  1322. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1323. spin_unlock_irq(&serial->serial_lock);
  1324. set_current_state(TASK_INTERRUPTIBLE);
  1325. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1326. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1327. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1328. ret = 0;
  1329. break;
  1330. }
  1331. schedule();
  1332. /* see if a signal did it */
  1333. if (signal_pending(current)) {
  1334. ret = -ERESTARTSYS;
  1335. break;
  1336. }
  1337. cprev = cnow;
  1338. }
  1339. __set_current_state(TASK_RUNNING);
  1340. remove_wait_queue(&tiocmget->waitq, &wait);
  1341. return ret;
  1342. }
  1343. /*
  1344. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1345. * Return: write counters to the user passed counter struct
  1346. * NB: both 1->0 and 0->1 transitions are counted except for
  1347. * RI where only 0->1 is counted.
  1348. */
  1349. static int hso_get_count(struct tty_struct *tty,
  1350. struct serial_icounter_struct *icount)
  1351. {
  1352. struct uart_icount cnow;
  1353. struct hso_serial *serial = tty->driver_data;
  1354. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1355. memset(icount, 0, sizeof(struct serial_icounter_struct));
  1356. if (!tiocmget)
  1357. return -ENOENT;
  1358. spin_lock_irq(&serial->serial_lock);
  1359. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1360. spin_unlock_irq(&serial->serial_lock);
  1361. icount->cts = cnow.cts;
  1362. icount->dsr = cnow.dsr;
  1363. icount->rng = cnow.rng;
  1364. icount->dcd = cnow.dcd;
  1365. icount->rx = cnow.rx;
  1366. icount->tx = cnow.tx;
  1367. icount->frame = cnow.frame;
  1368. icount->overrun = cnow.overrun;
  1369. icount->parity = cnow.parity;
  1370. icount->brk = cnow.brk;
  1371. icount->buf_overrun = cnow.buf_overrun;
  1372. return 0;
  1373. }
  1374. static int hso_serial_tiocmget(struct tty_struct *tty)
  1375. {
  1376. int retval;
  1377. struct hso_serial *serial = tty->driver_data;
  1378. struct hso_tiocmget *tiocmget;
  1379. u16 UART_state_bitmap;
  1380. /* sanity check */
  1381. if (!serial) {
  1382. hso_dbg(0x1, "no tty structures\n");
  1383. return -EINVAL;
  1384. }
  1385. spin_lock_irq(&serial->serial_lock);
  1386. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1387. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1388. tiocmget = serial->tiocmget;
  1389. if (tiocmget) {
  1390. UART_state_bitmap = le16_to_cpu(
  1391. tiocmget->prev_UART_state_bitmap);
  1392. if (UART_state_bitmap & B_RING_SIGNAL)
  1393. retval |= TIOCM_RNG;
  1394. if (UART_state_bitmap & B_RX_CARRIER)
  1395. retval |= TIOCM_CD;
  1396. if (UART_state_bitmap & B_TX_CARRIER)
  1397. retval |= TIOCM_DSR;
  1398. }
  1399. spin_unlock_irq(&serial->serial_lock);
  1400. return retval;
  1401. }
  1402. static int hso_serial_tiocmset(struct tty_struct *tty,
  1403. unsigned int set, unsigned int clear)
  1404. {
  1405. int val = 0;
  1406. unsigned long flags;
  1407. int if_num;
  1408. struct hso_serial *serial = tty->driver_data;
  1409. struct usb_interface *interface;
  1410. /* sanity check */
  1411. if (!serial) {
  1412. hso_dbg(0x1, "no tty structures\n");
  1413. return -EINVAL;
  1414. }
  1415. if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
  1416. return -EINVAL;
  1417. interface = serial->parent->interface;
  1418. if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  1419. spin_lock_irqsave(&serial->serial_lock, flags);
  1420. if (set & TIOCM_RTS)
  1421. serial->rts_state = 1;
  1422. if (set & TIOCM_DTR)
  1423. serial->dtr_state = 1;
  1424. if (clear & TIOCM_RTS)
  1425. serial->rts_state = 0;
  1426. if (clear & TIOCM_DTR)
  1427. serial->dtr_state = 0;
  1428. if (serial->dtr_state)
  1429. val |= 0x01;
  1430. if (serial->rts_state)
  1431. val |= 0x02;
  1432. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1433. return usb_control_msg(serial->parent->usb,
  1434. usb_sndctrlpipe(serial->parent->usb, 0), 0x22,
  1435. 0x21, val, if_num, NULL, 0,
  1436. USB_CTRL_SET_TIMEOUT);
  1437. }
  1438. static int hso_serial_ioctl(struct tty_struct *tty,
  1439. unsigned int cmd, unsigned long arg)
  1440. {
  1441. struct hso_serial *serial = tty->driver_data;
  1442. int ret = 0;
  1443. hso_dbg(0x8, "IOCTL cmd: %d, arg: %ld\n", cmd, arg);
  1444. if (!serial)
  1445. return -ENODEV;
  1446. switch (cmd) {
  1447. case TIOCMIWAIT:
  1448. ret = hso_wait_modem_status(serial, arg);
  1449. break;
  1450. default:
  1451. ret = -ENOIOCTLCMD;
  1452. break;
  1453. }
  1454. return ret;
  1455. }
  1456. /* starts a transmit */
  1457. static void hso_kick_transmit(struct hso_serial *serial)
  1458. {
  1459. unsigned long flags;
  1460. int res;
  1461. spin_lock_irqsave(&serial->serial_lock, flags);
  1462. if (!serial->tx_buffer_count)
  1463. goto out;
  1464. if (serial->tx_urb_used)
  1465. goto out;
  1466. /* Wakeup USB interface if necessary */
  1467. if (hso_get_activity(serial->parent) == -EAGAIN)
  1468. goto out;
  1469. /* Switch pointers around to avoid memcpy */
  1470. swap(serial->tx_buffer, serial->tx_data);
  1471. serial->tx_data_count = serial->tx_buffer_count;
  1472. serial->tx_buffer_count = 0;
  1473. /* If serial->tx_data is set, it means we switched buffers */
  1474. if (serial->tx_data && serial->write_data) {
  1475. res = serial->write_data(serial);
  1476. if (res >= 0)
  1477. serial->tx_urb_used = 1;
  1478. }
  1479. out:
  1480. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1481. }
  1482. /* make a request (for reading and writing data to muxed serial port) */
  1483. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1484. struct urb *ctrl_urb,
  1485. struct usb_ctrlrequest *ctrl_req,
  1486. u8 *ctrl_urb_data, u32 size)
  1487. {
  1488. int result;
  1489. int pipe;
  1490. /* Sanity check */
  1491. if (!serial || !ctrl_urb || !ctrl_req) {
  1492. pr_err("%s: Wrong arguments\n", __func__);
  1493. return -EINVAL;
  1494. }
  1495. /* initialize */
  1496. ctrl_req->wValue = 0;
  1497. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1498. ctrl_req->wLength = cpu_to_le16(size);
  1499. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1500. /* Reading command */
  1501. ctrl_req->bRequestType = USB_DIR_IN |
  1502. USB_TYPE_OPTION_VENDOR |
  1503. USB_RECIP_INTERFACE;
  1504. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1505. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1506. } else {
  1507. /* Writing command */
  1508. ctrl_req->bRequestType = USB_DIR_OUT |
  1509. USB_TYPE_OPTION_VENDOR |
  1510. USB_RECIP_INTERFACE;
  1511. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1512. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1513. }
  1514. /* syslog */
  1515. hso_dbg(0x2, "%s command (%02x) len: %d, port: %d\n",
  1516. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1517. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1518. /* Load ctrl urb */
  1519. ctrl_urb->transfer_flags = 0;
  1520. usb_fill_control_urb(ctrl_urb,
  1521. serial->parent->usb,
  1522. pipe,
  1523. (u8 *) ctrl_req,
  1524. ctrl_urb_data, size, ctrl_callback, serial);
  1525. /* Send it on merry way */
  1526. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1527. if (result) {
  1528. dev_err(&ctrl_urb->dev->dev,
  1529. "%s failed submit ctrl_urb %d type %d\n", __func__,
  1530. result, type);
  1531. return result;
  1532. }
  1533. /* done */
  1534. return size;
  1535. }
  1536. /* called by intr_callback when read occurs */
  1537. static int hso_mux_serial_read(struct hso_serial *serial)
  1538. {
  1539. if (!serial)
  1540. return -EINVAL;
  1541. /* clean data */
  1542. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1543. /* make the request */
  1544. if (serial->num_rx_urbs != 1) {
  1545. dev_err(&serial->parent->interface->dev,
  1546. "ERROR: mux'd reads with multiple buffers "
  1547. "not possible\n");
  1548. return 0;
  1549. }
  1550. return mux_device_request(serial,
  1551. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1552. serial->parent->port_spec & HSO_PORT_MASK,
  1553. serial->rx_urb[0],
  1554. &serial->ctrl_req_rx,
  1555. serial->rx_data[0], serial->rx_data_length);
  1556. }
  1557. /* used for muxed serial port callback (muxed serial read) */
  1558. static void intr_callback(struct urb *urb)
  1559. {
  1560. struct hso_shared_int *shared_int = urb->context;
  1561. struct hso_serial *serial;
  1562. unsigned char *port_req;
  1563. int status = urb->status;
  1564. unsigned long flags;
  1565. int i;
  1566. usb_mark_last_busy(urb->dev);
  1567. /* sanity check */
  1568. if (!shared_int)
  1569. return;
  1570. /* status check */
  1571. if (status) {
  1572. handle_usb_error(status, __func__, NULL);
  1573. return;
  1574. }
  1575. hso_dbg(0x8, "--- Got intr callback 0x%02X ---\n", status);
  1576. /* what request? */
  1577. port_req = urb->transfer_buffer;
  1578. hso_dbg(0x8, "port_req = 0x%.2X\n", *port_req);
  1579. /* loop over all muxed ports to find the one sending this */
  1580. for (i = 0; i < 8; i++) {
  1581. /* max 8 channels on MUX */
  1582. if (*port_req & (1 << i)) {
  1583. serial = get_serial_by_shared_int_and_type(shared_int,
  1584. (1 << i));
  1585. if (serial != NULL) {
  1586. hso_dbg(0x1, "Pending read interrupt on port %d\n",
  1587. i);
  1588. spin_lock_irqsave(&serial->serial_lock, flags);
  1589. if (serial->rx_state == RX_IDLE &&
  1590. serial->port.count > 0) {
  1591. /* Setup and send a ctrl req read on
  1592. * port i */
  1593. if (!serial->rx_urb_filled[0]) {
  1594. serial->rx_state = RX_SENT;
  1595. hso_mux_serial_read(serial);
  1596. } else
  1597. serial->rx_state = RX_PENDING;
  1598. } else {
  1599. hso_dbg(0x1, "Already a read pending on port %d or port not open\n",
  1600. i);
  1601. }
  1602. spin_unlock_irqrestore(&serial->serial_lock,
  1603. flags);
  1604. }
  1605. }
  1606. }
  1607. /* Resubmit interrupt urb */
  1608. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1609. }
  1610. /* called for writing to muxed serial port */
  1611. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1612. {
  1613. if (NULL == serial)
  1614. return -EINVAL;
  1615. return mux_device_request(serial,
  1616. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1617. serial->parent->port_spec & HSO_PORT_MASK,
  1618. serial->tx_urb,
  1619. &serial->ctrl_req_tx,
  1620. serial->tx_data, serial->tx_data_count);
  1621. }
  1622. /* write callback for Diag and CS port */
  1623. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1624. {
  1625. struct hso_serial *serial = urb->context;
  1626. int status = urb->status;
  1627. unsigned long flags;
  1628. /* sanity check */
  1629. if (!serial) {
  1630. hso_dbg(0x1, "serial == NULL\n");
  1631. return;
  1632. }
  1633. spin_lock_irqsave(&serial->serial_lock, flags);
  1634. serial->tx_urb_used = 0;
  1635. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1636. if (status) {
  1637. handle_usb_error(status, __func__, serial->parent);
  1638. return;
  1639. }
  1640. hso_put_activity(serial->parent);
  1641. tty_port_tty_wakeup(&serial->port);
  1642. hso_kick_transmit(serial);
  1643. hso_dbg(0x1, "\n");
  1644. }
  1645. /* called for writing diag or CS serial port */
  1646. static int hso_std_serial_write_data(struct hso_serial *serial)
  1647. {
  1648. int count = serial->tx_data_count;
  1649. int result;
  1650. usb_fill_bulk_urb(serial->tx_urb,
  1651. serial->parent->usb,
  1652. usb_sndbulkpipe(serial->parent->usb,
  1653. serial->out_endp->
  1654. bEndpointAddress & 0x7F),
  1655. serial->tx_data, serial->tx_data_count,
  1656. hso_std_serial_write_bulk_callback, serial);
  1657. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1658. if (result) {
  1659. dev_warn(&serial->parent->usb->dev,
  1660. "Failed to submit urb - res %d\n", result);
  1661. return result;
  1662. }
  1663. return count;
  1664. }
  1665. /* callback after read or write on muxed serial port */
  1666. static void ctrl_callback(struct urb *urb)
  1667. {
  1668. struct hso_serial *serial = urb->context;
  1669. struct usb_ctrlrequest *req;
  1670. int status = urb->status;
  1671. unsigned long flags;
  1672. /* sanity check */
  1673. if (!serial)
  1674. return;
  1675. spin_lock_irqsave(&serial->serial_lock, flags);
  1676. serial->tx_urb_used = 0;
  1677. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1678. if (status) {
  1679. handle_usb_error(status, __func__, serial->parent);
  1680. return;
  1681. }
  1682. /* what request? */
  1683. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1684. hso_dbg(0x8, "--- Got muxed ctrl callback 0x%02X ---\n", status);
  1685. hso_dbg(0x8, "Actual length of urb = %d\n", urb->actual_length);
  1686. DUMP1(urb->transfer_buffer, urb->actual_length);
  1687. if (req->bRequestType ==
  1688. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1689. /* response to a read command */
  1690. serial->rx_urb_filled[0] = 1;
  1691. spin_lock_irqsave(&serial->serial_lock, flags);
  1692. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1693. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1694. } else {
  1695. hso_put_activity(serial->parent);
  1696. tty_port_tty_wakeup(&serial->port);
  1697. /* response to a write command */
  1698. hso_kick_transmit(serial);
  1699. }
  1700. }
  1701. /* handle RX data for serial port */
  1702. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1703. {
  1704. struct tty_struct *tty;
  1705. int count;
  1706. /* Sanity check */
  1707. if (urb == NULL || serial == NULL) {
  1708. hso_dbg(0x1, "serial = NULL\n");
  1709. return -2;
  1710. }
  1711. tty = tty_port_tty_get(&serial->port);
  1712. if (tty && tty_throttled(tty)) {
  1713. tty_kref_put(tty);
  1714. return -1;
  1715. }
  1716. /* Push data to tty */
  1717. hso_dbg(0x1, "data to push to tty\n");
  1718. count = tty_buffer_request_room(&serial->port, urb->actual_length);
  1719. if (count >= urb->actual_length) {
  1720. tty_insert_flip_string(&serial->port, urb->transfer_buffer,
  1721. urb->actual_length);
  1722. tty_flip_buffer_push(&serial->port);
  1723. } else {
  1724. dev_warn(&serial->parent->usb->dev,
  1725. "dropping data, %d bytes lost\n", urb->actual_length);
  1726. }
  1727. tty_kref_put(tty);
  1728. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1729. return 0;
  1730. }
  1731. /* Base driver functions */
  1732. static void hso_log_port(struct hso_device *hso_dev)
  1733. {
  1734. char *port_type;
  1735. char port_dev[20];
  1736. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1737. case HSO_PORT_CONTROL:
  1738. port_type = "Control";
  1739. break;
  1740. case HSO_PORT_APP:
  1741. port_type = "Application";
  1742. break;
  1743. case HSO_PORT_GPS:
  1744. port_type = "GPS";
  1745. break;
  1746. case HSO_PORT_GPS_CONTROL:
  1747. port_type = "GPS control";
  1748. break;
  1749. case HSO_PORT_APP2:
  1750. port_type = "Application2";
  1751. break;
  1752. case HSO_PORT_PCSC:
  1753. port_type = "PCSC";
  1754. break;
  1755. case HSO_PORT_DIAG:
  1756. port_type = "Diagnostic";
  1757. break;
  1758. case HSO_PORT_DIAG2:
  1759. port_type = "Diagnostic2";
  1760. break;
  1761. case HSO_PORT_MODEM:
  1762. port_type = "Modem";
  1763. break;
  1764. case HSO_PORT_NETWORK:
  1765. port_type = "Network";
  1766. break;
  1767. default:
  1768. port_type = "Unknown";
  1769. break;
  1770. }
  1771. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1772. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1773. } else
  1774. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1775. dev2ser(hso_dev)->minor);
  1776. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1777. port_type, port_dev);
  1778. }
  1779. static int hso_start_net_device(struct hso_device *hso_dev)
  1780. {
  1781. int i, result = 0;
  1782. struct hso_net *hso_net = dev2net(hso_dev);
  1783. if (!hso_net)
  1784. return -ENODEV;
  1785. /* send URBs for all read buffers */
  1786. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1787. /* Prep a receive URB */
  1788. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1789. hso_dev->usb,
  1790. usb_rcvbulkpipe(hso_dev->usb,
  1791. hso_net->in_endp->
  1792. bEndpointAddress & 0x7F),
  1793. hso_net->mux_bulk_rx_buf_pool[i],
  1794. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1795. hso_net);
  1796. /* Put it out there so the device can send us stuff */
  1797. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1798. GFP_NOIO);
  1799. if (result)
  1800. dev_warn(&hso_dev->usb->dev,
  1801. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1802. i, result);
  1803. }
  1804. return result;
  1805. }
  1806. static int hso_stop_net_device(struct hso_device *hso_dev)
  1807. {
  1808. int i;
  1809. struct hso_net *hso_net = dev2net(hso_dev);
  1810. if (!hso_net)
  1811. return -ENODEV;
  1812. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1813. if (hso_net->mux_bulk_rx_urb_pool[i])
  1814. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1815. }
  1816. if (hso_net->mux_bulk_tx_urb)
  1817. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1818. return 0;
  1819. }
  1820. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1821. {
  1822. int i, result = 0;
  1823. struct hso_serial *serial = dev2ser(hso_dev);
  1824. if (!serial)
  1825. return -ENODEV;
  1826. /* If it is not the MUX port fill in and submit a bulk urb (already
  1827. * allocated in hso_serial_start) */
  1828. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1829. for (i = 0; i < serial->num_rx_urbs; i++) {
  1830. usb_fill_bulk_urb(serial->rx_urb[i],
  1831. serial->parent->usb,
  1832. usb_rcvbulkpipe(serial->parent->usb,
  1833. serial->in_endp->
  1834. bEndpointAddress &
  1835. 0x7F),
  1836. serial->rx_data[i],
  1837. serial->rx_data_length,
  1838. hso_std_serial_read_bulk_callback,
  1839. serial);
  1840. result = usb_submit_urb(serial->rx_urb[i], flags);
  1841. if (result) {
  1842. dev_warn(&serial->parent->usb->dev,
  1843. "Failed to submit urb - res %d\n",
  1844. result);
  1845. break;
  1846. }
  1847. }
  1848. } else {
  1849. mutex_lock(&serial->shared_int->shared_int_lock);
  1850. if (!serial->shared_int->use_count) {
  1851. result =
  1852. hso_mux_submit_intr_urb(serial->shared_int,
  1853. hso_dev->usb, flags);
  1854. }
  1855. serial->shared_int->use_count++;
  1856. mutex_unlock(&serial->shared_int->shared_int_lock);
  1857. }
  1858. if (serial->tiocmget)
  1859. tiocmget_submit_urb(serial,
  1860. serial->tiocmget,
  1861. serial->parent->usb);
  1862. return result;
  1863. }
  1864. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1865. {
  1866. int i;
  1867. struct hso_serial *serial = dev2ser(hso_dev);
  1868. struct hso_tiocmget *tiocmget;
  1869. if (!serial)
  1870. return -ENODEV;
  1871. for (i = 0; i < serial->num_rx_urbs; i++) {
  1872. if (serial->rx_urb[i]) {
  1873. usb_kill_urb(serial->rx_urb[i]);
  1874. serial->rx_urb_filled[i] = 0;
  1875. }
  1876. }
  1877. serial->curr_rx_urb_idx = 0;
  1878. if (serial->tx_urb)
  1879. usb_kill_urb(serial->tx_urb);
  1880. if (serial->shared_int) {
  1881. mutex_lock(&serial->shared_int->shared_int_lock);
  1882. if (serial->shared_int->use_count &&
  1883. (--serial->shared_int->use_count == 0)) {
  1884. struct urb *urb;
  1885. urb = serial->shared_int->shared_intr_urb;
  1886. if (urb)
  1887. usb_kill_urb(urb);
  1888. }
  1889. mutex_unlock(&serial->shared_int->shared_int_lock);
  1890. }
  1891. tiocmget = serial->tiocmget;
  1892. if (tiocmget) {
  1893. wake_up_interruptible(&tiocmget->waitq);
  1894. usb_kill_urb(tiocmget->urb);
  1895. }
  1896. return 0;
  1897. }
  1898. static void hso_serial_tty_unregister(struct hso_serial *serial)
  1899. {
  1900. tty_unregister_device(tty_drv, serial->minor);
  1901. release_minor(serial);
  1902. }
  1903. static void hso_serial_common_free(struct hso_serial *serial)
  1904. {
  1905. int i;
  1906. for (i = 0; i < serial->num_rx_urbs; i++) {
  1907. /* unlink and free RX URB */
  1908. usb_free_urb(serial->rx_urb[i]);
  1909. /* free the RX buffer */
  1910. kfree(serial->rx_data[i]);
  1911. }
  1912. /* unlink and free TX URB */
  1913. usb_free_urb(serial->tx_urb);
  1914. kfree(serial->tx_buffer);
  1915. kfree(serial->tx_data);
  1916. tty_port_destroy(&serial->port);
  1917. }
  1918. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1919. int rx_size, int tx_size)
  1920. {
  1921. int i;
  1922. tty_port_init(&serial->port);
  1923. if (obtain_minor(serial))
  1924. goto exit2;
  1925. /* register our minor number */
  1926. serial->parent->dev = tty_port_register_device_attr(&serial->port,
  1927. tty_drv, serial->minor, &serial->parent->interface->dev,
  1928. serial->parent, hso_serial_dev_groups);
  1929. if (IS_ERR(serial->parent->dev)) {
  1930. release_minor(serial);
  1931. goto exit2;
  1932. }
  1933. serial->magic = HSO_SERIAL_MAGIC;
  1934. spin_lock_init(&serial->serial_lock);
  1935. serial->num_rx_urbs = num_urbs;
  1936. /* RX, allocate urb and initialize */
  1937. /* prepare our RX buffer */
  1938. serial->rx_data_length = rx_size;
  1939. for (i = 0; i < serial->num_rx_urbs; i++) {
  1940. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  1941. if (!serial->rx_urb[i])
  1942. goto exit;
  1943. serial->rx_urb[i]->transfer_buffer = NULL;
  1944. serial->rx_urb[i]->transfer_buffer_length = 0;
  1945. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  1946. GFP_KERNEL);
  1947. if (!serial->rx_data[i])
  1948. goto exit;
  1949. }
  1950. /* TX, allocate urb and initialize */
  1951. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1952. if (!serial->tx_urb)
  1953. goto exit;
  1954. serial->tx_urb->transfer_buffer = NULL;
  1955. serial->tx_urb->transfer_buffer_length = 0;
  1956. /* prepare our TX buffer */
  1957. serial->tx_data_count = 0;
  1958. serial->tx_buffer_count = 0;
  1959. serial->tx_data_length = tx_size;
  1960. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1961. if (!serial->tx_data)
  1962. goto exit;
  1963. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  1964. if (!serial->tx_buffer)
  1965. goto exit;
  1966. return 0;
  1967. exit:
  1968. hso_serial_tty_unregister(serial);
  1969. exit2:
  1970. hso_serial_common_free(serial);
  1971. return -1;
  1972. }
  1973. /* Creates a general hso device */
  1974. static struct hso_device *hso_create_device(struct usb_interface *intf,
  1975. int port_spec)
  1976. {
  1977. struct hso_device *hso_dev;
  1978. hso_dev = kzalloc_obj(*hso_dev);
  1979. if (!hso_dev)
  1980. return NULL;
  1981. hso_dev->port_spec = port_spec;
  1982. hso_dev->usb = interface_to_usbdev(intf);
  1983. hso_dev->interface = intf;
  1984. kref_init(&hso_dev->ref);
  1985. mutex_init(&hso_dev->mutex);
  1986. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  1987. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  1988. return hso_dev;
  1989. }
  1990. /* Removes a network device in the network device table */
  1991. static int remove_net_device(struct hso_device *hso_dev)
  1992. {
  1993. int i;
  1994. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  1995. if (network_table[i] == hso_dev) {
  1996. network_table[i] = NULL;
  1997. break;
  1998. }
  1999. }
  2000. if (i == HSO_MAX_NET_DEVICES)
  2001. return -1;
  2002. return 0;
  2003. }
  2004. /* Frees our network device */
  2005. static void hso_free_net_device(struct hso_device *hso_dev)
  2006. {
  2007. int i;
  2008. struct hso_net *hso_net = dev2net(hso_dev);
  2009. if (!hso_net)
  2010. return;
  2011. remove_net_device(hso_net->parent);
  2012. if (hso_net->net)
  2013. unregister_netdev(hso_net->net);
  2014. /* start freeing */
  2015. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2016. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2017. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2018. hso_net->mux_bulk_rx_buf_pool[i] = NULL;
  2019. }
  2020. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2021. kfree(hso_net->mux_bulk_tx_buf);
  2022. hso_net->mux_bulk_tx_buf = NULL;
  2023. if (hso_net->net)
  2024. free_netdev(hso_net->net);
  2025. kfree(hso_dev);
  2026. }
  2027. static const struct net_device_ops hso_netdev_ops = {
  2028. .ndo_open = hso_net_open,
  2029. .ndo_stop = hso_net_close,
  2030. .ndo_start_xmit = hso_net_start_xmit,
  2031. .ndo_tx_timeout = hso_net_tx_timeout,
  2032. };
  2033. /* initialize the network interface */
  2034. static void hso_net_init(struct net_device *net)
  2035. {
  2036. struct hso_net *hso_net = netdev_priv(net);
  2037. hso_dbg(0x1, "sizeof hso_net is %zu\n", sizeof(*hso_net));
  2038. /* fill in the other fields */
  2039. net->netdev_ops = &hso_netdev_ops;
  2040. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2041. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2042. net->type = ARPHRD_NONE;
  2043. net->mtu = DEFAULT_MTU - 14;
  2044. net->tx_queue_len = 10;
  2045. net->ethtool_ops = &ops;
  2046. /* and initialize the semaphore */
  2047. spin_lock_init(&hso_net->net_lock);
  2048. }
  2049. /* Adds a network device in the network device table */
  2050. static int add_net_device(struct hso_device *hso_dev)
  2051. {
  2052. int i;
  2053. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2054. if (network_table[i] == NULL) {
  2055. network_table[i] = hso_dev;
  2056. break;
  2057. }
  2058. }
  2059. if (i == HSO_MAX_NET_DEVICES)
  2060. return -1;
  2061. return 0;
  2062. }
  2063. static int hso_rfkill_set_block(void *data, bool blocked)
  2064. {
  2065. struct hso_device *hso_dev = data;
  2066. int enabled = !blocked;
  2067. int rv;
  2068. mutex_lock(&hso_dev->mutex);
  2069. if (hso_dev->usb_gone)
  2070. rv = 0;
  2071. else
  2072. rv = usb_control_msg(hso_dev->usb, usb_sndctrlpipe(hso_dev->usb, 0),
  2073. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2074. USB_CTRL_SET_TIMEOUT);
  2075. mutex_unlock(&hso_dev->mutex);
  2076. return rv;
  2077. }
  2078. static const struct rfkill_ops hso_rfkill_ops = {
  2079. .set_block = hso_rfkill_set_block,
  2080. };
  2081. /* Creates and sets up everything for rfkill */
  2082. static void hso_create_rfkill(struct hso_device *hso_dev,
  2083. struct usb_interface *interface)
  2084. {
  2085. struct hso_net *hso_net = dev2net(hso_dev);
  2086. struct device *dev = &hso_net->net->dev;
  2087. static u32 rfkill_counter;
  2088. snprintf(hso_net->name, sizeof(hso_net->name), "hso-%d",
  2089. rfkill_counter++);
  2090. hso_net->rfkill = rfkill_alloc(hso_net->name,
  2091. &interface_to_usbdev(interface)->dev,
  2092. RFKILL_TYPE_WWAN,
  2093. &hso_rfkill_ops, hso_dev);
  2094. if (!hso_net->rfkill)
  2095. return;
  2096. if (rfkill_register(hso_net->rfkill) < 0) {
  2097. rfkill_destroy(hso_net->rfkill);
  2098. hso_net->rfkill = NULL;
  2099. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2100. return;
  2101. }
  2102. }
  2103. static const struct device_type hso_type = {
  2104. .name = "wwan",
  2105. };
  2106. /* Creates our network device */
  2107. static struct hso_device *hso_create_net_device(struct usb_interface *interface,
  2108. int port_spec)
  2109. {
  2110. int result, i;
  2111. struct net_device *net;
  2112. struct hso_net *hso_net;
  2113. struct hso_device *hso_dev;
  2114. hso_dev = hso_create_device(interface, port_spec);
  2115. if (!hso_dev)
  2116. return NULL;
  2117. /* allocate our network device, then we can put in our private data */
  2118. /* call hso_net_init to do the basic initialization */
  2119. net = alloc_netdev(sizeof(struct hso_net), "hso%d", NET_NAME_UNKNOWN,
  2120. hso_net_init);
  2121. if (!net) {
  2122. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2123. goto err_hso_dev;
  2124. }
  2125. hso_net = netdev_priv(net);
  2126. hso_dev->port_data.dev_net = hso_net;
  2127. hso_net->net = net;
  2128. hso_net->parent = hso_dev;
  2129. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2130. USB_DIR_IN);
  2131. if (!hso_net->in_endp) {
  2132. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2133. goto err_net;
  2134. }
  2135. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2136. USB_DIR_OUT);
  2137. if (!hso_net->out_endp) {
  2138. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2139. goto err_net;
  2140. }
  2141. SET_NETDEV_DEV(net, &interface->dev);
  2142. SET_NETDEV_DEVTYPE(net, &hso_type);
  2143. /* start allocating */
  2144. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2145. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2146. if (!hso_net->mux_bulk_rx_urb_pool[i])
  2147. goto err_mux_bulk_rx;
  2148. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2149. GFP_KERNEL);
  2150. if (!hso_net->mux_bulk_rx_buf_pool[i])
  2151. goto err_mux_bulk_rx;
  2152. }
  2153. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2154. if (!hso_net->mux_bulk_tx_urb)
  2155. goto err_mux_bulk_rx;
  2156. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2157. if (!hso_net->mux_bulk_tx_buf)
  2158. goto err_free_tx_urb;
  2159. result = add_net_device(hso_dev);
  2160. if (result) {
  2161. dev_err(&interface->dev, "Failed to add net device\n");
  2162. goto err_free_tx_buf;
  2163. }
  2164. /* registering our net device */
  2165. result = register_netdev(net);
  2166. if (result) {
  2167. dev_err(&interface->dev, "Failed to register device\n");
  2168. goto err_rmv_ndev;
  2169. }
  2170. hso_log_port(hso_dev);
  2171. hso_create_rfkill(hso_dev, interface);
  2172. return hso_dev;
  2173. err_rmv_ndev:
  2174. remove_net_device(hso_dev);
  2175. err_free_tx_buf:
  2176. kfree(hso_net->mux_bulk_tx_buf);
  2177. err_free_tx_urb:
  2178. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2179. err_mux_bulk_rx:
  2180. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2181. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2182. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2183. }
  2184. err_net:
  2185. free_netdev(net);
  2186. err_hso_dev:
  2187. kfree(hso_dev);
  2188. return NULL;
  2189. }
  2190. static void hso_free_tiomget(struct hso_serial *serial)
  2191. {
  2192. struct hso_tiocmget *tiocmget;
  2193. if (!serial)
  2194. return;
  2195. tiocmget = serial->tiocmget;
  2196. if (tiocmget) {
  2197. usb_free_urb(tiocmget->urb);
  2198. tiocmget->urb = NULL;
  2199. serial->tiocmget = NULL;
  2200. kfree(tiocmget->serial_state_notification);
  2201. tiocmget->serial_state_notification = NULL;
  2202. kfree(tiocmget);
  2203. }
  2204. }
  2205. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2206. static void hso_free_serial_device(struct hso_device *hso_dev)
  2207. {
  2208. struct hso_serial *serial = dev2ser(hso_dev);
  2209. if (!serial)
  2210. return;
  2211. hso_serial_common_free(serial);
  2212. if (serial->shared_int) {
  2213. mutex_lock(&serial->shared_int->shared_int_lock);
  2214. if (--serial->shared_int->ref_count == 0)
  2215. hso_free_shared_int(serial->shared_int);
  2216. else
  2217. mutex_unlock(&serial->shared_int->shared_int_lock);
  2218. }
  2219. hso_free_tiomget(serial);
  2220. kfree(serial);
  2221. kfree(hso_dev);
  2222. }
  2223. /* Creates a bulk AT channel */
  2224. static struct hso_device *hso_create_bulk_serial_device(
  2225. struct usb_interface *interface, int port)
  2226. {
  2227. struct hso_device *hso_dev;
  2228. struct hso_serial *serial;
  2229. int num_urbs;
  2230. struct hso_tiocmget *tiocmget;
  2231. hso_dev = hso_create_device(interface, port);
  2232. if (!hso_dev)
  2233. return NULL;
  2234. serial = kzalloc_obj(*serial);
  2235. if (!serial)
  2236. goto exit;
  2237. serial->parent = hso_dev;
  2238. hso_dev->port_data.dev_serial = serial;
  2239. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2240. num_urbs = 2;
  2241. serial->tiocmget = kzalloc_obj(struct hso_tiocmget);
  2242. if (!serial->tiocmget)
  2243. goto exit;
  2244. serial->tiocmget->serial_state_notification
  2245. = kzalloc_obj(struct hso_serial_state_notification);
  2246. if (!serial->tiocmget->serial_state_notification)
  2247. goto exit;
  2248. tiocmget = serial->tiocmget;
  2249. tiocmget->endp = hso_get_ep(interface,
  2250. USB_ENDPOINT_XFER_INT,
  2251. USB_DIR_IN);
  2252. if (!tiocmget->endp) {
  2253. dev_err(&interface->dev, "Failed to find INT IN ep\n");
  2254. goto exit;
  2255. }
  2256. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2257. if (!tiocmget->urb)
  2258. goto exit;
  2259. mutex_init(&tiocmget->mutex);
  2260. init_waitqueue_head(&tiocmget->waitq);
  2261. } else {
  2262. num_urbs = 1;
  2263. }
  2264. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2265. BULK_URB_TX_SIZE))
  2266. goto exit;
  2267. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2268. USB_DIR_IN);
  2269. if (!serial->in_endp) {
  2270. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2271. goto exit2;
  2272. }
  2273. if (!
  2274. (serial->out_endp =
  2275. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2276. dev_err(&interface->dev, "Failed to find BULK OUT ep\n");
  2277. goto exit2;
  2278. }
  2279. serial->write_data = hso_std_serial_write_data;
  2280. /* setup the proc dirs and files if needed */
  2281. hso_log_port(hso_dev);
  2282. /* done, return it */
  2283. return hso_dev;
  2284. exit2:
  2285. hso_serial_tty_unregister(serial);
  2286. hso_serial_common_free(serial);
  2287. exit:
  2288. hso_free_tiomget(serial);
  2289. kfree(serial);
  2290. kfree(hso_dev);
  2291. return NULL;
  2292. }
  2293. /* Creates a multiplexed AT channel */
  2294. static
  2295. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2296. int port,
  2297. struct hso_shared_int *mux)
  2298. {
  2299. struct hso_device *hso_dev;
  2300. struct hso_serial *serial;
  2301. int port_spec;
  2302. port_spec = HSO_INTF_MUX;
  2303. port_spec &= ~HSO_PORT_MASK;
  2304. port_spec |= hso_mux_to_port(port);
  2305. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2306. return NULL;
  2307. hso_dev = hso_create_device(interface, port_spec);
  2308. if (!hso_dev)
  2309. return NULL;
  2310. serial = kzalloc_obj(*serial);
  2311. if (!serial)
  2312. goto err_free_dev;
  2313. hso_dev->port_data.dev_serial = serial;
  2314. serial->parent = hso_dev;
  2315. if (hso_serial_common_create
  2316. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2317. goto err_free_serial;
  2318. serial->tx_data_length--;
  2319. serial->write_data = hso_mux_serial_write_data;
  2320. serial->shared_int = mux;
  2321. mutex_lock(&serial->shared_int->shared_int_lock);
  2322. serial->shared_int->ref_count++;
  2323. mutex_unlock(&serial->shared_int->shared_int_lock);
  2324. /* setup the proc dirs and files if needed */
  2325. hso_log_port(hso_dev);
  2326. /* done, return it */
  2327. return hso_dev;
  2328. err_free_serial:
  2329. kfree(serial);
  2330. err_free_dev:
  2331. kfree(hso_dev);
  2332. return NULL;
  2333. }
  2334. static void hso_free_shared_int(struct hso_shared_int *mux)
  2335. {
  2336. usb_free_urb(mux->shared_intr_urb);
  2337. kfree(mux->shared_intr_buf);
  2338. mutex_unlock(&mux->shared_int_lock);
  2339. kfree(mux);
  2340. }
  2341. static
  2342. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2343. {
  2344. struct hso_shared_int *mux = kzalloc_obj(*mux);
  2345. if (!mux)
  2346. return NULL;
  2347. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2348. USB_DIR_IN);
  2349. if (!mux->intr_endp) {
  2350. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2351. goto exit;
  2352. }
  2353. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2354. if (!mux->shared_intr_urb)
  2355. goto exit;
  2356. mux->shared_intr_buf =
  2357. kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
  2358. GFP_KERNEL);
  2359. if (!mux->shared_intr_buf)
  2360. goto exit;
  2361. mutex_init(&mux->shared_int_lock);
  2362. return mux;
  2363. exit:
  2364. kfree(mux->shared_intr_buf);
  2365. usb_free_urb(mux->shared_intr_urb);
  2366. kfree(mux);
  2367. return NULL;
  2368. }
  2369. /* Gets the port spec for a certain interface */
  2370. static int hso_get_config_data(struct usb_interface *interface)
  2371. {
  2372. struct usb_device *usbdev = interface_to_usbdev(interface);
  2373. u8 *config_data = kmalloc(17, GFP_KERNEL);
  2374. u32 if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  2375. s32 result;
  2376. if (!config_data)
  2377. return -ENOMEM;
  2378. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2379. 0x86, 0xC0, 0, 0, config_data, 17,
  2380. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2381. kfree(config_data);
  2382. return -EIO;
  2383. }
  2384. /* check if we have a valid interface */
  2385. if (if_num > 16) {
  2386. kfree(config_data);
  2387. return -EINVAL;
  2388. }
  2389. switch (config_data[if_num]) {
  2390. case 0x0:
  2391. result = 0;
  2392. break;
  2393. case 0x1:
  2394. result = HSO_PORT_DIAG;
  2395. break;
  2396. case 0x2:
  2397. result = HSO_PORT_GPS;
  2398. break;
  2399. case 0x3:
  2400. result = HSO_PORT_GPS_CONTROL;
  2401. break;
  2402. case 0x4:
  2403. result = HSO_PORT_APP;
  2404. break;
  2405. case 0x5:
  2406. result = HSO_PORT_APP2;
  2407. break;
  2408. case 0x6:
  2409. result = HSO_PORT_CONTROL;
  2410. break;
  2411. case 0x7:
  2412. result = HSO_PORT_NETWORK;
  2413. break;
  2414. case 0x8:
  2415. result = HSO_PORT_MODEM;
  2416. break;
  2417. case 0x9:
  2418. result = HSO_PORT_MSD;
  2419. break;
  2420. case 0xa:
  2421. result = HSO_PORT_PCSC;
  2422. break;
  2423. case 0xb:
  2424. result = HSO_PORT_VOICE;
  2425. break;
  2426. default:
  2427. result = 0;
  2428. }
  2429. if (result)
  2430. result |= HSO_INTF_BULK;
  2431. if (config_data[16] & 0x1)
  2432. result |= HSO_INFO_CRC_BUG;
  2433. kfree(config_data);
  2434. return result;
  2435. }
  2436. /* called once for each interface upon device insertion */
  2437. static int hso_probe(struct usb_interface *interface,
  2438. const struct usb_device_id *id)
  2439. {
  2440. int mux, i, if_num, port_spec;
  2441. unsigned char port_mask;
  2442. struct hso_device *hso_dev = NULL;
  2443. struct hso_shared_int *shared_int;
  2444. struct hso_device *tmp_dev = NULL;
  2445. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2446. dev_err(&interface->dev, "Not our interface\n");
  2447. return -ENODEV;
  2448. }
  2449. if_num = interface->cur_altsetting->desc.bInterfaceNumber;
  2450. /* Get the interface/port specification from either driver_info or from
  2451. * the device itself */
  2452. if (id->driver_info) {
  2453. /* if_num is controlled by the device, driver_info is a 0 terminated
  2454. * array. Make sure, the access is in bounds! */
  2455. for (i = 0; i <= if_num; ++i)
  2456. if (((u32 *)(id->driver_info))[i] == 0)
  2457. goto exit;
  2458. port_spec = ((u32 *)(id->driver_info))[if_num];
  2459. } else {
  2460. port_spec = hso_get_config_data(interface);
  2461. if (port_spec < 0)
  2462. goto exit;
  2463. }
  2464. /* Check if we need to switch to alt interfaces prior to port
  2465. * configuration */
  2466. if (interface->num_altsetting > 1)
  2467. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2468. interface->needs_remote_wakeup = 1;
  2469. /* Allocate new hso device(s) */
  2470. switch (port_spec & HSO_INTF_MASK) {
  2471. case HSO_INTF_MUX:
  2472. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2473. /* Create the network device */
  2474. if (!disable_net) {
  2475. hso_dev = hso_create_net_device(interface,
  2476. port_spec);
  2477. if (!hso_dev)
  2478. goto exit;
  2479. tmp_dev = hso_dev;
  2480. }
  2481. }
  2482. if (hso_get_mux_ports(interface, &port_mask))
  2483. /* TODO: de-allocate everything */
  2484. goto exit;
  2485. shared_int = hso_create_shared_int(interface);
  2486. if (!shared_int)
  2487. goto exit;
  2488. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2489. if (port_mask & i) {
  2490. hso_dev = hso_create_mux_serial_device(
  2491. interface, i, shared_int);
  2492. if (!hso_dev)
  2493. goto exit;
  2494. }
  2495. }
  2496. if (tmp_dev)
  2497. hso_dev = tmp_dev;
  2498. break;
  2499. case HSO_INTF_BULK:
  2500. /* It's a regular bulk interface */
  2501. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2502. if (!disable_net)
  2503. hso_dev =
  2504. hso_create_net_device(interface, port_spec);
  2505. } else {
  2506. hso_dev =
  2507. hso_create_bulk_serial_device(interface, port_spec);
  2508. }
  2509. if (!hso_dev)
  2510. goto exit;
  2511. break;
  2512. default:
  2513. goto exit;
  2514. }
  2515. /* save our data pointer in this device */
  2516. usb_set_intfdata(interface, hso_dev);
  2517. /* done */
  2518. return 0;
  2519. exit:
  2520. hso_free_interface(interface);
  2521. return -ENODEV;
  2522. }
  2523. /* device removed, cleaning up */
  2524. static void hso_disconnect(struct usb_interface *interface)
  2525. {
  2526. hso_free_interface(interface);
  2527. /* remove reference of our private data */
  2528. usb_set_intfdata(interface, NULL);
  2529. }
  2530. static void async_get_intf(struct work_struct *data)
  2531. {
  2532. struct hso_device *hso_dev =
  2533. container_of(data, struct hso_device, async_get_intf);
  2534. usb_autopm_get_interface(hso_dev->interface);
  2535. }
  2536. static void async_put_intf(struct work_struct *data)
  2537. {
  2538. struct hso_device *hso_dev =
  2539. container_of(data, struct hso_device, async_put_intf);
  2540. usb_autopm_put_interface(hso_dev->interface);
  2541. }
  2542. static int hso_get_activity(struct hso_device *hso_dev)
  2543. {
  2544. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2545. if (!hso_dev->is_active) {
  2546. hso_dev->is_active = 1;
  2547. schedule_work(&hso_dev->async_get_intf);
  2548. }
  2549. }
  2550. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2551. return -EAGAIN;
  2552. usb_mark_last_busy(hso_dev->usb);
  2553. return 0;
  2554. }
  2555. static int hso_put_activity(struct hso_device *hso_dev)
  2556. {
  2557. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2558. if (hso_dev->is_active) {
  2559. hso_dev->is_active = 0;
  2560. schedule_work(&hso_dev->async_put_intf);
  2561. return -EAGAIN;
  2562. }
  2563. }
  2564. hso_dev->is_active = 0;
  2565. return 0;
  2566. }
  2567. /* called by kernel when we need to suspend device */
  2568. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2569. {
  2570. int i, result;
  2571. /* Stop all serial ports */
  2572. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2573. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2574. result = hso_stop_serial_device(serial_table[i]);
  2575. if (result)
  2576. goto out;
  2577. }
  2578. }
  2579. /* Stop all network ports */
  2580. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2581. if (network_table[i] &&
  2582. (network_table[i]->interface == iface)) {
  2583. result = hso_stop_net_device(network_table[i]);
  2584. if (result)
  2585. goto out;
  2586. }
  2587. }
  2588. out:
  2589. return 0;
  2590. }
  2591. /* called by kernel when we need to resume device */
  2592. static int hso_resume(struct usb_interface *iface)
  2593. {
  2594. int i, result = 0;
  2595. struct hso_net *hso_net;
  2596. /* Start all serial ports */
  2597. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2598. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2599. if (dev2ser(serial_table[i])->port.count) {
  2600. result =
  2601. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2602. hso_kick_transmit(dev2ser(serial_table[i]));
  2603. if (result)
  2604. goto out;
  2605. }
  2606. }
  2607. }
  2608. /* Start all network ports */
  2609. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2610. if (network_table[i] &&
  2611. (network_table[i]->interface == iface)) {
  2612. hso_net = dev2net(network_table[i]);
  2613. if (hso_net->flags & IFF_UP) {
  2614. /* First transmit any lingering data,
  2615. then restart the device. */
  2616. if (hso_net->skb_tx_buf) {
  2617. dev_dbg(&iface->dev,
  2618. "Transmitting"
  2619. " lingering data\n");
  2620. hso_net_start_xmit(hso_net->skb_tx_buf,
  2621. hso_net->net);
  2622. hso_net->skb_tx_buf = NULL;
  2623. }
  2624. result = hso_start_net_device(network_table[i]);
  2625. if (result)
  2626. goto out;
  2627. }
  2628. }
  2629. }
  2630. out:
  2631. return result;
  2632. }
  2633. static void hso_serial_ref_free(struct kref *ref)
  2634. {
  2635. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2636. hso_free_serial_device(hso_dev);
  2637. }
  2638. static void hso_free_interface(struct usb_interface *interface)
  2639. {
  2640. struct hso_serial *serial;
  2641. int i;
  2642. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2643. if (serial_table[i] &&
  2644. (serial_table[i]->interface == interface)) {
  2645. serial = dev2ser(serial_table[i]);
  2646. tty_port_tty_hangup(&serial->port, false);
  2647. mutex_lock(&serial->parent->mutex);
  2648. serial->parent->usb_gone = 1;
  2649. mutex_unlock(&serial->parent->mutex);
  2650. cancel_work_sync(&serial_table[i]->async_put_intf);
  2651. cancel_work_sync(&serial_table[i]->async_get_intf);
  2652. hso_serial_tty_unregister(serial);
  2653. kref_put(&serial->parent->ref, hso_serial_ref_free);
  2654. }
  2655. }
  2656. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2657. if (network_table[i] &&
  2658. (network_table[i]->interface == interface)) {
  2659. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2660. /* hso_stop_net_device doesn't stop the net queue since
  2661. * traffic needs to start it again when suspended */
  2662. netif_stop_queue(dev2net(network_table[i])->net);
  2663. hso_stop_net_device(network_table[i]);
  2664. cancel_work_sync(&network_table[i]->async_put_intf);
  2665. cancel_work_sync(&network_table[i]->async_get_intf);
  2666. if (rfk) {
  2667. rfkill_unregister(rfk);
  2668. rfkill_destroy(rfk);
  2669. }
  2670. hso_free_net_device(network_table[i]);
  2671. }
  2672. }
  2673. }
  2674. /* Helper functions */
  2675. /* Get the endpoint ! */
  2676. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2677. int type, int dir)
  2678. {
  2679. int i;
  2680. struct usb_host_interface *iface = intf->cur_altsetting;
  2681. struct usb_endpoint_descriptor *endp;
  2682. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2683. endp = &iface->endpoint[i].desc;
  2684. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2685. (usb_endpoint_type(endp) == type))
  2686. return endp;
  2687. }
  2688. return NULL;
  2689. }
  2690. /* Get the byte that describes which ports are enabled */
  2691. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2692. {
  2693. int i;
  2694. struct usb_host_interface *iface = intf->cur_altsetting;
  2695. if (iface->extralen == 3) {
  2696. *ports = iface->extra[2];
  2697. return 0;
  2698. }
  2699. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2700. if (iface->endpoint[i].extralen == 3) {
  2701. *ports = iface->endpoint[i].extra[2];
  2702. return 0;
  2703. }
  2704. }
  2705. return -1;
  2706. }
  2707. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2708. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2709. struct usb_device *usb, gfp_t gfp)
  2710. {
  2711. int result;
  2712. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2713. usb_rcvintpipe(usb,
  2714. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2715. shared_int->shared_intr_buf,
  2716. 1,
  2717. intr_callback, shared_int,
  2718. shared_int->intr_endp->bInterval);
  2719. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2720. if (result)
  2721. dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
  2722. result);
  2723. return result;
  2724. }
  2725. /* operations setup of the serial interface */
  2726. static const struct tty_operations hso_serial_ops = {
  2727. .open = hso_serial_open,
  2728. .close = hso_serial_close,
  2729. .write = hso_serial_write,
  2730. .write_room = hso_serial_write_room,
  2731. .cleanup = hso_serial_cleanup,
  2732. .ioctl = hso_serial_ioctl,
  2733. .set_termios = hso_serial_set_termios,
  2734. .chars_in_buffer = hso_serial_chars_in_buffer,
  2735. .tiocmget = hso_serial_tiocmget,
  2736. .tiocmset = hso_serial_tiocmset,
  2737. .get_icount = hso_get_count,
  2738. .unthrottle = hso_unthrottle
  2739. };
  2740. static struct usb_driver hso_driver = {
  2741. .name = driver_name,
  2742. .probe = hso_probe,
  2743. .disconnect = hso_disconnect,
  2744. .id_table = hso_ids,
  2745. .suspend = hso_suspend,
  2746. .resume = hso_resume,
  2747. .reset_resume = hso_resume,
  2748. .supports_autosuspend = 1,
  2749. .disable_hub_initiated_lpm = 1,
  2750. };
  2751. static int __init hso_init(void)
  2752. {
  2753. int result;
  2754. /* allocate our driver using the proper amount of supported minors */
  2755. tty_drv = tty_alloc_driver(HSO_SERIAL_TTY_MINORS, TTY_DRIVER_REAL_RAW |
  2756. TTY_DRIVER_DYNAMIC_DEV);
  2757. if (IS_ERR(tty_drv))
  2758. return PTR_ERR(tty_drv);
  2759. /* fill in all needed values */
  2760. tty_drv->driver_name = driver_name;
  2761. tty_drv->name = tty_filename;
  2762. /* if major number is provided as parameter, use that one */
  2763. if (tty_major)
  2764. tty_drv->major = tty_major;
  2765. tty_drv->minor_start = 0;
  2766. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2767. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2768. tty_drv->init_termios = tty_std_termios;
  2769. hso_init_termios(&tty_drv->init_termios);
  2770. tty_set_operations(tty_drv, &hso_serial_ops);
  2771. /* register the tty driver */
  2772. result = tty_register_driver(tty_drv);
  2773. if (result) {
  2774. pr_err("%s - tty_register_driver failed(%d)\n",
  2775. __func__, result);
  2776. goto err_free_tty;
  2777. }
  2778. /* register this module as an usb driver */
  2779. result = usb_register(&hso_driver);
  2780. if (result) {
  2781. pr_err("Could not register hso driver - error: %d\n", result);
  2782. goto err_unreg_tty;
  2783. }
  2784. /* done */
  2785. return 0;
  2786. err_unreg_tty:
  2787. tty_unregister_driver(tty_drv);
  2788. err_free_tty:
  2789. tty_driver_kref_put(tty_drv);
  2790. return result;
  2791. }
  2792. static void __exit hso_exit(void)
  2793. {
  2794. tty_unregister_driver(tty_drv);
  2795. /* deregister the usb driver */
  2796. usb_deregister(&hso_driver);
  2797. tty_driver_kref_put(tty_drv);
  2798. }
  2799. /* Module definitions */
  2800. module_init(hso_init);
  2801. module_exit(hso_exit);
  2802. MODULE_AUTHOR(MOD_AUTHOR);
  2803. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2804. MODULE_LICENSE("GPL");
  2805. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2806. MODULE_PARM_DESC(debug, "debug level mask [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2807. module_param(debug, int, 0644);
  2808. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2809. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2810. module_param(tty_major, int, 0644);
  2811. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2812. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2813. module_param(disable_net, int, 0644);