hso.c 83 KB

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