hso.c 83 KB

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